source: GTP/trunk/App/Demos/Illum/Illumination Networks Demo [OpenGL]/src/main.cpp @ 845

Revision 845, 11.2 KB checked in by szirmay, 18 years ago (diff)
Line 
1// GlutRTTAlap.cpp : Defines the entry point for the console application.
2//
3
4#include "stdafx.h"
5#include "ThirdPersonCamera.h"
6#include <random.h>
7#include <stdio.h>
8
9Scene theScene;
10ThirdPersonCamera theCamera;
11PreIllumSystem *theSystem;
12
13bool headsup=true;
14
15bool anim_on=false;
16char* therendermode;
17int rendermode=0;
18
19SkyBox theSkyBox;
20
21float lightposangle1=0.4;
22float lightposangle2=1.2;
23
24bool isrecording=false;
25int framenum;
26
27Vector SkyColor(0,0,0);
28
29Vector LightPos(0,5,-5);
30//Vector LightColor(3,1.5,0);
31Vector LightColor(4.6,4.6,5.1);
32//Vector LightColor(0,0,0);
33
34Vector LightPos2(1,-10,-1);
35//Vector LightColor2(0.7,0.4,0.5);
36Vector LightColor2(5.6,5.7,6);
37//Vector LightColor2(1,0,0);
38
39Vector positions[2];
40float angle=0;
41
42unsigned int lastTime;
43unsigned int frameRate=0;
44unsigned int frameRateLast=0;
45unsigned int timeElapsed;
46unsigned int LastDtTime=0;
47unsigned int Dt=0;
48bool start=true;
49
50int direction=0;
51
52Impostor ScreenQuad;
53
54RenderTexture PhaseTexture;
55CgProgram PhaseProgram;
56
57
58void drawFrameRate(char *str, void *font, GLclampf r, GLclampf g, GLclampf b,
59              GLfloat x, GLfloat y) {
60  /* font: font to use, e.g., GLUT_BITMAP_HELVETICA_10
61     r, g, b: text colour
62     x, y: text position in window: range [0,0] (bottom left of window)
63           to [1,1] (top right of window). */
64
65  char *ch;
66  GLint matrixMode;
67  GLboolean lightingOn;
68
69 lightingOn= glIsEnabled(GL_LIGHTING);        /* lighting on? */
70 if (lightingOn) glDisable(GL_LIGHTING);
71
72 glGetIntegerv(GL_MATRIX_MODE, &matrixMode);  /* matrix mode? */
73
74 glMatrixMode(GL_PROJECTION);
75 glPushMatrix();
76   glLoadIdentity();
77   gluOrtho2D(0.0, 1.0, 0.0, 1.0);
78   glMatrixMode(GL_MODELVIEW);
79   glPushMatrix();
80     glLoadIdentity();
81     glPushAttrib(GL_COLOR_BUFFER_BIT);       /* save current colour */
82       glColor4f(r, g, b,1);
83       glRasterPos3f(x, y, 0.0);
84       for(ch= str; *ch; ch++) {
85          glutBitmapCharacter(font, (int)*ch);
86       }
87     glPopAttrib();
88   glPopMatrix();
89   glMatrixMode(GL_PROJECTION);
90 glPopMatrix();
91 glMatrixMode(matrixMode);
92 if (lightingOn) glEnable(GL_LIGHTING);
93 glColor4f(1, 1, 1,1);
94}
95
96void calculateFrameRate()
97{
98        unsigned int thisTime=(unsigned)timeGetTime();
99        timeElapsed=thisTime-lastTime;
100        Dt=thisTime-LastDtTime;
101        LastDtTime=thisTime;
102        frameRate++;
103        if(timeElapsed>1000)
104        {
105                lastTime=thisTime;
106                frameRateLast=frameRate;
107                frameRate=0;
108        }       
109}
110
111void RenderPhaseTexture()
112{
113        PhaseTexture.EnableTarget();
114        PhaseProgram.Enable();
115
116        glClearColor(1,1,1,1);
117        glClear(GL_COLOR_BUFFER_BIT);
118
119        glDisable(GL_DEPTH_TEST);
120        ScreenQuad.DisplayScreenQuad();
121        glEnable(GL_DEPTH_TEST);
122
123        PhaseProgram.Disable();
124        PhaseTexture.DisableTarget();
125        PhaseTexture.BindColorBuffer();
126}
127
128static void display()
129{
130       
131        LightPos=Vector(cos(lightposangle1)*cos(lightposangle2),sin(lightposangle2),sin(lightposangle1)*cos(lightposangle2));
132        LightPos=LightPos*6;
133
134        theSystem->setSkyColor(SkyColor);
135
136        calculateFrameRate();
137       
138        if(anim_on)
139        angle+=0.5;
140
141        if(start)
142        {
143                Dt=0;timeElapsed=0;
144                start=false;
145        }
146       
147        float x=cos(angle/180*3.14)*10;
148        float y=sin(angle/180*3.14)*10;
149        //float z=sin(angle/180*30+0.45)*1.5;
150        //LightPos.x=x;
151        //LightPos.z=y;
152        //LightPos.y=0;
153       
154       
155
156        glEnable(GL_DEPTH_TEST);               
157        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);     
158       
159        theSkyBox.Display(theCamera);
160
161        theCamera.SetViewandProjection();
162        //theScene.Display(false);
163       
164        glDisable(GL_LIGHTING);
165        glColor3f(LightColor.x/2.5,LightColor.y/2.5,LightColor.z/2.5);
166        glPushMatrix();
167        glTranslatef(LightPos.x,LightPos.y,LightPos.z);
168        glutSolidSphere(0.1,5,5);
169        glPopMatrix();
170       
171       
172        theSystem->Refresh(LightPos,LightPos2,LightColor,LightColor2);
173
174        glDisable(GL_LIGHTING);
175        glColor3f(LightColor2.x/2.5,LightColor2.y/2.5,LightColor2.z/2.5);
176        glPushMatrix();
177        glTranslatef(LightPos2.x,LightPos2.y,LightPos2.z);
178        glutSolidSphere(0.1,5,5);
179        glPopMatrix();
180               
181        therendermode="Default Dislay";
182        if(rendermode==0)
183                theSystem->Display();
184        else
185        therendermode=theSystem->DisplayTexture(rendermode);
186
187        char fr[255];
188        if(headsup)
189        {
190                sprintf( fr,"FPS:%u Iterate:%i RenderMode:%s Albedo:%f Opacity:%f",frameRateLast,theSystem->m_IterateCount,therendermode,theSystem->getAlbedo(),theSystem->getOpacity());
191        }
192        else
193        {
194                sprintf( fr,"LCol1: r:%.2f g:%.2f b:%.2f LCol2: r:%.2f g:%.2f b:%.2f SkyC: r:%.2f g:%.2f b:%.2f"
195                                                                                                                        ,LightColor.x,LightColor.y,LightColor.z
196                                                                                                                        ,LightColor2.x,LightColor2.y,LightColor2.z
197                                                                                                                        ,SkyColor.x,SkyColor.y,SkyColor.z);
198        }
199        //sprintf( fr,"campos:%f %f %f lookat:%f %f %f",theCamera.getPosition().x,theCamera.getPosition().y,theCamera.getPosition().z,
200        //      theCamera.getLookAt().x,theCamera.getLookAt().y,theCamera.getLookAt().z);
201        drawFrameRate(fr, GLUT_BITMAP_HELVETICA_10, 1.0f, 0.0f, 1.0f, 0.05, 0.95 );
202
203        glutSwapBuffers();
204
205        glutReportErrors();
206
207        if(isrecording)
208        {
209                framenum++;
210        ilutGLScreenie();
211                char fname[25];
212                sprintf( fname,"img%i.tga",framenum);
213                rename("screen0.tga",fname);
214        }
215}       
216
217
218void idle()
219{       
220    glutPostRedisplay();
221}
222
223void getExts(void)
224{
225  if(! glh_init_extensions(
226                          "GL_ARB_multitexture "
227                          "WGL_ARB_pbuffer "
228                          "WGL_ARB_pixel_format "
229                          "WGL_ARB_render_texture "
230                          "GL_NV_vertex_program "
231                          "GL_NV_fragment_program "
232                                                //  "GL_ARB_vertex_program "
233                                                //  "GL_ARB_fragment_program "
234                       //   "WGL_NV_float_buffer"
235                                                "GL_ARB_vertex_buffer_object "
236                                                "GL_ATI_draw_buffers "
237                                                "GL_EXT_blend_equation_separate "
238                                                "GL_EXT_blend_minmax "
239                                                "GL_EXT_blend_func_separate "
240                                                "GL_ARB_point_sprite "
241                        "GL_ARB_point_parameters "
242                                                "GL_EXT_blend_color "
243                                                //"GL_EXT_pixel_buffer_object "
244                                                  )) {
245    fprintf(stderr, "Error - required extensions were not supported: %s", glh_get_unsupported_extensions());
246        exit(-1);
247  }
248}
249
250void specialkey(int key, int x, int y)
251{
252        switch(key)
253        {
254                case GLUT_KEY_F1:
255                        if(glutGetModifiers())
256                        {
257                                theSystem->m_IterateCount--;
258                                if(theSystem->m_IterateCount==-1)
259                                        theSystem->m_IterateCount=0;
260                        }
261                        else
262                                theSystem->m_IterateCount++;
263                        break; 
264               
265                case GLUT_KEY_F2:
266                        if(glutGetModifiers())
267                        {
268                                theSystem->DecreaseAlbedo();
269                        }
270                        else
271                                theSystem->IncreaseAlbedo();
272                        break; 
273               
274                case GLUT_KEY_F3:
275                        if(glutGetModifiers())
276                        {
277                                theSystem->DecreaseOpacity();
278                        }
279                        else
280                                theSystem->IncreaseOpacity();
281                        break; 
282
283                case GLUT_KEY_F4:
284                        if(glutGetModifiers())
285                        {
286                                LightColor.x-=0.1;
287                                if(LightColor.x<0)LightColor.x=0;
288                        }
289                        else
290                        {
291                                LightColor.x+=0.1;
292                                if(LightColor.x>6)LightColor.x=6;
293                        }
294                        break; 
295                case GLUT_KEY_F5:
296                        if(glutGetModifiers())
297                        {
298                                LightColor.y-=0.1;
299                                if(LightColor.y<0)LightColor.y=0;
300                        }
301                        else
302                        {
303                                LightColor.y+=0.1;
304                                if(LightColor.y>6)LightColor.y=6;
305                        }
306                        break;
307                case GLUT_KEY_F6:
308                        if(glutGetModifiers())
309                        {
310                                LightColor.z-=0.1;
311                                if(LightColor.z<0)LightColor.z=0;
312                        }
313                        else
314                        {
315                                LightColor.z+=0.1;
316                                if(LightColor.z>6)LightColor.z=6;
317                        }
318                        break; 
319
320
321                        case GLUT_KEY_F7:
322                        if(glutGetModifiers())
323                        {
324                                LightColor2.x-=0.1;
325                                if(LightColor2.x<0)LightColor2.x=0;
326                        }
327                        else
328                        {
329                                LightColor2.x+=0.1;
330                                if(LightColor2.x>6)LightColor2.x=6;
331                        }
332                        break; 
333                case GLUT_KEY_F8:
334                        if(glutGetModifiers())
335                        {
336                                LightColor2.y-=0.1;
337                                if(LightColor2.y<0)LightColor2.y=0;
338                        }
339                        else
340                        {
341                                LightColor2.y+=0.1;
342                                if(LightColor2.y>6)LightColor2.y=6;
343                        }
344                        break;
345                case GLUT_KEY_F9:
346                        if(glutGetModifiers())
347                        {
348                                LightColor2.z-=0.1;
349                                if(LightColor2.z<0)LightColor2.z=0;
350                        }
351                        else
352                        {
353                                LightColor2.z+=0.1;
354                                if(LightColor2.z>6)LightColor2.z=6;
355                        }
356                        break; 
357
358
359                        case GLUT_KEY_F10:
360                        if(glutGetModifiers())
361                        {
362                                SkyColor.x-=0.1;
363                                if(SkyColor.x<0)SkyColor.x=0;
364                        }
365                        else
366                        {
367                                SkyColor.x+=0.1;
368                                if(SkyColor.x>6)SkyColor.x=6;
369                        }
370                        break; 
371                case GLUT_KEY_F11:
372                        if(glutGetModifiers())
373                        {
374                                SkyColor.y-=0.1;
375                                if(SkyColor.y<0)SkyColor.y=0;
376                        }
377                        else
378                        {
379                                SkyColor.y+=0.1;
380                                if(SkyColor.y>6)SkyColor.y=6;
381                        }
382                        break;
383                case GLUT_KEY_F12:
384                        if(glutGetModifiers())
385                        {
386                                SkyColor.z-=0.1;
387                                if(SkyColor.z<0)SkyColor.z=0;
388                        }
389                        else
390                        {
391                                SkyColor.z+=0.1;
392                                if(SkyColor.z>6)SkyColor.z=6;
393                        }
394                        break;
395/*
396                case GLUT_KEY_LEFT:
397                        //theCamera.Rotate(AXIS_Y,1);
398                        theCamera.Rotate(Vector(0,1,0));
399                        break;
400                case GLUT_KEY_RIGHT:
401                        //theCamera.Rotate(AXIS_Y,-1);
402                        theCamera.Rotate(Vector(0,-1,0));
403                        break;
404                case GLUT_KEY_UP:
405                        //theCamera.Rotate(AXIS_X,1);
406                        theCamera.Rotate(Vector(1,0,0));
407                        break;
408                case GLUT_KEY_DOWN:
409                        //theCamera.Rotate(AXIS_X,-1);
410                        theCamera.Rotate(Vector(-1,0,0));
411                        break;
412
413*/
414        case GLUT_KEY_LEFT:
415                        lightposangle1+=0.02;
416                        break;
417                case GLUT_KEY_RIGHT:
418                        lightposangle1-=0.02;
419                        break;
420                case GLUT_KEY_UP:
421                        lightposangle2+=0.02;
422                        break;
423                case GLUT_KEY_DOWN:
424                        lightposangle2-=0.02;
425                        break;
426        }
427}
428
429static void keyboard(unsigned char key, int x, int y)
430{
431   
432        switch(key)
433        {               
434                case 27:
435                        exit(0);
436                        break;
437
438                case ' ':
439                        anim_on=!anim_on;
440                        break;
441
442                case 'w':
443                        theCamera.Move(AXIS_Z,0.1);
444                        break;
445                case 's':
446                        theCamera.Move(AXIS_Z,-0.1);
447                        break;
448                case 'd':
449                        theCamera.Move(AXIS_X,0.1);
450                        break;
451                case 'a':
452                        theCamera.Move(AXIS_X,-0.1);
453                        break;
454
455                case 'r':
456                        theCamera.Move(AXIS_Y,0.1);
457                        break;
458                case 'f':
459                        theCamera.Move(AXIS_Y,-0.1);
460                        break;
461
462                case 'l':
463                        rendermode++;
464                        if(rendermode==7)
465                                rendermode=0;
466                        break;
467
468                case 'L':
469                        rendermode--;
470                        if(rendermode==-1)
471                                rendermode=6;
472                        break;
473               
474                case 'm':
475                        ilutGLScreenie();//screenshot
476                        break; 
477
478                case 'p':
479                        theSystem->ResetIllumTexture();
480                        break;
481                case 'o':
482                        isrecording=!isrecording;
483                        break;
484                case 'h':
485                        headsup=!headsup;
486                        break;
487        }
488}
489
490void initOpenGL()
491{
492        srand( (unsigned)timeGetTime() );
493
494        glEnable(GL_DEPTH_TEST);
495        glClearDepth(1.0);
496        //glClearColor(.6, .5, 1.0, 0.0);
497        glClearColor(.6, .6, 0.6, 0.0);
498       
499        getExts();
500        /*
501        theCamera.Move(AXIS_X,0);
502        theCamera.Move(AXIS_Y,0);       
503        theCamera.Move(AXIS_Z,-3);      */
504        //theCamera.Rotate(AXIS_Y,115);
505        //theCamera.Rotate(AXIS_X,0);
506
507        theCamera.setFarClipDistance(100);
508        theCamera.setNearClipDistance(0.01);
509       
510        theSkyBox.Initialize("bluedream");
511        //theSkyBox.Initialize("simple");
512
513        //theScene.Load("thinker.txt");
514
515        theSystem=new PreIllumSystem;
516        theSystem->SetEyeCamera(&theCamera);   
517        theSystem->Init(512,128);
518}
519
520int _tmain(int argc, _TCHAR* argv[])
521{
522        //srand(timeGetTime());
523        glutInit(&argc, argv);
524        ilInit();
525        iluInit();
526        ilutRenderer(ILUT_OPENGL);
527       
528    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
529    glutInitWindowSize(512, 512);
530    glutCreateWindow("GlutRTTAlap");
531
532        initOpenGL();
533               
534        glutSpecialFunc(specialkey);
535        glutKeyboardFunc(keyboard);
536    glutDisplayFunc(display);
537
538        glutIdleFunc(idle);
539   
540    glutMainLoop();
541
542   return 0;
543}
544
545
Note: See TracBrowser for help on using the repository browser.