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 | |
|
|
14 | #include "util.h" |
12 | #include "entity.h" |
15 | #include "entity.h" |
13 | |
|
|
14 | #include "txtprt_import.h" |
16 | #include "txtprt_import.h" |
|
|
17 | |
|
|
18 | #include "shader.h" |
15 | |
19 | |
16 | /**********************************************************************/ |
20 | /**********************************************************************/ |
17 | |
21 | |
18 | view camera; |
22 | view camera; |
19 | vec3 camera_velocity; |
23 | vec3 camera_velocity; |
|
|
24 | float camera_angle, camera_angle2, camera_velocity_angle, camera_velocity_angle2; |
20 | float camera_velocity_factor = 10; |
25 | float camera_velocity_factor = 80; |
21 | |
26 | |
22 | void |
27 | void |
23 | HotKey_ToggleFullScreen (void) |
28 | HotKey_ToggleFullScreen (void) |
24 | { |
29 | { |
25 | SDL_Surface *screen; |
30 | SDL_Surface *screen; |
26 | |
31 | |
27 | screen = SDL_GetVideoSurface (); |
32 | screen = SDL_GetVideoSurface (); |
|
|
33 | |
28 | if (SDL_WM_ToggleFullScreen (screen)) |
34 | if (SDL_WM_ToggleFullScreen (screen)) |
29 | printf ("Toggled fullscreen mode - now %s\n", |
35 | printf ("Toggled fullscreen mode - now %s\n", |
30 | (screen->flags & SDL_FULLSCREEN) ? "fullscreen" : "windowed"); |
36 | (screen->flags & SDL_FULLSCREEN) ? "fullscreen" : "windowed"); |
31 | else |
37 | else |
32 | printf ("Unable to toggle fullscreen mode\n"); |
38 | printf ("Unable to toggle fullscreen mode\n"); |
… | |
… | |
37 | { |
43 | { |
38 | SDL_GrabMode mode; |
44 | SDL_GrabMode mode; |
39 | |
45 | |
40 | printf ("Ctrl-G: toggling input grab!\n"); |
46 | printf ("Ctrl-G: toggling input grab!\n"); |
41 | mode = SDL_WM_GrabInput (SDL_GRAB_QUERY); |
47 | mode = SDL_WM_GrabInput (SDL_GRAB_QUERY); |
|
|
48 | |
42 | if (mode == SDL_GRAB_ON) |
49 | if (mode == SDL_GRAB_ON) |
43 | printf ("Grab was on\n"); |
50 | printf ("Grab was on\n"); |
44 | else |
51 | else |
45 | printf ("Grab was off\n"); |
52 | printf ("Grab was off\n"); |
46 | |
53 | |
47 | mode = SDL_WM_GrabInput (mode ? SDL_GRAB_OFF : SDL_GRAB_ON); |
54 | mode = SDL_WM_GrabInput (mode ? SDL_GRAB_OFF : SDL_GRAB_ON); |
|
|
55 | |
48 | if (mode == SDL_GRAB_ON) |
56 | if (mode == SDL_GRAB_ON) |
49 | printf ("Grab is now on\n"); |
57 | printf ("Grab is now on\n"); |
50 | else |
58 | else |
51 | printf ("Grab is now off\n"); |
59 | printf ("Grab is now off\n"); |
52 | } |
60 | } |
… | |
… | |
62 | HandleEvent (SDL_Event * event) |
70 | HandleEvent (SDL_Event * event) |
63 | { |
71 | { |
64 | int done; |
72 | int done; |
65 | |
73 | |
66 | done = 0; |
74 | done = 0; |
|
|
75 | |
67 | switch (event->type) |
76 | switch (event->type) |
68 | { |
77 | { |
69 | case SDL_ACTIVEEVENT: |
78 | case SDL_ACTIVEEVENT: |
70 | /* See what happened */ |
79 | /* See what happened */ |
71 | printf ("app %s ", event->active.gain ? "gained" : "lost"); |
80 | printf ("app %s ", event->active.gain ? "gained" : "lost"); |
72 | if (event->active.state & SDL_APPACTIVE) |
81 | if (event->active.state & SDL_APPACTIVE) |
73 | { |
|
|
74 | printf ("active "); |
82 | printf ("active "); |
75 | } |
|
|
76 | else if (event->active.state & SDL_APPMOUSEFOCUS) |
83 | else if (event->active.state & SDL_APPMOUSEFOCUS) |
77 | { |
|
|
78 | printf ("mouse "); |
84 | printf ("mouse "); |
79 | } |
|
|
80 | else if (event->active.state & SDL_APPINPUTFOCUS) |
85 | else if (event->active.state & SDL_APPINPUTFOCUS) |
81 | { |
|
|
82 | printf ("input "); |
86 | printf ("input "); |
83 | } |
|
|
84 | |
87 | |
85 | printf ("focus\n"); |
88 | printf ("focus\n"); |
86 | break; |
89 | break; |
87 | |
90 | |
88 | #define VELOCITY 10 |
|
|
89 | case SDL_KEYDOWN: |
91 | case SDL_KEYDOWN: |
90 | if (event->key.keysym.sym == SDLK_UP) camera_velocity.z--; |
92 | if (event->key.keysym.sym == SDLK_UP) camera_velocity.z--; |
91 | if (event->key.keysym.sym == SDLK_DOWN) camera_velocity.z++; |
93 | if (event->key.keysym.sym == SDLK_DOWN) camera_velocity.z++; |
92 | if (event->key.keysym.sym == SDLK_LEFT) camera_velocity.x--; |
|
|
93 | if (event->key.keysym.sym == SDLK_RIGHT) camera_velocity.x++; |
94 | if (event->key.keysym.sym == SDLK_RIGHT) camera_velocity_angle++; |
|
|
95 | if (event->key.keysym.sym == SDLK_LEFT) camera_velocity_angle--; |
|
|
96 | if (event->key.keysym.sym == SDLK_PAGEUP) camera_velocity_angle2++; |
|
|
97 | if (event->key.keysym.sym == SDLK_PAGEDOWN) camera_velocity_angle2--; |
94 | if (event->key.keysym.sym == SDLK_a) camera_velocity.y--; |
98 | if (event->key.keysym.sym == SDLK_e) camera_velocity.y--; |
95 | if (event->key.keysym.sym == SDLK_s) camera_velocity.y++; |
99 | if (event->key.keysym.sym == SDLK_q) camera_velocity.y++; |
96 | if (event->key.keysym.sym == SDLK_v) camera_velocity_factor *= 1.5; |
100 | if (event->key.keysym.sym == SDLK_v) camera_velocity_factor *= 1.5; |
97 | if (event->key.keysym.sym == SDLK_b) camera_velocity_factor /= 1.5; |
101 | if (event->key.keysym.sym == SDLK_b) camera_velocity_factor /= 1.5; |
98 | |
102 | |
99 | if (event->key.keysym.sym == SDLK_ESCAPE) |
103 | if (event->key.keysym.sym == SDLK_ESCAPE) |
100 | done = 1; |
104 | done = 1; |
101 | |
105 | |
102 | if ((event->key.keysym.sym == SDLK_g) && |
106 | if ((event->key.keysym.sym == SDLK_g) && |
… | |
… | |
114 | break; |
118 | break; |
115 | |
119 | |
116 | case SDL_KEYUP: |
120 | case SDL_KEYUP: |
117 | if (event->key.keysym.sym == SDLK_UP) camera_velocity.z++; |
121 | if (event->key.keysym.sym == SDLK_UP) camera_velocity.z++; |
118 | if (event->key.keysym.sym == SDLK_DOWN) camera_velocity.z--; |
122 | if (event->key.keysym.sym == SDLK_DOWN) camera_velocity.z--; |
119 | if (event->key.keysym.sym == SDLK_LEFT) camera_velocity.x++; |
123 | if (event->key.keysym.sym == SDLK_LEFT) camera_velocity_angle++; |
120 | if (event->key.keysym.sym == SDLK_RIGHT) camera_velocity.x--; |
124 | if (event->key.keysym.sym == SDLK_RIGHT) camera_velocity_angle--; |
|
|
125 | if (event->key.keysym.sym == SDLK_PAGEUP) camera_velocity_angle2--; |
|
|
126 | if (event->key.keysym.sym == SDLK_PAGEDOWN) camera_velocity_angle2++; |
121 | if (event->key.keysym.sym == SDLK_a) camera_velocity.y++; |
127 | if (event->key.keysym.sym == SDLK_e) camera_velocity.y++; |
122 | if (event->key.keysym.sym == SDLK_s) camera_velocity.y--; |
128 | if (event->key.keysym.sym == SDLK_q) camera_velocity.y--; |
123 | break; |
129 | break; |
124 | |
130 | |
125 | case SDL_QUIT: |
131 | case SDL_QUIT: |
126 | done = 1; |
132 | done = 1; |
127 | break; |
133 | break; |
128 | } |
134 | } |
129 | |
135 | |
130 | return (done); |
136 | return (done); |
131 | } |
137 | } |
132 | |
138 | |
133 | /* Quick utility function for texture creation */ |
139 | void draw_floor (int size, int dx, int dy, int dz) |
134 | static int |
|
|
135 | power_of_two (int input) |
|
|
136 | { |
140 | { |
137 | int value = 1; |
141 | int x, z, ry; |
138 | |
142 | |
139 | while (value < input) |
143 | for (x = 0; x < 10; x++) |
140 | { |
|
|
141 | value <<= 1; |
|
|
142 | } |
144 | { |
143 | return value; |
145 | for (z = 0; z < 10; z++) |
144 | } |
146 | { |
|
|
147 | vector<vertex_t2f_n3f_v3f> pts; |
|
|
148 | pts.push_back (vertex_t2f_n3f_v3f (point ( 0, 0, 0), vec3 (0, 1, 0), tex2 (0, 0))); |
|
|
149 | pts.push_back (vertex_t2f_n3f_v3f (point ( 0, 0, size), vec3 (0, 1, 0), tex2 (0, 1))); |
|
|
150 | pts.push_back (vertex_t2f_n3f_v3f (point (size, 0, size), vec3 (0, 1, 0), tex2 (1, 1))); |
|
|
151 | pts.push_back (vertex_t2f_n3f_v3f (point (size, 0, 0), vec3 (0, 1, 0), tex2 (1, 0))); |
145 | |
152 | |
146 | GLuint |
153 | geometry_quads *q = new geometry_quads; |
147 | SDL_GL_LoadTexture (SDL_Surface * surface, GLfloat * texcoord) |
154 | //q->m = new simple_material; |
148 | { |
155 | q->set (pts); |
149 | GLuint texture; |
156 | entity *e = new entity (q); |
150 | int w, h; |
157 | e->move (vec3 (dx + x * size, dy, dz + z * size)); |
151 | SDL_Surface *image; |
158 | e->show (); |
152 | SDL_Rect area; |
159 | } |
153 | Uint32 saved_flags; |
|
|
154 | Uint8 saved_alpha; |
|
|
155 | |
|
|
156 | /* Use the surface width and height expanded to powers of 2 */ |
|
|
157 | w = power_of_two (surface->w); |
|
|
158 | h = power_of_two (surface->h); |
|
|
159 | texcoord[0] = 0.0f; /* Min X */ |
|
|
160 | texcoord[1] = 0.0f; /* Min Y */ |
|
|
161 | texcoord[2] = (GLfloat) surface->w / w; /* Max X */ |
|
|
162 | texcoord[3] = (GLfloat) surface->h / h; /* Max Y */ |
|
|
163 | |
|
|
164 | image = SDL_CreateRGBSurface (SDL_SWSURFACE, w, h, 32, |
|
|
165 | #if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */ |
|
|
166 | 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000 |
|
|
167 | #else |
|
|
168 | 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF |
|
|
169 | #endif |
|
|
170 | ); |
|
|
171 | if (image == NULL) |
|
|
172 | { |
160 | } |
173 | return 0; |
161 | } |
|
|
162 | |
|
|
163 | void draw_test_nurb () |
|
|
164 | { |
|
|
165 | geometry_nurbs *q = new geometry_nurbs; |
|
|
166 | q->set (); |
|
|
167 | entity *e = new entity (q); |
|
|
168 | e->move (vec3 (10, 3, -4)); |
|
|
169 | e->show (); |
|
|
170 | } |
|
|
171 | |
|
|
172 | void fisch (vec3 center, GLfloat radius, int depth) |
|
|
173 | { |
|
|
174 | entity *planet = new entity (new geometry_sphere (testmat, radius)); |
|
|
175 | planet->move (center); |
|
|
176 | planet->show (); |
|
|
177 | |
|
|
178 | if (--depth) |
174 | } |
179 | { |
|
|
180 | GLfloat r2 = radius * .1F; |
|
|
181 | GLfloat r3 = radius + r2; |
175 | |
182 | |
176 | /* Save the alpha blending attributes */ |
183 | fisch (center + vec3 (r3, 0, 0), r2, depth); |
177 | saved_flags = surface->flags & (SDL_SRCALPHA | SDL_RLEACCELOK); |
184 | fisch (center + vec3 (0, 0, r3), r2, depth); |
178 | saved_alpha = surface->format->alpha; |
185 | fisch (center + vec3 (-r3, 0, 0), r2, depth); |
179 | if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA) |
|
|
180 | { |
186 | } |
181 | SDL_SetAlpha (surface, 0, 0); |
|
|
182 | } |
|
|
183 | |
|
|
184 | /* Copy the surface into the GL texture image */ |
|
|
185 | area.x = 0; |
|
|
186 | area.y = 0; |
|
|
187 | area.w = surface->w; |
|
|
188 | area.h = surface->h; |
|
|
189 | SDL_BlitSurface (surface, &area, image, &area); |
|
|
190 | |
|
|
191 | /* Restore the alpha blending attributes */ |
|
|
192 | if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA) |
|
|
193 | { |
|
|
194 | SDL_SetAlpha (surface, saved_flags, saved_alpha); |
|
|
195 | } |
|
|
196 | |
|
|
197 | /* Create an OpenGL texture for the image */ |
|
|
198 | glGenTextures (1, &texture); |
|
|
199 | glBindTexture (GL_TEXTURE_2D, texture); |
|
|
200 | glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
|
|
201 | glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
|
|
202 | glTexImage2D (GL_TEXTURE_2D, |
|
|
203 | 0, |
|
|
204 | GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels); |
|
|
205 | SDL_FreeSurface (image); /* No longer needed */ |
|
|
206 | |
|
|
207 | return texture; |
|
|
208 | } |
187 | } |
209 | |
188 | |
|
|
189 | extern void draw_level (); |
210 | int |
190 | int |
211 | RunGLTest (int argc, char *argv[], |
191 | RunGLTest (int argc, char *argv[], |
212 | int logo, int slowly, int bpp, float gamma, int noframe, int fsaa) |
192 | int logo, int slowly, int bpp, float gamma, int noframe, int fsaa) |
213 | { |
193 | { |
214 | int i; |
194 | int i; |
215 | int rgb_size[3]; |
195 | int rgb_size[3]; |
216 | int w = 640; |
196 | int w = 640; |
217 | int h = 480; |
197 | int h = 480; |
218 | int done = 0; |
198 | int done = 0; |
219 | int frames; |
199 | int frames; |
220 | Uint32 start_time, this_time; |
|
|
221 | Uint32 video_flags; |
200 | Uint32 video_flags; |
222 | int value; |
201 | int value; |
|
|
202 | GLenum gl_error; |
223 | |
203 | |
224 | if (SDL_Init (SDL_INIT_VIDEO) < 0) |
204 | if (SDL_Init (SDL_INIT_VIDEO) < 0) |
225 | { |
205 | { |
226 | fprintf (stderr, "Couldn't initialize SDL: %s\n", SDL_GetError ()); |
206 | fprintf (stderr, "Couldn't initialize SDL: %s\n", SDL_GetError ()); |
227 | exit (1); |
207 | exit (1); |
… | |
… | |
269 | } |
249 | } |
270 | |
250 | |
271 | SDL_GL_SetAttribute (SDL_GL_RED_SIZE, rgb_size[0]); |
251 | SDL_GL_SetAttribute (SDL_GL_RED_SIZE, rgb_size[0]); |
272 | SDL_GL_SetAttribute (SDL_GL_GREEN_SIZE, rgb_size[1]); |
252 | SDL_GL_SetAttribute (SDL_GL_GREEN_SIZE, rgb_size[1]); |
273 | SDL_GL_SetAttribute (SDL_GL_BLUE_SIZE, rgb_size[2]); |
253 | SDL_GL_SetAttribute (SDL_GL_BLUE_SIZE, rgb_size[2]); |
274 | SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE, 16); |
254 | SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE, 24); |
275 | SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, 1); |
255 | SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, 1); |
276 | |
256 | |
277 | if (fsaa) |
257 | if (fsaa) |
278 | { |
258 | { |
279 | SDL_GL_SetAttribute (SDL_GL_MULTISAMPLEBUFFERS, 1); |
259 | SDL_GL_SetAttribute (SDL_GL_MULTISAMPLEBUFFERS, 1); |
… | |
… | |
285 | fprintf (stderr, "Couldn't set GL mode: %s\n", SDL_GetError ()); |
265 | fprintf (stderr, "Couldn't set GL mode: %s\n", SDL_GetError ()); |
286 | SDL_Quit (); |
266 | SDL_Quit (); |
287 | exit (1); |
267 | exit (1); |
288 | } |
268 | } |
289 | |
269 | |
290 | printf ("Screen BPP: %d\n", SDL_GetVideoSurface ()->format->BitsPerPixel); |
270 | //printf ("Screen BPP: %d\n", SDL_GetVideoSurface ()->format->BitsPerPixel); |
291 | printf ("\n"); |
271 | //printf ("\n"); |
292 | printf ("Vendor : %s\n", glGetString (GL_VENDOR)); |
272 | //printf ("Vendor : %s\n", glGetString (GL_VENDOR)); |
293 | printf ("Renderer : %s\n", glGetString (GL_RENDERER)); |
273 | //printf ("Renderer : %s\n", glGetString (GL_RENDERER)); |
294 | printf ("Version : %s\n", glGetString (GL_VERSION)); |
274 | //printf ("Version : %s\n", glGetString (GL_VERSION)); |
295 | printf ("Extensions : %s\n", glGetString (GL_EXTENSIONS)); |
275 | //printf ("Extensions : %s\n", glGetString (GL_EXTENSIONS)); |
296 | printf ("\n"); |
276 | printf ("\n"); |
297 | |
277 | |
298 | SDL_GL_GetAttribute (SDL_GL_RED_SIZE, &value); |
278 | SDL_GL_GetAttribute (SDL_GL_RED_SIZE, &value); |
299 | printf ("SDL_GL_RED_SIZE: requested %d, got %d\n", rgb_size[0], value); |
279 | printf ("SDL_GL_RED_SIZE: requested %d, got %d\n", rgb_size[0], value); |
300 | SDL_GL_GetAttribute (SDL_GL_GREEN_SIZE, &value); |
280 | SDL_GL_GetAttribute (SDL_GL_GREEN_SIZE, &value); |
… | |
… | |
320 | |
300 | |
321 | /* Set the gamma for the window */ |
301 | /* Set the gamma for the window */ |
322 | if (gamma != 0.0) |
302 | if (gamma != 0.0) |
323 | SDL_SetGamma (gamma, gamma, gamma); |
303 | SDL_SetGamma (gamma, gamma, gamma); |
324 | |
304 | |
325 | // load a entity |
305 | testmat = new test_material; |
|
|
306 | |
|
|
307 | #if 0 |
|
|
308 | entity *planet = new entity (new geometry_sphere (10)); |
|
|
309 | planet->move (vec3 (0, 0, -20)); |
|
|
310 | planet->show (); |
|
|
311 | #endif |
|
|
312 | |
326 | for (int i = 0; i < 7; i++) |
313 | for (int i = 0; i < 20; i++) |
327 | { |
314 | { |
|
|
315 | // load a entity |
328 | txtprt_parser p; |
316 | txtprt_parser p; |
329 | entity_transform *f = new entity_transform; |
317 | geometry *g; |
330 | entity *e; |
|
|
331 | try |
318 | try |
332 | { |
319 | { |
333 | e = p.read ("test.blasc"); |
320 | g = p.read ("test.blasc"); |
|
|
321 | } |
334 | } catch (txtprt_i_exception & e) |
322 | catch (txtprt_i_exception & e) |
335 | { |
323 | { |
336 | cout << "ERR: " << e.msg << endl; |
324 | cout << "ERR: " << e.msg << endl; |
337 | } |
325 | } |
338 | f->set (e); |
326 | |
339 | f->update (matrix::translation (vec3 (0, -1, -i*5))); |
327 | entity *e = new entity (g); |
|
|
328 | e->move (vec3 (i*5, -3, -i*10)); |
340 | f->show (); |
329 | e->show (); |
|
|
330 | } |
|
|
331 | |
341 | } |
332 | { |
|
|
333 | fisch (vec3 (0, 0, -2e9), 1e9, 8); |
|
|
334 | //entity *planet = new entity (new geometry_sphere (1e9)); |
|
|
335 | //planet->move (vec3 (0, 0, -1.5e9)); |
|
|
336 | //planet->show (); |
|
|
337 | } |
342 | |
338 | |
|
|
339 | { |
|
|
340 | entity *planet = new entity (new geometry_sphere (testmat, 4e15)); |
|
|
341 | planet->move (vec3 (0, 0, 1e17)); |
|
|
342 | planet->show (); |
|
|
343 | } |
|
|
344 | |
|
|
345 | //draw_floor (10, -500, -10, -1000); |
|
|
346 | draw_level (); |
|
|
347 | |
|
|
348 | |
|
|
349 | #if 0 |
|
|
350 | { |
|
|
351 | geometry_heightfield *hf = new geometry_heightfield (100000., 100000.); |
|
|
352 | entity *e = new entity (hf); |
|
|
353 | e->move (vec3 (-100000, -100000 * 0.01, -100000)); |
|
|
354 | e->show (); |
|
|
355 | } |
|
|
356 | |
|
|
357 | //draw_test_nurb (); |
|
|
358 | #endif |
|
|
359 | |
|
|
360 | //camera.orig.x = 108; camera.orig.y = 0; camera.orig.z = -368; |
343 | camera.orig.x = camera.orig.y = camera.orig.z = 0; |
361 | camera.orig.x = 0; camera.orig.y = 0; camera.orig.z = 0; |
344 | camera.p = point (0, 0, 10); |
362 | camera.p = point (0, 0, 10); |
345 | camera.d = vec3 (0, 0, -1); |
363 | camera.d = vec3 (0, 0, -1); |
346 | camera.u = vec3 (0, 1, 0); |
364 | camera.u = vec3 (0, 1, 0); |
347 | camera.w = w; camera.h = h; |
365 | camera.w = w; camera.h = h; |
348 | camera.fov = 90; |
366 | camera.fov = 40.; |
349 | |
|
|
350 | glMatrixMode (GL_MODELVIEW); |
|
|
351 | glLoadIdentity (); |
|
|
352 | |
367 | |
353 | glEnable (GL_CULL_FACE); |
368 | glEnable (GL_CULL_FACE); |
354 | glEnable (GL_DEPTH_TEST); |
369 | glDisable (GL_ALPHA_TEST); |
355 | glEnable(GL_VERTEX_PROGRAM_ARB); |
370 | glDisable (GL_BLEND); |
356 | glEnable(GL_FRAGMENT_PROGRAM_ARB); |
|
|
357 | |
|
|
358 | glShadeModel (GL_SMOOTH); |
|
|
359 | |
|
|
360 | glEnable (GL_LIGHTING); |
|
|
361 | //GLfloat lightc[4] = { 1, 0.1, 0.1, 1 }; |
|
|
362 | //glLightf (GL_LIGHT0, GL_QUADRATIC_ATTENUATION); |
|
|
363 | //glLightfv (GL_LIGHT0, GL_DIFFUSE, lightc); |
|
|
364 | glEnable (GL_LIGHT0); |
|
|
365 | |
371 | |
366 | /* Loop until done. */ |
372 | /* Loop until done. */ |
367 | start_time = SDL_GetTicks (); |
|
|
368 | frames = 0; |
373 | frames = 0; |
369 | |
374 | |
370 | draw_context c (camera); |
375 | linear_light mylight; |
|
|
376 | mylight.radius = 100000; |
|
|
377 | mylight.c = colour (1., 1, 1, 1.); |
|
|
378 | mylight.intensity = 1.F; |
|
|
379 | |
|
|
380 | pass pass_light (&mylight); |
371 | |
381 | |
372 | while (!done) |
382 | while (!done) |
373 | { |
383 | { |
374 | GLenum gl_error; |
|
|
375 | char *sdl_error; |
384 | char *sdl_error; |
376 | SDL_Event event; |
385 | SDL_Event event; |
377 | |
386 | |
378 | glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
387 | camera_angle += 180 * camera_velocity_angle * timer.diff; |
|
|
388 | camera_angle2 += 180 * camera_velocity_angle2 * timer.diff; |
379 | |
389 | |
380 | camera.p.x += (camera_velocity_factor * camera_velocity.x) * timer.diff; |
390 | vec3 geradeaus = matrix::rotation (-camera_angle, vec3 (0, 1, 0)) * vec3 (0, 0, -1); |
381 | camera.p.y += (camera_velocity_factor * camera_velocity.y) * timer.diff; |
391 | vec3 right = matrix::rotation (90., vec3 (0, 1, 0)) * geradeaus; |
382 | camera.p.z += (camera_velocity_factor * camera_velocity.z) * timer.diff; |
|
|
383 | |
392 | |
384 | GLfloat lightp[4]; |
393 | camera.d = matrix::rotation (camera_angle2, right) * geradeaus; |
|
|
394 | camera.u = cross (camera.d, right); |
|
|
395 | |
|
|
396 | camera.p = camera.p - camera.d * (camera_velocity_factor * timer.diff) * camera_velocity.z; |
|
|
397 | camera.p = camera.p - camera.u * (camera_velocity_factor * timer.diff) * camera_velocity.y; |
|
|
398 | |
|
|
399 | mylight.orig = camera.orig; |
385 | lightp[0] = camera.p.x; |
400 | mylight.p = camera.p; |
386 | lightp[1] = camera.p.y; |
|
|
387 | lightp[2] = camera.p.z; |
|
|
388 | lightp[3] = 1; |
|
|
389 | glLightfv (GL_LIGHT0, GL_POSITION, lightp); |
|
|
390 | |
401 | |
391 | #if 0 |
402 | //mylight.intensity = max (sinf (timer.now) + 1.2F, 0.2F); |
392 | static GLfloat ry; |
403 | |
393 | ry += 0.001; |
|
|
394 | camera.d.x = cos (ry); |
|
|
395 | camera.d.z = sin (ry); |
404 | camera.begin (); |
396 | //camera.d.y = sin (ry * 0.1); |
405 | camera.render (view::DEPTH, pass_depth); |
397 | #endif |
406 | camera.render (view::POSTDEPTH, pass_depth); |
398 | |
407 | camera.render (view::LIGHTED, pass_light); |
399 | c.mode = draw_context::DEPTH; |
|
|
400 | camera.draw (c); |
408 | camera.end (); |
401 | c.mode = draw_context::LIGHTED; |
|
|
402 | camera.draw (c); |
|
|
403 | |
409 | |
404 | SDL_GL_SwapBuffers (); |
410 | SDL_GL_SwapBuffers (); |
405 | timer.frame (); |
411 | timer.frame (); |
406 | |
412 | |
|
|
413 | #if 0 |
407 | /* Check for error conditions. */ |
414 | /* Check for error conditions. */ |
408 | gl_error = glGetError (); |
415 | gl_error = glGetError (); |
409 | |
416 | |
410 | if (gl_error != GL_NO_ERROR) |
|
|
411 | fprintf (stderr, "testgl: OpenGL error: %d\n", gl_error); |
417 | if (gl_error != GL_NO_ERROR) fprintf (stderr, "testgl: OpenGL error: %d\n", gl_error); |
412 | |
418 | |
413 | sdl_error = SDL_GetError (); |
419 | sdl_error = SDL_GetError (); |
414 | |
420 | |
415 | if (sdl_error[0] != '\0') |
421 | if (sdl_error[0] != '\0') |
416 | { |
422 | { |
417 | fprintf (stderr, "testgl: SDL error '%s'\n", sdl_error); |
423 | fprintf (stderr, "testgl: SDL error '%s'\n", sdl_error); |
418 | SDL_ClearError (); |
424 | SDL_ClearError (); |
419 | } |
425 | } |
420 | |
426 | |
421 | /* Allow the user to see what's happening */ |
427 | /* Allow the user to see what's happening */ |
422 | //SDL_Delay (20); |
428 | //SDL_Delay (40); |
|
|
429 | #endif |
423 | |
430 | |
424 | /* Check if there's a pending event. */ |
431 | /* Check if there's a pending event. */ |
425 | while (SDL_PollEvent (&event)) |
432 | while (SDL_PollEvent (&event)) |
426 | done = HandleEvent (&event); |
433 | done = HandleEvent (&event); |
427 | |
434 | |
428 | |
|
|
429 | ++frames; |
435 | ++frames; |
430 | } |
|
|
431 | |
|
|
432 | /* Print out the frames per second */ |
|
|
433 | this_time = SDL_GetTicks (); |
|
|
434 | if (this_time != start_time) |
|
|
435 | { |
|
|
436 | printf ("%2.2f FPS\n", |
|
|
437 | ((float) frames / (this_time - start_time)) * 1000.0); |
|
|
438 | } |
436 | } |
439 | |
437 | |
440 | if (global_image) |
438 | if (global_image) |
441 | { |
439 | { |
442 | SDL_FreeSurface (global_image); |
440 | SDL_FreeSurface (global_image); |
443 | global_image = NULL; |
441 | global_image = NULL; |
444 | } |
442 | } |
|
|
443 | |
445 | if (global_texture) |
444 | if (global_texture) |
446 | { |
445 | { |
447 | glDeleteTextures (1, &global_texture); |
446 | glDeleteTextures (1, &global_texture); |
448 | global_texture = 0; |
447 | global_texture = 0; |
449 | } |
448 | } |
450 | |
449 | |
451 | /* Destroy our GL context, etc. */ |
450 | /* Destroy our GL context, etc. */ |
452 | SDL_Quit (); |
451 | SDL_Quit (); |
453 | return (0); |
452 | return (0); |
454 | } |
453 | } |
455 | |
|
|
456 | int |
454 | int |
457 | main (int argc, char *argv[]) |
455 | main (int argc, char *argv[]) |
458 | { |
456 | { |
459 | int i, logo; |
457 | int i, logo; |
460 | int numtests; |
458 | int numtests; |