ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libgender/test.C
Revision: 1.37
Committed: Wed Oct 6 06:05:23 2004 UTC (19 years, 8 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.36: +0 -77 lines
Log Message:
texture stuff 1

File Contents

# User Rev Content
1 root 1.1 #include <stdlib.h>
2     #include <stdio.h>
3     #include <string.h>
4     #include <math.h>
5    
6     #include "SDL.h"
7     #include "SDL_opengl.h"
8    
9     static SDL_Surface *global_image = NULL;
10     static GLuint global_texture = 0;
11    
12 root 1.29 #include "util.h"
13 root 1.2 #include "entity.h"
14 root 1.29 #include "txtprt_import.h"
15    
16     CGcontext cgc;
17     CGprogram vsh, fsh;
18 root 1.30 CGparameter mv, mvp, lightpos;
19 root 1.35 CGprofile vsh_profile, fsh_profile;
20 root 1.29
21     static void CheckCgError(void)
22     {
23     CGerror err = cgGetError();
24 root 1.2
25 root 1.29 if (err != CG_NO_ERROR)
26     {
27     printf("CG error: %s\n", cgGetErrorString(err));
28     exit(1);
29     }
30     }
31 root 1.4
32 root 1.1 /**********************************************************************/
33    
34 root 1.6 view camera;
35 root 1.18 vec3 camera_velocity;
36 root 1.32 float camera_velocity_factor = 20;
37 root 1.6
38 root 1.1 void
39     HotKey_ToggleFullScreen (void)
40     {
41     SDL_Surface *screen;
42    
43     screen = SDL_GetVideoSurface ();
44     if (SDL_WM_ToggleFullScreen (screen))
45 root 1.5 printf ("Toggled fullscreen mode - now %s\n",
46     (screen->flags & SDL_FULLSCREEN) ? "fullscreen" : "windowed");
47 root 1.1 else
48 root 1.5 printf ("Unable to toggle fullscreen mode\n");
49 root 1.1 }
50    
51     void
52     HotKey_ToggleGrab (void)
53     {
54     SDL_GrabMode mode;
55    
56     printf ("Ctrl-G: toggling input grab!\n");
57     mode = SDL_WM_GrabInput (SDL_GRAB_QUERY);
58     if (mode == SDL_GRAB_ON)
59 root 1.5 printf ("Grab was on\n");
60 root 1.1 else
61 root 1.5 printf ("Grab was off\n");
62 root 1.1
63     mode = SDL_WM_GrabInput (mode ? SDL_GRAB_OFF : SDL_GRAB_ON);
64     if (mode == SDL_GRAB_ON)
65 root 1.5 printf ("Grab is now on\n");
66 root 1.1 else
67 root 1.5 printf ("Grab is now off\n");
68 root 1.1 }
69    
70     void
71     HotKey_Iconify (void)
72     {
73     printf ("Ctrl-Z: iconifying window!\n");
74     SDL_WM_IconifyWindow ();
75     }
76    
77     int
78     HandleEvent (SDL_Event * event)
79     {
80     int done;
81    
82     done = 0;
83     switch (event->type)
84     {
85     case SDL_ACTIVEEVENT:
86     /* See what happened */
87     printf ("app %s ", event->active.gain ? "gained" : "lost");
88     if (event->active.state & SDL_APPACTIVE)
89     {
90     printf ("active ");
91     }
92     else if (event->active.state & SDL_APPMOUSEFOCUS)
93     {
94     printf ("mouse ");
95     }
96     else if (event->active.state & SDL_APPINPUTFOCUS)
97     {
98     printf ("input ");
99     }
100    
101     printf ("focus\n");
102     break;
103    
104     case SDL_KEYDOWN:
105 root 1.19 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_LEFT) camera_velocity.x--;
108     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_s) camera_velocity.y++;
111     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 root 1.6
114 root 1.1 if (event->key.keysym.sym == SDLK_ESCAPE)
115 root 1.6 done = 1;
116 root 1.1
117     if ((event->key.keysym.sym == SDLK_g) &&
118     (event->key.keysym.mod & KMOD_CTRL))
119 root 1.6 HotKey_ToggleGrab ();
120 root 1.1
121     if ((event->key.keysym.sym == SDLK_z) &&
122     (event->key.keysym.mod & KMOD_CTRL))
123 root 1.6 HotKey_Iconify ();
124 root 1.1
125     if ((event->key.keysym.sym == SDLK_RETURN) &&
126     (event->key.keysym.mod & KMOD_ALT))
127 root 1.6 HotKey_ToggleFullScreen ();
128 root 1.1
129     break;
130    
131 root 1.18 case SDL_KEYUP:
132 root 1.19 if (event->key.keysym.sym == SDLK_UP) camera_velocity.z++;
133     if (event->key.keysym.sym == SDLK_DOWN) camera_velocity.z--;
134     if (event->key.keysym.sym == SDLK_LEFT) camera_velocity.x++;
135     if (event->key.keysym.sym == SDLK_RIGHT) camera_velocity.x--;
136     if (event->key.keysym.sym == SDLK_a) camera_velocity.y++;
137     if (event->key.keysym.sym == SDLK_s) camera_velocity.y--;
138 root 1.18 break;
139    
140 root 1.1 case SDL_QUIT:
141     done = 1;
142     break;
143     }
144    
145     return (done);
146     }
147    
148     int
149     RunGLTest (int argc, char *argv[],
150     int logo, int slowly, int bpp, float gamma, int noframe, int fsaa)
151     {
152     int i;
153     int rgb_size[3];
154     int w = 640;
155     int h = 480;
156     int done = 0;
157     int frames;
158     Uint32 start_time, this_time;
159 root 1.2 Uint32 video_flags;
160 root 1.1 int value;
161    
162     if (SDL_Init (SDL_INIT_VIDEO) < 0)
163     {
164     fprintf (stderr, "Couldn't initialize SDL: %s\n", SDL_GetError ());
165     exit (1);
166     }
167    
168     /* See if we should detect the display depth */
169     if (bpp == 0)
170     {
171     if (SDL_GetVideoInfo ()->vfmt->BitsPerPixel <= 8)
172 root 1.3 bpp = 8;
173 root 1.1 else
174 root 1.3 bpp = 16; /* More doesn't seem to work */
175 root 1.1 }
176    
177 root 1.2 video_flags = SDL_OPENGL;
178    
179 root 1.1 for (i = 1; argv[i]; ++i)
180 root 1.3 if (strcmp (argv[1], "-fullscreen") == 0)
181     video_flags |= SDL_FULLSCREEN;
182 root 1.1
183     if (noframe)
184 root 1.3 video_flags |= SDL_NOFRAME;
185 root 1.1
186     /* Initialize the display */
187     switch (bpp)
188     {
189     case 8:
190     rgb_size[0] = 3;
191     rgb_size[1] = 3;
192     rgb_size[2] = 2;
193     break;
194 root 1.3
195 root 1.1 case 15:
196     case 16:
197     rgb_size[0] = 5;
198     rgb_size[1] = 5;
199     rgb_size[2] = 5;
200     break;
201 root 1.3
202 root 1.1 default:
203     rgb_size[0] = 8;
204     rgb_size[1] = 8;
205     rgb_size[2] = 8;
206     break;
207     }
208 root 1.18
209 root 1.1 SDL_GL_SetAttribute (SDL_GL_RED_SIZE, rgb_size[0]);
210     SDL_GL_SetAttribute (SDL_GL_GREEN_SIZE, rgb_size[1]);
211     SDL_GL_SetAttribute (SDL_GL_BLUE_SIZE, rgb_size[2]);
212     SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE, 16);
213     SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, 1);
214 root 1.3
215 root 1.1 if (fsaa)
216     {
217     SDL_GL_SetAttribute (SDL_GL_MULTISAMPLEBUFFERS, 1);
218     SDL_GL_SetAttribute (SDL_GL_MULTISAMPLESAMPLES, fsaa);
219     }
220 root 1.3
221 root 1.1 if (SDL_SetVideoMode (w, h, bpp, video_flags) == NULL)
222     {
223     fprintf (stderr, "Couldn't set GL mode: %s\n", SDL_GetError ());
224     SDL_Quit ();
225     exit (1);
226     }
227    
228     printf ("Screen BPP: %d\n", SDL_GetVideoSurface ()->format->BitsPerPixel);
229     printf ("\n");
230     printf ("Vendor : %s\n", glGetString (GL_VENDOR));
231     printf ("Renderer : %s\n", glGetString (GL_RENDERER));
232     printf ("Version : %s\n", glGetString (GL_VERSION));
233     printf ("Extensions : %s\n", glGetString (GL_EXTENSIONS));
234     printf ("\n");
235    
236     SDL_GL_GetAttribute (SDL_GL_RED_SIZE, &value);
237     printf ("SDL_GL_RED_SIZE: requested %d, got %d\n", rgb_size[0], value);
238     SDL_GL_GetAttribute (SDL_GL_GREEN_SIZE, &value);
239     printf ("SDL_GL_GREEN_SIZE: requested %d, got %d\n", rgb_size[1], value);
240     SDL_GL_GetAttribute (SDL_GL_BLUE_SIZE, &value);
241     printf ("SDL_GL_BLUE_SIZE: requested %d, got %d\n", rgb_size[2], value);
242     SDL_GL_GetAttribute (SDL_GL_DEPTH_SIZE, &value);
243     printf ("SDL_GL_DEPTH_SIZE: requested %d, got %d\n", bpp, value);
244     SDL_GL_GetAttribute (SDL_GL_DOUBLEBUFFER, &value);
245     printf ("SDL_GL_DOUBLEBUFFER: requested 1, got %d\n", value);
246 root 1.3
247 root 1.1 if (fsaa)
248     {
249     SDL_GL_GetAttribute (SDL_GL_MULTISAMPLEBUFFERS, &value);
250     printf ("SDL_GL_MULTISAMPLEBUFFERS: requested 1, got %d\n", value);
251     SDL_GL_GetAttribute (SDL_GL_MULTISAMPLESAMPLES, &value);
252     printf ("SDL_GL_MULTISAMPLESAMPLES: requested %d, got %d\n", fsaa,
253     value);
254     }
255    
256     /* Set the window manager title bar */
257 root 1.12 SDL_WM_SetCaption ("libgender rendering test", "gendertest");
258 root 1.1
259     /* Set the gamma for the window */
260     if (gamma != 0.0)
261 root 1.3 SDL_SetGamma (gamma, gamma, gamma);
262 root 1.1
263 root 1.17 // load a entity
264     for (int i = 0; i < 7; i++)
265     {
266     txtprt_parser p;
267 root 1.18 entity_transform *f = new entity_transform;
268 root 1.17 entity *e;
269     try
270     {
271     e = p.read ("test.blasc");
272     } catch (txtprt_i_exception & e)
273     {
274     cout << "ERR: " << e.msg << endl;
275     }
276     f->set (e);
277 root 1.26 f->update (matrix::translation (vec3 (0, -1, -i*5)));
278 root 1.17 f->show ();
279     }
280    
281 root 1.13 camera.orig.x = camera.orig.y = camera.orig.z = 0;
282 root 1.6 camera.p = point (0, 0, 10);
283     camera.d = vec3 (0, 0, -1);
284     camera.u = vec3 (0, 1, 0);
285     camera.w = w; camera.h = h;
286     camera.fov = 90;
287 root 1.1
288     glMatrixMode (GL_MODELVIEW);
289     glLoadIdentity ();
290    
291 root 1.16 glEnable (GL_CULL_FACE);
292 root 1.1 glEnable (GL_DEPTH_TEST);
293 root 1.22
294 root 1.11 glShadeModel (GL_SMOOTH);
295 root 1.6
296 root 1.3 glEnable (GL_LIGHTING);
297 root 1.9 //GLfloat lightc[4] = { 1, 0.1, 0.1, 1 };
298 root 1.6 //glLightf (GL_LIGHT0, GL_QUADRATIC_ATTENUATION);
299 root 1.9 //glLightfv (GL_LIGHT0, GL_DIFFUSE, lightc);
300 root 1.6 glEnable (GL_LIGHT0);
301 root 1.29
302     cgc = cgCreateContext ();
303 root 1.35
304     vsh_profile = CG_PROFILE_ARBVP1;
305 root 1.36 //if (cgGLIsProfileSupported (CG_PROFILE_VP30)) vsh_profile = CG_PROFILE_VP30;
306     //if (cgGLIsProfileSupported (CG_PROFILE_VP40)) vsh_profile = CG_PROFILE_VP40;
307 root 1.35 fsh_profile = CG_PROFILE_ARBFP1;
308 root 1.36 //if (cgGLIsProfileSupported (CG_PROFILE_FP30)) fsh_profile = CG_PROFILE_FP30;
309     //if (cgGLIsProfileSupported (CG_PROFILE_FP40)) fsh_profile = CG_PROFILE_FP40;
310 root 1.35
311     vsh = cgCreateProgramFromFile (cgc, CG_SOURCE, "vsh.cg", vsh_profile, 0, 0);
312 root 1.29 CheckCgError ();
313     cgGLLoadProgram (vsh);
314     CheckCgError ();
315     mv = cgGetNamedParameter (vsh, "WorldProj");
316     mvp = cgGetNamedParameter (vsh, "WorldViewProj");
317 root 1.30 lightpos = cgGetNamedParameter (vsh, "LightPos");
318 root 1.29 CheckCgError ();
319    
320 root 1.35 fsh = cgCreateProgramFromFile (cgc, CG_SOURCE, "fsh.cg", fsh_profile, 0, 0);
321 root 1.30 CheckCgError ();
322 root 1.29 cgGLLoadProgram (fsh);
323 root 1.30 CheckCgError ();
324 root 1.29
325     cgGLBindProgram (vsh);
326 root 1.30 CheckCgError ();
327 root 1.29 cgGLBindProgram (fsh);
328 root 1.30 CheckCgError ();
329 root 1.1
330     /* Loop until done. */
331     start_time = SDL_GetTicks ();
332     frames = 0;
333 root 1.16
334 root 1.1 while (!done)
335     {
336     GLenum gl_error;
337     char *sdl_error;
338     SDL_Event event;
339    
340     glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
341    
342 root 1.19 camera.p.x += (camera_velocity_factor * camera_velocity.x) * timer.diff;
343     camera.p.y += (camera_velocity_factor * camera_velocity.y) * timer.diff;
344     camera.p.z += (camera_velocity_factor * camera_velocity.z) * timer.diff;
345 root 1.18
346 root 1.30 cgGLSetParameter4f (lightpos, camera.p.x, camera.p.y, camera.p.z, 1);
347 root 1.6
348 root 1.5 #if 0
349 root 1.3 static GLfloat ry;
350 root 1.13 ry += 0.001;
351     camera.d.x = cos (ry);
352     camera.d.z = sin (ry);
353     //camera.d.y = sin (ry * 0.1);
354 root 1.5 #endif
355 root 1.1
356 root 1.34 camera.begin ();
357     camera.pass (view::DEPTH);
358     camera.pass (view::LIGHTED);
359     camera.end ();
360 root 1.1
361     SDL_GL_SwapBuffers ();
362 root 1.15 timer.frame ();
363 root 1.1
364     /* Check for error conditions. */
365     gl_error = glGetError ();
366    
367     if (gl_error != GL_NO_ERROR)
368 root 1.3 fprintf (stderr, "testgl: OpenGL error: %d\n", gl_error);
369 root 1.1
370     sdl_error = SDL_GetError ();
371    
372     if (sdl_error[0] != '\0')
373     {
374     fprintf (stderr, "testgl: SDL error '%s'\n", sdl_error);
375     SDL_ClearError ();
376     }
377    
378     /* Allow the user to see what's happening */
379 root 1.9 //SDL_Delay (20);
380 root 1.1
381     /* Check if there's a pending event. */
382     while (SDL_PollEvent (&event))
383 root 1.18 done = HandleEvent (&event);
384 root 1.20
385 root 1.18
386 root 1.1 ++frames;
387     }
388    
389     /* Print out the frames per second */
390     this_time = SDL_GetTicks ();
391     if (this_time != start_time)
392     {
393     printf ("%2.2f FPS\n",
394     ((float) frames / (this_time - start_time)) * 1000.0);
395     }
396    
397     if (global_image)
398     {
399     SDL_FreeSurface (global_image);
400     global_image = NULL;
401     }
402     if (global_texture)
403     {
404     glDeleteTextures (1, &global_texture);
405     global_texture = 0;
406     }
407    
408     /* Destroy our GL context, etc. */
409     SDL_Quit ();
410     return (0);
411     }
412    
413     int
414     main (int argc, char *argv[])
415     {
416     int i, logo;
417     int numtests;
418     int bpp = 0;
419     int slowly;
420     float gamma = 0.0;
421     int noframe = 0;
422     int fsaa = 0;
423 root 1.4
424 root 1.1 logo = 0;
425     slowly = 0;
426     numtests = 1;
427     for (i = 1; argv[i]; ++i)
428     {
429     if (strcmp (argv[i], "-twice") == 0)
430     {
431     ++numtests;
432     }
433     if (strcmp (argv[i], "-slow") == 0)
434     {
435     slowly = 1;
436     }
437     if (strcmp (argv[i], "-bpp") == 0)
438     {
439     bpp = atoi (argv[++i]);
440     }
441     if (strcmp (argv[i], "-gamma") == 0)
442     {
443     gamma = (float) atof (argv[++i]);
444     }
445     if (strcmp (argv[i], "-noframe") == 0)
446     {
447     noframe = 1;
448     }
449     if (strcmp (argv[i], "-fsaa") == 0)
450     {
451     ++fsaa;
452     }
453     if (strncmp (argv[i], "-h", 2) == 0)
454     {
455     printf
456     ("Usage: %s [-twice] [-logo] [-slow] [-bpp n] [-gamma n] [-noframe] [-fsaa]\n",
457     argv[0]);
458     exit (0);
459     }
460     }
461 root 1.17
462 root 1.1 for (i = 0; i < numtests; ++i)
463 root 1.17 RunGLTest (argc, argv, logo, slowly, bpp, gamma, noframe, fsaa);
464    
465 root 1.1 return 0;
466     }