## wxGLCanvas

wxGLCanvas is a class for displaying OpenGL graphics.

There are two ways to use this class:

For the older (before wx 2.7.x) and simpler method, create a wxGLCanvas window using one of the three constructors that implicitly create a rendering context, call wxGLCanvas::SetCurrent to direct normal OpenGL commands to the window, and then call wxGLCanvas::SwapBuffers to show the OpenGL buffer on the window.

For the newer (wx 2.7.x+) method, create a wxGLCanvas window using the constructor that does not create an implicit rendering context, create an explicit instance of a wxGLContext that is initialized with the wxGLCanvas yourself, then use either wxGLCanvas::SetCurrent with the instance of the wxGLContext or wxGLContext::SetCurrent with the instance of the wxGLCanvas to bind the OpenGL state that is represented by the rendering context to the canvas, and then call wxGLCanvas::SwapBuffers to swap the buffers of the OpenGL canvas and thus show your current output.

To set up the attributes for the canvas (number of bits for the depth buffer, number of bits for the stencil buffer and so on) you should set up the correct values of the attribList parameter. The values that should be set up and their meanings will be described below.

To switch on wxGLCanvas support on under Windows, edit setup.h and set `wxUSE_GLCANVAS` to 1. You may also need to have to add `opengl32.lib` to the list of libraries your program is linked with. On Unix, pass `--with-opengl` to configure to compile using OpenGL or Mesa.

Derived from

wxWindow
wxEvtHandler
wxObject

Include files

<wx/glcanvas.h>

Window styles

There are no specific window styles for this class.

See also window styles overview.

Constants

The generic GL implementation doesn't support many of these options, such as stereo, auxiliary buffers, alpha channel, and accum buffer. Other implementations may support them.

WX_GL_RGBA | Use true colour
WX_GL_BUFFER_SIZE | Bits for buffer if not WX_GL_RGBA
WX_GL_LEVEL | 0 for main buffer, >0 for overlay, <0 for underlay
WX_GL_DOUBLEBUFFER | Use doublebuffer
WX_GL_STEREO | Use stereoscopic display
WX_GL_AUX_BUFFERS | Number of auxiliary buffers (not all implementation support this option)
WX_GL_MIN_RED | Use red buffer with most bits (> MIN_RED bits)
WX_GL_MIN_GREEN | Use green buffer with most bits (> MIN_GREEN bits)
WX_GL_MIN_BLUE | Use blue buffer with most bits (> MIN_BLUE bits)
WX_GL_MIN_ALPHA | Use alpha buffer with most bits (> MIN_ALPHA bits)
WX_GL_DEPTH_SIZE | Bits for Z-buffer (0,16,32)
WX_GL_STENCIL_SIZE | Bits for stencil buffer
WX_GL_MIN_ACCUM_RED | Use red accum buffer with most bits (> MIN_ACCUM_RED bits)
WX_GL_MIN_ACCUM_GREEN | Use green buffer with most bits (> MIN_ACCUM_GREEN bits)
WX_GL_MIN_ACCUM_BLUE | Use blue buffer with most bits (> MIN_ACCUM_BLUE bits)
WX_GL_MIN_ACCUM_ALPHA | Use blue buffer with most bits (> MIN_ACCUM_ALPHA bits)

See also

wxGLContext

Members

wxGLCanvas::wxGLCanvas
wxGLCanvas::GetContext
wxGLCanvas::SetCurrent
wxGLCanvas::SetCurrent
wxGLCanvas::SetColour
wxGLCanvas::SwapBuffers

### wxGLCanvas::wxGLCanvas

void wxGLCanvas(wxWindow* parent, wxWindowID id = -1, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize, long style=0, const wxString& name="GLCanvas", int* attribList = 0, const wxPalette& palette = wxNullPalette)

void wxGLCanvas(wxWindow* parent, wxGLContext* sharedContext, wxWindowID id = -1, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize, long style=0, const wxString& name="GLCanvas", int* attribList = 0, const wxPalette& palette = wxNullPalette)

void wxGLCanvas(wxWindow* parent, wxGLCanvas* sharedCanvas, wxWindowID id = -1, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize, long style=0, const wxString& name="GLCanvas", int* attribList = 0, const wxPalette& palette = wxNullPalette)

void wxGLCanvas(wxWindow* parent, wxWindowID id = wxID_ANY, int* attribList = 0, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize, long style=0, const wxString& name="GLCanvas", const wxPalette& palette = wxNullPalette)

Constructors. The first three constructors implicitly create an instance of wxGLContext. The fourth constructur is identical to the first, except for the fact that it does not create such an implicit rendering context, which means that you have to create an explicit instance of wxGLContext yourself (highly recommended for future compatibility with wxWidgets and the flexibility of your own program!).

Note that if you used one of the first three constructors, wxGLCanvas::GetContext returns the pointer to the implicitly created instance, and the wxGLCanvas::SetCurrent method without the parameter should be used. If however you used the fourth constructor, wxGLCanvas::GetContext always returns NULL and the wxGLCanvas::SetCurrent method with the parameter must be used!

parent

Pointer to a parent window.

sharedContext

Context to share object resources with.

id

Window identifier. If -1, will automatically create an identifier.

pos

Window position. wxDefaultPosition is (-1, -1) which indicates that wxWidgets should generate a default position for the window.

size

Window size. wxDefaultSize is (-1, -1) which indicates that wxWidgets should generate a default size for the window. If no suitable size can be found, the window will be sized to 20x20 pixels so that the window is visible but obviously not correctly sized.

style

Window style.

name

Window name.

attribList

Array of int. With this parameter you can set the device context attributes associated to this window. This array is zero-terminated: it should be set up with constants described in the table above. If a constant should be followed by a value, put it in the next array position. For example, the WX_GL_DEPTH_SIZE should be followed by the value that indicates the number of bits for the depth buffer, so:

    attribList[index]= WX_GL_DEPTH_SIZE;
    attribList[index+1]=32;
    and so on.

palette

If the window has the palette, it should by pass this value. Note: palette and WX_GL_RGBA are mutually exclusive.

### wxGLCanvas::GetContext

wxGLContext* GetContext()

Obtains the context that is associated with this canvas if one was implicitly created (by use of one of the first three constructors). Always returns NULL if the canvas was constructed with the fourth constructor.

### wxGLCanvas::SetCurrent

void SetCurrent()

If this canvas was created with one of the first three constructors, a call to this method makes the implicit rendering context of this canvas current with this canvas, so that subsequent OpenGL calls modify the OpenGL state of the implicit rendering context.

If this canvas was created with the fourth constructor, this method should not be called (use the SetCurrent method below with the parameter instead)!

Note that this function may only be called after the window has been shown.

### wxGLCanvas::SetCurrent

void SetCurrent( const wxGLContext RC )

If this canvas was created with one of the first three constructors, this method should not be called (use the SetCurrent above without the parameter instead)!

If this canvas was created with the fourth constructor, a call to this method makes the OpenGL state that is represented by the OpenGL rendering context RC current with this canvas, and if win is an object of type wxGLCanvas, the statements win.SetCurrent(RC); and RC.SetCurrent(win); are equivalent, see wxGLContext::SetCurrent.

Note that this function may only be called after the window has been shown.

### wxGLCanvas::SetColour

void SetColour(const char* colour)

Sets the current colour for this window, using the wxWidgets colour database to find a named colour.

### wxGLCanvas::SwapBuffers

void SwapBuffers()

Swaps the double-buffer of this window, making the back-buffer the front-buffer and vice versa, so that the output of the previous OpenGL commands is displayed on the window.