]>
git.saurik.com Git - wxWidgets.git/blob - utils/glcanvas/src/glcanvas.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxGLCanvas, for using OpenGL/Mesa with wxWindows and GTK
4 // Author: Robert Roebling
8 // Copyright: (c) Robert Roebling
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "glcanvas.h"
16 #include "wx/wxprec.h"
19 #include "wx/colour.h"
23 //---------------------------------------------------------------------------
25 //---------------------------------------------------------------------------
27 IMPLEMENT_CLASS(wxGLContext
,wxObject
)
29 wxGLContext::wxGLContext( bool WXUNUSED(isRGB
), wxWindow
*win
, const wxPalette
& WXUNUSED(palette
) )
32 m_widget
= win
->m_wxwindow
;
34 int data
[] = {GLX_RGBA
,GLX_RED_SIZE
,1,GLX_GREEN_SIZE
,1,
35 GLX_BLUE_SIZE
,1,GLX_DOUBLEBUFFER
,None
};
37 Display
*display
= GDK_WINDOW_XDISPLAY( m_widget
->window
);
38 XVisualInfo
*visual_info
= glXChooseVisual( display
, DefaultScreen(display
), data
);
40 wxCHECK_RET( visual_info
!= NULL
, "Couldn't choose visual for OpenGl" );
42 m_glContext
= glXCreateContext( display
, visual_info
, None
, GL_TRUE
);
44 wxCHECK_RET( m_glContext
!= NULL
, "Couldn't create OpenGl context" );
46 glXMakeCurrent( display
, GDK_WINDOW_XWINDOW(m_widget
->window
), m_glContext
);
49 wxGLContext::~wxGLContext()
53 Display
*display
= GDK_WINDOW_XDISPLAY( m_widget
->window
);
54 glXMakeCurrent( display
, GDK_WINDOW_XWINDOW(m_widget
->window
), m_glContext
);
56 glXDestroyContext( display
, m_glContext
);
60 void wxGLContext::SwapBuffers()
64 Display
*display
= GDK_WINDOW_XDISPLAY( m_widget
->window
);
65 glXSwapBuffers( display
, GDK_WINDOW_XWINDOW( m_widget
->window
) );
69 void wxGLContext::SetCurrent()
73 Display
*display
= GDK_WINDOW_XDISPLAY( m_widget
->window
);
74 glXMakeCurrent( display
, GDK_WINDOW_XWINDOW(m_widget
->window
), m_glContext
);
78 void wxGLContext::SetColour(const char *colour
)
83 wxColour
*col
= wxTheColourDatabase
->FindColour(colour
);
86 r
= (float)(col
->Red()/256.0);
87 g
= (float)(col
->Green()/256.0);
88 b
= (float)(col
->Blue()/256.0);
93 void wxGLContext::SetupPixelFormat()
97 void wxGLContext::SetupPalette( const wxPalette
& WXUNUSED(palette
) )
101 wxPalette
wxGLContext::CreateDefaultPalette()
103 return wxNullPalette
;
106 //---------------------------------------------------------------------------
108 //---------------------------------------------------------------------------
110 IMPLEMENT_CLASS(wxGLCanvas
, wxScrolledWindow
)
112 BEGIN_EVENT_TABLE(wxGLCanvas
, wxScrolledWindow
)
113 EVT_SIZE(wxGLCanvas::OnSize
)
116 wxGLCanvas::wxGLCanvas(wxWindow
*parent
, wxWindowID id
,
117 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
,
118 int *WXUNUSED(attribList
), const wxPalette
& palette
):
119 wxScrolledWindow(parent
, id
, pos
, size
, style
, name
)
121 m_glContext
= new wxGLContext( TRUE
, this, palette
);
124 wxGLCanvas::~wxGLCanvas()
126 if (m_glContext
) delete m_glContext
;
129 void wxGLCanvas::SwapBuffers()
131 if (m_glContext
) m_glContext
->SwapBuffers();
134 void wxGLCanvas::OnSize(wxSizeEvent
& WXUNUSED(event
))
137 GetClientSize(& width
, & height
);
141 m_glContext
->SetCurrent();
143 glViewport(0, 0, (GLint
)width
, (GLint
)height
);
144 glMatrixMode(GL_PROJECTION
);
146 glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 15.0 );
147 glMatrixMode(GL_MODELVIEW
);
151 void wxGLCanvas::SetCurrent()
153 if (m_glContext
) m_glContext
->SetCurrent();
156 void wxGLCanvas::SetColour( const char *colour
)
158 if (m_glContext
) m_glContext
->SetColour( colour
);