+void TestGLCanvas::InitGL()
+{
+ SetCurrent();
+
+ /* set viewing projection */
+ glMatrixMode(GL_PROJECTION);
+ glFrustum(-0.5F, 0.5F, -0.5F, 0.5F, 1.0F, 3.0F);
+
+ /* position viewer */
+ glMatrixMode(GL_MODELVIEW);
+ glTranslatef(0.0F, 0.0F, -2.0F);
+
+ /* position object */
+ glRotatef(30.0F, 1.0F, 0.0F, 0.0F);
+ glRotatef(30.0F, 0.0F, 1.0F, 0.0F);
+
+ glEnable(GL_DEPTH_TEST);
+ glEnable(GL_LIGHTING);
+ glEnable(GL_LIGHT0);
+}
+
+GLfloat TestGLCanvas::CalcRotateSpeed( unsigned long acceltime )
+{
+ GLfloat t,v;
+
+ t = ((GLfloat)acceltime) / 1000.0f;
+
+ if( t < 0.5f )
+ v = t;
+ else if( t < 1.0f )
+ v = t * (2.0f - t);
+ else
+ v = 0.75f;
+
+ return(v);
+}
+
+GLfloat TestGLCanvas::CalcRotateAngle( unsigned long lasttime,
+ unsigned long acceltime )
+{
+ GLfloat t,s1,s2;
+
+ t = ((GLfloat)(acceltime - lasttime)) / 1000.0f;
+ s1 = CalcRotateSpeed( lasttime );
+ s2 = CalcRotateSpeed( acceltime );
+
+ return( t * (s1 + s2) * 135.0f );
+}
+
+void TestGLCanvas::Action( long code, unsigned long lasttime,
+ unsigned long acceltime )
+{
+ GLfloat angle = CalcRotateAngle( lasttime, acceltime );
+
+ if (code == m_rleft)
+ Rotate( angle );
+ else if (code == m_rright)
+ Rotate( -angle );
+}
+
+void TestGLCanvas::OnKeyDown( wxKeyEvent& event )
+{
+ long evkey = event.KeyCode();
+ if (evkey == 0) return;
+
+ if (!m_TimeInitialized)
+ {
+ m_TimeInitialized = 1;
+ m_xsynct = event.m_timeStamp;
+ m_gsynct = wxStopWatch(&m_secbase);
+
+ m_Key = evkey;
+ m_StartTime = 0;
+ m_LastTime = 0;
+ m_LastRedraw = 0;
+ }
+
+ unsigned long currTime = event.m_timeStamp - m_xsynct;
+
+ if (evkey != m_Key)
+ {
+ m_Key = evkey;
+ m_LastRedraw = m_StartTime = m_LastTime = currTime;
+ }
+
+ if (currTime >= m_LastRedraw) // Redraw:
+ {
+ Action( m_Key, m_LastTime-m_StartTime, currTime-m_StartTime );
+
+ m_LastRedraw = wxStopWatch(&m_secbase) - m_gsynct;
+ m_LastTime = currTime;
+ }
+
+ event.Skip();
+}
+
+void TestGLCanvas::OnKeyUp( wxKeyEvent& event )
+{
+ m_Key = 0;
+ m_StartTime = 0;
+ m_LastTime = 0;
+ m_LastRedraw = 0;
+
+ event.Skip();
+}
+
+void TestGLCanvas::Rotate( GLfloat deg )
+{
+ SetCurrent();
+
+ glMatrixMode(GL_MODELVIEW);
+ glRotatef((GLfloat)deg, 0.0F, 0.0F, 1.0F);
+ Refresh(FALSE);
+}
+
+
+/* -----------------------------------------------------------------------
+ Main Window
+-------------------------------------------------------------------------*/
+
+BEGIN_EVENT_TABLE(MyFrame, wxFrame)
+ EVT_MENU(wxID_EXIT, MyFrame::OnExit)
+ EVT_MENU( ID_NEW_WINDOW, MyFrame::OnNewWindow)
+ EVT_MENU( ID_DEF_ROTATE_LEFT_KEY, MyFrame::OnDefRotateLeftKey)
+ EVT_MENU( ID_DEF_ROTATE_RIGHT_KEY, MyFrame::OnDefRotateRightKey)
+END_EVENT_TABLE()
+
+// My frame constructor
+MyFrame::MyFrame(wxFrame *frame, const wxString& title, const wxPoint& pos,
+ const wxSize& size, long style)
+ : wxFrame(frame, -1, title, pos, size, style)
+{
+ m_canvas = NULL;
+}
+
+// Intercept menu commands
+void MyFrame::OnExit(wxCommandEvent& event)
+{
+ Destroy();
+}
+
+void MyFrame::OnNewWindow()
+{
+ MyFrame *frame = new MyFrame(NULL, "Cube OpenGL Demo Clone",
+ wxPoint(50, 50), wxSize(400, 300));
+ // Give it an icon
+#ifdef wx_msw
+ frame->SetIcon(wxIcon("mondrian"));
+#endif
+
+ // Make a menubar
+ wxMenu *winMenu = new wxMenu;
+
+ winMenu->Append(wxID_EXIT, "&Close");
+ winMenu->Append(ID_NEW_WINDOW, "&New" );
+ wxMenuBar *menuBar = new wxMenuBar;
+ menuBar->Append(winMenu, "&Window");
+
+ winMenu = new wxMenu;
+ winMenu->Append(ID_DEF_ROTATE_LEFT_KEY, "Rotate &left");
+ winMenu->Append(ID_DEF_ROTATE_RIGHT_KEY, "Rotate &right");
+ menuBar->Append(winMenu, "&Key");
+
+ frame->SetMenuBar(menuBar);
+
+ frame->m_canvas = new TestGLCanvas( frame, *m_canvas, -1,
+ wxPoint(0, 0), wxSize(200, 200) );
+
+ // Show the frame
+ frame->Show(TRUE);
+}
+
+void MyFrame::OnDefRotateLeftKey()
+{
+ ScanCodeDialog dial( this, -1, m_canvas->m_rleft,
+ wxString("Left"), "Define key" );
+ int result = dial.ShowModal();
+ if( result == wxID_OK )
+ m_canvas->m_rleft = dial.GetValue();
+}
+void MyFrame::OnDefRotateRightKey()
+{
+ ScanCodeDialog dial( this, -1, m_canvas->m_rright,
+ wxString("Right"), "Define key" );
+ int result = dial.ShowModal();
+ if( result == wxID_OK )
+ m_canvas->m_rright = dial.GetValue();
+}
+
+/*------------------------------------------------------------------
+ Application object ( equivalent to main() )
+------------------------------------------------------------------ */
+
+IMPLEMENT_APP(MyApp)
+
+bool MyApp::OnInit(void)
+{
+ wxLog::SetTraceMask(wxTraceMessages);
+
+ // Create the main frame window
+ MyFrame *frame = new MyFrame(NULL, "Cube OpenGL Demo", wxPoint(50, 50),
+ wxSize(400, 300));
+ // Give it an icon
+#ifdef wx_msw
+ frame->SetIcon(wxIcon("mondrian"));
+#endif
+
+ // Make a menubar
+ wxMenu *winMenu = new wxMenu;
+
+ winMenu->Append(wxID_EXIT, "&Close");
+ winMenu->Append(ID_NEW_WINDOW, "&New" );
+ wxMenuBar *menuBar = new wxMenuBar;
+ menuBar->Append(winMenu, "&Window");
+
+ winMenu = new wxMenu;
+ winMenu->Append(ID_DEF_ROTATE_LEFT_KEY, "Rotate &left");
+ winMenu->Append(ID_DEF_ROTATE_RIGHT_KEY, "Rotate &right");
+ menuBar->Append(winMenu, "&Key");
+
+ frame->SetMenuBar(menuBar);
+
+ frame->m_canvas = new TestGLCanvas(frame, -1, wxPoint(0, 0), wxSize(200, 200));
+
+ // Show the frame
+ frame->Show(TRUE);
+
+ return TRUE;
+}