]>
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 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Robert Roebling | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
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 | ||
23 | #if !wxUSE_GLCANVAS | |
24 | #error "OpenGL required: set wxUSE_GLCANVAS to 1 and rebuild the library" | |
25 | #endif | |
26 | ||
27 | #include "penguin.h" | |
28 | #ifdef __WXMAC__ | |
29 | # ifdef __DARWIN__ | |
30 | # include <OpenGL/glu.h> | |
31 | # else | |
32 | # include <glu.h> | |
33 | # endif | |
34 | #else | |
35 | # include <GL/glu.h> | |
36 | #endif | |
37 | ||
38 | #include "../../sample.xpm" | |
39 | ||
40 | // --------------------------------------------------------------------------- | |
41 | // MyApp | |
42 | // --------------------------------------------------------------------------- | |
43 | ||
44 | // `Main program' equivalent, creating windows and returning main app frame | |
45 | bool MyApp::OnInit() | |
46 | { | |
47 | // Create the main frame window | |
48 | MyFrame *frame = new MyFrame(NULL, wxT("wxWidgets Penguin Sample"), | |
49 | wxDefaultPosition, wxDefaultSize); | |
50 | ||
51 | #if wxUSE_ZLIB | |
52 | if (wxFileExists(wxT("penguin.dxf.gz"))) | |
53 | frame->GetCanvas()->LoadDXF(wxT("penguin.dxf.gz")); | |
54 | #else | |
55 | if (wxFileExists(wxT("penguin.dxf"))) | |
56 | frame->GetCanvas()->LoadDXF(wxT("penguin.dxf")); | |
57 | #endif | |
58 | ||
59 | /* Show the frame */ | |
60 | frame->Show(true); | |
61 | ||
62 | return true; | |
63 | } | |
64 | ||
65 | IMPLEMENT_APP(MyApp) | |
66 | ||
67 | // --------------------------------------------------------------------------- | |
68 | // MyFrame | |
69 | // --------------------------------------------------------------------------- | |
70 | ||
71 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
72 | EVT_MENU(wxID_OPEN, MyFrame::OnMenuFileOpen) | |
73 | EVT_MENU(wxID_EXIT, MyFrame::OnMenuFileExit) | |
74 | EVT_MENU(wxID_HELP, MyFrame::OnMenuHelpAbout) | |
75 | END_EVENT_TABLE() | |
76 | ||
77 | // MyFrame constructor | |
78 | MyFrame::MyFrame(wxFrame *frame, const wxString& title, const wxPoint& pos, | |
79 | const wxSize& size, long style) | |
80 | : wxFrame(frame, wxID_ANY, title, pos, size, style) | |
81 | { | |
82 | SetIcon(wxIcon(sample_xpm)); | |
83 | ||
84 | // Make the "File" menu | |
85 | wxMenu *fileMenu = new wxMenu; | |
86 | fileMenu->Append(wxID_OPEN, wxT("&Open...")); | |
87 | fileMenu->AppendSeparator(); | |
88 | fileMenu->Append(wxID_EXIT, wxT("E&xit\tALT-X")); | |
89 | // Make the "Help" menu | |
90 | wxMenu *helpMenu = new wxMenu; | |
91 | helpMenu->Append(wxID_HELP, wxT("&About...")); | |
92 | ||
93 | wxMenuBar *menuBar = new wxMenuBar; | |
94 | menuBar->Append(fileMenu, wxT("&File")); | |
95 | menuBar->Append(helpMenu, wxT("&Help")); | |
96 | SetMenuBar(menuBar); | |
97 | ||
98 | m_canvas = new TestGLCanvas(this, wxID_ANY, wxDefaultPosition, | |
99 | wxSize(300, 300), 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 | wxOPEN); | |
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, wxWindowID id, | |
144 | const wxPoint& pos, const wxSize& size, long style, const wxString& name) | |
145 | : wxGLCanvas(parent, id, pos, size, style|wxFULL_REPAINT_ON_RESIZE, name) | |
146 | { | |
147 | m_gldata.initialized = false; | |
148 | ||
149 | // initialize view matrix | |
150 | m_gldata.beginx = 0.0f; | |
151 | m_gldata.beginy = 0.0f; | |
152 | m_gldata.zoom = 45.0f; | |
153 | trackball(m_gldata.quat, 0.0f, 0.0f, 0.0f, 0.0f); | |
154 | } | |
155 | ||
156 | TestGLCanvas::~TestGLCanvas() | |
157 | { | |
158 | } | |
159 | ||
160 | void TestGLCanvas::OnPaint( wxPaintEvent& WXUNUSED(event) ) | |
161 | { | |
162 | // must always be here | |
163 | wxPaintDC dc(this); | |
164 | ||
165 | #ifndef __WXMOTIF__ | |
166 | if (!GetContext()) return; | |
167 | #endif | |
168 | ||
169 | SetCurrent(); | |
170 | ||
171 | // Initialize OpenGL | |
172 | if (!m_gldata.initialized) | |
173 | { | |
174 | InitGL(); | |
175 | ResetProjectionMode(); | |
176 | m_gldata.initialized = true; | |
177 | } | |
178 | ||
179 | // Clear | |
180 | glClearColor( 0.3f, 0.4f, 0.6f, 1.0f ); | |
181 | glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); | |
182 | ||
183 | // Transformations | |
184 | glLoadIdentity(); | |
185 | glTranslatef( 0.0f, 0.0f, -20.0f ); | |
186 | GLfloat m[4][4]; | |
187 | build_rotmatrix( m, m_gldata.quat ); | |
188 | glMultMatrixf( &m[0][0] ); | |
189 | ||
190 | m_renderer.Render(); | |
191 | ||
192 | // Flush | |
193 | glFlush(); | |
194 | ||
195 | // Swap | |
196 | SwapBuffers(); | |
197 | } | |
198 | ||
199 | void TestGLCanvas::OnSize(wxSizeEvent& event) | |
200 | { | |
201 | // this is also necessary to update the context on some platforms | |
202 | wxGLCanvas::OnSize(event); | |
203 | // Reset the OpenGL view aspect | |
204 | ResetProjectionMode(); | |
205 | } | |
206 | ||
207 | void TestGLCanvas::OnEraseBackground(wxEraseEvent& WXUNUSED(event)) | |
208 | { | |
209 | // Do nothing, to avoid flashing on MSW | |
210 | } | |
211 | ||
212 | // Load the DXF file. If the zlib support is compiled in wxWidgets, | |
213 | // supports also the ".dxf.gz" gzip compressed files. | |
214 | void TestGLCanvas::LoadDXF(const wxString& filename) | |
215 | { | |
216 | wxFileInputStream stream(filename); | |
217 | if (stream.Ok()) | |
218 | #if wxUSE_ZLIB | |
219 | { | |
220 | if (filename.Right(3).Lower() == wxT(".gz")) | |
221 | { | |
222 | wxZlibInputStream zstream(stream); | |
223 | m_renderer.Load(zstream); | |
224 | } else | |
225 | { | |
226 | m_renderer.Load(stream); | |
227 | } | |
228 | } | |
229 | #else | |
230 | { | |
231 | m_renderer.Load(stream); | |
232 | } | |
233 | #endif | |
234 | } | |
235 | ||
236 | void TestGLCanvas::OnMouse(wxMouseEvent& event) | |
237 | { | |
238 | if (event.Dragging()) | |
239 | { | |
240 | wxSize sz(GetClientSize()); | |
241 | ||
242 | /* drag in progress, simulate trackball */ | |
243 | float spin_quat[4]; | |
244 | trackball(spin_quat, | |
245 | (2.0*m_gldata.beginx - sz.x) / sz.x, | |
246 | (sz.y - 2.0*m_gldata.beginy) / sz.y, | |
247 | (2.0*event.GetX() - sz.x) / sz.x, | |
248 | (sz.y - 2.0*event.GetY()) / sz.y); | |
249 | ||
250 | add_quats(spin_quat, m_gldata.quat, m_gldata.quat); | |
251 | ||
252 | /* orientation has changed, redraw mesh */ | |
253 | Refresh(false); | |
254 | } | |
255 | ||
256 | m_gldata.beginx = event.GetX(); | |
257 | m_gldata.beginy = event.GetY(); | |
258 | } | |
259 | ||
260 | void TestGLCanvas::InitGL() | |
261 | { | |
262 | static const GLfloat light0_pos[4] = { -50.0f, 50.0f, 0.0f, 0.0f }; | |
263 | ||
264 | // white light | |
265 | static const GLfloat light0_color[4] = { 0.6f, 0.6f, 0.6f, 1.0f }; | |
266 | ||
267 | static const GLfloat light1_pos[4] = { 50.0f, 50.0f, 0.0f, 0.0f }; | |
268 | ||
269 | // cold blue light | |
270 | static const GLfloat light1_color[4] = { 0.4f, 0.4f, 1.0f, 1.0f }; | |
271 | ||
272 | /* remove back faces */ | |
273 | glDisable(GL_CULL_FACE); | |
274 | glEnable(GL_DEPTH_TEST); | |
275 | ||
276 | /* speedups */ | |
277 | glEnable(GL_DITHER); | |
278 | glShadeModel(GL_SMOOTH); | |
279 | glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST); | |
280 | glHint(GL_POLYGON_SMOOTH_HINT, GL_FASTEST); | |
281 | ||
282 | /* light */ | |
283 | glLightfv(GL_LIGHT0, GL_POSITION, light0_pos); | |
284 | glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_color); | |
285 | glLightfv(GL_LIGHT1, GL_POSITION, light1_pos); | |
286 | glLightfv(GL_LIGHT1, GL_DIFFUSE, light1_color); | |
287 | glEnable(GL_LIGHT0); | |
288 | glEnable(GL_LIGHT1); | |
289 | glEnable(GL_LIGHTING); | |
290 | ||
291 | glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE); | |
292 | glEnable(GL_COLOR_MATERIAL); | |
293 | } | |
294 | ||
295 | void TestGLCanvas::ResetProjectionMode() | |
296 | { | |
297 | int w, h; | |
298 | GetClientSize(&w, &h); | |
299 | #ifndef __WXMOTIF__ | |
300 | if ( GetContext() ) | |
301 | #endif | |
302 | { | |
303 | SetCurrent(); | |
304 | glViewport(0, 0, (GLint) w, (GLint) h); | |
305 | glMatrixMode(GL_PROJECTION); | |
306 | glLoadIdentity(); | |
307 | gluPerspective(45.0f, (GLfloat)w/h, 1.0, 100.0); | |
308 | glMatrixMode(GL_MODELVIEW); | |
309 | glLoadIdentity(); | |
310 | } | |
311 | } |