]> git.saurik.com Git - wxWidgets.git/blob - samples/opengl/penguin/penguin.cpp
corrected logic in wxQTMediaEvtHandler::OnEraseBackground(): only erase it if there...
[wxWidgets.git] / samples / opengl / penguin / penguin.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: penguin.cpp
3 // Purpose: wxGLCanvas demo program
4 // Author: Robert Roebling
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Robert Roebling
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation
14 #pragma interface
15 #endif
16
17 // For compilers that support precompilation, includes "wx.h".
18 #include "wx/wxprec.h"
19
20 #ifdef __BORLANDC__
21 #pragma hdrstop
22 #endif
23
24 #ifndef WX_PRECOMP
25 #include "wx/wx.h"
26 #endif
27
28 #if !wxUSE_GLCANVAS
29 #error "OpenGL required: set wxUSE_GLCANVAS to 1 and rebuild the library"
30 #endif
31
32 #include "penguin.h"
33 #ifdef __WXMAC__
34 # ifdef __DARWIN__
35 # include <OpenGL/glu.h>
36 # else
37 # include <glu.h>
38 # endif
39 #else
40 # include <GL/glu.h>
41 #endif
42
43 #include "../../sample.xpm"
44
45 #define VIEW_ASPECT 1.3
46
47 // `Main program' equivalent, creating windows and returning main app frame
48 bool MyApp::OnInit()
49 {
50 // Create the main frame window
51 MyFrame *frame = new MyFrame(NULL, wxT("wxWidgets OpenGL Penguin Sample"),
52 wxDefaultPosition, wxDefaultSize);
53
54 /* Make a menubar */
55 wxMenu *fileMenu = new wxMenu;
56
57 fileMenu->Append(wxID_EXIT, wxT("E&xit"));
58 wxMenuBar *menuBar = new wxMenuBar;
59 menuBar->Append(fileMenu, wxT("&File"));
60 frame->SetMenuBar(menuBar);
61
62 frame->SetCanvas( new TestGLCanvas(frame, wxID_ANY, wxDefaultPosition,
63 wxSize(200, 200), wxSUNKEN_BORDER) );
64
65 /* Load file wiht mesh data */
66 frame->GetCanvas()->LoadLWO( wxT("penguin.lwo") );
67
68 /* Show the frame */
69 frame->Show(true);
70
71 return true;
72 }
73
74 IMPLEMENT_APP(MyApp)
75
76 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
77 EVT_MENU(wxID_EXIT, MyFrame::OnExit)
78 END_EVENT_TABLE()
79
80 /* My frame constructor */
81 MyFrame::MyFrame(wxFrame *frame, const wxString& title, const wxPoint& pos,
82 const wxSize& size, long style)
83 : wxFrame(frame, wxID_ANY, title, pos, size, style)
84 {
85 m_canvas = NULL;
86 SetIcon(wxIcon(sample_xpm));
87 }
88
89 /* Intercept menu commands */
90 void MyFrame::OnExit( wxCommandEvent& WXUNUSED(event) )
91 {
92 // true is to force the frame to close
93 Close(true);
94 }
95
96 BEGIN_EVENT_TABLE(TestGLCanvas, wxGLCanvas)
97 EVT_SIZE(TestGLCanvas::OnSize)
98 EVT_PAINT(TestGLCanvas::OnPaint)
99 EVT_ERASE_BACKGROUND(TestGLCanvas::OnEraseBackground)
100 EVT_MOUSE_EVENTS(TestGLCanvas::OnMouse)
101 END_EVENT_TABLE()
102
103 TestGLCanvas::TestGLCanvas(wxWindow *parent, wxWindowID id,
104 const wxPoint& pos, const wxSize& size, long style, const wxString& name)
105 : wxGLCanvas(parent, id, pos, size, style|wxFULL_REPAINT_ON_RESIZE, name)
106 {
107 block = false;
108 }
109
110 TestGLCanvas::~TestGLCanvas()
111 {
112 /* destroy mesh */
113 lw_object_free(info.lwobject);
114 }
115
116 void TestGLCanvas::OnPaint( wxPaintEvent& WXUNUSED(event) )
117 {
118 /* must always be here */
119 wxPaintDC dc(this);
120
121 #ifndef __WXMOTIF__
122 if (!GetContext()) return;
123 #endif
124
125 SetCurrent();
126
127 // Initialize OpenGL
128 if (info.do_init)
129 {
130 InitGL();
131 info.do_init = false;
132 }
133
134 // View
135 glMatrixMode( GL_PROJECTION );
136 glLoadIdentity();
137 gluPerspective( info.zoom, VIEW_ASPECT, 1.0, 100.0 );
138 glMatrixMode( GL_MODELVIEW );
139
140 // Clear
141 glClearColor( 0.3f, 0.4f, 0.6f, 1.0f );
142 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
143
144 // Transformations
145 GLfloat m[4][4];
146 glLoadIdentity();
147 glTranslatef( 0.0f, 0.0f, -30.0f );
148 build_rotmatrix( m,info.quat );
149 glMultMatrixf( &m[0][0] );
150
151 // Draw object
152 lw_object_show( info.lwobject );
153
154 // Flush
155 glFlush();
156
157 // Swap
158 SwapBuffers();
159 }
160
161 void TestGLCanvas::OnSize(wxSizeEvent& event)
162 {
163 // this is also necessary to update the context on some platforms
164 wxGLCanvas::OnSize(event);
165
166 // set GL viewport (not called by wxGLCanvas::OnSize on all platforms...)
167 int w, h;
168 GetClientSize(&w, &h);
169 #ifndef __WXMOTIF__
170 if ( GetContext() )
171 #endif
172 {
173 SetCurrent();
174 glViewport(0, 0, (GLint) w, (GLint) h);
175 }
176 }
177
178 void TestGLCanvas::OnEraseBackground(wxEraseEvent& WXUNUSED(event))
179 {
180 /* Do nothing, to avoid flashing on MSW */
181 }
182
183 void TestGLCanvas::LoadLWO(const wxString &filename)
184 {
185 /* test if lightwave object */
186 if (!lw_is_lwobject(filename.mb_str())) return;
187
188 /* read lightwave object */
189 lwObject *lwobject = lw_object_read(filename.mb_str());
190
191 /* scale */
192 lw_object_scale(lwobject, 10.0 / lw_object_radius(lwobject));
193
194 /* set up mesh info */
195 info.do_init = true;
196 info.lwobject = lwobject;
197 info.beginx = 0.0f;
198 info.beginy = 0.0f;
199 info.zoom = 45.0f;
200 trackball( info.quat, 0.0f, 0.0f, 0.0f, 0.0f );
201 }
202
203 void TestGLCanvas::OnMouse( wxMouseEvent& event )
204 {
205
206 if ( event.Dragging() )
207 {
208 wxSize sz( GetClientSize() );
209
210 /* drag in progress, simulate trackball */
211 float spin_quat[4];
212 trackball(spin_quat,
213 (2.0*info.beginx - sz.x) / sz.x,
214 ( sz.y - 2.0*info.beginy) / sz.y,
215 ( 2.0*event.GetX() - sz.x) / sz.x,
216 ( sz.y - 2.0*event.GetY()) / sz.y);
217
218 add_quats( spin_quat, info.quat, info.quat );
219
220 /* orientation has changed, redraw mesh */
221 Refresh(false);
222 }
223
224 info.beginx = event.GetX();
225 info.beginy = event.GetY();
226 }
227
228 void TestGLCanvas::InitGL()
229 {
230 static const GLfloat light0_pos[4] = { -50.0f, 50.0f, 0.0f, 0.0f };
231
232 // white light
233 static const GLfloat light0_color[4] = { 0.6f, 0.6f, 0.6f, 1.0f };
234
235 static const GLfloat light1_pos[4] = { 50.0f, 50.0f, 0.0f, 0.0f };
236
237 // cold blue light
238 static const GLfloat light1_color[4] = { 0.4f, 0.4f, 1.0f, 1.0f };
239
240 /* remove back faces */
241 glDisable(GL_CULL_FACE);
242 glEnable(GL_DEPTH_TEST);
243
244 /* speedups */
245 glEnable(GL_DITHER);
246 glShadeModel(GL_SMOOTH);
247 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
248 glHint(GL_POLYGON_SMOOTH_HINT, GL_FASTEST);
249
250 /* light */
251 glLightfv(GL_LIGHT0, GL_POSITION, light0_pos);
252 glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_color);
253 glLightfv(GL_LIGHT1, GL_POSITION, light1_pos);
254 glLightfv(GL_LIGHT1, GL_DIFFUSE, light1_color);
255 glEnable(GL_LIGHT0);
256 glEnable(GL_LIGHT1);
257 glEnable(GL_LIGHTING);
258
259 glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
260 glEnable(GL_COLOR_MATERIAL);
261 }
262