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.41 by root, Wed Oct 6 15:57:26 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
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}
147
148/* Quick utility function for texture creation */
149static int
150power_of_two (int input)
151{
152 int value = 1;
153
154 while (value < input)
155 {
156 value <<= 1;
157 }
158 return value;
159}
160
161GLuint
162SDL_GL_LoadTexture (SDL_Surface * surface, GLfloat * texcoord)
163{
164 GLuint texture;
165 int w, h;
166 SDL_Surface *image;
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 {
188 return 0;
189 }
190
191 /* Save the alpha blending attributes */
192 saved_flags = surface->flags & (SDL_SRCALPHA | SDL_RLEACCELOK);
193 saved_alpha = surface->format->alpha;
194 if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA)
195 {
196 SDL_SetAlpha (surface, 0, 0);
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} 151}
224 152
225int 153int
226RunGLTest (int argc, char *argv[], 154RunGLTest (int argc, char *argv[],
227 int logo, int slowly, int bpp, float gamma, int noframe, int fsaa) 155 int logo, int slowly, int bpp, float gamma, int noframe, int fsaa)
335 263
336 /* Set the gamma for the window */ 264 /* Set the gamma for the window */
337 if (gamma != 0.0) 265 if (gamma != 0.0)
338 SDL_SetGamma (gamma, gamma, gamma); 266 SDL_SetGamma (gamma, gamma, gamma);
339 267
340 // load a entity
341 for (int i = 0; i < 7; i++) 268 for (int i = 0; i < 70; i++)
342 { 269 {
270 // load a entity
343 txtprt_parser p; 271 txtprt_parser p;
344 entity_transform *f = new entity_transform;
345 entity *e; 272 entity *e;
346 try 273 try
347 { 274 {
348 e = p.read ("test.blasc"); 275 e = p.read ("test.blasc");
276 }
349 } catch (txtprt_i_exception & e) 277 catch (txtprt_i_exception & e)
350 { 278 {
351 cout << "ERR: " << e.msg << endl; 279 cout << "ERR: " << e.msg << endl;
352 } 280 }
281
282 entity_transform *f = new entity_transform;
353 f->set (e); 283 f->set (e);
354 f->update (matrix::translation (vec3 (0, -1, -i*5))); 284 f->update (matrix::translation (vec3 (i*5, -1, -i*10)));
355 f->show (); 285 f->show ();
356 } 286 }
357 287
358 camera.orig.x = camera.orig.y = camera.orig.z = 0; 288 camera.orig.x = camera.orig.y = camera.orig.z = 0;
359 camera.p = point (0, 0, 10); 289 camera.p = point (0, 0, 10);
360 camera.d = vec3 (0, 0, -1); 290 camera.d = vec3 (0, 0, -1);
361 camera.u = vec3 (0, 1, 0); 291 camera.u = vec3 (0, 1, 0);
362 camera.w = w; camera.h = h; 292 camera.w = w; camera.h = h;
363 camera.fov = 90; 293 camera.fov = 80;
364 294
365 glMatrixMode (GL_MODELVIEW); 295 glMatrixMode (GL_MODELVIEW);
366 glLoadIdentity (); 296 glLoadIdentity ();
367 297
368 glEnable (GL_CULL_FACE); 298 glEnable (GL_CULL_FACE);
369 glEnable (GL_DEPTH_TEST); 299 glEnable (GL_DEPTH_TEST);
370 //glEnable(GL_VERTEX_PROGRAM_ARB);
371 //glEnable(GL_FRAGMENT_PROGRAM_ARB);
372 300
373 glShadeModel (GL_SMOOTH); 301 glShadeModel (GL_SMOOTH);
374 302
375 glEnable (GL_LIGHTING); 303 glEnable (GL_LIGHTING);
376 //GLfloat lightc[4] = { 1, 0.1, 0.1, 1 }; 304 //GLfloat lightc[4] = { 1, 0.1, 0.1, 1 };
377 //glLightf (GL_LIGHT0, GL_QUADRATIC_ATTENUATION); 305 //glLightf (GL_LIGHT0, GL_QUADRATIC_ATTENUATION);
378 //glLightfv (GL_LIGHT0, GL_DIFFUSE, lightc); 306 //glLightfv (GL_LIGHT0, GL_DIFFUSE, lightc);
379 glEnable (GL_LIGHT0); 307 glEnable (GL_LIGHT0);
380 308
381 cgc = cgCreateContext (); 309 cgc = cgCreateContext ();
310
311 vsh_profile = CG_PROFILE_ARBVP1;
312 //if (cgGLIsProfileSupported (CG_PROFILE_VP30)) vsh_profile = CG_PROFILE_VP30;
313 //if (cgGLIsProfileSupported (CG_PROFILE_VP40)) vsh_profile = CG_PROFILE_VP40;
314 fsh_profile = CG_PROFILE_ARBFP1;
315 //if (cgGLIsProfileSupported (CG_PROFILE_FP30)) fsh_profile = CG_PROFILE_FP30;
316 //if (cgGLIsProfileSupported (CG_PROFILE_FP40)) fsh_profile = CG_PROFILE_FP40;
317
382 vsh = cgCreateProgramFromFile (cgc, CG_SOURCE, "vsh.cg", CG_PROFILE_ARBVP1, 0, 0); 318 vsh = cgCreateProgramFromFile (cgc, CG_SOURCE, "vsh.cg", vsh_profile, 0, 0);
383 CheckCgError (); 319 CheckCgError ();
384 cgGLLoadProgram (vsh); 320 cgGLLoadProgram (vsh);
385 CheckCgError (); 321 CheckCgError ();
386 mv = cgGetNamedParameter (vsh, "WorldProj"); 322 mv = cgGetNamedParameter (vsh, "WorldProj");
387 CheckCgError ();
388 mvp = cgGetNamedParameter (vsh, "WorldViewProj"); 323 mvp = cgGetNamedParameter (vsh, "WorldViewProj");
324 lightpos = cgGetNamedParameter (vsh, "LightPos");
389 CheckCgError (); 325 CheckCgError ();
390 cgGLEnableProfile (CG_PROFILE_ARBVP1);
391 CheckCgError ();
392 326
327 CGparameter g_Texture; // the texture parameter
328
329
393 fsh = cgCreateProgramFromFile (cgc, CG_SOURCE, "fsh.cg", CG_PROFILE_ARBFP1, 0, 0); 330 fsh = cgCreateProgramFromFile (cgc, CG_SOURCE, "fsh.cg", fsh_profile, 0, 0);
331 Texture t("o.jpg");
332 g_Texture = cgGetNamedParameter(fsh, "Texture"); // the texture cg-warper ;)
333 cgGLSetTextureParameter(g_Texture, t.texture); // Bind the texture number 999 to g_Texture
334 CheckCgError ();
394 cgGLLoadProgram (fsh); 335 cgGLLoadProgram (fsh);
395 cgGLEnableProfile (CG_PROFILE_ARBFP1); 336 CheckCgError ();
396 337
397 cgGLBindProgram (vsh); 338 cgGLBindProgram (vsh);
339 CheckCgError ();
398 cgGLBindProgram (fsh); 340 cgGLBindProgram (fsh);
341 CheckCgError ();
399 342
400 /* Loop until done. */ 343 /* Loop until done. */
401 start_time = SDL_GetTicks (); 344 start_time = SDL_GetTicks ();
402 frames = 0; 345 frames = 0;
403 346
404 draw_context c (camera);
405 347
406 while (!done) 348 while (!done)
407 { 349 {
408 GLenum gl_error; 350 GLenum gl_error;
409 char *sdl_error; 351 char *sdl_error;
410 SDL_Event event; 352 SDL_Event event;
411 353
412 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 354 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
413 355
414 camera.p.x += (camera_velocity_factor * camera_velocity.x) * timer.diff; 356 camera.p.x += camera_velocity_factor * camera_velocity.x * timer.diff;
415 camera.p.y += (camera_velocity_factor * camera_velocity.y) * timer.diff; 357 camera.p.y += camera_velocity_factor * camera_velocity.y * timer.diff;
416 camera.p.z += (camera_velocity_factor * camera_velocity.z) * timer.diff; 358 camera.p.z += camera_velocity_factor * camera_velocity.z * timer.diff;
417 359
418 GLfloat lightp[4]; 360 camera_angle += camera_velocity_factor * camera_velocity_angle * timer.diff;
419 lightp[0] = camera.p.x; 361 camera.d.z = -cos (camera_angle * 12 / 180.);
420 lightp[1] = camera.p.y; 362 camera.d.x = sin (camera_angle * 12 / 180.);
421 lightp[2] = camera.p.z; 363
422 lightp[3] = 1; 364 cgGLSetParameter4f (lightpos, camera.p.x, camera.p.y, camera.p.z, 1);
423 glLightfv (GL_LIGHT0, GL_POSITION, lightp); 365
366 glBindTexture (GL_TEXTURE_2D, t.texture);
367 cgGLEnableTextureParameter (g_Texture); // Enable the texture parameter
424 368
425#if 0 369#if 0
426 static GLfloat ry; 370 static GLfloat ry;
427 ry += 0.001; 371 ry += 0.001;
428 camera.d.x = cos (ry); 372 camera.d.x = cos (ry);
429 camera.d.z = sin (ry); 373 camera.d.z = sin (ry);
430 //camera.d.y = sin (ry * 0.1); 374 //camera.d.y = sin (ry * 0.1);
431#endif 375#endif
432 376
433 c.mode = draw_context::DEPTH; 377 camera.begin ();
378 camera.pass (view::DEPTH);
379 camera.pass (view::LIGHTED);
434 camera.draw (c); 380 camera.end ();
435 c.mode = draw_context::LIGHTED;
436 camera.draw (c);
437 381
438 SDL_GL_SwapBuffers (); 382 SDL_GL_SwapBuffers ();
439 timer.frame (); 383 timer.frame ();
440 384
441 /* Check for error conditions. */ 385 /* Check for error conditions. */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines