From 3467315aef38864dcd14cfc38cfbacce7cdd1fea Mon Sep 17 00:00:00 2001 From: Ayumiwii Date: Fri, 8 Jan 2016 16:42:59 +0100 Subject: [PATCH] Lots of things done --- include/Engine/Rendering/DrawScenePass.h | 6 ++ include/Engine/Rendering/RenderQueue.h | 11 +++ include/Engine/Rendering/RenderQueueFactory.h | 2 + include/Engine/Rendering/Renderer.h | 2 + resources/Schema/Components.xsd | 1 + resources/Schema/Components/CoolDeathAnim.xml | 5 + resources/Schema/Components/CoolDeathAnim.xsd | 24 +++++ resources/Schema/Entities/Test.xml | 7 +- resources/Schema/Types/Entity.xsd | 1 + resources/Shaders/CoolDeathAnim.frag.glsl | 22 +++++ resources/Shaders/CoolDeathAnim.geom.glsl | 98 +++++++++++++++++++ resources/Shaders/CoolDeathAnim.vert.glsl | 34 +++++++ src/Engine/Rendering/DrawScenePass.cpp | 60 ++++++++++-- src/Engine/Rendering/RenderQueueFactory.cpp | 52 +++++++--- src/Engine/Rendering/Renderer.cpp | 9 ++ 15 files changed, 312 insertions(+), 22 deletions(-) create mode 100644 resources/Schema/Components/CoolDeathAnim.xml create mode 100644 resources/Schema/Components/CoolDeathAnim.xsd create mode 100644 resources/Shaders/CoolDeathAnim.frag.glsl create mode 100644 resources/Shaders/CoolDeathAnim.geom.glsl create mode 100644 resources/Shaders/CoolDeathAnim.vert.glsl diff --git a/include/Engine/Rendering/DrawScenePass.h b/include/Engine/Rendering/DrawScenePass.h index 782c5a49..5e79ae0f 100644 --- a/include/Engine/Rendering/DrawScenePass.h +++ b/include/Engine/Rendering/DrawScenePass.h @@ -31,6 +31,12 @@ private: ShaderProgram* m_BasicForwardProgram; + ShaderProgram* m_CoolDeathAnimProgram; + + glm::vec3 Origin; + GLfloat TimeDeath = 0.f; + GLfloat EndDeath = 0.f; + }; #endif \ No newline at end of file diff --git a/include/Engine/Rendering/RenderQueue.h b/include/Engine/Rendering/RenderQueue.h index 2942c743..af368b10 100644 --- a/include/Engine/Rendering/RenderQueue.h +++ b/include/Engine/Rendering/RenderQueue.h @@ -63,6 +63,17 @@ struct ModelJob : RenderJob } }; +// Cool Death Animation +struct CoolDeathAnimationJob : ModelJob +{ + glm::vec3 OriginPos; + float TimeSinceDeath = 0.f; + float EndOfDeath = 15.0f; + +}; + + + struct SpriteJob : RenderJob { unsigned int ShaderID = 0; diff --git a/include/Engine/Rendering/RenderQueueFactory.h b/include/Engine/Rendering/RenderQueueFactory.h index be2c55ca..e1a3507a 100644 --- a/include/Engine/Rendering/RenderQueueFactory.h +++ b/include/Engine/Rendering/RenderQueueFactory.h @@ -25,6 +25,8 @@ private: void FillModels(World* world, RenderQueue* renderQueue); void FillLights(World* world, RenderQueue* renderQueue); + void FillCoolDeathAnim(World* world, RenderQueue* renderQueue); + glm::mat4 ModelMatrix(World* world, EntityID entity); }; diff --git a/include/Engine/Rendering/Renderer.h b/include/Engine/Rendering/Renderer.h index b4aae346..476ac9f9 100644 --- a/include/Engine/Rendering/Renderer.h +++ b/include/Engine/Rendering/Renderer.h @@ -119,6 +119,8 @@ private: ShaderProgram* m_CalculateFrustumProgram; ShaderProgram* m_LightCullProgram; + ShaderProgram* m_CoolDeathAnimProgram; + }; #endif \ No newline at end of file diff --git a/resources/Schema/Components.xsd b/resources/Schema/Components.xsd index fd04fd39..f95937ee 100644 --- a/resources/Schema/Components.xsd +++ b/resources/Schema/Components.xsd @@ -8,4 +8,5 @@ + \ No newline at end of file diff --git a/resources/Schema/Components/CoolDeathAnim.xml b/resources/Schema/Components/CoolDeathAnim.xml new file mode 100644 index 00000000..36cdef98 --- /dev/null +++ b/resources/Schema/Components/CoolDeathAnim.xml @@ -0,0 +1,5 @@ + + + 0 + 15 + \ No newline at end of file diff --git a/resources/Schema/Components/CoolDeathAnim.xsd b/resources/Schema/Components/CoolDeathAnim.xsd new file mode 100644 index 00000000..768be77a --- /dev/null +++ b/resources/Schema/Components/CoolDeathAnim.xsd @@ -0,0 +1,24 @@ + + + + + + + + A component that trigger the death effect + + + + + The position in which to spawn the component + + + Time since death + + + How long the death animation should be + + + + + \ No newline at end of file diff --git a/resources/Schema/Entities/Test.xml b/resources/Schema/Entities/Test.xml index 528c7e95..329b511d 100644 --- a/resources/Schema/Entities/Test.xml +++ b/resources/Schema/Entities/Test.xml @@ -66,12 +66,17 @@ - + Models/Core/UnitRaptor.obj + + + 0 + 15 + diff --git a/resources/Schema/Types/Entity.xsd b/resources/Schema/Types/Entity.xsd index 92f7dc31..b321d7da 100644 --- a/resources/Schema/Types/Entity.xsd +++ b/resources/Schema/Types/Entity.xsd @@ -15,6 +15,7 @@ + diff --git a/resources/Shaders/CoolDeathAnim.frag.glsl b/resources/Shaders/CoolDeathAnim.frag.glsl new file mode 100644 index 00000000..aa13a18f --- /dev/null +++ b/resources/Shaders/CoolDeathAnim.frag.glsl @@ -0,0 +1,22 @@ +#version 430 + +uniform mat4 M; +uniform mat4 V; +uniform mat4 P; +uniform vec4 Color; + +uniform sampler2D texture0; + +in vec3 Normal; +in vec3 Position; +in vec2 TextureCoordinate; +in vec4 DiffuseColor; + +out vec4 fragmentColor; + + +void main() +{ + vec4 texel = texture2D(texture0, TextureCoordinate); + fragmentColor = texel * DiffuseColor * Color; +} \ No newline at end of file diff --git a/resources/Shaders/CoolDeathAnim.geom.glsl b/resources/Shaders/CoolDeathAnim.geom.glsl new file mode 100644 index 00000000..3bf80a7f --- /dev/null +++ b/resources/Shaders/CoolDeathAnim.geom.glsl @@ -0,0 +1,98 @@ +#version 430 + +uniform mat4 M; +uniform mat4 V; +uniform mat4 P; +uniform vec3 OriginPos; +uniform float TimeSinceDeath; +uniform float EndOfDeath; + +in VertexData{ + vec3 Position; + vec3 Normal; + vec2 TextureCoordinate; + vec4 DiffuseColor; +}Input[]; + +out vec3 Normal; +out vec3 Position; +out vec2 TextureCoordinate; +out vec4 DiffuseColor; + +layout(triangles) in; +layout(triangle_strip, max_vertices = 3) out; + +void main() +{ + // surface vectors for triangle + //vec3 v1 = Input[1].Position - Input[0].Position; + //vec3 v2 = Input[2].Position - Input[0].Position; + + //vec3 v3 = Input[1].Position - OriginPos; + //v3 = normalize(v3); + + // surface normal + //vec3 normal = cross(v1, v2); + //normal = normalize(normal); + + // calculate middle of a vectors + vec3 v1 = Input[1].Position - Input[0].Position; + vec3 v2 = Input[2].Position - Input[0].Position; + vec3 v3 = Input[2].Position - (v1 / 2); + vec3 v4 = Input[1].Position - (v2 / 2); + + // + //vec3 oriToTri = Input[1].Position - OriginPos; + //v5 = normalize(v3); + + + // calculate intersection + // normalize direction + vec3 dir1 = normalize(v1); + vec3 dir2 = normalize(v2); + + vec3 vecA = Input[2].Position - Input[1].Position; //o2 - o1 + vec3 vecB = cross(dir1, dir2); + + mat3 matrisA = mat3(vecA, dir2, vecB); + mat3 matrisB = mat3(vecA, dir1, vecB); + + float lengthA = length(cross(dir1, dir2)); + lengthA = lengthA * lengthA; + + float s = determinant(matrisA) / lengthA; + float t = determinant(matrisB) / lengthA; + + // center position of triangle + vec3 centerPosS = Input[1].Position + (s * dir1); + //vec3 centerPosT = Input[2].Position + (t * dir2); + + + // center position of triangle to origin + vec3 oriToCenterTri = normalize(centerPosS - OriginPos); + + for(int i = 0; i < gl_in.length(); i++) + { + vec4 screenPos = gl_in[i].gl_Position; + + if (distance(OriginPos, Input[0].Position) <= TimeSinceDeath) + { + vec4 changedPos = M * vec4(Input[i].Position + (oriToCenterTri * TimeSinceDeath), 1.0); //objectspace + changedPos = vec4(changedPos.x, changedPos.y - pow(TimeSinceDeath, 2), changedPos.z, changedPos.w); //objectspace + screenPos = P*V * changedPos; //sceenspace + } + // Explosion from origin + + + + Normal = Input[i].Normal; + Position = Input[i].Position; + TextureCoordinate = Input[i].TextureCoordinate; + DiffuseColor = vec4(Input[i].DiffuseColor.rg, Input[i].DiffuseColor.b + TimeSinceDeath, Input[i].DiffuseColor.a); + + gl_Position = screenPos; + //gl_Position = gl_in[i].gl_Position; + EmitVertex(); + } + +} \ No newline at end of file diff --git a/resources/Shaders/CoolDeathAnim.vert.glsl b/resources/Shaders/CoolDeathAnim.vert.glsl new file mode 100644 index 00000000..20ab9051 --- /dev/null +++ b/resources/Shaders/CoolDeathAnim.vert.glsl @@ -0,0 +1,34 @@ +#version 430 + +uniform mat4 M; +uniform mat4 V; +uniform mat4 P; + +layout(location = 0) in vec3 Position; +layout(location = 1) in vec3 Normal; +layout(location = 2) in vec3 Tangent; +layout(location = 3) in vec3 BiTangent; +layout(location = 4) in vec2 TextureCoords; +layout(location = 5) in vec4 DiffuseVertexColor; +layout(location = 6) in vec4 SpecularVertexColor; +layout(location = 7) in vec4 BoneIndices1; +layout(location = 8) in vec4 BoneIndices2; +layout(location = 9) in vec4 BoneWeights1; +layout(location = 10) in vec4 BoneWeights2; + +out VertexData{ + vec3 Position; + vec3 Normal; + vec2 TextureCoordinate; + vec4 DiffuseColor; +}Output; + +void main() +{ + gl_Position = P*V*M * vec4(Position, 1.0); + + Output.Position = Position; + Output.TextureCoordinate = TextureCoords; + Output.Normal = Normal; + Output.DiffuseColor = DiffuseVertexColor; +} \ No newline at end of file diff --git a/src/Engine/Rendering/DrawScenePass.cpp b/src/Engine/Rendering/DrawScenePass.cpp index 559a0f58..5e48e8f8 100644 --- a/src/Engine/Rendering/DrawScenePass.cpp +++ b/src/Engine/Rendering/DrawScenePass.cpp @@ -16,18 +16,32 @@ void DrawScenePass::InitializeShaderPrograms() { //Gör så att shaders är en resource, tex som texture classen. Konstruktorn måste vara privat. m_BasicForwardProgram = ResourceManager::Load("#BasicForwardProgram"); - m_BasicForwardProgram->AddShader(std::shared_ptr(new VertexShader("Shaders/BasicForward.vert.glsl"))); m_BasicForwardProgram->AddShader(std::shared_ptr(new FragmentShader("Shaders/BasicForward.frag.glsl"))); m_BasicForwardProgram->Compile(); m_BasicForwardProgram->Link(); - + m_CoolDeathAnimProgram = ResourceManager::Load("#CoolDeathAnimProgram"); + m_CoolDeathAnimProgram->AddShader(std::shared_ptr(new VertexShader("Shaders/CoolDeathAnim.vert.glsl"))); + m_CoolDeathAnimProgram->AddShader(std::shared_ptr(new GeometryShader("Shaders/CoolDeathAnim.geom.glsl"))); + m_CoolDeathAnimProgram->AddShader(std::shared_ptr(new FragmentShader("Shaders/CoolDeathAnim.frag.glsl"))); + m_CoolDeathAnimProgram->Compile(); + m_CoolDeathAnimProgram->Link(); + } void DrawScenePass::Draw(RenderQueueCollection& rq) { - //glBindFramebuffer(GL_FRAMEBUFFER, 0); + + if (TimeDeath > 2.f) { + TimeDeath = 0.f; + } + + Origin = glm::vec3(0.f, 0.0f, 0.f); + TimeDeath = TimeDeath + 0.01f; + EndDeath = EndDeath + 0.05f; + + glBindFramebuffer(GL_FRAMEBUFFER, 0); GLERROR("Renderer::Draw PickingPass"); DrawScenePassState state; @@ -35,17 +49,47 @@ void DrawScenePass::Draw(RenderQueueCollection& rq) //TODO: Render: Add code for more jobs than modeljobs. for (auto &job : rq.Forward) { + auto coolDeathAnimationJob = std::dynamic_pointer_cast(job); + if (coolDeathAnimationJob) { + GLuint ShaderHandle = m_CoolDeathAnimProgram->GetHandle(); //--- + //--- + m_CoolDeathAnimProgram->Bind(); + //TODO: Kolla upp "header/include/common" shader saken så man slipper skicka in asmycket uniforms + glUniformMatrix4fv(glGetUniformLocation(ShaderHandle, "M"), 1, GL_FALSE, glm::value_ptr(coolDeathAnimationJob->ModelMatrix)); + glUniformMatrix4fv(glGetUniformLocation(ShaderHandle, "V"), 1, GL_FALSE, glm::value_ptr(m_Renderer->Camera()->ViewMatrix())); + glUniformMatrix4fv(glGetUniformLocation(ShaderHandle, "P"), 1, GL_FALSE, glm::value_ptr(m_Renderer->Camera()->ProjectionMatrix())); + glUniform4fv(glGetUniformLocation(ShaderHandle, "Color"), 1, glm::value_ptr(coolDeathAnimationJob->Color)); + glUniform3fv(glGetUniformLocation(ShaderHandle, "OriginPos"), 1, glm::value_ptr(Origin)); + glUniform1f(glGetUniformLocation(ShaderHandle, "TimeSinceDeath"), TimeDeath); + glUniform1f(glGetUniformLocation(ShaderHandle, "EndOfDeath"), EndDeath); + + //TODO: Renderer: bättre textur felhantering samt fler texturer stöd + if (coolDeathAnimationJob->DiffuseTexture != nullptr) { + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, coolDeathAnimationJob->DiffuseTexture->m_Texture); + } else { + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, m_WhiteTexture->m_Texture); + } + glBindVertexArray(coolDeathAnimationJob->Model->VAO); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, coolDeathAnimationJob->Model->ElementBuffer); + glDrawElementsBaseVertex(GL_TRIANGLES, coolDeathAnimationJob->EndIndex - coolDeathAnimationJob->StartIndex + 1, GL_UNSIGNED_INT, 0, coolDeathAnimationJob->StartIndex); + continue; + } + auto modelJob = std::dynamic_pointer_cast(job); if (modelJob) { - GLuint ShaderHandle = m_BasicForwardProgram->GetHandle(); - + GLuint ShaderHandle = m_BasicForwardProgram->GetHandle(); //--- + GLERROR("1"); + //--- m_BasicForwardProgram->Bind(); + GLERROR("2.1"); //TODO: Kolla upp "header/include/common" shader saken så man slipper skicka in asmycket uniforms glUniformMatrix4fv(glGetUniformLocation(ShaderHandle, "M"), 1, GL_FALSE, glm::value_ptr(modelJob->ModelMatrix)); glUniformMatrix4fv(glGetUniformLocation(ShaderHandle, "V"), 1, GL_FALSE, glm::value_ptr(m_Renderer->Camera()->ViewMatrix())); glUniformMatrix4fv(glGetUniformLocation(ShaderHandle, "P"), 1, GL_FALSE, glm::value_ptr(m_Renderer->Camera()->ProjectionMatrix())); glUniform4fv(glGetUniformLocation(ShaderHandle, "Color"), 1, glm::value_ptr(modelJob->Color)); - + GLERROR("2"); //TODO: Renderer: bättre textur felhantering samt fler texturer stöd if (modelJob->DiffuseTexture != nullptr) { glActiveTexture(GL_TEXTURE0); @@ -54,11 +98,11 @@ void DrawScenePass::Draw(RenderQueueCollection& rq) glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, m_WhiteTexture->m_Texture); } - + GLERROR("3"); glBindVertexArray(modelJob->Model->VAO); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, modelJob->Model->ElementBuffer); glDrawElementsBaseVertex(GL_TRIANGLES, modelJob->EndIndex - modelJob->StartIndex + 1, GL_UNSIGNED_INT, 0, modelJob->StartIndex); - + GLERROR("4"); continue; } } diff --git a/src/Engine/Rendering/RenderQueueFactory.cpp b/src/Engine/Rendering/RenderQueueFactory.cpp index 8d5bb420..53c8c6d9 100644 --- a/src/Engine/Rendering/RenderQueueFactory.cpp +++ b/src/Engine/Rendering/RenderQueueFactory.cpp @@ -90,21 +90,47 @@ void RenderQueueFactory::FillModels(World* world, RenderQueue* renderQueue) } for (auto texGroup : model->TextureGroups) { - ModelJob job; - job.TextureID = (texGroup.Texture) ? texGroup.Texture->ResourceID : 0; - job.DiffuseTexture = texGroup.Texture.get(); - job.NormalTexture = texGroup.NormalMap.get(); - job.SpecularTexture = texGroup.SpecularMap.get(); - job.Model = model; - job.StartIndex = texGroup.StartIndex; - job.EndIndex = texGroup.EndIndex; - job.ModelMatrix = model->m_Matrix * ModelMatrix(world, modelC.EntityID); - job.Color = color; + auto deathComp = world->HasComponent(modelC.EntityID, "CoolDeathAnim"); + if (!deathComp) { + ModelJob job; - //TODO: RENDERER: Not sure if the best solution for pickingColor to entity link is this - job.Entity = modelC.EntityID; + job.TextureID = (texGroup.Texture) ? texGroup.Texture->ResourceID : 0; + job.DiffuseTexture = texGroup.Texture.get(); + job.NormalTexture = texGroup.NormalMap.get(); + job.SpecularTexture = texGroup.SpecularMap.get(); + job.Model = model; + job.StartIndex = texGroup.StartIndex; + job.EndIndex = texGroup.EndIndex; + job.ModelMatrix = model->m_Matrix * ModelMatrix(world, modelC.EntityID); + job.Color = color; - renderQueue->Add(job); + //TODO: RENDERER: Not sure if the best solution for pickingColor to entity link is this + job.Entity = modelC.EntityID; + + renderQueue->Add(job); + } + else { + CoolDeathAnimationJob job; + + job.TimeSinceDeath = 1.f; + job.EndOfDeath = 15.f; + job.OriginPos = glm::vec3(1000.f, 2.f, 0.f); + + job.TextureID = (texGroup.Texture) ? texGroup.Texture->ResourceID : 0; + job.DiffuseTexture = texGroup.Texture.get(); + job.NormalTexture = texGroup.NormalMap.get(); + job.SpecularTexture = texGroup.SpecularMap.get(); + job.Model = model; + job.StartIndex = texGroup.StartIndex; + job.EndIndex = texGroup.EndIndex; + job.ModelMatrix = model->m_Matrix * ModelMatrix(world, modelC.EntityID); + job.Color = color; + + //TODO: RENDERER: Not sure if the best solution for pickingColor to entity link is this + job.Entity = modelC.EntityID; + + renderQueue->Add(job); + } } } } diff --git a/src/Engine/Rendering/Renderer.cpp b/src/Engine/Rendering/Renderer.cpp index 21ff6c5a..e96d3df3 100644 --- a/src/Engine/Rendering/Renderer.cpp +++ b/src/Engine/Rendering/Renderer.cpp @@ -76,6 +76,15 @@ void Renderer::InitializeShaders() m_DrawScreenQuadProgram->Compile(); m_DrawScreenQuadProgram->Link(); + m_CoolDeathAnimProgram = ResourceManager::Load("#CoolDeathAnimProgram"); + //m_CoolDeathAnimProgram->AddShader(std::shared_ptr(new VertexShader("Shaders/CoolDeathAnim.vert.glsl"))); + //m_CoolDeathAnimProgram->AddShader(std::shared_ptr(new GeometryShader("Shaders/CoolDeathAnim.geom.glsl"))); + //m_CoolDeathAnimProgram->AddShader(std::shared_ptr(new FragmentShader("Shaders/CoolDeathAnim.frag.glsl"))); + //m_CoolDeathAnimProgram->Compile(); + //m_CoolDeathAnimProgram->Link(); + + + //m_CalculateFrustumProgram = ResourceManager::Load("#CalculateFrustumProgram"); //m_CalculateFrustumProgram.AddShader(std::shared_ptr(new ComputeShader("Shaders/GridFrustum.comp.glsl"))); //m_CalculateFrustumProgram.Compile();