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.24 by root, Tue Oct 5 02:42:12 2004 UTC

1#include <stdlib.h> 1#include <stdlib.h>
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#include <sh/sh.hpp>
6
7using namespace SH;
5 8
6#include "SDL.h" 9#include "SDL.h"
7 10
8#include "SDL_opengl.h" 11#include "SDL_opengl.h"
9 12
20 23
21#include "txtprt_import.h" 24#include "txtprt_import.h"
22 25
23/**********************************************************************/ 26/**********************************************************************/
24 27
28view camera;
29vec3 camera_velocity;
30float camera_velocity_factor = 10;
31
32ShColor3f color = ShColor3f(.5, 0.9, 0.2);
33ShPoint3f lightPos = ShPoint3f(0.0, 10.0, 10.0);
34ShMatrix4x4f mvp;
35ShProgram vsh, fsh;
36
37void
38init_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
25void 74void
26HotKey_ToggleFullScreen (void) 75HotKey_ToggleFullScreen (void)
27{ 76{
28 SDL_Surface *screen; 77 SDL_Surface *screen;
29 78
30 screen = SDL_GetVideoSurface (); 79 screen = SDL_GetVideoSurface ();
31 if (SDL_WM_ToggleFullScreen (screen)) 80 if (SDL_WM_ToggleFullScreen (screen))
32 {
33 printf ("Toggled fullscreen mode - now %s\n", 81 printf ("Toggled fullscreen mode - now %s\n",
34 (screen->flags & SDL_FULLSCREEN) ? "fullscreen" : "windowed"); 82 (screen->flags & SDL_FULLSCREEN) ? "fullscreen" : "windowed");
35 }
36 else 83 else
37 {
38 printf ("Unable to toggle fullscreen mode\n"); 84 printf ("Unable to toggle fullscreen mode\n");
39 }
40} 85}
41 86
42void 87void
43HotKey_ToggleGrab (void) 88HotKey_ToggleGrab (void)
44{ 89{
45 SDL_GrabMode mode; 90 SDL_GrabMode mode;
46 91
47 printf ("Ctrl-G: toggling input grab!\n"); 92 printf ("Ctrl-G: toggling input grab!\n");
48 mode = SDL_WM_GrabInput (SDL_GRAB_QUERY); 93 mode = SDL_WM_GrabInput (SDL_GRAB_QUERY);
49 if (mode == SDL_GRAB_ON) 94 if (mode == SDL_GRAB_ON)
50 {
51 printf ("Grab was on\n"); 95 printf ("Grab was on\n");
52 }
53 else 96 else
54 {
55 printf ("Grab was off\n"); 97 printf ("Grab was off\n");
56 }
57 98
58 mode = SDL_WM_GrabInput (mode ? SDL_GRAB_OFF : SDL_GRAB_ON); 99 mode = SDL_WM_GrabInput (mode ? SDL_GRAB_OFF : SDL_GRAB_ON);
59 if (mode == SDL_GRAB_ON) 100 if (mode == SDL_GRAB_ON)
60 {
61 printf ("Grab is now on\n"); 101 printf ("Grab is now on\n");
62 }
63 else 102 else
64 {
65 printf ("Grab is now off\n"); 103 printf ("Grab is now off\n");
66 }
67} 104}
68 105
69void 106void
70HotKey_Iconify (void) 107HotKey_Iconify (void)
71{ 108{
98 } 135 }
99 136
100 printf ("focus\n"); 137 printf ("focus\n");
101 break; 138 break;
102 139
140#define VELOCITY 10
103 case SDL_KEYDOWN: 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
104 if (event->key.keysym.sym == SDLK_ESCAPE) 151 if (event->key.keysym.sym == SDLK_ESCAPE)
105 {
106 done = 1; 152 done = 1;
107 }
108 153
109 if ((event->key.keysym.sym == SDLK_g) && 154 if ((event->key.keysym.sym == SDLK_g) &&
110 (event->key.keysym.mod & KMOD_CTRL)) 155 (event->key.keysym.mod & KMOD_CTRL))
111 {
112 HotKey_ToggleGrab (); 156 HotKey_ToggleGrab ();
113 }
114 157
115 if ((event->key.keysym.sym == SDLK_z) && 158 if ((event->key.keysym.sym == SDLK_z) &&
116 (event->key.keysym.mod & KMOD_CTRL)) 159 (event->key.keysym.mod & KMOD_CTRL))
117 {
118 HotKey_Iconify (); 160 HotKey_Iconify ();
119 }
120 161
121 if ((event->key.keysym.sym == SDLK_RETURN) && 162 if ((event->key.keysym.sym == SDLK_RETURN) &&
122 (event->key.keysym.mod & KMOD_ALT)) 163 (event->key.keysym.mod & KMOD_ALT))
123 {
124 HotKey_ToggleFullScreen (); 164 HotKey_ToggleFullScreen ();
125 }
126 165
127 printf ("key '%s' pressed\n", SDL_GetKeyName (event->key.keysym.sym)); 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--;
128 break; 175 break;
129 176
130 case SDL_QUIT: 177 case SDL_QUIT:
131 done = 1; 178 done = 1;
132 break; 179 break;
133 } 180 }
134 181
135 return (done); 182 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} 183}
181 184
182/* Quick utility function for texture creation */ 185/* Quick utility function for texture creation */
183static int 186static int
184power_of_two (int input) 187power_of_two (int input)
314 rgb_size[0] = 8; 317 rgb_size[0] = 8;
315 rgb_size[1] = 8; 318 rgb_size[1] = 8;
316 rgb_size[2] = 8; 319 rgb_size[2] = 8;
317 break; 320 break;
318 } 321 }
322
319 SDL_GL_SetAttribute (SDL_GL_RED_SIZE, rgb_size[0]); 323 SDL_GL_SetAttribute (SDL_GL_RED_SIZE, rgb_size[0]);
320 SDL_GL_SetAttribute (SDL_GL_GREEN_SIZE, rgb_size[1]); 324 SDL_GL_SetAttribute (SDL_GL_GREEN_SIZE, rgb_size[1]);
321 SDL_GL_SetAttribute (SDL_GL_BLUE_SIZE, rgb_size[2]); 325 SDL_GL_SetAttribute (SDL_GL_BLUE_SIZE, rgb_size[2]);
322 SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE, 16); 326 SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE, 16);
323 SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, 1); 327 SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, 1);
362 printf ("SDL_GL_MULTISAMPLESAMPLES: requested %d, got %d\n", fsaa, 366 printf ("SDL_GL_MULTISAMPLESAMPLES: requested %d, got %d\n", fsaa,
363 value); 367 value);
364 } 368 }
365 369
366 /* Set the window manager title bar */ 370 /* Set the window manager title bar */
367 SDL_WM_SetCaption ("SDL GL test", "testgl"); 371 SDL_WM_SetCaption ("libgender rendering test", "gendertest");
368 372
369 /* Set the gamma for the window */ 373 /* Set the gamma for the window */
370 if (gamma != 0.0) 374 if (gamma != 0.0)
371 SDL_SetGamma (gamma, gamma, gamma); 375 SDL_SetGamma (gamma, gamma, gamma);
372 376
373 view v; 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;
374 v.p = point (0, 0, 0); 396 camera.p = point (0, 0, 10);
375 v.d = vec3 (0, 0, -1); 397 camera.d = vec3 (0, 0, -1);
376 v.u = vec3 (0, 1, 0); 398 camera.u = vec3 (0, 1, 0);
377 v.w = w; v.h = h; 399 camera.w = w; camera.h = h;
378 v.fov = 90; 400 camera.fov = 90;
379 401
380 glMatrixMode (GL_MODELVIEW); 402 glMatrixMode (GL_MODELVIEW);
381 glLoadIdentity (); 403 glLoadIdentity ();
382 404
405 glEnable (GL_CULL_FACE);
383 glEnable (GL_DEPTH_TEST); 406 glEnable (GL_DEPTH_TEST);
407 shInit();
408 shSetBackend("arb");
409 glEnable(GL_VERTEX_PROGRAM_ARB);
410 glEnable(GL_FRAGMENT_PROGRAM_ARB);
384 411
385 glDepthFunc (GL_LESS); 412 init_shaders();
386 413 shBind(vsh);
414 shBind(fsh);
387 glShadeModel (GL_SMOOTH); 415 glShadeModel (GL_SMOOTH);
388 GLfloat ambient[4] = { 1, 1, 1, 1 }; 416
389 glEnable (GL_LIGHTING); 417 glEnable (GL_LIGHTING);
390 glEnable (GL_COLOR_MATERIAL); 418 //GLfloat lightc[4] = { 1, 0.1, 0.1, 1 };
391 glLightModelfv (GL_LIGHT_MODEL_AMBIENT, ambient); 419 //glLightf (GL_LIGHT0, GL_QUADRATIC_ATTENUATION);
420 //glLightfv (GL_LIGHT0, GL_DIFFUSE, lightc);
421 glEnable (GL_LIGHT0);
392 422
393 /* Loop until done. */ 423 /* Loop until done. */
394 start_time = SDL_GetTicks (); 424 start_time = SDL_GetTicks ();
395 frames = 0; 425 frames = 0;
426
427 draw_context c (camera);
428
396 while (!done) 429 while (!done)
397 { 430 {
398 GLenum gl_error; 431 GLenum gl_error;
399 char *sdl_error; 432 char *sdl_error;
400 SDL_Event event; 433 SDL_Event event;
401 434
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); 435 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
405 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
406 static GLfloat ry; 449 static GLfloat ry;
407 ry += 0.1; 450 ry += 0.001;
408 v.d.x = cos (ry); 451 camera.d.x = cos (ry);
409 v.d.z = sin (ry); 452 camera.d.z = sin (ry);
453 //camera.d.y = sin (ry * 0.1);
454#endif
410 455
411 draw_context c; 456 c.mode = draw_context::DEPTH;
412 v.draw (c); 457 camera.draw (c);
458 c.mode = draw_context::LIGHTED;
459 camera.draw (c);
413 460
414 SDL_GL_SwapBuffers (); 461 SDL_GL_SwapBuffers ();
462 timer.frame ();
415 463
416 /* Check for error conditions. */ 464 /* Check for error conditions. */
417 gl_error = glGetError (); 465 gl_error = glGetError ();
418 466
419 if (gl_error != GL_NO_ERROR) 467 if (gl_error != GL_NO_ERROR)
426 fprintf (stderr, "testgl: SDL error '%s'\n", sdl_error); 474 fprintf (stderr, "testgl: SDL error '%s'\n", sdl_error);
427 SDL_ClearError (); 475 SDL_ClearError ();
428 } 476 }
429 477
430 /* Allow the user to see what's happening */ 478 /* Allow the user to see what's happening */
431 SDL_Delay (20); 479 //SDL_Delay (20);
432 480
433 /* Check if there's a pending event. */ 481 /* Check if there's a pending event. */
434 while (SDL_PollEvent (&event)) 482 while (SDL_PollEvent (&event))
435 {
436 done = HandleEvent (&event); 483 done = HandleEvent (&event);
437 } 484
485
438 ++frames; 486 ++frames;
439 } 487 }
440 488
441 /* Print out the frames per second */ 489 /* Print out the frames per second */
442 this_time = SDL_GetTicks (); 490 this_time = SDL_GetTicks ();
471 int slowly; 519 int slowly;
472 float gamma = 0.0; 520 float gamma = 0.0;
473 int noframe = 0; 521 int noframe = 0;
474 int fsaa = 0; 522 int fsaa = 0;
475 523
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; 524 logo = 0;
486 slowly = 0; 525 slowly = 0;
487 numtests = 1; 526 numtests = 1;
488 for (i = 1; argv[i]; ++i) 527 for (i = 1; argv[i]; ++i)
489 { 528 {
517 ("Usage: %s [-twice] [-logo] [-slow] [-bpp n] [-gamma n] [-noframe] [-fsaa]\n", 556 ("Usage: %s [-twice] [-logo] [-slow] [-bpp n] [-gamma n] [-noframe] [-fsaa]\n",
518 argv[0]); 557 argv[0]);
519 exit (0); 558 exit (0);
520 } 559 }
521 } 560 }
561
522 for (i = 0; i < numtests; ++i) 562 for (i = 0; i < numtests; ++i)
523 {
524 RunGLTest (argc, argv, logo, slowly, bpp, gamma, noframe, fsaa); 563 RunGLTest (argc, argv, logo, slowly, bpp, gamma, noframe, fsaa);
525 } 564
526 return 0; 565 return 0;
527} 566}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines