… | |
… | |
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 "SDL.h" |
6 | #include "SDL.h" |
7 | |
|
|
8 | #include "SDL_opengl.h" |
7 | #include "SDL_opengl.h" |
9 | |
|
|
10 | /* Undefine this if you want a flat cube instead of a rainbow cube */ |
|
|
11 | #define SHADED_CUBE |
|
|
12 | |
|
|
13 | /* Define this to be the name of the logo image to use with -logo */ |
|
|
14 | #define LOGO_FILE "icon.bmp" |
|
|
15 | |
|
|
16 | /* The SDL_OPENGLBLIT interface is deprecated. |
|
|
17 | The code is still available for benchmark purposes though. |
|
|
18 | */ |
|
|
19 | |
|
|
20 | static SDL_bool USE_DEPRECATED_OPENGLBLIT = SDL_FALSE; |
|
|
21 | |
8 | |
22 | static SDL_Surface *global_image = NULL; |
9 | static SDL_Surface *global_image = NULL; |
23 | static GLuint global_texture = 0; |
10 | static GLuint global_texture = 0; |
24 | |
11 | |
|
|
12 | #include "util.h" |
|
|
13 | #include "entity.h" |
|
|
14 | #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 | |
25 | /**********************************************************************/ |
32 | /**********************************************************************/ |
|
|
33 | |
|
|
34 | view camera; |
|
|
35 | vec3 camera_velocity; |
|
|
36 | float camera_angle = 0, camera_velocity_angle; |
|
|
37 | float camera_velocity_factor = 20; |
26 | |
38 | |
27 | void |
39 | void |
28 | HotKey_ToggleFullScreen (void) |
40 | HotKey_ToggleFullScreen (void) |
29 | { |
41 | { |
30 | SDL_Surface *screen; |
42 | SDL_Surface *screen; |
31 | |
43 | |
32 | screen = SDL_GetVideoSurface (); |
44 | screen = SDL_GetVideoSurface (); |
33 | if (SDL_WM_ToggleFullScreen (screen)) |
45 | if (SDL_WM_ToggleFullScreen (screen)) |
34 | { |
|
|
35 | printf ("Toggled fullscreen mode - now %s\n", |
46 | printf ("Toggled fullscreen mode - now %s\n", |
36 | (screen->flags & SDL_FULLSCREEN) ? "fullscreen" : "windowed"); |
47 | (screen->flags & SDL_FULLSCREEN) ? "fullscreen" : "windowed"); |
37 | } |
|
|
38 | else |
48 | else |
39 | { |
|
|
40 | printf ("Unable to toggle fullscreen mode\n"); |
49 | printf ("Unable to toggle fullscreen mode\n"); |
41 | } |
|
|
42 | } |
50 | } |
43 | |
51 | |
44 | void |
52 | void |
45 | HotKey_ToggleGrab (void) |
53 | HotKey_ToggleGrab (void) |
46 | { |
54 | { |
47 | SDL_GrabMode mode; |
55 | SDL_GrabMode mode; |
48 | |
56 | |
49 | printf ("Ctrl-G: toggling input grab!\n"); |
57 | printf ("Ctrl-G: toggling input grab!\n"); |
50 | mode = SDL_WM_GrabInput (SDL_GRAB_QUERY); |
58 | mode = SDL_WM_GrabInput (SDL_GRAB_QUERY); |
51 | if (mode == SDL_GRAB_ON) |
59 | if (mode == SDL_GRAB_ON) |
52 | { |
|
|
53 | printf ("Grab was on\n"); |
60 | printf ("Grab was on\n"); |
54 | } |
|
|
55 | else |
61 | else |
56 | { |
|
|
57 | printf ("Grab was off\n"); |
62 | printf ("Grab was off\n"); |
58 | } |
|
|
59 | |
63 | |
60 | mode = SDL_WM_GrabInput (mode ? SDL_GRAB_OFF : SDL_GRAB_ON); |
64 | mode = SDL_WM_GrabInput (mode ? SDL_GRAB_OFF : SDL_GRAB_ON); |
61 | if (mode == SDL_GRAB_ON) |
65 | if (mode == SDL_GRAB_ON) |
62 | { |
|
|
63 | printf ("Grab is now on\n"); |
66 | printf ("Grab is now on\n"); |
64 | } |
|
|
65 | else |
67 | else |
66 | { |
|
|
67 | printf ("Grab is now off\n"); |
68 | printf ("Grab is now off\n"); |
68 | } |
|
|
69 | } |
69 | } |
70 | |
70 | |
71 | void |
71 | void |
72 | HotKey_Iconify (void) |
72 | HotKey_Iconify (void) |
73 | { |
73 | { |
… | |
… | |
101 | |
101 | |
102 | printf ("focus\n"); |
102 | printf ("focus\n"); |
103 | break; |
103 | break; |
104 | |
104 | |
105 | case SDL_KEYDOWN: |
105 | case SDL_KEYDOWN: |
|
|
106 | if (event->key.keysym.sym == SDLK_UP) camera_velocity.z--; |
|
|
107 | if (event->key.keysym.sym == SDLK_DOWN) camera_velocity.z++; |
|
|
108 | if (event->key.keysym.sym == SDLK_LEFT) camera_velocity.x--; |
|
|
109 | if (event->key.keysym.sym == SDLK_RIGHT) camera_velocity.x++; |
|
|
110 | if (event->key.keysym.sym == SDLK_a) camera_velocity.y--; |
|
|
111 | if (event->key.keysym.sym == SDLK_s) camera_velocity.y++; |
|
|
112 | if (event->key.keysym.sym == SDLK_v) camera_velocity_factor *= 1.5; |
|
|
113 | if (event->key.keysym.sym == SDLK_b) camera_velocity_factor /= 1.5; |
|
|
114 | if (event->key.keysym.sym == SDLK_e) camera_velocity_angle++; |
|
|
115 | if (event->key.keysym.sym == SDLK_q) camera_velocity_angle--; |
|
|
116 | |
106 | if (event->key.keysym.sym == SDLK_ESCAPE) |
117 | if (event->key.keysym.sym == SDLK_ESCAPE) |
107 | { |
|
|
108 | done = 1; |
118 | done = 1; |
109 | } |
|
|
110 | |
119 | |
111 | if ((event->key.keysym.sym == SDLK_g) && |
120 | if ((event->key.keysym.sym == SDLK_g) && |
112 | (event->key.keysym.mod & KMOD_CTRL)) |
121 | (event->key.keysym.mod & KMOD_CTRL)) |
113 | { |
|
|
114 | HotKey_ToggleGrab (); |
122 | HotKey_ToggleGrab (); |
115 | } |
|
|
116 | |
123 | |
117 | if ((event->key.keysym.sym == SDLK_z) && |
124 | if ((event->key.keysym.sym == SDLK_z) && |
118 | (event->key.keysym.mod & KMOD_CTRL)) |
125 | (event->key.keysym.mod & KMOD_CTRL)) |
119 | { |
|
|
120 | HotKey_Iconify (); |
126 | HotKey_Iconify (); |
121 | } |
|
|
122 | |
127 | |
123 | if ((event->key.keysym.sym == SDLK_RETURN) && |
128 | if ((event->key.keysym.sym == SDLK_RETURN) && |
124 | (event->key.keysym.mod & KMOD_ALT)) |
129 | (event->key.keysym.mod & KMOD_ALT)) |
125 | { |
|
|
126 | HotKey_ToggleFullScreen (); |
130 | HotKey_ToggleFullScreen (); |
127 | } |
|
|
128 | |
131 | |
129 | printf ("key '%s' pressed\n", SDL_GetKeyName (event->key.keysym.sym)); |
132 | break; |
|
|
133 | |
|
|
134 | case SDL_KEYUP: |
|
|
135 | if (event->key.keysym.sym == SDLK_UP) camera_velocity.z++; |
|
|
136 | if (event->key.keysym.sym == SDLK_DOWN) camera_velocity.z--; |
|
|
137 | if (event->key.keysym.sym == SDLK_LEFT) camera_velocity.x++; |
|
|
138 | if (event->key.keysym.sym == SDLK_RIGHT) camera_velocity.x--; |
|
|
139 | if (event->key.keysym.sym == SDLK_a) camera_velocity.y++; |
|
|
140 | if (event->key.keysym.sym == SDLK_s) camera_velocity.y--; |
|
|
141 | if (event->key.keysym.sym == SDLK_e) camera_velocity_angle--; |
|
|
142 | if (event->key.keysym.sym == SDLK_q) camera_velocity_angle++; |
130 | break; |
143 | break; |
131 | |
144 | |
132 | case SDL_QUIT: |
145 | case SDL_QUIT: |
133 | done = 1; |
146 | done = 1; |
134 | break; |
147 | break; |
135 | } |
148 | } |
136 | |
149 | |
137 | return (done); |
150 | return (done); |
138 | } |
|
|
139 | |
|
|
140 | void |
|
|
141 | SDL_GL_Enter2DMode () |
|
|
142 | { |
|
|
143 | SDL_Surface *screen = SDL_GetVideoSurface (); |
|
|
144 | |
|
|
145 | /* Note, there may be other things you need to change, |
|
|
146 | depending on how you have your OpenGL state set up. |
|
|
147 | */ |
|
|
148 | glPushAttrib (GL_ENABLE_BIT); |
|
|
149 | glDisable (GL_DEPTH_TEST); |
|
|
150 | glDisable (GL_CULL_FACE); |
|
|
151 | glEnable (GL_TEXTURE_2D); |
|
|
152 | |
|
|
153 | /* This allows alpha blending of 2D textures with the scene */ |
|
|
154 | glEnable (GL_BLEND); |
|
|
155 | glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
|
|
156 | |
|
|
157 | glViewport (0, 0, screen->w, screen->h); |
|
|
158 | |
|
|
159 | glMatrixMode (GL_PROJECTION); |
|
|
160 | glPushMatrix (); |
|
|
161 | glLoadIdentity (); |
|
|
162 | |
|
|
163 | glOrtho (0.0, (GLdouble) screen->w, (GLdouble) screen->h, 0.0, 0.0, 1.0); |
|
|
164 | |
|
|
165 | glMatrixMode (GL_MODELVIEW); |
|
|
166 | glPushMatrix (); |
|
|
167 | glLoadIdentity (); |
|
|
168 | |
|
|
169 | glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL); |
|
|
170 | } |
|
|
171 | |
|
|
172 | void |
|
|
173 | SDL_GL_Leave2DMode () |
|
|
174 | { |
|
|
175 | glMatrixMode (GL_MODELVIEW); |
|
|
176 | glPopMatrix (); |
|
|
177 | |
|
|
178 | glMatrixMode (GL_PROJECTION); |
|
|
179 | glPopMatrix (); |
|
|
180 | |
|
|
181 | glPopAttrib (); |
|
|
182 | } |
|
|
183 | |
|
|
184 | /* Quick utility function for texture creation */ |
|
|
185 | static int |
|
|
186 | power_of_two (int input) |
|
|
187 | { |
|
|
188 | int value = 1; |
|
|
189 | |
|
|
190 | while (value < input) |
|
|
191 | { |
|
|
192 | value <<= 1; |
|
|
193 | } |
|
|
194 | return value; |
|
|
195 | } |
|
|
196 | |
|
|
197 | GLuint |
|
|
198 | SDL_GL_LoadTexture (SDL_Surface * surface, GLfloat * texcoord) |
|
|
199 | { |
|
|
200 | GLuint texture; |
|
|
201 | int w, h; |
|
|
202 | SDL_Surface *image; |
|
|
203 | SDL_Rect area; |
|
|
204 | Uint32 saved_flags; |
|
|
205 | Uint8 saved_alpha; |
|
|
206 | |
|
|
207 | /* Use the surface width and height expanded to powers of 2 */ |
|
|
208 | w = power_of_two (surface->w); |
|
|
209 | h = power_of_two (surface->h); |
|
|
210 | texcoord[0] = 0.0f; /* Min X */ |
|
|
211 | texcoord[1] = 0.0f; /* Min Y */ |
|
|
212 | texcoord[2] = (GLfloat) surface->w / w; /* Max X */ |
|
|
213 | texcoord[3] = (GLfloat) surface->h / h; /* Max Y */ |
|
|
214 | |
|
|
215 | image = SDL_CreateRGBSurface (SDL_SWSURFACE, w, h, 32, |
|
|
216 | #if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */ |
|
|
217 | 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000 |
|
|
218 | #else |
|
|
219 | 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF |
|
|
220 | #endif |
|
|
221 | ); |
|
|
222 | if (image == NULL) |
|
|
223 | { |
|
|
224 | return 0; |
|
|
225 | } |
|
|
226 | |
|
|
227 | /* Save the alpha blending attributes */ |
|
|
228 | saved_flags = surface->flags & (SDL_SRCALPHA | SDL_RLEACCELOK); |
|
|
229 | saved_alpha = surface->format->alpha; |
|
|
230 | if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA) |
|
|
231 | { |
|
|
232 | SDL_SetAlpha (surface, 0, 0); |
|
|
233 | } |
|
|
234 | |
|
|
235 | /* Copy the surface into the GL texture image */ |
|
|
236 | area.x = 0; |
|
|
237 | area.y = 0; |
|
|
238 | area.w = surface->w; |
|
|
239 | area.h = surface->h; |
|
|
240 | SDL_BlitSurface (surface, &area, image, &area); |
|
|
241 | |
|
|
242 | /* Restore the alpha blending attributes */ |
|
|
243 | if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA) |
|
|
244 | { |
|
|
245 | SDL_SetAlpha (surface, saved_flags, saved_alpha); |
|
|
246 | } |
|
|
247 | |
|
|
248 | /* Create an OpenGL texture for the image */ |
|
|
249 | glGenTextures (1, &texture); |
|
|
250 | glBindTexture (GL_TEXTURE_2D, texture); |
|
|
251 | glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
|
|
252 | glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
|
|
253 | glTexImage2D (GL_TEXTURE_2D, |
|
|
254 | 0, |
|
|
255 | GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels); |
|
|
256 | SDL_FreeSurface (image); /* No longer needed */ |
|
|
257 | |
|
|
258 | return texture; |
|
|
259 | } |
|
|
260 | |
|
|
261 | |
|
|
262 | void |
|
|
263 | DrawLogoTexture (void) |
|
|
264 | { |
|
|
265 | static GLfloat texMinX, texMinY; |
|
|
266 | static GLfloat texMaxX, texMaxY; |
|
|
267 | static int x = 0; |
|
|
268 | static int y = 0; |
|
|
269 | static int w, h; |
|
|
270 | static int delta_x = 1; |
|
|
271 | static int delta_y = 1; |
|
|
272 | static Uint32 last_moved = 0; |
|
|
273 | |
|
|
274 | SDL_Surface *screen = SDL_GetVideoSurface (); |
|
|
275 | |
|
|
276 | if (!global_texture) |
|
|
277 | { |
|
|
278 | SDL_Surface *image; |
|
|
279 | GLfloat texcoord[4]; |
|
|
280 | |
|
|
281 | /* Load the image (could use SDL_image library here) */ |
|
|
282 | image = SDL_LoadBMP (LOGO_FILE); |
|
|
283 | if (image == NULL) |
|
|
284 | { |
|
|
285 | return; |
|
|
286 | } |
|
|
287 | w = image->w; |
|
|
288 | h = image->h; |
|
|
289 | |
|
|
290 | /* Convert the image into an OpenGL texture */ |
|
|
291 | global_texture = SDL_GL_LoadTexture (image, texcoord); |
|
|
292 | |
|
|
293 | /* Make texture coordinates easy to understand */ |
|
|
294 | texMinX = texcoord[0]; |
|
|
295 | texMinY = texcoord[1]; |
|
|
296 | texMaxX = texcoord[2]; |
|
|
297 | texMaxY = texcoord[3]; |
|
|
298 | |
|
|
299 | /* We don't need the original image anymore */ |
|
|
300 | SDL_FreeSurface (image); |
|
|
301 | |
|
|
302 | /* Make sure that the texture conversion is okay */ |
|
|
303 | if (!global_texture) |
|
|
304 | { |
|
|
305 | return; |
|
|
306 | } |
|
|
307 | } |
|
|
308 | |
|
|
309 | /* Move the image around */ |
|
|
310 | x += delta_x; |
|
|
311 | if (x < 0) |
|
|
312 | { |
|
|
313 | x = 0; |
|
|
314 | delta_x = -delta_x; |
|
|
315 | } |
|
|
316 | else if ((x + w) > screen->w) |
|
|
317 | { |
|
|
318 | x = screen->w - w; |
|
|
319 | delta_x = -delta_x; |
|
|
320 | } |
|
|
321 | y += delta_y; |
|
|
322 | if (y < 0) |
|
|
323 | { |
|
|
324 | y = 0; |
|
|
325 | delta_y = -delta_y; |
|
|
326 | } |
|
|
327 | else if ((y + h) > screen->h) |
|
|
328 | { |
|
|
329 | y = screen->h - h; |
|
|
330 | delta_y = -delta_y; |
|
|
331 | } |
|
|
332 | |
|
|
333 | /* Show the image on the screen */ |
|
|
334 | SDL_GL_Enter2DMode (); |
|
|
335 | glBindTexture (GL_TEXTURE_2D, global_texture); |
|
|
336 | glBegin (GL_TRIANGLE_STRIP); |
|
|
337 | glTexCoord2f (texMinX, texMinY); |
|
|
338 | glVertex2i (x, y); |
|
|
339 | glTexCoord2f (texMaxX, texMinY); |
|
|
340 | glVertex2i (x + w, y); |
|
|
341 | glTexCoord2f (texMinX, texMaxY); |
|
|
342 | glVertex2i (x, y + h); |
|
|
343 | glTexCoord2f (texMaxX, texMaxY); |
|
|
344 | glVertex2i (x + w, y + h); |
|
|
345 | glEnd (); |
|
|
346 | SDL_GL_Leave2DMode (); |
|
|
347 | } |
151 | } |
348 | |
152 | |
349 | int |
153 | int |
350 | RunGLTest (int argc, char *argv[], |
154 | RunGLTest (int argc, char *argv[], |
351 | int logo, int slowly, int bpp, float gamma, int noframe, int fsaa) |
155 | int logo, int slowly, int bpp, float gamma, int noframe, int fsaa) |
… | |
… | |
355 | int w = 640; |
159 | int w = 640; |
356 | int h = 480; |
160 | int h = 480; |
357 | int done = 0; |
161 | int done = 0; |
358 | int frames; |
162 | int frames; |
359 | Uint32 start_time, this_time; |
163 | Uint32 start_time, this_time; |
|
|
164 | Uint32 video_flags; |
360 | int value; |
165 | int value; |
|
|
166 | GLenum gl_error; |
361 | |
167 | |
362 | if (SDL_Init (SDL_INIT_VIDEO) < 0) |
168 | if (SDL_Init (SDL_INIT_VIDEO) < 0) |
363 | { |
169 | { |
364 | fprintf (stderr, "Couldn't initialize SDL: %s\n", SDL_GetError ()); |
170 | fprintf (stderr, "Couldn't initialize SDL: %s\n", SDL_GetError ()); |
365 | exit (1); |
171 | exit (1); |
… | |
… | |
367 | |
173 | |
368 | /* See if we should detect the display depth */ |
174 | /* See if we should detect the display depth */ |
369 | if (bpp == 0) |
175 | if (bpp == 0) |
370 | { |
176 | { |
371 | if (SDL_GetVideoInfo ()->vfmt->BitsPerPixel <= 8) |
177 | if (SDL_GetVideoInfo ()->vfmt->BitsPerPixel <= 8) |
372 | { |
|
|
373 | bpp = 8; |
178 | bpp = 8; |
374 | } |
|
|
375 | else |
179 | else |
376 | { |
|
|
377 | bpp = 16; /* More doesn't seem to work */ |
180 | bpp = 16; /* More doesn't seem to work */ |
378 | } |
|
|
379 | } |
|
|
380 | |
|
|
381 | /* Set the flags we want to use for setting the video mode */ |
|
|
382 | if (logo && USE_DEPRECATED_OPENGLBLIT) |
|
|
383 | { |
181 | } |
384 | video_flags = SDL_OPENGLBLIT; |
182 | |
385 | } |
|
|
386 | else |
|
|
387 | { |
|
|
388 | video_flags = SDL_OPENGL; |
183 | video_flags = SDL_OPENGL; |
389 | } |
184 | |
390 | for (i = 1; argv[i]; ++i) |
185 | for (i = 1; argv[i]; ++i) |
391 | { |
|
|
392 | if (strcmp (argv[1], "-fullscreen") == 0) |
186 | if (strcmp (argv[1], "-fullscreen") == 0) |
393 | { |
|
|
394 | video_flags |= SDL_FULLSCREEN; |
187 | video_flags |= SDL_FULLSCREEN; |
395 | } |
|
|
396 | } |
|
|
397 | |
188 | |
398 | if (noframe) |
189 | if (noframe) |
399 | { |
|
|
400 | video_flags |= SDL_NOFRAME; |
190 | video_flags |= SDL_NOFRAME; |
401 | } |
|
|
402 | |
191 | |
403 | /* Initialize the display */ |
192 | /* Initialize the display */ |
404 | switch (bpp) |
193 | switch (bpp) |
405 | { |
194 | { |
406 | case 8: |
195 | case 8: |
407 | rgb_size[0] = 3; |
196 | rgb_size[0] = 3; |
408 | rgb_size[1] = 3; |
197 | rgb_size[1] = 3; |
409 | rgb_size[2] = 2; |
198 | rgb_size[2] = 2; |
410 | break; |
199 | break; |
|
|
200 | |
411 | case 15: |
201 | case 15: |
412 | case 16: |
202 | case 16: |
413 | rgb_size[0] = 5; |
203 | rgb_size[0] = 5; |
414 | rgb_size[1] = 5; |
204 | rgb_size[1] = 5; |
415 | rgb_size[2] = 5; |
205 | rgb_size[2] = 5; |
416 | break; |
206 | break; |
|
|
207 | |
417 | default: |
208 | default: |
418 | rgb_size[0] = 8; |
209 | rgb_size[0] = 8; |
419 | rgb_size[1] = 8; |
210 | rgb_size[1] = 8; |
420 | rgb_size[2] = 8; |
211 | rgb_size[2] = 8; |
421 | break; |
212 | break; |
422 | } |
213 | } |
|
|
214 | |
423 | SDL_GL_SetAttribute (SDL_GL_RED_SIZE, rgb_size[0]); |
215 | SDL_GL_SetAttribute (SDL_GL_RED_SIZE, rgb_size[0]); |
424 | SDL_GL_SetAttribute (SDL_GL_GREEN_SIZE, rgb_size[1]); |
216 | SDL_GL_SetAttribute (SDL_GL_GREEN_SIZE, rgb_size[1]); |
425 | SDL_GL_SetAttribute (SDL_GL_BLUE_SIZE, rgb_size[2]); |
217 | SDL_GL_SetAttribute (SDL_GL_BLUE_SIZE, rgb_size[2]); |
426 | SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE, 16); |
218 | SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE, 16); |
427 | SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, 1); |
219 | SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, 1); |
|
|
220 | |
428 | if (fsaa) |
221 | if (fsaa) |
429 | { |
222 | { |
430 | SDL_GL_SetAttribute (SDL_GL_MULTISAMPLEBUFFERS, 1); |
223 | SDL_GL_SetAttribute (SDL_GL_MULTISAMPLEBUFFERS, 1); |
431 | SDL_GL_SetAttribute (SDL_GL_MULTISAMPLESAMPLES, fsaa); |
224 | SDL_GL_SetAttribute (SDL_GL_MULTISAMPLESAMPLES, fsaa); |
432 | } |
225 | } |
|
|
226 | |
433 | if (SDL_SetVideoMode (w, h, bpp, video_flags) == NULL) |
227 | if (SDL_SetVideoMode (w, h, bpp, video_flags) == NULL) |
434 | { |
228 | { |
435 | fprintf (stderr, "Couldn't set GL mode: %s\n", SDL_GetError ()); |
229 | fprintf (stderr, "Couldn't set GL mode: %s\n", SDL_GetError ()); |
436 | SDL_Quit (); |
230 | SDL_Quit (); |
437 | exit (1); |
231 | exit (1); |
… | |
… | |
453 | printf ("SDL_GL_BLUE_SIZE: requested %d, got %d\n", rgb_size[2], value); |
247 | printf ("SDL_GL_BLUE_SIZE: requested %d, got %d\n", rgb_size[2], value); |
454 | SDL_GL_GetAttribute (SDL_GL_DEPTH_SIZE, &value); |
248 | SDL_GL_GetAttribute (SDL_GL_DEPTH_SIZE, &value); |
455 | printf ("SDL_GL_DEPTH_SIZE: requested %d, got %d\n", bpp, value); |
249 | printf ("SDL_GL_DEPTH_SIZE: requested %d, got %d\n", bpp, value); |
456 | SDL_GL_GetAttribute (SDL_GL_DOUBLEBUFFER, &value); |
250 | SDL_GL_GetAttribute (SDL_GL_DOUBLEBUFFER, &value); |
457 | printf ("SDL_GL_DOUBLEBUFFER: requested 1, got %d\n", value); |
251 | printf ("SDL_GL_DOUBLEBUFFER: requested 1, got %d\n", value); |
|
|
252 | |
458 | if (fsaa) |
253 | if (fsaa) |
459 | { |
254 | { |
460 | SDL_GL_GetAttribute (SDL_GL_MULTISAMPLEBUFFERS, &value); |
255 | SDL_GL_GetAttribute (SDL_GL_MULTISAMPLEBUFFERS, &value); |
461 | printf ("SDL_GL_MULTISAMPLEBUFFERS: requested 1, got %d\n", value); |
256 | printf ("SDL_GL_MULTISAMPLEBUFFERS: requested 1, got %d\n", value); |
462 | SDL_GL_GetAttribute (SDL_GL_MULTISAMPLESAMPLES, &value); |
257 | SDL_GL_GetAttribute (SDL_GL_MULTISAMPLESAMPLES, &value); |
463 | printf ("SDL_GL_MULTISAMPLESAMPLES: requested %d, got %d\n", fsaa, |
258 | printf ("SDL_GL_MULTISAMPLESAMPLES: requested %d, got %d\n", fsaa, |
464 | value); |
259 | value); |
465 | } |
260 | } |
466 | |
261 | |
467 | /* Set the window manager title bar */ |
262 | /* Set the window manager title bar */ |
468 | SDL_WM_SetCaption ("SDL GL test", "testgl"); |
263 | SDL_WM_SetCaption ("libgender rendering test", "gendertest"); |
469 | |
264 | |
470 | /* Set the gamma for the window */ |
265 | /* Set the gamma for the window */ |
471 | if (gamma != 0.0) |
266 | if (gamma != 0.0) |
472 | { |
|
|
473 | SDL_SetGamma (gamma, gamma, gamma); |
267 | SDL_SetGamma (gamma, gamma, gamma); |
|
|
268 | |
|
|
269 | for (int i = 0; i < 70; i++) |
474 | } |
270 | { |
|
|
271 | // load a entity |
|
|
272 | txtprt_parser p; |
|
|
273 | entity *e; |
|
|
274 | try |
|
|
275 | { |
|
|
276 | e = p.read ("test.blasc"); |
|
|
277 | } |
|
|
278 | catch (txtprt_i_exception & e) |
|
|
279 | { |
|
|
280 | cout << "ERR: " << e.msg << endl; |
|
|
281 | } |
475 | |
282 | |
476 | glViewport (0, 0, w, h); |
283 | entity_transform *f = new entity_transform; |
477 | glMatrixMode (GL_PROJECTION); |
284 | f->set (e); |
478 | glLoadIdentity (); |
285 | f->update (matrix::translation (vec3 (i*5, -3, -i*10))); |
479 | |
286 | f->show (); |
480 | glOrtho (-2.0, 2.0, -2.0, 2.0, -20.0, 20.0); |
287 | } |
|
|
288 | draw_some_random_funky_floor_dance_music (10, -500, -10, -1000); |
|
|
289 | camera.orig.x = camera.orig.y = camera.orig.z = 0; |
|
|
290 | camera.p = point (0, 0, 10); |
|
|
291 | camera.d = vec3 (0, 0, -1); |
|
|
292 | camera.u = vec3 (0, 1, 0); |
|
|
293 | camera.w = w; camera.h = h; |
|
|
294 | camera.fov = 80; |
481 | |
295 | |
482 | glMatrixMode (GL_MODELVIEW); |
296 | glMatrixMode (GL_MODELVIEW); |
483 | glLoadIdentity (); |
297 | glLoadIdentity (); |
484 | |
298 | |
|
|
299 | glEnable (GL_CULL_FACE); |
485 | glEnable (GL_DEPTH_TEST); |
300 | glEnable (GL_DEPTH_TEST); |
486 | |
301 | |
487 | glDepthFunc (GL_LESS); |
|
|
488 | |
|
|
489 | glShadeModel (GL_SMOOTH); |
302 | glShadeModel (GL_SMOOTH); |
|
|
303 | |
|
|
304 | glEnable (GL_LIGHTING); |
|
|
305 | glEnable (GL_LIGHT0); |
|
|
306 | |
|
|
307 | cgc = cgCreateContext (); |
|
|
308 | |
|
|
309 | vsh_profile = CG_PROFILE_ARBVP1; |
|
|
310 | //if (cgGLIsProfileSupported (CG_PROFILE_VP30)) vsh_profile = CG_PROFILE_VP30; |
|
|
311 | //if (cgGLIsProfileSupported (CG_PROFILE_VP40)) vsh_profile = CG_PROFILE_VP40; |
|
|
312 | fsh_profile = CG_PROFILE_ARBFP1; |
|
|
313 | //if (cgGLIsProfileSupported (CG_PROFILE_FP30)) fsh_profile = CG_PROFILE_FP30; |
|
|
314 | //if (cgGLIsProfileSupported (CG_PROFILE_FP40)) fsh_profile = CG_PROFILE_FP40; |
|
|
315 | |
|
|
316 | vsh = cgCreateProgramFromFile (cgc, CG_SOURCE, "vsh.cg", vsh_profile, 0, 0); |
|
|
317 | CheckCgError (); |
|
|
318 | cgGLLoadProgram (vsh); |
|
|
319 | CheckCgError (); |
|
|
320 | mv = cgGetNamedParameter (vsh, "WorldProj"); |
|
|
321 | mvp = cgGetNamedParameter (vsh, "WorldViewProj"); |
|
|
322 | lightpos = cgGetNamedParameter (vsh, "LightPos"); |
|
|
323 | CheckCgError (); |
|
|
324 | |
|
|
325 | CGparameter g_Texture; // the texture parameter |
|
|
326 | |
|
|
327 | |
|
|
328 | fsh = cgCreateProgramFromFile (cgc, CG_SOURCE, "fsh.cg", fsh_profile, 0, 0); |
|
|
329 | Texture t("o.jpg"); |
|
|
330 | g_Texture = cgGetNamedParameter(fsh, "Texture"); // the texture cg-warper ;) |
|
|
331 | cgGLSetTextureParameter(g_Texture, t.texture); // Bind the texture number 999 to g_Texture |
|
|
332 | CheckCgError (); |
|
|
333 | cgGLLoadProgram (fsh); |
|
|
334 | CheckCgError (); |
|
|
335 | |
|
|
336 | cgGLBindProgram (vsh); |
|
|
337 | CheckCgError (); |
|
|
338 | cgGLBindProgram (fsh); |
|
|
339 | CheckCgError (); |
490 | |
340 | |
491 | /* Loop until done. */ |
341 | /* Loop until done. */ |
492 | start_time = SDL_GetTicks (); |
342 | start_time = SDL_GetTicks (); |
493 | frames = 0; |
343 | frames = 0; |
|
|
344 | |
494 | while (!done) |
345 | while (!done) |
495 | { |
346 | { |
496 | GLenum gl_error; |
|
|
497 | char *sdl_error; |
347 | char *sdl_error; |
498 | SDL_Event event; |
348 | SDL_Event event; |
499 | |
349 | |
500 | /* Do our drawing, too. */ |
350 | camera.p.x += camera_velocity_factor * camera_velocity.x * timer.diff; |
501 | glClearColor (0.0, 0.0, 0.0, 1.0); |
351 | camera.p.y += camera_velocity_factor * camera_velocity.y * timer.diff; |
502 | glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
352 | camera.p.z += camera_velocity_factor * camera_velocity.z * timer.diff; |
503 | |
353 | |
504 | glBegin (GL_QUADS); |
354 | camera_angle += camera_velocity_factor * camera_velocity_angle * timer.diff; |
|
|
355 | camera.d.z = -cos (camera_angle * 12 / 180.); |
|
|
356 | camera.d.x = sin (camera_angle * 12 / 180.); |
505 | |
357 | |
506 | #ifdef SHADED_CUBE |
358 | cgGLSetParameter4f (lightpos, camera.p.x, camera.p.y, camera.p.z, 1); |
507 | glColor3fv (color[0]); |
|
|
508 | glVertex3fv (cube[0]); |
|
|
509 | glColor3fv (color[1]); |
|
|
510 | glVertex3fv (cube[1]); |
|
|
511 | glColor3fv (color[2]); |
|
|
512 | glVertex3fv (cube[2]); |
|
|
513 | glColor3fv (color[3]); |
|
|
514 | glVertex3fv (cube[3]); |
|
|
515 | |
359 | |
516 | glColor3fv (color[3]); |
360 | glBindTexture (GL_TEXTURE_2D, t.texture); |
517 | glVertex3fv (cube[3]); |
361 | cgGLEnableTextureParameter (g_Texture); // Enable the texture parameter |
518 | glColor3fv (color[4]); |
|
|
519 | glVertex3fv (cube[4]); |
|
|
520 | glColor3fv (color[7]); |
|
|
521 | glVertex3fv (cube[7]); |
|
|
522 | glColor3fv (color[2]); |
|
|
523 | glVertex3fv (cube[2]); |
|
|
524 | |
362 | |
525 | glColor3fv (color[0]); |
363 | #if 0 |
526 | glVertex3fv (cube[0]); |
364 | static GLfloat ry; |
527 | glColor3fv (color[5]); |
365 | ry += 0.001; |
528 | glVertex3fv (cube[5]); |
366 | camera.d.x = cos (ry); |
529 | glColor3fv (color[6]); |
367 | camera.d.z = sin (ry); |
530 | glVertex3fv (cube[6]); |
368 | //camera.d.y = sin (ry * 0.1); |
531 | glColor3fv (color[1]); |
369 | #endif |
532 | glVertex3fv (cube[1]); |
|
|
533 | |
370 | |
534 | glColor3fv (color[5]); |
371 | camera.begin (); |
535 | glVertex3fv (cube[5]); |
372 | camera.pass (view::DEPTH); |
536 | glColor3fv (color[4]); |
373 | camera.pass (view::LIGHTED); |
537 | glVertex3fv (cube[4]); |
374 | camera.end (); |
538 | glColor3fv (color[7]); |
|
|
539 | glVertex3fv (cube[7]); |
|
|
540 | glColor3fv (color[6]); |
|
|
541 | glVertex3fv (cube[6]); |
|
|
542 | |
|
|
543 | glColor3fv (color[5]); |
|
|
544 | glVertex3fv (cube[5]); |
|
|
545 | glColor3fv (color[0]); |
|
|
546 | glVertex3fv (cube[0]); |
|
|
547 | glColor3fv (color[3]); |
|
|
548 | glVertex3fv (cube[3]); |
|
|
549 | glColor3fv (color[4]); |
|
|
550 | glVertex3fv (cube[4]); |
|
|
551 | |
|
|
552 | glColor3fv (color[6]); |
|
|
553 | glVertex3fv (cube[6]); |
|
|
554 | glColor3fv (color[1]); |
|
|
555 | glVertex3fv (cube[1]); |
|
|
556 | glColor3fv (color[2]); |
|
|
557 | glVertex3fv (cube[2]); |
|
|
558 | glColor3fv (color[7]); |
|
|
559 | glVertex3fv (cube[7]); |
|
|
560 | #else // flat cube |
|
|
561 | glColor3f (1.0, 0.0, 0.0); |
|
|
562 | glVertex3fv (cube[0]); |
|
|
563 | glVertex3fv (cube[1]); |
|
|
564 | glVertex3fv (cube[2]); |
|
|
565 | glVertex3fv (cube[3]); |
|
|
566 | |
|
|
567 | glColor3f (0.0, 1.0, 0.0); |
|
|
568 | glVertex3fv (cube[3]); |
|
|
569 | glVertex3fv (cube[4]); |
|
|
570 | glVertex3fv (cube[7]); |
|
|
571 | glVertex3fv (cube[2]); |
|
|
572 | |
|
|
573 | glColor3f (0.0, 0.0, 1.0); |
|
|
574 | glVertex3fv (cube[0]); |
|
|
575 | glVertex3fv (cube[5]); |
|
|
576 | glVertex3fv (cube[6]); |
|
|
577 | glVertex3fv (cube[1]); |
|
|
578 | |
|
|
579 | glColor3f (0.0, 1.0, 1.0); |
|
|
580 | glVertex3fv (cube[5]); |
|
|
581 | glVertex3fv (cube[4]); |
|
|
582 | glVertex3fv (cube[7]); |
|
|
583 | glVertex3fv (cube[6]); |
|
|
584 | |
|
|
585 | glColor3f (1.0, 1.0, 0.0); |
|
|
586 | glVertex3fv (cube[5]); |
|
|
587 | glVertex3fv (cube[0]); |
|
|
588 | glVertex3fv (cube[3]); |
|
|
589 | glVertex3fv (cube[4]); |
|
|
590 | |
|
|
591 | glColor3f (1.0, 0.0, 1.0); |
|
|
592 | glVertex3fv (cube[6]); |
|
|
593 | glVertex3fv (cube[1]); |
|
|
594 | glVertex3fv (cube[2]); |
|
|
595 | glVertex3fv (cube[7]); |
|
|
596 | #endif /* SHADED_CUBE */ |
|
|
597 | |
|
|
598 | glEnd (); |
|
|
599 | |
|
|
600 | glMatrixMode (GL_MODELVIEW); |
|
|
601 | glRotatef (5.0, 1.0, 1.0, 1.0); |
|
|
602 | |
375 | |
603 | SDL_GL_SwapBuffers (); |
376 | SDL_GL_SwapBuffers (); |
|
|
377 | timer.frame (); |
604 | |
378 | |
605 | /* Check for error conditions. */ |
379 | /* Check for error conditions. */ |
606 | gl_error = glGetError (); |
380 | gl_error = glGetError (); |
607 | |
381 | |
608 | if (gl_error != GL_NO_ERROR) |
|
|
609 | { |
|
|
610 | fprintf (stderr, "testgl: OpenGL error: %d\n", gl_error); |
382 | if (gl_error != GL_NO_ERROR) fprintf (stderr, "testgl: OpenGL error: %d\n", gl_error); |
611 | } |
|
|
612 | |
383 | |
613 | sdl_error = SDL_GetError (); |
384 | sdl_error = SDL_GetError (); |
614 | |
385 | |
615 | if (sdl_error[0] != '\0') |
386 | if (sdl_error[0] != '\0') |
616 | { |
387 | { |
617 | fprintf (stderr, "testgl: SDL error '%s'\n", sdl_error); |
388 | fprintf (stderr, "testgl: SDL error '%s'\n", sdl_error); |
618 | SDL_ClearError (); |
389 | SDL_ClearError (); |
619 | } |
390 | } |
620 | |
391 | |
621 | /* Allow the user to see what's happening */ |
392 | /* Allow the user to see what's happening */ |
622 | if (slowly) |
|
|
623 | { |
|
|
624 | SDL_Delay (20); |
|
|
625 | } |
|
|
626 | SDL_Delay (20); |
393 | //SDL_Delay (20); |
627 | |
394 | |
628 | /* Check if there's a pending event. */ |
395 | /* Check if there's a pending event. */ |
629 | while (SDL_PollEvent (&event)) |
396 | while (SDL_PollEvent (&event)) |
630 | { |
|
|
631 | done = HandleEvent (&event); |
397 | done = HandleEvent (&event); |
632 | } |
398 | |
|
|
399 | |
633 | ++frames; |
400 | ++frames; |
634 | } |
401 | } |
635 | |
402 | |
636 | /* Print out the frames per second */ |
403 | /* Print out the frames per second */ |
637 | this_time = SDL_GetTicks (); |
404 | this_time = SDL_GetTicks (); |
… | |
… | |
675 | { |
442 | { |
676 | if (strcmp (argv[i], "-twice") == 0) |
443 | if (strcmp (argv[i], "-twice") == 0) |
677 | { |
444 | { |
678 | ++numtests; |
445 | ++numtests; |
679 | } |
446 | } |
680 | if (strcmp (argv[i], "-logo") == 0) |
|
|
681 | { |
|
|
682 | logo = 1; |
|
|
683 | USE_DEPRECATED_OPENGLBLIT = SDL_FALSE; |
|
|
684 | } |
|
|
685 | if (strcmp (argv[i], "-logoblit") == 0) |
|
|
686 | { |
|
|
687 | logo = 1; |
|
|
688 | USE_DEPRECATED_OPENGLBLIT = SDL_TRUE; |
|
|
689 | } |
|
|
690 | if (strcmp (argv[i], "-slow") == 0) |
447 | if (strcmp (argv[i], "-slow") == 0) |
691 | { |
448 | { |
692 | slowly = 1; |
449 | slowly = 1; |
693 | } |
450 | } |
694 | if (strcmp (argv[i], "-bpp") == 0) |
451 | if (strcmp (argv[i], "-bpp") == 0) |
… | |
… | |
713 | ("Usage: %s [-twice] [-logo] [-slow] [-bpp n] [-gamma n] [-noframe] [-fsaa]\n", |
470 | ("Usage: %s [-twice] [-logo] [-slow] [-bpp n] [-gamma n] [-noframe] [-fsaa]\n", |
714 | argv[0]); |
471 | argv[0]); |
715 | exit (0); |
472 | exit (0); |
716 | } |
473 | } |
717 | } |
474 | } |
|
|
475 | |
718 | for (i = 0; i < numtests; ++i) |
476 | for (i = 0; i < numtests; ++i) |
719 | { |
|
|
720 | RunGLTest (argc, argv, logo, slowly, bpp, gamma, noframe, fsaa); |
477 | RunGLTest (argc, argv, logo, slowly, bpp, gamma, noframe, fsaa); |
721 | } |
478 | |
722 | return 0; |
479 | return 0; |
723 | } |
480 | } |