From dbad1ee7f5b154c835d0e1b0e64db27018300de0 Mon Sep 17 00:00:00 2001 From: Tleety Date: Wed, 21 May 2014 16:51:05 +0200 Subject: [PATCH] Work done on light and shadows. --- assets | 2 +- src/GameWorld.cpp | 26 ++++++++++++++------------ src/Model.cpp | 27 +++++++++++++++++++++------ src/Renderer.cpp | 16 ++++++++-------- src/Shaders/FinalPass.frag.glsl | 4 ++-- src/Shaders/Fragment.glsl | 2 +- src/Shaders/Fragment2.glsl | 23 ++--------------------- 7 files changed, 49 insertions(+), 51 deletions(-) diff --git a/assets b/assets index 836ffcb..21182ef 160000 --- a/assets +++ b/assets @@ -1 +1 @@ -Subproject commit 836ffcb9de17447cf429cef34a3a5d1cc5e72b4b +Subproject commit 21182efb938beeb1daca5d24f9957d394bfb8683 diff --git a/src/GameWorld.cpp b/src/GameWorld.cpp index cec494f..de85a0a 100755 --- a/src/GameWorld.cpp +++ b/src/GameWorld.cpp @@ -33,6 +33,19 @@ void GameWorld::Initialize() CommitEntity(ground); } + for(int i = 15; i < 200; i++) + { + auto Light = CreateEntity(); + auto transform = AddComponent(Light, "Transform"); + transform->Position = glm::vec3((5+(i/2.f))*cos(i/5.0f), 3.f, (5+(i/2.f))*sin(i/5.0f)); + auto light = AddComponent(Light, "PointLight"); + light->Specular = glm::vec3(0.5f, 0.5f, 0.5f); + light->Diffuse = glm::vec3(0.5f, 0.5f, 0.5f); + light->Radius = 15.f; + light->specularExponent = 100.f; + auto model = AddComponent(Light, "Model"); + model->ModelFile = "Models/Placeholders/PhysicsTest/PointLight.obj"; + } { auto jeep = CreateEntity(); @@ -49,18 +62,7 @@ void GameWorld::Initialize() AddComponent(jeep, "Input"); - for(int i = 0; i < 1; i++) - { - auto Light = CreateEntity(jeep); - auto transform = AddComponent(Light, "Transform"); - transform->Position = glm::vec3((5+(i/2.f))*cos(i/5.0f), 0.3f, 0 + (5+(i/5.f))*sin(i/2.0f)); - auto light = AddComponent(Light, "PointLight"); - light->Specular = glm::vec3(1.0f, 1.0f, 1.0f); - light->Diffuse = glm::vec3(1.0f, 1.0f, 1.0f); - light->Radius = 150.f; - auto model = AddComponent(Light, "Model"); - model->ModelFile = "Models/Placeholders/PhysicsTest/PointLight.obj"; - } + { auto camera = CreateEntity(); diff --git a/src/Model.cpp b/src/Model.cpp index 94031f7..1f1a5a7 100755 --- a/src/Model.cpp +++ b/src/Model.cpp @@ -48,6 +48,21 @@ Model::Model(OBJ &obj, ResourceManager* rm) currentTexGroup = &TextureGroups.back(); } + /*std::unordered_map similarNormals; + std::unordered_map normalCount; + for (auto &faceDef : face.Definitions) + { + if (faceDef.NormalIndex == 0) + continue; + + + similarNormals[faceDef.VertexIndex - 1] += normal; + normalCount[faceDef.VertexIndex - 1]++; + int index = pair.first; + glm::vec3 averagedNormal = ; + Normals[] + }*/ + // Face definitions for (auto faceDef : face.Definitions) { @@ -190,7 +205,7 @@ void Model::CreateBuffers( std::vector vertices, std::vector(10.f, -10.f, 10.f, -10.f, 10.f, -10.f); + m_SunProjection = glm::ortho(200.f, -200.f, 200.f, -200.f, -100.f, 100.f); /* Lights = 0;*/ } @@ -529,7 +529,7 @@ void Renderer::FrameBufferTextures() //Generate and bind diffuse texture glGenTextures(1, &m_fDiffuseTexture); glBindTexture(GL_TEXTURE_2D, m_fDiffuseTexture); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, WIDTH, HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, WIDTH, HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); @@ -547,7 +547,7 @@ void Renderer::FrameBufferTextures() //Generate and bind normal texture glGenTextures(1, &m_fNormalsTexture); glBindTexture(GL_TEXTURE_2D, m_fNormalsTexture); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, WIDTH, HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB16F, WIDTH, HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); @@ -593,7 +593,7 @@ void Renderer::FrameBufferTextures() glGenTextures(1, &m_fLightingTexture); glBindTexture(GL_TEXTURE_2D, m_fLightingTexture); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, WIDTH, HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, WIDTH, HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); @@ -625,7 +625,7 @@ void Renderer::DrawFBO() // Clear G-buffer GLenum windowBuffClear[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_COLOR_ATTACHMENT3 }; glDrawBuffers(4, windowBuffClear); - glClearColor(0.5f, 0.f, 0.f, 0.f); + glClearColor(0.0f, 0.3f, 0.7f, 0.f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Execute the first render stage which will fill out the internal buffers with data(??) @@ -668,7 +668,7 @@ void Renderer::DrawFBO() m_FinalPassProgram.Bind(); // Ambient light - glUniform3fv(glGetUniformLocation(m_FinalPassProgram.GetHandle(), "La"), 1, glm::value_ptr(glm::vec3(0.4f, 0.4f, 0.4f))); + glUniform3fv(glGetUniformLocation(m_FinalPassProgram.GetHandle(), "La"), 1, glm::value_ptr(glm::vec3(0.7f))); glUniform1f(glGetUniformLocation(m_FinalPassProgram.GetHandle(), "Gamma"), Gamma); glActiveTexture(GL_TEXTURE0); diff --git a/src/Shaders/FinalPass.frag.glsl b/src/Shaders/FinalPass.frag.glsl index 4d85ea6..df251d4 100644 --- a/src/Shaders/FinalPass.frag.glsl +++ b/src/Shaders/FinalPass.frag.glsl @@ -21,9 +21,9 @@ void main() vec4 LightingTexel = texture(LightingTexture, Input.TextureCoord); vec4 ShadowTexel = texture(ShadowTexture, Input.TextureCoord); - //FragmentColor = DiffuseTexel; + //FragmentColor = LightingTexel; - FragmentColor = DiffuseTexel * (LightingTexel + vec4(La, 0.0)); + FragmentColor = DiffuseTexel * (vec4(La, 0.0) + vec4(LightingTexel.rgb, 0.0)) + vec4(LightingTexel.a, LightingTexel.a, LightingTexel.a, 0.0); //FragmentColor = vec4(pow(_FragmentColor.rgb, vec3(1.0 / Gamma)), _FragmentColor.a); diff --git a/src/Shaders/Fragment.glsl b/src/Shaders/Fragment.glsl index f645dcf..3c9d8a7 100755 --- a/src/Shaders/Fragment.glsl +++ b/src/Shaders/Fragment.glsl @@ -28,7 +28,7 @@ float Shadow(vec4 ShadowCoord) bias = clamp(bias, 0.0, 0.01); if( texture(ShadowTexture, Input.ShadowCoord.xy).z < ShadowCoord.z - bias) { - return 0.3; + return 0.6; } else { diff --git a/src/Shaders/Fragment2.glsl b/src/Shaders/Fragment2.glsl index c7e3178..265cf2a 100644 --- a/src/Shaders/Fragment2.glsl +++ b/src/Shaders/Fragment2.glsl @@ -54,29 +54,10 @@ vec4 phong(vec3 position, vec3 normal, vec3 specular) //Attenuation float dist = distance(lightPos, position); - //float attenuation = -log(min(1.0, dist / LightRadius)); - - //float attenuation = 1.0 / (ConstantAttenuation + (LinearAttenuation * dist) + (QuadraticAttenuation * dist * dist)); float attenuation = pow(max(0.0f, 1.0 - (dist / LightRadius)), 2); - //float attenuation = 1.0 / (1.0 - 0.0001 * pow(dist, 2)); - - //float attenuation = clamp(0.0, 1.0, 1.0 / (0.001 + (0.001 * dist) + (0.001 * dist * dist))); - - //float attenuation = 1.0 / dot(directionToLight, directionToLight); - - //float att_s = 5; - //float attenuation = pow(dist, 2) / pow(5.0, 2); - //attenuation = 1.0 / (1.0 + attenuation * att_s); - //att_s = 1.0 / (1.0 + att_s); - //attenuation = attenuation / (1.0 - att_s); - - //float radius = 5.0; - //float alpha = dist / radius; - //float dampingFactor = 1.0 - pow(alpha, 3); - - return vec4((Id + Is) * attenuation, 1.0); + return vec4((Id) * attenuation, Is.r * attenuation); } void main() @@ -86,6 +67,6 @@ void main() vec4 NormalTexel = texture(NormalsTexture, TextureCoord); vec4 SpecularTexel = texture(SpecularTexture, TextureCoord); - FragColor = phong(vec3(PositionTexel), vec3(NormalTexel), vec3(SpecularTexel))+vec4(0.1f); + FragColor = phong(vec3(PositionTexel), vec3(NormalTexel), vec3(SpecularTexel)); //FragColor = NormalTexel; } \ No newline at end of file