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