ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libgender/test.C
(Generate patch)

Comparing libgender/test.C (file contents):
Revision 1.4 by root, Sun Oct 3 02:19:07 2004 UTC vs.
Revision 1.35 by root, Wed Oct 6 05:24:03 2004 UTC

2#include <stdio.h> 2#include <stdio.h>
3#include <string.h> 3#include <string.h>
4#include <math.h> 4#include <math.h>
5 5
6#include "SDL.h" 6#include "SDL.h"
7
8#include "SDL_opengl.h" 7#include "SDL_opengl.h"
9
10/* Undefine this if you want a flat cube instead of a rainbow cube */
11#define SHADED_CUBE
12
13/* Define this to be the name of the logo image to use with -logo */
14#define LOGO_FILE "icon.bmp"
15 8
16static SDL_Surface *global_image = NULL; 9static SDL_Surface *global_image = NULL;
17static GLuint global_texture = 0; 10static GLuint global_texture = 0;
18 11
12#include "util.h"
19#include "entity.h" 13#include "entity.h"
20
21#include "txtprt_import.h" 14#include "txtprt_import.h"
22 15
16CGcontext cgc;
17CGprogram vsh, fsh;
18CGparameter mv, mvp, lightpos;
19CGprofile vsh_profile, fsh_profile;
20
21static void CheckCgError(void)
22{
23 CGerror err = cgGetError();
24
25 if (err != CG_NO_ERROR)
26 {
27 printf("CG error: %s\n", cgGetErrorString(err));
28 exit(1);
29 }
30}
31
23/**********************************************************************/ 32/**********************************************************************/
33
34view camera;
35vec3 camera_velocity;
36float camera_velocity_factor = 20;
24 37
25void 38void
26HotKey_ToggleFullScreen (void) 39HotKey_ToggleFullScreen (void)
27{ 40{
28 SDL_Surface *screen; 41 SDL_Surface *screen;
29 42
30 screen = SDL_GetVideoSurface (); 43 screen = SDL_GetVideoSurface ();
31 if (SDL_WM_ToggleFullScreen (screen)) 44 if (SDL_WM_ToggleFullScreen (screen))
32 {
33 printf ("Toggled fullscreen mode - now %s\n", 45 printf ("Toggled fullscreen mode - now %s\n",
34 (screen->flags & SDL_FULLSCREEN) ? "fullscreen" : "windowed"); 46 (screen->flags & SDL_FULLSCREEN) ? "fullscreen" : "windowed");
35 }
36 else 47 else
37 {
38 printf ("Unable to toggle fullscreen mode\n"); 48 printf ("Unable to toggle fullscreen mode\n");
39 }
40} 49}
41 50
42void 51void
43HotKey_ToggleGrab (void) 52HotKey_ToggleGrab (void)
44{ 53{
45 SDL_GrabMode mode; 54 SDL_GrabMode mode;
46 55
47 printf ("Ctrl-G: toggling input grab!\n"); 56 printf ("Ctrl-G: toggling input grab!\n");
48 mode = SDL_WM_GrabInput (SDL_GRAB_QUERY); 57 mode = SDL_WM_GrabInput (SDL_GRAB_QUERY);
49 if (mode == SDL_GRAB_ON) 58 if (mode == SDL_GRAB_ON)
50 {
51 printf ("Grab was on\n"); 59 printf ("Grab was on\n");
52 }
53 else 60 else
54 {
55 printf ("Grab was off\n"); 61 printf ("Grab was off\n");
56 }
57 62
58 mode = SDL_WM_GrabInput (mode ? SDL_GRAB_OFF : SDL_GRAB_ON); 63 mode = SDL_WM_GrabInput (mode ? SDL_GRAB_OFF : SDL_GRAB_ON);
59 if (mode == SDL_GRAB_ON) 64 if (mode == SDL_GRAB_ON)
60 {
61 printf ("Grab is now on\n"); 65 printf ("Grab is now on\n");
62 }
63 else 66 else
64 {
65 printf ("Grab is now off\n"); 67 printf ("Grab is now off\n");
66 }
67} 68}
68 69
69void 70void
70HotKey_Iconify (void) 71HotKey_Iconify (void)
71{ 72{
99 100
100 printf ("focus\n"); 101 printf ("focus\n");
101 break; 102 break;
102 103
103 case SDL_KEYDOWN: 104 case SDL_KEYDOWN:
105 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
104 if (event->key.keysym.sym == SDLK_ESCAPE) 114 if (event->key.keysym.sym == SDLK_ESCAPE)
105 {
106 done = 1; 115 done = 1;
107 }
108 116
109 if ((event->key.keysym.sym == SDLK_g) && 117 if ((event->key.keysym.sym == SDLK_g) &&
110 (event->key.keysym.mod & KMOD_CTRL)) 118 (event->key.keysym.mod & KMOD_CTRL))
111 {
112 HotKey_ToggleGrab (); 119 HotKey_ToggleGrab ();
113 }
114 120
115 if ((event->key.keysym.sym == SDLK_z) && 121 if ((event->key.keysym.sym == SDLK_z) &&
116 (event->key.keysym.mod & KMOD_CTRL)) 122 (event->key.keysym.mod & KMOD_CTRL))
117 {
118 HotKey_Iconify (); 123 HotKey_Iconify ();
119 }
120 124
121 if ((event->key.keysym.sym == SDLK_RETURN) && 125 if ((event->key.keysym.sym == SDLK_RETURN) &&
122 (event->key.keysym.mod & KMOD_ALT)) 126 (event->key.keysym.mod & KMOD_ALT))
123 {
124 HotKey_ToggleFullScreen (); 127 HotKey_ToggleFullScreen ();
125 }
126 128
127 printf ("key '%s' pressed\n", SDL_GetKeyName (event->key.keysym.sym)); 129 break;
130
131 case SDL_KEYUP:
132 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--;
128 break; 138 break;
129 139
130 case SDL_QUIT: 140 case SDL_QUIT:
131 done = 1; 141 done = 1;
132 break; 142 break;
133 } 143 }
134 144
135 return (done); 145 return (done);
136}
137
138void
139SDL_GL_Enter2DMode ()
140{
141 SDL_Surface *screen = SDL_GetVideoSurface ();
142
143 /* Note, there may be other things you need to change,
144 depending on how you have your OpenGL state set up.
145 */
146 glPushAttrib (GL_ENABLE_BIT);
147 glDisable (GL_DEPTH_TEST);
148 glDisable (GL_CULL_FACE);
149 glEnable (GL_TEXTURE_2D);
150
151 /* This allows alpha blending of 2D textures with the scene */
152 glEnable (GL_BLEND);
153 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
154
155 glViewport (0, 0, screen->w, screen->h);
156
157 glMatrixMode (GL_PROJECTION);
158 glPushMatrix ();
159 glLoadIdentity ();
160
161 glOrtho (0.0, (GLdouble) screen->w, (GLdouble) screen->h, 0.0, 0.0, 1.0);
162
163 glMatrixMode (GL_MODELVIEW);
164 glPushMatrix ();
165 glLoadIdentity ();
166
167 glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
168}
169
170void
171SDL_GL_Leave2DMode ()
172{
173 glMatrixMode (GL_MODELVIEW);
174 glPopMatrix ();
175
176 glMatrixMode (GL_PROJECTION);
177 glPopMatrix ();
178
179 glPopAttrib ();
180} 146}
181 147
182/* Quick utility function for texture creation */ 148/* Quick utility function for texture creation */
183static int 149static int
184power_of_two (int input) 150power_of_two (int input)
314 rgb_size[0] = 8; 280 rgb_size[0] = 8;
315 rgb_size[1] = 8; 281 rgb_size[1] = 8;
316 rgb_size[2] = 8; 282 rgb_size[2] = 8;
317 break; 283 break;
318 } 284 }
285
319 SDL_GL_SetAttribute (SDL_GL_RED_SIZE, rgb_size[0]); 286 SDL_GL_SetAttribute (SDL_GL_RED_SIZE, rgb_size[0]);
320 SDL_GL_SetAttribute (SDL_GL_GREEN_SIZE, rgb_size[1]); 287 SDL_GL_SetAttribute (SDL_GL_GREEN_SIZE, rgb_size[1]);
321 SDL_GL_SetAttribute (SDL_GL_BLUE_SIZE, rgb_size[2]); 288 SDL_GL_SetAttribute (SDL_GL_BLUE_SIZE, rgb_size[2]);
322 SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE, 16); 289 SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE, 16);
323 SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, 1); 290 SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, 1);
362 printf ("SDL_GL_MULTISAMPLESAMPLES: requested %d, got %d\n", fsaa, 329 printf ("SDL_GL_MULTISAMPLESAMPLES: requested %d, got %d\n", fsaa,
363 value); 330 value);
364 } 331 }
365 332
366 /* Set the window manager title bar */ 333 /* Set the window manager title bar */
367 SDL_WM_SetCaption ("SDL GL test", "testgl"); 334 SDL_WM_SetCaption ("libgender rendering test", "gendertest");
368 335
369 /* Set the gamma for the window */ 336 /* Set the gamma for the window */
370 if (gamma != 0.0) 337 if (gamma != 0.0)
371 SDL_SetGamma (gamma, gamma, gamma); 338 SDL_SetGamma (gamma, gamma, gamma);
372 339
373 view v; 340 // load a entity
341 for (int i = 0; i < 7; i++)
342 {
343 txtprt_parser p;
344 entity_transform *f = new entity_transform;
345 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 f->update (matrix::translation (vec3 (0, -1, -i*5)));
355 f->show ();
356 }
357
358 camera.orig.x = camera.orig.y = camera.orig.z = 0;
374 v.p = point (0, 0, 0); 359 camera.p = point (0, 0, 10);
375 v.d = vec3 (0, 0, -1); 360 camera.d = vec3 (0, 0, -1);
376 v.u = vec3 (0, 1, 0); 361 camera.u = vec3 (0, 1, 0);
377 v.w = w; v.h = h; 362 camera.w = w; camera.h = h;
378 v.fov = 90; 363 camera.fov = 90;
379 364
380 glMatrixMode (GL_MODELVIEW); 365 glMatrixMode (GL_MODELVIEW);
381 glLoadIdentity (); 366 glLoadIdentity ();
382 367
368 glEnable (GL_CULL_FACE);
383 glEnable (GL_DEPTH_TEST); 369 glEnable (GL_DEPTH_TEST);
384 370
385 glDepthFunc (GL_LESS);
386
387 glShadeModel (GL_SMOOTH); 371 glShadeModel (GL_SMOOTH);
388 GLfloat ambient[4] = { 1, 1, 1, 1 }; 372
389 glEnable (GL_LIGHTING); 373 glEnable (GL_LIGHTING);
390 glEnable (GL_COLOR_MATERIAL); 374 //GLfloat lightc[4] = { 1, 0.1, 0.1, 1 };
391 glLightModelfv (GL_LIGHT_MODEL_AMBIENT, ambient); 375 //glLightf (GL_LIGHT0, GL_QUADRATIC_ATTENUATION);
376 //glLightfv (GL_LIGHT0, GL_DIFFUSE, lightc);
377 glEnable (GL_LIGHT0);
378
379 cgc = cgCreateContext ();
380
381 vsh_profile = CG_PROFILE_ARBVP1;
382 if (cgGLIsProfileSupported (CG_PROFILE_VP30)) vsh_profile = CG_PROFILE_VP30;
383 if (cgGLIsProfileSupported (CG_PROFILE_VP40)) vsh_profile = CG_PROFILE_VP40;
384 fsh_profile = CG_PROFILE_ARBFP1;
385 if (cgGLIsProfileSupported (CG_PROFILE_FP30)) fsh_profile = CG_PROFILE_FP30;
386 if (cgGLIsProfileSupported (CG_PROFILE_FP40)) fsh_profile = CG_PROFILE_FP40;
387
388 vsh = cgCreateProgramFromFile (cgc, CG_SOURCE, "vsh.cg", vsh_profile, 0, 0);
389 CheckCgError ();
390 cgGLLoadProgram (vsh);
391 CheckCgError ();
392 mv = cgGetNamedParameter (vsh, "WorldProj");
393 mvp = cgGetNamedParameter (vsh, "WorldViewProj");
394 lightpos = cgGetNamedParameter (vsh, "LightPos");
395 CheckCgError ();
396
397 fsh = cgCreateProgramFromFile (cgc, CG_SOURCE, "fsh.cg", fsh_profile, 0, 0);
398 CheckCgError ();
399 cgGLLoadProgram (fsh);
400 CheckCgError ();
401
402 cgGLBindProgram (vsh);
403 CheckCgError ();
404 cgGLBindProgram (fsh);
405 CheckCgError ();
392 406
393 /* Loop until done. */ 407 /* Loop until done. */
394 start_time = SDL_GetTicks (); 408 start_time = SDL_GetTicks ();
395 frames = 0; 409 frames = 0;
410
396 while (!done) 411 while (!done)
397 { 412 {
398 GLenum gl_error; 413 GLenum gl_error;
399 char *sdl_error; 414 char *sdl_error;
400 SDL_Event event; 415 SDL_Event event;
401 416
402 /* Do our drawing, too. */
403 glClearColor (0.0, 0.0, 0.0, 1.0);
404 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 417 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
405 418
419 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
423 cgGLSetParameter4f (lightpos, camera.p.x, camera.p.y, camera.p.z, 1);
424
425#if 0
406 static GLfloat ry; 426 static GLfloat ry;
407 ry += 0.1; 427 ry += 0.001;
408 v.d.x = cos (ry); 428 camera.d.x = cos (ry);
409 v.d.z = sin (ry); 429 camera.d.z = sin (ry);
430 //camera.d.y = sin (ry * 0.1);
431#endif
410 432
411 draw_context c; 433 camera.begin ();
412 v.draw (c); 434 camera.pass (view::DEPTH);
435 camera.pass (view::LIGHTED);
436 camera.end ();
413 437
414 SDL_GL_SwapBuffers (); 438 SDL_GL_SwapBuffers ();
439 timer.frame ();
415 440
416 /* Check for error conditions. */ 441 /* Check for error conditions. */
417 gl_error = glGetError (); 442 gl_error = glGetError ();
418 443
419 if (gl_error != GL_NO_ERROR) 444 if (gl_error != GL_NO_ERROR)
426 fprintf (stderr, "testgl: SDL error '%s'\n", sdl_error); 451 fprintf (stderr, "testgl: SDL error '%s'\n", sdl_error);
427 SDL_ClearError (); 452 SDL_ClearError ();
428 } 453 }
429 454
430 /* Allow the user to see what's happening */ 455 /* Allow the user to see what's happening */
431 SDL_Delay (20); 456 //SDL_Delay (20);
432 457
433 /* Check if there's a pending event. */ 458 /* Check if there's a pending event. */
434 while (SDL_PollEvent (&event)) 459 while (SDL_PollEvent (&event))
435 {
436 done = HandleEvent (&event); 460 done = HandleEvent (&event);
437 } 461
462
438 ++frames; 463 ++frames;
439 } 464 }
440 465
441 /* Print out the frames per second */ 466 /* Print out the frames per second */
442 this_time = SDL_GetTicks (); 467 this_time = SDL_GetTicks ();
471 int slowly; 496 int slowly;
472 float gamma = 0.0; 497 float gamma = 0.0;
473 int noframe = 0; 498 int noframe = 0;
474 int fsaa = 0; 499 int fsaa = 0;
475 500
476 // load a entity
477 txtprt_parser p;
478 try {
479 entity *e = p.read ("test.blasc");
480 } catch (txtprt_i_exception & e) {
481 cout << "ERR: " << e.msg << endl;
482 }
483 // now please draw my entity ;)
484
485 logo = 0; 501 logo = 0;
486 slowly = 0; 502 slowly = 0;
487 numtests = 1; 503 numtests = 1;
488 for (i = 1; argv[i]; ++i) 504 for (i = 1; argv[i]; ++i)
489 { 505 {
517 ("Usage: %s [-twice] [-logo] [-slow] [-bpp n] [-gamma n] [-noframe] [-fsaa]\n", 533 ("Usage: %s [-twice] [-logo] [-slow] [-bpp n] [-gamma n] [-noframe] [-fsaa]\n",
518 argv[0]); 534 argv[0]);
519 exit (0); 535 exit (0);
520 } 536 }
521 } 537 }
538
522 for (i = 0; i < numtests; ++i) 539 for (i = 0; i < numtests; ++i)
523 {
524 RunGLTest (argc, argv, logo, slowly, bpp, gamma, noframe, fsaa); 540 RunGLTest (argc, argv, logo, slowly, bpp, gamma, noframe, fsaa);
525 } 541
526 return 0; 542 return 0;
527} 543}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines