From b1b60bce6186fd4b647cc5373d87e9b413fcdc75 Mon Sep 17 00:00:00 2001 From: Tleety Date: Wed, 7 May 2014 14:23:01 +0000 Subject: [PATCH] WIP --- src/Components/PointLight.h | 6 +- src/GameWorld.cpp | 15 ++++- src/Renderer.cpp | 112 +++++++++++++++++++++++-------- src/Renderer.h | 28 +++++--- src/Shaders/Fragment2-Debug.glsl | 2 +- src/Shaders/Fragment2.glsl | 77 ++++++++++++++++++++- src/Shaders/Vertex2.glsl | 4 +- src/Systems/RenderSystem.cpp | 8 +-- 8 files changed, 203 insertions(+), 49 deletions(-) diff --git a/src/Components/PointLight.h b/src/Components/PointLight.h index a7a5d4a..29da989 100755 --- a/src/Components/PointLight.h +++ b/src/Components/PointLight.h @@ -11,11 +11,13 @@ struct PointLight : Component { float Intensity; float MaxRange; - glm::vec3 Specular; - glm::vec3 Diffuse; float constantAttenuation, linearAttenuation, quadraticAttenuation; float spotExponent; Color color; + + glm::vec3 Specular; + glm::vec3 Diffuse; + float specularExponent; }; } diff --git a/src/GameWorld.cpp b/src/GameWorld.cpp index 7926dcb..bb01e03 100755 --- a/src/GameWorld.cpp +++ b/src/GameWorld.cpp @@ -58,6 +58,7 @@ void GameWorld::Initialize() transform->Orientation = glm::quat(glm::vec3(-glm::pi() / 8.f, 0.f, 0.f)); auto cameraComp = AddComponent(camera, "Camera"); cameraComp->FarClip = 2000.f; + cameraComp->FOV = glm::radians(90.f); AddComponent(camera, "Input"); auto freeSteering = AddComponent(camera, "FreeSteering"); CommitEntity(camera); @@ -149,7 +150,6 @@ void GameWorld::Initialize() } /* - { // Front Right Wheel auto ent = CreateEntity(car); @@ -224,7 +224,18 @@ void GameWorld::Initialize() CommitEntity(car); } */ - + for(int i = 0; i < 5; i++) + { + auto Light = CreateEntity(); + auto transform = AddComponent(Light, "Transform"); + transform->Position = glm::vec3(20+ 10*cos(i), 3, 0 + 10*sin(i)); + auto light = AddComponent(Light, "PointLight"); + light->Diffuse = glm::vec3(94.f/255.f, 227.f/255.f, 230.f/255.f); + light->Specular = glm::vec3(94.f/255.f, 227.f/255.f, 230.f/255.f); + light->specularExponent = 1.0f; + auto model = AddComponent(Light, "Model"); + model->ModelFile = "Models/Placeholders/PhysicsTest/PointLight.obj"; + } for(int i = 0; i < 10; i++) diff --git a/src/Renderer.cpp b/src/Renderer.cpp index 1125dc0..bf6241b 100755 --- a/src/Renderer.cpp +++ b/src/Renderer.cpp @@ -155,9 +155,6 @@ void Renderer::Draw(double dt) #pragma region TempRegion - - - void Renderer::DrawSkybox() { glBindFramebuffer(GL_FRAMEBUFFER, 0); @@ -199,9 +196,9 @@ void Renderer::DrawScene() m_ShaderProgram.Bind(); glUniform1i(glGetUniformLocation(m_ShaderProgram.GetHandle(), "numberOfLights"), Lights); - glUniform3fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "position"), Lights, Light_position.data()); - glUniform3fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "specular"), Lights, Light_specular.data()); - glUniform3fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "diffuse"), Lights, Light_diffuse.data()); +// glUniform3fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "position"), Lights, Light_position.data()); +// glUniform3fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "specular"), Lights, Light_specular.data()); +// glUniform3fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "diffuse"), Lights, Light_diffuse.data()); glUniform1fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "constantAttenuation"), Lights, Light_constantAttenuation.data()); glUniform1fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "linearAttenuation"), Lights, Light_linearAttenuation.data()); glUniform1fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "quadraticAttenuation"), Lights, Light_quadraticAttenuation.data()); @@ -359,26 +356,30 @@ void Renderer::AddPointLightToDraw( glm::vec3 _position, glm::vec3 _specular, glm::vec3 _diffuse, - float _constantAttenuation, - float _linearAttenuation, - float _quadraticAttenuation, - float _spotExponent + float _specularExponent ) { - Light_position.push_back(_position.x); - Light_position.push_back(_position.y); - Light_position.push_back(_position.z); - Light_specular.push_back(_specular.x); - Light_specular.push_back(_specular.y); - Light_specular.push_back(_specular.z); - Light_diffuse.push_back(_diffuse.x); - Light_diffuse.push_back(_diffuse.y); - Light_diffuse.push_back(_diffuse.z); - Light_constantAttenuation.push_back(_constantAttenuation); - Light_linearAttenuation.push_back(_linearAttenuation); - Light_quadraticAttenuation.push_back(_quadraticAttenuation); - Light_spotExponent.push_back(_spotExponent); - Lights = Light_constantAttenuation.size(); + Light_position.push_back(_position); + Light_specular.push_back(_specular); + Light_diffuse.push_back(_diffuse); + Light_specularExponent.push_back(_specularExponent); + Lights = Light_position.size(); + CreateLightMatrix(); + +// Light_position.push_back(_position.x); +// Light_position.push_back(_position.y); +// Light_position.push_back(_position.z); +// Light_specular.push_back(_specular.x); +// Light_specular.push_back(_specular.y); +// Light_specular.push_back(_specular.z); +// Light_diffuse.push_back(_diffuse.x); +// Light_diffuse.push_back(_diffuse.y); +// Light_diffuse.push_back(_diffuse.z); +// Light_constantAttenuation.push_back(_constantAttenuation); +// Light_linearAttenuation.push_back(_linearAttenuation); +// Light_quadraticAttenuation.push_back(_quadraticAttenuation); +// Light_spotExponent.push_back(_spotExponent); +// Lights = Light_constantAttenuation.size(); } void Renderer::AddAABBToDraw(glm::vec3 origin, glm::vec3 volumeVector, bool colliding) @@ -523,6 +524,7 @@ void Renderer::ClearStuff() Light_linearAttenuation.clear(); Light_quadraticAttenuation.clear(); Light_spotExponent.clear(); + Light_specularExponent.clear(); Lights = 0; } @@ -657,13 +659,14 @@ void Renderer::DrawFBO() // Clear G-buffer GLenum windowBuffClear[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2 }; glDrawBuffers(3, windowBuffClear); - glClearColor(0.0f, 0.0f, 0.0f, 0.0f); + glClearColor(0.2f, 0.2f, 0.2f, 0.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Execute the first render stage which will fill out the internal buffers with data(??) m_FirstPassProgram.Bind(); GLenum windowBuffOpaque[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2 }; glDrawBuffers(3, windowBuffOpaque); + DrawFBOScene(); // Draw to screen @@ -678,7 +681,7 @@ void Renderer::DrawFBO() } glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - ////SetupDefferedStageUniforms(); // Probably what we do in FrameBufferTextures(); + glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, m_fDiffuseTexture); @@ -688,9 +691,11 @@ void Renderer::DrawFBO() glActiveTexture(GL_TEXTURE2); glBindTexture(GL_TEXTURE_2D, m_fNormalsTexture); + DrawLightScene(); + glBindVertexArray(m_ScreenQuad); glEnableVertexAttribArray(0); -/* glEnableVertexAttribArray(2);*/ + glEnableVertexAttribArray(2); glDrawArrays(GL_TRIANGLES, 0, 6); } @@ -721,7 +726,6 @@ void Renderer::DrawFBO() // glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // glDisable(GL_BLEND); // -// // glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); // //Probably means to use the second_pass shader // //EnableRenderProgramDeferredStage(); @@ -781,3 +785,55 @@ void Renderer::DrawFBOScene() } } + + +void Renderer::DrawLightScene() +{ + glEnable(GL_BLEND); + glBlendEquation (GL_FUNC_ADD); + glBlendFunc(GL_ONE,GL_ONE); + + glDisable (GL_DEPTH_TEST); + glDepthMask (GL_FALSE); + glBindVertexArray(m_sphereModel->VAO); + + glm::mat4 cameraMatrix = m_Camera->ProjectionMatrix() * m_Camera->ViewMatrix(); + glm::mat4 MVP; + + for(int i = 0; i < Lights; i++) + { + glm::mat4 MVP = m_Camera->ProjectionMatrix() * m_Camera->ViewMatrix() * lM[i]; + + glUniformMatrix4fv(glGetUniformLocation(m_FirstPassProgram.GetHandle(), "MVP"), 1, GL_FALSE, glm::value_ptr(MVP)); + glUniformMatrix4fv(glGetUniformLocation(m_FirstPassProgram.GetHandle(), "V"), 1, GL_FALSE, glm::value_ptr(m_Camera->ViewMatrix())); + glUniformMatrix4fv(glGetUniformLocation(m_FirstPassProgram.GetHandle(), "P"), 1, GL_FALSE, glm::value_ptr(m_Camera->ProjectionMatrix())); + glUniformMatrix4fv(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "M"), 1, GL_FALSE, glm::value_ptr(lM[i])); + glUniform3f(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "ls"), Light_specular[i].x, Light_specular[i].y, Light_specular[i].z); + glUniform3f(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "ld"), Light_diffuse[i].x, Light_diffuse[i].y, Light_diffuse[i].z); + glUniform3f(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "lp"), Light_position[i].x, Light_position[i].y, Light_position[i].z); + glUniform3f(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "CameraPosition"), m_Camera->Position().x, m_Camera->Position().y, m_Camera->Position().z); + //glUniform1f(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "speculatExponent"), Light_specularExponent[i]); + glDrawArrays(GL_TRIANGLES, 0, m_sphereModel->Vertices.size()); + }; + glEnable (GL_DEPTH_TEST); + glDepthMask (GL_TRUE); + glDisable (GL_BLEND); +} + +void Renderer::SetSphereModel( Model* _model ) +{ + m_sphereModel = _model; +} + +void Renderer::CreateLightMatrix() +{ + for(int i = 0; i < Lights; i++) + { + const float radius = 5.0f; + lM[i] = glm::scale(glm::mat4(1.0), glm::vec3(radius, radius, radius)); + lM[i] = glm::translate(lM[i], Light_position[i]); + lM[i] = m_Camera->ProjectionMatrix() * m_Camera->ViewMatrix() * lM[i]; + } + +} + diff --git a/src/Renderer.h b/src/Renderer.h index 540be71..1c73d49 100755 --- a/src/Renderer.h +++ b/src/Renderer.h @@ -12,6 +12,7 @@ #include "Model.h" #include "Components/PointLight.h" #include "Skybox.h" +#include "ResourceManager.h" class Renderer { @@ -24,13 +25,17 @@ public: std::list> ModelsToRender; int Lights; - std::vector Light_position; - std::vector Light_specular; - std::vector Light_diffuse; - std::vector Light_constantAttenuation; - std::vector Light_linearAttenuation; - std::vector Light_quadraticAttenuation; + std::vector Light_position; + std::vector Light_specular; + std::vector Light_diffuse; + std::vector Light_specularExponent; + + + std::vector Light_constantAttenuation; + std::vector Light_linearAttenuation; + std::vector Light_quadraticAttenuation; std::vector Light_spotExponent; + std::list> AABBsToRender; Renderer(); @@ -45,10 +50,7 @@ public: glm::vec3 _position, glm::vec3 _specular, glm::vec3 _diffuse, - float _constantAttenuation, - float _linearAttenuation, - float _quadraticAttenuation, - float _spotExponent + float _specularExponent ); void AddAABBToDraw(glm::vec3 origin, glm::vec3 volumeVector, bool colliding); @@ -65,6 +67,8 @@ public: void DrawBounds(bool val) { m_DrawBounds = val; } void DrawSkybox(); + void SetSphereModel(Model* _model); + private: @@ -94,7 +98,9 @@ private: GLuint m_fb; GLuint m_fDepthBuffer; GLenum draw_bufs[2]; + glm::mat4 lM[5]; GLuint m_ScreenQuad; + Model* m_sphereModel; bool m_QuadView; @@ -118,7 +124,9 @@ private: void FrameBufferTextures(); void DrawFBO(); void DrawFBOScene(); + void DrawLightScene(); void BindFragDataLocation(); + void CreateLightMatrix(); GLuint CreateQuad(); void DrawDebugShadowMap(); diff --git a/src/Shaders/Fragment2-Debug.glsl b/src/Shaders/Fragment2-Debug.glsl index db35d6e..0ab00d4 100644 --- a/src/Shaders/Fragment2-Debug.glsl +++ b/src/Shaders/Fragment2-Debug.glsl @@ -30,7 +30,7 @@ void main() //FragColor = texture2D(DiffuseTexture, Input.TextureCoord * 2 + vec2(0, -1)); DrawQuadrant(texture2D(DiffuseTexture, Input.TextureCoord * 2), vec2(-1, 1)); DrawQuadrant(texture2D(PositionTexture, Input.TextureCoord * 2), vec2(1, 1)); - DrawQuadrant(texture2D(NormalTexture, Input.TextureCoord * 2), vec2(-1, -1)); + DrawQuadrant((texture2D(NormalTexture, Input.TextureCoord * 2)+1)/2, vec2(-1, -1)); vec4 AllTexel = texture2D(DiffuseTexture, Input.TextureCoord*2)*texture2D(PositionTexture, Input.TextureCoord*2)*texture2D(NormalTexture, Input.TextureCoord*2); DrawQuadrant(AllTexel, vec2(1, -1)); diff --git a/src/Shaders/Fragment2.glsl b/src/Shaders/Fragment2.glsl index cbc905e..aff7665 100644 --- a/src/Shaders/Fragment2.glsl +++ b/src/Shaders/Fragment2.glsl @@ -4,6 +4,20 @@ layout (binding=0) uniform sampler2D DiffuseTexture; layout (binding=1) uniform sampler2D PositionTexture; layout (binding=2) uniform sampler2D NormalTexture; +uniform mat4 MVP; +uniform mat4 M; +uniform mat4 V; +uniform mat4 P; +uniform vec3 ls; +uniform vec3 ld; +uniform vec3 lp; +const float specularExponent = 20.0; +uniform vec3 CameraPosition; + +const vec3 kd = vec3(1.0, 1.0, 1.0); +const vec3 ks = vec3(1.0, 1.0, 1.0); +const float kshine = 1.0; + in VertexData { vec3 Position; @@ -13,7 +27,68 @@ in VertexData out vec4 FragColor; +vec3 phong (vec3 PositionTexel, vec3 NormalTexel) +{ + vec3 lightPosition = vec3( M * vec4( lp, 1.0 ) ); + vec3 distToLight = lightPosition - PositionTexel; + vec3 directionToLight = normalize(distToLight); + + //Diffuse light + float dotProdDiffuse = max(dot(directionToLight, NormalTexel), 0.0); + vec3 Id = ld * kd * dotProdDiffuse; //Final diffuse intensity + + //Specular light + vec3 reflection = reflect(-directionToLight, NormalTexel); + vec3 surfaceToCamera = normalize(PositionTexel); + vec3 HalfWay = normalize(surfaceToCamera + directionToLight); + float dotProdSpecular = dot(HalfWay, NormalTexel); + dotProdSpecular = max(dotProdSpecular, 0.0); + float specularFactor = pow(dotProdSpecular, specularExponent); + vec3 Is = ls * ks * specularFactor; //Final specular intensity + + //Attenuation + float dist2D = distance(lightPosition, PositionTexel); + float attenuationFactor = -log(min(1.0, dist2D / 5.0)); + + //vec3 FinalOut = (Id + Is) * attenuationFactor; + vec3 FinalOut = Id + Is; + return FinalOut; +} + + +vec3 phong2 (vec3 PositionTexel, vec3 NormalTexel) +{ + vec3 LightVector = lp - PositionTexel; + vec3 ViewVector = normalize(PositionTexel); + + //diffuse + vec3 Id = max(0.0, dot(LightVector, NormalTexel)) * ld; + + vec3 FinalOut = Id; + return FinalOut; +} + +vec3 phong3 (vec3 PositionTexel, vec3 NormalTexel) +{ + vec3 lightDir = lp - PositionTexel; + lightDir = normalize(lightDir); + + vec3 eyeDir = normalize(CameraPosition-PositionTexel); + vec3 vHalfVector = normalize(lightDir.xyz+eyeDir); + vec3 Id = max(0.0, dot(NormalTexel, lightDir)) * ld; + vec3 Is = pow(max(0.0, dot(NormalTexel, vHalfVector)), 100.0) * ls; + vec3 FinalFrag = Id + Is; + return FinalFrag; +} + void main() { - FragColor = texture2D(DiffuseTexture, Input.TextureCoord); + vec4 DiffuseTexel = texture2D(DiffuseTexture, Input.TextureCoord); + vec4 PositionTexel = texture2D(PositionTexture, Input.TextureCoord); + vec4 NormalTexel = texture2D(NormalTexture, Input.TextureCoord); + + vec4 Frag_color; + Frag_color.rgb = phong((MVP * PositionTexel).rgb, normalize(NormalTexel).rgb); + Frag_color.a = 1.0; + FragColor = Frag_color * DiffuseTexel; } \ No newline at end of file diff --git a/src/Shaders/Vertex2.glsl b/src/Shaders/Vertex2.glsl index e866f94..f341840 100644 --- a/src/Shaders/Vertex2.glsl +++ b/src/Shaders/Vertex2.glsl @@ -3,6 +3,8 @@ layout (location = 0) in vec3 Position; layout (location = 2) in vec2 TextureCoord; + + out VertexData { vec3 Position; @@ -11,7 +13,7 @@ out VertexData void main() { - gl_Position = vec4(Position, 1.0); + gl_Position = vec4(Position, 1.0); Output.Position = Position; Output.TextureCoord = TextureCoord; } \ No newline at end of file diff --git a/src/Systems/RenderSystem.cpp b/src/Systems/RenderSystem.cpp index fe254f4..2dc4116 100755 --- a/src/Systems/RenderSystem.cpp +++ b/src/Systems/RenderSystem.cpp @@ -38,10 +38,8 @@ void Systems::RenderSystem::UpdateEntity(double dt, EntityID entity, EntityID pa position, pointLightComponent->Specular, pointLightComponent->Diffuse, - pointLightComponent->constantAttenuation, - pointLightComponent->linearAttenuation, - pointLightComponent->quadraticAttenuation, - pointLightComponent->spotExponent); + pointLightComponent->specularExponent + ); } auto cameraComponent = m_World->GetComponent(entity, "Camera"); @@ -59,6 +57,8 @@ void Systems::RenderSystem::UpdateEntity(double dt, EntityID entity, EntityID pa void Systems::RenderSystem::Initialize() { m_TransformSystem = m_World->GetSystem("TransformSystem"); + + m_Renderer->SetSphereModel(m_World->GetResourceManager()->Load("Model", "Models/Placeholders/PhysicsTest/Sphere.obj")); } void Systems::RenderSystem::RegisterComponents(ComponentFactory* cf)