void App::onInit() { GApp::onInit(); renderDevice->setSwapBuffersAutomatically(true); logPrintf("App::onInit()\n"); createDeveloperHUD(); showRenderingStats = false; developerWindow->cameraControlWindow->setVisible(false); developerWindow->setVisible(false); developerWindow->videoRecordDialog->setCaptureGui(false); m_debugCamera->filmSettings().setBloomStrength(0.20f); m_debugCamera->filmSettings().setBloomRadiusFraction(0.017f); m_debugCamera->filmSettings().setAntialiasingEnabled(true); m_debugCamera->filmSettings().setCelluloidToneCurve(); if (! filename.empty()) { window()->setCaption(filenameBaseExt(filename) + " - G3D Viewer"); } lighting = shared_ptr<LightingEnvironment>(new LightingEnvironment()); lighting->lightArray.clear(); // The spot light is designed to just barely fit the 3D models. Note that it has no attenuation lighting->lightArray.append(Light::spotTarget("Light", Point3(40, 120, 80), Point3::zero(), 10 * units::degrees(), Power3(50.0f), 1, 0, 0, true, 8192)); lighting->lightArray.last()->shadowMap()->setBias(0.1f); Texture::Encoding e; e.readMultiplyFirst = Color4(Color3(0.5f)); e.format = ImageFormat::RGB32F(); lighting->environmentMapArray.append(Texture::fromFile(System::findDataFile("uffizi/uffizi-*.exr"), e, Texture::DIM_CUBE_MAP)); lighting->ambientOcclusionSettings.numSamples = 24; lighting->ambientOcclusionSettings.radius = 0.75f * units::meters(); lighting->ambientOcclusionSettings.intensity = 2.0f; lighting->ambientOcclusionSettings.bias = 0.06f * units::meters(); lighting->ambientOcclusionSettings.useDepthPeelBuffer = true; m_debugCamera->setFarPlaneZ(-finf()); m_debugCamera->setNearPlaneZ(-0.05f); // Don't clip to the near plane glDisable(GL_DEPTH_CLAMP); colorClear = Color3::white() * 0.9f; //modelController = ThirdPersonManipulator::create(); m_gbufferSpecification.encoding[GBuffer::Field::CS_POSITION_CHANGE].format = NULL; gbuffer()->setSpecification(m_gbufferSpecification); setViewer(filename); developerWindow->sceneEditorWindow->setVisible(false); logPrintf("Done App::onInit()\n"); }
int main(void){ static const int width(1280), height(720); sf::Window window(sf::VideoMode(width, height), "OpenGL", sf::Style::Default, sf::ContextSettings(32)); window.setFramerateLimit(60); GLint error; if(GLEW_OK != (error = glewInit())) { std::cerr << "Glew init failed" << std::endl; return EXIT_FAILURE; } // RAW CPU DATA int count = 12; int index[] = {0, 1, 2, 2, 1, 3, 4, 5, 6, 6, 5, 7, 8, 9, 10, 10, 9, 11, 12, 13, 14, 14, 13, 15, 16, 17, 18, 19, 17, 20, 21, 22, 23, 24, 25, 26, }; float uv[] = {0.f, 0.f, 0.f, 1.f, 1.f, 0.f, 1.f, 1.f, 0.f, 0.f, 0.f, 1.f, 1.f, 0.f, 1.f, 1.f, 0.f, 0.f, 0.f, 1.f, 1.f, 0.f, 1.f, 1.f, 0.f, 0.f, 0.f, 1.f, 1.f, 0.f, 1.f, 1.f, 0.f, 0.f, 0.f, 1.f, 1.f, 0.f, 1.f, 0.f, 1.f, 1.f, 0.f, 1.f, 1.f, 1.f, 0.f, 0.f, 0.f, 0.f, 1.f, 1.f, 1.f, 0.f, }; float vertices[] = {-0.5, -0.5, 0.5, 0.5, -0.5, 0.5, -0.5, 0.5, 0.5, 0.5, 0.5, 0.5, -0.5, 0.5, 0.5, 0.5, 0.5, 0.5, -0.5, 0.5, -0.5, 0.5, 0.5, -0.5, -0.5, 0.5, -0.5, 0.5, 0.5, -0.5, -0.5, -0.5, -0.5, 0.5, -0.5, -0.5, -0.5, -0.5, -0.5, 0.5, -0.5, -0.5, -0.5, -0.5, 0.5, 0.5, -0.5, 0.5, 0.5, -0.5, 0.5, 0.5, -0.5, -0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, 0.5, -0.5, 0.5, -0.5, -0.5, 0.5, -0.5, -0.5, -0.5, 0.5, -0.5, 0.5, 0.5 }; float normals[] = {0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, }; int quad_triangleCount = 2; int quad_triangleList[] = {0, 1, 2, 2, 1, 3}; float quad_vertices[] = {-1.0, -1.0, 1.0, -1.0, -1.0, 1.0, 1.0, 1.0}; // Creatin Buffer Objects hglt::VAO vao; hglt::VAO quadVAO; hglt::VBO quadIndexVBO; hglt::VBO quadVerticesVBO; hglt::VBO indexVBO; hglt::VBO verticesVBO; hglt::VBO normalsVBO; hglt::VBO uvVBO; // Send RAW Data from CPU to BO in GPU /// Quad quadVAO.bind(); quadIndexVBO.bind(GL_ELEMENT_ARRAY_BUFFER); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(quad_triangleList), quad_triangleList, GL_STATIC_DRAW); quadVerticesVBO.bind(); glEnableVertexAttribArray(0); glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(GL_FLOAT)*2, (void*)0); glBufferData(GL_ARRAY_BUFFER, sizeof(quad_vertices), quad_vertices, GL_STATIC_DRAW); quadVAO.unbind(); /// Index indexVBO.bind(GL_ELEMENT_ARRAY_BUFFER); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(index), index, GL_STATIC_DRAW); hglt::VBO::unbind(GL_ELEMENT_ARRAY_BUFFER); /// Vertices verticesVBO.bind(); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); hglt::VBO::unbind(); /// Normals normalsVBO.bind(); glBufferData(GL_ARRAY_BUFFER, sizeof(normals), normals, GL_STATIC_DRAW); hglt::VBO::unbind(); /// UV uvVBO.bind(); glBufferData(GL_ARRAY_BUFFER, sizeof(uv), uv, GL_STATIC_DRAW); hglt::VBO::unbind(); // Build VAO vao.bind(); // Set Index GL_ELEMENT_ARRAY_BUFFER indexVBO.bind(GL_ELEMENT_ARRAY_BUFFER); // Set Buffer Object GL_ARRAY_BUFFER glEnableVertexAttribArray(0); glEnableVertexAttribArray(1); glEnableVertexAttribArray(2); verticesVBO.bind(GL_ARRAY_BUFFER); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (const GLvoid*) (0 * sizeof(GLfloat))); hglt::VBO::unbind(); normalsVBO.bind(); glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (const GLvoid*) (0 * sizeof(GLfloat))); hglt::VBO::unbind(); uvVBO.bind(); glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), (const GLvoid*) (0 * sizeof(GLfloat))); hglt::VBO::unbind(); hglt::VAO::unbind(); // LOAD SHADER hglt::Program geometryShader = hglt::loadProgram("shader/simple.vs.glsl", "shader/simple.fs.glsl"); hglt::Program laccumShader = hglt::loadProgram( "shader/lightAccumulation.vs.glsl", "shader/lightAccumulation.fs.glsl"); hglt::Program blitShader = hglt::loadProgram( "shader/blit.vs.glsl", "shader/blit.fs.glsl"); // LOAD TEXTURE hglt::Texture spec = hglt::loadTexture2D("texture/spec.tga"); hglt::Texture diff = hglt::loadTexture2D("texture/diff.tga", GL_TEXTURE1); // Get Uniform Location GLint ksLocation = glGetUniformLocation(geometryShader.getGLId(), "uMat.ks"); GLint kdLocation = glGetUniformLocation(geometryShader.getGLId(), "uMat.kd"); GLint shininessLocation = glGetUniformLocation(geometryShader.getGLId(), "uMat.shininess"); GLint laccumMaterialLocation = glGetUniformLocation(laccumShader.getGLId(), "uMaterial"); GLint laccumNormalLocation = glGetUniformLocation(laccumShader.getGLId(), "uNormal"); GLint laccumDepthLocation = glGetUniformLocation(laccumShader.getGLId(), "uDepth"); GLint laccumCameraPositionLocation = glGetUniformLocation(laccumShader.getGLId(), "uCameraPosition"); GLint laccumInverseViewProjectionLocation = glGetUniformLocation(laccumShader.getGLId(), "uInvViewProjection"); GLint laccumLightPositionLocation = glGetUniformLocation(laccumShader.getGLId(), "uLightPosition"); GLint laccumLightColorLocation = glGetUniformLocation(laccumShader.getGLId(), "uLightColor"); GLint laccumLightIntensityLocation = glGetUniformLocation(laccumShader.getGLId(), "uLightIntensity"); GLuint blitTex1Location = glGetUniformLocation(blitShader.getGLId(), "uTexture1"); glUniform1i(kdLocation, 0); glUniform1i(ksLocation, 1); // TRANSORMATION MATRIX glm::mat4 model(1.0); glm::mat4 view(1.0); glm::mat4 projection(1.0); GLint modelLocation = glGetUniformLocation(geometryShader.getGLId(), "uModel"); GLint viewLocation = glGetUniformLocation(geometryShader.getGLId(), "uView"); GLint projectionLocation = glGetUniformLocation(geometryShader.getGLId(), "uProjection"); GLint cameraPositionLocation = glGetUniformLocation(geometryShader.getGLId(), "uCameraPosition"); GLint timeLocation = glGetUniformLocation(geometryShader.getGLId(), "uTime"); //std::cout << glm::to_string(model) << std::endl; sf::Clock clock; sf::Clock clock2; // Create Camera hglt::Camera camera; glEnable(GL_DEPTH_TEST); // Alloc FrameBuffer hglt::FrameBuffer gbuffer(width, height, 2); if(!gbuffer.isValid()){ std::cerr << "Can't build FrameBuffer." << std::endl; return EXIT_FAILURE; } // Main Loop while (window.isOpen()){ sf::Time frameTime = clock.restart(); float framerate = 1 / (frameTime.asMilliseconds() * 0.001); //std::cout << framerate << "FPS" << std::endl; // GEOMETRY PASS gbuffer.bind(); glDrawBuffers(gbuffer.outCount, gbuffer.drawBuffers); glViewport( 0, 0, width, height ); glEnable(GL_DEPTH_TEST); // Reset glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Use Geometry Shader geometryShader.use(); // Upload Uniform glUniformMatrix4fv(projectionLocation, 1, GL_FALSE, glm::value_ptr(camera.getProjectionMatrix())); glUniformMatrix4fv(viewLocation, 1, GL_FALSE, glm::value_ptr(camera.getViewMatrix())); glUniformMatrix4fv(modelLocation, 1, GL_FALSE, glm::value_ptr(model)); glUniform3fv(cameraPositionLocation, 1, glm::value_ptr(camera.getPosition())); glUniform1f(timeLocation, clock2.getElapsedTime().asMilliseconds()* 0.001); glUniform1f(shininessLocation, 1000.0f); // Draw /// Bind Texture diff.bind(GL_TEXTURE0); spec.bind(GL_TEXTURE1); vao.bind(); //glDrawArrays(GL_TRIANGLES, 0, 3); glDrawElements(GL_TRIANGLES, count*3, GL_UNSIGNED_INT, 0); hglt::VAO::unbind(); hglt::Texture::unbind(); hglt::FrameBuffer::unbind(); // LIGHT ACCUMULATION PASS glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); laccumShader.use(); glUniform1i(laccumMaterialLocation, 0); glUniform1i(laccumNormalLocation, 1); glUniform1i(laccumDepthLocation, 2); glUniform3fv(laccumCameraPositionLocation, 1, glm::value_ptr(camera.getPosition())); glUniformMatrix4fv( laccumInverseViewProjectionLocation, 1, 0, glm::value_ptr(glm::inverse(camera.getProjectionMatrix()))); // Bind color to unit 0 glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, gbuffer.colorTexId[0]); // Bind normal to unit 1 glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, gbuffer.colorTexId[1]); // Bind depth to unit 2 glActiveTexture(GL_TEXTURE2); glBindTexture(GL_TEXTURE_2D, gbuffer.depthTexId); // Blit above the rest glDisable(GL_DEPTH_TEST); glEnable(GL_BLEND); glBlendFunc(GL_ONE, GL_ONE); for (unsigned int i = 0; i < 10; ++i) { float tl = clock2.getElapsedTime().asMilliseconds()* 0.0001 * i; //Update light uniforms float lightPosition[3] = { sinf(tl) * 10.0f, 0.5f, cosf(tl) * 10.0f}; float lightColor[3] = {sinf(tl) * 1.0f, 1.0f - cosf(tl), -sinf(tl)}; float lightIntensity = 10.0f; glUniform3fv(laccumLightPositionLocation, 1, lightPosition); glUniform3fv(laccumLightColorLocation, 1, lightColor); glUniform1f(laccumLightIntensityLocation, lightIntensity); // Draw quad glBindVertexArray(quadVAO.getGLId()); glDrawElements(GL_TRIANGLES, quad_triangleCount * 3, GL_UNSIGNED_INT, (void*)0); } glDisable(GL_BLEND); // Bind blit shader blitShader.use(); // Upload uniforms glUniform1i(blitTex1Location, 0); // use only unit 0 glActiveTexture(GL_TEXTURE0); // Viewport glViewport( 0, 0, width/3, height/4 ); // Bind texture glBindTexture(GL_TEXTURE_2D, gbuffer.colorTexId[0]); // Draw quad quadVAO.bind(); glDrawElements(GL_TRIANGLES, quad_triangleCount * 3, GL_UNSIGNED_INT, (void*)0); // Viewport glViewport( width/3, 0, width/3, height/4 ); // Bind texture glBindTexture(GL_TEXTURE_2D, gbuffer.colorTexId[1]); // Draw quad quadVAO.bind(); glDrawElements(GL_TRIANGLES, quad_triangleCount * 3, GL_UNSIGNED_INT, (void*)0); // Viewport glViewport( width/3 * 2, 0, width/3, height/4 ); // Bind texture glBindTexture(GL_TEXTURE_2D, gbuffer.depthTexId); // Draw quad quadVAO.bind(); glDrawElements(GL_TRIANGLES, quad_triangleCount * 3, GL_UNSIGNED_INT, (void*)0); // Check for OpenGL errors GLenum err = glGetError(); if(err != GL_NO_ERROR) std::cerr << "OpenGL Error" << gluErrorString(err); sf::Event event; while(window.pollEvent(event)){ if(event.type == sf::Event::Closed) window.close(); else if(event.type == sf::Event::Resized) glViewport(0, 0, event.size.width, event.size.height); } { // Keyboard Manager static bool shiftLock(false); if( sf::Keyboard::isKeyPressed(sf::Keyboard::Escape) ) window.close(); if( sf::Keyboard::isKeyPressed(sf::Keyboard::Z) ) camera.moveFront(-0.1); if( sf::Keyboard::isKeyPressed(sf::Keyboard::S) ) camera.moveFront(0.1); if( sf::Keyboard::isKeyPressed(sf::Keyboard::LShift) ){ static sf::Vector2i lockMousePos; if( shiftLock ){ sf::Vector2i mouse = sf::Mouse::getPosition(window); camera.rotateLeft(float(mouse.x - lockMousePos.x) / 10.0); camera.rotateUp(float(mouse.y - lockMousePos.y) / 10.0); sf::Mouse::setPosition(lockMousePos, window); } else{ shiftLock = true; lockMousePos = sf::Mouse::getPosition(window); window.setMouseCursorVisible(false); } } else{ shiftLock = false; window.setMouseCursorVisible(true); } } window.display(); } return EXIT_SUCCESS; }