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

Comparing libgender/test.C (file contents):
Revision 1.24 by root, Tue Oct 5 02:42:12 2004 UTC vs.
Revision 1.53 by root, Sun Oct 10 00:00:52 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, camera_angle2, camera_velocity_angle, camera_velocity_angle2;
30float camera_velocity_factor = 10; 37float camera_velocity_factor = 80;
31
32ShColor3f color = ShColor3f(.5, 0.9, 0.2);
33ShPoint3f lightPos = ShPoint3f(0.0, 10.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 out(0,1,2) = kd;
70 SH_END;
71 }
72}
73 38
74void 39void
75HotKey_ToggleFullScreen (void) 40HotKey_ToggleFullScreen (void)
76{ 41{
77 SDL_Surface *screen; 42 SDL_Surface *screen;
135 } 100 }
136 101
137 printf ("focus\n"); 102 printf ("focus\n");
138 break; 103 break;
139 104
140#define VELOCITY 10
141 case SDL_KEYDOWN: 105 case SDL_KEYDOWN:
142 if (event->key.keysym.sym == SDLK_UP) camera_velocity.z--; 106 if (event->key.keysym.sym == SDLK_UP) camera_velocity.z--;
143 if (event->key.keysym.sym == SDLK_DOWN) camera_velocity.z++; 107 if (event->key.keysym.sym == SDLK_DOWN) camera_velocity.z++;
144 if (event->key.keysym.sym == SDLK_LEFT) camera_velocity.x--;
145 if (event->key.keysym.sym == SDLK_RIGHT) camera_velocity.x++; 108 if (event->key.keysym.sym == SDLK_RIGHT) camera_velocity_angle++;
109 if (event->key.keysym.sym == SDLK_LEFT) camera_velocity_angle--;
110 if (event->key.keysym.sym == SDLK_PAGEUP) camera_velocity_angle2++;
111 if (event->key.keysym.sym == SDLK_PAGEDOWN) camera_velocity_angle2--;
146 if (event->key.keysym.sym == SDLK_a) camera_velocity.y--; 112 if (event->key.keysym.sym == SDLK_e) camera_velocity.y--;
147 if (event->key.keysym.sym == SDLK_s) camera_velocity.y++; 113 if (event->key.keysym.sym == SDLK_q) camera_velocity.y++;
148 if (event->key.keysym.sym == SDLK_v) camera_velocity_factor *= 1.5; 114 if (event->key.keysym.sym == SDLK_v) camera_velocity_factor *= 1.5;
149 if (event->key.keysym.sym == SDLK_b) camera_velocity_factor /= 1.5; 115 if (event->key.keysym.sym == SDLK_b) camera_velocity_factor /= 1.5;
150 116
151 if (event->key.keysym.sym == SDLK_ESCAPE) 117 if (event->key.keysym.sym == SDLK_ESCAPE)
152 done = 1; 118 done = 1;
153 119
154 if ((event->key.keysym.sym == SDLK_g) && 120 if ((event->key.keysym.sym == SDLK_g) &&
166 break; 132 break;
167 133
168 case SDL_KEYUP: 134 case SDL_KEYUP:
169 if (event->key.keysym.sym == SDLK_UP) camera_velocity.z++; 135 if (event->key.keysym.sym == SDLK_UP) camera_velocity.z++;
170 if (event->key.keysym.sym == SDLK_DOWN) camera_velocity.z--; 136 if (event->key.keysym.sym == SDLK_DOWN) camera_velocity.z--;
171 if (event->key.keysym.sym == SDLK_LEFT) camera_velocity.x++; 137 if (event->key.keysym.sym == SDLK_LEFT) camera_velocity_angle++;
172 if (event->key.keysym.sym == SDLK_RIGHT) camera_velocity.x--; 138 if (event->key.keysym.sym == SDLK_RIGHT) camera_velocity_angle--;
139 if (event->key.keysym.sym == SDLK_PAGEUP) camera_velocity_angle2--;
140 if (event->key.keysym.sym == SDLK_PAGEDOWN) camera_velocity_angle2++;
173 if (event->key.keysym.sym == SDLK_a) camera_velocity.y++; 141 if (event->key.keysym.sym == SDLK_e) camera_velocity.y++;
174 if (event->key.keysym.sym == SDLK_s) camera_velocity.y--; 142 if (event->key.keysym.sym == SDLK_q) camera_velocity.y--;
175 break; 143 break;
176 144
177 case SDL_QUIT: 145 case SDL_QUIT:
178 done = 1; 146 done = 1;
179 break; 147 break;
180 } 148 }
181 149
182 return (done); 150 return (done);
183} 151}
184 152
185/* Quick utility function for texture creation */ 153void draw_floor (int size, int dx, int dy, int dz)
186static int
187power_of_two (int input)
188{ 154{
189 int value = 1; 155 int x, z, ry;
190 156
191 while (value < input) 157 for (x = 0; x < 100; x++)
192 {
193 value <<= 1;
194 } 158 {
195 return value; 159 for (z = 0; z < 100; z++)
196} 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)));
197 166
198GLuint 167 geometry_quads *q = new geometry_quads;
199SDL_GL_LoadTexture (SDL_Surface * surface, GLfloat * texcoord) 168 q->set (pts);
200{ 169 entity *e = new entity (q);
201 GLuint texture; 170 e->move (vec3 (dx + x * size, dy, dz + z * size));
202 int w, h; 171 e->show ();
203 SDL_Surface *image; 172 }
204 SDL_Rect area;
205 Uint32 saved_flags;
206 Uint8 saved_alpha;
207
208 /* Use the surface width and height expanded to powers of 2 */
209 w = power_of_two (surface->w);
210 h = power_of_two (surface->h);
211 texcoord[0] = 0.0f; /* Min X */
212 texcoord[1] = 0.0f; /* Min Y */
213 texcoord[2] = (GLfloat) surface->w / w; /* Max X */
214 texcoord[3] = (GLfloat) surface->h / h; /* Max Y */
215
216 image = SDL_CreateRGBSurface (SDL_SWSURFACE, w, h, 32,
217#if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */
218 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000
219#else
220 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF
221#endif
222 );
223 if (image == NULL)
224 { 173 }
225 return 0; 174}
226 }
227 175
228 /* Save the alpha blending attributes */ 176void draw_test_nurb ()
229 saved_flags = surface->flags & (SDL_SRCALPHA | SDL_RLEACCELOK); 177{
230 saved_alpha = surface->format->alpha; 178 geometry_nurbs *q = new geometry_nurbs;
231 if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA) 179 q->set ();
232 { 180 entity *e = new entity (q);
233 SDL_SetAlpha (surface, 0, 0); 181 e->move (vec3 (10, 3, -4));
234 } 182 e->show ();
235
236 /* Copy the surface into the GL texture image */
237 area.x = 0;
238 area.y = 0;
239 area.w = surface->w;
240 area.h = surface->h;
241 SDL_BlitSurface (surface, &area, image, &area);
242
243 /* Restore the alpha blending attributes */
244 if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA)
245 {
246 SDL_SetAlpha (surface, saved_flags, saved_alpha);
247 }
248
249 /* Create an OpenGL texture for the image */
250 glGenTextures (1, &texture);
251 glBindTexture (GL_TEXTURE_2D, texture);
252 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
253 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
254 glTexImage2D (GL_TEXTURE_2D,
255 0,
256 GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels);
257 SDL_FreeSurface (image); /* No longer needed */
258
259 return texture;
260} 183}
261 184
262int 185int
263RunGLTest (int argc, char *argv[], 186RunGLTest (int argc, char *argv[],
264 int logo, int slowly, int bpp, float gamma, int noframe, int fsaa) 187 int logo, int slowly, int bpp, float gamma, int noframe, int fsaa)
270 int done = 0; 193 int done = 0;
271 int frames; 194 int frames;
272 Uint32 start_time, this_time; 195 Uint32 start_time, this_time;
273 Uint32 video_flags; 196 Uint32 video_flags;
274 int value; 197 int value;
198 GLenum gl_error;
275 199
276 if (SDL_Init (SDL_INIT_VIDEO) < 0) 200 if (SDL_Init (SDL_INIT_VIDEO) < 0)
277 { 201 {
278 fprintf (stderr, "Couldn't initialize SDL: %s\n", SDL_GetError ()); 202 fprintf (stderr, "Couldn't initialize SDL: %s\n", SDL_GetError ());
279 exit (1); 203 exit (1);
372 296
373 /* Set the gamma for the window */ 297 /* Set the gamma for the window */
374 if (gamma != 0.0) 298 if (gamma != 0.0)
375 SDL_SetGamma (gamma, gamma, gamma); 299 SDL_SetGamma (gamma, gamma, gamma);
376 300
377 // load a entity
378 for (int i = 0; i < 7; i++) 301 for (int i = 0; i < 1; i++)
379 { 302 {
303 // load a entity
380 txtprt_parser p; 304 txtprt_parser p;
381 entity_transform *f = new entity_transform; 305 geometry *g;
382 entity *e;
383 try 306 try
384 { 307 {
385 e = p.read ("test.blasc"); 308 g = p.read ("test.blasc");
309 }
386 } catch (txtprt_i_exception & e) 310 catch (txtprt_i_exception & e)
387 { 311 {
388 cout << "ERR: " << e.msg << endl; 312 cout << "ERR: " << e.msg << endl;
389 } 313 }
390 f->set (e); 314
391 f->update (gl_matrix::translation (vec3 (0, -1, -i*5))); 315 entity *e = new entity (g);
316 e->move (vec3 (i*5, -3, -i*10));
392 f->show (); 317 e->show ();
393 } 318 }
319
320 draw_floor (10, -500, -10, -1000);
321 //draw_test_nurb ();
394 322
395 camera.orig.x = camera.orig.y = camera.orig.z = 0; 323 camera.orig.x = camera.orig.y = camera.orig.z = 0;
396 camera.p = point (0, 0, 10); 324 camera.p = point (0, 0, 10);
397 camera.d = vec3 (0, 0, -1); 325 camera.d = vec3 (0, 0, -1);
398 camera.u = vec3 (0, 1, 0); 326 camera.u = vec3 (0, 1, 0);
399 camera.w = w; camera.h = h; 327 camera.w = w; camera.h = h;
400 camera.fov = 90; 328 camera.fov = 35;
329 camera.z_near = 1.;
401 330
402 glMatrixMode (GL_MODELVIEW); 331 glMatrixMode (GL_MODELVIEW);
403 glLoadIdentity (); 332 glLoadIdentity ();
404 333
405 glEnable (GL_CULL_FACE); 334 glEnable (GL_CULL_FACE);
406 glEnable (GL_DEPTH_TEST); 335 glEnable (GL_DEPTH_TEST);
407 shInit();
408 shSetBackend("arb");
409 glEnable(GL_VERTEX_PROGRAM_ARB);
410 glEnable(GL_FRAGMENT_PROGRAM_ARB);
411 336
412 init_shaders();
413 shBind(vsh);
414 shBind(fsh);
415 glShadeModel (GL_SMOOTH); 337 glShadeModel (GL_SMOOTH);
416 338
417 glEnable (GL_LIGHTING); 339 glEnable (GL_LIGHTING);
418 //GLfloat lightc[4] = { 1, 0.1, 0.1, 1 };
419 //glLightf (GL_LIGHT0, GL_QUADRATIC_ATTENUATION);
420 //glLightfv (GL_LIGHT0, GL_DIFFUSE, lightc);
421 glEnable (GL_LIGHT0); 340 glEnable (GL_LIGHT0);
341
342 cgc = cgCreateContext ();
343
344 vsh_profile = CG_PROFILE_ARBVP1;
345 //if (cgGLIsProfileSupported (CG_PROFILE_VP30)) vsh_profile = CG_PROFILE_VP30;
346 //if (cgGLIsProfileSupported (CG_PROFILE_VP40)) vsh_profile = CG_PROFILE_VP40;
347 fsh_profile = CG_PROFILE_ARBFP1;
348 //if (cgGLIsProfileSupported (CG_PROFILE_FP30)) fsh_profile = CG_PROFILE_FP30;
349 //if (cgGLIsProfileSupported (CG_PROFILE_FP40)) fsh_profile = CG_PROFILE_FP40;
350
351 vsh = cgCreateProgramFromFile (cgc, CG_SOURCE, "vsh.cg", vsh_profile, 0, 0);
352 CheckCgError ();
353 cgGLLoadProgram (vsh);
354 CheckCgError ();
355 mv = cgGetNamedParameter (vsh, "WorldProj");
356 mvp = cgGetNamedParameter (vsh, "WorldViewProj");
357 lightpos = cgGetNamedParameter (vsh, "LightPos");
358 CheckCgError ();
359
360 CGparameter g_Texture; // the texture parameter
361
362
363 fsh = cgCreateProgramFromFile (cgc, CG_SOURCE, "fsh.cg", fsh_profile, 0, 0);
364 Texture t("o.jpg");
365 g_Texture = cgGetNamedParameter(fsh, "Texture"); // the texture cg-warper ;)
366 cgGLSetTextureParameter(g_Texture, t.texture); // Bind the texture number 999 to g_Texture
367 CheckCgError ();
368 cgGLLoadProgram (fsh);
369 CheckCgError ();
370
371 cgGLBindProgram (vsh);
372 CheckCgError ();
373 cgGLBindProgram (fsh);
374 CheckCgError ();
422 375
423 /* Loop until done. */ 376 /* Loop until done. */
424 start_time = SDL_GetTicks (); 377 start_time = SDL_GetTicks ();
425 frames = 0; 378 frames = 0;
426 379
427 draw_context c (camera);
428
429 while (!done) 380 while (!done)
430 { 381 {
431 GLenum gl_error;
432 char *sdl_error; 382 char *sdl_error;
433 SDL_Event event; 383 SDL_Event event;
434 384
435 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 385 camera_angle += 180 * camera_velocity_angle * timer.diff;
386 camera_angle2 += 180 * camera_velocity_angle2 * timer.diff;
436 387
437 camera.p.x += (camera_velocity_factor * camera_velocity.x) * timer.diff; 388 vec3 geradeaus = matrix::rotation (-camera_angle, vec3 (0, 1, 0)) * vec3 (0, 0, -1);
438 camera.p.y += (camera_velocity_factor * camera_velocity.y) * timer.diff; 389 vec3 right = matrix::rotation (90., vec3 (0, 1, 0)) * geradeaus;
439 camera.p.z += (camera_velocity_factor * camera_velocity.z) * timer.diff;
440 390
441 GLfloat lightp[4]; 391 camera.d = matrix::rotation (camera_angle2, right) * geradeaus;
442 lightp[0] = camera.p.x; 392 camera.u = cross (camera.d, right);
443 lightp[1] = camera.p.y; 393
444 lightp[2] = camera.p.z; 394 camera.p = camera.p - camera.d * (camera_velocity_factor * timer.diff) * camera_velocity.z;
445 lightp[3] = 1; 395 camera.p = camera.p - camera.u * (camera_velocity_factor * timer.diff) * camera_velocity.y;
446 glLightfv (GL_LIGHT0, GL_POSITION, lightp); 396
397 cgGLSetParameter4f (lightpos, camera.p.x, camera.p.y, camera.p.z, 1);
398
399 glBindTexture (GL_TEXTURE_2D, t.texture);
400 cgGLEnableTextureParameter (g_Texture); // Enable the texture parameter
447 401
448#if 0 402#if 0
449 static GLfloat ry; 403 static GLfloat ry;
450 ry += 0.001; 404 ry += 0.001;
451 camera.d.x = cos (ry); 405 camera.d.x = cos (ry);
452 camera.d.z = sin (ry); 406 camera.d.z = sin (ry);
453 //camera.d.y = sin (ry * 0.1); 407 //camera.d.y = sin (ry * 0.1);
454#endif 408#endif
455 409
456 c.mode = draw_context::DEPTH; 410 camera.begin ();
411 camera.pass (view::DEPTH);
412 camera.pass (view::LIGHTED);
457 camera.draw (c); 413 camera.end ();
458 c.mode = draw_context::LIGHTED;
459 camera.draw (c);
460 414
461 SDL_GL_SwapBuffers (); 415 SDL_GL_SwapBuffers ();
462 timer.frame (); 416 timer.frame ();
463 417
464 /* Check for error conditions. */ 418 /* Check for error conditions. */
465 gl_error = glGetError (); 419 gl_error = glGetError ();
466 420
467 if (gl_error != GL_NO_ERROR)
468 fprintf (stderr, "testgl: OpenGL error: %d\n", gl_error); 421 if (gl_error != GL_NO_ERROR) fprintf (stderr, "testgl: OpenGL error: %d\n", gl_error);
469 422
470 sdl_error = SDL_GetError (); 423 sdl_error = SDL_GetError ();
471 424
472 if (sdl_error[0] != '\0') 425 if (sdl_error[0] != '\0')
473 { 426 {
474 fprintf (stderr, "testgl: SDL error '%s'\n", sdl_error); 427 fprintf (stderr, "testgl: SDL error '%s'\n", sdl_error);
475 SDL_ClearError (); 428 SDL_ClearError ();
476 } 429 }
477 430
478 /* Allow the user to see what's happening */ 431 /* Allow the user to see what's happening */
479 //SDL_Delay (20); 432 //SDL_Delay (40);
480 433
481 /* Check if there's a pending event. */ 434 /* Check if there's a pending event. */
482 while (SDL_PollEvent (&event)) 435 while (SDL_PollEvent (&event))
483 done = HandleEvent (&event); 436 done = HandleEvent (&event);
484 437

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines