]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: penguin.cpp | |
3 | // Purpose: wxGLCanvas demo program | |
4 | // Author: Robert Roebling | |
5 | // Modified by: Sandro Sigala | |
6 | // Created: 04/01/98 | |
7 | // Copyright: (c) Robert Roebling | |
8 | // Licence: wxWindows licence | |
9 | ///////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | // For compilers that support precompilation, includes "wx.h". | |
12 | #include "wx/wxprec.h" | |
13 | ||
14 | #ifdef __BORLANDC__ | |
15 | #pragma hdrstop | |
16 | #endif | |
17 | ||
18 | #ifndef WX_PRECOMP | |
19 | #include "wx/wx.h" | |
20 | #endif | |
21 | ||
22 | #if !wxUSE_GLCANVAS | |
23 | #error "OpenGL required: set wxUSE_GLCANVAS to 1 and rebuild the library" | |
24 | #endif | |
25 | ||
26 | #include "penguin.h" | |
27 | #ifdef __DARWIN__ | |
28 | #include <OpenGL/glu.h> | |
29 | #else | |
30 | #include <GL/glu.h> | |
31 | #endif | |
32 | ||
33 | #include "../../sample.xpm" | |
34 | ||
35 | // --------------------------------------------------------------------------- | |
36 | // MyApp | |
37 | // --------------------------------------------------------------------------- | |
38 | ||
39 | // `Main program' equivalent, creating windows and returning main app frame | |
40 | bool MyApp::OnInit() | |
41 | { | |
42 | if ( !wxApp::OnInit() ) | |
43 | return false; | |
44 | ||
45 | // Create the main frame window | |
46 | MyFrame *frame = new MyFrame(NULL, wxT("wxWidgets Penguin Sample"), | |
47 | wxDefaultPosition, wxDefaultSize); | |
48 | ||
49 | #if wxUSE_ZLIB | |
50 | if (wxFileExists(wxT("penguin.dxf.gz"))) | |
51 | frame->GetCanvas()->LoadDXF(wxT("penguin.dxf.gz")); | |
52 | #else | |
53 | if (wxFileExists(wxT("penguin.dxf"))) | |
54 | frame->GetCanvas()->LoadDXF(wxT("penguin.dxf")); | |
55 | #endif | |
56 | ||
57 | /* Show the frame */ | |
58 | frame->Show(true); | |
59 | ||
60 | return true; | |
61 | } | |
62 | ||
63 | IMPLEMENT_APP(MyApp) | |
64 | ||
65 | // --------------------------------------------------------------------------- | |
66 | // MyFrame | |
67 | // --------------------------------------------------------------------------- | |
68 | ||
69 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
70 | EVT_MENU(wxID_OPEN, MyFrame::OnMenuFileOpen) | |
71 | EVT_MENU(wxID_EXIT, MyFrame::OnMenuFileExit) | |
72 | EVT_MENU(wxID_HELP, MyFrame::OnMenuHelpAbout) | |
73 | END_EVENT_TABLE() | |
74 | ||
75 | // MyFrame constructor | |
76 | MyFrame::MyFrame(wxFrame *frame, const wxString& title, const wxPoint& pos, | |
77 | const wxSize& size, long style) | |
78 | : wxFrame(frame, wxID_ANY, title, pos, size, style) | |
79 | { | |
80 | SetIcon(wxICON(sample)); | |
81 | ||
82 | // Make the "File" menu | |
83 | wxMenu *fileMenu = new wxMenu; | |
84 | fileMenu->Append(wxID_OPEN, wxT("&Open...")); | |
85 | fileMenu->AppendSeparator(); | |
86 | fileMenu->Append(wxID_EXIT, wxT("E&xit\tALT-X")); | |
87 | // Make the "Help" menu | |
88 | wxMenu *helpMenu = new wxMenu; | |
89 | helpMenu->Append(wxID_HELP, wxT("&About")); | |
90 | ||
91 | wxMenuBar *menuBar = new wxMenuBar; | |
92 | menuBar->Append(fileMenu, wxT("&File")); | |
93 | menuBar->Append(helpMenu, wxT("&Help")); | |
94 | SetMenuBar(menuBar); | |
95 | ||
96 | Show(true); | |
97 | ||
98 | m_canvas = new TestGLCanvas(this, wxID_ANY, wxDefaultPosition, | |
99 | GetClientSize(), wxSUNKEN_BORDER); | |
100 | } | |
101 | ||
102 | // File|Open... command | |
103 | void MyFrame::OnMenuFileOpen( wxCommandEvent& WXUNUSED(event) ) | |
104 | { | |
105 | wxString filename = wxFileSelector(wxT("Choose DXF Model"), wxT(""), wxT(""), wxT(""), | |
106 | #if wxUSE_ZLIB | |
107 | wxT("DXF Drawing (*.dxf;*.dxf.gz)|*.dxf;*.dxf.gz|All files (*.*)|*.*"), | |
108 | #else | |
109 | wxT("DXF Drawing (*.dxf)|*.dxf)|All files (*.*)|*.*"), | |
110 | #endif | |
111 | wxFD_OPEN); | |
112 | if (!filename.IsEmpty()) | |
113 | { | |
114 | m_canvas->LoadDXF(filename); | |
115 | m_canvas->Refresh(false); | |
116 | } | |
117 | } | |
118 | ||
119 | // File|Exit command | |
120 | void MyFrame::OnMenuFileExit( wxCommandEvent& WXUNUSED(event) ) | |
121 | { | |
122 | // true is to force the frame to close | |
123 | Close(true); | |
124 | } | |
125 | ||
126 | // Help|About command | |
127 | void MyFrame::OnMenuHelpAbout( wxCommandEvent& WXUNUSED(event) ) | |
128 | { | |
129 | wxMessageBox(wxT("OpenGL Penguin Sample (c) Robert Roebling, Sandro Sigala et al")); | |
130 | } | |
131 | ||
132 | // --------------------------------------------------------------------------- | |
133 | // TestGLCanvas | |
134 | // --------------------------------------------------------------------------- | |
135 | ||
136 | BEGIN_EVENT_TABLE(TestGLCanvas, wxGLCanvas) | |
137 | EVT_SIZE(TestGLCanvas::OnSize) | |
138 | EVT_PAINT(TestGLCanvas::OnPaint) | |
139 | EVT_ERASE_BACKGROUND(TestGLCanvas::OnEraseBackground) | |
140 | EVT_MOUSE_EVENTS(TestGLCanvas::OnMouse) | |
141 | END_EVENT_TABLE() | |
142 | ||
143 | TestGLCanvas::TestGLCanvas(wxWindow *parent, | |
144 | wxWindowID id, | |
145 | const wxPoint& pos, | |
146 | const wxSize& size, | |
147 | long style, | |
148 | const wxString& name) | |
149 | : wxGLCanvas(parent, id, NULL, pos, size, | |
150 | style | wxFULL_REPAINT_ON_RESIZE, name) | |
151 | { | |
152 | // Explicitly create a new rendering context instance for this canvas. | |
153 | m_glRC = new wxGLContext(this); | |
154 | ||
155 | m_gldata.initialized = false; | |
156 | ||
157 | // initialize view matrix | |
158 | m_gldata.beginx = 0.0f; | |
159 | m_gldata.beginy = 0.0f; | |
160 | m_gldata.zoom = 45.0f; | |
161 | trackball(m_gldata.quat, 0.0f, 0.0f, 0.0f, 0.0f); | |
162 | } | |
163 | ||
164 | TestGLCanvas::~TestGLCanvas() | |
165 | { | |
166 | delete m_glRC; | |
167 | } | |
168 | ||
169 | void TestGLCanvas::OnPaint( wxPaintEvent& WXUNUSED(event) ) | |
170 | { | |
171 | // must always be here | |
172 | wxPaintDC dc(this); | |
173 | ||
174 | SetCurrent(*m_glRC); | |
175 | ||
176 | // Initialize OpenGL | |
177 | if (!m_gldata.initialized) | |
178 | { | |
179 | InitGL(); | |
180 | ResetProjectionMode(); | |
181 | m_gldata.initialized = true; | |
182 | } | |
183 | ||
184 | // Clear | |
185 | glClearColor( 0.3f, 0.4f, 0.6f, 1.0f ); | |
186 | glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); | |
187 | ||
188 | // Transformations | |
189 | glLoadIdentity(); | |
190 | glTranslatef( 0.0f, 0.0f, -20.0f ); | |
191 | GLfloat m[4][4]; | |
192 | build_rotmatrix( m, m_gldata.quat ); | |
193 | glMultMatrixf( &m[0][0] ); | |
194 | ||
195 | m_renderer.Render(); | |
196 | ||
197 | // Flush | |
198 | glFlush(); | |
199 | ||
200 | // Swap | |
201 | SwapBuffers(); | |
202 | } | |
203 | ||
204 | void TestGLCanvas::OnSize(wxSizeEvent& WXUNUSED(event)) | |
205 | { | |
206 | // Reset the OpenGL view aspect. | |
207 | // This is OK only because there is only one canvas that uses the context. | |
208 | // See the cube sample for that case that multiple canvases are made current with one context. | |
209 | ResetProjectionMode(); | |
210 | } | |
211 | ||
212 | void TestGLCanvas::OnEraseBackground(wxEraseEvent& WXUNUSED(event)) | |
213 | { | |
214 | // Do nothing, to avoid flashing on MSW | |
215 | } | |
216 | ||
217 | // Load the DXF file. If the zlib support is compiled in wxWidgets, | |
218 | // supports also the ".dxf.gz" gzip compressed files. | |
219 | void TestGLCanvas::LoadDXF(const wxString& filename) | |
220 | { | |
221 | wxFileInputStream stream(filename); | |
222 | if (stream.IsOk()) | |
223 | #if wxUSE_ZLIB | |
224 | { | |
225 | if (filename.Right(3).Lower() == wxT(".gz")) | |
226 | { | |
227 | wxZlibInputStream zstream(stream); | |
228 | m_renderer.Load(zstream); | |
229 | } else | |
230 | { | |
231 | m_renderer.Load(stream); | |
232 | } | |
233 | } | |
234 | #else | |
235 | { | |
236 | m_renderer.Load(stream); | |
237 | } | |
238 | #endif | |
239 | } | |
240 | ||
241 | void TestGLCanvas::OnMouse(wxMouseEvent& event) | |
242 | { | |
243 | if (event.Dragging()) | |
244 | { | |
245 | wxSize sz(GetClientSize()); | |
246 | ||
247 | /* drag in progress, simulate trackball */ | |
248 | float spin_quat[4]; | |
249 | trackball(spin_quat, | |
250 | (2.0*m_gldata.beginx - sz.x) / sz.x, | |
251 | (sz.y - 2.0*m_gldata.beginy) / sz.y, | |
252 | (2.0*event.GetX() - sz.x) / sz.x, | |
253 | (sz.y - 2.0*event.GetY()) / sz.y); | |
254 | ||
255 | add_quats(spin_quat, m_gldata.quat, m_gldata.quat); | |
256 | ||
257 | /* orientation has changed, redraw mesh */ | |
258 | Refresh(false); | |
259 | } | |
260 | ||
261 | m_gldata.beginx = event.GetX(); | |
262 | m_gldata.beginy = event.GetY(); | |
263 | } | |
264 | ||
265 | void TestGLCanvas::InitGL() | |
266 | { | |
267 | static const GLfloat light0_pos[4] = { -50.0f, 50.0f, 0.0f, 0.0f }; | |
268 | ||
269 | // white light | |
270 | static const GLfloat light0_color[4] = { 0.6f, 0.6f, 0.6f, 1.0f }; | |
271 | ||
272 | static const GLfloat light1_pos[4] = { 50.0f, 50.0f, 0.0f, 0.0f }; | |
273 | ||
274 | // cold blue light | |
275 | static const GLfloat light1_color[4] = { 0.4f, 0.4f, 1.0f, 1.0f }; | |
276 | ||
277 | /* remove back faces */ | |
278 | glEnable(GL_CULL_FACE); | |
279 | glEnable(GL_DEPTH_TEST); | |
280 | ||
281 | /* speedups */ | |
282 | glEnable(GL_DITHER); | |
283 | glShadeModel(GL_SMOOTH); | |
284 | glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST); | |
285 | glHint(GL_POLYGON_SMOOTH_HINT, GL_FASTEST); | |
286 | ||
287 | /* light */ | |
288 | glLightfv(GL_LIGHT0, GL_POSITION, light0_pos); | |
289 | glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_color); | |
290 | glLightfv(GL_LIGHT1, GL_POSITION, light1_pos); | |
291 | glLightfv(GL_LIGHT1, GL_DIFFUSE, light1_color); | |
292 | glEnable(GL_LIGHT0); | |
293 | glEnable(GL_LIGHT1); | |
294 | glEnable(GL_LIGHTING); | |
295 | ||
296 | glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE); | |
297 | glEnable(GL_COLOR_MATERIAL); | |
298 | } | |
299 | ||
300 | void TestGLCanvas::ResetProjectionMode() | |
301 | { | |
302 | if ( !IsShownOnScreen() ) | |
303 | return; | |
304 | ||
305 | // This is normally only necessary if there is more than one wxGLCanvas | |
306 | // or more than one wxGLContext in the application. | |
307 | SetCurrent(*m_glRC); | |
308 | ||
309 | int w, h; | |
310 | GetClientSize(&w, &h); | |
311 | ||
312 | // It's up to the application code to update the OpenGL viewport settings. | |
313 | // In order to avoid extensive context switching, consider doing this in | |
314 | // OnPaint() rather than here, though. | |
315 | glViewport(0, 0, (GLint) w, (GLint) h); | |
316 | ||
317 | glMatrixMode(GL_PROJECTION); | |
318 | glLoadIdentity(); | |
319 | gluPerspective(45.0f, (GLfloat)w/h, 1.0, 100.0); | |
320 | glMatrixMode(GL_MODELVIEW); | |
321 | glLoadIdentity(); | |
322 | } |