-/////////////////////////////////////////////////////////////////////////////
-// Name: glcanvas.cpp
-// Purpose: wxGLCanvas, for using OpenGL with wxWindows 2.0 for Motif.
+///////////////////////////////////////////////////////////////////////////////
+// Name: src/x11/glcanvas.cpp
+// Purpose: wxGLCanvas, for using OpenGL with wxWidgets
// Uses the GLX extension.
// Author: Julian Smart and Wolfram Gloger
-// Modified by:
+// Modified by: Vadim Zeitlin to update to new API
// Created: 1995, 1999
// RCS-ID: $Id$
// Copyright: (c) Julian Smart, Wolfram Gloger
-// Licence: wxWindows licence
-/////////////////////////////////////////////////////////////////////////////
+// Licence: wxWindows licence
+///////////////////////////////////////////////////////////////////////////////
-#ifdef __GNUG__
-#pragma implementation "glcanvas.h"
-#endif
+// TODO: merge this with wxGTK version in some src/unix/glcanvasx11.cpp
+
+// ============================================================================
+// declarations
+// ============================================================================
+
+// ----------------------------------------------------------------------------
+// headers
+// ----------------------------------------------------------------------------
+
+// for compilers that support precompilation, includes "wx.h".
+#include "wx/wxprec.h"
-#include "wx/setup.h"
+#if defined(__BORLANDC__)
+ #pragma hdrstop
+#endif
#if wxUSE_GLCANVAS
#include "wx/glcanvas.h"
-#include "wx/utils.h"
-#include "wx/app.h"
+
+#ifndef WX_PRECOMP
+ #include "wx/log.h"
+ #include "wx/app.h"
+ #include "wx/utils.h"
+#endif
#ifdef __VMS
# pragma message disable nosimpint
#endif
-#include <Xm/Xm.h>
+#include <X11/Xlib.h>
#ifdef __VMS
# pragma message enable nosimpint
#endif
-#include "wx/motif/private.h"
+#include "wx/x11/private.h"
+
+// ----------------------------------------------------------------------------
+// local functions
+// ----------------------------------------------------------------------------
-#ifdef OLD_MESA
-// workaround for bug in Mesa's glx.c
-static int bitcount( unsigned long n )
+static inline Window wxGetClientAreaWindow(const wxWindow* win)
{
- int bits;
- for (bits=0; n>0;) {
- if(n & 1) bits++;
- n = n >> 1;
- }
- return bits;
-}
+ return (Window)win->
+#ifdef __WXMOTIF__
+ GetClientXWindow();
+#else
+ GetClientAreaWindow();
#endif
+}
-/*
- * GLContext implementation
- */
+// ============================================================================
+// implementation
+// ============================================================================
-IMPLEMENT_CLASS(wxGLContext,wxObject)
+// ----------------------------------------------------------------------------
+// wxGLContext
+// ----------------------------------------------------------------------------
-wxGLContext::wxGLContext( bool WXUNUSED(isRGB), wxWindow *win,
- const wxPalette& WXUNUSED(palette) )
-{
- m_window = win;
- // m_widget = win->m_wxwindow;
-
- wxGLCanvas *gc = (wxGLCanvas*) win;
- XVisualInfo *vi = (XVisualInfo *) gc->m_vi;
-
- wxCHECK_RET( vi, "invalid visual for OpenGl" );
-
- m_glContext = glXCreateContext( (Display *)m_window->GetXDisplay(), vi,
- None, GL_TRUE);
-
- wxCHECK_RET( m_glContext, "Couldn't create OpenGl context" );
-}
+IMPLEMENT_CLASS(wxGLContext, wxObject)
-wxGLContext::wxGLContext(
- bool WXUNUSED(isRGB), wxWindow *win,
- const wxPalette& WXUNUSED(palette),
- const wxGLContext *other /* for sharing display lists */
-)
+wxGLContext::wxGLContext(wxGLCanvas *win, const wxGLContext *other)
{
- m_window = win;
- // m_widget = win->m_wxwindow;
-
- wxGLCanvas *gc = (wxGLCanvas*) win;
- XVisualInfo *vi = (XVisualInfo *) gc->m_vi;
-
- wxCHECK_RET( vi, "invalid visual for OpenGl" );
-
- if( other != 0 )
- m_glContext = glXCreateContext( (Display *)m_window->GetXDisplay(), vi,
- other->m_glContext, GL_TRUE );
- else
- m_glContext = glXCreateContext( (Display *)m_window->GetXDisplay(), vi,
- None, GL_TRUE );
-
- wxCHECK_RET( m_glContext, "Couldn't create OpenGl context" );
+ XVisualInfo *vi = (XVisualInfo *) win->m_vi;
+ wxASSERT_MSG( vi, wxT("invalid visual in wxGLCanvas") );
+
+ m_glContext = glXCreateContext( (Display *)wxGetDisplay(),
+ vi,
+ other ? other->m_glContext : None,
+ GL_TRUE);
+
+ wxASSERT_MSG( m_glContext, wxT("Couldn't create OpenGL context") );
}
wxGLContext::~wxGLContext()
{
- if (!m_glContext) return;
-
+ if (!m_glContext)
+ return;
+
if (m_glContext == glXGetCurrentContext())
{
- glXMakeCurrent( (Display*) m_window->GetXDisplay(), None, NULL);
+ glXMakeCurrent( (Display*) wxGetDisplay(), None, NULL);
}
-
- glXDestroyContext( (Display*) m_window->GetXDisplay(), m_glContext );
+
+ glXDestroyContext( (Display*) wxGetDisplay(), m_glContext );
}
-void wxGLContext::SwapBuffers()
+void wxGLContext::SetCurrent(const wxGLCanvas& win) const
{
if (m_glContext)
{
- Display* display = (Display*) m_window->GetXDisplay();
- glXSwapBuffers(display, (Window) m_window->GetXWindow());
- }
-}
-
-void wxGLContext::SetCurrent()
-{
- if (m_glContext)
- {
- Display* display = (Display*) m_window->GetXDisplay();
- glXMakeCurrent(display, (Window) m_window->GetXWindow(),
- m_glContext );;
- }
-}
-
-void wxGLContext::SetColour(const char *colour)
-{
- wxColour *the_colour = wxTheColourDatabase->FindColour(colour);
- if(the_colour) {
- GLboolean b;
- glGetBooleanv(GL_RGBA_MODE, &b);
- if(b) {
- glColor3ub(the_colour->Red(),
- the_colour->Green(),
- the_colour->Blue());
- } else {
- the_colour->CalcPixel(wxTheApp->GetMainColormap(m_window->GetXDisplay()));
- GLint pix = (GLint)the_colour->GetPixel();
- if(pix == -1)
- {
- wxLogError("wxGLCanvas: cannot allocate color\n");
- return;
- }
- glIndexi(pix);
- }
+ Display* display = (Display*) wxGetDisplay();
+ glXMakeCurrent(display, wxGetClientAreaWindow(&win), m_glContext);
}
}
-void wxGLContext::SetupPixelFormat()
-{
-}
-
-void wxGLContext::SetupPalette( const wxPalette& WXUNUSED(palette) )
-{
-}
-
-wxPalette wxGLContext::CreateDefaultPalette()
-{
- return wxNullPalette;
-}
-
-
-
+// ----------------------------------------------------------------------------
+// wxGLCanvas
+// ----------------------------------------------------------------------------
-/*
- * GLCanvas implementation
- */
+IMPLEMENT_CLASS(wxGLCanvas, wxWindow)
-IMPLEMENT_CLASS(wxGLCanvas, wxScrolledWindow)
-
-BEGIN_EVENT_TABLE(wxGLCanvas, wxScrolledWindow)
-// EVT_SIZE(wxGLCanvas::OnSize)
-END_EVENT_TABLE()
-
-
-wxGLCanvas::wxGLCanvas( wxWindow *parent, wxWindowID id,
- const wxPoint& pos, const wxSize& size,
- long style, const wxString& name,
- int *attribList,
- const wxPalette& palette )
-: wxScrolledWindow(parent, id, pos, size, style, name)
+wxGLCanvas::wxGLCanvas(wxWindow *parent,
+ wxWindowID id,
+ const int *attribList,
+ const wxPoint& pos,
+ const wxSize& size,
+ long style,
+ const wxString& name,
+ const wxPalette& palette)
{
- Create( parent, NULL, NULL, id, pos, size, style, name, attribList, palette );
-}
-
-wxGLCanvas::wxGLCanvas( wxWindow *parent,
- const wxGLContext *shared,
- wxWindowID id,
- const wxPoint& pos, const wxSize& size,
- long style, const wxString& name,
- int *attribList,
- const wxPalette& palette )
-: wxScrolledWindow(parent, id, pos, size, style, name)
-{
- Create( parent, shared, NULL, id, pos, size, style, name, attribList, palette );
+ Create(parent, id, pos, size, style, name, attribList, palette);
}
-wxGLCanvas::wxGLCanvas( wxWindow *parent,
- const wxGLCanvas *shared,
+bool wxGLCanvas::Create(wxWindow *parent,
wxWindowID id,
- const wxPoint& pos, const wxSize& size,
- long style, const wxString& name,
- int *attribList,
- const wxPalette& palette )
-: wxScrolledWindow(parent, id, pos, size, style, name)
-{
- Create( parent, NULL, shared, id, pos, size, style, name, attribList, palette );
-}
+ const wxPoint& pos,
+ const wxSize& size,
+ long style,
+ const wxString& name,
+ const int *attribList,
+ const wxPalette& palette)
+{
+ m_vi = NULL;
+ if ( !wxWindow::Create(parent, id, pos, size, style, name) )
+ return false;
-/*
-bool wxGLCanvas::Create(wxWindow *parent,
- const wxGLContext *shared, const wxGLCanvas *shared_context_of,
- wxWindowID id = -1, const wxPoint& pos,
- const wxSize& size, long style,
- const wxString& name, int *attribList, const wxPalette& palette):
- wxScrolledWindow(parent, id, pos, size, style, name)
-*/
-
-bool wxGLCanvas::Create( wxWindow *parent,
- const wxGLContext *shared,
- const wxGLCanvas *shared_context_of,
- wxWindowID id,
- const wxPoint& pos, const wxSize& size,
- long style, const wxString& name,
- int *attribList,
- const wxPalette& palette)
-{
XVisualInfo *vi, vi_templ;
XWindowAttributes xwa;
int val, n;
- m_sharedContext = (wxGLContext*)shared; // const_cast
- m_sharedContextOf = (wxGLCanvas*)shared_context_of; // const_cast
- m_glContext = (wxGLContext*) NULL;
-
- Display* display = (Display*) GetXDisplay();
+ Display* display = (Display*) wxGetDisplay();
// Check for the presence of the GLX extension
- if(!glXQueryExtension(display, NULL, NULL)) {
- wxDebugMsg("wxGLCanvas: GLX extension is missing\n");
- return false;
+ if(!glXQueryExtension(display, NULL, NULL))
+ {
+ wxLogDebug(wxT("wxGLCanvas: GLX extension is missing"));
+ return false;
}
if(attribList) {
int data[512], arg=0, p=0;
-
+
while( (attribList[arg]!=0) && (p<512) )
{
switch( attribList[arg++] )
data[p++]=GLX_BLUE_SIZE; data[p++]=attribList[arg++]; break;
case WX_GL_MIN_ALPHA:
data[p++]=GLX_ALPHA_SIZE; data[p++]=attribList[arg++]; break;
- case WX_GL_DEPTH_SIZE:
+ case WX_GL_DEPTH_SIZE:
data[p++]=GLX_DEPTH_SIZE; data[p++]=attribList[arg++]; break;
- case WX_GL_STENCIL_SIZE:
+ case WX_GL_STENCIL_SIZE:
data[p++]=GLX_STENCIL_SIZE; data[p++]=attribList[arg++]; break;
case WX_GL_MIN_ACCUM_RED:
data[p++]=GLX_ACCUM_RED_SIZE; data[p++]=attribList[arg++]; break;
default:
break;
}
- }
- data[p] = 0;
+ }
+ data[p] = 0;
- attribList = (int*) data;
// Get an appropriate visual
- vi = glXChooseVisual(display, DefaultScreen(display), attribList);
+ vi = glXChooseVisual(display, DefaultScreen(display), data);
if(!vi) return false;
-
+
// Here we should make sure that vi is the same visual as the
// one used by the xwindow drawable in wxCanvas. However,
// there is currently no mechanism for this in wx_canvs.cc.
- } else {
- // By default, we use the visual of xwindow
- // NI: is this really senseful ? opengl in e.g. color index mode ?
- XGetWindowAttributes(display, (Window) GetXWindow(), &xwa);
- vi_templ.visualid = XVisualIDFromVisual(xwa.visual);
- vi = XGetVisualInfo(display, VisualIDMask, &vi_templ, &n);
- if(!vi) return false;
- glXGetConfig(display, vi, GLX_USE_GL, &val);
- if(!val) return false;
- // Basically, this is it. It should be possible to use vi
- // in glXCreateContext() below. But this fails with Mesa.
- // I notified the Mesa author about it; there may be a fix.
-#ifdef OLD_MESA
- // Construct an attribute list matching the visual
- int a_list[32];
- n = 0;
- if(vi->c_class==TrueColor || vi->c_class==DirectColor) { // RGBA visual
- a_list[n++] = GLX_RGBA;
- a_list[n++] = GLX_RED_SIZE;
- a_list[n++] = bitcount(vi->red_mask);
- a_list[n++] = GLX_GREEN_SIZE;
- a_list[n++] = bitcount(vi->green_mask);
- a_list[n++] = GLX_BLUE_SIZE;
- a_list[n++] = bitcount(vi->blue_mask);
- glXGetConfig(display, vi, GLX_ALPHA_SIZE, &val);
- a_list[n++] = GLX_ALPHA_SIZE;
- a_list[n++] = val;
- } else { // Color index visual
- glXGetConfig(display, vi, GLX_BUFFER_SIZE, &val);
- a_list[n++] = GLX_BUFFER_SIZE;
- a_list[n++] = val;
- }
- a_list[n] = None;
- // XFree(vi);
- vi = glXChooseVisual(display, DefaultScreen(display), a_list);
- if(!vi) return false;
-#endif /* OLD_MESA */
+ }
+ else // default attributes
+ {
+ // By default, we use the visual of xwindow
+ XGetWindowAttributes(display, wxGetClientAreaWindow(this), &xwa);
+ vi_templ.visualid = XVisualIDFromVisual(xwa.visual);
+ vi = XGetVisualInfo(display, VisualIDMask, &vi_templ, &n);
+ if(!vi) return false;
+ glXGetConfig(display, vi, GLX_USE_GL, &val);
+ if(!val) return false;
}
m_vi = vi; // safe for later use
-
- wxCHECK_MSG( m_vi, FALSE, "required visual couldn't be found" );
-
- // Create the GLX context and make it current
-
- wxGLContext *share= m_sharedContext;
- if (share==NULL && m_sharedContextOf)
- share = m_sharedContextOf->GetContext();
- m_glContext = new wxGLContext( TRUE, this, wxNullPalette, share );
-
-#ifndef OLD_MESA
- // XFree(vi);
-#endif
- SetCurrent();
+ wxCHECK_MSG( m_vi, false, wxT("required visual couldn't be found") );
return true;
}
-wxGLCanvas::~wxGLCanvas(void)
+wxGLCanvas::~wxGLCanvas()
{
XVisualInfo *vi = (XVisualInfo *) m_vi;
-
- if (vi) XFree( vi );
- if (m_glContext) delete m_glContext;
- // Display* display = (Display*) GetXDisplay();
- // if(glx_cx) glXDestroyContext(display, glx_cx);
+ if (vi)
+ XFree( vi );
}
void wxGLCanvas::SwapBuffers()
{
- if( m_glContext ) m_glContext->SwapBuffers();
-
- // Display* display = (Display*) GetXDisplay();
- // if(glx_cx) glXSwapBuffers(display, (Window) GetXWindow());
+ glXSwapBuffers((Display *)wxGetDisplay(), wxGetClientAreaWindow(this));
}
-void wxGLCanvas::SetCurrent()
+int wxGLCanvas::GetColourIndex(const wxColour& col_)
{
- if( m_glContext ) m_glContext->SetCurrent();
+ wxColour& col = wx_const_cast(wxColour&, col_);
- // Display* display = (Display*) GetXDisplay();
- // if(glx_cx) glXMakeCurrent(display, (Window) GetXWindow(), glx_cx);
-}
+#ifdef __WXMOTIF__
+ col.AllocColour(GetXDisplay());
+#else
+ col.CalcPixel(wxTheApp->GetMainColormap(wxGetDisplay()));
+#endif
-void wxGLCanvas::SetColour(const char *col)
-{
- if( m_glContext ) m_glContext->SetColour(col);
+ return col.GetPixel();
}
-#endif
- // wxUSE_GLCANVAS
-
+#endif // wxUSE_GLCANVAS