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