… | |
… | |
14 | #include "txtprt_import.h" |
14 | #include "txtprt_import.h" |
15 | |
15 | |
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 | |
20 | |
20 | static void CheckCgError(void) |
21 | static void CheckCgError(void) |
21 | { |
22 | { |
22 | CGerror err = cgGetError(); |
23 | CGerror err = cgGetError(); |
23 | |
24 | |
… | |
… | |
30 | |
31 | |
31 | /**********************************************************************/ |
32 | /**********************************************************************/ |
32 | |
33 | |
33 | view camera; |
34 | view camera; |
34 | vec3 camera_velocity; |
35 | vec3 camera_velocity; |
|
|
36 | float camera_angle = 0, camera_velocity_angle; |
35 | float camera_velocity_factor = 10; |
37 | float camera_velocity_factor = 20; |
36 | |
38 | |
37 | void |
39 | void |
38 | HotKey_ToggleFullScreen (void) |
40 | HotKey_ToggleFullScreen (void) |
39 | { |
41 | { |
40 | SDL_Surface *screen; |
42 | SDL_Surface *screen; |
… | |
… | |
98 | } |
100 | } |
99 | |
101 | |
100 | printf ("focus\n"); |
102 | printf ("focus\n"); |
101 | break; |
103 | break; |
102 | |
104 | |
103 | #define VELOCITY 10 |
|
|
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_LEFT) camera_velocity.x--; |
108 | if (event->key.keysym.sym == SDLK_RIGHT) camera_velocity.x++; |
109 | if (event->key.keysym.sym == SDLK_RIGHT) camera_velocity.x++; |
109 | if (event->key.keysym.sym == SDLK_a) camera_velocity.y--; |
110 | if (event->key.keysym.sym == SDLK_a) camera_velocity.y--; |
110 | if (event->key.keysym.sym == SDLK_s) camera_velocity.y++; |
111 | if (event->key.keysym.sym == SDLK_s) camera_velocity.y++; |
111 | if (event->key.keysym.sym == SDLK_v) camera_velocity_factor *= 1.5; |
112 | 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; |
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--; |
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) && |
… | |
… | |
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.x++; |
135 | if (event->key.keysym.sym == SDLK_RIGHT) camera_velocity.x--; |
138 | if (event->key.keysym.sym == SDLK_RIGHT) camera_velocity.x--; |
136 | if (event->key.keysym.sym == SDLK_a) camera_velocity.y++; |
139 | if (event->key.keysym.sym == SDLK_a) camera_velocity.y++; |
137 | if (event->key.keysym.sym == SDLK_s) 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++; |
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); |
146 | } |
|
|
147 | |
|
|
148 | /* Quick utility function for texture creation */ |
|
|
149 | static int |
|
|
150 | power_of_two (int input) |
|
|
151 | { |
|
|
152 | int value = 1; |
|
|
153 | |
|
|
154 | while (value < input) |
|
|
155 | { |
|
|
156 | value <<= 1; |
|
|
157 | } |
|
|
158 | return value; |
|
|
159 | } |
|
|
160 | |
|
|
161 | GLuint |
|
|
162 | SDL_GL_LoadTexture (SDL_Surface * surface, GLfloat * texcoord) |
|
|
163 | { |
|
|
164 | GLuint texture; |
|
|
165 | int w, h; |
|
|
166 | SDL_Surface *image; |
|
|
167 | SDL_Rect area; |
|
|
168 | Uint32 saved_flags; |
|
|
169 | Uint8 saved_alpha; |
|
|
170 | |
|
|
171 | /* Use the surface width and height expanded to powers of 2 */ |
|
|
172 | w = power_of_two (surface->w); |
|
|
173 | h = power_of_two (surface->h); |
|
|
174 | texcoord[0] = 0.0f; /* Min X */ |
|
|
175 | texcoord[1] = 0.0f; /* Min Y */ |
|
|
176 | texcoord[2] = (GLfloat) surface->w / w; /* Max X */ |
|
|
177 | texcoord[3] = (GLfloat) surface->h / h; /* Max Y */ |
|
|
178 | |
|
|
179 | image = SDL_CreateRGBSurface (SDL_SWSURFACE, w, h, 32, |
|
|
180 | #if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */ |
|
|
181 | 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000 |
|
|
182 | #else |
|
|
183 | 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF |
|
|
184 | #endif |
|
|
185 | ); |
|
|
186 | if (image == NULL) |
|
|
187 | { |
|
|
188 | return 0; |
|
|
189 | } |
|
|
190 | |
|
|
191 | /* Save the alpha blending attributes */ |
|
|
192 | saved_flags = surface->flags & (SDL_SRCALPHA | SDL_RLEACCELOK); |
|
|
193 | saved_alpha = surface->format->alpha; |
|
|
194 | if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA) |
|
|
195 | { |
|
|
196 | SDL_SetAlpha (surface, 0, 0); |
|
|
197 | } |
|
|
198 | |
|
|
199 | /* Copy the surface into the GL texture image */ |
|
|
200 | area.x = 0; |
|
|
201 | area.y = 0; |
|
|
202 | area.w = surface->w; |
|
|
203 | area.h = surface->h; |
|
|
204 | SDL_BlitSurface (surface, &area, image, &area); |
|
|
205 | |
|
|
206 | /* Restore the alpha blending attributes */ |
|
|
207 | if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA) |
|
|
208 | { |
|
|
209 | SDL_SetAlpha (surface, saved_flags, saved_alpha); |
|
|
210 | } |
|
|
211 | |
|
|
212 | /* Create an OpenGL texture for the image */ |
|
|
213 | glGenTextures (1, &texture); |
|
|
214 | glBindTexture (GL_TEXTURE_2D, texture); |
|
|
215 | glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
|
|
216 | glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
|
|
217 | glTexImage2D (GL_TEXTURE_2D, |
|
|
218 | 0, |
|
|
219 | GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels); |
|
|
220 | SDL_FreeSurface (image); /* No longer needed */ |
|
|
221 | |
|
|
222 | return texture; |
|
|
223 | } |
151 | } |
224 | |
152 | |
225 | int |
153 | int |
226 | RunGLTest (int argc, char *argv[], |
154 | RunGLTest (int argc, char *argv[], |
227 | 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) |
… | |
… | |
233 | int done = 0; |
161 | int done = 0; |
234 | int frames; |
162 | int frames; |
235 | Uint32 start_time, this_time; |
163 | Uint32 start_time, this_time; |
236 | Uint32 video_flags; |
164 | Uint32 video_flags; |
237 | int value; |
165 | int value; |
|
|
166 | GLenum gl_error; |
238 | |
167 | |
239 | if (SDL_Init (SDL_INIT_VIDEO) < 0) |
168 | if (SDL_Init (SDL_INIT_VIDEO) < 0) |
240 | { |
169 | { |
241 | fprintf (stderr, "Couldn't initialize SDL: %s\n", SDL_GetError ()); |
170 | fprintf (stderr, "Couldn't initialize SDL: %s\n", SDL_GetError ()); |
242 | exit (1); |
171 | exit (1); |
… | |
… | |
335 | |
264 | |
336 | /* Set the gamma for the window */ |
265 | /* Set the gamma for the window */ |
337 | if (gamma != 0.0) |
266 | if (gamma != 0.0) |
338 | SDL_SetGamma (gamma, gamma, gamma); |
267 | SDL_SetGamma (gamma, gamma, gamma); |
339 | |
268 | |
340 | // load a entity |
|
|
341 | for (int i = 0; i < 7; i++) |
269 | for (int i = 0; i < 70; i++) |
342 | { |
270 | { |
|
|
271 | // load a entity |
343 | txtprt_parser p; |
272 | txtprt_parser p; |
344 | entity_transform *f = new entity_transform; |
|
|
345 | entity *e; |
273 | entity *e; |
346 | try |
274 | try |
347 | { |
275 | { |
348 | e = p.read ("test.blasc"); |
276 | e = p.read ("test.blasc"); |
|
|
277 | } |
349 | } catch (txtprt_i_exception & e) |
278 | catch (txtprt_i_exception & e) |
350 | { |
279 | { |
351 | cout << "ERR: " << e.msg << endl; |
280 | cout << "ERR: " << e.msg << endl; |
352 | } |
281 | } |
|
|
282 | |
|
|
283 | entity_transform *f = new entity_transform; |
353 | f->set (e); |
284 | f->set (e); |
354 | f->update (matrix::translation (vec3 (0, -1, -i*5))); |
285 | f->update (matrix::translation (vec3 (i*5, -3, -i*10))); |
355 | f->show (); |
286 | f->show (); |
356 | } |
287 | } |
357 | |
288 | draw_some_random_funky_floor_dance_music (10, -500, -10, -1000); |
358 | camera.orig.x = camera.orig.y = camera.orig.z = 0; |
289 | camera.orig.x = camera.orig.y = camera.orig.z = 0; |
359 | camera.p = point (0, 0, 10); |
290 | camera.p = point (0, 0, 10); |
360 | camera.d = vec3 (0, 0, -1); |
291 | camera.d = vec3 (0, 0, -1); |
361 | camera.u = vec3 (0, 1, 0); |
292 | camera.u = vec3 (0, 1, 0); |
362 | camera.w = w; camera.h = h; |
293 | camera.w = w; camera.h = h; |
363 | camera.fov = 90; |
294 | camera.fov = 80; |
364 | |
295 | |
365 | glMatrixMode (GL_MODELVIEW); |
296 | glMatrixMode (GL_MODELVIEW); |
366 | glLoadIdentity (); |
297 | glLoadIdentity (); |
367 | |
298 | |
368 | glEnable (GL_CULL_FACE); |
299 | glEnable (GL_CULL_FACE); |
369 | glEnable (GL_DEPTH_TEST); |
300 | glEnable (GL_DEPTH_TEST); |
370 | //glEnable(GL_VERTEX_PROGRAM_ARB); |
|
|
371 | //glEnable(GL_FRAGMENT_PROGRAM_ARB); |
|
|
372 | |
301 | |
373 | glShadeModel (GL_SMOOTH); |
302 | glShadeModel (GL_SMOOTH); |
374 | |
303 | |
375 | glEnable (GL_LIGHTING); |
304 | glEnable (GL_LIGHTING); |
376 | //GLfloat lightc[4] = { 1, 0.1, 0.1, 1 }; |
|
|
377 | //glLightf (GL_LIGHT0, GL_QUADRATIC_ATTENUATION); |
|
|
378 | //glLightfv (GL_LIGHT0, GL_DIFFUSE, lightc); |
|
|
379 | glEnable (GL_LIGHT0); |
305 | glEnable (GL_LIGHT0); |
380 | |
306 | |
381 | cgc = cgCreateContext (); |
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 | |
382 | vsh = cgCreateProgramFromFile (cgc, CG_SOURCE, "vsh.cg", CG_PROFILE_ARBVP1, 0, 0); |
316 | vsh = cgCreateProgramFromFile (cgc, CG_SOURCE, "vsh.cg", vsh_profile, 0, 0); |
383 | CheckCgError (); |
317 | CheckCgError (); |
384 | cgGLLoadProgram (vsh); |
318 | cgGLLoadProgram (vsh); |
385 | CheckCgError (); |
319 | CheckCgError (); |
386 | mv = cgGetNamedParameter (vsh, "WorldProj"); |
320 | mv = cgGetNamedParameter (vsh, "WorldProj"); |
387 | mvp = cgGetNamedParameter (vsh, "WorldViewProj"); |
321 | mvp = cgGetNamedParameter (vsh, "WorldViewProj"); |
388 | lightpos = cgGetNamedParameter (vsh, "LightPos"); |
322 | lightpos = cgGetNamedParameter (vsh, "LightPos"); |
389 | CheckCgError (); |
323 | CheckCgError (); |
390 | cgGLEnableProfile (CG_PROFILE_ARBVP1); |
|
|
391 | CheckCgError (); |
|
|
392 | |
324 | |
|
|
325 | CGparameter g_Texture; // the texture parameter |
|
|
326 | |
|
|
327 | |
393 | fsh = cgCreateProgramFromFile (cgc, CG_SOURCE, "fsh.cg", CG_PROFILE_ARBFP1, 0, 0); |
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 |
394 | CheckCgError (); |
332 | CheckCgError (); |
395 | cgGLLoadProgram (fsh); |
333 | cgGLLoadProgram (fsh); |
396 | CheckCgError (); |
|
|
397 | cgGLEnableProfile (CG_PROFILE_ARBFP1); |
|
|
398 | CheckCgError (); |
334 | CheckCgError (); |
399 | |
335 | |
400 | cgGLBindProgram (vsh); |
336 | cgGLBindProgram (vsh); |
401 | CheckCgError (); |
337 | CheckCgError (); |
402 | cgGLBindProgram (fsh); |
338 | cgGLBindProgram (fsh); |
… | |
… | |
404 | |
340 | |
405 | /* Loop until done. */ |
341 | /* Loop until done. */ |
406 | start_time = SDL_GetTicks (); |
342 | start_time = SDL_GetTicks (); |
407 | frames = 0; |
343 | frames = 0; |
408 | |
344 | |
409 | draw_context c (camera); |
|
|
410 | |
|
|
411 | while (!done) |
345 | while (!done) |
412 | { |
346 | { |
413 | GLenum gl_error; |
|
|
414 | char *sdl_error; |
347 | char *sdl_error; |
415 | SDL_Event event; |
348 | SDL_Event event; |
416 | |
349 | |
417 | glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
|
|
418 | |
|
|
419 | camera.p.x += (camera_velocity_factor * camera_velocity.x) * timer.diff; |
350 | camera.p.x += camera_velocity_factor * camera_velocity.x * timer.diff; |
420 | camera.p.y += (camera_velocity_factor * camera_velocity.y) * timer.diff; |
351 | camera.p.y += camera_velocity_factor * camera_velocity.y * timer.diff; |
421 | camera.p.z += (camera_velocity_factor * camera_velocity.z) * timer.diff; |
352 | camera.p.z += camera_velocity_factor * camera_velocity.z * timer.diff; |
|
|
353 | |
|
|
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.); |
422 | |
357 | |
423 | cgGLSetParameter4f (lightpos, camera.p.x, camera.p.y, camera.p.z, 1); |
358 | cgGLSetParameter4f (lightpos, camera.p.x, camera.p.y, camera.p.z, 1); |
|
|
359 | |
|
|
360 | glBindTexture (GL_TEXTURE_2D, t.texture); |
|
|
361 | cgGLEnableTextureParameter (g_Texture); // Enable the texture parameter |
424 | |
362 | |
425 | #if 0 |
363 | #if 0 |
426 | static GLfloat ry; |
364 | static GLfloat ry; |
427 | ry += 0.001; |
365 | ry += 0.001; |
428 | camera.d.x = cos (ry); |
366 | camera.d.x = cos (ry); |
429 | camera.d.z = sin (ry); |
367 | camera.d.z = sin (ry); |
430 | //camera.d.y = sin (ry * 0.1); |
368 | //camera.d.y = sin (ry * 0.1); |
431 | #endif |
369 | #endif |
432 | |
370 | |
433 | c.mode = draw_context::DEPTH; |
371 | camera.begin (); |
|
|
372 | camera.pass (view::DEPTH); |
|
|
373 | camera.pass (view::LIGHTED); |
434 | camera.draw (c); |
374 | camera.end (); |
435 | c.mode = draw_context::LIGHTED; |
|
|
436 | camera.draw (c); |
|
|
437 | |
375 | |
438 | SDL_GL_SwapBuffers (); |
376 | SDL_GL_SwapBuffers (); |
439 | timer.frame (); |
377 | timer.frame (); |
440 | |
378 | |
441 | /* Check for error conditions. */ |
379 | /* Check for error conditions. */ |
442 | gl_error = glGetError (); |
380 | gl_error = glGetError (); |
443 | |
381 | |
444 | if (gl_error != GL_NO_ERROR) |
|
|
445 | 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); |
446 | |
383 | |
447 | sdl_error = SDL_GetError (); |
384 | sdl_error = SDL_GetError (); |
448 | |
385 | |
449 | if (sdl_error[0] != '\0') |
386 | if (sdl_error[0] != '\0') |
450 | { |
387 | { |