]>
git.saurik.com Git - wxWidgets.git/blob - samples/opengl/cube/cube.cpp
8bf22a199a0d7f063ddb5b8bc0bb54771af30c5c
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 void MyApp::SetCurrent(wxGLCanvas
*canvas
)
71 wxCHECK_RET( canvas
, _T("canvas can't be NULL") );
74 m_glContext
= new wxGLContext(canvas
);
76 m_glContext
->SetCurrent(*canvas
);
79 // ----------------------------------------------------------------------------
81 // ----------------------------------------------------------------------------
83 BEGIN_EVENT_TABLE(TestGLCanvas
, wxGLCanvas
)
84 EVT_SIZE(TestGLCanvas::OnSize
)
85 EVT_PAINT(TestGLCanvas::OnPaint
)
87 EVT_KEY_DOWN(TestGLCanvas::OnKeyDown
)
90 static /* const */ int attribs
[] = { WX_GL_RGBA
, WX_GL_DOUBLEBUFFER
, 0 };
92 TestGLCanvas::TestGLCanvas(wxWindow
*parent
)
93 : wxGLCanvas(parent
, wxID_ANY
, attribs
)
98 // this function is called on each repaint so it should be fast
99 void TestGLCanvas::Render()
101 wxGetApp().SetCurrent(this);
103 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
104 glCallList(m_gllist
);
110 void TestGLCanvas::OnPaint(wxPaintEvent
& WXUNUSED(event
))
117 void TestGLCanvas::OnSize(wxSizeEvent
& WXUNUSED(event
))
119 // set GL viewport (not called by wxGLCanvas::OnSize on all platforms...)
121 GetClientSize(&w
, &h
);
123 wxGetApp().SetCurrent(this);
124 glViewport(0, 0, w
, h
);
127 void TestGLCanvas::InitGL()
129 wxGetApp().SetCurrent(this);
131 /* set viewing projection */
132 glMatrixMode(GL_PROJECTION
);
134 glFrustum(-0.5f
, 0.5f
, -0.5f
, 0.5f
, 1.0f
, 3.0f
);
136 /* position viewer */
137 glMatrixMode(GL_MODELVIEW
);
139 glTranslatef(0.0f
, 0.0f
, -2.0f
);
141 /* position object */
142 glRotatef(30.0f
, 1.0f
, 0.0f
, 0.0f
);
143 glRotatef(30.0f
, 0.0f
, 1.0f
, 0.0f
);
145 glEnable(GL_DEPTH_TEST
);
146 glEnable(GL_LIGHTING
);
149 // create the list of commands to draw the cube: then we can just (quickly)
150 // execute it in Render() later
151 m_gllist
= glGenLists(1);
152 glNewList(m_gllist
, GL_COMPILE
);
154 /* draw six faces of a cube */
156 glNormal3f( 0.0f
, 0.0f
, 1.0f
);
157 glVertex3f( 0.5f
, 0.5f
, 0.5f
); glVertex3f(-0.5f
, 0.5f
, 0.5f
);
158 glVertex3f(-0.5f
,-0.5f
, 0.5f
); glVertex3f( 0.5f
,-0.5f
, 0.5f
);
160 glNormal3f( 0.0f
, 0.0f
,-1.0f
);
161 glVertex3f(-0.5f
,-0.5f
,-0.5f
); glVertex3f(-0.5f
, 0.5f
,-0.5f
);
162 glVertex3f( 0.5f
, 0.5f
,-0.5f
); glVertex3f( 0.5f
,-0.5f
,-0.5f
);
164 glNormal3f( 0.0f
, 1.0f
, 0.0f
);
165 glVertex3f( 0.5f
, 0.5f
, 0.5f
); glVertex3f( 0.5f
, 0.5f
,-0.5f
);
166 glVertex3f(-0.5f
, 0.5f
,-0.5f
); glVertex3f(-0.5f
, 0.5f
, 0.5f
);
168 glNormal3f( 0.0f
,-1.0f
, 0.0f
);
169 glVertex3f(-0.5f
,-0.5f
,-0.5f
); glVertex3f( 0.5f
,-0.5f
,-0.5f
);
170 glVertex3f( 0.5f
,-0.5f
, 0.5f
); glVertex3f(-0.5f
,-0.5f
, 0.5f
);
172 glNormal3f( 1.0f
, 0.0f
, 0.0f
);
173 glVertex3f( 0.5f
, 0.5f
, 0.5f
); glVertex3f( 0.5f
,-0.5f
, 0.5f
);
174 glVertex3f( 0.5f
,-0.5f
,-0.5f
); glVertex3f( 0.5f
, 0.5f
,-0.5f
);
176 glNormal3f(-1.0f
, 0.0f
, 0.0f
);
177 glVertex3f(-0.5f
,-0.5f
,-0.5f
); glVertex3f(-0.5f
,-0.5f
, 0.5f
);
178 glVertex3f(-0.5f
, 0.5f
, 0.5f
); glVertex3f(-0.5f
, 0.5f
,-0.5f
);
184 void TestGLCanvas::OnKeyDown( wxKeyEvent
& event
)
190 bool inverse
= false;
192 switch ( event
.GetKeyCode() )
199 // rotate around Z axis
208 // rotate around Y axis
221 wxGetApp().SetCurrent(this);
223 glMatrixMode(GL_MODELVIEW
);
224 glRotatef(angle
, x
, y
, z
);
227 for ( wxWindowList::const_iterator i
= wxTopLevelWindows
.begin();
228 i
!= wxTopLevelWindows
.end();
231 (*i
)->Refresh(false);
235 // ----------------------------------------------------------------------------
236 // MyFrame: main application window
237 // ----------------------------------------------------------------------------
239 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
240 EVT_MENU(wxID_EXIT
, MyFrame::OnExit
)
241 EVT_MENU(wxID_NEW
, MyFrame::OnNewWindow
)
245 : wxFrame(NULL
, wxID_ANY
, _T("wxWidgets OpenGL Cube Sample"),
246 wxDefaultPosition
, wxSize(400, 300))
248 m_canvas
= new TestGLCanvas(this);
250 SetIcon(wxICON(sample
));
253 wxMenu
*winMenu
= new wxMenu
;
254 winMenu
->Append(wxID_EXIT
, _T("&Close"));
255 winMenu
->Append(wxID_NEW
, _T("&New") );
256 wxMenuBar
*menuBar
= new wxMenuBar
;
257 menuBar
->Append(winMenu
, _T("&Window"));
264 void MyFrame::OnExit( wxCommandEvent
& WXUNUSED(event
) )
266 // true is to force the frame to close
270 void MyFrame::OnNewWindow( wxCommandEvent
& WXUNUSED(event
) )
272 (void) new MyFrame();