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

Comparing libgender/test.C (file contents):
Revision 1.32 by root, Tue Oct 5 10:28:49 2004 UTC vs.
Revision 1.45 by root, Wed Oct 6 17:55:40 2004 UTC

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, lightpos; 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 = 20; 37float camera_velocity_factor = 20;
36 38
37void 39void
38HotKey_ToggleFullScreen (void) 40HotKey_ToggleFullScreen (void)
39{ 41{
107 if (event->key.keysym.sym == SDLK_RIGHT) camera_velocity.x++; 109 if (event->key.keysym.sym == SDLK_RIGHT) camera_velocity.x++;
108 if (event->key.keysym.sym == SDLK_a) camera_velocity.y--; 110 if (event->key.keysym.sym == SDLK_a) camera_velocity.y--;
109 if (event->key.keysym.sym == SDLK_s) camera_velocity.y++; 111 if (event->key.keysym.sym == SDLK_s) camera_velocity.y++;
110 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;
111 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--;
112 116
113 if (event->key.keysym.sym == SDLK_ESCAPE) 117 if (event->key.keysym.sym == SDLK_ESCAPE)
114 done = 1; 118 done = 1;
115 119
116 if ((event->key.keysym.sym == SDLK_g) && 120 if ((event->key.keysym.sym == SDLK_g) &&
132 if (event->key.keysym.sym == SDLK_DOWN) camera_velocity.z--; 136 if (event->key.keysym.sym == SDLK_DOWN) camera_velocity.z--;
133 if (event->key.keysym.sym == SDLK_LEFT) camera_velocity.x++; 137 if (event->key.keysym.sym == SDLK_LEFT) camera_velocity.x++;
134 if (event->key.keysym.sym == SDLK_RIGHT) camera_velocity.x--; 138 if (event->key.keysym.sym == SDLK_RIGHT) camera_velocity.x--;
135 if (event->key.keysym.sym == SDLK_a) camera_velocity.y++; 139 if (event->key.keysym.sym == SDLK_a) camera_velocity.y++;
136 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++;
137 break; 143 break;
138 144
139 case SDL_QUIT: 145 case SDL_QUIT:
140 done = 1; 146 done = 1;
141 break; 147 break;
142 } 148 }
143 149
144 return (done); 150 return (done);
145}
146
147/* Quick utility function for texture creation */
148static int
149power_of_two (int input)
150{
151 int value = 1;
152
153 while (value < input)
154 {
155 value <<= 1;
156 }
157 return value;
158}
159
160GLuint
161SDL_GL_LoadTexture (SDL_Surface * surface, GLfloat * texcoord)
162{
163 GLuint texture;
164 int w, h;
165 SDL_Surface *image;
166 SDL_Rect area;
167 Uint32 saved_flags;
168 Uint8 saved_alpha;
169
170 /* Use the surface width and height expanded to powers of 2 */
171 w = power_of_two (surface->w);
172 h = power_of_two (surface->h);
173 texcoord[0] = 0.0f; /* Min X */
174 texcoord[1] = 0.0f; /* Min Y */
175 texcoord[2] = (GLfloat) surface->w / w; /* Max X */
176 texcoord[3] = (GLfloat) surface->h / h; /* Max Y */
177
178 image = SDL_CreateRGBSurface (SDL_SWSURFACE, w, h, 32,
179#if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */
180 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000
181#else
182 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF
183#endif
184 );
185 if (image == NULL)
186 {
187 return 0;
188 }
189
190 /* Save the alpha blending attributes */
191 saved_flags = surface->flags & (SDL_SRCALPHA | SDL_RLEACCELOK);
192 saved_alpha = surface->format->alpha;
193 if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA)
194 {
195 SDL_SetAlpha (surface, 0, 0);
196 }
197
198 /* Copy the surface into the GL texture image */
199 area.x = 0;
200 area.y = 0;
201 area.w = surface->w;
202 area.h = surface->h;
203 SDL_BlitSurface (surface, &area, image, &area);
204
205 /* Restore the alpha blending attributes */
206 if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA)
207 {
208 SDL_SetAlpha (surface, saved_flags, saved_alpha);
209 }
210
211 /* Create an OpenGL texture for the image */
212 glGenTextures (1, &texture);
213 glBindTexture (GL_TEXTURE_2D, texture);
214 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
215 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
216 glTexImage2D (GL_TEXTURE_2D,
217 0,
218 GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels);
219 SDL_FreeSurface (image); /* No longer needed */
220
221 return texture;
222} 151}
223 152
224int 153int
225RunGLTest (int argc, char *argv[], 154RunGLTest (int argc, char *argv[],
226 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)
232 int done = 0; 161 int done = 0;
233 int frames; 162 int frames;
234 Uint32 start_time, this_time; 163 Uint32 start_time, this_time;
235 Uint32 video_flags; 164 Uint32 video_flags;
236 int value; 165 int value;
166 GLenum gl_error;
237 167
238 if (SDL_Init (SDL_INIT_VIDEO) < 0) 168 if (SDL_Init (SDL_INIT_VIDEO) < 0)
239 { 169 {
240 fprintf (stderr, "Couldn't initialize SDL: %s\n", SDL_GetError ()); 170 fprintf (stderr, "Couldn't initialize SDL: %s\n", SDL_GetError ());
241 exit (1); 171 exit (1);
334 264
335 /* Set the gamma for the window */ 265 /* Set the gamma for the window */
336 if (gamma != 0.0) 266 if (gamma != 0.0)
337 SDL_SetGamma (gamma, gamma, gamma); 267 SDL_SetGamma (gamma, gamma, gamma);
338 268
339 // load a entity
340 for (int i = 0; i < 7; i++) 269 for (int i = 0; i < 70; i++)
341 { 270 {
271 // load a entity
342 txtprt_parser p; 272 txtprt_parser p;
343 entity_transform *f = new entity_transform;
344 entity *e; 273 entity *e;
345 try 274 try
346 { 275 {
347 e = p.read ("test.blasc"); 276 e = p.read ("test.blasc");
277 }
348 } catch (txtprt_i_exception & e) 278 catch (txtprt_i_exception & e)
349 { 279 {
350 cout << "ERR: " << e.msg << endl; 280 cout << "ERR: " << e.msg << endl;
351 } 281 }
282
283 entity_transform *f = new entity_transform;
352 f->set (e); 284 f->set (e);
353 f->update (matrix::translation (vec3 (0, -1, -i*5))); 285 f->update (matrix::translation (vec3 (i*5, -3, -i*10)));
354 f->show (); 286 f->show ();
355 } 287 }
356 288 draw_some_random_funky_floor_dance_music (10, -500, -10, -1000);
357 camera.orig.x = camera.orig.y = camera.orig.z = 0; 289 camera.orig.x = camera.orig.y = camera.orig.z = 0;
358 camera.p = point (0, 0, 10); 290 camera.p = point (0, 0, 10);
359 camera.d = vec3 (0, 0, -1); 291 camera.d = vec3 (0, 0, -1);
360 camera.u = vec3 (0, 1, 0); 292 camera.u = vec3 (0, 1, 0);
361 camera.w = w; camera.h = h; 293 camera.w = w; camera.h = h;
362 camera.fov = 90; 294 camera.fov = 80;
363 295
364 glMatrixMode (GL_MODELVIEW); 296 glMatrixMode (GL_MODELVIEW);
365 glLoadIdentity (); 297 glLoadIdentity ();
366 298
367 glEnable (GL_CULL_FACE); 299 glEnable (GL_CULL_FACE);
368 glEnable (GL_DEPTH_TEST); 300 glEnable (GL_DEPTH_TEST);
369 301
370 glShadeModel (GL_SMOOTH); 302 glShadeModel (GL_SMOOTH);
371 303
372 glEnable (GL_LIGHTING); 304 glEnable (GL_LIGHTING);
373 //GLfloat lightc[4] = { 1, 0.1, 0.1, 1 };
374 //glLightf (GL_LIGHT0, GL_QUADRATIC_ATTENUATION);
375 //glLightfv (GL_LIGHT0, GL_DIFFUSE, lightc);
376 glEnable (GL_LIGHT0); 305 glEnable (GL_LIGHT0);
377 306
378 cgc = cgCreateContext (); 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
379 vsh = cgCreateProgramFromFile (cgc, CG_SOURCE, "vsh.cg", CG_PROFILE_ARBVP1, 0, 0); 316 vsh = cgCreateProgramFromFile (cgc, CG_SOURCE, "vsh.cg", vsh_profile, 0, 0);
380 CheckCgError (); 317 CheckCgError ();
381 cgGLLoadProgram (vsh); 318 cgGLLoadProgram (vsh);
382 CheckCgError (); 319 CheckCgError ();
383 mv = cgGetNamedParameter (vsh, "WorldProj"); 320 mv = cgGetNamedParameter (vsh, "WorldProj");
384 mvp = cgGetNamedParameter (vsh, "WorldViewProj"); 321 mvp = cgGetNamedParameter (vsh, "WorldViewProj");
385 lightpos = cgGetNamedParameter (vsh, "LightPos"); 322 lightpos = cgGetNamedParameter (vsh, "LightPos");
386 CheckCgError (); 323 CheckCgError ();
387 324
325 CGparameter g_Texture; // the texture parameter
326
327
388 fsh = cgCreateProgramFromFile (cgc, CG_SOURCE, "fsh.cg", CG_PROFILE_ARBFP1, 0, 0); 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
389 CheckCgError (); 332 CheckCgError ();
390 cgGLLoadProgram (fsh); 333 cgGLLoadProgram (fsh);
391 CheckCgError (); 334 CheckCgError ();
392 335
393 cgGLBindProgram (vsh); 336 cgGLBindProgram (vsh);
397 340
398 /* Loop until done. */ 341 /* Loop until done. */
399 start_time = SDL_GetTicks (); 342 start_time = SDL_GetTicks ();
400 frames = 0; 343 frames = 0;
401 344
402 draw_context c (camera);
403
404 while (!done) 345 while (!done)
405 { 346 {
406 GLenum gl_error;
407 char *sdl_error; 347 char *sdl_error;
408 SDL_Event event; 348 SDL_Event event;
409 349
410 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
411
412 camera.p.x += (camera_velocity_factor * camera_velocity.x) * timer.diff; 350 camera.p.x += camera_velocity_factor * camera_velocity.x * timer.diff;
413 camera.p.y += (camera_velocity_factor * camera_velocity.y) * timer.diff; 351 camera.p.y += camera_velocity_factor * camera_velocity.y * timer.diff;
414 camera.p.z += (camera_velocity_factor * camera_velocity.z) * 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;
355 camera.d.z = -cos (camera_angle * 12 / 180.);
356 camera.d.x = sin (camera_angle * 12 / 180.);
415 357
416 cgGLSetParameter4f (lightpos, camera.p.x, camera.p.y, camera.p.z, 1); 358 cgGLSetParameter4f (lightpos, camera.p.x, camera.p.y, camera.p.z, 1);
359
360 glBindTexture (GL_TEXTURE_2D, t.texture);
361 cgGLEnableTextureParameter (g_Texture); // Enable the texture parameter
417 362
418#if 0 363#if 0
419 static GLfloat ry; 364 static GLfloat ry;
420 ry += 0.001; 365 ry += 0.001;
421 camera.d.x = cos (ry); 366 camera.d.x = cos (ry);
422 camera.d.z = sin (ry); 367 camera.d.z = sin (ry);
423 //camera.d.y = sin (ry * 0.1); 368 //camera.d.y = sin (ry * 0.1);
424#endif 369#endif
425 370
426 c.mode = draw_context::DEPTH; 371 camera.begin ();
372 camera.pass (view::DEPTH);
373 camera.pass (view::LIGHTED);
427 camera.draw (c); 374 camera.end ();
428 c.mode = draw_context::LIGHTED;
429 camera.draw (c);
430 375
431 SDL_GL_SwapBuffers (); 376 SDL_GL_SwapBuffers ();
432 timer.frame (); 377 timer.frame ();
433 378
434 /* Check for error conditions. */ 379 /* Check for error conditions. */
435 gl_error = glGetError (); 380 gl_error = glGetError ();
436 381
437 if (gl_error != GL_NO_ERROR)
438 fprintf (stderr, "testgl: OpenGL error: %d\n", gl_error); 382 if (gl_error != GL_NO_ERROR) fprintf (stderr, "testgl: OpenGL error: %d\n", gl_error);
439 383
440 sdl_error = SDL_GetError (); 384 sdl_error = SDL_GetError ();
441 385
442 if (sdl_error[0] != '\0') 386 if (sdl_error[0] != '\0')
443 { 387 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines