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

Comparing libgender/test.C (file contents):
Revision 1.3 by root, Sun Oct 3 01:14:40 2004 UTC vs.
Revision 1.45 by root, Wed Oct 6 17:55:40 2004 UTC

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 5
6#include "SDL.h" 6#include "SDL.h"
7
8#include "SDL_opengl.h" 7#include "SDL_opengl.h"
9
10/* Undefine this if you want a flat cube instead of a rainbow cube */
11#define SHADED_CUBE
12
13/* Define this to be the name of the logo image to use with -logo */
14#define LOGO_FILE "icon.bmp"
15 8
16static SDL_Surface *global_image = NULL; 9static SDL_Surface *global_image = NULL;
17static GLuint global_texture = 0; 10static GLuint global_texture = 0;
18 11
12#include "util.h"
19#include "entity.h" 13#include "entity.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}
20 31
21/**********************************************************************/ 32/**********************************************************************/
33
34view camera;
35vec3 camera_velocity;
36float camera_angle = 0, camera_velocity_angle;
37float camera_velocity_factor = 20;
22 38
23void 39void
24HotKey_ToggleFullScreen (void) 40HotKey_ToggleFullScreen (void)
25{ 41{
26 SDL_Surface *screen; 42 SDL_Surface *screen;
27 43
28 screen = SDL_GetVideoSurface (); 44 screen = SDL_GetVideoSurface ();
29 if (SDL_WM_ToggleFullScreen (screen)) 45 if (SDL_WM_ToggleFullScreen (screen))
30 {
31 printf ("Toggled fullscreen mode - now %s\n", 46 printf ("Toggled fullscreen mode - now %s\n",
32 (screen->flags & SDL_FULLSCREEN) ? "fullscreen" : "windowed"); 47 (screen->flags & SDL_FULLSCREEN) ? "fullscreen" : "windowed");
33 }
34 else 48 else
35 {
36 printf ("Unable to toggle fullscreen mode\n"); 49 printf ("Unable to toggle fullscreen mode\n");
37 }
38} 50}
39 51
40void 52void
41HotKey_ToggleGrab (void) 53HotKey_ToggleGrab (void)
42{ 54{
43 SDL_GrabMode mode; 55 SDL_GrabMode mode;
44 56
45 printf ("Ctrl-G: toggling input grab!\n"); 57 printf ("Ctrl-G: toggling input grab!\n");
46 mode = SDL_WM_GrabInput (SDL_GRAB_QUERY); 58 mode = SDL_WM_GrabInput (SDL_GRAB_QUERY);
47 if (mode == SDL_GRAB_ON) 59 if (mode == SDL_GRAB_ON)
48 {
49 printf ("Grab was on\n"); 60 printf ("Grab was on\n");
50 }
51 else 61 else
52 {
53 printf ("Grab was off\n"); 62 printf ("Grab was off\n");
54 }
55 63
56 mode = SDL_WM_GrabInput (mode ? SDL_GRAB_OFF : SDL_GRAB_ON); 64 mode = SDL_WM_GrabInput (mode ? SDL_GRAB_OFF : SDL_GRAB_ON);
57 if (mode == SDL_GRAB_ON) 65 if (mode == SDL_GRAB_ON)
58 {
59 printf ("Grab is now on\n"); 66 printf ("Grab is now on\n");
60 }
61 else 67 else
62 {
63 printf ("Grab is now off\n"); 68 printf ("Grab is now off\n");
64 }
65} 69}
66 70
67void 71void
68HotKey_Iconify (void) 72HotKey_Iconify (void)
69{ 73{
97 101
98 printf ("focus\n"); 102 printf ("focus\n");
99 break; 103 break;
100 104
101 case SDL_KEYDOWN: 105 case SDL_KEYDOWN:
106 if (event->key.keysym.sym == SDLK_UP) camera_velocity.z--;
107 if (event->key.keysym.sym == SDLK_DOWN) camera_velocity.z++;
108 if (event->key.keysym.sym == SDLK_LEFT) camera_velocity.x--;
109 if (event->key.keysym.sym == SDLK_RIGHT) camera_velocity.x++;
110 if (event->key.keysym.sym == SDLK_a) camera_velocity.y--;
111 if (event->key.keysym.sym == SDLK_s) camera_velocity.y++;
112 if (event->key.keysym.sym == SDLK_v) 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--;
116
102 if (event->key.keysym.sym == SDLK_ESCAPE) 117 if (event->key.keysym.sym == SDLK_ESCAPE)
103 {
104 done = 1; 118 done = 1;
105 }
106 119
107 if ((event->key.keysym.sym == SDLK_g) && 120 if ((event->key.keysym.sym == SDLK_g) &&
108 (event->key.keysym.mod & KMOD_CTRL)) 121 (event->key.keysym.mod & KMOD_CTRL))
109 {
110 HotKey_ToggleGrab (); 122 HotKey_ToggleGrab ();
111 }
112 123
113 if ((event->key.keysym.sym == SDLK_z) && 124 if ((event->key.keysym.sym == SDLK_z) &&
114 (event->key.keysym.mod & KMOD_CTRL)) 125 (event->key.keysym.mod & KMOD_CTRL))
115 {
116 HotKey_Iconify (); 126 HotKey_Iconify ();
117 }
118 127
119 if ((event->key.keysym.sym == SDLK_RETURN) && 128 if ((event->key.keysym.sym == SDLK_RETURN) &&
120 (event->key.keysym.mod & KMOD_ALT)) 129 (event->key.keysym.mod & KMOD_ALT))
121 {
122 HotKey_ToggleFullScreen (); 130 HotKey_ToggleFullScreen ();
123 }
124 131
125 printf ("key '%s' pressed\n", SDL_GetKeyName (event->key.keysym.sym)); 132 break;
133
134 case SDL_KEYUP:
135 if (event->key.keysym.sym == SDLK_UP) camera_velocity.z++;
136 if (event->key.keysym.sym == SDLK_DOWN) camera_velocity.z--;
137 if (event->key.keysym.sym == SDLK_LEFT) camera_velocity.x++;
138 if (event->key.keysym.sym == SDLK_RIGHT) camera_velocity.x--;
139 if (event->key.keysym.sym == SDLK_a) 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++;
126 break; 143 break;
127 144
128 case SDL_QUIT: 145 case SDL_QUIT:
129 done = 1; 146 done = 1;
130 break; 147 break;
131 } 148 }
132 149
133 return (done); 150 return (done);
134}
135
136void
137SDL_GL_Enter2DMode ()
138{
139 SDL_Surface *screen = SDL_GetVideoSurface ();
140
141 /* Note, there may be other things you need to change,
142 depending on how you have your OpenGL state set up.
143 */
144 glPushAttrib (GL_ENABLE_BIT);
145 glDisable (GL_DEPTH_TEST);
146 glDisable (GL_CULL_FACE);
147 glEnable (GL_TEXTURE_2D);
148
149 /* This allows alpha blending of 2D textures with the scene */
150 glEnable (GL_BLEND);
151 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
152
153 glViewport (0, 0, screen->w, screen->h);
154
155 glMatrixMode (GL_PROJECTION);
156 glPushMatrix ();
157 glLoadIdentity ();
158
159 glOrtho (0.0, (GLdouble) screen->w, (GLdouble) screen->h, 0.0, 0.0, 1.0);
160
161 glMatrixMode (GL_MODELVIEW);
162 glPushMatrix ();
163 glLoadIdentity ();
164
165 glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
166}
167
168void
169SDL_GL_Leave2DMode ()
170{
171 glMatrixMode (GL_MODELVIEW);
172 glPopMatrix ();
173
174 glMatrixMode (GL_PROJECTION);
175 glPopMatrix ();
176
177 glPopAttrib ();
178}
179
180/* Quick utility function for texture creation */
181static int
182power_of_two (int input)
183{
184 int value = 1;
185
186 while (value < input)
187 {
188 value <<= 1;
189 }
190 return value;
191}
192
193GLuint
194SDL_GL_LoadTexture (SDL_Surface * surface, GLfloat * texcoord)
195{
196 GLuint texture;
197 int w, h;
198 SDL_Surface *image;
199 SDL_Rect area;
200 Uint32 saved_flags;
201 Uint8 saved_alpha;
202
203 /* Use the surface width and height expanded to powers of 2 */
204 w = power_of_two (surface->w);
205 h = power_of_two (surface->h);
206 texcoord[0] = 0.0f; /* Min X */
207 texcoord[1] = 0.0f; /* Min Y */
208 texcoord[2] = (GLfloat) surface->w / w; /* Max X */
209 texcoord[3] = (GLfloat) surface->h / h; /* Max Y */
210
211 image = SDL_CreateRGBSurface (SDL_SWSURFACE, w, h, 32,
212#if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */
213 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000
214#else
215 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF
216#endif
217 );
218 if (image == NULL)
219 {
220 return 0;
221 }
222
223 /* Save the alpha blending attributes */
224 saved_flags = surface->flags & (SDL_SRCALPHA | SDL_RLEACCELOK);
225 saved_alpha = surface->format->alpha;
226 if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA)
227 {
228 SDL_SetAlpha (surface, 0, 0);
229 }
230
231 /* Copy the surface into the GL texture image */
232 area.x = 0;
233 area.y = 0;
234 area.w = surface->w;
235 area.h = surface->h;
236 SDL_BlitSurface (surface, &area, image, &area);
237
238 /* Restore the alpha blending attributes */
239 if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA)
240 {
241 SDL_SetAlpha (surface, saved_flags, saved_alpha);
242 }
243
244 /* Create an OpenGL texture for the image */
245 glGenTextures (1, &texture);
246 glBindTexture (GL_TEXTURE_2D, texture);
247 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
248 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
249 glTexImage2D (GL_TEXTURE_2D,
250 0,
251 GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels);
252 SDL_FreeSurface (image); /* No longer needed */
253
254 return texture;
255} 151}
256 152
257int 153int
258RunGLTest (int argc, char *argv[], 154RunGLTest (int argc, char *argv[],
259 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)
265 int done = 0; 161 int done = 0;
266 int frames; 162 int frames;
267 Uint32 start_time, this_time; 163 Uint32 start_time, this_time;
268 Uint32 video_flags; 164 Uint32 video_flags;
269 int value; 165 int value;
166 GLenum gl_error;
270 167
271 if (SDL_Init (SDL_INIT_VIDEO) < 0) 168 if (SDL_Init (SDL_INIT_VIDEO) < 0)
272 { 169 {
273 fprintf (stderr, "Couldn't initialize SDL: %s\n", SDL_GetError ()); 170 fprintf (stderr, "Couldn't initialize SDL: %s\n", SDL_GetError ());
274 exit (1); 171 exit (1);
312 rgb_size[0] = 8; 209 rgb_size[0] = 8;
313 rgb_size[1] = 8; 210 rgb_size[1] = 8;
314 rgb_size[2] = 8; 211 rgb_size[2] = 8;
315 break; 212 break;
316 } 213 }
214
317 SDL_GL_SetAttribute (SDL_GL_RED_SIZE, rgb_size[0]); 215 SDL_GL_SetAttribute (SDL_GL_RED_SIZE, rgb_size[0]);
318 SDL_GL_SetAttribute (SDL_GL_GREEN_SIZE, rgb_size[1]); 216 SDL_GL_SetAttribute (SDL_GL_GREEN_SIZE, rgb_size[1]);
319 SDL_GL_SetAttribute (SDL_GL_BLUE_SIZE, rgb_size[2]); 217 SDL_GL_SetAttribute (SDL_GL_BLUE_SIZE, rgb_size[2]);
320 SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE, 16); 218 SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE, 16);
321 SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, 1); 219 SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, 1);
360 printf ("SDL_GL_MULTISAMPLESAMPLES: requested %d, got %d\n", fsaa, 258 printf ("SDL_GL_MULTISAMPLESAMPLES: requested %d, got %d\n", fsaa,
361 value); 259 value);
362 } 260 }
363 261
364 /* Set the window manager title bar */ 262 /* Set the window manager title bar */
365 SDL_WM_SetCaption ("SDL GL test", "testgl"); 263 SDL_WM_SetCaption ("libgender rendering test", "gendertest");
366 264
367 /* Set the gamma for the window */ 265 /* Set the gamma for the window */
368 if (gamma != 0.0) 266 if (gamma != 0.0)
369 SDL_SetGamma (gamma, gamma, gamma); 267 SDL_SetGamma (gamma, gamma, gamma);
370 268
371 view v; 269 for (int i = 0; i < 70; i++)
270 {
271 // load a entity
272 txtprt_parser p;
273 entity *e;
274 try
275 {
276 e = p.read ("test.blasc");
277 }
278 catch (txtprt_i_exception & e)
279 {
280 cout << "ERR: " << e.msg << endl;
281 }
282
283 entity_transform *f = new entity_transform;
284 f->set (e);
285 f->update (matrix::translation (vec3 (i*5, -3, -i*10)));
286 f->show ();
287 }
288 draw_some_random_funky_floor_dance_music (10, -500, -10, -1000);
289 camera.orig.x = camera.orig.y = camera.orig.z = 0;
372 v.p = point (0, 0, 0); 290 camera.p = point (0, 0, 10);
373 v.d = vec3 (0, 0, -1); 291 camera.d = vec3 (0, 0, -1);
374 v.u = vec3 (0, 1, 0); 292 camera.u = vec3 (0, 1, 0);
375 v.w = w; v.h = h; 293 camera.w = w; camera.h = h;
376 v.fov = 90; 294 camera.fov = 80;
377 295
378 glMatrixMode (GL_MODELVIEW); 296 glMatrixMode (GL_MODELVIEW);
379 glLoadIdentity (); 297 glLoadIdentity ();
380 298
299 glEnable (GL_CULL_FACE);
381 glEnable (GL_DEPTH_TEST); 300 glEnable (GL_DEPTH_TEST);
382 301
383 glDepthFunc (GL_LESS);
384
385 glShadeModel (GL_SMOOTH); 302 glShadeModel (GL_SMOOTH);
386 GLfloat ambient[4] = { 1, 1, 1, 1 }; 303
387 glEnable (GL_LIGHTING); 304 glEnable (GL_LIGHTING);
388 glEnable (GL_COLOR_MATERIAL); 305 glEnable (GL_LIGHT0);
389 glLightModelfv (GL_LIGHT_MODEL_AMBIENT, ambient); 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 ();
390 340
391 /* Loop until done. */ 341 /* Loop until done. */
392 start_time = SDL_GetTicks (); 342 start_time = SDL_GetTicks ();
393 frames = 0; 343 frames = 0;
344
394 while (!done) 345 while (!done)
395 { 346 {
396 GLenum gl_error;
397 char *sdl_error; 347 char *sdl_error;
398 SDL_Event event; 348 SDL_Event event;
399 349
400 /* Do our drawing, too. */ 350 camera.p.x += camera_velocity_factor * camera_velocity.x * timer.diff;
401 glClearColor (0.0, 0.0, 0.0, 1.0); 351 camera.p.y += camera_velocity_factor * camera_velocity.y * timer.diff;
402 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 352 camera.p.z += camera_velocity_factor * camera_velocity.z * timer.diff;
403 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.);
357
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
362
363#if 0
404 static GLfloat ry; 364 static GLfloat ry;
405 ry += 0.1; 365 ry += 0.001;
406 v.d.x = cos (ry); 366 camera.d.x = cos (ry);
407 v.d.z = sin (ry); 367 camera.d.z = sin (ry);
368 //camera.d.y = sin (ry * 0.1);
369#endif
408 370
409 draw_context c; 371 camera.begin ();
410 v.draw (c); 372 camera.pass (view::DEPTH);
373 camera.pass (view::LIGHTED);
374 camera.end ();
411 375
412 SDL_GL_SwapBuffers (); 376 SDL_GL_SwapBuffers ();
377 timer.frame ();
413 378
414 /* Check for error conditions. */ 379 /* Check for error conditions. */
415 gl_error = glGetError (); 380 gl_error = glGetError ();
416 381
417 if (gl_error != GL_NO_ERROR)
418 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);
419 383
420 sdl_error = SDL_GetError (); 384 sdl_error = SDL_GetError ();
421 385
422 if (sdl_error[0] != '\0') 386 if (sdl_error[0] != '\0')
423 { 387 {
424 fprintf (stderr, "testgl: SDL error '%s'\n", sdl_error); 388 fprintf (stderr, "testgl: SDL error '%s'\n", sdl_error);
425 SDL_ClearError (); 389 SDL_ClearError ();
426 } 390 }
427 391
428 /* Allow the user to see what's happening */ 392 /* Allow the user to see what's happening */
429 SDL_Delay (20); 393 //SDL_Delay (20);
430 394
431 /* Check if there's a pending event. */ 395 /* Check if there's a pending event. */
432 while (SDL_PollEvent (&event)) 396 while (SDL_PollEvent (&event))
433 {
434 done = HandleEvent (&event); 397 done = HandleEvent (&event);
435 } 398
399
436 ++frames; 400 ++frames;
437 } 401 }
438 402
439 /* Print out the frames per second */ 403 /* Print out the frames per second */
440 this_time = SDL_GetTicks (); 404 this_time = SDL_GetTicks ();
506 ("Usage: %s [-twice] [-logo] [-slow] [-bpp n] [-gamma n] [-noframe] [-fsaa]\n", 470 ("Usage: %s [-twice] [-logo] [-slow] [-bpp n] [-gamma n] [-noframe] [-fsaa]\n",
507 argv[0]); 471 argv[0]);
508 exit (0); 472 exit (0);
509 } 473 }
510 } 474 }
475
511 for (i = 0; i < numtests; ++i) 476 for (i = 0; i < numtests; ++i)
512 {
513 RunGLTest (argc, argv, logo, slowly, bpp, gamma, noframe, fsaa); 477 RunGLTest (argc, argv, logo, slowly, bpp, gamma, noframe, fsaa);
514 } 478
515 return 0; 479 return 0;
516} 480}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines