]>
git.saurik.com Git - wxWidgets.git/blob - samples/opengl/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 -------------------------------------------------------------------*/
80 class ScanCodeDialog
: public wxDialog
83 ScanCodeDialog( wxWindow
* parent
, wxWindowID id
, const int code
,
84 const wxString
&descr
, const wxString
& title
);
87 ScanCodeCtrl
*m_ScanCode
;
88 wxTextCtrl
*m_Description
;
91 ScanCodeDialog::ScanCodeDialog( wxWindow
* parent
, wxWindowID id
,
92 const int code
, const wxString
&descr
, const wxString
& title
)
93 : wxDialog( parent
, id
, title
, wxPoint(-1, -1), wxSize(96*2,76*2) )
95 new wxStaticText( this, -1, "Scancode", wxPoint(4*2,3*2),
97 m_ScanCode
= new ScanCodeCtrl( this, -1, code
, wxPoint(37*2,6*2),
100 new wxStaticText( this, -1, "Description", wxPoint(4*2,24*2),
102 m_Description
= new wxTextCtrl( this, -1, descr
, wxPoint(37*2,27*2),
105 new wxButton( this, wxID_OK
, "Ok", wxPoint(20*2,50*2), wxSize(20*2,13*2) );
106 new wxButton( this, wxID_CANCEL
, "Cancel", wxPoint(44*2,50*2),
110 int ScanCodeDialog::GetValue()
113 wxString buf
= m_ScanCode
->GetValue();
114 sscanf( buf
.c_str(), "%i", &code
);
118 /*----------------------------------------------------------------------
119 Utility function to get the elapsed time (in msec) since a given point
120 in time (in sec) (because current version of wxGetElapsedTime doesn´t
121 works right with glibc-2.1 and linux, at least for me)
122 -----------------------------------------------------------------------*/
123 unsigned long wxStopWatch( unsigned long *sec_base
)
125 unsigned long secs
,msec
;
127 #ifndef __WXMSW__ // think every unice has gettimeofday
129 gettimeofday( &tv
, (struct timezone
*)NULL
);
131 msec
= tv
.tv_usec
/1000;
146 return( (secs
-*sec_base
)*1000 + msec
);
149 /*----------------------------------------------------------------
150 Implementation of Test-GLCanvas
151 -----------------------------------------------------------------*/
153 BEGIN_EVENT_TABLE(TestGLCanvas
, wxGLCanvas
)
154 EVT_SIZE(TestGLCanvas::OnSize
)
155 EVT_PAINT(TestGLCanvas::OnPaint
)
156 EVT_ERASE_BACKGROUND(TestGLCanvas::OnEraseBackground
)
157 EVT_KEY_DOWN( TestGLCanvas::OnKeyDown
)
158 EVT_KEY_UP( TestGLCanvas::OnKeyUp
)
159 EVT_ENTER_WINDOW( TestGLCanvas::OnEnterWindow
)
162 unsigned long TestGLCanvas::m_secbase
= 0;
163 int TestGLCanvas::m_TimeInitialized
= 0;
164 unsigned long TestGLCanvas::m_xsynct
;
165 unsigned long TestGLCanvas::m_gsynct
;
167 TestGLCanvas::TestGLCanvas(wxWindow
*parent
, wxWindowID id
,
168 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
):
169 wxGLCanvas(parent
, (wxGLCanvas
*) NULL
, id
, pos
, size
, style
, name
)
174 m_rright
= WXK_RIGHT
;
177 TestGLCanvas::TestGLCanvas(wxWindow
*parent
, const TestGLCanvas
&other
,
178 wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
, long style
,
179 const wxString
& name
) :
180 wxGLCanvas(parent
, other
.GetContext(), id
, pos
, size
, style
, name
)
183 m_gllist
= other
.m_gllist
; /* share display list */
185 m_rright
= WXK_RIGHT
;
188 TestGLCanvas::~TestGLCanvas()
192 void TestGLCanvas::Render()
197 if (!GetContext()) return;
201 /* 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 glNewList( m_gllist
, GL_COMPILE_AND_EXECUTE
);
215 /* draw six faces of a cube */
217 glNormal3f( 0.0F
, 0.0F
, 1.0F
);
218 glVertex3f( 0.5F
, 0.5F
, 0.5F
); glVertex3f(-0.5F
, 0.5F
, 0.5F
);
219 glVertex3f(-0.5F
,-0.5F
, 0.5F
); glVertex3f( 0.5F
,-0.5F
, 0.5F
);
221 glNormal3f( 0.0F
, 0.0F
,-1.0F
);
222 glVertex3f(-0.5F
,-0.5F
,-0.5F
); glVertex3f(-0.5F
, 0.5F
,-0.5F
);
223 glVertex3f( 0.5F
, 0.5F
,-0.5F
); glVertex3f( 0.5F
,-0.5F
,-0.5F
);
225 glNormal3f( 0.0F
, 1.0F
, 0.0F
);
226 glVertex3f( 0.5F
, 0.5F
, 0.5F
); glVertex3f( 0.5F
, 0.5F
,-0.5F
);
227 glVertex3f(-0.5F
, 0.5F
,-0.5F
); glVertex3f(-0.5F
, 0.5F
, 0.5F
);
229 glNormal3f( 0.0F
,-1.0F
, 0.0F
);
230 glVertex3f(-0.5F
,-0.5F
,-0.5F
); glVertex3f( 0.5F
,-0.5F
,-0.5F
);
231 glVertex3f( 0.5F
,-0.5F
, 0.5F
); glVertex3f(-0.5F
,-0.5F
, 0.5F
);
233 glNormal3f( 1.0F
, 0.0F
, 0.0F
);
234 glVertex3f( 0.5F
, 0.5F
, 0.5F
); glVertex3f( 0.5F
,-0.5F
, 0.5F
);
235 glVertex3f( 0.5F
,-0.5F
,-0.5F
); glVertex3f( 0.5F
, 0.5F
,-0.5F
);
237 glNormal3f(-1.0F
, 0.0F
, 0.0F
);
238 glVertex3f(-0.5F
,-0.5F
,-0.5F
); glVertex3f(-0.5F
,-0.5F
, 0.5F
);
239 glVertex3f(-0.5F
, 0.5F
, 0.5F
); glVertex3f(-0.5F
, 0.5F
,-0.5F
);
245 glCallList( m_gllist
);
251 void TestGLCanvas::OnEnterWindow( wxMouseEvent
& event
)
256 void TestGLCanvas::OnPaint( wxPaintEvent
& event
)
261 void TestGLCanvas::OnSize(wxSizeEvent
& event
)
264 GetClientSize(& width
, & height
);
271 glViewport(0, 0, width
, height
);
275 void TestGLCanvas::OnEraseBackground(wxEraseEvent
& event
)
277 // Do nothing, to avoid flashing.
280 void TestGLCanvas::InitGL()
284 /* set viewing projection */
285 glMatrixMode(GL_PROJECTION
);
286 glFrustum(-0.5F
, 0.5F
, -0.5F
, 0.5F
, 1.0F
, 3.0F
);
288 /* position viewer */
289 glMatrixMode(GL_MODELVIEW
);
290 glTranslatef(0.0F
, 0.0F
, -2.0F
);
292 /* position object */
293 glRotatef(30.0F
, 1.0F
, 0.0F
, 0.0F
);
294 glRotatef(30.0F
, 0.0F
, 1.0F
, 0.0F
);
296 glEnable(GL_DEPTH_TEST
);
297 glEnable(GL_LIGHTING
);
301 GLfloat
TestGLCanvas::CalcRotateSpeed( unsigned long acceltime
)
305 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
);
326 return( t
* (s1
+ s2
) * 135.0f
);
329 void TestGLCanvas::Action( long code
, unsigned long lasttime
,
330 unsigned long acceltime
)
332 GLfloat angle
= CalcRotateAngle( lasttime
, acceltime
);
336 else if (code
== m_rright
)
340 void TestGLCanvas::OnKeyDown( wxKeyEvent
& event
)
342 long evkey
= event
.KeyCode();
343 if (evkey
== 0) return;
345 if (!m_TimeInitialized
)
347 m_TimeInitialized
= 1;
348 m_xsynct
= event
.m_timeStamp
;
349 m_gsynct
= wxStopWatch(&m_secbase
);
357 unsigned long currTime
= event
.m_timeStamp
- m_xsynct
;
362 m_LastRedraw
= m_StartTime
= m_LastTime
= currTime
;
365 if (currTime
>= m_LastRedraw
) // Redraw:
367 Action( m_Key
, m_LastTime
-m_StartTime
, currTime
-m_StartTime
);
369 m_LastRedraw
= wxStopWatch(&m_secbase
) - m_gsynct
;
370 m_LastTime
= currTime
;
376 void TestGLCanvas::OnKeyUp( wxKeyEvent
& event
)
386 void TestGLCanvas::Rotate( GLfloat deg
)
390 glMatrixMode(GL_MODELVIEW
);
391 glRotatef((GLfloat
)deg
, 0.0F
, 0.0F
, 1.0F
);
396 /* -----------------------------------------------------------------------
398 -------------------------------------------------------------------------*/
400 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
401 EVT_MENU(wxID_EXIT
, MyFrame::OnExit
)
402 EVT_MENU( ID_NEW_WINDOW
, MyFrame::OnNewWindow
)
403 EVT_MENU( ID_DEF_ROTATE_LEFT_KEY
, MyFrame::OnDefRotateLeftKey
)
404 EVT_MENU( ID_DEF_ROTATE_RIGHT_KEY
, MyFrame::OnDefRotateRightKey
)
407 // My frame constructor
408 MyFrame::MyFrame(wxFrame
*frame
, const wxString
& title
, const wxPoint
& pos
,
409 const wxSize
& size
, long style
)
410 : wxFrame(frame
, -1, title
, pos
, size
, style
)
415 // Intercept menu commands
416 void MyFrame::OnExit(wxCommandEvent
& event
)
421 void MyFrame::OnNewWindow()
423 MyFrame
*frame
= new MyFrame(NULL
, "Cube OpenGL Demo Clone",
424 wxPoint(50, 50), wxSize(400, 300));
427 frame
->SetIcon(wxIcon("mondrian"));
431 wxMenu
*winMenu
= new wxMenu
;
433 winMenu
->Append(wxID_EXIT
, "&Close");
434 winMenu
->Append(ID_NEW_WINDOW
, "&New" );
435 wxMenuBar
*menuBar
= new wxMenuBar
;
436 menuBar
->Append(winMenu
, "&Window");
438 winMenu
= new wxMenu
;
439 winMenu
->Append(ID_DEF_ROTATE_LEFT_KEY
, "Rotate &left");
440 winMenu
->Append(ID_DEF_ROTATE_RIGHT_KEY
, "Rotate &right");
441 menuBar
->Append(winMenu
, "&Key");
443 frame
->SetMenuBar(menuBar
);
445 frame
->m_canvas
= new TestGLCanvas( frame
, *m_canvas
, -1,
446 wxPoint(0, 0), wxSize(200, 200) );
452 void MyFrame::OnDefRotateLeftKey()
454 ScanCodeDialog
dial( this, -1, m_canvas
->m_rleft
,
455 wxString("Left"), "Define key" );
456 int result
= dial
.ShowModal();
457 if( result
== wxID_OK
)
458 m_canvas
->m_rleft
= dial
.GetValue();
460 void MyFrame::OnDefRotateRightKey()
462 ScanCodeDialog
dial( this, -1, m_canvas
->m_rright
,
463 wxString("Right"), "Define key" );
464 int result
= dial
.ShowModal();
465 if( result
== wxID_OK
)
466 m_canvas
->m_rright
= dial
.GetValue();
469 /*------------------------------------------------------------------
470 Application object ( equivalent to main() )
471 ------------------------------------------------------------------ */
475 bool MyApp::OnInit(void)
477 wxLog::SetTraceMask(wxTraceMessages
);
479 // Create the main frame window
480 MyFrame
*frame
= new MyFrame(NULL
, "Cube OpenGL Demo", wxPoint(50, 50),
484 frame
->SetIcon(wxIcon("mondrian"));
488 wxMenu
*winMenu
= new wxMenu
;
490 winMenu
->Append(wxID_EXIT
, "&Close");
491 winMenu
->Append(ID_NEW_WINDOW
, "&New" );
492 wxMenuBar
*menuBar
= new wxMenuBar
;
493 menuBar
->Append(winMenu
, "&Window");
495 winMenu
= new wxMenu
;
496 winMenu
->Append(ID_DEF_ROTATE_LEFT_KEY
, "Rotate &left");
497 winMenu
->Append(ID_DEF_ROTATE_RIGHT_KEY
, "Rotate &right");
498 menuBar
->Append(winMenu
, "&Key");
500 frame
->SetMenuBar(menuBar
);
502 frame
->m_canvas
= new TestGLCanvas(frame
, -1, wxPoint(0, 0), wxSize(200, 200));