1 |
#include <stdlib.h> |
2 |
#include <stdio.h> |
3 |
#include <string.h> |
4 |
#include <math.h> |
5 |
#include <sh/sh.hpp> |
6 |
|
7 |
using namespace SH; |
8 |
|
9 |
#include "SDL.h" |
10 |
|
11 |
#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 |
|
19 |
static SDL_Surface *global_image = NULL; |
20 |
static GLuint global_texture = 0; |
21 |
|
22 |
#include "entity.h" |
23 |
|
24 |
#include "txtprt_import.h" |
25 |
|
26 |
/**********************************************************************/ |
27 |
|
28 |
view camera; |
29 |
vec3 camera_velocity; |
30 |
float camera_velocity_factor = 10; |
31 |
|
32 |
ShColor3f color = ShColor3f(.5, 0.9, 0.2); |
33 |
ShPoint3f lightPos = ShPoint3f(0.0, 0.0, 10.0); |
34 |
ShMatrix4x4f mvp; |
35 |
ShProgram vsh, fsh; |
36 |
|
37 |
void |
38 |
init_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 |
|
73 |
void |
74 |
HotKey_ToggleFullScreen (void) |
75 |
{ |
76 |
SDL_Surface *screen; |
77 |
|
78 |
screen = SDL_GetVideoSurface (); |
79 |
if (SDL_WM_ToggleFullScreen (screen)) |
80 |
printf ("Toggled fullscreen mode - now %s\n", |
81 |
(screen->flags & SDL_FULLSCREEN) ? "fullscreen" : "windowed"); |
82 |
else |
83 |
printf ("Unable to toggle fullscreen mode\n"); |
84 |
} |
85 |
|
86 |
void |
87 |
HotKey_ToggleGrab (void) |
88 |
{ |
89 |
SDL_GrabMode mode; |
90 |
|
91 |
printf ("Ctrl-G: toggling input grab!\n"); |
92 |
mode = SDL_WM_GrabInput (SDL_GRAB_QUERY); |
93 |
if (mode == SDL_GRAB_ON) |
94 |
printf ("Grab was on\n"); |
95 |
else |
96 |
printf ("Grab was off\n"); |
97 |
|
98 |
mode = SDL_WM_GrabInput (mode ? SDL_GRAB_OFF : SDL_GRAB_ON); |
99 |
if (mode == SDL_GRAB_ON) |
100 |
printf ("Grab is now on\n"); |
101 |
else |
102 |
printf ("Grab is now off\n"); |
103 |
} |
104 |
|
105 |
void |
106 |
HotKey_Iconify (void) |
107 |
{ |
108 |
printf ("Ctrl-Z: iconifying window!\n"); |
109 |
SDL_WM_IconifyWindow (); |
110 |
} |
111 |
|
112 |
int |
113 |
HandleEvent (SDL_Event * event) |
114 |
{ |
115 |
int done; |
116 |
|
117 |
done = 0; |
118 |
switch (event->type) |
119 |
{ |
120 |
case SDL_ACTIVEEVENT: |
121 |
/* See what happened */ |
122 |
printf ("app %s ", event->active.gain ? "gained" : "lost"); |
123 |
if (event->active.state & SDL_APPACTIVE) |
124 |
{ |
125 |
printf ("active "); |
126 |
} |
127 |
else if (event->active.state & SDL_APPMOUSEFOCUS) |
128 |
{ |
129 |
printf ("mouse "); |
130 |
} |
131 |
else if (event->active.state & SDL_APPINPUTFOCUS) |
132 |
{ |
133 |
printf ("input "); |
134 |
} |
135 |
|
136 |
printf ("focus\n"); |
137 |
break; |
138 |
|
139 |
#define VELOCITY 10 |
140 |
case SDL_KEYDOWN: |
141 |
if (event->key.keysym.sym == SDLK_UP) camera_velocity.z--; |
142 |
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++; |
145 |
if (event->key.keysym.sym == SDLK_a) camera_velocity.y--; |
146 |
if (event->key.keysym.sym == SDLK_s) camera_velocity.y++; |
147 |
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; |
149 |
|
150 |
if (event->key.keysym.sym == SDLK_ESCAPE) |
151 |
done = 1; |
152 |
|
153 |
if ((event->key.keysym.sym == SDLK_g) && |
154 |
(event->key.keysym.mod & KMOD_CTRL)) |
155 |
HotKey_ToggleGrab (); |
156 |
|
157 |
if ((event->key.keysym.sym == SDLK_z) && |
158 |
(event->key.keysym.mod & KMOD_CTRL)) |
159 |
HotKey_Iconify (); |
160 |
|
161 |
if ((event->key.keysym.sym == SDLK_RETURN) && |
162 |
(event->key.keysym.mod & KMOD_ALT)) |
163 |
HotKey_ToggleFullScreen (); |
164 |
|
165 |
break; |
166 |
|
167 |
case SDL_KEYUP: |
168 |
if (event->key.keysym.sym == SDLK_UP) camera_velocity.z++; |
169 |
if (event->key.keysym.sym == SDLK_DOWN) camera_velocity.z--; |
170 |
if (event->key.keysym.sym == SDLK_LEFT) camera_velocity.x++; |
171 |
if (event->key.keysym.sym == SDLK_RIGHT) camera_velocity.x--; |
172 |
if (event->key.keysym.sym == SDLK_a) camera_velocity.y++; |
173 |
if (event->key.keysym.sym == SDLK_s) camera_velocity.y--; |
174 |
break; |
175 |
|
176 |
case SDL_QUIT: |
177 |
done = 1; |
178 |
break; |
179 |
} |
180 |
|
181 |
return (done); |
182 |
} |
183 |
|
184 |
/* Quick utility function for texture creation */ |
185 |
static int |
186 |
power_of_two (int input) |
187 |
{ |
188 |
int value = 1; |
189 |
|
190 |
while (value < input) |
191 |
{ |
192 |
value <<= 1; |
193 |
} |
194 |
return value; |
195 |
} |
196 |
|
197 |
GLuint |
198 |
SDL_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 |
} |
260 |
|
261 |
int |
262 |
RunGLTest (int argc, char *argv[], |
263 |
int logo, int slowly, int bpp, float gamma, int noframe, int fsaa) |
264 |
{ |
265 |
int i; |
266 |
int rgb_size[3]; |
267 |
int w = 640; |
268 |
int h = 480; |
269 |
int done = 0; |
270 |
int frames; |
271 |
Uint32 start_time, this_time; |
272 |
Uint32 video_flags; |
273 |
int value; |
274 |
|
275 |
if (SDL_Init (SDL_INIT_VIDEO) < 0) |
276 |
{ |
277 |
fprintf (stderr, "Couldn't initialize SDL: %s\n", SDL_GetError ()); |
278 |
exit (1); |
279 |
} |
280 |
|
281 |
/* See if we should detect the display depth */ |
282 |
if (bpp == 0) |
283 |
{ |
284 |
if (SDL_GetVideoInfo ()->vfmt->BitsPerPixel <= 8) |
285 |
bpp = 8; |
286 |
else |
287 |
bpp = 16; /* More doesn't seem to work */ |
288 |
} |
289 |
|
290 |
video_flags = SDL_OPENGL; |
291 |
|
292 |
for (i = 1; argv[i]; ++i) |
293 |
if (strcmp (argv[1], "-fullscreen") == 0) |
294 |
video_flags |= SDL_FULLSCREEN; |
295 |
|
296 |
if (noframe) |
297 |
video_flags |= SDL_NOFRAME; |
298 |
|
299 |
/* Initialize the display */ |
300 |
switch (bpp) |
301 |
{ |
302 |
case 8: |
303 |
rgb_size[0] = 3; |
304 |
rgb_size[1] = 3; |
305 |
rgb_size[2] = 2; |
306 |
break; |
307 |
|
308 |
case 15: |
309 |
case 16: |
310 |
rgb_size[0] = 5; |
311 |
rgb_size[1] = 5; |
312 |
rgb_size[2] = 5; |
313 |
break; |
314 |
|
315 |
default: |
316 |
rgb_size[0] = 8; |
317 |
rgb_size[1] = 8; |
318 |
rgb_size[2] = 8; |
319 |
break; |
320 |
} |
321 |
|
322 |
SDL_GL_SetAttribute (SDL_GL_RED_SIZE, rgb_size[0]); |
323 |
SDL_GL_SetAttribute (SDL_GL_GREEN_SIZE, rgb_size[1]); |
324 |
SDL_GL_SetAttribute (SDL_GL_BLUE_SIZE, rgb_size[2]); |
325 |
SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE, 16); |
326 |
SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, 1); |
327 |
|
328 |
if (fsaa) |
329 |
{ |
330 |
SDL_GL_SetAttribute (SDL_GL_MULTISAMPLEBUFFERS, 1); |
331 |
SDL_GL_SetAttribute (SDL_GL_MULTISAMPLESAMPLES, fsaa); |
332 |
} |
333 |
|
334 |
if (SDL_SetVideoMode (w, h, bpp, video_flags) == NULL) |
335 |
{ |
336 |
fprintf (stderr, "Couldn't set GL mode: %s\n", SDL_GetError ()); |
337 |
SDL_Quit (); |
338 |
exit (1); |
339 |
} |
340 |
|
341 |
printf ("Screen BPP: %d\n", SDL_GetVideoSurface ()->format->BitsPerPixel); |
342 |
printf ("\n"); |
343 |
printf ("Vendor : %s\n", glGetString (GL_VENDOR)); |
344 |
printf ("Renderer : %s\n", glGetString (GL_RENDERER)); |
345 |
printf ("Version : %s\n", glGetString (GL_VERSION)); |
346 |
printf ("Extensions : %s\n", glGetString (GL_EXTENSIONS)); |
347 |
printf ("\n"); |
348 |
|
349 |
SDL_GL_GetAttribute (SDL_GL_RED_SIZE, &value); |
350 |
printf ("SDL_GL_RED_SIZE: requested %d, got %d\n", rgb_size[0], value); |
351 |
SDL_GL_GetAttribute (SDL_GL_GREEN_SIZE, &value); |
352 |
printf ("SDL_GL_GREEN_SIZE: requested %d, got %d\n", rgb_size[1], value); |
353 |
SDL_GL_GetAttribute (SDL_GL_BLUE_SIZE, &value); |
354 |
printf ("SDL_GL_BLUE_SIZE: requested %d, got %d\n", rgb_size[2], value); |
355 |
SDL_GL_GetAttribute (SDL_GL_DEPTH_SIZE, &value); |
356 |
printf ("SDL_GL_DEPTH_SIZE: requested %d, got %d\n", bpp, value); |
357 |
SDL_GL_GetAttribute (SDL_GL_DOUBLEBUFFER, &value); |
358 |
printf ("SDL_GL_DOUBLEBUFFER: requested 1, got %d\n", value); |
359 |
|
360 |
if (fsaa) |
361 |
{ |
362 |
SDL_GL_GetAttribute (SDL_GL_MULTISAMPLEBUFFERS, &value); |
363 |
printf ("SDL_GL_MULTISAMPLEBUFFERS: requested 1, got %d\n", value); |
364 |
SDL_GL_GetAttribute (SDL_GL_MULTISAMPLESAMPLES, &value); |
365 |
printf ("SDL_GL_MULTISAMPLESAMPLES: requested %d, got %d\n", fsaa, |
366 |
value); |
367 |
} |
368 |
|
369 |
/* Set the window manager title bar */ |
370 |
SDL_WM_SetCaption ("libgender rendering test", "gendertest"); |
371 |
|
372 |
/* Set the gamma for the window */ |
373 |
if (gamma != 0.0) |
374 |
SDL_SetGamma (gamma, gamma, gamma); |
375 |
|
376 |
// load a entity |
377 |
for (int i = 0; i < 7; i++) |
378 |
{ |
379 |
txtprt_parser p; |
380 |
entity_transform *f = new entity_transform; |
381 |
entity *e; |
382 |
try |
383 |
{ |
384 |
e = p.read ("test.blasc"); |
385 |
} catch (txtprt_i_exception & e) |
386 |
{ |
387 |
cout << "ERR: " << e.msg << endl; |
388 |
} |
389 |
f->set (e); |
390 |
f->update (gl_matrix::translation (vec3 (0, -1, -i*5))); |
391 |
f->show (); |
392 |
} |
393 |
|
394 |
camera.orig.x = camera.orig.y = camera.orig.z = 0; |
395 |
camera.p = point (0, 0, 10); |
396 |
camera.d = vec3 (0, 0, -1); |
397 |
camera.u = vec3 (0, 1, 0); |
398 |
camera.w = w; camera.h = h; |
399 |
camera.fov = 90; |
400 |
|
401 |
glMatrixMode (GL_MODELVIEW); |
402 |
glLoadIdentity (); |
403 |
|
404 |
glEnable (GL_CULL_FACE); |
405 |
glEnable (GL_DEPTH_TEST); |
406 |
shInit(); |
407 |
shSetBackend("arb"); |
408 |
glEnable(GL_VERTEX_PROGRAM_ARB); |
409 |
glEnable(GL_FRAGMENT_PROGRAM_ARB); |
410 |
|
411 |
init_shaders(); |
412 |
shBind(vsh); |
413 |
shBind(fsh); |
414 |
glShadeModel (GL_SMOOTH); |
415 |
|
416 |
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); |
421 |
|
422 |
/* Loop until done. */ |
423 |
start_time = SDL_GetTicks (); |
424 |
frames = 0; |
425 |
|
426 |
draw_context c (camera); |
427 |
|
428 |
while (!done) |
429 |
{ |
430 |
GLenum gl_error; |
431 |
char *sdl_error; |
432 |
SDL_Event event; |
433 |
|
434 |
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
435 |
|
436 |
camera.p.x += (camera_velocity_factor * camera_velocity.x) * timer.diff; |
437 |
camera.p.y += (camera_velocity_factor * camera_velocity.y) * timer.diff; |
438 |
camera.p.z += (camera_velocity_factor * camera_velocity.z) * timer.diff; |
439 |
|
440 |
GLfloat lightp[4]; |
441 |
lightp[0] = camera.p.x; |
442 |
lightp[1] = camera.p.y; |
443 |
lightp[2] = camera.p.z; |
444 |
lightp[3] = 1; |
445 |
glLightfv (GL_LIGHT0, GL_POSITION, lightp); |
446 |
|
447 |
#if 0 |
448 |
static GLfloat ry; |
449 |
ry += 0.001; |
450 |
camera.d.x = cos (ry); |
451 |
camera.d.z = sin (ry); |
452 |
//camera.d.y = sin (ry * 0.1); |
453 |
#endif |
454 |
|
455 |
c.mode = draw_context::DEPTH; |
456 |
camera.draw (c); |
457 |
c.mode = draw_context::LIGHTED; |
458 |
camera.draw (c); |
459 |
|
460 |
SDL_GL_SwapBuffers (); |
461 |
timer.frame (); |
462 |
|
463 |
/* Check for error conditions. */ |
464 |
gl_error = glGetError (); |
465 |
|
466 |
if (gl_error != GL_NO_ERROR) |
467 |
fprintf (stderr, "testgl: OpenGL error: %d\n", gl_error); |
468 |
|
469 |
sdl_error = SDL_GetError (); |
470 |
|
471 |
if (sdl_error[0] != '\0') |
472 |
{ |
473 |
fprintf (stderr, "testgl: SDL error '%s'\n", sdl_error); |
474 |
SDL_ClearError (); |
475 |
} |
476 |
|
477 |
/* Allow the user to see what's happening */ |
478 |
//SDL_Delay (20); |
479 |
|
480 |
/* Check if there's a pending event. */ |
481 |
while (SDL_PollEvent (&event)) |
482 |
done = HandleEvent (&event); |
483 |
|
484 |
|
485 |
++frames; |
486 |
} |
487 |
|
488 |
/* Print out the frames per second */ |
489 |
this_time = SDL_GetTicks (); |
490 |
if (this_time != start_time) |
491 |
{ |
492 |
printf ("%2.2f FPS\n", |
493 |
((float) frames / (this_time - start_time)) * 1000.0); |
494 |
} |
495 |
|
496 |
if (global_image) |
497 |
{ |
498 |
SDL_FreeSurface (global_image); |
499 |
global_image = NULL; |
500 |
} |
501 |
if (global_texture) |
502 |
{ |
503 |
glDeleteTextures (1, &global_texture); |
504 |
global_texture = 0; |
505 |
} |
506 |
|
507 |
/* Destroy our GL context, etc. */ |
508 |
SDL_Quit (); |
509 |
return (0); |
510 |
} |
511 |
|
512 |
int |
513 |
main (int argc, char *argv[]) |
514 |
{ |
515 |
int i, logo; |
516 |
int numtests; |
517 |
int bpp = 0; |
518 |
int slowly; |
519 |
float gamma = 0.0; |
520 |
int noframe = 0; |
521 |
int fsaa = 0; |
522 |
|
523 |
logo = 0; |
524 |
slowly = 0; |
525 |
numtests = 1; |
526 |
for (i = 1; argv[i]; ++i) |
527 |
{ |
528 |
if (strcmp (argv[i], "-twice") == 0) |
529 |
{ |
530 |
++numtests; |
531 |
} |
532 |
if (strcmp (argv[i], "-slow") == 0) |
533 |
{ |
534 |
slowly = 1; |
535 |
} |
536 |
if (strcmp (argv[i], "-bpp") == 0) |
537 |
{ |
538 |
bpp = atoi (argv[++i]); |
539 |
} |
540 |
if (strcmp (argv[i], "-gamma") == 0) |
541 |
{ |
542 |
gamma = (float) atof (argv[++i]); |
543 |
} |
544 |
if (strcmp (argv[i], "-noframe") == 0) |
545 |
{ |
546 |
noframe = 1; |
547 |
} |
548 |
if (strcmp (argv[i], "-fsaa") == 0) |
549 |
{ |
550 |
++fsaa; |
551 |
} |
552 |
if (strncmp (argv[i], "-h", 2) == 0) |
553 |
{ |
554 |
printf |
555 |
("Usage: %s [-twice] [-logo] [-slow] [-bpp n] [-gamma n] [-noframe] [-fsaa]\n", |
556 |
argv[0]); |
557 |
exit (0); |
558 |
} |
559 |
} |
560 |
|
561 |
for (i = 0; i < numtests; ++i) |
562 |
RunGLTest (argc, argv, logo, slowly, bpp, gamma, noframe, fsaa); |
563 |
|
564 |
return 0; |
565 |
} |