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