ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libgender/test.C
Revision: 1.63
Committed: Sun Oct 17 09:43:07 2004 UTC (19 years, 7 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.62: +3 -3 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 root 1.62 #include "opengl.h"
7    
8 root 1.1 #include "SDL.h"
9     #include "SDL_opengl.h"
10    
11     static SDL_Surface *global_image = NULL;
12     static GLuint global_texture = 0;
13    
14 root 1.29 #include "util.h"
15 root 1.2 #include "entity.h"
16 root 1.29 #include "txtprt_import.h"
17    
18 root 1.1 /**********************************************************************/
19    
20 root 1.6 view camera;
21 root 1.18 vec3 camera_velocity;
22 root 1.51 float camera_angle, camera_angle2, camera_velocity_angle, camera_velocity_angle2;
23 root 1.50 float camera_velocity_factor = 80;
24 root 1.6
25 root 1.1 void
26     HotKey_ToggleFullScreen (void)
27     {
28     SDL_Surface *screen;
29    
30     screen = SDL_GetVideoSurface ();
31     if (SDL_WM_ToggleFullScreen (screen))
32 root 1.5 printf ("Toggled fullscreen mode - now %s\n",
33     (screen->flags & SDL_FULLSCREEN) ? "fullscreen" : "windowed");
34 root 1.1 else
35 root 1.5 printf ("Unable to toggle fullscreen mode\n");
36 root 1.1 }
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 root 1.5 printf ("Grab was on\n");
47 root 1.1 else
48 root 1.5 printf ("Grab was off\n");
49 root 1.1
50     mode = SDL_WM_GrabInput (mode ? SDL_GRAB_OFF : SDL_GRAB_ON);
51     if (mode == SDL_GRAB_ON)
52 root 1.5 printf ("Grab is now on\n");
53 root 1.1 else
54 root 1.5 printf ("Grab is now off\n");
55 root 1.1 }
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 root 1.62 printf ("active ");
77 root 1.1 else if (event->active.state & SDL_APPMOUSEFOCUS)
78 root 1.62 printf ("mouse ");
79 root 1.1 else if (event->active.state & SDL_APPINPUTFOCUS)
80 root 1.62 printf ("input ");
81 root 1.1
82     printf ("focus\n");
83     break;
84    
85     case SDL_KEYDOWN:
86 root 1.51 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 root 1.6
97 root 1.1 if (event->key.keysym.sym == SDLK_ESCAPE)
98 root 1.6 done = 1;
99 root 1.1
100     if ((event->key.keysym.sym == SDLK_g) &&
101     (event->key.keysym.mod & KMOD_CTRL))
102 root 1.6 HotKey_ToggleGrab ();
103 root 1.1
104     if ((event->key.keysym.sym == SDLK_z) &&
105     (event->key.keysym.mod & KMOD_CTRL))
106 root 1.6 HotKey_Iconify ();
107 root 1.1
108     if ((event->key.keysym.sym == SDLK_RETURN) &&
109     (event->key.keysym.mod & KMOD_ALT))
110 root 1.6 HotKey_ToggleFullScreen ();
111 root 1.1
112     break;
113    
114 root 1.18 case SDL_KEYUP:
115 root 1.19 if (event->key.keysym.sym == SDLK_UP) camera_velocity.z++;
116     if (event->key.keysym.sym == SDLK_DOWN) camera_velocity.z--;
117 root 1.50 if (event->key.keysym.sym == SDLK_LEFT) camera_velocity_angle++;
118     if (event->key.keysym.sym == SDLK_RIGHT) camera_velocity_angle--;
119 root 1.51 if (event->key.keysym.sym == SDLK_PAGEUP) camera_velocity_angle2--;
120     if (event->key.keysym.sym == SDLK_PAGEDOWN) camera_velocity_angle2++;
121 root 1.50 if (event->key.keysym.sym == SDLK_e) camera_velocity.y++;
122     if (event->key.keysym.sym == SDLK_q) camera_velocity.y--;
123 root 1.18 break;
124    
125 root 1.1 case SDL_QUIT:
126     done = 1;
127     break;
128     }
129    
130     return (done);
131     }
132    
133 root 1.47 void draw_floor (int size, int dx, int dy, int dz)
134     {
135     int x, z, ry;
136    
137 root 1.60 for (x = 0; x < 10; x++)
138 root 1.47 {
139 root 1.60 for (z = 0; z < 10; z++)
140 root 1.47 {
141 root 1.62 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 root 1.47
147     geometry_quads *q = new geometry_quads;
148 root 1.57 q->m = new simple_material;
149 root 1.47 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 root 1.52 void draw_test_nurb ()
158     {
159 root 1.49 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 root 1.54 extern void draw_level ();
167 root 1.1 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 start_time, this_time;
178 root 1.2 Uint32 video_flags;
179 root 1.1 int value;
180 root 1.45 GLenum gl_error;
181 root 1.1
182     if (SDL_Init (SDL_INIT_VIDEO) < 0)
183     {
184     fprintf (stderr, "Couldn't initialize SDL: %s\n", SDL_GetError ());
185     exit (1);
186     }
187    
188     /* See if we should detect the display depth */
189     if (bpp == 0)
190     {
191     if (SDL_GetVideoInfo ()->vfmt->BitsPerPixel <= 8)
192 root 1.3 bpp = 8;
193 root 1.1 else
194 root 1.3 bpp = 16; /* More doesn't seem to work */
195 root 1.1 }
196    
197 root 1.2 video_flags = SDL_OPENGL;
198    
199 root 1.1 for (i = 1; argv[i]; ++i)
200 root 1.3 if (strcmp (argv[1], "-fullscreen") == 0)
201     video_flags |= SDL_FULLSCREEN;
202 root 1.1
203     if (noframe)
204 root 1.3 video_flags |= SDL_NOFRAME;
205 root 1.1
206     /* Initialize the display */
207     switch (bpp)
208     {
209     case 8:
210     rgb_size[0] = 3;
211     rgb_size[1] = 3;
212     rgb_size[2] = 2;
213     break;
214 root 1.3
215 root 1.1 case 15:
216     case 16:
217     rgb_size[0] = 5;
218     rgb_size[1] = 5;
219     rgb_size[2] = 5;
220     break;
221 root 1.3
222 root 1.1 default:
223     rgb_size[0] = 8;
224     rgb_size[1] = 8;
225     rgb_size[2] = 8;
226     break;
227     }
228 root 1.18
229 root 1.1 SDL_GL_SetAttribute (SDL_GL_RED_SIZE, rgb_size[0]);
230     SDL_GL_SetAttribute (SDL_GL_GREEN_SIZE, rgb_size[1]);
231     SDL_GL_SetAttribute (SDL_GL_BLUE_SIZE, rgb_size[2]);
232     SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE, 16);
233     SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, 1);
234 root 1.3
235 root 1.1 if (fsaa)
236     {
237     SDL_GL_SetAttribute (SDL_GL_MULTISAMPLEBUFFERS, 1);
238     SDL_GL_SetAttribute (SDL_GL_MULTISAMPLESAMPLES, fsaa);
239     }
240 root 1.3
241 root 1.1 if (SDL_SetVideoMode (w, h, bpp, video_flags) == NULL)
242     {
243     fprintf (stderr, "Couldn't set GL mode: %s\n", SDL_GetError ());
244     SDL_Quit ();
245     exit (1);
246     }
247    
248     printf ("Screen BPP: %d\n", SDL_GetVideoSurface ()->format->BitsPerPixel);
249     printf ("\n");
250     printf ("Vendor : %s\n", glGetString (GL_VENDOR));
251     printf ("Renderer : %s\n", glGetString (GL_RENDERER));
252     printf ("Version : %s\n", glGetString (GL_VERSION));
253     printf ("Extensions : %s\n", glGetString (GL_EXTENSIONS));
254     printf ("\n");
255    
256     SDL_GL_GetAttribute (SDL_GL_RED_SIZE, &value);
257     printf ("SDL_GL_RED_SIZE: requested %d, got %d\n", rgb_size[0], value);
258     SDL_GL_GetAttribute (SDL_GL_GREEN_SIZE, &value);
259     printf ("SDL_GL_GREEN_SIZE: requested %d, got %d\n", rgb_size[1], value);
260     SDL_GL_GetAttribute (SDL_GL_BLUE_SIZE, &value);
261     printf ("SDL_GL_BLUE_SIZE: requested %d, got %d\n", rgb_size[2], value);
262     SDL_GL_GetAttribute (SDL_GL_DEPTH_SIZE, &value);
263     printf ("SDL_GL_DEPTH_SIZE: requested %d, got %d\n", bpp, value);
264     SDL_GL_GetAttribute (SDL_GL_DOUBLEBUFFER, &value);
265     printf ("SDL_GL_DOUBLEBUFFER: requested 1, got %d\n", value);
266 root 1.3
267 root 1.1 if (fsaa)
268     {
269     SDL_GL_GetAttribute (SDL_GL_MULTISAMPLEBUFFERS, &value);
270     printf ("SDL_GL_MULTISAMPLEBUFFERS: requested 1, got %d\n", value);
271     SDL_GL_GetAttribute (SDL_GL_MULTISAMPLESAMPLES, &value);
272     printf ("SDL_GL_MULTISAMPLESAMPLES: requested %d, got %d\n", fsaa,
273     value);
274     }
275    
276     /* Set the window manager title bar */
277 root 1.12 SDL_WM_SetCaption ("libgender rendering test", "gendertest");
278 root 1.1
279     /* Set the gamma for the window */
280     if (gamma != 0.0)
281 root 1.3 SDL_SetGamma (gamma, gamma, gamma);
282 root 1.1
283 root 1.60 entity *planet = new entity (new geometry_sphere (10));
284     planet->move (vec3 (0, 0, -20));
285     planet->show ();
286    
287     #if 1
288 root 1.56 for (int i = 0; i < 20; i++)
289 root 1.17 {
290 root 1.40 // load a entity
291 root 1.17 txtprt_parser p;
292 root 1.47 geometry *g;
293 root 1.17 try
294 root 1.40 {
295 root 1.47 g = p.read ("test.blasc");
296 root 1.40 }
297     catch (txtprt_i_exception & e)
298     {
299     cout << "ERR: " << e.msg << endl;
300     }
301    
302 root 1.47 entity *e = new entity (g);
303     e->move (vec3 (i*5, -3, -i*10));
304     e->show ();
305 root 1.17 }
306 root 1.47
307 root 1.60 {
308     entity *planet = new entity (new geometry_sphere (1e9));
309     planet->move (vec3 (0, 0, -1.5e9));
310     planet->show ();
311     }
312    
313     {
314     entity *planet = new entity (new geometry_sphere (4e15));
315     planet->move (vec3 (0, 0, 1e17));
316     planet->show ();
317     }
318    
319 root 1.56 draw_floor (10, -500, -10, -1000);
320 root 1.54 draw_level ();
321 root 1.60 //draw_test_nurb ();
322     #endif
323 root 1.47
324 root 1.55 //camera.orig.x = 108; camera.orig.y = 0; camera.orig.z = -368;
325     camera.orig.x = 0; camera.orig.y = 0; camera.orig.z = 0;
326 root 1.6 camera.p = point (0, 0, 10);
327     camera.d = vec3 (0, 0, -1);
328     camera.u = vec3 (0, 1, 0);
329     camera.w = w; camera.h = h;
330 root 1.46 camera.fov = 35;
331 root 1.53 camera.z_near = 1.;
332 root 1.1
333 root 1.41 glEnable (GL_CULL_FACE);
334 root 1.29
335 root 1.58 init_shaders ();
336 root 1.1
337 root 1.59 osama_material osa_mat;
338    
339 root 1.1 /* Loop until done. */
340     start_time = SDL_GetTicks ();
341     frames = 0;
342 root 1.16
343 root 1.1 while (!done)
344     {
345     char *sdl_error;
346     SDL_Event event;
347    
348 root 1.51 camera_angle += 180 * camera_velocity_angle * timer.diff;
349     camera_angle2 += 180 * camera_velocity_angle2 * timer.diff;
350    
351     vec3 geradeaus = matrix::rotation (-camera_angle, vec3 (0, 1, 0)) * vec3 (0, 0, -1);
352     vec3 right = matrix::rotation (90., vec3 (0, 1, 0)) * geradeaus;
353    
354     camera.d = matrix::rotation (camera_angle2, right) * geradeaus;
355     camera.u = cross (camera.d, right);
356    
357 root 1.50 camera.p = camera.p - camera.d * (camera_velocity_factor * timer.diff) * camera_velocity.z;
358     camera.p = camera.p - camera.u * (camera_velocity_factor * timer.diff) * camera_velocity.y;
359 root 1.18
360 root 1.59 osa_mat.begin ();
361 root 1.1
362 root 1.34 camera.begin ();
363 root 1.63 camera.render (view::DEPTH);
364     camera.render (view::POSTDEPTH);
365     camera.render (view::LIGHTED);
366 root 1.34 camera.end ();
367 root 1.1
368     SDL_GL_SwapBuffers ();
369 root 1.15 timer.frame ();
370 root 1.1
371 root 1.59 osa_mat.end ();
372 root 1.62
373     #if 0
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.62 #endif
390 root 1.1
391     /* Check if there's a pending event. */
392     while (SDL_PollEvent (&event))
393 root 1.18 done = HandleEvent (&event);
394 root 1.20
395 root 1.18
396 root 1.1 ++frames;
397     }
398    
399     /* Print out the frames per second */
400     this_time = SDL_GetTicks ();
401     if (this_time != start_time)
402     {
403     printf ("%2.2f FPS\n",
404     ((float) frames / (this_time - start_time)) * 1000.0);
405     }
406    
407     if (global_image)
408     {
409     SDL_FreeSurface (global_image);
410     global_image = NULL;
411     }
412     if (global_texture)
413     {
414     glDeleteTextures (1, &global_texture);
415     global_texture = 0;
416     }
417    
418     /* Destroy our GL context, etc. */
419     SDL_Quit ();
420     return (0);
421     }
422     int
423     main (int argc, char *argv[])
424     {
425     int i, logo;
426     int numtests;
427     int bpp = 0;
428     int slowly;
429     float gamma = 0.0;
430     int noframe = 0;
431     int fsaa = 0;
432 root 1.4
433 root 1.1 logo = 0;
434     slowly = 0;
435     numtests = 1;
436     for (i = 1; argv[i]; ++i)
437     {
438     if (strcmp (argv[i], "-twice") == 0)
439     {
440     ++numtests;
441     }
442     if (strcmp (argv[i], "-slow") == 0)
443     {
444     slowly = 1;
445     }
446     if (strcmp (argv[i], "-bpp") == 0)
447     {
448     bpp = atoi (argv[++i]);
449     }
450     if (strcmp (argv[i], "-gamma") == 0)
451     {
452     gamma = (float) atof (argv[++i]);
453     }
454     if (strcmp (argv[i], "-noframe") == 0)
455     {
456     noframe = 1;
457     }
458     if (strcmp (argv[i], "-fsaa") == 0)
459     {
460     ++fsaa;
461     }
462     if (strncmp (argv[i], "-h", 2) == 0)
463     {
464     printf
465     ("Usage: %s [-twice] [-logo] [-slow] [-bpp n] [-gamma n] [-noframe] [-fsaa]\n",
466     argv[0]);
467     exit (0);
468     }
469     }
470 root 1.17
471 root 1.1 for (i = 0; i < numtests; ++i)
472 root 1.17 RunGLTest (argc, argv, logo, slowly, bpp, gamma, noframe, fsaa);
473    
474 root 1.1 return 0;
475     }