From f9a74252496224d7d1b9256d61b97b08a62d5661 Mon Sep 17 00:00:00 2001 From: Tleety Date: Thu, 17 Apr 2014 00:54:01 +0200 Subject: [PATCH 01/22] Added Comments in Renderer.cpp --- src/Renderer.cpp | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/Renderer.cpp b/src/Renderer.cpp index e7dfed6..55cd40b 100755 --- a/src/Renderer.cpp +++ b/src/Renderer.cpp @@ -280,16 +280,19 @@ void Renderer::DrawScene() void Renderer::DrawShadowMap() { - glEnable(GL_DEPTH_TEST); - glEnable(GL_CULL_FACE); - glCullFace(GL_FRONT); + glEnable(GL_DEPTH_TEST);//Tests where objects are and display them correctly + glEnable(GL_CULL_FACE); //removes triangles on the wrong side of the object + glCullFace(GL_FRONT); //Make it so that only the back faces are rendered + //Binds the FBO and sets the veiwport, witch in effect is how large the shadowmap is and what resolution it has. glBindFramebuffer(GL_FRAMEBUFFER, m_ShadowFrameBuffer); glViewport(0, 0, m_ShadowMapRes, m_ShadowMapRes); glClear(GL_DEPTH_BUFFER_BIT); //glClearColor(0.0f, 0.0f, 0.0f, 0.0f); + + //Creates the "camera" for the shadowmap from the direction of the sun. glm::mat4 depthViewMatrix = glm::lookAt(m_SunPosition, m_SunTarget, glm::vec3(0, 1, 0)) * glm::translate(-m_Camera->Position() * glm::vec3(1, 0, 0)); // glm::mat4 depthViewMatrix = glm::lookAt(m_SunPosition, m_SunTarget, glm::vec3(0, 1, 0)); glm::mat4 depthCamera = m_SunProjection * depthViewMatrix; @@ -299,7 +302,9 @@ void Renderer::DrawShadowMap() glm::mat4 MVP; m_ShaderProgramShadows.Bind(); - glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); + glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); //Draws filled polygons + + //For each model, render them to the shadowmap for (auto tuple : ModelsToRender) { Model* model; From 6d75336496a71439e1136218794d1ba15e4b0d31 Mon Sep 17 00:00:00 2001 From: Tleety Date: Thu, 24 Apr 2014 00:11:16 +0200 Subject: [PATCH 02/22] Started on deferred rendering framework. --- src/Renderer.cpp | 192 ++++++++++++++---- src/Renderer.h | 10 + src/Shaders/First_pass.frag.glsl | 12 ++ src/Shaders/First_pass.vert.glsl | 16 ++ src/Shaders/Second_pass.frag.glsl | 24 +++ src/Shaders/Second_pass.vert.glsl | 9 + src/Shaders/geometry_pass.frag.glsl | 20 -- src/Shaders/geometry_pass.vert.glsl | 20 -- src/Util/defferedUtil.h | 28 +++ src/gBuffer.cpp | 40 ---- src/gBuffer.h | 35 ---- vs11/Returngeance/Returngeance.vcxproj | 18 ++ .../Returngeance/Returngeance.vcxproj.filters | 15 ++ 13 files changed, 289 insertions(+), 150 deletions(-) create mode 100644 src/Shaders/First_pass.frag.glsl create mode 100644 src/Shaders/First_pass.vert.glsl create mode 100644 src/Shaders/Second_pass.frag.glsl create mode 100644 src/Shaders/Second_pass.vert.glsl delete mode 100644 src/Shaders/geometry_pass.frag.glsl delete mode 100644 src/Shaders/geometry_pass.vert.glsl create mode 100644 src/Util/defferedUtil.h delete mode 100644 src/gBuffer.cpp delete mode 100644 src/gBuffer.h diff --git a/src/Renderer.cpp b/src/Renderer.cpp index 55cd40b..50c820b 100755 --- a/src/Renderer.cpp +++ b/src/Renderer.cpp @@ -72,6 +72,8 @@ void Renderer::Initialize() glEnable(GL_DEPTH_TEST); LoadContent(); + + FrameBufferTextures(); } void Renderer::LoadContent() @@ -110,6 +112,11 @@ void Renderer::LoadContent() m_ShaderProgramSkybox.Compile(); m_ShaderProgramSkybox.Link(); + m_FirstPassProgram.AddShader(std::shared_ptr(new VertexShader("Shaders/First_pass.vert.glsl"))); + m_FirstPassProgram.AddShader(std::shared_ptr(new FragmentShader("Shaders/First_pass.frag.glsl"))); + m_FirstPassProgram.Compile(); + m_FirstPassProgram.Link(); + m_Skybox = std::make_shared("Textures/Skybox/Sunset", "jpg"); m_DebugAABB = CreateAABB(); @@ -147,43 +154,17 @@ void Renderer::Draw(double dt) { glDisable(GL_BLEND); - DrawSkybox(); - DrawShadowMap(); - DrawScene(); - -#ifdef DEBUG - // Draw bounding boxes - if (m_DrawBounds) - { - glEnable(GL_BLEND); - glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ZERO); - m_ShaderProgramDebugAABB.Bind(); - for (auto tuple : AABBsToRender) - { - glm::mat4 modelMatrix; - bool colliding; - std::tie(modelMatrix, colliding) = tuple; - // Model matrix - glm::mat4 cameraMatrix = m_Camera->ProjectionMatrix() * m_Camera->ViewMatrix(); - glm::mat4 MVP = cameraMatrix * modelMatrix; - glUniformMatrix4fv(glGetUniformLocation(m_ShaderProgramDebugAABB.GetHandle(), "MVP"), 1, GL_FALSE, glm::value_ptr(MVP)); - // Color - glm::vec4 color(1.f, 1.f, 1.f, 0.f); - if (colliding) - color = glm::vec4(1.f, 0.f, 0.f, 0.f); - glUniform4fv(glGetUniformLocation(m_ShaderProgramDebugAABB.GetHandle(), "Color"), 1, glm::value_ptr(color)); - glBindVertexArray(m_DebugAABB); - glDrawArrays(GL_LINES, 0, 24); - } - } - - DrawDebugShadowMap(); -#endif + DrawFBO(); ClearStuff(); glfwSwapBuffers(m_Window); } +#pragma region TempRegion + + + + void Renderer::DrawSkybox() { glBindFramebuffer(GL_FRAMEBUFFER, 0); @@ -199,8 +180,8 @@ void Renderer::DrawSkybox() void Renderer::DrawScene() { - glBindFramebuffer(GL_FRAMEBUFFER, 0); - glViewport(0, 0, WIDTH, HEIGHT); +// glBindFramebuffer(GL_FRAMEBUFFER, 0); +// glViewport(0, 0, WIDTH, HEIGHT); glClear(GL_DEPTH_BUFFER_BIT); //glClearColor(1.0f, 1.0f, 0.0f, 1.0f); @@ -537,6 +518,7 @@ GLuint Renderer::CreateSkybox() return vao; } + void Renderer::ClearStuff() { AABBsToRender.clear(); @@ -549,4 +531,144 @@ void Renderer::ClearStuff() Light_quadraticAttenuation.clear(); Light_spotExponent.clear(); Lights = 0; -} \ No newline at end of file +} + +#pragma endregion + +void Renderer::FrameBufferTextures() +{ + m_fb = 0; + + glGenFramebuffers(1, &m_fb); + GLERROR("GLERROR: Failed to generate frame buffer"); + glGenTextures(1, &m_fb_PositionTexture); + GLERROR("GLERROR: Failed to generate Position texture"); + glBindTexture(GL_TEXTURE_2D, m_fb_PositionTexture); + GLERROR("GLERROR: Failed to bind Position texture"); + glTexImage2D( + GL_TEXTURE_2D, + 0, + GL_RGB16F, + WIDTH, + HEIGHT, + 0, + GL_RGBA, + GL_UNSIGNED_BYTE, + NULL + ); + GLERROR("GLERROR: Failed to generate Position texture image"); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + GLERROR("GLERROR: Failed to generate Position Parameters"); + + glGenTextures(1, &m_fb_NormalsTexture); + GLERROR("GLERROR: Failed to generate Normal texture"); + glBindTexture(GL_TEXTURE_2D, m_fb_NormalsTexture); + GLERROR("GLERROR: Failed to bind Normal texture"); + glTexImage2D( + GL_TEXTURE_2D, + 0, + GL_RGB16F, + WIDTH, + HEIGHT, + 0, + GL_RGBA, + GL_UNSIGNED_BYTE, + NULL + ); + GLERROR("GLERROR: Failed to generate Normal texture image"); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + GLERROR("GLERROR: Failed to generate Normals Parameters"); + + glBindFramebuffer(GL_FRAMEBUFFER, m_fb); + GLERROR("GLERROR: Failed to bind framebuffer"); + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_fb_PositionTexture, 0); + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, m_fb_NormalsTexture, 0); + GLERROR("GLERROR: Failed to FrameBufferTexture2D"); + + m_rb = 0; + glGenRenderbuffers(1, &m_rb); + GLERROR("GLERROR: Failed to generate RenderBuffer"); + glBindRenderbuffer(GL_RENDERBUFFER, m_rb); + GLERROR("GLERROR: Failed to bind RenderBuffer"); + glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, WIDTH, HEIGHT); + glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, m_rb); + + draw_bufs[1] = GL_COLOR_ATTACHMENT0; + draw_bufs[2] = GL_COLOR_ATTACHMENT1; + + +} + +void Renderer::DrawFBO() +{ + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glBindFramebuffer(GL_FRAMEBUFFER, m_fb); + glEnable(GL_DEPTH_TEST); + glEnable(GL_CULL_FACE); + glCullFace(GL_BACK); +#ifdef DEBUG + glDisable(GL_CULL_FACE); + glPolygonMode(GL_BACK, GL_LINE); +#endif + + // Draw models + glm::mat4 depthViewMatrix = glm::lookAt(m_SunPosition, m_SunTarget, glm::vec3(0, 1, 0)) * glm::translate(-m_Camera->Position() * glm::vec3(1, 0, 0)); + glm::mat4 depthCamera = m_SunProjection * depthViewMatrix; + glm::mat4 biasMatrix( + 0.5, 0.0, 0.0, 0.0, + 0.0, 0.5, 0.0, 0.0, + 0.0, 0.0, 0.5, 0.0, + 0.5, 0.5, 0.5, 1.0 + ); + + m_ShaderProgram.Bind(); + if (m_DrawWireframe) + { + glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); + } + glm::mat4 cameraMatrix = m_Camera->ProjectionMatrix() * m_Camera->ViewMatrix(); + glm::mat4 depthCameraMatrix = biasMatrix * depthCamera; + glm::mat4 MVP; + glm::mat4 depthMVP; + for (auto tuple : ModelsToRender) + { + Model* model; + glm::mat4 modelMatrix; + bool visible; + std::tie(model, modelMatrix, visible, std::ignore) = tuple; + if (!visible) + continue; + + MVP = cameraMatrix * modelMatrix; + depthMVP = depthCameraMatrix * modelMatrix; + glUniformMatrix4fv(glGetUniformLocation(m_FirstPassProgram.GetHandle(), "M"), 1, GL_FALSE, glm::value_ptr(modelMatrix)); + 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())); + glBindVertexArray(model->VAO); +// for (auto texGroup : model->TextureGroups) +// { +// glActiveTexture(GL_TEXTURE0); +// glBindTexture(GL_TEXTURE_2D, *texGroup.Texture); +// glDrawArrays(GL_TRIANGLES, texGroup.StartIndex, texGroup.EndIndex - texGroup.StartIndex + 1); +// } + } + +#ifdef DEBUG + // Debug draw model normals + if (m_DrawNormals) + { + m_ShaderProgramNormals.Bind(); + glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); + DrawModels(m_ShaderProgramNormals); + } +#endif + + //glDrawBuffers(2, draw_bufs); +} + diff --git a/src/Renderer.h b/src/Renderer.h index 8d0da1f..3243eef 100755 --- a/src/Renderer.h +++ b/src/Renderer.h @@ -88,9 +88,16 @@ private: GLuint m_ShadowFrameBuffer; GLuint m_ShadowDepthTexture; + GLuint m_fb_PositionTexture; + GLuint m_fb_NormalsTexture; + GLuint m_fb; + GLuint m_rb; + GLenum draw_bufs[2]; + std::shared_ptr m_Camera; ShaderProgram m_ShaderProgram; + ShaderProgram m_FirstPassProgram; ShaderProgram m_ShaderProgramNormals; ShaderProgram m_ShaderProgramShadows; ShaderProgram m_ShaderProgramShadowsDrawDepth; @@ -102,6 +109,9 @@ private: void DrawModels(ShaderProgram &shader); void DrawShadowMap(); void CreateShadowMap(int resolution); + void FrameBufferTextures(); + void DrawFBO(); + GLuint CreateQuad(); void DrawDebugShadowMap(); GLuint CreateAABB(); diff --git a/src/Shaders/First_pass.frag.glsl b/src/Shaders/First_pass.frag.glsl new file mode 100644 index 0000000..df66547 --- /dev/null +++ b/src/Shaders/First_pass.frag.glsl @@ -0,0 +1,12 @@ +#version 400 + +in vec3 p_eye; +in vec3 n_eye; + +layout (location = 0) out vec4 def_p; // "go to GL_COLOR_ATTACHMENT0" +layout (location = 1) out vec4 def_n; // "go to GL_COLOR_ATTACHMENT1" + +void main () { + def_p = vec4(p_eye, 1.0); + def_n = vec4(n_eye, 1.0); +} \ No newline at end of file diff --git a/src/Shaders/First_pass.vert.glsl b/src/Shaders/First_pass.vert.glsl new file mode 100644 index 0000000..f6cd49c --- /dev/null +++ b/src/Shaders/First_pass.vert.glsl @@ -0,0 +1,16 @@ +#version 400 + +layout(location = 0) in vec3 vp; +layout(location = 1) in vec3 vn; +layout(location = 2) in vec2 TextureCoord; + +uniform mat4 P, V, M; + +out vec3 p_eye; +out vec3 n_eye; + +void main () { + p_eye = (V * M * vec4 (vp, 1.0)).xyz; + n_eye = (V * M * vec4 (vn, 0.0)).xyz; + gl_Position = P * vec4 (p_eye, 1.0); +} \ No newline at end of file diff --git a/src/Shaders/Second_pass.frag.glsl b/src/Shaders/Second_pass.frag.glsl new file mode 100644 index 0000000..5792748 --- /dev/null +++ b/src/Shaders/Second_pass.frag.glsl @@ -0,0 +1,24 @@ +#version 430 + +uniform sampler2D tDiffuse; +uniform sampler2D tPosition; +uniform sampler2D tNormals; +uniform vec3 cameraPosition; + +void main( void ) +{ + vec4 image = texture2D( tDiffuse, gl_TexCoord[0].xy ); + vec4 position = texture2D( tPosition, gl_TexCoord[0].xy ); + vec4 normal = texture2D( tNormals, gl_TexCoord[0].xy ); + + vec3 light = vec3(50,100,50); + vec3 lightDir = light - position.xyz ; + + normal = normalize(normal); + lightDir = normalize(lightDir); + + vec3 eyeDir = normalize(cameraPosition-position.xyz); + vec3 vHalfVector = normalize(lightDir.xyz+eyeDir); + + gl_FragColor = max(dot(normal,lightDir),0) * image + pow(max(dot(normal,vHalfVector),0.0), 100) * 1.5; +} diff --git a/src/Shaders/Second_pass.vert.glsl b/src/Shaders/Second_pass.vert.glsl new file mode 100644 index 0000000..23a3280 --- /dev/null +++ b/src/Shaders/Second_pass.vert.glsl @@ -0,0 +1,9 @@ +#version 430 + +void main( void ) +{ + gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; + gl_TexCoord[0] = gl_MultiTexCoord0; + + gl_FrontColor = vec4(1.0, 1.0, 1.0, 1.0); +} diff --git a/src/Shaders/geometry_pass.frag.glsl b/src/Shaders/geometry_pass.frag.glsl deleted file mode 100644 index 4fe34ba..0000000 --- a/src/Shaders/geometry_pass.frag.glsl +++ /dev/null @@ -1,20 +0,0 @@ -#version 430 - -in vec2 TexCoord0; -in vec3 Normal0; -in vec3 WorldPos0; - -layout (location = 0) out vec3 WorldPosOut; -layout (location = 1) out vec3 DiffuseOut; -layout (location = 2) out vec3 NormalOut; -layout (location = 3) out vec3 TexCoordOut; - -uniform sampler2D gColorMap; - -void main() -{ - WorldPosOut = WorldPos0; - DiffuseOut = texture(gColorMap, TexCoord0).xyz; - NormalOut = normalize(Normal0); - TexCoordOut = vec3(TexCoord0, 0.0); -} \ No newline at end of file diff --git a/src/Shaders/geometry_pass.vert.glsl b/src/Shaders/geometry_pass.vert.glsl deleted file mode 100644 index c431ba9..0000000 --- a/src/Shaders/geometry_pass.vert.glsl +++ /dev/null @@ -1,20 +0,0 @@ -#version 430 - -layout (location = 0) in vec3 Position; -layout (location = 1) in vec2 TexCoord; -layout (location = 2) in vec3 Normal; - -uniform mat4 gWVP; -uniform mat4 gWorld; - -out vec2 TexCoord0; -out vec3 Normal0; -out vec3 WorldPos0; - -void main() -{ - gl_Position = gWVP * vec4(Position, 1.0); - TexCoord0 = TexCoord; - Normal0 = (gWorld * vec4(Normal, 0.0)).xyz; - WorldPos0 = (gWorld * vec4(Position, 1.0)).xyz; -} \ No newline at end of file diff --git a/src/Util/defferedUtil.h b/src/Util/defferedUtil.h new file mode 100644 index 0000000..d362fac --- /dev/null +++ b/src/Util/defferedUtil.h @@ -0,0 +1,28 @@ +#ifndef UTIL_H +#define UTIL_H + +#include +#include +#include + +#define ZERO_MEM(a) memset(a, 0, sizeof(a)) + +#define ARRAY_SIZE_IN_ELEMENTS(a) (sizeof(a)/sizeof(a[0])) + +#define INVALID_OGL_VALUE 0xFFFFFFFF + +#define SAFE_DELETE(p) if (p) { delete p; p = NULL; } + +#define GLExitIfError() \ +{ \ + GLenum Error = glGetError(); \ + \ + if (Error != GL_NO_ERROR) { \ + printf("OpenGL error in %s:%d: 0x%x\n", __FILE__, __LINE__, Error); \ + exit(0); \ + } \ +} + +#define GLCheckError() (glGetError() == GL_NO_ERROR) + +#endif /* UTIL_H */ diff --git a/src/gBuffer.cpp b/src/gBuffer.cpp deleted file mode 100644 index 5acf690..0000000 --- a/src/gBuffer.cpp +++ /dev/null @@ -1,40 +0,0 @@ -#include "PrecompiledHeader.h" -#include "gBuffer.h" - -bool GBuffer::Init(unsigned int WindowWidth, unsigned int WindowHeight) -{ - // Create the FBO - glGenFramebuffers(1, &m_fbo); - glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_fbo); - - // Create the gbuffer textures - glGenTextures(ARRAY_SIZE_IN_ELEMENTS(m_textures), m_textures); - glGenTextures(1, &m_depthTexture); - - for (unsigned int i = 0 ; i < ARRAY_SIZE_IN_ELEMENTS(m_textures) ; i++) { - glBindTexture(GL_TEXTURE_2D, m_textures[i]); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F, WindowWidth, WindowHeight, 0, GL_RGB, GL_FLOAT, NULL); - glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, GL_TEXTURE_2D, m_textures[i], 0); - } - - // depth - glBindTexture(GL_TEXTURE_2D, m_depthTexture); - glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32F, WindowWidth, WindowHeight, 0, GL_DEPTH_COMPONENT, GL_FLOAT, - NULL); - glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, m_depthTexture, 0); - - GLenum DrawBuffers[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_COLOR_ATTACHMENT3 }; - glDrawBuffers(ARRAY_SIZE_IN_ELEMENTS(DrawBuffers), DrawBuffers); - - GLenum Status = glCheckFramebufferStatus(GL_FRAMEBUFFER); - - if (Status != GL_FRAMEBUFFER_COMPLETE) { - printf("FB error, status: 0x%x\n", Status); - return false; - } - - // restore default FBO - glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); - - return true; -} \ No newline at end of file diff --git a/src/gBuffer.h b/src/gBuffer.h deleted file mode 100644 index 721eb15..0000000 --- a/src/gBuffer.h +++ /dev/null @@ -1,35 +0,0 @@ -#ifndef gBuffer_h__ -#define gBuffer_h__ - -#include - -class GBuffer -{ -public: - - enum GBUFFER_TEXTURE_TYPE { - GBUFFER_TEXTURE_TYPE_POSITION, - GBUFFER_TEXTURE_TYPE_DIFFUSE, - GBUFFER_TEXTURE_TYPE_NORMAL, - GBUFFER_TEXTURE_TYPE_TEXCOORD, - GBUFFER_NUM_TEXTURES - }; - - GBuffer(); - - ~GBuffer(); - - bool Init(unsigned int WindowWidth, unsigned int WindowHeight); - - void BindForWriting(); - - void BindForReading(); - -private: - - GLuint m_fbo; - GLuint m_textures[GBUFFER_NUM_TEXTURES]; - GLuint m_depthTexture; -}; - -#endif //gBuffer_h__ \ No newline at end of file diff --git a/vs11/Returngeance/Returngeance.vcxproj b/vs11/Returngeance/Returngeance.vcxproj index 4726981..46c68eb 100755 --- a/vs11/Returngeance/Returngeance.vcxproj +++ b/vs11/Returngeance/Returngeance.vcxproj @@ -156,15 +156,33 @@ + + + + + true + + + + + + + + + true + + + true + diff --git a/vs11/Returngeance/Returngeance.vcxproj.filters b/vs11/Returngeance/Returngeance.vcxproj.filters index 672e8dd..0186e7f 100755 --- a/vs11/Returngeance/Returngeance.vcxproj.filters +++ b/vs11/Returngeance/Returngeance.vcxproj.filters @@ -214,6 +214,9 @@ + + Util + @@ -249,5 +252,17 @@ Shaders + + Shaders + + + Shaders + + + Shaders + + + Shaders + \ No newline at end of file From 9c329145c4542818a89bb26283c8454d8a18569d Mon Sep 17 00:00:00 2001 From: Tleety Date: Fri, 25 Apr 2014 04:09:42 +0200 Subject: [PATCH 03/22] Deferred rendering base mostly complete. --- src/Renderer.cpp | 218 +++++++++++++++--------------- src/Renderer.h | 10 +- src/Shaders/First_pass.frag.glsl | 27 +++- src/Shaders/First_pass.vert.glsl | 34 ++++- src/Shaders/Second_pass.frag.glsl | 50 ++++--- src/Shaders/Second_pass.vert.glsl | 14 +- 6 files changed, 206 insertions(+), 147 deletions(-) diff --git a/src/Renderer.cpp b/src/Renderer.cpp index 50c820b..de9d6d3 100755 --- a/src/Renderer.cpp +++ b/src/Renderer.cpp @@ -115,8 +115,14 @@ void Renderer::LoadContent() m_FirstPassProgram.AddShader(std::shared_ptr(new VertexShader("Shaders/First_pass.vert.glsl"))); m_FirstPassProgram.AddShader(std::shared_ptr(new FragmentShader("Shaders/First_pass.frag.glsl"))); m_FirstPassProgram.Compile(); + BindFragDataLocation(); m_FirstPassProgram.Link(); + m_SecondPassProgram.AddShader(std::shared_ptr(new VertexShader("Shaders/Second_pass.vert.glsl"))); + m_SecondPassProgram.AddShader(std::shared_ptr(new FragmentShader("Shaders/Second_pass.frag.glsl"))); + m_SecondPassProgram.Compile(); + m_SecondPassProgram.Link(); + m_Skybox = std::make_shared("Textures/Skybox/Sunset", "jpg"); m_DebugAABB = CreateAABB(); @@ -538,137 +544,125 @@ void Renderer::ClearStuff() void Renderer::FrameBufferTextures() { m_fb = 0; - + m_fDepthBuffer = 0; + glGenFramebuffers(1, &m_fb); - GLERROR("GLERROR: Failed to generate frame buffer"); - glGenTextures(1, &m_fb_PositionTexture); - GLERROR("GLERROR: Failed to generate Position texture"); - glBindTexture(GL_TEXTURE_2D, m_fb_PositionTexture); - GLERROR("GLERROR: Failed to bind Position texture"); - glTexImage2D( - GL_TEXTURE_2D, - 0, - GL_RGB16F, - WIDTH, - HEIGHT, - 0, - GL_RGBA, - GL_UNSIGNED_BYTE, - NULL - ); - GLERROR("GLERROR: Failed to generate Position texture image"); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glGenRenderbuffers(1, &m_fDepthBuffer); + + glBindRenderbuffer(GL_RENDERBUFFER, m_fDepthBuffer); + glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, WIDTH, HEIGHT); + + //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); + 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_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - GLERROR("GLERROR: Failed to generate Position Parameters"); - glGenTextures(1, &m_fb_NormalsTexture); - GLERROR("GLERROR: Failed to generate Normal texture"); - glBindTexture(GL_TEXTURE_2D, m_fb_NormalsTexture); - GLERROR("GLERROR: Failed to bind Normal texture"); - glTexImage2D( - GL_TEXTURE_2D, - 0, - GL_RGB16F, - WIDTH, - HEIGHT, - 0, - GL_RGBA, - GL_UNSIGNED_BYTE, - NULL - ); - GLERROR("GLERROR: Failed to generate Normal texture image"); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + //Generate and bind position texture + glGenTextures(1, &m_fPositionTexture); + glBindTexture(GL_TEXTURE_2D, m_fPositionTexture); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, 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_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - GLERROR("GLERROR: Failed to generate Normals Parameters"); + //Generate and bind normal texture + glGenTextures(1, &m_fNormalsTexture); + glBindTexture(GL_TEXTURE_2D, m_fNormalsTexture); + 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_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + + //Generate and bind blend texture + glGenTextures(1, &m_fBlendTexture); + glBindTexture(GL_TEXTURE_2D, m_fBlendTexture); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 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_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + + //Bind fb glBindFramebuffer(GL_FRAMEBUFFER, m_fb); - GLERROR("GLERROR: Failed to bind framebuffer"); - glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_fb_PositionTexture, 0); - glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, m_fb_NormalsTexture, 0); - GLERROR("GLERROR: Failed to FrameBufferTexture2D"); + glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, m_fDepthBuffer); - m_rb = 0; - glGenRenderbuffers(1, &m_rb); - GLERROR("GLERROR: Failed to generate RenderBuffer"); - glBindRenderbuffer(GL_RENDERBUFFER, m_rb); - GLERROR("GLERROR: Failed to bind RenderBuffer"); - glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, WIDTH, HEIGHT); - glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, m_rb); - - draw_bufs[1] = GL_COLOR_ATTACHMENT0; - draw_bufs[2] = GL_COLOR_ATTACHMENT1; - + //Attach textures to the FB + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_fDiffuseTexture, 0); + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, m_fPositionTexture, 0); + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, GL_TEXTURE_2D, m_fNormalsTexture, 0); + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT3, GL_TEXTURE_2D, m_fBlendTexture, 0); + GLenum fbStatus = glCheckFramebufferStatus(GL_FRAMEBUFFER); + if(fbStatus != GL_FRAMEBUFFER_COMPLETE) + { + printf("DeferredLighting:Init: FrameBuffer incomplete: 0x%x\n", fbStatus); + exit(1); + } + + glBindFramebuffer(GL_FRAMEBUFFER, 0); } void Renderer::DrawFBO() { + glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_fb); + + GLenum windowBuffClear[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_COLOR_ATTACHMENT3 }; + glDrawBuffers(4, windowBuffClear); + glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - glBindFramebuffer(GL_FRAMEBUFFER, m_fb); - glEnable(GL_DEPTH_TEST); - glEnable(GL_CULL_FACE); - glCullFace(GL_BACK); -#ifdef DEBUG - glDisable(GL_CULL_FACE); - glPolygonMode(GL_BACK, GL_LINE); -#endif - // Draw models - glm::mat4 depthViewMatrix = glm::lookAt(m_SunPosition, m_SunTarget, glm::vec3(0, 1, 0)) * glm::translate(-m_Camera->Position() * glm::vec3(1, 0, 0)); - glm::mat4 depthCamera = m_SunProjection * depthViewMatrix; - glm::mat4 biasMatrix( - 0.5, 0.0, 0.0, 0.0, - 0.0, 0.5, 0.0, 0.0, - 0.0, 0.0, 0.5, 0.0, - 0.5, 0.5, 0.5, 1.0 - ); + // Execute the first render stage which will fill out the internal buffers with data(??) + //EnableRenderProgramStage1; + GLenum windowBuffOpaque[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_NONE }; + glDrawBuffers(4, windowBuffOpaque); + //DrawTheWorld(); - m_ShaderProgram.Bind(); - if (m_DrawWireframe) - { - glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); - } - glm::mat4 cameraMatrix = m_Camera->ProjectionMatrix() * m_Camera->ViewMatrix(); - glm::mat4 depthCameraMatrix = biasMatrix * depthCamera; - glm::mat4 MVP; - glm::mat4 depthMVP; - for (auto tuple : ModelsToRender) - { - Model* model; - glm::mat4 modelMatrix; - bool visible; - std::tie(model, modelMatrix, visible, std::ignore) = tuple; - if (!visible) - continue; + GLenum windowBuffTransp[] = { GL_NONE, GL_NONE, GL_NONE, GL_COLOR_ATTACHMENT3 }; + glDrawBuffers(4, windowBuffTransp); + glEnable(GL_BLEND); + glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); + //Depth buffer shall not be updated + glDepthMask(GL_FALSE); + //DrawTransparent items + glDepthMask(GL_TRUE); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + glDisable(GL_BLEND); + glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); - MVP = cameraMatrix * modelMatrix; - depthMVP = depthCameraMatrix * modelMatrix; - glUniformMatrix4fv(glGetUniformLocation(m_FirstPassProgram.GetHandle(), "M"), 1, GL_FALSE, glm::value_ptr(modelMatrix)); - 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())); - glBindVertexArray(model->VAO); -// for (auto texGroup : model->TextureGroups) -// { -// glActiveTexture(GL_TEXTURE0); -// glBindTexture(GL_TEXTURE_2D, *texGroup.Texture); -// glDrawArrays(GL_TRIANGLES, texGroup.StartIndex, texGroup.EndIndex - texGroup.StartIndex + 1); -// } - } + //Probably means to use the second_pass shader + //EnableRenderProgramDeferredStage(); -#ifdef DEBUG - // Debug draw model normals - if (m_DrawNormals) - { - m_ShaderProgramNormals.Bind(); - glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); - DrawModels(m_ShaderProgramNormals); - } -#endif + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); + //SetupDefferedStageUniforms(); // Probably what we do in FrameBufferTextures(); + //glEnableVertexAttribArray(fVertexIndex); // VertexIndex? + glActiveTexture(GL_TEXTURE3); + glBindTexture(GL_TEXTURE_2D, m_fBlendTexture); + + glActiveTexture(GL_TEXTURE2); + glBindTexture(GL_TEXTURE_2D, m_fNormalsTexture); + + glActiveTexture(GL_TEXTURE1); + glBindTexture(GL_TEXTURE_2D, m_fPositionTexture); + + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, m_fDiffuseTexture); + + //DrawSimpleSquare(); //I guess this draw a square and put the textures on it + + //glDisableVertexAttribArray(fVertexIndex); //VertexIndex? - //glDrawBuffers(2, draw_bufs); } +void Renderer::BindFragDataLocation() +{ + glBindFragDataLocation(m_FirstPassProgram.GetHandle(), 0, "diffuseOutput"); + glBindFragDataLocation(m_FirstPassProgram.GetHandle(), 1, "posOutput"); + glBindFragDataLocation(m_FirstPassProgram.GetHandle(), 2, "normOutput"); + glBindFragDataLocation(m_FirstPassProgram.GetHandle(), 3, "blendOutput"); +} diff --git a/src/Renderer.h b/src/Renderer.h index 3243eef..d915c9c 100755 --- a/src/Renderer.h +++ b/src/Renderer.h @@ -88,16 +88,19 @@ private: GLuint m_ShadowFrameBuffer; GLuint m_ShadowDepthTexture; - GLuint m_fb_PositionTexture; - GLuint m_fb_NormalsTexture; + GLuint m_fDiffuseTexture; + GLuint m_fPositionTexture; + GLuint m_fNormalsTexture; + GLuint m_fBlendTexture; GLuint m_fb; - GLuint m_rb; + GLuint m_fDepthBuffer; GLenum draw_bufs[2]; std::shared_ptr m_Camera; ShaderProgram m_ShaderProgram; ShaderProgram m_FirstPassProgram; + ShaderProgram m_SecondPassProgram; ShaderProgram m_ShaderProgramNormals; ShaderProgram m_ShaderProgramShadows; ShaderProgram m_ShaderProgramShadowsDrawDepth; @@ -111,6 +114,7 @@ private: void CreateShadowMap(int resolution); void FrameBufferTextures(); void DrawFBO(); + void BindFragDataLocation(); GLuint CreateQuad(); void DrawDebugShadowMap(); diff --git a/src/Shaders/First_pass.frag.glsl b/src/Shaders/First_pass.frag.glsl index df66547..2f3405b 100644 --- a/src/Shaders/First_pass.frag.glsl +++ b/src/Shaders/First_pass.frag.glsl @@ -1,4 +1,27 @@ -#version 400 +#version 130 +uniform sampler2D firstTexture; +in vec3 fragmentNormal; +in vec2 fragmentTexCoord; +in vec3 position; +layout (location = 0) out vec4 diffuseOutput; +layout (location = 1) out vec4 posOutput; +layout (location = 2) out vec4 normOutput; +layout (location = 3) out vec4 blendOutput; + +void main(void) +{ + posOutput.xyz = position; + normOutPut = vec4(fragmentNormal, 0); + vec4 clr = texture(firstTexture, fragmentTexCoord); + float alpha = clr.a; + if(alpha < 0.1) + discard; //Some optimizing + blendOutput.rgb = clr.rgb * clr.a; //Pre multiplied alpha + blendOutput.a = clr.a; + diffuseOutput = clr; +} + +/*#version 400 in vec3 p_eye; in vec3 n_eye; @@ -9,4 +32,4 @@ layout (location = 1) out vec4 def_n; // "go to GL_COLOR_ATTACHMENT1" void main () { def_p = vec4(p_eye, 1.0); def_n = vec4(n_eye, 1.0); -} \ No newline at end of file +}*/ \ No newline at end of file diff --git a/src/Shaders/First_pass.vert.glsl b/src/Shaders/First_pass.vert.glsl index f6cd49c..832c0d8 100644 --- a/src/Shaders/First_pass.vert.glsl +++ b/src/Shaders/First_pass.vert.glsl @@ -1,4 +1,34 @@ -#version 400 +#version 130 + +precision mediump float; +uniform mat4 projectionMatrix; +uniform mat4 modelMatrix; +uniform mat4 viewMatrix; + +in vec3 normal; +in vec2 texCoord; +in vec3 vertex; + +in float intensity; +in float ambientLight; + +out vec3 fragmentNormal; +out vec2 fragmentTexCoord; +out float extIntensity; +out float extAmbientLight; + +void main(void) +{ + fragmentTexCoord = texCoord; + fragmentNormal = normalize((modelMatrix*vec4(normal, 0.0).xyz); + gl_Position = vec3(modelMatrix * vertex); //Copy position to the fragment shader + extIntensity = intensity/255.0; + extAmbientLight = ambientLight/255.0; +} + + + +/*#version 400 layout(location = 0) in vec3 vp; layout(location = 1) in vec3 vn; @@ -13,4 +43,4 @@ void main () { p_eye = (V * M * vec4 (vp, 1.0)).xyz; n_eye = (V * M * vec4 (vn, 0.0)).xyz; gl_Position = P * vec4 (p_eye, 1.0); -} \ No newline at end of file +}*/ \ No newline at end of file diff --git a/src/Shaders/Second_pass.frag.glsl b/src/Shaders/Second_pass.frag.glsl index 5792748..3591f9b 100644 --- a/src/Shaders/Second_pass.frag.glsl +++ b/src/Shaders/Second_pass.frag.glsl @@ -1,24 +1,32 @@ -#version 430 +#version 130 -uniform sampler2D tDiffuse; -uniform sampler2D tPosition; -uniform sampler2D tNormals; -uniform vec3 cameraPosition; +uniform sampler2D diffuseTex; // The color information +uniform sampler2D posTex; // World position +uniform sampler2D normalTex; // Normals +uniform sampler2D blendTex; // A bitmap with colors to blend with. +uniform vec3 camera; // The coordinate of the camera +in vec2 position; // The world position +layout (location = 0) out vec4 fragColor; -void main( void ) +void main(void) { - vec4 image = texture2D( tDiffuse, gl_TexCoord[0].xy ); - vec4 position = texture2D( tPosition, gl_TexCoord[0].xy ); - vec4 normal = texture2D( tNormals, gl_TexCoord[0].xy ); - - vec3 light = vec3(50,100,50); - vec3 lightDir = light - position.xyz ; - - normal = normalize(normal); - lightDir = normalize(lightDir); - - vec3 eyeDir = normalize(cameraPosition-position.xyz); - vec3 vHalfVector = normalize(lightDir.xyz+eyeDir); - - gl_FragColor = max(dot(normal,lightDir),0) * image + pow(max(dot(normal,vHalfVector),0.0), 100) * 1.5; -} + // Load data, stored in textures, from the first stage rendering. + vec4 diffuse = texture2D(diffuseTex, position.xy); + vec4 blend = texture2D(blendTex, position.xy); + vec4 worldPos = texture2D(posTex, position.xy); + vec4 normal = texture2D(normalTex, position.xy); + // Use information about lamp coordinate (not shown here), the pixel + // coordinate (worldpos.xyz), the normal of this pixel (normal.xyz) + // to compute a lighting effect. + // Use this lighting effect to update 'diffuse' + vec4 preBlend = diffuse * lamp + specularGlare; + // manual blending, using premultiplied alpha. + fragColor = blend + preBlend*(1-blend.a); +// Some debug features. Enable any of them to get a visual representation +// of an internal buffer. +// fragColor = (normal+1)/2; +// fragColor = diffuse; +// fragColor = blend; +// fragColor = worldPos; // Scaling may be needed to range [0,1] +// fragColor = lamp*vec4(1,1,1,1); +} \ No newline at end of file diff --git a/src/Shaders/Second_pass.vert.glsl b/src/Shaders/Second_pass.vert.glsl index 23a3280..3845ced 100644 --- a/src/Shaders/Second_pass.vert.glsl +++ b/src/Shaders/Second_pass.vert.glsl @@ -1,9 +1,9 @@ -#version 430 +#version 130 +in vec4 vertex; out vec2 position; -void main( void ) +void main(void) { - gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; - gl_TexCoord[0] = gl_MultiTexCoord0; - - gl_FrontColor = vec4(1.0, 1.0, 1.0, 1.0); -} + gl_Position = vertex*2-1; + gl_Position.z = 0.0; + position = vertex.xy; +} \ No newline at end of file From 87cac66cd8222b7417c71f74f7bd4a0a1877c625 Mon Sep 17 00:00:00 2001 From: Tleety Date: Sun, 27 Apr 2014 02:44:04 +0200 Subject: [PATCH 04/22] Basic working deferred rendering --- src/GameWorld.cpp | 2 +- src/Renderer.cpp | 277 ++++++++++++------ src/Renderer.h | 3 +- src/ShaderProgram.cpp | 7 +- src/ShaderProgram.h | 2 +- src/Shaders/First_pass.frag.glsl | 35 --- src/Shaders/First_pass.vert.glsl | 46 --- src/Shaders/Fragment.glsl | 107 +------ src/Shaders/Fragment2.glsl | 19 ++ src/Shaders/Second_pass.frag.glsl | 32 -- src/Shaders/Second_pass.vert.glsl | 9 - src/Shaders/Vertex.glsl | 11 +- src/Shaders/Vertex2.glsl | 17 ++ vs11/Returngeance.psess | 83 ++++++ vs11/Returngeance.sln | 3 + vs11/Returngeance/Returngeance.vcxproj | 2 + .../Returngeance/Returngeance.vcxproj.filters | 14 +- 17 files changed, 344 insertions(+), 325 deletions(-) delete mode 100644 src/Shaders/First_pass.frag.glsl delete mode 100644 src/Shaders/First_pass.vert.glsl create mode 100644 src/Shaders/Fragment2.glsl delete mode 100644 src/Shaders/Second_pass.frag.glsl delete mode 100644 src/Shaders/Second_pass.vert.glsl create mode 100644 src/Shaders/Vertex2.glsl create mode 100644 vs11/Returngeance.psess diff --git a/src/GameWorld.cpp b/src/GameWorld.cpp index 62e3980..f51cddf 100755 --- a/src/GameWorld.cpp +++ b/src/GameWorld.cpp @@ -67,7 +67,7 @@ void GameWorld::Initialize() model->ModelFile = "Models/Placeholders/PhysicsTest/PointLight.obj"; } - for(int i = 0; i < 500; i++) + for(int i = 0; i < 0; i++) { auto ball = CreateEntity(); auto transform = AddComponent(ball, "Transform"); diff --git a/src/Renderer.cpp b/src/Renderer.cpp index de9d6d3..44032b0 100755 --- a/src/Renderer.cpp +++ b/src/Renderer.cpp @@ -72,13 +72,11 @@ void Renderer::Initialize() glEnable(GL_DEPTH_TEST); LoadContent(); - - FrameBufferTextures(); } void Renderer::LoadContent() { - auto standardVS = std::shared_ptr(new VertexShader("Shaders/Vertex.glsl")); + /*auto standardVS = std::shared_ptr(new VertexShader("Shaders/Vertex.glsl")); auto standardFS = std::shared_ptr(new FragmentShader("Shaders/Fragment.glsl")); m_ShaderProgram.AddShader(standardVS); @@ -110,52 +108,26 @@ void Renderer::LoadContent() m_ShaderProgramSkybox.AddShader(std::shared_ptr(new VertexShader("Shaders/Skybox.vert.glsl"))); m_ShaderProgramSkybox.AddShader(std::shared_ptr(new FragmentShader("Shaders/Skybox.frag.glsl"))); m_ShaderProgramSkybox.Compile(); - m_ShaderProgramSkybox.Link(); + m_ShaderProgramSkybox.Link();*/ - m_FirstPassProgram.AddShader(std::shared_ptr(new VertexShader("Shaders/First_pass.vert.glsl"))); - m_FirstPassProgram.AddShader(std::shared_ptr(new FragmentShader("Shaders/First_pass.frag.glsl"))); + m_FirstPassProgram.AddShader(std::shared_ptr(new VertexShader("Shaders/Vertex.glsl"))); + m_FirstPassProgram.AddShader(std::shared_ptr(new FragmentShader("Shaders/Fragment.glsl"))); m_FirstPassProgram.Compile(); - BindFragDataLocation(); + glBindFragDataLocation(m_FirstPassProgram.GetHandle(), 0, "frag_Diffuse"); + glBindFragDataLocation(m_FirstPassProgram.GetHandle(), 1, "frag_Position"); + glBindFragDataLocation(m_FirstPassProgram.GetHandle(), 2, "frag_Normal"); m_FirstPassProgram.Link(); - m_SecondPassProgram.AddShader(std::shared_ptr(new VertexShader("Shaders/Second_pass.vert.glsl"))); - m_SecondPassProgram.AddShader(std::shared_ptr(new FragmentShader("Shaders/Second_pass.frag.glsl"))); + m_SecondPassProgram.AddShader(std::shared_ptr(new VertexShader("Shaders/Vertex2.glsl"))); + m_SecondPassProgram.AddShader(std::shared_ptr(new FragmentShader("Shaders/Fragment2.glsl"))); m_SecondPassProgram.Compile(); m_SecondPassProgram.Link(); - m_Skybox = std::make_shared("Textures/Skybox/Sunset", "jpg"); - - m_DebugAABB = CreateAABB(); m_ScreenQuad = CreateQuad(); - CreateShadowMap(m_ShadowMapRes); + + FrameBufferTextures(); } -void Renderer::CreateShadowMap(int resolution) -{ - glGenFramebuffers(1, &m_ShadowFrameBuffer); - glBindFramebuffer(GL_FRAMEBUFFER, m_ShadowFrameBuffer); - - // Depth texture - glGenTextures(1, &m_ShadowDepthTexture); - glBindTexture(GL_TEXTURE_2D, m_ShadowDepthTexture); - glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT16, resolution, resolution, 0, GL_DEPTH_COMPONENT, GL_FLOAT, nullptr); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER); - - //glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE ); - //glTexParameteri( GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE, GL_INTENSITY ); - - glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, m_ShadowDepthTexture, 0); - glDrawBuffer(GL_NONE); - - if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) - { - LOG_ERROR("Framebuffer incomplete!"); - return; - } -} void Renderer::Draw(double dt) { glDisable(GL_BLEND); @@ -550,7 +522,7 @@ void Renderer::FrameBufferTextures() glGenRenderbuffers(1, &m_fDepthBuffer); glBindRenderbuffer(GL_RENDERBUFFER, m_fDepthBuffer); - glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, WIDTH, HEIGHT); + glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, WIDTH, HEIGHT); //Generate and bind diffuse texture glGenTextures(1, &m_fDiffuseTexture); @@ -579,15 +551,6 @@ void Renderer::FrameBufferTextures() glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - //Generate and bind blend texture - glGenTextures(1, &m_fBlendTexture); - glBindTexture(GL_TEXTURE_2D, m_fBlendTexture); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 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_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - //Bind fb glBindFramebuffer(GL_FRAMEBUFFER, m_fb); glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, m_fDepthBuffer); @@ -596,73 +559,201 @@ void Renderer::FrameBufferTextures() glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_fDiffuseTexture, 0); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, m_fPositionTexture, 0); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, GL_TEXTURE_2D, m_fNormalsTexture, 0); - glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT3, GL_TEXTURE_2D, m_fBlendTexture, 0); GLenum fbStatus = glCheckFramebufferStatus(GL_FRAMEBUFFER); if(fbStatus != GL_FRAMEBUFFER_COMPLETE) { - printf("DeferredLighting:Init: FrameBuffer incomplete: 0x%x\n", fbStatus); - exit(1); + LOG_ERROR("DeferredLighting:Init: FrameBuffer incomplete: 0x%x\n", fbStatus); + //exit(1); } - - glBindFramebuffer(GL_FRAMEBUFFER, 0); } +//void Renderer::FrameBufferTextures() +//{ +// m_fb = 0; +// m_fDepthBuffer = 0; +// +// glGenFramebuffers(1, &m_fb); +// glGenRenderbuffers(1, &m_fDepthBuffer); +// +// glBindRenderbuffer(GL_RENDERBUFFER, m_fDepthBuffer); +// glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, WIDTH, HEIGHT); +// +// //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); +// 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_CLAMP_TO_EDGE); +// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); +// +// //Generate and bind position texture +// glGenTextures(1, &m_fPositionTexture); +// glBindTexture(GL_TEXTURE_2D, m_fPositionTexture); +// glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, 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_CLAMP_TO_EDGE); +// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); +// +// //Generate and bind normal texture +// glGenTextures(1, &m_fNormalsTexture); +// glBindTexture(GL_TEXTURE_2D, m_fNormalsTexture); +// 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_CLAMP_TO_EDGE); +// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); +// +// //Generate and bind blend texture +// glGenTextures(1, &m_fBlendTexture); +// glBindTexture(GL_TEXTURE_2D, m_fBlendTexture); +// glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 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_CLAMP_TO_EDGE); +// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); +// +// //Bind fb +// glBindFramebuffer(GL_FRAMEBUFFER, m_fb); +// glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, m_fDepthBuffer); +// +// //Attach textures to the FB +// glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_fDiffuseTexture, 0); +// glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, m_fPositionTexture, 0); +// glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, GL_TEXTURE_2D, m_fNormalsTexture, 0); +// glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT3, GL_TEXTURE_2D, m_fBlendTexture, 0); +// +// GLenum fbStatus = glCheckFramebufferStatus(GL_FRAMEBUFFER); +// if(fbStatus != GL_FRAMEBUFFER_COMPLETE) +// { +// printf("DeferredLighting:Init: FrameBuffer incomplete: 0x%x\n", fbStatus); +// exit(1); +// } +// +// glBindFramebuffer(GL_FRAMEBUFFER, 0); +//} + void Renderer::DrawFBO() { glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_fb); - GLenum windowBuffClear[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_COLOR_ATTACHMENT3 }; - glDrawBuffers(4, windowBuffClear); + // 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); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Execute the first render stage which will fill out the internal buffers with data(??) - //EnableRenderProgramStage1; - GLenum windowBuffOpaque[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_NONE }; - glDrawBuffers(4, windowBuffOpaque); - //DrawTheWorld(); + m_FirstPassProgram.Bind(); + GLenum windowBuffOpaque[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2 }; + glDrawBuffers(3, windowBuffOpaque); + DrawFBOScene(); - GLenum windowBuffTransp[] = { GL_NONE, GL_NONE, GL_NONE, GL_COLOR_ATTACHMENT3 }; - glDrawBuffers(4, windowBuffTransp); - glEnable(GL_BLEND); - glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); - //Depth buffer shall not be updated - glDepthMask(GL_FALSE); - //DrawTransparent items - glDepthMask(GL_TRUE); - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - glDisable(GL_BLEND); + // Draw to screen glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); + m_SecondPassProgram.Bind(); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - //Probably means to use the second_pass shader - //EnableRenderProgramDeferredStage(); - - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); - //SetupDefferedStageUniforms(); // Probably what we do in FrameBufferTextures(); - //glEnableVertexAttribArray(fVertexIndex); // VertexIndex? - glActiveTexture(GL_TEXTURE3); - glBindTexture(GL_TEXTURE_2D, m_fBlendTexture); - - glActiveTexture(GL_TEXTURE2); - glBindTexture(GL_TEXTURE_2D, m_fNormalsTexture); + ////SetupDefferedStageUniforms(); // Probably what we do in FrameBufferTextures(); + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, m_fDiffuseTexture); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, m_fPositionTexture); - glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, m_fDiffuseTexture); - - //DrawSimpleSquare(); //I guess this draw a square and put the textures on it - - //glDisableVertexAttribArray(fVertexIndex); //VertexIndex? + glActiveTexture(GL_TEXTURE2); + glBindTexture(GL_TEXTURE_2D, m_fNormalsTexture); + glBindVertexArray(m_ScreenQuad); + glEnableVertexAttribArray(0); + glEnableVertexAttribArray(2); + glDrawArrays(GL_TRIANGLES, 0, 6); } -void Renderer::BindFragDataLocation() -{ - glBindFragDataLocation(m_FirstPassProgram.GetHandle(), 0, "diffuseOutput"); - glBindFragDataLocation(m_FirstPassProgram.GetHandle(), 1, "posOutput"); - glBindFragDataLocation(m_FirstPassProgram.GetHandle(), 2, "normOutput"); - glBindFragDataLocation(m_FirstPassProgram.GetHandle(), 3, "blendOutput"); +//void Renderer::DrawFBO() +//{ +// glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_fb); +// +// GLenum windowBuffClear[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_COLOR_ATTACHMENT3 }; +// glDrawBuffers(4, windowBuffClear); +// glClearColor(0.0f, 0.0f, 0.0f, 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(??) +// //EnableRenderProgramStage1; +// m_FirstPassProgram.Bind(); +// GLenum windowBuffOpaque[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_NONE }; +// glDrawBuffers(4, windowBuffOpaque); +// DrawFBOScene(); +// +// GLenum windowBuffTransp[] = { GL_NONE, GL_NONE, GL_NONE, GL_COLOR_ATTACHMENT3 }; +// glDrawBuffers(4, windowBuffTransp); +// glEnable(GL_BLEND); +// glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); +// //Depth buffer shall not be updated +// glDepthMask(GL_FALSE); +// //DrawTransparent items +// glDepthMask(GL_TRUE); +// 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(); +// m_SecondPassProgram.Bind(); +// glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); +// //SetupDefferedStageUniforms(); // Probably what we do in FrameBufferTextures(); +// glEnableVertexAttribArray(0); +// glActiveTexture(GL_TEXTURE0); +// glBindTexture(GL_TEXTURE_2D, m_fDiffuseTexture); +// +// glActiveTexture(GL_TEXTURE1); +// glBindTexture(GL_TEXTURE_2D, m_fPositionTexture); +// +// glActiveTexture(GL_TEXTURE2); +// glBindTexture(GL_TEXTURE_2D, m_fNormalsTexture); +// +// glActiveTexture(GL_TEXTURE3); +// glBindTexture(GL_TEXTURE_2D, m_fBlendTexture); +// +// +// +// +// +// +// +// //DrawSimpleSquare(); //I guess this draw a square and put the textures on it +// glBindVertexArray(m_ScreenQuad); +// glDrawArrays(GL_TRIANGLES, 0, 6); +// glDisableVertexAttribArray(0); +// +//} + +void Renderer::DrawFBOScene() +{ + glm::mat4 cameraMatrix = m_Camera->ProjectionMatrix() * m_Camera->ViewMatrix(); + glm::mat4 MVP; + for (auto tuple : ModelsToRender) + { + Model* model; + glm::mat4 modelMatrix; + bool visible; + std::tie(model, modelMatrix, visible, std::ignore) = tuple; + if (!visible) + continue; + + MVP = cameraMatrix * modelMatrix; + glUniformMatrix4fv(glGetUniformLocation(m_FirstPassProgram.GetHandle(), "MVP"), 1, GL_FALSE, glm::value_ptr(MVP)); + glBindVertexArray(model->VAO); + for (auto texGroup : model->TextureGroups) + { + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, *texGroup.Texture); + glDrawArrays(GL_TRIANGLES, texGroup.StartIndex, texGroup.EndIndex - texGroup.StartIndex + 1); + } + } } + diff --git a/src/Renderer.h b/src/Renderer.h index d915c9c..2d5eab0 100755 --- a/src/Renderer.h +++ b/src/Renderer.h @@ -84,7 +84,6 @@ private: glm::mat4 m_SunProjection; GLuint m_DebugAABB; - GLuint m_ScreenQuad; GLuint m_ShadowFrameBuffer; GLuint m_ShadowDepthTexture; @@ -95,6 +94,7 @@ private: GLuint m_fb; GLuint m_fDepthBuffer; GLenum draw_bufs[2]; + GLuint m_ScreenQuad; std::shared_ptr m_Camera; @@ -114,6 +114,7 @@ private: void CreateShadowMap(int resolution); void FrameBufferTextures(); void DrawFBO(); + void DrawFBOScene(); void BindFragDataLocation(); GLuint CreateQuad(); diff --git a/src/ShaderProgram.cpp b/src/ShaderProgram.cpp index e9bd864..bd67e90 100755 --- a/src/ShaderProgram.cpp +++ b/src/ShaderProgram.cpp @@ -103,6 +103,11 @@ void ShaderProgram::AddShader(std::shared_ptr shader) void ShaderProgram::Compile() { + if (m_ShaderProgramHandle == 0) + { + m_ShaderProgramHandle = glCreateProgram(); + } + for (auto &shader : m_Shaders) { if (!shader->IsCompiled()) @@ -121,7 +126,7 @@ GLuint ShaderProgram::Link() } LOG_INFO("Linking shader program"); - m_ShaderProgramHandle = glCreateProgram(); + for (auto &shader : m_Shaders) { glAttachShader(m_ShaderProgramHandle, shader->GetHandle()); diff --git a/src/ShaderProgram.h b/src/ShaderProgram.h index 07dbf9b..a27300c 100755 --- a/src/ShaderProgram.h +++ b/src/ShaderProgram.h @@ -61,7 +61,7 @@ class ShaderProgram { public: ShaderProgram() - : m_ShaderProgramHandle(0) { } + : m_ShaderProgramHandle(0) { } ~ShaderProgram(); void AddShader(std::shared_ptr shader); diff --git a/src/Shaders/First_pass.frag.glsl b/src/Shaders/First_pass.frag.glsl deleted file mode 100644 index 2f3405b..0000000 --- a/src/Shaders/First_pass.frag.glsl +++ /dev/null @@ -1,35 +0,0 @@ -#version 130 -uniform sampler2D firstTexture; -in vec3 fragmentNormal; -in vec2 fragmentTexCoord; -in vec3 position; -layout (location = 0) out vec4 diffuseOutput; -layout (location = 1) out vec4 posOutput; -layout (location = 2) out vec4 normOutput; -layout (location = 3) out vec4 blendOutput; - -void main(void) -{ - posOutput.xyz = position; - normOutPut = vec4(fragmentNormal, 0); - vec4 clr = texture(firstTexture, fragmentTexCoord); - float alpha = clr.a; - if(alpha < 0.1) - discard; //Some optimizing - blendOutput.rgb = clr.rgb * clr.a; //Pre multiplied alpha - blendOutput.a = clr.a; - diffuseOutput = clr; -} - -/*#version 400 - -in vec3 p_eye; -in vec3 n_eye; - -layout (location = 0) out vec4 def_p; // "go to GL_COLOR_ATTACHMENT0" -layout (location = 1) out vec4 def_n; // "go to GL_COLOR_ATTACHMENT1" - -void main () { - def_p = vec4(p_eye, 1.0); - def_n = vec4(n_eye, 1.0); -}*/ \ No newline at end of file diff --git a/src/Shaders/First_pass.vert.glsl b/src/Shaders/First_pass.vert.glsl deleted file mode 100644 index 832c0d8..0000000 --- a/src/Shaders/First_pass.vert.glsl +++ /dev/null @@ -1,46 +0,0 @@ -#version 130 - -precision mediump float; -uniform mat4 projectionMatrix; -uniform mat4 modelMatrix; -uniform mat4 viewMatrix; - -in vec3 normal; -in vec2 texCoord; -in vec3 vertex; - -in float intensity; -in float ambientLight; - -out vec3 fragmentNormal; -out vec2 fragmentTexCoord; -out float extIntensity; -out float extAmbientLight; - -void main(void) -{ - fragmentTexCoord = texCoord; - fragmentNormal = normalize((modelMatrix*vec4(normal, 0.0).xyz); - gl_Position = vec3(modelMatrix * vertex); //Copy position to the fragment shader - extIntensity = intensity/255.0; - extAmbientLight = ambientLight/255.0; -} - - - -/*#version 400 - -layout(location = 0) in vec3 vp; -layout(location = 1) in vec3 vn; -layout(location = 2) in vec2 TextureCoord; - -uniform mat4 P, V, M; - -out vec3 p_eye; -out vec3 n_eye; - -void main () { - p_eye = (V * M * vec4 (vp, 1.0)).xyz; - n_eye = (V * M * vec4 (vn, 0.0)).xyz; - gl_Position = P * vec4 (p_eye, 1.0); -}*/ \ No newline at end of file diff --git a/src/Shaders/Fragment.glsl b/src/Shaders/Fragment.glsl index 11a43ab..7aa72d7 100755 --- a/src/Shaders/Fragment.glsl +++ b/src/Shaders/Fragment.glsl @@ -1,113 +1,26 @@ #version 430 -uniform mat4 model; -uniform mat4 view; - -layout(binding=0) uniform sampler2D texture0; -layout(binding=1) uniform sampler2D shadowMap; - -const int maxNumberOfLights = 82; -uniform int numberOfLights; -uniform vec3 position[maxNumberOfLights]; -uniform vec3 specular[maxNumberOfLights]; -uniform vec3 diffuse[maxNumberOfLights]; -uniform float constantAttenuation[maxNumberOfLights]; -uniform float linearAttenuation[maxNumberOfLights]; -uniform float quadraticAttenuation[maxNumberOfLights]; -uniform float spotExponent[maxNumberOfLights]; +layout (binding=0) uniform sampler2D DiffuseTexture; in VertexData { vec3 Position; vec3 Normal; vec2 TextureCoord; - vec3 ShadowCoord; } Input; -vec3 scene_ambient = vec3(0.5, 0.5, 0.5); - -out vec4 fragmentColor; +out vec4 frag_Diffuse; +out vec4 frag_Position; +out vec4 frag_Normal; void main() { + // Diffuse Texture + frag_Diffuse = texture2D(DiffuseTexture, Input.TextureCoord); - // Texture - vec4 texel = texture2D(texture0, Input.TextureCoord); - //vec4 texel = (blend.x * texel0) + (blend.y * texel1) + (blend.z * texel2); + // G-buffer Position + frag_Position = vec4(Input.Position.xy, 0.0, 0.0); - // - // Phong shading - // - - // Ambient light - vec3 La = scene_ambient; // Ambient light - vec3 Ks = vec3(0.3, 0.3, 0.3); // Specular reflectance - vec3 Kd = vec3(1.0, 1.0, 1.0); // Diffuse reflectance - vec3 Ka = vec3(1.0, 1.0, 1.0); // Ambient reflectance - vec3 Is; - vec3 Id; - - // Shadows - //float cosTheta = clamp(dot(Input.Normal, vec3(0, 1, 0)), 0.0, 1.0); - //float bias = 0.001 * tan(acos(cosTheta)); // cosTheta is dot( n,l ), clamped between 0 and 1 - //bias = clamp(bias, 0.0, 0.01); - float visibility = 1.0; - if (Input.ShadowCoord.x >= 0.0 && Input.ShadowCoord.x <= 1.0 && Input.ShadowCoord.y >= 0.0 && Input.ShadowCoord.y <= 1.0) - { - float bias = 0.00005; - vec4 shadowMapValue = texture(shadowMap, Input.ShadowCoord.xy); - if (shadowMapValue.z < clamp(Input.ShadowCoord.z - bias, 0, 1)) - { - visibility = 0.3; - } - } - - vec3 totalLighting = La * Ka * visibility; - - float attenuation; - - for(int i = 0; i < numberOfLights && i < maxNumberOfLights; i++) - { - // Light - //vec3 lightPosition = vec3(0, 0, 2); - vec3 Ls = specular[i]; // Specular light - vec3 Ld = diffuse[i]; // Diffuse light - - vec3 lightPosView = vec3(view * vec4(position[i], 1.0)); - vec3 surfacePosition = vec3(model * vec4(Input.Position, 1.0)); - vec3 surfacePosView = vec3(view * vec4(surfacePosition, 1.0)); - vec3 surfaceToLight = normalize(lightPosView - surfacePosView); - mat3 normalMatrix = transpose(inverse(mat3(view * model))); - vec3 surfaceNormal = normalize(normalMatrix * Input.Normal); - - float dist = length(position[i] - surfacePosition); - - attenuation = 1.0 / (constantAttenuation[i] - + linearAttenuation[i] * dist - + quadraticAttenuation[i] * pow(dist, 2.0)); - //attenuation = attenuation * pow(clampedCosine, spotExponent[i]); - - // Diffuse light - float dotProd = dot(surfaceToLight, surfaceNormal); - dotProd = max(dotProd, 0.0); - - Id = Ld * Kd * abs(dotProd) * attenuation; - - // Specular light - vec3 reflection = reflect(-surfaceToLight, surfaceNormal); - float dotSpecular = dot(reflection, normalize(-surfacePosView)); - dotSpecular = max(dotSpecular, 0.0); - float specularFactor = pow(dotSpecular, 30.0); // Specular factor - - Is = attenuation * Ls * Ks * specularFactor; - - totalLighting = totalLighting + Id + Is; - } - - fragmentColor = vec4(totalLighting, 1.0) * texel; - - - //fragmentColor = vec4(Id, 1.0) * texel; - - //fragmentColor = texel; + // G-buffer Normal + frag_Normal = vec4(Input.Normal, 0.0); } \ No newline at end of file diff --git a/src/Shaders/Fragment2.glsl b/src/Shaders/Fragment2.glsl new file mode 100644 index 0000000..cbc905e --- /dev/null +++ b/src/Shaders/Fragment2.glsl @@ -0,0 +1,19 @@ +#version 430 + +layout (binding=0) uniform sampler2D DiffuseTexture; +layout (binding=1) uniform sampler2D PositionTexture; +layout (binding=2) uniform sampler2D NormalTexture; + +in VertexData +{ + vec3 Position; + vec3 Normal; + vec2 TextureCoord; +} Input; + +out vec4 FragColor; + +void main() +{ + FragColor = texture2D(DiffuseTexture, Input.TextureCoord); +} \ No newline at end of file diff --git a/src/Shaders/Second_pass.frag.glsl b/src/Shaders/Second_pass.frag.glsl deleted file mode 100644 index 3591f9b..0000000 --- a/src/Shaders/Second_pass.frag.glsl +++ /dev/null @@ -1,32 +0,0 @@ -#version 130 - -uniform sampler2D diffuseTex; // The color information -uniform sampler2D posTex; // World position -uniform sampler2D normalTex; // Normals -uniform sampler2D blendTex; // A bitmap with colors to blend with. -uniform vec3 camera; // The coordinate of the camera -in vec2 position; // The world position -layout (location = 0) out vec4 fragColor; - -void main(void) -{ - // Load data, stored in textures, from the first stage rendering. - vec4 diffuse = texture2D(diffuseTex, position.xy); - vec4 blend = texture2D(blendTex, position.xy); - vec4 worldPos = texture2D(posTex, position.xy); - vec4 normal = texture2D(normalTex, position.xy); - // Use information about lamp coordinate (not shown here), the pixel - // coordinate (worldpos.xyz), the normal of this pixel (normal.xyz) - // to compute a lighting effect. - // Use this lighting effect to update 'diffuse' - vec4 preBlend = diffuse * lamp + specularGlare; - // manual blending, using premultiplied alpha. - fragColor = blend + preBlend*(1-blend.a); -// Some debug features. Enable any of them to get a visual representation -// of an internal buffer. -// fragColor = (normal+1)/2; -// fragColor = diffuse; -// fragColor = blend; -// fragColor = worldPos; // Scaling may be needed to range [0,1] -// fragColor = lamp*vec4(1,1,1,1); -} \ No newline at end of file diff --git a/src/Shaders/Second_pass.vert.glsl b/src/Shaders/Second_pass.vert.glsl deleted file mode 100644 index 3845ced..0000000 --- a/src/Shaders/Second_pass.vert.glsl +++ /dev/null @@ -1,9 +0,0 @@ -#version 130 -in vec4 vertex; out vec2 position; - -void main(void) -{ - gl_Position = vertex*2-1; - gl_Position.z = 0.0; - position = vertex.xy; -} \ No newline at end of file diff --git a/src/Shaders/Vertex.glsl b/src/Shaders/Vertex.glsl index 295d523..6254b60 100755 --- a/src/Shaders/Vertex.glsl +++ b/src/Shaders/Vertex.glsl @@ -1,26 +1,23 @@ #version 430 uniform mat4 MVP; -uniform mat4 DepthMVP; -layout(location = 0) in vec3 Position; -layout(location = 1) in vec3 Normal; -layout(location = 2) in vec2 TextureCoord; +layout (location = 0) in vec3 Position; +layout (location = 1) in vec3 Normal; +layout (location = 2) in vec2 TextureCoord; out VertexData { vec3 Position; vec3 Normal; vec2 TextureCoord; - vec3 ShadowCoord; } Output; void main() { gl_Position = MVP * vec4(Position, 1.0); - Output.Position = Position; + Output.Position = gl_Position.xyz; Output.Normal = Normal; Output.TextureCoord = TextureCoord; - Output.ShadowCoord = vec3(DepthMVP * vec4(Position, 1.0)); } \ No newline at end of file diff --git a/src/Shaders/Vertex2.glsl b/src/Shaders/Vertex2.glsl new file mode 100644 index 0000000..e866f94 --- /dev/null +++ b/src/Shaders/Vertex2.glsl @@ -0,0 +1,17 @@ +#version 430 + +layout (location = 0) in vec3 Position; +layout (location = 2) in vec2 TextureCoord; + +out VertexData +{ + vec3 Position; + vec2 TextureCoord; +} Output; + +void main() +{ + gl_Position = vec4(Position, 1.0); + Output.Position = Position; + Output.TextureCoord = TextureCoord; +} \ No newline at end of file diff --git a/vs11/Returngeance.psess b/vs11/Returngeance.psess new file mode 100644 index 0000000..1912c4c --- /dev/null +++ b/vs11/Returngeance.psess @@ -0,0 +1,83 @@ + + + + Returngeance.sln + Sampling + None + true + true + Timestamp + Cycles + 10000000 + 10 + 10 + + false + + + + false + 500 + + \Memory\Pages/sec + \PhysicalDisk(_Total)\Avg. Disk Queue Length + \Processor(_Total)\% Processor Time + + + + true + false + false + + false + + + false + + + + bin\Debug\Returngeance.exe + 01/01/0001 00:00:00 + true + true + false + false + false + false + false + true + false + Executable + bin\Debug\Returngeance.exe + ..\bin\Debug + + + IIS + InternetExplorer + true + false + + false + + + false + + {E8B4A2A2-882B-402A-98A7-8B4F7233C8B3}|Returngeance\Returngeance.vcxproj + Returngeance\Returngeance.vcxproj + Returngeance + + + + + Returngeance140427.vsp + + + Returngeance140427(1).vsp + + + + + :PB:{E8B4A2A2-882B-402A-98A7-8B4F7233C8B3}|Returngeance\Returngeance.vcxproj + + + \ No newline at end of file diff --git a/vs11/Returngeance.sln b/vs11/Returngeance.sln index 10ce52b..8daf9b5 100644 --- a/vs11/Returngeance.sln +++ b/vs11/Returngeance.sln @@ -39,4 +39,7 @@ Global GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection + GlobalSection(Performance) = preSolution + HasPerformanceSessions = true + EndGlobalSection EndGlobal diff --git a/vs11/Returngeance/Returngeance.vcxproj b/vs11/Returngeance/Returngeance.vcxproj index 46c68eb..f84e662 100755 --- a/vs11/Returngeance/Returngeance.vcxproj +++ b/vs11/Returngeance/Returngeance.vcxproj @@ -175,6 +175,7 @@ + @@ -188,6 +189,7 @@ + diff --git a/vs11/Returngeance/Returngeance.vcxproj.filters b/vs11/Returngeance/Returngeance.vcxproj.filters index 0186e7f..7747f2d 100755 --- a/vs11/Returngeance/Returngeance.vcxproj.filters +++ b/vs11/Returngeance/Returngeance.vcxproj.filters @@ -4,7 +4,6 @@ - Rendering\Systems @@ -50,6 +49,9 @@ + + Rendering + @@ -129,7 +131,6 @@ - Rendering\Components @@ -217,6 +218,9 @@ Util + + Rendering + @@ -264,5 +268,11 @@ Shaders + + Shaders + + + Shaders + \ No newline at end of file From a199e777bc0ee8e83e06ae84db660e32fafa75ac Mon Sep 17 00:00:00 2001 From: Tleety Date: Sun, 27 Apr 2014 03:59:41 +0200 Subject: [PATCH 05/22] Working Debug mode --- src/Renderer.cpp | 17 +++++----- src/Shaders/Fragment2-Debug.glsl | 33 +++++++++++++++++++ src/Shaders/Fragment2.glsl | 2 +- src/Shaders/Vertex.glsl | 3 +- vs11/Returngeance/Returngeance.vcxproj | 18 +--------- .../Returngeance/Returngeance.vcxproj.filters | 18 +++++++--- 6 files changed, 60 insertions(+), 31 deletions(-) create mode 100644 src/Shaders/Fragment2-Debug.glsl diff --git a/src/Renderer.cpp b/src/Renderer.cpp index 44032b0..17e3db6 100755 --- a/src/Renderer.cpp +++ b/src/Renderer.cpp @@ -119,7 +119,7 @@ void Renderer::LoadContent() m_FirstPassProgram.Link(); m_SecondPassProgram.AddShader(std::shared_ptr(new VertexShader("Shaders/Vertex2.glsl"))); - m_SecondPassProgram.AddShader(std::shared_ptr(new FragmentShader("Shaders/Fragment2.glsl"))); + m_SecondPassProgram.AddShader(std::shared_ptr(new FragmentShader("Shaders/Fragment2-Debug.glsl"))); m_SecondPassProgram.Compile(); m_SecondPassProgram.Link(); @@ -530,8 +530,8 @@ void Renderer::FrameBufferTextures() glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 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_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); //Generate and bind position texture glGenTextures(1, &m_fPositionTexture); @@ -539,8 +539,8 @@ void Renderer::FrameBufferTextures() glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, 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_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); //Generate and bind normal texture glGenTextures(1, &m_fNormalsTexture); @@ -548,8 +548,8 @@ void Renderer::FrameBufferTextures() 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_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); //Bind fb glBindFramebuffer(GL_FRAMEBUFFER, m_fb); @@ -668,7 +668,7 @@ void Renderer::DrawFBO() glBindVertexArray(m_ScreenQuad); glEnableVertexAttribArray(0); - glEnableVertexAttribArray(2); +/* glEnableVertexAttribArray(2);*/ glDrawArrays(GL_TRIANGLES, 0, 6); } @@ -747,6 +747,7 @@ void Renderer::DrawFBOScene() MVP = cameraMatrix * modelMatrix; glUniformMatrix4fv(glGetUniformLocation(m_FirstPassProgram.GetHandle(), "MVP"), 1, GL_FALSE, glm::value_ptr(MVP)); + glUniformMatrix4fv(glGetUniformLocation(m_FirstPassProgram.GetHandle(), "ModelMatrix"), 1, GL_FALSE, glm::value_ptr(modelMatrix)); glBindVertexArray(model->VAO); for (auto texGroup : model->TextureGroups) { diff --git a/src/Shaders/Fragment2-Debug.glsl b/src/Shaders/Fragment2-Debug.glsl new file mode 100644 index 0000000..dc258c8 --- /dev/null +++ b/src/Shaders/Fragment2-Debug.glsl @@ -0,0 +1,33 @@ +#version 430 + +layout (binding=0) uniform sampler2D DiffuseTexture; +layout (binding=1) uniform sampler2D PositionTexture; +layout (binding=2) uniform sampler2D NormalTexture; + +in VertexData +{ + vec3 Position; + vec3 Normal; + vec2 TextureCoord; +} Input; + +out vec4 FragColor; + +void DrawQuadrant(vec4 texel, vec2 quadrant) +{ + if (-quadrant.x * Input.Position.x < 0 && -quadrant.y * Input.Position.y < 0) + { + FragColor = texel; + } +} + +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)); + 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..42c3997 100644 --- a/src/Shaders/Fragment2.glsl +++ b/src/Shaders/Fragment2.glsl @@ -15,5 +15,5 @@ out vec4 FragColor; void main() { - FragColor = texture2D(DiffuseTexture, Input.TextureCoord); + FragColor = texture2D(NormalTexture, Input.TextureCoord); } \ No newline at end of file diff --git a/src/Shaders/Vertex.glsl b/src/Shaders/Vertex.glsl index 6254b60..aaa0857 100755 --- a/src/Shaders/Vertex.glsl +++ b/src/Shaders/Vertex.glsl @@ -1,6 +1,7 @@ #version 430 uniform mat4 MVP; +uniform mat4 ModelMatrix; layout (location = 0) in vec3 Position; layout (location = 1) in vec3 Normal; @@ -18,6 +19,6 @@ void main() gl_Position = MVP * vec4(Position, 1.0); Output.Position = gl_Position.xyz; - Output.Normal = Normal; + Output.Normal = normalize((ModelMatrix * vec4(Normal, 0.0)).xyz); Output.TextureCoord = TextureCoord; } \ No newline at end of file diff --git a/vs11/Returngeance/Returngeance.vcxproj b/vs11/Returngeance/Returngeance.vcxproj index a51c769..d8ca087 100755 --- a/vs11/Returngeance/Returngeance.vcxproj +++ b/vs11/Returngeance/Returngeance.vcxproj @@ -167,27 +167,11 @@ - - - - true - - - - - - - + - - true - - - true - diff --git a/vs11/Returngeance/Returngeance.vcxproj.filters b/vs11/Returngeance/Returngeance.vcxproj.filters index b8f30e6..1a05dbf 100755 --- a/vs11/Returngeance/Returngeance.vcxproj.filters +++ b/vs11/Returngeance/Returngeance.vcxproj.filters @@ -226,12 +226,10 @@ Physics\Components + - - Shaders - - + Shaders @@ -255,11 +253,23 @@ Shaders + + Shaders + Shaders Shaders + + Shaders + + + Shaders + + + Shaders + \ No newline at end of file From 4389f2db4c39e08210f67192b034ff4a7e4807f0 Mon Sep 17 00:00:00 2001 From: Tleety Date: Sun, 27 Apr 2014 04:59:11 +0200 Subject: [PATCH 06/22] Fixed being able to switch between Quad view and normal view. --- src/Renderer.cpp | 27 +++++++++++++++++++++++++-- src/Renderer.h | 3 +++ src/Shaders/Fragment2-Debug.glsl | 5 +++++ src/Shaders/Fragment2.glsl | 2 +- 4 files changed, 34 insertions(+), 3 deletions(-) diff --git a/src/Renderer.cpp b/src/Renderer.cpp index 17e3db6..1125dc0 100755 --- a/src/Renderer.cpp +++ b/src/Renderer.cpp @@ -119,10 +119,15 @@ void Renderer::LoadContent() m_FirstPassProgram.Link(); m_SecondPassProgram.AddShader(std::shared_ptr(new VertexShader("Shaders/Vertex2.glsl"))); - m_SecondPassProgram.AddShader(std::shared_ptr(new FragmentShader("Shaders/Fragment2-Debug.glsl"))); + m_SecondPassProgram.AddShader(std::shared_ptr(new FragmentShader("Shaders/Fragment2.glsl"))); m_SecondPassProgram.Compile(); m_SecondPassProgram.Link(); + m_SecondPassProgram_Debug.AddShader(std::shared_ptr(new VertexShader("Shaders/Vertex2.glsl"))); + m_SecondPassProgram_Debug.AddShader(std::shared_ptr(new FragmentShader("Shaders/Fragment2-Debug.glsl"))); + m_SecondPassProgram_Debug.Compile(); + m_SecondPassProgram_Debug.Link(); + m_ScreenQuad = CreateQuad(); FrameBufferTextures(); @@ -130,6 +135,16 @@ void Renderer::LoadContent() void Renderer::Draw(double dt) { + + if(glfwGetKey(m_Window, GLFW_KEY_F1)) + { + m_QuadView = false; + } + if(glfwGetKey(m_Window, GLFW_KEY_F2)) + { + m_QuadView = true; + } + glDisable(GL_BLEND); DrawFBO(); @@ -653,7 +668,14 @@ void Renderer::DrawFBO() // Draw to screen glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); - m_SecondPassProgram.Bind(); + if(!m_QuadView) + { + m_SecondPassProgram.Bind(); + } + else + { + m_SecondPassProgram_Debug.Bind(); + } glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); ////SetupDefferedStageUniforms(); // Probably what we do in FrameBufferTextures(); @@ -736,6 +758,7 @@ void Renderer::DrawFBOScene() { glm::mat4 cameraMatrix = m_Camera->ProjectionMatrix() * m_Camera->ViewMatrix(); glm::mat4 MVP; + for (auto tuple : ModelsToRender) { Model* model; diff --git a/src/Renderer.h b/src/Renderer.h index 2d5eab0..540be71 100755 --- a/src/Renderer.h +++ b/src/Renderer.h @@ -96,11 +96,14 @@ private: GLenum draw_bufs[2]; GLuint m_ScreenQuad; + bool m_QuadView; + std::shared_ptr m_Camera; ShaderProgram m_ShaderProgram; ShaderProgram m_FirstPassProgram; ShaderProgram m_SecondPassProgram; + ShaderProgram m_SecondPassProgram_Debug; ShaderProgram m_ShaderProgramNormals; ShaderProgram m_ShaderProgramShadows; ShaderProgram m_ShaderProgramShadowsDrawDepth; diff --git a/src/Shaders/Fragment2-Debug.glsl b/src/Shaders/Fragment2-Debug.glsl index dc258c8..db35d6e 100644 --- a/src/Shaders/Fragment2-Debug.glsl +++ b/src/Shaders/Fragment2-Debug.glsl @@ -23,10 +23,15 @@ void DrawQuadrant(vec4 texel, vec2 quadrant) void main() { + vec4 DiffuseTexel = texture2D(DiffuseTexture, Input.TextureCoord); + vec4 PositionTexel = texture2D(PositionTexture, Input.TextureCoord); + vec4 NormalTexel = texture2D(NormalTexture, Input.TextureCoord); + //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)); + 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 42c3997..cbc905e 100644 --- a/src/Shaders/Fragment2.glsl +++ b/src/Shaders/Fragment2.glsl @@ -15,5 +15,5 @@ out vec4 FragColor; void main() { - FragColor = texture2D(NormalTexture, Input.TextureCoord); + FragColor = texture2D(DiffuseTexture, Input.TextureCoord); } \ No newline at end of file From b1b60bce6186fd4b647cc5373d87e9b413fcdc75 Mon Sep 17 00:00:00 2001 From: Tleety Date: Wed, 7 May 2014 14:23:01 +0000 Subject: [PATCH 07/22] 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) From 62ca923821ef0ee7fab83feaea9ccd55b8464cff Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Wed, 7 May 2014 22:34:38 +0200 Subject: [PATCH 08/22] Defucked defucked rendering --- src/GameWorld.cpp | 6 +- src/Renderer.cpp | 130 +++++++++++++----- src/Renderer.h | 9 +- src/Shaders/FinalPass.frag.glsl | 22 +++ src/Shaders/FinalPass.vert.glsl | 16 +++ src/Shaders/Fragment.glsl | 2 +- src/Shaders/Fragment2-Debug.glsl | 2 +- src/Shaders/Fragment2.glsl | 106 +++++++------- src/Shaders/Vertex.glsl | 8 +- src/Shaders/Vertex2.glsl | 8 +- vs11/Returngeance.sln | 3 - vs11/Returngeance/Returngeance.vcxproj | 2 + .../Returngeance/Returngeance.vcxproj.filters | 6 + 13 files changed, 207 insertions(+), 113 deletions(-) create mode 100644 src/Shaders/FinalPass.frag.glsl create mode 100644 src/Shaders/FinalPass.vert.glsl diff --git a/src/GameWorld.cpp b/src/GameWorld.cpp index bb01e03..6e78089 100755 --- a/src/GameWorld.cpp +++ b/src/GameWorld.cpp @@ -230,8 +230,8 @@ void GameWorld::Initialize() 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->Diffuse = glm::vec3(0.5f, 0.5f, 1.0f); + light->Specular = glm::vec3(1.0f, 1.0f, 1.0f); light->specularExponent = 1.0f; auto model = AddComponent(Light, "Model"); model->ModelFile = "Models/Placeholders/PhysicsTest/PointLight.obj"; @@ -243,7 +243,7 @@ void GameWorld::Initialize() auto cube = CreateEntity(); auto transform = AddComponent(cube, "Transform"); transform->Position = glm::vec3(20, 10 + i*2, 0); - transform->Scale = glm::vec3(1); + //transform->Scale = glm::vec3(1); transform->Orientation = glm::quat(glm::vec3(0.0f, 0.0f, 0.0f)); auto model = AddComponent(cube, "Model"); model->ModelFile = "Models/Placeholders/PhysicsTest/Cube2.obj"; diff --git a/src/Renderer.cpp b/src/Renderer.cpp index bf6241b..27820a9 100755 --- a/src/Renderer.cpp +++ b/src/Renderer.cpp @@ -128,6 +128,11 @@ void Renderer::LoadContent() m_SecondPassProgram_Debug.Compile(); m_SecondPassProgram_Debug.Link(); + m_FinalPassProgram.AddShader(std::shared_ptr(new VertexShader("Shaders/FinalPass.vert.glsl"))); + m_FinalPassProgram.AddShader(std::shared_ptr(new FragmentShader("Shaders/FinalPass.frag.glsl"))); + m_FinalPassProgram.Compile(); + m_FinalPassProgram.Link(); + m_ScreenQuad = CreateQuad(); FrameBufferTextures(); @@ -532,10 +537,10 @@ void Renderer::ClearStuff() void Renderer::FrameBufferTextures() { - m_fb = 0; + m_fbBasePass = 0; m_fDepthBuffer = 0; - glGenFramebuffers(1, &m_fb); + glGenFramebuffers(1, &m_fbBasePass); glGenRenderbuffers(1, &m_fDepthBuffer); glBindRenderbuffer(GL_RENDERBUFFER, m_fDepthBuffer); @@ -562,14 +567,14 @@ void Renderer::FrameBufferTextures() //Generate and bind normal texture glGenTextures(1, &m_fNormalsTexture); glBindTexture(GL_TEXTURE_2D, m_fNormalsTexture); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, WIDTH, HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, 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); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); //Bind fb - glBindFramebuffer(GL_FRAMEBUFFER, m_fb); + glBindFramebuffer(GL_FRAMEBUFFER, m_fbBasePass); glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, m_fDepthBuffer); //Attach textures to the FB @@ -583,6 +588,29 @@ void Renderer::FrameBufferTextures() LOG_ERROR("DeferredLighting:Init: FrameBuffer incomplete: 0x%x\n", fbStatus); //exit(1); } + + m_fbLightingPass = 0; + glGenFramebuffers(1, &m_fbLightingPass); + + 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); + 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); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); + + glBindFramebuffer(GL_FRAMEBUFFER, m_fbLightingPass); + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_fLightingTexture, 0); + + fbStatus = glCheckFramebufferStatus(GL_FRAMEBUFFER); + if(fbStatus != GL_FRAMEBUFFER_COMPLETE) + { + LOG_ERROR("DeferredLighting:Init: FrameBuffer incomplete: 0x%x\n", fbStatus); + //exit(1); + } + + } //void Renderer::FrameBufferTextures() @@ -654,12 +682,15 @@ void Renderer::FrameBufferTextures() void Renderer::DrawFBO() { - glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_fb); + /* + Base pass + */ + glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_fbBasePass); // Clear G-buffer GLenum windowBuffClear[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2 }; glDrawBuffers(3, windowBuffClear); - glClearColor(0.2f, 0.2f, 0.2f, 0.0f); + glClearColor(0.f, 0.f, 0.f, 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(??) @@ -667,35 +698,54 @@ void Renderer::DrawFBO() GLenum windowBuffOpaque[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2 }; glDrawBuffers(3, windowBuffOpaque); + glCullFace(GL_BACK); DrawFBOScene(); - // Draw to screen - glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); - if(!m_QuadView) - { - m_SecondPassProgram.Bind(); - } - else - { - m_SecondPassProgram_Debug.Bind(); - } - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + /* + Lighting pass + */ + glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_fbLightingPass); + GLenum lightingPassAttachments[] = { GL_COLOR_ATTACHMENT0 }; + glDrawBuffers(1, lightingPassAttachments); - + glClearColor(0.f, 0.f, 0.f, 0.f); + glClear(GL_COLOR_BUFFER_BIT); + + m_SecondPassProgram.Bind(); glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, m_fDiffuseTexture); - - glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, m_fPositionTexture); - - glActiveTexture(GL_TEXTURE2); + glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, m_fNormalsTexture); + glCullFace(GL_FRONT); DrawLightScene(); + /* + Final pass + */ + glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + //if(!m_QuadView) + //{ + m_FinalPassProgram.Bind(); + //} + //else + //{ + // m_SecondPassProgram_Debug.Bind(); + //} + + // Ambient light + glUniform3fv(glGetUniformLocation(m_FinalPassProgram.GetHandle(), "La"), 1, glm::value_ptr(glm::vec3(0.3f, 0.3f, 0.3f))); + + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, m_fDiffuseTexture); + glActiveTexture(GL_TEXTURE1); + glBindTexture(GL_TEXTURE_2D, m_fLightingTexture); + + glCullFace(GL_BACK); glBindVertexArray(m_ScreenQuad); glEnableVertexAttribArray(0); - glEnableVertexAttribArray(2); glDrawArrays(GL_TRIANGLES, 0, 6); } @@ -774,7 +824,9 @@ void Renderer::DrawFBOScene() MVP = cameraMatrix * modelMatrix; glUniformMatrix4fv(glGetUniformLocation(m_FirstPassProgram.GetHandle(), "MVP"), 1, GL_FALSE, glm::value_ptr(MVP)); - glUniformMatrix4fv(glGetUniformLocation(m_FirstPassProgram.GetHandle(), "ModelMatrix"), 1, GL_FALSE, glm::value_ptr(modelMatrix)); + glUniformMatrix4fv(glGetUniformLocation(m_FirstPassProgram.GetHandle(), "M"), 1, GL_FALSE, glm::value_ptr(modelMatrix)); + 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())); glBindVertexArray(model->VAO); for (auto texGroup : model->TextureGroups) { @@ -792,7 +844,7 @@ 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); @@ -802,15 +854,18 @@ void Renderer::DrawLightScene() for(int i = 0; i < Lights; i++) { - glm::mat4 MVP = m_Camera->ProjectionMatrix() * m_Camera->ViewMatrix() * lM[i]; + MVP = cameraMatrix * 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())); + glUniform2fv(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "ViewportSize"), 1,glm::value_ptr(glm::vec2(WIDTH, HEIGHT))); + glUniformMatrix4fv(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "MVP"), 1, GL_FALSE, glm::value_ptr(MVP)); + glUniformMatrix4fv(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "V"), 1, GL_FALSE, glm::value_ptr(m_Camera->ViewMatrix())); + glUniformMatrix4fv(glGetUniformLocation(m_SecondPassProgram.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); + glUniform3fv(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "la"), 1, glm::value_ptr(glm::vec3(0.3f, 0.3f, 0.3f))); + glUniform3fv(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "ls"), 1, glm::value_ptr(Light_specular[i])); + glUniform3fv(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "ld"), 1, glm::value_ptr(Light_diffuse[i])); + glUniform3fv(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "lp"), 1, glm::value_ptr(Light_position[i])); + glUniform1f(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "LightRadius"), 5.0f); 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()); @@ -829,10 +884,11 @@ 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]; + const float scale = 10.0f; + glm::mat4 model; + model *= glm::translate(Light_position[i]); + model *= glm::scale(glm::vec3(scale)); + lM[i] = model; } } diff --git a/src/Renderer.h b/src/Renderer.h index 1c73d49..6754701 100755 --- a/src/Renderer.h +++ b/src/Renderer.h @@ -91,11 +91,14 @@ private: GLuint m_ShadowFrameBuffer; GLuint m_ShadowDepthTexture; + GLuint m_fbBasePass; GLuint m_fDiffuseTexture; GLuint m_fPositionTexture; GLuint m_fNormalsTexture; GLuint m_fBlendTexture; - GLuint m_fb; + GLuint m_fbLightingPass; + GLuint m_fLightingTexture; + GLuint m_fDepthBuffer; GLenum draw_bufs[2]; glm::mat4 lM[5]; @@ -110,12 +113,16 @@ private: ShaderProgram m_FirstPassProgram; ShaderProgram m_SecondPassProgram; ShaderProgram m_SecondPassProgram_Debug; + ShaderProgram m_FinalPassProgram; + ShaderProgram m_ShaderProgramNormals; ShaderProgram m_ShaderProgramShadows; ShaderProgram m_ShaderProgramShadowsDrawDepth; ShaderProgram m_ShaderProgramDebugAABB; ShaderProgram m_ShaderProgramSkybox; + + void ClearStuff(); void DrawScene(); void DrawModels(ShaderProgram &shader); diff --git a/src/Shaders/FinalPass.frag.glsl b/src/Shaders/FinalPass.frag.glsl new file mode 100644 index 0000000..8509ea8 --- /dev/null +++ b/src/Shaders/FinalPass.frag.glsl @@ -0,0 +1,22 @@ +#version 430 + +uniform vec3 La; + +layout (binding=0) uniform sampler2D DiffuseTexture; +layout (binding=1) uniform sampler2D LightingTexture; + +in VertexData +{ + vec3 Position; + vec2 TextureCoord; +} Input; + +out vec4 FragmentColor; + +void main() +{ + vec4 DiffuseTexel = texture(DiffuseTexture, Input.TextureCoord); + vec4 LightingTexel = texture(LightingTexture, Input.TextureCoord); + + FragmentColor = DiffuseTexel * vec4(La, 1.0) + LightingTexel; +} \ No newline at end of file diff --git a/src/Shaders/FinalPass.vert.glsl b/src/Shaders/FinalPass.vert.glsl new file mode 100644 index 0000000..05deece --- /dev/null +++ b/src/Shaders/FinalPass.vert.glsl @@ -0,0 +1,16 @@ +#version 430 + +layout(location = 0) in vec3 Position; + +out VertexData +{ + vec3 Position; + vec2 TextureCoord; +} Output; + +void main() +{ + gl_Position = vec4(Position, 1.0); + Output.Position = Position; + Output.TextureCoord = (vec2(Position) + 1) / 2; +} \ No newline at end of file diff --git a/src/Shaders/Fragment.glsl b/src/Shaders/Fragment.glsl index 7aa72d7..64d4a59 100755 --- a/src/Shaders/Fragment.glsl +++ b/src/Shaders/Fragment.glsl @@ -19,7 +19,7 @@ void main() frag_Diffuse = texture2D(DiffuseTexture, Input.TextureCoord); // G-buffer Position - frag_Position = vec4(Input.Position.xy, 0.0, 0.0); + frag_Position = vec4(Input.Position.xyz, 0.0); // G-buffer Normal frag_Normal = vec4(Input.Normal, 0.0); diff --git a/src/Shaders/Fragment2-Debug.glsl b/src/Shaders/Fragment2-Debug.glsl index 0ab00d4..db35d6e 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)+1)/2, vec2(-1, -1)); + DrawQuadrant(texture2D(NormalTexture, Input.TextureCoord * 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 aff7665..a27a268 100644 --- a/src/Shaders/Fragment2.glsl +++ b/src/Shaders/Fragment2.glsl @@ -1,94 +1,80 @@ #version 430 -layout (binding=0) uniform sampler2D DiffuseTexture; -layout (binding=1) uniform sampler2D PositionTexture; -layout (binding=2) uniform sampler2D NormalTexture; +layout (binding=0) uniform sampler2D PositionTexture; +layout (binding=1) uniform sampler2D NormalsTexture; +uniform vec2 ViewportSize; uniform mat4 MVP; uniform mat4 M; uniform mat4 V; uniform mat4 P; +uniform vec3 la; uniform vec3 ls; uniform vec3 ld; -uniform vec3 lp; -const float specularExponent = 20.0; +uniform vec3 lp; +uniform float LightRadius; +const float specularExponent = 50.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 vec3 ks = vec3(1.0, 0.0, 0.0); +const vec3 kd = vec3(0.8, 0.8, 0.8); +const vec3 ka = vec3(1.0, 1.0, 1.0); const float kshine = 1.0; in VertexData { vec3 Position; - vec3 Normal; vec2 TextureCoord; } Input; out vec4 FragColor; -vec3 phong (vec3 PositionTexel, vec3 NormalTexel) +vec4 phong4(vec3 position, vec3 normal) { - vec3 lightPosition = vec3( M * vec4( lp, 1.0 ) ); - vec3 distToLight = lightPosition - PositionTexel; - vec3 directionToLight = normalize(distToLight); + // Diffuse + vec3 lightPos = vec3(V * vec4(lp, 1.0)); + vec3 distanceToLight = lightPos - position; + vec3 directionToLight = normalize(distanceToLight); + float dotProd = dot(directionToLight, normal); + dotProd = max(dotProd, 0.0); + vec3 Id = kd * ld * dotProd; - //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 + // Specular + //vec3 reflection = reflect(-directionToLight, normal); + vec3 surfaceToViewer = normalize(-position); + vec3 halfWay = normalize(surfaceToViewer + directionToLight); + float dotSpecular = max(dot(halfWay, normal), 0.0); + float specularFactor = pow(dotSpecular, specularExponent * 2); + vec3 Is = ks * ls * specularFactor; //Attenuation - float dist2D = distance(lightPosition, PositionTexel); - float attenuationFactor = -log(min(1.0, dist2D / 5.0)); + float dist = distance(lightPos, position); + float attenuation = -log(min(1.0, dist / LightRadius)); + //float attenuation = 1.0 / (1.0 - 0.0001 * pow(dist, 2)); - //vec3 FinalOut = (Id + Is) * attenuationFactor; - vec3 FinalOut = Id + Is; - return FinalOut; -} + //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); -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; + return vec4((Id + Is) * attenuation, 1.0); } void main() { - vec4 DiffuseTexel = texture2D(DiffuseTexture, Input.TextureCoord); - vec4 PositionTexel = texture2D(PositionTexture, Input.TextureCoord); - vec4 NormalTexel = texture2D(NormalTexture, Input.TextureCoord); + vec2 TextureCoord = gl_FragCoord.xy / ViewportSize; + vec4 PositionTexel = texture(PositionTexture, TextureCoord); + vec4 NormalTexel = texture(NormalsTexture, TextureCoord); - vec4 Frag_color; - Frag_color.rgb = phong((MVP * PositionTexel).rgb, normalize(NormalTexel).rgb); - Frag_color.a = 1.0; - FragColor = Frag_color * DiffuseTexel; + FragColor = phong4(vec3(PositionTexel), vec3(NormalTexel)); } \ No newline at end of file diff --git a/src/Shaders/Vertex.glsl b/src/Shaders/Vertex.glsl index aaa0857..ea38b24 100755 --- a/src/Shaders/Vertex.glsl +++ b/src/Shaders/Vertex.glsl @@ -1,7 +1,9 @@ #version 430 uniform mat4 MVP; -uniform mat4 ModelMatrix; +uniform mat4 M; +uniform mat4 V; +uniform mat4 P; layout (location = 0) in vec3 Position; layout (location = 1) in vec3 Normal; @@ -18,7 +20,7 @@ void main() { gl_Position = MVP * vec4(Position, 1.0); - Output.Position = gl_Position.xyz; - Output.Normal = normalize((ModelMatrix * vec4(Normal, 0.0)).xyz); + Output.Position = vec3(V * M * vec4(Position, 1.0)); + Output.Normal = normalize(vec3(inverse(transpose(V * M)) * vec4(Normal, 0.0))); Output.TextureCoord = TextureCoord; } \ No newline at end of file diff --git a/src/Shaders/Vertex2.glsl b/src/Shaders/Vertex2.glsl index f341840..3156821 100644 --- a/src/Shaders/Vertex2.glsl +++ b/src/Shaders/Vertex2.glsl @@ -1,10 +1,10 @@ #version 430 +uniform mat4 MVP; + layout (location = 0) in vec3 Position; layout (location = 2) in vec2 TextureCoord; - - out VertexData { vec3 Position; @@ -13,7 +13,7 @@ out VertexData void main() { - gl_Position = vec4(Position, 1.0); + gl_Position = MVP * vec4(Position, 1.0); Output.Position = Position; - Output.TextureCoord = TextureCoord; + Output.TextureCoord = (vec2(Position) + 1) / 2; } \ No newline at end of file diff --git a/vs11/Returngeance.sln b/vs11/Returngeance.sln index 8daf9b5..10ce52b 100644 --- a/vs11/Returngeance.sln +++ b/vs11/Returngeance.sln @@ -39,7 +39,4 @@ Global GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection - GlobalSection(Performance) = preSolution - HasPerformanceSessions = true - EndGlobalSection EndGlobal diff --git a/vs11/Returngeance/Returngeance.vcxproj b/vs11/Returngeance/Returngeance.vcxproj index d8ca087..b079859 100755 --- a/vs11/Returngeance/Returngeance.vcxproj +++ b/vs11/Returngeance/Returngeance.vcxproj @@ -167,6 +167,8 @@ + + diff --git a/vs11/Returngeance/Returngeance.vcxproj.filters b/vs11/Returngeance/Returngeance.vcxproj.filters index 1a05dbf..e416604 100755 --- a/vs11/Returngeance/Returngeance.vcxproj.filters +++ b/vs11/Returngeance/Returngeance.vcxproj.filters @@ -271,5 +271,11 @@ Shaders + + Shaders + + + Shaders + \ No newline at end of file From 1268b3d8fd7a5a4176686a88f96e19270cedf2d5 Mon Sep 17 00:00:00 2001 From: Tleety Date: Sat, 10 May 2014 17:44:55 +0200 Subject: [PATCH 09/22] Removed Unused code in renderer --- src/GameWorld.cpp | 2 +- src/Renderer.cpp | 137 +------------------------------------ src/Shaders/Fragment2.glsl | 4 +- 3 files changed, 5 insertions(+), 138 deletions(-) diff --git a/src/GameWorld.cpp b/src/GameWorld.cpp index 6e78089..3002e6b 100755 --- a/src/GameWorld.cpp +++ b/src/GameWorld.cpp @@ -224,7 +224,7 @@ void GameWorld::Initialize() CommitEntity(car); } */ - for(int i = 0; i < 5; i++) + for(int i = 0; i < 6; i++) { auto Light = CreateEntity(); auto transform = AddComponent(Light, "Transform"); diff --git a/src/Renderer.cpp b/src/Renderer.cpp index 27820a9..64a0064 100755 --- a/src/Renderer.cpp +++ b/src/Renderer.cpp @@ -613,73 +613,6 @@ void Renderer::FrameBufferTextures() } -//void Renderer::FrameBufferTextures() -//{ -// m_fb = 0; -// m_fDepthBuffer = 0; -// -// glGenFramebuffers(1, &m_fb); -// glGenRenderbuffers(1, &m_fDepthBuffer); -// -// glBindRenderbuffer(GL_RENDERBUFFER, m_fDepthBuffer); -// glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, WIDTH, HEIGHT); -// -// //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); -// 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_CLAMP_TO_EDGE); -// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); -// -// //Generate and bind position texture -// glGenTextures(1, &m_fPositionTexture); -// glBindTexture(GL_TEXTURE_2D, m_fPositionTexture); -// glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, 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_CLAMP_TO_EDGE); -// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); -// -// //Generate and bind normal texture -// glGenTextures(1, &m_fNormalsTexture); -// glBindTexture(GL_TEXTURE_2D, m_fNormalsTexture); -// 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_CLAMP_TO_EDGE); -// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); -// -// //Generate and bind blend texture -// glGenTextures(1, &m_fBlendTexture); -// glBindTexture(GL_TEXTURE_2D, m_fBlendTexture); -// glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 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_CLAMP_TO_EDGE); -// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); -// -// //Bind fb -// glBindFramebuffer(GL_FRAMEBUFFER, m_fb); -// glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, m_fDepthBuffer); -// -// //Attach textures to the FB -// glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_fDiffuseTexture, 0); -// glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, m_fPositionTexture, 0); -// glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, GL_TEXTURE_2D, m_fNormalsTexture, 0); -// glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT3, GL_TEXTURE_2D, m_fBlendTexture, 0); -// -// GLenum fbStatus = glCheckFramebufferStatus(GL_FRAMEBUFFER); -// if(fbStatus != GL_FRAMEBUFFER_COMPLETE) -// { -// printf("DeferredLighting:Init: FrameBuffer incomplete: 0x%x\n", fbStatus); -// exit(1); -// } -// -// glBindFramebuffer(GL_FRAMEBUFFER, 0); -//} - void Renderer::DrawFBO() { /* @@ -725,15 +658,8 @@ void Renderer::DrawFBO() */ glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - - //if(!m_QuadView) - //{ - m_FinalPassProgram.Bind(); - //} - //else - //{ - // m_SecondPassProgram_Debug.Bind(); - //} + + m_FinalPassProgram.Bind(); // Ambient light glUniform3fv(glGetUniformLocation(m_FinalPassProgram.GetHandle(), "La"), 1, glm::value_ptr(glm::vec3(0.3f, 0.3f, 0.3f))); @@ -749,65 +675,6 @@ void Renderer::DrawFBO() glDrawArrays(GL_TRIANGLES, 0, 6); } -//void Renderer::DrawFBO() -//{ -// glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_fb); -// -// GLenum windowBuffClear[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_COLOR_ATTACHMENT3 }; -// glDrawBuffers(4, windowBuffClear); -// glClearColor(0.0f, 0.0f, 0.0f, 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(??) -// //EnableRenderProgramStage1; -// m_FirstPassProgram.Bind(); -// GLenum windowBuffOpaque[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_NONE }; -// glDrawBuffers(4, windowBuffOpaque); -// DrawFBOScene(); -// -// GLenum windowBuffTransp[] = { GL_NONE, GL_NONE, GL_NONE, GL_COLOR_ATTACHMENT3 }; -// glDrawBuffers(4, windowBuffTransp); -// glEnable(GL_BLEND); -// glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); -// //Depth buffer shall not be updated -// glDepthMask(GL_FALSE); -// //DrawTransparent items -// glDepthMask(GL_TRUE); -// 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(); -// m_SecondPassProgram.Bind(); -// glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); -// //SetupDefferedStageUniforms(); // Probably what we do in FrameBufferTextures(); -// glEnableVertexAttribArray(0); -// glActiveTexture(GL_TEXTURE0); -// glBindTexture(GL_TEXTURE_2D, m_fDiffuseTexture); -// -// glActiveTexture(GL_TEXTURE1); -// glBindTexture(GL_TEXTURE_2D, m_fPositionTexture); -// -// glActiveTexture(GL_TEXTURE2); -// glBindTexture(GL_TEXTURE_2D, m_fNormalsTexture); -// -// glActiveTexture(GL_TEXTURE3); -// glBindTexture(GL_TEXTURE_2D, m_fBlendTexture); -// -// -// -// -// -// -// -// //DrawSimpleSquare(); //I guess this draw a square and put the textures on it -// glBindVertexArray(m_ScreenQuad); -// glDrawArrays(GL_TRIANGLES, 0, 6); -// glDisableVertexAttribArray(0); -// -//} - void Renderer::DrawFBOScene() { glm::mat4 cameraMatrix = m_Camera->ProjectionMatrix() * m_Camera->ViewMatrix(); diff --git a/src/Shaders/Fragment2.glsl b/src/Shaders/Fragment2.glsl index a27a268..4dde49f 100644 --- a/src/Shaders/Fragment2.glsl +++ b/src/Shaders/Fragment2.glsl @@ -29,7 +29,7 @@ in VertexData out vec4 FragColor; -vec4 phong4(vec3 position, vec3 normal) +vec4 phong(vec3 position, vec3 normal) { // Diffuse vec3 lightPos = vec3(V * vec4(lp, 1.0)); @@ -76,5 +76,5 @@ void main() vec4 PositionTexel = texture(PositionTexture, TextureCoord); vec4 NormalTexel = texture(NormalsTexture, TextureCoord); - FragColor = phong4(vec3(PositionTexel), vec3(NormalTexel)); + FragColor = phong(vec3(PositionTexel), vec3(NormalTexel)); } \ No newline at end of file From d00ff3c2ed448555f0624ef45365b7222c195a4f Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Sat, 10 May 2014 19:13:41 +0200 Subject: [PATCH 10/22] Assets now as Git submodule! (cherry picked from commit 6e6a115ac3ad18184b6bb8d5e0b87ade9e0cd39f) --- .gitignore | 1 - .gitmodules | 4 ++++ assets | 1 + 3 files changed, 5 insertions(+), 1 deletion(-) create mode 100644 .gitmodules create mode 160000 assets diff --git a/.gitignore b/.gitignore index 3b5dc2e..5982b10 100755 --- a/.gitignore +++ b/.gitignore @@ -31,5 +31,4 @@ ipch/ Ankh.NoLoad *.orig -assets/ !libs/*.lib \ No newline at end of file diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..185a1f9 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,4 @@ +[submodule "assets"] + path = assets + url = returngeance@shard.imon.nu:Assets + branch = master diff --git a/assets b/assets new file mode 160000 index 0000000..672e8a2 --- /dev/null +++ b/assets @@ -0,0 +1 @@ +Subproject commit 672e8a2b11ecaafc95a5b0286b5ec310c62438ad From c1c30b00197c8a692b8b0ab4f63abbd921e6c393 Mon Sep 17 00:00:00 2001 From: Tleety Date: Sat, 10 May 2014 19:51:48 +0200 Subject: [PATCH 11/22] Fixed lights and added a bad gamma correction --- src/Components/PointLight.h | 11 +++- src/GameWorld.cpp | 8 +-- src/Renderer.cpp | 100 ++++++++++++++------------------ src/Renderer.h | 29 +++++---- src/Shaders/FinalPass.frag.glsl | 4 +- src/Shaders/Fragment2.glsl | 6 +- src/Systems/RenderSystem.cpp | 3 +- 7 files changed, 81 insertions(+), 80 deletions(-) diff --git a/src/Components/PointLight.h b/src/Components/PointLight.h index 29da989..ef6eec5 100755 --- a/src/Components/PointLight.h +++ b/src/Components/PointLight.h @@ -9,15 +9,20 @@ namespace Components struct PointLight : Component { - float Intensity; - float MaxRange; + PointLight() + : Specular(1.0f, 1.0f, 1.0f) + , Diffuse(0.4f, 0.4f, 0.4f) + , specularExponent(50.0f) + , Scale(10.0f) + { } + float constantAttenuation, linearAttenuation, quadraticAttenuation; - float spotExponent; Color color; glm::vec3 Specular; glm::vec3 Diffuse; float specularExponent; + float Scale; }; } diff --git a/src/GameWorld.cpp b/src/GameWorld.cpp index 3002e6b..f703c76 100755 --- a/src/GameWorld.cpp +++ b/src/GameWorld.cpp @@ -224,15 +224,13 @@ void GameWorld::Initialize() CommitEntity(car); } */ - for(int i = 0; i < 6; i++) + for(int i = 0; i < 20; i++) { auto Light = CreateEntity(); auto transform = AddComponent(Light, "Transform"); - transform->Position = glm::vec3(20+ 10*cos(i), 3, 0 + 10*sin(i)); + transform->Position = glm::vec3(i*cos(i), 3, 0 + i*sin(i)); auto light = AddComponent(Light, "PointLight"); - light->Diffuse = glm::vec3(0.5f, 0.5f, 1.0f); - light->Specular = glm::vec3(1.0f, 1.0f, 1.0f); - light->specularExponent = 1.0f; + //light->Specular = glm::vec3(1.0f, 1.0f, 1.0f); auto model = AddComponent(Light, "Model"); model->ModelFile = "Models/Placeholders/PhysicsTest/PointLight.obj"; } diff --git a/src/Renderer.cpp b/src/Renderer.cpp index 8e6c999..82ac29a 100755 --- a/src/Renderer.cpp +++ b/src/Renderer.cpp @@ -18,7 +18,7 @@ Renderer::Renderer() m_SunPosition = glm::vec3(0, 3.5f, 10); m_SunTarget = glm::vec3(0, 0, 0); m_SunProjection = glm::ortho(-100, 100, -100, 100, -100, 100); - Lights = 0; +/* Lights = 0;*/ } void Renderer::Initialize() @@ -132,7 +132,7 @@ void Renderer::LoadContent() m_FinalPassProgram.AddShader(std::shared_ptr(new FragmentShader("Shaders/FinalPass.frag.glsl"))); m_FinalPassProgram.Compile(); m_FinalPassProgram.Link(); - + Gamma = 1; m_ScreenQuad = CreateQuad(); FrameBufferTextures(); @@ -150,6 +150,17 @@ void Renderer::Draw(double dt) m_QuadView = true; } + if(glfwGetKey(m_Window, GLFW_KEY_KP_1)) + { + Gamma -= 0.3f * dt; + LOG_INFO("Gamma_UP: %f", Gamma); + } + if(glfwGetKey(m_Window, GLFW_KEY_KP_4)) + { + Gamma += 0.3f * dt; + LOG_INFO("Gamma_DOWN: %f", Gamma); + } + glDisable(GL_BLEND); DrawFBO(); @@ -200,14 +211,14 @@ void Renderer::DrawScene() ); m_ShaderProgram.Bind(); - glUniform1i(glGetUniformLocation(m_ShaderProgram.GetHandle(), "numberOfLights"), Lights); + glUniform1i(glGetUniformLocation(m_ShaderProgram.GetHandle(), "numberOfLights"), Lights.size()); // 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()); - glUniform1fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "spotExponent"), Lights, Light_spotExponent.data()); + glUniform1fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "constantAttenuation"), Lights.size(), Light_constantAttenuation.data()); + glUniform1fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "linearAttenuation"), Lights.size(), Light_linearAttenuation.data()); + glUniform1fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "quadraticAttenuation"), Lights.size(), Light_quadraticAttenuation.data()); + glUniform1fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "spotExponent"), Lights.size(), Light_spotExponent.data()); if (m_DrawWireframe) { glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); @@ -361,30 +372,18 @@ void Renderer::AddPointLightToDraw( glm::vec3 _position, glm::vec3 _specular, glm::vec3 _diffuse, - float _specularExponent + float _specularExponent, + float _scale ) { - 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(); + Light light; + light.Position = _position; + light.Diffuse = _diffuse; + light.Specular = _specular; + light.Scale = _scale; + light.SpecularExponent = _specularExponent; + light.SphereModelMatrix = CreateLightMatrix(light); + Lights.push_back(light); } void Renderer::AddAABBToDraw(glm::vec3 origin, glm::vec3 volumeVector, bool colliding) @@ -522,15 +521,7 @@ void Renderer::ClearStuff() { AABBsToRender.clear(); ModelsToRender.clear(); - Light_position.clear(); - Light_specular.clear(); - Light_diffuse.clear(); - Light_constantAttenuation.clear(); - Light_linearAttenuation.clear(); - Light_quadraticAttenuation.clear(); - Light_spotExponent.clear(); - Light_specularExponent.clear(); - Lights = 0; + Lights.clear(); } #pragma endregion @@ -663,6 +654,7 @@ void Renderer::DrawFBO() // Ambient light glUniform3fv(glGetUniformLocation(m_FinalPassProgram.GetHandle(), "La"), 1, glm::value_ptr(glm::vec3(0.3f, 0.3f, 0.3f))); + glUniform1f(glGetUniformLocation(m_FinalPassProgram.GetHandle(), "Gamma"), Gamma); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, m_fDiffuseTexture); @@ -719,22 +711,21 @@ void Renderer::DrawLightScene() glm::mat4 cameraMatrix = m_Camera->ProjectionMatrix() * m_Camera->ViewMatrix(); glm::mat4 MVP; - for(int i = 0; i < Lights; i++) + for (auto &light : Lights) { - MVP = cameraMatrix * lM[i]; + MVP = cameraMatrix * light.SphereModelMatrix; glUniform2fv(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "ViewportSize"), 1,glm::value_ptr(glm::vec2(WIDTH, HEIGHT))); glUniformMatrix4fv(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "MVP"), 1, GL_FALSE, glm::value_ptr(MVP)); glUniformMatrix4fv(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "V"), 1, GL_FALSE, glm::value_ptr(m_Camera->ViewMatrix())); glUniformMatrix4fv(glGetUniformLocation(m_SecondPassProgram.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])); - glUniform3fv(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "la"), 1, glm::value_ptr(glm::vec3(0.3f, 0.3f, 0.3f))); - glUniform3fv(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "ls"), 1, glm::value_ptr(Light_specular[i])); - glUniform3fv(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "ld"), 1, glm::value_ptr(Light_diffuse[i])); - glUniform3fv(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "lp"), 1, glm::value_ptr(Light_position[i])); - glUniform1f(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "LightRadius"), 5.0f); + glUniformMatrix4fv(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "M"), 1, GL_FALSE, glm::value_ptr(light.SphereModelMatrix)); + glUniform3fv(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "ls"), 1, glm::value_ptr(light.Specular)); + glUniform3fv(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "ld"), 1, glm::value_ptr(light.Diffuse)); + glUniform3fv(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "lp"), 1, glm::value_ptr(light.Position)); + glUniform1f(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "LightRadius"), light.Scale/2.f); 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]); + glUniform1f(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "specularExponent"), light.SpecularExponent); glDrawArrays(GL_TRIANGLES, 0, m_sphereModel->Vertices.size()); }; glEnable (GL_DEPTH_TEST); @@ -747,16 +738,11 @@ void Renderer::SetSphereModel( Model* _model ) m_sphereModel = _model; } -void Renderer::CreateLightMatrix() +glm::mat4 Renderer::CreateLightMatrix(Light &_light) { - for(int i = 0; i < Lights; i++) - { - const float scale = 10.0f; - glm::mat4 model; - model *= glm::translate(Light_position[i]); - model *= glm::scale(glm::vec3(scale)); - lM[i] = model; - } - + glm::mat4 model; + model *= glm::translate(_light.Position); + model *= glm::scale(glm::vec3(_light.Scale)); + return model; } diff --git a/src/Renderer.h b/src/Renderer.h index 6754701..0a8bc12 100755 --- a/src/Renderer.h +++ b/src/Renderer.h @@ -24,13 +24,7 @@ public: int HEIGHT, WIDTH; std::list> ModelsToRender; - int Lights; - 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; @@ -50,7 +44,8 @@ public: glm::vec3 _position, glm::vec3 _specular, glm::vec3 _diffuse, - float _specularExponent + float _specularExponent, + float _scale ); void AddAABBToDraw(glm::vec3 origin, glm::vec3 volumeVector, bool colliding); @@ -72,6 +67,21 @@ public: private: + + struct Light + { + glm::vec3 Position; + glm::vec3 Specular; + glm::vec3 Diffuse; + float SpecularExponent; + float Scale; + glm::mat4 SphereModelMatrix; + }; + + float Gamma; + + std::list Lights; + GLFWwindow* m_Window; GLint m_glVersion[2]; GLchar* m_glVendor; @@ -101,7 +111,6 @@ private: GLuint m_fDepthBuffer; GLenum draw_bufs[2]; - glm::mat4 lM[5]; GLuint m_ScreenQuad; Model* m_sphereModel; @@ -133,7 +142,7 @@ private: void DrawFBOScene(); void DrawLightScene(); void BindFragDataLocation(); - void CreateLightMatrix(); + glm::mat4 CreateLightMatrix(Light &_light); GLuint CreateQuad(); void DrawDebugShadowMap(); diff --git a/src/Shaders/FinalPass.frag.glsl b/src/Shaders/FinalPass.frag.glsl index 8509ea8..c6b9257 100644 --- a/src/Shaders/FinalPass.frag.glsl +++ b/src/Shaders/FinalPass.frag.glsl @@ -1,6 +1,7 @@ #version 430 uniform vec3 La; +uniform float Gamma; layout (binding=0) uniform sampler2D DiffuseTexture; layout (binding=1) uniform sampler2D LightingTexture; @@ -18,5 +19,6 @@ void main() vec4 DiffuseTexel = texture(DiffuseTexture, Input.TextureCoord); vec4 LightingTexel = texture(LightingTexture, Input.TextureCoord); - FragmentColor = DiffuseTexel * vec4(La, 1.0) + LightingTexel; + vec4 _FragmentColor = DiffuseTexel * vec4(La, 1.0) + LightingTexel; + FragmentColor = vec4(pow(_FragmentColor.rgb, vec3(1.0 / Gamma)), _FragmentColor.a); } \ No newline at end of file diff --git a/src/Shaders/Fragment2.glsl b/src/Shaders/Fragment2.glsl index 4dde49f..2467d35 100644 --- a/src/Shaders/Fragment2.glsl +++ b/src/Shaders/Fragment2.glsl @@ -13,11 +13,11 @@ uniform vec3 ls; uniform vec3 ld; uniform vec3 lp; uniform float LightRadius; -const float specularExponent = 50.0; +uniform float specularExponent; uniform vec3 CameraPosition; -const vec3 ks = vec3(1.0, 0.0, 0.0); -const vec3 kd = vec3(0.8, 0.8, 0.8); +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; diff --git a/src/Systems/RenderSystem.cpp b/src/Systems/RenderSystem.cpp index 2dc4116..47bc389 100755 --- a/src/Systems/RenderSystem.cpp +++ b/src/Systems/RenderSystem.cpp @@ -38,7 +38,8 @@ void Systems::RenderSystem::UpdateEntity(double dt, EntityID entity, EntityID pa position, pointLightComponent->Specular, pointLightComponent->Diffuse, - pointLightComponent->specularExponent + pointLightComponent->specularExponent, + pointLightComponent->Scale ); } From 22d855a521fd170174fc4b7b28504503d63e622c Mon Sep 17 00:00:00 2001 From: Tleety Date: Sun, 11 May 2014 00:02:34 +0200 Subject: [PATCH 12/22] Fixed attenuation for lights. --- src/Components/PointLight.h | 6 ++++-- src/GameWorld.cpp | 4 ++-- src/Renderer.cpp | 30 +++++++++++++++++++++--------- src/Renderer.h | 12 ++++-------- src/Shaders/Fragment.glsl | 4 ++-- src/Shaders/Fragment2.glsl | 19 +++++++++++++------ src/Shaders/Vertex2.glsl | 2 +- src/Systems/RenderSystem.cpp | 4 +++- 8 files changed, 50 insertions(+), 31 deletions(-) diff --git a/src/Components/PointLight.h b/src/Components/PointLight.h index ef6eec5..646ea01 100755 --- a/src/Components/PointLight.h +++ b/src/Components/PointLight.h @@ -13,10 +13,12 @@ struct PointLight : Component : Specular(1.0f, 1.0f, 1.0f) , Diffuse(0.4f, 0.4f, 0.4f) , specularExponent(50.0f) - , Scale(10.0f) + , ConstantAttenuation(1.05f) + , LinearAttenuation(0.f) + , QuadraticAttenuation(2.55f) { } - float constantAttenuation, linearAttenuation, quadraticAttenuation; + float ConstantAttenuation, LinearAttenuation, QuadraticAttenuation; Color color; glm::vec3 Specular; diff --git a/src/GameWorld.cpp b/src/GameWorld.cpp index f703c76..679a71b 100755 --- a/src/GameWorld.cpp +++ b/src/GameWorld.cpp @@ -224,11 +224,11 @@ void GameWorld::Initialize() CommitEntity(car); } */ - for(int i = 0; i < 20; i++) + for(int i = 0; i < 50; i++) { auto Light = CreateEntity(); auto transform = AddComponent(Light, "Transform"); - transform->Position = glm::vec3(i*cos(i), 3, 0 + i*sin(i)); + transform->Position = glm::vec3(i*cos(i), (float)(0.1*i), 0 + i*sin(i)); auto light = AddComponent(Light, "PointLight"); //light->Specular = glm::vec3(1.0f, 1.0f, 1.0f); auto model = AddComponent(Light, "Model"); diff --git a/src/Renderer.cpp b/src/Renderer.cpp index 82ac29a..b8204c7 100755 --- a/src/Renderer.cpp +++ b/src/Renderer.cpp @@ -132,7 +132,7 @@ void Renderer::LoadContent() m_FinalPassProgram.AddShader(std::shared_ptr(new FragmentShader("Shaders/FinalPass.frag.glsl"))); m_FinalPassProgram.Compile(); m_FinalPassProgram.Link(); - Gamma = 1; + Gamma = 2.2f; m_ScreenQuad = CreateQuad(); FrameBufferTextures(); @@ -215,10 +215,10 @@ void Renderer::DrawScene() // 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.size(), Light_constantAttenuation.data()); - glUniform1fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "linearAttenuation"), Lights.size(), Light_linearAttenuation.data()); - glUniform1fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "quadraticAttenuation"), Lights.size(), Light_quadraticAttenuation.data()); - glUniform1fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "spotExponent"), Lights.size(), Light_spotExponent.data()); +// glUniform1fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "constantAttenuation"), Lights.size(), Light_constantAttenuation.data()); +// glUniform1fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "linearAttenuation"), Lights.size(), Light_linearAttenuation.data()); +// glUniform1fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "quadraticAttenuation"), Lights.size(), Light_quadraticAttenuation.data()); +// glUniform1fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "spotExponent"), Lights.size(), Light_spotExponent.data()); if (m_DrawWireframe) { glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); @@ -373,15 +373,19 @@ void Renderer::AddPointLightToDraw( glm::vec3 _specular, glm::vec3 _diffuse, float _specularExponent, - float _scale + float _ConstantAttenuation, + float _LinearAttenuation, + float _QuadraticAttenuation ) { Light light; light.Position = _position; light.Diffuse = _diffuse; light.Specular = _specular; - light.Scale = _scale; light.SpecularExponent = _specularExponent; + light.ConstantAttenuation = _ConstantAttenuation; + light.LinearAttenuation = _LinearAttenuation; + light.QuadraticAttenuation = _QuadraticAttenuation; light.SphereModelMatrix = CreateLightMatrix(light); Lights.push_back(light); } @@ -723,9 +727,12 @@ void Renderer::DrawLightScene() glUniform3fv(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "ls"), 1, glm::value_ptr(light.Specular)); glUniform3fv(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "ld"), 1, glm::value_ptr(light.Diffuse)); glUniform3fv(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "lp"), 1, glm::value_ptr(light.Position)); - glUniform1f(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "LightRadius"), light.Scale/2.f); glUniform3f(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "CameraPosition"), m_Camera->Position().x, m_Camera->Position().y, m_Camera->Position().z); glUniform1f(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "specularExponent"), light.SpecularExponent); + glUniform1f(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "ConstantAttenuation"), light.ConstantAttenuation); + glUniform1f(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "LinearAttenuation"), light.LinearAttenuation); + glUniform1f(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "QuadraticAttenuation"), light.QuadraticAttenuation); + glDrawArrays(GL_TRIANGLES, 0, m_sphereModel->Vertices.size()); }; glEnable (GL_DEPTH_TEST); @@ -740,9 +747,14 @@ void Renderer::SetSphereModel( Model* _model ) glm::mat4 Renderer::CreateLightMatrix(Light &_light) { + float c = _light.ConstantAttenuation; + float l = _light.LinearAttenuation; + float q = _light.QuadraticAttenuation; + 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(_light.Scale)); + model *= glm::scale(glm::vec3(cutOffRadius)); return model; } diff --git a/src/Renderer.h b/src/Renderer.h index 0a8bc12..f1f728e 100755 --- a/src/Renderer.h +++ b/src/Renderer.h @@ -24,12 +24,6 @@ public: int HEIGHT, WIDTH; std::list> ModelsToRender; - - std::vector Light_constantAttenuation; - std::vector Light_linearAttenuation; - std::vector Light_quadraticAttenuation; - std::vector Light_spotExponent; - std::list> AABBsToRender; Renderer(); @@ -45,7 +39,9 @@ public: glm::vec3 _specular, glm::vec3 _diffuse, float _specularExponent, - float _scale + float _ConstantAttenuation, + float _LinearAttenuation, + float _QuadraticAttenuation ); void AddAABBToDraw(glm::vec3 origin, glm::vec3 volumeVector, bool colliding); @@ -74,8 +70,8 @@ private: glm::vec3 Specular; glm::vec3 Diffuse; float SpecularExponent; - float Scale; glm::mat4 SphereModelMatrix; + float ConstantAttenuation, LinearAttenuation, QuadraticAttenuation; }; float Gamma; diff --git a/src/Shaders/Fragment.glsl b/src/Shaders/Fragment.glsl index 64d4a59..6741851 100755 --- a/src/Shaders/Fragment.glsl +++ b/src/Shaders/Fragment.glsl @@ -16,10 +16,10 @@ out vec4 frag_Normal; void main() { // Diffuse Texture - frag_Diffuse = texture2D(DiffuseTexture, Input.TextureCoord); + frag_Diffuse = texture(DiffuseTexture, Input.TextureCoord); // G-buffer Position - frag_Position = vec4(Input.Position.xyz, 0.0); + frag_Position = vec4(Input.Position.xyz, 1.0); // G-buffer Normal frag_Normal = vec4(Input.Normal, 0.0); diff --git a/src/Shaders/Fragment2.glsl b/src/Shaders/Fragment2.glsl index 2467d35..2a01693 100644 --- a/src/Shaders/Fragment2.glsl +++ b/src/Shaders/Fragment2.glsl @@ -12,15 +12,19 @@ uniform vec3 la; uniform vec3 ls; uniform vec3 ld; uniform vec3 lp; -uniform float LightRadius; uniform float specularExponent; uniform vec3 CameraPosition; +uniform float ConstantAttenuation; +uniform float LinearAttenuation; +uniform float QuadraticAttenuation; 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; + + in VertexData { vec3 Position; @@ -44,12 +48,15 @@ vec4 phong(vec3 position, vec3 normal) vec3 surfaceToViewer = normalize(-position); vec3 halfWay = normalize(surfaceToViewer + directionToLight); float dotSpecular = max(dot(halfWay, normal), 0.0); - float specularFactor = pow(dotSpecular, specularExponent * 2); + float specularFactor = pow(dotSpecular, specularExponent * 2.0); vec3 Is = ks * ls * specularFactor; //Attenuation float dist = distance(lightPos, position); - float attenuation = -log(min(1.0, dist / LightRadius)); + //float attenuation = -log(min(1.0, dist / LightRadius)); + + float attenuation = 1.0 / (ConstantAttenuation + (LinearAttenuation * dist) + (QuadraticAttenuation * dist * dist)); + //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))); @@ -62,9 +69,9 @@ vec4 phong(vec3 position, vec3 normal) //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); + //float radius = 5.0; + //float alpha = dist / radius; + //float dampingFactor = 1.0 - pow(alpha, 3); return vec4((Id + Is) * attenuation, 1.0); diff --git a/src/Shaders/Vertex2.glsl b/src/Shaders/Vertex2.glsl index 3156821..b7a0ee3 100644 --- a/src/Shaders/Vertex2.glsl +++ b/src/Shaders/Vertex2.glsl @@ -15,5 +15,5 @@ void main() { gl_Position = MVP * vec4(Position, 1.0); Output.Position = Position; - Output.TextureCoord = (vec2(Position) + 1) / 2; + Output.TextureCoord = (vec2(Position) + 1.0) / 2.0; } \ No newline at end of file diff --git a/src/Systems/RenderSystem.cpp b/src/Systems/RenderSystem.cpp index 47bc389..5f7ba73 100755 --- a/src/Systems/RenderSystem.cpp +++ b/src/Systems/RenderSystem.cpp @@ -39,7 +39,9 @@ void Systems::RenderSystem::UpdateEntity(double dt, EntityID entity, EntityID pa pointLightComponent->Specular, pointLightComponent->Diffuse, pointLightComponent->specularExponent, - pointLightComponent->Scale + pointLightComponent->ConstantAttenuation, + pointLightComponent->LinearAttenuation, + pointLightComponent->QuadraticAttenuation ); } From 37a8af6b65aa9e10ac771cf00c9c60ee11ec1e12 Mon Sep 17 00:00:00 2001 From: Tleety Date: Sun, 11 May 2014 01:05:22 +0200 Subject: [PATCH 13/22] Added being able to change attenuation in real time --- src/Components/PointLight.h | 6 ++-- src/Renderer.cpp | 55 ++++++++++++++++++++++++++++++++++++- src/Renderer.h | 1 + 3 files changed, 58 insertions(+), 4 deletions(-) diff --git a/src/Components/PointLight.h b/src/Components/PointLight.h index 646ea01..b08be67 100755 --- a/src/Components/PointLight.h +++ b/src/Components/PointLight.h @@ -11,11 +11,11 @@ struct PointLight : Component { PointLight() : Specular(1.0f, 1.0f, 1.0f) - , Diffuse(0.4f, 0.4f, 0.4f) + , Diffuse(1.0f, 1.0f, 1.0f) , specularExponent(50.0f) - , ConstantAttenuation(1.05f) + , ConstantAttenuation(1.0f) , LinearAttenuation(0.f) - , QuadraticAttenuation(2.55f) + , QuadraticAttenuation(3.f) { } float ConstantAttenuation, LinearAttenuation, QuadraticAttenuation; diff --git a/src/Renderer.cpp b/src/Renderer.cpp index b8204c7..f652a28 100755 --- a/src/Renderer.cpp +++ b/src/Renderer.cpp @@ -133,6 +133,9 @@ void Renderer::LoadContent() m_FinalPassProgram.Compile(); m_FinalPassProgram.Link(); Gamma = 2.2f; + CAtt = 1.0f; + LAtt = 0.0f; + QAtt = 3.0f; m_ScreenQuad = CreateQuad(); FrameBufferTextures(); @@ -161,6 +164,49 @@ void Renderer::Draw(double dt) LOG_INFO("Gamma_DOWN: %f", Gamma); } + if(glfwGetKey(m_Window, GLFW_KEY_1)) + { + if(glfwGetKey(m_Window, GLFW_KEY_KP_ADD)) + { + CAtt += 0.5f * dt; + LOG_INFO("Const: %f", CAtt); + } + if(glfwGetKey(m_Window, GLFW_KEY_KP_SUBTRACT)) + { + CAtt -= 0.5f * dt; + LOG_INFO("Const: %f", CAtt); + } + } + if(glfwGetKey(m_Window, GLFW_KEY_2)) + { + if(glfwGetKey(m_Window, GLFW_KEY_KP_ADD)) + { + LAtt += 0.5f * dt; + LOG_INFO("Linear: %f", LAtt); + } + if(glfwGetKey(m_Window, GLFW_KEY_KP_SUBTRACT)) + { + LAtt -= 0.5f * dt; + LOG_INFO("Linear: %f", LAtt); + } + } + if(glfwGetKey(m_Window, GLFW_KEY_3)) + { + if(glfwGetKey(m_Window, GLFW_KEY_KP_ADD)) + { + QAtt += 0.5f * dt; + LOG_INFO("Quadratic: %f", QAtt); + } + if(glfwGetKey(m_Window, GLFW_KEY_KP_SUBTRACT)) + { + QAtt -= 0.5f * dt; + LOG_INFO("Quadratic: %f", QAtt); + } + } + + + + glDisable(GL_BLEND); DrawFBO(); @@ -657,7 +703,7 @@ void Renderer::DrawFBO() m_FinalPassProgram.Bind(); // Ambient light - glUniform3fv(glGetUniformLocation(m_FinalPassProgram.GetHandle(), "La"), 1, glm::value_ptr(glm::vec3(0.3f, 0.3f, 0.3f))); + glUniform3fv(glGetUniformLocation(m_FinalPassProgram.GetHandle(), "La"), 1, glm::value_ptr(glm::vec3(0.1f, 0.1f, 0.1f))); glUniform1f(glGetUniformLocation(m_FinalPassProgram.GetHandle(), "Gamma"), Gamma); glActiveTexture(GL_TEXTURE0); @@ -733,6 +779,10 @@ void Renderer::DrawLightScene() glUniform1f(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "LinearAttenuation"), light.LinearAttenuation); glUniform1f(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "QuadraticAttenuation"), light.QuadraticAttenuation); +// glUniform1f(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "ConstantAttenuation"), CAtt); +// glUniform1f(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "LinearAttenuation"), LAtt); +// glUniform1f(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "QuadraticAttenuation"), QAtt); + glDrawArrays(GL_TRIANGLES, 0, m_sphereModel->Vertices.size()); }; glEnable (GL_DEPTH_TEST); @@ -750,6 +800,9 @@ 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)); glm::mat4 model; diff --git a/src/Renderer.h b/src/Renderer.h index f1f728e..672c6f6 100755 --- a/src/Renderer.h +++ b/src/Renderer.h @@ -85,6 +85,7 @@ private: bool m_DrawNormals; bool m_DrawWireframe; bool m_DrawBounds; + float CAtt, LAtt, QAtt; std::shared_ptr m_Skybox; From d9ca9ac1f70da9429f0bc79e62609ace9a84b92a Mon Sep 17 00:00:00 2001 From: Tleety Date: Sun, 11 May 2014 01:09:06 +0200 Subject: [PATCH 14/22] Removed unused code. --- src/Renderer.cpp | 85 ------------------------------------------------ 1 file changed, 85 deletions(-) diff --git a/src/Renderer.cpp b/src/Renderer.cpp index f652a28..a965873 100755 --- a/src/Renderer.cpp +++ b/src/Renderer.cpp @@ -143,7 +143,6 @@ void Renderer::LoadContent() void Renderer::Draw(double dt) { - if(glfwGetKey(m_Window, GLFW_KEY_F1)) { m_QuadView = false; @@ -204,9 +203,6 @@ void Renderer::Draw(double dt) } } - - - glDisable(GL_BLEND); DrawFBO(); @@ -230,87 +226,6 @@ void Renderer::DrawSkybox() m_Skybox->Draw(); } -void Renderer::DrawScene() -{ -// glBindFramebuffer(GL_FRAMEBUFFER, 0); -// glViewport(0, 0, WIDTH, HEIGHT); - - glClear(GL_DEPTH_BUFFER_BIT); - //glClearColor(1.0f, 1.0f, 0.0f, 1.0f); - - glEnable(GL_DEPTH_TEST); - glEnable(GL_CULL_FACE); - glCullFace(GL_BACK); -#ifdef DEBUG - glDisable(GL_CULL_FACE); - glPolygonMode(GL_BACK, GL_LINE); -#endif - - // Draw models - glm::mat4 depthViewMatrix = glm::lookAt(m_SunPosition, m_SunTarget, glm::vec3(0, 1, 0)) * glm::translate(-m_Camera->Position() * glm::vec3(1, 0, 0)); - glm::mat4 depthCamera = m_SunProjection * depthViewMatrix; - glm::mat4 biasMatrix( - 0.5, 0.0, 0.0, 0.0, - 0.0, 0.5, 0.0, 0.0, - 0.0, 0.0, 0.5, 0.0, - 0.5, 0.5, 0.5, 1.0 - ); - - m_ShaderProgram.Bind(); - glUniform1i(glGetUniformLocation(m_ShaderProgram.GetHandle(), "numberOfLights"), Lights.size()); -// 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.size(), Light_constantAttenuation.data()); -// glUniform1fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "linearAttenuation"), Lights.size(), Light_linearAttenuation.data()); -// glUniform1fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "quadraticAttenuation"), Lights.size(), Light_quadraticAttenuation.data()); -// glUniform1fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "spotExponent"), Lights.size(), Light_spotExponent.data()); - if (m_DrawWireframe) - { - glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); - } - glActiveTexture(GL_TEXTURE1); - glBindTexture(GL_TEXTURE_2D, m_ShadowDepthTexture); - //DrawModels(m_ShaderProgram); - glm::mat4 cameraMatrix = m_Camera->ProjectionMatrix() * m_Camera->ViewMatrix(); - glm::mat4 depthCameraMatrix = biasMatrix * depthCamera; - glm::mat4 MVP; - glm::mat4 depthMVP; - for (auto tuple : ModelsToRender) - { - Model* model; - glm::mat4 modelMatrix; - bool visible; - std::tie(model, modelMatrix, visible, std::ignore) = tuple; - if (!visible) - continue; - - MVP = cameraMatrix * modelMatrix; - depthMVP = depthCameraMatrix * modelMatrix; - glUniformMatrix4fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "MVP"), 1, GL_FALSE, glm::value_ptr(MVP)); - glUniformMatrix4fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "DepthMVP"), 1, GL_FALSE, glm::value_ptr(depthMVP)); - glUniformMatrix4fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "model"), 1, GL_FALSE, glm::value_ptr(modelMatrix)); - glUniformMatrix4fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "view"), 1, GL_FALSE, glm::value_ptr(m_Camera->ViewMatrix())); - glBindVertexArray(model->VAO); - for (auto texGroup : model->TextureGroups) - { - glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, *texGroup.Texture); - glDrawArrays(GL_TRIANGLES, texGroup.StartIndex, texGroup.EndIndex - texGroup.StartIndex + 1); - } - } - -#ifdef DEBUG - // Debug draw model normals - if (m_DrawNormals) - { - m_ShaderProgramNormals.Bind(); - glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); - DrawModels(m_ShaderProgramNormals); - } -#endif -} - void Renderer::DrawShadowMap() { glEnable(GL_DEPTH_TEST);//Tests where objects are and display them correctly From 4639a6e4c33ec414da46dc2f57f58f267ac7c07d Mon Sep 17 00:00:00 2001 From: Tleety Date: Mon, 12 May 2014 18:03:39 +0200 Subject: [PATCH 15/22] Working shadows, however ugly they are. --- src/GameWorld.cpp | 32 ++++---- src/Renderer.cpp | 128 +++++++++++++++++++++++--------- src/Renderer.h | 1 + src/Shaders/FinalPass.frag.glsl | 5 ++ src/Shaders/Fragment.glsl | 16 +++- src/Shaders/Fragment2.glsl | 3 - src/Shaders/Vertex.glsl | 3 + src/Shaders/Vertex2.glsl | 3 + 8 files changed, 136 insertions(+), 55 deletions(-) diff --git a/src/GameWorld.cpp b/src/GameWorld.cpp index 679a71b..19026b2 100755 --- a/src/GameWorld.cpp +++ b/src/GameWorld.cpp @@ -49,6 +49,17 @@ void GameWorld::Initialize() AddComponent(jeep, "Input"); + for(int i = 0; i < 5; 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((float)(rand()%255)/255.f, (float)(rand()%255)/255.f, (float)(rand()%255)/255.f); + auto model = AddComponent(Light, "Model"); + model->ModelFile = "Models/Placeholders/PhysicsTest/PointLight.obj"; + } { auto camera = CreateEntity(); @@ -69,7 +80,7 @@ void GameWorld::Initialize() auto transform = AddComponent(chassis, "Transform"); transform->Position = glm::vec3(0, -0.6577f, 0); auto model = AddComponent(chassis, "Model"); - model->ModelFile = "Models/JeepV2/Chassi/chassi.OBJ"; + model->ModelFile = "Models/Jeep/Chassi/chassi.OBJ"; } @@ -79,7 +90,7 @@ void GameWorld::Initialize() 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/JeepV2/WheelFront/wheelFront.obj"; + 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; @@ -99,7 +110,7 @@ void GameWorld::Initialize() 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/JeepV2/WheelFront/wheelFront.obj"; + 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; @@ -117,7 +128,7 @@ void GameWorld::Initialize() 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/JeepV2/WheelBack/wheelBack.obj"; + 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; @@ -135,7 +146,7 @@ void GameWorld::Initialize() 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/JeepV2/WheelBack/wheelBack.obj"; + 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; @@ -224,16 +235,7 @@ void GameWorld::Initialize() CommitEntity(car); } */ - for(int i = 0; i < 50; i++) - { - auto Light = CreateEntity(); - auto transform = AddComponent(Light, "Transform"); - transform->Position = glm::vec3(i*cos(i), (float)(0.1*i), 0 + i*sin(i)); - auto light = AddComponent(Light, "PointLight"); - //light->Specular = glm::vec3(1.0f, 1.0f, 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 a965873..429e449 100755 --- a/src/Renderer.cpp +++ b/src/Renderer.cpp @@ -13,7 +13,10 @@ Renderer::Renderer() m_DrawWireframe = false; m_DrawBounds = false; #endif - + Gamma = 2.2f; + CAtt = 1.0f; + LAtt = 0.0f; + QAtt = 3.0f; m_ShadowMapRes = 2048; m_SunPosition = glm::vec3(0, 3.5f, 10); m_SunTarget = glm::vec3(0, 0, 0); @@ -89,12 +92,7 @@ void Renderer::LoadContent() m_ShaderProgramNormals.AddShader(std::shared_ptr(new FragmentShader("Shaders/Normals.frag.glsl"))); m_ShaderProgramNormals.Compile(); m_ShaderProgramNormals.Link(); - - m_ShaderProgramShadows.AddShader(std::shared_ptr(new VertexShader("Shaders/ShadowMap.vert.glsl"))); - m_ShaderProgramShadows.AddShader(std::shared_ptr(new FragmentShader("Shaders/ShadowMap.frag.glsl"))); - m_ShaderProgramShadows.Compile(); - m_ShaderProgramShadows.Link(); - + m_ShaderProgramShadowsDrawDepth.AddShader(std::shared_ptr(new VertexShader("Shaders/VisualizeDepth.vert.glsl"))); m_ShaderProgramShadowsDrawDepth.AddShader(std::shared_ptr(new FragmentShader("Shaders/VisualizeDepth.frag.glsl"))); m_ShaderProgramShadowsDrawDepth.Compile(); @@ -110,9 +108,15 @@ void Renderer::LoadContent() m_ShaderProgramSkybox.Compile(); m_ShaderProgramSkybox.Link();*/ + m_ShaderProgramShadows.AddShader(std::shared_ptr(new VertexShader("Shaders/ShadowMap.vert.glsl"))); + m_ShaderProgramShadows.AddShader(std::shared_ptr(new FragmentShader("Shaders/ShadowMap.frag.glsl"))); + m_ShaderProgramShadows.Compile(); + m_ShaderProgramShadows.Link(); + m_FirstPassProgram.AddShader(std::shared_ptr(new VertexShader("Shaders/Vertex.glsl"))); m_FirstPassProgram.AddShader(std::shared_ptr(new FragmentShader("Shaders/Fragment.glsl"))); m_FirstPassProgram.Compile(); + glBindFragDataLocation(m_FirstPassProgram.GetHandle(), 0, "frag_Diffuse"); glBindFragDataLocation(m_FirstPassProgram.GetHandle(), 1, "frag_Position"); glBindFragDataLocation(m_FirstPassProgram.GetHandle(), 2, "frag_Normal"); @@ -132,12 +136,8 @@ void Renderer::LoadContent() m_FinalPassProgram.AddShader(std::shared_ptr(new FragmentShader("Shaders/FinalPass.frag.glsl"))); m_FinalPassProgram.Compile(); m_FinalPassProgram.Link(); - Gamma = 2.2f; - CAtt = 1.0f; - LAtt = 0.0f; - QAtt = 3.0f; m_ScreenQuad = CreateQuad(); - + CreateShadowMap(m_ShadowMapRes); FrameBufferTextures(); } @@ -226,6 +226,32 @@ void Renderer::DrawSkybox() m_Skybox->Draw(); } +void Renderer::CreateShadowMap(int resolution) +{ + glGenFramebuffers(1, &m_ShadowFrameBuffer); + glBindFramebuffer(GL_FRAMEBUFFER, m_ShadowFrameBuffer); + + // Depth texture + glGenTextures(1, &m_ShadowDepthTexture); + glBindTexture(GL_TEXTURE_2D, m_ShadowDepthTexture); + glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT16, resolution, resolution, 0, GL_DEPTH_COMPONENT, GL_FLOAT, nullptr); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER); + + //glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE ); + //glTexParameteri( GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE, GL_INTENSITY ); + + glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, m_ShadowDepthTexture, 0); + glDrawBuffer(GL_NONE); + + if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) { + LOG_ERROR("Framebuffer incomplete!"); + return; + } +} + void Renderer::DrawShadowMap() { glEnable(GL_DEPTH_TEST);//Tests where objects are and display them correctly @@ -239,14 +265,9 @@ void Renderer::DrawShadowMap() glClear(GL_DEPTH_BUFFER_BIT); //glClearColor(0.0f, 0.0f, 0.0f, 0.0f); - //Creates the "camera" for the shadowmap from the direction of the sun. - glm::mat4 depthViewMatrix = glm::lookAt(m_SunPosition, m_SunTarget, glm::vec3(0, 1, 0)) * glm::translate(-m_Camera->Position() * glm::vec3(1, 0, 0)); -// glm::mat4 depthViewMatrix = glm::lookAt(m_SunPosition, m_SunTarget, glm::vec3(0, 1, 0)); + glm::mat4 depthViewMatrix = glm::lookAt(m_SunPosition, m_SunTarget, glm::vec3(0, 1, 0)); glm::mat4 depthCamera = m_SunProjection * depthViewMatrix; - - //glm::mat4 cameraMatrix = depthProjectionMatrix * m_Camera->ViewMatrix(); - glm::mat4 MVP; m_ShaderProgramShadows.Bind(); @@ -271,6 +292,7 @@ void Renderer::DrawShadowMap() glDrawArrays(GL_TRIANGLES, texGroup.StartIndex, texGroup.EndIndex - texGroup.StartIndex + 1); } } + } void Renderer::DrawDebugShadowMap() @@ -529,6 +551,14 @@ void Renderer::FrameBufferTextures() glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); + /*glGenTextures(1, &m_fShadowTexture); + glBindTexture(GL_TEXTURE_2D, m_fShadowTexture); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, 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); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);*/ + //Bind fb glBindFramebuffer(GL_FRAMEBUFFER, m_fbBasePass); glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, m_fDepthBuffer); @@ -537,11 +567,12 @@ void Renderer::FrameBufferTextures() glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_fDiffuseTexture, 0); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, m_fPositionTexture, 0); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, GL_TEXTURE_2D, m_fNormalsTexture, 0); + //glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT3, GL_TEXTURE_2D, m_fShadowTexture, 0); GLenum fbStatus = glCheckFramebufferStatus(GL_FRAMEBUFFER); if(fbStatus != GL_FRAMEBUFFER_COMPLETE) { - LOG_ERROR("DeferredLighting:Init: FrameBuffer incomplete: 0x%x\n", fbStatus); + LOG_ERROR("DeferredLighting:Init: m_fbBasePass incomplete: 0x%x\n", fbStatus); //exit(1); } @@ -562,15 +593,18 @@ void Renderer::FrameBufferTextures() fbStatus = glCheckFramebufferStatus(GL_FRAMEBUFFER); if(fbStatus != GL_FRAMEBUFFER_COMPLETE) { - LOG_ERROR("DeferredLighting:Init: FrameBuffer incomplete: 0x%x\n", fbStatus); + LOG_ERROR("DeferredLighting:Init: m_fbLightingPass incomplete: 0x%x\n", fbStatus); //exit(1); } + } void Renderer::DrawFBO() { + DrawShadowMap(); + /* Base pass */ @@ -586,8 +620,10 @@ void Renderer::DrawFBO() m_FirstPassProgram.Bind(); GLenum windowBuffOpaque[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2 }; glDrawBuffers(3, windowBuffOpaque); - + glCullFace(GL_BACK); + + glViewport(0, 0, WIDTH, HEIGHT); DrawFBOScene(); /* @@ -617,7 +653,7 @@ void Renderer::DrawFBO() m_FinalPassProgram.Bind(); - // Ambient light + // Ambient light & Shadow Matrix glUniform3fv(glGetUniformLocation(m_FinalPassProgram.GetHandle(), "La"), 1, glm::value_ptr(glm::vec3(0.1f, 0.1f, 0.1f))); glUniform1f(glGetUniformLocation(m_FinalPassProgram.GetHandle(), "Gamma"), Gamma); @@ -634,8 +670,27 @@ void Renderer::DrawFBO() void Renderer::DrawFBOScene() { + glEnable(GL_DEPTH_TEST);//Tests where objects are and display them correctly + glEnable(GL_CULL_FACE); //removes triangles on the wrong side of the object + glCullFace(GL_BACK); //Make it so that only the back faces are rendered + glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); //Draws filled polygons + glm::mat4 cameraMatrix = m_Camera->ProjectionMatrix() * m_Camera->ViewMatrix(); glm::mat4 MVP; + glm::mat4 biasMatrix( + 0.5, 0.0, 0.0, 0.0, + 0.0, 0.5, 0.0, 0.0, + 0.0, 0.0, 0.5, 0.0, + 0.5, 0.5, 0.5, 1.0 + ); + + glm::mat4 depthViewMatrix = glm::lookAt(m_SunPosition, m_SunTarget, glm::vec3(0, 1, 0)); + glm::mat4 depthCamera = m_SunProjection * depthViewMatrix; + glm::mat4 depthCameraMatrix = biasMatrix * depthCamera; + glm::mat4 depthMVP; + + glActiveTexture(GL_TEXTURE1); + glBindTexture(GL_TEXTURE_2D, m_ShadowDepthTexture); for (auto tuple : ModelsToRender) { @@ -645,9 +700,11 @@ void Renderer::DrawFBOScene() std::tie(model, modelMatrix, visible, std::ignore) = tuple; if (!visible) continue; - + MVP = cameraMatrix * modelMatrix; + depthMVP = depthCameraMatrix * modelMatrix; glUniformMatrix4fv(glGetUniformLocation(m_FirstPassProgram.GetHandle(), "MVP"), 1, GL_FALSE, glm::value_ptr(MVP)); + glUniformMatrix4fv(glGetUniformLocation(m_FirstPassProgram.GetHandle(), "DepthMVP"), 1, GL_FALSE, glm::value_ptr(depthMVP)); glUniformMatrix4fv(glGetUniformLocation(m_FirstPassProgram.GetHandle(), "M"), 1, GL_FALSE, glm::value_ptr(modelMatrix)); 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())); @@ -690,13 +747,12 @@ void Renderer::DrawLightScene() glUniform3fv(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "lp"), 1, glm::value_ptr(light.Position)); glUniform3f(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "CameraPosition"), m_Camera->Position().x, m_Camera->Position().y, m_Camera->Position().z); glUniform1f(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "specularExponent"), light.SpecularExponent); - glUniform1f(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "ConstantAttenuation"), light.ConstantAttenuation); - glUniform1f(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "LinearAttenuation"), light.LinearAttenuation); - glUniform1f(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "QuadraticAttenuation"), light.QuadraticAttenuation); - -// 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(), "ConstantAttenuation"), light.ConstantAttenuation); +// glUniform1f(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "LinearAttenuation"), light.LinearAttenuation); +// glUniform1f(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "QuadraticAttenuation"), light.QuadraticAttenuation); + glUniform1f(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "ConstantAttenuation"), CAtt); + glUniform1f(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "LinearAttenuation"), LAtt); + glUniform1f(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "QuadraticAttenuation"), QAtt); glDrawArrays(GL_TRIANGLES, 0, m_sphereModel->Vertices.size()); }; @@ -712,12 +768,12 @@ void Renderer::SetSphereModel( Model* _model ) 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 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)); glm::mat4 model; diff --git a/src/Renderer.h b/src/Renderer.h index 672c6f6..e1a2aec 100755 --- a/src/Renderer.h +++ b/src/Renderer.h @@ -105,6 +105,7 @@ private: GLuint m_fBlendTexture; GLuint m_fbLightingPass; GLuint m_fLightingTexture; + GLuint m_fShadowTexture; GLuint m_fDepthBuffer; GLenum draw_bufs[2]; diff --git a/src/Shaders/FinalPass.frag.glsl b/src/Shaders/FinalPass.frag.glsl index c6b9257..2a6cf4c 100644 --- a/src/Shaders/FinalPass.frag.glsl +++ b/src/Shaders/FinalPass.frag.glsl @@ -5,6 +5,7 @@ uniform float Gamma; layout (binding=0) uniform sampler2D DiffuseTexture; layout (binding=1) uniform sampler2D LightingTexture; +layout (binding=2) uniform sampler2D ShadowTexture; in VertexData { @@ -18,7 +19,11 @@ void main() { vec4 DiffuseTexel = texture(DiffuseTexture, Input.TextureCoord); vec4 LightingTexel = texture(LightingTexture, Input.TextureCoord); + vec4 ShadowTexel = texture(ShadowTexture, Input.TextureCoord); + vec4 _FragmentColor = DiffuseTexel * vec4(La, 1.0) + LightingTexel; FragmentColor = vec4(pow(_FragmentColor.rgb, vec3(1.0 / Gamma)), _FragmentColor.a); + //FragmentColor = ShadowTexel; + } \ No newline at end of file diff --git a/src/Shaders/Fragment.glsl b/src/Shaders/Fragment.glsl index 6741851..e64e2ec 100755 --- a/src/Shaders/Fragment.glsl +++ b/src/Shaders/Fragment.glsl @@ -1,22 +1,36 @@ #version 430 layout (binding=0) uniform sampler2D DiffuseTexture; +layout (binding=1) uniform sampler2D ShadowTexture; in VertexData { vec3 Position; vec3 Normal; vec2 TextureCoord; + vec4 ShadowCoord; } Input; out vec4 frag_Diffuse; out vec4 frag_Position; out vec4 frag_Normal; +float Shadow(vec4 ShadowCoord) +{ + if( texture(ShadowTexture, Input.ShadowCoord.xy).z < ShadowCoord.z ) + { + return 0.3; + } + else + { + return 1.0; + } +} + void main() { // Diffuse Texture - frag_Diffuse = texture(DiffuseTexture, Input.TextureCoord); + frag_Diffuse = texture(DiffuseTexture, Input.TextureCoord) * Shadow(Input.ShadowCoord); // G-buffer Position frag_Position = vec4(Input.Position.xyz, 1.0); diff --git a/src/Shaders/Fragment2.glsl b/src/Shaders/Fragment2.glsl index 2a01693..f531f76 100644 --- a/src/Shaders/Fragment2.glsl +++ b/src/Shaders/Fragment2.glsl @@ -23,8 +23,6 @@ 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; - - in VertexData { vec3 Position; @@ -73,7 +71,6 @@ vec4 phong(vec3 position, vec3 normal) //float alpha = dist / radius; //float dampingFactor = 1.0 - pow(alpha, 3); - return vec4((Id + Is) * attenuation, 1.0); } diff --git a/src/Shaders/Vertex.glsl b/src/Shaders/Vertex.glsl index ea38b24..2f799ef 100755 --- a/src/Shaders/Vertex.glsl +++ b/src/Shaders/Vertex.glsl @@ -4,6 +4,7 @@ uniform mat4 MVP; uniform mat4 M; uniform mat4 V; uniform mat4 P; +uniform mat4 DepthMVP; layout (location = 0) in vec3 Position; layout (location = 1) in vec3 Normal; @@ -14,6 +15,7 @@ out VertexData vec3 Position; vec3 Normal; vec2 TextureCoord; + vec4 ShadowCoord; } Output; void main() @@ -23,4 +25,5 @@ void main() Output.Position = vec3(V * M * vec4(Position, 1.0)); Output.Normal = normalize(vec3(inverse(transpose(V * M)) * vec4(Normal, 0.0))); Output.TextureCoord = TextureCoord; + Output.ShadowCoord = DepthMVP * vec4(Position, 1.0); } \ No newline at end of file diff --git a/src/Shaders/Vertex2.glsl b/src/Shaders/Vertex2.glsl index b7a0ee3..e617846 100644 --- a/src/Shaders/Vertex2.glsl +++ b/src/Shaders/Vertex2.glsl @@ -5,6 +5,8 @@ uniform mat4 MVP; layout (location = 0) in vec3 Position; layout (location = 2) in vec2 TextureCoord; +uniform mat4 depthBiasMVP; + out VertexData { vec3 Position; @@ -16,4 +18,5 @@ void main() gl_Position = MVP * vec4(Position, 1.0); Output.Position = Position; Output.TextureCoord = (vec2(Position) + 1.0) / 2.0; + } \ No newline at end of file From ccd1393dac181e95f4500a0a24962f57e73db433 Mon Sep 17 00:00:00 2001 From: Tleety Date: Mon, 12 May 2014 23:02:19 +0200 Subject: [PATCH 16/22] Fixed some shadow buggs. --- src/Renderer.cpp | 14 +++++++------- src/Shaders/Fragment.glsl | 5 ++++- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/Renderer.cpp b/src/Renderer.cpp index 429e449..2e781a0 100755 --- a/src/Renderer.cpp +++ b/src/Renderer.cpp @@ -17,10 +17,10 @@ Renderer::Renderer() CAtt = 1.0f; LAtt = 0.0f; QAtt = 3.0f; - m_ShadowMapRes = 2048; + m_ShadowMapRes = 2048*8; m_SunPosition = glm::vec3(0, 3.5f, 10); m_SunTarget = glm::vec3(0, 0, 0); - m_SunProjection = glm::ortho(-100, 100, -100, 100, -100, 100); + m_SunProjection = glm::ortho(-200.f, 200.f, -200.f, 200.f, -100, 200); /* Lights = 0;*/ } @@ -256,7 +256,7 @@ void Renderer::DrawShadowMap() { glEnable(GL_DEPTH_TEST);//Tests where objects are and display them correctly glEnable(GL_CULL_FACE); //removes triangles on the wrong side of the object - glCullFace(GL_FRONT); //Make it so that only the back faces are rendered + glCullFace(GL_BACK); //Make it so that only the back faces are rendered //Binds the FBO and sets the veiwport, witch in effect is how large the shadowmap is and what resolution it has. glBindFramebuffer(GL_FRAMEBUFFER, m_ShadowFrameBuffer); @@ -653,7 +653,7 @@ void Renderer::DrawFBO() m_FinalPassProgram.Bind(); - // Ambient light & Shadow Matrix + // Ambient light glUniform3fv(glGetUniformLocation(m_FinalPassProgram.GetHandle(), "La"), 1, glm::value_ptr(glm::vec3(0.1f, 0.1f, 0.1f))); glUniform1f(glGetUniformLocation(m_FinalPassProgram.GetHandle(), "Gamma"), Gamma); @@ -670,9 +670,9 @@ void Renderer::DrawFBO() void Renderer::DrawFBOScene() { - glEnable(GL_DEPTH_TEST);//Tests where objects are and display them correctly - glEnable(GL_CULL_FACE); //removes triangles on the wrong side of the object - glCullFace(GL_BACK); //Make it so that only the back faces are rendered +// glEnable(GL_DEPTH_TEST);//Tests where objects are and display them correctly +// glEnable(GL_CULL_FACE); //removes triangles on the wrong side of the object +// glCullFace(GL_BACK); //Make it so that only the back faces are rendered glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); //Draws filled polygons glm::mat4 cameraMatrix = m_Camera->ProjectionMatrix() * m_Camera->ViewMatrix(); diff --git a/src/Shaders/Fragment.glsl b/src/Shaders/Fragment.glsl index e64e2ec..cc5370f 100755 --- a/src/Shaders/Fragment.glsl +++ b/src/Shaders/Fragment.glsl @@ -17,7 +17,10 @@ out vec4 frag_Normal; float Shadow(vec4 ShadowCoord) { - if( texture(ShadowTexture, Input.ShadowCoord.xy).z < ShadowCoord.z ) + //float cosTheta = clamp(dot(Input.Normal, 1.0), 0.0, 1.0); + float bias = 0.0005; // cosTheta is dot( n,l ), clamped between 0 and 1 + bias = clamp(bias, 0.0, 0.01); + if( texture(ShadowTexture, Input.ShadowCoord.xy).z < ShadowCoord.z - bias) { return 0.3; } From 33b14b1859f1262dab5e633c002d0542d4cfdd81 Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Sat, 17 May 2014 00:49:23 +0200 Subject: [PATCH 17/22] Mouse locking event --- src/Events/LockMouse.h | 14 ++++++++ src/InputManager.cpp | 35 ++++++++++++++----- src/InputManager.h | 18 +++++++--- vs11/Returngeance/Returngeance.vcxproj | 1 + .../Returngeance/Returngeance.vcxproj.filters | 3 ++ 5 files changed, 58 insertions(+), 13 deletions(-) create mode 100644 src/Events/LockMouse.h diff --git a/src/Events/LockMouse.h b/src/Events/LockMouse.h new file mode 100644 index 0000000..b045c55 --- /dev/null +++ b/src/Events/LockMouse.h @@ -0,0 +1,14 @@ +#ifndef Events_LockMouse_h__ +#define Events_LockMouse_h__ + +#include "EventBroker.h" + +namespace Events +{ + +struct LockMouse : Event { }; +struct UnlockMouse : Event { }; + +} + +#endif // Events_LockMouse_h__ \ No newline at end of file diff --git a/src/InputManager.cpp b/src/InputManager.cpp index fc225ec..edbb736 100644 --- a/src/InputManager.cpp +++ b/src/InputManager.cpp @@ -5,6 +5,9 @@ void InputManager::Initialize() { m_LastGamepadAxisState = std::array(); m_LastGamepadButtonState = std::array(); + + EVENT_SUBSCRIBE_MEMBER(m_ELockMouse, &InputManager::OnLockMouse); + EVENT_SUBSCRIBE_MEMBER(m_EUnlockMouse, &InputManager::OnUnlockMouse); } void InputManager::Update(double dt) @@ -20,13 +23,13 @@ void InputManager::Update(double dt) { Events::KeyDown e; e.KeyCode = i; - m_EventBroker->Publish(e); + EventBroker->Publish(e); } else { Events::KeyUp e; e.KeyCode = i; - m_EventBroker->Publish(e); + EventBroker->Publish(e); } } } @@ -42,13 +45,13 @@ void InputManager::Update(double dt) { Events::MousePress e; e.Button = i; - m_EventBroker->Publish(e); + EventBroker->Publish(e); } else { Events::MouseRelease e; e.Button = i; - m_EventBroker->Publish(e); + EventBroker->Publish(e); } } } @@ -65,7 +68,7 @@ void InputManager::Update(double dt) e.Y = m_CurrentMouseY; e.DeltaX = m_CurrentMouseDeltaX; e.DeltaY = m_CurrentMouseDeltaY; - m_EventBroker->Publish(e); + EventBroker->Publish(e); } // // Lock mouse while holding LMB @@ -156,7 +159,7 @@ void InputManager::PublishGamepadAxisIfChanged(int gamepadID, Gamepad::Axis axis e.GamepadID = gamepadID; e.Axis = axis; e.Value = currentValue; - m_EventBroker->Publish(e); + EventBroker->Publish(e); } } @@ -171,14 +174,30 @@ void InputManager::PublishGamepadButtonIfChanged(int gamepadID, Gamepad::Button Events::GamepadButtonDown e; e.GamepadID = gamepadID; e.Button = button; - m_EventBroker->Publish(e); + EventBroker->Publish(e); } else { Events::GamepadButtonUp e; e.GamepadID = gamepadID; e.Button = button; - m_EventBroker->Publish(e); + EventBroker->Publish(e); } } } + +bool InputManager::OnLockMouse(const Events::LockMouse &event) +{ + m_MouseLocked = true; + glfwSetInputMode(m_GLFWWindow, GLFW_CURSOR, GLFW_CURSOR_DISABLED); + + return true; +} + +bool InputManager::OnUnlockMouse(const Events::UnlockMouse &event) +{ + m_MouseLocked = false; + glfwSetInputMode(m_GLFWWindow, GLFW_CURSOR, GLFW_CURSOR_NORMAL); + + return true; +} \ No newline at end of file diff --git a/src/InputManager.h b/src/InputManager.h index 41d77e7..591923e 100644 --- a/src/InputManager.h +++ b/src/InputManager.h @@ -11,22 +11,24 @@ #include "Events/MousePress.h" #include "Events/MouseRelease.h" #include "Events/MouseMove.h" +#include "Events/LockMouse.h" #include "Events/GamepadAxis.h" #include "Events/GamepadButton.h" class InputManager { public: - InputManager(GLFWwindow* window, std::shared_ptr eventBroker) + InputManager(GLFWwindow* window, std::shared_ptr<::EventBroker> eventBroker) : m_GLFWWindow(window) - , m_EventBroker(eventBroker) + , EventBroker(eventBroker) , m_CurrentKeyState() , m_LastKeyState() , m_CurrentMouseState() , m_LastMouseState() , m_CurrentMouseX(0), m_CurrentMouseY(0) , m_LastMouseX(0), m_LastMouseY(0) - , m_CurrentMouseDeltaX(0), m_CurrentMouseDeltaY(0) + , m_CurrentMouseDeltaX(0), m_CurrentMouseDeltaY(0) + , m_MouseLocked(false) { Initialize(); } void Initialize(); @@ -35,8 +37,13 @@ public: private: GLFWwindow* m_GLFWWindow; - std::shared_ptr m_EventBroker; - + std::shared_ptr<::EventBroker> EventBroker; + + EventRelay m_ELockMouse; + bool OnLockMouse(const Events::LockMouse &event); + EventRelay m_EUnlockMouse; + bool OnUnlockMouse(const Events::UnlockMouse &event); + std::array m_CurrentKeyState; std::array m_LastKeyState; std::array m_CurrentMouseState; @@ -51,6 +58,7 @@ private: double m_CurrentMouseX, m_CurrentMouseY; double m_LastMouseX, m_LastMouseY; double m_CurrentMouseDeltaX, m_CurrentMouseDeltaY; + bool m_MouseLocked; void PublishGamepadAxisIfChanged(int gamepadID, Gamepad::Axis axis); void PublishGamepadButtonIfChanged(int gamepadID, Gamepad::Button button); diff --git a/vs11/Returngeance/Returngeance.vcxproj b/vs11/Returngeance/Returngeance.vcxproj index 5226852..9849fc0 100755 --- a/vs11/Returngeance/Returngeance.vcxproj +++ b/vs11/Returngeance/Returngeance.vcxproj @@ -161,6 +161,7 @@ + diff --git a/vs11/Returngeance/Returngeance.vcxproj.filters b/vs11/Returngeance/Returngeance.vcxproj.filters index f0252d5..49cc263 100755 --- a/vs11/Returngeance/Returngeance.vcxproj.filters +++ b/vs11/Returngeance/Returngeance.vcxproj.filters @@ -353,6 +353,9 @@ Input\Events + + Input\Events + From 2a28f65c37e9b3ac4c2e0876d250604705e8c082 Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Sat, 17 May 2014 00:50:45 +0200 Subject: [PATCH 18/22] FreeSteeringSystem updated to allow for controller input --- src/Systems/FreeSteeringSystem.cpp | 124 +++++++++++++---------------- src/Systems/FreeSteeringSystem.h | 6 +- 2 files changed, 59 insertions(+), 71 deletions(-) diff --git a/src/Systems/FreeSteeringSystem.cpp b/src/Systems/FreeSteeringSystem.cpp index d59b475..edf24d0 100755 --- a/src/Systems/FreeSteeringSystem.cpp +++ b/src/Systems/FreeSteeringSystem.cpp @@ -24,95 +24,85 @@ void Systems::FreeSteeringSystem::UpdateEntity(double dt, EntityID entity, Entit { auto transform = m_World->GetComponent(entity, "Transform"); - glm::vec3 cameraRight = glm::vec3(m_InputController->Orientation * glm::vec4(1, 0, 0, 0)); - glm::vec3 cameraForward = glm::vec3(m_InputController->Orientation * glm::vec4(0, 0, -1, 0)); + glm::vec3 cameraRight = glm::vec3(transform->Orientation * glm::vec4(1, 0, 0, 0)); + glm::vec3 cameraForward = glm::vec3(transform->Orientation * glm::vec4(0, 0, -1, 0)); glm::vec3 movement; movement += cameraRight * m_InputController->Movement.x; movement.y += m_InputController->Movement.y; movement += cameraForward * -m_InputController->Movement.z; - transform->Position += movement * steering->Speed * m_InputController->SpeedMultiplier * (float)dt; - transform->Orientation = m_InputController->Orientation; + float speedMultiplier = 1.f; + if (m_InputController->SpeedMultiplier > 0) + speedMultiplier *= 4; + else if (m_InputController->SpeedMultiplier < 0) + speedMultiplier /= 4; + + transform->Position += movement * steering->Speed * speedMultiplier * (float)dt; + + glm::quat mouseOrientationPitch = glm::quat(m_InputController->MouseOrientation * glm::vec3(1, 0, 0)); + glm::quat mouseOrientationYaw = glm::quat(m_InputController->MouseOrientation * glm::vec3(0, 1, 0)); + + glm::vec3 controllerOrientationEuler = m_InputController->ControllerOrientation * (float)dt; + glm::quat controllerOrientationPitch = glm::quat(controllerOrientationEuler * glm::vec3(1, 0, 0)); + glm::quat controllerOrientationYaw = glm::quat(controllerOrientationEuler * glm::vec3(0, 1, 0)); + + // TOUCHING THIS CODE MIGHT CAUSE THE UNIVERSE TO IMPLODE, ALSO DRAGONS + //--------------------------------------------------------------------- + transform->Orientation = (mouseOrientationYaw * controllerOrientationYaw) + * transform->Orientation + * (mouseOrientationPitch * controllerOrientationPitch); + //--------------------------------------------------------------------- + // TOUCHING THIS CODE MIGHT CAUSE THE UNIVERSE TO IMPLODE, ALSO DRAGONS } + + m_InputController->MouseOrientation = glm::vec3(0); } bool Systems::FreeSteeringSystem::FreeSteeringInputController::OnCommand(const Events::InputCommand &event) { // Movement - if (event.Command == "+cam_forward") + if (event.Command == "vertical") { - Movement.z += -1.f; + Movement.z = -event.Value; } - else if (event.Command == "-cam_forward") + else if (event.Command == "horizontal") { - Movement.z -= -1.f; + Movement.x = event.Value; } - else if (event.Command == "+cam_backward") + else if (event.Command == "normal") { - Movement.z += 1.f; - } - else if (event.Command == "-cam_backward") - { - Movement.z -= 1.f; - } - else if (event.Command == "+cam_right") - { - Movement.x -= 1.f; - } - else if (event.Command == "-cam_right") - { - Movement.x += 1.f; - } - else if (event.Command == "+cam_left") - { - Movement.x -= -1.f; - } - else if (event.Command == "-cam_left") - { - Movement.x += -1.f; - } - else if (event.Command == "+up") - { - Movement.y += 1.f; - } - else if (event.Command == "-up") - { - Movement.y -= 1.f; - } - else if (event.Command == "+down") - { - Movement.y += -1.f; - } - else if (event.Command == "-down") - { - Movement.y -= -1.f; + Movement.y = event.Value; } // Speed - else if (event.Command == "+fast") + else if (event.Command == "speed") { - SpeedMultiplier *= 4.f; - } - else if (event.Command == "-fast") - { - SpeedMultiplier /= 4.f; - } - else if (event.Command == "+slow") - { - SpeedMultiplier /= 4.f; - } - else if (event.Command == "-slow") - { - SpeedMultiplier *= 4.f; + SpeedMultiplier = event.Value; } // Mouse click - else if (event.Command == "+attack") + else if (event.Command == "attack") { - OrientationActive = true; + OrientationActive = event.Value > 0; + + if (OrientationActive) + { + Events::LockMouse e; + EventBroker->Publish(e); + } + else + { + Events::UnlockMouse e; + EventBroker->Publish(e); + } } - else if (event.Command == "-attack") + + else if (event.Command == "vertical2") { - OrientationActive = false; + ControllerOrientation.x = event.Value; + } + else if (event.Command == "horizontal2") + { + ControllerOrientation.y = -event.Value; } return true; @@ -122,11 +112,7 @@ bool Systems::FreeSteeringSystem::FreeSteeringInputController::OnMouseMove(const { if (OrientationActive) { - // TOUCHING THIS CODE MIGHT CAUSE THE UNIVERSE TO IMPLODE, ALSO DRAGONS - //--------------------------------------------------------------------- - Orientation = glm::angleAxis(event.DeltaX / 300.f, glm::vec3(0, -1, 0)) * Orientation * glm::angleAxis(event.DeltaY / 300.f, glm::vec3(-1, 0, 0)); - //--------------------------------------------------------------------- - // TOUCHING THIS CODE MIGHT CAUSE THE UNIVERSE TO IMPLODE, ALSO DRAGONS + MouseOrientation = -glm::vec3(event.DeltaY / 300.f, event.DeltaX / 300.f, 0.f); } return true; diff --git a/src/Systems/FreeSteeringSystem.h b/src/Systems/FreeSteeringSystem.h index 4535cbb..b94466b 100755 --- a/src/Systems/FreeSteeringSystem.h +++ b/src/Systems/FreeSteeringSystem.h @@ -4,6 +4,7 @@ #include "Components/Transform.h" #include "Components/FreeSteering.h" #include "InputController.h" +#include "Events/LockMouse.h" namespace Systems { @@ -31,11 +32,12 @@ class FreeSteeringSystem::FreeSteeringInputController : InputController public: FreeSteeringInputController(std::shared_ptr<::EventBroker> eventBroker) : InputController(eventBroker) - , SpeedMultiplier(1.f) + , SpeedMultiplier(0.f) , OrientationActive(false) { } glm::vec3 Movement; - glm::quat Orientation; + glm::vec3 MouseOrientation; + glm::vec3 ControllerOrientation; float SpeedMultiplier; bool OrientationActive; From f180abcd6357bce5dd6d25eb1955738dae3165c1 Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Sat, 17 May 2014 00:52:11 +0200 Subject: [PATCH 19/22] Input System improved to allow for multiple inputs for same bind Did not work properly between keyboard and controller before. --- src/Events/BindMouseButton.h | 1 + src/GameWorld.cpp | 45 ++++++++------------- src/GameWorld.h | 2 +- src/Systems/InputSystem.cpp | 78 +++++++++++++++++++++++++++++------- src/Systems/InputSystem.h | 8 +++- 5 files changed, 89 insertions(+), 45 deletions(-) diff --git a/src/Events/BindMouseButton.h b/src/Events/BindMouseButton.h index 1f46647..e7fb8a4 100644 --- a/src/Events/BindMouseButton.h +++ b/src/Events/BindMouseButton.h @@ -10,6 +10,7 @@ struct BindMouseButton : Event { int Button; std::string Command; + float Value; }; } diff --git a/src/GameWorld.cpp b/src/GameWorld.cpp index 151ad43..16dec14 100755 --- a/src/GameWorld.cpp +++ b/src/GameWorld.cpp @@ -13,20 +13,30 @@ void GameWorld::Initialize() BindKey(GLFW_KEY_A, "horizontal", -1.f); BindKey(GLFW_KEY_D, "horizontal", 1.f); BindGamepadAxis(Gamepad::Axis::LeftX, "horizontal", 1.f); - BindGamepadAxis(Gamepad::Axis::RightTrigger, "vertical", 1.f); - BindGamepadAxis(Gamepad::Axis::LeftTrigger, "vertical", -1.f); + BindGamepadAxis(Gamepad::Axis::LeftY, "vertical", 1.f); + BindGamepadAxis(Gamepad::Axis::RightX, "horizontal", 1.f); + BindGamepadAxis(Gamepad::Axis::RightY, "vertical", 1.f); + BindGamepadAxis(Gamepad::Axis::RightTrigger, "normal", 1.f); + BindGamepadAxis(Gamepad::Axis::LeftTrigger, "normal", -1.f); + BindKey(GLFW_KEY_SPACE, "normal", 1.f); + BindKey(GLFW_KEY_LEFT_CONTROL, "normal", -1.f); + + BindKey(GLFW_KEY_LEFT_SHIFT, "speed", 1.f); + BindKey(GLFW_KEY_LEFT_ALT, "speed", -1.f); + + BindMouseButton(GLFW_MOUSE_BUTTON_1, "attack", 1.f); - BindKey(GLFW_KEY_UP, "barrel_rotation", 1.f); + /*BindKey(GLFW_KEY_UP, "barrel_rotation", 1.f); BindKey(GLFW_KEY_DOWN, "barrel_rotation", -1.f); BindKey(GLFW_KEY_LEFT, "tower_rotation", -1.f); BindKey(GLFW_KEY_RIGHT, "tower_rotation", 1.f); BindGamepadAxis(Gamepad::Axis::RightX, "tower_rotation", 1.f); BindGamepadAxis(Gamepad::Axis::RightY, "barrel_rotation", 1.f); - + BindKey(GLFW_KEY_SPACE, "handbrake", 1.f); BindGamepadButton(Gamepad::Button::A, "handbrake", 1.f); - BindKey(GLFW_KEY_Z, "shoot", 1.f); + BindKey(GLFW_KEY_Z, "shoot", 1.f);*/ //BindGamepadButton(Gamepad::Button::Up, "Gamepad::Button::Up", 1.f); //BindGamepadButton(Gamepad::Button::Down, "Gamepad::Button::Down", 1.f); @@ -42,28 +52,6 @@ void GameWorld::Initialize() //BindGamepadButton(Gamepad::Button::B, "Gamepad::Button::B", 1.f); //BindGamepadButton(Gamepad::Button::X, "Gamepad::Button::X", 1.f); //BindGamepadButton(Gamepad::Button::Y, "Gamepad::Button::Y", 1.f); -// -// BindKey(GLFW_KEY_UP, "vertical", -1.f); -// BindKey(GLFW_KEY_DOWN, "vertical", 1.f); -// BindKey(GLFW_KEY_LEFT, "horizontal", -1.f); -// BindKey(GLFW_KEY_RIGHT, "horizontal", 1.f); - /* - BindKey(GLFW_KEY_Q, "+tower_right"); - BindKey(GLFW_KEY_E, "+tower_left"); - - BindKey(GLFW_KEY_Q, "+up"); - BindKey(GLFW_KEY_LEFT_CONTROL, "+down"); - BindKey(GLFW_KEY_LEFT_ALT, "+slow"); - BindKey(GLFW_KEY_LEFT_SHIFT, "+fast"); - BindMouseButton(GLFW_MOUSE_BUTTON_1, "+attack"); - BindMouseButton(GLFW_MOUSE_BUTTON_2, "+attack2"); - BindMouseButton(GLFW_MOUSE_BUTTON_3, "+attack3"); - - - BindKey(GLFW_KEY_UP, "+cam_forward"); - BindKey(GLFW_KEY_DOWN, "+cam_backward"); - BindKey(GLFW_KEY_LEFT, "+cam_left"); - BindKey(GLFW_KEY_RIGHT, "+cam_right");*/ RegisterComponents(); @@ -793,11 +781,12 @@ void GameWorld::BindKey(int keyCode, std::string command, float value) m_EventBroker->Publish(e); } -void GameWorld::BindMouseButton(int button, std::string command) +void GameWorld::BindMouseButton(int button, std::string command, float value) { Events::BindMouseButton e; e.Button = button; e.Command = command; + e.Value = value; m_EventBroker->Publish(e); } diff --git a/src/GameWorld.h b/src/GameWorld.h index 5e990bb..7235e25 100755 --- a/src/GameWorld.h +++ b/src/GameWorld.h @@ -57,7 +57,7 @@ private: std::shared_ptr m_Renderer; void BindKey(int keyCode, std::string command, float value); - void BindMouseButton(int button, std::string command); + void BindMouseButton(int button, std::string command, float value); void BindGamepadAxis(Gamepad::Axis axis, std::string command, float value); void BindGamepadButton(Gamepad::Button button, std::string command, float value); }; diff --git a/src/Systems/InputSystem.cpp b/src/Systems/InputSystem.cpp index 7c4cde7..4c5fa6f 100755 --- a/src/Systems/InputSystem.cpp +++ b/src/Systems/InputSystem.cpp @@ -52,8 +52,8 @@ bool Systems::InputSystem::OnKeyDown(const Events::KeyDown &event) std::string command; float value; std::tie(command, value) = bindingIt->second; - m_CommandValues[command] += value; - PublishCommand(0, command, std::max(-1.f, std::min(m_CommandValues[command], 1.f))); + m_CommandKeyboardValues[command][event.KeyCode] = value; + PublishCommand(0, command, GetCommandTotalValue(command)); } return true; @@ -67,8 +67,8 @@ bool Systems::InputSystem::OnKeyUp(const Events::KeyUp &event) std::string command; float value; std::tie(command, value) = bindingIt->second; - m_CommandValues[command] -= value; - PublishCommand(0, command, std::max(-1.f, std::min(m_CommandValues[command], 1.f))); + m_CommandKeyboardValues[command][event.KeyCode] = 0; + PublishCommand(0, command, GetCommandTotalValue(command));; } return true; @@ -79,7 +79,11 @@ bool Systems::InputSystem::OnMousePress(const Events::MousePress &event) auto bindingIt = m_MouseButtonBindings.find(event.Button); if (bindingIt != m_MouseButtonBindings.end()) { - PublishCommand(0, bindingIt->second, 1.f); + std::string command; + float value; + std::tie(command, value) = bindingIt->second; + m_CommandMouseButtonValues[command][event.Button] = value; + PublishCommand(0, command, GetCommandTotalValue(command)); } return true; @@ -90,7 +94,11 @@ bool Systems::InputSystem::OnMouseRelease(const Events::MouseRelease &event) auto bindingIt = m_MouseButtonBindings.find(event.Button); if (bindingIt != m_MouseButtonBindings.end()) { - PublishCommand(0, bindingIt->second, 1.f); + std::string command; + float value; + std::tie(command, value) = bindingIt->second; + m_CommandMouseButtonValues[command][event.Button] = 0; + PublishCommand(0, command, GetCommandTotalValue(command)); } return true; @@ -104,7 +112,8 @@ bool Systems::InputSystem::OnGamepadAxis(const Events::GamepadAxis &event) std::string command; float value; std::tie(command, value) = bindingIt->second; - PublishCommand(event.GamepadID + 1, command, event.Value * value); + m_CommandGamepadAxisValues[command][event.Axis] = event.Value * value; + PublishCommand(event.GamepadID + 1, command, GetCommandTotalValue(command)); } return true; @@ -118,8 +127,8 @@ bool Systems::InputSystem::OnGamepadButtonDown(const Events::GamepadButtonDown & std::string command; float value; std::tie(command, value) = bindingIt->second; - m_CommandValues[command] += value; - PublishCommand(event.GamepadID + 1, command, std::max(-1.f, std::min(m_CommandValues[command], 1.f))); + m_CommandGamepadButtonValues[command][event.Button] = value; + PublishCommand(event.GamepadID + 1, command, GetCommandTotalValue(command)); } return true; @@ -133,8 +142,8 @@ bool Systems::InputSystem::OnGamepadButtonUp(const Events::GamepadButtonUp &even std::string command; float value; std::tie(command, value) = bindingIt->second; - m_CommandValues[command] -= value; - PublishCommand(event.GamepadID + 1, command, std::max(-1.f, std::min(m_CommandValues[command], 1.f))); + m_CommandGamepadButtonValues[command][event.Button] = 0; + PublishCommand(event.GamepadID + 1, command, GetCommandTotalValue(command)); } return true; @@ -164,7 +173,7 @@ bool Systems::InputSystem::OnBindMouseButton(const Events::BindMouseButton &even } else { - m_MouseButtonBindings[event.Button] = event.Command; + m_MouseButtonBindings[event.Button] = std::make_tuple(event.Command, event.Value); LOG_DEBUG("Input: Bound mouse button %i to %s", event.Button, event.Command.c_str()); } @@ -201,6 +210,49 @@ bool Systems::InputSystem::OnBindGamepadButton(const Events::BindGamepadButton & return true; } +float Systems::InputSystem::GetCommandTotalValue(std::string command) +{ + float value = 0.f; + + auto keyboardIt = m_CommandKeyboardValues.find(command); + if (keyboardIt != m_CommandKeyboardValues.end()) + { + for (auto &key : keyboardIt->second) + { + value += key.second; + } + } + + auto mouseButtonIt = m_CommandMouseButtonValues.find(command); + if (mouseButtonIt != m_CommandMouseButtonValues.end()) + { + for (auto &button : mouseButtonIt->second) + { + value += button.second; + } + } + + auto gamepadAxisIt = m_CommandGamepadAxisValues.find(command); + if (gamepadAxisIt != m_CommandGamepadAxisValues.end()) + { + for (auto &axis : gamepadAxisIt->second) + { + value += axis.second; + } + } + + auto gamepadButtonIt = m_CommandGamepadButtonValues.find(command); + if (gamepadButtonIt != m_CommandGamepadButtonValues.end()) + { + for (auto &button : gamepadButtonIt->second) + { + value += button.second; + } + } + + return std::max(-1.f, std::min(value, 1.f)); +} + void Systems::InputSystem::PublishCommand(int playerID, std::string command, float value) { Events::InputCommand e; @@ -211,5 +263,3 @@ void Systems::InputSystem::PublishCommand(int playerID, std::string command, flo LOG_DEBUG("Input: Published command %s=%f for player %i", e.Command.c_str(), e.Value, playerID); } - - diff --git a/src/Systems/InputSystem.h b/src/Systems/InputSystem.h index ce9a7f3..d13c249 100755 --- a/src/Systems/InputSystem.h +++ b/src/Systems/InputSystem.h @@ -34,10 +34,13 @@ public: void Update(double dt) override; private: - std::unordered_map m_CommandValues; // command string -> command current value + std::unordered_map> m_CommandKeyboardValues; // command string -> keyboard key value for command + std::unordered_map> m_CommandMouseButtonValues; // command string -> mouse button value for command + std::unordered_map> m_CommandGamepadAxisValues; // command string -> gamepad axis value for command + std::unordered_map> m_CommandGamepadButtonValues; // command string -> gamepad button value for command // Input binding tables std::unordered_map> m_KeyBindings; // GLFW_KEY... -> command string & value - std::unordered_map m_MouseButtonBindings; // GLFW_MOUSE_BUTTON... -> command string + std::unordered_map> m_MouseButtonBindings; // GLFW_MOUSE_BUTTON... -> command string std::unordered_map> m_GamepadAxisBindings; // Gamepad::Axis -> command string & value std::unordered_map> m_GamepadButtonBindings; // Gamepad::Button -> command string @@ -66,6 +69,7 @@ private: EventRelay m_EBindGamepadButton; bool OnBindGamepadButton(const Events::BindGamepadButton &event); + float GetCommandTotalValue(std::string command); void PublishCommand(int playerID, std::string command, float value); }; From d98badc59a45f602e2833ecec460a3dca2c851cd Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Sat, 17 May 2014 00:57:24 +0200 Subject: [PATCH 20/22] Latest assets --- assets | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/assets b/assets index 15ac025..5b022cc 160000 --- a/assets +++ b/assets @@ -1 +1 @@ -Subproject commit 15ac02523ad374b4aaf60a16db89b1934de7f303 +Subproject commit 5b022ccb9bbe502e14556831a41636b602260574 From add27aa8f4dfa721474ac3bdb71d97b1b5016135 Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Sat, 17 May 2014 01:21:40 +0200 Subject: [PATCH 21/22] Back to working order --- src/GameWorld.cpp | 65 ++++++++++++++++++++--------------------------- 1 file changed, 28 insertions(+), 37 deletions(-) diff --git a/src/GameWorld.cpp b/src/GameWorld.cpp index 16dec14..f0c5999 100755 --- a/src/GameWorld.cpp +++ b/src/GameWorld.cpp @@ -14,19 +14,8 @@ void GameWorld::Initialize() BindKey(GLFW_KEY_D, "horizontal", 1.f); BindGamepadAxis(Gamepad::Axis::LeftX, "horizontal", 1.f); BindGamepadAxis(Gamepad::Axis::LeftY, "vertical", 1.f); - BindGamepadAxis(Gamepad::Axis::RightX, "horizontal", 1.f); - BindGamepadAxis(Gamepad::Axis::RightY, "vertical", 1.f); - BindGamepadAxis(Gamepad::Axis::RightTrigger, "normal", 1.f); - BindGamepadAxis(Gamepad::Axis::LeftTrigger, "normal", -1.f); - BindKey(GLFW_KEY_SPACE, "normal", 1.f); - BindKey(GLFW_KEY_LEFT_CONTROL, "normal", -1.f); - BindKey(GLFW_KEY_LEFT_SHIFT, "speed", 1.f); - BindKey(GLFW_KEY_LEFT_ALT, "speed", -1.f); - - BindMouseButton(GLFW_MOUSE_BUTTON_1, "attack", 1.f); - - /*BindKey(GLFW_KEY_UP, "barrel_rotation", 1.f); + BindKey(GLFW_KEY_UP, "barrel_rotation", 1.f); BindKey(GLFW_KEY_DOWN, "barrel_rotation", -1.f); BindKey(GLFW_KEY_LEFT, "tower_rotation", -1.f); BindKey(GLFW_KEY_RIGHT, "tower_rotation", 1.f); @@ -36,7 +25,8 @@ void GameWorld::Initialize() BindKey(GLFW_KEY_SPACE, "handbrake", 1.f); BindGamepadButton(Gamepad::Button::A, "handbrake", 1.f); - BindKey(GLFW_KEY_Z, "shoot", 1.f);*/ + BindKey(GLFW_KEY_Z, "shoot", 1.f); + BindGamepadAxis(Gamepad::Axis::RightTrigger, "shoot", 1.f); //BindGamepadButton(Gamepad::Button::Up, "Gamepad::Button::Up", 1.f); //BindGamepadButton(Gamepad::Button::Down, "Gamepad::Button::Down", 1.f); @@ -55,17 +45,17 @@ void GameWorld::Initialize() RegisterComponents(); - { - auto camera = CreateEntity(); - auto transform = AddComponent(camera, "Transform"); - transform->Position.z = 20.f; - transform->Position.y = 20.f; - //transform->Orientation = glm::quat(glm::vec3(glm::pi() / 8.f, 0.f, 0.f)); - auto cameraComp = AddComponent(camera, "Camera"); - cameraComp->FarClip = 2000.f; - auto freeSteering = AddComponent(camera, "FreeSteering"); - CommitEntity(camera); - } + //{ + // auto camera = CreateEntity(); + // auto transform = AddComponent(camera, "Transform"); + // transform->Position.z = 20.f; + // transform->Position.y = 20.f; + // //transform->Orientation = glm::quat(glm::vec3(glm::pi() / 8.f, 0.f, 0.f)); + // auto cameraComp = AddComponent(camera, "Camera"); + // cameraComp->FarClip = 2000.f; + // auto freeSteering = AddComponent(camera, "FreeSteering"); + // CommitEntity(camera); + //} { @@ -307,6 +297,20 @@ void GameWorld::Initialize() } CommitEntity(barrel); } + + { + auto camera = CreateEntity(tower); + auto transform = AddComponent(camera, "Transform"); + transform->Position.z = 30.f; + transform->Position.y = 5.f; + //transform->Orientation = glm::quat(glm::vec3(-glm::pi() / 8.f, 0.f, 0.f)); + transform->Orientation = glm::angleAxis(glm::pi() / 100, glm::vec3(1, 0, 0)); + auto cameraComp = AddComponent(camera, "Camera"); + cameraComp->FarClip = 2000.f; + AddComponent(camera, "Input"); + //auto freeSteering = AddComponent(camera, "FreeSteering"); + CommitEntity(camera); + } } { @@ -622,19 +626,6 @@ void GameWorld::Initialize() } CommitEntity(tank); - { - auto camera = CreateEntity(tank); - auto transform = AddComponent(camera, "Transform"); - transform->Position.z = 30.f; - transform->Position.y = 5.f; - //transform->Orientation = glm::quat(glm::vec3(-glm::pi() / 8.f, 0.f, 0.f)); - transform->Orientation = glm::angleAxis(glm::pi() / 100, glm::vec3(1,0,0)); - auto cameraComp = AddComponent(camera, "Camera"); - cameraComp->FarClip = 2000.f; - AddComponent(camera, "Input"); - auto freeSteering = AddComponent(camera, "FreeSteering"); - CommitEntity(camera); - } } From 542ad075ea9f55c753e030dd9e3472e0bcdd3180 Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Sat, 17 May 2014 17:46:17 +0200 Subject: [PATCH 22/22] ECS improved to make it less annoying to use AddComponent(entity, "Transform") is now AddComponent(entity) etc. --- src/Factory.h | 24 ++- src/GameWorld.cpp | 294 ++++++++++++++--------------- src/Physics/VehicleSetup.cpp | 6 +- src/Systems/FreeSteeringSystem.cpp | 6 +- src/Systems/InputSystem.cpp | 2 +- src/Systems/ParticleSystem.cpp | 24 +-- src/Systems/PhysicsSystem.cpp | 48 ++--- src/Systems/RenderSystem.cpp | 24 +-- src/Systems/SoundSystem.cpp | 6 +- src/Systems/TankSteeringSystem.cpp | 22 +-- src/Systems/TransformSystem.cpp | 16 +- src/World.cpp | 12 +- src/World.h | 40 ++-- 13 files changed, 268 insertions(+), 256 deletions(-) diff --git a/src/Factory.h b/src/Factory.h index adcfbbf..80649ee 100755 --- a/src/Factory.h +++ b/src/Factory.h @@ -10,12 +10,18 @@ template class Factory { public: - void Register(std::string name, std::function factoryFunction) + /*void Register(std::string name, std::function factoryFunction) { m_FactoryFunctions[name] = factoryFunction; + }*/ + + template + void Register(std::function factoryFunction) + { + m_FactoryFunctions[typeid(T2).name()] = factoryFunction; } - T Create(std::string name) + /*T Create(std::string name) { auto it = m_FactoryFunctions.find(name); if (it != m_FactoryFunctions.end()) @@ -26,6 +32,20 @@ public: { return nullptr; } + }*/ + + template + T Create() + { + auto it = m_FactoryFunctions.find(typeid(T2).name()); + if (it != m_FactoryFunctions.end()) + { + return it->second(); + } + else + { + return nullptr; + } } private: diff --git a/src/GameWorld.cpp b/src/GameWorld.cpp index f0c5999..85a2869 100755 --- a/src/GameWorld.cpp +++ b/src/GameWorld.cpp @@ -47,35 +47,35 @@ void GameWorld::Initialize() //{ // auto camera = CreateEntity(); - // auto transform = AddComponent(camera, "Transform"); + // auto transform = AddComponent(camera); // transform->Position.z = 20.f; // transform->Position.y = 20.f; // //transform->Orientation = glm::quat(glm::vec3(glm::pi() / 8.f, 0.f, 0.f)); - // auto cameraComp = AddComponent(camera, "Camera"); + // auto cameraComp = AddComponent(camera); // cameraComp->FarClip = 2000.f; - // auto freeSteering = AddComponent(camera, "FreeSteering"); + // auto freeSteering = AddComponent(camera); // CommitEntity(camera); //} { auto ground = CreateEntity(); - auto transform = AddComponent(ground, "Transform"); + auto transform = AddComponent(ground); transform->Position = glm::vec3(0, 0, 0); //transform->Scale = glm::vec3(400.0f, 10.0f, 400.0f); transform->Orientation = glm::quat(glm::vec3(0.0f, 0.0f, 0.0f)); - auto model = AddComponent(ground, "Model"); + auto model = AddComponent(ground); //model->ModelFile = "Models/TestScene/testScene.obj"; model->ModelFile = "Models/Placeholders/Terrain/Terrain2.obj"; - auto physics = AddComponent(ground, "Physics"); + auto physics = AddComponent(ground); physics->Mass = 10; physics->Static = true; auto groundshape = CreateEntity(ground); - auto transformshape = AddComponent(groundshape, "Transform"); - auto meshShape = AddComponent(groundshape, "MeshShape"); + auto transformshape = AddComponent(groundshape); + auto meshShape = AddComponent(groundshape); meshShape->ResourceName = "Models/Placeholders/Terrain/Terrain2.obj"; //meshShape->ResourceName = "Models/TestScene/testScene.obj"; @@ -88,23 +88,23 @@ void GameWorld::Initialize() /*{ auto jeep = CreateEntity(); - auto transform = AddComponent(jeep, "Transform"); + auto transform = AddComponent(jeep); transform->Position = glm::vec3(0, 5, 0); transform->Orientation = glm::angleAxis(glm::pi()/2, glm::vec3(0, 1, 0)); - auto physics = AddComponent(jeep, "Physics"); + auto physics = AddComponent(jeep); physics->Mass = 1800; physics->Static = false; - auto vehicle = AddComponent(jeep, "Vehicle"); - AddComponent(jeep, "Input"); + auto vehicle = AddComponent(jeep); + AddComponent(jeep); { auto shape = CreateEntity(jeep); - auto transform = AddComponent(shape, "Transform"); - auto meshShape = AddComponent(shape, "MeshShape"); + auto transform = AddComponent(shape); + auto meshShape = AddComponent(shape); meshShape->ResourceName = "Models/Jeep/Chassi/ChassiCollision.obj"; CommitEntity(shape); - // auto box = AddComponent(jeep, "Box"); + // auto box = AddComponent(jeep); // box->Width = 1.487f; // box->Height = 0.727f; // box->Depth = 2.594f; @@ -113,17 +113,17 @@ void GameWorld::Initialize() { auto chassis = CreateEntity(jeep); - auto transform = AddComponent(chassis, "Transform"); + auto transform = AddComponent(chassis); transform->Position = glm::vec3(0, 0, 0); // 0.6577f - auto model = AddComponent(chassis, "Model"); + auto model = AddComponent(chassis); model->ModelFile = "Models/Jeep/Chassi/chassi.obj"; } { auto lightentity = CreateEntity(jeep); - auto transform = AddComponent(lightentity, "Transform"); + auto transform = AddComponent(lightentity); transform->Position = glm::vec3(0, 15, 0); - auto light = AddComponent(lightentity, "PointLight"); + auto light = AddComponent(lightentity); light->Diffuse = glm::vec3(128.f/255.f, 172.f/255.f, 242.f/255.f); light->Specular = glm::vec3(1.f); light->constantAttenuation = 0.3f; @@ -138,12 +138,12 @@ void GameWorld::Initialize() float suspensionStrength = 35.f; { auto wheel = CreateEntity(jeep); - auto transform = AddComponent(wheel, "Transform"); + auto transform = AddComponent(wheel); transform->Position = glm::vec3(1.9f, 0.5546f - wheelOffset, -0.9242f); transform->Scale = glm::vec3(1.0f); - auto model = AddComponent(wheel, "Model"); + auto model = AddComponent(wheel); model->ModelFile = "Models/Jeep/WheelFront/wheelFront.obj"; - auto Wheel = AddComponent(wheel, "Wheel"); + auto Wheel = AddComponent(wheel); Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); Wheel->AxleID = 0; Wheel->Mass = 50; @@ -157,13 +157,13 @@ void GameWorld::Initialize() { auto wheel = CreateEntity(jeep); - auto transform = AddComponent(wheel, "Transform"); + auto transform = AddComponent(wheel); transform->Position = glm::vec3(-1.9f, 0.5546f - wheelOffset, -0.9242f); transform->Scale = glm::vec3(1.0f); transform->Orientation = glm::angleAxis(glm::pi(), glm::vec3(0, 0, 1)); - auto model = AddComponent(wheel, "Model"); + auto model = AddComponent(wheel); model->ModelFile = "Models/Jeep/WheelFront/wheelFront.obj"; - auto Wheel = AddComponent(wheel, "Wheel"); + auto Wheel = AddComponent(wheel); Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); Wheel->AxleID = 0; Wheel->Mass = 50; @@ -177,11 +177,11 @@ void GameWorld::Initialize() { auto wheel = CreateEntity(jeep); - auto transform = AddComponent(wheel, "Transform"); + auto transform = AddComponent(wheel); transform->Position = glm::vec3(0.2726f, 0.2805f - wheelOffset, 1.9307f); - auto model = AddComponent(wheel, "Model"); + auto model = AddComponent(wheel); model->ModelFile = "Models/Jeep/WheelBack/wheelBack.obj"; - auto Wheel = AddComponent(wheel, "Wheel"); + auto Wheel = AddComponent(wheel); Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); Wheel->AxleID = 1; Wheel->Mass = 50; @@ -195,12 +195,12 @@ void GameWorld::Initialize() { auto wheel = CreateEntity(jeep); - auto transform = AddComponent(wheel, "Transform"); + auto transform = AddComponent(wheel); transform->Position = glm::vec3(-0.2726f, 0.2805f - wheelOffset, 1.9307f); transform->Orientation = glm::angleAxis(glm::pi(), glm::vec3(0, 0, 1)); - auto model = AddComponent(wheel, "Model"); + auto model = AddComponent(wheel); model->ModelFile = "Models/Jeep/WheelBack/wheelBack.obj"; - auto Wheel = AddComponent(wheel, "Wheel"); + auto Wheel = AddComponent(wheel); Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); Wheel->AxleID = 1; Wheel->Mass = 50; @@ -218,25 +218,25 @@ void GameWorld::Initialize() { auto tank = CreateEntity(); - auto transform = AddComponent(tank, "Transform"); + auto transform = AddComponent(tank); transform->Position = glm::vec3(0, 5, 0); //transform->Orientation = glm::angleAxis(0.f, glm::vec3(0, 1, 0)); - auto physics = AddComponent(tank, "Physics"); + auto physics = AddComponent(tank); physics->Mass = 45000; physics->Static = false; - auto vehicle = AddComponent(tank, "Vehicle"); + auto vehicle = AddComponent(tank); vehicle->MaxTorque = 5200.f; - AddComponent(tank, "TankSteering"); - AddComponent(tank, "Input"); + AddComponent(tank); + AddComponent(tank); { auto shape = CreateEntity(tank); - auto transform = AddComponent(shape, "Transform"); - auto meshShape = AddComponent(shape, "MeshShape"); + auto transform = AddComponent(shape); + auto meshShape = AddComponent(shape); meshShape->ResourceName = "Models/Tank/Fix/ChassiCollision.obj"; CommitEntity(shape); - // auto box = AddComponent(jeep, "Box"); + // auto box = AddComponent(jeep); // box->Width = 1.487f; // box->Height = 0.727f; // box->Depth = 2.594f; @@ -245,48 +245,48 @@ void GameWorld::Initialize() { auto chassis = CreateEntity(tank); - auto transform = AddComponent(chassis, "Transform"); + auto transform = AddComponent(chassis); transform->Position = glm::vec3(0, 0, 0); - auto model = AddComponent(chassis, "Model"); + auto model = AddComponent(chassis); model->ModelFile = "Models/Tank/Fix/Chassi.obj"; } { auto tower = CreateEntity(tank); SetProperty(tower, "Name", "tower"); - auto transform = AddComponent(tower, "Transform"); + auto transform = AddComponent(tower); transform->Position = glm::vec3(0.f, 1.2f, 1.8f); - auto model = AddComponent(tower, "Model"); + auto model = AddComponent(tower); model->ModelFile = "Models/Tank/Fix/Top.obj"; - auto towerSteering = AddComponent(tower, "TowerSteering"); + auto towerSteering = AddComponent(tower); towerSteering->Axis = glm::vec3(0.f, 1.f, 0.f); towerSteering->TurnSpeed = glm::pi()/4.f; { auto barrel = CreateEntity(tower); - auto transform = AddComponent(barrel, "Transform"); + auto transform = AddComponent(barrel); transform->Position = glm::vec3(-0.018f, -0.2, -1.3f); - auto model = AddComponent(barrel, "Model"); + auto model = AddComponent(barrel); model->ModelFile = "Models/Tank/Fix/Barrel.obj"; - auto barrelSteering = AddComponent(barrel, "BarrelSteering"); + auto barrelSteering = AddComponent(barrel); barrelSteering->Axis = glm::vec3(1.f, 0.f, 0.f); barrelSteering->TurnSpeed = glm::pi()/4.f; barrelSteering->ShotSpeed = 70.f; { auto shot = CreateEntity(barrel); - auto transform = AddComponent(shot, "Transform"); + auto transform = AddComponent(shot); transform->Position = glm::vec3(0.35f, 0.f, -2.f); transform->Orientation = glm::angleAxis(-glm::pi()/2.f, glm::vec3(1, 0, 0)); transform->Scale = glm::vec3(3.f); - AddComponent(shot, "Template"); - auto physics = AddComponent(shot, "Physics"); + AddComponent(shot); + auto physics = AddComponent(shot); physics->Mass = 10.f; physics->Static = false; - auto modelComponent = AddComponent(shot, "Model"); + auto modelComponent = AddComponent(shot); modelComponent->ModelFile = "Models/Placeholders/rocket/Rocket.obj"; { auto shape = CreateEntity(shot); - auto transform = AddComponent(shape, "Transform"); - auto boxShape = AddComponent(shape, "BoxShape"); + auto transform = AddComponent(shape); + auto boxShape = AddComponent(shape); boxShape->Width = 0.5f; boxShape->Height = 0.5f; boxShape->Depth = 0.5f; @@ -300,24 +300,24 @@ void GameWorld::Initialize() { auto camera = CreateEntity(tower); - auto transform = AddComponent(camera, "Transform"); + auto transform = AddComponent(camera); transform->Position.z = 30.f; transform->Position.y = 5.f; //transform->Orientation = glm::quat(glm::vec3(-glm::pi() / 8.f, 0.f, 0.f)); transform->Orientation = glm::angleAxis(glm::pi() / 100, glm::vec3(1, 0, 0)); - auto cameraComp = AddComponent(camera, "Camera"); + auto cameraComp = AddComponent(camera); cameraComp->FarClip = 2000.f; - AddComponent(camera, "Input"); - //auto freeSteering = AddComponent(camera, "FreeSteering"); + AddComponent(camera); + //auto freeSteering = AddComponent(camera); CommitEntity(camera); } } { auto lightentity = CreateEntity(tank); - auto transform = AddComponent(lightentity, "Transform"); + auto transform = AddComponent(lightentity); transform->Position = glm::vec3(0, 0, 0); - auto light = AddComponent(lightentity, "PointLight"); + auto light = AddComponent(lightentity); //light->Diffuse = glm::vec3(128.f/255.f, 172.f/255.f, 242.f/255.f); //light->Specular = glm::vec3(1.f); /*light->ConstantAttenuation = 0.3f; @@ -336,12 +336,12 @@ void GameWorld::Initialize() { auto wheel = CreateEntity(tank); - auto transform = AddComponent(wheel, "Transform"); + auto transform = AddComponent(wheel); transform->Position = glm::vec3(1.68f, -0.83f - wheelOffset, -2.6f); transform->Orientation = glm::angleAxis(glm::pi(), glm::vec3(0, 1, 0)); - auto model = AddComponent(wheel, "Model"); + auto model = AddComponent(wheel); model->ModelFile = "Models/Tank/Fix/WheelPhysics.obj"; - auto Wheel = AddComponent(wheel, "Wheel"); + auto Wheel = AddComponent(wheel); Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); Wheel->AxleID = 0; Wheel->Mass = 2000; @@ -354,9 +354,9 @@ void GameWorld::Initialize() Wheel->Width = 0.6f; { auto shape = CreateEntity(wheel); - auto shapetransform = AddComponent(shape, "Transform"); + auto shapetransform = AddComponent(shape); shapetransform->Position = glm::vec3(0.f, 0.32f, 0.f) + transform->Position; - auto boxShape = AddComponent(shape, "BoxShape"); + auto boxShape = AddComponent(shape); boxShape->Width = 0.7f; boxShape->Height = 0.34f; boxShape->Depth = 0.7f; @@ -366,12 +366,12 @@ void GameWorld::Initialize() } { auto wheel = CreateEntity(tank); - auto transform = AddComponent(wheel, "Transform"); + auto transform = AddComponent(wheel); transform->Position = glm::vec3(1.68f, -0.83f - wheelOffset, -0.83f); transform->Orientation = glm::angleAxis(glm::pi(), glm::vec3(0, 1, 0)); - auto model = AddComponent(wheel, "Model"); + auto model = AddComponent(wheel); model->ModelFile = "Models/Tank/Fix/WheelPhysics.obj"; - auto Wheel = AddComponent(wheel, "Wheel"); + auto Wheel = AddComponent(wheel); Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); Wheel->AxleID = 0; Wheel->Mass = 2000; @@ -384,9 +384,9 @@ void GameWorld::Initialize() Wheel->Width = 0.6f; { auto shape = CreateEntity(wheel); - auto shapetransform = AddComponent(shape, "Transform"); + auto shapetransform = AddComponent(shape); shapetransform->Position = glm::vec3(0.f, 0.32f, 0.f) + transform->Position; - auto boxShape = AddComponent(shape, "BoxShape"); + auto boxShape = AddComponent(shape); boxShape->Width = 0.7f; boxShape->Height = 0.34f; boxShape->Depth = 0.7f; @@ -397,12 +397,12 @@ void GameWorld::Initialize() { auto wheel = CreateEntity(tank); - auto transform = AddComponent(wheel, "Transform"); + auto transform = AddComponent(wheel); transform->Position = glm::vec3(-1.68f, -0.83f - wheelOffset, -2.6f); transform->Orientation = glm::angleAxis(glm::pi(), glm::vec3(0, 1, 0)); - auto model = AddComponent(wheel, "Model"); + auto model = AddComponent(wheel); model->ModelFile = "Models/Tank/Fix/WheelPhysics.obj"; - auto Wheel = AddComponent(wheel, "Wheel"); + auto Wheel = AddComponent(wheel); Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); Wheel->AxleID = 0; Wheel->Mass = 2000; @@ -415,9 +415,9 @@ void GameWorld::Initialize() Wheel->Width = 0.6f; { auto shape = CreateEntity(wheel); - auto shapetransform = AddComponent(shape, "Transform"); + auto shapetransform = AddComponent(shape); shapetransform->Position = glm::vec3(0.f, 0.32f, 0.f) + transform->Position; - auto boxShape = AddComponent(shape, "BoxShape"); + auto boxShape = AddComponent(shape); boxShape->Width = 0.7f; boxShape->Height = 0.34f; boxShape->Depth = 0.7f; @@ -427,12 +427,12 @@ void GameWorld::Initialize() } { auto wheel = CreateEntity(tank); - auto transform = AddComponent(wheel, "Transform"); + auto transform = AddComponent(wheel); transform->Position = glm::vec3(-1.68f, -0.83f - wheelOffset, -0.83f); transform->Orientation = glm::angleAxis(glm::pi(), glm::vec3(0, 1, 0)); - auto model = AddComponent(wheel, "Model"); + auto model = AddComponent(wheel); model->ModelFile = "Models/Tank/Fix/WheelPhysics.obj"; - auto Wheel = AddComponent(wheel, "Wheel"); + auto Wheel = AddComponent(wheel); Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); Wheel->AxleID = 0; Wheel->Mass = 2000; @@ -445,9 +445,9 @@ void GameWorld::Initialize() Wheel->Width = 0.6f; { auto shape = CreateEntity(wheel); - auto shapetransform = AddComponent(shape, "Transform"); + auto shapetransform = AddComponent(shape); shapetransform->Position = glm::vec3(0.f, 0.32f, 0.f) + transform->Position; - auto boxShape = AddComponent(shape, "BoxShape"); + auto boxShape = AddComponent(shape); boxShape->Width = 0.7f; boxShape->Height = 0.34f; boxShape->Depth = 0.7f; @@ -460,11 +460,11 @@ void GameWorld::Initialize() //Back { auto wheel = CreateEntity(tank); - auto transform = AddComponent(wheel, "Transform"); + auto transform = AddComponent(wheel); transform->Position = glm::vec3(1.68f, -0.83f - wheelOffset, 1.f); - auto model = AddComponent(wheel, "Model"); + auto model = AddComponent(wheel); model->ModelFile = "Models/Tank/Fix/WheelPhysics.obj"; - auto Wheel = AddComponent(wheel, "Wheel"); + auto Wheel = AddComponent(wheel); Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); Wheel->AxleID = 1; Wheel->Mass = 2000; @@ -477,9 +477,9 @@ void GameWorld::Initialize() Wheel->Width = 0.6f; { auto shape = CreateEntity(wheel); - auto shapetransform = AddComponent(shape, "Transform"); + auto shapetransform = AddComponent(shape); shapetransform->Position = glm::vec3(0.f, 0.32f, 0.f) + transform->Position; - auto boxShape = AddComponent(shape, "BoxShape"); + auto boxShape = AddComponent(shape); boxShape->Width = 0.7f; boxShape->Height = 0.34f; boxShape->Depth = 0.7f; @@ -489,11 +489,11 @@ void GameWorld::Initialize() } { auto wheel = CreateEntity(tank); - auto transform = AddComponent(wheel, "Transform"); + auto transform = AddComponent(wheel); transform->Position = glm::vec3(1.68f, -0.83f - wheelOffset, 2.95f); - auto model = AddComponent(wheel, "Model"); + auto model = AddComponent(wheel); model->ModelFile = "Models/Tank/Fix/WheelPhysics.obj"; - auto Wheel = AddComponent(wheel, "Wheel"); + auto Wheel = AddComponent(wheel); Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); Wheel->AxleID = 1; Wheel->Mass = 2000; @@ -506,9 +506,9 @@ void GameWorld::Initialize() Wheel->Width = 0.6f; { auto shape = CreateEntity(wheel); - auto shapetransform = AddComponent(shape, "Transform"); + auto shapetransform = AddComponent(shape); shapetransform->Position = glm::vec3(0.f, 0.32f, 0.f) + transform->Position; - auto boxShape = AddComponent(shape, "BoxShape"); + auto boxShape = AddComponent(shape); boxShape->Width = 0.7f; boxShape->Height = 0.34f; boxShape->Depth = 0.7f; @@ -517,11 +517,11 @@ void GameWorld::Initialize() CommitEntity(wheel); auto entity = CreateEntity(tank); - auto transformComponent = AddComponent(entity, "Transform"); + auto transformComponent = AddComponent(entity); transformComponent->Position = glm::vec3(2,-1.7,2.0); transformComponent->Scale = glm::vec3(3,3,3); transformComponent->Orientation = glm::angleAxis(glm::pi()/2, glm::vec3(1,0,0)); - auto emitterComponent = AddComponent(entity, "ParticleEmitter"); + auto emitterComponent = AddComponent(entity); emitterComponent->SpawnCount = 2; emitterComponent->SpawnFrequency = 0.005; emitterComponent->SpreadAngle = glm::pi(); @@ -532,9 +532,9 @@ void GameWorld::Initialize() CommitEntity(entity); auto particleEntity = CreateEntity(entity); - auto TEMP = AddComponent(particleEntity, "Transform"); + auto TEMP = AddComponent(particleEntity); TEMP->Scale = glm::vec3(0); - auto spriteComponent = AddComponent(particleEntity, "Sprite"); + auto spriteComponent = AddComponent(particleEntity); spriteComponent->SpriteFile = "Models/Textures/Sprites/Dust.png"; emitterComponent->ParticleTemplate = particleEntity; @@ -543,12 +543,12 @@ void GameWorld::Initialize() { auto wheel = CreateEntity(tank); - auto transform = AddComponent(wheel, "Transform"); + auto transform = AddComponent(wheel); transform->Position = glm::vec3(-1.68f, -0.83f - wheelOffset, 1.f); transform->Orientation = glm::angleAxis(glm::pi(), glm::vec3(0, 1, 0)); - auto model = AddComponent(wheel, "Model"); + auto model = AddComponent(wheel); model->ModelFile = "Models/Tank/Fix/WheelPhysics.obj"; - auto Wheel = AddComponent(wheel, "Wheel"); + auto Wheel = AddComponent(wheel); Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); Wheel->AxleID = 1; Wheel->Mass = 2000; @@ -561,9 +561,9 @@ void GameWorld::Initialize() Wheel->Width = 0.6f; { auto shape = CreateEntity(wheel); - auto shapetransform = AddComponent(shape, "Transform"); + auto shapetransform = AddComponent(shape); shapetransform->Position = glm::vec3(0.f, 0.32f, 0.f) + transform->Position; - auto boxShape = AddComponent(shape, "BoxShape"); + auto boxShape = AddComponent(shape); boxShape->Width = 0.7f; boxShape->Height = 0.34f; boxShape->Depth = 0.7f; @@ -573,11 +573,11 @@ void GameWorld::Initialize() } { auto wheel = CreateEntity(tank); - auto transform = AddComponent(wheel, "Transform"); + auto transform = AddComponent(wheel); transform->Position = glm::vec3(-1.68f, -0.83f - wheelOffset, 2.95f); - auto model = AddComponent(wheel, "Model"); + auto model = AddComponent(wheel); model->ModelFile = "Models/Tank/Fix/WheelPhysics.obj"; - auto Wheel = AddComponent(wheel, "Wheel"); + auto Wheel = AddComponent(wheel); Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); Wheel->AxleID = 1; Wheel->Mass = 2000; @@ -590,9 +590,9 @@ void GameWorld::Initialize() Wheel->Width = 0.6f; { auto shape = CreateEntity(wheel); - auto shapetransform = AddComponent(shape, "Transform"); + auto shapetransform = AddComponent(shape); shapetransform->Position = glm::vec3(0.f, 0.32f, 0.f) + transform->Position; - auto boxShape = AddComponent(shape, "BoxShape"); + auto boxShape = AddComponent(shape); boxShape->Width = 0.7f; boxShape->Height = 0.34f; boxShape->Depth = 0.7f; @@ -601,11 +601,11 @@ void GameWorld::Initialize() CommitEntity(wheel); auto entity = CreateEntity(tank); - auto transformComponent = AddComponent(entity, "Transform"); + auto transformComponent = AddComponent(entity); transformComponent->Position = glm::vec3(-2,-1.7,2.0); transformComponent->Scale = glm::vec3(3,3,3); transformComponent->Orientation = glm::angleAxis(glm::pi()/2, glm::vec3(1,0,0)); - auto emitterComponent = AddComponent(entity, "ParticleEmitter"); + auto emitterComponent = AddComponent(entity); emitterComponent->SpawnCount = 2; emitterComponent->SpawnFrequency = 0.005; emitterComponent->SpreadAngle = glm::pi(); @@ -616,9 +616,9 @@ void GameWorld::Initialize() CommitEntity(entity); auto particleEntity = CreateEntity(entity); - auto TEMP = AddComponent(particleEntity, "Transform"); + auto TEMP = AddComponent(particleEntity); TEMP->Scale = glm::vec3(0); - auto spriteComponent = AddComponent(particleEntity, "Sprite"); + auto spriteComponent = AddComponent(particleEntity); spriteComponent->SpriteFile = "Models/Textures/Sprites/Dust.png"; emitterComponent->ParticleTemplate = particleEntity; @@ -634,7 +634,7 @@ void GameWorld::Initialize() for(int i = 0; i < 10; i++) { auto entity = CreateEntity(); - auto transform = AddComponent(entity, "Transform"); + auto transform = AddComponent(entity); transform->Position = glm::vec3(30 + i*0.1f, 0 + i*0.1f, 10 + i*0.1f); transform->Scale = glm::vec3(0); transform->Orientation = glm::quat(glm::vec3(0.0f, 0.0f, 0.0f)); @@ -642,13 +642,13 @@ void GameWorld::Initialize() std::stringstream ss; ss << "Models/Placeholders/ShatterTest/" << i+1 << ".obj"; - auto model = AddComponent(entity, "Model"); + auto model = AddComponent(entity); model->ModelFile = ss.str(); - auto physics = AddComponent(entity, "Physics"); + auto physics = AddComponent(entity); physics->Mass = 100; physics->Static = true; - auto meshShape = AddComponent(entity, "MeshShape"); + auto meshShape = AddComponent(entity); meshShape->ResourceName = ss.str(); CommitEntity(entity); @@ -661,22 +661,22 @@ void GameWorld::Initialize() for (int x = -5; x < 5; x++) { auto brick = CreateEntity(); - auto transform = AddComponent(brick, "Transform"); + auto transform = AddComponent(brick); transform->Position = glm::vec3(x + 0.01f, y * 0.3f + 0.01f, -20); transform->Position.x += (y % 2)*0.5f; transform->Scale = glm::vec3(1, 0.3f, 0.4f); transform->Orientation = glm::quat(glm::vec3(0.0f, 0.0f, 0.0f)); - auto model = AddComponent(brick, "Model"); + auto model = AddComponent(brick); model->ModelFile = "Models/Placeholders/PhysicsTest/Cube2.obj"; - auto physics = AddComponent(brick, "Physics"); + auto physics = AddComponent(brick); physics->Mass = 3; auto shape = CreateEntity(brick); - auto transformshape = AddComponent(shape, "Transform"); - auto box = AddComponent(shape, "BoxShape"); + auto transformshape = AddComponent(shape); + auto box = AddComponent(shape); box->Width = 0.5f; box->Height = 0.15f; box->Depth = 0.3f; @@ -690,16 +690,16 @@ void GameWorld::Initialize() for (int y = 0; y < 5; y++) { auto cube = CreateEntity(); - auto transform = AddComponent(cube, "Transform"); + auto transform = AddComponent(cube); transform->Position = glm::vec3(3 * x + 0.1f + -20.f, 3 * y + 0.1f + 1.f, 0); transform->Scale = glm::vec3(3); transform->Orientation = glm::quat(glm::vec3(0.0f, 0.0f, 0.0f)); - auto model = AddComponent(cube, "Model"); + auto model = AddComponent(cube); model->ModelFile = "Models/Placeholders/PhysicsTest/Cube2.obj"; - auto physics = AddComponent(cube, "Physics"); + auto physics = AddComponent(cube); physics->Mass = 100; - auto box = AddComponent(cube, "BoxShape"); + auto box = AddComponent(cube); box->Width = 1.5f; box->Height = 1.5f; box->Depth = 1.5f; @@ -711,7 +711,7 @@ void GameWorld::Initialize() /*{ auto entity = CreateEntity(); AddComponent(entity, "Transform"); - auto emitter = AddComponent(entity, "SoundEmitter"); + auto emitter = AddComponent(entity); emitter->Path = "Sounds/korvring.wav"; emitter->Loop = true; GetSystem("SoundSystem")->PlaySound(emitter); @@ -727,40 +727,40 @@ void GameWorld::Update(double dt) void GameWorld::RegisterComponents() { - m_ComponentFactory.Register("Transform", []() { return new Components::Transform(); }); - m_ComponentFactory.Register("Template", []() { return new Components::Template(); }); + m_ComponentFactory.Register([]() { return new Components::Transform(); }); + m_ComponentFactory.Register([]() { return new Components::Template(); }); } void GameWorld::RegisterSystems() { - m_SystemFactory.Register("TransformSystem", [this]() { return new Systems::TransformSystem(this, m_EventBroker); }); - //m_SystemFactory.Register("LevelGenerationSystem", [this]() { return new Systems::LevelGenerationSystem(this); }); - m_SystemFactory.Register("InputSystem", [this]() { return new Systems::InputSystem(this, m_EventBroker); }); - m_SystemFactory.Register("DebugSystem", [this]() { return new Systems::DebugSystem(this, m_EventBroker); }); - //m_SystemFactory.Register("CollisionSystem", [this]() { return new Systems::CollisionSystem(this); }); - m_SystemFactory.Register("ParticleSystem", [this]() { return new Systems::ParticleSystem(this, m_EventBroker); }); - //m_SystemFactory.Register("PlayerSystem", [this]() { return new Systems::PlayerSystem(this); }); - m_SystemFactory.Register("FreeSteeringSystem", [this]() { return new Systems::FreeSteeringSystem(this, m_EventBroker); }); - m_SystemFactory.Register("TankSteeringSystem", [this]() { return new Systems::TankSteeringSystem(this, m_EventBroker); }); - m_SystemFactory.Register("SoundSystem", [this]() { return new Systems::SoundSystem(this, m_EventBroker); }); - m_SystemFactory.Register("PhysicsSystem", [this]() { return new Systems::PhysicsSystem(this, m_EventBroker); }); - m_SystemFactory.Register("RenderSystem", [this]() { return new Systems::RenderSystem(this, m_EventBroker, m_Renderer); }); + m_SystemFactory.Register([this]() { return new Systems::TransformSystem(this, m_EventBroker); }); + //m_SystemFactory.Register([this]() { return new Systems::LevelGenerationSystem(this); }); + m_SystemFactory.Register([this]() { return new Systems::InputSystem(this, m_EventBroker); }); + m_SystemFactory.Register([this]() { return new Systems::DebugSystem(this, m_EventBroker); }); + //m_SystemFactory.Register([this]() { return new Systems::CollisionSystem(this); }); + m_SystemFactory.Register([this]() { return new Systems::ParticleSystem(this, m_EventBroker); }); + //m_SystemFactory.Register([this]() { return new Systems::PlayerSystem(this); }); + m_SystemFactory.Register([this]() { return new Systems::FreeSteeringSystem(this, m_EventBroker); }); + m_SystemFactory.Register([this]() { return new Systems::TankSteeringSystem(this, m_EventBroker); }); + m_SystemFactory.Register([this]() { return new Systems::SoundSystem(this, m_EventBroker); }); + m_SystemFactory.Register([this]() { return new Systems::PhysicsSystem(this, m_EventBroker); }); + m_SystemFactory.Register([this]() { return new Systems::RenderSystem(this, m_EventBroker, m_Renderer); }); } void GameWorld::AddSystems() { - AddSystem("TransformSystem"); - //AddSystem("LevelGenerationSystem"); - AddSystem("InputSystem"); - AddSystem("DebugSystem"); - //AddSystem("CollisionSystem"); - AddSystem("ParticleSystem"); - //AddSystem("PlayerSystem"); - AddSystem("FreeSteeringSystem"); - AddSystem("TankSteeringSystem"); - AddSystem("SoundSystem"); - AddSystem("PhysicsSystem"); - AddSystem("RenderSystem"); + AddSystem(); + //AddSystem(); + AddSystem(); + AddSystem(); + //AddSystem(); + AddSystem(); + //AddSystem(); + AddSystem(); + AddSystem(); + AddSystem(); + AddSystem(); + AddSystem(); } void GameWorld::BindKey(int keyCode, std::string command, float value) diff --git a/src/Physics/VehicleSetup.cpp b/src/Physics/VehicleSetup.cpp index 90e65dc..7db47af 100644 --- a/src/Physics/VehicleSetup.cpp +++ b/src/Physics/VehicleSetup.cpp @@ -7,13 +7,13 @@ void VehicleSetup::buildVehicle(World *world, const hkpWorld* physicsWorld, hkpVehicleInstance& vehicle, EntityID vehicleEntity, std::vector wheelEntities) { - auto vehicleComponent = world->GetComponent(vehicleEntity, "Vehicle"); + auto vehicleComponent = world->GetComponent(vehicleEntity); WheelData wheelData; for (int i = 0; i < wheelEntities.size(); i++) { - wheelData.WheelComponent = world->GetComponent(wheelEntities[i], "Wheel"); - wheelData.TransformComponent = world->GetComponent(wheelEntities[i], "Transform"); + wheelData.WheelComponent = world->GetComponent(wheelEntities[i]); + wheelData.TransformComponent = world->GetComponent(wheelEntities[i]); m_Wheels.push_back(wheelData); } diff --git a/src/Systems/FreeSteeringSystem.cpp b/src/Systems/FreeSteeringSystem.cpp index edf24d0..8a5d638 100755 --- a/src/Systems/FreeSteeringSystem.cpp +++ b/src/Systems/FreeSteeringSystem.cpp @@ -4,7 +4,7 @@ void Systems::FreeSteeringSystem::RegisterComponents(ComponentFactory* cf) { - cf->Register("FreeSteering", []() { return new Components::FreeSteering(); }); + cf->Register([]() { return new Components::FreeSteering(); }); } void Systems::FreeSteeringSystem::Initialize() @@ -19,10 +19,10 @@ void Systems::FreeSteeringSystem::Update(double dt) void Systems::FreeSteeringSystem::UpdateEntity(double dt, EntityID entity, EntityID parent) { - auto steering = m_World->GetComponent(entity, "FreeSteering"); + auto steering = m_World->GetComponent(entity); if (steering) { - auto transform = m_World->GetComponent(entity, "Transform"); + auto transform = m_World->GetComponent(entity); glm::vec3 cameraRight = glm::vec3(transform->Orientation * glm::vec4(1, 0, 0, 0)); glm::vec3 cameraForward = glm::vec3(transform->Orientation * glm::vec4(0, 0, -1, 0)); diff --git a/src/Systems/InputSystem.cpp b/src/Systems/InputSystem.cpp index 4c5fa6f..dbb9ba6 100755 --- a/src/Systems/InputSystem.cpp +++ b/src/Systems/InputSystem.cpp @@ -4,7 +4,7 @@ void Systems::InputSystem::RegisterComponents(ComponentFactory* cf) { - cf->Register("Input", []() { return new Components::Input(); }); + cf->Register([]() { return new Components::Input(); }); } void Systems::InputSystem::Initialize() diff --git a/src/Systems/ParticleSystem.cpp b/src/Systems/ParticleSystem.cpp index 0882f9d..5bcd97f 100644 --- a/src/Systems/ParticleSystem.cpp +++ b/src/Systems/ParticleSystem.cpp @@ -6,7 +6,7 @@ void Systems::ParticleSystem::Initialize() { - m_TransformSystem = m_World->GetSystem("TransformSystem"); + m_TransformSystem = m_World->GetSystem(); } void Systems::ParticleSystem::Update(double dt) @@ -16,15 +16,15 @@ void Systems::ParticleSystem::Update(double dt) void Systems::ParticleSystem::UpdateEntity(double dt, EntityID entity, EntityID parent) { - auto transformComponent = m_World->GetComponent(entity, "Transform"); + auto transformComponent = m_World->GetComponent(entity); if(!transformComponent) return; - auto emitterComponent = m_World->GetComponent(entity, "ParticleEmitter"); + auto emitterComponent = m_World->GetComponent(entity); if(emitterComponent) { emitterComponent->TimeSinceLastSpawn += dt; - auto emitterTransformComponent = m_World->GetComponent(entity, "Transform"); + auto emitterTransformComponent = m_World->GetComponent(entity); if(emitterComponent->TimeSinceLastSpawn > emitterComponent->SpawnFrequency) { SpawnParticles(entity); @@ -35,8 +35,8 @@ void Systems::ParticleSystem::UpdateEntity(double dt, EntityID entity, EntityID for(it = m_ParticleEmitter[entity].begin(); it != m_ParticleEmitter[entity].end();) { EntityID particleID = (it)->ParticleID; - auto transformComponent = m_World->GetComponent(particleID, "Transform"); - auto particleComponent = m_World->GetComponent(particleID, "Particle"); + auto transformComponent = m_World->GetComponent(particleID); + auto particleComponent = m_World->GetComponent(particleID); double timeLived = glfwGetTime() - it->SpawnTime; if(timeLived > particleComponent->LifeTime) @@ -94,15 +94,15 @@ void Systems::ParticleSystem::UpdateEntity(double dt, EntityID entity, EntityID void Systems::ParticleSystem::RegisterComponents(ComponentFactory* cf) { - cf->Register("ParticleEmitter", []() { return new Components::ParticleEmitter(); }); - cf->Register("Particle", []() { return new Components::Particle(); }); + cf->Register([]() { return new Components::ParticleEmitter(); }); + cf->Register([]() { return new Components::Particle(); }); } void Systems::ParticleSystem::SpawnParticles(EntityID emitterID) { - auto emitterComponent = m_World->GetComponent(emitterID, "ParticleEmitter"); - auto emitterTransform = m_World->GetComponent(emitterID, "Transform"); + auto emitterComponent = m_World->GetComponent(emitterID); + auto emitterTransform = m_World->GetComponent(emitterID); glm::vec3 emitterPos = m_TransformSystem->AbsolutePosition(emitterID); glm::quat emitterOrientation = emitterTransform->Orientation; @@ -113,7 +113,7 @@ void Systems::ParticleSystem::SpawnParticles(EntityID emitterID) { auto ent = m_World->CloneEntity(emitterComponent->ParticleTemplate); - auto particleTransform = m_World->GetComponent(ent, "Transform"); + auto particleTransform = m_World->GetComponent(ent); particleTransform->Position = emitterPos; particleTransform->Orientation = emitterOrientation; @@ -124,7 +124,7 @@ void Systems::ParticleSystem::SpawnParticles(EntityID emitterID) glm::normalize(glm::angleAxis(RandomizeAngle(spreadAngle), glm::vec3(0, 1, 0))) * glm::normalize(glm::angleAxis(RandomizeAngle(spreadAngle), glm::vec3(0, 0, 1))); - auto particle = m_World->AddComponent(ent, "Particle"); + auto particle = m_World->AddComponent(ent); particle->LifeTime = emitterComponent->LifeTime; particle->ScaleSpectrum = emitterComponent->ScaleSpectrum; particle->VelocitySpectrum.push_back(particleTransform->Velocity); diff --git a/src/Systems/PhysicsSystem.cpp b/src/Systems/PhysicsSystem.cpp index 8efa4bb..1fd737b 100644 --- a/src/Systems/PhysicsSystem.cpp +++ b/src/Systems/PhysicsSystem.cpp @@ -108,14 +108,14 @@ void Systems::PhysicsSystem::Initialize() void Systems::PhysicsSystem::RegisterComponents(ComponentFactory* cf) { - cf->Register("Physics", []() { return new Components::Physics(); }); - cf->Register("BoxShape", []() { return new Components::BoxShape(); }); - cf->Register("SphereShape", []() { return new Components::SphereShape(); }); - cf->Register("Vehicle", []() { return new Components::Vehicle(); }); - cf->Register("Wheel", []() { return new Components::Wheel(); }); - cf->Register("MeshShape", []() { return new Components::MeshShape(); }); - cf->Register("HingeConstraint", []() { return new Components::HingeConstraint(); }); - cf->Register("WheelPair", []() { return new Components::WheelPair(); }); + cf->Register([]() { return new Components::Physics(); }); + cf->Register([]() { return new Components::BoxShape(); }); + cf->Register([]() { return new Components::SphereShape(); }); + cf->Register([]() { return new Components::Vehicle(); }); + cf->Register([]() { return new Components::Wheel(); }); + cf->Register([]() { return new Components::MeshShape(); }); + cf->Register([]() { return new Components::HingeConstraint(); }); + cf->Register([]() { return new Components::WheelPair(); }); } void Systems::PhysicsSystem::Update(double dt) @@ -128,7 +128,7 @@ void Systems::PhysicsSystem::Update(double dt) if (m_RigidBodies.find(entity) == m_RigidBodies.end()) continue; - auto transformComponent = m_World->GetComponent(entity, "Transform"); + auto transformComponent = m_World->GetComponent(entity); if (!transformComponent) continue; @@ -139,7 +139,7 @@ void Systems::PhysicsSystem::Update(double dt) if (parent) { - auto absoluteTransform = m_World->GetSystem("TransformSystem")->AbsoluteTransform(entity); + auto absoluteTransform = m_World->GetSystem()->AbsoluteTransform(entity); position = ConvertPosition(absoluteTransform.Position); rotation = ConvertRotation(absoluteTransform.Orientation); } @@ -180,11 +180,11 @@ void Systems::PhysicsSystem::Update(double dt) void Systems::PhysicsSystem::UpdateEntity(double dt, EntityID entity, EntityID parent) { - auto transformComponent = m_World->GetComponent(entity, "Transform"); + auto transformComponent = m_World->GetComponent(entity); if (!transformComponent) return; - auto wheelComponent = m_World->GetComponent(entity, "Wheel"); + auto wheelComponent = m_World->GetComponent(entity); if (wheelComponent) { EntityID car = m_World->GetEntityParent(entity); @@ -208,7 +208,7 @@ void Systems::PhysicsSystem::UpdateEntity(double dt, EntityID entity, EntityID p } else if(m_RigidBodies.find(entity) != m_RigidBodies.end()) { - auto transformComponentParent = m_World->GetComponent(parent, "Transform"); + auto transformComponentParent = m_World->GetComponent(parent); transformComponent->Position = ConvertPosition(m_RigidBodies[entity]->getPosition()); transformComponent->Orientation = ConvertRotation(m_RigidBodies[entity]->getRotation()); @@ -227,11 +227,11 @@ void Systems::PhysicsSystem::UpdateEntity(double dt, EntityID entity, EntityID p void Systems::PhysicsSystem::OnEntityCommit( EntityID entity ) { - auto transformComponent = m_World->GetComponent(entity, "Transform"); + auto transformComponent = m_World->GetComponent(entity); if (!transformComponent) return; - auto wheelComponent = m_World->GetComponent(entity, "Wheel"); + auto wheelComponent = m_World->GetComponent(entity); if (wheelComponent) { wheelComponent->ID = m_Wheels.size(); @@ -241,9 +241,9 @@ void Systems::PhysicsSystem::OnEntityCommit( EntityID entity ) EntityID entityParent = m_World->GetEntityBaseParent(entity); - auto sphereComponent = m_World->GetComponent(entity, "SphereShape"); - auto boxComponent = m_World->GetComponent(entity, "BoxShape"); - auto meshShapeComponent = m_World->GetComponent(entity, "MeshShape"); + auto sphereComponent = m_World->GetComponent(entity); + auto boxComponent = m_World->GetComponent(entity); + auto meshShapeComponent = m_World->GetComponent(entity); if(entityParent == entity && (sphereComponent || boxComponent || meshShapeComponent)) { @@ -251,7 +251,7 @@ void Systems::PhysicsSystem::OnEntityCommit( EntityID entity ) return; } - auto physicsComponent = m_World->GetComponent(entity, "Physics"); + auto physicsComponent = m_World->GetComponent(entity); if (physicsComponent) { hkpShape* shape; @@ -292,7 +292,7 @@ void Systems::PhysicsSystem::OnEntityCommit( EntityID entity ) { rigidBodyInfo.m_shape = shape; rigidBodyInfo.m_motionType = hkpMotion::MOTION_DYNAMIC; - auto absoluteTransform = m_World->GetSystem("TransformSystem")->AbsoluteTransform(entity); + auto absoluteTransform = m_World->GetSystem()->AbsoluteTransform(entity); hkVector4 position = ConvertPosition(absoluteTransform.Position); hkQuaternion rotation = ConvertRotation(absoluteTransform.Orientation); rigidBodyInfo.m_position.set(position(0), position(1), position(2), position(3)); @@ -305,7 +305,7 @@ void Systems::PhysicsSystem::OnEntityCommit( EntityID entity ) // Create RigidBody hkpRigidBody* rigidBody = new hkpRigidBody(rigidBodyInfo); - auto vehicleComponent = m_World->GetComponent(entity, "Vehicle"); + auto vehicleComponent = m_World->GetComponent(entity); if (vehicleComponent && m_Vehicles.find(entity) == m_Vehicles.end()) { for (int i = 0; i < m_Wheels.size(); i++) @@ -357,7 +357,7 @@ void Systems::PhysicsSystem::OnEntityCommit( EntityID entity ) for (auto &shapeData : m_Shapes[entity]) { - auto childTransformComponent = m_World->GetComponent(shapeData.Entity, "Transform"); + auto childTransformComponent = m_World->GetComponent(shapeData.Entity); hkVector4 position = ConvertPosition(childTransformComponent->Position); hkQuaternion rotation = ConvertRotation(childTransformComponent->Orientation); @@ -378,7 +378,7 @@ void Systems::PhysicsSystem::OnEntityCommit( EntityID entity ) { rigidBodyInfo.m_shape = shape; rigidBodyInfo.m_motionType = hkpMotion::MOTION_FIXED; - auto absoluteTransform = m_World->GetSystem("TransformSystem")->AbsoluteTransform(entity); + auto absoluteTransform = m_World->GetSystem()->AbsoluteTransform(entity); hkVector4 position = ConvertPosition(absoluteTransform.Position); hkQuaternion rotation = ConvertRotation(absoluteTransform.Orientation); rigidBodyInfo.m_position.set(position(0), position(1), position(2), position(3)); @@ -567,7 +567,7 @@ const hkVector4& Systems::PhysicsSystem::ConvertScale(glm::vec3 glmScale) bool Systems::PhysicsSystem::OnTankSteer(const Events::TankSteer &event) { - auto vehicleComponent = m_World->GetComponent(event.Entity, "Vehicle"); + auto vehicleComponent = m_World->GetComponent(event.Entity); if (vehicleComponent && m_Vehicles.find(event.Entity) != m_Vehicles.end() && m_RigidBodies.find(event.Entity) != m_RigidBodies.end()) { m_PhysicsWorld->markForWrite(); diff --git a/src/Systems/RenderSystem.cpp b/src/Systems/RenderSystem.cpp index 4f0e727..1166531 100755 --- a/src/Systems/RenderSystem.cpp +++ b/src/Systems/RenderSystem.cpp @@ -12,12 +12,12 @@ void Systems::RenderSystem::OnComponentCreated(std::string type, std::shared_ptr void Systems::RenderSystem::UpdateEntity(double dt, EntityID entity, EntityID parent) { - auto transformComponent = m_World->GetComponent(entity, "Transform"); + auto transformComponent = m_World->GetComponent(entity); if (transformComponent == nullptr) return; // Draw models - auto modelComponent = m_World->GetComponent(entity, "Model"); + auto modelComponent = m_World->GetComponent(entity); if (modelComponent != nullptr) { auto model = m_World->GetResourceManager()->Load("Model", modelComponent->ModelFile); @@ -31,7 +31,7 @@ void Systems::RenderSystem::UpdateEntity(double dt, EntityID entity, EntityID pa } } - auto pointLightComponent = m_World->GetComponent(entity, "PointLight"); + auto pointLightComponent = m_World->GetComponent(entity); if (pointLightComponent != nullptr) { glm::vec3 position = m_TransformSystem->AbsolutePosition(entity); @@ -46,7 +46,7 @@ void Systems::RenderSystem::UpdateEntity(double dt, EntityID entity, EntityID pa ); } - auto cameraComponent = m_World->GetComponent(entity, "Camera"); + auto cameraComponent = m_World->GetComponent(entity); if (cameraComponent != nullptr) { m_Renderer->GetCamera()->Position(m_TransformSystem->AbsolutePosition(entity)); @@ -57,13 +57,13 @@ void Systems::RenderSystem::UpdateEntity(double dt, EntityID entity, EntityID pa m_Renderer->GetCamera()->FarClip(cameraComponent->FarClip); } - auto spriteComponent = m_World->GetComponent(entity, "Sprite"); + auto spriteComponent = m_World->GetComponent(entity); if(spriteComponent != nullptr) { //TEMP Texture* texture = m_World->GetResourceManager()->Load("Texture", spriteComponent->SpriteFile); //glBindTexture(GL_TEXTURE_2D, texture); - auto transform = m_World->GetComponent(spriteComponent->Entity, "Transform"); + auto transform = m_World->GetComponent(spriteComponent->Entity); glm::quat orientation2D = glm::angleAxis(glm::eulerAngles(transform->Orientation).z, glm::vec3(0, 0, -1)); m_Renderer->AddTextureToDraw(texture, transform->Position, orientation2D, transform->Scale); } @@ -71,18 +71,18 @@ void Systems::RenderSystem::UpdateEntity(double dt, EntityID entity, EntityID pa void Systems::RenderSystem::Initialize() { - m_TransformSystem = m_World->GetSystem("TransformSystem"); + m_TransformSystem = m_World->GetSystem(); m_Renderer->SetSphereModel(m_World->GetResourceManager()->Load("Model", "Models/Placeholders/PhysicsTest/Sphere.obj")); } void Systems::RenderSystem::RegisterComponents(ComponentFactory* cf) { - cf->Register("Camera", []() { return new Components::Camera(); }); - cf->Register("Model", []() { return new Components::Model(); }); - cf->Register("Sprite", []() { return new Components::Sprite(); }); - cf->Register("PointLight", []() { return new Components::PointLight(); }); - cf->Register("DirectionalLight", []() { return new Components::DirectionalLight(); }); + cf->Register([]() { return new Components::Camera(); }); + cf->Register([]() { return new Components::Model(); }); + cf->Register([]() { return new Components::Sprite(); }); + cf->Register([]() { return new Components::PointLight(); }); + cf->Register([]() { return new Components::DirectionalLight(); }); } void Systems::RenderSystem::RegisterResourceTypes(ResourceManager* rm) diff --git a/src/Systems/SoundSystem.cpp b/src/Systems/SoundSystem.cpp index 02a665e..b36bf47 100755 --- a/src/Systems/SoundSystem.cpp +++ b/src/Systems/SoundSystem.cpp @@ -29,7 +29,7 @@ void Systems::SoundSystem::Initialize() void Systems::SoundSystem::RegisterComponents(ComponentFactory* cf) { - cf->Register("SoundEmitter", []() { return new Components::SoundEmitter(); }); + cf->Register([]() { return new Components::SoundEmitter(); }); } void Systems::SoundSystem::RegisterResourceTypes(ResourceManager* rm) @@ -44,7 +44,7 @@ void Systems::SoundSystem::Update(double dt) void Systems::SoundSystem::UpdateEntity(double dt, EntityID entity, EntityID parent) { - auto transformComponent = m_World->GetComponent(entity, "Transform"); + auto transformComponent = m_World->GetComponent(entity); if (transformComponent == nullptr) return; @@ -68,7 +68,7 @@ void Systems::SoundSystem::UpdateEntity(double dt, EntityID entity, EntityID par alListenerfv(AL_ORIENTATION, listenerOri); } - auto soundEmitter = m_World->GetComponent(entity, "SoundEmitter"); + auto soundEmitter = m_World->GetComponent(entity); if(soundEmitter != nullptr) { ALuint source = m_Sources[soundEmitter]; diff --git a/src/Systems/TankSteeringSystem.cpp b/src/Systems/TankSteeringSystem.cpp index 6ecbfb9..dca3441 100644 --- a/src/Systems/TankSteeringSystem.cpp +++ b/src/Systems/TankSteeringSystem.cpp @@ -4,9 +4,9 @@ void Systems::TankSteeringSystem::RegisterComponents( ComponentFactory* cf ) { - cf->Register("TankSteering", []() { return new Components::TankSteering(); }); - cf->Register("TowerSteering", []() { return new Components::TowerSteering(); }); - cf->Register("BarrelSteering", []() { return new Components::BarrelSteering(); }); + cf->Register([]() { return new Components::TankSteering(); }); + cf->Register([]() { return new Components::TowerSteering(); }); + cf->Register([]() { return new Components::BarrelSteering(); }); } void Systems::TankSteeringSystem::Initialize() @@ -23,7 +23,7 @@ void Systems::TankSteeringSystem::Update(double dt) void Systems::TankSteeringSystem::UpdateEntity(double dt, EntityID entity, EntityID parent) { - auto tankSteeringComponent = m_World->GetComponent(entity, "TankSteering"); + auto tankSteeringComponent = m_World->GetComponent(entity); if(tankSteeringComponent) { Events::TankSteer e; @@ -34,27 +34,27 @@ void Systems::TankSteeringSystem::UpdateEntity(double dt, EntityID entity, Entit EventBroker->Publish(e); } - auto towerSteeringComponent = m_World->GetComponent(entity, "TowerSteering"); + auto towerSteeringComponent = m_World->GetComponent(entity); if(towerSteeringComponent) { - auto transformComponent = m_World->GetComponent(entity, "Transform"); + auto transformComponent = m_World->GetComponent(entity); glm::quat orientation = glm::angleAxis(towerSteeringComponent->TurnSpeed * m_TowerInputController->TowerDirection * (float)dt, towerSteeringComponent->Axis); transformComponent->Orientation *= orientation; } - auto barrelSteeringComponent = m_World->GetComponent(entity, "BarrelSteering"); + auto barrelSteeringComponent = m_World->GetComponent(entity); if(barrelSteeringComponent) { - auto transformComponent = m_World->GetComponent(entity, "Transform"); - auto absoluteTransform = m_World->GetSystem("TransformSystem")->AbsoluteTransform(entity); + auto transformComponent = m_World->GetComponent(entity); + auto absoluteTransform = m_World->GetSystem()->AbsoluteTransform(entity); glm::quat orientation = glm::angleAxis(barrelSteeringComponent->TurnSpeed * m_TowerInputController->BarrelDirection * (float)dt, barrelSteeringComponent->Axis); transformComponent->Orientation *= orientation; if(m_TowerInputController->Shoot && m_TimeSinceLastShot[entity] > 1.0) { EntityID clone = m_World->CloneEntity(barrelSteeringComponent->ShotTemplate); - auto templateAbsoluteTransform = m_World->GetSystem("TransformSystem")->AbsoluteTransform(barrelSteeringComponent->ShotTemplate); - auto cloneTransform = m_World->GetComponent(clone, "Transform"); + auto templateAbsoluteTransform = m_World->GetSystem()->AbsoluteTransform(barrelSteeringComponent->ShotTemplate); + auto cloneTransform = m_World->GetComponent(clone); cloneTransform->Position = templateAbsoluteTransform.Position; cloneTransform->Orientation = absoluteTransform.Orientation * cloneTransform->Orientation; Events::SetVelocity e; diff --git a/src/Systems/TransformSystem.cpp b/src/Systems/TransformSystem.cpp index 3eccc26..69ec6fb 100755 --- a/src/Systems/TransformSystem.cpp +++ b/src/Systems/TransformSystem.cpp @@ -7,8 +7,8 @@ // if (parent == 0) // return; // -// auto transform = m_World->GetComponent(entity, "Transform"); -// auto parentTransform = m_World->GetComponent(parent, "Transform"); +// auto transform = m_World->GetComponent(entity); +// auto parentTransform = m_World->GetComponent(parent); // // transform->Position = parentTransform->Position + transform->RelativePosition; //} @@ -20,10 +20,10 @@ glm::vec3 Systems::TransformSystem::AbsolutePosition(EntityID entity) do { - auto transform = m_World->GetComponent(entity, "Transform"); + auto transform = m_World->GetComponent(entity); //absPosition += transform->Position; entity = m_World->GetEntityParent(entity); - auto transform2 = m_World->GetComponent(entity, "Transform"); + auto transform2 = m_World->GetComponent(entity); if (entity == 0) absPosition += transform->Position; else @@ -39,7 +39,7 @@ glm::quat Systems::TransformSystem::AbsoluteOrientation(EntityID entity) do { - auto transform = m_World->GetComponent(entity, "Transform"); + auto transform = m_World->GetComponent(entity); absOrientation = transform->Orientation * absOrientation; entity = m_World->GetEntityParent(entity); } while (entity != 0); @@ -53,7 +53,7 @@ glm::vec3 Systems::TransformSystem::AbsoluteScale(EntityID entity) do { - auto transform = m_World->GetComponent(entity, "Transform"); + auto transform = m_World->GetComponent(entity); absScale *= transform->Scale; entity = m_World->GetEntityParent(entity); } while (entity != 0); @@ -69,9 +69,9 @@ Components::Transform Systems::TransformSystem::AbsoluteTransform(EntityID entit do { - auto transform = m_World->GetComponent(entity, "Transform"); + auto transform = m_World->GetComponent(entity); entity = m_World->GetEntityParent(entity); - auto transform2 = m_World->GetComponent(entity, "Transform"); + auto transform2 = m_World->GetComponent(entity); // Position if (entity == 0) diff --git a/src/World.cpp b/src/World.cpp index c84a2bd..2a25856 100755 --- a/src/World.cpp +++ b/src/World.cpp @@ -132,11 +132,6 @@ void World::Initialize() } } -std::shared_ptr World::AddComponent(EntityID entity, std::string componentType) -{ - return AddComponent(entity, componentType); -} - void World::CommitEntity(EntityID entity) { for (auto pair : m_Systems) @@ -158,11 +153,6 @@ void World::AddComponent(EntityID entity, std::string componentType, std::shared } } -void World::AddSystem(std::string systemType) -{ - m_Systems[systemType] = std::shared_ptr(m_SystemFactory.Create(systemType)); -} - EntityID World::CloneEntity(EntityID entity, EntityID parent /* = 0 */) { int clone = CreateEntity(parent); @@ -170,7 +160,7 @@ EntityID World::CloneEntity(EntityID entity, EntityID parent /* = 0 */) for (auto pair : m_EntityComponents[entity]) { auto type = pair.first; - if (type == "Template") + if (type == typeid(Components::Template).name()) continue; auto component = std::shared_ptr(pair.second->Clone()); if (component != nullptr) diff --git a/src/World.h b/src/World.h index 0c4d76e..87bfecf 100755 --- a/src/World.h +++ b/src/World.h @@ -13,6 +13,7 @@ #include "Factory.h" #include "Entity.h" #include "Component.h" +#include "Components/Template.h" #include "System.h" #include "EventBroker.h" #include "ResourceManager.h" @@ -31,10 +32,14 @@ public: virtual void AddSystems() = 0; virtual void RegisterComponents() = 0; + template + void AddSystem() + { + m_Systems[typeid(T).name()] = std::shared_ptr(m_SystemFactory.Create()); + } - void AddSystem(std::string systemType); - template - std::shared_ptr GetSystem(std::string systemType); + template + std::shared_ptr GetSystem(); EntityID CreateEntity(EntityID parent = 0); EntityID CloneEntity(EntityID entity, EntityID parent = 0); @@ -69,10 +74,9 @@ public: } template - std::shared_ptr AddComponent(EntityID entity, std::string componentType); - std::shared_ptr AddComponent(EntityID entity, std::string componentType); + std::shared_ptr AddComponent(EntityID entity); template - T* GetComponent(EntityID entity, std::string componentType); + T* GetComponent(EntityID entity); // Triggers commit events in systems void CommitEntity(EntityID entity); @@ -117,11 +121,13 @@ protected: }; template -std::shared_ptr World::GetSystem(std::string systemType) +std::shared_ptr World::GetSystem() { + const char* systemType = typeid(T).name(); + if (m_Systems.find(systemType) == m_Systems.end()) { - LOG_WARNING("Tried to get pointer to unregistered system \"%s\"!", systemType.c_str()); + LOG_WARNING("Tried to get pointer to unregistered system \"%s\"!", systemType); return nullptr; } @@ -129,12 +135,14 @@ std::shared_ptr World::GetSystem(std::string systemType) } template -std::shared_ptr World::AddComponent(EntityID entity, std::string componentType) +std::shared_ptr World::AddComponent(EntityID entity) { - std::shared_ptr component = std::shared_ptr(static_cast(m_ComponentFactory.Create(componentType))); + const char* componentType = typeid(T).name(); + + std::shared_ptr component = std::shared_ptr(static_cast(m_ComponentFactory.Create())); if (component == nullptr) { - LOG_ERROR("Failed to attach invalid component \"%s\" to entity #%i", componentType.c_str(), entity); + LOG_ERROR("Failed to attach invalid component \"%s\" to entity #%i", componentType, entity); return nullptr; } @@ -145,17 +153,11 @@ std::shared_ptr World::AddComponent(EntityID entity, std::string componentTyp template -T* World::GetComponent(EntityID entity, std::string componentType) +T* World::GetComponent(EntityID entity) { - - /*auto it0 = m_EntityComponents.find(entity); - - if (it0 == m_EntityComponents.end()) - return nullptr;*/ - auto components = m_EntityComponents[entity]; - auto it = components.find(componentType); + auto it = components.find(typeid(T).name()); if (it != components.end()) { return static_cast(it->second.get());