]>
git.saurik.com Git - wxWidgets.git/blob - utils/glcanvas/samples/cube/cube.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxGLCanvas demo program
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation
17 // For compilers that support precompilation, includes "wx.h".
18 #include "wx/wxprec.h"
32 #ifndef __WXMSW__ // for wxStopWatch, see remark below
34 #include <sys/unistd.h>
36 #include <sys/timeb.h>
39 #define ID_NEW_WINDOW 10000
40 #define ID_DEF_ROTATE_LEFT_KEY 10001
41 #define ID_DEF_ROTATE_RIGHT_KEY 10002
43 /*----------------------------------------------------------
44 Control to get a keycode
45 ----------------------------------------------------------*/
46 class ScanCodeCtrl
: public wxTextCtrl
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
);
54 // any class wishing to process wxWindows events must use this macro
57 BEGIN_EVENT_TABLE( ScanCodeCtrl
, wxTextCtrl
)
58 EVT_CHAR( ScanCodeCtrl::OnChar
)
59 EVT_KEY_DOWN( ScanCodeCtrl::OnKeyDown
)
62 ScanCodeCtrl::ScanCodeCtrl( wxWindow
* parent
, wxWindowID id
, int code
,
63 const wxPoint
& pos
, const wxSize
& size
)
64 : wxTextCtrl( parent
, id
, "", pos
, size
)
66 buf
.Printf( "0x%04x", code
);
70 void ScanCodeCtrl::OnKeyDown( wxKeyEvent
& event
)
72 buf
.Printf( "0x%04x", event
.KeyCode() );
76 /*------------------------------------------------------------------
77 Dialog for defining a keypress
78 -------------------------------------------------------------------*/
79 class ScanCodeDialog
: public wxDialog
82 ScanCodeDialog( wxWindow
* parent
, wxWindowID id
, const int code
,
83 const wxString
&descr
, const wxString
& title
);
86 ScanCodeCtrl
*m_ScanCode
;
87 wxTextCtrl
*m_Description
;
88 // any class wishing to process wxWindows events must use this macro
92 BEGIN_EVENT_TABLE( ScanCodeDialog
, wxDialog
)
96 /* ---------------------------------------------------------------- */
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) )
102 new wxStaticText( this, -1, "Scancode", wxPoint(4*2,3*2),
104 m_ScanCode
= new ScanCodeCtrl( this, -1, code
, wxPoint(37*2,6*2),
107 new wxStaticText( this, -1, "Description", wxPoint(4*2,24*2),
109 m_Description
= new wxTextCtrl( this, -1, descr
, wxPoint(37*2,27*2),
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),
117 int ScanCodeDialog::GetValue()
120 wxString buf
= m_ScanCode
->GetValue();
121 sscanf( buf
.c_str(), "%i", &code
);
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
)
132 unsigned long secs
,msec
;
134 #ifndef __WXMSW__ // think every unice has gettimeofday
136 gettimeofday( &tv
, (struct timezone
*)NULL
);
138 msec
= tv
.tv_usec
/1000;
149 return( (secs
-*sec_base
)*1000 + msec
);
152 /*----------------------------------------------------------------
153 Implementation of Test-GLCanvas
154 -----------------------------------------------------------------*/
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
)
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
;
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
)
178 m_rright
= WXK_RIGHT
;
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
)
186 m_gllist
= other
.m_gllist
; /* share display list */
188 m_rright
= WXK_RIGHT
;
190 TestGLCanvas::~TestGLCanvas(void)
194 void TestGLCanvas::Render( void )
199 if (!GetContext()) return;
202 /* init OpenGL once, but after SetCurrent */
208 /* clear color and depth buffers */
209 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
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 */
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
);
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
);
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
);
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
);
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
);
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
);
246 glCallList( m_gllist
);
252 void TestGLCanvas::OnEnterWindow( wxMouseEvent
& event
)
257 void TestGLCanvas::OnPaint( wxPaintEvent
& event
)
262 void TestGLCanvas::OnSize(wxSizeEvent
& event
)
265 GetClientSize(& width
, & height
);
272 glViewport(0, 0, width
, height
);
276 void TestGLCanvas::OnEraseBackground(wxEraseEvent
& event
)
278 // Do nothing, to avoid flashing.
281 void TestGLCanvas::InitGL(void)
285 /* set viewing projection */
286 glMatrixMode(GL_PROJECTION
);
287 glFrustum(-0.5F
, 0.5F
, -0.5F
, 0.5F
, 1.0F
, 3.0F
);
289 /* position viewer */
290 glMatrixMode(GL_MODELVIEW
);
291 glTranslatef(0.0F
, 0.0F
, -2.0F
);
293 /* position object */
294 glRotatef(30.0F
, 1.0F
, 0.0F
, 0.0F
);
295 glRotatef(30.0F
, 0.0F
, 1.0F
, 0.0F
);
297 glEnable(GL_DEPTH_TEST
);
298 glEnable(GL_LIGHTING
);
302 GLfloat
TestGLCanvas::CalcRotateSpeed( unsigned long acceltime
)
306 t
= ((GLfloat
)acceltime
) / 1000.0f
;
317 GLfloat
TestGLCanvas::CalcRotateAngle( unsigned long lasttime
,
318 unsigned long acceltime
)
322 t
= ((GLfloat
)(acceltime
- lasttime
)) / 1000.0f
;
323 s1
= CalcRotateSpeed( lasttime
);
324 s2
= CalcRotateSpeed( acceltime
);
325 return( t
* (s1
+ s2
) * 135.0f
);
327 void TestGLCanvas::Action( long code
, unsigned long lasttime
,
328 unsigned long acceltime
)
330 GLfloat angle
= CalcRotateAngle( lasttime
, acceltime
);
332 if( code
== m_rleft
) Rotate( angle
);
333 else if( code
== m_rright
) Rotate( -angle
);
336 void TestGLCanvas::OnKeyDown( wxKeyEvent
& event
)
338 long evkey
= event
.KeyCode();
339 if( evkey
== 0 ) return;
341 if( !m_TimeInitialized
)
343 m_TimeInitialized
= 1;
344 m_xsynct
= event
.m_timeStamp
;
345 m_gsynct
= wxStopWatch(&m_secbase
);
353 unsigned long currTime
= event
.m_timeStamp
- m_xsynct
;
358 m_LastRedraw
= m_StartTime
= m_LastTime
= currTime
;
361 if( currTime
>= m_LastRedraw
) // Redraw:
363 Action( m_Key
, m_LastTime
-m_StartTime
, currTime
-m_StartTime
);
365 m_LastRedraw
= wxStopWatch(&m_secbase
) - m_gsynct
;
366 m_LastTime
= currTime
;
370 void TestGLCanvas::OnKeyUp( wxKeyEvent
& event
)
378 void TestGLCanvas::Rotate( GLfloat deg
)
382 glMatrixMode(GL_MODELVIEW
);
383 glRotatef((GLfloat
)deg
, 0.0F
, 0.0F
, 1.0F
);
388 /* -----------------------------------------------------------------------
390 -------------------------------------------------------------------------*/
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
)
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
)
407 // Intercept menu commands
408 void MyFrame::OnExit(wxCommandEvent
& event
)
413 void MyFrame::OnNewWindow()
415 MyFrame
*frame
= new MyFrame(NULL
, "Cube OpenGL Demo Clone",
416 wxPoint(50, 50), wxSize(400, 300));
419 frame
->SetIcon(wxIcon("mondrian"));
423 wxMenu
*winMenu
= new wxMenu
;
425 winMenu
->Append(wxID_EXIT
, "&Close");
426 winMenu
->Append(ID_NEW_WINDOW
, "&New" );
427 wxMenuBar
*menuBar
= new wxMenuBar
;
428 menuBar
->Append(winMenu
, "&Window");
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");
435 frame
->SetMenuBar(menuBar
);
437 frame
->m_canvas
= new TestGLCanvas( frame
, *m_canvas
, -1,
438 wxPoint(0, 0), wxSize(200, 200) );
444 void MyFrame::OnDefRotateLeftKey()
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();
452 void MyFrame::OnDefRotateRightKey()
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();
461 /*------------------------------------------------------------------
462 Application object ( equivalent to main() )
463 ------------------------------------------------------------------ */
467 bool MyApp::OnInit(void)
469 wxLog::SetTraceMask(wxTraceMessages
);
471 // Create the main frame window
472 MyFrame
*frame
= new MyFrame(NULL
, "Cube OpenGL Demo", wxPoint(50, 50),
476 frame
->SetIcon(wxIcon("mondrian"));
480 wxMenu
*winMenu
= new wxMenu
;
482 winMenu
->Append(wxID_EXIT
, "&Close");
483 winMenu
->Append(ID_NEW_WINDOW
, "&New" );
484 wxMenuBar
*menuBar
= new wxMenuBar
;
485 menuBar
->Append(winMenu
, "&Window");
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");
492 frame
->SetMenuBar(menuBar
);
494 frame
->m_canvas
= new TestGLCanvas(frame
, -1, wxPoint(0, 0), wxSize(200, 200));