… | |
… | |
16 | CGcontext cgc; |
16 | CGcontext cgc; |
17 | CGprogram vsh, fsh; |
17 | CGprogram vsh, fsh; |
18 | CGparameter mv, mvp, lightpos; |
18 | CGparameter mv, mvp, lightpos; |
19 | CGprofile vsh_profile, fsh_profile; |
19 | CGprofile vsh_profile, fsh_profile; |
20 | |
20 | |
21 | static void CheckCgError(void) |
21 | static void CheckCgError (void) |
22 | { |
22 | { |
23 | CGerror err = cgGetError(); |
23 | CGerror err = cgGetError (); |
24 | |
24 | |
25 | if (err != CG_NO_ERROR) |
25 | if (err != CG_NO_ERROR) |
26 | { |
26 | { |
27 | printf("CG error: %s\n", cgGetErrorString(err)); |
27 | printf("CG error: %s\n", cgGetErrorString (err)); |
28 | exit(1); |
28 | exit(1); |
29 | } |
29 | } |
30 | } |
30 | } |
31 | |
31 | |
32 | /**********************************************************************/ |
32 | /**********************************************************************/ |
33 | |
33 | |
34 | view camera; |
34 | view camera; |
35 | vec3 camera_velocity; |
35 | vec3 camera_velocity; |
|
|
36 | float camera_angle, camera_angle2, camera_velocity_angle, camera_velocity_angle2; |
36 | float camera_velocity_factor = 20; |
37 | float camera_velocity_factor = 80; |
37 | |
38 | |
38 | void |
39 | void |
39 | HotKey_ToggleFullScreen (void) |
40 | HotKey_ToggleFullScreen (void) |
40 | { |
41 | { |
41 | SDL_Surface *screen; |
42 | SDL_Surface *screen; |
… | |
… | |
100 | |
101 | |
101 | printf ("focus\n"); |
102 | printf ("focus\n"); |
102 | break; |
103 | break; |
103 | |
104 | |
104 | case SDL_KEYDOWN: |
105 | case SDL_KEYDOWN: |
105 | if (event->key.keysym.sym == SDLK_UP) camera_velocity.z--; |
106 | if (event->key.keysym.sym == SDLK_UP) camera_velocity.z--; |
106 | if (event->key.keysym.sym == SDLK_DOWN) camera_velocity.z++; |
107 | 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++; |
108 | if (event->key.keysym.sym == SDLK_RIGHT) camera_velocity_angle++; |
|
|
109 | if (event->key.keysym.sym == SDLK_LEFT) camera_velocity_angle--; |
|
|
110 | if (event->key.keysym.sym == SDLK_PAGEUP) camera_velocity_angle2++; |
|
|
111 | if (event->key.keysym.sym == SDLK_PAGEDOWN) camera_velocity_angle2--; |
109 | if (event->key.keysym.sym == SDLK_a) camera_velocity.y--; |
112 | if (event->key.keysym.sym == SDLK_e) camera_velocity.y--; |
110 | if (event->key.keysym.sym == SDLK_s) camera_velocity.y++; |
113 | if (event->key.keysym.sym == SDLK_q) camera_velocity.y++; |
111 | if (event->key.keysym.sym == SDLK_v) camera_velocity_factor *= 1.5; |
114 | 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; |
115 | if (event->key.keysym.sym == SDLK_b) camera_velocity_factor /= 1.5; |
113 | |
116 | |
114 | if (event->key.keysym.sym == SDLK_ESCAPE) |
117 | if (event->key.keysym.sym == SDLK_ESCAPE) |
115 | done = 1; |
118 | done = 1; |
116 | |
119 | |
117 | if ((event->key.keysym.sym == SDLK_g) && |
120 | if ((event->key.keysym.sym == SDLK_g) && |
… | |
… | |
129 | break; |
132 | break; |
130 | |
133 | |
131 | case SDL_KEYUP: |
134 | case SDL_KEYUP: |
132 | if (event->key.keysym.sym == SDLK_UP) camera_velocity.z++; |
135 | if (event->key.keysym.sym == SDLK_UP) camera_velocity.z++; |
133 | if (event->key.keysym.sym == SDLK_DOWN) camera_velocity.z--; |
136 | if (event->key.keysym.sym == SDLK_DOWN) camera_velocity.z--; |
134 | if (event->key.keysym.sym == SDLK_LEFT) camera_velocity.x++; |
137 | if (event->key.keysym.sym == SDLK_LEFT) camera_velocity_angle++; |
135 | if (event->key.keysym.sym == SDLK_RIGHT) camera_velocity.x--; |
138 | if (event->key.keysym.sym == SDLK_RIGHT) camera_velocity_angle--; |
|
|
139 | if (event->key.keysym.sym == SDLK_PAGEUP) camera_velocity_angle2--; |
|
|
140 | if (event->key.keysym.sym == SDLK_PAGEDOWN) camera_velocity_angle2++; |
136 | if (event->key.keysym.sym == SDLK_a) camera_velocity.y++; |
141 | if (event->key.keysym.sym == SDLK_e) camera_velocity.y++; |
137 | if (event->key.keysym.sym == SDLK_s) camera_velocity.y--; |
142 | if (event->key.keysym.sym == SDLK_q) camera_velocity.y--; |
138 | break; |
143 | break; |
139 | |
144 | |
140 | case SDL_QUIT: |
145 | case SDL_QUIT: |
141 | done = 1; |
146 | done = 1; |
142 | break; |
147 | break; |
143 | } |
148 | } |
144 | |
149 | |
145 | return (done); |
150 | return (done); |
|
|
151 | } |
|
|
152 | |
|
|
153 | void draw_floor (int size, int dx, int dy, int dz) |
|
|
154 | { |
|
|
155 | int x, z, ry; |
|
|
156 | |
|
|
157 | for (x = 0; x < 100; x++) |
|
|
158 | { |
|
|
159 | for (z = 0; z < 100; z++) |
|
|
160 | { |
|
|
161 | vector<vertex2d> pts; |
|
|
162 | pts.push_back (vertex2d (point ( 0, 0, 0), vec3 (0, 1, 0), texc (0, 0))); |
|
|
163 | pts.push_back (vertex2d (point ( 0, 0, size), vec3 (0, 1, 0), texc (0, 1))); |
|
|
164 | pts.push_back (vertex2d (point (size, 0, size), vec3 (0, 1, 0), texc (1, 1))); |
|
|
165 | pts.push_back (vertex2d (point (size, 0, 0), vec3 (0, 1, 0), texc (1, 0))); |
|
|
166 | |
|
|
167 | geometry_quads *q = new geometry_quads; |
|
|
168 | q->set (pts); |
|
|
169 | entity *e = new entity (q); |
|
|
170 | e->move (vec3 (dx + x * size, dy, dz + z * size)); |
|
|
171 | e->show (); |
|
|
172 | } |
|
|
173 | } |
|
|
174 | } |
|
|
175 | |
|
|
176 | void draw_test_nurb () |
|
|
177 | { |
|
|
178 | geometry_nurbs *q = new geometry_nurbs; |
|
|
179 | q->set (); |
|
|
180 | entity *e = new entity (q); |
|
|
181 | e->move (vec3 (10, 3, -4)); |
|
|
182 | e->show (); |
146 | } |
183 | } |
147 | |
184 | |
148 | int |
185 | int |
149 | RunGLTest (int argc, char *argv[], |
186 | RunGLTest (int argc, char *argv[], |
150 | int logo, int slowly, int bpp, float gamma, int noframe, int fsaa) |
187 | int logo, int slowly, int bpp, float gamma, int noframe, int fsaa) |
… | |
… | |
156 | int done = 0; |
193 | int done = 0; |
157 | int frames; |
194 | int frames; |
158 | Uint32 start_time, this_time; |
195 | Uint32 start_time, this_time; |
159 | Uint32 video_flags; |
196 | Uint32 video_flags; |
160 | int value; |
197 | int value; |
|
|
198 | GLenum gl_error; |
161 | |
199 | |
162 | if (SDL_Init (SDL_INIT_VIDEO) < 0) |
200 | if (SDL_Init (SDL_INIT_VIDEO) < 0) |
163 | { |
201 | { |
164 | fprintf (stderr, "Couldn't initialize SDL: %s\n", SDL_GetError ()); |
202 | fprintf (stderr, "Couldn't initialize SDL: %s\n", SDL_GetError ()); |
165 | exit (1); |
203 | exit (1); |
… | |
… | |
258 | |
296 | |
259 | /* Set the gamma for the window */ |
297 | /* Set the gamma for the window */ |
260 | if (gamma != 0.0) |
298 | if (gamma != 0.0) |
261 | SDL_SetGamma (gamma, gamma, gamma); |
299 | SDL_SetGamma (gamma, gamma, gamma); |
262 | |
300 | |
263 | // load a entity |
|
|
264 | for (int i = 0; i < 7; i++) |
301 | for (int i = 0; i < 1; i++) |
265 | { |
302 | { |
|
|
303 | // load a entity |
266 | txtprt_parser p; |
304 | txtprt_parser p; |
267 | entity_transform *f = new entity_transform; |
305 | geometry *g; |
268 | entity *e; |
|
|
269 | try |
306 | try |
270 | { |
307 | { |
271 | e = p.read ("test.blasc"); |
308 | g = p.read ("test.blasc"); |
|
|
309 | } |
272 | } catch (txtprt_i_exception & e) |
310 | catch (txtprt_i_exception & e) |
273 | { |
311 | { |
274 | cout << "ERR: " << e.msg << endl; |
312 | cout << "ERR: " << e.msg << endl; |
275 | } |
313 | } |
276 | f->set (e); |
314 | |
277 | f->update (matrix::translation (vec3 (0, -1, -i*5))); |
315 | entity *e = new entity (g); |
|
|
316 | e->move (vec3 (i*5, -3, -i*10)); |
278 | f->show (); |
317 | e->show (); |
279 | } |
318 | } |
|
|
319 | |
|
|
320 | draw_floor (10, -500, -10, -1000); |
|
|
321 | //draw_test_nurb (); |
280 | |
322 | |
281 | camera.orig.x = camera.orig.y = camera.orig.z = 0; |
323 | camera.orig.x = camera.orig.y = camera.orig.z = 0; |
282 | camera.p = point (0, 0, 10); |
324 | camera.p = point (0, 0, 10); |
283 | camera.d = vec3 (0, 0, -1); |
325 | camera.d = vec3 (0, 0, -1); |
284 | camera.u = vec3 (0, 1, 0); |
326 | camera.u = vec3 (0, 1, 0); |
285 | camera.w = w; camera.h = h; |
327 | camera.w = w; camera.h = h; |
286 | camera.fov = 90; |
328 | camera.fov = 35; |
|
|
329 | camera.z_near = 1.; |
287 | |
330 | |
288 | glMatrixMode (GL_MODELVIEW); |
331 | glMatrixMode (GL_MODELVIEW); |
289 | glLoadIdentity (); |
332 | glLoadIdentity (); |
290 | |
333 | |
291 | glEnable (GL_CULL_FACE); |
334 | glEnable (GL_CULL_FACE); |
292 | glEnable (GL_DEPTH_TEST); |
335 | glEnable (GL_DEPTH_TEST); |
293 | |
336 | |
294 | glShadeModel (GL_SMOOTH); |
337 | glShadeModel (GL_SMOOTH); |
295 | |
338 | |
296 | glEnable (GL_LIGHTING); |
339 | glEnable (GL_LIGHTING); |
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); |
340 | glEnable (GL_LIGHT0); |
301 | |
341 | |
302 | cgc = cgCreateContext (); |
342 | cgc = cgCreateContext (); |
303 | |
343 | |
304 | vsh_profile = CG_PROFILE_ARBVP1; |
344 | vsh_profile = CG_PROFILE_ARBVP1; |
… | |
… | |
315 | mv = cgGetNamedParameter (vsh, "WorldProj"); |
355 | mv = cgGetNamedParameter (vsh, "WorldProj"); |
316 | mvp = cgGetNamedParameter (vsh, "WorldViewProj"); |
356 | mvp = cgGetNamedParameter (vsh, "WorldViewProj"); |
317 | lightpos = cgGetNamedParameter (vsh, "LightPos"); |
357 | lightpos = cgGetNamedParameter (vsh, "LightPos"); |
318 | CheckCgError (); |
358 | CheckCgError (); |
319 | |
359 | |
|
|
360 | CGparameter g_Texture; // the texture parameter |
|
|
361 | |
|
|
362 | |
320 | fsh = cgCreateProgramFromFile (cgc, CG_SOURCE, "fsh.cg", fsh_profile, 0, 0); |
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 |
321 | CheckCgError (); |
367 | CheckCgError (); |
322 | cgGLLoadProgram (fsh); |
368 | cgGLLoadProgram (fsh); |
323 | CheckCgError (); |
369 | CheckCgError (); |
324 | |
370 | |
325 | cgGLBindProgram (vsh); |
371 | cgGLBindProgram (vsh); |
… | |
… | |
331 | start_time = SDL_GetTicks (); |
377 | start_time = SDL_GetTicks (); |
332 | frames = 0; |
378 | frames = 0; |
333 | |
379 | |
334 | while (!done) |
380 | while (!done) |
335 | { |
381 | { |
336 | GLenum gl_error; |
|
|
337 | char *sdl_error; |
382 | char *sdl_error; |
338 | SDL_Event event; |
383 | SDL_Event event; |
339 | |
384 | |
340 | glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
385 | camera_angle += 180 * camera_velocity_angle * timer.diff; |
|
|
386 | camera_angle2 += 180 * camera_velocity_angle2 * timer.diff; |
341 | |
387 | |
342 | camera.p.x += (camera_velocity_factor * camera_velocity.x) * timer.diff; |
388 | 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; |
389 | vec3 right = matrix::rotation (90., vec3 (0, 1, 0)) * geradeaus; |
344 | camera.p.z += (camera_velocity_factor * camera_velocity.z) * timer.diff; |
390 | |
|
|
391 | camera.d = matrix::rotation (camera_angle2, right) * geradeaus; |
|
|
392 | camera.u = cross (camera.d, right); |
|
|
393 | |
|
|
394 | 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; |
345 | |
396 | |
346 | cgGLSetParameter4f (lightpos, camera.p.x, camera.p.y, camera.p.z, 1); |
397 | cgGLSetParameter4f (lightpos, camera.p.x, camera.p.y, camera.p.z, 1); |
|
|
398 | |
|
|
399 | glBindTexture (GL_TEXTURE_2D, t.texture); |
|
|
400 | cgGLEnableTextureParameter (g_Texture); // Enable the texture parameter |
347 | |
401 | |
348 | #if 0 |
402 | #if 0 |
349 | static GLfloat ry; |
403 | static GLfloat ry; |
350 | ry += 0.001; |
404 | ry += 0.001; |
351 | camera.d.x = cos (ry); |
405 | camera.d.x = cos (ry); |
… | |
… | |
362 | timer.frame (); |
416 | timer.frame (); |
363 | |
417 | |
364 | /* Check for error conditions. */ |
418 | /* Check for error conditions. */ |
365 | gl_error = glGetError (); |
419 | gl_error = glGetError (); |
366 | |
420 | |
367 | if (gl_error != GL_NO_ERROR) |
|
|
368 | fprintf (stderr, "testgl: OpenGL error: %d\n", gl_error); |
421 | if (gl_error != GL_NO_ERROR) fprintf (stderr, "testgl: OpenGL error: %d\n", gl_error); |
369 | |
422 | |
370 | sdl_error = SDL_GetError (); |
423 | sdl_error = SDL_GetError (); |
371 | |
424 | |
372 | if (sdl_error[0] != '\0') |
425 | if (sdl_error[0] != '\0') |
373 | { |
426 | { |
374 | fprintf (stderr, "testgl: SDL error '%s'\n", sdl_error); |
427 | fprintf (stderr, "testgl: SDL error '%s'\n", sdl_error); |
375 | SDL_ClearError (); |
428 | SDL_ClearError (); |
376 | } |
429 | } |
377 | |
430 | |
378 | /* Allow the user to see what's happening */ |
431 | /* Allow the user to see what's happening */ |
379 | //SDL_Delay (20); |
432 | //SDL_Delay (40); |
380 | |
433 | |
381 | /* Check if there's a pending event. */ |
434 | /* Check if there's a pending event. */ |
382 | while (SDL_PollEvent (&event)) |
435 | while (SDL_PollEvent (&event)) |
383 | done = HandleEvent (&event); |
436 | done = HandleEvent (&event); |
384 | |
437 | |