void Sphere::draw( const std::string &_shaderName, ngl::TransformStack &_transformStack, const ngl::Mat4 &_globalTx, ngl::Camera *_cam )const { // draw wireframe if hit if(m_hit) { glPolygonMode(GL_FRONT_AND_BACK,GL_LINE); } else { glPolygonMode(GL_FRONT_AND_BACK,GL_FILL); } ngl::ShaderLib *shader=ngl::ShaderLib::instance(); shader->use(_shaderName); // grab an instance of the primitives for drawing ngl::VAOPrimitives *prim=ngl::VAOPrimitives::instance(); _transformStack.pushTransform(); { _transformStack.setPosition(m_pos); _transformStack.setScale(m_radius,m_radius,m_radius); loadMatricesToShader(_transformStack,_globalTx,_cam); prim->draw("sphere"); } // and before a pop _transformStack.popTransform(); glPolygonMode(GL_FRONT_AND_BACK,GL_FILL); }
void DeferredShading::deferredDirectionalLight( GLint &_shaderNumber, ngl::TransformStack &_tx, GLint &_lightIndex ) { glDrawBuffer(GL_COLOR_ATTACHMENT7); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, m_posTex); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, m_normTex); glActiveTexture(GL_TEXTURE2); glBindTexture(GL_TEXTURE_2D, m_colorTex); glActiveTexture(GL_TEXTURE3); glBindTexture(GL_TEXTURE_2D, m_tangentTex); glActiveTexture(GL_TEXTURE4); glBindTexture(GL_TEXTURE_2D, m_binormalTex); glActiveTexture(GL_TEXTURE5); glBindTexture(GL_TEXTURE_2D, m_normalMapTex); glActiveTexture(GL_TEXTURE6); glBindTexture(GL_TEXTURE_2D, m_specularTex); glDisable(GL_DEPTH_TEST); glEnable(GL_BLEND); glBlendEquation(GL_FUNC_ADD); glBlendFunc(GL_ONE, GL_ONE); if((_shaderNumber <= m_shaderNumber) && (_shaderNumber >= 0)) { // Set our shader as active ngl::ShaderLib *shader=ngl::ShaderLib::instance(); m_deferredShader[_shaderNumber]->setShaderAsActive(); // Making the light stay fixed relative to the world. // This is the only way to make it "stick". Multiplying by the transpose matrix doesn't work for unknown reasons... ngl::Vec4 m_currPos = m_deferredShader[_shaderNumber]->m_light[_lightIndex]->getPos(); ngl::Mat4 globalMV = _tx.getGlobalTransform().getMatrix() * m_camera->getViewMatrix(); // For rotation m_currPos = m_currPos * globalMV; // For position m_currPos.m_x = m_currPos.m_x + globalMV.m_30; m_currPos.m_y = m_currPos.m_y + globalMV.m_31; m_currPos.m_z = m_currPos.m_z + globalMV.m_32; ngl::Vec4 result = ngl::Vec4(m_currPos.m_x, m_currPos.m_y, m_currPos.m_z, m_currPos.m_w); // load these values to the shader as well m_deferredShader[_shaderNumber]->m_light[_lightIndex]->loadToShader("light"); shader->setShaderParam4f("light.position",result.m_x, result.m_y, result.m_z, result.m_w); shader->setShaderParam2f("gScreenSize", m_renderWidth, m_renderHeight); _tx.pushTransform(); { // transformation matrices ngl::Mat4 MVP; MVP.identity(); shader->setShaderParamFromMat4("MVP",MVP); // Render the quad glBindVertexArray(m_quad); glDrawArrays(GL_TRIANGLES, 0, 6); glDisable(GL_BLEND); } _tx.popTransform(); } else { std::cout<<"The shader with number "<< _shaderNumber<<" doesn't exist. Cannot render with an unknown shader! \n"; } }
//---------------------------------------------------------------------------------------------------------------------- // The SSAO pass. // It uses the position and normal texture attachements. // The result is blended in the texture 7 (COLOR_ATTACHEMENT7) which holds the final image // The blending is done using the alpha channel since the ssao is outputed in the alpha channel in the shader. // void DeferredShading::generateSSAOPass(GLint &_shaderNumber, ngl::TransformStack &_tx ) { // We attach our SSAO texture. glDrawBuffer(GL_COLOR_ATTACHMENT7); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, m_posTex); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, m_normTex); // The sample textures used in SSAO glActiveTexture(GL_TEXTURE19); // The sample texture loaded in the begining. glBindTexture(GL_TEXTURE_2D,11); glDisable(GL_DEPTH_TEST); glEnable(GL_BLEND); glBlendEquation(GL_FUNC_ADD); // Blending the final image with the SSAO texture. In the shader we store the SSAO values as alpha. glBlendFunc(GL_ONE, GL_SRC_ALPHA); //glBlendFunc(GL_ONE, GL_ONE); if((_shaderNumber <= m_shaderNumber) && (_shaderNumber >= 0)) { // Set our shader as active ngl::ShaderLib *shader=ngl::ShaderLib::instance(); m_deferredShader[_shaderNumber]->setShaderAsActive(); // Set the textures to be used. shader->setShaderParam1i("PosTex", 0); shader->setShaderParam1i("NormalTex", 2); shader->setShaderParam1i("SampleTex", 19); // Projection matrix ngl::Mat3 ProjectionMatrix = m_camera->getProjectionMatrix(); shader->setShaderParamFromMat3("Projection",ProjectionMatrix); // Set the screensize in order to use the correct TexCoords shader->setShaderParam2f("gScreenSize", m_renderWidth, m_renderHeight); _tx.pushTransform(); { // transformation matrices ngl::Mat4 MVP; MVP.identity(); shader->setShaderParamFromMat4("MVP",MVP); // Render the quad with the SSAO glBindVertexArray(m_quad); glDrawArrays(GL_TRIANGLES, 0, 6); glDisable(GL_BLEND); } _tx.popTransform(); } else { std::cout<<"The shader with number "<< _shaderNumber<<" doesn't exist. Cannot render with an unknown shader! \n"; } }