// GPUObscurances. // Sergi Funtané & Nicolau Sunyer. #define GLEW_STATIC 1 #include "glew.h" #include #include #include #include "textfile.h" //Includes #include "CMesh.h" #include "Maths.h" #include "vcObscuranceMap.h" //GUI includes #include "Main.h" #include "MyFrame.h" #include //To use Offsets in the PBOs. #define BUFFER_OFFSET(i) ((char *)NULL + (i)) //GLSL // Handlers GLuint vRender,fRender,pRender; GLuint vDepth, fDepth, pDepth; //Uniform Parameters //Depth Step GLint locAngle; //Render Step GLint ProjParam0; GLint texture0; GLint texture20; GLint texture30; GLint kd0; GLint lpos0; GLint Time0; GLint ModeDirect0; GLint ModeIndirect0; GLint ModeAmbient0; GLint AmbientIntensity0; GLint LightPower0; GLint Factor0; GLint LightMap0; //End GLSL //FBO GLuint fb; static GLuint texFBO[2]; //Textures static GLuint texNam[2]; int g_nFPS = 0, g_nFrames = 0; // FPS and FPS Counter DWORD g_dwLastFPS = 0; // Last FPS Check Time float it = 0.0; float angle = 0.0; float mode = 0.0; float modeDirect = 0.0, modeIndirect = 0.0, modeAmbient = 0.0, modeLight = 0.0; float intensityr = 0.5, intensityg = 0.5, intensityb = 0.5, intensityinc = 0.1; float lightr = 0.5, lightg = 0.5, lightb = 0.5, lightinc = 0.1; float factor = 0.5, factorinc = 0.1; //int vsync = 0; GLfloat *m_pSpotlight=NULL; float *ObscuranceMap; static CSphere Esf; //Mouse control float zoom = 5.0f; float width, height; float rotx = 0; float roty = 0.001f; float tx = 0; float ty = 0; int lastx=0; int lasty=0; unsigned char Buttons[3] = {0}; MATRIX4X4 project,mview,inverse; static MATRIX4X4 biasMatrix(0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 0.5f, 0.0f, 0.5f, 0.5f, 0.5f, 1.0f); //bias from [-1, 1] to [0, 1] MATRIX4X4 textureMatrix; CMesh geom; //End global variables //testing void changeSize1(int w, int h) { // Prevent a divide by zero, when window is too short // (you cant make a window of zero width). if(h == 0) h = 1; // we're storing these values for latter use in the // mouse motion functions width = w; height = h; float ratio = (GLfloat) w / (GLfloat)h; // Reset the coordinate system before modifying glMatrixMode(GL_PROJECTION); glLoadIdentity(); // Set the viewport to be the entire window glViewport(0, 0, w, h); // Set the correct perspective. gluPerspective(30,ratio,0.1,100.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } //Functonal to use when the window is resized void changeSize2(int w, int h) { if(h == 0) h = 1; float ratio = (GLfloat) w / (GLfloat)h; glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(60,ratio,0.01,100); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glViewport(0, 0, w, h); } //Keyboard Processing void processNormalKeys(unsigned char key, int x, int y) { if (key == 27) exit(0); else if (key=='1') modeDirect = ((modeDirect == 1.0)? 0.0:1.0); else if (key == '2') modeIndirect = ((modeIndirect == 1.0)? 0.0:1.0); else if (key == '3') modeAmbient = ((modeAmbient == 1.0)? 0.0:1.0); else if (key == '4') modeLight = ((modeLight == 1.0)? 0.0:1.0); else if (key == 'z') factor -= factorinc; else if (key == 'x') factor += factorinc; else if (key == 'c') angle--; else if (key == 'v') angle++; else if (key == 'q') intensityr += intensityinc; else if (key == 'w') intensityg += intensityinc; else if (key == 'e') intensityb += intensityinc; else if (key == 'a') intensityr -= intensityinc; else if (key == 's') intensityg -= intensityinc; else if (key == 'd') intensityb -= intensityinc; else if (key == 'r') { intensityr += intensityinc; intensityg += intensityinc; intensityb += intensityinc; } else if (key == 'f') { intensityr -= intensityinc; intensityg -= intensityinc; intensityb -= intensityinc; } else if (key == 'u') lightr += lightinc; else if (key == 'h') lightg += lightinc; else if (key == 'i') lightb += lightinc; else if (key == 'j') lightr -= lightinc; else if (key == 'o') lightg -= lightinc; else if (key == 'k') lightb -= lightinc; else if (key == 'p') { lightr += lightinc; lightg += lightinc; lightb += lightinc; } else if (key == 'l') { lightr -= lightinc; lightg -= lightinc; lightb -= lightinc; }; if(lightr < 0.0) lightr = 0.0; if(lightg < 0.0) lightg = 0.0; if(lightb < 0.0) lightb = 0.0; printf("\nDirect: %1.0f Indirect: %1.0f Ambient: %1.0f Factor: %1.1f Angle: %1.0f",modeDirect,modeIndirect,modeAmbient,factor,angle); } void init(int numargs, char **argv) { //->vcVrmlLoaderLoadFile("tricicle.wrl", &vcO); int err, i, j; int qualitat = 1; int resX = 256 * pow(2,qualitat-1), resY = 256 * pow(2,qualitat-1); int RX = 512; int RY = 512; ObscuranceMap = new float[resX * resY * 4]; printf("\nAbans: ResolucioX = %d , ResolucioY = %d", resX, resY); err = vcObscurerGenerateImage(geom, numargs, argv, qualitat, &resX, &resY, &ObscuranceMap); printf("\nx = %f, y = %f, z = %f\n", geom.m_pObject._geo[0]._triSet[0]._ver[0], geom.m_pObject._geo[0]._triSet[0]._ver[1], geom.m_pObject._geo[0]._triSet[0]._ver[2]); printf("\nDespres: ResolucioX = %d , ResolucioY = %d", resX, resY); // GLubyte *Obscurance= new GLubyte[resX * resY * 4]; // for(int s = 0; s < resX * resY * 4; s++) Obscurance[s] = (GLubyte) ObscuranceMap[s]; m_pSpotlight = new float[RX * RY * 4]; for(i = 0; iRX*RY/5.0) { m_pSpotlight[4*(i*RY+j)] = 0.0; m_pSpotlight[4*(i*RY+j)+1] = 0.0; m_pSpotlight[4*(i*RY+j)+2] = 0.0; m_pSpotlight[4*(i*RY+j)+3] = 1.0; } else { m_pSpotlight[4*(i*RY+j)] = 1.0; m_pSpotlight[4*(i*RY+j)+1] = 1.0; m_pSpotlight[4*(i*RY+j)+2] = 1.0; m_pSpotlight[4*(i*RY+j)+3] = 1.0; } } } //Create Textures. glGenTextures(2, &texNam[0]); // Create The Textures //Texture // Binding glBindTexture( GL_TEXTURE_2D, texNam[0] ); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, resX, resY, 0, GL_RGBA, GL_FLOAT, ObscuranceMap); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glBindTexture( GL_TEXTURE_2D, texNam[1] ); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA_FLOAT32_ATI, RX, RY, 0, GL_RGBA, GL_FLOAT, m_pSpotlight); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glGenFramebuffersEXT(1, &fb); glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb); glGenTextures(1,&texFBO[0]); glBindTexture( GL_TEXTURE_2D, texFBO[0] ); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA_FLOAT32_ATI, RX, RY, 0, GL_RGBA, GL_FLOAT, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, texFBO[0],0); glGenRenderbuffersEXT(1, &texFBO[1]); glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, texFBO[1]); glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT24, RX, RY ); glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, texFBO[1]); //We unbind the texture. glBindTexture(GL_TEXTURE_2D, 0); //We set the rendering context to render to the backbuffer again glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); //GLSL //Depth Shaders char *vsDepth = NULL, *fsDepth = NULL; vDepth = glCreateShader(GL_VERTEX_SHADER); fDepth = glCreateShader(GL_FRAGMENT_SHADER); vsDepth = textFileRead("depth.vert"); fsDepth = textFileRead("depth.frag"); const char *vvDepth = vsDepth; const char *ffDepth = fsDepth; glShaderSource(vDepth, 1, &vvDepth, NULL); glShaderSource(fDepth, 1, &ffDepth, NULL); free(vsDepth); free(fsDepth); glCompileShader(vDepth); glCompileShader(fDepth); pDepth = glCreateProgram(); glAttachShader(pDepth, vDepth); glAttachShader(pDepth, fDepth); glLinkProgram(pDepth); glUseProgram(pDepth); locAngle = glGetUniformLocation(pDepth, "angle"); glUseProgram(0); //Render Shaders char *vsRender = NULL, *fsRender = NULL; vRender = glCreateShader(GL_VERTEX_SHADER); fRender = glCreateShader(GL_FRAGMENT_SHADER); vsRender = textFileRead("render.vert"); fsRender = textFileRead("render.frag"); const char *vvRender = vsRender; const char *ffRender = fsRender; glShaderSource(vRender, 1, &vvRender, NULL); glShaderSource(fRender, 1, &ffRender, NULL); free(vsRender); free(fsRender); glCompileShader(vRender); glCompileShader(fRender); pRender = glCreateProgram(); glAttachShader(pRender, vRender); glAttachShader(pRender, fRender); glLinkProgram(pRender); glUseProgram(pRender); ProjParam0 = glGetUniformLocation(pRender, "projMatrix"); Time0 = glGetUniformLocation(pRender, "time"); texture0 = glGetUniformLocation(pRender, "texture"); texture20 = glGetUniformLocation(pRender, "shadowMap"); texture30 = glGetUniformLocation(pRender, "spotlight"); kd0 = glGetUniformLocation(pRender, "Kd"); lpos0 = glGetUniformLocation(pRender, "LightPos"); ModeDirect0 = glGetUniformLocation(pRender, "modeDirect"); ModeIndirect0 = glGetUniformLocation(pRender, "modeIndirect"); ModeAmbient0 = glGetUniformLocation(pRender, "modeAmbient"); AmbientIntensity0 = glGetUniformLocation(pRender, "intensity"); LightPower0 = glGetUniformLocation(pRender, "lpower"); Factor0 = glGetUniformLocation(pRender, "factor"); LightMap0 = glGetUniformLocation(pRender, "modeLight"); glUseProgram(0); //End GLSL //->geom.LoadMesh(numargs, argv); for(i = 0; iglDrawElements( GL_TRIANGLES, geom.m_pObject._geo[i]._triSet[j]._numTris*3,GL_UNSIGNED_INT,BUFFER_OFFSET(0) ); // Draw All Of The Quads At Once } } glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); glUseProgram(0); } //Render the Scene void renderScene(void) { renderDepth(); glUseProgram(pRender); changeSize2(512, 512); glLoadIdentity(); glTranslatef(0,0,-zoom); glTranslatef(tx,ty,0); glRotatef(rotx,1,0,0); glRotatef(roty,0,1,0); glRotatef(90.0f, 0.0f, 0.0f, 1.0f); glClearColor(1.0,1.0,1.0,1.0); glClearDepth(1.0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glViewport(0,0,512*2,512*2); textureMatrix = biasMatrix * project * mview; glMatrixMode(GL_TEXTURE); glLoadIdentity(); glMultMatrixf(textureMatrix); glEnable(GL_CULL_FACE); glUniform1f(Time0, angle); glUniform1f(ModeDirect0, modeDirect); glUniform1f(ModeIndirect0, modeIndirect); glUniform1f(ModeAmbient0, modeAmbient); glUniform1f(LightMap0, modeLight); glUniform3f(kd0, 0.5,0.5,0.5); glUniform3f(AmbientIntensity0, intensityr,intensityg,intensityb); glUniform3f(LightPower0, lightr,lightg,lightb); glUniform3f(Factor0, factor,factor,factor); glUniform3f(lpos0, (Esf.radius+1.0) * cos(angle/50), (Esf.radius+1.0) * sin(angle/50), 1.0); glUniformMatrix4fv(ProjParam0, 1, GL_FALSE, textureMatrix); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, texNam[0]); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glUniform1i(texture0, 0); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, texFBO[0]); glUniform1i(texture20, 1); glActiveTexture(GL_TEXTURE2); glBindTexture(GL_TEXTURE_2D, texNam[1]); glUniform1i(texture30,2); glActiveTexture(GL_TEXTURE0); // Get FPS if( GetTickCount() - g_dwLastFPS >= 1000 ) // When A Second Has Passed... { g_dwLastFPS = GetTickCount(); // Update Our Time Variable g_nFPS = g_nFrames; // Save The FPS g_nFrames = 0; // Reset The FPS Counter char szTitle[256]={0}; // Build The Title String sprintf( szTitle, "Obscurances: Nicolau i Sergi - %d FPS", g_nFPS ); glutSetWindowTitle(szTitle); } g_nFrames++; // Increment Our FPS Counter // Enable Pointers glEnableClientState( GL_VERTEX_ARRAY ); // Enable Vertex Arrays glEnableClientState( GL_TEXTURE_COORD_ARRAY ); // Enable Texture Coord Arrays glEnableClientState( GL_COLOR_ARRAY ); glEnableClientState( GL_NORMAL_ARRAY ); for(int i=0; iShow(true); SetTopWindow(frame); return true; } void Main::OnIdle(wxIdleEvent& event) { //Obtenim els resultats dels Views (estaran en threads?) //... ids de textura //wake up idle: wxWakeUpIdle(); event.Skip(); // normal processing } //Main Program /*->void main(int argc, char **argv) { modeDirect = 0.0; modeIndirect = 0.0; modeAmbient = 0.0; glutInit(&argc, argv); glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA); glutInitWindowPosition(100,100); glutInitWindowSize(1024,1024); glutCreateWindow("Test Pbuffer"); glutReshapeFunc(changeSize1); GLenum err = glewInit(); glutDisplayFunc(renderScene); glutIdleFunc(renderScene); glutMouseFunc(Mouse); glutMotionFunc(Motion); glutIgnoreKeyRepeat(1); glutKeyboardFunc(processNormalKeys); init(argc, argv); glutMainLoop(); }*/