ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libgender/test.C
Revision: 1.36
Committed: Wed Oct 6 05:27:19 2004 UTC (19 years, 8 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.35: +4 -4 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.35 CGprofile vsh_profile, fsh_profile;
20 root 1.29
21     static void CheckCgError(void)
22     {
23     CGerror err = cgGetError();
24 root 1.2
25 root 1.29 if (err != CG_NO_ERROR)
26     {
27     printf("CG error: %s\n", cgGetErrorString(err));
28     exit(1);
29     }
30     }
31 root 1.4
32 root 1.1 /**********************************************************************/
33    
34 root 1.6 view camera;
35 root 1.18 vec3 camera_velocity;
36 root 1.32 float camera_velocity_factor = 20;
37 root 1.6
38 root 1.1 void
39     HotKey_ToggleFullScreen (void)
40     {
41     SDL_Surface *screen;
42    
43     screen = SDL_GetVideoSurface ();
44     if (SDL_WM_ToggleFullScreen (screen))
45 root 1.5 printf ("Toggled fullscreen mode - now %s\n",
46     (screen->flags & SDL_FULLSCREEN) ? "fullscreen" : "windowed");
47 root 1.1 else
48 root 1.5 printf ("Unable to toggle fullscreen mode\n");
49 root 1.1 }
50    
51     void
52     HotKey_ToggleGrab (void)
53     {
54     SDL_GrabMode mode;
55    
56     printf ("Ctrl-G: toggling input grab!\n");
57     mode = SDL_WM_GrabInput (SDL_GRAB_QUERY);
58     if (mode == SDL_GRAB_ON)
59 root 1.5 printf ("Grab was on\n");
60 root 1.1 else
61 root 1.5 printf ("Grab was off\n");
62 root 1.1
63     mode = SDL_WM_GrabInput (mode ? SDL_GRAB_OFF : SDL_GRAB_ON);
64     if (mode == SDL_GRAB_ON)
65 root 1.5 printf ("Grab is now on\n");
66 root 1.1 else
67 root 1.5 printf ("Grab is now off\n");
68 root 1.1 }
69    
70     void
71     HotKey_Iconify (void)
72     {
73     printf ("Ctrl-Z: iconifying window!\n");
74     SDL_WM_IconifyWindow ();
75     }
76    
77     int
78     HandleEvent (SDL_Event * event)
79     {
80     int done;
81    
82     done = 0;
83     switch (event->type)
84     {
85     case SDL_ACTIVEEVENT:
86     /* See what happened */
87     printf ("app %s ", event->active.gain ? "gained" : "lost");
88     if (event->active.state & SDL_APPACTIVE)
89     {
90     printf ("active ");
91     }
92     else if (event->active.state & SDL_APPMOUSEFOCUS)
93     {
94     printf ("mouse ");
95     }
96     else if (event->active.state & SDL_APPINPUTFOCUS)
97     {
98     printf ("input ");
99     }
100    
101     printf ("focus\n");
102     break;
103    
104     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 root 1.35
381     vsh_profile = CG_PROFILE_ARBVP1;
382 root 1.36 //if (cgGLIsProfileSupported (CG_PROFILE_VP30)) vsh_profile = CG_PROFILE_VP30;
383     //if (cgGLIsProfileSupported (CG_PROFILE_VP40)) vsh_profile = CG_PROFILE_VP40;
384 root 1.35 fsh_profile = CG_PROFILE_ARBFP1;
385 root 1.36 //if (cgGLIsProfileSupported (CG_PROFILE_FP30)) fsh_profile = CG_PROFILE_FP30;
386     //if (cgGLIsProfileSupported (CG_PROFILE_FP40)) fsh_profile = CG_PROFILE_FP40;
387 root 1.35
388     vsh = cgCreateProgramFromFile (cgc, CG_SOURCE, "vsh.cg", vsh_profile, 0, 0);
389 root 1.29 CheckCgError ();
390     cgGLLoadProgram (vsh);
391     CheckCgError ();
392     mv = cgGetNamedParameter (vsh, "WorldProj");
393     mvp = cgGetNamedParameter (vsh, "WorldViewProj");
394 root 1.30 lightpos = cgGetNamedParameter (vsh, "LightPos");
395 root 1.29 CheckCgError ();
396    
397 root 1.35 fsh = cgCreateProgramFromFile (cgc, CG_SOURCE, "fsh.cg", fsh_profile, 0, 0);
398 root 1.30 CheckCgError ();
399 root 1.29 cgGLLoadProgram (fsh);
400 root 1.30 CheckCgError ();
401 root 1.29
402     cgGLBindProgram (vsh);
403 root 1.30 CheckCgError ();
404 root 1.29 cgGLBindProgram (fsh);
405 root 1.30 CheckCgError ();
406 root 1.1
407     /* Loop until done. */
408     start_time = SDL_GetTicks ();
409     frames = 0;
410 root 1.16
411 root 1.1 while (!done)
412     {
413     GLenum gl_error;
414     char *sdl_error;
415     SDL_Event event;
416    
417     glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
418    
419 root 1.19 camera.p.x += (camera_velocity_factor * camera_velocity.x) * timer.diff;
420     camera.p.y += (camera_velocity_factor * camera_velocity.y) * timer.diff;
421     camera.p.z += (camera_velocity_factor * camera_velocity.z) * timer.diff;
422 root 1.18
423 root 1.30 cgGLSetParameter4f (lightpos, camera.p.x, camera.p.y, camera.p.z, 1);
424 root 1.6
425 root 1.5 #if 0
426 root 1.3 static GLfloat ry;
427 root 1.13 ry += 0.001;
428     camera.d.x = cos (ry);
429     camera.d.z = sin (ry);
430     //camera.d.y = sin (ry * 0.1);
431 root 1.5 #endif
432 root 1.1
433 root 1.34 camera.begin ();
434     camera.pass (view::DEPTH);
435     camera.pass (view::LIGHTED);
436     camera.end ();
437 root 1.1
438     SDL_GL_SwapBuffers ();
439 root 1.15 timer.frame ();
440 root 1.1
441     /* Check for error conditions. */
442     gl_error = glGetError ();
443    
444     if (gl_error != GL_NO_ERROR)
445 root 1.3 fprintf (stderr, "testgl: OpenGL error: %d\n", gl_error);
446 root 1.1
447     sdl_error = SDL_GetError ();
448    
449     if (sdl_error[0] != '\0')
450     {
451     fprintf (stderr, "testgl: SDL error '%s'\n", sdl_error);
452     SDL_ClearError ();
453     }
454    
455     /* Allow the user to see what's happening */
456 root 1.9 //SDL_Delay (20);
457 root 1.1
458     /* Check if there's a pending event. */
459     while (SDL_PollEvent (&event))
460 root 1.18 done = HandleEvent (&event);
461 root 1.20
462 root 1.18
463 root 1.1 ++frames;
464     }
465    
466     /* Print out the frames per second */
467     this_time = SDL_GetTicks ();
468     if (this_time != start_time)
469     {
470     printf ("%2.2f FPS\n",
471     ((float) frames / (this_time - start_time)) * 1000.0);
472     }
473    
474     if (global_image)
475     {
476     SDL_FreeSurface (global_image);
477     global_image = NULL;
478     }
479     if (global_texture)
480     {
481     glDeleteTextures (1, &global_texture);
482     global_texture = 0;
483     }
484    
485     /* Destroy our GL context, etc. */
486     SDL_Quit ();
487     return (0);
488     }
489    
490     int
491     main (int argc, char *argv[])
492     {
493     int i, logo;
494     int numtests;
495     int bpp = 0;
496     int slowly;
497     float gamma = 0.0;
498     int noframe = 0;
499     int fsaa = 0;
500 root 1.4
501 root 1.1 logo = 0;
502     slowly = 0;
503     numtests = 1;
504     for (i = 1; argv[i]; ++i)
505     {
506     if (strcmp (argv[i], "-twice") == 0)
507     {
508     ++numtests;
509     }
510     if (strcmp (argv[i], "-slow") == 0)
511     {
512     slowly = 1;
513     }
514     if (strcmp (argv[i], "-bpp") == 0)
515     {
516     bpp = atoi (argv[++i]);
517     }
518     if (strcmp (argv[i], "-gamma") == 0)
519     {
520     gamma = (float) atof (argv[++i]);
521     }
522     if (strcmp (argv[i], "-noframe") == 0)
523     {
524     noframe = 1;
525     }
526     if (strcmp (argv[i], "-fsaa") == 0)
527     {
528     ++fsaa;
529     }
530     if (strncmp (argv[i], "-h", 2) == 0)
531     {
532     printf
533     ("Usage: %s [-twice] [-logo] [-slow] [-bpp n] [-gamma n] [-noframe] [-fsaa]\n",
534     argv[0]);
535     exit (0);
536     }
537     }
538 root 1.17
539 root 1.1 for (i = 0; i < numtests; ++i)
540 root 1.17 RunGLTest (argc, argv, logo, slowly, bpp, gamma, noframe, fsaa);
541    
542 root 1.1 return 0;
543     }