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 | |
5 | |
|
|
6 | #include "opengl.h" |
|
|
7 | |
6 | #include "SDL.h" |
8 | #include "SDL.h" |
7 | #include "SDL_opengl.h" |
9 | #include "SDL_opengl.h" |
8 | |
10 | |
9 | static SDL_Surface *global_image = NULL; |
11 | static SDL_Surface *global_image = NULL; |
10 | static GLuint global_texture = 0; |
12 | static GLuint global_texture = 0; |
11 | |
13 | |
12 | #include "util.h" |
14 | #include "util.h" |
13 | #include "entity.h" |
15 | #include "entity.h" |
14 | #include "txtprt_import.h" |
16 | #include "txtprt_import.h" |
15 | |
17 | |
16 | CGcontext cgc; |
|
|
17 | CGprogram vsh, fsh; |
|
|
18 | CGparameter mv, mvp, lightpos; |
|
|
19 | CGprofile vsh_profile, fsh_profile; |
|
|
20 | |
|
|
21 | static 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 | } |
|
|
31 | |
|
|
32 | /**********************************************************************/ |
18 | /**********************************************************************/ |
33 | |
19 | |
34 | view camera; |
20 | view camera; |
35 | vec3 camera_velocity; |
21 | vec3 camera_velocity; |
|
|
22 | float camera_angle, camera_angle2, camera_velocity_angle, camera_velocity_angle2; |
36 | float camera_velocity_factor = 20; |
23 | float camera_velocity_factor = 80; |
37 | |
24 | |
38 | void |
25 | void |
39 | HotKey_ToggleFullScreen (void) |
26 | HotKey_ToggleFullScreen (void) |
40 | { |
27 | { |
41 | SDL_Surface *screen; |
28 | SDL_Surface *screen; |
… | |
… | |
84 | { |
71 | { |
85 | case SDL_ACTIVEEVENT: |
72 | case SDL_ACTIVEEVENT: |
86 | /* See what happened */ |
73 | /* See what happened */ |
87 | printf ("app %s ", event->active.gain ? "gained" : "lost"); |
74 | printf ("app %s ", event->active.gain ? "gained" : "lost"); |
88 | if (event->active.state & SDL_APPACTIVE) |
75 | if (event->active.state & SDL_APPACTIVE) |
89 | { |
|
|
90 | printf ("active "); |
76 | printf ("active "); |
91 | } |
|
|
92 | else if (event->active.state & SDL_APPMOUSEFOCUS) |
77 | else if (event->active.state & SDL_APPMOUSEFOCUS) |
93 | { |
|
|
94 | printf ("mouse "); |
78 | printf ("mouse "); |
95 | } |
|
|
96 | else if (event->active.state & SDL_APPINPUTFOCUS) |
79 | else if (event->active.state & SDL_APPINPUTFOCUS) |
97 | { |
|
|
98 | printf ("input "); |
80 | printf ("input "); |
99 | } |
|
|
100 | |
81 | |
101 | printf ("focus\n"); |
82 | printf ("focus\n"); |
102 | break; |
83 | break; |
103 | |
84 | |
104 | case SDL_KEYDOWN: |
85 | case SDL_KEYDOWN: |
105 | if (event->key.keysym.sym == SDLK_UP) camera_velocity.z--; |
86 | if (event->key.keysym.sym == SDLK_UP) camera_velocity.z--; |
106 | if (event->key.keysym.sym == SDLK_DOWN) camera_velocity.z++; |
87 | 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++; |
88 | if (event->key.keysym.sym == SDLK_RIGHT) camera_velocity_angle++; |
|
|
89 | if (event->key.keysym.sym == SDLK_LEFT) camera_velocity_angle--; |
|
|
90 | if (event->key.keysym.sym == SDLK_PAGEUP) camera_velocity_angle2++; |
|
|
91 | if (event->key.keysym.sym == SDLK_PAGEDOWN) camera_velocity_angle2--; |
109 | if (event->key.keysym.sym == SDLK_a) camera_velocity.y--; |
92 | if (event->key.keysym.sym == SDLK_e) camera_velocity.y--; |
110 | if (event->key.keysym.sym == SDLK_s) camera_velocity.y++; |
93 | if (event->key.keysym.sym == SDLK_q) camera_velocity.y++; |
111 | if (event->key.keysym.sym == SDLK_v) camera_velocity_factor *= 1.5; |
94 | 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; |
95 | if (event->key.keysym.sym == SDLK_b) camera_velocity_factor /= 1.5; |
113 | |
96 | |
114 | if (event->key.keysym.sym == SDLK_ESCAPE) |
97 | if (event->key.keysym.sym == SDLK_ESCAPE) |
115 | done = 1; |
98 | done = 1; |
116 | |
99 | |
117 | if ((event->key.keysym.sym == SDLK_g) && |
100 | if ((event->key.keysym.sym == SDLK_g) && |
… | |
… | |
129 | break; |
112 | break; |
130 | |
113 | |
131 | case SDL_KEYUP: |
114 | case SDL_KEYUP: |
132 | if (event->key.keysym.sym == SDLK_UP) camera_velocity.z++; |
115 | if (event->key.keysym.sym == SDLK_UP) camera_velocity.z++; |
133 | if (event->key.keysym.sym == SDLK_DOWN) camera_velocity.z--; |
116 | if (event->key.keysym.sym == SDLK_DOWN) camera_velocity.z--; |
134 | if (event->key.keysym.sym == SDLK_LEFT) camera_velocity.x++; |
117 | if (event->key.keysym.sym == SDLK_LEFT) camera_velocity_angle++; |
135 | if (event->key.keysym.sym == SDLK_RIGHT) camera_velocity.x--; |
118 | if (event->key.keysym.sym == SDLK_RIGHT) camera_velocity_angle--; |
|
|
119 | if (event->key.keysym.sym == SDLK_PAGEUP) camera_velocity_angle2--; |
|
|
120 | if (event->key.keysym.sym == SDLK_PAGEDOWN) camera_velocity_angle2++; |
136 | if (event->key.keysym.sym == SDLK_a) camera_velocity.y++; |
121 | if (event->key.keysym.sym == SDLK_e) camera_velocity.y++; |
137 | if (event->key.keysym.sym == SDLK_s) camera_velocity.y--; |
122 | if (event->key.keysym.sym == SDLK_q) camera_velocity.y--; |
138 | break; |
123 | break; |
139 | |
124 | |
140 | case SDL_QUIT: |
125 | case SDL_QUIT: |
141 | done = 1; |
126 | done = 1; |
142 | break; |
127 | break; |
143 | } |
128 | } |
144 | |
129 | |
145 | return (done); |
130 | return (done); |
146 | } |
131 | } |
147 | |
132 | |
|
|
133 | void draw_floor (int size, int dx, int dy, int dz) |
|
|
134 | { |
|
|
135 | int x, z, ry; |
|
|
136 | |
|
|
137 | for (x = 0; x < 10; x++) |
|
|
138 | { |
|
|
139 | for (z = 0; z < 10; z++) |
|
|
140 | { |
|
|
141 | vector<vertex_t2f_n3f_v3f> pts; |
|
|
142 | pts.push_back (vertex_t2f_n3f_v3f (point ( 0, 0, 0), vec3 (0, 1, 0), tex2 (0, 0))); |
|
|
143 | pts.push_back (vertex_t2f_n3f_v3f (point ( 0, 0, size), vec3 (0, 1, 0), tex2 (0, 1))); |
|
|
144 | pts.push_back (vertex_t2f_n3f_v3f (point (size, 0, size), vec3 (0, 1, 0), tex2 (1, 1))); |
|
|
145 | pts.push_back (vertex_t2f_n3f_v3f (point (size, 0, 0), vec3 (0, 1, 0), tex2 (1, 0))); |
|
|
146 | |
|
|
147 | geometry_quads *q = new geometry_quads; |
|
|
148 | q->m = new simple_material; |
|
|
149 | q->set (pts); |
|
|
150 | entity *e = new entity (q); |
|
|
151 | e->move (vec3 (dx + x * size, dy, dz + z * size)); |
|
|
152 | e->show (); |
|
|
153 | } |
|
|
154 | } |
|
|
155 | } |
|
|
156 | |
|
|
157 | void draw_test_nurb () |
|
|
158 | { |
|
|
159 | geometry_nurbs *q = new geometry_nurbs; |
|
|
160 | q->set (); |
|
|
161 | entity *e = new entity (q); |
|
|
162 | e->move (vec3 (10, 3, -4)); |
|
|
163 | e->show (); |
|
|
164 | } |
|
|
165 | |
|
|
166 | void fisch (vec3 center, GLfloat radius, int depth) |
|
|
167 | { |
|
|
168 | entity *planet = new entity (new geometry_sphere (radius)); |
|
|
169 | planet->move (center); |
|
|
170 | planet->show (); |
|
|
171 | |
|
|
172 | if (--depth) |
|
|
173 | { |
|
|
174 | GLfloat r2 = radius * .1F; |
|
|
175 | GLfloat r3 = radius + r2; |
|
|
176 | |
|
|
177 | fisch (center + vec3 (r3, 0, 0), r2, depth); |
|
|
178 | fisch (center + vec3 (0, 0, r3), r2, depth); |
|
|
179 | fisch (center + vec3 (-r3, 0, 0), r2, depth); |
|
|
180 | } |
|
|
181 | } |
|
|
182 | |
|
|
183 | extern void draw_level (); |
148 | int |
184 | int |
149 | RunGLTest (int argc, char *argv[], |
185 | RunGLTest (int argc, char *argv[], |
150 | int logo, int slowly, int bpp, float gamma, int noframe, int fsaa) |
186 | int logo, int slowly, int bpp, float gamma, int noframe, int fsaa) |
151 | { |
187 | { |
152 | int i; |
188 | int i; |
153 | int rgb_size[3]; |
189 | int rgb_size[3]; |
154 | int w = 640; |
190 | int w = 640; |
155 | int h = 480; |
191 | int h = 480; |
156 | int done = 0; |
192 | int done = 0; |
157 | int frames; |
193 | int frames; |
158 | Uint32 start_time, this_time; |
|
|
159 | Uint32 video_flags; |
194 | Uint32 video_flags; |
160 | int value; |
195 | int value; |
|
|
196 | GLenum gl_error; |
161 | |
197 | |
162 | if (SDL_Init (SDL_INIT_VIDEO) < 0) |
198 | if (SDL_Init (SDL_INIT_VIDEO) < 0) |
163 | { |
199 | { |
164 | fprintf (stderr, "Couldn't initialize SDL: %s\n", SDL_GetError ()); |
200 | fprintf (stderr, "Couldn't initialize SDL: %s\n", SDL_GetError ()); |
165 | exit (1); |
201 | exit (1); |
… | |
… | |
258 | |
294 | |
259 | /* Set the gamma for the window */ |
295 | /* Set the gamma for the window */ |
260 | if (gamma != 0.0) |
296 | if (gamma != 0.0) |
261 | SDL_SetGamma (gamma, gamma, gamma); |
297 | SDL_SetGamma (gamma, gamma, gamma); |
262 | |
298 | |
263 | // load a entity |
299 | entity *planet = new entity (new geometry_sphere (10)); |
|
|
300 | planet->move (vec3 (0, 0, -20)); |
|
|
301 | planet->show (); |
|
|
302 | |
|
|
303 | #if 1 |
264 | for (int i = 0; i < 7; i++) |
304 | for (int i = 0; i < 20; i++) |
265 | { |
305 | { |
|
|
306 | // load a entity |
266 | txtprt_parser p; |
307 | txtprt_parser p; |
267 | entity_transform *f = new entity_transform; |
308 | geometry *g; |
268 | entity *e; |
|
|
269 | try |
309 | try |
270 | { |
310 | { |
271 | e = p.read ("test.blasc"); |
311 | g = p.read ("test.blasc"); |
|
|
312 | } |
272 | } catch (txtprt_i_exception & e) |
313 | catch (txtprt_i_exception & e) |
273 | { |
314 | { |
274 | cout << "ERR: " << e.msg << endl; |
315 | cout << "ERR: " << e.msg << endl; |
275 | } |
316 | } |
276 | f->set (e); |
317 | |
277 | f->update (matrix::translation (vec3 (0, -1, -i*5))); |
318 | entity *e = new entity (g); |
|
|
319 | e->move (vec3 (i*5, -3, -i*10)); |
278 | f->show (); |
320 | e->show (); |
|
|
321 | } |
|
|
322 | |
279 | } |
323 | { |
|
|
324 | fisch (vec3 (0, 0, -2e9), 1e9, 8); |
|
|
325 | //entity *planet = new entity (new geometry_sphere (1e9)); |
|
|
326 | //planet->move (vec3 (0, 0, -1.5e9)); |
|
|
327 | //planet->show (); |
|
|
328 | } |
280 | |
329 | |
|
|
330 | { |
|
|
331 | entity *planet = new entity (new geometry_sphere (4e15)); |
|
|
332 | planet->move (vec3 (0, 0, 1e17)); |
|
|
333 | planet->show (); |
|
|
334 | } |
|
|
335 | |
|
|
336 | draw_floor (10, -500, -10, -1000); |
|
|
337 | draw_level (); |
|
|
338 | //draw_test_nurb (); |
|
|
339 | #endif |
|
|
340 | |
|
|
341 | //camera.orig.x = 108; camera.orig.y = 0; camera.orig.z = -368; |
281 | camera.orig.x = camera.orig.y = camera.orig.z = 0; |
342 | camera.orig.x = 0; camera.orig.y = 0; camera.orig.z = 0; |
282 | camera.p = point (0, 0, 10); |
343 | camera.p = point (0, 0, 10); |
283 | camera.d = vec3 (0, 0, -1); |
344 | camera.d = vec3 (0, 0, -1); |
284 | camera.u = vec3 (0, 1, 0); |
345 | camera.u = vec3 (0, 1, 0); |
285 | camera.w = w; camera.h = h; |
346 | camera.w = w; camera.h = h; |
286 | camera.fov = 90; |
347 | camera.fov = 40.; |
287 | |
|
|
288 | glMatrixMode (GL_MODELVIEW); |
|
|
289 | glLoadIdentity (); |
|
|
290 | |
348 | |
291 | glEnable (GL_CULL_FACE); |
349 | glEnable (GL_CULL_FACE); |
292 | glEnable (GL_DEPTH_TEST); |
350 | glDisable (GL_ALPHA_TEST); |
|
|
351 | glDisable (GL_BLEND); |
293 | |
352 | |
294 | glShadeModel (GL_SMOOTH); |
353 | init_shaders (); |
295 | |
354 | |
296 | glEnable (GL_LIGHTING); |
355 | osama_material osa_mat; |
297 | //GLfloat lightc[4] = { 1, 0.1, 0.1, 1 }; |
|
|
298 | //glLightf (GL_LIGHT0, GL_QUADRATIC_ATTENUATION); |
|
|
299 | //glLightfv (GL_LIGHT0, GL_DIFFUSE, lightc); |
|
|
300 | glEnable (GL_LIGHT0); |
|
|
301 | |
|
|
302 | cgc = cgCreateContext (); |
|
|
303 | |
|
|
304 | vsh_profile = CG_PROFILE_ARBVP1; |
|
|
305 | //if (cgGLIsProfileSupported (CG_PROFILE_VP30)) vsh_profile = CG_PROFILE_VP30; |
|
|
306 | //if (cgGLIsProfileSupported (CG_PROFILE_VP40)) vsh_profile = CG_PROFILE_VP40; |
|
|
307 | fsh_profile = CG_PROFILE_ARBFP1; |
|
|
308 | //if (cgGLIsProfileSupported (CG_PROFILE_FP30)) fsh_profile = CG_PROFILE_FP30; |
|
|
309 | //if (cgGLIsProfileSupported (CG_PROFILE_FP40)) fsh_profile = CG_PROFILE_FP40; |
|
|
310 | |
|
|
311 | vsh = cgCreateProgramFromFile (cgc, CG_SOURCE, "vsh.cg", vsh_profile, 0, 0); |
|
|
312 | CheckCgError (); |
|
|
313 | cgGLLoadProgram (vsh); |
|
|
314 | CheckCgError (); |
|
|
315 | mv = cgGetNamedParameter (vsh, "WorldProj"); |
|
|
316 | mvp = cgGetNamedParameter (vsh, "WorldViewProj"); |
|
|
317 | lightpos = cgGetNamedParameter (vsh, "LightPos"); |
|
|
318 | CheckCgError (); |
|
|
319 | |
|
|
320 | fsh = cgCreateProgramFromFile (cgc, CG_SOURCE, "fsh.cg", fsh_profile, 0, 0); |
|
|
321 | CheckCgError (); |
|
|
322 | cgGLLoadProgram (fsh); |
|
|
323 | CheckCgError (); |
|
|
324 | |
|
|
325 | cgGLBindProgram (vsh); |
|
|
326 | CheckCgError (); |
|
|
327 | cgGLBindProgram (fsh); |
|
|
328 | CheckCgError (); |
|
|
329 | |
356 | |
330 | /* Loop until done. */ |
357 | /* Loop until done. */ |
331 | start_time = SDL_GetTicks (); |
|
|
332 | frames = 0; |
358 | frames = 0; |
333 | |
359 | |
334 | while (!done) |
360 | while (!done) |
335 | { |
361 | { |
336 | GLenum gl_error; |
|
|
337 | char *sdl_error; |
362 | char *sdl_error; |
338 | SDL_Event event; |
363 | SDL_Event event; |
339 | |
364 | |
340 | glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
365 | camera_angle += 180 * camera_velocity_angle * timer.diff; |
|
|
366 | camera_angle2 += 180 * camera_velocity_angle2 * timer.diff; |
341 | |
367 | |
342 | camera.p.x += (camera_velocity_factor * camera_velocity.x) * timer.diff; |
368 | vec3 geradeaus = matrix::rotation (-camera_angle, vec3 (0, 1, 0)) * vec3 (0, 0, -1); |
343 | camera.p.y += (camera_velocity_factor * camera_velocity.y) * timer.diff; |
369 | vec3 right = matrix::rotation (90., vec3 (0, 1, 0)) * geradeaus; |
344 | camera.p.z += (camera_velocity_factor * camera_velocity.z) * timer.diff; |
|
|
345 | |
370 | |
346 | cgGLSetParameter4f (lightpos, camera.p.x, camera.p.y, camera.p.z, 1); |
371 | camera.d = matrix::rotation (camera_angle2, right) * geradeaus; |
|
|
372 | camera.u = cross (camera.d, right); |
347 | |
373 | |
348 | #if 0 |
374 | camera.p = camera.p - camera.d * (camera_velocity_factor * timer.diff) * camera_velocity.z; |
349 | static GLfloat ry; |
375 | camera.p = camera.p - camera.u * (camera_velocity_factor * timer.diff) * camera_velocity.y; |
350 | ry += 0.001; |
376 | |
351 | camera.d.x = cos (ry); |
377 | osa_mat.begin (); |
352 | camera.d.z = sin (ry); |
|
|
353 | //camera.d.y = sin (ry * 0.1); |
|
|
354 | #endif |
|
|
355 | |
378 | |
356 | camera.begin (); |
379 | camera.begin (); |
357 | camera.pass (view::DEPTH); |
380 | camera.render (view::DEPTH); |
|
|
381 | camera.render (view::POSTDEPTH); |
358 | camera.pass (view::LIGHTED); |
382 | camera.render (view::LIGHTED); |
359 | camera.end (); |
383 | camera.end (); |
360 | |
384 | |
361 | SDL_GL_SwapBuffers (); |
385 | SDL_GL_SwapBuffers (); |
362 | timer.frame (); |
386 | timer.frame (); |
363 | |
387 | |
|
|
388 | osa_mat.end (); |
|
|
389 | |
|
|
390 | #if 0 |
364 | /* Check for error conditions. */ |
391 | /* Check for error conditions. */ |
365 | gl_error = glGetError (); |
392 | gl_error = glGetError (); |
366 | |
393 | |
367 | if (gl_error != GL_NO_ERROR) |
|
|
368 | fprintf (stderr, "testgl: OpenGL error: %d\n", gl_error); |
394 | if (gl_error != GL_NO_ERROR) fprintf (stderr, "testgl: OpenGL error: %d\n", gl_error); |
369 | |
395 | |
370 | sdl_error = SDL_GetError (); |
396 | sdl_error = SDL_GetError (); |
371 | |
397 | |
372 | if (sdl_error[0] != '\0') |
398 | if (sdl_error[0] != '\0') |
373 | { |
399 | { |
374 | fprintf (stderr, "testgl: SDL error '%s'\n", sdl_error); |
400 | fprintf (stderr, "testgl: SDL error '%s'\n", sdl_error); |
375 | SDL_ClearError (); |
401 | SDL_ClearError (); |
376 | } |
402 | } |
377 | |
403 | |
378 | /* Allow the user to see what's happening */ |
404 | /* Allow the user to see what's happening */ |
379 | //SDL_Delay (20); |
405 | //SDL_Delay (40); |
|
|
406 | #endif |
380 | |
407 | |
381 | /* Check if there's a pending event. */ |
408 | /* Check if there's a pending event. */ |
382 | while (SDL_PollEvent (&event)) |
409 | while (SDL_PollEvent (&event)) |
383 | done = HandleEvent (&event); |
410 | done = HandleEvent (&event); |
384 | |
411 | |
385 | |
412 | |
386 | ++frames; |
413 | ++frames; |
387 | } |
414 | } |
388 | |
415 | |
389 | /* Print out the frames per second */ |
|
|
390 | this_time = SDL_GetTicks (); |
|
|
391 | if (this_time != start_time) |
|
|
392 | { |
|
|
393 | printf ("%2.2f FPS\n", |
|
|
394 | ((float) frames / (this_time - start_time)) * 1000.0); |
|
|
395 | } |
|
|
396 | |
|
|
397 | if (global_image) |
416 | if (global_image) |
398 | { |
417 | { |
399 | SDL_FreeSurface (global_image); |
418 | SDL_FreeSurface (global_image); |
400 | global_image = NULL; |
419 | global_image = NULL; |
401 | } |
420 | } |
… | |
… | |
407 | |
426 | |
408 | /* Destroy our GL context, etc. */ |
427 | /* Destroy our GL context, etc. */ |
409 | SDL_Quit (); |
428 | SDL_Quit (); |
410 | return (0); |
429 | return (0); |
411 | } |
430 | } |
412 | |
|
|
413 | int |
431 | int |
414 | main (int argc, char *argv[]) |
432 | main (int argc, char *argv[]) |
415 | { |
433 | { |
416 | int i, logo; |
434 | int i, logo; |
417 | int numtests; |
435 | int numtests; |
… | |
… | |
419 | int slowly; |
437 | int slowly; |
420 | float gamma = 0.0; |
438 | float gamma = 0.0; |
421 | int noframe = 0; |
439 | int noframe = 0; |
422 | int fsaa = 0; |
440 | int fsaa = 0; |
423 | |
441 | |
|
|
442 | shader::debdebdebdebug ();//D |
|
|
443 | |
424 | logo = 0; |
444 | logo = 0; |
425 | slowly = 0; |
445 | slowly = 0; |
426 | numtests = 1; |
446 | numtests = 1; |
427 | for (i = 1; argv[i]; ++i) |
447 | for (i = 1; argv[i]; ++i) |
428 | { |
448 | { |