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