… | |
… | |
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 | |
16 | static SDL_Surface *global_image = NULL; |
9 | static SDL_Surface *global_image = NULL; |
17 | static GLuint global_texture = 0; |
10 | static GLuint global_texture = 0; |
18 | |
11 | |
|
|
12 | #include "util.h" |
19 | #include "entity.h" |
13 | #include "entity.h" |
20 | |
|
|
21 | #include "txtprt_import.h" |
14 | #include "txtprt_import.h" |
22 | |
15 | |
|
|
16 | CGcontext cgc; |
|
|
17 | CGprogram vsh, fsh; |
|
|
18 | CGparameter mv, mvp; |
|
|
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 | |
23 | /**********************************************************************/ |
31 | /**********************************************************************/ |
24 | |
32 | |
25 | view camera; |
33 | view camera; |
|
|
34 | vec3 camera_velocity; |
|
|
35 | float camera_velocity_factor = 10; |
26 | |
36 | |
27 | void |
37 | void |
28 | HotKey_ToggleFullScreen (void) |
38 | HotKey_ToggleFullScreen (void) |
29 | { |
39 | { |
30 | SDL_Surface *screen; |
40 | SDL_Surface *screen; |
… | |
… | |
88 | } |
98 | } |
89 | |
99 | |
90 | printf ("focus\n"); |
100 | printf ("focus\n"); |
91 | break; |
101 | break; |
92 | |
102 | |
|
|
103 | #define VELOCITY 10 |
93 | case SDL_KEYDOWN: |
104 | case SDL_KEYDOWN: |
94 | |
|
|
95 | if (event->key.keysym.sym == SDLK_UP) |
105 | if (event->key.keysym.sym == SDLK_UP) camera_velocity.z--; |
96 | camera.p.z -= 1; |
|
|
97 | if (event->key.keysym.sym == SDLK_DOWN) |
106 | if (event->key.keysym.sym == SDLK_DOWN) camera_velocity.z++; |
98 | camera.p.z += 1; |
|
|
99 | if (event->key.keysym.sym == SDLK_LEFT) |
107 | if (event->key.keysym.sym == SDLK_LEFT) camera_velocity.x--; |
100 | camera.p.x -= 1; |
|
|
101 | if (event->key.keysym.sym == SDLK_RIGHT) |
108 | if (event->key.keysym.sym == SDLK_RIGHT) camera_velocity.x++; |
102 | camera.p.x += 1; |
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; |
103 | |
113 | |
104 | if (event->key.keysym.sym == SDLK_ESCAPE) |
114 | if (event->key.keysym.sym == SDLK_ESCAPE) |
105 | done = 1; |
115 | done = 1; |
106 | |
116 | |
107 | if ((event->key.keysym.sym == SDLK_g) && |
117 | if ((event->key.keysym.sym == SDLK_g) && |
… | |
… | |
114 | |
124 | |
115 | if ((event->key.keysym.sym == SDLK_RETURN) && |
125 | if ((event->key.keysym.sym == SDLK_RETURN) && |
116 | (event->key.keysym.mod & KMOD_ALT)) |
126 | (event->key.keysym.mod & KMOD_ALT)) |
117 | HotKey_ToggleFullScreen (); |
127 | HotKey_ToggleFullScreen (); |
118 | |
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--; |
119 | break; |
138 | break; |
120 | |
139 | |
121 | case SDL_QUIT: |
140 | case SDL_QUIT: |
122 | done = 1; |
141 | done = 1; |
123 | break; |
142 | break; |
… | |
… | |
261 | rgb_size[0] = 8; |
280 | rgb_size[0] = 8; |
262 | rgb_size[1] = 8; |
281 | rgb_size[1] = 8; |
263 | rgb_size[2] = 8; |
282 | rgb_size[2] = 8; |
264 | break; |
283 | break; |
265 | } |
284 | } |
|
|
285 | |
266 | SDL_GL_SetAttribute (SDL_GL_RED_SIZE, rgb_size[0]); |
286 | SDL_GL_SetAttribute (SDL_GL_RED_SIZE, rgb_size[0]); |
267 | SDL_GL_SetAttribute (SDL_GL_GREEN_SIZE, rgb_size[1]); |
287 | SDL_GL_SetAttribute (SDL_GL_GREEN_SIZE, rgb_size[1]); |
268 | SDL_GL_SetAttribute (SDL_GL_BLUE_SIZE, rgb_size[2]); |
288 | SDL_GL_SetAttribute (SDL_GL_BLUE_SIZE, rgb_size[2]); |
269 | SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE, 16); |
289 | SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE, 16); |
270 | SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, 1); |
290 | SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, 1); |
… | |
… | |
315 | |
335 | |
316 | /* Set the gamma for the window */ |
336 | /* Set the gamma for the window */ |
317 | if (gamma != 0.0) |
337 | if (gamma != 0.0) |
318 | SDL_SetGamma (gamma, gamma, gamma); |
338 | SDL_SetGamma (gamma, gamma, gamma); |
319 | |
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 | |
320 | camera.orig.x = camera.orig.y = camera.orig.z = 0; |
358 | camera.orig.x = camera.orig.y = camera.orig.z = 0; |
321 | camera.p = point (0, 0, 10); |
359 | camera.p = point (0, 0, 10); |
322 | camera.d = vec3 (0, 0, -1); |
360 | camera.d = vec3 (0, 0, -1); |
323 | camera.u = vec3 (0, 1, 0); |
361 | camera.u = vec3 (0, 1, 0); |
324 | camera.w = w; camera.h = h; |
362 | camera.w = w; camera.h = h; |
325 | camera.fov = 90; |
363 | camera.fov = 90; |
326 | |
364 | |
327 | glMatrixMode (GL_MODELVIEW); |
365 | glMatrixMode (GL_MODELVIEW); |
328 | glLoadIdentity (); |
366 | glLoadIdentity (); |
329 | |
367 | |
330 | //glEnable (GL_CULL_FACE); |
368 | glEnable (GL_CULL_FACE); |
331 | glEnable (GL_DEPTH_TEST); |
369 | glEnable (GL_DEPTH_TEST); |
332 | glDepthFunc (GL_LESS); |
370 | //glEnable(GL_VERTEX_PROGRAM_ARB); |
|
|
371 | //glEnable(GL_FRAGMENT_PROGRAM_ARB); |
|
|
372 | |
333 | glShadeModel (GL_SMOOTH); |
373 | glShadeModel (GL_SMOOTH); |
334 | |
374 | |
335 | glEnable (GL_LIGHTING); |
375 | glEnable (GL_LIGHTING); |
336 | //GLfloat lightc[4] = { 1, 0.1, 0.1, 1 }; |
376 | //GLfloat lightc[4] = { 1, 0.1, 0.1, 1 }; |
337 | //glLightf (GL_LIGHT0, GL_QUADRATIC_ATTENUATION); |
377 | //glLightf (GL_LIGHT0, GL_QUADRATIC_ATTENUATION); |
338 | //glLightfv (GL_LIGHT0, GL_DIFFUSE, lightc); |
378 | //glLightfv (GL_LIGHT0, GL_DIFFUSE, lightc); |
339 | glEnable (GL_LIGHT0); |
379 | glEnable (GL_LIGHT0); |
340 | |
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 | CheckCgError (); |
|
|
388 | mvp = cgGetNamedParameter (vsh, "WorldViewProj"); |
|
|
389 | CheckCgError (); |
|
|
390 | cgGLEnableProfile (CG_PROFILE_ARBVP1); |
|
|
391 | CheckCgError (); |
|
|
392 | |
|
|
393 | fsh = cgCreateProgramFromFile (cgc, CG_SOURCE, "fsh.cg", CG_PROFILE_ARBFP1, 0, 0); |
|
|
394 | cgGLLoadProgram (fsh); |
|
|
395 | cgGLEnableProfile (CG_PROFILE_ARBFP1); |
|
|
396 | |
|
|
397 | cgGLBindProgram (vsh); |
|
|
398 | cgGLBindProgram (fsh); |
|
|
399 | |
341 | /* Loop until done. */ |
400 | /* Loop until done. */ |
342 | start_time = SDL_GetTicks (); |
401 | start_time = SDL_GetTicks (); |
343 | frames = 0; |
402 | frames = 0; |
|
|
403 | |
|
|
404 | draw_context c (camera); |
|
|
405 | |
344 | while (!done) |
406 | while (!done) |
345 | { |
407 | { |
346 | GLenum gl_error; |
408 | GLenum gl_error; |
347 | char *sdl_error; |
409 | char *sdl_error; |
348 | SDL_Event event; |
410 | SDL_Event event; |
349 | |
411 | |
350 | /* Do our drawing, too. */ |
|
|
351 | glClearColor (0.0, 0.0, 0.0, 1.0); |
|
|
352 | glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
412 | glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
|
|
413 | |
|
|
414 | camera.p.x += (camera_velocity_factor * camera_velocity.x) * timer.diff; |
|
|
415 | camera.p.y += (camera_velocity_factor * camera_velocity.y) * timer.diff; |
|
|
416 | camera.p.z += (camera_velocity_factor * camera_velocity.z) * timer.diff; |
353 | |
417 | |
354 | GLfloat lightp[4]; |
418 | GLfloat lightp[4]; |
355 | lightp[0] = camera.p.x; |
419 | lightp[0] = camera.p.x; |
356 | lightp[1] = camera.p.y; |
420 | lightp[1] = camera.p.y; |
357 | lightp[2] = camera.p.z; |
421 | lightp[2] = camera.p.z; |
… | |
… | |
364 | camera.d.x = cos (ry); |
428 | camera.d.x = cos (ry); |
365 | camera.d.z = sin (ry); |
429 | camera.d.z = sin (ry); |
366 | //camera.d.y = sin (ry * 0.1); |
430 | //camera.d.y = sin (ry * 0.1); |
367 | #endif |
431 | #endif |
368 | |
432 | |
369 | draw_context c (camera); |
433 | c.mode = draw_context::DEPTH; |
|
|
434 | camera.draw (c); |
370 | c.mode = draw_context::LIGHTED; |
435 | c.mode = draw_context::LIGHTED; |
371 | camera.draw (c); |
436 | camera.draw (c); |
372 | |
437 | |
373 | SDL_GL_SwapBuffers (); |
438 | SDL_GL_SwapBuffers (); |
|
|
439 | timer.frame (); |
374 | |
440 | |
375 | /* Check for error conditions. */ |
441 | /* Check for error conditions. */ |
376 | gl_error = glGetError (); |
442 | gl_error = glGetError (); |
377 | |
443 | |
378 | if (gl_error != GL_NO_ERROR) |
444 | if (gl_error != GL_NO_ERROR) |
… | |
… | |
389 | /* Allow the user to see what's happening */ |
455 | /* Allow the user to see what's happening */ |
390 | //SDL_Delay (20); |
456 | //SDL_Delay (20); |
391 | |
457 | |
392 | /* Check if there's a pending event. */ |
458 | /* Check if there's a pending event. */ |
393 | while (SDL_PollEvent (&event)) |
459 | while (SDL_PollEvent (&event)) |
394 | { |
|
|
395 | done = HandleEvent (&event); |
460 | done = HandleEvent (&event); |
396 | } |
461 | |
|
|
462 | |
397 | ++frames; |
463 | ++frames; |
398 | } |
464 | } |
399 | |
465 | |
400 | /* Print out the frames per second */ |
466 | /* Print out the frames per second */ |
401 | this_time = SDL_GetTicks (); |
467 | this_time = SDL_GetTicks (); |
… | |
… | |
430 | int slowly; |
496 | int slowly; |
431 | float gamma = 0.0; |
497 | float gamma = 0.0; |
432 | int noframe = 0; |
498 | int noframe = 0; |
433 | int fsaa = 0; |
499 | int fsaa = 0; |
434 | |
500 | |
435 | gl_matrix m(2); |
|
|
436 | m.translate (vec3 (2,3,4)); |
|
|
437 | |
|
|
438 | // load a entity |
|
|
439 | for (int i = 0; i < 10; i++) { |
|
|
440 | txtprt_parser p; |
|
|
441 | entity_transform *f = new entity_transform; |
|
|
442 | entity *e; |
|
|
443 | try { |
|
|
444 | e = p.read ("test.blasc"); |
|
|
445 | } catch (txtprt_i_exception & e) { |
|
|
446 | cout << "ERR: " << e.msg << endl; |
|
|
447 | } |
|
|
448 | f->set (e); |
|
|
449 | f->m.translate (vec3 (i, -1, 0)); |
|
|
450 | f->m.rotate (i * 10, vec3 (0, 0, 1)); |
|
|
451 | f->show (); |
|
|
452 | } |
|
|
453 | |
|
|
454 | logo = 0; |
501 | logo = 0; |
455 | slowly = 0; |
502 | slowly = 0; |
456 | numtests = 1; |
503 | numtests = 1; |
457 | for (i = 1; argv[i]; ++i) |
504 | for (i = 1; argv[i]; ++i) |
458 | { |
505 | { |
… | |
… | |
486 | ("Usage: %s [-twice] [-logo] [-slow] [-bpp n] [-gamma n] [-noframe] [-fsaa]\n", |
533 | ("Usage: %s [-twice] [-logo] [-slow] [-bpp n] [-gamma n] [-noframe] [-fsaa]\n", |
487 | argv[0]); |
534 | argv[0]); |
488 | exit (0); |
535 | exit (0); |
489 | } |
536 | } |
490 | } |
537 | } |
|
|
538 | |
491 | for (i = 0; i < numtests; ++i) |
539 | for (i = 0; i < numtests; ++i) |
492 | { |
|
|
493 | RunGLTest (argc, argv, logo, slowly, bpp, gamma, noframe, fsaa); |
540 | RunGLTest (argc, argv, logo, slowly, bpp, gamma, noframe, fsaa); |
494 | } |
541 | |
495 | return 0; |
542 | return 0; |
496 | } |
543 | } |