]>
Commit | Line | Data |
---|---|---|
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 | |
2f6c54eb | 9 | // Licence: wxWindows licence |
8b089c5e JS |
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 | ||
806e2f15 VZ |
28 | #if !wxUSE_GLCANVAS |
29 | #error "OpenGL required: set wxUSE_GLCANVAS to 1 and rebuild the library" | |
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 | ||
5cf036d0 | 45 | // `Main program' equivalent, creating windows and returning main app frame |
bcc4c541 | 46 | bool MyApp::OnInit() |
8b089c5e JS |
47 | { |
48 | ||
5cf036d0 | 49 | // Create the main frame window |
be5a51fb | 50 | MyFrame *frame = new MyFrame(NULL, wxT("wxWidgets OpenGL Penguin Sample"), |
5cf036d0 | 51 | wxDefaultPosition, wxDefaultSize); |
8b089c5e | 52 | |
5cf036d0 DS |
53 | /* Make a menubar */ |
54 | wxMenu *fileMenu = new wxMenu; | |
8b089c5e | 55 | |
5cf036d0 DS |
56 | fileMenu->Append(wxID_EXIT, wxT("E&xit")); |
57 | wxMenuBar *menuBar = new wxMenuBar; | |
58 | menuBar->Append(fileMenu, wxT("&File")); | |
59 | frame->SetMenuBar(menuBar); | |
8b089c5e | 60 | |
5cf036d0 DS |
61 | frame->SetCanvas( new TestGLCanvas(frame, wxID_ANY, wxDefaultPosition, |
62 | wxSize(200, 200), wxSUNKEN_BORDER) ); | |
8b089c5e | 63 | |
5cf036d0 DS |
64 | /* Load file wiht mesh data */ |
65 | frame->GetCanvas()->LoadLWO( wxT("penguin.lwo") ); | |
8b089c5e | 66 | |
5cf036d0 DS |
67 | /* Show the frame */ |
68 | frame->Show(true); | |
69 | ||
70 | return true; | |
8b089c5e JS |
71 | } |
72 | ||
73 | IMPLEMENT_APP(MyApp) | |
74 | ||
75 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
76 | EVT_MENU(wxID_EXIT, MyFrame::OnExit) | |
77 | END_EVENT_TABLE() | |
78 | ||
79 | /* My frame constructor */ | |
80 | MyFrame::MyFrame(wxFrame *frame, const wxString& title, const wxPoint& pos, | |
5cf036d0 DS |
81 | const wxSize& size, long style) |
82 | : wxFrame(frame, wxID_ANY, title, pos, size, style) | |
8b089c5e JS |
83 | { |
84 | m_canvas = NULL; | |
85 | } | |
86 | ||
87 | /* Intercept menu commands */ | |
5cf036d0 | 88 | void MyFrame::OnExit( wxCommandEvent& WXUNUSED(event) ) |
8b089c5e | 89 | { |
5cf036d0 DS |
90 | // true is to force the frame to close |
91 | Close(true); | |
8b089c5e JS |
92 | } |
93 | ||
94 | BEGIN_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) | |
99 | END_EVENT_TABLE() | |
100 | ||
101 | TestGLCanvas::TestGLCanvas(wxWindow *parent, wxWindowID id, | |
5cf036d0 DS |
102 | const wxPoint& pos, const wxSize& size, long style, const wxString& name) |
103 | : wxGLCanvas(parent, id, pos, size, style, name) | |
8b089c5e | 104 | { |
5cf036d0 | 105 | block = false; |
8b089c5e JS |
106 | } |
107 | ||
5cf036d0 | 108 | TestGLCanvas::~TestGLCanvas() |
8b089c5e JS |
109 | { |
110 | /* destroy mesh */ | |
111 | lw_object_free(info.lwobject); | |
112 | } | |
113 | ||
2db98bf5 | 114 | void TestGLCanvas::OnPaint( wxPaintEvent& WXUNUSED(event) ) |
8b089c5e JS |
115 | { |
116 | /* must always be here */ | |
117 | wxPaintDC dc(this); | |
118 | ||
119 | #ifndef __WXMOTIF__ | |
120 | if (!GetContext()) return; | |
121 | #endif | |
122 | ||
123 | SetCurrent(); | |
5cf036d0 DS |
124 | |
125 | // Initialize OpenGL | |
126 | if (info.do_init) | |
8b089c5e JS |
127 | { |
128 | InitGL(); | |
5cf036d0 | 129 | info.do_init = false; |
8b089c5e | 130 | } |
5cf036d0 DS |
131 | |
132 | // View | |
8b089c5e JS |
133 | glMatrixMode( GL_PROJECTION ); |
134 | glLoadIdentity(); | |
5cf036d0 | 135 | gluPerspective( info.zoom, VIEW_ASPECT, 1.0, 100.0 ); |
8b089c5e JS |
136 | glMatrixMode( GL_MODELVIEW ); |
137 | ||
5cf036d0 DS |
138 | // Clear |
139 | glClearColor( 0.3f, 0.4f, 0.6f, 1.0f ); | |
8b089c5e JS |
140 | glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); |
141 | ||
5cf036d0 | 142 | // Transformations |
8b089c5e JS |
143 | GLfloat m[4][4]; |
144 | glLoadIdentity(); | |
5cf036d0 | 145 | glTranslatef( 0.0f, 0.0f, -30.0f ); |
8b089c5e JS |
146 | build_rotmatrix( m,info.quat ); |
147 | glMultMatrixf( &m[0][0] ); | |
148 | ||
5cf036d0 | 149 | // Draw object |
8b089c5e | 150 | lw_object_show( info.lwobject ); |
5cf036d0 DS |
151 | |
152 | // Flush | |
8b089c5e JS |
153 | glFlush(); |
154 | ||
5cf036d0 | 155 | // Swap |
8b089c5e JS |
156 | SwapBuffers(); |
157 | } | |
158 | ||
159 | void TestGLCanvas::OnSize(wxSizeEvent& event) | |
160 | { | |
3dec57ad SC |
161 | // this is also necessary to update the context on some platforms |
162 | wxGLCanvas::OnSize(event); | |
5cf036d0 | 163 | |
9d705dfa | 164 | // set GL viewport (not called by wxGLCanvas::OnSize on all platforms...) |
2f6c54eb VZ |
165 | int w, h; |
166 | GetClientSize(&w, &h); | |
9d705dfa | 167 | #ifndef __WXMOTIF__ |
5cf036d0 | 168 | if ( GetContext() ) |
9d705dfa GD |
169 | #endif |
170 | { | |
171 | SetCurrent(); | |
172 | glViewport(0, 0, (GLint) w, (GLint) h); | |
173 | } | |
8b089c5e JS |
174 | } |
175 | ||
2db98bf5 | 176 | void TestGLCanvas::OnEraseBackground(wxEraseEvent& WXUNUSED(event)) |
8b089c5e JS |
177 | { |
178 | /* Do nothing, to avoid flashing on MSW */ | |
179 | } | |
180 | ||
181 | void TestGLCanvas::LoadLWO(const wxString &filename) | |
182 | { | |
183 | /* test if lightwave object */ | |
bcc4c541 | 184 | if (!lw_is_lwobject(filename.mb_str())) return; |
5cf036d0 | 185 | |
8b089c5e | 186 | /* read lightwave object */ |
bcc4c541 | 187 | lwObject *lwobject = lw_object_read(filename.mb_str()); |
5cf036d0 | 188 | |
8b089c5e JS |
189 | /* scale */ |
190 | lw_object_scale(lwobject, 10.0 / lw_object_radius(lwobject)); | |
5cf036d0 | 191 | |
8b089c5e | 192 | /* set up mesh info */ |
5cf036d0 | 193 | info.do_init = true; |
8b089c5e | 194 | info.lwobject = lwobject; |
5cf036d0 DS |
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 ); | |
8b089c5e JS |
199 | } |
200 | ||
201 | void TestGLCanvas::OnMouse( wxMouseEvent& event ) | |
202 | { | |
5cf036d0 DS |
203 | |
204 | if ( event.Dragging() ) | |
8b089c5e | 205 | { |
5cf036d0 DS |
206 | wxSize sz( GetClientSize() ); |
207 | ||
8b089c5e JS |
208 | /* drag in progress, simulate trackball */ |
209 | float spin_quat[4]; | |
210 | trackball(spin_quat, | |
5cf036d0 DS |
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 | ||
8b089c5e | 216 | add_quats( spin_quat, info.quat, info.quat ); |
2f6c54eb | 217 | |
8b089c5e | 218 | /* orientation has changed, redraw mesh */ |
5cf036d0 | 219 | Refresh(false); |
8b089c5e | 220 | } |
2f6c54eb | 221 | |
8b089c5e JS |
222 | info.beginx = event.GetX(); |
223 | info.beginy = event.GetY(); | |
224 | } | |
225 | ||
5cf036d0 | 226 | void TestGLCanvas::InitGL() |
8b089c5e | 227 | { |
5cf036d0 DS |
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 }; | |
8b089c5e JS |
237 | |
238 | /* remove back faces */ | |
239 | glDisable(GL_CULL_FACE); | |
240 | glEnable(GL_DEPTH_TEST); | |
5cf036d0 | 241 | |
8b089c5e JS |
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); | |
5cf036d0 | 250 | glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_color); |
8b089c5e JS |
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); | |
5cf036d0 DS |
256 | |
257 | glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE); | |
258 | glEnable(GL_COLOR_MATERIAL); | |
8b089c5e JS |
259 | } |
260 |