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

Comparing libgender/test.C (file contents):
Revision 1.45 by root, Wed Oct 6 17:55:40 2004 UTC vs.
Revision 1.68 by root, Thu Oct 21 21:20:09 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 5
6#include "opengl.h"
7
6#include "SDL.h" 8#include "SDL.h"
7#include "SDL_opengl.h" 9#include "SDL_opengl.h"
8 10
9static SDL_Surface *global_image = NULL; 11static SDL_Surface *global_image = NULL;
10static GLuint global_texture = 0; 12static GLuint global_texture = 0;
11 13
12#include "util.h" 14#include "util.h"
13#include "entity.h" 15#include "entity.h"
14#include "txtprt_import.h" 16#include "txtprt_import.h"
15 17
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
32/**********************************************************************/ 18/**********************************************************************/
33 19
34view camera; 20view camera;
35vec3 camera_velocity; 21vec3 camera_velocity;
36float camera_angle = 0, camera_velocity_angle; 22float camera_angle, camera_angle2, camera_velocity_angle, camera_velocity_angle2;
37float camera_velocity_factor = 20; 23float camera_velocity_factor = 80;
38 24
39void 25void
40HotKey_ToggleFullScreen (void) 26HotKey_ToggleFullScreen (void)
41{ 27{
42 SDL_Surface *screen; 28 SDL_Surface *screen;
85 { 71 {
86 case SDL_ACTIVEEVENT: 72 case SDL_ACTIVEEVENT:
87 /* See what happened */ 73 /* See what happened */
88 printf ("app %s ", event->active.gain ? "gained" : "lost"); 74 printf ("app %s ", event->active.gain ? "gained" : "lost");
89 if (event->active.state & SDL_APPACTIVE) 75 if (event->active.state & SDL_APPACTIVE)
90 {
91 printf ("active "); 76 printf ("active ");
92 }
93 else if (event->active.state & SDL_APPMOUSEFOCUS) 77 else if (event->active.state & SDL_APPMOUSEFOCUS)
94 {
95 printf ("mouse "); 78 printf ("mouse ");
96 }
97 else if (event->active.state & SDL_APPINPUTFOCUS) 79 else if (event->active.state & SDL_APPINPUTFOCUS)
98 {
99 printf ("input "); 80 printf ("input ");
100 }
101 81
102 printf ("focus\n"); 82 printf ("focus\n");
103 break; 83 break;
104 84
105 case SDL_KEYDOWN: 85 case SDL_KEYDOWN:
106 if (event->key.keysym.sym == SDLK_UP) camera_velocity.z--; 86 if (event->key.keysym.sym == SDLK_UP) camera_velocity.z--;
107 if (event->key.keysym.sym == SDLK_DOWN) camera_velocity.z++; 87 if (event->key.keysym.sym == SDLK_DOWN) camera_velocity.z++;
108 if (event->key.keysym.sym == SDLK_LEFT) camera_velocity.x--;
109 if (event->key.keysym.sym == SDLK_RIGHT) camera_velocity.x++; 88 if (event->key.keysym.sym == SDLK_RIGHT) camera_velocity_angle++;
89 if (event->key.keysym.sym == SDLK_LEFT) camera_velocity_angle--;
90 if (event->key.keysym.sym == SDLK_PAGEUP) camera_velocity_angle2++;
91 if (event->key.keysym.sym == SDLK_PAGEDOWN) camera_velocity_angle2--;
110 if (event->key.keysym.sym == SDLK_a) camera_velocity.y--; 92 if (event->key.keysym.sym == SDLK_e) camera_velocity.y--;
111 if (event->key.keysym.sym == SDLK_s) camera_velocity.y++; 93 if (event->key.keysym.sym == SDLK_q) camera_velocity.y++;
112 if (event->key.keysym.sym == SDLK_v) camera_velocity_factor *= 1.5; 94 if (event->key.keysym.sym == SDLK_v) camera_velocity_factor *= 1.5;
113 if (event->key.keysym.sym == SDLK_b) camera_velocity_factor /= 1.5; 95 if (event->key.keysym.sym == SDLK_b) camera_velocity_factor /= 1.5;
114 if (event->key.keysym.sym == SDLK_e) camera_velocity_angle++;
115 if (event->key.keysym.sym == SDLK_q) camera_velocity_angle--;
116 96
117 if (event->key.keysym.sym == SDLK_ESCAPE) 97 if (event->key.keysym.sym == SDLK_ESCAPE)
118 done = 1; 98 done = 1;
119 99
120 if ((event->key.keysym.sym == SDLK_g) && 100 if ((event->key.keysym.sym == SDLK_g) &&
132 break; 112 break;
133 113
134 case SDL_KEYUP: 114 case SDL_KEYUP:
135 if (event->key.keysym.sym == SDLK_UP) camera_velocity.z++; 115 if (event->key.keysym.sym == SDLK_UP) camera_velocity.z++;
136 if (event->key.keysym.sym == SDLK_DOWN) camera_velocity.z--; 116 if (event->key.keysym.sym == SDLK_DOWN) camera_velocity.z--;
137 if (event->key.keysym.sym == SDLK_LEFT) camera_velocity.x++; 117 if (event->key.keysym.sym == SDLK_LEFT) camera_velocity_angle++;
138 if (event->key.keysym.sym == SDLK_RIGHT) camera_velocity.x--; 118 if (event->key.keysym.sym == SDLK_RIGHT) camera_velocity_angle--;
119 if (event->key.keysym.sym == SDLK_PAGEUP) camera_velocity_angle2--;
120 if (event->key.keysym.sym == SDLK_PAGEDOWN) camera_velocity_angle2++;
139 if (event->key.keysym.sym == SDLK_a) camera_velocity.y++; 121 if (event->key.keysym.sym == SDLK_e) camera_velocity.y++;
140 if (event->key.keysym.sym == SDLK_s) camera_velocity.y--; 122 if (event->key.keysym.sym == SDLK_q) camera_velocity.y--;
141 if (event->key.keysym.sym == SDLK_e) camera_velocity_angle--;
142 if (event->key.keysym.sym == SDLK_q) camera_velocity_angle++;
143 break; 123 break;
144 124
145 case SDL_QUIT: 125 case SDL_QUIT:
146 done = 1; 126 done = 1;
147 break; 127 break;
148 } 128 }
149 129
150 return (done); 130 return (done);
151} 131}
152 132
133void draw_floor (int size, int dx, int dy, int dz)
134{
135 int x, z, ry;
136
137 for (x = 0; x < 10; x++)
138 {
139 for (z = 0; z < 10; z++)
140 {
141 vector<vertex_t2f_n3f_v3f> pts;
142 pts.push_back (vertex_t2f_n3f_v3f (point ( 0, 0, 0), vec3 (0, 1, 0), tex2 (0, 0)));
143 pts.push_back (vertex_t2f_n3f_v3f (point ( 0, 0, size), vec3 (0, 1, 0), tex2 (0, 1)));
144 pts.push_back (vertex_t2f_n3f_v3f (point (size, 0, size), vec3 (0, 1, 0), tex2 (1, 1)));
145 pts.push_back (vertex_t2f_n3f_v3f (point (size, 0, 0), vec3 (0, 1, 0), tex2 (1, 0)));
146
147 geometry_quads *q = new geometry_quads;
148 q->m = new simple_material;
149 q->set (pts);
150 entity *e = new entity (q);
151 e->move (vec3 (dx + x * size, dy, dz + z * size));
152 e->show ();
153 }
154 }
155}
156
157void draw_test_nurb ()
158{
159 geometry_nurbs *q = new geometry_nurbs;
160 q->set ();
161 entity *e = new entity (q);
162 e->move (vec3 (10, 3, -4));
163 e->show ();
164}
165
166void fisch (vec3 center, GLfloat radius, int depth)
167{
168 entity *planet = new entity (new geometry_sphere (radius));
169 planet->move (center);
170 planet->show ();
171
172 if (--depth)
173 {
174 GLfloat r2 = radius * .1F;
175 GLfloat r3 = radius + r2;
176
177 fisch (center + vec3 (r3, 0, 0), r2, depth);
178 fisch (center + vec3 (0, 0, r3), r2, depth);
179 fisch (center + vec3 (-r3, 0, 0), r2, depth);
180 }
181}
182
183extern void draw_level ();
153int 184int
154RunGLTest (int argc, char *argv[], 185RunGLTest (int argc, char *argv[],
155 int logo, int slowly, int bpp, float gamma, int noframe, int fsaa) 186 int logo, int slowly, int bpp, float gamma, int noframe, int fsaa)
156{ 187{
157 int i; 188 int i;
158 int rgb_size[3]; 189 int rgb_size[3];
159 int w = 640; 190 int w = 640;
160 int h = 480; 191 int h = 480;
161 int done = 0; 192 int done = 0;
162 int frames; 193 int frames;
163 Uint32 start_time, this_time;
164 Uint32 video_flags; 194 Uint32 video_flags;
165 int value; 195 int value;
166 GLenum gl_error; 196 GLenum gl_error;
167 197
168 if (SDL_Init (SDL_INIT_VIDEO) < 0) 198 if (SDL_Init (SDL_INIT_VIDEO) < 0)
264 294
265 /* Set the gamma for the window */ 295 /* Set the gamma for the window */
266 if (gamma != 0.0) 296 if (gamma != 0.0)
267 SDL_SetGamma (gamma, gamma, gamma); 297 SDL_SetGamma (gamma, gamma, gamma);
268 298
299 entity *planet = new entity (new geometry_sphere (10));
300 planet->move (vec3 (0, 0, -20));
301 planet->show ();
302
303#if 1
269 for (int i = 0; i < 70; i++) 304 for (int i = 0; i < 20; i++)
270 { 305 {
271 // load a entity 306 // load a entity
272 txtprt_parser p; 307 txtprt_parser p;
273 entity *e; 308 geometry *g;
274 try 309 try
275 { 310 {
276 e = p.read ("test.blasc"); 311 g = p.read ("test.blasc");
277 } 312 }
278 catch (txtprt_i_exception & e) 313 catch (txtprt_i_exception & e)
279 { 314 {
280 cout << "ERR: " << e.msg << endl; 315 cout << "ERR: " << e.msg << endl;
281 } 316 }
282 317
283 entity_transform *f = new entity_transform; 318 entity *e = new entity (g);
284 f->set (e); 319 e->move (vec3 (i*5, -3, -i*10));
285 f->update (matrix::translation (vec3 (i*5, -3, -i*10)));
286 f->show (); 320 e->show ();
321 }
322
287 } 323 {
288 draw_some_random_funky_floor_dance_music (10, -500, -10, -1000); 324 fisch (vec3 (0, 0, -2e9), 1e9, 8);
325 //entity *planet = new entity (new geometry_sphere (1e9));
326 //planet->move (vec3 (0, 0, -1.5e9));
327 //planet->show ();
328 }
329
330 {
331 entity *planet = new entity (new geometry_sphere (4e15));
332 planet->move (vec3 (0, 0, 1e17));
333 planet->show ();
334 }
335
336 draw_floor (10, -500, -10, -1000);
337 draw_level ();
338 //draw_test_nurb ();
339#endif
340
341 //camera.orig.x = 108; camera.orig.y = 0; camera.orig.z = -368;
289 camera.orig.x = camera.orig.y = camera.orig.z = 0; 342 camera.orig.x = 0; camera.orig.y = 0; camera.orig.z = 0;
290 camera.p = point (0, 0, 10); 343 camera.p = point (0, 0, 10);
291 camera.d = vec3 (0, 0, -1); 344 camera.d = vec3 (0, 0, -1);
292 camera.u = vec3 (0, 1, 0); 345 camera.u = vec3 (0, 1, 0);
293 camera.w = w; camera.h = h; 346 camera.w = w; camera.h = h;
294 camera.fov = 80; 347 camera.fov = 40.;
295
296 glMatrixMode (GL_MODELVIEW);
297 glLoadIdentity ();
298 348
299 glEnable (GL_CULL_FACE); 349 glEnable (GL_CULL_FACE);
300 glEnable (GL_DEPTH_TEST); 350 glDisable (GL_ALPHA_TEST);
351 glDisable (GL_BLEND);
301 352
302 glShadeModel (GL_SMOOTH); 353 init_shaders ();
303 354
304 glEnable (GL_LIGHTING); 355 osama_material osa_mat;
305 glEnable (GL_LIGHT0);
306
307 cgc = cgCreateContext ();
308
309 vsh_profile = CG_PROFILE_ARBVP1;
310 //if (cgGLIsProfileSupported (CG_PROFILE_VP30)) vsh_profile = CG_PROFILE_VP30;
311 //if (cgGLIsProfileSupported (CG_PROFILE_VP40)) vsh_profile = CG_PROFILE_VP40;
312 fsh_profile = CG_PROFILE_ARBFP1;
313 //if (cgGLIsProfileSupported (CG_PROFILE_FP30)) fsh_profile = CG_PROFILE_FP30;
314 //if (cgGLIsProfileSupported (CG_PROFILE_FP40)) fsh_profile = CG_PROFILE_FP40;
315
316 vsh = cgCreateProgramFromFile (cgc, CG_SOURCE, "vsh.cg", vsh_profile, 0, 0);
317 CheckCgError ();
318 cgGLLoadProgram (vsh);
319 CheckCgError ();
320 mv = cgGetNamedParameter (vsh, "WorldProj");
321 mvp = cgGetNamedParameter (vsh, "WorldViewProj");
322 lightpos = cgGetNamedParameter (vsh, "LightPos");
323 CheckCgError ();
324
325 CGparameter g_Texture; // the texture parameter
326
327
328 fsh = cgCreateProgramFromFile (cgc, CG_SOURCE, "fsh.cg", fsh_profile, 0, 0);
329 Texture t("o.jpg");
330 g_Texture = cgGetNamedParameter(fsh, "Texture"); // the texture cg-warper ;)
331 cgGLSetTextureParameter(g_Texture, t.texture); // Bind the texture number 999 to g_Texture
332 CheckCgError ();
333 cgGLLoadProgram (fsh);
334 CheckCgError ();
335
336 cgGLBindProgram (vsh);
337 CheckCgError ();
338 cgGLBindProgram (fsh);
339 CheckCgError ();
340 356
341 /* Loop until done. */ 357 /* Loop until done. */
342 start_time = SDL_GetTicks ();
343 frames = 0; 358 frames = 0;
344 359
345 while (!done) 360 while (!done)
346 { 361 {
347 char *sdl_error; 362 char *sdl_error;
348 SDL_Event event; 363 SDL_Event event;
349 364
350 camera.p.x += camera_velocity_factor * camera_velocity.x * timer.diff;
351 camera.p.y += camera_velocity_factor * camera_velocity.y * timer.diff;
352 camera.p.z += camera_velocity_factor * camera_velocity.z * timer.diff;
353
354 camera_angle += camera_velocity_factor * camera_velocity_angle * timer.diff; 365 camera_angle += 180 * camera_velocity_angle * timer.diff;
355 camera.d.z = -cos (camera_angle * 12 / 180.); 366 camera_angle2 += 180 * camera_velocity_angle2 * timer.diff;
356 camera.d.x = sin (camera_angle * 12 / 180.);
357 367
358 cgGLSetParameter4f (lightpos, camera.p.x, camera.p.y, camera.p.z, 1); 368 vec3 geradeaus = matrix::rotation (-camera_angle, vec3 (0, 1, 0)) * vec3 (0, 0, -1);
369 vec3 right = matrix::rotation (90., vec3 (0, 1, 0)) * geradeaus;
359 370
360 glBindTexture (GL_TEXTURE_2D, t.texture); 371 camera.d = matrix::rotation (camera_angle2, right) * geradeaus;
361 cgGLEnableTextureParameter (g_Texture); // Enable the texture parameter 372 camera.u = cross (camera.d, right);
362 373
363#if 0 374 camera.p = camera.p - camera.d * (camera_velocity_factor * timer.diff) * camera_velocity.z;
364 static GLfloat ry; 375 camera.p = camera.p - camera.u * (camera_velocity_factor * timer.diff) * camera_velocity.y;
365 ry += 0.001; 376
366 camera.d.x = cos (ry); 377 osa_mat.begin ();
367 camera.d.z = sin (ry);
368 //camera.d.y = sin (ry * 0.1);
369#endif
370 378
371 camera.begin (); 379 camera.begin ();
372 camera.pass (view::DEPTH); 380 camera.render (view::DEPTH);
381 camera.render (view::POSTDEPTH);
373 camera.pass (view::LIGHTED); 382 camera.render (view::LIGHTED);
374 camera.end (); 383 camera.end ();
375 384
376 SDL_GL_SwapBuffers (); 385 SDL_GL_SwapBuffers ();
377 timer.frame (); 386 timer.frame ();
378 387
388 osa_mat.end ();
389
390#if 0
379 /* Check for error conditions. */ 391 /* Check for error conditions. */
380 gl_error = glGetError (); 392 gl_error = glGetError ();
381 393
382 if (gl_error != GL_NO_ERROR) fprintf (stderr, "testgl: OpenGL error: %d\n", gl_error); 394 if (gl_error != GL_NO_ERROR) fprintf (stderr, "testgl: OpenGL error: %d\n", gl_error);
383 395
388 fprintf (stderr, "testgl: SDL error '%s'\n", sdl_error); 400 fprintf (stderr, "testgl: SDL error '%s'\n", sdl_error);
389 SDL_ClearError (); 401 SDL_ClearError ();
390 } 402 }
391 403
392 /* Allow the user to see what's happening */ 404 /* Allow the user to see what's happening */
393 //SDL_Delay (20); 405 //SDL_Delay (40);
406#endif
394 407
395 /* Check if there's a pending event. */ 408 /* Check if there's a pending event. */
396 while (SDL_PollEvent (&event)) 409 while (SDL_PollEvent (&event))
397 done = HandleEvent (&event); 410 done = HandleEvent (&event);
398 411
399 412
400 ++frames; 413 ++frames;
401 } 414 }
402 415
403 /* Print out the frames per second */
404 this_time = SDL_GetTicks ();
405 if (this_time != start_time)
406 {
407 printf ("%2.2f FPS\n",
408 ((float) frames / (this_time - start_time)) * 1000.0);
409 }
410
411 if (global_image) 416 if (global_image)
412 { 417 {
413 SDL_FreeSurface (global_image); 418 SDL_FreeSurface (global_image);
414 global_image = NULL; 419 global_image = NULL;
415 } 420 }
421 426
422 /* Destroy our GL context, etc. */ 427 /* Destroy our GL context, etc. */
423 SDL_Quit (); 428 SDL_Quit ();
424 return (0); 429 return (0);
425} 430}
426
427int 431int
428main (int argc, char *argv[]) 432main (int argc, char *argv[])
429{ 433{
430 int i, logo; 434 int i, logo;
431 int numtests; 435 int numtests;
433 int slowly; 437 int slowly;
434 float gamma = 0.0; 438 float gamma = 0.0;
435 int noframe = 0; 439 int noframe = 0;
436 int fsaa = 0; 440 int fsaa = 0;
437 441
442 shader::debdebdebdebug ();//D
443
438 logo = 0; 444 logo = 0;
439 slowly = 0; 445 slowly = 0;
440 numtests = 1; 446 numtests = 1;
441 for (i = 1; argv[i]; ++i) 447 for (i = 1; argv[i]; ++i)
442 { 448 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines