diff --git a/shaders/lighting.frag.glsl b/shaders/lighting.frag.glsl index 54dfe17..ccd0f61 100644 --- a/shaders/lighting.frag.glsl +++ b/shaders/lighting.frag.glsl @@ -61,6 +61,11 @@ const float MAX_REFLECTION_LOD = 4.0; // FUNCTIONS #ifdef AMBIENT_LIGHT +vec3 fresnelSchlickRoughness(float cosTheta, vec3 F0, float roughness) +{ + return F0 + (max(vec3(1.0 - roughness), F0) - F0) * pow(1.0 - cosTheta, 5.0); +} + vec3 GGX( in vec3 albedoColor, in float metallic, @@ -71,20 +76,26 @@ vec3 GGX( float NdotV = max(dot(N, V), 0.0); vec3 R = reflect(-V, N); vec3 albedo = pow(albedoColor, vec3(2.2)); - vec3 F0 = mix(vec3(0.04), albedo, metallic); + // calculate reflectance at normal incidence; if dia-electric (like plastic) use F0 + // of 0.04 and if it's a metal, use their albedo color as F0 (metallic workflow) + vec3 F0 = vec3(0.04); + F0 = mix(F0, albedo, metallic); // ambient lighting (we now use IBL as the ambient term) (fresnelSchlickRoughness function) - vec3 F = F0 + (max(vec3(1.0 - roughness), F0) - F0) * pow(1.0 - NdotV, 5.0); + vec3 F = fresnelSchlickRoughness(NdotV, F0, roughness); - vec3 kD = 1.0 - F; + vec3 kS = F; + vec3 kD = 1.0 - kS; kD *= 1.0 - metallic; - vec3 irradiance = texture(ambientMap, inverseViewMatrix * N).rgb; - vec3 diffuse = irradiance * albedo; + mat3 viewToWorld = inverseViewMatrix; + + vec3 irradiance = texture(ambientMap, viewToWorld*N).rgb; + vec3 diffuse = irradiance * albedo; // sample both the pre-filter map and the BRDF lut and combine them together as per the Split-Sum approximation to get the IBL specular part. - vec3 prefilteredColor = textureLod(reflectMap, inverseViewMatrix * R, roughness * MAX_REFLECTION_LOD).rgb; + vec3 prefilteredColor = textureLod(reflectMap, viewToWorld * R, roughness * MAX_REFLECTION_LOD).rgb; vec2 brdf2 = texture(brdfLUT, vec2(NdotV, roughness)).rg; vec3 specular = prefilteredColor * (F * brdf2.x + brdf2.y); diff --git a/shaders/skybox.frag.glsl b/shaders/skybox.frag.glsl new file mode 100644 index 0000000..ec35fd2 --- /dev/null +++ b/shaders/skybox.frag.glsl @@ -0,0 +1,25 @@ +#version 330 core + +// - Position in view space +layout (location = 0) out vec4 outPosition; +// - Albedo + Roughness +layout (location = 1) out vec4 outAlbedo; +// - Normal buffer +layout (location = 2) out vec3 outNormal; +// - Emission + Metallic +layout (location = 3) out vec4 outEmission; + +in vec3 varTexCoord; + +out vec4 outColor; + +uniform samplerCube skybox; + +void main() +{ + outAlbedo.rgb = vec3(0., 0., 0.); // black + outAlbedo.a = 1.; // full roughness + outEmission.a = 0.; // not metallic + outNormal = vec3(0., 0., 1.); // oriented towards the eye + outEmission.rgb = texture(skybox, varTexCoord).rgb; +} diff --git a/shaders/skybox.vert.glsl b/shaders/skybox.vert.glsl new file mode 100644 index 0000000..575d693 --- /dev/null +++ b/shaders/skybox.vert.glsl @@ -0,0 +1,13 @@ +#version 330 core + +layout(location = 0)in vec3 inPosition; + +out vec3 varTexCoord; + +uniform mat4 MVP; + +void main() +{ + gl_Position = MVP * vec4(inPosition, 1.0); + varTexCoord = inPosition; +} diff --git a/src/deferredpipeline.cpp b/src/deferredpipeline.cpp index d22cdeb..89c66d0 100644 --- a/src/deferredpipeline.cpp +++ b/src/deferredpipeline.cpp @@ -121,6 +121,12 @@ void DeferredPipeline::renderGL(Scene *scene) glClearColor(0.0f, 0.0f, 0.0f, 1.f); glClearDepth(1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + // draw skybox + if(m_skybox != nullptr) + { + m_skybox->renderGL(m_camera); + glClear(GL_DEPTH_BUFFER_BIT); + } // loop on geometry for(SceneIterator* geometryIt = scene->getGeometry(); geometryIt->isValid(); geometryIt->next()) @@ -156,9 +162,6 @@ void DeferredPipeline::renderGL(Scene *scene) glEnable(GL_BLEND); glBlendFunc(GL_ONE, GL_ONE); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - if(m_skybox != nullptr) - m_skybox->renderGL(m_camera); - m_gBuffer->bindTextures(); for(SceneIterator* lightIt = scene->getLights(); lightIt->isValid(); lightIt->next()) diff --git a/src/skybox.cpp b/src/skybox.cpp index 0e0e991..6039b75 100644 --- a/src/skybox.cpp +++ b/src/skybox.cpp @@ -6,9 +6,12 @@ #include "texture.h" #include "camera.h" #include "opengl.h" +#include #define BUFFER_OFFSET(i) ((char *)NULL + (i)) +RESOURCE_PACK(shaders) + Skybox::Skybox(Texture* myCubeMap) { cubeMap = myCubeMap; @@ -24,6 +27,10 @@ Skybox::Skybox(Texture* myCubeMap) glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3*sizeof(GLfloat), BUFFER_OFFSET(0)); glEnableVertexAttribArray(0); + Resource::ResourceMap shaderMap; + Resource::getResourcePack_shaders(shaderMap); + std::string vertSource = shaderMap["shaders/skybox.vert.glsl"]; + std::string fragSource = shaderMap["shaders/skybox.frag.glsl"]; shader = new Shader(vertSource, fragSource); mvpLocation = shader->getLocation("MVP"); cubemapLocation = shader->getLocation("skybox"); @@ -39,10 +46,7 @@ Skybox::~Skybox() void Skybox::renderGL(Camera* myCamera) { - glViewport(0, 0, width, height); glm::mat4 viewMatrix = glm::mat4(glm::mat3(myCamera->getViewMatrix())); - glDisable(GL_CULL_FACE); - glDepthMask(GL_FALSE); shader->bind(); shader->bindMat4(mvpLocation, myCamera->getProjectionMatrix() * viewMatrix); @@ -53,9 +57,6 @@ void Skybox::renderGL(Camera* myCamera) glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbos[0]); glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_BYTE, NULL); glBindVertexArray(0); - - glEnable(GL_CULL_FACE); - glDepthMask(GL_TRUE); } const GLfloat Skybox::skyboxVertices[] = { @@ -83,24 +84,3 @@ const GLubyte Skybox::skyboxIndices[] = { 2, 6, 3, 6, 7, 3 }; - -const std::string Skybox::vertSource = - "#version 330 core\n\ - layout(location = 0)in vec3 inPosition;\n\ - out vec3 varTexCoord;\n\ - uniform mat4 MVP;\n\ - void main()\n\ - {\n\ - gl_Position = MVP * vec4(inPosition, 1.0);\n\ - varTexCoord = inPosition;\n\ - }\n"; - -const std::string Skybox::fragSource = - "#version 330 core\n\ - in vec3 varTexCoord;\n\ - out vec4 outColor;\n\ - uniform samplerCube skybox;\n\ - void main()\n\ - {\n\ - outColor = texture(skybox, varTexCoord);\n\ - }\n"; diff --git a/src/skybox.h b/src/skybox.h index 95c0968..c773f29 100644 --- a/src/skybox.h +++ b/src/skybox.h @@ -12,8 +12,6 @@ class Skybox { static const GLfloat skyboxVertices[]; static const GLubyte skyboxIndices[]; - static const std::string vertSource; - static const std::string fragSource; int width; int height;