]>
git.saurik.com Git - wxWidgets.git/blob - src/motif/glcanvas.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxGLCanvas, for using OpenGL with wxWindows 2.0 for Motif.
4 // Uses the GLX extension.
5 // Author: Julian Smart and Wolfram Gloger
9 // Copyright: (c) Julian Smart, Wolfram Gloger
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
14 #pragma implementation "glcanvas.h"
21 #include "wx/glcanvas.h"
26 #include "wx/motif/private.h"
29 // workaround for bug in Mesa's glx.c
30 static int bitcount( unsigned long n
)
42 * GLCanvas implementation
45 IMPLEMENT_CLASS(wxGLCanvas
, wxScrolledWindow
)
47 wxGLCanvas::wxGLCanvas(wxWindow
*parent
, wxWindowID id
= -1, const wxPoint
& pos
,
48 const wxSize
& size
, long style
,
49 const wxString
& name
, int *attrib_list
, const wxPalette
& palette
):
50 wxScrolledWindow(parent
, id
, pos
, size
, style
, name
)
52 XVisualInfo
*vi
, vi_templ
;
53 XWindowAttributes xwa
;
56 Display
* display
= (Display
*) GetXDisplay();
59 // Check for the presence of the GLX extension
60 if(!glXQueryExtension(display
, NULL
, NULL
)) {
61 wxDebugMsg("wxGLCanvas: GLX extension is missing\n");
66 // Get an appropriate visual
67 vi
= glXChooseVisual(display
, DefaultScreen(display
), attrib_list
);
70 // Here we should make sure that vi is the same visual as the
71 // one used by the xwindow drawable in wxCanvas. However,
72 // there is currently no mechanism for this in wx_canvs.cc.
74 // By default, we use the visual of xwindow
75 XGetWindowAttributes(display
, (Window
) GetXWindow(), &xwa
);
76 vi_templ
.visualid
= XVisualIDFromVisual(xwa
.visual
);
77 vi
= XGetVisualInfo(display
, VisualIDMask
, &vi_templ
, &n
);
79 glXGetConfig(display
, vi
, GLX_USE_GL
, &val
);
81 // Basically, this is it. It should be possible to use vi
82 // in glXCreateContext() below. But this fails with Mesa.
83 // I notified the Mesa author about it; there may be a fix.
85 // Construct an attribute list matching the visual
88 if(vi
->c_class
==TrueColor
|| vi
->c_class
==DirectColor
) { // RGBA visual
89 a_list
[n
++] = GLX_RGBA
;
90 a_list
[n
++] = GLX_RED_SIZE
;
91 a_list
[n
++] = bitcount(vi
->red_mask
);
92 a_list
[n
++] = GLX_GREEN_SIZE
;
93 a_list
[n
++] = bitcount(vi
->green_mask
);
94 a_list
[n
++] = GLX_BLUE_SIZE
;
95 a_list
[n
++] = bitcount(vi
->blue_mask
);
96 glXGetConfig(display
, vi
, GLX_ALPHA_SIZE
, &val
);
97 a_list
[n
++] = GLX_ALPHA_SIZE
;
99 } else { // Color index visual
100 glXGetConfig(display
, vi
, GLX_BUFFER_SIZE
, &val
);
101 a_list
[n
++] = GLX_BUFFER_SIZE
;
106 vi
= glXChooseVisual(display
, DefaultScreen(display
), a_list
);
108 #endif /* OLD_MESA */
111 // Create the GLX context and make it current
112 glx_cx
= glXCreateContext(display
, vi
, 0, GL_TRUE
);
119 wxGLCanvas::~wxGLCanvas(void)
121 Display
* display
= (Display
*) GetXDisplay();
122 if(glx_cx
) glXDestroyContext(display
, glx_cx
);
125 void wxGLCanvas::SwapBuffers()
127 Display
* display
= (Display
*) GetXDisplay();
128 if(glx_cx
) glXSwapBuffers(display
, (Window
) GetXWindow());
131 void wxGLCanvas::SetCurrent()
133 Display
* display
= (Display
*) GetXDisplay();
134 if(glx_cx
) glXMakeCurrent(display
, (Window
) GetXWindow(), glx_cx
);
137 void wxGLCanvas::SetColour(const char *col
)
139 wxColour
*the_colour
= wxTheColourDatabase
->FindColour(col
);
142 glGetBooleanv(GL_RGBA_MODE
, &b
);
144 glColor3ub(the_colour
->Red(),
148 GLint pix
= (GLint
)the_colour
->m_pixel
;
151 exact_def
.red
= (unsigned short)the_colour
->Red() << 8;
152 exact_def
.green
= (unsigned short)the_colour
->Green() << 8;
153 exact_def
.blue
= (unsigned short)the_colour
->Blue() << 8;
154 exact_def
.flags
= DoRed
| DoGreen
| DoBlue
;
155 if(!XAllocColor((Display
*) GetXDisplay(), (Colormap
) wxTheApp
->GetMainColormap(GetXDisplay()), &exact_def
)) {
156 wxDebugMsg("wxGLCanvas: cannot allocate color\n");
159 pix
= the_colour
->m_pixel
= exact_def
.pixel
;