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