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

Comparing libgender/test.C (file contents):
Revision 1.9 by root, Sun Oct 3 04:00:51 2004 UTC vs.
Revision 1.36 by root, Wed Oct 6 05:27:19 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/**********************************************************************/
24 33
25view camera; 34view camera;
35vec3 camera_velocity;
36float camera_velocity_factor = 20;
26 37
27void 38void
28HotKey_ToggleFullScreen (void) 39HotKey_ToggleFullScreen (void)
29{ 40{
30 SDL_Surface *screen; 41 SDL_Surface *screen;
89 100
90 printf ("focus\n"); 101 printf ("focus\n");
91 break; 102 break;
92 103
93 case SDL_KEYDOWN: 104 case SDL_KEYDOWN:
94
95 if (event->key.keysym.sym == SDLK_UP) 105 if (event->key.keysym.sym == SDLK_UP) camera_velocity.z--;
96 camera.p.z -= 1;
97 if (event->key.keysym.sym == SDLK_DOWN) 106 if (event->key.keysym.sym == SDLK_DOWN) camera_velocity.z++;
98 camera.p.z += 1;
99 if (event->key.keysym.sym == SDLK_LEFT) 107 if (event->key.keysym.sym == SDLK_LEFT) camera_velocity.x--;
100 camera.p.x -= 1;
101 if (event->key.keysym.sym == SDLK_RIGHT) 108 if (event->key.keysym.sym == SDLK_RIGHT) camera_velocity.x++;
102 camera.p.x += 1; 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;
103 113
104 if (event->key.keysym.sym == SDLK_ESCAPE) 114 if (event->key.keysym.sym == SDLK_ESCAPE)
105 done = 1; 115 done = 1;
106 116
107 if ((event->key.keysym.sym == SDLK_g) && 117 if ((event->key.keysym.sym == SDLK_g) &&
114 124
115 if ((event->key.keysym.sym == SDLK_RETURN) && 125 if ((event->key.keysym.sym == SDLK_RETURN) &&
116 (event->key.keysym.mod & KMOD_ALT)) 126 (event->key.keysym.mod & KMOD_ALT))
117 HotKey_ToggleFullScreen (); 127 HotKey_ToggleFullScreen ();
118 128
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--;
119 break; 138 break;
120 139
121 case SDL_QUIT: 140 case SDL_QUIT:
122 done = 1; 141 done = 1;
123 break; 142 break;
261 rgb_size[0] = 8; 280 rgb_size[0] = 8;
262 rgb_size[1] = 8; 281 rgb_size[1] = 8;
263 rgb_size[2] = 8; 282 rgb_size[2] = 8;
264 break; 283 break;
265 } 284 }
285
266 SDL_GL_SetAttribute (SDL_GL_RED_SIZE, rgb_size[0]); 286 SDL_GL_SetAttribute (SDL_GL_RED_SIZE, rgb_size[0]);
267 SDL_GL_SetAttribute (SDL_GL_GREEN_SIZE, rgb_size[1]); 287 SDL_GL_SetAttribute (SDL_GL_GREEN_SIZE, rgb_size[1]);
268 SDL_GL_SetAttribute (SDL_GL_BLUE_SIZE, rgb_size[2]); 288 SDL_GL_SetAttribute (SDL_GL_BLUE_SIZE, rgb_size[2]);
269 SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE, 16); 289 SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE, 16);
270 SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, 1); 290 SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, 1);
309 printf ("SDL_GL_MULTISAMPLESAMPLES: requested %d, got %d\n", fsaa, 329 printf ("SDL_GL_MULTISAMPLESAMPLES: requested %d, got %d\n", fsaa,
310 value); 330 value);
311 } 331 }
312 332
313 /* Set the window manager title bar */ 333 /* Set the window manager title bar */
314 SDL_WM_SetCaption ("SDL GL test", "testgl"); 334 SDL_WM_SetCaption ("libgender rendering test", "gendertest");
315 335
316 /* Set the gamma for the window */ 336 /* Set the gamma for the window */
317 if (gamma != 0.0) 337 if (gamma != 0.0)
318 SDL_SetGamma (gamma, gamma, gamma); 338 SDL_SetGamma (gamma, gamma, gamma);
319 339
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;
320 camera.p = point (0, 0, 10); 359 camera.p = point (0, 0, 10);
321 camera.d = vec3 (0, 0, -1); 360 camera.d = vec3 (0, 0, -1);
322 camera.u = vec3 (0, 1, 0); 361 camera.u = vec3 (0, 1, 0);
323 camera.w = w; camera.h = h; 362 camera.w = w; camera.h = h;
324 camera.fov = 90; 363 camera.fov = 90;
325 364
326 glMatrixMode (GL_MODELVIEW); 365 glMatrixMode (GL_MODELVIEW);
327 glLoadIdentity (); 366 glLoadIdentity ();
328 367
368 glEnable (GL_CULL_FACE);
329 glEnable (GL_DEPTH_TEST); 369 glEnable (GL_DEPTH_TEST);
330 370
331 glDepthFunc (GL_LESS);
332
333 glShadeModel (GL_SMOOTH); 371 glShadeModel (GL_SMOOTH);
334 372
335 glEnable (GL_LIGHTING); 373 glEnable (GL_LIGHTING);
336
337 //GLfloat lightc[4] = { 1, 0.1, 0.1, 1 }; 374 //GLfloat lightc[4] = { 1, 0.1, 0.1, 1 };
338 //glLightf (GL_LIGHT0, GL_QUADRATIC_ATTENUATION); 375 //glLightf (GL_LIGHT0, GL_QUADRATIC_ATTENUATION);
339 //glLightfv (GL_LIGHT0, GL_DIFFUSE, lightc); 376 //glLightfv (GL_LIGHT0, GL_DIFFUSE, lightc);
340 glEnable (GL_LIGHT0); 377 glEnable (GL_LIGHT0);
341 glEnable (GL_COLOR_MATERIAL); 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 ();
342 406
343 /* Loop until done. */ 407 /* Loop until done. */
344 start_time = SDL_GetTicks (); 408 start_time = SDL_GetTicks ();
345 frames = 0; 409 frames = 0;
410
346 while (!done) 411 while (!done)
347 { 412 {
348 GLenum gl_error; 413 GLenum gl_error;
349 char *sdl_error; 414 char *sdl_error;
350 SDL_Event event; 415 SDL_Event event;
351 416
352 /* Do our drawing, too. */
353 glClearColor (0.0, 0.0, 0.0, 1.0);
354 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 417 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
355 418
356 GLfloat lightp[4]; 419 camera.p.x += (camera_velocity_factor * camera_velocity.x) * timer.diff;
357 lightp[0] = camera.p.x; 420 camera.p.y += (camera_velocity_factor * camera_velocity.y) * timer.diff;
358 lightp[1] = camera.p.y; 421 camera.p.z += (camera_velocity_factor * camera_velocity.z) * timer.diff;
359 lightp[2] = camera.p.z; 422
360 lightp[3] = 1; 423 cgGLSetParameter4f (lightpos, camera.p.x, camera.p.y, camera.p.z, 1);
361 glLightfv (GL_LIGHT0, GL_POSITION, lightp);
362 424
363#if 0 425#if 0
364 static GLfloat ry; 426 static GLfloat ry;
365 ry += 0.03; 427 ry += 0.001;
366 v.d.x = cos (ry); 428 camera.d.x = cos (ry);
367 v.d.z = sin (ry); 429 camera.d.z = sin (ry);
430 //camera.d.y = sin (ry * 0.1);
368#endif 431#endif
369 432
370 draw_context c; 433 camera.begin ();
371 c.mode = draw_context::LIGHTED; 434 camera.pass (view::DEPTH);
435 camera.pass (view::LIGHTED);
372 camera.draw (c); 436 camera.end ();
373 437
374 SDL_GL_SwapBuffers (); 438 SDL_GL_SwapBuffers ();
439 timer.frame ();
375 440
376 /* Check for error conditions. */ 441 /* Check for error conditions. */
377 gl_error = glGetError (); 442 gl_error = glGetError ();
378 443
379 if (gl_error != GL_NO_ERROR) 444 if (gl_error != GL_NO_ERROR)
390 /* Allow the user to see what's happening */ 455 /* Allow the user to see what's happening */
391 //SDL_Delay (20); 456 //SDL_Delay (20);
392 457
393 /* Check if there's a pending event. */ 458 /* Check if there's a pending event. */
394 while (SDL_PollEvent (&event)) 459 while (SDL_PollEvent (&event))
395 {
396 done = HandleEvent (&event); 460 done = HandleEvent (&event);
397 } 461
462
398 ++frames; 463 ++frames;
399 } 464 }
400 465
401 /* Print out the frames per second */ 466 /* Print out the frames per second */
402 this_time = SDL_GetTicks (); 467 this_time = SDL_GetTicks ();
431 int slowly; 496 int slowly;
432 float gamma = 0.0; 497 float gamma = 0.0;
433 int noframe = 0; 498 int noframe = 0;
434 int fsaa = 0; 499 int fsaa = 0;
435 500
436 // load a entity
437 txtprt_parser p;
438 entity *e;
439 try {
440 e = p.read ("test.blasc");
441 } catch (txtprt_i_exception & e) {
442 cout << "ERR: " << e.msg << endl;
443 }
444 e->show ();
445
446 logo = 0; 501 logo = 0;
447 slowly = 0; 502 slowly = 0;
448 numtests = 1; 503 numtests = 1;
449 for (i = 1; argv[i]; ++i) 504 for (i = 1; argv[i]; ++i)
450 { 505 {
478 ("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",
479 argv[0]); 534 argv[0]);
480 exit (0); 535 exit (0);
481 } 536 }
482 } 537 }
538
483 for (i = 0; i < numtests; ++i) 539 for (i = 0; i < numtests; ++i)
484 {
485 RunGLTest (argc, argv, logo, slowly, bpp, gamma, noframe, fsaa); 540 RunGLTest (argc, argv, logo, slowly, bpp, gamma, noframe, fsaa);
486 } 541
487 return 0; 542 return 0;
488} 543}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines