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

Comparing libgender/test.C (file contents):
Revision 1.29 by root, Tue Oct 5 07:09:17 2004 UTC vs.
Revision 1.49 by root, Fri Oct 8 16:52:49 2004 UTC

13#include "entity.h" 13#include "entity.h"
14#include "txtprt_import.h" 14#include "txtprt_import.h"
15 15
16CGcontext cgc; 16CGcontext cgc;
17CGprogram vsh, fsh; 17CGprogram vsh, fsh;
18CGparameter mv, mvp; 18CGparameter mv, mvp, lightpos;
19CGprofile vsh_profile, fsh_profile;
19 20
20static void CheckCgError(void) 21static void CheckCgError (void)
21{ 22{
22 CGerror err = cgGetError(); 23 CGerror err = cgGetError ();
23 24
24 if (err != CG_NO_ERROR) 25 if (err != CG_NO_ERROR)
25 { 26 {
26 printf("CG error: %s\n", cgGetErrorString(err)); 27 printf("CG error: %s\n", cgGetErrorString (err));
27 exit(1); 28 exit(1);
28 } 29 }
29} 30}
30 31
31/**********************************************************************/ 32/**********************************************************************/
32 33
33view camera; 34view camera;
34vec3 camera_velocity; 35vec3 camera_velocity;
36float camera_angle = 0, camera_velocity_angle;
35float camera_velocity_factor = 10; 37float camera_velocity_factor = 20;
36 38
37void 39void
38HotKey_ToggleFullScreen (void) 40HotKey_ToggleFullScreen (void)
39{ 41{
40 SDL_Surface *screen; 42 SDL_Surface *screen;
98 } 100 }
99 101
100 printf ("focus\n"); 102 printf ("focus\n");
101 break; 103 break;
102 104
103#define VELOCITY 10
104 case SDL_KEYDOWN: 105 case SDL_KEYDOWN:
105 if (event->key.keysym.sym == SDLK_UP) camera_velocity.z--; 106 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_DOWN) camera_velocity.z++;
107 if (event->key.keysym.sym == SDLK_LEFT) camera_velocity.x--; 108 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_RIGHT) camera_velocity.x++;
109 if (event->key.keysym.sym == SDLK_a) camera_velocity.y--; 110 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_s) camera_velocity.y++;
111 if (event->key.keysym.sym == SDLK_v) camera_velocity_factor *= 1.5; 112 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 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--;
113 116
114 if (event->key.keysym.sym == SDLK_ESCAPE) 117 if (event->key.keysym.sym == SDLK_ESCAPE)
115 done = 1; 118 done = 1;
116 119
117 if ((event->key.keysym.sym == SDLK_g) && 120 if ((event->key.keysym.sym == SDLK_g) &&
133 if (event->key.keysym.sym == SDLK_DOWN) camera_velocity.z--; 136 if (event->key.keysym.sym == SDLK_DOWN) camera_velocity.z--;
134 if (event->key.keysym.sym == SDLK_LEFT) camera_velocity.x++; 137 if (event->key.keysym.sym == SDLK_LEFT) camera_velocity.x++;
135 if (event->key.keysym.sym == SDLK_RIGHT) camera_velocity.x--; 138 if (event->key.keysym.sym == SDLK_RIGHT) camera_velocity.x--;
136 if (event->key.keysym.sym == SDLK_a) camera_velocity.y++; 139 if (event->key.keysym.sym == SDLK_a) camera_velocity.y++;
137 if (event->key.keysym.sym == SDLK_s) camera_velocity.y--; 140 if (event->key.keysym.sym == SDLK_s) 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++;
138 break; 143 break;
139 144
140 case SDL_QUIT: 145 case SDL_QUIT:
141 done = 1; 146 done = 1;
142 break; 147 break;
143 } 148 }
144 149
145 return (done); 150 return (done);
146} 151}
147 152
148/* Quick utility function for texture creation */ 153void draw_floor (int size, int dx, int dy, int dz)
149static int
150power_of_two (int input)
151{ 154{
152 int value = 1; 155 int x, z, ry;
153 156
154 while (value < input) 157 for (x = 0; x < 100; x++)
155 {
156 value <<= 1;
157 } 158 {
158 return value; 159 for (z = 0; z < 100; z++)
159} 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)));
160 166
161GLuint 167 geometry_quads *q = new geometry_quads;
162SDL_GL_LoadTexture (SDL_Surface * surface, GLfloat * texcoord) 168 q->set (pts);
163{ 169 entity *e = new entity (q);
164 GLuint texture; 170 e->move (vec3 (dx + x * size, dy, dz + z * size));
165 int w, h; 171 e->show ();
166 SDL_Surface *image; 172 }
167 SDL_Rect area;
168 Uint32 saved_flags;
169 Uint8 saved_alpha;
170
171 /* Use the surface width and height expanded to powers of 2 */
172 w = power_of_two (surface->w);
173 h = power_of_two (surface->h);
174 texcoord[0] = 0.0f; /* Min X */
175 texcoord[1] = 0.0f; /* Min Y */
176 texcoord[2] = (GLfloat) surface->w / w; /* Max X */
177 texcoord[3] = (GLfloat) surface->h / h; /* Max Y */
178
179 image = SDL_CreateRGBSurface (SDL_SWSURFACE, w, h, 32,
180#if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */
181 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000
182#else
183 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF
184#endif
185 );
186 if (image == NULL)
187 { 173 }
188 return 0; 174}
189 }
190 175
191 /* Save the alpha blending attributes */ 176void draw_test_nurb () {
192 saved_flags = surface->flags & (SDL_SRCALPHA | SDL_RLEACCELOK); 177 geometry_nurbs *q = new geometry_nurbs;
193 saved_alpha = surface->format->alpha; 178 q->set ();
194 if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA) 179 entity *e = new entity (q);
195 { 180 e->move (vec3 (10, 3, -4));
196 SDL_SetAlpha (surface, 0, 0); 181 e->show ();
197 }
198
199 /* Copy the surface into the GL texture image */
200 area.x = 0;
201 area.y = 0;
202 area.w = surface->w;
203 area.h = surface->h;
204 SDL_BlitSurface (surface, &area, image, &area);
205
206 /* Restore the alpha blending attributes */
207 if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA)
208 {
209 SDL_SetAlpha (surface, saved_flags, saved_alpha);
210 }
211
212 /* Create an OpenGL texture for the image */
213 glGenTextures (1, &texture);
214 glBindTexture (GL_TEXTURE_2D, texture);
215 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
216 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
217 glTexImage2D (GL_TEXTURE_2D,
218 0,
219 GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels);
220 SDL_FreeSurface (image); /* No longer needed */
221
222 return texture;
223} 182}
224 183
225int 184int
226RunGLTest (int argc, char *argv[], 185RunGLTest (int argc, char *argv[],
227 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)
233 int done = 0; 192 int done = 0;
234 int frames; 193 int frames;
235 Uint32 start_time, this_time; 194 Uint32 start_time, this_time;
236 Uint32 video_flags; 195 Uint32 video_flags;
237 int value; 196 int value;
197 GLenum gl_error;
238 198
239 if (SDL_Init (SDL_INIT_VIDEO) < 0) 199 if (SDL_Init (SDL_INIT_VIDEO) < 0)
240 { 200 {
241 fprintf (stderr, "Couldn't initialize SDL: %s\n", SDL_GetError ()); 201 fprintf (stderr, "Couldn't initialize SDL: %s\n", SDL_GetError ());
242 exit (1); 202 exit (1);
335 295
336 /* Set the gamma for the window */ 296 /* Set the gamma for the window */
337 if (gamma != 0.0) 297 if (gamma != 0.0)
338 SDL_SetGamma (gamma, gamma, gamma); 298 SDL_SetGamma (gamma, gamma, gamma);
339 299
340 // load a entity
341 for (int i = 0; i < 7; i++) 300 for (int i = 0; i < 1; i++)
342 { 301 {
302 // load a entity
343 txtprt_parser p; 303 txtprt_parser p;
344 entity_transform *f = new entity_transform; 304 geometry *g;
345 entity *e;
346 try 305 try
347 { 306 {
348 e = p.read ("test.blasc"); 307 g = p.read ("test.blasc");
308 }
349 } catch (txtprt_i_exception & e) 309 catch (txtprt_i_exception & e)
350 { 310 {
351 cout << "ERR: " << e.msg << endl; 311 cout << "ERR: " << e.msg << endl;
352 } 312 }
353 f->set (e); 313
354 f->update (matrix::translation (vec3 (0, -1, -i*5))); 314 entity *e = new entity (g);
315 e->move (vec3 (i*5, -3, -i*10));
355 f->show (); 316 e->show ();
356 } 317 }
318
319 draw_floor (10, -500, -10, -1000);
320 draw_test_nurb ();
357 321
358 camera.orig.x = camera.orig.y = camera.orig.z = 0; 322 camera.orig.x = camera.orig.y = camera.orig.z = 0;
359 camera.p = point (0, 0, 10); 323 camera.p = point (0, 0, 10);
360 camera.d = vec3 (0, 0, -1); 324 camera.d = vec3 (0, 0, -1);
361 camera.u = vec3 (0, 1, 0); 325 camera.u = vec3 (0, 1, 0);
362 camera.w = w; camera.h = h; 326 camera.w = w; camera.h = h;
363 camera.fov = 90; 327 camera.fov = 35;
364 328
365 glMatrixMode (GL_MODELVIEW); 329 glMatrixMode (GL_MODELVIEW);
366 glLoadIdentity (); 330 glLoadIdentity ();
367 331
368 glEnable (GL_CULL_FACE); 332 glEnable (GL_CULL_FACE);
369 glEnable (GL_DEPTH_TEST); 333 glEnable (GL_DEPTH_TEST);
370 //glEnable(GL_VERTEX_PROGRAM_ARB);
371 //glEnable(GL_FRAGMENT_PROGRAM_ARB);
372 334
373 glShadeModel (GL_SMOOTH); 335 glShadeModel (GL_SMOOTH);
374 336
375 glEnable (GL_LIGHTING); 337 glEnable (GL_LIGHTING);
376 //GLfloat lightc[4] = { 1, 0.1, 0.1, 1 };
377 //glLightf (GL_LIGHT0, GL_QUADRATIC_ATTENUATION);
378 //glLightfv (GL_LIGHT0, GL_DIFFUSE, lightc);
379 glEnable (GL_LIGHT0); 338 glEnable (GL_LIGHT0);
380 339
381 cgc = cgCreateContext (); 340 cgc = cgCreateContext ();
341
342 vsh_profile = CG_PROFILE_ARBVP1;
343 //if (cgGLIsProfileSupported (CG_PROFILE_VP30)) vsh_profile = CG_PROFILE_VP30;
344 //if (cgGLIsProfileSupported (CG_PROFILE_VP40)) vsh_profile = CG_PROFILE_VP40;
345 fsh_profile = CG_PROFILE_ARBFP1;
346 //if (cgGLIsProfileSupported (CG_PROFILE_FP30)) fsh_profile = CG_PROFILE_FP30;
347 //if (cgGLIsProfileSupported (CG_PROFILE_FP40)) fsh_profile = CG_PROFILE_FP40;
348
382 vsh = cgCreateProgramFromFile (cgc, CG_SOURCE, "vsh.cg", CG_PROFILE_ARBVP1, 0, 0); 349 vsh = cgCreateProgramFromFile (cgc, CG_SOURCE, "vsh.cg", vsh_profile, 0, 0);
383 CheckCgError (); 350 CheckCgError ();
384 cgGLLoadProgram (vsh); 351 cgGLLoadProgram (vsh);
385 CheckCgError (); 352 CheckCgError ();
386 mv = cgGetNamedParameter (vsh, "WorldProj"); 353 mv = cgGetNamedParameter (vsh, "WorldProj");
387 CheckCgError ();
388 mvp = cgGetNamedParameter (vsh, "WorldViewProj"); 354 mvp = cgGetNamedParameter (vsh, "WorldViewProj");
355 lightpos = cgGetNamedParameter (vsh, "LightPos");
389 CheckCgError (); 356 CheckCgError ();
390 cgGLEnableProfile (CG_PROFILE_ARBVP1);
391 CheckCgError ();
392 357
358 CGparameter g_Texture; // the texture parameter
359
360
393 fsh = cgCreateProgramFromFile (cgc, CG_SOURCE, "fsh.cg", CG_PROFILE_ARBFP1, 0, 0); 361 fsh = cgCreateProgramFromFile (cgc, CG_SOURCE, "fsh.cg", fsh_profile, 0, 0);
362 Texture t("o.jpg");
363 g_Texture = cgGetNamedParameter(fsh, "Texture"); // the texture cg-warper ;)
364 cgGLSetTextureParameter(g_Texture, t.texture); // Bind the texture number 999 to g_Texture
365 CheckCgError ();
394 cgGLLoadProgram (fsh); 366 cgGLLoadProgram (fsh);
395 cgGLEnableProfile (CG_PROFILE_ARBFP1); 367 CheckCgError ();
396 368
397 cgGLBindProgram (vsh); 369 cgGLBindProgram (vsh);
370 CheckCgError ();
398 cgGLBindProgram (fsh); 371 cgGLBindProgram (fsh);
372 CheckCgError ();
399 373
400 /* Loop until done. */ 374 /* Loop until done. */
401 start_time = SDL_GetTicks (); 375 start_time = SDL_GetTicks ();
402 frames = 0; 376 frames = 0;
403 377
404 draw_context c (camera);
405
406 while (!done) 378 while (!done)
407 { 379 {
408 GLenum gl_error;
409 char *sdl_error; 380 char *sdl_error;
410 SDL_Event event; 381 SDL_Event event;
411 382
412 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
413
414 camera.p.x += (camera_velocity_factor * camera_velocity.x) * timer.diff; 383 camera.p.x += camera_velocity_factor * camera_velocity.x * timer.diff;
415 camera.p.y += (camera_velocity_factor * camera_velocity.y) * timer.diff; 384 camera.p.y += camera_velocity_factor * camera_velocity.y * timer.diff;
416 camera.p.z += (camera_velocity_factor * camera_velocity.z) * timer.diff; 385 camera.p.z += camera_velocity_factor * camera_velocity.z * timer.diff;
417 386
418 GLfloat lightp[4]; 387 camera_angle += camera_velocity_factor * camera_velocity_angle * timer.diff;
419 lightp[0] = camera.p.x; 388 camera.d.z = -cos (camera_angle * 12 / 180.);
420 lightp[1] = camera.p.y; 389 camera.d.x = sin (camera_angle * 12 / 180.);
421 lightp[2] = camera.p.z; 390
422 lightp[3] = 1; 391 cgGLSetParameter4f (lightpos, camera.p.x, camera.p.y, camera.p.z, 1);
423 glLightfv (GL_LIGHT0, GL_POSITION, lightp); 392
393 glBindTexture (GL_TEXTURE_2D, t.texture);
394 cgGLEnableTextureParameter (g_Texture); // Enable the texture parameter
424 395
425#if 0 396#if 0
426 static GLfloat ry; 397 static GLfloat ry;
427 ry += 0.001; 398 ry += 0.001;
428 camera.d.x = cos (ry); 399 camera.d.x = cos (ry);
429 camera.d.z = sin (ry); 400 camera.d.z = sin (ry);
430 //camera.d.y = sin (ry * 0.1); 401 //camera.d.y = sin (ry * 0.1);
431#endif 402#endif
432 403
433 c.mode = draw_context::DEPTH; 404 camera.begin ();
405 camera.pass (view::DEPTH);
406 camera.pass (view::LIGHTED);
434 camera.draw (c); 407 camera.end ();
435 c.mode = draw_context::LIGHTED;
436 camera.draw (c);
437 408
438 SDL_GL_SwapBuffers (); 409 SDL_GL_SwapBuffers ();
439 timer.frame (); 410 timer.frame ();
440 411
441 /* Check for error conditions. */ 412 /* Check for error conditions. */
442 gl_error = glGetError (); 413 gl_error = glGetError ();
443 414
444 if (gl_error != GL_NO_ERROR)
445 fprintf (stderr, "testgl: OpenGL error: %d\n", gl_error); 415 if (gl_error != GL_NO_ERROR) fprintf (stderr, "testgl: OpenGL error: %d\n", gl_error);
446 416
447 sdl_error = SDL_GetError (); 417 sdl_error = SDL_GetError ();
448 418
449 if (sdl_error[0] != '\0') 419 if (sdl_error[0] != '\0')
450 { 420 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines