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