]> git.saurik.com Git - wxWidgets.git/blob - samples/opengl/cube/cube.cpp
draw the selected items in another colour when the control doesn't have focus, a...
[wxWidgets.git] / samples / opengl / cube / cube.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: cube.cpp
3 // Purpose: wxGLCanvas demo program
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation
14 #pragma interface
15 #endif
16
17 // For compilers that support precompilation, includes "wx.h".
18 #include "wx/wxprec.h"
19
20 #ifdef __BORLANDC__
21 #pragma hdrstop
22 #endif
23
24 #ifndef WX_PRECOMP
25 #include "wx/wx.h"
26 #endif
27
28 #include "wx/log.h"
29
30 #if !wxUSE_GLCANVAS
31 #error Please set wxUSE_GLCANVAS to 1 in setup.h.
32 #endif
33
34 #include "cube.h"
35
36 #ifndef __WXMSW__ // for wxStopWatch, see remark below
37 #if defined(__WXMAC__) && !defined(__DARWIN__)
38 #include <utime.h>
39 #include <unistd.h>
40 #else
41 #include <sys/time.h>
42 #include <sys/unistd.h>
43 #endif
44 #else
45 #include <sys/timeb.h>
46 #endif
47
48 #define ID_NEW_WINDOW 10000
49 #define ID_DEF_ROTATE_LEFT_KEY 10001
50 #define ID_DEF_ROTATE_RIGHT_KEY 10002
51
52 /*----------------------------------------------------------
53 Control to get a keycode
54 ----------------------------------------------------------*/
55 class ScanCodeCtrl : public wxTextCtrl
56 {
57 public:
58 ScanCodeCtrl( wxWindow* parent, wxWindowID id, int code,
59 const wxPoint& pos, const wxSize& size );
60 void OnChar( wxKeyEvent& event ) { } /* do nothing */
61 void OnKeyDown(wxKeyEvent& event);
62 private:
63 // any class wishing to process wxWindows events must use this macro
64 DECLARE_EVENT_TABLE()
65 };
66 BEGIN_EVENT_TABLE( ScanCodeCtrl, wxTextCtrl )
67 EVT_CHAR( ScanCodeCtrl::OnChar )
68 EVT_KEY_DOWN( ScanCodeCtrl::OnKeyDown )
69 END_EVENT_TABLE()
70
71 ScanCodeCtrl::ScanCodeCtrl( wxWindow* parent, wxWindowID id, int code,
72 const wxPoint& pos, const wxSize& size )
73 : wxTextCtrl( parent, id, "", pos, size )
74 { wxString buf;
75 buf.Printf( "0x%04x", code );
76 SetValue( buf );
77 }
78
79 void ScanCodeCtrl::OnKeyDown( wxKeyEvent& event )
80 { wxString buf;
81 buf.Printf( "0x%04x", event.KeyCode() );
82 SetValue( buf );
83 }
84
85 /*------------------------------------------------------------------
86 Dialog for defining a keypress
87 -------------------------------------------------------------------*/
88
89 class ScanCodeDialog : public wxDialog
90 {
91 public:
92 ScanCodeDialog( wxWindow* parent, wxWindowID id, const int code,
93 const wxString &descr, const wxString& title );
94 int GetValue();
95 private:
96 ScanCodeCtrl *m_ScanCode;
97 wxTextCtrl *m_Description;
98 };
99
100 ScanCodeDialog::ScanCodeDialog( wxWindow* parent, wxWindowID id,
101 const int code, const wxString &descr, const wxString& title )
102 : wxDialog( parent, id, title, wxPoint(-1, -1), wxSize(96*2,76*2) )
103 {
104 new wxStaticText( this, -1, "Scancode", wxPoint(4*2,3*2),
105 wxSize(31*2,12*2) );
106 m_ScanCode = new ScanCodeCtrl( this, -1, code, wxPoint(37*2,6*2),
107 wxSize(53*2,14*2) );
108
109 new wxStaticText( this, -1, "Description", wxPoint(4*2,24*2),
110 wxSize(32*2,12*2) );
111 m_Description = new wxTextCtrl( this, -1, descr, wxPoint(37*2,27*2),
112 wxSize(53*2,14*2) );
113
114 new wxButton( this, wxID_OK, "Ok", wxPoint(20*2,50*2), wxSize(20*2,13*2) );
115 new wxButton( this, wxID_CANCEL, "Cancel", wxPoint(44*2,50*2),
116 wxSize(25*2,13*2) );
117 }
118
119 int ScanCodeDialog::GetValue()
120 {
121 int code;
122 wxString buf = m_ScanCode->GetValue();
123 sscanf( buf.c_str(), "%i", &code );
124 return( code );
125 }
126
127 /*----------------------------------------------------------------------
128 Utility function to get the elapsed time (in msec) since a given point
129 in time (in sec) (because current version of wxGetElapsedTime doesn´t
130 works right with glibc-2.1 and linux, at least for me)
131 -----------------------------------------------------------------------*/
132 unsigned long wxStopWatch( unsigned long *sec_base )
133 {
134 unsigned long secs,msec;
135
136 #ifndef __WXMSW__ // think every unice has gettimeofday
137 struct timeval tv;
138 gettimeofday( &tv, (struct timezone *)NULL );
139 secs = tv.tv_sec;
140 msec = tv.tv_usec/1000;
141 #else
142 struct timeb tb;
143
144 ftime( &tb );
145
146 secs = tb.time;
147
148 msec = tb.millitm;
149
150 #endif
151
152 if( *sec_base == 0 )
153 *sec_base = secs;
154
155 return( (secs-*sec_base)*1000 + msec );
156 }
157
158 /*----------------------------------------------------------------
159 Implementation of Test-GLCanvas
160 -----------------------------------------------------------------*/
161
162 BEGIN_EVENT_TABLE(TestGLCanvas, wxGLCanvas)
163 EVT_SIZE(TestGLCanvas::OnSize)
164 EVT_PAINT(TestGLCanvas::OnPaint)
165 EVT_ERASE_BACKGROUND(TestGLCanvas::OnEraseBackground)
166 EVT_KEY_DOWN( TestGLCanvas::OnKeyDown )
167 EVT_KEY_UP( TestGLCanvas::OnKeyUp )
168 EVT_ENTER_WINDOW( TestGLCanvas::OnEnterWindow )
169 END_EVENT_TABLE()
170
171 unsigned long TestGLCanvas::m_secbase = 0;
172 int TestGLCanvas::m_TimeInitialized = 0;
173 unsigned long TestGLCanvas::m_xsynct;
174 unsigned long TestGLCanvas::m_gsynct;
175
176 TestGLCanvas::TestGLCanvas(wxWindow *parent, wxWindowID id,
177 const wxPoint& pos, const wxSize& size, long style, const wxString& name):
178 wxGLCanvas(parent, (wxGLCanvas*) NULL, id, pos, size, style, name )
179 {
180 m_init = FALSE;
181 m_gllist = 0;
182 m_rleft = WXK_LEFT;
183 m_rright = WXK_RIGHT;
184 }
185
186 TestGLCanvas::TestGLCanvas(wxWindow *parent, const TestGLCanvas &other,
187 wxWindowID id, const wxPoint& pos, const wxSize& size, long style,
188 const wxString& name ) :
189 wxGLCanvas(parent, other.GetContext(), id, pos, size, style, name )
190 {
191 m_init = FALSE;
192 m_gllist = other.m_gllist; /* share display list */
193 m_rleft = WXK_LEFT;
194 m_rright = WXK_RIGHT;
195 }
196
197 TestGLCanvas::~TestGLCanvas()
198 {
199 }
200
201 void TestGLCanvas::Render()
202 {
203 wxPaintDC dc(this);
204
205 #ifndef __WXMOTIF__
206 if (!GetContext()) return;
207 #endif
208
209 SetCurrent();
210 /* init OpenGL once, but after SetCurrent */
211 if (!m_init)
212 {
213 InitGL();
214 m_init = TRUE;
215 }
216
217 /* clear color and depth buffers */
218 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
219
220 if( m_gllist == 0 )
221 {
222 m_gllist = glGenLists( 1 );
223 glNewList( m_gllist, GL_COMPILE_AND_EXECUTE );
224 /* draw six faces of a cube */
225 glBegin(GL_QUADS);
226 glNormal3f( 0.0F, 0.0F, 1.0F);
227 glVertex3f( 0.5F, 0.5F, 0.5F); glVertex3f(-0.5F, 0.5F, 0.5F);
228 glVertex3f(-0.5F,-0.5F, 0.5F); glVertex3f( 0.5F,-0.5F, 0.5F);
229
230 glNormal3f( 0.0F, 0.0F,-1.0F);
231 glVertex3f(-0.5F,-0.5F,-0.5F); glVertex3f(-0.5F, 0.5F,-0.5F);
232 glVertex3f( 0.5F, 0.5F,-0.5F); glVertex3f( 0.5F,-0.5F,-0.5F);
233
234 glNormal3f( 0.0F, 1.0F, 0.0F);
235 glVertex3f( 0.5F, 0.5F, 0.5F); glVertex3f( 0.5F, 0.5F,-0.5F);
236 glVertex3f(-0.5F, 0.5F,-0.5F); glVertex3f(-0.5F, 0.5F, 0.5F);
237
238 glNormal3f( 0.0F,-1.0F, 0.0F);
239 glVertex3f(-0.5F,-0.5F,-0.5F); glVertex3f( 0.5F,-0.5F,-0.5F);
240 glVertex3f( 0.5F,-0.5F, 0.5F); glVertex3f(-0.5F,-0.5F, 0.5F);
241
242 glNormal3f( 1.0F, 0.0F, 0.0F);
243 glVertex3f( 0.5F, 0.5F, 0.5F); glVertex3f( 0.5F,-0.5F, 0.5F);
244 glVertex3f( 0.5F,-0.5F,-0.5F); glVertex3f( 0.5F, 0.5F,-0.5F);
245
246 glNormal3f(-1.0F, 0.0F, 0.0F);
247 glVertex3f(-0.5F,-0.5F,-0.5F); glVertex3f(-0.5F,-0.5F, 0.5F);
248 glVertex3f(-0.5F, 0.5F, 0.5F); glVertex3f(-0.5F, 0.5F,-0.5F);
249 glEnd();
250
251 glEndList();
252 }
253 else
254 glCallList( m_gllist );
255
256 glFlush();
257 SwapBuffers();
258 }
259
260 void TestGLCanvas::OnEnterWindow( wxMouseEvent& event )
261 {
262 SetFocus();
263 }
264
265 void TestGLCanvas::OnPaint( wxPaintEvent& event )
266 {
267 Render();
268 }
269
270 void TestGLCanvas::OnSize(wxSizeEvent& event)
271 {
272 int width, height;
273 GetClientSize(& width, & height);
274
275 #ifndef __WXMOTIF__
276 if (GetContext())
277 #endif
278 {
279 SetCurrent();
280 glViewport(0, 0, width, height);
281 }
282 }
283
284 void TestGLCanvas::OnEraseBackground(wxEraseEvent& event)
285 {
286 // Do nothing, to avoid flashing.
287 }
288
289 void TestGLCanvas::InitGL()
290 {
291 SetCurrent();
292
293 /* set viewing projection */
294 glMatrixMode(GL_PROJECTION);
295 glFrustum(-0.5F, 0.5F, -0.5F, 0.5F, 1.0F, 3.0F);
296
297 /* position viewer */
298 glMatrixMode(GL_MODELVIEW);
299 glTranslatef(0.0F, 0.0F, -2.0F);
300
301 /* position object */
302 glRotatef(30.0F, 1.0F, 0.0F, 0.0F);
303 glRotatef(30.0F, 0.0F, 1.0F, 0.0F);
304
305 glEnable(GL_DEPTH_TEST);
306 glEnable(GL_LIGHTING);
307 glEnable(GL_LIGHT0);
308 }
309
310 GLfloat TestGLCanvas::CalcRotateSpeed( unsigned long acceltime )
311 {
312 GLfloat t,v;
313
314 t = ((GLfloat)acceltime) / 1000.0f;
315
316 if( t < 0.5f )
317 v = t;
318 else if( t < 1.0f )
319 v = t * (2.0f - t);
320 else
321 v = 0.75f;
322
323 return(v);
324 }
325
326 GLfloat TestGLCanvas::CalcRotateAngle( unsigned long lasttime,
327 unsigned long acceltime )
328 {
329 GLfloat t,s1,s2;
330
331 t = ((GLfloat)(acceltime - lasttime)) / 1000.0f;
332 s1 = CalcRotateSpeed( lasttime );
333 s2 = CalcRotateSpeed( acceltime );
334
335 return( t * (s1 + s2) * 135.0f );
336 }
337
338 void TestGLCanvas::Action( long code, unsigned long lasttime,
339 unsigned long acceltime )
340 {
341 GLfloat angle = CalcRotateAngle( lasttime, acceltime );
342
343 if (code == m_rleft)
344 Rotate( angle );
345 else if (code == m_rright)
346 Rotate( -angle );
347 }
348
349 void TestGLCanvas::OnKeyDown( wxKeyEvent& event )
350 {
351 long evkey = event.KeyCode();
352 if (evkey == 0) return;
353
354 if (!m_TimeInitialized)
355 {
356 m_TimeInitialized = 1;
357 m_xsynct = event.m_timeStamp;
358 m_gsynct = wxStopWatch(&m_secbase);
359
360 m_Key = evkey;
361 m_StartTime = 0;
362 m_LastTime = 0;
363 m_LastRedraw = 0;
364 }
365
366 unsigned long currTime = event.m_timeStamp - m_xsynct;
367
368 // we have to test for m_Key != 0 because otherwise the test would be
369 // always true because it is set to 0 in OnKeyUp() below - I don't know
370 // why is it like this, just fixing blindly (VZ)
371 if (evkey != m_Key && m_Key != 0)
372 {
373 m_Key = evkey;
374 m_LastRedraw = m_StartTime = m_LastTime = currTime;
375 }
376
377 if (currTime >= m_LastRedraw) // Redraw:
378 {
379 Action( m_Key, m_LastTime-m_StartTime, currTime-m_StartTime );
380
381 m_LastRedraw = wxStopWatch(&m_secbase) - m_gsynct;
382 m_LastTime = currTime;
383 }
384
385 event.Skip();
386 }
387
388 void TestGLCanvas::OnKeyUp( wxKeyEvent& event )
389 {
390 m_Key = 0;
391 m_StartTime = 0;
392 m_LastTime = 0;
393 m_LastRedraw = 0;
394
395 event.Skip();
396 }
397
398 void TestGLCanvas::Rotate( GLfloat deg )
399 {
400 SetCurrent();
401
402 glMatrixMode(GL_MODELVIEW);
403 glRotatef((GLfloat)deg, 0.0F, 0.0F, 1.0F);
404 Refresh(FALSE);
405 }
406
407
408 /* -----------------------------------------------------------------------
409 Main Window
410 -------------------------------------------------------------------------*/
411
412 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
413 EVT_MENU(wxID_EXIT, MyFrame::OnExit)
414 EVT_MENU( ID_NEW_WINDOW, MyFrame::OnNewWindow)
415 EVT_MENU( ID_DEF_ROTATE_LEFT_KEY, MyFrame::OnDefRotateLeftKey)
416 EVT_MENU( ID_DEF_ROTATE_RIGHT_KEY, MyFrame::OnDefRotateRightKey)
417 END_EVENT_TABLE()
418
419 // My frame constructor
420 MyFrame::MyFrame(wxFrame *frame, const wxString& title, const wxPoint& pos,
421 const wxSize& size, long style)
422 : wxFrame(frame, -1, title, pos, size, style)
423 {
424 m_canvas = NULL;
425 }
426
427 // Intercept menu commands
428 void MyFrame::OnExit(wxCommandEvent& event)
429 {
430 Destroy();
431 }
432
433 void MyFrame::OnNewWindow()
434 {
435 MyFrame *frame = new MyFrame(NULL, "Cube OpenGL Demo Clone",
436 wxPoint(50, 50), wxSize(400, 300));
437 // Give it an icon
438 #ifdef __WXMSW__
439 frame->SetIcon(wxIcon("mondrian"));
440 #endif
441
442 // Make a menubar
443 wxMenu *winMenu = new wxMenu;
444
445 winMenu->Append(wxID_EXIT, "&Close");
446 winMenu->Append(ID_NEW_WINDOW, "&New" );
447 wxMenuBar *menuBar = new wxMenuBar;
448 menuBar->Append(winMenu, "&Window");
449
450 winMenu = new wxMenu;
451 winMenu->Append(ID_DEF_ROTATE_LEFT_KEY, "Rotate &left");
452 winMenu->Append(ID_DEF_ROTATE_RIGHT_KEY, "Rotate &right");
453 menuBar->Append(winMenu, "&Key");
454
455 frame->SetMenuBar(menuBar);
456
457 frame->m_canvas = new TestGLCanvas( frame, *m_canvas, -1,
458 wxPoint(0, 0), wxSize(200, 200) );
459
460 // Show the frame
461 frame->Show(TRUE);
462 }
463
464 void MyFrame::OnDefRotateLeftKey()
465 {
466 ScanCodeDialog dial( this, -1, m_canvas->m_rleft,
467 wxString("Left"), "Define key" );
468 int result = dial.ShowModal();
469 if( result == wxID_OK )
470 m_canvas->m_rleft = dial.GetValue();
471 }
472 void MyFrame::OnDefRotateRightKey()
473 {
474 ScanCodeDialog dial( this, -1, m_canvas->m_rright,
475 wxString("Right"), "Define key" );
476 int result = dial.ShowModal();
477 if( result == wxID_OK )
478 m_canvas->m_rright = dial.GetValue();
479 }
480
481 /*------------------------------------------------------------------
482 Application object ( equivalent to main() )
483 ------------------------------------------------------------------ */
484
485 IMPLEMENT_APP(MyApp)
486
487 bool MyApp::OnInit(void)
488 {
489 wxLog::SetTraceMask(wxTraceMessages);
490
491 // Create the main frame window
492 MyFrame *frame = new MyFrame(NULL, "Cube OpenGL Demo", wxPoint(50, 50),
493 wxSize(400, 300));
494 // Give it an icon
495 #ifdef wx_msw
496 frame->SetIcon(wxIcon("mondrian"));
497 #endif
498
499 // Make a menubar
500 wxMenu *winMenu = new wxMenu;
501
502 winMenu->Append(wxID_EXIT, "&Close");
503 winMenu->Append(ID_NEW_WINDOW, "&New" );
504 wxMenuBar *menuBar = new wxMenuBar;
505 menuBar->Append(winMenu, "&Window");
506
507 winMenu = new wxMenu;
508 winMenu->Append(ID_DEF_ROTATE_LEFT_KEY, "Rotate &left");
509 winMenu->Append(ID_DEF_ROTATE_RIGHT_KEY, "Rotate &right");
510 menuBar->Append(winMenu, "&Key");
511
512 frame->SetMenuBar(menuBar);
513
514 frame->m_canvas = new TestGLCanvas(frame, -1, wxPoint(0, 0), wxSize(200, 200));
515
516 // Show the frame
517 frame->Show(TRUE);
518
519 return TRUE;
520 }