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