1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxGLCanvas demo program
4 // Author: Julian Smart
5 // Modified by: Vadim Zeitlin to use new wxGLCanvas API (2007-04-09)
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
32 #error "OpenGL required: set wxUSE_GLCANVAS to 1 and rebuild the library"
37 #if !defined(__WXMSW__) && !defined(__WXPM__)
38 #include "../../sample.xpm"
41 // ============================================================================
43 // ============================================================================
45 // ----------------------------------------------------------------------------
46 // MyApp: the application object
47 // ----------------------------------------------------------------------------
53 if ( !wxApp
::OnInit() )
56 // Create the main window
66 return wxApp
::OnExit();
69 TestGLContext
& MyApp
::GetContext(wxGLCanvas
*canvas
)
72 m_glContext
= new TestGLContext(canvas
);
74 m_glContext
->SetCurrent(*canvas
);
79 // ----------------------------------------------------------------------------
81 // ----------------------------------------------------------------------------
83 TestGLContext
::TestGLContext(wxGLCanvas
*canvas
)
89 void TestGLContext
::Init()
94 /* set viewing projection */
95 glMatrixMode(GL_PROJECTION
);
97 glFrustum(-0.5f
, 0.5f
, -0.5f
, 0.5f
, 1.0f
, 3.0f
);
99 glEnable(GL_DEPTH_TEST
);
100 glEnable(GL_LIGHTING
);
103 // create the list of commands to draw the cube: then we can just (quickly)
104 // execute it in DrawRotatedCube() later
105 m_gllist
= glGenLists(1);
106 glNewList(m_gllist
, GL_COMPILE
);
108 /* draw six faces of a cube */
110 glNormal3f( 0.0f
, 0.0f
, 1.0f
);
111 glVertex3f( 0.5f
, 0.5f
, 0.5f
); glVertex3f(-0.5f
, 0.5f
, 0.5f
);
112 glVertex3f(-0.5f
,-0.5f
, 0.5f
); glVertex3f( 0.5f
,-0.5f
, 0.5f
);
114 glNormal3f( 0.0f
, 0.0f
,-1.0f
);
115 glVertex3f(-0.5f
,-0.5f
,-0.5f
); glVertex3f(-0.5f
, 0.5f
,-0.5f
);
116 glVertex3f( 0.5f
, 0.5f
,-0.5f
); glVertex3f( 0.5f
,-0.5f
,-0.5f
);
118 glNormal3f( 0.0f
, 1.0f
, 0.0f
);
119 glVertex3f( 0.5f
, 0.5f
, 0.5f
); glVertex3f( 0.5f
, 0.5f
,-0.5f
);
120 glVertex3f(-0.5f
, 0.5f
,-0.5f
); glVertex3f(-0.5f
, 0.5f
, 0.5f
);
122 glNormal3f( 0.0f
,-1.0f
, 0.0f
);
123 glVertex3f(-0.5f
,-0.5f
,-0.5f
); glVertex3f( 0.5f
,-0.5f
,-0.5f
);
124 glVertex3f( 0.5f
,-0.5f
, 0.5f
); glVertex3f(-0.5f
,-0.5f
, 0.5f
);
126 glNormal3f( 1.0f
, 0.0f
, 0.0f
);
127 glVertex3f( 0.5f
, 0.5f
, 0.5f
); glVertex3f( 0.5f
,-0.5f
, 0.5f
);
128 glVertex3f( 0.5f
,-0.5f
,-0.5f
); glVertex3f( 0.5f
, 0.5f
,-0.5f
);
130 glNormal3f(-1.0f
, 0.0f
, 0.0f
);
131 glVertex3f(-0.5f
,-0.5f
,-0.5f
); glVertex3f(-0.5f
,-0.5f
, 0.5f
);
132 glVertex3f(-0.5f
, 0.5f
, 0.5f
); glVertex3f(-0.5f
, 0.5f
,-0.5f
);
138 void TestGLContext
::DrawRotatedCube(float xangle
, float yangle
)
142 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
144 glMatrixMode(GL_MODELVIEW
);
146 glTranslatef(0.0f
, 0.0f
, -2.0f
);
147 glRotatef(xangle
, 1.0f
, 0.0f
, 0.0f
);
148 glRotatef(yangle
, 0.0f
, 1.0f
, 0.0f
);
150 glCallList(m_gllist
);
155 // ----------------------------------------------------------------------------
157 // ----------------------------------------------------------------------------
159 BEGIN_EVENT_TABLE(TestGLCanvas
, wxGLCanvas
)
160 EVT_SIZE(TestGLCanvas
::OnSize
)
161 EVT_PAINT(TestGLCanvas
::OnPaint
)
163 EVT_KEY_DOWN(TestGLCanvas
::OnKeyDown
)
166 TestGLCanvas
::TestGLCanvas(wxWindow
*parent
)
167 : wxGLCanvas(parent
, wxID_ANY
, NULL
/* attribs */)
173 void TestGLCanvas
::OnPaint(wxPaintEvent
& WXUNUSED(event
))
177 wxGetApp().GetContext(this).DrawRotatedCube(m_xangle
, m_yangle
);
182 void TestGLCanvas
::OnSize(wxSizeEvent
& event
)
184 // don't prevent default processing from taking place
190 // set GL viewport (not called by wxGLCanvas::OnSize on all platforms...)
192 GetClientSize(&w
, &h
);
194 wxGetApp().GetContext(this);
195 glViewport(0, 0, w
, h
);
198 void TestGLCanvas
::OnKeyDown( wxKeyEvent
& event
)
202 bool inverse
= false;
204 switch ( event
.GetKeyCode() )
211 // rotate around Y axis
220 // rotate around X axis
238 // ----------------------------------------------------------------------------
239 // MyFrame: main application window
240 // ----------------------------------------------------------------------------
242 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
243 EVT_MENU(wxID_NEW
, MyFrame
::OnNewWindow
)
244 EVT_MENU(wxID_CLOSE
, MyFrame
::OnClose
)
248 : wxFrame(NULL
, wxID_ANY
, _T("wxWidgets OpenGL Cube Sample"))
250 new TestGLCanvas(this);
252 SetIcon(wxICON(sample
));
255 wxMenu
*menu
= new wxMenu
;
256 menu
->Append(wxID_NEW
);
257 menu
->AppendSeparator();
258 menu
->Append(wxID_CLOSE
);
259 wxMenuBar
*menuBar
= new wxMenuBar
;
260 menuBar
->Append(menu
, _T("&Cube"));
266 SetClientSize(400, 400);
270 void MyFrame
::OnClose(wxCommandEvent
& WXUNUSED(event
))
272 // true is to force the frame to close
276 void MyFrame
::OnNewWindow( wxCommandEvent
& WXUNUSED(event
) )
278 (void) new MyFrame();