]>
git.saurik.com Git - wxWidgets.git/blob - samples/opengl/cube/cube.cpp
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
, NULL
/* attribs */)
97 // notice that we can't call InitGL() from here: we must wait until the
98 // window is shown on screen to be able to perform OpenGL calls
101 // this function is called on each repaint so it should be fast
102 void TestGLCanvas::Render()
104 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
105 glCallList(m_gllist
);
111 void TestGLCanvas::OnPaint(wxPaintEvent
& WXUNUSED(event
))
113 wxGetApp().SetCurrent(this);
115 // initialize if not done yet
123 void TestGLCanvas::OnSize(wxSizeEvent
& event
)
125 // don't prevent default processing from taking place
128 if ( !IsInitialized() )
131 // set GL viewport (not called by wxGLCanvas::OnSize on all platforms...)
133 GetClientSize(&w
, &h
);
135 wxGetApp().SetCurrent(this);
136 glViewport(0, 0, w
, h
);
139 void TestGLCanvas::InitGL()
141 if ( IsInitialized() )
144 /* set viewing projection */
145 glMatrixMode(GL_PROJECTION
);
147 glFrustum(-0.5f
, 0.5f
, -0.5f
, 0.5f
, 1.0f
, 3.0f
);
149 /* position viewer */
150 glMatrixMode(GL_MODELVIEW
);
152 glTranslatef(0.0f
, 0.0f
, -2.0f
);
154 /* position object */
155 glRotatef(30.0f
, 1.0f
, 0.0f
, 0.0f
);
156 glRotatef(30.0f
, 0.0f
, 1.0f
, 0.0f
);
158 glEnable(GL_DEPTH_TEST
);
159 glEnable(GL_LIGHTING
);
162 // create the list of commands to draw the cube: then we can just (quickly)
163 // execute it in Render() later
164 m_gllist
= glGenLists(1);
165 glNewList(m_gllist
, GL_COMPILE
);
167 /* draw six faces of a cube */
169 glNormal3f( 0.0f
, 0.0f
, 1.0f
);
170 glVertex3f( 0.5f
, 0.5f
, 0.5f
); glVertex3f(-0.5f
, 0.5f
, 0.5f
);
171 glVertex3f(-0.5f
,-0.5f
, 0.5f
); glVertex3f( 0.5f
,-0.5f
, 0.5f
);
173 glNormal3f( 0.0f
, 0.0f
,-1.0f
);
174 glVertex3f(-0.5f
,-0.5f
,-0.5f
); glVertex3f(-0.5f
, 0.5f
,-0.5f
);
175 glVertex3f( 0.5f
, 0.5f
,-0.5f
); glVertex3f( 0.5f
,-0.5f
,-0.5f
);
177 glNormal3f( 0.0f
, 1.0f
, 0.0f
);
178 glVertex3f( 0.5f
, 0.5f
, 0.5f
); glVertex3f( 0.5f
, 0.5f
,-0.5f
);
179 glVertex3f(-0.5f
, 0.5f
,-0.5f
); glVertex3f(-0.5f
, 0.5f
, 0.5f
);
181 glNormal3f( 0.0f
,-1.0f
, 0.0f
);
182 glVertex3f(-0.5f
,-0.5f
,-0.5f
); glVertex3f( 0.5f
,-0.5f
,-0.5f
);
183 glVertex3f( 0.5f
,-0.5f
, 0.5f
); glVertex3f(-0.5f
,-0.5f
, 0.5f
);
185 glNormal3f( 1.0f
, 0.0f
, 0.0f
);
186 glVertex3f( 0.5f
, 0.5f
, 0.5f
); glVertex3f( 0.5f
,-0.5f
, 0.5f
);
187 glVertex3f( 0.5f
,-0.5f
,-0.5f
); glVertex3f( 0.5f
, 0.5f
,-0.5f
);
189 glNormal3f(-1.0f
, 0.0f
, 0.0f
);
190 glVertex3f(-0.5f
,-0.5f
,-0.5f
); glVertex3f(-0.5f
,-0.5f
, 0.5f
);
191 glVertex3f(-0.5f
, 0.5f
, 0.5f
); glVertex3f(-0.5f
, 0.5f
,-0.5f
);
197 void TestGLCanvas::OnKeyDown( wxKeyEvent
& event
)
203 bool inverse
= false;
205 switch ( event
.GetKeyCode() )
212 // rotate around Z axis
221 // rotate around Y axis
234 wxGetApp().SetCurrent(this);
236 glMatrixMode(GL_MODELVIEW
);
237 glRotatef(angle
, x
, y
, z
);
240 for ( wxWindowList::const_iterator i
= wxTopLevelWindows
.begin();
241 i
!= wxTopLevelWindows
.end();
244 MyFrame
*frame
= (MyFrame
*)*i
;
245 frame
->RefreshCanvas();
249 // ----------------------------------------------------------------------------
250 // MyFrame: main application window
251 // ----------------------------------------------------------------------------
253 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
254 EVT_MENU(wxID_NEW
, MyFrame::OnNewWindow
)
255 EVT_MENU(wxID_CLOSE
, MyFrame::OnClose
)
259 : wxFrame(NULL
, wxID_ANY
, _T("wxWidgets OpenGL Cube Sample"),
260 wxDefaultPosition
, wxSize(400, 300))
262 m_canvas
= new TestGLCanvas(this);
264 SetIcon(wxICON(sample
));
267 wxMenu
*menu
= new wxMenu
;
268 menu
->Append(wxID_NEW
);
269 menu
->AppendSeparator();
270 menu
->Append(wxID_CLOSE
);
271 wxMenuBar
*menuBar
= new wxMenuBar
;
272 menuBar
->Append(menu
, _T("&Cube"));
281 void MyFrame::OnClose(wxCommandEvent
& WXUNUSED(event
))
283 // true is to force the frame to close
287 void MyFrame::OnNewWindow( wxCommandEvent
& WXUNUSED(event
) )
289 (void) new MyFrame();
292 void MyFrame::RefreshCanvas()
294 m_canvas
->Refresh(false);