diff --git a/include/Engine/Common.h b/include/Engine/Common.h index 8038b8af..8295554c 100644 --- a/include/Engine/Common.h +++ b/include/Engine/Common.h @@ -5,6 +5,7 @@ #include #include #include +#include #include "Core/Util/Logging.h" #include "Core/Util/IfDebug.h" \ No newline at end of file diff --git a/include/Engine/Rendering/DrawFinalPass.h b/include/Engine/Rendering/DrawFinalPass.h index 4cf88842..6bd8acec 100644 --- a/include/Engine/Rendering/DrawFinalPass.h +++ b/include/Engine/Rendering/DrawFinalPass.h @@ -43,6 +43,7 @@ private: const LightCullingPass* m_LightCullingPass; ShaderProgram* m_ForwardPlusProgram; + ShaderProgram* m_ExplosionEffectProgram; }; #endif \ No newline at end of file diff --git a/include/Engine/Rendering/ExplosionEffectJob.h b/include/Engine/Rendering/ExplosionEffectJob.h new file mode 100644 index 00000000..89b1342b --- /dev/null +++ b/include/Engine/Rendering/ExplosionEffectJob.h @@ -0,0 +1,106 @@ +#ifndef ExplosionEffectJob_h__ +#define ExplosionEffectJob_h__ + +#include + +#include "../Common.h" +#include "../GLM.h" +#include "../Core/ComponentWrapper.h" +#include "Texture.h" +#include "Model.h" +#include "RenderJob.h" +#include "../Core/ResourceManager.h" +#include "Camera.h" +#include "../Core/World.h" + +struct ExplosionEffectJob : ModelJob +{ + ExplosionEffectJob(ComponentWrapper explosionEffectComponent, ::Model* model, Camera* camera, glm::mat4 matrix, ::RawModel::MaterialGroup matGroup, ComponentWrapper modelComponent, ::World* world, glm::vec4 fillColor, float fillPercentage) + : ModelJob(model, camera, matrix, matGroup, modelComponent, world, fillColor, fillPercentage) + { + ExplosionOrigin = (glm::vec3)explosionEffectComponent["ExplosionOrigin"]; + TimeSinceDeath = (double)explosionEffectComponent["TimeSinceDeath"]; + ExplosionDuration = (double)explosionEffectComponent["ExplosionDuration"]; + EndColor = (glm::vec4)explosionEffectComponent["EndColor"]; + Randomness = (bool)explosionEffectComponent["Randomness"]; + RandomnessScalar = (double)explosionEffectComponent["RandomnessScalar"]; + Velocity = (glm::vec2)explosionEffectComponent["Velocity"]; + ColorByDistance = (bool)explosionEffectComponent["ColorByDistance"]; + ExponentialAccelaration = (bool)explosionEffectComponent["ExponentialAccelaration"]; + }; + + glm::vec3 ExplosionOrigin; + double TimeSinceDeath = 0.f; //Seconds + double ExplosionDuration = 2.f; + //bool Gravity = true; + //double GravityForce = 1.f; // Speed + //double ObjectRadius = 2.f; // TODO: Change this for object radius when it's available + glm::vec4 EndColor; + bool Randomness = false; + + float RandomnessScalar = 1.f; + glm::vec2 Velocity; + bool ColorByDistance = false; + //bool ReverseAnimation = false; + //bool Wireframe = false; + bool ExponentialAccelaration = false; + + std::array RandomNumbers = { + 0.3257552917701f, + 0.07601508315467f, + 0.57408909014151f, + 0.0f, + 0.8618231368539f, + 0.074957156588769f, + 0.39413607511396f, + 0.54579346698979f, + 0.83222648353885f, + 0.83635707285086f, + 0.34473986148124f, + 0.98092448710507f, + 0.46346380070944f, + 0.7308761201477f, + 0.70832470371776f, + 0.28268750909841f, + 0.26291620883295f, + 0.07685032816457f, + 0.30760929515008f, + 0.2781575388639f , + 0.016950582161988f , + 0.25787915254844f , + 0.76293767279151f , + 0.67730279903733f , + 0.49042877018937f , + 0.13002237823327f , + 0.62803373841012f , + 0.94525353747665f , + 0.32893980076953f , + 0.95952951719962f , + 0.056386206790985f , + 0.012030893476694f , + 0.93090049220757f, + 0.83579883064879f, + 0.79733513938139f, + 0.8796381046435f , + 0.94160434600972f , + 0.76901855588379f , + 0.50253688101775f , + 0.82457069578793f , + 0.80157403359263f , + 0.87291810189975f , + 0.64679548919517f , + 0.42664138899494f , + 0.77171308303797f , + 0.75567959237643f , + 0.45190826265696f , + 0.59844922628181f , + 0.56534937655802f , + 0.195656257307f}; + + void CalculateHash() override + { + Hash = 0; + } +}; + +#endif \ No newline at end of file diff --git a/include/Engine/Rendering/RenderQueue.h b/include/Engine/Rendering/RenderQueue.h index b533f992..08a67ac7 100644 --- a/include/Engine/Rendering/RenderQueue.h +++ b/include/Engine/Rendering/RenderQueue.h @@ -14,6 +14,7 @@ #include "TextJob.h" #include "PointLightJob.h" #include "DirectionalLightJob.h" +#include "ExplosionEffectJob.h" struct RenderScene { diff --git a/include/Engine/Rendering/Renderer.h b/include/Engine/Rendering/Renderer.h index d84f41d5..33a61edf 100644 --- a/include/Engine/Rendering/Renderer.h +++ b/include/Engine/Rendering/Renderer.h @@ -73,6 +73,8 @@ private: void GenerateTexture(GLuint* texture, GLenum wrapping, GLenum filtering, glm::vec2 dimensions, GLint internalFormat, GLint format, GLenum type); //--------------------ShaderPrograms-------------------// ShaderProgram* m_BasicForwardProgram; + ShaderProgram* m_ExplosionEffectProgram; + }; #endif \ No newline at end of file diff --git a/include/Game/ExplosionEffectSystem.h b/include/Game/ExplosionEffectSystem.h new file mode 100644 index 00000000..061ad221 --- /dev/null +++ b/include/Game/ExplosionEffectSystem.h @@ -0,0 +1,24 @@ +#include "Common.h" +#include "Core/System.h" + +class ExplosionEffectSystem : public PureSystem +{ +public: + ExplosionEffectSystem(World* world, EventBroker* eventBroker) + : System(world, eventBroker) + , PureSystem("ExplosionEffect") + { } + + virtual void UpdateComponent(EntityWrapper& entity, ComponentWrapper& component, double dt) override + { + + if ((double)component["TimeSinceDeath"] > (double)component["ExplosionDuration"]) { + (double)component["TimeSinceDeath"] = 0.f; + } + (double&)component["TimeSinceDeath"] += dt; + + //if ((bool)Component["Gravity"] == true) { + // (bool)Component["ExponentialAccelaration"] = false; + //} + } +}; \ No newline at end of file diff --git a/include/Game/Game.h b/include/Game/Game.h index fab10a49..c1d90faa 100644 --- a/include/Game/Game.h +++ b/include/Game/Game.h @@ -14,6 +14,7 @@ #include "Core/EKeyDown.h" #include "Core/EntityFilePreprocessor.h" #include "Core/SystemPipeline.h" +#include "ExplosionEffectSystem.h" #include "Editor/EditorSystem.h" #include "Core/EntityFile.h" #include "Rendering/RenderSystem.h" diff --git a/resources/Schema/Components.xsd b/resources/Schema/Components.xsd index 5ce7fa5f..346f0503 100644 --- a/resources/Schema/Components.xsd +++ b/resources/Schema/Components.xsd @@ -12,6 +12,7 @@ + diff --git a/resources/Schema/Components/ExplosionEffect.xml b/resources/Schema/Components/ExplosionEffect.xml new file mode 100644 index 00000000..22692729 --- /dev/null +++ b/resources/Schema/Components/ExplosionEffect.xml @@ -0,0 +1,17 @@ + + + + 0 + 2 + + + + + 0 + 1 + + 0 + + + 0 + \ No newline at end of file diff --git a/resources/Schema/Components/ExplosionEffect.xsd b/resources/Schema/Components/ExplosionEffect.xsd new file mode 100644 index 00000000..cdde5e52 --- /dev/null +++ b/resources/Schema/Components/ExplosionEffect.xsd @@ -0,0 +1,57 @@ + + + + + + + + A component that trigger the death effect + + + + + The position in which to spawn the component + + + Seconds since death + + + How many seconds the death animation should be + + + + + + The tint the polys end with + + + Add some randomness to the explosion + + + Alter the power of the randomness + + + The velocity factors (start/end) + + + Change the color by its moved distance instead of by its time + + + + + Linear/Exponential accelaration + + + + + \ No newline at end of file diff --git a/resources/Schema/Entities/FastWorld.xml b/resources/Schema/Entities/FastWorld.xml index 68d1dccb..1289d9d1 100644 --- a/resources/Schema/Entities/FastWorld.xml +++ b/resources/Schema/Entities/FastWorld.xml @@ -25,10 +25,16 @@ - Crouch Walk - - 32 + Run + + 1 + + + + 0.95625903442123672 + + Models/AssaultAnimated.mesh diff --git a/resources/Schema/Entities/Test.xml b/resources/Schema/Entities/Test.xml index 1f8da1a3..2cdf4e17 100644 --- a/resources/Schema/Entities/Test.xml +++ b/resources/Schema/Entities/Test.xml @@ -18,6 +18,23 @@ + + + + 0 + 2 + + + + + 0 + 1 + + 0 + + + 0 + Models/Camera.mesh diff --git a/resources/Schema/Types/Entity.xsd b/resources/Schema/Types/Entity.xsd index 5d135847..345d820e 100644 --- a/resources/Schema/Types/Entity.xsd +++ b/resources/Schema/Types/Entity.xsd @@ -15,6 +15,7 @@ + diff --git a/resources/Shaders/ExplosionEffect.frag.glsl b/resources/Shaders/ExplosionEffect.frag.glsl new file mode 100644 index 00000000..32ababe5 --- /dev/null +++ b/resources/Shaders/ExplosionEffect.frag.glsl @@ -0,0 +1,25 @@ +#version 430 + +uniform mat4 M; +uniform mat4 V; +uniform mat4 P; +uniform vec4 Color; + +uniform sampler2D texture0; + +in VertexData{ + vec3 Position; + vec3 Normal; + vec2 TextureCoordinate; + vec4 DiffuseColor; + vec4 ExplosionColor; +}Input; + +out vec4 fragmentColor; + + +void main() +{ + vec4 texel = texture2D(texture0, Input.TextureCoordinate); + fragmentColor = texel * Input.DiffuseColor * Color + Input.ExplosionColor; +} \ No newline at end of file diff --git a/resources/Shaders/ExplosionEffect.geom.glsl b/resources/Shaders/ExplosionEffect.geom.glsl new file mode 100644 index 00000000..cb91b545 --- /dev/null +++ b/resources/Shaders/ExplosionEffect.geom.glsl @@ -0,0 +1,179 @@ +#version 430 + +uniform mat4 M; +uniform mat4 V; +uniform mat4 P; +uniform vec3 ExplosionOrigin; +uniform float TimeSinceDeath; +uniform float ExplosionDuration; +uniform vec4 EndColor; +uniform bool Randomness; +uniform float RandomNumbers[50]; +uniform float RandomnessScalar; +uniform vec2 Velocity; +uniform bool ColorByDistance; +uniform bool ExponentialAccelaration; + +in VertexData{ + vec3 Position; + vec3 Normal; + vec2 TextureCoordinate; + vec4 ExplosionColor; +}Input[]; + +out VertexData{ + vec3 Position; + vec3 Normal; + vec2 TextureCoordinate; + vec4 ExplosionColor; +}Output; + +layout(triangles) in; +layout(triangle_strip, max_vertices = 3) out; + +// returns a "random" number based on input parameter +float GetRandomNumber(int polygon_index) +{ + int randomIndex = int(mod(polygon_index, 50)); + + return RandomNumbers[randomIndex]; +} + +float randomNumber = 0.0; +float randomDistance = 0.0; + +void main() +{ + // calculate middle of 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); + + // 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); + + float lengthA = length(vecB); + lengthA = lengthA * lengthA; + + float s = determinant(matrisA) / lengthA; + + // center position of triangle + vec3 centerOfTriangle = Input[1].Position + (s * dir1); + + // center position of triangle to origin vector + vec3 origin2TriangleCenterVector = centerOfTriangle - ExplosionOrigin; + vec3 normalizedOrigin2TriangleCenterVector = normalize(origin2TriangleCenterVector); + + // time percentage until end of explosion (0.0-1.0) + float timePercetage = TimeSinceDeath / ExplosionDuration; + + // get a random number if randomness is enabled, otherwise the random number will be zero and won't affect the other algorithms + if (Randomness == true) + { + randomDistance = GetRandomNumber(gl_PrimitiveIDIn) * RandomnessScalar; + } + + vec2 randomVelocity = Velocity * (randomDistance + 1.0); + + // accelaration to use on current frame. is a interpolation between start and end value + float currentVelocity = mix(randomVelocity.x, randomVelocity.y, timePercetage); + + if (ExponentialAccelaration == true) + { + currentVelocity = pow(currentVelocity, 2) / 2.0; + } + + // distance between origin and the center of the current triangle + float origin2TriangleCenterDistance = length(origin2TriangleCenterVector); + + // the distance from origin to the triangle center with eventual randomness added + float fullDistanceWithRandomness = origin2TriangleCenterDistance + randomDistance; + + // vector from the triangle center to the explosion's shockwave + vec3 triangleCenter2ExplosionRadius = (normalizedOrigin2TriangleCenterVector * (TimeSinceDeath * currentVelocity)) - (normalizedOrigin2TriangleCenterVector * fullDistanceWithRandomness); + + // if the triangle is inside the blast radius... + if (fullDistanceWithRandomness <= (TimeSinceDeath * currentVelocity)) + { + float maxRadius = max(TimeSinceDeath * currentVelocity, (ExplosionDuration * currentVelocity)); + + float a = (randomVelocity.y - randomVelocity.x) / ExplosionDuration; + //float t = sqrt((2 * origin2TriangleCenterDistance) / a); + + // if explosion color should be affected by distance instead of time... + if (ColorByDistance == true) + { + // calculate the max distance (s) the triangle will move + float s = (randomVelocity.x * ExplosionDuration) + (0.5 * a * pow(ExplosionDuration, 2)); + + Output.ExplosionColor = EndColor * (length(triangleCenter2ExplosionRadius) / s); + } + else + { + Output.ExplosionColor = EndColor * timePercetage; + } + + // for every vertex on the triangle... + for (int i = 0; i < gl_in.length(); i++) + { + // move the triangle to the blast radius + vec3 ExplodedPosition = Input[i].Position + triangleCenter2ExplosionRadius; + + // pass through vertex data + Output.Normal = Input[i].Normal; + Output.Position = Input[i].Position; + Output.TextureCoordinate = Input[i].TextureCoordinate; + + // convert to model space for the gravity to always be in -y + vec4 ExplodedPositionInModelSpace = M * vec4(ExplodedPosition, 1.0); + + // if there is gravity, apply it + //if (Gravity == true) + //{ + // // ---DO THIS----> //ExplodedPositionInModelSpace.y = ExplodedPositionInModelSpace.y - TimeSinceHit; + // + // ExplodedPositionInModelSpace.y = ExplodedPositionInModelSpace.y - pow(TimeSinceDeath, 2); + //} + + // convert to screen space + gl_Position = P*V * ExplodedPositionInModelSpace; + EmitVertex(); + } + } + else + { + // if explosion color should be affected by distance instead of time... + if (ColorByDistance == true) + { + Output.ExplosionColor = vec4(0.0); + } + else + { + Output.ExplosionColor = EndColor * timePercetage; + } + + // for every vertex on the triangle... + for(int i = 0; i < gl_in.length(); i++) + { + // pass through vertex data + Output.Normal = Input[i].Normal; + Output.Position = Input[i].Position; + Output.TextureCoordinate = Input[i].TextureCoordinate; + + // no change in position, pass through vertex + gl_Position = gl_in[i].gl_Position; + EmitVertex(); + } + } + + //EndPrimitive(); +} diff --git a/resources/Shaders/ExplosionEffect.vert.glsl b/resources/Shaders/ExplosionEffect.vert.glsl new file mode 100644 index 00000000..04e1bda2 --- /dev/null +++ b/resources/Shaders/ExplosionEffect.vert.glsl @@ -0,0 +1,36 @@ +#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; + vec4 ExplosionColor; +}Output; + +void main() +{ + gl_Position = P*V*M * vec4(Position, 1.0); + + Output.Position = Position; + Output.TextureCoordinate = TextureCoords; + Output.Normal = Normal; + Output.DiffuseColor = DiffuseVertexColor; + Output.ExplosionColor = vec4(0.0); +} \ No newline at end of file diff --git a/resources/Shaders/ForwardPlus.frag.glsl b/resources/Shaders/ForwardPlus.frag.glsl index f33d870b..42500b44 100644 --- a/resources/Shaders/ForwardPlus.frag.glsl +++ b/resources/Shaders/ForwardPlus.frag.glsl @@ -50,6 +50,7 @@ in VertexData{ vec3 Position; vec3 Normal; vec2 TextureCoordinate; + vec4 ExplosionColor; }Input; out vec4 sceneColor; @@ -137,8 +138,8 @@ void main() totalLighting.Specular += light_result.Specular; } - //sceneColor += Input.DiffuseColor; - vec4 color_result = DiffuseColor * (totalLighting.Diffuse + totalLighting.Specular) * diffuseTexel * Color; + + vec4 color_result = (DiffuseColor + Input.ExplosionColor) * (totalLighting.Diffuse + totalLighting.Specular) * diffuseTexel * Color; float pos = ((P * vec4(Input.Position, 1)).y + 1.0)/2.0; @@ -161,9 +162,7 @@ void main() } else { bloomColor = vec4(0.0, 0.0, 0.0, 1.0); } */ - - //sceneColor += Input.DiffuseColor * (totalLighting.Diffuse) * diffuseTexel * Color; - //sceneColor += Input.DiffuseColor + vec4(0.0, LightGrids.Data[currentTile].Amount/3, 0, 1); + diff --git a/resources/Shaders/ForwardPlus.vert.glsl b/resources/Shaders/ForwardPlus.vert.glsl index c51216cb..bd7df47e 100644 --- a/resources/Shaders/ForwardPlus.vert.glsl +++ b/resources/Shaders/ForwardPlus.vert.glsl @@ -17,6 +17,7 @@ out VertexData{ vec3 Position; vec3 Normal; vec2 TextureCoordinate; + vec4 ExplosionColor; }Output; void main() @@ -36,4 +37,5 @@ void main() Output.Position = (boneTransform * vec4(Position, 1.0)).xyz; Output.TextureCoordinate = TextureCoords; Output.Normal = vec3(M * vec4(Normal, 0.0)); + Output.ExplosionColor = vec4(0.0); } \ No newline at end of file diff --git a/src/Engine/Rendering/DrawFinalPass.cpp b/src/Engine/Rendering/DrawFinalPass.cpp index 0cd0e576..a85a80d6 100644 --- a/src/Engine/Rendering/DrawFinalPass.cpp +++ b/src/Engine/Rendering/DrawFinalPass.cpp @@ -25,7 +25,7 @@ void DrawFinalPass::InitializeFrameBuffers() //GenerateTexture(&m_BloomTexture, GL_CLAMP_TO_EDGE, GL_LINEAR, glm::vec2(m_Renderer->Resolution().Width, m_Renderer->Resolution().Height), GL_RGB16F, GL_RGB, GL_FLOAT); GenerateTexture(&m_BloomTexture, GL_CLAMP_TO_EDGE, GL_LINEAR, glm::vec2(m_Renderer->Resolution().Width, m_Renderer->Resolution().Height), GL_RGB16F, GL_RGB, GL_FLOAT); //GenerateMipMapTexture(&m_BloomTexture, GL_CLAMP_TO_EDGE, glm::vec2(m_Renderer->Resolution().Width, m_Renderer->Resolution().Height), GL_RGB16F, GL_FLOAT, 4); - + m_FinalPassFrameBuffer.AddResource(std::shared_ptr(new RenderBuffer(&m_DepthBuffer, GL_DEPTH_ATTACHMENT))); m_FinalPassFrameBuffer.AddResource(std::shared_ptr(new Texture2D(&m_SceneTexture, GL_COLOR_ATTACHMENT0))); m_FinalPassFrameBuffer.AddResource(std::shared_ptr(new Texture2D(&m_BloomTexture, GL_COLOR_ATTACHMENT1))); @@ -42,6 +42,15 @@ void DrawFinalPass::InitializeShaderPrograms() m_ForwardPlusProgram->BindFragDataLocation(0, "sceneColor"); m_ForwardPlusProgram->BindFragDataLocation(1, "bloomColor"); m_ForwardPlusProgram->Link(); + + m_ExplosionEffectProgram = ResourceManager::Load("#ExplosionEffectProgram"); + m_ExplosionEffectProgram->AddShader(std::shared_ptr(new VertexShader("Shaders/ForwardPlus.vert.glsl"))); + m_ExplosionEffectProgram->AddShader(std::shared_ptr(new GeometryShader("Shaders/ExplosionEffect.geom.glsl"))); + m_ExplosionEffectProgram->AddShader(std::shared_ptr(new FragmentShader("Shaders/ForwardPlus.frag.glsl"))); + m_ExplosionEffectProgram->Compile(); + m_ExplosionEffectProgram->BindFragDataLocation(0, "sceneColor"); + m_ExplosionEffectProgram->BindFragDataLocation(1, "bloomColor"); + m_ExplosionEffectProgram->Link(); } void DrawFinalPass::Draw(RenderScene& scene) @@ -49,68 +58,123 @@ void DrawFinalPass::Draw(RenderScene& scene) GLERROR("DrawFinalPass::Draw: Pre"); DrawFinalPassState* state = new DrawFinalPassState(m_FinalPassFrameBuffer.GetHandle()); - - m_ForwardPlusProgram->Bind(); - GLuint shaderHandle = m_ForwardPlusProgram->GetHandle(); - - if (scene.ClearDepth) { - glClear(GL_DEPTH_BUFFER_BIT); - } - - glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, m_LightCullingPass->LightSSBO()); - glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, m_LightCullingPass->LightGridSSBO()); - glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 4, m_LightCullingPass->LightIndexSSBO()); - - glUniformMatrix4fv(glGetUniformLocation(shaderHandle, "V"), 1, GL_FALSE, glm::value_ptr(scene.Camera->ViewMatrix())); - glUniformMatrix4fv(glGetUniformLocation(shaderHandle, "P"), 1, GL_FALSE, glm::value_ptr(scene.Camera->ProjectionMatrix())); - glUniform2f(glGetUniformLocation(shaderHandle, "ScreenDimensions"), m_Renderer->Resolution().Width, m_Renderer->Resolution().Height); + GLuint shaderHandle; //TODO: Render: Add code for more jobs than modeljobs. for (auto &job : scene.ForwardJobs) { - auto modelJob = std::dynamic_pointer_cast(job); - if(modelJob) { + auto explosionEffectJob = std::dynamic_pointer_cast(job); + if (explosionEffectJob) { + m_ExplosionEffectProgram->Bind(); + shaderHandle = m_ExplosionEffectProgram->GetHandle(); //--- + + GLERROR("DrawFinalPass::ExplosionEffect: 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->Matrix)); - glUniform4fv(glGetUniformLocation(shaderHandle, "Color"), 1, glm::value_ptr(modelJob->Color)); - glUniform4fv(glGetUniformLocation(shaderHandle, "DiffuseColor"), 1, glm::value_ptr(modelJob->DiffuseColor)); + glUniformMatrix4fv(glGetUniformLocation(shaderHandle, "M"), 1, GL_FALSE, glm::value_ptr(explosionEffectJob->Matrix)); + glUniformMatrix4fv(glGetUniformLocation(shaderHandle, "V"), 1, GL_FALSE, glm::value_ptr(scene.Camera->ViewMatrix())); + glUniformMatrix4fv(glGetUniformLocation(shaderHandle, "P"), 1, GL_FALSE, glm::value_ptr(scene.Camera->ProjectionMatrix())); + glUniform4fv(glGetUniformLocation(shaderHandle, "Color"), 1, glm::value_ptr(explosionEffectJob->Color)); + glUniform3fv(glGetUniformLocation(shaderHandle, "ExplosionOrigin"), 1, glm::value_ptr(explosionEffectJob->ExplosionOrigin)); + glUniform1f(glGetUniformLocation(shaderHandle, "TimeSinceDeath"), explosionEffectJob->TimeSinceDeath); + glUniform1f(glGetUniformLocation(shaderHandle, "ExplosionDuration"), explosionEffectJob->ExplosionDuration); + glUniform4fv(glGetUniformLocation(shaderHandle, "EndColor"), 1, glm::value_ptr(explosionEffectJob->EndColor)); + glUniform1i(glGetUniformLocation(shaderHandle, "Randomness"), explosionEffectJob->Randomness); + glUniform1fv(glGetUniformLocation(shaderHandle, "RandomNumbers"), 50, explosionEffectJob->RandomNumbers.data()); + glUniform1f(glGetUniformLocation(shaderHandle, "RandomnessScalar"), explosionEffectJob->RandomnessScalar); + glUniform2fv(glGetUniformLocation(shaderHandle, "Velocity"), 1, glm::value_ptr(explosionEffectJob->Velocity)); + glUniform1i(glGetUniformLocation(shaderHandle, "ColorByDistance"), explosionEffectJob->ColorByDistance); + glUniform1i(glGetUniformLocation(shaderHandle, "ExponentialAccelaration"), explosionEffectJob->ExponentialAccelaration); + glUniform4fv(glGetUniformLocation(shaderHandle, "DiffuseColor"), 1, glm::value_ptr(explosionEffectJob->DiffuseColor)); + GLERROR("DrawFinalPass::ExplosionEffect: 2"); + + if (explosionEffectJob->Model->m_RawModel->m_Skeleton != nullptr) { + + if (explosionEffectJob->Animation != nullptr) { + std::vector frameBones = explosionEffectJob->Skeleton->GetFrameBones(*explosionEffectJob->Animation, explosionEffectJob->AnimationTime); + glUniformMatrix4fv(glGetUniformLocation(shaderHandle, "Bones"), frameBones.size(), GL_FALSE, glm::value_ptr(frameBones[0])); + } + } + + //TODO: Renderer: bättre textur felhantering samt fler texturer stöd + if (explosionEffectJob->DiffuseTexture != nullptr) { + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, explosionEffectJob->DiffuseTexture->m_Texture); + } else { + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, m_WhiteTexture->m_Texture); + } + glBindVertexArray(explosionEffectJob->Model->VAO); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, explosionEffectJob->Model->ElementBuffer); + glDrawElements(GL_TRIANGLES, explosionEffectJob->EndIndex - explosionEffectJob->StartIndex + 1, GL_UNSIGNED_INT, (void*)(explosionEffectJob->StartIndex*sizeof(unsigned int))); + GLERROR("DrawFinalPass::ExplosionEffect: END"); + //m_ExplosionEffectProgram->Unbind(); + + } else { + auto modelJob = std::dynamic_pointer_cast(job); + if (modelJob) { + m_ForwardPlusProgram->Bind(); + GLERROR("1.1"); + shaderHandle = m_ForwardPlusProgram->GetHandle(); + + if (scene.ClearDepth) { + glClear(GL_DEPTH_BUFFER_BIT); + } + + glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, m_LightCullingPass->LightSSBO()); + glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, m_LightCullingPass->LightGridSSBO()); + glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 4, m_LightCullingPass->LightIndexSSBO()); + glUniformMatrix4fv(glGetUniformLocation(shaderHandle, "V"), 1, GL_FALSE, glm::value_ptr(scene.Camera->ViewMatrix())); + glUniformMatrix4fv(glGetUniformLocation(shaderHandle, "P"), 1, GL_FALSE, glm::value_ptr(scene.Camera->ProjectionMatrix())); + glUniform2f(glGetUniformLocation(shaderHandle, "ScreenDimensions"), m_Renderer->Resolution().Width, m_Renderer->Resolution().Height); + + //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->Matrix)); + glUniform4fv(glGetUniformLocation(shaderHandle, "Color"), 1, glm::value_ptr(modelJob->Color)); + glUniform4fv(glGetUniformLocation(shaderHandle, "DiffuseColor"), 1, glm::value_ptr(modelJob->DiffuseColor)); glUniform4fv(glGetUniformLocation(shaderHandle, "FillColor"), 1, glm::value_ptr(modelJob->FillColor)); glUniform1f(glGetUniformLocation(shaderHandle, "FillPercentage"), modelJob->FillPercentage); - glActiveTexture(GL_TEXTURE0); - if(modelJob->DiffuseTexture != nullptr) { - glBindTexture(GL_TEXTURE_2D, modelJob->DiffuseTexture->m_Texture); - } else { - glBindTexture(GL_TEXTURE_2D, m_WhiteTexture->m_Texture); - } - glActiveTexture(GL_TEXTURE1); - if (modelJob->IncandescenceTexture != nullptr) { - glBindTexture(GL_TEXTURE_2D, modelJob->IncandescenceTexture->m_Texture); - } else { - glBindTexture(GL_TEXTURE_2D, m_BlackTexture->m_Texture); - } - - /*if(modelJob->GlowMap != nullptr) { - glBindTexture(GL_TEXTURE_2D, modelJob->GlowMap->m_Texture); - } else { - glBindTexture(GL_TEXTURE_2D, m_BlackTexture->m_Texture); - }*/ - - //TODO: Fixa så att modelsJobs kan spela upp olika animationer och så att den kan få in en tid istället för 1.0f - Hälsningar Johan och Andreas :) - if (modelJob->Model->m_RawModel->m_Skeleton != nullptr) { - - if (modelJob->Animation != nullptr) { - std::vector frameBones = modelJob->Skeleton->GetFrameBones( *modelJob->Animation, modelJob->AnimationTime); - glUniformMatrix4fv(glGetUniformLocation(shaderHandle, "Bones"), frameBones.size(), GL_FALSE, glm::value_ptr(frameBones[0])); + if (modelJob->DiffuseTexture != nullptr) { + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, modelJob->DiffuseTexture->m_Texture); + } else { + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, m_WhiteTexture->m_Texture); + } + glActiveTexture(GL_TEXTURE1); + if (modelJob->IncandescenceTexture != nullptr) { + glBindTexture(GL_TEXTURE_2D, modelJob->IncandescenceTexture->m_Texture); + } else { + glBindTexture(GL_TEXTURE_2D, m_BlackTexture->m_Texture); } - } - glBindVertexArray(modelJob->Model->VAO); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, modelJob->Model->ElementBuffer); - glDrawElements(GL_TRIANGLES, modelJob->EndIndex - modelJob->StartIndex + 1, GL_UNSIGNED_INT, (void*)(modelJob->StartIndex * sizeof(unsigned int))); + /*if(modelJob->GlowMap != nullptr) { + glBindTexture(GL_TEXTURE_2D, modelJob->GlowMap->m_Texture); + } else { + glBindTexture(GL_TEXTURE_2D, m_BlackTexture->m_Texture); + }*/ + + //TODO: Fixa så att modelsJobs kan spela upp olika animationer och så att den kan få in en tid istället för 1.0f - Hälsningar Johan och Andreas :) + if (modelJob->Model->m_RawModel->m_Skeleton != nullptr) { + + if (modelJob->Animation != nullptr) { + std::vector frameBones = modelJob->Skeleton->GetFrameBones(*modelJob->Animation, modelJob->AnimationTime); + glUniformMatrix4fv(glGetUniformLocation(shaderHandle, "Bones"), frameBones.size(), GL_FALSE, glm::value_ptr(frameBones[0])); + } + } + + glBindVertexArray(modelJob->Model->VAO); + + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, modelJob->Model->ElementBuffer); + glDrawElements(GL_TRIANGLES, modelJob->EndIndex - modelJob->StartIndex + 1, GL_UNSIGNED_INT, (void*)(modelJob->StartIndex*sizeof(unsigned int))); + GLERROR("DrawFinalPass::Model: END"); + // m_ForwardPlusProgram->Unbind(); + + } } + } GLERROR("DrawFinalPass::Draw: END"); delete state; diff --git a/src/Engine/Rendering/RenderSystem.cpp b/src/Engine/Rendering/RenderSystem.cpp index a67f2c75..6d40ed29 100644 --- a/src/Engine/Rendering/RenderSystem.cpp +++ b/src/Engine/Rendering/RenderSystem.cpp @@ -78,19 +78,41 @@ void RenderSystem::fillModels(std::list>& jobs) float fillPercentage = 0.f; glm::vec4 fillColor = glm::vec4(0); - if(m_World->HasComponent(modelComponent.EntityID, "Fill")) { auto fillComponent = m_World->GetComponent(modelComponent.EntityID, "Fill"); fillPercentage = (float)(double)fillComponent["Percentage"]; fillColor = (glm::vec4)fillComponent["Color"]; } - - glm::mat4 modelMatrix = Transform::ModelMatrix(modelComponent.EntityID, m_World); for (auto matGroup : model->MaterialGroups()) { - std::shared_ptr modelJob = std::shared_ptr(new ModelJob(model, m_Camera, modelMatrix, matGroup, modelComponent, m_World, fillColor, fillPercentage)); - jobs.push_back(modelJob); + if (m_World->HasComponent(modelComponent.EntityID, "ExplosionEffect")) { + auto explosionEffectComponent = m_World->GetComponent(modelComponent.EntityID, "ExplosionEffect"); + std::shared_ptr explosionEffectJob = std::shared_ptr(new ExplosionEffectJob( + explosionEffectComponent, + model, + m_Camera, + modelMatrix, + matGroup, + modelComponent, + m_World, + fillColor, + fillPercentage + )); + jobs.push_back(explosionEffectJob); + } else { + std::shared_ptr modelJob = std::shared_ptr(new ModelJob( + model, + m_Camera, + modelMatrix, + matGroup, + modelComponent, + m_World, + fillColor, + fillPercentage + )); + jobs.push_back(modelJob); + } } } } diff --git a/src/Engine/Rendering/Renderer.cpp b/src/Engine/Rendering/Renderer.cpp index 4f687a99..af64b5cf 100644 --- a/src/Engine/Rendering/Renderer.cpp +++ b/src/Engine/Rendering/Renderer.cpp @@ -63,6 +63,15 @@ void Renderer::InitializeWindow() void Renderer::InitializeShaders() { m_BasicForwardProgram = ResourceManager::Load("#m_BasicForwardProgram"); + m_ExplosionEffectProgram = ResourceManager::Load("#ExplosionEffectProgram"); + //m_ExplosionEffectProgram->AddShader(std::shared_ptr(new VertexShader("Shaders/ExplosionEffect.vert.glsl"))); + //m_ExplosionEffectProgram->AddShader(std::shared_ptr(new GeometryShader("Shaders/ExplosionEffect.geom.glsl"))); + //m_ExplosionEffectProgram->AddShader(std::shared_ptr(new FragmentShader("Shaders/ExplosionEffect.frag.glsl"))); + //m_ExplosionEffectProgram->Compile(); + //m_ExplosionEffectProgram->Link(); + + + } void Renderer::InputUpdate(double dt) @@ -98,6 +107,7 @@ void Renderer::Draw(RenderFrame& frame) m_LightCullingPass->FillLightList(*scene); m_LightCullingPass->CullLights(*scene); m_DrawFinalPass->Draw(*scene); + //m_DrawScenePass->Draw(*scene); GLERROR("Renderer::Draw m_DrawScenePass->Draw"); diff --git a/src/Game/Game.cpp b/src/Game/Game.cpp index 903b270f..d13d62d4 100644 --- a/src/Game/Game.cpp +++ b/src/Game/Game.cpp @@ -80,6 +80,7 @@ Game::Game(int argc, char* argv[]) // All systems with orderlevel 0 will be updated first. unsigned int updateOrderLevel = 0; m_SystemPipeline->AddSystem(updateOrderLevel); + m_SystemPipeline->AddSystem(updateOrderLevel); m_SystemPipeline->AddSystem(updateOrderLevel); m_SystemPipeline->AddSystem(updateOrderLevel); m_SystemPipeline->AddSystem(updateOrderLevel);