]> git.saurik.com Git - wxWidgets.git/blob - samples/opengl/penguin/penguin.cpp
Misc release-related mods
[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 Please set wxUSE_GLCANVAS to 1 in setup.h.
30 #endif
31
32 #include "penguin.h"
33 #include <GL/glu.h>
34
35 #define VIEW_ASPECT 1.3
36
37 /* `Main program' equivalent, creating windows and returning main app frame */
38 bool MyApp::OnInit(void)
39 {
40
41 /* Create the main frame window */
42 MyFrame *frame = new MyFrame(NULL, "wxWindows OpenGL Demo", wxPoint(50, 50), wxSize(400, 300));
43
44 /* Make a menubar */
45 wxMenu *fileMenu = new wxMenu;
46
47 fileMenu->Append(wxID_EXIT, "E&xit");
48 wxMenuBar *menuBar = new wxMenuBar;
49 menuBar->Append(fileMenu, "&File");
50 frame->SetMenuBar(menuBar);
51
52 frame->m_canvas = new TestGLCanvas(frame, -1, wxPoint(0, 0), wxSize(200, 200));
53
54 /* Load file wiht mesh data */
55 frame->m_canvas->LoadLWO( "penguin.lwo" );
56
57 /* Show the frame */
58 frame->Show(TRUE);
59
60 return TRUE;
61 }
62
63 IMPLEMENT_APP(MyApp)
64
65 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
66 EVT_MENU(wxID_EXIT, MyFrame::OnExit)
67 END_EVENT_TABLE()
68
69 /* My frame constructor */
70 MyFrame::MyFrame(wxFrame *frame, const wxString& title, const wxPoint& pos,
71 const wxSize& size, long style):
72 wxFrame(frame, -1, title, pos, size, style)
73 {
74 m_canvas = NULL;
75 }
76
77 /* Intercept menu commands */
78 void MyFrame::OnExit(wxCommandEvent& event)
79 {
80 Destroy();
81 }
82
83 BEGIN_EVENT_TABLE(TestGLCanvas, wxGLCanvas)
84 EVT_SIZE(TestGLCanvas::OnSize)
85 EVT_PAINT(TestGLCanvas::OnPaint)
86 EVT_ERASE_BACKGROUND(TestGLCanvas::OnEraseBackground)
87 EVT_MOUSE_EVENTS(TestGLCanvas::OnMouse)
88 END_EVENT_TABLE()
89
90 TestGLCanvas::TestGLCanvas(wxWindow *parent, wxWindowID id,
91 const wxPoint& pos, const wxSize& size, long style, const wxString& name):
92 wxGLCanvas(parent, id, pos, size, style, name)
93 {
94 block = FALSE;
95 }
96
97 TestGLCanvas::~TestGLCanvas(void)
98 {
99 /* destroy mesh */
100 lw_object_free(info.lwobject);
101 }
102
103 void TestGLCanvas::OnPaint( wxPaintEvent& event )
104 {
105 /* must always be here */
106 wxPaintDC dc(this);
107
108 #ifndef __WXMOTIF__
109 if (!GetContext()) return;
110 #endif
111
112 SetCurrent();
113
114 /* initialize OpenGL */
115 if (info.do_init == TRUE)
116 {
117 InitGL();
118 info.do_init = FALSE;
119 }
120
121 /* view */
122 glMatrixMode( GL_PROJECTION );
123 glLoadIdentity();
124 gluPerspective( info.zoom, VIEW_ASPECT, 1, 100 );
125 glMatrixMode( GL_MODELVIEW );
126
127 /* clear */
128 glClearColor( .3, .4, .6, 1 );
129 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
130
131 /* transformations */
132 GLfloat m[4][4];
133 glLoadIdentity();
134 glTranslatef( 0, 0, -30 );
135 build_rotmatrix( m,info.quat );
136 glMultMatrixf( &m[0][0] );
137
138 /* draw object */
139 lw_object_show( info.lwobject );
140
141 /* flush */
142 glFlush();
143
144 /* swap */
145 SwapBuffers();
146 }
147
148 void TestGLCanvas::OnSize(wxSizeEvent& event)
149 {
150 int width, height;
151 GetClientSize(& width, & height);
152
153 #ifndef __WXMOTIF__
154 if (GetContext())
155 #endif
156 {
157 SetCurrent();
158 glViewport(0, 0, width, height);
159 }
160 }
161
162 void TestGLCanvas::OnEraseBackground(wxEraseEvent& event)
163 {
164 /* Do nothing, to avoid flashing on MSW */
165 }
166
167 void TestGLCanvas::LoadLWO(const wxString &filename)
168 {
169 /* test if lightwave object */
170 if (!lw_is_lwobject(filename)) return;
171
172 /* read lightwave object */
173 lwObject *lwobject = lw_object_read(filename);
174
175 /* scale */
176 lw_object_scale(lwobject, 10.0 / lw_object_radius(lwobject));
177
178 /* set up mesh info */
179 info.do_init = TRUE;
180 info.lwobject = lwobject;
181 info.beginx = 0;
182 info.beginy = 0;
183 info.zoom = 45;
184 trackball( info.quat, 0.0, 0.0, 0.0, 0.0 );
185 }
186
187 void TestGLCanvas::OnMouse( wxMouseEvent& event )
188 {
189 wxSize sz(GetClientSize());
190 if (event.Dragging())
191 {
192 /* drag in progress, simulate trackball */
193 float spin_quat[4];
194 trackball(spin_quat,
195 (2.0*info.beginx - sz.x) / sz.x,
196 ( sz.y - 2.0*info.beginy) / sz.y,
197 ( 2.0*event.GetX() - sz.x) / sz.x,
198 ( sz.y - 2.0*event.GetY()) / sz.y);
199
200 add_quats( spin_quat, info.quat, info.quat );
201
202 /* orientation has changed, redraw mesh */
203 Refresh(FALSE);
204 }
205
206 info.beginx = event.GetX();
207 info.beginy = event.GetY();
208 }
209
210 void TestGLCanvas::InitGL(void)
211 {
212 GLfloat light0_pos[4] = { -50.0, 50.0, 0.0, 0.0 };
213 GLfloat light0_color[4] = { .6, .6, .6, 1.0 }; /* white light */
214 GLfloat light1_pos[4] = { 50.0, 50.0, 0.0, 0.0 };
215 GLfloat light1_color[4] = { .4, .4, 1, 1.0 }; /* cold blue light */
216
217 /* remove back faces */
218 glDisable(GL_CULL_FACE);
219 glEnable(GL_DEPTH_TEST);
220
221 /* speedups */
222 glEnable(GL_DITHER);
223 glShadeModel(GL_SMOOTH);
224 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
225 glHint(GL_POLYGON_SMOOTH_HINT, GL_FASTEST);
226
227 /* light */
228 glLightfv(GL_LIGHT0, GL_POSITION, light0_pos);
229 glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_color);
230 glLightfv(GL_LIGHT1, GL_POSITION, light1_pos);
231 glLightfv(GL_LIGHT1, GL_DIFFUSE, light1_color);
232 glEnable(GL_LIGHT0);
233 glEnable(GL_LIGHT1);
234 glEnable(GL_LIGHTING);
235
236 glColorMaterial(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE);
237 glEnable(GL_COLOR_MATERIAL);
238 }
239
240