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

Comparing libgender/test.C (file contents):
Revision 1.23 by root, Tue Oct 5 02:17:57 2004 UTC vs.
Revision 1.58 by root, Sun Oct 10 19:50:37 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"
25 15
26/**********************************************************************/ 16/**********************************************************************/
27 17
28view camera; 18view camera;
29vec3 camera_velocity; 19vec3 camera_velocity;
20float camera_angle, camera_angle2, camera_velocity_angle, camera_velocity_angle2;
30float camera_velocity_factor = 10; 21float camera_velocity_factor = 80;
31
32ShColor3f color = ShColor3f(.5, 0.9, 0.2);
33ShPoint3f lightPos = ShPoint3f(0.0, 10.0, 10.0);
34ShMatrix4x4f mvp, mv;
35ShProgram vsh, fsh;
36
37void
38init_shaders() {
39 {
40 vsh = SH_BEGIN_PROGRAM("gpu:vertex")
41 ShInputNormal3f normal;
42 ShInputPosition4f p;
43
44 ShOutputPoint4f ov;
45 ShOutputNormal3f on;
46 ShOutputVector3f lvv;
47 ShOutputPosition4f opd;
48
49 opd = mvp | p;
50 on = normalize(mv | normal);
51 ov = -normalize(mv | p);
52 lvv = normalize(lightPos - (mv | p)(0,1,2));
53 SH_END;
54 }
55 // declare and initialize diffuse color
56 ShColor3f kd = ShColor3f(0.5, 0.7, 0.9);
57 {
58 fsh = SH_BEGIN_PROGRAM("gpu:fragment")
59 ShInputVector4f v;
60 ShInputNormal3f n;
61 ShInputVector3f lvv;
62 ShInputPosition4f p;
63
64 ShOutputColor3f out;
65 out(0,1,2) = color * dot(normalize(n), normalize(lvv));
66
67
68 SH_END;
69 }
70}
71
72 22
73void 23void
74HotKey_ToggleFullScreen (void) 24HotKey_ToggleFullScreen (void)
75{ 25{
76 SDL_Surface *screen; 26 SDL_Surface *screen;
134 } 84 }
135 85
136 printf ("focus\n"); 86 printf ("focus\n");
137 break; 87 break;
138 88
139#define VELOCITY 10
140 case SDL_KEYDOWN: 89 case SDL_KEYDOWN:
141 if (event->key.keysym.sym == SDLK_UP) camera_velocity.z--; 90 if (event->key.keysym.sym == SDLK_UP) camera_velocity.z--;
142 if (event->key.keysym.sym == SDLK_DOWN) camera_velocity.z++; 91 if (event->key.keysym.sym == SDLK_DOWN) camera_velocity.z++;
143 if (event->key.keysym.sym == SDLK_LEFT) camera_velocity.x--;
144 if (event->key.keysym.sym == SDLK_RIGHT) camera_velocity.x++; 92 if (event->key.keysym.sym == SDLK_RIGHT) camera_velocity_angle++;
93 if (event->key.keysym.sym == SDLK_LEFT) camera_velocity_angle--;
94 if (event->key.keysym.sym == SDLK_PAGEUP) camera_velocity_angle2++;
95 if (event->key.keysym.sym == SDLK_PAGEDOWN) camera_velocity_angle2--;
145 if (event->key.keysym.sym == SDLK_a) camera_velocity.y--; 96 if (event->key.keysym.sym == SDLK_e) camera_velocity.y--;
146 if (event->key.keysym.sym == SDLK_s) camera_velocity.y++; 97 if (event->key.keysym.sym == SDLK_q) camera_velocity.y++;
147 if (event->key.keysym.sym == SDLK_v) camera_velocity_factor *= 1.5; 98 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; 99 if (event->key.keysym.sym == SDLK_b) camera_velocity_factor /= 1.5;
149 100
150 if (event->key.keysym.sym == SDLK_ESCAPE) 101 if (event->key.keysym.sym == SDLK_ESCAPE)
151 done = 1; 102 done = 1;
152 103
153 if ((event->key.keysym.sym == SDLK_g) && 104 if ((event->key.keysym.sym == SDLK_g) &&
165 break; 116 break;
166 117
167 case SDL_KEYUP: 118 case SDL_KEYUP:
168 if (event->key.keysym.sym == SDLK_UP) camera_velocity.z++; 119 if (event->key.keysym.sym == SDLK_UP) camera_velocity.z++;
169 if (event->key.keysym.sym == SDLK_DOWN) camera_velocity.z--; 120 if (event->key.keysym.sym == SDLK_DOWN) camera_velocity.z--;
170 if (event->key.keysym.sym == SDLK_LEFT) camera_velocity.x++; 121 if (event->key.keysym.sym == SDLK_LEFT) camera_velocity_angle++;
171 if (event->key.keysym.sym == SDLK_RIGHT) camera_velocity.x--; 122 if (event->key.keysym.sym == SDLK_RIGHT) camera_velocity_angle--;
123 if (event->key.keysym.sym == SDLK_PAGEUP) camera_velocity_angle2--;
124 if (event->key.keysym.sym == SDLK_PAGEDOWN) camera_velocity_angle2++;
172 if (event->key.keysym.sym == SDLK_a) camera_velocity.y++; 125 if (event->key.keysym.sym == SDLK_e) camera_velocity.y++;
173 if (event->key.keysym.sym == SDLK_s) camera_velocity.y--; 126 if (event->key.keysym.sym == SDLK_q) camera_velocity.y--;
174 break; 127 break;
175 128
176 case SDL_QUIT: 129 case SDL_QUIT:
177 done = 1; 130 done = 1;
178 break; 131 break;
179 } 132 }
180 133
181 return (done); 134 return (done);
182} 135}
183 136
184/* Quick utility function for texture creation */ 137void draw_floor (int size, int dx, int dy, int dz)
185static int
186power_of_two (int input)
187{ 138{
188 int value = 1; 139 int x, z, ry;
189 140
190 while (value < input) 141 for (x = 0; x < 100; x++)
191 {
192 value <<= 1;
193 } 142 {
194 return value; 143 for (z = 0; z < 100; z++)
195} 144 {
145 vector<vertex2d> pts;
146 pts.push_back (vertex2d (point ( 0, 0, 0), vec3 (0, 1, 0), texc (0, 0)));
147 pts.push_back (vertex2d (point ( 0, 0, size), vec3 (0, 1, 0), texc (0, 1)));
148 pts.push_back (vertex2d (point (size, 0, size), vec3 (0, 1, 0), texc (1, 1)));
149 pts.push_back (vertex2d (point (size, 0, 0), vec3 (0, 1, 0), texc (1, 0)));
196 150
197GLuint 151 geometry_quads *q = new geometry_quads;
198SDL_GL_LoadTexture (SDL_Surface * surface, GLfloat * texcoord) 152 q->m = new simple_material;
199{ 153 q->set (pts);
200 GLuint texture; 154 entity *e = new entity (q);
201 int w, h; 155 e->move (vec3 (dx + x * size, dy, dz + z * size));
202 SDL_Surface *image; 156 e->show ();
203 SDL_Rect area; 157 }
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 { 158 }
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} 159}
260 160
161void draw_test_nurb ()
162{
163 geometry_nurbs *q = new geometry_nurbs;
164 q->set ();
165 entity *e = new entity (q);
166 e->move (vec3 (10, 3, -4));
167 e->show ();
168}
169
170extern void draw_level ();
261int 171int
262RunGLTest (int argc, char *argv[], 172RunGLTest (int argc, char *argv[],
263 int logo, int slowly, int bpp, float gamma, int noframe, int fsaa) 173 int logo, int slowly, int bpp, float gamma, int noframe, int fsaa)
264{ 174{
265 int i; 175 int i;
269 int done = 0; 179 int done = 0;
270 int frames; 180 int frames;
271 Uint32 start_time, this_time; 181 Uint32 start_time, this_time;
272 Uint32 video_flags; 182 Uint32 video_flags;
273 int value; 183 int value;
184 GLenum gl_error;
274 185
275 if (SDL_Init (SDL_INIT_VIDEO) < 0) 186 if (SDL_Init (SDL_INIT_VIDEO) < 0)
276 { 187 {
277 fprintf (stderr, "Couldn't initialize SDL: %s\n", SDL_GetError ()); 188 fprintf (stderr, "Couldn't initialize SDL: %s\n", SDL_GetError ());
278 exit (1); 189 exit (1);
371 282
372 /* Set the gamma for the window */ 283 /* Set the gamma for the window */
373 if (gamma != 0.0) 284 if (gamma != 0.0)
374 SDL_SetGamma (gamma, gamma, gamma); 285 SDL_SetGamma (gamma, gamma, gamma);
375 286
376 // load a entity
377 for (int i = 0; i < 7; i++) 287 for (int i = 0; i < 20; i++)
378 { 288 {
289 // load a entity
379 txtprt_parser p; 290 txtprt_parser p;
380 entity_transform *f = new entity_transform; 291 geometry *g;
381 entity *e;
382 try 292 try
383 { 293 {
384 e = p.read ("test.blasc"); 294 g = p.read ("test.blasc");
295 }
385 } catch (txtprt_i_exception & e) 296 catch (txtprt_i_exception & e)
386 { 297 {
387 cout << "ERR: " << e.msg << endl; 298 cout << "ERR: " << e.msg << endl;
388 } 299 }
389 f->set (e); 300
390 f->update (gl_matrix::translation (vec3 (0, -1, -i*5))); 301 entity *e = new entity (g);
302 e->move (vec3 (i*5, -3, -i*10));
391 f->show (); 303 e->show ();
392 } 304 }
393 305
306 draw_floor (10, -500, -10, -1000);
307 draw_level ();
308 draw_test_nurb ();
309
310 //camera.orig.x = 108; camera.orig.y = 0; camera.orig.z = -368;
394 camera.orig.x = camera.orig.y = camera.orig.z = 0; 311 camera.orig.x = 0; camera.orig.y = 0; camera.orig.z = 0;
395 camera.p = point (0, 0, 10); 312 camera.p = point (0, 0, 10);
396 camera.d = vec3 (0, 0, -1); 313 camera.d = vec3 (0, 0, -1);
397 camera.u = vec3 (0, 1, 0); 314 camera.u = vec3 (0, 1, 0);
398 camera.w = w; camera.h = h; 315 camera.w = w; camera.h = h;
399 camera.fov = 90; 316 camera.fov = 35;
317 camera.z_near = 1.;
400 318
401 glMatrixMode (GL_MODELVIEW); 319 glMatrixMode (GL_MODELVIEW);
402 glLoadIdentity (); 320 glLoadIdentity ();
403 321
404 glEnable (GL_CULL_FACE); 322 glEnable (GL_CULL_FACE);
405 glEnable (GL_DEPTH_TEST); 323 glEnable (GL_DEPTH_TEST);
406 shInit();
407 shSetBackend("arb");
408 glEnable(GL_VERTEX_PROGRAM_ARB);
409 glEnable(GL_FRAGMENT_PROGRAM_ARB);
410 324
411 init_shaders();
412 shBind(vsh);
413 shBind(fsh);
414 glShadeModel (GL_SMOOTH); 325 glShadeModel (GL_SMOOTH);
415 326
416 glEnable (GL_LIGHTING); 327 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); 328 glEnable (GL_LIGHT0);
329
330 init_shaders ();
421 331
422 /* Loop until done. */ 332 /* Loop until done. */
423 start_time = SDL_GetTicks (); 333 start_time = SDL_GetTicks ();
424 frames = 0; 334 frames = 0;
425 335
426 draw_context c (camera);
427
428 while (!done) 336 while (!done)
429 { 337 {
430 GLenum gl_error;
431 char *sdl_error; 338 char *sdl_error;
432 SDL_Event event; 339 SDL_Event event;
433 340
434 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 341 camera_angle += 180 * camera_velocity_angle * timer.diff;
342 camera_angle2 += 180 * camera_velocity_angle2 * timer.diff;
435 343
436 camera.p.x += (camera_velocity_factor * camera_velocity.x) * timer.diff; 344 vec3 geradeaus = matrix::rotation (-camera_angle, vec3 (0, 1, 0)) * vec3 (0, 0, -1);
437 camera.p.y += (camera_velocity_factor * camera_velocity.y) * timer.diff; 345 vec3 right = matrix::rotation (90., vec3 (0, 1, 0)) * geradeaus;
438 camera.p.z += (camera_velocity_factor * camera_velocity.z) * timer.diff;
439 346
440 GLfloat lightp[4]; 347 camera.d = matrix::rotation (camera_angle2, right) * geradeaus;
441 lightp[0] = camera.p.x; 348 camera.u = cross (camera.d, right);
442 lightp[1] = camera.p.y; 349
443 lightp[2] = camera.p.z; 350 camera.p = camera.p - camera.d * (camera_velocity_factor * timer.diff) * camera_velocity.z;
444 lightp[3] = 1; 351 camera.p = camera.p - camera.u * (camera_velocity_factor * timer.diff) * camera_velocity.y;
445 glLightfv (GL_LIGHT0, GL_POSITION, lightp); 352
446 353
447#if 0 354#if 0
448 static GLfloat ry; 355 static GLfloat ry;
449 ry += 0.001; 356 ry += 0.001;
450 camera.d.x = cos (ry); 357 camera.d.x = cos (ry);
451 camera.d.z = sin (ry); 358 camera.d.z = sin (ry);
452 //camera.d.y = sin (ry * 0.1); 359 //camera.d.y = sin (ry * 0.1);
453#endif 360#endif
454 361
455 c.mode = draw_context::DEPTH; 362 camera.begin ();
363 camera.pass (view::DEPTH);
364 camera.pass (view::LIGHTED);
456 camera.draw (c); 365 camera.end ();
457 c.mode = draw_context::LIGHTED;
458 camera.draw (c);
459 366
460 SDL_GL_SwapBuffers (); 367 SDL_GL_SwapBuffers ();
461 timer.frame (); 368 timer.frame ();
462 369
463 /* Check for error conditions. */ 370 /* Check for error conditions. */
464 gl_error = glGetError (); 371 gl_error = glGetError ();
465 372
466 if (gl_error != GL_NO_ERROR)
467 fprintf (stderr, "testgl: OpenGL error: %d\n", gl_error); 373 if (gl_error != GL_NO_ERROR) fprintf (stderr, "testgl: OpenGL error: %d\n", gl_error);
468 374
469 sdl_error = SDL_GetError (); 375 sdl_error = SDL_GetError ();
470 376
471 if (sdl_error[0] != '\0') 377 if (sdl_error[0] != '\0')
472 { 378 {
473 fprintf (stderr, "testgl: SDL error '%s'\n", sdl_error); 379 fprintf (stderr, "testgl: SDL error '%s'\n", sdl_error);
474 SDL_ClearError (); 380 SDL_ClearError ();
475 } 381 }
476 382
477 /* Allow the user to see what's happening */ 383 /* Allow the user to see what's happening */
478 //SDL_Delay (20); 384 //SDL_Delay (40);
479 385
480 /* Check if there's a pending event. */ 386 /* Check if there's a pending event. */
481 while (SDL_PollEvent (&event)) 387 while (SDL_PollEvent (&event))
482 done = HandleEvent (&event); 388 done = HandleEvent (&event);
483 389
506 412
507 /* Destroy our GL context, etc. */ 413 /* Destroy our GL context, etc. */
508 SDL_Quit (); 414 SDL_Quit ();
509 return (0); 415 return (0);
510} 416}
511
512int 417int
513main (int argc, char *argv[]) 418main (int argc, char *argv[])
514{ 419{
515 int i, logo; 420 int i, logo;
516 int numtests; 421 int numtests;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines