From e414087816edb6990eaeaf5bb62ce0b7be1b6ee5 Mon Sep 17 00:00:00 2001 From: Tleety Date: Tue, 20 May 2014 03:33:42 +0200 Subject: [PATCH] Some work done on lights and normals and other buggs/fixes --- assets | 2 +- src/Components/PointLight.h | 2 ++ src/GameWorld.cpp | 17 +++++++++-------- src/Model.cpp | 12 ++++++++++++ src/Renderer.cpp | 18 +++++++++--------- src/Renderer.h | 5 +++-- src/Shaders/FinalPass.frag.glsl | 4 ++-- src/Shaders/Fragment.glsl | 3 ++- src/Shaders/Fragment2.glsl | 7 ++++--- src/Systems/RenderSystem.cpp | 3 ++- 10 files changed, 46 insertions(+), 27 deletions(-) diff --git a/assets b/assets index cdee701..836ffcb 160000 --- a/assets +++ b/assets @@ -1 +1 @@ -Subproject commit cdee7015793193f0e78a2b75b25e3a2c07881632 +Subproject commit 836ffcb9de17447cf429cef34a3a5d1cc5e72b4b diff --git a/src/Components/PointLight.h b/src/Components/PointLight.h index b08be67..4862040 100755 --- a/src/Components/PointLight.h +++ b/src/Components/PointLight.h @@ -16,9 +16,11 @@ struct PointLight : Component , ConstantAttenuation(1.0f) , LinearAttenuation(0.f) , QuadraticAttenuation(3.f) + , Radius(5.f) { } float ConstantAttenuation, LinearAttenuation, QuadraticAttenuation; + float Radius; Color color; glm::vec3 Specular; diff --git a/src/GameWorld.cpp b/src/GameWorld.cpp index 46ca94d..cec494f 100755 --- a/src/GameWorld.cpp +++ b/src/GameWorld.cpp @@ -57,6 +57,7 @@ void GameWorld::Initialize() 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"; } @@ -89,8 +90,8 @@ void GameWorld::Initialize() auto transform = AddComponent(wheel, "Transform"); transform->Position = glm::vec3(1.4f, 0.5546f - 0.6577f - 0.2, -0.9242f); transform->Scale = glm::vec3(1.0f); - auto model = AddComponent(wheel, "Model"); - model->ModelFile = "Models/Jeep/WheelFront/wheelFront.obj"; + //auto model = AddComponent(wheel, "Model"); + //model->ModelFile = "Models/Jeep/WheelFront/wheelFront.obj"; auto Wheel = AddComponent(wheel, "Wheel"); Wheel->Hardpoint = transform->Position + glm::vec3(0.f, 1.f, 0.f); Wheel->AxleID = 0; @@ -109,8 +110,8 @@ void GameWorld::Initialize() transform->Position = glm::vec3(-1.4f, 0.5546f - 0.6577f - 0.2, -0.9242f); transform->Scale = glm::vec3(1.0f); transform->Orientation = glm::angleAxis(glm::pi(), glm::vec3(0, 0, 1)); - auto model = AddComponent(wheel, "Model"); - model->ModelFile = "Models/Jeep/WheelFront/wheelFront.obj"; + //auto model = AddComponent(wheel, "Model"); + //model->ModelFile = "Models/Jeep/WheelFront/wheelFront.obj"; auto Wheel = AddComponent(wheel, "Wheel"); Wheel->Hardpoint = transform->Position + glm::vec3(0.f, 1.f, 0.f); Wheel->AxleID = 0; @@ -127,8 +128,8 @@ void GameWorld::Initialize() auto wheel = CreateEntity(jeep); auto transform = AddComponent(wheel, "Transform"); transform->Position = glm::vec3(0.2726f, 0.2805f - 0.6577f, 1.9307f); - auto model = AddComponent(wheel, "Model"); - model->ModelFile = "Models/Jeep/WheelBack/wheelBack.obj"; + //auto model = AddComponent(wheel, "Model"); + //model->ModelFile = "Models/Jeep/WheelBack/wheelBack.obj"; auto Wheel = AddComponent(wheel, "Wheel"); Wheel->Hardpoint = transform->Position + glm::vec3(0.f, 1.f, 0.f); Wheel->AxleID = 1; @@ -145,8 +146,8 @@ void GameWorld::Initialize() auto transform = AddComponent(wheel, "Transform"); transform->Position = glm::vec3(-0.2726f, 0.2805f - 0.6577f, 1.9307f); transform->Orientation = glm::angleAxis(glm::pi(), glm::vec3(0, 0, 1)); - auto model = AddComponent(wheel, "Model"); - model->ModelFile = "Models/Jeep/WheelBack/wheelBack.obj"; + //auto model = AddComponent(wheel, "Model"); + //model->ModelFile = "Models/Jeep/WheelBack/wheelBack.obj"; auto Wheel = AddComponent(wheel, "Wheel"); Wheel->Hardpoint = transform->Position + glm::vec3(0.f, 1.f, 0.f); Wheel->AxleID = 1; diff --git a/src/Model.cpp b/src/Model.cpp index ae99194..94031f7 100755 --- a/src/Model.cpp +++ b/src/Model.cpp @@ -23,11 +23,23 @@ Model::Model(OBJ &obj, ResourceManager* rm) // TODO: Load normal map std::shared_ptr normalMap = nullptr; if (!currentMaterial->NormalMap.FileName.empty()) + { normalMap = std::shared_ptr(rm->Load("Texture", currentMaterial->NormalMap.FileName)); + } + else + { + normalMap = std::shared_ptr(rm->Load("Texture", "Textures/NeutralNormalMap.png")); + } // Load specular map std::shared_ptr specularMap = nullptr; if (!currentMaterial->SpecularMap.FileName.empty()) + { specularMap = std::shared_ptr(rm->Load("Texture", currentMaterial->SpecularMap.FileName)); + } + else + { + specularMap = std::shared_ptr(rm->Load("Texture", "Textures/NeutralSpecularMap.png")); + } // TODO: Load material parameters // Create new texture group (start index of new group is upcoming index) diff --git a/src/Renderer.cpp b/src/Renderer.cpp index cd212e3..fdf2605 100755 --- a/src/Renderer.cpp +++ b/src/Renderer.cpp @@ -358,7 +358,8 @@ void Renderer::AddPointLightToDraw( float _specularExponent, float _ConstantAttenuation, float _LinearAttenuation, - float _QuadraticAttenuation + float _QuadraticAttenuation, + float _radius ) { Light light; @@ -369,6 +370,7 @@ void Renderer::AddPointLightToDraw( light.ConstantAttenuation = _ConstantAttenuation; light.LinearAttenuation = _LinearAttenuation; light.QuadraticAttenuation = _QuadraticAttenuation; + light.Radius = _radius; light.SphereModelMatrix = CreateLightMatrix(light); Lights.push_back(light); } @@ -775,6 +777,7 @@ void Renderer::DrawLightScene() glUniform1f(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "ConstantAttenuation"), CAtt); glUniform1f(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "LinearAttenuation"), LAtt); glUniform1f(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "QuadraticAttenuation"), QAtt); + glUniform1f(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "LightRadius"), light.Radius); glDrawArrays(GL_TRIANGLES, 0, m_sphereModel->Vertices.size()); }; @@ -793,14 +796,14 @@ glm::mat4 Renderer::CreateLightMatrix(Light &_light) // float c = _light.ConstantAttenuation; // float l = _light.LinearAttenuation; // float q = _light.QuadraticAttenuation; - float c = CAtt; - float l = LAtt; - float q = QAtt; - float cutOffRadius = abs(sqrt((-4*c*q) + pow(l, 2) + (1024*q) - l) / (2*q)); + //float c = CAtt; + //float l = LAtt; + //float q = QAtt; + //float cutOffRadius = abs(sqrt((-4*c*q) + pow(l, 2) + (1024*q) - l) / (2*q)); glm::mat4 model; model *= glm::translate(_light.Position); - model *= glm::scale(glm::vec3(cutOffRadius)); + model *= glm::scale(glm::vec3(_light.Radius*2)); return model; } @@ -830,6 +833,3 @@ void Renderer::UpdateSunProjection() //Pass the bounding box's extents to glOrtho or similar to set up the orthographic projection matrix for the shadow map. } - - - diff --git a/src/Renderer.h b/src/Renderer.h index 29635b4..0c784ba 100755 --- a/src/Renderer.h +++ b/src/Renderer.h @@ -41,7 +41,8 @@ public: float _specularExponent, float _ConstantAttenuation, float _LinearAttenuation, - float _QuadraticAttenuation + float _QuadraticAttenuation, + float _radius ); void AddAABBToDraw(glm::vec3 origin, glm::vec3 volumeVector, bool colliding); @@ -71,7 +72,7 @@ private: glm::vec3 Diffuse; float SpecularExponent; glm::mat4 SphereModelMatrix; - float ConstantAttenuation, LinearAttenuation, QuadraticAttenuation; + float ConstantAttenuation, LinearAttenuation, QuadraticAttenuation, Radius; }; float Gamma; diff --git a/src/Shaders/FinalPass.frag.glsl b/src/Shaders/FinalPass.frag.glsl index 9d8e39b..4d85ea6 100644 --- a/src/Shaders/FinalPass.frag.glsl +++ b/src/Shaders/FinalPass.frag.glsl @@ -21,11 +21,11 @@ void main() vec4 LightingTexel = texture(LightingTexture, Input.TextureCoord); vec4 ShadowTexel = texture(ShadowTexture, Input.TextureCoord); + //FragmentColor = DiffuseTexel; FragmentColor = DiffuseTexel * (LightingTexel + vec4(La, 0.0)); //FragmentColor = vec4(pow(_FragmentColor.rgb, vec3(1.0 / Gamma)), _FragmentColor.a); - //FragmentColor = DiffuseTexel; - //FragmentColor = DiffuseTexel; + } \ No newline at end of file diff --git a/src/Shaders/Fragment.glsl b/src/Shaders/Fragment.glsl index 57652d5..f645dcf 100755 --- a/src/Shaders/Fragment.glsl +++ b/src/Shaders/Fragment.glsl @@ -46,8 +46,9 @@ void main() frag_Position = vec4(Input.Position.xyz, 1.0); // G-buffer Normal - mat3 TBN = transpose(mat3(Input.Tangent, Input.BiTangent, Input.Normal)); + mat3 TBN = mat3(Input.Tangent, Input.BiTangent, Input.Normal); frag_Normal = normalize(vec4(TBN * vec3(texture(NormalMapTexture, Input.TextureCoord)), 0.0)); + //frag_Diffuse = normalize(vec4(TBN * vec3(texture(NormalMapTexture, Input.TextureCoord)), 0.0)); //frag_Normal = vec4(Input.Normal, 0.0); //G-buffer Specular diff --git a/src/Shaders/Fragment2.glsl b/src/Shaders/Fragment2.glsl index ac99741..c7e3178 100644 --- a/src/Shaders/Fragment2.glsl +++ b/src/Shaders/Fragment2.glsl @@ -18,12 +18,13 @@ uniform vec3 CameraPosition; uniform float ConstantAttenuation; uniform float LinearAttenuation; uniform float QuadraticAttenuation; +uniform float LightRadius; const vec3 ks = vec3(1.0, 1.0, 1.0); const vec3 kd = vec3(1.0, 1.0, 1.0); const vec3 ka = vec3(1.0, 1.0, 1.0); const float kshine = 1.0; -const float LRadius = 5.0; + in VertexData { @@ -57,7 +58,7 @@ vec4 phong(vec3 position, vec3 normal, vec3 specular) //float attenuation = 1.0 / (ConstantAttenuation + (LinearAttenuation * dist) + (QuadraticAttenuation * dist * dist)); - float attenuation = pow(max(0.0f, 1.0 - (dist / LRadius)), 2); + float attenuation = pow(max(0.0f, 1.0 - (dist / LightRadius)), 2); //float attenuation = 1.0 / (1.0 - 0.0001 * pow(dist, 2)); @@ -85,6 +86,6 @@ void main() vec4 NormalTexel = texture(NormalsTexture, TextureCoord); vec4 SpecularTexel = texture(SpecularTexture, TextureCoord); - FragColor = phong(vec3(PositionTexel), vec3(NormalTexel), vec3(SpecularTexel)); + FragColor = phong(vec3(PositionTexel), vec3(NormalTexel), vec3(SpecularTexel))+vec4(0.1f); //FragColor = NormalTexel; } \ No newline at end of file diff --git a/src/Systems/RenderSystem.cpp b/src/Systems/RenderSystem.cpp index 5f7ba73..e684467 100755 --- a/src/Systems/RenderSystem.cpp +++ b/src/Systems/RenderSystem.cpp @@ -41,7 +41,8 @@ void Systems::RenderSystem::UpdateEntity(double dt, EntityID entity, EntityID pa pointLightComponent->specularExponent, pointLightComponent->ConstantAttenuation, pointLightComponent->LinearAttenuation, - pointLightComponent->QuadraticAttenuation + pointLightComponent->QuadraticAttenuation, + pointLightComponent->Radius ); }