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 | } |
… | |
… | |
297 | |
278 | |
298 | /* Set the gamma for the window */ |
279 | /* Set the gamma for the window */ |
299 | if (gamma != 0.0) |
280 | if (gamma != 0.0) |
300 | SDL_SetGamma (gamma, gamma, gamma); |
281 | SDL_SetGamma (gamma, gamma, gamma); |
301 | |
282 | |
|
|
283 | entity *planet = new entity (new geometry_sphere (10)); |
|
|
284 | planet->move (vec3 (0, 0, -20)); |
|
|
285 | planet->show (); |
|
|
286 | |
|
|
287 | #if 1 |
302 | for (int i = 0; i < 1; i++) |
288 | for (int i = 0; i < 20; i++) |
303 | { |
289 | { |
304 | // load a entity |
290 | // load a entity |
305 | txtprt_parser p; |
291 | txtprt_parser p; |
306 | geometry *g; |
292 | geometry *g; |
307 | try |
293 | try |
… | |
… | |
316 | entity *e = new entity (g); |
302 | entity *e = new entity (g); |
317 | e->move (vec3 (i*5, -3, -i*10)); |
303 | e->move (vec3 (i*5, -3, -i*10)); |
318 | e->show (); |
304 | e->show (); |
319 | } |
305 | } |
320 | |
306 | |
|
|
307 | { |
|
|
308 | entity *planet = new entity (new geometry_sphere (1e9)); |
|
|
309 | planet->move (vec3 (0, 0, -1.5e9)); |
|
|
310 | planet->show (); |
|
|
311 | } |
|
|
312 | |
|
|
313 | { |
|
|
314 | entity *planet = new entity (new geometry_sphere (4e15)); |
|
|
315 | planet->move (vec3 (0, 0, 1e17)); |
|
|
316 | planet->show (); |
|
|
317 | } |
|
|
318 | |
321 | //draw_floor (10, -500, -10, -1000); |
319 | draw_floor (10, -500, -10, -1000); |
322 | draw_level (); |
320 | draw_level (); |
323 | //draw_test_nurb (); |
321 | //draw_test_nurb (); |
|
|
322 | #endif |
324 | |
323 | |
325 | //camera.orig.x = 108; camera.orig.y = 0; camera.orig.z = -368; |
324 | //camera.orig.x = 108; camera.orig.y = 0; camera.orig.z = -368; |
326 | camera.orig.x = 0; camera.orig.y = 0; camera.orig.z = 0; |
325 | camera.orig.x = 0; camera.orig.y = 0; camera.orig.z = 0; |
327 | camera.p = point (0, 0, 10); |
326 | camera.p = point (0, 0, 10); |
328 | camera.d = vec3 (0, 0, -1); |
327 | camera.d = vec3 (0, 0, -1); |
329 | camera.u = vec3 (0, 1, 0); |
328 | camera.u = vec3 (0, 1, 0); |
330 | camera.w = w; camera.h = h; |
329 | camera.w = w; camera.h = h; |
331 | camera.fov = 35; |
330 | camera.fov = 35; |
332 | camera.z_near = 1.; |
331 | camera.z_near = 1.; |
333 | |
332 | |
334 | glMatrixMode (GL_MODELVIEW); |
|
|
335 | glLoadIdentity (); |
|
|
336 | |
|
|
337 | glEnable (GL_CULL_FACE); |
333 | glEnable (GL_CULL_FACE); |
338 | glEnable (GL_DEPTH_TEST); |
|
|
339 | |
334 | |
340 | glShadeModel (GL_SMOOTH); |
335 | init_shaders (); |
341 | |
336 | |
342 | glEnable (GL_LIGHTING); |
337 | osama_material osa_mat; |
343 | glEnable (GL_LIGHT0); |
|
|
344 | |
|
|
345 | cgc = cgCreateContext (); |
|
|
346 | |
|
|
347 | vsh_profile = CG_PROFILE_ARBVP1; |
|
|
348 | //if (cgGLIsProfileSupported (CG_PROFILE_VP30)) vsh_profile = CG_PROFILE_VP30; |
|
|
349 | //if (cgGLIsProfileSupported (CG_PROFILE_VP40)) vsh_profile = CG_PROFILE_VP40; |
|
|
350 | fsh_profile = CG_PROFILE_ARBFP1; |
|
|
351 | //if (cgGLIsProfileSupported (CG_PROFILE_FP30)) fsh_profile = CG_PROFILE_FP30; |
|
|
352 | //if (cgGLIsProfileSupported (CG_PROFILE_FP40)) fsh_profile = CG_PROFILE_FP40; |
|
|
353 | |
|
|
354 | vsh = cgCreateProgramFromFile (cgc, CG_SOURCE, "vsh.cg", vsh_profile, 0, 0); |
|
|
355 | CheckCgError (); |
|
|
356 | cgGLLoadProgram (vsh); |
|
|
357 | CheckCgError (); |
|
|
358 | mv = cgGetNamedParameter (vsh, "WorldProj"); |
|
|
359 | mvp = cgGetNamedParameter (vsh, "WorldViewProj"); |
|
|
360 | lightpos = cgGetNamedParameter (vsh, "LightPos"); |
|
|
361 | CheckCgError (); |
|
|
362 | |
|
|
363 | CGparameter g_Texture; // the texture parameter |
|
|
364 | |
|
|
365 | |
|
|
366 | fsh = cgCreateProgramFromFile (cgc, CG_SOURCE, "fsh.cg", fsh_profile, 0, 0); |
|
|
367 | Texture t("o.jpg"); |
|
|
368 | g_Texture = cgGetNamedParameter(fsh, "Texture"); // the texture cg-warper ;) |
|
|
369 | cgGLSetTextureParameter(g_Texture, t.texture); // Bind the texture number 999 to g_Texture |
|
|
370 | CheckCgError (); |
|
|
371 | cgGLLoadProgram (fsh); |
|
|
372 | CheckCgError (); |
|
|
373 | |
|
|
374 | cgGLBindProgram (vsh); |
|
|
375 | CheckCgError (); |
|
|
376 | cgGLBindProgram (fsh); |
|
|
377 | CheckCgError (); |
|
|
378 | |
338 | |
379 | /* Loop until done. */ |
339 | /* Loop until done. */ |
380 | start_time = SDL_GetTicks (); |
340 | start_time = SDL_GetTicks (); |
381 | frames = 0; |
341 | frames = 0; |
382 | |
342 | |
… | |
… | |
395 | camera.u = cross (camera.d, right); |
355 | camera.u = cross (camera.d, right); |
396 | |
356 | |
397 | camera.p = camera.p - camera.d * (camera_velocity_factor * timer.diff) * camera_velocity.z; |
357 | camera.p = camera.p - camera.d * (camera_velocity_factor * timer.diff) * camera_velocity.z; |
398 | camera.p = camera.p - camera.u * (camera_velocity_factor * timer.diff) * camera_velocity.y; |
358 | camera.p = camera.p - camera.u * (camera_velocity_factor * timer.diff) * camera_velocity.y; |
399 | |
359 | |
400 | cgGLSetParameter4f (lightpos, camera.p.x, camera.p.y, camera.p.z, 1); |
360 | osa_mat.begin (); |
401 | |
|
|
402 | glBindTexture (GL_TEXTURE_2D, t.texture); |
|
|
403 | cgGLEnableTextureParameter (g_Texture); // Enable the texture parameter |
|
|
404 | |
|
|
405 | #if 0 |
|
|
406 | static GLfloat ry; |
|
|
407 | ry += 0.001; |
|
|
408 | camera.d.x = cos (ry); |
|
|
409 | camera.d.z = sin (ry); |
|
|
410 | //camera.d.y = sin (ry * 0.1); |
|
|
411 | #endif |
|
|
412 | |
361 | |
413 | camera.begin (); |
362 | camera.begin (); |
414 | camera.pass (view::DEPTH); |
363 | camera.pass (view::DEPTH); |
|
|
364 | camera.pass (view::POSTDEPTH); |
415 | camera.pass (view::LIGHTED); |
365 | camera.pass (view::LIGHTED); |
416 | camera.end (); |
366 | camera.end (); |
417 | |
367 | |
418 | SDL_GL_SwapBuffers (); |
368 | SDL_GL_SwapBuffers (); |
419 | timer.frame (); |
369 | timer.frame (); |
420 | |
370 | |
|
|
371 | osa_mat.end (); |
|
|
372 | |
|
|
373 | #if 0 |
421 | /* Check for error conditions. */ |
374 | /* Check for error conditions. */ |
422 | gl_error = glGetError (); |
375 | gl_error = glGetError (); |
423 | |
376 | |
424 | if (gl_error != GL_NO_ERROR) fprintf (stderr, "testgl: OpenGL error: %d\n", gl_error); |
377 | if (gl_error != GL_NO_ERROR) fprintf (stderr, "testgl: OpenGL error: %d\n", gl_error); |
425 | |
378 | |
… | |
… | |
431 | SDL_ClearError (); |
384 | SDL_ClearError (); |
432 | } |
385 | } |
433 | |
386 | |
434 | /* Allow the user to see what's happening */ |
387 | /* Allow the user to see what's happening */ |
435 | //SDL_Delay (40); |
388 | //SDL_Delay (40); |
|
|
389 | #endif |
436 | |
390 | |
437 | /* Check if there's a pending event. */ |
391 | /* Check if there's a pending event. */ |
438 | while (SDL_PollEvent (&event)) |
392 | while (SDL_PollEvent (&event)) |
439 | done = HandleEvent (&event); |
393 | done = HandleEvent (&event); |
440 | |
394 | |