]> git.saurik.com Git - wxWidgets.git/blame - samples/opengl/penguin/penguin.cpp
Fix recreating of wxBitmapComboBox using untyped client data.
[wxWidgets.git] / samples / opengl / penguin / penguin.cpp
CommitLineData
8b089c5e
JS
1/////////////////////////////////////////////////////////////////////////////
2// Name: penguin.cpp
3// Purpose: wxGLCanvas demo program
4// Author: Robert Roebling
4b764db3 5// Modified by: Sandro Sigala
8b089c5e 6// Created: 04/01/98
8b089c5e 7// Copyright: (c) Robert Roebling
2f6c54eb 8// Licence: wxWindows licence
8b089c5e
JS
9/////////////////////////////////////////////////////////////////////////////
10
8b089c5e
JS
11// For compilers that support precompilation, includes "wx.h".
12#include "wx/wxprec.h"
13
14#ifdef __BORLANDC__
15#pragma hdrstop
16#endif
17
18#ifndef WX_PRECOMP
19#include "wx/wx.h"
20#endif
21
806e2f15
VZ
22#if !wxUSE_GLCANVAS
23 #error "OpenGL required: set wxUSE_GLCANVAS to 1 and rebuild the library"
24#endif
25
8b089c5e 26#include "penguin.h"
65f4ce38
VZ
27#ifdef __DARWIN__
28 #include <OpenGL/glu.h>
3dec57ad 29#else
65f4ce38 30 #include <GL/glu.h>
3dec57ad 31#endif
8b089c5e 32
3a992940
JS
33#include "../../sample.xpm"
34
4b764db3
JS
35// ---------------------------------------------------------------------------
36// MyApp
37// ---------------------------------------------------------------------------
8b089c5e 38
5cf036d0 39// `Main program' equivalent, creating windows and returning main app frame
bcc4c541 40bool MyApp::OnInit()
8b089c5e 41{
45e6e6f8
VZ
42 if ( !wxApp::OnInit() )
43 return false;
44
5cf036d0 45 // Create the main frame window
4b764db3 46 MyFrame *frame = new MyFrame(NULL, wxT("wxWidgets Penguin Sample"),
5cf036d0 47 wxDefaultPosition, wxDefaultSize);
8b089c5e 48
4b764db3
JS
49#if wxUSE_ZLIB
50 if (wxFileExists(wxT("penguin.dxf.gz")))
51 frame->GetCanvas()->LoadDXF(wxT("penguin.dxf.gz"));
52#else
53 if (wxFileExists(wxT("penguin.dxf")))
54 frame->GetCanvas()->LoadDXF(wxT("penguin.dxf"));
55#endif
8b089c5e 56
5cf036d0
DS
57 /* Show the frame */
58 frame->Show(true);
59
60 return true;
8b089c5e
JS
61}
62
63IMPLEMENT_APP(MyApp)
64
4b764db3
JS
65// ---------------------------------------------------------------------------
66// MyFrame
67// ---------------------------------------------------------------------------
68
8b089c5e 69BEGIN_EVENT_TABLE(MyFrame, wxFrame)
4b764db3
JS
70 EVT_MENU(wxID_OPEN, MyFrame::OnMenuFileOpen)
71 EVT_MENU(wxID_EXIT, MyFrame::OnMenuFileExit)
72 EVT_MENU(wxID_HELP, MyFrame::OnMenuHelpAbout)
8b089c5e
JS
73END_EVENT_TABLE()
74
4b764db3 75// MyFrame constructor
8b089c5e 76MyFrame::MyFrame(wxFrame *frame, const wxString& title, const wxPoint& pos,
5cf036d0
DS
77 const wxSize& size, long style)
78 : wxFrame(frame, wxID_ANY, title, pos, size, style)
8b089c5e 79{
3cb332c1 80 SetIcon(wxICON(sample));
4b764db3
JS
81
82 // Make the "File" menu
83 wxMenu *fileMenu = new wxMenu;
84 fileMenu->Append(wxID_OPEN, wxT("&Open..."));
85 fileMenu->AppendSeparator();
86 fileMenu->Append(wxID_EXIT, wxT("E&xit\tALT-X"));
87 // Make the "Help" menu
88 wxMenu *helpMenu = new wxMenu;
2d143b66 89 helpMenu->Append(wxID_HELP, wxT("&About"));
4b764db3
JS
90
91 wxMenuBar *menuBar = new wxMenuBar;
92 menuBar->Append(fileMenu, wxT("&File"));
93 menuBar->Append(helpMenu, wxT("&Help"));
94 SetMenuBar(menuBar);
95
451c13c8
VZ
96 Show(true);
97
4b764db3 98 m_canvas = new TestGLCanvas(this, wxID_ANY, wxDefaultPosition,
451c13c8 99 GetClientSize(), wxSUNKEN_BORDER);
8b089c5e
JS
100}
101
4b764db3
JS
102// File|Open... command
103void MyFrame::OnMenuFileOpen( wxCommandEvent& WXUNUSED(event) )
104{
105 wxString filename = wxFileSelector(wxT("Choose DXF Model"), wxT(""), wxT(""), wxT(""),
106#if wxUSE_ZLIB
107 wxT("DXF Drawing (*.dxf;*.dxf.gz)|*.dxf;*.dxf.gz|All files (*.*)|*.*"),
108#else
109 wxT("DXF Drawing (*.dxf)|*.dxf)|All files (*.*)|*.*"),
110#endif
ff3e84ff 111 wxFD_OPEN);
4b764db3
JS
112 if (!filename.IsEmpty())
113 {
114 m_canvas->LoadDXF(filename);
115 m_canvas->Refresh(false);
116 }
117}
118
119// File|Exit command
120void MyFrame::OnMenuFileExit( wxCommandEvent& WXUNUSED(event) )
8b089c5e 121{
5cf036d0
DS
122 // true is to force the frame to close
123 Close(true);
8b089c5e
JS
124}
125
2d143b66 126// Help|About command
4b764db3
JS
127void MyFrame::OnMenuHelpAbout( wxCommandEvent& WXUNUSED(event) )
128{
129 wxMessageBox(wxT("OpenGL Penguin Sample (c) Robert Roebling, Sandro Sigala et al"));
130}
131
132// ---------------------------------------------------------------------------
133// TestGLCanvas
134// ---------------------------------------------------------------------------
135
8b089c5e
JS
136BEGIN_EVENT_TABLE(TestGLCanvas, wxGLCanvas)
137 EVT_SIZE(TestGLCanvas::OnSize)
138 EVT_PAINT(TestGLCanvas::OnPaint)
139 EVT_ERASE_BACKGROUND(TestGLCanvas::OnEraseBackground)
140 EVT_MOUSE_EVENTS(TestGLCanvas::OnMouse)
141END_EVENT_TABLE()
142
451c13c8
VZ
143TestGLCanvas::TestGLCanvas(wxWindow *parent,
144 wxWindowID id,
145 const wxPoint& pos,
146 const wxSize& size,
147 long style,
148 const wxString& name)
149 : wxGLCanvas(parent, id, NULL, pos, size,
150 style | wxFULL_REPAINT_ON_RESIZE, name)
8b089c5e 151{
451c13c8
VZ
152 // Explicitly create a new rendering context instance for this canvas.
153 m_glRC = new wxGLContext(this);
154
4b764db3
JS
155 m_gldata.initialized = false;
156
157 // initialize view matrix
158 m_gldata.beginx = 0.0f;
159 m_gldata.beginy = 0.0f;
160 m_gldata.zoom = 45.0f;
161 trackball(m_gldata.quat, 0.0f, 0.0f, 0.0f, 0.0f);
8b089c5e
JS
162}
163
5cf036d0 164TestGLCanvas::~TestGLCanvas()
8b089c5e 165{
451c13c8 166 delete m_glRC;
8b089c5e
JS
167}
168
2db98bf5 169void TestGLCanvas::OnPaint( wxPaintEvent& WXUNUSED(event) )
8b089c5e 170{
4b764db3 171 // must always be here
8b089c5e
JS
172 wxPaintDC dc(this);
173
451c13c8 174 SetCurrent(*m_glRC);
5cf036d0
DS
175
176 // Initialize OpenGL
4b764db3 177 if (!m_gldata.initialized)
8b089c5e
JS
178 {
179 InitGL();
4b764db3
JS
180 ResetProjectionMode();
181 m_gldata.initialized = true;
8b089c5e 182 }
5cf036d0 183
5cf036d0
DS
184 // Clear
185 glClearColor( 0.3f, 0.4f, 0.6f, 1.0f );
8b089c5e
JS
186 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
187
5cf036d0 188 // Transformations
8b089c5e 189 glLoadIdentity();
4b764db3
JS
190 glTranslatef( 0.0f, 0.0f, -20.0f );
191 GLfloat m[4][4];
192 build_rotmatrix( m, m_gldata.quat );
8b089c5e
JS
193 glMultMatrixf( &m[0][0] );
194
4b764db3 195 m_renderer.Render();
5cf036d0
DS
196
197 // Flush
8b089c5e
JS
198 glFlush();
199
5cf036d0 200 // Swap
8b089c5e
JS
201 SwapBuffers();
202}
203
451c13c8 204void TestGLCanvas::OnSize(wxSizeEvent& WXUNUSED(event))
8b089c5e 205{
451c13c8
VZ
206 // Reset the OpenGL view aspect.
207 // This is OK only because there is only one canvas that uses the context.
208 // See the cube sample for that case that multiple canvases are made current with one context.
4b764db3 209 ResetProjectionMode();
8b089c5e
JS
210}
211
2db98bf5 212void TestGLCanvas::OnEraseBackground(wxEraseEvent& WXUNUSED(event))
8b089c5e 213{
4b764db3 214 // Do nothing, to avoid flashing on MSW
8b089c5e
JS
215}
216
4b764db3
JS
217// Load the DXF file. If the zlib support is compiled in wxWidgets,
218// supports also the ".dxf.gz" gzip compressed files.
219void TestGLCanvas::LoadDXF(const wxString& filename)
8b089c5e 220{
4b764db3 221 wxFileInputStream stream(filename);
a1b806b9 222 if (stream.IsOk())
4b764db3
JS
223#if wxUSE_ZLIB
224 {
225 if (filename.Right(3).Lower() == wxT(".gz"))
226 {
227 wxZlibInputStream zstream(stream);
228 m_renderer.Load(zstream);
229 } else
230 {
231 m_renderer.Load(stream);
232 }
233 }
234#else
235 {
236 m_renderer.Load(stream);
237 }
238#endif
8b089c5e
JS
239}
240
4b764db3 241void TestGLCanvas::OnMouse(wxMouseEvent& event)
8b089c5e 242{
4b764db3 243 if (event.Dragging())
8b089c5e 244 {
4b764db3 245 wxSize sz(GetClientSize());
5cf036d0 246
8b089c5e
JS
247 /* drag in progress, simulate trackball */
248 float spin_quat[4];
249 trackball(spin_quat,
4b764db3
JS
250 (2.0*m_gldata.beginx - sz.x) / sz.x,
251 (sz.y - 2.0*m_gldata.beginy) / sz.y,
252 (2.0*event.GetX() - sz.x) / sz.x,
253 (sz.y - 2.0*event.GetY()) / sz.y);
5cf036d0 254
4b764db3 255 add_quats(spin_quat, m_gldata.quat, m_gldata.quat);
2f6c54eb 256
8b089c5e 257 /* orientation has changed, redraw mesh */
5cf036d0 258 Refresh(false);
8b089c5e 259 }
2f6c54eb 260
4b764db3
JS
261 m_gldata.beginx = event.GetX();
262 m_gldata.beginy = event.GetY();
8b089c5e
JS
263}
264
5cf036d0 265void TestGLCanvas::InitGL()
8b089c5e 266{
5cf036d0
DS
267 static const GLfloat light0_pos[4] = { -50.0f, 50.0f, 0.0f, 0.0f };
268
269 // white light
270 static const GLfloat light0_color[4] = { 0.6f, 0.6f, 0.6f, 1.0f };
271
272 static const GLfloat light1_pos[4] = { 50.0f, 50.0f, 0.0f, 0.0f };
273
274 // cold blue light
275 static const GLfloat light1_color[4] = { 0.4f, 0.4f, 1.0f, 1.0f };
8b089c5e
JS
276
277 /* remove back faces */
948f48e7 278 glEnable(GL_CULL_FACE);
8b089c5e 279 glEnable(GL_DEPTH_TEST);
5cf036d0 280
8b089c5e
JS
281 /* speedups */
282 glEnable(GL_DITHER);
283 glShadeModel(GL_SMOOTH);
284 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
285 glHint(GL_POLYGON_SMOOTH_HINT, GL_FASTEST);
286
287 /* light */
288 glLightfv(GL_LIGHT0, GL_POSITION, light0_pos);
5cf036d0 289 glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_color);
8b089c5e
JS
290 glLightfv(GL_LIGHT1, GL_POSITION, light1_pos);
291 glLightfv(GL_LIGHT1, GL_DIFFUSE, light1_color);
292 glEnable(GL_LIGHT0);
293 glEnable(GL_LIGHT1);
294 glEnable(GL_LIGHTING);
5cf036d0
DS
295
296 glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
297 glEnable(GL_COLOR_MATERIAL);
8b089c5e
JS
298}
299
4b764db3
JS
300void TestGLCanvas::ResetProjectionMode()
301{
97f85100
VZ
302 if ( !IsShownOnScreen() )
303 return;
304
451c13c8
VZ
305 // This is normally only necessary if there is more than one wxGLCanvas
306 // or more than one wxGLContext in the application.
307 SetCurrent(*m_glRC);
308
4b764db3
JS
309 int w, h;
310 GetClientSize(&w, &h);
451c13c8
VZ
311
312 // It's up to the application code to update the OpenGL viewport settings.
313 // In order to avoid extensive context switching, consider doing this in
314 // OnPaint() rather than here, though.
315 glViewport(0, 0, (GLint) w, (GLint) h);
316
317 glMatrixMode(GL_PROJECTION);
318 glLoadIdentity();
319 gluPerspective(45.0f, (GLfloat)w/h, 1.0, 100.0);
320 glMatrixMode(GL_MODELVIEW);
321 glLoadIdentity();
4b764db3 322}