ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libgender/test.C
Revision: 1.59
Committed: Sun Oct 10 20:22:33 2004 UTC (19 years, 7 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.58: +4 -0 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     for (x = 0; x < 100; x++)
142     {
143     for (z = 0; z < 100; z++)
144     {
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.56 for (int i = 0; i < 20; i++)
288 root 1.17 {
289 root 1.40 // load a entity
290 root 1.17 txtprt_parser p;
291 root 1.47 geometry *g;
292 root 1.17 try
293 root 1.40 {
294 root 1.47 g = p.read ("test.blasc");
295 root 1.40 }
296     catch (txtprt_i_exception & e)
297     {
298     cout << "ERR: " << e.msg << endl;
299     }
300    
301 root 1.47 entity *e = new entity (g);
302     e->move (vec3 (i*5, -3, -i*10));
303     e->show ();
304 root 1.17 }
305 root 1.47
306 root 1.56 draw_floor (10, -500, -10, -1000);
307 root 1.54 draw_level ();
308 root 1.56 draw_test_nurb ();
309 root 1.47
310 root 1.55 //camera.orig.x = 108; camera.orig.y = 0; camera.orig.z = -368;
311     camera.orig.x = 0; camera.orig.y = 0; camera.orig.z = 0;
312 root 1.6 camera.p = point (0, 0, 10);
313     camera.d = vec3 (0, 0, -1);
314     camera.u = vec3 (0, 1, 0);
315     camera.w = w; camera.h = h;
316 root 1.46 camera.fov = 35;
317 root 1.53 camera.z_near = 1.;
318 root 1.1
319     glMatrixMode (GL_MODELVIEW);
320     glLoadIdentity ();
321    
322 root 1.41 glEnable (GL_CULL_FACE);
323 root 1.1 glEnable (GL_DEPTH_TEST);
324 root 1.22
325 root 1.11 glShadeModel (GL_SMOOTH);
326 root 1.6
327 root 1.3 glEnable (GL_LIGHTING);
328 root 1.6 glEnable (GL_LIGHT0);
329 root 1.29
330 root 1.58 init_shaders ();
331 root 1.1
332 root 1.59 osama_material osa_mat;
333    
334 root 1.1 /* Loop until done. */
335     start_time = SDL_GetTicks ();
336     frames = 0;
337 root 1.16
338 root 1.1 while (!done)
339     {
340     char *sdl_error;
341     SDL_Event event;
342    
343 root 1.51 camera_angle += 180 * camera_velocity_angle * timer.diff;
344     camera_angle2 += 180 * camera_velocity_angle2 * timer.diff;
345    
346     vec3 geradeaus = matrix::rotation (-camera_angle, vec3 (0, 1, 0)) * vec3 (0, 0, -1);
347     vec3 right = matrix::rotation (90., vec3 (0, 1, 0)) * geradeaus;
348    
349     camera.d = matrix::rotation (camera_angle2, right) * geradeaus;
350     camera.u = cross (camera.d, right);
351    
352 root 1.50 camera.p = camera.p - camera.d * (camera_velocity_factor * timer.diff) * camera_velocity.z;
353     camera.p = camera.p - camera.u * (camera_velocity_factor * timer.diff) * camera_velocity.y;
354 root 1.18
355 root 1.6
356 root 1.5 #if 0
357 root 1.3 static GLfloat ry;
358 root 1.13 ry += 0.001;
359     camera.d.x = cos (ry);
360     camera.d.z = sin (ry);
361     //camera.d.y = sin (ry * 0.1);
362 root 1.5 #endif
363 root 1.59 osa_mat.begin ();
364 root 1.1
365 root 1.34 camera.begin ();
366     camera.pass (view::DEPTH);
367     camera.pass (view::LIGHTED);
368     camera.end ();
369 root 1.1
370     SDL_GL_SwapBuffers ();
371 root 1.15 timer.frame ();
372 root 1.1
373 root 1.59 osa_mat.end ();
374 root 1.1 /* Check for error conditions. */
375     gl_error = glGetError ();
376    
377 root 1.45 if (gl_error != GL_NO_ERROR) fprintf (stderr, "testgl: OpenGL error: %d\n", gl_error);
378 root 1.1
379     sdl_error = SDL_GetError ();
380    
381     if (sdl_error[0] != '\0')
382     {
383     fprintf (stderr, "testgl: SDL error '%s'\n", sdl_error);
384     SDL_ClearError ();
385     }
386    
387     /* Allow the user to see what's happening */
388 root 1.51 //SDL_Delay (40);
389 root 1.1
390     /* Check if there's a pending event. */
391     while (SDL_PollEvent (&event))
392 root 1.18 done = HandleEvent (&event);
393 root 1.20
394 root 1.18
395 root 1.1 ++frames;
396     }
397    
398     /* Print out the frames per second */
399     this_time = SDL_GetTicks ();
400     if (this_time != start_time)
401     {
402     printf ("%2.2f FPS\n",
403     ((float) frames / (this_time - start_time)) * 1000.0);
404     }
405    
406     if (global_image)
407     {
408     SDL_FreeSurface (global_image);
409     global_image = NULL;
410     }
411     if (global_texture)
412     {
413     glDeleteTextures (1, &global_texture);
414     global_texture = 0;
415     }
416    
417     /* Destroy our GL context, etc. */
418     SDL_Quit ();
419     return (0);
420     }
421     int
422     main (int argc, char *argv[])
423     {
424     int i, logo;
425     int numtests;
426     int bpp = 0;
427     int slowly;
428     float gamma = 0.0;
429     int noframe = 0;
430     int fsaa = 0;
431 root 1.4
432 root 1.1 logo = 0;
433     slowly = 0;
434     numtests = 1;
435     for (i = 1; argv[i]; ++i)
436     {
437     if (strcmp (argv[i], "-twice") == 0)
438     {
439     ++numtests;
440     }
441     if (strcmp (argv[i], "-slow") == 0)
442     {
443     slowly = 1;
444     }
445     if (strcmp (argv[i], "-bpp") == 0)
446     {
447     bpp = atoi (argv[++i]);
448     }
449     if (strcmp (argv[i], "-gamma") == 0)
450     {
451     gamma = (float) atof (argv[++i]);
452     }
453     if (strcmp (argv[i], "-noframe") == 0)
454     {
455     noframe = 1;
456     }
457     if (strcmp (argv[i], "-fsaa") == 0)
458     {
459     ++fsaa;
460     }
461     if (strncmp (argv[i], "-h", 2) == 0)
462     {
463     printf
464     ("Usage: %s [-twice] [-logo] [-slow] [-bpp n] [-gamma n] [-noframe] [-fsaa]\n",
465     argv[0]);
466     exit (0);
467     }
468     }
469 root 1.17
470 root 1.1 for (i = 0; i < numtests; ++i)
471 root 1.17 RunGLTest (argc, argv, logo, slowly, bpp, gamma, noframe, fsaa);
472    
473 root 1.1 return 0;
474     }