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.43 by root, Wed Oct 6 16:51:20 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)
371 263
372 /* Set the gamma for the window */ 264 /* Set the gamma for the window */
373 if (gamma != 0.0) 265 if (gamma != 0.0)
374 SDL_SetGamma (gamma, gamma, gamma); 266 SDL_SetGamma (gamma, gamma, gamma);
375 267
376 // load a entity
377 for (int i = 0; i < 7; i++) 268 for (int i = 0; i < 70; i++)
378 { 269 {
270 // load a entity
379 txtprt_parser p; 271 txtprt_parser p;
380 entity_transform *f = new entity_transform;
381 entity *e; 272 entity *e;
382 try 273 try
383 { 274 {
384 e = p.read ("test.blasc"); 275 e = p.read ("test.blasc");
276 }
385 } catch (txtprt_i_exception & e) 277 catch (txtprt_i_exception & e)
386 { 278 {
387 cout << "ERR: " << e.msg << endl; 279 cout << "ERR: " << e.msg << endl;
388 } 280 }
281
282 entity_transform *f = new entity_transform;
389 f->set (e); 283 f->set (e);
390 f->update (matrix::translation (vec3 (0, -1, -i*5))); 284 f->update (matrix::translation (vec3 (i*5, -3, -i*10)));
391 f->show (); 285 f->show ();
392 } 286 }
393 287 draw_some_random_funky_floor_dance_music (10, -500, -10, -1000);
394 camera.orig.x = camera.orig.y = camera.orig.z = 0; 288 camera.orig.x = camera.orig.y = camera.orig.z = 0;
395 camera.p = point (0, 0, 10); 289 camera.p = point (0, 0, 10);
396 camera.d = vec3 (0, 0, -1); 290 camera.d = vec3 (0, 0, -1);
397 camera.u = vec3 (0, 1, 0); 291 camera.u = vec3 (0, 1, 0);
398 camera.w = w; camera.h = h; 292 camera.w = w; camera.h = h;
399 camera.fov = 90; 293 camera.fov = 80;
400 294
401 glMatrixMode (GL_MODELVIEW); 295 glMatrixMode (GL_MODELVIEW);
402 glLoadIdentity (); 296 glLoadIdentity ();
403 297
404 glEnable (GL_CULL_FACE); 298 glEnable (GL_CULL_FACE);
405 glEnable (GL_DEPTH_TEST); 299 glEnable (GL_DEPTH_TEST);
406 shInit();
407 shSetBackend("arb");
408 glEnable(GL_VERTEX_PROGRAM_ARB);
409 glEnable(GL_FRAGMENT_PROGRAM_ARB);
410 300
411 init_shaders();
412 shBind(vsh);
413 shBind(fsh);
414 glShadeModel (GL_SMOOTH); 301 glShadeModel (GL_SMOOTH);
415 302
416 glEnable (GL_LIGHTING); 303 glEnable (GL_LIGHTING);
417 //GLfloat lightc[4] = { 1, 0.1, 0.1, 1 }; 304 //GLfloat lightc[4] = { 1, 0.1, 0.1, 1 };
418 //glLightf (GL_LIGHT0, GL_QUADRATIC_ATTENUATION); 305 //glLightf (GL_LIGHT0, GL_QUADRATIC_ATTENUATION);
419 //glLightfv (GL_LIGHT0, GL_DIFFUSE, lightc); 306 //glLightfv (GL_LIGHT0, GL_DIFFUSE, lightc);
420 glEnable (GL_LIGHT0); 307 glEnable (GL_LIGHT0);
421 308
309 cgc = cgCreateContext ();
310
311 vsh_profile = CG_PROFILE_ARBVP1;
312 //if (cgGLIsProfileSupported (CG_PROFILE_VP30)) vsh_profile = CG_PROFILE_VP30;
313 //if (cgGLIsProfileSupported (CG_PROFILE_VP40)) vsh_profile = CG_PROFILE_VP40;
314 fsh_profile = CG_PROFILE_ARBFP1;
315 //if (cgGLIsProfileSupported (CG_PROFILE_FP30)) fsh_profile = CG_PROFILE_FP30;
316 //if (cgGLIsProfileSupported (CG_PROFILE_FP40)) fsh_profile = CG_PROFILE_FP40;
317
318 vsh = cgCreateProgramFromFile (cgc, CG_SOURCE, "vsh.cg", vsh_profile, 0, 0);
319 CheckCgError ();
320 cgGLLoadProgram (vsh);
321 CheckCgError ();
322 mv = cgGetNamedParameter (vsh, "WorldProj");
323 mvp = cgGetNamedParameter (vsh, "WorldViewProj");
324 lightpos = cgGetNamedParameter (vsh, "LightPos");
325 CheckCgError ();
326
327 CGparameter g_Texture; // the texture parameter
328
329
330 fsh = cgCreateProgramFromFile (cgc, CG_SOURCE, "fsh.cg", fsh_profile, 0, 0);
331 Texture t("o.jpg");
332 g_Texture = cgGetNamedParameter(fsh, "Texture"); // the texture cg-warper ;)
333 cgGLSetTextureParameter(g_Texture, t.texture); // Bind the texture number 999 to g_Texture
334 CheckCgError ();
335 cgGLLoadProgram (fsh);
336 CheckCgError ();
337
338 cgGLBindProgram (vsh);
339 CheckCgError ();
340 cgGLBindProgram (fsh);
341 CheckCgError ();
342
422 /* Loop until done. */ 343 /* Loop until done. */
423 start_time = SDL_GetTicks (); 344 start_time = SDL_GetTicks ();
424 frames = 0; 345 frames = 0;
425 346
426 draw_context c (camera);
427
428 while (!done) 347 while (!done)
429 { 348 {
430 GLenum gl_error; 349 GLenum gl_error;
431 char *sdl_error; 350 char *sdl_error;
432 SDL_Event event; 351 SDL_Event event;
433 352
434 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
435
436 camera.p.x += (camera_velocity_factor * camera_velocity.x) * timer.diff; 353 camera.p.x += camera_velocity_factor * camera_velocity.x * timer.diff;
437 camera.p.y += (camera_velocity_factor * camera_velocity.y) * timer.diff; 354 camera.p.y += camera_velocity_factor * camera_velocity.y * timer.diff;
438 camera.p.z += (camera_velocity_factor * camera_velocity.z) * timer.diff; 355 camera.p.z += camera_velocity_factor * camera_velocity.z * timer.diff;
439 356
440 GLfloat lightp[4]; 357 camera_angle += camera_velocity_factor * camera_velocity_angle * timer.diff;
441 lightp[0] = camera.p.x; 358 camera.d.z = -cos (camera_angle * 12 / 180.);
442 lightp[1] = camera.p.y; 359 camera.d.x = sin (camera_angle * 12 / 180.);
443 lightp[2] = camera.p.z; 360
444 lightp[3] = 1; 361 cgGLSetParameter4f (lightpos, camera.p.x, camera.p.y, camera.p.z, 1);
445 glLightfv (GL_LIGHT0, GL_POSITION, lightp); 362
363 glBindTexture (GL_TEXTURE_2D, t.texture);
364 cgGLEnableTextureParameter (g_Texture); // Enable the texture parameter
446 365
447#if 0 366#if 0
448 static GLfloat ry; 367 static GLfloat ry;
449 ry += 0.001; 368 ry += 0.001;
450 camera.d.x = cos (ry); 369 camera.d.x = cos (ry);
451 camera.d.z = sin (ry); 370 camera.d.z = sin (ry);
452 //camera.d.y = sin (ry * 0.1); 371 //camera.d.y = sin (ry * 0.1);
453#endif 372#endif
454 373
455 c.mode = draw_context::DEPTH; 374 camera.begin ();
375 camera.pass (view::DEPTH);
376 camera.pass (view::LIGHTED);
456 camera.draw (c); 377 camera.end ();
457 c.mode = draw_context::LIGHTED;
458 camera.draw (c);
459 378
460 SDL_GL_SwapBuffers (); 379 SDL_GL_SwapBuffers ();
461 timer.frame (); 380 timer.frame ();
462 381
463 /* Check for error conditions. */ 382 /* Check for error conditions. */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines