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 | |
|
|
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 | |
17 | |
32 | /**********************************************************************/ |
18 | /**********************************************************************/ |
33 | |
19 | |
34 | view camera; |
20 | view camera; |
35 | vec3 camera_velocity; |
21 | vec3 camera_velocity; |
… | |
… | |
85 | { |
71 | { |
86 | case SDL_ACTIVEEVENT: |
72 | case SDL_ACTIVEEVENT: |
87 | /* See what happened */ |
73 | /* See what happened */ |
88 | printf ("app %s ", event->active.gain ? "gained" : "lost"); |
74 | printf ("app %s ", event->active.gain ? "gained" : "lost"); |
89 | if (event->active.state & SDL_APPACTIVE) |
75 | if (event->active.state & SDL_APPACTIVE) |
90 | { |
|
|
91 | printf ("active "); |
76 | printf ("active "); |
92 | } |
|
|
93 | else if (event->active.state & SDL_APPMOUSEFOCUS) |
77 | else if (event->active.state & SDL_APPMOUSEFOCUS) |
94 | { |
|
|
95 | printf ("mouse "); |
78 | printf ("mouse "); |
96 | } |
|
|
97 | else if (event->active.state & SDL_APPINPUTFOCUS) |
79 | else if (event->active.state & SDL_APPINPUTFOCUS) |
98 | { |
|
|
99 | printf ("input "); |
80 | printf ("input "); |
100 | } |
|
|
101 | |
81 | |
102 | printf ("focus\n"); |
82 | printf ("focus\n"); |
103 | break; |
83 | break; |
104 | |
84 | |
105 | case SDL_KEYDOWN: |
85 | case SDL_KEYDOWN: |
… | |
… | |
152 | |
132 | |
153 | void draw_floor (int size, int dx, int dy, int dz) |
133 | void draw_floor (int size, int dx, int dy, int dz) |
154 | { |
134 | { |
155 | int x, z, ry; |
135 | int x, z, ry; |
156 | |
136 | |
157 | for (x = 0; x < 100; x++) |
137 | for (x = 0; x < 10; x++) |
158 | { |
138 | { |
159 | for (z = 0; z < 100; z++) |
139 | for (z = 0; z < 10; z++) |
160 | { |
140 | { |
161 | vector<vertex2d> pts; |
141 | vector<vertex_t2f_n3f_v3f> pts; |
162 | pts.push_back (vertex2d (point ( 0, 0, 0), vec3 (0, 1, 0), texc (0, 0))); |
142 | pts.push_back (vertex_t2f_n3f_v3f (point ( 0, 0, 0), vec3 (0, 1, 0), tex2 (0, 0))); |
163 | pts.push_back (vertex2d (point ( 0, 0, size), vec3 (0, 1, 0), texc (0, 1))); |
143 | pts.push_back (vertex_t2f_n3f_v3f (point ( 0, 0, size), vec3 (0, 1, 0), tex2 (0, 1))); |
164 | pts.push_back (vertex2d (point (size, 0, size), vec3 (0, 1, 0), texc (1, 1))); |
144 | pts.push_back (vertex_t2f_n3f_v3f (point (size, 0, size), vec3 (0, 1, 0), tex2 (1, 1))); |
165 | pts.push_back (vertex2d (point (size, 0, 0), vec3 (0, 1, 0), texc (1, 0))); |
145 | pts.push_back (vertex_t2f_n3f_v3f (point (size, 0, 0), vec3 (0, 1, 0), tex2 (1, 0))); |
166 | |
146 | |
167 | geometry_quads *q = new geometry_quads; |
147 | geometry_quads *q = new geometry_quads; |
|
|
148 | q->m = new simple_material; |
168 | q->set (pts); |
149 | q->set (pts); |
169 | entity *e = new entity (q); |
150 | entity *e = new entity (q); |
170 | e->move (vec3 (dx + x * size, dy, dz + z * size)); |
151 | e->move (vec3 (dx + x * size, dy, dz + z * size)); |
171 | e->show (); |
152 | e->show (); |
172 | } |
153 | } |
173 | } |
154 | } |
174 | } |
155 | } |
175 | |
156 | |
176 | void draw_test_nurb () { |
157 | void draw_test_nurb () |
|
|
158 | { |
177 | geometry_nurbs *q = new geometry_nurbs; |
159 | geometry_nurbs *q = new geometry_nurbs; |
178 | q->set (); |
160 | q->set (); |
179 | entity *e = new entity (q); |
161 | entity *e = new entity (q); |
180 | e->move (vec3 (10, 3, -4)); |
162 | e->move (vec3 (10, 3, -4)); |
181 | e->show (); |
163 | e->show (); |
182 | } |
164 | } |
183 | |
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 (); |
184 | int |
184 | int |
185 | RunGLTest (int argc, char *argv[], |
185 | RunGLTest (int argc, char *argv[], |
186 | 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) |
187 | { |
187 | { |
188 | int i; |
188 | int i; |
189 | int rgb_size[3]; |
189 | int rgb_size[3]; |
190 | int w = 640; |
190 | int w = 640; |
191 | int h = 480; |
191 | int h = 480; |
192 | int done = 0; |
192 | int done = 0; |
193 | int frames; |
193 | int frames; |
194 | Uint32 start_time, this_time; |
|
|
195 | Uint32 video_flags; |
194 | Uint32 video_flags; |
196 | int value; |
195 | int value; |
197 | GLenum gl_error; |
196 | GLenum gl_error; |
198 | |
197 | |
199 | if (SDL_Init (SDL_INIT_VIDEO) < 0) |
198 | if (SDL_Init (SDL_INIT_VIDEO) < 0) |
… | |
… | |
295 | |
294 | |
296 | /* Set the gamma for the window */ |
295 | /* Set the gamma for the window */ |
297 | if (gamma != 0.0) |
296 | if (gamma != 0.0) |
298 | SDL_SetGamma (gamma, gamma, gamma); |
297 | SDL_SetGamma (gamma, gamma, gamma); |
299 | |
298 | |
|
|
299 | entity *planet = new entity (new geometry_sphere (10)); |
|
|
300 | planet->move (vec3 (0, 0, -20)); |
|
|
301 | planet->show (); |
|
|
302 | |
|
|
303 | #if 1 |
300 | for (int i = 0; i < 1; i++) |
304 | for (int i = 0; i < 20; i++) |
301 | { |
305 | { |
302 | // load a entity |
306 | // load a entity |
303 | txtprt_parser p; |
307 | txtprt_parser p; |
304 | geometry *g; |
308 | geometry *g; |
305 | try |
309 | try |
… | |
… | |
314 | entity *e = new entity (g); |
318 | entity *e = new entity (g); |
315 | e->move (vec3 (i*5, -3, -i*10)); |
319 | e->move (vec3 (i*5, -3, -i*10)); |
316 | e->show (); |
320 | e->show (); |
317 | } |
321 | } |
318 | |
322 | |
|
|
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 | } |
|
|
329 | |
|
|
330 | { |
|
|
331 | entity *planet = new entity (new geometry_sphere (4e15)); |
|
|
332 | planet->move (vec3 (0, 0, 1e17)); |
|
|
333 | planet->show (); |
|
|
334 | } |
|
|
335 | |
319 | draw_floor (10, -500, -10, -1000); |
336 | draw_floor (10, -500, -10, -1000); |
|
|
337 | draw_level (); |
320 | draw_test_nurb (); |
338 | //draw_test_nurb (); |
|
|
339 | #endif |
321 | |
340 | |
|
|
341 | //camera.orig.x = 108; camera.orig.y = 0; camera.orig.z = -368; |
322 | camera.orig.x = camera.orig.y = camera.orig.z = 0; |
342 | camera.orig.x = 0; camera.orig.y = 0; camera.orig.z = 0; |
323 | camera.p = point (0, 0, 10); |
343 | camera.p = point (0, 0, 10); |
324 | camera.d = vec3 (0, 0, -1); |
344 | camera.d = vec3 (0, 0, -1); |
325 | camera.u = vec3 (0, 1, 0); |
345 | camera.u = vec3 (0, 1, 0); |
326 | camera.w = w; camera.h = h; |
346 | camera.w = w; camera.h = h; |
327 | camera.fov = 35; |
347 | camera.fov = 40.; |
328 | camera.near = 1.; |
|
|
329 | camera.far = 10.; |
|
|
330 | |
|
|
331 | glMatrixMode (GL_MODELVIEW); |
|
|
332 | glLoadIdentity (); |
|
|
333 | |
348 | |
334 | glEnable (GL_CULL_FACE); |
349 | glEnable (GL_CULL_FACE); |
335 | glEnable (GL_DEPTH_TEST); |
350 | glDisable (GL_ALPHA_TEST); |
|
|
351 | glDisable (GL_BLEND); |
336 | |
352 | |
337 | glShadeModel (GL_SMOOTH); |
353 | init_shaders (); |
338 | |
354 | |
339 | glEnable (GL_LIGHTING); |
355 | osama_material osa_mat; |
340 | glEnable (GL_LIGHT0); |
|
|
341 | |
|
|
342 | cgc = cgCreateContext (); |
|
|
343 | |
|
|
344 | vsh_profile = CG_PROFILE_ARBVP1; |
|
|
345 | //if (cgGLIsProfileSupported (CG_PROFILE_VP30)) vsh_profile = CG_PROFILE_VP30; |
|
|
346 | //if (cgGLIsProfileSupported (CG_PROFILE_VP40)) vsh_profile = CG_PROFILE_VP40; |
|
|
347 | fsh_profile = CG_PROFILE_ARBFP1; |
|
|
348 | //if (cgGLIsProfileSupported (CG_PROFILE_FP30)) fsh_profile = CG_PROFILE_FP30; |
|
|
349 | //if (cgGLIsProfileSupported (CG_PROFILE_FP40)) fsh_profile = CG_PROFILE_FP40; |
|
|
350 | |
|
|
351 | vsh = cgCreateProgramFromFile (cgc, CG_SOURCE, "vsh.cg", vsh_profile, 0, 0); |
|
|
352 | CheckCgError (); |
|
|
353 | cgGLLoadProgram (vsh); |
|
|
354 | CheckCgError (); |
|
|
355 | mv = cgGetNamedParameter (vsh, "WorldProj"); |
|
|
356 | mvp = cgGetNamedParameter (vsh, "WorldViewProj"); |
|
|
357 | lightpos = cgGetNamedParameter (vsh, "LightPos"); |
|
|
358 | CheckCgError (); |
|
|
359 | |
|
|
360 | CGparameter g_Texture; // the texture parameter |
|
|
361 | |
|
|
362 | |
|
|
363 | fsh = cgCreateProgramFromFile (cgc, CG_SOURCE, "fsh.cg", fsh_profile, 0, 0); |
|
|
364 | Texture t("o.jpg"); |
|
|
365 | g_Texture = cgGetNamedParameter(fsh, "Texture"); // the texture cg-warper ;) |
|
|
366 | cgGLSetTextureParameter(g_Texture, t.texture); // Bind the texture number 999 to g_Texture |
|
|
367 | CheckCgError (); |
|
|
368 | cgGLLoadProgram (fsh); |
|
|
369 | CheckCgError (); |
|
|
370 | |
|
|
371 | cgGLBindProgram (vsh); |
|
|
372 | CheckCgError (); |
|
|
373 | cgGLBindProgram (fsh); |
|
|
374 | CheckCgError (); |
|
|
375 | |
356 | |
376 | /* Loop until done. */ |
357 | /* Loop until done. */ |
377 | start_time = SDL_GetTicks (); |
|
|
378 | frames = 0; |
358 | frames = 0; |
379 | |
359 | |
380 | while (!done) |
360 | while (!done) |
381 | { |
361 | { |
382 | char *sdl_error; |
362 | char *sdl_error; |
… | |
… | |
392 | camera.u = cross (camera.d, right); |
372 | camera.u = cross (camera.d, right); |
393 | |
373 | |
394 | camera.p = camera.p - camera.d * (camera_velocity_factor * timer.diff) * camera_velocity.z; |
374 | camera.p = camera.p - camera.d * (camera_velocity_factor * timer.diff) * camera_velocity.z; |
395 | camera.p = camera.p - camera.u * (camera_velocity_factor * timer.diff) * camera_velocity.y; |
375 | camera.p = camera.p - camera.u * (camera_velocity_factor * timer.diff) * camera_velocity.y; |
396 | |
376 | |
397 | cgGLSetParameter4f (lightpos, camera.p.x, camera.p.y, camera.p.z, 1); |
377 | osa_mat.begin (); |
398 | |
|
|
399 | glBindTexture (GL_TEXTURE_2D, t.texture); |
|
|
400 | cgGLEnableTextureParameter (g_Texture); // Enable the texture parameter |
|
|
401 | |
|
|
402 | #if 0 |
|
|
403 | static GLfloat ry; |
|
|
404 | ry += 0.001; |
|
|
405 | camera.d.x = cos (ry); |
|
|
406 | camera.d.z = sin (ry); |
|
|
407 | //camera.d.y = sin (ry * 0.1); |
|
|
408 | #endif |
|
|
409 | |
378 | |
410 | camera.begin (); |
379 | camera.begin (); |
411 | camera.pass (view::DEPTH); |
380 | camera.render (view::DEPTH); |
|
|
381 | camera.render (view::POSTDEPTH); |
412 | camera.pass (view::LIGHTED); |
382 | camera.render (view::LIGHTED); |
413 | camera.end (); |
383 | camera.end (); |
414 | |
384 | |
415 | SDL_GL_SwapBuffers (); |
385 | SDL_GL_SwapBuffers (); |
416 | timer.frame (); |
386 | timer.frame (); |
417 | |
387 | |
|
|
388 | osa_mat.end (); |
|
|
389 | |
|
|
390 | #if 0 |
418 | /* Check for error conditions. */ |
391 | /* Check for error conditions. */ |
419 | gl_error = glGetError (); |
392 | gl_error = glGetError (); |
420 | |
393 | |
421 | if (gl_error != GL_NO_ERROR) 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); |
422 | |
395 | |
… | |
… | |
428 | SDL_ClearError (); |
401 | SDL_ClearError (); |
429 | } |
402 | } |
430 | |
403 | |
431 | /* Allow the user to see what's happening */ |
404 | /* Allow the user to see what's happening */ |
432 | //SDL_Delay (40); |
405 | //SDL_Delay (40); |
|
|
406 | #endif |
433 | |
407 | |
434 | /* Check if there's a pending event. */ |
408 | /* Check if there's a pending event. */ |
435 | while (SDL_PollEvent (&event)) |
409 | while (SDL_PollEvent (&event)) |
436 | done = HandleEvent (&event); |
410 | done = HandleEvent (&event); |
437 | |
411 | |
438 | |
412 | |
439 | ++frames; |
413 | ++frames; |
440 | } |
414 | } |
441 | |
415 | |
442 | /* Print out the frames per second */ |
|
|
443 | this_time = SDL_GetTicks (); |
|
|
444 | if (this_time != start_time) |
|
|
445 | { |
|
|
446 | printf ("%2.2f FPS\n", |
|
|
447 | ((float) frames / (this_time - start_time)) * 1000.0); |
|
|
448 | } |
|
|
449 | |
|
|
450 | if (global_image) |
416 | if (global_image) |
451 | { |
417 | { |
452 | SDL_FreeSurface (global_image); |
418 | SDL_FreeSurface (global_image); |
453 | global_image = NULL; |
419 | global_image = NULL; |
454 | } |
420 | } |
… | |
… | |
460 | |
426 | |
461 | /* Destroy our GL context, etc. */ |
427 | /* Destroy our GL context, etc. */ |
462 | SDL_Quit (); |
428 | SDL_Quit (); |
463 | return (0); |
429 | return (0); |
464 | } |
430 | } |
465 | |
|
|
466 | int |
431 | int |
467 | main (int argc, char *argv[]) |
432 | main (int argc, char *argv[]) |
468 | { |
433 | { |
469 | int i, logo; |
434 | int i, logo; |
470 | int numtests; |
435 | int numtests; |
… | |
… | |
472 | int slowly; |
437 | int slowly; |
473 | float gamma = 0.0; |
438 | float gamma = 0.0; |
474 | int noframe = 0; |
439 | int noframe = 0; |
475 | int fsaa = 0; |
440 | int fsaa = 0; |
476 | |
441 | |
|
|
442 | shader::debdebdebdebug ();//D |
|
|
443 | |
477 | logo = 0; |
444 | logo = 0; |
478 | slowly = 0; |
445 | slowly = 0; |
479 | numtests = 1; |
446 | numtests = 1; |
480 | for (i = 1; argv[i]; ++i) |
447 | for (i = 1; argv[i]; ++i) |
481 | { |
448 | { |