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