]>
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"
31 #error Please set wxUSE_GLCANVAS to 1 in setup.h.
36 #ifndef __WXMSW__ // for wxStopWatch, see remark below
37 #if defined(__WXMAC__) && !defined(__DARWIN__)
42 #include <sys/unistd.h>
45 #include <sys/timeb.h>
48 #define ID_NEW_WINDOW 10000
49 #define ID_DEF_ROTATE_LEFT_KEY 10001
50 #define ID_DEF_ROTATE_RIGHT_KEY 10002
52 /*----------------------------------------------------------
53 Control to get a keycode
54 ----------------------------------------------------------*/
55 class ScanCodeCtrl
: public wxTextCtrl
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
);
63 // any class wishing to process wxWindows events must use this macro
66 BEGIN_EVENT_TABLE( ScanCodeCtrl
, wxTextCtrl
)
67 EVT_CHAR( ScanCodeCtrl::OnChar
)
68 EVT_KEY_DOWN( ScanCodeCtrl::OnKeyDown
)
71 ScanCodeCtrl::ScanCodeCtrl( wxWindow
* parent
, wxWindowID id
, int code
,
72 const wxPoint
& pos
, const wxSize
& size
)
73 : wxTextCtrl( parent
, id
, "", pos
, size
)
75 buf
.Printf( "0x%04x", code
);
79 void ScanCodeCtrl::OnKeyDown( wxKeyEvent
& event
)
81 buf
.Printf( "0x%04x", event
.KeyCode() );
85 /*------------------------------------------------------------------
86 Dialog for defining a keypress
87 -------------------------------------------------------------------*/
89 class ScanCodeDialog
: public wxDialog
92 ScanCodeDialog( wxWindow
* parent
, wxWindowID id
, const int code
,
93 const wxString
&descr
, const wxString
& title
);
96 ScanCodeCtrl
*m_ScanCode
;
97 wxTextCtrl
*m_Description
;
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) )
104 new wxStaticText( this, -1, "Scancode", wxPoint(4*2,3*2),
106 m_ScanCode
= new ScanCodeCtrl( this, -1, code
, wxPoint(37*2,6*2),
109 new wxStaticText( this, -1, "Description", wxPoint(4*2,24*2),
111 m_Description
= new wxTextCtrl( this, -1, descr
, wxPoint(37*2,27*2),
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),
119 int ScanCodeDialog::GetValue()
122 wxString buf
= m_ScanCode
->GetValue();
123 sscanf( buf
.c_str(), "%i", &code
);
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
)
134 unsigned long secs
,msec
;
136 #ifndef __WXMSW__ // think every unice has gettimeofday
138 gettimeofday( &tv
, (struct timezone
*)NULL
);
140 msec
= tv
.tv_usec
/1000;
155 return( (secs
-*sec_base
)*1000 + msec
);
158 /*----------------------------------------------------------------
159 Implementation of Test-GLCanvas
160 -----------------------------------------------------------------*/
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
)
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
;
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
)
183 m_rright
= WXK_RIGHT
;
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
)
192 m_gllist
= other
.m_gllist
; /* share display list */
194 m_rright
= WXK_RIGHT
;
197 TestGLCanvas::~TestGLCanvas()
201 void TestGLCanvas::Render()
206 if (!GetContext()) return;
210 /* init OpenGL once, but after SetCurrent */
217 /* clear color and depth buffers */
218 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
222 m_gllist
= glGenLists( 1 );
223 glNewList( m_gllist
, GL_COMPILE_AND_EXECUTE
);
224 /* draw six faces of a cube */
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
);
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
);
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
);
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
);
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
);
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
);
254 glCallList( m_gllist
);
260 void TestGLCanvas::OnEnterWindow( wxMouseEvent
& event
)
265 void TestGLCanvas::OnPaint( wxPaintEvent
& event
)
270 void TestGLCanvas::OnSize(wxSizeEvent
& event
)
273 GetClientSize(& width
, & height
);
280 glViewport(0, 0, width
, height
);
284 void TestGLCanvas::OnEraseBackground(wxEraseEvent
& event
)
286 // Do nothing, to avoid flashing.
289 void TestGLCanvas::InitGL()
293 /* set viewing projection */
294 glMatrixMode(GL_PROJECTION
);
295 glFrustum(-0.5F
, 0.5F
, -0.5F
, 0.5F
, 1.0F
, 3.0F
);
297 /* position viewer */
298 glMatrixMode(GL_MODELVIEW
);
299 glTranslatef(0.0F
, 0.0F
, -2.0F
);
301 /* position object */
302 glRotatef(30.0F
, 1.0F
, 0.0F
, 0.0F
);
303 glRotatef(30.0F
, 0.0F
, 1.0F
, 0.0F
);
305 glEnable(GL_DEPTH_TEST
);
306 glEnable(GL_LIGHTING
);
310 GLfloat
TestGLCanvas::CalcRotateSpeed( unsigned long acceltime
)
314 t
= ((GLfloat
)acceltime
) / 1000.0f
;
326 GLfloat
TestGLCanvas::CalcRotateAngle( unsigned long lasttime
,
327 unsigned long acceltime
)
331 t
= ((GLfloat
)(acceltime
- lasttime
)) / 1000.0f
;
332 s1
= CalcRotateSpeed( lasttime
);
333 s2
= CalcRotateSpeed( acceltime
);
335 return( t
* (s1
+ s2
) * 135.0f
);
338 void TestGLCanvas::Action( long code
, unsigned long lasttime
,
339 unsigned long acceltime
)
341 GLfloat angle
= CalcRotateAngle( lasttime
, acceltime
);
345 else if (code
== m_rright
)
349 void TestGLCanvas::OnKeyDown( wxKeyEvent
& event
)
351 long evkey
= event
.KeyCode();
352 if (evkey
== 0) return;
354 if (!m_TimeInitialized
)
356 m_TimeInitialized
= 1;
357 m_xsynct
= event
.m_timeStamp
;
358 m_gsynct
= wxStopWatch(&m_secbase
);
366 unsigned long currTime
= event
.m_timeStamp
- m_xsynct
;
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)
374 m_LastRedraw
= m_StartTime
= m_LastTime
= currTime
;
377 if (currTime
>= m_LastRedraw
) // Redraw:
379 Action( m_Key
, m_LastTime
-m_StartTime
, currTime
-m_StartTime
);
381 m_LastRedraw
= wxStopWatch(&m_secbase
) - m_gsynct
;
382 m_LastTime
= currTime
;
388 void TestGLCanvas::OnKeyUp( wxKeyEvent
& event
)
398 void TestGLCanvas::Rotate( GLfloat deg
)
402 glMatrixMode(GL_MODELVIEW
);
403 glRotatef((GLfloat
)deg
, 0.0F
, 0.0F
, 1.0F
);
408 /* -----------------------------------------------------------------------
410 -------------------------------------------------------------------------*/
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
)
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
)
427 // Intercept menu commands
428 void MyFrame::OnExit(wxCommandEvent
& event
)
433 void MyFrame::OnNewWindow()
435 MyFrame
*frame
= new MyFrame(NULL
, "Cube OpenGL Demo Clone",
436 wxPoint(50, 50), wxSize(400, 300));
439 frame
->SetIcon(wxIcon("mondrian"));
443 wxMenu
*winMenu
= new wxMenu
;
445 winMenu
->Append(wxID_EXIT
, "&Close");
446 winMenu
->Append(ID_NEW_WINDOW
, "&New" );
447 wxMenuBar
*menuBar
= new wxMenuBar
;
448 menuBar
->Append(winMenu
, "&Window");
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");
455 frame
->SetMenuBar(menuBar
);
457 frame
->m_canvas
= new TestGLCanvas( frame
, *m_canvas
, -1,
458 wxPoint(0, 0), wxSize(200, 200) );
464 void MyFrame::OnDefRotateLeftKey()
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();
472 void MyFrame::OnDefRotateRightKey()
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();
481 /*------------------------------------------------------------------
482 Application object ( equivalent to main() )
483 ------------------------------------------------------------------ */
487 bool MyApp::OnInit(void)
489 wxLog::SetTraceMask(wxTraceMessages
);
491 // Create the main frame window
492 MyFrame
*frame
= new MyFrame(NULL
, "Cube OpenGL Demo", wxPoint(50, 50),
496 frame
->SetIcon(wxIcon("mondrian"));
500 wxMenu
*winMenu
= new wxMenu
;
502 winMenu
->Append(wxID_EXIT
, "&Close");
503 winMenu
->Append(ID_NEW_WINDOW
, "&New" );
504 wxMenuBar
*menuBar
= new wxMenuBar
;
505 menuBar
->Append(winMenu
, "&Window");
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");
512 frame
->SetMenuBar(menuBar
);
514 frame
->m_canvas
= new TestGLCanvas(frame
, -1, wxPoint(0, 0), wxSize(200, 200));