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