… | |
… | |
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 | |
8 | |
16 | static SDL_Surface *global_image = NULL; |
9 | static SDL_Surface *global_image = NULL; |
17 | static GLuint global_texture = 0; |
10 | static GLuint global_texture = 0; |
18 | |
11 | |
|
|
12 | #include "util.h" |
19 | #include "entity.h" |
13 | #include "entity.h" |
20 | |
|
|
21 | #include "txtprt_import.h" |
14 | #include "txtprt_import.h" |
22 | |
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 | |
23 | /**********************************************************************/ |
32 | /**********************************************************************/ |
24 | |
33 | |
25 | view camera; |
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; |
… | |
… | |
89 | |
101 | |
90 | printf ("focus\n"); |
102 | printf ("focus\n"); |
91 | break; |
103 | break; |
92 | |
104 | |
93 | case SDL_KEYDOWN: |
105 | case SDL_KEYDOWN: |
94 | |
|
|
95 | if (event->key.keysym.sym == SDLK_UP) |
106 | if (event->key.keysym.sym == SDLK_UP) camera_velocity.z--; |
96 | camera.p.z -= 1; |
|
|
97 | if (event->key.keysym.sym == SDLK_DOWN) |
107 | if (event->key.keysym.sym == SDLK_DOWN) camera_velocity.z++; |
98 | camera.p.z += 1; |
|
|
99 | if (event->key.keysym.sym == SDLK_LEFT) |
108 | if (event->key.keysym.sym == SDLK_LEFT) camera_velocity.x--; |
100 | camera.p.x -= 1; |
|
|
101 | if (event->key.keysym.sym == SDLK_RIGHT) |
109 | if (event->key.keysym.sym == SDLK_RIGHT) camera_velocity.x++; |
102 | camera.p.x += 1; |
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--; |
103 | |
116 | |
104 | if (event->key.keysym.sym == SDLK_ESCAPE) |
117 | if (event->key.keysym.sym == SDLK_ESCAPE) |
105 | done = 1; |
118 | done = 1; |
106 | |
119 | |
107 | if ((event->key.keysym.sym == SDLK_g) && |
120 | if ((event->key.keysym.sym == SDLK_g) && |
… | |
… | |
116 | (event->key.keysym.mod & KMOD_ALT)) |
129 | (event->key.keysym.mod & KMOD_ALT)) |
117 | HotKey_ToggleFullScreen (); |
130 | HotKey_ToggleFullScreen (); |
118 | |
131 | |
119 | break; |
132 | break; |
120 | |
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++; |
|
|
143 | break; |
|
|
144 | |
121 | case SDL_QUIT: |
145 | case SDL_QUIT: |
122 | done = 1; |
146 | done = 1; |
123 | break; |
147 | break; |
124 | } |
148 | } |
125 | |
149 | |
126 | return (done); |
150 | return (done); |
127 | } |
|
|
128 | |
|
|
129 | /* Quick utility function for texture creation */ |
|
|
130 | static int |
|
|
131 | power_of_two (int input) |
|
|
132 | { |
|
|
133 | int value = 1; |
|
|
134 | |
|
|
135 | while (value < input) |
|
|
136 | { |
|
|
137 | value <<= 1; |
|
|
138 | } |
|
|
139 | return value; |
|
|
140 | } |
|
|
141 | |
|
|
142 | GLuint |
|
|
143 | SDL_GL_LoadTexture (SDL_Surface * surface, GLfloat * texcoord) |
|
|
144 | { |
|
|
145 | GLuint texture; |
|
|
146 | int w, h; |
|
|
147 | SDL_Surface *image; |
|
|
148 | SDL_Rect area; |
|
|
149 | Uint32 saved_flags; |
|
|
150 | Uint8 saved_alpha; |
|
|
151 | |
|
|
152 | /* Use the surface width and height expanded to powers of 2 */ |
|
|
153 | w = power_of_two (surface->w); |
|
|
154 | h = power_of_two (surface->h); |
|
|
155 | texcoord[0] = 0.0f; /* Min X */ |
|
|
156 | texcoord[1] = 0.0f; /* Min Y */ |
|
|
157 | texcoord[2] = (GLfloat) surface->w / w; /* Max X */ |
|
|
158 | texcoord[3] = (GLfloat) surface->h / h; /* Max Y */ |
|
|
159 | |
|
|
160 | image = SDL_CreateRGBSurface (SDL_SWSURFACE, w, h, 32, |
|
|
161 | #if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */ |
|
|
162 | 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000 |
|
|
163 | #else |
|
|
164 | 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF |
|
|
165 | #endif |
|
|
166 | ); |
|
|
167 | if (image == NULL) |
|
|
168 | { |
|
|
169 | return 0; |
|
|
170 | } |
|
|
171 | |
|
|
172 | /* Save the alpha blending attributes */ |
|
|
173 | saved_flags = surface->flags & (SDL_SRCALPHA | SDL_RLEACCELOK); |
|
|
174 | saved_alpha = surface->format->alpha; |
|
|
175 | if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA) |
|
|
176 | { |
|
|
177 | SDL_SetAlpha (surface, 0, 0); |
|
|
178 | } |
|
|
179 | |
|
|
180 | /* Copy the surface into the GL texture image */ |
|
|
181 | area.x = 0; |
|
|
182 | area.y = 0; |
|
|
183 | area.w = surface->w; |
|
|
184 | area.h = surface->h; |
|
|
185 | SDL_BlitSurface (surface, &area, image, &area); |
|
|
186 | |
|
|
187 | /* Restore the alpha blending attributes */ |
|
|
188 | if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA) |
|
|
189 | { |
|
|
190 | SDL_SetAlpha (surface, saved_flags, saved_alpha); |
|
|
191 | } |
|
|
192 | |
|
|
193 | /* Create an OpenGL texture for the image */ |
|
|
194 | glGenTextures (1, &texture); |
|
|
195 | glBindTexture (GL_TEXTURE_2D, texture); |
|
|
196 | glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
|
|
197 | glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
|
|
198 | glTexImage2D (GL_TEXTURE_2D, |
|
|
199 | 0, |
|
|
200 | GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels); |
|
|
201 | SDL_FreeSurface (image); /* No longer needed */ |
|
|
202 | |
|
|
203 | return texture; |
|
|
204 | } |
151 | } |
205 | |
152 | |
206 | int |
153 | int |
207 | RunGLTest (int argc, char *argv[], |
154 | RunGLTest (int argc, char *argv[], |
208 | 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) |
… | |
… | |
261 | rgb_size[0] = 8; |
208 | rgb_size[0] = 8; |
262 | rgb_size[1] = 8; |
209 | rgb_size[1] = 8; |
263 | rgb_size[2] = 8; |
210 | rgb_size[2] = 8; |
264 | break; |
211 | break; |
265 | } |
212 | } |
|
|
213 | |
266 | SDL_GL_SetAttribute (SDL_GL_RED_SIZE, rgb_size[0]); |
214 | SDL_GL_SetAttribute (SDL_GL_RED_SIZE, rgb_size[0]); |
267 | SDL_GL_SetAttribute (SDL_GL_GREEN_SIZE, rgb_size[1]); |
215 | SDL_GL_SetAttribute (SDL_GL_GREEN_SIZE, rgb_size[1]); |
268 | SDL_GL_SetAttribute (SDL_GL_BLUE_SIZE, rgb_size[2]); |
216 | SDL_GL_SetAttribute (SDL_GL_BLUE_SIZE, rgb_size[2]); |
269 | SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE, 16); |
217 | SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE, 16); |
270 | SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, 1); |
218 | SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, 1); |
… | |
… | |
309 | printf ("SDL_GL_MULTISAMPLESAMPLES: requested %d, got %d\n", fsaa, |
257 | printf ("SDL_GL_MULTISAMPLESAMPLES: requested %d, got %d\n", fsaa, |
310 | value); |
258 | value); |
311 | } |
259 | } |
312 | |
260 | |
313 | /* Set the window manager title bar */ |
261 | /* Set the window manager title bar */ |
314 | SDL_WM_SetCaption ("SDL GL test", "testgl"); |
262 | SDL_WM_SetCaption ("libgender rendering test", "gendertest"); |
315 | |
263 | |
316 | /* Set the gamma for the window */ |
264 | /* Set the gamma for the window */ |
317 | if (gamma != 0.0) |
265 | if (gamma != 0.0) |
318 | SDL_SetGamma (gamma, gamma, gamma); |
266 | SDL_SetGamma (gamma, gamma, gamma); |
319 | |
267 | |
|
|
268 | for (int i = 0; i < 70; i++) |
|
|
269 | { |
|
|
270 | // load a entity |
|
|
271 | txtprt_parser p; |
|
|
272 | entity *e; |
|
|
273 | try |
|
|
274 | { |
|
|
275 | e = p.read ("test.blasc"); |
|
|
276 | } |
|
|
277 | catch (txtprt_i_exception & e) |
|
|
278 | { |
|
|
279 | cout << "ERR: " << e.msg << endl; |
|
|
280 | } |
|
|
281 | |
|
|
282 | entity_transform *f = new entity_transform; |
|
|
283 | f->set (e); |
|
|
284 | f->update (matrix::translation (vec3 (i*5, -1, -i*10))); |
|
|
285 | f->show (); |
|
|
286 | } |
|
|
287 | |
|
|
288 | camera.orig.x = camera.orig.y = camera.orig.z = 0; |
320 | camera.p = point (0, 0, 10); |
289 | camera.p = point (0, 0, 10); |
321 | camera.d = vec3 (0, 0, -1); |
290 | camera.d = vec3 (0, 0, -1); |
322 | camera.u = vec3 (0, 1, 0); |
291 | camera.u = vec3 (0, 1, 0); |
323 | camera.w = w; camera.h = h; |
292 | camera.w = w; camera.h = h; |
324 | camera.fov = 90; |
293 | camera.fov = 80; |
325 | |
294 | |
326 | glMatrixMode (GL_MODELVIEW); |
295 | glMatrixMode (GL_MODELVIEW); |
327 | glLoadIdentity (); |
296 | glLoadIdentity (); |
328 | |
297 | |
|
|
298 | //glEnable (GL_CULL_FACE); |
329 | glEnable (GL_DEPTH_TEST); |
299 | glEnable (GL_DEPTH_TEST); |
330 | |
300 | glEnable (GL_TEXTURE_2D); //texturing |
331 | glDepthFunc (GL_LESS); |
|
|
332 | |
301 | |
333 | glShadeModel (GL_SMOOTH); |
302 | glShadeModel (GL_SMOOTH); |
334 | |
303 | |
335 | glEnable (GL_LIGHTING); |
304 | glEnable (GL_LIGHTING); |
336 | |
|
|
337 | GLfloat lightc[4] = { 1, 0.1, 0.1, 1 }; |
305 | //GLfloat lightc[4] = { 1, 0.1, 0.1, 1 }; |
338 | //glLightf (GL_LIGHT0, GL_QUADRATIC_ATTENUATION); |
306 | //glLightf (GL_LIGHT0, GL_QUADRATIC_ATTENUATION); |
339 | glLightfv (GL_LIGHT0, GL_DIFFUSE, lightc); |
307 | //glLightfv (GL_LIGHT0, GL_DIFFUSE, lightc); |
340 | glEnable (GL_LIGHT0); |
308 | glEnable (GL_LIGHT0); |
341 | glEnable (GL_COLOR_MATERIAL); |
309 | |
|
|
310 | cgc = cgCreateContext (); |
|
|
311 | |
|
|
312 | vsh_profile = CG_PROFILE_ARBVP1; |
|
|
313 | //if (cgGLIsProfileSupported (CG_PROFILE_VP30)) vsh_profile = CG_PROFILE_VP30; |
|
|
314 | //if (cgGLIsProfileSupported (CG_PROFILE_VP40)) vsh_profile = CG_PROFILE_VP40; |
|
|
315 | fsh_profile = CG_PROFILE_ARBFP1; |
|
|
316 | //if (cgGLIsProfileSupported (CG_PROFILE_FP30)) fsh_profile = CG_PROFILE_FP30; |
|
|
317 | //if (cgGLIsProfileSupported (CG_PROFILE_FP40)) fsh_profile = CG_PROFILE_FP40; |
|
|
318 | |
|
|
319 | vsh = cgCreateProgramFromFile (cgc, CG_SOURCE, "vsh.cg", vsh_profile, 0, 0); |
|
|
320 | CheckCgError (); |
|
|
321 | cgGLLoadProgram (vsh); |
|
|
322 | CheckCgError (); |
|
|
323 | mv = cgGetNamedParameter (vsh, "WorldProj"); |
|
|
324 | mvp = cgGetNamedParameter (vsh, "WorldViewProj"); |
|
|
325 | lightpos = cgGetNamedParameter (vsh, "LightPos"); |
|
|
326 | CheckCgError (); |
|
|
327 | |
|
|
328 | CGparameter g_Texture; // the texture parameter |
|
|
329 | |
|
|
330 | |
|
|
331 | fsh = cgCreateProgramFromFile (cgc, CG_SOURCE, "fsh.cg", fsh_profile, 0, 0); |
|
|
332 | Texture t("o.jpg"); |
|
|
333 | g_Texture = cgGetNamedParameter(fsh, "Texture"); // the texture cg-warper ;) |
|
|
334 | cgGLSetTextureParameter(g_Texture, t.texture); // Bind the texture number 999 to g_Texture |
|
|
335 | CheckCgError (); |
|
|
336 | cgGLLoadProgram (fsh); |
|
|
337 | CheckCgError (); |
|
|
338 | |
|
|
339 | cgGLBindProgram (vsh); |
|
|
340 | CheckCgError (); |
|
|
341 | cgGLBindProgram (fsh); |
|
|
342 | CheckCgError (); |
342 | |
343 | |
343 | /* Loop until done. */ |
344 | /* Loop until done. */ |
344 | start_time = SDL_GetTicks (); |
345 | start_time = SDL_GetTicks (); |
345 | frames = 0; |
346 | frames = 0; |
|
|
347 | |
|
|
348 | |
346 | while (!done) |
349 | while (!done) |
347 | { |
350 | { |
348 | GLenum gl_error; |
351 | GLenum gl_error; |
349 | char *sdl_error; |
352 | char *sdl_error; |
350 | SDL_Event event; |
353 | SDL_Event event; |
351 | |
354 | |
352 | /* Do our drawing, too. */ |
|
|
353 | glClearColor (0.0, 0.0, 0.0, 1.0); |
|
|
354 | glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
355 | glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
355 | |
356 | |
356 | GLfloat lightp[4]; |
357 | camera.p.x += camera_velocity_factor * camera_velocity.x * timer.diff; |
357 | lightp[0] = camera.p.x; |
358 | camera.p.y += camera_velocity_factor * camera_velocity.y * timer.diff; |
358 | lightp[1] = camera.p.y; |
359 | camera.p.z += camera_velocity_factor * camera_velocity.z * timer.diff; |
359 | lightp[2] = camera.p.z; |
360 | |
360 | lightp[3] = 1; |
361 | camera_angle += camera_velocity_factor * camera_velocity_angle * timer.diff; |
361 | glLightfv (GL_LIGHT0, GL_POSITION, lightp); |
362 | camera.d.z = -cos (camera_angle * 12 / 180.); |
|
|
363 | camera.d.x = sin (camera_angle * 12 / 180.); |
|
|
364 | |
|
|
365 | cgGLSetParameter4f (lightpos, camera.p.x, camera.p.y, camera.p.z, 1); |
|
|
366 | |
|
|
367 | glBindTexture (GL_TEXTURE_2D, t.texture); |
|
|
368 | cgGLEnableTextureParameter (g_Texture); // Enable the texture parameter |
362 | |
369 | |
363 | #if 0 |
370 | #if 0 |
364 | static GLfloat ry; |
371 | static GLfloat ry; |
365 | ry += 0.03; |
372 | ry += 0.001; |
366 | v.d.x = cos (ry); |
373 | camera.d.x = cos (ry); |
367 | v.d.z = sin (ry); |
374 | camera.d.z = sin (ry); |
|
|
375 | //camera.d.y = sin (ry * 0.1); |
368 | #endif |
376 | #endif |
369 | |
377 | |
370 | draw_context c; |
378 | camera.begin (); |
|
|
379 | camera.pass (view::DEPTH); |
|
|
380 | camera.pass (view::LIGHTED); |
371 | camera.draw (c); |
381 | camera.end (); |
372 | |
382 | |
373 | SDL_GL_SwapBuffers (); |
383 | SDL_GL_SwapBuffers (); |
|
|
384 | timer.frame (); |
374 | |
385 | |
375 | /* Check for error conditions. */ |
386 | /* Check for error conditions. */ |
376 | gl_error = glGetError (); |
387 | gl_error = glGetError (); |
377 | |
388 | |
378 | if (gl_error != GL_NO_ERROR) |
389 | if (gl_error != GL_NO_ERROR) |
… | |
… | |
385 | fprintf (stderr, "testgl: SDL error '%s'\n", sdl_error); |
396 | fprintf (stderr, "testgl: SDL error '%s'\n", sdl_error); |
386 | SDL_ClearError (); |
397 | SDL_ClearError (); |
387 | } |
398 | } |
388 | |
399 | |
389 | /* Allow the user to see what's happening */ |
400 | /* Allow the user to see what's happening */ |
390 | SDL_Delay (20); |
401 | //SDL_Delay (20); |
391 | |
402 | |
392 | /* Check if there's a pending event. */ |
403 | /* Check if there's a pending event. */ |
393 | while (SDL_PollEvent (&event)) |
404 | while (SDL_PollEvent (&event)) |
394 | { |
|
|
395 | done = HandleEvent (&event); |
405 | done = HandleEvent (&event); |
396 | } |
406 | |
|
|
407 | |
397 | ++frames; |
408 | ++frames; |
398 | } |
409 | } |
399 | |
410 | |
400 | /* Print out the frames per second */ |
411 | /* Print out the frames per second */ |
401 | this_time = SDL_GetTicks (); |
412 | this_time = SDL_GetTicks (); |
… | |
… | |
430 | int slowly; |
441 | int slowly; |
431 | float gamma = 0.0; |
442 | float gamma = 0.0; |
432 | int noframe = 0; |
443 | int noframe = 0; |
433 | int fsaa = 0; |
444 | int fsaa = 0; |
434 | |
445 | |
435 | // load a entity |
|
|
436 | txtprt_parser p; |
|
|
437 | entity *e; |
|
|
438 | try { |
|
|
439 | e = p.read ("test.blasc"); |
|
|
440 | } catch (txtprt_i_exception & e) { |
|
|
441 | cout << "ERR: " << e.msg << endl; |
|
|
442 | } |
|
|
443 | e->show (); |
|
|
444 | |
|
|
445 | logo = 0; |
446 | logo = 0; |
446 | slowly = 0; |
447 | slowly = 0; |
447 | numtests = 1; |
448 | numtests = 1; |
448 | for (i = 1; argv[i]; ++i) |
449 | for (i = 1; argv[i]; ++i) |
449 | { |
450 | { |
… | |
… | |
477 | ("Usage: %s [-twice] [-logo] [-slow] [-bpp n] [-gamma n] [-noframe] [-fsaa]\n", |
478 | ("Usage: %s [-twice] [-logo] [-slow] [-bpp n] [-gamma n] [-noframe] [-fsaa]\n", |
478 | argv[0]); |
479 | argv[0]); |
479 | exit (0); |
480 | exit (0); |
480 | } |
481 | } |
481 | } |
482 | } |
|
|
483 | |
482 | for (i = 0; i < numtests; ++i) |
484 | for (i = 0; i < numtests; ++i) |
483 | { |
|
|
484 | RunGLTest (argc, argv, logo, slowly, bpp, gamma, noframe, fsaa); |
485 | RunGLTest (argc, argv, logo, slowly, bpp, gamma, noframe, fsaa); |
485 | } |
486 | |
486 | return 0; |
487 | return 0; |
487 | } |
488 | } |