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