]>
git.saurik.com Git - wxWidgets.git/blob - utils/glcanvas/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"
22 #include "wx/motif/private.h"
25 // workaround for bug in Mesa's glx.c
26 static int bitcount( unsigned long n
)
38 * GLCanvas implementation
41 IMPLEMENT_CLASS(wxGLCanvas
, wxScrolledWindow
)
43 wxGLCanvas::wxGLCanvas(wxWindow
*parent
, wxWindowID id
= -1, const wxPoint
& pos
,
44 const wxSize
& size
, long style
,
45 const wxString
& name
, int *attrib_list
, const wxPalette
& palette
):
46 wxScrolledWindow(parent
, id
, pos
, size
, style
, name
)
48 XVisualInfo
*vi
, vi_templ
;
49 XWindowAttributes xwa
;
52 Display
* display
= (Display
*) GetXDisplay();
55 // Check for the presence of the GLX extension
56 if(!glXQueryExtension(display
, NULL
, NULL
)) {
57 wxDebugMsg("wxGLCanvas: GLX extension is missing\n");
62 // Get an appropriate visual
63 vi
= glXChooseVisual(display
, DefaultScreen(display
), attrib_list
);
66 // Here we should make sure that vi is the same visual as the
67 // one used by the xwindow drawable in wxCanvas. However,
68 // there is currently no mechanism for this in wx_canvs.cc.
70 // By default, we use the visual of xwindow
71 XGetWindowAttributes(display
, (Window
) GetXWindow(), &xwa
);
72 vi_templ
.visualid
= XVisualIDFromVisual(xwa
.visual
);
73 vi
= XGetVisualInfo(display
, VisualIDMask
, &vi_templ
, &n
);
75 glXGetConfig(display
, vi
, GLX_USE_GL
, &val
);
77 // Basically, this is it. It should be possible to use vi
78 // in glXCreateContext() below. But this fails with Mesa.
79 // I notified the Mesa author about it; there may be a fix.
81 // Construct an attribute list matching the visual
84 if(vi
->c_class
==TrueColor
|| vi
->c_class
==DirectColor
) { // RGBA visual
85 a_list
[n
++] = GLX_RGBA
;
86 a_list
[n
++] = GLX_RED_SIZE
;
87 a_list
[n
++] = bitcount(vi
->red_mask
);
88 a_list
[n
++] = GLX_GREEN_SIZE
;
89 a_list
[n
++] = bitcount(vi
->green_mask
);
90 a_list
[n
++] = GLX_BLUE_SIZE
;
91 a_list
[n
++] = bitcount(vi
->blue_mask
);
92 glXGetConfig(display
, vi
, GLX_ALPHA_SIZE
, &val
);
93 a_list
[n
++] = GLX_ALPHA_SIZE
;
95 } else { // Color index visual
96 glXGetConfig(display
, vi
, GLX_BUFFER_SIZE
, &val
);
97 a_list
[n
++] = GLX_BUFFER_SIZE
;
102 vi
= glXChooseVisual(display
, DefaultScreen(display
), a_list
);
104 #endif /* OLD_MESA */
107 // Create the GLX context and make it current
108 glx_cx
= glXCreateContext(display
, vi
, 0, GL_TRUE
);
115 wxGLCanvas::~wxGLCanvas(void)
117 Display
* display
= (Display
*) GetXDisplay();
118 if(glx_cx
) glXDestroyContext(display
, glx_cx
);
121 void wxGLCanvas::SwapBuffers()
123 Display
* display
= (Display
*) GetXDisplay();
124 if(glx_cx
) glXSwapBuffers(display
, (Window
) GetXWindow());
127 void wxGLCanvas::SetCurrent()
129 Display
* display
= (Display
*) GetXDisplay();
130 if(glx_cx
) glXMakeCurrent(display
, (Window
) GetXWindow(), glx_cx
);
133 void wxGLCanvas::SetColour(const char *col
)
135 wxColour
*the_colour
= wxTheColourDatabase
->FindColour(col
);
138 glGetBooleanv(GL_RGBA_MODE
, &b
);
140 glColor3ub(the_colour
->Red(),
144 GLint pix
= (GLint
)the_colour
->m_pixel
;
147 exact_def
.red
= (unsigned short)the_colour
->Red() << 8;
148 exact_def
.green
= (unsigned short)the_colour
->Green() << 8;
149 exact_def
.blue
= (unsigned short)the_colour
->Blue() << 8;
150 exact_def
.flags
= DoRed
| DoGreen
| DoBlue
;
151 if(!XAllocColor((Display
*) GetXDisplay(), (Colormap
) wxTheApp
->GetMainColormap(GetXDisplay()), &exact_def
)) {
152 wxDebugMsg("wxGLCanvas: cannot allocate color\n");
155 pix
= the_colour
->m_pixel
= exact_def
.pixel
;