… | |
… | |
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); |
… | |
… | |
319 | |
339 | |
320 | // load a entity |
340 | // load a entity |
321 | for (int i = 0; i < 7; i++) |
341 | for (int i = 0; i < 7; i++) |
322 | { |
342 | { |
323 | txtprt_parser p; |
343 | txtprt_parser p; |
324 | entity_anim *f = new entity_anim; |
344 | entity_transform *f = new entity_transform; |
325 | entity *e; |
345 | entity *e; |
326 | try |
346 | try |
327 | { |
347 | { |
328 | e = p.read ("test.blasc"); |
348 | e = p.read ("test.blasc"); |
329 | } catch (txtprt_i_exception & e) |
349 | } catch (txtprt_i_exception & e) |
330 | { |
350 | { |
331 | cout << "ERR: " << e.msg << endl; |
351 | cout << "ERR: " << e.msg << endl; |
332 | } |
352 | } |
333 | f->set (e); |
353 | f->set (e); |
334 | f->m.translate (vec3 (i, -1, 0)); |
354 | f->update (matrix::translation (vec3 (0, -1, -i*5))); |
335 | f->vx = 0; |
|
|
336 | f->vy = 0; |
|
|
337 | f->vz = 0; |
|
|
338 | f->show (); |
355 | f->show (); |
339 | } |
356 | } |
340 | |
357 | |
341 | camera.orig.x = camera.orig.y = camera.orig.z = 0; |
358 | camera.orig.x = camera.orig.y = camera.orig.z = 0; |
342 | camera.p = point (0, 0, 10); |
359 | camera.p = point (0, 0, 10); |
… | |
… | |
348 | glMatrixMode (GL_MODELVIEW); |
365 | glMatrixMode (GL_MODELVIEW); |
349 | glLoadIdentity (); |
366 | glLoadIdentity (); |
350 | |
367 | |
351 | glEnable (GL_CULL_FACE); |
368 | glEnable (GL_CULL_FACE); |
352 | glEnable (GL_DEPTH_TEST); |
369 | glEnable (GL_DEPTH_TEST); |
|
|
370 | //glEnable(GL_VERTEX_PROGRAM_ARB); |
|
|
371 | //glEnable(GL_FRAGMENT_PROGRAM_ARB); |
|
|
372 | |
353 | glShadeModel (GL_SMOOTH); |
373 | glShadeModel (GL_SMOOTH); |
354 | |
374 | |
355 | glEnable (GL_LIGHTING); |
375 | glEnable (GL_LIGHTING); |
356 | //GLfloat lightc[4] = { 1, 0.1, 0.1, 1 }; |
376 | //GLfloat lightc[4] = { 1, 0.1, 0.1, 1 }; |
357 | //glLightf (GL_LIGHT0, GL_QUADRATIC_ATTENUATION); |
377 | //glLightf (GL_LIGHT0, GL_QUADRATIC_ATTENUATION); |
358 | //glLightfv (GL_LIGHT0, GL_DIFFUSE, lightc); |
378 | //glLightfv (GL_LIGHT0, GL_DIFFUSE, lightc); |
359 | glEnable (GL_LIGHT0); |
379 | glEnable (GL_LIGHT0); |
360 | |
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 | |
361 | /* Loop until done. */ |
400 | /* Loop until done. */ |
362 | start_time = SDL_GetTicks (); |
401 | start_time = SDL_GetTicks (); |
363 | frames = 0; |
402 | frames = 0; |
364 | |
403 | |
365 | draw_context c (camera); |
404 | draw_context c (camera); |
… | |
… | |
368 | { |
407 | { |
369 | GLenum gl_error; |
408 | GLenum gl_error; |
370 | char *sdl_error; |
409 | char *sdl_error; |
371 | SDL_Event event; |
410 | SDL_Event event; |
372 | |
411 | |
373 | /* Do our drawing, too. */ |
|
|
374 | glClearColor (0.0, 0.0, 0.0, 1.0); |
|
|
375 | 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; |
376 | |
417 | |
377 | GLfloat lightp[4]; |
418 | GLfloat lightp[4]; |
378 | lightp[0] = camera.p.x; |
419 | lightp[0] = camera.p.x; |
379 | lightp[1] = camera.p.y; |
420 | lightp[1] = camera.p.y; |
380 | lightp[2] = camera.p.z; |
421 | lightp[2] = camera.p.z; |
… | |
… | |
389 | //camera.d.y = sin (ry * 0.1); |
430 | //camera.d.y = sin (ry * 0.1); |
390 | #endif |
431 | #endif |
391 | |
432 | |
392 | c.mode = draw_context::DEPTH; |
433 | c.mode = draw_context::DEPTH; |
393 | camera.draw (c); |
434 | camera.draw (c); |
394 | c.drawn.clear (); |
|
|
395 | c.mode = draw_context::LIGHTED; |
435 | c.mode = draw_context::LIGHTED; |
396 | camera.draw (c); |
436 | camera.draw (c); |
397 | |
437 | |
398 | SDL_GL_SwapBuffers (); |
438 | SDL_GL_SwapBuffers (); |
399 | timer.frame (); |
439 | timer.frame (); |
… | |
… | |
415 | /* Allow the user to see what's happening */ |
455 | /* Allow the user to see what's happening */ |
416 | //SDL_Delay (20); |
456 | //SDL_Delay (20); |
417 | |
457 | |
418 | /* Check if there's a pending event. */ |
458 | /* Check if there's a pending event. */ |
419 | while (SDL_PollEvent (&event)) |
459 | while (SDL_PollEvent (&event)) |
420 | { |
|
|
421 | done = HandleEvent (&event); |
460 | done = HandleEvent (&event); |
422 | } |
461 | |
|
|
462 | |
423 | ++frames; |
463 | ++frames; |
424 | } |
464 | } |
425 | |
465 | |
426 | /* Print out the frames per second */ |
466 | /* Print out the frames per second */ |
427 | this_time = SDL_GetTicks (); |
467 | this_time = SDL_GetTicks (); |