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" |
|
|
17 | |
|
|
18 | bool doom3parse (const char *f); |
|
|
19 | |
|
|
20 | #include "shader.h" |
15 | |
21 | |
16 | /**********************************************************************/ |
22 | /**********************************************************************/ |
17 | |
23 | |
18 | view camera; |
24 | view camera; |
19 | vec3 camera_velocity; |
25 | vec3 camera_velocity; |
… | |
… | |
24 | HotKey_ToggleFullScreen (void) |
30 | HotKey_ToggleFullScreen (void) |
25 | { |
31 | { |
26 | SDL_Surface *screen; |
32 | SDL_Surface *screen; |
27 | |
33 | |
28 | screen = SDL_GetVideoSurface (); |
34 | screen = SDL_GetVideoSurface (); |
|
|
35 | |
29 | if (SDL_WM_ToggleFullScreen (screen)) |
36 | if (SDL_WM_ToggleFullScreen (screen)) |
30 | printf ("Toggled fullscreen mode - now %s\n", |
37 | printf ("Toggled fullscreen mode - now %s\n", |
31 | (screen->flags & SDL_FULLSCREEN) ? "fullscreen" : "windowed"); |
38 | (screen->flags & SDL_FULLSCREEN) ? "fullscreen" : "windowed"); |
32 | else |
39 | else |
33 | printf ("Unable to toggle fullscreen mode\n"); |
40 | printf ("Unable to toggle fullscreen mode\n"); |
… | |
… | |
38 | { |
45 | { |
39 | SDL_GrabMode mode; |
46 | SDL_GrabMode mode; |
40 | |
47 | |
41 | printf ("Ctrl-G: toggling input grab!\n"); |
48 | printf ("Ctrl-G: toggling input grab!\n"); |
42 | mode = SDL_WM_GrabInput (SDL_GRAB_QUERY); |
49 | mode = SDL_WM_GrabInput (SDL_GRAB_QUERY); |
|
|
50 | |
43 | if (mode == SDL_GRAB_ON) |
51 | if (mode == SDL_GRAB_ON) |
44 | printf ("Grab was on\n"); |
52 | printf ("Grab was on\n"); |
45 | else |
53 | else |
46 | printf ("Grab was off\n"); |
54 | printf ("Grab was off\n"); |
47 | |
55 | |
48 | mode = SDL_WM_GrabInput (mode ? SDL_GRAB_OFF : SDL_GRAB_ON); |
56 | mode = SDL_WM_GrabInput (mode ? SDL_GRAB_OFF : SDL_GRAB_ON); |
|
|
57 | |
49 | if (mode == SDL_GRAB_ON) |
58 | if (mode == SDL_GRAB_ON) |
50 | printf ("Grab is now on\n"); |
59 | printf ("Grab is now on\n"); |
51 | else |
60 | else |
52 | printf ("Grab is now off\n"); |
61 | printf ("Grab is now off\n"); |
53 | } |
62 | } |
… | |
… | |
63 | HandleEvent (SDL_Event * event) |
72 | HandleEvent (SDL_Event * event) |
64 | { |
73 | { |
65 | int done; |
74 | int done; |
66 | |
75 | |
67 | done = 0; |
76 | done = 0; |
|
|
77 | |
68 | switch (event->type) |
78 | switch (event->type) |
69 | { |
79 | { |
70 | case SDL_ACTIVEEVENT: |
80 | case SDL_ACTIVEEVENT: |
71 | /* See what happened */ |
81 | /* See what happened */ |
72 | printf ("app %s ", event->active.gain ? "gained" : "lost"); |
82 | printf ("app %s ", event->active.gain ? "gained" : "lost"); |
73 | if (event->active.state & SDL_APPACTIVE) |
83 | if (event->active.state & SDL_APPACTIVE) |
74 | { |
|
|
75 | printf ("active "); |
84 | printf ("active "); |
76 | } |
|
|
77 | else if (event->active.state & SDL_APPMOUSEFOCUS) |
85 | else if (event->active.state & SDL_APPMOUSEFOCUS) |
78 | { |
|
|
79 | printf ("mouse "); |
86 | printf ("mouse "); |
80 | } |
|
|
81 | else if (event->active.state & SDL_APPINPUTFOCUS) |
87 | else if (event->active.state & SDL_APPINPUTFOCUS) |
82 | { |
|
|
83 | printf ("input "); |
88 | printf ("input "); |
84 | } |
|
|
85 | |
89 | |
86 | printf ("focus\n"); |
90 | printf ("focus\n"); |
87 | break; |
91 | break; |
88 | |
92 | |
89 | case SDL_KEYDOWN: |
93 | case SDL_KEYDOWN: |
… | |
… | |
140 | |
144 | |
141 | for (x = 0; x < 10; x++) |
145 | for (x = 0; x < 10; x++) |
142 | { |
146 | { |
143 | for (z = 0; z < 10; z++) |
147 | for (z = 0; z < 10; z++) |
144 | { |
148 | { |
145 | vector<vertex2d> pts; |
149 | vector<vertex_t2f_n3f_v3f> pts; |
146 | pts.push_back (vertex2d (point ( 0, 0, 0), vec3 (0, 1, 0), texc (0, 0))); |
150 | pts.push_back (vertex_t2f_n3f_v3f (point ( 0, 0, 0), vec3 (0, 1, 0), tex2 (0, 0))); |
147 | pts.push_back (vertex2d (point ( 0, 0, size), vec3 (0, 1, 0), texc (0, 1))); |
151 | pts.push_back (vertex_t2f_n3f_v3f (point ( 0, 0, size), vec3 (0, 1, 0), tex2 (0, 1))); |
148 | pts.push_back (vertex2d (point (size, 0, size), vec3 (0, 1, 0), texc (1, 1))); |
152 | pts.push_back (vertex_t2f_n3f_v3f (point (size, 0, size), vec3 (0, 1, 0), tex2 (1, 1))); |
149 | pts.push_back (vertex2d (point (size, 0, 0), vec3 (0, 1, 0), texc (1, 0))); |
153 | pts.push_back (vertex_t2f_n3f_v3f (point (size, 0, 0), vec3 (0, 1, 0), tex2 (1, 0))); |
150 | |
154 | |
151 | geometry_quads *q = new geometry_quads; |
155 | geometry_quads *q = new geometry_quads; |
152 | q->m = new simple_material; |
156 | //q->m = new simple_material; |
153 | q->set (pts); |
157 | q->set (pts); |
154 | entity *e = new entity (q); |
158 | entity *e = new entity (q); |
155 | e->move (vec3 (dx + x * size, dy, dz + z * size)); |
159 | e->move (vec3 (dx + x * size, dy, dz + z * size)); |
156 | e->show (); |
160 | e->show (); |
157 | } |
161 | } |
… | |
… | |
163 | geometry_nurbs *q = new geometry_nurbs; |
167 | geometry_nurbs *q = new geometry_nurbs; |
164 | q->set (); |
168 | q->set (); |
165 | entity *e = new entity (q); |
169 | entity *e = new entity (q); |
166 | e->move (vec3 (10, 3, -4)); |
170 | e->move (vec3 (10, 3, -4)); |
167 | e->show (); |
171 | e->show (); |
|
|
172 | } |
|
|
173 | |
|
|
174 | void fisch (vec3 center, GLfloat radius, int depth) |
|
|
175 | { |
|
|
176 | entity *planet = new entity (new geometry_sphere (testmat, radius)); |
|
|
177 | planet->move (center); |
|
|
178 | planet->show (); |
|
|
179 | |
|
|
180 | if (--depth) |
|
|
181 | { |
|
|
182 | GLfloat r2 = radius * .1F; |
|
|
183 | GLfloat r3 = radius + r2; |
|
|
184 | |
|
|
185 | fisch (center + vec3 (r3, 0, 0), r2, depth); |
|
|
186 | fisch (center + vec3 (0, 0, r3), r2, depth); |
|
|
187 | fisch (center + vec3 (-r3, 0, 0), r2, depth); |
|
|
188 | } |
168 | } |
189 | } |
169 | |
190 | |
170 | extern void draw_level (); |
191 | extern void draw_level (); |
171 | int |
192 | int |
172 | RunGLTest (int argc, char *argv[], |
193 | RunGLTest (int argc, char *argv[], |
… | |
… | |
176 | int rgb_size[3]; |
197 | int rgb_size[3]; |
177 | int w = 640; |
198 | int w = 640; |
178 | int h = 480; |
199 | int h = 480; |
179 | int done = 0; |
200 | int done = 0; |
180 | int frames; |
201 | int frames; |
181 | Uint32 start_time, this_time; |
|
|
182 | Uint32 video_flags; |
202 | Uint32 video_flags; |
183 | int value; |
203 | int value; |
184 | GLenum gl_error; |
204 | GLenum gl_error; |
185 | |
205 | |
186 | if (SDL_Init (SDL_INIT_VIDEO) < 0) |
206 | if (SDL_Init (SDL_INIT_VIDEO) < 0) |
… | |
… | |
231 | } |
251 | } |
232 | |
252 | |
233 | SDL_GL_SetAttribute (SDL_GL_RED_SIZE, rgb_size[0]); |
253 | SDL_GL_SetAttribute (SDL_GL_RED_SIZE, rgb_size[0]); |
234 | SDL_GL_SetAttribute (SDL_GL_GREEN_SIZE, rgb_size[1]); |
254 | SDL_GL_SetAttribute (SDL_GL_GREEN_SIZE, rgb_size[1]); |
235 | SDL_GL_SetAttribute (SDL_GL_BLUE_SIZE, rgb_size[2]); |
255 | SDL_GL_SetAttribute (SDL_GL_BLUE_SIZE, rgb_size[2]); |
236 | SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE, 16); |
256 | SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE, 24); |
237 | SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, 1); |
257 | SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, 1); |
238 | |
258 | |
239 | if (fsaa) |
259 | if (fsaa) |
240 | { |
260 | { |
241 | SDL_GL_SetAttribute (SDL_GL_MULTISAMPLEBUFFERS, 1); |
261 | SDL_GL_SetAttribute (SDL_GL_MULTISAMPLEBUFFERS, 1); |
… | |
… | |
247 | fprintf (stderr, "Couldn't set GL mode: %s\n", SDL_GetError ()); |
267 | fprintf (stderr, "Couldn't set GL mode: %s\n", SDL_GetError ()); |
248 | SDL_Quit (); |
268 | SDL_Quit (); |
249 | exit (1); |
269 | exit (1); |
250 | } |
270 | } |
251 | |
271 | |
252 | printf ("Screen BPP: %d\n", SDL_GetVideoSurface ()->format->BitsPerPixel); |
272 | //printf ("Screen BPP: %d\n", SDL_GetVideoSurface ()->format->BitsPerPixel); |
253 | printf ("\n"); |
273 | //printf ("\n"); |
254 | printf ("Vendor : %s\n", glGetString (GL_VENDOR)); |
274 | //printf ("Vendor : %s\n", glGetString (GL_VENDOR)); |
255 | printf ("Renderer : %s\n", glGetString (GL_RENDERER)); |
275 | //printf ("Renderer : %s\n", glGetString (GL_RENDERER)); |
256 | printf ("Version : %s\n", glGetString (GL_VERSION)); |
276 | //printf ("Version : %s\n", glGetString (GL_VERSION)); |
257 | printf ("Extensions : %s\n", glGetString (GL_EXTENSIONS)); |
277 | //printf ("Extensions : %s\n", glGetString (GL_EXTENSIONS)); |
258 | printf ("\n"); |
278 | printf ("\n"); |
259 | |
279 | |
260 | SDL_GL_GetAttribute (SDL_GL_RED_SIZE, &value); |
280 | SDL_GL_GetAttribute (SDL_GL_RED_SIZE, &value); |
261 | printf ("SDL_GL_RED_SIZE: requested %d, got %d\n", rgb_size[0], value); |
281 | printf ("SDL_GL_RED_SIZE: requested %d, got %d\n", rgb_size[0], value); |
262 | SDL_GL_GetAttribute (SDL_GL_GREEN_SIZE, &value); |
282 | SDL_GL_GetAttribute (SDL_GL_GREEN_SIZE, &value); |
… | |
… | |
282 | |
302 | |
283 | /* Set the gamma for the window */ |
303 | /* Set the gamma for the window */ |
284 | if (gamma != 0.0) |
304 | if (gamma != 0.0) |
285 | SDL_SetGamma (gamma, gamma, gamma); |
305 | SDL_SetGamma (gamma, gamma, gamma); |
286 | |
306 | |
|
|
307 | testmat = new test_material; |
|
|
308 | |
287 | entity *planet = new entity (new geometry_sphere (10)); |
309 | entity *planet = new entity (new geometry_sphere (testmat, 10)); |
288 | planet->move (vec3 (0, 0, -20)); |
310 | planet->move (vec3 (0, 0, -20)); |
289 | planet->show (); |
311 | planet->show (); |
290 | |
312 | |
291 | #if 1 |
313 | doom3parse ("test.proc"); |
|
|
314 | |
292 | for (int i = 0; i < 20; i++) |
315 | for (int i = 0; i < 20; i++) |
293 | { |
316 | { |
294 | // load a entity |
317 | // load a entity |
295 | txtprt_parser p; |
318 | txtprt_parser p; |
296 | geometry *g; |
319 | geometry *g; |
… | |
… | |
306 | entity *e = new entity (g); |
329 | entity *e = new entity (g); |
307 | e->move (vec3 (i*5, -3, -i*10)); |
330 | e->move (vec3 (i*5, -3, -i*10)); |
308 | e->show (); |
331 | e->show (); |
309 | } |
332 | } |
310 | |
333 | |
311 | { |
334 | { |
|
|
335 | fisch (vec3 (0, 0, -2e9), 1e9, 8); |
312 | entity *planet = new entity (new geometry_sphere (1e9)); |
336 | //entity *planet = new entity (new geometry_sphere (1e9)); |
313 | planet->move (vec3 (0, 0, -1.5e9)); |
337 | //planet->move (vec3 (0, 0, -1.5e9)); |
314 | planet->show (); |
338 | //planet->show (); |
315 | } |
339 | } |
316 | |
340 | |
317 | { |
341 | { |
318 | entity *planet = new entity (new geometry_sphere (4e15)); |
342 | entity *planet = new entity (new geometry_sphere (testmat, 4e15)); |
319 | planet->move (vec3 (0, 0, 1e17)); |
343 | planet->move (vec3 (0, 0, 1e17)); |
320 | planet->show (); |
344 | planet->show (); |
321 | } |
345 | } |
322 | |
346 | |
323 | draw_floor (10, -500, -10, -1000); |
347 | //draw_floor (10, -500, -10, -1000); |
324 | draw_level (); |
348 | draw_level (); |
|
|
349 | |
|
|
350 | #if 0 |
|
|
351 | { |
|
|
352 | geometry_heightfield *hf = new geometry_heightfield (100000., 100000.); |
|
|
353 | entity *e = new entity (hf); |
|
|
354 | e->move (vec3 (-100000, -100000 * 0.01, -100000)); |
|
|
355 | e->show (); |
|
|
356 | } |
|
|
357 | #endif |
|
|
358 | |
325 | //draw_test_nurb (); |
359 | //draw_test_nurb (); |
326 | #endif |
|
|
327 | |
360 | |
328 | //camera.orig.x = 108; camera.orig.y = 0; camera.orig.z = -368; |
361 | //camera.orig.x = 108; camera.orig.y = 0; camera.orig.z = -368; |
329 | camera.orig.x = 0; camera.orig.y = 0; camera.orig.z = 0; |
362 | camera.orig.x = 0; camera.orig.y = 0; camera.orig.z = 0; |
330 | camera.p = point (0, 0, 10); |
363 | camera.p = point (0, 0, 10); |
331 | camera.d = vec3 (0, 0, -1); |
364 | camera.d = vec3 (0, 0, -1); |
332 | camera.u = vec3 (0, 1, 0); |
365 | camera.u = vec3 (0, 1, 0); |
333 | camera.w = w; camera.h = h; |
366 | camera.w = w; camera.h = h; |
334 | camera.fov = 35; |
367 | camera.fov = 40.; |
335 | camera.z_near = 1.; |
|
|
336 | |
368 | |
337 | glEnable (GL_CULL_FACE); |
369 | glEnable (GL_CULL_FACE); |
338 | |
370 | glDisable (GL_ALPHA_TEST); |
339 | init_shaders (); |
371 | glDisable (GL_BLEND); |
340 | |
|
|
341 | osama_material osa_mat; |
|
|
342 | |
372 | |
343 | /* Loop until done. */ |
373 | /* Loop until done. */ |
344 | start_time = SDL_GetTicks (); |
|
|
345 | frames = 0; |
374 | frames = 0; |
|
|
375 | |
|
|
376 | linear_light mylight; |
|
|
377 | mylight.radius = 100000; |
|
|
378 | mylight.c = colour (1., 1, 1, 1.); |
|
|
379 | mylight.intensity = 1.F; |
|
|
380 | |
|
|
381 | pass pass_light (&mylight); |
346 | |
382 | |
347 | while (!done) |
383 | while (!done) |
348 | { |
384 | { |
349 | char *sdl_error; |
385 | char *sdl_error; |
350 | SDL_Event event; |
386 | SDL_Event event; |
351 | |
387 | |
352 | camera_angle += 180 * camera_velocity_angle * timer.diff; |
388 | camera_angle += 90 * camera_velocity_angle * timer.diff; |
353 | camera_angle2 += 180 * camera_velocity_angle2 * timer.diff; |
389 | camera_angle2 += 90 * camera_velocity_angle2 * timer.diff; |
354 | |
390 | |
355 | vec3 geradeaus = matrix::rotation (-camera_angle, vec3 (0, 1, 0)) * vec3 (0, 0, -1); |
391 | vec3 geradeaus = matrix::rotation (-camera_angle, vec3 (0, 1, 0)) * vec3 (0, 0, -1); |
356 | vec3 right = matrix::rotation (90., vec3 (0, 1, 0)) * geradeaus; |
392 | vec3 right = matrix::rotation (90., vec3 (0, 1, 0)) * geradeaus; |
357 | |
393 | |
358 | camera.d = matrix::rotation (camera_angle2, right) * geradeaus; |
394 | camera.d = matrix::rotation (camera_angle2, right) * geradeaus; |
359 | camera.u = cross (camera.d, right); |
395 | camera.u = cross (camera.d, right); |
360 | |
396 | |
361 | camera.p = camera.p - camera.d * (camera_velocity_factor * timer.diff) * camera_velocity.z; |
397 | camera.p = camera.p - camera.d * (camera_velocity_factor * timer.diff) * camera_velocity.z; |
362 | camera.p = camera.p - camera.u * (camera_velocity_factor * timer.diff) * camera_velocity.y; |
398 | camera.p = camera.p - camera.u * (camera_velocity_factor * timer.diff) * camera_velocity.y; |
363 | |
399 | |
364 | osa_mat.begin (); |
400 | mylight.orig = camera.orig; |
|
|
401 | mylight.p = camera.p; |
365 | |
402 | |
|
|
403 | //mylight.intensity = max (sinf (timer.now) + 1.2F, 0.2F); |
|
|
404 | |
366 | camera.begin (); |
405 | camera.begin (); |
367 | camera.pass (view::DEPTH); |
406 | camera.render (view::DEPTH, pass_depth); |
368 | camera.pass (view::POSTDEPTH); |
407 | camera.render (view::POSTDEPTH, pass_depth); |
369 | camera.pass (view::LIGHTED); |
408 | camera.render (view::LIGHTED, pass_light); |
370 | camera.end (); |
409 | camera.end (); |
371 | |
410 | |
372 | SDL_GL_SwapBuffers (); |
411 | SDL_GL_SwapBuffers (); |
373 | timer.frame (); |
412 | timer.frame (); |
374 | |
413 | |
375 | osa_mat.end (); |
414 | #if 0 |
376 | /* Check for error conditions. */ |
415 | /* Check for error conditions. */ |
377 | gl_error = glGetError (); |
416 | gl_error = glGetError (); |
378 | |
417 | |
379 | if (gl_error != GL_NO_ERROR) fprintf (stderr, "testgl: OpenGL error: %d\n", gl_error); |
418 | if (gl_error != GL_NO_ERROR) fprintf (stderr, "testgl: OpenGL error: %d\n", gl_error); |
380 | |
419 | |
… | |
… | |
386 | SDL_ClearError (); |
425 | SDL_ClearError (); |
387 | } |
426 | } |
388 | |
427 | |
389 | /* Allow the user to see what's happening */ |
428 | /* Allow the user to see what's happening */ |
390 | //SDL_Delay (40); |
429 | //SDL_Delay (40); |
|
|
430 | #endif |
391 | |
431 | |
392 | /* Check if there's a pending event. */ |
432 | /* Check if there's a pending event. */ |
393 | while (SDL_PollEvent (&event)) |
433 | while (SDL_PollEvent (&event)) |
394 | done = HandleEvent (&event); |
434 | done = HandleEvent (&event); |
395 | |
435 | |
396 | |
|
|
397 | ++frames; |
436 | ++frames; |
398 | } |
|
|
399 | |
|
|
400 | /* Print out the frames per second */ |
|
|
401 | this_time = SDL_GetTicks (); |
|
|
402 | if (this_time != start_time) |
|
|
403 | { |
|
|
404 | printf ("%2.2f FPS\n", |
|
|
405 | ((float) frames / (this_time - start_time)) * 1000.0); |
|
|
406 | } |
437 | } |
407 | |
438 | |
408 | if (global_image) |
439 | if (global_image) |
409 | { |
440 | { |
410 | SDL_FreeSurface (global_image); |
441 | SDL_FreeSurface (global_image); |
411 | global_image = NULL; |
442 | global_image = NULL; |
412 | } |
443 | } |
|
|
444 | |
413 | if (global_texture) |
445 | if (global_texture) |
414 | { |
446 | { |
415 | glDeleteTextures (1, &global_texture); |
447 | glDeleteTextures (1, &global_texture); |
416 | global_texture = 0; |
448 | global_texture = 0; |
417 | } |
449 | } |