]> git.saurik.com Git - wxWidgets.git/blame - samples/opengl/penguin/penguin.cpp
WinCE build fix.
[wxWidgets.git] / samples / opengl / penguin / penguin.cpp
CommitLineData
8b089c5e
JS
1/////////////////////////////////////////////////////////////////////////////
2// Name: penguin.cpp
3// Purpose: wxGLCanvas demo program
4// Author: Robert Roebling
4b764db3 5// Modified by: Sandro Sigala
8b089c5e
JS
6// Created: 04/01/98
7// RCS-ID: $Id$
8// Copyright: (c) Robert Roebling
2f6c54eb 9// Licence: wxWindows licence
8b089c5e
JS
10/////////////////////////////////////////////////////////////////////////////
11
8b089c5e
JS
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
15#ifdef __BORLANDC__
16#pragma hdrstop
17#endif
18
19#ifndef WX_PRECOMP
20#include "wx/wx.h"
21#endif
22
806e2f15
VZ
23#if !wxUSE_GLCANVAS
24 #error "OpenGL required: set wxUSE_GLCANVAS to 1 and rebuild the library"
25#endif
26
8b089c5e 27#include "penguin.h"
65f4ce38
VZ
28#ifdef __DARWIN__
29 #include <OpenGL/glu.h>
3dec57ad 30#else
65f4ce38 31 #include <GL/glu.h>
3dec57ad 32#endif
8b089c5e 33
3a992940
JS
34#include "../../sample.xpm"
35
4b764db3
JS
36// ---------------------------------------------------------------------------
37// MyApp
38// ---------------------------------------------------------------------------
8b089c5e 39
5cf036d0 40// `Main program' equivalent, creating windows and returning main app frame
bcc4c541 41bool MyApp::OnInit()
8b089c5e 42{
45e6e6f8
VZ
43 if ( !wxApp::OnInit() )
44 return false;
45
5cf036d0 46 // Create the main frame window
4b764db3 47 MyFrame *frame = new MyFrame(NULL, wxT("wxWidgets Penguin Sample"),
5cf036d0 48 wxDefaultPosition, wxDefaultSize);
8b089c5e 49
4b764db3
JS
50#if wxUSE_ZLIB
51 if (wxFileExists(wxT("penguin.dxf.gz")))
52 frame->GetCanvas()->LoadDXF(wxT("penguin.dxf.gz"));
53#else
54 if (wxFileExists(wxT("penguin.dxf")))
55 frame->GetCanvas()->LoadDXF(wxT("penguin.dxf"));
56#endif
8b089c5e 57
5cf036d0
DS
58 /* Show the frame */
59 frame->Show(true);
60
61 return true;
8b089c5e
JS
62}
63
64IMPLEMENT_APP(MyApp)
65
4b764db3
JS
66// ---------------------------------------------------------------------------
67// MyFrame
68// ---------------------------------------------------------------------------
69
8b089c5e 70BEGIN_EVENT_TABLE(MyFrame, wxFrame)
4b764db3
JS
71 EVT_MENU(wxID_OPEN, MyFrame::OnMenuFileOpen)
72 EVT_MENU(wxID_EXIT, MyFrame::OnMenuFileExit)
73 EVT_MENU(wxID_HELP, MyFrame::OnMenuHelpAbout)
8b089c5e
JS
74END_EVENT_TABLE()
75
4b764db3 76// MyFrame constructor
8b089c5e 77MyFrame::MyFrame(wxFrame *frame, const wxString& title, const wxPoint& pos,
5cf036d0
DS
78 const wxSize& size, long style)
79 : wxFrame(frame, wxID_ANY, title, pos, size, style)
8b089c5e 80{
3a992940 81 SetIcon(wxIcon(sample_xpm));
4b764db3
JS
82
83 // Make the "File" menu
84 wxMenu *fileMenu = new wxMenu;
85 fileMenu->Append(wxID_OPEN, wxT("&Open..."));
86 fileMenu->AppendSeparator();
87 fileMenu->Append(wxID_EXIT, wxT("E&xit\tALT-X"));
88 // Make the "Help" menu
89 wxMenu *helpMenu = new wxMenu;
90 helpMenu->Append(wxID_HELP, wxT("&About..."));
91
92 wxMenuBar *menuBar = new wxMenuBar;
93 menuBar->Append(fileMenu, wxT("&File"));
94 menuBar->Append(helpMenu, wxT("&Help"));
95 SetMenuBar(menuBar);
96
97 m_canvas = new TestGLCanvas(this, wxID_ANY, wxDefaultPosition,
98 wxSize(300, 300), wxSUNKEN_BORDER);
8b089c5e
JS
99}
100
4b764db3
JS
101// File|Open... command
102void MyFrame::OnMenuFileOpen( wxCommandEvent& WXUNUSED(event) )
103{
104 wxString filename = wxFileSelector(wxT("Choose DXF Model"), wxT(""), wxT(""), wxT(""),
105#if wxUSE_ZLIB
106 wxT("DXF Drawing (*.dxf;*.dxf.gz)|*.dxf;*.dxf.gz|All files (*.*)|*.*"),
107#else
108 wxT("DXF Drawing (*.dxf)|*.dxf)|All files (*.*)|*.*"),
109#endif
ff3e84ff 110 wxFD_OPEN);
4b764db3
JS
111 if (!filename.IsEmpty())
112 {
113 m_canvas->LoadDXF(filename);
114 m_canvas->Refresh(false);
115 }
116}
117
118// File|Exit command
119void MyFrame::OnMenuFileExit( wxCommandEvent& WXUNUSED(event) )
8b089c5e 120{
5cf036d0
DS
121 // true is to force the frame to close
122 Close(true);
8b089c5e
JS
123}
124
4b764db3
JS
125// Help|About... command
126void MyFrame::OnMenuHelpAbout( wxCommandEvent& WXUNUSED(event) )
127{
128 wxMessageBox(wxT("OpenGL Penguin Sample (c) Robert Roebling, Sandro Sigala et al"));
129}
130
131// ---------------------------------------------------------------------------
132// TestGLCanvas
133// ---------------------------------------------------------------------------
134
8b089c5e
JS
135BEGIN_EVENT_TABLE(TestGLCanvas, wxGLCanvas)
136 EVT_SIZE(TestGLCanvas::OnSize)
137 EVT_PAINT(TestGLCanvas::OnPaint)
138 EVT_ERASE_BACKGROUND(TestGLCanvas::OnEraseBackground)
139 EVT_MOUSE_EVENTS(TestGLCanvas::OnMouse)
140END_EVENT_TABLE()
141
142TestGLCanvas::TestGLCanvas(wxWindow *parent, wxWindowID id,
5cf036d0 143 const wxPoint& pos, const wxSize& size, long style, const wxString& name)
3a992940 144 : wxGLCanvas(parent, id, pos, size, style|wxFULL_REPAINT_ON_RESIZE, name)
8b089c5e 145{
4b764db3
JS
146 m_gldata.initialized = false;
147
148 // initialize view matrix
149 m_gldata.beginx = 0.0f;
150 m_gldata.beginy = 0.0f;
151 m_gldata.zoom = 45.0f;
152 trackball(m_gldata.quat, 0.0f, 0.0f, 0.0f, 0.0f);
8b089c5e
JS
153}
154
5cf036d0 155TestGLCanvas::~TestGLCanvas()
8b089c5e 156{
8b089c5e
JS
157}
158
2db98bf5 159void TestGLCanvas::OnPaint( wxPaintEvent& WXUNUSED(event) )
8b089c5e 160{
4b764db3 161 // must always be here
8b089c5e
JS
162 wxPaintDC dc(this);
163
164#ifndef __WXMOTIF__
165 if (!GetContext()) return;
166#endif
167
168 SetCurrent();
5cf036d0
DS
169
170 // Initialize OpenGL
4b764db3 171 if (!m_gldata.initialized)
8b089c5e
JS
172 {
173 InitGL();
4b764db3
JS
174 ResetProjectionMode();
175 m_gldata.initialized = true;
8b089c5e 176 }
5cf036d0 177
5cf036d0
DS
178 // Clear
179 glClearColor( 0.3f, 0.4f, 0.6f, 1.0f );
8b089c5e
JS
180 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
181
5cf036d0 182 // Transformations
8b089c5e 183 glLoadIdentity();
4b764db3
JS
184 glTranslatef( 0.0f, 0.0f, -20.0f );
185 GLfloat m[4][4];
186 build_rotmatrix( m, m_gldata.quat );
8b089c5e
JS
187 glMultMatrixf( &m[0][0] );
188
4b764db3 189 m_renderer.Render();
5cf036d0
DS
190
191 // Flush
8b089c5e
JS
192 glFlush();
193
5cf036d0 194 // Swap
8b089c5e
JS
195 SwapBuffers();
196}
197
198void TestGLCanvas::OnSize(wxSizeEvent& event)
199{
3dec57ad
SC
200 // this is also necessary to update the context on some platforms
201 wxGLCanvas::OnSize(event);
4b764db3
JS
202 // Reset the OpenGL view aspect
203 ResetProjectionMode();
8b089c5e
JS
204}
205
2db98bf5 206void TestGLCanvas::OnEraseBackground(wxEraseEvent& WXUNUSED(event))
8b089c5e 207{
4b764db3 208 // Do nothing, to avoid flashing on MSW
8b089c5e
JS
209}
210
4b764db3
JS
211// Load the DXF file. If the zlib support is compiled in wxWidgets,
212// supports also the ".dxf.gz" gzip compressed files.
213void TestGLCanvas::LoadDXF(const wxString& filename)
8b089c5e 214{
4b764db3
JS
215 wxFileInputStream stream(filename);
216 if (stream.Ok())
217#if wxUSE_ZLIB
218 {
219 if (filename.Right(3).Lower() == wxT(".gz"))
220 {
221 wxZlibInputStream zstream(stream);
222 m_renderer.Load(zstream);
223 } else
224 {
225 m_renderer.Load(stream);
226 }
227 }
228#else
229 {
230 m_renderer.Load(stream);
231 }
232#endif
8b089c5e
JS
233}
234
4b764db3 235void TestGLCanvas::OnMouse(wxMouseEvent& event)
8b089c5e 236{
4b764db3 237 if (event.Dragging())
8b089c5e 238 {
4b764db3 239 wxSize sz(GetClientSize());
5cf036d0 240
8b089c5e
JS
241 /* drag in progress, simulate trackball */
242 float spin_quat[4];
243 trackball(spin_quat,
4b764db3
JS
244 (2.0*m_gldata.beginx - sz.x) / sz.x,
245 (sz.y - 2.0*m_gldata.beginy) / sz.y,
246 (2.0*event.GetX() - sz.x) / sz.x,
247 (sz.y - 2.0*event.GetY()) / sz.y);
5cf036d0 248
4b764db3 249 add_quats(spin_quat, m_gldata.quat, m_gldata.quat);
2f6c54eb 250
8b089c5e 251 /* orientation has changed, redraw mesh */
5cf036d0 252 Refresh(false);
8b089c5e 253 }
2f6c54eb 254
4b764db3
JS
255 m_gldata.beginx = event.GetX();
256 m_gldata.beginy = event.GetY();
8b089c5e
JS
257}
258
5cf036d0 259void TestGLCanvas::InitGL()
8b089c5e 260{
5cf036d0
DS
261 static const GLfloat light0_pos[4] = { -50.0f, 50.0f, 0.0f, 0.0f };
262
263 // white light
264 static const GLfloat light0_color[4] = { 0.6f, 0.6f, 0.6f, 1.0f };
265
266 static const GLfloat light1_pos[4] = { 50.0f, 50.0f, 0.0f, 0.0f };
267
268 // cold blue light
269 static const GLfloat light1_color[4] = { 0.4f, 0.4f, 1.0f, 1.0f };
8b089c5e
JS
270
271 /* remove back faces */
272 glDisable(GL_CULL_FACE);
273 glEnable(GL_DEPTH_TEST);
5cf036d0 274
8b089c5e
JS
275 /* speedups */
276 glEnable(GL_DITHER);
277 glShadeModel(GL_SMOOTH);
278 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
279 glHint(GL_POLYGON_SMOOTH_HINT, GL_FASTEST);
280
281 /* light */
282 glLightfv(GL_LIGHT0, GL_POSITION, light0_pos);
5cf036d0 283 glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_color);
8b089c5e
JS
284 glLightfv(GL_LIGHT1, GL_POSITION, light1_pos);
285 glLightfv(GL_LIGHT1, GL_DIFFUSE, light1_color);
286 glEnable(GL_LIGHT0);
287 glEnable(GL_LIGHT1);
288 glEnable(GL_LIGHTING);
5cf036d0
DS
289
290 glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
291 glEnable(GL_COLOR_MATERIAL);
8b089c5e
JS
292}
293
4b764db3
JS
294void TestGLCanvas::ResetProjectionMode()
295{
296 int w, h;
297 GetClientSize(&w, &h);
298#ifndef __WXMOTIF__
299 if ( GetContext() )
300#endif
301 {
302 SetCurrent();
303 glViewport(0, 0, (GLint) w, (GLint) h);
304 glMatrixMode(GL_PROJECTION);
305 glLoadIdentity();
306 gluPerspective(45.0f, (GLfloat)w/h, 1.0, 100.0);
307 glMatrixMode(GL_MODELVIEW);
308 glLoadIdentity();
309 }
310}