ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libgender/test.C
Revision: 1.60
Committed: Mon Oct 11 00:05:48 2004 UTC (19 years, 7 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.59: +21 -11 lines
Log Message:
*** empty log message ***

File Contents

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