]>
git.saurik.com Git - wxWidgets.git/blob - samples/opengl/cube/cube.cpp
b5f5ba419e1dfc45a62c0b6dc9f71df53bbfa80e
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
)
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 wxGetApp().SetCurrent(this);
106 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
107 glCallList(m_gllist
);
113 void TestGLCanvas::OnPaint(wxPaintEvent
& WXUNUSED(event
))
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 wxGetApp().SetCurrent(this);
146 /* set viewing projection */
147 glMatrixMode(GL_PROJECTION
);
149 glFrustum(-0.5f
, 0.5f
, -0.5f
, 0.5f
, 1.0f
, 3.0f
);
151 /* position viewer */
152 glMatrixMode(GL_MODELVIEW
);
154 glTranslatef(0.0f
, 0.0f
, -2.0f
);
156 /* position object */
157 glRotatef(30.0f
, 1.0f
, 0.0f
, 0.0f
);
158 glRotatef(30.0f
, 0.0f
, 1.0f
, 0.0f
);
160 glEnable(GL_DEPTH_TEST
);
161 glEnable(GL_LIGHTING
);
164 // create the list of commands to draw the cube: then we can just (quickly)
165 // execute it in Render() later
166 m_gllist
= glGenLists(1);
167 glNewList(m_gllist
, GL_COMPILE
);
169 /* draw six faces of a cube */
171 glNormal3f( 0.0f
, 0.0f
, 1.0f
);
172 glVertex3f( 0.5f
, 0.5f
, 0.5f
); glVertex3f(-0.5f
, 0.5f
, 0.5f
);
173 glVertex3f(-0.5f
,-0.5f
, 0.5f
); glVertex3f( 0.5f
,-0.5f
, 0.5f
);
175 glNormal3f( 0.0f
, 0.0f
,-1.0f
);
176 glVertex3f(-0.5f
,-0.5f
,-0.5f
); glVertex3f(-0.5f
, 0.5f
,-0.5f
);
177 glVertex3f( 0.5f
, 0.5f
,-0.5f
); glVertex3f( 0.5f
,-0.5f
,-0.5f
);
179 glNormal3f( 0.0f
, 1.0f
, 0.0f
);
180 glVertex3f( 0.5f
, 0.5f
, 0.5f
); glVertex3f( 0.5f
, 0.5f
,-0.5f
);
181 glVertex3f(-0.5f
, 0.5f
,-0.5f
); glVertex3f(-0.5f
, 0.5f
, 0.5f
);
183 glNormal3f( 0.0f
,-1.0f
, 0.0f
);
184 glVertex3f(-0.5f
,-0.5f
,-0.5f
); glVertex3f( 0.5f
,-0.5f
,-0.5f
);
185 glVertex3f( 0.5f
,-0.5f
, 0.5f
); glVertex3f(-0.5f
,-0.5f
, 0.5f
);
187 glNormal3f( 1.0f
, 0.0f
, 0.0f
);
188 glVertex3f( 0.5f
, 0.5f
, 0.5f
); glVertex3f( 0.5f
,-0.5f
, 0.5f
);
189 glVertex3f( 0.5f
,-0.5f
,-0.5f
); glVertex3f( 0.5f
, 0.5f
,-0.5f
);
191 glNormal3f(-1.0f
, 0.0f
, 0.0f
);
192 glVertex3f(-0.5f
,-0.5f
,-0.5f
); glVertex3f(-0.5f
,-0.5f
, 0.5f
);
193 glVertex3f(-0.5f
, 0.5f
, 0.5f
); glVertex3f(-0.5f
, 0.5f
,-0.5f
);
199 void TestGLCanvas::OnKeyDown( wxKeyEvent
& event
)
205 bool inverse
= false;
207 switch ( event
.GetKeyCode() )
214 // rotate around Z axis
223 // rotate around Y axis
236 wxGetApp().SetCurrent(this);
238 glMatrixMode(GL_MODELVIEW
);
239 glRotatef(angle
, x
, y
, z
);
242 for ( wxWindowList::const_iterator i
= wxTopLevelWindows
.begin();
243 i
!= wxTopLevelWindows
.end();
246 (*i
)->Refresh(false);
250 // ----------------------------------------------------------------------------
251 // MyFrame: main application window
252 // ----------------------------------------------------------------------------
254 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
255 EVT_MENU(wxID_EXIT
, MyFrame::OnExit
)
256 EVT_MENU(wxID_NEW
, MyFrame::OnNewWindow
)
260 : wxFrame(NULL
, wxID_ANY
, _T("wxWidgets OpenGL Cube Sample"),
261 wxDefaultPosition
, wxSize(400, 300))
263 m_canvas
= new TestGLCanvas(this);
265 SetIcon(wxICON(sample
));
268 wxMenu
*winMenu
= new wxMenu
;
269 winMenu
->Append(wxID_EXIT
, _T("&Close"));
270 winMenu
->Append(wxID_NEW
, _T("&New") );
271 wxMenuBar
*menuBar
= new wxMenuBar
;
272 menuBar
->Append(winMenu
, _T("&Window"));
279 void MyFrame::OnExit( wxCommandEvent
& WXUNUSED(event
) )
281 // true is to force the frame to close
285 void MyFrame::OnNewWindow( wxCommandEvent
& WXUNUSED(event
) )
287 (void) new MyFrame();