1 | /************************************************************************
|
---|
2 |
|
---|
3 | MxStdPane
|
---|
4 |
|
---|
5 | Copyright (C) 1998 Michael Garland. See "COPYING.txt" for details.
|
---|
6 |
|
---|
7 | $Id: MxStdPane.cxx,v 1.1 2002/09/24 16:53:54 wimmer Exp $
|
---|
8 |
|
---|
9 | ************************************************************************/
|
---|
10 |
|
---|
11 | #include "stdmix.h"
|
---|
12 | #include "MxStdPane.h"
|
---|
13 | #include "MxGLUtils.h"
|
---|
14 |
|
---|
15 | #define MX_SELECT_NONE 0
|
---|
16 | #define MX_SELECT_FACE 1
|
---|
17 | #define MX_SELECT_VERT 2
|
---|
18 |
|
---|
19 | void MxStdPane::bind_asp_vars(MxAspStore *store)
|
---|
20 | {
|
---|
21 | store->defvar("cam.fovy", MXASP_DOUBLE, &camera.fovy);
|
---|
22 | store->defvar("cam.aspect", MXASP_DOUBLE, &camera.aspect);
|
---|
23 | store->defvar("cam.znear", MXASP_DOUBLE, &camera.znear);
|
---|
24 | store->defvar("cam.zfar", MXASP_DOUBLE, &camera.zfar);
|
---|
25 | store->defvar("cam.from", MXASP_DOUBLE, (double*)camera.from, 3);
|
---|
26 | store->defvar("cam.at", MXASP_DOUBLE, (double *)camera.at, 3);
|
---|
27 | store->defvar("cam.up", MXASP_DOUBLE, (double *)camera.up, 3);
|
---|
28 |
|
---|
29 | store->defvar("light.pos", MXASP_FLOAT, light_position, 3);
|
---|
30 |
|
---|
31 | store->defvar("bounds.min", MXASP_DOUBLE, (double*)bounds.min, 3);
|
---|
32 | store->defvar("bounds.max", MXASP_DOUBLE, (double*)bounds.max, 3);
|
---|
33 | store->defvar("bounds.ctr", MXASP_DOUBLE, (double*)bounds.center, 3);
|
---|
34 | store->defvar("bounds.radius", MXASP_DOUBLE, &bounds.radius, 1);
|
---|
35 |
|
---|
36 | store->defvar("manip.vnow", MXASP_DOUBLE, (double*)manip.vNow, 4);
|
---|
37 | store->defvar("manip.cam", MXASP_FLOAT, (double*)manip.cam, 3);
|
---|
38 |
|
---|
39 | store->defvar("will_draw_surface", MXASP_BOOL, &will_draw_surface);
|
---|
40 | store->defvar("will_draw_mesh", MXASP_BOOL, &will_draw_mesh);
|
---|
41 | store->defvar("will_draw_points", MXASP_BOOL, &will_draw_points);
|
---|
42 | store->defvar("will_draw_texture", MXASP_BOOL, &will_draw_texture);
|
---|
43 | store->defvar("will_illustrate", MXASP_BOOL, &will_illustrate);
|
---|
44 | store->defvar("will_light_scene", MXASP_BOOL, &will_light_scene);
|
---|
45 | store->defvar("ntsc_output", MXASP_BOOL, &ntsc_output);
|
---|
46 |
|
---|
47 | extern float mx_mesh_color[3];
|
---|
48 | store->defvar("mesh_color", MXASP_FLOAT, mx_mesh_color, 3);
|
---|
49 | }
|
---|
50 |
|
---|
51 | void MxStdPane::write_pane_config(ostream& out)
|
---|
52 | {
|
---|
53 | out << "set cam.fovy " << camera.fovy << endl;
|
---|
54 | out << "set cam.aspect " << camera.aspect << endl;
|
---|
55 | out << "set cam.znear " << camera.znear << endl;
|
---|
56 | out << "set cam.zfar " << camera.zfar << endl;
|
---|
57 |
|
---|
58 | out << "set cam.from " << camera.from[X] << " "
|
---|
59 | << camera.from[Y] << " " << camera.from[Z] << endl;
|
---|
60 | out << "set cam.at " << camera.at[X] << " "
|
---|
61 | << camera.at[Y] << " " << camera.at[Z] << endl;
|
---|
62 | out << "set cam.up " << camera.up[X] << " "
|
---|
63 | << camera.up[Y] << " " << camera.up[Z] << endl;
|
---|
64 |
|
---|
65 | glGetLightfv(GL_LIGHT0, GL_POSITION, light_position);
|
---|
66 | out << "set light.pos" << light_position[X] << " "
|
---|
67 | << light_position[Y] << " " << light_position[Z] << endl;
|
---|
68 |
|
---|
69 | out << "set manip.vnow " << manip.vNow[X] << " " << manip.vNow[Y] << " "
|
---|
70 | << manip.vNow[Z] << " " << manip.vNow[W] << endl;
|
---|
71 | out << "set manip.cam " << manip.cam[X] << " " << manip.cam[Y] << " "
|
---|
72 | << manip.cam[Z] << endl;
|
---|
73 | }
|
---|
74 |
|
---|
75 | void MxStdPane::gl_init()
|
---|
76 | {
|
---|
77 | glMatrixMode(GL_PROJECTION);
|
---|
78 | glLoadIdentity();
|
---|
79 | // camera.apply();
|
---|
80 | glMatrixMode(GL_MODELVIEW);
|
---|
81 |
|
---|
82 | MxGLPane::gl_init();
|
---|
83 |
|
---|
84 | glGetLightfv(GL_LIGHT0, GL_POSITION, light_position);
|
---|
85 | CAREFUL(mxgl_check_errors("at end of MxStdPane::gl_init"));
|
---|
86 |
|
---|
87 | mxgl_default_texinit(m->texmap(), will_draw_texture);
|
---|
88 | }
|
---|
89 |
|
---|
90 | uint MxStdPane::pick_something(int x, int y, double window, int mode)
|
---|
91 | {
|
---|
92 | selection_state = mode;
|
---|
93 |
|
---|
94 | MxGLPicker pick(x, y, window);
|
---|
95 | begin_redraw(); redraw(); end_redraw();
|
---|
96 |
|
---|
97 | selection_state = MX_SELECT_NONE;
|
---|
98 | return pick.complete();
|
---|
99 | }
|
---|
100 |
|
---|
101 | uint MxStdPane::pick_face(int x, int y)
|
---|
102 | {
|
---|
103 | return pick_something(x, y, 1.0, MX_SELECT_FACE);
|
---|
104 | }
|
---|
105 |
|
---|
106 | uint MxStdPane::pick_vertex(int x, int y)
|
---|
107 | {
|
---|
108 | return pick_something(x, y, 16.0, MX_SELECT_VERT);
|
---|
109 | }
|
---|
110 |
|
---|
111 | void MxStdPane::move_light(float dx, float dy, float dz)
|
---|
112 | {
|
---|
113 | light_position[0] += dx;
|
---|
114 | light_position[1] += dy;
|
---|
115 | light_position[2] += dz;
|
---|
116 | glLightfv(GL_LIGHT0, GL_POSITION, light_position);
|
---|
117 | }
|
---|
118 |
|
---|
119 | void MxStdPane::size(int width, int height)
|
---|
120 | {
|
---|
121 | MxGLPane::size(width, height);
|
---|
122 |
|
---|
123 | camera.look_at(bounds, (float)width / (float)height);
|
---|
124 | glMatrixMode(GL_PROJECTION);
|
---|
125 | glLoadIdentity();
|
---|
126 | //camera.apply();
|
---|
127 | }
|
---|
128 |
|
---|
129 | void MxStdPane::button(int kind, int which, int x, int y)
|
---|
130 | {
|
---|
131 | switch( kind&0xf )
|
---|
132 | {
|
---|
133 | case MX_BUTTONDOWN:
|
---|
134 | if( manip.mouse_down(which, x, y, kind) )
|
---|
135 | post_redraw();
|
---|
136 | break;
|
---|
137 |
|
---|
138 | case MX_BUTTONUP:
|
---|
139 | if( manip.mouse_up(which, x, y, kind) )
|
---|
140 | post_redraw();
|
---|
141 | break;
|
---|
142 |
|
---|
143 | case MX_POINTERMOTION:
|
---|
144 | if( manip.motion(x, y) )
|
---|
145 | post_redraw();
|
---|
146 | break;
|
---|
147 | }
|
---|
148 | }
|
---|
149 |
|
---|
150 | void MxStdPane::key(char c)
|
---|
151 | {
|
---|
152 | switch( c )
|
---|
153 | {
|
---|
154 | case 'm':
|
---|
155 | will_draw_mesh = !will_draw_mesh;
|
---|
156 | if( will_draw_mesh )
|
---|
157 | glOffsetForMesh();
|
---|
158 | else
|
---|
159 | glNoOffset();
|
---|
160 | break;
|
---|
161 |
|
---|
162 | case 'L':
|
---|
163 | if( will_light_scene )
|
---|
164 | glDisable(GL_LIGHTING);
|
---|
165 | else
|
---|
166 | glEnable(GL_LIGHTING);
|
---|
167 | will_light_scene = !will_light_scene;
|
---|
168 | break;
|
---|
169 |
|
---|
170 | case 'p': will_draw_points = !will_draw_points; break;
|
---|
171 |
|
---|
172 | case 'x': snapshot_ppm(); return;
|
---|
173 | case 'X': snapshot_tiff(); return;
|
---|
174 |
|
---|
175 | case 's':
|
---|
176 | will_draw_surface = !will_draw_surface;
|
---|
177 | break;
|
---|
178 |
|
---|
179 | case 't':
|
---|
180 | will_draw_texture = !will_draw_texture;
|
---|
181 | if( m->texmap() )
|
---|
182 | {
|
---|
183 | if( will_draw_texture ) glEnable(GL_TEXTURE_2D);
|
---|
184 | else glDisable(GL_TEXTURE_2D);
|
---|
185 | }
|
---|
186 | break;
|
---|
187 |
|
---|
188 | case 'i': will_illustrate = !will_illustrate; break;
|
---|
189 |
|
---|
190 | case 'n':
|
---|
191 | if(m->normal_binding()==MX_PERFACE) m->normal_binding(MX_PERVERTEX);
|
---|
192 | else m->normal_binding(MX_PERFACE);
|
---|
193 | m->synthesize_normals();
|
---|
194 | break;
|
---|
195 |
|
---|
196 | case 'V':
|
---|
197 | ntsc_output = !ntsc_output;
|
---|
198 | if( ntsc_output )
|
---|
199 | {
|
---|
200 | glEnable(GL_LINE_SMOOTH);
|
---|
201 | glLineWidth(1.5);
|
---|
202 | }
|
---|
203 | else
|
---|
204 | {
|
---|
205 | glDisable(GL_LINE_SMOOTH);
|
---|
206 | glLineWidth(1.0);
|
---|
207 | }
|
---|
208 | break;
|
---|
209 |
|
---|
210 | case 'D': write_pane_config(cerr); return;
|
---|
211 |
|
---|
212 | case 'q':
|
---|
213 | quit();
|
---|
214 | break;
|
---|
215 |
|
---|
216 | case MXKEY_DEL: case MXKEY_BS: if(typein.length()) typein.drop(); break;
|
---|
217 | case '@': typein.reset(); break;
|
---|
218 |
|
---|
219 | default:
|
---|
220 | if( c>='0' && c<='9' )
|
---|
221 | typein.add(c);
|
---|
222 | else
|
---|
223 | return;
|
---|
224 |
|
---|
225 | break;
|
---|
226 | }
|
---|
227 |
|
---|
228 | post_redraw();
|
---|
229 | }
|
---|
230 |
|
---|
231 | void MxStdPane::begin_redraw()
|
---|
232 | {
|
---|
233 | MxGLPane::begin_redraw();
|
---|
234 |
|
---|
235 | if( will_draw_mesh ) glOffsetForMesh(); else glNoOffset();
|
---|
236 | if( will_light_scene ) glEnable(GL_LIGHTING); else glDisable(GL_LIGHTING);
|
---|
237 |
|
---|
238 | glMatrixMode(GL_PROJECTION);
|
---|
239 | // Conserve stack space by avoiding glPushMatrix
|
---|
240 | glGetDoublev(GL_PROJECTION_MATRIX, __proj_save);
|
---|
241 | camera.apply();
|
---|
242 |
|
---|
243 | glMatrixMode(GL_MODELVIEW);
|
---|
244 | manip.apply();
|
---|
245 |
|
---|
246 | // Use a pure white surface color when we're texturing
|
---|
247 | if( will_draw_texture )
|
---|
248 | glC(1.0, 1.0, 1.0);
|
---|
249 | else
|
---|
250 | // glC(0.5, 0.5, 0.5);
|
---|
251 | glC(0.569, 0.447, 0.315);
|
---|
252 |
|
---|
253 | CAREFUL(mxgl_check_errors("at end of MxStdPane::begin_redraw"));
|
---|
254 | }
|
---|
255 |
|
---|
256 | void MxStdPane::end_redraw()
|
---|
257 | {
|
---|
258 | manip.unapply();
|
---|
259 | glMatrixMode(GL_PROJECTION);
|
---|
260 | glLoadMatrixd(__proj_save); // Explicit save to save stack space
|
---|
261 | glMatrixMode(GL_MODELVIEW);
|
---|
262 | MxGLPane::end_redraw();
|
---|
263 | CAREFUL(mxgl_check_errors("at end of MxStdPane::end_redraw"));
|
---|
264 | }
|
---|
265 |
|
---|
266 | void MxStdPane::redraw()
|
---|
267 | {
|
---|
268 | if( !selection_state )
|
---|
269 | {
|
---|
270 | if( will_draw_surface )
|
---|
271 | {
|
---|
272 | mx_render_model(*m);
|
---|
273 | if( will_draw_mesh )
|
---|
274 | mx_draw_mesh(*m);
|
---|
275 | }
|
---|
276 | else if( will_draw_mesh )
|
---|
277 | mx_draw_wireframe(*m);
|
---|
278 |
|
---|
279 | if( will_draw_points )
|
---|
280 | mx_draw_pointcloud(*m);
|
---|
281 | }
|
---|
282 | else if( selection_state == MX_SELECT_FACE )
|
---|
283 | mx_render_model(*m);
|
---|
284 | else if( selection_state == MX_SELECT_VERT )
|
---|
285 | mx_draw_pointcloud(*m);
|
---|
286 | }
|
---|
287 |
|
---|
288 | void MxStdPane::illustrate()
|
---|
289 | {
|
---|
290 | if( !will_illustrate ) return;
|
---|
291 |
|
---|
292 | char buf[128];
|
---|
293 | int w, h;
|
---|
294 | get_canvas_geometry(NULL, NULL, &w, &h);
|
---|
295 |
|
---|
296 | if( typein.length() )
|
---|
297 | {
|
---|
298 | buf[0] = '>';
|
---|
299 | buf[1] = ' ';
|
---|
300 | strncpy(buf+2, typein, typein.length());
|
---|
301 | buf[typein.length() + 2] = '\0';
|
---|
302 |
|
---|
303 | draw_string(10, h-30, buf);
|
---|
304 | }
|
---|
305 | }
|
---|
306 |
|
---|
307 | #ifdef WIN32
|
---|
308 |
|
---|
309 | void MxStdPane::quit()
|
---|
310 | {
|
---|
311 | DestroyWindow();
|
---|
312 | }
|
---|
313 |
|
---|
314 | #else
|
---|
315 |
|
---|
316 | void MxStdPane::quit()
|
---|
317 | {
|
---|
318 | exit(0);
|
---|
319 | }
|
---|
320 |
|
---|
321 | #endif
|
---|