diff --git a/src/Components/BlendMap.h b/src/Components/BlendMap.h new file mode 100644 index 0000000..aba9f7c --- /dev/null +++ b/src/Components/BlendMap.h @@ -0,0 +1,38 @@ +#ifndef Components_BlendMap_h__ +#define Components_BlendMap_h__ + +#include "Component.h" +#include "Entity.h" + +namespace Components +{ + struct BlendMap : Component + { + BlendMap() + : TextureRed("Textures/ErrorTextureRed.png") + , TextureRedNormal("Textures/NeutralNormalMap.png") + , TextureRedSpecular("Textures/NeutralSpecularMap.png") + , TextureGreen("Textures/ErrorTextureGreen.png") + , TextureGreenNormal("Textures/NeutralNormalMap.png") + , TextureGreenSpecular("Textures/NeutralSpecularMap.png") + , TextureBlue("Textures/ErrorTextureBlue.png") + , TextureBlueNormal("Textures/NeutralNormalMap.png") + , TextureBlueSpecular("Textures/NeutralSpecularMap.png") + , TextureRepeats(100.f) { } + + std::string TextureRed; + std::string TextureRedNormal; + std::string TextureRedSpecular; + std::string TextureGreen; + std::string TextureGreenNormal; + std::string TextureGreenSpecular; + std::string TextureBlue; + std::string TextureBlueNormal; + std::string TextureBlueSpecular; + float TextureRepeats; + + virtual BlendMap* Clone() const override { return new BlendMap(*this); } + }; + +} +#endif // !Components_BlendMap_h__ \ No newline at end of file diff --git a/src/Components/Model.h b/src/Components/Model.h index cacfe17..7ac792f 100755 --- a/src/Components/Model.h +++ b/src/Components/Model.h @@ -4,18 +4,18 @@ #include #include "Component.h" -#include "Color.h" namespace Components { struct Model : Component { - Model() : Visible(true), ShadowCaster(true) { } + Model() : Color(glm::vec4(1.0f, 1.0f, 1.0f, 1.0f)), Visible(true), ShadowCaster(true), Transparent(false) { } std::string ModelFile; - Color Color; + glm::vec4 Color; bool Visible; bool ShadowCaster; + bool Transparent; virtual Model* Clone() const override { return new Model(*this); } }; diff --git a/src/Components/Particle.h b/src/Components/Particle.h index d08b1a7..065ab62 100644 --- a/src/Components/Particle.h +++ b/src/Components/Particle.h @@ -11,9 +11,10 @@ namespace Components struct Particle : Component { - std::vector ColorSpectrum; std::vector ScaleSpectrum; double LifeTime; + double SpawnTime; + bool Fade; std::vector VelocitySpectrum; std::vector AngularVelocitySpectrum; std::vector OrientationSpectrum; //Keep? diff --git a/src/Components/ParticleEmitter.h b/src/Components/ParticleEmitter.h index 03eaf05..f7cb678 100755 --- a/src/Components/ParticleEmitter.h +++ b/src/Components/ParticleEmitter.h @@ -19,20 +19,22 @@ struct ParticleEmitter : Component , SpawnCount(0) , SpreadAngle(0) , LifeTime(0) - , TimeSinceLastSpawn(100) { } // TEMP fulhack så att partiklarna spawnar direkt + , TimeSinceLastSpawn(100) + , Color(glm::vec4(0)) { } EntityID ParticleTemplate; float SpawnFrequency; float Speed; int SpawnCount; - std::vector ColorSpectrum; + glm::vec4 Color; std::vector ScaleSpectrum; float SpreadAngle; double LifeTime; bool UseGoalVelocity; + bool Fade; glm::vec3 GoalVelocity; std::vector AngularVelocitySpectrum; - std::vector OrientationSpectrum; //Keep? + std::vector OrientationSpectrum; //Keep? noo.. private: double TimeSinceLastSpawn; diff --git a/src/Components/Sprite.h b/src/Components/Sprite.h index 29c930f..d28f058 100755 --- a/src/Components/Sprite.h +++ b/src/Components/Sprite.h @@ -4,15 +4,16 @@ #include #include "Component.h" -#include "Color.h" namespace Components { struct Sprite : Component { + Sprite() : Color(glm::vec4(1.0f, 1.0f, 1.0f, 1.0f)) { } + std::string SpriteFile; - Color Color; + glm::vec4 Color; virtual Sprite* Clone() const override { return new Sprite(*this); } }; diff --git a/src/Events/CreateExplosion.h b/src/Events/CreateExplosion.h new file mode 100644 index 0000000..0fb6310 --- /dev/null +++ b/src/Events/CreateExplosion.h @@ -0,0 +1,27 @@ +#ifndef Event_CreateExplosion_h__ +#define Event_CreateExplosion_h__ + +#include "EventBroker.h" +#include +#include + +namespace Events +{ + struct CreateExplosion : Event + { + CreateExplosion() + : Color(glm::vec4(0)) {} + glm::vec3 Position; + glm::vec4 Color; + double LifeTime; + int ParticlesToSpawn; + std::string spritePath; + glm::quat RelativeUpOrientation; + float Speed; + float SpreadAngle; + std::vector ParticleScale; + }; + +} + +#endif // Event_CreateExplosion_h__ diff --git a/src/GUI/Frame.h b/src/GUI/Frame.h index 47d5146..6a649b5 100644 --- a/src/GUI/Frame.h +++ b/src/GUI/Frame.h @@ -45,7 +45,7 @@ public: , m_Hidden(false) { SetParent(std::shared_ptr(parent)); Initialize(); } - ::RenderQueue RenderQueue; + ::RenderQueuePair RenderQueue; std::shared_ptr Parent() const { return m_Parent; } void SetParent(std::shared_ptr parent) diff --git a/src/GUI/TextureFrame.h b/src/GUI/TextureFrame.h index f15dfd8..aced664 100644 --- a/src/GUI/TextureFrame.h +++ b/src/GUI/TextureFrame.h @@ -32,7 +32,7 @@ public: job.TextureID = m_Texture->ResourceID; job.Texture = *m_Texture; job.Color = glm::vec4(m_Color.r, m_Color.g, m_Color.b, m_Color.a * m_CurrentFade); - RenderQueue.Add(job); + RenderQueue.Forward.Add(job); } // Texture while fading @@ -42,7 +42,7 @@ public: job.TextureID = m_FadeTexture->ResourceID; job.Texture = *m_FadeTexture; job.Color = glm::vec4(m_Color.r, m_Color.g, m_Color.b, m_Color.a); - RenderQueue.Add(job); + RenderQueue.Forward.Add(job); } renderer->SetCamera(nullptr); diff --git a/src/GUI/WorldFrame.h b/src/GUI/WorldFrame.h index 995b133..03b9708 100644 --- a/src/GUI/WorldFrame.h +++ b/src/GUI/WorldFrame.h @@ -11,6 +11,8 @@ #include "Components/Model.h" #include "Components/Sprite.h" #include "Components/PointLight.h" +#include "Components/BlendMap.h" + namespace GUI { @@ -42,6 +44,10 @@ public: { EntityID entity = pair.first; + auto templateComponent = m_World->GetComponent(entity); + if (templateComponent) + continue; + auto transform = m_World->GetComponent(entity); if (!transform) continue; @@ -56,7 +62,33 @@ public: glm::mat4 modelMatrix = glm::translate(glm::mat4(), absoluteTransform.Position) * glm::toMat4(absoluteTransform.Orientation) * glm::scale(absoluteTransform.Scale); - EnqueueModel(modelAsset, modelMatrix); + auto blendmapComponent = m_World->GetComponent(entity); + if (blendmapComponent) + { + BlendMapTexture RedTexture; + RedTexture.Diffuse = *ResourceManager->Load("Texture", blendmapComponent->TextureRed); + RedTexture.Normal = *ResourceManager->Load("Texture", blendmapComponent->TextureRedNormal); + RedTexture.Specular = *ResourceManager->Load("Texture", blendmapComponent->TextureRedSpecular); + + BlendMapTexture GreenTexture; + GreenTexture.Diffuse = *ResourceManager->Load("Texture", blendmapComponent->TextureGreen); + GreenTexture.Normal = *ResourceManager->Load("Texture", blendmapComponent->TextureGreenNormal); + GreenTexture.Specular = *ResourceManager->Load("Texture", blendmapComponent->TextureGreenSpecular); + + BlendMapTexture BlueTexture; + BlueTexture.Diffuse = *ResourceManager->Load("Texture", blendmapComponent->TextureBlue); + BlueTexture.Normal = *ResourceManager->Load("Texture", blendmapComponent->TextureBlueNormal); + BlueTexture.Specular = *ResourceManager->Load("Texture", blendmapComponent->TextureBlueSpecular); + + float textureRepeat = blendmapComponent->TextureRepeats; + + EnqueueBlendMapModel(modelAsset, RedTexture, GreenTexture, BlueTexture, textureRepeat, modelMatrix, modelComponent->Color); + } + else + { + EnqueueModel(modelAsset, modelMatrix, modelComponent->Transparent, modelComponent->Color); + } + } } @@ -71,7 +103,7 @@ public: glm::mat4 modelMatrix = glm::translate(absoluteTransform.Position) * glm::toMat4(orientation2D) * glm::scale(absoluteTransform.Scale); - EnqueueSprite(textureAsset, modelMatrix); + EnqueueSprite(textureAsset, modelMatrix, spriteComponent->Color); } } @@ -91,12 +123,21 @@ public: ); } } + + RenderQueue.Sort(); } protected: std::shared_ptr m_World; private: + struct BlendMapTexture + { + GLuint Diffuse; + GLuint Normal; + GLuint Specular; + }; + EventRelay m_ESetViewportCamera; bool OnSetViewportCamera(const Events::SetViewportCamera &event) { @@ -116,7 +157,7 @@ private: std::shared_ptr m_TransformSystem; - void EnqueueModel(Model* model, glm::mat4 modelMatrix) + void EnqueueModel(Model* model, glm::mat4 modelMatrix, float transparent, glm::vec4 color) { for (auto texGroup : model->TextureGroups) { @@ -129,20 +170,60 @@ private: job.StartIndex = texGroup.StartIndex; job.EndIndex = texGroup.EndIndex; job.ModelMatrix = modelMatrix; - - RenderQueue.Add(job); + job.Transparent = transparent; + job.Color = color; + + if(job.Transparent) + { + RenderQueue.Forward.Add(job); + } + else + { + RenderQueue.Deferred.Add(job); + } } } - void EnqueueSprite(Texture* texture, glm::mat4 modelMatrix) + void EnqueueBlendMapModel(Model* model, BlendMapTexture textureRed, BlendMapTexture textureGreen, BlendMapTexture textureBlue, float textureRepeat, glm::mat4 modelMatrix, glm::vec4 color) + { + for (auto texGroup : model->TextureGroups) + { + BlendMapModelJob job; + job.TextureID = texGroup.Texture->ResourceID; + job.DiffuseTexture = *texGroup.Texture; + job.NormalTexture = (texGroup.NormalMap) ? *texGroup.NormalMap : 0; + job.SpecularTexture = (texGroup.SpecularMap) ? *texGroup.SpecularMap : 0; + job.BlendMapTextureRed = textureRed.Diffuse; + job.BlendMapTextureRedNormal = textureRed.Normal; + job.BlendMapTextureRedSpecular = textureRed.Specular; + job.BlendMapTextureGreen = textureGreen.Diffuse; + job.BlendMapTextureGreenNormal = textureGreen.Normal; + job.BlendMapTextureGreenSpecular = textureGreen.Specular; + job.BlendMapTextureBlue = textureBlue.Diffuse; + job.BlendMapTextureBlueNormal = textureBlue.Normal; + job.BlendMapTextureBlueSpecular = textureBlue.Specular; + job.TextureRepeat = textureRepeat; + job.VAO = model->VAO; + job.StartIndex = texGroup.StartIndex; + job.EndIndex = texGroup.EndIndex; + job.ModelMatrix = modelMatrix; + job.Color = color; + + RenderQueue.Deferred.Add(job); + } + } + + void EnqueueSprite(Texture* texture, glm::mat4 modelMatrix, glm::vec4 color) { SpriteJob job; job.TextureID = texture->ResourceID; job.Texture = *texture; job.ModelMatrix = modelMatrix; + job.Color = color; - RenderQueue.Add(job); + RenderQueue.Forward.Add(job); } + }; } diff --git a/src/GameWorld.cpp b/src/GameWorld.cpp index aaa25bc..4faebb7 100755 --- a/src/GameWorld.cpp +++ b/src/GameWorld.cpp @@ -72,38 +72,294 @@ void GameWorld::Initialize() EventBroker->Publish(e); } - { - auto ground = CreateEntity(); - auto transform = AddComponent(ground); - transform->Position = glm::vec3(0, -50, 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->ModelFile = "Models/TestScene3/testScene2.obj"; - //model->ModelFile = "Models/Placeholders/Terrain/Terrain2.obj"; + { + auto road_base = CreateEntity(); + auto transform = AddComponent(road_base); + transform->Position = glm::vec3(0, -50, 0); + auto model = AddComponent(road_base); + model->ModelFile = "Models/TerrainFiveIstles/Roads/BaseRoad.obj"; + auto physics = AddComponent(road_base); + physics->Mass = 10; + physics->Static = true; + physics->CollisionLayer = 1; + + auto groundshape = CreateEntity(road_base); + auto transformshape = AddComponent(groundshape); + auto meshShape = AddComponent(groundshape); + meshShape->ResourceName = "Models/TerrainFiveIstles/Roads/BaseRoad.obj"; + + + CommitEntity(groundshape); + CommitEntity(road_base); + } - auto physics = AddComponent(ground); + { + auto road_middle = CreateEntity(); + auto transform = AddComponent(road_middle); + transform->Position = glm::vec3(0, -50, 0); + auto model = AddComponent(road_middle); + model->ModelFile = "Models/TerrainFiveIstles/Roads/MiddleRoad.obj"; + auto physics = AddComponent(road_middle); physics->Mass = 10; physics->Static = true; - physics->CollisionLayer = (int)Components::Physics::CollisionLayer::STATIC; + physics->CollisionLayer = 1; - auto groundshape = CreateEntity(ground); + auto groundshape = CreateEntity(road_middle); auto transformshape = AddComponent(groundshape); auto meshShape = AddComponent(groundshape); - //meshShape->ResourceName = "Models/Placeholders/Terrain/Terrain2.obj"; - meshShape->ResourceName = "Models/TestScene3/testScene2.obj"; + meshShape->ResourceName = "Models/TerrainFiveIstles/Roads/MiddleRoad.obj"; + CommitEntity(groundshape); + CommitEntity(road_middle); + } + + { + auto road_small = CreateEntity(); + auto transform = AddComponent(road_small); + transform->Position = glm::vec3(0, -50, 0); + auto model = AddComponent(road_small); + model->ModelFile = "Models/TerrainFiveIstles/Roads/SmallRoad.obj"; + auto physics = AddComponent(road_small); + physics->Mass = 10; + physics->Static = true; + physics->CollisionLayer = 1; + + auto groundshape = CreateEntity(road_small); + auto transformshape = AddComponent(groundshape); + auto meshShape = AddComponent(groundshape); + meshShape->ResourceName = "Models/TerrainFiveIstles/Roads/SmallRoad.obj"; CommitEntity(groundshape); - CommitEntity(ground); + CommitEntity(road_small); + } + + + { + auto water = CreateEntity(); + auto transform = AddComponent(water); + transform->Position = glm::vec3(0, -35, 0); + transform->Orientation = glm::quat(glm::vec3(0.0f, 0.0f, 0.0f)); + transform->Scale = glm::vec3(5000.f, 10.f, 5000.f); + auto model = AddComponent(water); + model->ModelFile = "Models/Placeholders/PhysicsTest/Cube.obj"; + auto blendmap = AddComponent(water); + blendmap->TextureRed = "Textures/Skybox/Sky34/bottom.jpg"; + blendmap->TextureGreen = "Textures/Skybox/Sky34/bottom.jpg"; + blendmap->TextureBlue = "Textures/Skybox/Sky34/bottom.jpg"; + blendmap->TextureRepeats = 1.f; + + auto physics = AddComponent(water); + physics->Mass = 10; + physics->Static = true; + physics->CollisionLayer = (int)Components::Physics::CollisionLayer::STATIC; + { + + auto groundshape = CreateEntity(water); + auto box = AddComponent(groundshape); + box->Depth = 250.f; + box->Height = 5.f; + box->Width = 250.f; + + + CommitEntity(groundshape); + } + CommitEntity(water); + } + + { + auto ground_middle = CreateEntity(); + auto transform = AddComponent(ground_middle); + transform->Position = glm::vec3(0, -50, 0); + transform->Orientation = glm::quat(glm::vec3(0.0f, 0.0f, 0.0f)); + auto model = AddComponent(ground_middle); + model->ModelFile = "Models/TerrainFiveIstles/Middle.obj"; + auto blendmap = AddComponent(ground_middle); + blendmap->TextureRed = "Textures/Ground/SoilBeach0087_11_S.jpg"; + blendmap->TextureRedNormal = "Textures/Ground/SoilBeach0087_11_SNM.png"; + blendmap->TextureGreen = "Textures/Ground/Grass0126_2_S.jpg"; + blendmap->TextureGreenNormal = "Textures/Ground/Grass0126_2_SNM.png"; + blendmap->TextureBlue = "Textures/Ground/Cliffs2.png"; + blendmap->TextureBlueNormal = "Textures/Ground/Cliffs2NM.png"; + blendmap->TextureRepeats = 30.f; + + auto physics = AddComponent(ground_middle); + physics->Mass = 10; + physics->Static = true; + physics->CollisionLayer = 1; + + auto groundshape = CreateEntity(ground_middle); + auto transformshape = AddComponent(groundshape); + auto meshShape = AddComponent(groundshape); + meshShape->ResourceName = "Models/TerrainFiveIstles/Middle.obj"; + + + CommitEntity(groundshape); + CommitEntity(ground_middle); + } + + { + auto ground_small = CreateEntity(); + auto transform = AddComponent(ground_small); + transform->Position = glm::vec3(0, -50, 0); + transform->Orientation = glm::quat(glm::vec3(0.0f, 0.0f, 0.0f)); + auto model = AddComponent(ground_small); + model->ModelFile = "Models/TerrainFiveIstles/Small.obj"; + auto blendmap = AddComponent(ground_small); + blendmap->TextureRed = "Textures/Ground/SoilBeach0087_11_S.jpg"; + blendmap->TextureRedNormal = "Textures/Ground/SoilBeach0087_11_SNM.png"; + blendmap->TextureGreen = "Textures/Ground/Grass0126_2_S.jpg"; + blendmap->TextureGreenNormal = "Textures/Ground/Grass0126_2_SNM.png"; + blendmap->TextureBlue = "Textures/Ground/Cliffs2.png"; + blendmap->TextureBlueNormal = "Textures/Ground/Cliffs2NM.png"; + blendmap->TextureRepeats = 30.f; + + auto physics = AddComponent(ground_small); + physics->Mass = 10; + physics->Static = true; + physics->CollisionLayer = 1; + + auto groundshape = CreateEntity(ground_small); + auto transformshape = AddComponent(groundshape); + auto meshShape = AddComponent(groundshape); + meshShape->ResourceName = "Models/TerrainFiveIstles/Small.obj"; + + + CommitEntity(groundshape); + CommitEntity(ground_small); + } + + { + auto ground_small_mirrored = CreateEntity(); + auto transform = AddComponent(ground_small_mirrored); + transform->Position = glm::vec3(0, -50, 0); + transform->Orientation = glm::angleAxis(glm::radians(180.f), glm::vec3(0, 1, 0)); + auto model = AddComponent(ground_small_mirrored); + model->ModelFile = "Models/TerrainFiveIstles/Small.obj"; + auto blendmap = AddComponent(ground_small_mirrored); + blendmap->TextureRed = "Textures/Ground/SoilBeach0087_11_S.jpg"; + blendmap->TextureRedNormal = "Textures/Ground/SoilBeach0087_11_SNM.png"; + blendmap->TextureGreen = "Textures/Ground/Grass0126_2_S.jpg"; + blendmap->TextureGreenNormal = "Textures/Ground/Grass0126_2_SNM.png"; + blendmap->TextureBlue = "Textures/Ground/Cliffs2.png"; + blendmap->TextureBlueNormal = "Textures/Ground/Cliffs2NM.png"; + blendmap->TextureRepeats = 30.f; + + auto physics = AddComponent(ground_small_mirrored); + physics->Mass = 10; + physics->Static = true; + physics->CollisionLayer = 1; + + auto groundshape = CreateEntity(ground_small_mirrored); + auto transformshape = AddComponent(groundshape); + auto meshShape = AddComponent(groundshape); + meshShape->ResourceName = "Models/TerrainFiveIstles/Small.obj"; + + + CommitEntity(groundshape); + CommitEntity(ground_small_mirrored); + } + + { + auto ground_base = CreateEntity(); + auto transform = AddComponent(ground_base); + transform->Position = glm::vec3(0, -50, 0); + transform->Orientation = glm::quat(glm::vec3(0.0f, 0.0f, 0.0f)); + auto model = AddComponent(ground_base); + model->ModelFile = "Models/TerrainFiveIstles/Base.obj"; + auto blendmap = AddComponent(ground_base); + blendmap->TextureRed = "Textures/Ground/SoilBeach0087_11_S.jpg"; + blendmap->TextureRedNormal = "Textures/Ground/SoilBeach0087_11_SNM.png"; + blendmap->TextureGreen = "Textures/Ground/Grass0126_2_S.jpg"; + blendmap->TextureGreenNormal = "Textures/Ground/Grass0126_2_SNM.png"; + blendmap->TextureBlue = "Textures/Ground/Cliffs2.png"; + blendmap->TextureBlueNormal = "Textures/Ground/Cliffs2NM.png"; + blendmap->TextureRepeats = 30.f; + + auto physics = AddComponent(ground_base); + physics->Mass = 10; + physics->Static = true; + physics->CollisionLayer = 1; + + auto groundshape = CreateEntity(ground_base); + auto transformshape = AddComponent(groundshape); + auto meshShape = AddComponent(groundshape); + meshShape->ResourceName = "Models/TerrainFiveIstles/Base.obj"; + + + CommitEntity(groundshape); + CommitEntity(ground_base); + } + + { + auto ground_base_mirrored = CreateEntity(); + auto transform = AddComponent(ground_base_mirrored); + transform->Position = glm::vec3(0, -50, 0); + transform->Orientation = glm::angleAxis(glm::radians(180.f), glm::vec3(0, 1, 0)); + auto model = AddComponent(ground_base_mirrored); + model->ModelFile = "Models/TerrainFiveIstles/Base.obj"; + auto blendmap = AddComponent(ground_base_mirrored); + blendmap->TextureRed = "Textures/Ground/SoilBeach0087_11_S.jpg"; + blendmap->TextureRedNormal = "Textures/Ground/SoilBeach0087_11_SNM.png"; + blendmap->TextureGreen = "Textures/Ground/Grass0126_2_S.jpg"; + blendmap->TextureGreenNormal = "Textures/Ground/Grass0126_2_SNM.png"; + blendmap->TextureBlue = "Textures/Ground/Cliffs2.png"; + blendmap->TextureBlueNormal = "Textures/Ground/Cliffs2NM.png"; + blendmap->TextureRepeats = 30.f; + + auto physics = AddComponent(ground_base_mirrored); + physics->Mass = 10; + physics->Static = true; + physics->CollisionLayer = 1; + + auto groundshape = CreateEntity(ground_base_mirrored); + auto transformshape = AddComponent(groundshape); + auto meshShape = AddComponent(groundshape); + meshShape->ResourceName = "Models/TerrainFiveIstles/Base.obj"; + + + CommitEntity(groundshape); + CommitEntity(ground_base_mirrored); + } + + { + /* auto tree = CreateEntity(); + auto transform = AddComponent(tree); + transform->Position = glm::vec3(0, -10, 0); + auto model = AddComponent(tree); + model->ModelFile = "Models/Tree/Stem/Stem.obj"; + auto physics = AddComponent(tree); + physics->Mass = 100.f; + physics->Static = false; + physics->CalculateCenterOfMass = true; + { + auto leafs = CreateEntity(tree); + auto transform = AddComponent(leafs); + auto model = AddComponent(leafs); + model->ModelFile = "Models/Tree/Leafs/Leafs.obj"; + model->Transparent = true; + CommitEntity(leafs); + } + + { + auto shape = CreateEntity(tree); + auto transform = AddComponent(shape); + auto box = AddComponent(shape); + transform->Position = glm::vec3(0.f, 0.f, 2.29552f); + box->Width = 0.296f; + box->Height = 2.511f; + box->Depth = 0.296f; + } + + CommitEntity(tree);*/ + } EntityID tank1 = CreateTank(1); { auto transform = GetComponent(tank1); - transform->Position.z = -125.f; - transform->Position.y = -20.f; - transform->Orientation = glm::angleAxis(glm::pi(), glm::vec3(0, 1, 0)); + transform->Position.z = 50; + transform->Position.z = 50.f; + Events::SetViewportCamera e; e.CameraEntity = GetProperty(tank1, "Camera"); e.ViewportFrame = "Viewport1"; @@ -154,166 +410,6 @@ void GameWorld::Initialize() CommitEntity(flag); } - CreateGate(glm::vec3(0, -50, 150)); - glm::vec3 Position = glm::vec3(-3.93076, -49, 0); - { - auto garage = CreateEntity(); - auto transform = AddComponent(garage); - transform->Position = Position; - - auto ElevatorBase = CreateEntity(garage); - { - auto transform = AddComponent(ElevatorBase); - auto model = AddComponent(ElevatorBase); - model->ModelFile = "Models/SpawnRamp/Main/Main.obj"; - auto physics = AddComponent(ElevatorBase); - physics->Static = true; - physics->CollisionLayer = (int)Components::Physics::CollisionLayer::STATIC; - - { - auto mainShape = CreateEntity(ElevatorBase); - auto transform = AddComponent(mainShape); - transform->Position = glm::vec3(0.f, 0, 0.f); - auto mesh = AddComponent(mainShape); - mesh->ResourceName = "Models/SpawnRamp/Main/CollisionShape.obj"; - CommitEntity(mainShape); - } - { - auto StairsShape = CreateEntity(ElevatorBase); - auto transform = AddComponent(StairsShape); - transform->Position = glm::vec3(-7.48639f, 0.4809f, 3.79166f); - auto box = AddComponent(StairsShape); - box->Width = 1.211f; - box->Height = 1.798f; - box->Depth = 3.229f; - CommitEntity(StairsShape); - } - } - CommitEntity(ElevatorBase); - - auto leftHatch = CreateEntity(garage); - { - auto transform = AddComponent(leftHatch); - transform->Position = glm::vec3(5.83103f, 1.3553f, -2.40268f); - auto model = AddComponent(leftHatch); - model->ModelFile = "Models/SpawnRamp/LeftHatch/LeftHatch.obj"; - auto physics = AddComponent(leftHatch); - physics->Static = true; - physics->CollisionLayer = (int)Components::Physics::CollisionLayer::STATIC; - auto rotate = AddComponent(leftHatch); - rotate->StartRotation = transform->Orientation; - rotate->GoalRotation = transform->Orientation * glm::angleAxis(-glm::radians(110.f),glm::vec3(0, 0, 1)); - rotate->Time = 5.f; - { - auto shape = CreateEntity(leftHatch); - auto transform = AddComponent(shape); - transform->Position = glm::vec3(-2.79331, 0, 0); - auto box = AddComponent(shape); - box->Width = 2.317f; - box->Height = 0.183f; - box->Depth = 6.987f; - CommitEntity(shape); - } - CommitEntity(leftHatch); - } - - auto rightHatch = CreateEntity(garage); - { - auto transform = AddComponent(rightHatch); - transform->Position = glm::vec3(-4.52423f, 1.3553f, -2.40268f); - auto model = AddComponent(rightHatch); - model->ModelFile = "Models/SpawnRamp/RightHatch/RightHatch.obj"; - auto physics = AddComponent(rightHatch); - physics->Static = true; - physics->CollisionLayer = (int)Components::Physics::CollisionLayer::STATIC; - auto rotate = AddComponent(rightHatch); - rotate->StartRotation = transform->Orientation; - rotate->GoalRotation = transform->Orientation * glm::angleAxis(glm::radians(110.f),glm::vec3(0, 0, 1)); - rotate->Time = 5.f; - { - auto shape = CreateEntity(rightHatch); - auto transform = AddComponent(shape); - transform->Position = glm::vec3(2.79331, 0, 0); - auto box = AddComponent(shape); - box->Width = 2.317f; - box->Height = 0.183f; - box->Depth = 6.987f; - CommitEntity(shape); - } - CommitEntity(rightHatch); - } - - auto elevator = CreateEntity(garage); - { - auto transform = AddComponent(elevator); - transform->Position = glm::vec3(0.69385f, 1.25222f, -2.39938f) + glm::vec3(0, -9.5f, 0); - auto model = AddComponent(elevator); - model->ModelFile = "Models/SpawnRamp/Elevator/Elevator.obj"; - auto physics = AddComponent(elevator); - physics->Static = true; - physics->CollisionLayer = (int)Components::Physics::CollisionLayer::STATIC; - auto move = AddComponent(elevator); - move->StartPosition = transform->Position; - move->GoalPosition = transform->Position + glm::vec3(0, 9.5f, 0); - move->Speed = 1.5f; - { - auto shape = CreateEntity(elevator); - auto transform = AddComponent(shape); - transform->Position = glm::vec3(0, 0, 0); - auto box = AddComponent(shape); - box->Width = 4.754f; - box->Height = 0.141f; - box->Depth = 6.86f; - CommitEntity(shape); - } - } - CommitEntity(elevator); - - auto garageComponent = AddComponent(garage); - garageComponent->DoorLeft = leftHatch; - garageComponent->DoorRight = rightHatch; - garageComponent->Elevator = elevator; - CommitEntity(garage); - - { - auto trigger = CreateEntity(garage); - SetProperty(trigger, "Name", "BoundsTrigger"); - auto transform = AddComponent(trigger); - transform->Position = glm::vec3(0, 44.87521f, 0); - auto triggerComponent = AddComponent(trigger); - { - auto shape = CreateEntity(trigger); - auto transform = AddComponent(shape); - transform->Position = glm::vec3(0, 0, 0); - auto box = AddComponent(shape); - box->Width = 72.98f; - box->Height = 54.53f; - box->Depth = 72.98f; - CommitEntity(shape); - } - CommitEntity(trigger); - } - - { - auto trigger = CreateEntity(garage); - SetProperty(trigger, "Name", "ElevatorShaftTrigger"); - auto transform = AddComponent(trigger); - transform->Position = glm::vec3(0.69385f, -6.7997f, 2.39938f); - auto triggerComponent = AddComponent(trigger); - { - auto shape = CreateEntity(trigger); - auto transform = AddComponent(shape); - transform->Position = glm::vec3(0, 0, 0); - auto box = AddComponent(shape); - box->Width = 1.0f; - box->Height = 10.f; - box->Depth = 1.0f; - CommitEntity(shape); - } - CommitEntity(trigger); - } - } - //for (int i = 0; i < 500; i++) //{ // auto Light = CreateEntity(); @@ -328,6 +424,71 @@ void GameWorld::Initialize() // //auto model = AddComponent(Light, "Model"); // //model->ModelFile = "Models/Placeholders/PhysicsTest/PointLight.obj"; //} + + + { + auto gateBase = CreateEntity(); + auto transform = AddComponent(gateBase); + transform->Position = glm::vec3(0, -50, 150); + auto model = AddComponent(gateBase); + model->ModelFile = "Models/Gate/Lift/Lift.obj"; + + auto physics = AddComponent(gateBase); + physics->Static = true; + physics->CollisionLayer = 1; // Terrain-Layer + { + auto leftBaseShape = CreateEntity(gateBase); + auto transform = AddComponent(leftBaseShape); + transform->Position = glm::vec3(8.5f, 3, 0); + auto box = AddComponent(leftBaseShape); + box->Width = 3.f/2; + box->Height = 6.094f/2; + box->Depth = 4.f/2; + CommitEntity(leftBaseShape); + } + { + auto RightBaseShape = CreateEntity(gateBase); + auto transform = AddComponent(RightBaseShape); + transform->Position = glm::vec3(-8.5f, 3, 0); + auto box = AddComponent(RightBaseShape); + box->Width = 3.f/2; + box->Height = 6.094f/2; + box->Depth = 4.f/2; + CommitEntity(RightBaseShape); + } + { + auto LeftTopShape = CreateEntity(gateBase); + auto transform = AddComponent(LeftTopShape); + transform->Position = glm::vec3(7.5485f, 9.385f, 0); + auto box = AddComponent(LeftTopShape); + box->Width = 1.747f/2; + box->Height = 6.851f/2; + box->Depth = 1.767f/2; + CommitEntity(LeftTopShape); + } + { + auto RightTopShape = CreateEntity(gateBase); + auto transform = AddComponent(RightTopShape); + transform->Position = glm::vec3(-7.5485f, 9.385f, 0); + auto box = AddComponent(RightTopShape); + box->Width = 1.747f/2; + box->Height = 6.851f/2; + box->Depth = 1.767f/2; + CommitEntity(RightTopShape); + } + { + auto RampShape = CreateEntity(gateBase); + auto transform = AddComponent(RampShape); + transform->Position = glm::vec3(0, 0.24f, 0); + auto box = AddComponent(RampShape); + box->Width = 14.9f/2; + box->Height = 0.38f/2; + box->Depth = 1.484f/2; + CommitEntity(RampShape); + } + CommitEntity(gateBase); + } + } void GameWorld::Update(double dt) @@ -425,129 +586,6 @@ void GameWorld::BindGamepadButton(Gamepad::Button button, std::string command, f EventBroker->Publish(e); } -void GameWorld::CreateGate(glm::vec3 Position) -{ - { - auto gateBase = CreateEntity(); - auto transform = AddComponent(gateBase); - transform->Position = Position; - auto model = AddComponent(gateBase); - model->ModelFile = "Models/Gate/Lift/Lift.obj"; - auto physics = AddComponent(gateBase); - physics->Static = true; - physics->CollisionLayer = (int)Components::Physics::CollisionLayer::STATIC; - - { - auto leftBaseShape = CreateEntity(gateBase); - auto transform = AddComponent(leftBaseShape); - transform->Position = glm::vec3(8.5f, 3, 0); - auto box = AddComponent(leftBaseShape); - box->Width = 3.f/2; - box->Height = 6.094f/2; - box->Depth = 4.f/2; - CommitEntity(leftBaseShape); - } - { - auto RightBaseShape = CreateEntity(gateBase); - auto transform = AddComponent(RightBaseShape); - transform->Position = glm::vec3(-8.5f, 3, 0); - auto box = AddComponent(RightBaseShape); - box->Width = 3.f/2; - box->Height = 6.094f/2; - box->Depth = 4.f/2; - CommitEntity(RightBaseShape); - } - { - auto LeftTopShape = CreateEntity(gateBase); - auto transform = AddComponent(LeftTopShape); - transform->Position = glm::vec3(7.5485f, 9.385f, 0); - auto box = AddComponent(LeftTopShape); - box->Width = 1.747f/2; - box->Height = 6.851f/2; - box->Depth = 1.767f/2; - CommitEntity(LeftTopShape); - } - { - auto RightTopShape = CreateEntity(gateBase); - auto transform = AddComponent(RightTopShape); - transform->Position = glm::vec3(-7.5485f, 9.385f, 0); - auto box = AddComponent(RightTopShape); - box->Width = 1.747f/2; - box->Height = 6.851f/2; - box->Depth = 1.767f/2; - CommitEntity(RightTopShape); - } - { - auto RampShape = CreateEntity(gateBase); - auto transform = AddComponent(RampShape); - transform->Position = glm::vec3(0, 0.24f, 0); - auto mesh = AddComponent(RampShape); - mesh->ResourceName = "Models/Gate/Lift/RampCollision.obj"; - CommitEntity(RampShape); - } - CommitEntity(gateBase); - } - - { - auto gate = CreateEntity(); - auto transform = AddComponent(gate); - transform->Position = Position; - auto model = AddComponent(gate); - model->ModelFile = "Models/Gate/Gate/Gate.obj"; - auto physics = AddComponent(gate); - physics->Static = true; - physics->CollisionLayer = (int)Components::Physics::CollisionLayer::STATIC; - auto move = AddComponent(gate); - move->Speed = 5.f; - move->GoalPosition = glm::vec3(Position.x, Position.y + 9.02345f, Position.z); - move->StartPosition = glm::vec3(Position.x, Position.y, Position.z); - { - auto mainShape = CreateEntity(gate); - auto transform = AddComponent(mainShape); - transform->Position = glm::vec3(0.f, 3.09776f, 0.f); - auto box = AddComponent(mainShape); - box->Width = 6.963f; - box->Height = 2.478f; - box->Depth = 0.522f; - CommitEntity(mainShape); - } - { - auto lowerShape = CreateEntity(gate); - auto transform = AddComponent(lowerShape); - transform->Position = glm::vec3(0.f, 0.35f, 0.f); - auto box = AddComponent(lowerShape); - box->Width = 6.963f; - box->Height = 0.308f; - box->Depth = 0.222f; - CommitEntity(lowerShape); - } - CommitEntity(gate); - - { - auto gateTrigger = CreateEntity(); - auto transform = AddComponent(gateTrigger); - transform->Position = glm::vec3(Position.x, Position.y + 2.8241, Position.z); - auto trigger = AddComponent(gateTrigger); - auto triggerMove = AddComponent(gateTrigger); - triggerMove->Entity = gate; - - - { - auto shape = CreateEntity(gateTrigger); - auto transform = AddComponent(shape); - transform->Position = glm::vec3(0.f, 2.8241f, 0.f); - auto box = AddComponent(shape); - box->Width = 6.963f; - box->Height = 3.104f; - box->Depth = 10.356f; - CommitEntity(shape); - } - CommitEntity(gateTrigger); - } - - } -} - EntityID GameWorld::CreateTank(int playerID) { auto tank = CreateEntity(); @@ -558,7 +596,7 @@ EntityID GameWorld::CreateTank(int playerID) physics->Mass = 63000 - 16000; physics->Static = false; auto vehicle = AddComponent(tank); - vehicle->MaxTorque = 8000.f; + vehicle->MaxTorque = 36000.f; vehicle->MaxSteeringAngle = 90.f; vehicle->MaxSpeedFullSteeringAngle = 4.f; auto player = AddComponent(tank); @@ -572,7 +610,7 @@ EntityID GameWorld::CreateTank(int playerID) auto shape = CreateEntity(tank); auto transform = AddComponent(shape); auto meshShape = AddComponent(shape); - meshShape->ResourceName = "Models/Tank/TankCollisionShape.obj"; + meshShape->ResourceName = "Models/Tank/Fix/ChassiCollision.obj"; CommitEntity(shape); } { @@ -606,7 +644,7 @@ EntityID GameWorld::CreateTank(int playerID) { auto barrel = CreateEntity(tower); auto transform = AddComponent(barrel); - transform->Position = glm::vec3(-0.012f, 0.3f, -0.95); + transform->Position = glm::vec3(-0.012f, 0.4f, -0.75); auto model = AddComponent(barrel); model->ModelFile = "Models/Tank/tankBarrel.obj"; auto barrelSteering = AddComponent(barrel); @@ -701,31 +739,32 @@ EntityID GameWorld::CreateTank(int playerID) AddTankWheelPair(tank, glm::vec3(-1.68f, wheelOffset, 2.375), 1, false); #pragma endregion - { - auto entity = CreateEntity(tank); - 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); - emitterComponent->SpawnCount = 2; - emitterComponent->SpawnFrequency = 0.005; - emitterComponent->SpreadAngle = glm::pi(); - emitterComponent->UseGoalVelocity = false; - emitterComponent->LifeTime = 0.5; - //emitterComponent->AngularVelocitySpectrum.push_back(glm::pi() / 100); - emitterComponent->ScaleSpectrum.push_back(glm::vec3(0.05)); - CommitEntity(entity); - - auto particleEntity = CreateEntity(entity); - auto TEMP = AddComponent(particleEntity); - TEMP->Scale = glm::vec3(0); - auto spriteComponent = AddComponent(particleEntity); - spriteComponent->SpriteFile = "Models/Textures/Sprites/Dust.png"; - emitterComponent->ParticleTemplate = particleEntity; - - CommitEntity(particleEntity); - } + +// auto entity = CreateEntity(tank); +// 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); +// emitterComponent->SpawnCount = 2; +// emitterComponent->SpawnFrequency = 0.005; +// emitterComponent->SpreadAngle = glm::pi(); +// emitterComponent->UseGoalVelocity = false; +// emitterComponent->LifeTime = 0.5; +// //emitterComponent->AngularVelocitySpectrum.push_back(glm::pi() / 100); +// emitterComponent->ScaleSpectrum.push_back(glm::vec3(0.05)); +// CommitEntity(entity); +// +// { +// auto particleEntity = CreateEntity(entity); +// auto templateComponent = AddComponent(particleEntity); +// auto TEMP = AddComponent(particleEntity); +// TEMP->Scale = glm::vec3(0); +// auto spriteComponent = AddComponent(particleEntity); +// spriteComponent->SpriteFile = "Models/Textures/Sprites/Dust.png"; +// CommitEntity(particleEntity); +// emitterComponent->ParticleTemplate = particleEntity; +// } CommitEntity(tank); @@ -756,10 +795,20 @@ void GameWorld::AddTankWheelPair(EntityID tankEntity, glm::vec3 position, int ax Wheel->Radius = 0.6f; Wheel->Steering = steering; Wheel->SuspensionStrength = suspensionStrength; - Wheel->Friction = 4.f; + Wheel->Friction = 3.f; Wheel->ConnectedToHandbrake = true; Wheel->TorqueRatio = 0.125f; Wheel->Width = 0.6f; + { + auto shape = CreateEntity(wheelFront); + auto shapetransform = AddComponent(shape); + shapetransform->Position = glm::vec3(0.f, 0.32f, 0.f) + transform->Position; + auto boxShape = AddComponent(shape); + boxShape->Width = 0.7f; + boxShape->Height = 0.34f; + boxShape->Depth = 0.7f; + CommitEntity(shape); + } CommitEntity(wheelFront); } @@ -779,10 +828,20 @@ void GameWorld::AddTankWheelPair(EntityID tankEntity, glm::vec3 position, int ax Wheel->Radius = 0.6f; Wheel->Steering = steering; Wheel->SuspensionStrength = suspensionStrength; - Wheel->Friction = 4.f; + Wheel->Friction = 3.f; Wheel->ConnectedToHandbrake = true; Wheel->TorqueRatio = 0.125f; Wheel->Width = 0.6f; + { + auto shape = CreateEntity(wheelBack); + auto shapetransform = AddComponent(shape); + shapetransform->Position = glm::vec3(0.f, 0.32f, 0.f) + transform->Position; + auto boxShape = AddComponent(shape); + boxShape->Width = 0.7f; + boxShape->Height = 0.34f; + boxShape->Depth = 0.7f; + CommitEntity(shape); + } CommitEntity(wheelBack); } @@ -856,6 +915,16 @@ EntityID GameWorld::CreateJeep(int playerID) model->ModelFile = "Models/Jeep/Chassi/chassi.obj"; } + { + auto lightentity = CreateEntity(jeep); + auto transform = AddComponent(lightentity); + transform->Position = glm::vec3(0, 15, 0); + 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); + } + + //Create wheels float wheelOffset = 0.4f; float springLength = 0.3f; diff --git a/src/InputManager.cpp b/src/InputManager.cpp index b20e072..01d61ba 100644 --- a/src/InputManager.cpp +++ b/src/InputManager.cpp @@ -85,6 +85,7 @@ void InputManager::Update(double dt) EventBroker->Publish(e); } + // // Lock mouse while holding LMB // if (m_CurrentMouseState[GLFW_MOUSE_BUTTON_LEFT]) // { diff --git a/src/Model.cpp b/src/Model.cpp index 1186671..dd5757c 100755 --- a/src/Model.cpp +++ b/src/Model.cpp @@ -93,7 +93,7 @@ Model::Model(std::shared_ptr rm, OBJ &obj) if (Vertices.size() > 0) { CreateTangents(); - //getSimilarVertexIndex(); + getSimilarVertexIndex(); CreateBuffers(Vertices, Normals, TangentNormals, BiTangentNormals, TextureCoords); } else diff --git a/src/OBJ.cpp b/src/OBJ.cpp index d8f91bf..71ec109 100755 --- a/src/OBJ.cpp +++ b/src/OBJ.cpp @@ -268,7 +268,7 @@ void OBJ::ParseMaterial() continue; } // Normal map (bump map) - if (prefix == "bump") + if (prefix == "bump" || prefix == "map_Bump" ) { MaterialInfo::BumpMap bumpMap; diff --git a/src/RenderQueue.h b/src/RenderQueue.h index a99535c..2636f84 100644 --- a/src/RenderQueue.h +++ b/src/RenderQueue.h @@ -14,6 +14,9 @@ struct RenderJob { friend class RenderQueue; + glm::mat4 ModelMatrix; + float Depth; + protected: uint64_t Hash; @@ -37,7 +40,7 @@ struct ModelJob : RenderJob GLuint VAO; unsigned int StartIndex; unsigned int EndIndex; - glm::mat4 ModelMatrix; + float Transparent; void CalculateHash() override { @@ -45,6 +48,20 @@ struct ModelJob : RenderJob } }; +struct BlendMapModelJob : ModelJob +{ + GLuint BlendMapTextureRed; + GLuint BlendMapTextureRedNormal; + GLuint BlendMapTextureRedSpecular; + GLuint BlendMapTextureGreen; + GLuint BlendMapTextureGreenNormal; + GLuint BlendMapTextureGreenSpecular; + GLuint BlendMapTextureBlue; + GLuint BlendMapTextureBlueNormal; + GLuint BlendMapTextureBlueSpecular; + float TextureRepeat; +}; + struct SpriteJob : RenderJob { unsigned int ShaderID; @@ -52,7 +69,6 @@ struct SpriteJob : RenderJob GLuint Texture; glm::vec4 Color; - glm::mat4 ModelMatrix; void CalculateHash() override { @@ -67,27 +83,48 @@ public: void Add(T &job) { job.CalculateHash(); - m_Jobs.push_front(std::shared_ptr(new T(job))); - m_Jobs.sort(); + Jobs.push_front(std::shared_ptr(new T(job))); + } + + void Sort() + { + Jobs.sort(); } void Clear() { - m_Jobs.clear(); + Jobs.clear(); } std::forward_list>::const_iterator begin() { - return m_Jobs.begin(); + return Jobs.begin(); } std::forward_list>::const_iterator end() { - return m_Jobs.end(); + return Jobs.end(); } -private: - std::forward_list> m_Jobs; + std::forward_list> Jobs; +}; + +struct RenderQueuePair +{ + RenderQueue Deferred; + RenderQueue Forward; + + void Clear() + { + Deferred.Clear(); + Forward.Clear(); + } + + void Sort() + { + Deferred.Sort(); + Forward.Sort(); + } }; #endif // RenderQueue_h__ diff --git a/src/Renderer.cpp b/src/Renderer.cpp index 0c1fd1d..ff01e67 100755 --- a/src/Renderer.cpp +++ b/src/Renderer.cpp @@ -21,7 +21,7 @@ Renderer::Renderer(std::shared_ptr<::ResourceManager> resourceManager) CAtt = 1.0f; LAtt = 0.0f; QAtt = 3.0f; - m_ShadowMapRes = 2048*2; + m_ShadowMapRes = 1; m_SunPosition = glm::vec3(0.f, 1.0f, 0.5f); m_SunTarget = glm::vec3(0, 0, 0); m_SunProjection_height = glm::vec2(-40.f, 40.f); @@ -78,10 +78,6 @@ void Renderer::Initialize() m_Camera->SetPosition(glm::vec3(0.0f, 0.0f, 2.f)); glfwSwapInterval(m_VSync); - glEnable(GL_CULL_FACE); - glCullFace(GL_BACK); - glEnable(GL_DEPTH_TEST); - glEnable(GL_SCISSOR_TEST); LoadContent(); } @@ -112,6 +108,16 @@ void Renderer::LoadContent() m_ShaderProgramDebugAABB.Compile(); m_ShaderProgramDebugAABB.Link();*/ + m_FinalForwardPassProgram.AddShader(std::shared_ptr(new VertexShader("Shaders/FinalForwardPass.vert.glsl"))); + m_FinalForwardPassProgram.AddShader(std::shared_ptr(new FragmentShader("Shaders/FinalForwardPass.frag.glsl"))); + m_FinalForwardPassProgram.Compile(); + m_FinalForwardPassProgram.Link(); + + m_BlendMapProgram.AddShader(std::shared_ptr(new VertexShader("Shaders/BlendMap.vert.glsl"))); + m_BlendMapProgram.AddShader(std::shared_ptr(new FragmentShader("Shaders/BlendMap.frag.glsl"))); + m_BlendMapProgram.Compile(); + m_BlendMapProgram.Link(); + 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(); @@ -125,6 +131,7 @@ void Renderer::LoadContent() m_ForwardRendering.AddShader(std::shared_ptr(new VertexShader("Shaders/ForwardRendering.vert.glsl"))); m_ForwardRendering.AddShader(std::shared_ptr(new FragmentShader("Shaders/ForwardRendering.frag.glsl"))); m_ForwardRendering.Compile(); + glBindFragDataLocation(m_ForwardRendering.GetHandle(), 0, "frag_Diffuse"); m_ForwardRendering.Link(); m_ShaderProgramShadows.AddShader(std::shared_ptr(new VertexShader("Shaders/ShadowMap.vert.glsl"))); @@ -160,7 +167,7 @@ void Renderer::LoadContent() FrameBufferTextures(); m_sphereModel = ResourceManager->Load("Model", "Models/Placeholders/PhysicsTest/Sphere.obj"); - m_Skybox = std::make_shared("Textures/Skybox/Sunset", "jpg"); + m_Skybox = std::make_shared("Textures/Skybox/sunset", "jpg"); } void Renderer::Draw(double dt) @@ -260,7 +267,7 @@ void Renderer::Draw(double dt) glfwSwapBuffers(m_Window); } -void Renderer::DrawFrame(RenderQueue &rq) +void Renderer::DrawFrame(RenderQueuePair &rq) { glBindFramebuffer(GL_FRAMEBUFFER, 0); glViewport(m_Viewport.X, m_Height - m_Viewport.Y - m_Viewport.Height, m_Viewport.Width, m_Viewport.Height); @@ -304,7 +311,7 @@ void Renderer::DrawFrame(RenderQueue &rq) // } //} - for (auto &job : rq) + for (auto &job : rq.Forward) { //auto modelJob = std::dynamic_pointer_cast(job); //if (modelJob) @@ -357,11 +364,27 @@ void Renderer::DrawFrame(RenderQueue &rq) } } -void Renderer::DrawWorld(RenderQueue &rq) +void Renderer::DrawWorld(RenderQueuePair &rq) { glDisable(GL_BLEND); + glEnable(GL_CULL_FACE); + glCullFace(GL_BACK); + glEnable(GL_DEPTH_TEST); + glDepthMask(GL_TRUE); + glEnable(GL_SCISSOR_TEST); - DrawShadowMap(rq); + //Sort forward rendering items by z value. + for(auto job : rq.Forward) + { + glm::mat4 cameraProjection = m_Camera->ProjectionMatrix((float)m_Viewport.Width / m_Viewport.Height); + glm::mat4 cameraMatrix = cameraProjection * m_Camera->ViewMatrix(); + + glm::vec3 spritePos = glm::vec3(cameraMatrix * job->ModelMatrix * glm::vec4(1, 1, 1, 0)); + job->Depth = spritePos.z; + } + rq.Forward.Jobs.sort(Renderer::DepthSort); + + //DrawShadowMap(rq.Deferred); /* Base pass @@ -384,12 +407,15 @@ void Renderer::DrawWorld(RenderQueue &rq) glCullFace(GL_BACK); glEnable(GL_DEPTH_TEST); - DrawFBOScene(rq); + DrawSkybox(); + DrawFBOScene(rq.Deferred); /* Lighting pass */ glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_fbLightingPass); + glViewport(m_Viewport.X, m_Height - m_Viewport.Y - m_Viewport.Height, m_Viewport.Width, m_Viewport.Height); + glScissor(m_Viewport.X, m_Height - m_Viewport.Y - m_Viewport.Height, m_Viewport.Width, m_Viewport.Height); GLenum lightingPassAttachments[] = { GL_COLOR_ATTACHMENT0 }; glDrawBuffers(1, lightingPassAttachments); @@ -405,7 +431,7 @@ void Renderer::DrawWorld(RenderQueue &rq) glBindTexture(GL_TEXTURE_2D, m_fSpecularTexture); glCullFace(GL_FRONT); - DrawLightScene(rq); + DrawLightScene(rq.Deferred); DrawSunLightScene(); /* @@ -414,7 +440,9 @@ void Renderer::DrawWorld(RenderQueue &rq) glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); //glViewport(m_Viewport.X, m_Viewport.Y, m_Viewport.Width, m_Viewport.Height); glViewport(0, 0, m_Width, m_Height); - glScissor(0, 0, m_Width, m_Height); + //glScissor(0, 0, m_Width, m_Height); + glScissor(m_Viewport.X, m_Height - m_Viewport.Y - m_Viewport.Height, m_Viewport.Width, m_Viewport.Height); + glClear(GL_DEPTH_BUFFER_BIT); m_FinalPassProgram.Bind(); @@ -432,6 +460,147 @@ void Renderer::DrawWorld(RenderQueue &rq) glBindVertexArray(m_ScreenQuad); glEnableVertexAttribArray(0); glDrawArrays(GL_TRIANGLES, 0, 6); + + /* + Transparency + */ + ForwardRendering(rq.Forward); +} + +void Renderer::ForwardRendering(RenderQueue &rq) +{ + glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_fbBasePass); + + glViewport(m_Viewport.X, m_Height - m_Viewport.Y - m_Viewport.Height, m_Viewport.Width, m_Viewport.Height); + glScissor(m_Viewport.X, m_Height - m_Viewport.Y - m_Viewport.Height, m_Viewport.Width, m_Viewport.Height); + + // Clear G-buffer + GLenum attachments[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_COLOR_ATTACHMENT3 }; + glDrawBuffers(4, attachments); + glClearColor(0.f, 0.f, 0.f, 0.f); + glClear(GL_COLOR_BUFFER_BIT); + + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + glEnable(GL_DEPTH_TEST); + glDepthMask(GL_FALSE); + + glEnable(GL_CULL_FACE); + glCullFace(GL_BACK); + + glm::mat4 cameraProjection = m_Camera->ProjectionMatrix((float)m_Viewport.Width / m_Viewport.Height); + glm::mat4 cameraMatrix = cameraProjection * m_Camera->ViewMatrix(); + glm::mat4 MVP; + + m_ForwardRendering.Bind(); + GLuint ShaderProgramHandle = m_ForwardRendering.GetHandle(); + for (auto &job : rq) + { + auto modelJob = std::dynamic_pointer_cast(job); + if (modelJob) + { + glm::mat4 modelMatrix = modelJob->ModelMatrix; + MVP = cameraMatrix * modelMatrix; + + glUniformMatrix4fv(glGetUniformLocation(ShaderProgramHandle, "MVP"), 1, GL_FALSE, glm::value_ptr(MVP)); + glUniformMatrix4fv(glGetUniformLocation(ShaderProgramHandle, "M"), 1, GL_FALSE, glm::value_ptr(modelMatrix)); + glUniformMatrix4fv(glGetUniformLocation(ShaderProgramHandle, "V"), 1, GL_FALSE, glm::value_ptr(m_Camera->ViewMatrix())); + glUniformMatrix4fv(glGetUniformLocation(ShaderProgramHandle, "P"), 1, GL_FALSE, glm::value_ptr(cameraProjection)); + glUniform4fv(glGetUniformLocation(ShaderProgramHandle, "Color"), 1, glm::value_ptr(modelJob->Color)); + + glBindVertexArray(modelJob->VAO); + + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, modelJob->DiffuseTexture); + if (modelJob->NormalTexture != 0) + { + glActiveTexture(GL_TEXTURE2); + glBindTexture(GL_TEXTURE_2D, modelJob->NormalTexture); + } + if (modelJob->SpecularTexture) + { + glActiveTexture(GL_TEXTURE3); + glBindTexture(GL_TEXTURE_2D, modelJob->SpecularTexture); + } + glDrawArrays(GL_TRIANGLES, modelJob->StartIndex, modelJob->EndIndex - modelJob->StartIndex + 1); + + continue; + } + + + + auto spriteJob = std::dynamic_pointer_cast(job); + if (spriteJob) + { + glm::mat4 modelMatrix = spriteJob->ModelMatrix; + MVP = cameraMatrix * modelMatrix; + + glUniformMatrix4fv(glGetUniformLocation(ShaderProgramHandle, "MVP"), 1, GL_FALSE, glm::value_ptr(glm::mat4(MVP))); + glUniformMatrix4fv(glGetUniformLocation(ShaderProgramHandle, "M"), 1, GL_FALSE, glm::value_ptr(glm::mat4(modelMatrix))); + glUniformMatrix4fv(glGetUniformLocation(ShaderProgramHandle, "V"), 1, GL_FALSE, glm::value_ptr(glm::mat4(m_Camera->ViewMatrix()))); + glUniformMatrix4fv(glGetUniformLocation(ShaderProgramHandle, "P"), 1, GL_FALSE, glm::value_ptr(glm::mat4(cameraProjection))); + glUniform4fv(glGetUniformLocation(ShaderProgramHandle, "Color"), 1, glm::value_ptr(spriteJob->Color)); + + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, spriteJob->Texture); + glBindVertexArray(m_ScreenQuad); + glDrawArrays(GL_TRIANGLES, 0, 6); + + continue; + } + } + //glDepthMask (GL_TRUE); + //glDisable (GL_BLEND); + + /* + Final pass + */ + glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); + glViewport(0, 0, m_Width, m_Height); + glScissor(0, 0, m_Width, m_Height); + + glDisable(GL_DEPTH_TEST); + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + + m_FinalForwardPassProgram.Bind(); + //ShaderProgramHandle = m_FinalForwardPassProgram.GetHandle(); + + // Ambient light + //glUniform3fv(glGetUniformLocation(ShaderProgramHandle, "La"), 1, glm::value_ptr(glm::vec3(0.7f))); + //glUniform1f(glGetUniformLocation(ShaderProgramHandle, "Gamma"), Gamma); + + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, m_fDiffuseTexture); + + glCullFace(GL_BACK); + glBindVertexArray(m_ScreenQuad); + glEnableVertexAttribArray(0); + glDrawArrays(GL_TRIANGLES, 0, 6); + + //for (auto tuple : ModelsToRender) //// Todo: Add so it's TransparentModelsToRender + //{ + // Model* model; + // glm::mat4 modelMatrix; + // bool visible; + // std::tie(model, modelMatrix, visible, std::ignore) = tuple; + // if (!visible) + // continue; + + // MVP = cameraMatrix * modelMatrix; + // glUniformMatrix4fv(glGetUniformLocation(ShaderProgramHandle, "MVP"), 1, GL_FALSE, glm::value_ptr(MVP)); + // glUniformMatrix4fv(glGetUniformLocation(ShaderProgramHandle, "M"), 1, GL_FALSE, glm::value_ptr(modelMatrix)); + // glUniformMatrix4fv(glGetUniformLocation(ShaderProgramHandle, "V"), 1, GL_FALSE, glm::value_ptr(m_Camera->ViewMatrix())); + // glUniformMatrix4fv(glGetUniformLocation(ShaderProgramHandle, "P"), 1, GL_FALSE, glm::value_ptr(m_Camera->ProjectionMatrix((float)m_Width / m_Height))); + + // 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); + // } + //} } void Renderer::Swap() @@ -439,17 +608,19 @@ void Renderer::Swap() glfwSwapBuffers(m_Window); } -#pragma region TempRegion - void Renderer::DrawSkybox() { //glBindFramebuffer(GL_FRAMEBUFFER, 0); - //glViewport(0, 0, m_Width, m_Height); - //glScissor(0, 0, m_Width, m_Height); + glViewport(m_Viewport.X, m_Height - m_Viewport.Y - m_Viewport.Height, m_Viewport.Width, m_Viewport.Height); + glScissor(m_Viewport.X, m_Height - m_Viewport.Y - m_Viewport.Height, m_Viewport.Width, m_Viewport.Height); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + //glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); m_ShaderProgramSkybox.Bind(); - glm::mat4 cameraMatrix = m_Camera->ProjectionMatrix((float)m_Width / m_Height) * glm::toMat4(glm::inverse(m_Camera->Orientation())); + + //glm::mat4 cameraProjection = m_Camera->ProjectionMatrix((float)m_Viewport.Width / m_Viewport.Height); + //glm::mat4 cameraMatrix = cameraProjection * m_Camera->ViewMatrix(); + + glm::mat4 cameraMatrix = m_Camera->ProjectionMatrix((float)m_Viewport.Width / m_Viewport.Height) * glm::inverse(glm::toMat4(m_Camera->Orientation())); glUniformMatrix4fv(glGetUniformLocation(m_ShaderProgramSkybox.GetHandle(), "MVP"), 1, GL_FALSE, glm::value_ptr(cameraMatrix)); glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); m_Skybox->Draw(); @@ -762,8 +933,6 @@ void Renderer::ClearStuff() Lights.clear(); } -#pragma endregion - void Renderer::FrameBufferTextures() { m_fbBasePass = 0; @@ -857,9 +1026,6 @@ void Renderer::FrameBufferTextures() LOG_ERROR("DeferredLighting:Init: m_fbLightingPass incomplete: 0x%x\n", fbStatus); //exit(1); } - - - } void Renderer::DrawFBO() @@ -945,9 +1111,8 @@ void Renderer::DrawFBO() //} } -void Renderer::DrawFBO2() +void Renderer::DrawFBO2(RenderQueue &rq) { - ForwardRendering(); } void Renderer::DrawFBOScene(RenderQueue &rq) @@ -976,17 +1141,74 @@ void Renderer::DrawFBOScene(RenderQueue &rq) glm::vec3 sunDirection = m_SunTarget - m_SunPosition; glm::vec3 sunDirection_cameraview = glm::vec3(cameraProjection * m_Camera->ViewMatrix() * glm::vec4(sunDirection, 1.0)); - m_FirstPassProgram.Bind(); - GLuint ShaderProgramHandle = m_FirstPassProgram.GetHandle(); - glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, m_ShadowDepthTexture); for (auto &job : rq) { + + auto blendMapJob = std::dynamic_pointer_cast(job); + if(blendMapJob) + { + m_BlendMapProgram.Bind(); + GLuint ShaderProgramHandle = m_BlendMapProgram.GetHandle(); + + glm::mat4 modelMatrix = blendMapJob->ModelMatrix; + + MVP = cameraMatrix * modelMatrix; + depthMVP = depthCameraMatrix * modelMatrix; + glUniformMatrix4fv(glGetUniformLocation(ShaderProgramHandle, "MVP"), 1, GL_FALSE, glm::value_ptr(MVP)); + glUniformMatrix4fv(glGetUniformLocation(ShaderProgramHandle, "DepthMVP"), 1, GL_FALSE, glm::value_ptr(depthMVP)); + glUniformMatrix4fv(glGetUniformLocation(ShaderProgramHandle, "M"), 1, GL_FALSE, glm::value_ptr(modelMatrix)); + glUniformMatrix4fv(glGetUniformLocation(ShaderProgramHandle, "V"), 1, GL_FALSE, glm::value_ptr(m_Camera->ViewMatrix())); + glUniformMatrix4fv(glGetUniformLocation(ShaderProgramHandle, "P"), 1, GL_FALSE, glm::value_ptr(cameraProjection)); + glUniform3fv(glGetUniformLocation(ShaderProgramHandle, "SunDirection_cameraspace"), 1, glm::value_ptr(sunDirection_cameraview)); + glUniform1f(glGetUniformLocation(ShaderProgramHandle, "TextureRepeats"), blendMapJob->TextureRepeat); + + glBindVertexArray(blendMapJob->VAO); + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, blendMapJob->DiffuseTexture); + if (blendMapJob->NormalTexture != 0) + { + glActiveTexture(GL_TEXTURE2); + glBindTexture(GL_TEXTURE_2D, blendMapJob->NormalTexture); + } + if (blendMapJob->SpecularTexture) + { + glActiveTexture(GL_TEXTURE3); + glBindTexture(GL_TEXTURE_2D, blendMapJob->SpecularTexture); + } + + glActiveTexture(GL_TEXTURE4); + glBindTexture(GL_TEXTURE_2D, blendMapJob->BlendMapTextureRed); + glActiveTexture(GL_TEXTURE5); + glBindTexture(GL_TEXTURE_2D, blendMapJob->BlendMapTextureRedNormal); + glActiveTexture(GL_TEXTURE6); + glBindTexture(GL_TEXTURE_2D, blendMapJob->BlendMapTextureRedSpecular); + glActiveTexture(GL_TEXTURE7); + glBindTexture(GL_TEXTURE_2D, blendMapJob->BlendMapTextureGreen); + glActiveTexture(GL_TEXTURE8); + glBindTexture(GL_TEXTURE_2D, blendMapJob->BlendMapTextureGreenNormal); + glActiveTexture(GL_TEXTURE9); + glBindTexture(GL_TEXTURE_2D, blendMapJob->BlendMapTextureGreenSpecular); + glActiveTexture(GL_TEXTURE10); + glBindTexture(GL_TEXTURE_2D, blendMapJob->BlendMapTextureBlue); + glActiveTexture(GL_TEXTURE11); + glBindTexture(GL_TEXTURE_2D, blendMapJob->BlendMapTextureBlueNormal); + glActiveTexture(GL_TEXTURE12); + glBindTexture(GL_TEXTURE_2D, blendMapJob->BlendMapTextureBlueSpecular); + + glDrawArrays(GL_TRIANGLES, blendMapJob->StartIndex, blendMapJob->EndIndex - blendMapJob->StartIndex + 1); + + continue; + } + auto modelJob = std::dynamic_pointer_cast(job); if (modelJob) { + m_FirstPassProgram.Bind(); + GLuint ShaderProgramHandle = m_FirstPassProgram.GetHandle(); + glm::mat4 modelMatrix = modelJob->ModelMatrix; MVP = cameraMatrix * modelMatrix; @@ -1094,7 +1316,7 @@ void Renderer::DrawSunLightScene() glCullFace(GL_BACK); glEnable(GL_BLEND); - glBlendEquation (GL_FUNC_ADD); + glBlendEquation(GL_FUNC_ADD); glBlendFunc(GL_ONE,GL_ONE); glDisable (GL_DEPTH_TEST); @@ -1173,49 +1395,6 @@ void Renderer::UpdateSunProjection() //Pass the bounding box's extents to glOrtho or similar to set up the orthographic projection matrix for the shadow map. } -void Renderer::ForwardRendering() -{ - glBindFramebuffer(GL_FRAMEBUFFER, 0); - glViewport(0, 0, m_Width, m_Height); - - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - glClearColor(0.0f, 0.5f, 0.0f, 1.0f); - - glEnable(GL_DEPTH_TEST); - glEnable(GL_CULL_FACE); - glCullFace(GL_BACK); - - glm::mat4 cameraMatrix = m_Camera->ProjectionMatrix((float)m_Width / m_Height) * m_Camera->ViewMatrix(); - glm::mat4 MVP; - - m_ForwardRendering.Bind(); - GLuint ShaderProgramHandle = m_ForwardRendering.GetHandle(); - - for (auto tuple : ModelsToRender) //// Todo: Add so it's TransparentModelsToRender - { - Model* model; - glm::mat4 modelMatrix; - bool visible; - std::tie(model, modelMatrix, visible, std::ignore) = tuple; - if (!visible) - continue; - - MVP = cameraMatrix * modelMatrix; - glUniformMatrix4fv(glGetUniformLocation(ShaderProgramHandle, "MVP"), 1, GL_FALSE, glm::value_ptr(MVP)); - glUniformMatrix4fv(glGetUniformLocation(ShaderProgramHandle, "M"), 1, GL_FALSE, glm::value_ptr(modelMatrix)); - glUniformMatrix4fv(glGetUniformLocation(ShaderProgramHandle, "V"), 1, GL_FALSE, glm::value_ptr(m_Camera->ViewMatrix())); - glUniformMatrix4fv(glGetUniformLocation(ShaderProgramHandle, "P"), 1, GL_FALSE, glm::value_ptr(m_Camera->ProjectionMatrix((float)m_Width / m_Height))); - - 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); - } - } -} - void Renderer::RegisterCamera(int identifier, float FOV, float nearClip, float farClip) { m_Cameras[identifier] = std::make_shared(FOV, nearClip, farClip); diff --git a/src/Renderer.h b/src/Renderer.h index 9116b67..7a1c214 100755 --- a/src/Renderer.h +++ b/src/Renderer.h @@ -69,8 +69,8 @@ public: m_Camera = camera; } - void DrawFrame(RenderQueue &rq); - void DrawWorld(RenderQueue &rq); + void DrawFrame(RenderQueuePair &rq); + void DrawWorld(RenderQueuePair &rq); void Swap(); #pragma endregion @@ -191,6 +191,8 @@ private: ShaderProgram m_FinalPassProgram; ShaderProgram m_SunPassProgram; ShaderProgram m_ForwardRendering; + ShaderProgram m_BlendMapProgram; + ShaderProgram m_FinalForwardPassProgram; ShaderProgram m_ShaderProgramNormals; ShaderProgram m_ShaderProgramShadows; @@ -207,7 +209,7 @@ private: void CreateShadowMap(int resolution); void FrameBufferTextures(); void DrawFBO(); - void DrawFBO2(); + void DrawFBO2(RenderQueue &rq); void DrawFBOScene(RenderQueue &rq); void DrawLightScene(RenderQueue &rq); void DrawSunLightScene(); @@ -215,9 +217,10 @@ private: glm::mat4 CreateLightMatrix(Light &_light); void UpdateSunProjection(); void CreateNormalMapTangent(); - void ForwardRendering(); + void ForwardRendering(RenderQueue &rq); + + static bool DepthSort(const std::shared_ptr &i, const std::shared_ptr &j) { return (i->Depth > j->Depth); } - GLuint CreateQuad(); void DrawDebugShadowMap(); GLuint CreateAABB(); diff --git a/src/Shaders/BlendMap.frag.glsl b/src/Shaders/BlendMap.frag.glsl new file mode 100644 index 0000000..2e128be --- /dev/null +++ b/src/Shaders/BlendMap.frag.glsl @@ -0,0 +1,108 @@ +#version 430 + +layout (binding=0) uniform sampler2D DiffuseTexture; +layout (binding=1) uniform sampler2D ShadowTexture; +layout (binding=2) uniform sampler2D NormalMapTexture; +layout (binding=3) uniform sampler2D SpecularMapTexture; + +//TerrainTextures +layout (binding=4) uniform sampler2D TextureRed; +layout (binding=5) uniform sampler2D TextureRedNormal; +layout (binding=6) uniform sampler2D TextureRedSpecular; +layout (binding=7) uniform sampler2D TextureGreen; +layout (binding=8) uniform sampler2D TextureGreenNormal; +layout (binding=9) uniform sampler2D TextureGreenSpecular; +layout (binding=10) uniform sampler2D TextureBlue; +layout (binding=11) uniform sampler2D TextureBlueNormal; +layout (binding=12) uniform sampler2D TextureBlueSpecular; + +uniform float TextureRepeats; //Determines how many times the textures will loop over the terrain +uniform vec3 SunDirection_cameraspace; +uniform mat4 V; + +in VertexData +{ + vec3 Position; + vec3 Normal; + vec2 TextureCoord; + vec4 ShadowCoord; + vec3 Tangent; + vec3 BiTangent; +} Input; + +out vec4 frag_Diffuse; +out vec4 frag_Position; +out vec4 frag_Normal; +out vec4 frag_Specular; + +float Shadow(vec4 ShadowCoord, vec3 normal) +{ + return 1.0; + + if (Input.ShadowCoord.x < 0.0 || Input.ShadowCoord.x > 1.0 || Input.ShadowCoord.y < 0.0 || Input.ShadowCoord.y > 1.0) + return 0.9; + + //Variable bias + vec3 n = normalize(normal); + vec3 l = normalize(SunDirection_cameraspace); + float cosTheta = clamp(dot(n, l), 0.0, 1.0); + float bias = tan(acos(cosTheta)); + bias = clamp(bias, 0.0, 0.00003); + + //Fixed bias + bias = 0; + + if( texture(ShadowTexture, Input.ShadowCoord.xy).z < ShadowCoord.z + bias) + { + return 0.6; + } + else + { + return 1.0; + } +} + +void main() +{ + vec4 BlendMap = texture(DiffuseTexture, Input.TextureCoord); + + vec4 TextureRedTexel = texture(TextureRed, Input.TextureCoord * TextureRepeats); + vec4 TextureRedTexelNormal = texture(TextureRedNormal, Input.TextureCoord * TextureRepeats); + //vec4 TextureRedTexelSpecular = texture(TextureRedSpecular, Input.TextureCoord * TextureRepeats); + + vec4 TextureGreenTexel = texture(TextureGreen, Input.TextureCoord * TextureRepeats); + vec4 TextureGreenTexelNormal = texture(TextureGreenNormal, Input.TextureCoord * TextureRepeats); + //vec4 TextureGreenTexelSpecular = texture(TextureGreenSpecular, Input.TextureCoord * TextureRepeats); + + vec4 TextureBlueTexel = texture(TextureBlue, Input.TextureCoord * TextureRepeats); + vec4 TextureBlueTexelNormal = texture(TextureBlueNormal, Input.TextureCoord * TextureRepeats); + //vec4 TextureBlueTexelSpecular = texture(TextureBlueSpecular, Input.TextureCoord * TextureRepeats); + + //Mix the Terrain-textures together + TextureRedTexel *= BlendMap.r; + TextureGreenTexel = mix(TextureRedTexel, TextureGreenTexel, BlendMap.g); + vec4 finalBlendTexel = mix(TextureGreenTexel, TextureBlueTexel, BlendMap.b); + + TextureRedTexelNormal *= BlendMap.r; + TextureGreenTexelNormal = mix(TextureRedTexelNormal, TextureGreenTexelNormal, BlendMap.g); + vec4 finalBlendTexelNormal = mix(TextureGreenTexelNormal, TextureBlueTexelNormal, BlendMap.b); + + //TextureRedTexelSpecular *= BlendMap.r; + //TextureGreenTexelSpecular = mix(TextureRedTexelSpecular, TextureGreenTexelSpecular, BlendMap.g); + //vec4 finalBlendTexelSpecular = mix(TextureGreenTexelSpecular, TextureBlueTexelSpecular, BlendMap.b); + + // G-buffer Position + frag_Position = vec4(Input.Position.xyz, 1.0); + + // G-buffer Normal + mat3 TBN = mat3(Input.Tangent, Input.BiTangent, Input.Normal); + frag_Normal = normalize(vec4(TBN * vec3(finalBlendTexelNormal), 0.0)); + //frag_Diffuse = normalize(vec4(TBN * vec3(finalBlendTexelNormal), 0.0)); + //frag_Normal = vec4(Input.Normal, 0.0); + + // Diffuse Texture + frag_Diffuse = finalBlendTexel; + + //G-buffer Specular + frag_Specular = texture(SpecularMapTexture, Input.TextureCoord); +} \ No newline at end of file diff --git a/src/Shaders/BlendMap.vert.glsl b/src/Shaders/BlendMap.vert.glsl new file mode 100644 index 0000000..4c63e0d --- /dev/null +++ b/src/Shaders/BlendMap.vert.glsl @@ -0,0 +1,35 @@ +#version 430 + +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; +layout (location = 2) in vec2 TextureCoord; +layout (location = 3) in vec3 Tangent; +layout (location = 4) in vec3 BiTangent; + +out VertexData +{ + vec3 Position; + vec3 Normal; + vec2 TextureCoord; + vec4 ShadowCoord; + vec3 Tangent; + vec3 BiTangent; +} Output; + +void main() +{ + gl_Position = MVP * vec4(Position, 1.0); + + 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); + Output.Tangent = normalize(vec3(inverse(transpose(V * M)) * vec4(Tangent, 0.0))); + Output.BiTangent = normalize(vec3(inverse(transpose(V * M)) * vec4(BiTangent, 0.0))); +} \ No newline at end of file diff --git a/src/Shaders/FinalForwardPass.frag.glsl b/src/Shaders/FinalForwardPass.frag.glsl new file mode 100644 index 0000000..022efde --- /dev/null +++ b/src/Shaders/FinalForwardPass.frag.glsl @@ -0,0 +1,25 @@ +#version 430 + +//uniform vec3 La; + +layout (binding=0) uniform sampler2D DiffuseTexture; + +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 = LightingTexel + vec4(LightingTexel.a, LightingTexel.a, LightingTexel.a, 0.0); + //FragmentColor = DiffuseTexel; + + FragmentColor = DiffuseTexel; + //FragmentColor = vec4(pow(_FragmentColor.rgb, vec3(1.0 / Gamma)), _FragmentColor.a); +} \ No newline at end of file diff --git a/src/Shaders/FinalForwardPass.vert.glsl b/src/Shaders/FinalForwardPass.vert.glsl new file mode 100644 index 0000000..05deece --- /dev/null +++ b/src/Shaders/FinalForwardPass.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/FinalPass.frag.glsl b/src/Shaders/FinalPass.frag.glsl index 26073a2..5a4faf0 100644 --- a/src/Shaders/FinalPass.frag.glsl +++ b/src/Shaders/FinalPass.frag.glsl @@ -5,7 +5,6 @@ uniform float Gamma; layout (binding=0) uniform sampler2D DiffuseTexture; layout (binding=1) uniform sampler2D LightingTexture; -layout (binding=2) uniform sampler2D ShadowTexture; in VertexData { @@ -19,12 +18,10 @@ void main() { vec4 DiffuseTexel = texture(DiffuseTexture, Input.TextureCoord); vec4 LightingTexel = texture(LightingTexture, Input.TextureCoord); - vec4 ShadowTexel = texture(ShadowTexture, Input.TextureCoord); //FragmentColor = LightingTexel + vec4(LightingTexel.a, LightingTexel.a, LightingTexel.a, 0.0); //FragmentColor = DiffuseTexel; FragmentColor = DiffuseTexel * (vec4(La, 0.0) + vec4(LightingTexel.rgb, 0.0)) + vec4(LightingTexel.a, LightingTexel.a, LightingTexel.a, 0.0); //FragmentColor = vec4(pow(_FragmentColor.rgb, vec3(1.0 / Gamma)), _FragmentColor.a); - } \ No newline at end of file diff --git a/src/Shaders/ForwardRendering.frag.glsl b/src/Shaders/ForwardRendering.frag.glsl index b76f3aa..0d3a7c8 100644 --- a/src/Shaders/ForwardRendering.frag.glsl +++ b/src/Shaders/ForwardRendering.frag.glsl @@ -10,11 +10,11 @@ in VertexData { vec2 TextureCoord; } Input; -out vec4 fragmentColor; +out vec4 frag_Diffuse; void main() { // Texture vec4 texel = texture(texture0, Input.TextureCoord); - fragmentColor = texel * Color; + frag_Diffuse = texel * Color; } \ No newline at end of file diff --git a/src/Shaders/Fragment.glsl b/src/Shaders/Fragment.glsl index 51a2cf5..439ef3a 100755 --- a/src/Shaders/Fragment.glsl +++ b/src/Shaders/Fragment.glsl @@ -5,13 +5,6 @@ layout (binding=1) uniform sampler2D ShadowTexture; layout (binding=2) uniform sampler2D NormalMapTexture; layout (binding=3) uniform sampler2D SpecularMapTexture; -//TerrainTextures -layout (binding=4) uniform sampler2D AsphaltTexture; -layout (binding=5) uniform sampler2D GrassTexture; -layout (binding=6) uniform sampler2D SandTexture; -layout (binding=7) uniform sampler2D BlendMap; - -uniform float texScale; //Determines how many times the textures will loop over the terrain uniform vec3 SunDirection_cameraspace; uniform mat4 V; @@ -59,25 +52,6 @@ float Shadow(vec4 ShadowCoord, vec3 normal) void main() { - //Fixa så den bara gör detta om modellen har en blend map - //vvvvvv - - vec4 Blend = texture2D(BlendMap, Input.TextureCoord.st ); - vec4 AsphaltTexel = texture2D(AsphaltTexture, Input.TextureCoord.st * texScale); - vec4 GrassTexel = texture2D(GrassTexture, Input.TextureCoord.st * texScale); - vec4 SandTexel = texture2D(SandTexture, Input.TextureCoord.st * texScale); - - //Mix the Terrain-textures together - AsphaltTexel *= Blend.r; - GrassTexel = mix(AsphaltTexel, GrassTexel, Blend.g); - vec4 tex = mix(GrassTexel, SandTexel, Blend.b); - - //^^^^^^ - //Fixa så den bara gör detta om modellen har en blend map - - - - // G-buffer Position frag_Position = vec4(Input.Position.xyz, 1.0); diff --git a/src/Shaders/Skybox.frag.glsl b/src/Shaders/Skybox.frag.glsl index 195fa21..0c97492 100755 --- a/src/Shaders/Skybox.frag.glsl +++ b/src/Shaders/Skybox.frag.glsl @@ -12,5 +12,4 @@ out vec4 FragColor; void main() { FragColor = texture(CubemapTexture, Input.TextureCoord); - //FragColor = vec4(1.0, 1.0, 1.0, 0.0); } \ No newline at end of file diff --git a/src/Systems/ParticleSystem.cpp b/src/Systems/ParticleSystem.cpp index 893bb07..45c3646 100644 --- a/src/Systems/ParticleSystem.cpp +++ b/src/Systems/ParticleSystem.cpp @@ -8,7 +8,7 @@ void Systems::ParticleSystem::Initialize() { m_TransformSystem = m_World->GetSystem(); tempSpawnedExplosions = false; - EVENT_SUBSCRIBE_MEMBER(m_EKeyUp, &Systems::ParticleSystem::OnKeyUp); + EVENT_SUBSCRIBE_MEMBER(m_EExplosion, &ParticleSystem::CreateExplosion); } void Systems::ParticleSystem::Update(double dt) @@ -22,11 +22,14 @@ void Systems::ParticleSystem::Update(double dt) double timeLived = glfwGetTime() - spawnTime; auto eComp = m_World->GetComponent(explosionID); - if(timeLived > eComp->LifeTime) + if(timeLived > eComp->LifeTime && m_ParticlesToEmitter[explosionID] == NULL) { + auto e = m_World->GetComponent(explosionID); + m_World->RemoveEntity(e->ParticleTemplate); m_World->RemoveEntity(explosionID); it = m_ExplosionEmitters.erase(it); //LOG_INFO("Deleted explosion emitter successfully"); + //LOG_INFO("Deleted explosion emitter successfully. nr of emitters%i", m_ExplosionEmitters.size()); } else { @@ -46,69 +49,82 @@ void Systems::ParticleSystem::UpdateEntity(double dt, EntityID entity, EntityID { emitterComponent->TimeSinceLastSpawn += dt; auto emitterTransformComponent = m_World->GetComponent(entity); - if(emitterComponent->TimeSinceLastSpawn > emitterComponent->SpawnFrequency) +// if(emitterComponent->TimeSinceLastSpawn > emitterComponent->SpawnFrequency) +// { +// SpawnParticles(entity); +// emitterComponent->TimeSinceLastSpawn = 0; +// } + } + + auto particleComponent = m_World->GetComponent(entity); + if(particleComponent) + { + + + EntityID particleID = entity; + double timeLived = glfwGetTime() - particleComponent->SpawnTime; + + if(timeLived > particleComponent->LifeTime) { - SpawnParticles(entity); - emitterComponent->TimeSinceLastSpawn = 0; + m_World->RemoveEntity(particleID); + m_ParticlesToEmitter.erase(particleID); + + return; } - - std::list::iterator it; - for(it = m_ParticleEmitter[entity].begin(); it != m_ParticleEmitter[entity].end();) + else { - EntityID particleID = (it)->ParticleID; auto transformComponent = m_World->GetComponent(particleID); - auto particleComponent = m_World->GetComponent(particleID); - - double timeLived = glfwGetTime() - it->SpawnTime; - if(timeLived > particleComponent->LifeTime) + auto eComponent = m_World->GetComponent(m_ParticlesToEmitter[particleID]); + auto sprite = m_World->GetComponent(entity); + // FIX: calculate once + float timeProgress = timeLived / particleComponent->LifeTime; + // ColorInterpolation(timeProgress, particleComponent->ColorSpectrum, color); + // Scale interpolation + if(particleComponent->ScaleSpectrum.size() > 1) + VectorInterpolation(timeProgress, particleComponent->ScaleSpectrum, transformComponent->Scale); + // Velocity interpolation + if(particleComponent->VelocitySpectrum.size() > 1) + VectorInterpolation(timeProgress, particleComponent->VelocitySpectrum, transformComponent->Velocity); + + if(particleComponent->Fade == true) { - m_World->RemoveEntity(particleID); - it = m_ParticleEmitter[entity].erase(it); + std::vector spectrum; + spectrum.push_back(1); + spectrum.push_back(0); + float alpha; + ScalarInterpolation(timeProgress, spectrum, alpha); + sprite->Color.w = alpha; } - else + + + + /*// Angular velocity interpolation + if (particleComponent->AngularVelocitySpectrum.size() != 0) { - // FIX: calculate once - float timeProgress = timeLived / particleComponent->LifeTime; - // ColorInterpolation(timeProgress, particleComponent->ColorSpectrum, color); - // Scale interpolation - if(particleComponent->ScaleSpectrum.size() > 1) - VectorInterpolation(timeProgress, particleComponent->ScaleSpectrum, transformComponent->Scale); - // Velocity interpolation - if(particleComponent->VelocitySpectrum.size() > 1) - VectorInterpolation(timeProgress, particleComponent->VelocitySpectrum, transformComponent->Velocity); - - // Angular velocity interpolation - if (particleComponent->AngularVelocitySpectrum.size() != 0) + if(particleComponent->AngularVelocitySpectrum.size() > 1) { - if(particleComponent->AngularVelocitySpectrum.size() > 1) - { - ScalarInterpolation(timeProgress, particleComponent->AngularVelocitySpectrum, it->AngularVelocity); - transformComponent->Orientation = glm::angleAxis(it->AngularVelocity, it->Orientation); - } - else - { - transformComponent->Orientation *= glm::angleAxis(it->AngularVelocity, it->Orientation); - //it->Orientation = glm::angleAxis(it->AngularVelocity, it->Orientation); - } + ScalarInterpolation(timeProgress, particleComponent->AngularVelocitySpectrum, it->AngularVelocity); + transformComponent->Orientation = glm::angleAxis(it->AngularVelocity, it->Orientation); } - - //Angular velocity interpolation - if(particleComponent->OrientationSpectrum.size() > 1) + else { - VectorInterpolation(timeProgress, particleComponent->OrientationSpectrum, it->Orientation); - glm::vec3 v1 = (particleComponent->OrientationSpectrum[0]); - glm::vec3 v2 = (it->Orientation); - glm::vec3 v3 = glm::normalize(glm::cross(v1,v2)); - float angle = glm::acos(glm::dot(v1, v2) / (glm::length(v1) * glm::length(v2))); - - transformComponent->Orientation = glm::angleAxis(angle, v3); + transformComponent->Orientation *= glm::angleAxis(it->AngularVelocity, it->Orientation); + //it->Orientation = glm::angleAxis(it->AngularVelocity, it->Orientation); } - - - transformComponent->Position += transformComponent->Velocity * (float)dt; - - it++; } + + //Angular velocity interpolation + if(particleComponent->OrientationSpectrum.size() > 1) + { + VectorInterpolation(timeProgress, particleComponent->OrientationSpectrum, it->Orientation); + glm::vec3 v1 = (particleComponent->OrientationSpectrum[0]); + glm::vec3 v2 = (it->Orientation); + glm::vec3 v3 = glm::normalize(glm::cross(v1,v2)); + float angle = glm::acos(glm::dot(v1, v2) / (glm::length(v1) * glm::length(v2))); + + transformComponent->Orientation = glm::angleAxis(angle, v3); + }*/ + transformComponent->Position += transformComponent->Velocity * (float)dt; } } } @@ -127,12 +143,11 @@ void Systems::ParticleSystem::SpawnParticles(EntityID emitterID) glm::vec3 ePosition = m_TransformSystem->AbsolutePosition(emitterID); glm::quat eOrientation = eTransform->Orientation; glm::vec3 paticleSpeed = glm::vec3(eComponent->Speed); - for(int i = 0; i < eComponent->SpawnCount; i++) { - auto particleEntity = m_World->CloneEntity(eComponent->ParticleTemplate); - - auto particleTransform = m_World->GetComponent(particleEntity); + auto ent = m_World->CloneEntity(eComponent->ParticleTemplate); + m_ParticlesToEmitter.insert(std::make_pair(ent, emitterID)); + auto particleTransform = m_World->GetComponent(ent); particleTransform->Position = ePosition; particleTransform->Orientation = eOrientation; @@ -143,16 +158,21 @@ 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 particleComponent = m_World->AddComponent(particleEntity); - particleComponent->LifeTime = eComponent->LifeTime - 0.5; - particleComponent->ScaleSpectrum = eComponent->ScaleSpectrum; - particleComponent->VelocitySpectrum.push_back(particleTransform->Velocity); + auto particle = m_World->AddComponent(ent); + particle->LifeTime = eComponent->LifeTime; + particle->ScaleSpectrum = eComponent->ScaleSpectrum; + particle->VelocitySpectrum.push_back(particleTransform->Velocity); + particle->Fade = eComponent->Fade; + + auto sprite = m_World->GetComponent(ent); + if(eComponent->Color != glm::vec4(0)) + sprite->Color = eComponent->Color; if (eComponent->ScaleSpectrum.size() > 0) { if (eComponent->ScaleSpectrum.size() > 1) { - particleComponent->ScaleSpectrum = eComponent->ScaleSpectrum; + particle->ScaleSpectrum = eComponent->ScaleSpectrum; } else { @@ -165,22 +185,20 @@ void Systems::ParticleSystem::SpawnParticles(EntityID emitterID) } if(eComponent->UseGoalVelocity) - particleComponent->VelocitySpectrum.push_back(eComponent->GoalVelocity); - particleComponent->OrientationSpectrum = eComponent->OrientationSpectrum; - if(particleComponent->OrientationSpectrum.size() != 0) - particleTransform->Orientation = glm::angleAxis(0.f, particleComponent->OrientationSpectrum[0]); - particleComponent->AngularVelocitySpectrum = eComponent->AngularVelocitySpectrum; + particle->VelocitySpectrum.push_back(eComponent->GoalVelocity); + particle->OrientationSpectrum = eComponent->OrientationSpectrum; + if(particle->OrientationSpectrum.size() != 0) + particleTransform->Orientation = glm::angleAxis(0.f, particle->OrientationSpectrum[0]); + particle->AngularVelocitySpectrum = eComponent->AngularVelocitySpectrum; - ParticleData data; - data.ParticleID = particleEntity; - data.SpawnTime = glfwGetTime(); - if (particleComponent->AngularVelocitySpectrum.size() != 0) - data.AngularVelocity = particleComponent->AngularVelocitySpectrum[0]; - if (particleComponent->OrientationSpectrum.size() != 0) - data.Orientation = particleComponent->OrientationSpectrum[0]; - else data.Orientation = eOrientation * glm::vec3(0,0,-1); - m_ParticleEmitter[emitterID].push_back(data); + particle->SpawnTime = glfwGetTime(); +// if (particle->AngularVelocitySpectrum.size() != 0) +// data.AngularVelocity = particle->AngularVelocitySpectrum[0]; +// if (particle->OrientationSpectrum.size() != 0) +// data.Orientation = particle->OrientationSpectrum[0]; +// else data.Orientation = eOrientation * glm::vec3(0,0,-1); + //m_ParticleEmitter[emitterID].push_back(data); } } @@ -200,23 +218,13 @@ void Systems::ParticleSystem::VectorInterpolation(double timeProgress, std::vect dAxisValue = glm::abs(spectrum[0].y - spectrum[1].y); if (spectrum[0].y > spectrum[1].y) dAxisValue *= -1; - v.y = spectrum[0].y + dAxisValue * timeProgress; + v.y = spectrum[0].y + dAxisValue * timeProgress; dAxisValue = glm::abs(spectrum[0].z - spectrum[1].z); if(spectrum[0].z > spectrum[1].z) dAxisValue *= -1; v.z = spectrum[0].z + dAxisValue * timeProgress; } -// void Systems::ParticleSystem::ColorInterpolation(double timeProgress, std::vector spectrum, Color &c) -// { -// float dColor = glm::abs(spectrum[0].r - spectrum[1].r); -// c.r = spectrum[0].r + dColor * timeProgress; -// dColor = glm::abs(spectrum[0].g - spectrum[1].g); -// c.g = spectrum[0].g + dColor * timeProgress; -// dColor = glm::abs(spectrum[0].b - spectrum[1].b); -// c.b = spectrum[0].b + dColor * timeProgress; -// } - void Systems::ParticleSystem::ScalarInterpolation(double timeProgress, std::vector spectrum, float &alpha) { float dAlpha = glm::abs(spectrum[0] - spectrum[1]); @@ -225,53 +233,41 @@ void Systems::ParticleSystem::ScalarInterpolation(double timeProgress, std::vect alpha = spectrum[0] + dAlpha * timeProgress; } -void Systems::ParticleSystem::CreateExplosion(glm::vec3 _pos, double _lifeTime, int _particlesToSpawn, std::string _spritePath, glm::quat _relativeUpOri, float _speed, float _spreadAngle, float _particleScale) +bool Systems::ParticleSystem::CreateExplosion(const Events::CreateExplosion &e) { + LOG_INFO("Spawning an explosion"); auto explosion = m_World->CreateEntity(); auto emitter = m_World->AddComponent(explosion); - emitter->LifeTime = _lifeTime; - emitter->SpawnCount = _particlesToSpawn; - emitter->Speed = _speed; - emitter->SpreadAngle = _spreadAngle; - emitter->SpawnFrequency = _lifeTime + 20; //temp -// emitter->UseGoalVelocity = true; -// emitter->GoalVelocity = glm::vec3(0,-_speed, 0); + emitter->LifeTime = e.LifeTime; + emitter->SpawnCount = e.ParticlesToSpawn; + emitter->Speed = e.Speed; + emitter->SpreadAngle = e.SpreadAngle; + //emitter->ScaleSpectrum.push_back(glm::vec3(e.ParticleScale)); + emitter->SpawnFrequency = e.LifeTime + 20; //temp + emitter->Fade = true; + emitter->Color = e.Color; + std::vector scale; + scale.push_back(glm::vec3(e.ParticleScale[0])); + if(e.ParticleScale.size() >= 2) + scale.push_back(glm::vec3(e.ParticleScale[1])); + emitter->ScaleSpectrum = scale; + // emitter->GoalVelocity = glm::vec3(0,-_speed, 0); m_World->CommitEntity(explosion); auto particleEnt = m_World->CreateEntity(); + auto templateComponent = m_World->AddComponent(particleEnt); auto TEMP = m_World->AddComponent(particleEnt); - TEMP->Scale = glm::vec3(0); auto spriteComponent = m_World->AddComponent(particleEnt); - spriteComponent->SpriteFile = _spritePath; + spriteComponent->SpriteFile = e.spritePath; m_World->CommitEntity(particleEnt); emitter->ParticleTemplate = particleEnt; - + auto transform = m_World->AddComponent(explosion); - transform->Position = _pos; - transform->Orientation = _relativeUpOri; - + transform->Position = e.Position; + transform->Orientation = e.RelativeUpOrientation; + SpawnParticles(explosion); m_ExplosionEmitters[explosion] = glfwGetTime(); -} -bool Systems::ParticleSystem::OnKeyUp(const Events::KeyUp &e) -{ - if(!tempSpawnedExplosions) - { - if (e.KeyCode == GLFW_KEY_B) - { - tempSpawnedExplosions = true; - CreateExplosion( - glm::vec3(0, 10, 0), - 0.5, - 60, - "Textures/Sprites/NewtonTreeDeleteASAPPlease.png", - glm::angleAxis(glm::pi()/2, glm::vec3(1,0,0)), - 40, - glm::pi(), - 0.5f - ); - } - } return true; } \ No newline at end of file diff --git a/src/Systems/ParticleSystem.h b/src/Systems/ParticleSystem.h index e11e73c..f70cc9d 100644 --- a/src/Systems/ParticleSystem.h +++ b/src/Systems/ParticleSystem.h @@ -9,21 +9,13 @@ #include "Components/Sprite.h" #include "EventBroker.h" #include "Events/KeyUp.h" +#include "Events/CreateExplosion.h" #include "Color.h" #include namespace Systems { - struct ParticleData - { - EntityID ParticleID; - double SpawnTime; - float AngularVelocity; - glm::vec3 Orientation; - Color color; - }; - class ParticleSystem : public System { public: @@ -36,7 +28,7 @@ public: void UpdateEntity(double dt, EntityID entity, EntityID parent) override; void Initialize() override; - void CreateExplosion(glm::vec3 _pos, double _lifeTime, int _particlesToSpawn, std::string _spritePath, glm::quat _relativeUpOri, float _speed, float _spreadAngle, float _particleScale); + //void CreateExplosion(glm::vec3 _pos, double _lifeTime, int _particlesToSpawn, std::string _spritePath, glm::quat _relativeUpOri, float _speed, float _spreadAngle, float _particleScale); virtual bool OnCommand(const Events::KeyUp &event) { return false; } @@ -45,19 +37,21 @@ private: float RandomizeAngle(float spreadAngle); //void ScaleInterpolation(double timeProgress, std::vector spectrum, glm::vec3 &scale); void VectorInterpolation(double timeProgress, std::vector spectrum, glm::vec3 &velocity); - //void ColorInterpolation(double timeProgress, std::vector spectrum, Color &color); void ScalarInterpolation(double timeProgress, std::vector spectrum, float &alpha); void Billboard(); - std::map> m_ParticleEmitter; - std::map m_TimeSinceLastSpawn; - std::map m_ExplosionEmitters; + //std::map> m_ParticleEmitter; + std::shared_ptr m_TransformSystem; bool tempSpawnedExplosions; - EventRelay m_EKeyUp; - bool OnKeyUp(const Events::KeyUp &e); - +// EventRelay m_EKeyUp; +// bool OnKeyUp(const Events::KeyUp &e); + EventRelay m_EExplosion; + bool CreateExplosion(const Events::CreateExplosion &e); + + std::map m_ExplosionEmitters; + std::map m_ParticlesToEmitter; }; } diff --git a/src/Systems/RenderSystem.cpp b/src/Systems/RenderSystem.cpp index e101f30..99acd91 100755 --- a/src/Systems/RenderSystem.cpp +++ b/src/Systems/RenderSystem.cpp @@ -15,6 +15,7 @@ void Systems::RenderSystem::RegisterComponents(ComponentFactory* cf) cf->Register([]() { return new Components::PointLight(); }); cf->Register([]() { return new Components::DirectionalLight(); }); cf->Register([]() { return new Components::Viewport(); }); + cf->Register([]() { return new Components::BlendMap(); }); } void Systems::RenderSystem::OnEntityCommit(EntityID entity) diff --git a/src/Systems/RenderSystem.h b/src/Systems/RenderSystem.h index e6350db..0829f29 100755 --- a/src/Systems/RenderSystem.h +++ b/src/Systems/RenderSystem.h @@ -20,6 +20,7 @@ #include "Renderer.h" #include "RenderQueue.h" #include "Events/SetViewportCamera.h" +#include "Components/BlendMap.h" namespace Systems { diff --git a/src/Systems/TankSteeringSystem.cpp b/src/Systems/TankSteeringSystem.cpp index f5edcaa..a04dbc8 100644 --- a/src/Systems/TankSteeringSystem.cpp +++ b/src/Systems/TankSteeringSystem.cpp @@ -102,7 +102,7 @@ void Systems::TankSteeringSystem::UpdateEntity(double dt, EntityID entity, Entit Events::ApplyPointImpulse ePointImpulse ; ePointImpulse.Entity = entity; ePointImpulse.Position = absoluteTransform.Position; - ePointImpulse.Impulse = glm::normalize(absoluteTransform.Orientation * glm::vec3(0, 0, 1)) * clonePhysicsComponent->Mass * 1670.f; + ePointImpulse.Impulse = glm::normalize(absoluteTransform.Orientation * glm::vec3(0, 0, 1)) * clonePhysicsComponent->Mass * 6.f * 1670.f; EventBroker->Publish(ePointImpulse); } @@ -141,6 +141,41 @@ bool Systems::TankSteeringSystem::OnCollision( const Events::Collision &e ) auto physicsComponents = m_World->GetComponentsOfType(); auto shellTransform = m_World->GetComponent(shellEntity); //auto otherTransform = m_World->GetComponent(otherEntity); + + { + Events::CreateExplosion e; + e.LifeTime = 3; + e.ParticleScale.push_back(8); + e.ParticlesToSpawn = 20; + e.Position = shellTransform->Position; + e.RelativeUpOrientation = glm::angleAxis(glm::pi() / 2, glm::vec3(1,0,0)); + e.Speed = 3; + e.SpreadAngle = glm::pi(); + e.spritePath = "Textures/Sprites/Smoke1.png"; + e.Color = glm::vec4(0.6,0.6,0.6,1); + EventBroker->Publish(e); + e.LifeTime = 0.7; + e.ParticleScale.push_back(12); + e.ParticlesToSpawn = 10; + e.Position = shellTransform->Position; + e.RelativeUpOrientation = glm::angleAxis(glm::pi() / 2, glm::vec3(1,0,0)); + e.Speed = 17; + e.SpreadAngle = glm::pi(); + e.spritePath = "Textures/Sprites/Fire.png"; + EventBroker->Publish(e); + e.LifeTime = 0.2; + e.ParticleScale.push_back(1); + e.ParticleScale.push_back(15); + e.ParticlesToSpawn = 5; + e.Position = shellTransform->Position; + e.RelativeUpOrientation = glm::angleAxis(glm::pi() / 2, glm::vec3(1,0,0)); + e.Speed = 6; + e.SpreadAngle = glm::pi(); + e.spritePath = "Textures/Sprites/Blast1.png"; + e.Color = glm::vec4(2, 2, 2, 0.2); + EventBroker->Publish(e); + } + for (auto &physComponent : *physicsComponents) { EntityID physicsEntity = std::dynamic_pointer_cast(physComponent)->Entity; diff --git a/src/Systems/TankSteeringSystem.h b/src/Systems/TankSteeringSystem.h index 01fc27a..8c996fa 100644 --- a/src/Systems/TankSteeringSystem.h +++ b/src/Systems/TankSteeringSystem.h @@ -5,6 +5,7 @@ #include "Events/SetVelocity.h" #include "Events/ApplyForce.h" #include "Events/ApplyPointImpulse.h" +#include "Events/CreateExplosion.h" #include "Events/Collision.h" #include "Components/Transform.h" #include "Components/TankSteering.h" diff --git a/vs11/Returngeance/Returngeance.vcxproj b/vs11/Returngeance/Returngeance.vcxproj index 1a131d0..2cbd9ab 100755 --- a/vs11/Returngeance/Returngeance.vcxproj +++ b/vs11/Returngeance/Returngeance.vcxproj @@ -133,6 +133,7 @@ + @@ -184,6 +185,7 @@ + @@ -255,6 +257,10 @@ + + + + diff --git a/vs11/Returngeance/Returngeance.vcxproj.filters b/vs11/Returngeance/Returngeance.vcxproj.filters index 2d645ad..b7c5c5a 100755 --- a/vs11/Returngeance/Returngeance.vcxproj.filters +++ b/vs11/Returngeance/Returngeance.vcxproj.filters @@ -182,13 +182,19 @@ {cb06b441-90b8-46ed-b347-4190dac7185b} - + + {ddd3c442-7c2f-4690-9308-fcef62deee0b} + + {3c2ea0e5-41a1-4b11-a891-1d59ead7223c} {3cbe68d9-2446-45ee-8637-ffb805997dcd} + + {a025d51e-594d-4844-983b-f683726bf1bf} + {9702064a-02a2-4b3c-a2ab-47c23a9cf49c} @@ -428,6 +434,9 @@ Game\Components + + Particle System\Events + Physics\Events @@ -542,6 +551,9 @@ Base\Components + + Rendering\Components + GUI @@ -607,5 +619,17 @@ Shaders + + Shaders + + + Shaders + + + Shaders + + + Shaders + \ No newline at end of file