]>
git.saurik.com Git - wxWidgets.git/blob - utils/glcanvas/samples/cube/cube.cpp
ce8afb40c2bbd90c86f5abdef4f3bd5557adeb6f
   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 #define ID_NEW_WINDOW 10000 
  33 #define ID_DEF_ROTATE_LEFT_KEY 10001 
  34 #define ID_DEF_ROTATE_RIGHT_KEY 10002 
  36 ////////////////////////////////////////////////////////////////////////////////// 
  37 // Control to get a keycode 
  39 class CScanTextCtrl 
: public wxTextCtrl
 
  42   CScanTextCtrl( wxWindow
* parent
, wxWindowID id
, int code
, 
  43                  const wxPoint
& pos
, const wxSize
& size 
); 
  45   void OnChar( wxKeyEvent
& event 
) { } /* do nothing */ 
  46   void OnKeyDown(wxKeyEvent
& event
); 
  49 // any class wishing to process wxWindows events must use this macro 
  53 BEGIN_EVENT_TABLE( CScanTextCtrl
, wxTextCtrl 
) 
  54   EVT_CHAR( CScanTextCtrl::OnChar 
) 
  55   EVT_KEY_DOWN( CScanTextCtrl::OnKeyDown 
) 
  58 CScanTextCtrl::CScanTextCtrl( wxWindow
* parent
, wxWindowID id
, int code
, 
  59                               const wxPoint
& pos
, const wxSize
& size 
) 
  60                   : wxTextCtrl( parent
, id
, "", pos
, size 
) 
  63   buf
.Printf( "0x%04x", code 
); 
  66 void CScanTextCtrl::OnKeyDown( wxKeyEvent
& event 
) 
  69     wxLogTrace(wxTraceMessages
, "[EVT_KEYDOWN]: Key = %04x, time = %d\n", event
.KeyCode(), 
  74   buf
.Printf( "0x%04x", event
.KeyCode() ); 
  78 /////////////////////////////////////////////////////////// 
  79 // Dialog for defining a keypress 
  81 class CMenuKeyDialog 
: public wxDialog
 
  84   CMenuKeyDialog( wxWindow
* parent
, wxWindowID id
, const int code
, const wxString 
&descr
, 
  85                   const wxString
& title 
); 
  89   CScanTextCtrl      
*m_ScanCode
; 
  90   wxTextCtrl         
*m_Description
; 
  92 // any class wishing to process wxWindows events must use this macro 
  96 BEGIN_EVENT_TABLE( CMenuKeyDialog
, wxDialog 
) 
 100 CMenuKeyDialog::CMenuKeyDialog( wxWindow
* parent
, wxWindowID id
, const int code
, 
 101                                  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), wxSize(31*2,12*2) ); 
 105   m_ScanCode 
= new CScanTextCtrl( this, -1, code
, wxPoint(37*2,6*2), wxSize(53*2,14*2) ); 
 107   new wxStaticText( this, -1, "Description", wxPoint(4*2,24*2), wxSize(32*2,12*2) ); 
 108   m_Description 
= new wxTextCtrl( this, -1, descr
, wxPoint(37*2,27*2), wxSize(53*2,14*2) ); 
 110   new wxButton( this, wxID_OK
, "Ok", wxPoint(20*2,50*2), wxSize(20*2,13*2) ); 
 111   new wxButton( this, wxID_CANCEL
, "Cancel", wxPoint(44*2,50*2), wxSize(25*2,13*2) ); 
 113 int CMenuKeyDialog::GetValue() 
 116   wxString buf 
= m_ScanCode
->GetValue(); 
 118     wxLogTrace(wxTraceMessages
, buf
.c_str() ); 
 119   #endif // __WXDEBUG__ 
 120   sscanf( buf
.c_str(), "%i", &code 
); 
 124 // `Main program' equivalent, creating windows and returning main app frame 
 125 bool MyApp::OnInit(void) 
 127   wxLog::SetTraceMask(wxTraceMessages
); 
 129   // Create the main frame window 
 130   MyFrame 
*frame 
= new MyFrame(NULL
, "Cube OpenGL Demo", wxPoint(50, 50), wxSize(400, 300)); 
 134   frame
->SetIcon(wxIcon("mondrian")); 
 138   wxMenu 
*winMenu 
= new wxMenu
; 
 140   winMenu
->Append(wxID_EXIT
, "&Close"); 
 141   winMenu
->Append(ID_NEW_WINDOW
, "&New" ); 
 142   wxMenuBar 
*menuBar 
= new wxMenuBar
; 
 143   menuBar
->Append(winMenu
, "&Window"); 
 145   winMenu 
= new wxMenu
; 
 146   winMenu
->Append(ID_DEF_ROTATE_LEFT_KEY
, "Rotate &left"); 
 147   winMenu
->Append(ID_DEF_ROTATE_RIGHT_KEY
, "Rotate &right"); 
 148   menuBar
->Append(winMenu
, "&Key"); 
 150   frame
->SetMenuBar(menuBar
); 
 152   frame
->m_canvas 
= new TestGLCanvas(frame
, -1, wxPoint(0, 0), wxSize(200, 200)); 
 160 void MyFrame::OnNewWindow() 
 162   MyFrame 
*frame 
= new MyFrame(NULL
, "Cube OpenGL Demo Clone", wxPoint(50, 50), wxSize(400, 300)); 
 166   frame
->SetIcon(wxIcon("mondrian")); 
 170   wxMenu 
*winMenu 
= new wxMenu
; 
 172   winMenu
->Append(wxID_EXIT
, "&Close"); 
 173   winMenu
->Append(ID_NEW_WINDOW
, "&New" ); 
 174   wxMenuBar 
*menuBar 
= new wxMenuBar
; 
 175   menuBar
->Append(winMenu
, "&Window"); 
 177   winMenu 
= new wxMenu
; 
 178   winMenu
->Append(ID_DEF_ROTATE_LEFT_KEY
, "Rotate &left"); 
 179   winMenu
->Append(ID_DEF_ROTATE_RIGHT_KEY
, "Rotate &right"); 
 180   menuBar
->Append(winMenu
, "&Key"); 
 182   frame
->SetMenuBar(menuBar
); 
 184   frame
->m_canvas 
= new TestGLCanvas( frame
, *m_canvas
, -1,  
 185                wxPoint(0, 0), wxSize(200, 200) ); 
 191 void MyFrame::OnDefRotateLeftKey() 
 193   CMenuKeyDialog 
dial( this, -1, m_canvas
->m_rleft
, wxString("Rotate left key"), "Define key" ); 
 194   int result 
= dial
.ShowModal(); 
 195   if( result 
== wxID_OK 
) 
 196     m_canvas
->m_rleft 
= dial
.GetValue(); 
 198 void MyFrame::OnDefRotateRightKey() 
 200   CMenuKeyDialog 
dial( this, -1, m_canvas
->m_rright
, wxString("Rotate right key"), "Define key" ); 
 201   int result 
= dial
.ShowModal(); 
 202   if( result 
== wxID_OK 
) 
 203     m_canvas
->m_rright 
= dial
.GetValue(); 
 208 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
) 
 209     EVT_MENU(wxID_EXIT
, MyFrame::OnExit
) 
 210     EVT_MENU( ID_NEW_WINDOW
, MyFrame::OnNewWindow
) 
 211     EVT_MENU( ID_DEF_ROTATE_LEFT_KEY
, MyFrame::OnDefRotateLeftKey
) 
 212     EVT_MENU( ID_DEF_ROTATE_RIGHT_KEY
, MyFrame::OnDefRotateRightKey
) 
 215 // My frame constructor 
 216 MyFrame::MyFrame(wxFrame 
*frame
, const wxString
& title
, const wxPoint
& pos
, 
 217     const wxSize
& size
, long style
): 
 218   wxFrame(frame
, -1, title
, pos
, size
, style
) 
 223 // Intercept menu commands 
 224 void MyFrame::OnExit(wxCommandEvent
& event
) 
 229 BEGIN_EVENT_TABLE(TestGLCanvas
, wxGLCanvas
) 
 230     EVT_SIZE(TestGLCanvas::OnSize
) 
 231     EVT_PAINT(TestGLCanvas::OnPaint
) 
 232     EVT_ERASE_BACKGROUND(TestGLCanvas::OnEraseBackground
) 
 233     EVT_KEY_DOWN( TestGLCanvas::OnKeyDown 
) 
 234     EVT_KEY_UP( TestGLCanvas::OnKeyUp 
) 
 237 TestGLCanvas::TestGLCanvas(wxWindow 
*parent
, wxWindowID id
, 
 238     const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
): 
 239   wxGLCanvas(parent
, NULL
, id
, pos
, size
, style
, name 
) 
 247 TestGLCanvas::TestGLCanvas(wxWindow 
*parent
, const TestGLCanvas 
&other
,  
 248     wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
, long style
, 
 249     const wxString
& name 
) : 
 250       wxGLCanvas(parent
, other
.GetContext(), id
, pos
, size
, style
, name  
) 
 253   m_gllist 
= other
.m_gllist
;    /* share display list */ 
 257 //  SetBackgroundColour( *wxGREEN ); 
 258 // virtual void SetBackgroundColour(const wxColour& colour) 
 262 TestGLCanvas::~TestGLCanvas(void) 
 266 void TestGLCanvas::OnPaint( wxPaintEvent
& event 
) 
 268     // This is a dummy, to avoid an endless succession of paint messages. 
 269     // OnPaint handlers must always create a wxPaintDC. 
 273     if (!GetContext()) return; 
 278     /* init OpenGL once, but after SetCurrent */ 
 285     /* clear color and depth buffers */ 
 286     glClear(GL_COLOR_BUFFER_BIT 
| GL_DEPTH_BUFFER_BIT
); 
 290         m_gllist 
= glGenLists( 1 ); 
 291         printf( "List=%d\n", m_gllist 
); 
 292         glNewList( m_gllist
, GL_COMPILE_AND_EXECUTE 
); 
 294         /* draw six faces of a cube */ 
 296         glNormal3f( 0.0F
, 0.0F
, 1.0F
); 
 297         glVertex3f( 0.5F
, 0.5F
, 0.5F
); glVertex3f(-0.5F
, 0.5F
, 0.5F
); 
 298         glVertex3f(-0.5F
,-0.5F
, 0.5F
); glVertex3f( 0.5F
,-0.5F
, 0.5F
); 
 300         glNormal3f( 0.0F
, 0.0F
,-1.0F
); 
 301         glVertex3f(-0.5F
,-0.5F
,-0.5F
); glVertex3f(-0.5F
, 0.5F
,-0.5F
); 
 302         glVertex3f( 0.5F
, 0.5F
,-0.5F
); glVertex3f( 0.5F
,-0.5F
,-0.5F
); 
 304         glNormal3f( 0.0F
, 1.0F
, 0.0F
); 
 305         glVertex3f( 0.5F
, 0.5F
, 0.5F
); glVertex3f( 0.5F
, 0.5F
,-0.5F
); 
 306         glVertex3f(-0.5F
, 0.5F
,-0.5F
); glVertex3f(-0.5F
, 0.5F
, 0.5F
); 
 308         glNormal3f( 0.0F
,-1.0F
, 0.0F
); 
 309         glVertex3f(-0.5F
,-0.5F
,-0.5F
); glVertex3f( 0.5F
,-0.5F
,-0.5F
); 
 310         glVertex3f( 0.5F
,-0.5F
, 0.5F
); glVertex3f(-0.5F
,-0.5F
, 0.5F
); 
 312         glNormal3f( 1.0F
, 0.0F
, 0.0F
); 
 313         glVertex3f( 0.5F
, 0.5F
, 0.5F
); glVertex3f( 0.5F
,-0.5F
, 0.5F
); 
 314         glVertex3f( 0.5F
,-0.5F
,-0.5F
); glVertex3f( 0.5F
, 0.5F
,-0.5F
); 
 316         glNormal3f(-1.0F
, 0.0F
, 0.0F
); 
 317         glVertex3f(-0.5F
,-0.5F
,-0.5F
); glVertex3f(-0.5F
,-0.5F
, 0.5F
); 
 318         glVertex3f(-0.5F
, 0.5F
, 0.5F
); glVertex3f(-0.5F
, 0.5F
,-0.5F
); 
 324         glCallList( m_gllist 
);     
 330 void TestGLCanvas::OnSize(wxSizeEvent
& event
) 
 333     GetClientSize(& width
, & height
); 
 340         glViewport(0, 0, width
, height
); 
 344 void TestGLCanvas::OnEraseBackground(wxEraseEvent
& event
) 
 346     // Do nothing, to avoid flashing. 
 347   printf( "in erase background\n" ); 
 350 void TestGLCanvas::InitGL(void) 
 354     /* set viewing projection */ 
 355     glMatrixMode(GL_PROJECTION
); 
 356     glFrustum(-0.5F
, 0.5F
, -0.5F
, 0.5F
, 1.0F
, 3.0F
); 
 358     /* position viewer */ 
 359     glMatrixMode(GL_MODELVIEW
); 
 360     glTranslatef(0.0F
, 0.0F
, -2.0F
); 
 362     /* position object */ 
 363     glRotatef(30.0F
, 1.0F
, 0.0F
, 0.0F
); 
 364     glRotatef(30.0F
, 0.0F
, 1.0F
, 0.0F
); 
 366     glEnable(GL_DEPTH_TEST
); 
 367     glEnable(GL_LIGHTING
); 
 371 void TestGLCanvas::OnKeyDown( wxKeyEvent
& event 
) 
 373   if( event
.KeyCode() == m_rleft 
) Rotate( -5.0 ); 
 374   else if( event
.KeyCode() == m_rright 
) Rotate( 5.0 ); 
 376     wxLogTrace(wxTraceMessages
, "[EVT_KEYDOWN]: Key = %04x, time = %d\n", event
.KeyCode(), 
 378   #endif // __WXDEBUG__ 
 380 void TestGLCanvas::OnKeyUp( wxKeyEvent
& event 
) 
 383     wxLogTrace(wxTraceMessages
, "[EVT_KEYUP]: Key = %04x, time = %d\n", event
.KeyCode(), 
 385   #endif // __WXDEBUG__ 
 388 void TestGLCanvas::Rotate( double deg 
) 
 392     glMatrixMode(GL_MODELVIEW
); 
 393     glRotatef((GLfloat
)deg
, 0.0F
, 0.0F
, 1.0F
);