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

Comparing libgender/test.C (file contents):
Revision 1.26 by root, Tue Oct 5 03:39:47 2004 UTC vs.
Revision 1.45 by root, Wed Oct 6 17:55:40 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#include <sh/sh.hpp>
6
7using namespace SH;
8 5
9#include "SDL.h" 6#include "SDL.h"
10
11#include "SDL_opengl.h" 7#include "SDL_opengl.h"
12
13/* Undefine this if you want a flat cube instead of a rainbow cube */
14#define SHADED_CUBE
15
16/* Define this to be the name of the logo image to use with -logo */
17#define LOGO_FILE "icon.bmp"
18 8
19static SDL_Surface *global_image = NULL; 9static SDL_Surface *global_image = NULL;
20static GLuint global_texture = 0; 10static GLuint global_texture = 0;
21 11
12#include "util.h"
22#include "entity.h" 13#include "entity.h"
23
24#include "txtprt_import.h" 14#include "txtprt_import.h"
15
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}
25 31
26/**********************************************************************/ 32/**********************************************************************/
27 33
28view camera; 34view camera;
29vec3 camera_velocity; 35vec3 camera_velocity;
36float camera_angle = 0, camera_velocity_angle;
30float camera_velocity_factor = 10; 37float camera_velocity_factor = 20;
31
32ShColor3f color = ShColor3f(.5, 0.9, 0.2);
33ShPoint3f lightPos = ShPoint3f(0.0, 0.0, 10.0);
34ShMatrix4x4f mvp;
35ShProgram vsh, fsh;
36
37void
38init_shaders ()
39{
40 {
41 vsh = SH_BEGIN_PROGRAM ("gpu:vertex")
42 ShInputNormal3f normal;
43 ShInputPosition4f p;
44
45 ShOutputPoint4f ov;
46 ShOutputNormal3f on;
47 ShOutputVector3f lvv;
48 ShOutputPosition4f opd;
49
50 opd = mvp | p;
51 on = normalize (mvp | normal);
52 ov = -normalize (mvp | p);
53 lvv = normalize (lightPos - (mvp | p) (0, 1, 2));
54 SH_END;
55 }
56
57 // declare and initialize diffuse color
58 ShColor3f kd = ShColor3f (0.5, 0.7, 0.9);
59
60 {
61 fsh = SH_BEGIN_PROGRAM ("gpu:fragment")
62 ShInputVector4f v;
63 ShInputNormal3f n;
64 ShInputVector3f lvv;
65 ShInputPosition4f p;
66
67 ShOutputColor3f out;
68 out (0, 1, 2) = color * dot (normalize (n), normalize (lvv));
69 SH_END;
70 }
71}
72 38
73void 39void
74HotKey_ToggleFullScreen (void) 40HotKey_ToggleFullScreen (void)
75{ 41{
76 SDL_Surface *screen; 42 SDL_Surface *screen;
134 } 100 }
135 101
136 printf ("focus\n"); 102 printf ("focus\n");
137 break; 103 break;
138 104
139#define VELOCITY 10
140 case SDL_KEYDOWN: 105 case SDL_KEYDOWN:
141 if (event->key.keysym.sym == SDLK_UP) camera_velocity.z--; 106 if (event->key.keysym.sym == SDLK_UP) camera_velocity.z--;
142 if (event->key.keysym.sym == SDLK_DOWN) camera_velocity.z++; 107 if (event->key.keysym.sym == SDLK_DOWN) camera_velocity.z++;
143 if (event->key.keysym.sym == SDLK_LEFT) camera_velocity.x--; 108 if (event->key.keysym.sym == SDLK_LEFT) camera_velocity.x--;
144 if (event->key.keysym.sym == SDLK_RIGHT) camera_velocity.x++; 109 if (event->key.keysym.sym == SDLK_RIGHT) camera_velocity.x++;
145 if (event->key.keysym.sym == SDLK_a) camera_velocity.y--; 110 if (event->key.keysym.sym == SDLK_a) camera_velocity.y--;
146 if (event->key.keysym.sym == SDLK_s) camera_velocity.y++; 111 if (event->key.keysym.sym == SDLK_s) camera_velocity.y++;
147 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;
148 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--;
149 116
150 if (event->key.keysym.sym == SDLK_ESCAPE) 117 if (event->key.keysym.sym == SDLK_ESCAPE)
151 done = 1; 118 done = 1;
152 119
153 if ((event->key.keysym.sym == SDLK_g) && 120 if ((event->key.keysym.sym == SDLK_g) &&
169 if (event->key.keysym.sym == SDLK_DOWN) camera_velocity.z--; 136 if (event->key.keysym.sym == SDLK_DOWN) camera_velocity.z--;
170 if (event->key.keysym.sym == SDLK_LEFT) camera_velocity.x++; 137 if (event->key.keysym.sym == SDLK_LEFT) camera_velocity.x++;
171 if (event->key.keysym.sym == SDLK_RIGHT) camera_velocity.x--; 138 if (event->key.keysym.sym == SDLK_RIGHT) camera_velocity.x--;
172 if (event->key.keysym.sym == SDLK_a) camera_velocity.y++; 139 if (event->key.keysym.sym == SDLK_a) camera_velocity.y++;
173 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++;
174 break; 143 break;
175 144
176 case SDL_QUIT: 145 case SDL_QUIT:
177 done = 1; 146 done = 1;
178 break; 147 break;
179 } 148 }
180 149
181 return (done); 150 return (done);
182}
183
184/* Quick utility function for texture creation */
185static int
186power_of_two (int input)
187{
188 int value = 1;
189
190 while (value < input)
191 {
192 value <<= 1;
193 }
194 return value;
195}
196
197GLuint
198SDL_GL_LoadTexture (SDL_Surface * surface, GLfloat * texcoord)
199{
200 GLuint texture;
201 int w, h;
202 SDL_Surface *image;
203 SDL_Rect area;
204 Uint32 saved_flags;
205 Uint8 saved_alpha;
206
207 /* Use the surface width and height expanded to powers of 2 */
208 w = power_of_two (surface->w);
209 h = power_of_two (surface->h);
210 texcoord[0] = 0.0f; /* Min X */
211 texcoord[1] = 0.0f; /* Min Y */
212 texcoord[2] = (GLfloat) surface->w / w; /* Max X */
213 texcoord[3] = (GLfloat) surface->h / h; /* Max Y */
214
215 image = SDL_CreateRGBSurface (SDL_SWSURFACE, w, h, 32,
216#if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */
217 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000
218#else
219 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF
220#endif
221 );
222 if (image == NULL)
223 {
224 return 0;
225 }
226
227 /* Save the alpha blending attributes */
228 saved_flags = surface->flags & (SDL_SRCALPHA | SDL_RLEACCELOK);
229 saved_alpha = surface->format->alpha;
230 if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA)
231 {
232 SDL_SetAlpha (surface, 0, 0);
233 }
234
235 /* Copy the surface into the GL texture image */
236 area.x = 0;
237 area.y = 0;
238 area.w = surface->w;
239 area.h = surface->h;
240 SDL_BlitSurface (surface, &area, image, &area);
241
242 /* Restore the alpha blending attributes */
243 if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA)
244 {
245 SDL_SetAlpha (surface, saved_flags, saved_alpha);
246 }
247
248 /* Create an OpenGL texture for the image */
249 glGenTextures (1, &texture);
250 glBindTexture (GL_TEXTURE_2D, texture);
251 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
252 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
253 glTexImage2D (GL_TEXTURE_2D,
254 0,
255 GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels);
256 SDL_FreeSurface (image); /* No longer needed */
257
258 return texture;
259} 151}
260 152
261int 153int
262RunGLTest (int argc, char *argv[], 154RunGLTest (int argc, char *argv[],
263 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)
269 int done = 0; 161 int done = 0;
270 int frames; 162 int frames;
271 Uint32 start_time, this_time; 163 Uint32 start_time, this_time;
272 Uint32 video_flags; 164 Uint32 video_flags;
273 int value; 165 int value;
166 GLenum gl_error;
274 167
275 if (SDL_Init (SDL_INIT_VIDEO) < 0) 168 if (SDL_Init (SDL_INIT_VIDEO) < 0)
276 { 169 {
277 fprintf (stderr, "Couldn't initialize SDL: %s\n", SDL_GetError ()); 170 fprintf (stderr, "Couldn't initialize SDL: %s\n", SDL_GetError ());
278 exit (1); 171 exit (1);
371 264
372 /* Set the gamma for the window */ 265 /* Set the gamma for the window */
373 if (gamma != 0.0) 266 if (gamma != 0.0)
374 SDL_SetGamma (gamma, gamma, gamma); 267 SDL_SetGamma (gamma, gamma, gamma);
375 268
376 // load a entity
377 for (int i = 0; i < 7; i++) 269 for (int i = 0; i < 70; i++)
378 { 270 {
271 // load a entity
379 txtprt_parser p; 272 txtprt_parser p;
380 entity_transform *f = new entity_transform;
381 entity *e; 273 entity *e;
382 try 274 try
383 { 275 {
384 e = p.read ("test.blasc"); 276 e = p.read ("test.blasc");
277 }
385 } catch (txtprt_i_exception & e) 278 catch (txtprt_i_exception & e)
386 { 279 {
387 cout << "ERR: " << e.msg << endl; 280 cout << "ERR: " << e.msg << endl;
388 } 281 }
282
283 entity_transform *f = new entity_transform;
389 f->set (e); 284 f->set (e);
390 f->update (matrix::translation (vec3 (0, -1, -i*5))); 285 f->update (matrix::translation (vec3 (i*5, -3, -i*10)));
391 f->show (); 286 f->show ();
392 } 287 }
393 288 draw_some_random_funky_floor_dance_music (10, -500, -10, -1000);
394 camera.orig.x = camera.orig.y = camera.orig.z = 0; 289 camera.orig.x = camera.orig.y = camera.orig.z = 0;
395 camera.p = point (0, 0, 10); 290 camera.p = point (0, 0, 10);
396 camera.d = vec3 (0, 0, -1); 291 camera.d = vec3 (0, 0, -1);
397 camera.u = vec3 (0, 1, 0); 292 camera.u = vec3 (0, 1, 0);
398 camera.w = w; camera.h = h; 293 camera.w = w; camera.h = h;
399 camera.fov = 90; 294 camera.fov = 80;
400 295
401 glMatrixMode (GL_MODELVIEW); 296 glMatrixMode (GL_MODELVIEW);
402 glLoadIdentity (); 297 glLoadIdentity ();
403 298
404 glEnable (GL_CULL_FACE); 299 glEnable (GL_CULL_FACE);
405 glEnable (GL_DEPTH_TEST); 300 glEnable (GL_DEPTH_TEST);
406 shInit();
407 shSetBackend("arb");
408 glEnable(GL_VERTEX_PROGRAM_ARB);
409 glEnable(GL_FRAGMENT_PROGRAM_ARB);
410 301
411 init_shaders();
412 shBind(vsh);
413 shBind(fsh);
414 glShadeModel (GL_SMOOTH); 302 glShadeModel (GL_SMOOTH);
415 303
416 glEnable (GL_LIGHTING); 304 glEnable (GL_LIGHTING);
417 //GLfloat lightc[4] = { 1, 0.1, 0.1, 1 };
418 //glLightf (GL_LIGHT0, GL_QUADRATIC_ATTENUATION);
419 //glLightfv (GL_LIGHT0, GL_DIFFUSE, lightc);
420 glEnable (GL_LIGHT0); 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 ();
421 340
422 /* Loop until done. */ 341 /* Loop until done. */
423 start_time = SDL_GetTicks (); 342 start_time = SDL_GetTicks ();
424 frames = 0; 343 frames = 0;
425 344
426 draw_context c (camera);
427
428 while (!done) 345 while (!done)
429 { 346 {
430 GLenum gl_error;
431 char *sdl_error; 347 char *sdl_error;
432 SDL_Event event; 348 SDL_Event event;
433 349
434 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
435
436 camera.p.x += (camera_velocity_factor * camera_velocity.x) * timer.diff; 350 camera.p.x += camera_velocity_factor * camera_velocity.x * timer.diff;
437 camera.p.y += (camera_velocity_factor * camera_velocity.y) * timer.diff; 351 camera.p.y += camera_velocity_factor * camera_velocity.y * timer.diff;
438 camera.p.z += (camera_velocity_factor * camera_velocity.z) * timer.diff; 352 camera.p.z += camera_velocity_factor * camera_velocity.z * timer.diff;
439 353
440 GLfloat lightp[4]; 354 camera_angle += camera_velocity_factor * camera_velocity_angle * timer.diff;
441 lightp[0] = camera.p.x; 355 camera.d.z = -cos (camera_angle * 12 / 180.);
442 lightp[1] = camera.p.y; 356 camera.d.x = sin (camera_angle * 12 / 180.);
443 lightp[2] = camera.p.z; 357
444 lightp[3] = 1; 358 cgGLSetParameter4f (lightpos, camera.p.x, camera.p.y, camera.p.z, 1);
445 glLightfv (GL_LIGHT0, GL_POSITION, lightp); 359
360 glBindTexture (GL_TEXTURE_2D, t.texture);
361 cgGLEnableTextureParameter (g_Texture); // Enable the texture parameter
446 362
447#if 0 363#if 0
448 static GLfloat ry; 364 static GLfloat ry;
449 ry += 0.001; 365 ry += 0.001;
450 camera.d.x = cos (ry); 366 camera.d.x = cos (ry);
451 camera.d.z = sin (ry); 367 camera.d.z = sin (ry);
452 //camera.d.y = sin (ry * 0.1); 368 //camera.d.y = sin (ry * 0.1);
453#endif 369#endif
454 370
455 c.mode = draw_context::DEPTH; 371 camera.begin ();
372 camera.pass (view::DEPTH);
373 camera.pass (view::LIGHTED);
456 camera.draw (c); 374 camera.end ();
457 c.mode = draw_context::LIGHTED;
458 camera.draw (c);
459 375
460 SDL_GL_SwapBuffers (); 376 SDL_GL_SwapBuffers ();
461 timer.frame (); 377 timer.frame ();
462 378
463 /* Check for error conditions. */ 379 /* Check for error conditions. */
464 gl_error = glGetError (); 380 gl_error = glGetError ();
465 381
466 if (gl_error != GL_NO_ERROR)
467 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);
468 383
469 sdl_error = SDL_GetError (); 384 sdl_error = SDL_GetError ();
470 385
471 if (sdl_error[0] != '\0') 386 if (sdl_error[0] != '\0')
472 { 387 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines