ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libgender/test.C
Revision: 1.22
Committed: Tue Oct 5 02:14:06 2004 UTC (19 years, 8 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.21: +51 -0 lines
Log Message:
sh4d3rz

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