Show / Hide Table of Contents

Class GraphicsContext

Represents and provides methods to manipulate an OpenGL render context.

Inheritance
Object
GraphicsContext
Implements
IGraphicsContext
IDisposable
IGraphicsContextInternal
Inherited Members
Object.Equals(Object, Object)
Object.ReferenceEquals(Object, Object)
Object.GetType()
Object.MemberwiseClone()
Namespace: OpenTK.Graphics
Assembly: OpenTK.dll
Syntax
public sealed class GraphicsContext : IGraphicsContext, IDisposable, IGraphicsContextInternal

Constructors

GraphicsContext(ContextHandle, GraphicsContext.GetAddressDelegate, GraphicsContext.GetCurrentContextDelegate)

Initializes a new instance of the GraphicsContext class using an external context handle that was created by a third-party library.

Declaration
public GraphicsContext(ContextHandle handle, GraphicsContext.GetAddressDelegate getAddress, GraphicsContext.GetCurrentContextDelegate getCurrent)
Parameters
Type Name Description
ContextHandle handle

A valid, unique handle for an external OpenGL context, or ContextHandle.Zero to use the current context. It is an error to specify a handle that has been created through OpenTK or that has been passed to OpenTK before.

GraphicsContext.GetAddressDelegate getAddress

A GetAddressDelegate instance that accepts the name of an OpenGL function and returns a valid function pointer, or IntPtr.Zero if that function is not supported. This delegate should be implemented using the same toolkit that created the OpenGL context (i.e. if the context was created with SDL_GL_CreateContext(), then this delegate should use SDL_GL_GetProcAddress() to retrieve function pointers.)

GraphicsContext.GetCurrentContextDelegate getCurrent

A GetCurrentContextDelegate instance that returns the handle of the current OpenGL context, or IntPtr.Zero if no context is current on the calling thread. This delegate should be implemented using the same toolkit that created the OpenGL context (i.e. if the context was created with SDL_GL_CreateContext(), then this delegate should use SDL_GL_GetCurrentContext() to retrieve the current context.)

GraphicsContext(ContextHandle, IWindowInfo)

Constructs a new GraphicsContext from a pre-existing context created outside of OpenTK. A dummy context will be created if both the handle and the window are null.

Declaration
public GraphicsContext(ContextHandle handle, IWindowInfo window)
Parameters
Type Name Description
ContextHandle handle

The handle of the existing context. This must be a valid, unique handle that is not known to OpenTK.

IWindowInfo window

This parameter is reserved.

GraphicsContext(ContextHandle, IWindowInfo, IGraphicsContext, Int32, Int32, GraphicsContextFlags)

Constructs a new GraphicsContext from a pre-existing context created outside of OpenTK.

Declaration
public GraphicsContext(ContextHandle handle, IWindowInfo window, IGraphicsContext shareContext, int major, int minor, GraphicsContextFlags flags)
Parameters
Type Name Description
ContextHandle handle

The handle of the existing context. This must be a valid, unique handle that is not known to OpenTK.

IWindowInfo window

This parameter is reserved.

IGraphicsContext shareContext

This parameter is reserved.

Int32 major

This parameter is reserved.

Int32 minor

This parameter is reserved.

GraphicsContextFlags flags

This parameter is reserved..

GraphicsContext(GraphicsMode, IWindowInfo)

Constructs a new GraphicsContext with the specified GraphicsMode and attaches it to the specified window.

Declaration
public GraphicsContext(GraphicsMode mode, IWindowInfo window)
Parameters
Type Name Description
GraphicsMode mode

The OpenTK.Graphics.GraphicsMode of the GraphicsContext.

IWindowInfo window

The OpenTK.Platform.IWindowInfo to attach the GraphicsContext to.

GraphicsContext(GraphicsMode, IWindowInfo, IGraphicsContext, Int32, Int32, GraphicsContextFlags)

Constructs a new GraphicsContext with the specified GraphicsMode, version and flags, and attaches it to the specified window. A dummy context will be created if both the handle and the window are null.

Declaration
public GraphicsContext(GraphicsMode mode, IWindowInfo window, IGraphicsContext shareContext, int major, int minor, GraphicsContextFlags flags)
Parameters
Type Name Description
GraphicsMode mode

The OpenTK.Graphics.GraphicsMode of the GraphicsContext.

IWindowInfo window

The OpenTK.Platform.IWindowInfo to attach the GraphicsContext to.

IGraphicsContext shareContext

The GraphicsContext to share resources with, or null for explicit non-sharing.

Int32 major

The major version of the new GraphicsContext.

Int32 minor

The minor version of the new GraphicsContext.

GraphicsContextFlags flags

The GraphicsContextFlags for the GraphicsContext.

Remarks

Different hardware supports different flags, major and minor versions. Invalid parameters will be silently ignored.

GraphicsContext(GraphicsMode, IWindowInfo, Int32, Int32, GraphicsContextFlags)

Constructs a new GraphicsContext with the specified GraphicsMode, version and flags, and attaches it to the specified window.

Declaration
public GraphicsContext(GraphicsMode mode, IWindowInfo window, int major, int minor, GraphicsContextFlags flags)
Parameters
Type Name Description
GraphicsMode mode

The OpenTK.Graphics.GraphicsMode of the GraphicsContext.

IWindowInfo window

The OpenTK.Platform.IWindowInfo to attach the GraphicsContext to.

Int32 major

The major version of the new GraphicsContext.

Int32 minor

The minor version of the new GraphicsContext.

GraphicsContextFlags flags

The GraphicsContextFlags for the GraphicsContext.

Remarks

Different hardware supports different flags, major and minor versions. Invalid parameters will be silently ignored.

Properties

CurrentContext

Gets the GraphicsContext that is current in the calling thread.

Declaration
public static IGraphicsContext CurrentContext { get; }
Property Value
Type Description
IGraphicsContext
Remarks

Note: this property will not function correctly when both desktop and EGL contexts are available in the same process. This scenario is very unlikely to appear in practice.

CurrentContextHandle

Gets the handle of the current GraphicsContext in the calling thread.

Declaration
public static ContextHandle CurrentContextHandle { get; }
Property Value
Type Description
ContextHandle

DirectRendering

Gets or sets a System.Boolean, indicating whether GraphicsContexts will perform direct rendering.

Declaration
public static bool DirectRendering { get; set; }
Property Value
Type Description
Boolean
Remarks

If DirectRendering is true, new contexts will be constructed with direct rendering capabilities, if possible. If DirectRendering is false, new contexts will be constructed with indirect rendering capabilities.

This property does not affect existing GraphicsContexts, unless they are recreated.

This property is ignored on Operating Systems without support for indirect rendering, like Windows and OS X.

ErrorChecking

Gets or sets a System.Boolean, indicating whether automatic error checking should be performed. Influences the debug version of OpenTK.dll, only.

Declaration
public bool ErrorChecking { get; set; }
Property Value
Type Description
Boolean
Remarks

Automatic error checking will clear the OpenGL error state. Set CheckErrors to false if you use the OpenGL error state in your code flow (e.g. for checking supported texture formats).

GraphicsMode

Gets the GraphicsMode of the context.

Declaration
public GraphicsMode GraphicsMode { get; }
Property Value
Type Description
GraphicsMode

IsCurrent

Gets a Boolean indicating whether this instance is current in the calling thread.

Declaration
public bool IsCurrent { get; }
Property Value
Type Description
Boolean

IsDisposed

Gets a Boolean indicating whether this instance has been disposed. It is an error to access any instance methods if this property returns true.

Declaration
public bool IsDisposed { get; }
Property Value
Type Description
Boolean

ShareContexts

Gets or sets a System.Boolean, indicating whether GraphicsContext resources are shared

Declaration
public static bool ShareContexts { get; set; }
Property Value
Type Description
Boolean
Remarks

If ShareContexts is true, new GLContexts will share resources. If this value is false, new GLContexts will not share resources.

Changing this value will not affect already created GLContexts.

SwapInterval

Gets or sets a positive integer in the range [1, n), indicating the number of DisplayDevice refreshes between consecutive SwapBuffers() calls. The maximum value for n is implementation-dependent. The default value is 1. Invalid values will be clamped to the valid range.

Declaration
public int SwapInterval { get; set; }
Property Value
Type Description
Int32

Methods

Assert()

Checks if a GraphicsContext exists in the calling thread and throws a GraphicsContextMissingException if it doesn't.

Declaration
public static void Assert()
Exceptions
Type Condition
GraphicsContextMissingException

Generated when no GraphicsContext is current in the calling thread.

Dispose()

Disposes of the GraphicsContext.

Declaration
public void Dispose()

Equals(Object)

Compares two instances.

Declaration
public override bool Equals(object obj)
Parameters
Type Name Description
Object obj

The instance to compare to.

Returns
Type Description
Boolean

True, if obj is equal to this instance; false otherwise.

Overrides
Object.Equals(Object)

Finalize()

Marks this context as deleted, but does not actually release unmanaged resources due to the threading requirements of OpenGL. Use Dispose() instead.

Declaration
protected void Finalize()

GetHashCode()

Returns the hash code for this instance.

Declaration
public override int GetHashCode()
Returns
Type Description
Int32

A System.Int32 with the hash code of this instance.

Overrides
Object.GetHashCode()

LoadAll()

Loads all OpenGL entry points.

Declaration
public void LoadAll()
Exceptions
Type Condition
GraphicsContextException

Occurs when this instance is not current on the calling thread.

MakeCurrent(IWindowInfo)

Makes the GraphicsContext the current rendering target.

Declaration
public void MakeCurrent(IWindowInfo window)
Parameters
Type Name Description
IWindowInfo window

A valid IWindowInfo structure.

Remarks

You can use this method to bind the GraphicsContext to a different window than the one it was created from.

SwapBuffers()

Swaps buffers on a context. This presents the rendered scene to the user.

Declaration
public void SwapBuffers()

ToString()

Returns a String representing this instance.

Declaration
public override string ToString()
Returns
Type Description
String

A String that contains a string representation of this instance.

Overrides
Object.ToString()

Update(IWindowInfo)

Updates the graphics context. This must be called when the render target is resized for proper behavior on Mac OS X.

Declaration
public void Update(IWindowInfo window)
Parameters
Type Name Description
IWindowInfo window

Explicit Interface Implementations

IGraphicsContextInternal.Context

Gets a handle to the OpenGL rendering context.

Declaration
ContextHandle IGraphicsContextInternal.Context { get; }
Returns
Type Description
ContextHandle

IGraphicsContextInternal.GetAddress(IntPtr)

Retrieves the implementation-defined address of an OpenGL function.

Declaration
IntPtr IGraphicsContextInternal.GetAddress(IntPtr function)
Parameters
Type Name Description
IntPtr function

A pointer to a null-terminated buffer containing the name of the OpenGL function.

Returns
Type Description
IntPtr

A pointer to the specified function or an invalid pointer if the function is not available in the current OpenGL context. The return value and calling convention depends on the underlying platform.

IGraphicsContextInternal.GetAddress(String)

Retrieves the implementation-defined address of an OpenGL function.

Declaration
IntPtr IGraphicsContextInternal.GetAddress(string function)
Parameters
Type Name Description
String function

The name of the OpenGL function (e.g. "glGetString")

Returns
Type Description
IntPtr

A pointer to the specified function or an invalid pointer if the function is not available in the current OpenGL context. The return value and calling convention depends on the underlying platform.

IGraphicsContextInternal.Implementation

Gets the platform-specific implementation of this IGraphicsContext.

Declaration
IGraphicsContext IGraphicsContextInternal.Implementation { get; }
Returns
Type Description
IGraphicsContext

Implements

IGraphicsContext
System.IDisposable
IGraphicsContextInternal
In This Article
Back to top Generated by DocFX