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

Comparing libgender/test.C (file contents):
Revision 1.5 by root, Sun Oct 3 02:38:33 2004 UTC vs.
Revision 1.55 by root, Sun Oct 10 00:41: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_angle, camera_angle2, camera_velocity_angle, camera_velocity_angle2;
37float camera_velocity_factor = 80;
24 38
25void 39void
26HotKey_ToggleFullScreen (void) 40HotKey_ToggleFullScreen (void)
27{ 41{
28 SDL_Surface *screen; 42 SDL_Surface *screen;
87 101
88 printf ("focus\n"); 102 printf ("focus\n");
89 break; 103 break;
90 104
91 case SDL_KEYDOWN: 105 case SDL_KEYDOWN:
106 if (event->key.keysym.sym == SDLK_UP) camera_velocity.z--;
107 if (event->key.keysym.sym == SDLK_DOWN) camera_velocity.z++;
108 if (event->key.keysym.sym == SDLK_RIGHT) camera_velocity_angle++;
109 if (event->key.keysym.sym == SDLK_LEFT) camera_velocity_angle--;
110 if (event->key.keysym.sym == SDLK_PAGEUP) camera_velocity_angle2++;
111 if (event->key.keysym.sym == SDLK_PAGEDOWN) camera_velocity_angle2--;
112 if (event->key.keysym.sym == SDLK_e) camera_velocity.y--;
113 if (event->key.keysym.sym == SDLK_q) camera_velocity.y++;
114 if (event->key.keysym.sym == SDLK_v) camera_velocity_factor *= 1.5;
115 if (event->key.keysym.sym == SDLK_b) camera_velocity_factor /= 1.5;
116
92 if (event->key.keysym.sym == SDLK_ESCAPE) 117 if (event->key.keysym.sym == SDLK_ESCAPE)
93 {
94 done = 1; 118 done = 1;
95 }
96 119
97 if ((event->key.keysym.sym == SDLK_g) && 120 if ((event->key.keysym.sym == SDLK_g) &&
98 (event->key.keysym.mod & KMOD_CTRL)) 121 (event->key.keysym.mod & KMOD_CTRL))
99 {
100 HotKey_ToggleGrab (); 122 HotKey_ToggleGrab ();
101 }
102 123
103 if ((event->key.keysym.sym == SDLK_z) && 124 if ((event->key.keysym.sym == SDLK_z) &&
104 (event->key.keysym.mod & KMOD_CTRL)) 125 (event->key.keysym.mod & KMOD_CTRL))
105 {
106 HotKey_Iconify (); 126 HotKey_Iconify ();
107 }
108 127
109 if ((event->key.keysym.sym == SDLK_RETURN) && 128 if ((event->key.keysym.sym == SDLK_RETURN) &&
110 (event->key.keysym.mod & KMOD_ALT)) 129 (event->key.keysym.mod & KMOD_ALT))
111 {
112 HotKey_ToggleFullScreen (); 130 HotKey_ToggleFullScreen ();
113 }
114 131
115 printf ("key '%s' pressed\n", SDL_GetKeyName (event->key.keysym.sym)); 132 break;
133
134 case SDL_KEYUP:
135 if (event->key.keysym.sym == SDLK_UP) camera_velocity.z++;
136 if (event->key.keysym.sym == SDLK_DOWN) camera_velocity.z--;
137 if (event->key.keysym.sym == SDLK_LEFT) camera_velocity_angle++;
138 if (event->key.keysym.sym == SDLK_RIGHT) camera_velocity_angle--;
139 if (event->key.keysym.sym == SDLK_PAGEUP) camera_velocity_angle2--;
140 if (event->key.keysym.sym == SDLK_PAGEDOWN) camera_velocity_angle2++;
141 if (event->key.keysym.sym == SDLK_e) camera_velocity.y++;
142 if (event->key.keysym.sym == SDLK_q) camera_velocity.y--;
116 break; 143 break;
117 144
118 case SDL_QUIT: 145 case SDL_QUIT:
119 done = 1; 146 done = 1;
120 break; 147 break;
121 } 148 }
122 149
123 return (done); 150 return (done);
124} 151}
125 152
126void 153void draw_floor (int size, int dx, int dy, int dz)
127SDL_GL_Enter2DMode ()
128{ 154{
129 SDL_Surface *screen = SDL_GetVideoSurface (); 155 int x, z, ry;
130 156
131 /* Note, there may be other things you need to change, 157 for (x = 0; x < 100; x++)
132 depending on how you have your OpenGL state set up.
133 */
134 glPushAttrib (GL_ENABLE_BIT);
135 glDisable (GL_DEPTH_TEST);
136 glDisable (GL_CULL_FACE);
137 glEnable (GL_TEXTURE_2D);
138
139 /* This allows alpha blending of 2D textures with the scene */
140 glEnable (GL_BLEND);
141 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
142
143 glViewport (0, 0, screen->w, screen->h);
144
145 glMatrixMode (GL_PROJECTION);
146 glPushMatrix ();
147 glLoadIdentity ();
148
149 glOrtho (0.0, (GLdouble) screen->w, (GLdouble) screen->h, 0.0, 0.0, 1.0);
150
151 glMatrixMode (GL_MODELVIEW);
152 glPushMatrix ();
153 glLoadIdentity ();
154
155 glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
156}
157
158void
159SDL_GL_Leave2DMode ()
160{
161 glMatrixMode (GL_MODELVIEW);
162 glPopMatrix ();
163
164 glMatrixMode (GL_PROJECTION);
165 glPopMatrix ();
166
167 glPopAttrib ();
168}
169
170/* Quick utility function for texture creation */
171static int
172power_of_two (int input)
173{
174 int value = 1;
175
176 while (value < input)
177 {
178 value <<= 1;
179 } 158 {
180 return value; 159 for (z = 0; z < 100; z++)
181} 160 {
161 vector<vertex2d> pts;
162 pts.push_back (vertex2d (point ( 0, 0, 0), vec3 (0, 1, 0), texc (0, 0)));
163 pts.push_back (vertex2d (point ( 0, 0, size), vec3 (0, 1, 0), texc (0, 1)));
164 pts.push_back (vertex2d (point (size, 0, size), vec3 (0, 1, 0), texc (1, 1)));
165 pts.push_back (vertex2d (point (size, 0, 0), vec3 (0, 1, 0), texc (1, 0)));
182 166
183GLuint 167 geometry_quads *q = new geometry_quads;
184SDL_GL_LoadTexture (SDL_Surface * surface, GLfloat * texcoord) 168 q->set (pts);
185{ 169 entity *e = new entity (q);
186 GLuint texture; 170 e->move (vec3 (dx + x * size, dy, dz + z * size));
187 int w, h; 171 e->show ();
188 SDL_Surface *image; 172 }
189 SDL_Rect area;
190 Uint32 saved_flags;
191 Uint8 saved_alpha;
192
193 /* Use the surface width and height expanded to powers of 2 */
194 w = power_of_two (surface->w);
195 h = power_of_two (surface->h);
196 texcoord[0] = 0.0f; /* Min X */
197 texcoord[1] = 0.0f; /* Min Y */
198 texcoord[2] = (GLfloat) surface->w / w; /* Max X */
199 texcoord[3] = (GLfloat) surface->h / h; /* Max Y */
200
201 image = SDL_CreateRGBSurface (SDL_SWSURFACE, w, h, 32,
202#if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */
203 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000
204#else
205 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF
206#endif
207 );
208 if (image == NULL)
209 { 173 }
210 return 0;
211 }
212
213 /* Save the alpha blending attributes */
214 saved_flags = surface->flags & (SDL_SRCALPHA | SDL_RLEACCELOK);
215 saved_alpha = surface->format->alpha;
216 if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA)
217 {
218 SDL_SetAlpha (surface, 0, 0);
219 }
220
221 /* Copy the surface into the GL texture image */
222 area.x = 0;
223 area.y = 0;
224 area.w = surface->w;
225 area.h = surface->h;
226 SDL_BlitSurface (surface, &area, image, &area);
227
228 /* Restore the alpha blending attributes */
229 if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA)
230 {
231 SDL_SetAlpha (surface, saved_flags, saved_alpha);
232 }
233
234 /* Create an OpenGL texture for the image */
235 glGenTextures (1, &texture);
236 glBindTexture (GL_TEXTURE_2D, texture);
237 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
238 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
239 glTexImage2D (GL_TEXTURE_2D,
240 0,
241 GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels);
242 SDL_FreeSurface (image); /* No longer needed */
243
244 return texture;
245} 174}
246 175
176void draw_test_nurb ()
177{
178 geometry_nurbs *q = new geometry_nurbs;
179 q->set ();
180 entity *e = new entity (q);
181 e->move (vec3 (10, 3, -4));
182 e->show ();
183}
184
185extern void draw_level ();
247int 186int
248RunGLTest (int argc, char *argv[], 187RunGLTest (int argc, char *argv[],
249 int logo, int slowly, int bpp, float gamma, int noframe, int fsaa) 188 int logo, int slowly, int bpp, float gamma, int noframe, int fsaa)
250{ 189{
251 int i; 190 int i;
255 int done = 0; 194 int done = 0;
256 int frames; 195 int frames;
257 Uint32 start_time, this_time; 196 Uint32 start_time, this_time;
258 Uint32 video_flags; 197 Uint32 video_flags;
259 int value; 198 int value;
199 GLenum gl_error;
260 200
261 if (SDL_Init (SDL_INIT_VIDEO) < 0) 201 if (SDL_Init (SDL_INIT_VIDEO) < 0)
262 { 202 {
263 fprintf (stderr, "Couldn't initialize SDL: %s\n", SDL_GetError ()); 203 fprintf (stderr, "Couldn't initialize SDL: %s\n", SDL_GetError ());
264 exit (1); 204 exit (1);
302 rgb_size[0] = 8; 242 rgb_size[0] = 8;
303 rgb_size[1] = 8; 243 rgb_size[1] = 8;
304 rgb_size[2] = 8; 244 rgb_size[2] = 8;
305 break; 245 break;
306 } 246 }
247
307 SDL_GL_SetAttribute (SDL_GL_RED_SIZE, rgb_size[0]); 248 SDL_GL_SetAttribute (SDL_GL_RED_SIZE, rgb_size[0]);
308 SDL_GL_SetAttribute (SDL_GL_GREEN_SIZE, rgb_size[1]); 249 SDL_GL_SetAttribute (SDL_GL_GREEN_SIZE, rgb_size[1]);
309 SDL_GL_SetAttribute (SDL_GL_BLUE_SIZE, rgb_size[2]); 250 SDL_GL_SetAttribute (SDL_GL_BLUE_SIZE, rgb_size[2]);
310 SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE, 16); 251 SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE, 16);
311 SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, 1); 252 SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, 1);
350 printf ("SDL_GL_MULTISAMPLESAMPLES: requested %d, got %d\n", fsaa, 291 printf ("SDL_GL_MULTISAMPLESAMPLES: requested %d, got %d\n", fsaa,
351 value); 292 value);
352 } 293 }
353 294
354 /* Set the window manager title bar */ 295 /* Set the window manager title bar */
355 SDL_WM_SetCaption ("SDL GL test", "testgl"); 296 SDL_WM_SetCaption ("libgender rendering test", "gendertest");
356 297
357 /* Set the gamma for the window */ 298 /* Set the gamma for the window */
358 if (gamma != 0.0) 299 if (gamma != 0.0)
359 SDL_SetGamma (gamma, gamma, gamma); 300 SDL_SetGamma (gamma, gamma, gamma);
360 301
361 view v; 302 for (int i = 0; i < 1; i++)
303 {
304 // load a entity
305 txtprt_parser p;
306 geometry *g;
307 try
308 {
309 g = p.read ("test.blasc");
310 }
311 catch (txtprt_i_exception & e)
312 {
313 cout << "ERR: " << e.msg << endl;
314 }
315
316 entity *e = new entity (g);
317 e->move (vec3 (i*5, -3, -i*10));
318 e->show ();
319 }
320
321 //draw_floor (10, -500, -10, -1000);
322 draw_level ();
323 //draw_test_nurb ();
324
325 //camera.orig.x = 108; camera.orig.y = 0; camera.orig.z = -368;
326 camera.orig.x = 0; camera.orig.y = 0; camera.orig.z = 0;
362 v.p = point (0, 0, 0); 327 camera.p = point (0, 0, 10);
363 v.d = vec3 (0, 0, -1); 328 camera.d = vec3 (0, 0, -1);
364 v.u = vec3 (0, 1, 0); 329 camera.u = vec3 (0, 1, 0);
365 v.w = w; v.h = h; 330 camera.w = w; camera.h = h;
366 v.fov = 90; 331 camera.fov = 35;
332 camera.z_near = 1.;
367 333
368 glMatrixMode (GL_MODELVIEW); 334 glMatrixMode (GL_MODELVIEW);
369 glLoadIdentity (); 335 glLoadIdentity ();
370 336
337 glEnable (GL_CULL_FACE);
371 glEnable (GL_DEPTH_TEST); 338 glEnable (GL_DEPTH_TEST);
372 339
373 glDepthFunc (GL_LESS);
374
375 glShadeModel (GL_SMOOTH); 340 glShadeModel (GL_SMOOTH);
376 GLfloat ambient[4] = { 1, 1, 1, 1 }; 341
377 glEnable (GL_LIGHTING); 342 glEnable (GL_LIGHTING);
378 glEnable (GL_COLOR_MATERIAL); 343 glEnable (GL_LIGHT0);
379 glLightModelfv (GL_LIGHT_MODEL_AMBIENT, ambient); 344
345 cgc = cgCreateContext ();
346
347 vsh_profile = CG_PROFILE_ARBVP1;
348 //if (cgGLIsProfileSupported (CG_PROFILE_VP30)) vsh_profile = CG_PROFILE_VP30;
349 //if (cgGLIsProfileSupported (CG_PROFILE_VP40)) vsh_profile = CG_PROFILE_VP40;
350 fsh_profile = CG_PROFILE_ARBFP1;
351 //if (cgGLIsProfileSupported (CG_PROFILE_FP30)) fsh_profile = CG_PROFILE_FP30;
352 //if (cgGLIsProfileSupported (CG_PROFILE_FP40)) fsh_profile = CG_PROFILE_FP40;
353
354 vsh = cgCreateProgramFromFile (cgc, CG_SOURCE, "vsh.cg", vsh_profile, 0, 0);
355 CheckCgError ();
356 cgGLLoadProgram (vsh);
357 CheckCgError ();
358 mv = cgGetNamedParameter (vsh, "WorldProj");
359 mvp = cgGetNamedParameter (vsh, "WorldViewProj");
360 lightpos = cgGetNamedParameter (vsh, "LightPos");
361 CheckCgError ();
362
363 CGparameter g_Texture; // the texture parameter
364
365
366 fsh = cgCreateProgramFromFile (cgc, CG_SOURCE, "fsh.cg", fsh_profile, 0, 0);
367 Texture t("o.jpg");
368 g_Texture = cgGetNamedParameter(fsh, "Texture"); // the texture cg-warper ;)
369 cgGLSetTextureParameter(g_Texture, t.texture); // Bind the texture number 999 to g_Texture
370 CheckCgError ();
371 cgGLLoadProgram (fsh);
372 CheckCgError ();
373
374 cgGLBindProgram (vsh);
375 CheckCgError ();
376 cgGLBindProgram (fsh);
377 CheckCgError ();
380 378
381 /* Loop until done. */ 379 /* Loop until done. */
382 start_time = SDL_GetTicks (); 380 start_time = SDL_GetTicks ();
383 frames = 0; 381 frames = 0;
382
384 while (!done) 383 while (!done)
385 { 384 {
386 GLenum gl_error;
387 char *sdl_error; 385 char *sdl_error;
388 SDL_Event event; 386 SDL_Event event;
389 387
390 /* Do our drawing, too. */ 388 camera_angle += 180 * camera_velocity_angle * timer.diff;
391 glClearColor (0.0, 0.0, 0.0, 1.0); 389 camera_angle2 += 180 * camera_velocity_angle2 * timer.diff;
392 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 390
391 vec3 geradeaus = matrix::rotation (-camera_angle, vec3 (0, 1, 0)) * vec3 (0, 0, -1);
392 vec3 right = matrix::rotation (90., vec3 (0, 1, 0)) * geradeaus;
393
394 camera.d = matrix::rotation (camera_angle2, right) * geradeaus;
395 camera.u = cross (camera.d, right);
396
397 camera.p = camera.p - camera.d * (camera_velocity_factor * timer.diff) * camera_velocity.z;
398 camera.p = camera.p - camera.u * (camera_velocity_factor * timer.diff) * camera_velocity.y;
399
400 cgGLSetParameter4f (lightpos, camera.p.x, camera.p.y, camera.p.z, 1);
401
402 glBindTexture (GL_TEXTURE_2D, t.texture);
403 cgGLEnableTextureParameter (g_Texture); // Enable the texture parameter
393 404
394#if 0 405#if 0
395 static GLfloat ry; 406 static GLfloat ry;
396 ry += 0.03; 407 ry += 0.001;
397 v.d.x = cos (ry); 408 camera.d.x = cos (ry);
398 v.d.z = sin (ry); 409 camera.d.z = sin (ry);
410 //camera.d.y = sin (ry * 0.1);
399#endif 411#endif
400 412
401 draw_context c; 413 camera.begin ();
402 v.draw (c); 414 camera.pass (view::DEPTH);
415 camera.pass (view::LIGHTED);
416 camera.end ();
403 417
404 SDL_GL_SwapBuffers (); 418 SDL_GL_SwapBuffers ();
419 timer.frame ();
405 420
406 /* Check for error conditions. */ 421 /* Check for error conditions. */
407 gl_error = glGetError (); 422 gl_error = glGetError ();
408 423
409 if (gl_error != GL_NO_ERROR)
410 fprintf (stderr, "testgl: OpenGL error: %d\n", gl_error); 424 if (gl_error != GL_NO_ERROR) fprintf (stderr, "testgl: OpenGL error: %d\n", gl_error);
411 425
412 sdl_error = SDL_GetError (); 426 sdl_error = SDL_GetError ();
413 427
414 if (sdl_error[0] != '\0') 428 if (sdl_error[0] != '\0')
415 { 429 {
416 fprintf (stderr, "testgl: SDL error '%s'\n", sdl_error); 430 fprintf (stderr, "testgl: SDL error '%s'\n", sdl_error);
417 SDL_ClearError (); 431 SDL_ClearError ();
418 } 432 }
419 433
420 /* Allow the user to see what's happening */ 434 /* Allow the user to see what's happening */
421 SDL_Delay (20); 435 //SDL_Delay (40);
422 436
423 /* Check if there's a pending event. */ 437 /* Check if there's a pending event. */
424 while (SDL_PollEvent (&event)) 438 while (SDL_PollEvent (&event))
425 {
426 done = HandleEvent (&event); 439 done = HandleEvent (&event);
427 } 440
441
428 ++frames; 442 ++frames;
429 } 443 }
430 444
431 /* Print out the frames per second */ 445 /* Print out the frames per second */
432 this_time = SDL_GetTicks (); 446 this_time = SDL_GetTicks ();
449 463
450 /* Destroy our GL context, etc. */ 464 /* Destroy our GL context, etc. */
451 SDL_Quit (); 465 SDL_Quit ();
452 return (0); 466 return (0);
453} 467}
454
455int 468int
456main (int argc, char *argv[]) 469main (int argc, char *argv[])
457{ 470{
458 int i, logo; 471 int i, logo;
459 int numtests; 472 int numtests;
461 int slowly; 474 int slowly;
462 float gamma = 0.0; 475 float gamma = 0.0;
463 int noframe = 0; 476 int noframe = 0;
464 int fsaa = 0; 477 int fsaa = 0;
465 478
466 // load a entity
467 txtprt_parser p;
468 entity_base *e;
469 try {
470 e = p.read ("test.blasc");
471 } catch (txtprt_i_exception & e) {
472 cout << "ERR: " << e.msg << endl;
473 }
474 world.add (e);
475
476 logo = 0; 479 logo = 0;
477 slowly = 0; 480 slowly = 0;
478 numtests = 1; 481 numtests = 1;
479 for (i = 1; argv[i]; ++i) 482 for (i = 1; argv[i]; ++i)
480 { 483 {
508 ("Usage: %s [-twice] [-logo] [-slow] [-bpp n] [-gamma n] [-noframe] [-fsaa]\n", 511 ("Usage: %s [-twice] [-logo] [-slow] [-bpp n] [-gamma n] [-noframe] [-fsaa]\n",
509 argv[0]); 512 argv[0]);
510 exit (0); 513 exit (0);
511 } 514 }
512 } 515 }
516
513 for (i = 0; i < numtests; ++i) 517 for (i = 0; i < numtests; ++i)
514 {
515 RunGLTest (argc, argv, logo, slowly, bpp, gamma, noframe, fsaa); 518 RunGLTest (argc, argv, logo, slowly, bpp, gamma, noframe, fsaa);
516 } 519
517 return 0; 520 return 0;
518} 521}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines