]> git.saurik.com Git - wxWidgets.git/blob - utils/glcanvas/src/glcanvas.cpp
GTK's toolbar class has no OnPaint
[wxWidgets.git] / utils / glcanvas / src / glcanvas.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: glcanvas.cpp
3 // Purpose: wxGLCanvas, for using OpenGL/Mesa with wxWindows and GTK
4 // Author: Robert Roebling
5 // Modified by:
6 // Created: 17/08/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Robert Roebling
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "glcanvas.h"
14 #endif
15
16 #include "wx/wxprec.h"
17
18 #include "wx/frame.h"
19 #include "wx/colour.h"
20 #include "glcanvas.h"
21 #include <gdk/gdkx.h>
22
23 //---------------------------------------------------------------------------
24 // wxGLContext
25 //---------------------------------------------------------------------------
26
27 IMPLEMENT_CLASS(wxGLContext,wxObject)
28
29 wxGLContext::wxGLContext( bool WXUNUSED(isRGB), wxWindow *win, const wxPalette& WXUNUSED(palette) )
30 {
31 m_window = win;
32 m_widget = win->m_wxwindow;
33
34 int data[] = {GLX_RGBA,GLX_RED_SIZE,1,GLX_GREEN_SIZE,1,
35 GLX_BLUE_SIZE,1,GLX_DOUBLEBUFFER,None};
36
37 Display *display = GDK_WINDOW_XDISPLAY( m_widget->window );
38 XVisualInfo *visual_info = glXChooseVisual( display, DefaultScreen(display), data );
39
40 wxCHECK_RET( visual_info != NULL, "Couldn't choose visual for OpenGl" );
41
42 m_glContext = glXCreateContext( display, visual_info, None, GL_TRUE );
43
44 wxCHECK_RET( m_glContext != NULL, "Couldn't create OpenGl context" );
45
46 glXMakeCurrent( display, GDK_WINDOW_XWINDOW(m_widget->window), m_glContext );
47 }
48
49 wxGLContext::~wxGLContext()
50 {
51 if (m_glContext)
52 {
53 Display *display = GDK_WINDOW_XDISPLAY( m_widget->window );
54 glXMakeCurrent( display, GDK_WINDOW_XWINDOW(m_widget->window), m_glContext );
55
56 glXDestroyContext( display, m_glContext );
57 }
58 }
59
60 void wxGLContext::SwapBuffers()
61 {
62 if (m_glContext)
63 {
64 Display *display = GDK_WINDOW_XDISPLAY( m_widget->window );
65 glXSwapBuffers( display, GDK_WINDOW_XWINDOW( m_widget->window ) );
66 }
67 }
68
69 void wxGLContext::SetCurrent()
70 {
71 if (m_glContext)
72 {
73 Display *display = GDK_WINDOW_XDISPLAY( m_widget->window );
74 glXMakeCurrent( display, GDK_WINDOW_XWINDOW(m_widget->window), m_glContext );
75 }
76 }
77
78 void wxGLContext::SetColour(const char *colour)
79 {
80 float r = 0.0;
81 float g = 0.0;
82 float b = 0.0;
83 wxColour *col = wxTheColourDatabase->FindColour(colour);
84 if (col)
85 {
86 r = (float)(col->Red()/256.0);
87 g = (float)(col->Green()/256.0);
88 b = (float)(col->Blue()/256.0);
89 glColor3f( r, g, b);
90 }
91 }
92
93 void wxGLContext::SetupPixelFormat()
94 {
95 }
96
97 void wxGLContext::SetupPalette( const wxPalette& WXUNUSED(palette) )
98 {
99 }
100
101 wxPalette wxGLContext::CreateDefaultPalette()
102 {
103 return wxNullPalette;
104 }
105
106 //---------------------------------------------------------------------------
107 // wxGlCanvas
108 //---------------------------------------------------------------------------
109
110 IMPLEMENT_CLASS(wxGLCanvas, wxScrolledWindow)
111
112 BEGIN_EVENT_TABLE(wxGLCanvas, wxScrolledWindow)
113 EVT_SIZE(wxGLCanvas::OnSize)
114 END_EVENT_TABLE()
115
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)
120 {
121 m_glContext = new wxGLContext( TRUE, this, palette );
122 }
123
124 wxGLCanvas::~wxGLCanvas()
125 {
126 if (m_glContext) delete m_glContext;
127 }
128
129 void wxGLCanvas::SwapBuffers()
130 {
131 if (m_glContext) m_glContext->SwapBuffers();
132 }
133
134 void wxGLCanvas::OnSize(wxSizeEvent& WXUNUSED(event))
135 {
136 int width, height;
137 GetClientSize(& width, & height);
138
139 if (m_glContext)
140 {
141 m_glContext->SetCurrent();
142
143 glViewport(0, 0, (GLint)width, (GLint)height);
144 glMatrixMode(GL_PROJECTION);
145 glLoadIdentity();
146 glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 15.0 );
147 glMatrixMode(GL_MODELVIEW);
148 }
149 }
150
151 void wxGLCanvas::SetCurrent()
152 {
153 if (m_glContext) m_glContext->SetCurrent();
154 }
155
156 void wxGLCanvas::SetColour( const char *colour )
157 {
158 if (m_glContext) m_glContext->SetColour( colour );
159 }
160
161