ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libgender/test.C
Revision: 1.64
Committed: Sun Oct 17 17:19:55 2004 UTC (19 years, 7 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.63: +0 -10 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <math.h>
5
6 #include "opengl.h"
7
8 #include "SDL.h"
9 #include "SDL_opengl.h"
10
11 static SDL_Surface *global_image = NULL;
12 static GLuint global_texture = 0;
13
14 #include "util.h"
15 #include "entity.h"
16 #include "txtprt_import.h"
17
18 /**********************************************************************/
19
20 view camera;
21 vec3 camera_velocity;
22 float camera_angle, camera_angle2, camera_velocity_angle, camera_velocity_angle2;
23 float camera_velocity_factor = 80;
24
25 void
26 HotKey_ToggleFullScreen (void)
27 {
28 SDL_Surface *screen;
29
30 screen = SDL_GetVideoSurface ();
31 if (SDL_WM_ToggleFullScreen (screen))
32 printf ("Toggled fullscreen mode - now %s\n",
33 (screen->flags & SDL_FULLSCREEN) ? "fullscreen" : "windowed");
34 else
35 printf ("Unable to toggle fullscreen mode\n");
36 }
37
38 void
39 HotKey_ToggleGrab (void)
40 {
41 SDL_GrabMode mode;
42
43 printf ("Ctrl-G: toggling input grab!\n");
44 mode = SDL_WM_GrabInput (SDL_GRAB_QUERY);
45 if (mode == SDL_GRAB_ON)
46 printf ("Grab was on\n");
47 else
48 printf ("Grab was off\n");
49
50 mode = SDL_WM_GrabInput (mode ? SDL_GRAB_OFF : SDL_GRAB_ON);
51 if (mode == SDL_GRAB_ON)
52 printf ("Grab is now on\n");
53 else
54 printf ("Grab is now off\n");
55 }
56
57 void
58 HotKey_Iconify (void)
59 {
60 printf ("Ctrl-Z: iconifying window!\n");
61 SDL_WM_IconifyWindow ();
62 }
63
64 int
65 HandleEvent (SDL_Event * event)
66 {
67 int done;
68
69 done = 0;
70 switch (event->type)
71 {
72 case SDL_ACTIVEEVENT:
73 /* See what happened */
74 printf ("app %s ", event->active.gain ? "gained" : "lost");
75 if (event->active.state & SDL_APPACTIVE)
76 printf ("active ");
77 else if (event->active.state & SDL_APPMOUSEFOCUS)
78 printf ("mouse ");
79 else if (event->active.state & SDL_APPINPUTFOCUS)
80 printf ("input ");
81
82 printf ("focus\n");
83 break;
84
85 case SDL_KEYDOWN:
86 if (event->key.keysym.sym == SDLK_UP) camera_velocity.z--;
87 if (event->key.keysym.sym == SDLK_DOWN) camera_velocity.z++;
88 if (event->key.keysym.sym == SDLK_RIGHT) camera_velocity_angle++;
89 if (event->key.keysym.sym == SDLK_LEFT) camera_velocity_angle--;
90 if (event->key.keysym.sym == SDLK_PAGEUP) camera_velocity_angle2++;
91 if (event->key.keysym.sym == SDLK_PAGEDOWN) camera_velocity_angle2--;
92 if (event->key.keysym.sym == SDLK_e) camera_velocity.y--;
93 if (event->key.keysym.sym == SDLK_q) camera_velocity.y++;
94 if (event->key.keysym.sym == SDLK_v) camera_velocity_factor *= 1.5;
95 if (event->key.keysym.sym == SDLK_b) camera_velocity_factor /= 1.5;
96
97 if (event->key.keysym.sym == SDLK_ESCAPE)
98 done = 1;
99
100 if ((event->key.keysym.sym == SDLK_g) &&
101 (event->key.keysym.mod & KMOD_CTRL))
102 HotKey_ToggleGrab ();
103
104 if ((event->key.keysym.sym == SDLK_z) &&
105 (event->key.keysym.mod & KMOD_CTRL))
106 HotKey_Iconify ();
107
108 if ((event->key.keysym.sym == SDLK_RETURN) &&
109 (event->key.keysym.mod & KMOD_ALT))
110 HotKey_ToggleFullScreen ();
111
112 break;
113
114 case SDL_KEYUP:
115 if (event->key.keysym.sym == SDLK_UP) camera_velocity.z++;
116 if (event->key.keysym.sym == SDLK_DOWN) camera_velocity.z--;
117 if (event->key.keysym.sym == SDLK_LEFT) camera_velocity_angle++;
118 if (event->key.keysym.sym == SDLK_RIGHT) camera_velocity_angle--;
119 if (event->key.keysym.sym == SDLK_PAGEUP) camera_velocity_angle2--;
120 if (event->key.keysym.sym == SDLK_PAGEDOWN) camera_velocity_angle2++;
121 if (event->key.keysym.sym == SDLK_e) camera_velocity.y++;
122 if (event->key.keysym.sym == SDLK_q) camera_velocity.y--;
123 break;
124
125 case SDL_QUIT:
126 done = 1;
127 break;
128 }
129
130 return (done);
131 }
132
133 void draw_floor (int size, int dx, int dy, int dz)
134 {
135 int x, z, ry;
136
137 for (x = 0; x < 10; x++)
138 {
139 for (z = 0; z < 10; z++)
140 {
141 vector<vertex_t2f_n3f_v3f> pts;
142 pts.push_back (vertex_t2f_n3f_v3f (point ( 0, 0, 0), vec3 (0, 1, 0), tex2 (0, 0)));
143 pts.push_back (vertex_t2f_n3f_v3f (point ( 0, 0, size), vec3 (0, 1, 0), tex2 (0, 1)));
144 pts.push_back (vertex_t2f_n3f_v3f (point (size, 0, size), vec3 (0, 1, 0), tex2 (1, 1)));
145 pts.push_back (vertex_t2f_n3f_v3f (point (size, 0, 0), vec3 (0, 1, 0), tex2 (1, 0)));
146
147 geometry_quads *q = new geometry_quads;
148 q->m = new simple_material;
149 q->set (pts);
150 entity *e = new entity (q);
151 e->move (vec3 (dx + x * size, dy, dz + z * size));
152 e->show ();
153 }
154 }
155 }
156
157 void draw_test_nurb ()
158 {
159 geometry_nurbs *q = new geometry_nurbs;
160 q->set ();
161 entity *e = new entity (q);
162 e->move (vec3 (10, 3, -4));
163 e->show ();
164 }
165
166 extern void draw_level ();
167 int
168 RunGLTest (int argc, char *argv[],
169 int logo, int slowly, int bpp, float gamma, int noframe, int fsaa)
170 {
171 int i;
172 int rgb_size[3];
173 int w = 640;
174 int h = 480;
175 int done = 0;
176 int frames;
177 Uint32 video_flags;
178 int value;
179 GLenum gl_error;
180
181 if (SDL_Init (SDL_INIT_VIDEO) < 0)
182 {
183 fprintf (stderr, "Couldn't initialize SDL: %s\n", SDL_GetError ());
184 exit (1);
185 }
186
187 /* See if we should detect the display depth */
188 if (bpp == 0)
189 {
190 if (SDL_GetVideoInfo ()->vfmt->BitsPerPixel <= 8)
191 bpp = 8;
192 else
193 bpp = 16; /* More doesn't seem to work */
194 }
195
196 video_flags = SDL_OPENGL;
197
198 for (i = 1; argv[i]; ++i)
199 if (strcmp (argv[1], "-fullscreen") == 0)
200 video_flags |= SDL_FULLSCREEN;
201
202 if (noframe)
203 video_flags |= SDL_NOFRAME;
204
205 /* Initialize the display */
206 switch (bpp)
207 {
208 case 8:
209 rgb_size[0] = 3;
210 rgb_size[1] = 3;
211 rgb_size[2] = 2;
212 break;
213
214 case 15:
215 case 16:
216 rgb_size[0] = 5;
217 rgb_size[1] = 5;
218 rgb_size[2] = 5;
219 break;
220
221 default:
222 rgb_size[0] = 8;
223 rgb_size[1] = 8;
224 rgb_size[2] = 8;
225 break;
226 }
227
228 SDL_GL_SetAttribute (SDL_GL_RED_SIZE, rgb_size[0]);
229 SDL_GL_SetAttribute (SDL_GL_GREEN_SIZE, rgb_size[1]);
230 SDL_GL_SetAttribute (SDL_GL_BLUE_SIZE, rgb_size[2]);
231 SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE, 16);
232 SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, 1);
233
234 if (fsaa)
235 {
236 SDL_GL_SetAttribute (SDL_GL_MULTISAMPLEBUFFERS, 1);
237 SDL_GL_SetAttribute (SDL_GL_MULTISAMPLESAMPLES, fsaa);
238 }
239
240 if (SDL_SetVideoMode (w, h, bpp, video_flags) == NULL)
241 {
242 fprintf (stderr, "Couldn't set GL mode: %s\n", SDL_GetError ());
243 SDL_Quit ();
244 exit (1);
245 }
246
247 printf ("Screen BPP: %d\n", SDL_GetVideoSurface ()->format->BitsPerPixel);
248 printf ("\n");
249 printf ("Vendor : %s\n", glGetString (GL_VENDOR));
250 printf ("Renderer : %s\n", glGetString (GL_RENDERER));
251 printf ("Version : %s\n", glGetString (GL_VERSION));
252 printf ("Extensions : %s\n", glGetString (GL_EXTENSIONS));
253 printf ("\n");
254
255 SDL_GL_GetAttribute (SDL_GL_RED_SIZE, &value);
256 printf ("SDL_GL_RED_SIZE: requested %d, got %d\n", rgb_size[0], value);
257 SDL_GL_GetAttribute (SDL_GL_GREEN_SIZE, &value);
258 printf ("SDL_GL_GREEN_SIZE: requested %d, got %d\n", rgb_size[1], value);
259 SDL_GL_GetAttribute (SDL_GL_BLUE_SIZE, &value);
260 printf ("SDL_GL_BLUE_SIZE: requested %d, got %d\n", rgb_size[2], value);
261 SDL_GL_GetAttribute (SDL_GL_DEPTH_SIZE, &value);
262 printf ("SDL_GL_DEPTH_SIZE: requested %d, got %d\n", bpp, value);
263 SDL_GL_GetAttribute (SDL_GL_DOUBLEBUFFER, &value);
264 printf ("SDL_GL_DOUBLEBUFFER: requested 1, got %d\n", value);
265
266 if (fsaa)
267 {
268 SDL_GL_GetAttribute (SDL_GL_MULTISAMPLEBUFFERS, &value);
269 printf ("SDL_GL_MULTISAMPLEBUFFERS: requested 1, got %d\n", value);
270 SDL_GL_GetAttribute (SDL_GL_MULTISAMPLESAMPLES, &value);
271 printf ("SDL_GL_MULTISAMPLESAMPLES: requested %d, got %d\n", fsaa,
272 value);
273 }
274
275 /* Set the window manager title bar */
276 SDL_WM_SetCaption ("libgender rendering test", "gendertest");
277
278 /* Set the gamma for the window */
279 if (gamma != 0.0)
280 SDL_SetGamma (gamma, gamma, gamma);
281
282 entity *planet = new entity (new geometry_sphere (10));
283 planet->move (vec3 (0, 0, -20));
284 planet->show ();
285
286 #if 1
287 for (int i = 0; i < 20; i++)
288 {
289 // load a entity
290 txtprt_parser p;
291 geometry *g;
292 try
293 {
294 g = p.read ("test.blasc");
295 }
296 catch (txtprt_i_exception & e)
297 {
298 cout << "ERR: " << e.msg << endl;
299 }
300
301 entity *e = new entity (g);
302 e->move (vec3 (i*5, -3, -i*10));
303 e->show ();
304 }
305
306 {
307 entity *planet = new entity (new geometry_sphere (1e9));
308 planet->move (vec3 (0, 0, -1.5e9));
309 planet->show ();
310 }
311
312 {
313 entity *planet = new entity (new geometry_sphere (4e15));
314 planet->move (vec3 (0, 0, 1e17));
315 planet->show ();
316 }
317
318 draw_floor (10, -500, -10, -1000);
319 draw_level ();
320 //draw_test_nurb ();
321 #endif
322
323 //camera.orig.x = 108; camera.orig.y = 0; camera.orig.z = -368;
324 camera.orig.x = 0; camera.orig.y = 0; camera.orig.z = 0;
325 camera.p = point (0, 0, 10);
326 camera.d = vec3 (0, 0, -1);
327 camera.u = vec3 (0, 1, 0);
328 camera.w = w; camera.h = h;
329 camera.fov = 35;
330 camera.z_near = 1.;
331
332 glEnable (GL_CULL_FACE);
333
334 init_shaders ();
335
336 osama_material osa_mat;
337
338 /* Loop until done. */
339 frames = 0;
340
341 while (!done)
342 {
343 char *sdl_error;
344 SDL_Event event;
345
346 camera_angle += 180 * camera_velocity_angle * timer.diff;
347 camera_angle2 += 180 * camera_velocity_angle2 * timer.diff;
348
349 vec3 geradeaus = matrix::rotation (-camera_angle, vec3 (0, 1, 0)) * vec3 (0, 0, -1);
350 vec3 right = matrix::rotation (90., vec3 (0, 1, 0)) * geradeaus;
351
352 camera.d = matrix::rotation (camera_angle2, right) * geradeaus;
353 camera.u = cross (camera.d, right);
354
355 camera.p = camera.p - camera.d * (camera_velocity_factor * timer.diff) * camera_velocity.z;
356 camera.p = camera.p - camera.u * (camera_velocity_factor * timer.diff) * camera_velocity.y;
357
358 osa_mat.begin ();
359
360 camera.begin ();
361 camera.render (view::DEPTH);
362 camera.render (view::POSTDEPTH);
363 camera.render (view::LIGHTED);
364 camera.end ();
365
366 SDL_GL_SwapBuffers ();
367 timer.frame ();
368
369 osa_mat.end ();
370
371 #if 0
372 /* Check for error conditions. */
373 gl_error = glGetError ();
374
375 if (gl_error != GL_NO_ERROR) fprintf (stderr, "testgl: OpenGL error: %d\n", gl_error);
376
377 sdl_error = SDL_GetError ();
378
379 if (sdl_error[0] != '\0')
380 {
381 fprintf (stderr, "testgl: SDL error '%s'\n", sdl_error);
382 SDL_ClearError ();
383 }
384
385 /* Allow the user to see what's happening */
386 //SDL_Delay (40);
387 #endif
388
389 /* Check if there's a pending event. */
390 while (SDL_PollEvent (&event))
391 done = HandleEvent (&event);
392
393
394 ++frames;
395 }
396
397 if (global_image)
398 {
399 SDL_FreeSurface (global_image);
400 global_image = NULL;
401 }
402 if (global_texture)
403 {
404 glDeleteTextures (1, &global_texture);
405 global_texture = 0;
406 }
407
408 /* Destroy our GL context, etc. */
409 SDL_Quit ();
410 return (0);
411 }
412 int
413 main (int argc, char *argv[])
414 {
415 int i, logo;
416 int numtests;
417 int bpp = 0;
418 int slowly;
419 float gamma = 0.0;
420 int noframe = 0;
421 int fsaa = 0;
422
423 logo = 0;
424 slowly = 0;
425 numtests = 1;
426 for (i = 1; argv[i]; ++i)
427 {
428 if (strcmp (argv[i], "-twice") == 0)
429 {
430 ++numtests;
431 }
432 if (strcmp (argv[i], "-slow") == 0)
433 {
434 slowly = 1;
435 }
436 if (strcmp (argv[i], "-bpp") == 0)
437 {
438 bpp = atoi (argv[++i]);
439 }
440 if (strcmp (argv[i], "-gamma") == 0)
441 {
442 gamma = (float) atof (argv[++i]);
443 }
444 if (strcmp (argv[i], "-noframe") == 0)
445 {
446 noframe = 1;
447 }
448 if (strcmp (argv[i], "-fsaa") == 0)
449 {
450 ++fsaa;
451 }
452 if (strncmp (argv[i], "-h", 2) == 0)
453 {
454 printf
455 ("Usage: %s [-twice] [-logo] [-slow] [-bpp n] [-gamma n] [-noframe] [-fsaa]\n",
456 argv[0]);
457 exit (0);
458 }
459 }
460
461 for (i = 0; i < numtests; ++i)
462 RunGLTest (argc, argv, logo, slowly, bpp, gamma, noframe, fsaa);
463
464 return 0;
465 }