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