ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libgender/test.C
Revision: 1.24
Committed: Tue Oct 5 02:42:12 2004 UTC (19 years, 8 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.23: +33 -32 lines
Log Message:
*** empty log message ***

File Contents

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