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

Comparing libgender/test.C (file contents):
Revision 1.31 by root, Tue Oct 5 08:28:39 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 = 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)
233 int done = 0; 161 int done = 0;
234 int frames; 162 int frames;
235 Uint32 start_time, this_time; 163 Uint32 start_time, this_time;
236 Uint32 video_flags; 164 Uint32 video_flags;
237 int value; 165 int value;
166 GLenum gl_error;
238 167
239 if (SDL_Init (SDL_INIT_VIDEO) < 0) 168 if (SDL_Init (SDL_INIT_VIDEO) < 0)
240 { 169 {
241 fprintf (stderr, "Couldn't initialize SDL: %s\n", SDL_GetError ()); 170 fprintf (stderr, "Couldn't initialize SDL: %s\n", SDL_GetError ());
242 exit (1); 171 exit (1);
335 264
336 /* Set the gamma for the window */ 265 /* Set the gamma for the window */
337 if (gamma != 0.0) 266 if (gamma != 0.0)
338 SDL_SetGamma (gamma, gamma, gamma); 267 SDL_SetGamma (gamma, gamma, gamma);
339 268
340 // load a entity
341 for (int i = 0; i < 7; i++) 269 for (int i = 0; i < 70; i++)
342 { 270 {
271 // load a entity
343 txtprt_parser p; 272 txtprt_parser p;
344 entity_transform *f = new entity_transform;
345 entity *e; 273 entity *e;
346 try 274 try
347 { 275 {
348 e = p.read ("test.blasc"); 276 e = p.read ("test.blasc");
277 }
349 } catch (txtprt_i_exception & e) 278 catch (txtprt_i_exception & e)
350 { 279 {
351 cout << "ERR: " << e.msg << endl; 280 cout << "ERR: " << e.msg << endl;
352 } 281 }
282
283 entity_transform *f = new entity_transform;
353 f->set (e); 284 f->set (e);
354 f->update (matrix::translation (vec3 (0, -1, -i*5))); 285 f->update (matrix::translation (vec3 (i*5, -3, -i*10)));
355 f->show (); 286 f->show ();
356 } 287 }
357 288 draw_some_random_funky_floor_dance_music (10, -500, -10, -1000);
358 camera.orig.x = camera.orig.y = camera.orig.z = 0; 289 camera.orig.x = camera.orig.y = camera.orig.z = 0;
359 camera.p = point (0, 0, 10); 290 camera.p = point (0, 0, 10);
360 camera.d = vec3 (0, 0, -1); 291 camera.d = vec3 (0, 0, -1);
361 camera.u = vec3 (0, 1, 0); 292 camera.u = vec3 (0, 1, 0);
362 camera.w = w; camera.h = h; 293 camera.w = w; camera.h = h;
363 camera.fov = 90; 294 camera.fov = 80;
364 295
365 glMatrixMode (GL_MODELVIEW); 296 glMatrixMode (GL_MODELVIEW);
366 glLoadIdentity (); 297 glLoadIdentity ();
367 298
368 glEnable (GL_CULL_FACE); 299 glEnable (GL_CULL_FACE);
369 glEnable (GL_DEPTH_TEST); 300 glEnable (GL_DEPTH_TEST);
370 301
371 glShadeModel (GL_SMOOTH); 302 glShadeModel (GL_SMOOTH);
372 303
373 glEnable (GL_LIGHTING); 304 glEnable (GL_LIGHTING);
374 //GLfloat lightc[4] = { 1, 0.1, 0.1, 1 };
375 //glLightf (GL_LIGHT0, GL_QUADRATIC_ATTENUATION);
376 //glLightfv (GL_LIGHT0, GL_DIFFUSE, lightc);
377 glEnable (GL_LIGHT0); 305 glEnable (GL_LIGHT0);
378 306
379 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
380 vsh = cgCreateProgramFromFile (cgc, CG_SOURCE, "vsh.cg", CG_PROFILE_ARBVP1, 0, 0); 316 vsh = cgCreateProgramFromFile (cgc, CG_SOURCE, "vsh.cg", vsh_profile, 0, 0);
381 CheckCgError (); 317 CheckCgError ();
382 cgGLLoadProgram (vsh); 318 cgGLLoadProgram (vsh);
383 CheckCgError (); 319 CheckCgError ();
384 mv = cgGetNamedParameter (vsh, "WorldProj"); 320 mv = cgGetNamedParameter (vsh, "WorldProj");
385 mvp = cgGetNamedParameter (vsh, "WorldViewProj"); 321 mvp = cgGetNamedParameter (vsh, "WorldViewProj");
386 lightpos = cgGetNamedParameter (vsh, "LightPos"); 322 lightpos = cgGetNamedParameter (vsh, "LightPos");
387 CheckCgError (); 323 CheckCgError ();
388 324
325 CGparameter g_Texture; // the texture parameter
326
327
389 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
390 CheckCgError (); 332 CheckCgError ();
391 cgGLLoadProgram (fsh); 333 cgGLLoadProgram (fsh);
392 CheckCgError (); 334 CheckCgError ();
393 335
394 cgGLBindProgram (vsh); 336 cgGLBindProgram (vsh);
398 340
399 /* Loop until done. */ 341 /* Loop until done. */
400 start_time = SDL_GetTicks (); 342 start_time = SDL_GetTicks ();
401 frames = 0; 343 frames = 0;
402 344
403 draw_context c (camera);
404
405 while (!done) 345 while (!done)
406 { 346 {
407 GLenum gl_error;
408 char *sdl_error; 347 char *sdl_error;
409 SDL_Event event; 348 SDL_Event event;
410 349
411 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
412
413 camera.p.x += (camera_velocity_factor * camera_velocity.x) * timer.diff; 350 camera.p.x += camera_velocity_factor * camera_velocity.x * timer.diff;
414 camera.p.y += (camera_velocity_factor * camera_velocity.y) * timer.diff; 351 camera.p.y += camera_velocity_factor * camera_velocity.y * timer.diff;
415 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.);
416 357
417 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
418 362
419#if 0 363#if 0
420 static GLfloat ry; 364 static GLfloat ry;
421 ry += 0.001; 365 ry += 0.001;
422 camera.d.x = cos (ry); 366 camera.d.x = cos (ry);
423 camera.d.z = sin (ry); 367 camera.d.z = sin (ry);
424 //camera.d.y = sin (ry * 0.1); 368 //camera.d.y = sin (ry * 0.1);
425#endif 369#endif
426 370
427 c.mode = draw_context::DEPTH; 371 camera.begin ();
372 camera.pass (view::DEPTH);
373 camera.pass (view::LIGHTED);
428 camera.draw (c); 374 camera.end ();
429 c.mode = draw_context::LIGHTED;
430 camera.draw (c);
431 375
432 SDL_GL_SwapBuffers (); 376 SDL_GL_SwapBuffers ();
433 timer.frame (); 377 timer.frame ();
434 378
435 /* Check for error conditions. */ 379 /* Check for error conditions. */
436 gl_error = glGetError (); 380 gl_error = glGetError ();
437 381
438 if (gl_error != GL_NO_ERROR)
439 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);
440 383
441 sdl_error = SDL_GetError (); 384 sdl_error = SDL_GetError ();
442 385
443 if (sdl_error[0] != '\0') 386 if (sdl_error[0] != '\0')
444 { 387 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines