ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libgender/test.C
Revision: 1.28
Committed: Tue Oct 5 06:03:32 2004 UTC (19 years, 8 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.27: +2 -2 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.2 #include "entity.h"
13    
14 root 1.4 #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.24 float camera_velocity_factor = 10;
21 root 1.6
22 root 1.1 void
23     HotKey_ToggleFullScreen (void)
24     {
25     SDL_Surface *screen;
26    
27     screen = SDL_GetVideoSurface ();
28     if (SDL_WM_ToggleFullScreen (screen))
29 root 1.5 printf ("Toggled fullscreen mode - now %s\n",
30     (screen->flags & SDL_FULLSCREEN) ? "fullscreen" : "windowed");
31 root 1.1 else
32 root 1.5 printf ("Unable to toggle fullscreen mode\n");
33 root 1.1 }
34    
35     void
36     HotKey_ToggleGrab (void)
37     {
38     SDL_GrabMode mode;
39    
40     printf ("Ctrl-G: toggling input grab!\n");
41     mode = SDL_WM_GrabInput (SDL_GRAB_QUERY);
42     if (mode == SDL_GRAB_ON)
43 root 1.5 printf ("Grab was on\n");
44 root 1.1 else
45 root 1.5 printf ("Grab was off\n");
46 root 1.1
47     mode = SDL_WM_GrabInput (mode ? SDL_GRAB_OFF : SDL_GRAB_ON);
48     if (mode == SDL_GRAB_ON)
49 root 1.5 printf ("Grab is now on\n");
50 root 1.1 else
51 root 1.5 printf ("Grab is now off\n");
52 root 1.1 }
53    
54     void
55     HotKey_Iconify (void)
56     {
57     printf ("Ctrl-Z: iconifying window!\n");
58     SDL_WM_IconifyWindow ();
59     }
60    
61     int
62     HandleEvent (SDL_Event * event)
63     {
64     int done;
65    
66     done = 0;
67     switch (event->type)
68     {
69     case SDL_ACTIVEEVENT:
70     /* See what happened */
71     printf ("app %s ", event->active.gain ? "gained" : "lost");
72     if (event->active.state & SDL_APPACTIVE)
73     {
74     printf ("active ");
75     }
76     else if (event->active.state & SDL_APPMOUSEFOCUS)
77     {
78     printf ("mouse ");
79     }
80     else if (event->active.state & SDL_APPINPUTFOCUS)
81     {
82     printf ("input ");
83     }
84    
85     printf ("focus\n");
86     break;
87    
88 root 1.18 #define VELOCITY 10
89 root 1.1 case SDL_KEYDOWN:
90 root 1.19 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_LEFT) camera_velocity.x--;
93     if (event->key.keysym.sym == SDLK_RIGHT) camera_velocity.x++;
94     if (event->key.keysym.sym == SDLK_a) camera_velocity.y--;
95     if (event->key.keysym.sym == SDLK_s) camera_velocity.y++;
96     if (event->key.keysym.sym == SDLK_v) camera_velocity_factor *= 1.5;
97     if (event->key.keysym.sym == SDLK_b) camera_velocity_factor /= 1.5;
98 root 1.6
99 root 1.1 if (event->key.keysym.sym == SDLK_ESCAPE)
100 root 1.6 done = 1;
101 root 1.1
102     if ((event->key.keysym.sym == SDLK_g) &&
103     (event->key.keysym.mod & KMOD_CTRL))
104 root 1.6 HotKey_ToggleGrab ();
105 root 1.1
106     if ((event->key.keysym.sym == SDLK_z) &&
107     (event->key.keysym.mod & KMOD_CTRL))
108 root 1.6 HotKey_Iconify ();
109 root 1.1
110     if ((event->key.keysym.sym == SDLK_RETURN) &&
111     (event->key.keysym.mod & KMOD_ALT))
112 root 1.6 HotKey_ToggleFullScreen ();
113 root 1.1
114     break;
115    
116 root 1.18 case SDL_KEYUP:
117 root 1.19 if (event->key.keysym.sym == SDLK_UP) camera_velocity.z++;
118     if (event->key.keysym.sym == SDLK_DOWN) camera_velocity.z--;
119     if (event->key.keysym.sym == SDLK_LEFT) camera_velocity.x++;
120     if (event->key.keysym.sym == SDLK_RIGHT) camera_velocity.x--;
121     if (event->key.keysym.sym == SDLK_a) camera_velocity.y++;
122     if (event->key.keysym.sym == SDLK_s) 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     /* Quick utility function for texture creation */
134     static int
135     power_of_two (int input)
136     {
137     int value = 1;
138    
139     while (value < input)
140     {
141     value <<= 1;
142     }
143     return value;
144     }
145    
146     GLuint
147     SDL_GL_LoadTexture (SDL_Surface * surface, GLfloat * texcoord)
148     {
149     GLuint texture;
150     int w, h;
151     SDL_Surface *image;
152     SDL_Rect area;
153     Uint32 saved_flags;
154     Uint8 saved_alpha;
155    
156     /* Use the surface width and height expanded to powers of 2 */
157     w = power_of_two (surface->w);
158     h = power_of_two (surface->h);
159     texcoord[0] = 0.0f; /* Min X */
160     texcoord[1] = 0.0f; /* Min Y */
161     texcoord[2] = (GLfloat) surface->w / w; /* Max X */
162     texcoord[3] = (GLfloat) surface->h / h; /* Max Y */
163    
164     image = SDL_CreateRGBSurface (SDL_SWSURFACE, w, h, 32,
165     #if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */
166     0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000
167     #else
168     0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF
169     #endif
170     );
171     if (image == NULL)
172     {
173     return 0;
174     }
175    
176     /* Save the alpha blending attributes */
177     saved_flags = surface->flags & (SDL_SRCALPHA | SDL_RLEACCELOK);
178     saved_alpha = surface->format->alpha;
179     if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA)
180     {
181     SDL_SetAlpha (surface, 0, 0);
182     }
183    
184     /* Copy the surface into the GL texture image */
185     area.x = 0;
186     area.y = 0;
187     area.w = surface->w;
188     area.h = surface->h;
189     SDL_BlitSurface (surface, &area, image, &area);
190    
191     /* Restore the alpha blending attributes */
192     if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA)
193     {
194     SDL_SetAlpha (surface, saved_flags, saved_alpha);
195     }
196    
197     /* Create an OpenGL texture for the image */
198     glGenTextures (1, &texture);
199     glBindTexture (GL_TEXTURE_2D, texture);
200     glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
201     glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
202     glTexImage2D (GL_TEXTURE_2D,
203     0,
204     GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels);
205     SDL_FreeSurface (image); /* No longer needed */
206    
207     return texture;
208     }
209    
210     int
211     RunGLTest (int argc, char *argv[],
212     int logo, int slowly, int bpp, float gamma, int noframe, int fsaa)
213     {
214     int i;
215     int rgb_size[3];
216     int w = 640;
217     int h = 480;
218     int done = 0;
219     int frames;
220     Uint32 start_time, this_time;
221 root 1.2 Uint32 video_flags;
222 root 1.1 int value;
223    
224     if (SDL_Init (SDL_INIT_VIDEO) < 0)
225     {
226     fprintf (stderr, "Couldn't initialize SDL: %s\n", SDL_GetError ());
227     exit (1);
228     }
229    
230     /* See if we should detect the display depth */
231     if (bpp == 0)
232     {
233     if (SDL_GetVideoInfo ()->vfmt->BitsPerPixel <= 8)
234 root 1.3 bpp = 8;
235 root 1.1 else
236 root 1.3 bpp = 16; /* More doesn't seem to work */
237 root 1.1 }
238    
239 root 1.2 video_flags = SDL_OPENGL;
240    
241 root 1.1 for (i = 1; argv[i]; ++i)
242 root 1.3 if (strcmp (argv[1], "-fullscreen") == 0)
243     video_flags |= SDL_FULLSCREEN;
244 root 1.1
245     if (noframe)
246 root 1.3 video_flags |= SDL_NOFRAME;
247 root 1.1
248     /* Initialize the display */
249     switch (bpp)
250     {
251     case 8:
252     rgb_size[0] = 3;
253     rgb_size[1] = 3;
254     rgb_size[2] = 2;
255     break;
256 root 1.3
257 root 1.1 case 15:
258     case 16:
259     rgb_size[0] = 5;
260     rgb_size[1] = 5;
261     rgb_size[2] = 5;
262     break;
263 root 1.3
264 root 1.1 default:
265     rgb_size[0] = 8;
266     rgb_size[1] = 8;
267     rgb_size[2] = 8;
268     break;
269     }
270 root 1.18
271 root 1.1 SDL_GL_SetAttribute (SDL_GL_RED_SIZE, rgb_size[0]);
272     SDL_GL_SetAttribute (SDL_GL_GREEN_SIZE, rgb_size[1]);
273     SDL_GL_SetAttribute (SDL_GL_BLUE_SIZE, rgb_size[2]);
274     SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE, 16);
275     SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, 1);
276 root 1.3
277 root 1.1 if (fsaa)
278     {
279     SDL_GL_SetAttribute (SDL_GL_MULTISAMPLEBUFFERS, 1);
280     SDL_GL_SetAttribute (SDL_GL_MULTISAMPLESAMPLES, fsaa);
281     }
282 root 1.3
283 root 1.1 if (SDL_SetVideoMode (w, h, bpp, video_flags) == NULL)
284     {
285     fprintf (stderr, "Couldn't set GL mode: %s\n", SDL_GetError ());
286     SDL_Quit ();
287     exit (1);
288     }
289    
290     printf ("Screen BPP: %d\n", SDL_GetVideoSurface ()->format->BitsPerPixel);
291     printf ("\n");
292     printf ("Vendor : %s\n", glGetString (GL_VENDOR));
293     printf ("Renderer : %s\n", glGetString (GL_RENDERER));
294     printf ("Version : %s\n", glGetString (GL_VERSION));
295     printf ("Extensions : %s\n", glGetString (GL_EXTENSIONS));
296     printf ("\n");
297    
298     SDL_GL_GetAttribute (SDL_GL_RED_SIZE, &value);
299     printf ("SDL_GL_RED_SIZE: requested %d, got %d\n", rgb_size[0], value);
300     SDL_GL_GetAttribute (SDL_GL_GREEN_SIZE, &value);
301     printf ("SDL_GL_GREEN_SIZE: requested %d, got %d\n", rgb_size[1], value);
302     SDL_GL_GetAttribute (SDL_GL_BLUE_SIZE, &value);
303     printf ("SDL_GL_BLUE_SIZE: requested %d, got %d\n", rgb_size[2], value);
304     SDL_GL_GetAttribute (SDL_GL_DEPTH_SIZE, &value);
305     printf ("SDL_GL_DEPTH_SIZE: requested %d, got %d\n", bpp, value);
306     SDL_GL_GetAttribute (SDL_GL_DOUBLEBUFFER, &value);
307     printf ("SDL_GL_DOUBLEBUFFER: requested 1, got %d\n", value);
308 root 1.3
309 root 1.1 if (fsaa)
310     {
311     SDL_GL_GetAttribute (SDL_GL_MULTISAMPLEBUFFERS, &value);
312     printf ("SDL_GL_MULTISAMPLEBUFFERS: requested 1, got %d\n", value);
313     SDL_GL_GetAttribute (SDL_GL_MULTISAMPLESAMPLES, &value);
314     printf ("SDL_GL_MULTISAMPLESAMPLES: requested %d, got %d\n", fsaa,
315     value);
316     }
317    
318     /* Set the window manager title bar */
319 root 1.12 SDL_WM_SetCaption ("libgender rendering test", "gendertest");
320 root 1.1
321     /* Set the gamma for the window */
322     if (gamma != 0.0)
323 root 1.3 SDL_SetGamma (gamma, gamma, gamma);
324 root 1.1
325 root 1.17 // load a entity
326     for (int i = 0; i < 7; i++)
327     {
328     txtprt_parser p;
329 root 1.18 entity_transform *f = new entity_transform;
330 root 1.17 entity *e;
331     try
332     {
333     e = p.read ("test.blasc");
334     } catch (txtprt_i_exception & e)
335     {
336     cout << "ERR: " << e.msg << endl;
337     }
338     f->set (e);
339 root 1.26 f->update (matrix::translation (vec3 (0, -1, -i*5)));
340 root 1.17 f->show ();
341     }
342    
343 root 1.13 camera.orig.x = camera.orig.y = camera.orig.z = 0;
344 root 1.6 camera.p = point (0, 0, 10);
345     camera.d = vec3 (0, 0, -1);
346     camera.u = vec3 (0, 1, 0);
347     camera.w = w; camera.h = h;
348     camera.fov = 90;
349 root 1.1
350     glMatrixMode (GL_MODELVIEW);
351     glLoadIdentity ();
352    
353 root 1.16 glEnable (GL_CULL_FACE);
354 root 1.1 glEnable (GL_DEPTH_TEST);
355 root 1.28 //glEnable(GL_VERTEX_PROGRAM_ARB);
356     //glEnable(GL_FRAGMENT_PROGRAM_ARB);
357 root 1.22
358 root 1.11 glShadeModel (GL_SMOOTH);
359 root 1.6
360 root 1.3 glEnable (GL_LIGHTING);
361 root 1.9 //GLfloat lightc[4] = { 1, 0.1, 0.1, 1 };
362 root 1.6 //glLightf (GL_LIGHT0, GL_QUADRATIC_ATTENUATION);
363 root 1.9 //glLightfv (GL_LIGHT0, GL_DIFFUSE, lightc);
364 root 1.6 glEnable (GL_LIGHT0);
365 root 1.1
366     /* Loop until done. */
367     start_time = SDL_GetTicks ();
368     frames = 0;
369 root 1.16
370     draw_context c (camera);
371    
372 root 1.1 while (!done)
373     {
374     GLenum gl_error;
375     char *sdl_error;
376     SDL_Event event;
377    
378     glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
379    
380 root 1.19 camera.p.x += (camera_velocity_factor * camera_velocity.x) * timer.diff;
381     camera.p.y += (camera_velocity_factor * camera_velocity.y) * timer.diff;
382     camera.p.z += (camera_velocity_factor * camera_velocity.z) * timer.diff;
383 root 1.18
384 root 1.6 GLfloat lightp[4];
385     lightp[0] = camera.p.x;
386     lightp[1] = camera.p.y;
387     lightp[2] = camera.p.z;
388     lightp[3] = 1;
389     glLightfv (GL_LIGHT0, GL_POSITION, lightp);
390    
391 root 1.5 #if 0
392 root 1.3 static GLfloat ry;
393 root 1.13 ry += 0.001;
394     camera.d.x = cos (ry);
395     camera.d.z = sin (ry);
396     //camera.d.y = sin (ry * 0.1);
397 root 1.5 #endif
398 root 1.1
399 root 1.16 c.mode = draw_context::DEPTH;
400     camera.draw (c);
401 root 1.9 c.mode = draw_context::LIGHTED;
402 root 1.6 camera.draw (c);
403 root 1.1
404     SDL_GL_SwapBuffers ();
405 root 1.15 timer.frame ();
406 root 1.1
407     /* Check for error conditions. */
408     gl_error = glGetError ();
409    
410     if (gl_error != GL_NO_ERROR)
411 root 1.3 fprintf (stderr, "testgl: OpenGL error: %d\n", gl_error);
412 root 1.1
413     sdl_error = SDL_GetError ();
414    
415     if (sdl_error[0] != '\0')
416     {
417     fprintf (stderr, "testgl: SDL error '%s'\n", sdl_error);
418     SDL_ClearError ();
419     }
420    
421     /* Allow the user to see what's happening */
422 root 1.9 //SDL_Delay (20);
423 root 1.1
424     /* Check if there's a pending event. */
425     while (SDL_PollEvent (&event))
426 root 1.18 done = HandleEvent (&event);
427 root 1.20
428 root 1.18
429 root 1.1 ++frames;
430     }
431    
432     /* Print out the frames per second */
433     this_time = SDL_GetTicks ();
434     if (this_time != start_time)
435     {
436     printf ("%2.2f FPS\n",
437     ((float) frames / (this_time - start_time)) * 1000.0);
438     }
439    
440     if (global_image)
441     {
442     SDL_FreeSurface (global_image);
443     global_image = NULL;
444     }
445     if (global_texture)
446     {
447     glDeleteTextures (1, &global_texture);
448     global_texture = 0;
449     }
450    
451     /* Destroy our GL context, etc. */
452     SDL_Quit ();
453     return (0);
454     }
455    
456     int
457     main (int argc, char *argv[])
458     {
459     int i, logo;
460     int numtests;
461     int bpp = 0;
462     int slowly;
463     float gamma = 0.0;
464     int noframe = 0;
465     int fsaa = 0;
466 root 1.4
467 root 1.1 logo = 0;
468     slowly = 0;
469     numtests = 1;
470     for (i = 1; argv[i]; ++i)
471     {
472     if (strcmp (argv[i], "-twice") == 0)
473     {
474     ++numtests;
475     }
476     if (strcmp (argv[i], "-slow") == 0)
477     {
478     slowly = 1;
479     }
480     if (strcmp (argv[i], "-bpp") == 0)
481     {
482     bpp = atoi (argv[++i]);
483     }
484     if (strcmp (argv[i], "-gamma") == 0)
485     {
486     gamma = (float) atof (argv[++i]);
487     }
488     if (strcmp (argv[i], "-noframe") == 0)
489     {
490     noframe = 1;
491     }
492     if (strcmp (argv[i], "-fsaa") == 0)
493     {
494     ++fsaa;
495     }
496     if (strncmp (argv[i], "-h", 2) == 0)
497     {
498     printf
499     ("Usage: %s [-twice] [-logo] [-slow] [-bpp n] [-gamma n] [-noframe] [-fsaa]\n",
500     argv[0]);
501     exit (0);
502     }
503     }
504 root 1.17
505 root 1.1 for (i = 0; i < numtests; ++i)
506 root 1.17 RunGLTest (argc, argv, logo, slowly, bpp, gamma, noframe, fsaa);
507    
508 root 1.1 return 0;
509     }