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