]> git.saurik.com Git - wxWidgets.git/blame - samples/opengl/penguin/penguin.cpp
Extract compiler-specific macro definitions in a new wx/compiler.h.
[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{
3cb332c1 81 SetIcon(wxICON(sample));
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;
2d143b66 90 helpMenu->Append(wxID_HELP, wxT("&About"));
4b764db3
JS
91
92 wxMenuBar *menuBar = new wxMenuBar;
93 menuBar->Append(fileMenu, wxT("&File"));
94 menuBar->Append(helpMenu, wxT("&Help"));
95 SetMenuBar(menuBar);
96
451c13c8
VZ
97 Show(true);
98
4b764db3 99 m_canvas = new TestGLCanvas(this, wxID_ANY, wxDefaultPosition,
451c13c8 100 GetClientSize(), wxSUNKEN_BORDER);
8b089c5e
JS
101}
102
4b764db3
JS
103// File|Open... command
104void MyFrame::OnMenuFileOpen( wxCommandEvent& WXUNUSED(event) )
105{
106 wxString filename = wxFileSelector(wxT("Choose DXF Model"), wxT(""), wxT(""), wxT(""),
107#if wxUSE_ZLIB
108 wxT("DXF Drawing (*.dxf;*.dxf.gz)|*.dxf;*.dxf.gz|All files (*.*)|*.*"),
109#else
110 wxT("DXF Drawing (*.dxf)|*.dxf)|All files (*.*)|*.*"),
111#endif
ff3e84ff 112 wxFD_OPEN);
4b764db3
JS
113 if (!filename.IsEmpty())
114 {
115 m_canvas->LoadDXF(filename);
116 m_canvas->Refresh(false);
117 }
118}
119
120// File|Exit command
121void MyFrame::OnMenuFileExit( wxCommandEvent& WXUNUSED(event) )
8b089c5e 122{
5cf036d0
DS
123 // true is to force the frame to close
124 Close(true);
8b089c5e
JS
125}
126
2d143b66 127// Help|About command
4b764db3
JS
128void MyFrame::OnMenuHelpAbout( wxCommandEvent& WXUNUSED(event) )
129{
130 wxMessageBox(wxT("OpenGL Penguin Sample (c) Robert Roebling, Sandro Sigala et al"));
131}
132
133// ---------------------------------------------------------------------------
134// TestGLCanvas
135// ---------------------------------------------------------------------------
136
8b089c5e
JS
137BEGIN_EVENT_TABLE(TestGLCanvas, wxGLCanvas)
138 EVT_SIZE(TestGLCanvas::OnSize)
139 EVT_PAINT(TestGLCanvas::OnPaint)
140 EVT_ERASE_BACKGROUND(TestGLCanvas::OnEraseBackground)
141 EVT_MOUSE_EVENTS(TestGLCanvas::OnMouse)
142END_EVENT_TABLE()
143
451c13c8
VZ
144TestGLCanvas::TestGLCanvas(wxWindow *parent,
145 wxWindowID id,
146 const wxPoint& pos,
147 const wxSize& size,
148 long style,
149 const wxString& name)
150 : wxGLCanvas(parent, id, NULL, pos, size,
151 style | wxFULL_REPAINT_ON_RESIZE, name)
8b089c5e 152{
451c13c8
VZ
153 // Explicitly create a new rendering context instance for this canvas.
154 m_glRC = new wxGLContext(this);
155
4b764db3
JS
156 m_gldata.initialized = false;
157
158 // initialize view matrix
159 m_gldata.beginx = 0.0f;
160 m_gldata.beginy = 0.0f;
161 m_gldata.zoom = 45.0f;
162 trackball(m_gldata.quat, 0.0f, 0.0f, 0.0f, 0.0f);
8b089c5e
JS
163}
164
5cf036d0 165TestGLCanvas::~TestGLCanvas()
8b089c5e 166{
451c13c8 167 delete m_glRC;
8b089c5e
JS
168}
169
2db98bf5 170void TestGLCanvas::OnPaint( wxPaintEvent& WXUNUSED(event) )
8b089c5e 171{
4b764db3 172 // must always be here
8b089c5e
JS
173 wxPaintDC dc(this);
174
451c13c8 175 SetCurrent(*m_glRC);
5cf036d0
DS
176
177 // Initialize OpenGL
4b764db3 178 if (!m_gldata.initialized)
8b089c5e
JS
179 {
180 InitGL();
4b764db3
JS
181 ResetProjectionMode();
182 m_gldata.initialized = true;
8b089c5e 183 }
5cf036d0 184
5cf036d0
DS
185 // Clear
186 glClearColor( 0.3f, 0.4f, 0.6f, 1.0f );
8b089c5e
JS
187 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
188
5cf036d0 189 // Transformations
8b089c5e 190 glLoadIdentity();
4b764db3
JS
191 glTranslatef( 0.0f, 0.0f, -20.0f );
192 GLfloat m[4][4];
193 build_rotmatrix( m, m_gldata.quat );
8b089c5e
JS
194 glMultMatrixf( &m[0][0] );
195
4b764db3 196 m_renderer.Render();
5cf036d0
DS
197
198 // Flush
8b089c5e
JS
199 glFlush();
200
5cf036d0 201 // Swap
8b089c5e
JS
202 SwapBuffers();
203}
204
451c13c8 205void TestGLCanvas::OnSize(wxSizeEvent& WXUNUSED(event))
8b089c5e 206{
451c13c8
VZ
207 // Reset the OpenGL view aspect.
208 // This is OK only because there is only one canvas that uses the context.
209 // See the cube sample for that case that multiple canvases are made current with one context.
4b764db3 210 ResetProjectionMode();
8b089c5e
JS
211}
212
2db98bf5 213void TestGLCanvas::OnEraseBackground(wxEraseEvent& WXUNUSED(event))
8b089c5e 214{
4b764db3 215 // Do nothing, to avoid flashing on MSW
8b089c5e
JS
216}
217
4b764db3
JS
218// Load the DXF file. If the zlib support is compiled in wxWidgets,
219// supports also the ".dxf.gz" gzip compressed files.
220void TestGLCanvas::LoadDXF(const wxString& filename)
8b089c5e 221{
4b764db3 222 wxFileInputStream stream(filename);
a1b806b9 223 if (stream.IsOk())
4b764db3
JS
224#if wxUSE_ZLIB
225 {
226 if (filename.Right(3).Lower() == wxT(".gz"))
227 {
228 wxZlibInputStream zstream(stream);
229 m_renderer.Load(zstream);
230 } else
231 {
232 m_renderer.Load(stream);
233 }
234 }
235#else
236 {
237 m_renderer.Load(stream);
238 }
239#endif
8b089c5e
JS
240}
241
4b764db3 242void TestGLCanvas::OnMouse(wxMouseEvent& event)
8b089c5e 243{
4b764db3 244 if (event.Dragging())
8b089c5e 245 {
4b764db3 246 wxSize sz(GetClientSize());
5cf036d0 247
8b089c5e
JS
248 /* drag in progress, simulate trackball */
249 float spin_quat[4];
250 trackball(spin_quat,
4b764db3
JS
251 (2.0*m_gldata.beginx - sz.x) / sz.x,
252 (sz.y - 2.0*m_gldata.beginy) / sz.y,
253 (2.0*event.GetX() - sz.x) / sz.x,
254 (sz.y - 2.0*event.GetY()) / sz.y);
5cf036d0 255
4b764db3 256 add_quats(spin_quat, m_gldata.quat, m_gldata.quat);
2f6c54eb 257
8b089c5e 258 /* orientation has changed, redraw mesh */
5cf036d0 259 Refresh(false);
8b089c5e 260 }
2f6c54eb 261
4b764db3
JS
262 m_gldata.beginx = event.GetX();
263 m_gldata.beginy = event.GetY();
8b089c5e
JS
264}
265
5cf036d0 266void TestGLCanvas::InitGL()
8b089c5e 267{
5cf036d0
DS
268 static const GLfloat light0_pos[4] = { -50.0f, 50.0f, 0.0f, 0.0f };
269
270 // white light
271 static const GLfloat light0_color[4] = { 0.6f, 0.6f, 0.6f, 1.0f };
272
273 static const GLfloat light1_pos[4] = { 50.0f, 50.0f, 0.0f, 0.0f };
274
275 // cold blue light
276 static const GLfloat light1_color[4] = { 0.4f, 0.4f, 1.0f, 1.0f };
8b089c5e
JS
277
278 /* remove back faces */
948f48e7 279 glEnable(GL_CULL_FACE);
8b089c5e 280 glEnable(GL_DEPTH_TEST);
5cf036d0 281
8b089c5e
JS
282 /* speedups */
283 glEnable(GL_DITHER);
284 glShadeModel(GL_SMOOTH);
285 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
286 glHint(GL_POLYGON_SMOOTH_HINT, GL_FASTEST);
287
288 /* light */
289 glLightfv(GL_LIGHT0, GL_POSITION, light0_pos);
5cf036d0 290 glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_color);
8b089c5e
JS
291 glLightfv(GL_LIGHT1, GL_POSITION, light1_pos);
292 glLightfv(GL_LIGHT1, GL_DIFFUSE, light1_color);
293 glEnable(GL_LIGHT0);
294 glEnable(GL_LIGHT1);
295 glEnable(GL_LIGHTING);
5cf036d0
DS
296
297 glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
298 glEnable(GL_COLOR_MATERIAL);
8b089c5e
JS
299}
300
4b764db3
JS
301void TestGLCanvas::ResetProjectionMode()
302{
97f85100
VZ
303 if ( !IsShownOnScreen() )
304 return;
305
451c13c8
VZ
306 // This is normally only necessary if there is more than one wxGLCanvas
307 // or more than one wxGLContext in the application.
308 SetCurrent(*m_glRC);
309
4b764db3
JS
310 int w, h;
311 GetClientSize(&w, &h);
451c13c8
VZ
312
313 // It's up to the application code to update the OpenGL viewport settings.
314 // In order to avoid extensive context switching, consider doing this in
315 // OnPaint() rather than here, though.
316 glViewport(0, 0, (GLint) w, (GLint) h);
317
318 glMatrixMode(GL_PROJECTION);
319 glLoadIdentity();
320 gluPerspective(45.0f, (GLfloat)w/h, 1.0, 100.0);
321 glMatrixMode(GL_MODELVIEW);
322 glLoadIdentity();
4b764db3 323}