1 |
#include <stdlib.h> |
2 |
#include <stdio.h> |
3 |
#include <string.h> |
4 |
#include <math.h> |
5 |
|
6 |
#include "SDL.h" |
7 |
#include "SDL_opengl.h" |
8 |
|
9 |
static SDL_Surface *global_image = NULL; |
10 |
static GLuint global_texture = 0; |
11 |
|
12 |
#include "util.h" |
13 |
#include "entity.h" |
14 |
#include "txtprt_import.h" |
15 |
|
16 |
CGcontext cgc; |
17 |
CGprogram vsh, fsh; |
18 |
CGparameter mv, mvp, lightpos; |
19 |
|
20 |
static void CheckCgError(void) |
21 |
{ |
22 |
CGerror err = cgGetError(); |
23 |
|
24 |
if (err != CG_NO_ERROR) |
25 |
{ |
26 |
printf("CG error: %s\n", cgGetErrorString(err)); |
27 |
exit(1); |
28 |
} |
29 |
} |
30 |
|
31 |
/**********************************************************************/ |
32 |
|
33 |
view camera; |
34 |
vec3 camera_velocity; |
35 |
float camera_velocity_factor = 10; |
36 |
|
37 |
void |
38 |
HotKey_ToggleFullScreen (void) |
39 |
{ |
40 |
SDL_Surface *screen; |
41 |
|
42 |
screen = SDL_GetVideoSurface (); |
43 |
if (SDL_WM_ToggleFullScreen (screen)) |
44 |
printf ("Toggled fullscreen mode - now %s\n", |
45 |
(screen->flags & SDL_FULLSCREEN) ? "fullscreen" : "windowed"); |
46 |
else |
47 |
printf ("Unable to toggle fullscreen mode\n"); |
48 |
} |
49 |
|
50 |
void |
51 |
HotKey_ToggleGrab (void) |
52 |
{ |
53 |
SDL_GrabMode mode; |
54 |
|
55 |
printf ("Ctrl-G: toggling input grab!\n"); |
56 |
mode = SDL_WM_GrabInput (SDL_GRAB_QUERY); |
57 |
if (mode == SDL_GRAB_ON) |
58 |
printf ("Grab was on\n"); |
59 |
else |
60 |
printf ("Grab was off\n"); |
61 |
|
62 |
mode = SDL_WM_GrabInput (mode ? SDL_GRAB_OFF : SDL_GRAB_ON); |
63 |
if (mode == SDL_GRAB_ON) |
64 |
printf ("Grab is now on\n"); |
65 |
else |
66 |
printf ("Grab is now off\n"); |
67 |
} |
68 |
|
69 |
void |
70 |
HotKey_Iconify (void) |
71 |
{ |
72 |
printf ("Ctrl-Z: iconifying window!\n"); |
73 |
SDL_WM_IconifyWindow (); |
74 |
} |
75 |
|
76 |
int |
77 |
HandleEvent (SDL_Event * event) |
78 |
{ |
79 |
int done; |
80 |
|
81 |
done = 0; |
82 |
switch (event->type) |
83 |
{ |
84 |
case SDL_ACTIVEEVENT: |
85 |
/* See what happened */ |
86 |
printf ("app %s ", event->active.gain ? "gained" : "lost"); |
87 |
if (event->active.state & SDL_APPACTIVE) |
88 |
{ |
89 |
printf ("active "); |
90 |
} |
91 |
else if (event->active.state & SDL_APPMOUSEFOCUS) |
92 |
{ |
93 |
printf ("mouse "); |
94 |
} |
95 |
else if (event->active.state & SDL_APPINPUTFOCUS) |
96 |
{ |
97 |
printf ("input "); |
98 |
} |
99 |
|
100 |
printf ("focus\n"); |
101 |
break; |
102 |
|
103 |
#define VELOCITY 10 |
104 |
case SDL_KEYDOWN: |
105 |
if (event->key.keysym.sym == SDLK_UP) camera_velocity.z--; |
106 |
if (event->key.keysym.sym == SDLK_DOWN) camera_velocity.z++; |
107 |
if (event->key.keysym.sym == SDLK_LEFT) camera_velocity.x--; |
108 |
if (event->key.keysym.sym == SDLK_RIGHT) camera_velocity.x++; |
109 |
if (event->key.keysym.sym == SDLK_a) camera_velocity.y--; |
110 |
if (event->key.keysym.sym == SDLK_s) camera_velocity.y++; |
111 |
if (event->key.keysym.sym == SDLK_v) camera_velocity_factor *= 1.5; |
112 |
if (event->key.keysym.sym == SDLK_b) camera_velocity_factor /= 1.5; |
113 |
|
114 |
if (event->key.keysym.sym == SDLK_ESCAPE) |
115 |
done = 1; |
116 |
|
117 |
if ((event->key.keysym.sym == SDLK_g) && |
118 |
(event->key.keysym.mod & KMOD_CTRL)) |
119 |
HotKey_ToggleGrab (); |
120 |
|
121 |
if ((event->key.keysym.sym == SDLK_z) && |
122 |
(event->key.keysym.mod & KMOD_CTRL)) |
123 |
HotKey_Iconify (); |
124 |
|
125 |
if ((event->key.keysym.sym == SDLK_RETURN) && |
126 |
(event->key.keysym.mod & KMOD_ALT)) |
127 |
HotKey_ToggleFullScreen (); |
128 |
|
129 |
break; |
130 |
|
131 |
case SDL_KEYUP: |
132 |
if (event->key.keysym.sym == SDLK_UP) camera_velocity.z++; |
133 |
if (event->key.keysym.sym == SDLK_DOWN) camera_velocity.z--; |
134 |
if (event->key.keysym.sym == SDLK_LEFT) camera_velocity.x++; |
135 |
if (event->key.keysym.sym == SDLK_RIGHT) camera_velocity.x--; |
136 |
if (event->key.keysym.sym == SDLK_a) camera_velocity.y++; |
137 |
if (event->key.keysym.sym == SDLK_s) camera_velocity.y--; |
138 |
break; |
139 |
|
140 |
case SDL_QUIT: |
141 |
done = 1; |
142 |
break; |
143 |
} |
144 |
|
145 |
return (done); |
146 |
} |
147 |
|
148 |
/* Quick utility function for texture creation */ |
149 |
static int |
150 |
power_of_two (int input) |
151 |
{ |
152 |
int value = 1; |
153 |
|
154 |
while (value < input) |
155 |
{ |
156 |
value <<= 1; |
157 |
} |
158 |
return value; |
159 |
} |
160 |
|
161 |
GLuint |
162 |
SDL_GL_LoadTexture (SDL_Surface * surface, GLfloat * texcoord) |
163 |
{ |
164 |
GLuint texture; |
165 |
int w, h; |
166 |
SDL_Surface *image; |
167 |
SDL_Rect area; |
168 |
Uint32 saved_flags; |
169 |
Uint8 saved_alpha; |
170 |
|
171 |
/* Use the surface width and height expanded to powers of 2 */ |
172 |
w = power_of_two (surface->w); |
173 |
h = power_of_two (surface->h); |
174 |
texcoord[0] = 0.0f; /* Min X */ |
175 |
texcoord[1] = 0.0f; /* Min Y */ |
176 |
texcoord[2] = (GLfloat) surface->w / w; /* Max X */ |
177 |
texcoord[3] = (GLfloat) surface->h / h; /* Max Y */ |
178 |
|
179 |
image = SDL_CreateRGBSurface (SDL_SWSURFACE, w, h, 32, |
180 |
#if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */ |
181 |
0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000 |
182 |
#else |
183 |
0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF |
184 |
#endif |
185 |
); |
186 |
if (image == NULL) |
187 |
{ |
188 |
return 0; |
189 |
} |
190 |
|
191 |
/* Save the alpha blending attributes */ |
192 |
saved_flags = surface->flags & (SDL_SRCALPHA | SDL_RLEACCELOK); |
193 |
saved_alpha = surface->format->alpha; |
194 |
if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA) |
195 |
{ |
196 |
SDL_SetAlpha (surface, 0, 0); |
197 |
} |
198 |
|
199 |
/* Copy the surface into the GL texture image */ |
200 |
area.x = 0; |
201 |
area.y = 0; |
202 |
area.w = surface->w; |
203 |
area.h = surface->h; |
204 |
SDL_BlitSurface (surface, &area, image, &area); |
205 |
|
206 |
/* Restore the alpha blending attributes */ |
207 |
if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA) |
208 |
{ |
209 |
SDL_SetAlpha (surface, saved_flags, saved_alpha); |
210 |
} |
211 |
|
212 |
/* Create an OpenGL texture for the image */ |
213 |
glGenTextures (1, &texture); |
214 |
glBindTexture (GL_TEXTURE_2D, texture); |
215 |
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
216 |
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
217 |
glTexImage2D (GL_TEXTURE_2D, |
218 |
0, |
219 |
GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels); |
220 |
SDL_FreeSurface (image); /* No longer needed */ |
221 |
|
222 |
return texture; |
223 |
} |
224 |
|
225 |
int |
226 |
RunGLTest (int argc, char *argv[], |
227 |
int logo, int slowly, int bpp, float gamma, int noframe, int fsaa) |
228 |
{ |
229 |
int i; |
230 |
int rgb_size[3]; |
231 |
int w = 640; |
232 |
int h = 480; |
233 |
int done = 0; |
234 |
int frames; |
235 |
Uint32 start_time, this_time; |
236 |
Uint32 video_flags; |
237 |
int value; |
238 |
|
239 |
if (SDL_Init (SDL_INIT_VIDEO) < 0) |
240 |
{ |
241 |
fprintf (stderr, "Couldn't initialize SDL: %s\n", SDL_GetError ()); |
242 |
exit (1); |
243 |
} |
244 |
|
245 |
/* See if we should detect the display depth */ |
246 |
if (bpp == 0) |
247 |
{ |
248 |
if (SDL_GetVideoInfo ()->vfmt->BitsPerPixel <= 8) |
249 |
bpp = 8; |
250 |
else |
251 |
bpp = 16; /* More doesn't seem to work */ |
252 |
} |
253 |
|
254 |
video_flags = SDL_OPENGL; |
255 |
|
256 |
for (i = 1; argv[i]; ++i) |
257 |
if (strcmp (argv[1], "-fullscreen") == 0) |
258 |
video_flags |= SDL_FULLSCREEN; |
259 |
|
260 |
if (noframe) |
261 |
video_flags |= SDL_NOFRAME; |
262 |
|
263 |
/* Initialize the display */ |
264 |
switch (bpp) |
265 |
{ |
266 |
case 8: |
267 |
rgb_size[0] = 3; |
268 |
rgb_size[1] = 3; |
269 |
rgb_size[2] = 2; |
270 |
break; |
271 |
|
272 |
case 15: |
273 |
case 16: |
274 |
rgb_size[0] = 5; |
275 |
rgb_size[1] = 5; |
276 |
rgb_size[2] = 5; |
277 |
break; |
278 |
|
279 |
default: |
280 |
rgb_size[0] = 8; |
281 |
rgb_size[1] = 8; |
282 |
rgb_size[2] = 8; |
283 |
break; |
284 |
} |
285 |
|
286 |
SDL_GL_SetAttribute (SDL_GL_RED_SIZE, rgb_size[0]); |
287 |
SDL_GL_SetAttribute (SDL_GL_GREEN_SIZE, rgb_size[1]); |
288 |
SDL_GL_SetAttribute (SDL_GL_BLUE_SIZE, rgb_size[2]); |
289 |
SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE, 16); |
290 |
SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, 1); |
291 |
|
292 |
if (fsaa) |
293 |
{ |
294 |
SDL_GL_SetAttribute (SDL_GL_MULTISAMPLEBUFFERS, 1); |
295 |
SDL_GL_SetAttribute (SDL_GL_MULTISAMPLESAMPLES, fsaa); |
296 |
} |
297 |
|
298 |
if (SDL_SetVideoMode (w, h, bpp, video_flags) == NULL) |
299 |
{ |
300 |
fprintf (stderr, "Couldn't set GL mode: %s\n", SDL_GetError ()); |
301 |
SDL_Quit (); |
302 |
exit (1); |
303 |
} |
304 |
|
305 |
printf ("Screen BPP: %d\n", SDL_GetVideoSurface ()->format->BitsPerPixel); |
306 |
printf ("\n"); |
307 |
printf ("Vendor : %s\n", glGetString (GL_VENDOR)); |
308 |
printf ("Renderer : %s\n", glGetString (GL_RENDERER)); |
309 |
printf ("Version : %s\n", glGetString (GL_VERSION)); |
310 |
printf ("Extensions : %s\n", glGetString (GL_EXTENSIONS)); |
311 |
printf ("\n"); |
312 |
|
313 |
SDL_GL_GetAttribute (SDL_GL_RED_SIZE, &value); |
314 |
printf ("SDL_GL_RED_SIZE: requested %d, got %d\n", rgb_size[0], value); |
315 |
SDL_GL_GetAttribute (SDL_GL_GREEN_SIZE, &value); |
316 |
printf ("SDL_GL_GREEN_SIZE: requested %d, got %d\n", rgb_size[1], value); |
317 |
SDL_GL_GetAttribute (SDL_GL_BLUE_SIZE, &value); |
318 |
printf ("SDL_GL_BLUE_SIZE: requested %d, got %d\n", rgb_size[2], value); |
319 |
SDL_GL_GetAttribute (SDL_GL_DEPTH_SIZE, &value); |
320 |
printf ("SDL_GL_DEPTH_SIZE: requested %d, got %d\n", bpp, value); |
321 |
SDL_GL_GetAttribute (SDL_GL_DOUBLEBUFFER, &value); |
322 |
printf ("SDL_GL_DOUBLEBUFFER: requested 1, got %d\n", value); |
323 |
|
324 |
if (fsaa) |
325 |
{ |
326 |
SDL_GL_GetAttribute (SDL_GL_MULTISAMPLEBUFFERS, &value); |
327 |
printf ("SDL_GL_MULTISAMPLEBUFFERS: requested 1, got %d\n", value); |
328 |
SDL_GL_GetAttribute (SDL_GL_MULTISAMPLESAMPLES, &value); |
329 |
printf ("SDL_GL_MULTISAMPLESAMPLES: requested %d, got %d\n", fsaa, |
330 |
value); |
331 |
} |
332 |
|
333 |
/* Set the window manager title bar */ |
334 |
SDL_WM_SetCaption ("libgender rendering test", "gendertest"); |
335 |
|
336 |
/* Set the gamma for the window */ |
337 |
if (gamma != 0.0) |
338 |
SDL_SetGamma (gamma, gamma, gamma); |
339 |
|
340 |
// load a entity |
341 |
for (int i = 0; i < 7; i++) |
342 |
{ |
343 |
txtprt_parser p; |
344 |
entity_transform *f = new entity_transform; |
345 |
entity *e; |
346 |
try |
347 |
{ |
348 |
e = p.read ("test.blasc"); |
349 |
} catch (txtprt_i_exception & e) |
350 |
{ |
351 |
cout << "ERR: " << e.msg << endl; |
352 |
} |
353 |
f->set (e); |
354 |
f->update (matrix::translation (vec3 (0, -1, -i*5))); |
355 |
f->show (); |
356 |
} |
357 |
|
358 |
camera.orig.x = camera.orig.y = camera.orig.z = 0; |
359 |
camera.p = point (0, 0, 10); |
360 |
camera.d = vec3 (0, 0, -1); |
361 |
camera.u = vec3 (0, 1, 0); |
362 |
camera.w = w; camera.h = h; |
363 |
camera.fov = 90; |
364 |
|
365 |
glMatrixMode (GL_MODELVIEW); |
366 |
glLoadIdentity (); |
367 |
|
368 |
glEnable (GL_CULL_FACE); |
369 |
glEnable (GL_DEPTH_TEST); |
370 |
//glEnable(GL_VERTEX_PROGRAM_ARB); |
371 |
//glEnable(GL_FRAGMENT_PROGRAM_ARB); |
372 |
|
373 |
glShadeModel (GL_SMOOTH); |
374 |
|
375 |
glEnable (GL_LIGHTING); |
376 |
//GLfloat lightc[4] = { 1, 0.1, 0.1, 1 }; |
377 |
//glLightf (GL_LIGHT0, GL_QUADRATIC_ATTENUATION); |
378 |
//glLightfv (GL_LIGHT0, GL_DIFFUSE, lightc); |
379 |
glEnable (GL_LIGHT0); |
380 |
|
381 |
cgc = cgCreateContext (); |
382 |
vsh = cgCreateProgramFromFile (cgc, CG_SOURCE, "vsh.cg", CG_PROFILE_ARBVP1, 0, 0); |
383 |
CheckCgError (); |
384 |
cgGLLoadProgram (vsh); |
385 |
CheckCgError (); |
386 |
mv = cgGetNamedParameter (vsh, "WorldProj"); |
387 |
mvp = cgGetNamedParameter (vsh, "WorldViewProj"); |
388 |
lightpos = cgGetNamedParameter (vsh, "LightPos"); |
389 |
CheckCgError (); |
390 |
cgGLEnableProfile (CG_PROFILE_ARBVP1); |
391 |
CheckCgError (); |
392 |
|
393 |
fsh = cgCreateProgramFromFile (cgc, CG_SOURCE, "fsh.cg", CG_PROFILE_ARBFP1, 0, 0); |
394 |
CheckCgError (); |
395 |
cgGLLoadProgram (fsh); |
396 |
CheckCgError (); |
397 |
cgGLEnableProfile (CG_PROFILE_ARBFP1); |
398 |
CheckCgError (); |
399 |
|
400 |
cgGLBindProgram (vsh); |
401 |
CheckCgError (); |
402 |
cgGLBindProgram (fsh); |
403 |
CheckCgError (); |
404 |
|
405 |
/* Loop until done. */ |
406 |
start_time = SDL_GetTicks (); |
407 |
frames = 0; |
408 |
|
409 |
draw_context c (camera); |
410 |
|
411 |
while (!done) |
412 |
{ |
413 |
GLenum gl_error; |
414 |
char *sdl_error; |
415 |
SDL_Event event; |
416 |
|
417 |
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
418 |
|
419 |
camera.p.x += (camera_velocity_factor * camera_velocity.x) * timer.diff; |
420 |
camera.p.y += (camera_velocity_factor * camera_velocity.y) * timer.diff; |
421 |
camera.p.z += (camera_velocity_factor * camera_velocity.z) * timer.diff; |
422 |
|
423 |
cgGLSetParameter4f (lightpos, camera.p.x, camera.p.y, camera.p.z, 1); |
424 |
|
425 |
#if 0 |
426 |
static GLfloat ry; |
427 |
ry += 0.001; |
428 |
camera.d.x = cos (ry); |
429 |
camera.d.z = sin (ry); |
430 |
//camera.d.y = sin (ry * 0.1); |
431 |
#endif |
432 |
|
433 |
c.mode = draw_context::DEPTH; |
434 |
camera.draw (c); |
435 |
c.mode = draw_context::LIGHTED; |
436 |
camera.draw (c); |
437 |
|
438 |
SDL_GL_SwapBuffers (); |
439 |
timer.frame (); |
440 |
|
441 |
/* Check for error conditions. */ |
442 |
gl_error = glGetError (); |
443 |
|
444 |
if (gl_error != GL_NO_ERROR) |
445 |
fprintf (stderr, "testgl: OpenGL error: %d\n", gl_error); |
446 |
|
447 |
sdl_error = SDL_GetError (); |
448 |
|
449 |
if (sdl_error[0] != '\0') |
450 |
{ |
451 |
fprintf (stderr, "testgl: SDL error '%s'\n", sdl_error); |
452 |
SDL_ClearError (); |
453 |
} |
454 |
|
455 |
/* Allow the user to see what's happening */ |
456 |
//SDL_Delay (20); |
457 |
|
458 |
/* Check if there's a pending event. */ |
459 |
while (SDL_PollEvent (&event)) |
460 |
done = HandleEvent (&event); |
461 |
|
462 |
|
463 |
++frames; |
464 |
} |
465 |
|
466 |
/* Print out the frames per second */ |
467 |
this_time = SDL_GetTicks (); |
468 |
if (this_time != start_time) |
469 |
{ |
470 |
printf ("%2.2f FPS\n", |
471 |
((float) frames / (this_time - start_time)) * 1000.0); |
472 |
} |
473 |
|
474 |
if (global_image) |
475 |
{ |
476 |
SDL_FreeSurface (global_image); |
477 |
global_image = NULL; |
478 |
} |
479 |
if (global_texture) |
480 |
{ |
481 |
glDeleteTextures (1, &global_texture); |
482 |
global_texture = 0; |
483 |
} |
484 |
|
485 |
/* Destroy our GL context, etc. */ |
486 |
SDL_Quit (); |
487 |
return (0); |
488 |
} |
489 |
|
490 |
int |
491 |
main (int argc, char *argv[]) |
492 |
{ |
493 |
int i, logo; |
494 |
int numtests; |
495 |
int bpp = 0; |
496 |
int slowly; |
497 |
float gamma = 0.0; |
498 |
int noframe = 0; |
499 |
int fsaa = 0; |
500 |
|
501 |
logo = 0; |
502 |
slowly = 0; |
503 |
numtests = 1; |
504 |
for (i = 1; argv[i]; ++i) |
505 |
{ |
506 |
if (strcmp (argv[i], "-twice") == 0) |
507 |
{ |
508 |
++numtests; |
509 |
} |
510 |
if (strcmp (argv[i], "-slow") == 0) |
511 |
{ |
512 |
slowly = 1; |
513 |
} |
514 |
if (strcmp (argv[i], "-bpp") == 0) |
515 |
{ |
516 |
bpp = atoi (argv[++i]); |
517 |
} |
518 |
if (strcmp (argv[i], "-gamma") == 0) |
519 |
{ |
520 |
gamma = (float) atof (argv[++i]); |
521 |
} |
522 |
if (strcmp (argv[i], "-noframe") == 0) |
523 |
{ |
524 |
noframe = 1; |
525 |
} |
526 |
if (strcmp (argv[i], "-fsaa") == 0) |
527 |
{ |
528 |
++fsaa; |
529 |
} |
530 |
if (strncmp (argv[i], "-h", 2) == 0) |
531 |
{ |
532 |
printf |
533 |
("Usage: %s [-twice] [-logo] [-slow] [-bpp n] [-gamma n] [-noframe] [-fsaa]\n", |
534 |
argv[0]); |
535 |
exit (0); |
536 |
} |
537 |
} |
538 |
|
539 |
for (i = 0; i < numtests; ++i) |
540 |
RunGLTest (argc, argv, logo, slowly, bpp, gamma, noframe, fsaa); |
541 |
|
542 |
return 0; |
543 |
} |