diff --git a/src/Camera.cpp b/src/Camera.cpp index 1256e64..6e8bc0d 100755 --- a/src/Camera.cpp +++ b/src/Camera.cpp @@ -1,25 +1,21 @@ #include "PrecompiledHeader.h" #include "Camera.h" -Camera::Camera(float yFOV, float aspectRatio, float nearClip, float farClip) +Camera::Camera(float yFOV, float nearClip, float farClip) { m_FOV = yFOV; - m_AspectRatio = aspectRatio; m_NearClip = nearClip; m_FarClip = farClip; m_Position = glm::vec3(0.0); - /*m_Pitch = 0.f; - m_Yaw = 0.f;*/ - UpdateProjectionMatrix(); UpdateViewMatrix(); } -//glm::vec3 Camera::Forward() -//{ -// return glm::rotate(glm::vec3(0.f, 0.f, -1.f), -m_Yaw, glm::vec3(0.f, 1.f, 0.f)); -//} +glm::vec3 Camera::Forward() +{ + return m_Orientation * glm::vec3(0, 0, -1); +} // //glm::vec3 Camera::Right() //{ @@ -34,20 +30,14 @@ Camera::Camera(float yFOV, float aspectRatio, float nearClip, float farClip) // return orientation; //} -void Camera::AspectRatio(float val) -{ - m_AspectRatio = val; - UpdateProjectionMatrix(); -} - -void Camera::Position(glm::vec3 val) +void Camera::SetPosition(glm::vec3 val) { m_Position = val; UpdateViewMatrix(); } -void Camera::Orientation(glm::quat val) +void Camera::SetOrientation(glm::quat val) { m_Orientation = val; UpdateViewMatrix(); @@ -65,35 +55,32 @@ void Camera::Orientation(glm::quat val) // UpdateViewMatrix(); //} -void Camera::UpdateProjectionMatrix() -{ - m_ProjectionMatrix = glm::perspective( - m_FOV, - m_AspectRatio, - m_NearClip, - m_FarClip - ); -} - void Camera::UpdateViewMatrix() { m_ViewMatrix = glm::toMat4(glm::inverse(m_Orientation)) * glm::translate(-m_Position); } -void Camera::FOV(float val) +void Camera::SetFOV(float val) { m_FOV = val; - UpdateProjectionMatrix(); } -void Camera::NearClip(float val) +void Camera::SetNearClip(float val) { m_NearClip = val; - UpdateProjectionMatrix(); } -void Camera::FarClip(float val) +void Camera::SetFarClip(float val) { m_FarClip = val; - UpdateProjectionMatrix(); -} \ No newline at end of file +} + +glm::mat4 Camera::ProjectionMatrix(float aspectRatio) +{ + return glm::perspective( + m_FOV, + aspectRatio, + m_NearClip, + m_FarClip + ); +} diff --git a/src/Camera.h b/src/Camera.h index a50670a..663a16e 100755 --- a/src/Camera.h +++ b/src/Camera.h @@ -1,60 +1,48 @@ #ifndef Camera_h__ #define Camera_h__ -//#include "PrecompiledHeader.h" - class Camera { public: - Camera(float yFOV, float aspectRatio, float nearClip, float farClip); + Camera(float yFOV, float nearClip, float farClip); glm::vec3 Forward(); glm::vec3 Right(); - float AspectRatio() const { return m_AspectRatio; } - void AspectRatio(float val); - glm::vec3 Position() const { return m_Position; } - void Position(glm::vec3 val); + void SetPosition(glm::vec3 val); glm::quat Orientation() const { return m_Orientation; } - void Orientation(glm::quat val); + void SetOrientation(glm::quat val); /*float Pitch() const { return m_Pitch; } void Pitch(float val); float Yaw() const { return m_Yaw; } void Yaw(float val);*/ - glm::mat4 ProjectionMatrix() const { return m_ProjectionMatrix; } - void ProjectionMatrix(glm::mat4 val) { m_ProjectionMatrix = val; } + glm::mat4 ProjectionMatrix(float aspectRatio); glm::mat4 ViewMatrix() const { return m_ViewMatrix; } - void ViewMatrix(glm::mat4 val) { m_ViewMatrix = val; } float FOV() const { return m_FOV; } - void FOV(float val); + void SetFOV(float val); float NearClip() const { return m_NearClip; } - void NearClip(float val); + void SetNearClip(float val); float FarClip() const { return m_FarClip; } - void FarClip(float val); + void SetFarClip(float val); private: - void UpdateProjectionMatrix(); void UpdateViewMatrix(); float m_FOV; - float m_AspectRatio; float m_NearClip; float m_FarClip; glm::vec3 m_Position; glm::quat m_Orientation; - //float m_Pitch; - //float m_Yaw; - glm::mat4 m_ProjectionMatrix; glm::mat4 m_ViewMatrix; }; diff --git a/src/Components/Health.h b/src/Components/Health.h index 5100aaf..ca8daee 100644 --- a/src/Components/Health.h +++ b/src/Components/Health.h @@ -9,9 +9,9 @@ namespace Components struct Health : Component { Health() - : health(1.0f){ } + : Amount(1.0f) { } - float health; + float Amount; virtual Health* Clone() const override { return new Health(*this); } }; diff --git a/src/Components/PointLight.h b/src/Components/PointLight.h index 9037b89..1f74165 100755 --- a/src/Components/PointLight.h +++ b/src/Components/PointLight.h @@ -16,9 +16,11 @@ struct PointLight : Component , ConstantAttenuation(1.0f) , LinearAttenuation(0.f) , QuadraticAttenuation(3.f) + , Radius(5.f) { } float ConstantAttenuation, LinearAttenuation, QuadraticAttenuation; + float Radius; Color color; glm::vec3 Specular; diff --git a/src/Engine.h b/src/Engine.h index 2d8215c..22f8451 100755 --- a/src/Engine.h +++ b/src/Engine.h @@ -1,11 +1,16 @@ #include #include +#include "ResourceManager.h" +#include "OBJ.h" +#include "Model.h" +#include "Texture.h" #include "EventBroker.h" +#include "RenderQueue.h" #include "Renderer.h" #include "InputManager.h" #include "GUI/Frame.h" -#include "GameWorld.h" +#include "GUI/GameFrame.h" class Engine { @@ -14,15 +19,24 @@ public: { m_EventBroker = std::make_shared(); - m_Renderer = std::make_shared(); + m_ResourceManager = std::make_shared(); + m_ResourceManager->RegisterType("OBJ", [](std::string resourceName) { return new OBJ(resourceName); }); + auto rm = m_ResourceManager; + m_ResourceManager->RegisterType("Model", [rm](std::string resourceName) { return new Model(rm, *rm->Load("OBJ", resourceName)); }); + m_ResourceManager->RegisterType("Texture", [](std::string resourceName) { return new Texture(resourceName); }); + + m_Renderer = std::make_shared(m_ResourceManager); m_Renderer->Initialize(); m_InputManager = std::make_shared(m_Renderer->GetWindow(), m_EventBroker); - //m_UIParent = std::make_shared(m_EventBroker); + m_FrameStack = new GUI::Frame(m_EventBroker, m_ResourceManager); + m_FrameStack->Width = 1280; + m_FrameStack->Height = 720; + new GUI::GameFrame(m_FrameStack, "GameFrame"); - m_World = std::make_shared(m_EventBroker, m_Renderer); - m_World->Initialize(); + //m_World = std::make_shared(m_EventBroker, m_ResourceManager); + //m_World->Initialize(); m_LastTime = glfwGetTime(); } @@ -32,24 +46,34 @@ public: void Tick() { double currentTime = glfwGetTime(); - double dt = currentTime - m_LastTime; + double dt = currentTime - m_LastTime; m_LastTime = currentTime; + // Update input m_InputManager->Update(dt); - m_World->Update(dt); - m_Renderer->Draw(dt); + + // Update frame stack + m_EventBroker->Process(); + m_FrameStack->UpdateLayered(dt); + + // Render scene + m_FrameStack->DrawLayered(m_Renderer); + m_Renderer->Swap(); + + // Swap event queues m_EventBroker->Clear(); glfwPollEvents(); } private: + std::shared_ptr m_ResourceManager; std::shared_ptr m_EventBroker; std::shared_ptr m_Renderer; std::shared_ptr m_InputManager; - //std::shared_ptr m_UIParent; + GUI::Frame* m_FrameStack; // TODO: This should ultimately live in GameFrame - std::shared_ptr m_World; + //std::shared_ptr m_World; double m_LastTime; }; \ No newline at end of file diff --git a/src/Events/Damage.h b/src/Events/Damage.h index 5f46f3e..a5ddec0 100644 --- a/src/Events/Damage.h +++ b/src/Events/Damage.h @@ -5,13 +5,13 @@ namespace Events { - struct Damage : Event { EntityID Entity; - float damage; + float Amount; }; - } + + #endif // Events_Damage_h__ \ No newline at end of file diff --git a/src/Events/SetViewportCamera.h b/src/Events/SetViewportCamera.h new file mode 100644 index 0000000..66ad9e4 --- /dev/null +++ b/src/Events/SetViewportCamera.h @@ -0,0 +1,18 @@ +#ifndef Events_SetViewportCamera_h__ +#define Events_SetViewportCamera_h__ + +#include "EventBroker.h" +#include "Entity.h" + +namespace Events +{ + +struct SetViewportCamera : Event +{ + std::string ViewportFrame; + EntityID CameraEntity; +}; + +} + +#endif // Events_SetViewportCamera_h__ diff --git a/src/GUI/Frame.h b/src/GUI/Frame.h index 628a650..a2acb8e 100644 --- a/src/GUI/Frame.h +++ b/src/GUI/Frame.h @@ -2,12 +2,14 @@ #define GUI_Frame_h__ #include +#include #include "Util/Rectangle.h" #include "EventBroker.h" - -// HACK: Decouple renderer plz +#include "ResourceManager.h" #include "Renderer.h" +#include "RenderQueue.h" +#include "Texture.h" namespace GUI { @@ -24,50 +26,134 @@ public: }; // Set up a base frame with an event broker - Frame(std::shared_ptr<::EventBroker> eventBroker) + Frame(std::shared_ptr<::EventBroker> eventBroker, std::shared_ptr<::ResourceManager> resourceManager) : EventBroker(eventBroker) - , Rectangle() - { Initialize(); } - // Create a frame as a child - Frame(std::shared_ptr parent) - : Rectangle(static_cast(*parent)) // Clone parent rectangle using copy constructor - { SetParent(parent); Initialize(); } + , ResourceManager(resourceManager) + , Rectangle() + , m_Name("UIParent") + , m_Layer(0) + { } + + // Create a frame as a child + Frame(Frame* parent, std::string name) + : m_Name(name) + , m_Layer(0) + { SetParent(std::shared_ptr(parent)); } + + ::RenderQueue RenderQueue; - virtual void Initialize() { } std::shared_ptr Parent() const { return m_Parent; } void SetParent(std::shared_ptr parent) - { + { + if (parent == nullptr) + { + LOG_ERROR("Failed to create frame \"%s\": Invalid parent", m_Name.c_str()); + return; + } + + Width = parent->Width; + Height = parent->Height; + m_Layer = parent->Layer() + 1; parent->AddChild(std::shared_ptr(this)); m_Parent = parent; EventBroker = parent->EventBroker; + ResourceManager = parent->ResourceManager; } void AddChild(std::shared_ptr child) { - m_Children.push_back(child); - if (m_Parent != nullptr) + m_Children[child->m_Layer].insert(std::make_pair(child->Name(), child)); + if (m_Parent) { m_Parent->AddChild(child); } } - typedef std::list>::const_iterator FrameChildrenIterator; - FrameChildrenIterator begin() - { - return m_Children.begin(); + typedef std::map>::const_iterator FrameChildrenIterator; + + std::string Name() const { return m_Name; } + void SetName(std::string val) { m_Name = val; } + int Layer() const { return m_Layer; } + + int Left() const override + { + if (m_Parent) + return m_Parent->Left() + X; + else + return X; } - FrameChildrenIterator end() + int Right() const override + { + return Left() + Width; + } + int Top() const override + { + if (m_Parent) + return m_Parent->Top() + Y; + else + return Y; + } + int Bottom() const override { - return m_Children.end(); + return Top() + Height; } + Rectangle AbsoluteRectangle() + { + return Rectangle(Left(), Top(), Width, Height); + } + + void UpdateLayered(double dt) + { + // Update ourselves + this->Update(dt); + + // Update children + for (auto &pairLayer : m_Children) + { + auto children = pairLayer.second; + for (auto &pairChild : children) + { + auto child = pairChild.second; + child->Update(dt); + } + } + } virtual void Update(double dt) { } - virtual void Draw(Renderer* renderer) { } + + void DrawLayered(std::shared_ptr renderer) + { + // Draw ourselves + renderer->SetViewport(AbsoluteRectangle()); + this->Draw(renderer); + + // Draw children + for (auto &pairLayer : m_Children) + { + auto children = pairLayer.second; + for (auto &pairChild : children) + { + auto child = pairChild.second; + Rectangle rect = child->AbsoluteRectangle(); + renderer->SetViewport(rect); + child->Draw(renderer); + } + } + } + + virtual void Draw(std::shared_ptr renderer) { } protected: std::shared_ptr<::EventBroker> EventBroker; + std::shared_ptr<::ResourceManager> ResourceManager; + + std::string m_Name; + int m_Layer; + std::shared_ptr m_Parent; - std::list> m_Children; + typedef std::multimap> Children_t; // name -> frame + std::map m_Children; // layer -> Children_t + }; } diff --git a/src/GUI/GameFrame.h b/src/GUI/GameFrame.h new file mode 100644 index 0000000..8286997 --- /dev/null +++ b/src/GUI/GameFrame.h @@ -0,0 +1,51 @@ +#ifndef GUI_GameFrame_h__ +#define GUI_GameFrame_h__ + +#include "GUI/Frame.h" +#include "GUI/WorldFrame.h" +#include "GUI/Viewport.h" +#include "GUI/TextureFrame.h" +#include "GUI/PlayerHUD.h" + +#include "GameWorld.h" + +namespace GUI +{ + +class GameFrame : public Frame +{ +public: + GameFrame(Frame* parent, std::string name) + : Frame(parent, name) + { + m_World = std::make_shared(EventBroker, ResourceManager); + auto worldFrame = new WorldFrame(this, "GameWorldFrame", m_World); + { + vp1 = new Viewport(worldFrame, "Viewport1", m_World); + vp1->X = 0; + vp1->Width = 640; + vp1->Height = 720 / 2; + new PlayerHUD(vp1, "PlayerHUD", m_World, 1); + + vp2 = new Viewport(worldFrame, "Viewport2", m_World); + vp2->X = vp1->Right(); + vp2->Width = 640; + vp2->Height = 720 / 2; + new PlayerHUD(vp2, "PlayerHUD", m_World, 2); + + auto vpc = new Viewport(worldFrame, "ViewportFreeCam", m_World); + vpc->Y = 720 / 2; + vpc->Height = 720 / 2; + } + m_World->Initialize(); + } + +private: + std::shared_ptr m_World; + Viewport* vp1; + Viewport* vp2; +}; + +} + +#endif // GUI_GameFrame_h__ diff --git a/src/GUI/HealthOverlay.h b/src/GUI/HealthOverlay.h new file mode 100644 index 0000000..3760a93 --- /dev/null +++ b/src/GUI/HealthOverlay.h @@ -0,0 +1,53 @@ +#ifndef GUI_HealthOverlay_h__ +#define GUI_HealthOverlay_h__ + +#include "GUI/TextureFrame.h" +#include "World.h" +#include "Events/Damage.h" +#include "Components/Player.h" +#include "Components/Health.h" + +namespace GUI +{ + + class HealthOverlay : public TextureFrame + { + public: + HealthOverlay(Frame* parent, std::string name, std::shared_ptr world, int playerID) + : TextureFrame(parent, name) + , m_World(world) + , m_PlayerID(playerID) + { + EVENT_SUBSCRIBE_MEMBER(m_EDamage, &HealthOverlay::OnDamage); + + SetTexture("Textures/GUI/hurt.png"); + SetColor(glm::vec4(0.f)); + } + + bool OnDamage(const Events::Damage &event) + { + auto player = m_World->GetComponent(event.Entity); + if (!player) + return false; + + if (player->ID != m_PlayerID) + return false; + + auto health = m_World->GetComponent(event.Entity); + if (!health) + return false; + + SetColor(glm::vec4(1.f, 1.f, 1.f, 1 - health->Amount / 100.f)); + + return true; + } + + protected: + std::shared_ptr m_World; + int m_PlayerID; + EventRelay m_EDamage; + }; + +} + +#endif // GUI_TextureFrame_h__ diff --git a/src/GUI/PlayerHUD.h b/src/GUI/PlayerHUD.h new file mode 100644 index 0000000..648230a --- /dev/null +++ b/src/GUI/PlayerHUD.h @@ -0,0 +1,30 @@ +#include "GUI/Frame.h" +#include "GUI/HealthOverlay.h" + +#ifndef GUI_PlayerHUD_h__ +#define GUI_PlayerHUD_h__ + +namespace GUI +{ + + class PlayerHUD : public Frame + { + public: + PlayerHUD(Frame* parent, std::string name, std::shared_ptr world, int playerID) + : Frame(parent, name) + , m_World(world) + , m_PlayerID(playerID) + { + m_HealthOverlay = new HealthOverlay(this, "HealthOverlay", m_World, m_PlayerID); + } + + protected: + std::shared_ptr m_World; + int m_PlayerID; + + HealthOverlay* m_HealthOverlay; + }; + +} + +#endif // GUI_TextureFrame_h__ diff --git a/src/GUI/TextureFrame.h b/src/GUI/TextureFrame.h new file mode 100644 index 0000000..6018478 --- /dev/null +++ b/src/GUI/TextureFrame.h @@ -0,0 +1,52 @@ +#ifndef GUI_TextureFrame_h__ +#define GUI_TextureFrame_h__ + +#include "GUI/Frame.h" +#include "Texture.h" + +namespace GUI +{ + +class TextureFrame : public Frame +{ +public: + TextureFrame(Frame* parent, std::string name) + : Frame(parent, name) + , m_Texture(nullptr) + , m_Color(glm::vec4(1.f, 1.f, 1.f, 1.f)) + { } + + void Draw(std::shared_ptr renderer) override + { + if (m_Texture == nullptr) + return; + + RenderQueue.Clear(); + SpriteJob job; + job.TextureID = m_Texture->ResourceID; + job.Texture = *m_Texture; + job.Color = m_Color; + RenderQueue.Add(job); + + renderer->SetCamera(nullptr); + renderer->DrawFrame(RenderQueue); + } + + ::Texture* Texture() const { return m_Texture; } + void SetTexture(std::string resourceName) + { + m_Texture = ResourceManager->Load<::Texture>("Texture", resourceName); + } + + glm::vec4 Color() const { return m_Color; } + void SetColor(glm::vec4 val) { m_Color = val; } + +protected: + ::Texture* m_Texture; + glm::vec4 m_Color; + +}; + +} + +#endif // GUI_TextureFrame_h__ diff --git a/src/GUI/Viewport.h b/src/GUI/Viewport.h index b4568b5..88af865 100644 --- a/src/GUI/Viewport.h +++ b/src/GUI/Viewport.h @@ -4,6 +4,12 @@ #include #include "GUI/Frame.h" +#include "World.h" +#include "Systems/TransformSystem.h" +#include "Components/Transform.h" +#include "Components/Camera.h" +#include "RenderQueue.h" +#include "Camera.h" namespace GUI { @@ -11,9 +17,62 @@ namespace GUI class Viewport : public Frame { public: - // Create a frame as a child - Viewport(std::shared_ptr parent) - : Frame(parent) { } + Viewport(Frame* parent, std::string name, std::shared_ptr world) + : Frame(parent, name) + , m_World(world) + { } + + EntityID CameraEntity() const { return m_CameraEntity; } + void SetCameraEntity(EntityID cameraEntity) + { + m_CameraEntity = cameraEntity; + auto transformComponent = m_World->GetComponent(cameraEntity); + if (!transformComponent) + return; + auto cameraComponent = m_World->GetComponent(cameraEntity); + if (!cameraComponent) + return; + + m_Camera = std::make_shared(cameraComponent->FOV, cameraComponent->NearClip, cameraComponent->FarClip); + } + + + void Update(double dt) override + { + if (!m_TransformSystem) + m_TransformSystem = m_World->GetSystem(); + + auto transformComponent = m_World->GetComponent(m_CameraEntity); + if (!transformComponent) + return; + + auto cameraComponent = m_World->GetComponent(m_CameraEntity); + if (!cameraComponent) + return; + + Components::Transform absoluteTransform = m_TransformSystem->AbsoluteTransform(m_CameraEntity); + + m_Camera->SetFOV(cameraComponent->FOV); + m_Camera->SetNearClip(cameraComponent->NearClip); + m_Camera->SetFarClip(cameraComponent->FarClip); + m_Camera->SetPosition(absoluteTransform.Position); + m_Camera->SetOrientation(absoluteTransform.Orientation); + } + + void Draw(std::shared_ptr renderer) override + { + if (!m_Camera) + return; + + renderer->SetCamera(m_Camera); + renderer->DrawWorld(m_Parent->RenderQueue); + } + +private: + std::shared_ptr m_World; + std::shared_ptr m_TransformSystem; + std::shared_ptr m_Camera; + EntityID m_CameraEntity; }; } diff --git a/src/GUI/WorldFrame.h b/src/GUI/WorldFrame.h new file mode 100644 index 0000000..995b133 --- /dev/null +++ b/src/GUI/WorldFrame.h @@ -0,0 +1,150 @@ +#ifndef GUI_WorldFrame_h__ +#define GUI_WorldFrame_h__ + +#include "GUI/Frame.h" +#include "GUI/Viewport.h" +#include "RenderQueue.h" +#include "World.h" +#include "Systems/TransformSystem.h" +#include "Events/SetViewportCamera.h" +#include "Components/Transform.h" +#include "Components/Model.h" +#include "Components/Sprite.h" +#include "Components/PointLight.h" + +namespace GUI +{ + +class WorldFrame : public Frame +{ +public: + WorldFrame(Frame* parent, std::string name, std::shared_ptr world) + : Frame(parent, name) + , m_World(world) + { + EVENT_SUBSCRIBE_MEMBER(m_ESetViewportCamera, &WorldFrame::OnSetViewportCamera); + } + + void Update(double dt) override + { + if (!m_TransformSystem) + m_TransformSystem = m_World->GetSystem(); + + m_World->Update(dt); + } + + void Draw(std::shared_ptr renderer) override + { + RenderQueue.Clear(); + renderer->ClearPointLights(); + + for (auto &pair : *m_World->GetEntities()) + { + EntityID entity = pair.first; + + auto transform = m_World->GetComponent(entity); + if (!transform) + continue; + + auto modelComponent = m_World->GetComponent(entity); + if (modelComponent) + { + auto modelAsset = ResourceManager->Load("Model", modelComponent->ModelFile); + if (modelAsset) + { + Components::Transform absoluteTransform = m_TransformSystem->AbsoluteTransform(entity); + glm::mat4 modelMatrix = glm::translate(glm::mat4(), absoluteTransform.Position) + * glm::toMat4(absoluteTransform.Orientation) + * glm::scale(absoluteTransform.Scale); + EnqueueModel(modelAsset, modelMatrix); + } + } + + auto spriteComponent = m_World->GetComponent(entity); + if (spriteComponent) + { + auto textureAsset = ResourceManager->Load("Texture", spriteComponent->SpriteFile); + if (textureAsset) + { + Components::Transform absoluteTransform = m_TransformSystem->AbsoluteTransform(entity); + glm::quat orientation2D = glm::angleAxis(glm::eulerAngles(absoluteTransform.Orientation).z, glm::vec3(0, 0, -1)); + glm::mat4 modelMatrix = glm::translate(absoluteTransform.Position) + * glm::toMat4(orientation2D) + * glm::scale(absoluteTransform.Scale); + EnqueueSprite(textureAsset, modelMatrix); + } + } + + auto pointLightComponent = m_World->GetComponent(entity); + if (pointLightComponent) + { + glm::vec3 position = m_TransformSystem->AbsolutePosition(entity); + renderer->AddPointLightToDraw( + position, + pointLightComponent->Specular, + pointLightComponent->Diffuse, + pointLightComponent->specularExponent, + pointLightComponent->ConstantAttenuation, + pointLightComponent->LinearAttenuation, + pointLightComponent->QuadraticAttenuation, + pointLightComponent->Radius + ); + } + } + } + +protected: + std::shared_ptr m_World; + +private: + EventRelay m_ESetViewportCamera; + bool OnSetViewportCamera(const Events::SetViewportCamera &event) + { + // Search next layer for viewports and update cameras + auto itpair = m_Children[m_Layer + 1].equal_range(event.ViewportFrame); + for (auto it = itpair.first; it != itpair.second; ++it) + { + auto viewportFrame = std::dynamic_pointer_cast(it->second); + if (!viewportFrame) + continue; + + viewportFrame->SetCameraEntity(event.CameraEntity); + } + + return true; + } + + std::shared_ptr m_TransformSystem; + + void EnqueueModel(Model* model, glm::mat4 modelMatrix) + { + for (auto texGroup : model->TextureGroups) + { + ModelJob 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.VAO = model->VAO; + job.StartIndex = texGroup.StartIndex; + job.EndIndex = texGroup.EndIndex; + job.ModelMatrix = modelMatrix; + + RenderQueue.Add(job); + } + } + + void EnqueueSprite(Texture* texture, glm::mat4 modelMatrix) + { + SpriteJob job; + job.TextureID = texture->ResourceID; + job.Texture = *texture; + job.ModelMatrix = modelMatrix; + + RenderQueue.Add(job); + } +}; + +} + +#endif // GUI_WorldFrame_h__ diff --git a/src/GameWorld.cpp b/src/GameWorld.cpp index e3bd7a3..d2a591c 100755 --- a/src/GameWorld.cpp +++ b/src/GameWorld.cpp @@ -5,8 +5,8 @@ void GameWorld::Initialize() { World::Initialize(); - m_ResourceManager.Preload("Model", "Models/Placeholders/PhysicsTest/Plane.obj"); - m_ResourceManager.Preload("Model", "Models/Placeholders/PhysicsTest/ArrowCube.obj"); + ResourceManager->Preload("Model", "Models/Placeholders/PhysicsTest/Plane.obj"); + ResourceManager->Preload("Model", "Models/Placeholders/PhysicsTest/ArrowCube.obj"); BindKey(GLFW_KEY_W, "vertical", 1.f); BindKey(GLFW_KEY_S, "vertical", -1.f); @@ -15,9 +15,6 @@ void GameWorld::Initialize() BindGamepadAxis(Gamepad::Axis::LeftX, "horizontal", 1.f); BindGamepadAxis(Gamepad::Axis::LeftY, "vertical", 1.f); - BindKey(GLFW_KEY_1, "EnableCollisions", 1.f); - BindKey(GLFW_KEY_2, "DisableCollisions", 1.f); - BindKey(GLFW_KEY_UP, "barrel_rotation", 1.f); BindKey(GLFW_KEY_DOWN, "barrel_rotation", -1.f); BindKey(GLFW_KEY_LEFT, "tower_rotation", -1.f); @@ -30,6 +27,14 @@ void GameWorld::Initialize() BindKey(GLFW_KEY_Z, "shoot", 1.f); BindGamepadAxis(Gamepad::Axis::RightTrigger, "shoot", 1.f); + + BindMouseButton(GLFW_MOUSE_BUTTON_1, "cam_lock", 1.f); + BindKey(GLFW_KEY_Y, "cam_vertical", 1.f); + BindKey(GLFW_KEY_H, "cam_vertical", -1.f); + BindKey(GLFW_KEY_G, "cam_horizontal", -1.f); + BindKey(GLFW_KEY_J, "cam_horizontal", 1.f); + BindKey(GLFW_KEY_U, "cam_normal", 1.f); + BindKey(GLFW_KEY_T, "cam_normal", -1.f); //BindGamepadButton(Gamepad::Button::Up, "Gamepad::Button::Up", 1.f); //BindGamepadButton(Gamepad::Button::Down, "Gamepad::Button::Down", 1.f); @@ -58,36 +63,40 @@ void GameWorld::Initialize() cameraComp->FarClip = 2000.f; auto freeSteering = AddComponent(camera); } - CommitEntity(camera); - - auto viewport1 = CreateEntity(); { - auto viewport = AddComponent(viewport1); - viewport->Right = 0.5f; - viewport->Camera = camera; - } - CommitEntity(viewport1); - - auto viewport2 = CreateEntity(); - { - auto viewport = AddComponent(viewport2); - viewport->Left = 0.5f; - } - CommitEntity(viewport2); - - auto player1 = CreateEntity(); - { - auto player = AddComponent(player1); - player->ID = 1; + Events::SetViewportCamera e; + e.CameraEntity = camera; + e.ViewportFrame = "ViewportFreeCam"; + EventBroker->Publish(e); } - auto player2 = CreateEntity(); - { - auto player = AddComponent(player2); - player->ID = 2; - } + //{ + // 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/testScene.obj"; + // //model->ModelFile = "Models/Placeholders/Terrain/Terrain2.obj"; + // + // auto physics = AddComponent(ground); + // physics->Mass = 10; + // physics->Static = true; + + + // auto groundshape = CreateEntity(ground); + // auto transformshape = AddComponent(groundshape); + // auto meshShape = AddComponent(groundshape); + // //meshShape->ResourceName = "Models/Placeholders/Terrain/Terrain2.obj"; + // meshShape->ResourceName = "Models/TestScene3/testScene.obj"; + + // + // CommitEntity(groundshape); + // CommitEntity(ground); + //} + - { auto ground = CreateEntity(); auto transform = AddComponent(ground); @@ -97,7 +106,7 @@ void GameWorld::Initialize() auto model = AddComponent(ground); model->ModelFile = "Models/TestScene3/testScene.obj"; //model->ModelFile = "Models/Placeholders/Terrain/Terrain2.obj"; - + auto physics = AddComponent(ground); physics->Mass = 10; physics->Static = true; @@ -109,11 +118,34 @@ void GameWorld::Initialize() //meshShape->ResourceName = "Models/Placeholders/Terrain/Terrain2.obj"; meshShape->ResourceName = "Models/TestScene3/testScene.obj"; - + CommitEntity(groundshape); CommitEntity(ground); } + EntityID tank1 = CreateTank(1); + { + auto transform = GetComponent(tank1); + transform->Position.z = 50.f; + + Events::SetViewportCamera e; + e.CameraEntity = GetProperty(tank1, "Camera"); + e.ViewportFrame = "Viewport1"; + EventBroker->Publish(e); + } + + EntityID tank2 = CreateTank(2); + { + auto transform = GetComponent(tank2); + transform->Position.x = 10.f; + transform->Position.z = 50.f; + + Events::SetViewportCamera e; + e.CameraEntity = GetProperty(tank2, "Camera"); + e.ViewportFrame = "Viewport2"; + EventBroker->Publish(e); + } + { auto flag = CreateEntity(); auto transform = AddComponent(flag); @@ -146,1050 +178,22 @@ void GameWorld::Initialize() CommitEntity(flag); } - /*{ - auto jeep = CreateEntity(); - auto transform = AddComponent(jeep); - transform->Position = glm::vec3(0, 5, 0); - transform->Orientation = glm::angleAxis(glm::pi()/2, glm::vec3(0, 1, 0)); - auto physics = AddComponent(jeep); - physics->Mass = 1800; - physics->Static = false; - auto vehicle = AddComponent(jeep); - AddComponent(jeep); - - { - auto shape = CreateEntity(jeep); - auto transform = AddComponent(shape); - auto meshShape = AddComponent(shape); - meshShape->ResourceName = "Models/Jeep/Chassi/ChassiCollision.obj"; - CommitEntity(shape); - - // auto box = AddComponent(jeep); - // box->Width = 1.487f; - // box->Height = 0.727f; - // box->Depth = 2.594f; - - } - - { - auto chassis = CreateEntity(jeep); - auto transform = AddComponent(chassis); - transform->Position = glm::vec3(0, 0, 0); // 0.6577f - auto model = AddComponent(chassis); - 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); - light->constantAttenuation = 0.3f; - light->linearAttenuation = 0.003f; - light->quadraticAttenuation = 0.002f; - } - - - //Create wheels - float wheelOffset = 0.4f; - float springLength = 0.3f; - float suspensionStrength = 35.f; - { - auto wheel = CreateEntity(jeep); - auto transform = AddComponent(wheel); - transform->Position = glm::vec3(1.9f, 0.5546f - wheelOffset, -0.9242f); - transform->Scale = glm::vec3(1.0f); - auto model = AddComponent(wheel); - model->ModelFile = "Models/Jeep/WheelFront/wheelFront.obj"; - auto Wheel = AddComponent(wheel); - Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); - Wheel->AxleID = 0; - Wheel->Mass = 50; - Wheel->Radius = 0.837f; - Wheel->Steering = true; - Wheel->SuspensionStrength = suspensionStrength; - Wheel->Friction = 4.f; - Wheel->ConnectedToHandbrake = true; - CommitEntity(wheel); - } - - { - auto wheel = CreateEntity(jeep); - auto transform = AddComponent(wheel); - transform->Position = glm::vec3(-1.9f, 0.5546f - wheelOffset, -0.9242f); - transform->Scale = glm::vec3(1.0f); - transform->Orientation = glm::angleAxis(glm::pi(), glm::vec3(0, 0, 1)); - auto model = AddComponent(wheel); - model->ModelFile = "Models/Jeep/WheelFront/wheelFront.obj"; - auto Wheel = AddComponent(wheel); - Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); - Wheel->AxleID = 0; - Wheel->Mass = 50; - Wheel->Radius = 0.837f; - Wheel->Steering = true; - Wheel->SuspensionStrength = suspensionStrength; - Wheel->Friction = 4.f; - Wheel->ConnectedToHandbrake = true; - CommitEntity(wheel); - } - - { - auto wheel = CreateEntity(jeep); - auto transform = AddComponent(wheel); - transform->Position = glm::vec3(0.2726f, 0.2805f - wheelOffset, 1.9307f); - auto model = AddComponent(wheel); - model->ModelFile = "Models/Jeep/WheelBack/wheelBack.obj"; - auto Wheel = AddComponent(wheel); - Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); - Wheel->AxleID = 1; - Wheel->Mass = 50; - Wheel->Radius = 0.737f; - Wheel->Steering = false; - Wheel->SuspensionStrength = suspensionStrength; - Wheel->Friction = 4.f; - Wheel->ConnectedToHandbrake = true; - CommitEntity(wheel); - } - - { - auto wheel = CreateEntity(jeep); - auto transform = AddComponent(wheel); - transform->Position = glm::vec3(-0.2726f, 0.2805f - wheelOffset, 1.9307f); - transform->Orientation = glm::angleAxis(glm::pi(), glm::vec3(0, 0, 1)); - auto model = AddComponent(wheel); - model->ModelFile = "Models/Jeep/WheelBack/wheelBack.obj"; - auto Wheel = AddComponent(wheel); - Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); - Wheel->AxleID = 1; - Wheel->Mass = 50; - Wheel->Radius = 0.737f; - Wheel->Steering = false; - Wheel->SuspensionStrength = suspensionStrength; - Wheel->Friction = 4.f; - Wheel->ConnectedToHandbrake = true; - CommitEntity(wheel); - } - - CommitEntity(jeep); - }*/ - - - { - auto tank = CreateEntity(); - auto transform = AddComponent(tank); - transform->Position = glm::vec3(0, 5, 0); - //transform->Orientation = glm::angleAxis(0.f, glm::vec3(0, 1, 0)); - auto physics = AddComponent(tank); - physics->Mass = 63000 - 16000; - physics->Static = false; - physics->CollisionLayer = 2; - - auto vehicle = AddComponent(tank); - vehicle->MaxTorque = 36000.f; - vehicle->MaxSteeringAngle = 90.f; - vehicle->MaxSpeedFullSteeringAngle = 4.f; - auto tankSteering = AddComponent(tank); - tankSteering->Player = player1; - AddComponent(tank); - auto health = AddComponent(tank); - health->health = 100; - { - auto shape = CreateEntity(tank); - auto transform = AddComponent(shape); - auto meshShape = AddComponent(shape); - meshShape->ResourceName = "Models/Tank/Fix/ChassiCollision.obj"; - CommitEntity(shape); - - // auto box = AddComponent(jeep); - // box->Width = 1.487f; - // box->Height = 0.727f; - // box->Depth = 2.594f; - - } - - { - auto chassis = CreateEntity(tank); - auto transform = AddComponent(chassis); - transform->Position = glm::vec3(0, 0, 0); - auto model = AddComponent(chassis); - model->ModelFile = "Models/Tank/tankBody.obj"; - } - { - auto tower = CreateEntity(tank); - SetProperty(tower, "Name", "tower"); - auto transform = AddComponent(tower); - transform->Position = glm::vec3(0.f, 0.68f, 0.9f); - auto model = AddComponent(tower); - model->ModelFile = "Models/Tank/tankTop.obj"; - auto towerSteering = AddComponent(tower); - towerSteering->Axis = glm::vec3(0.f, 1.f, 0.f); - towerSteering->TurnSpeed = glm::pi()/4.f; - { - auto barrel = CreateEntity(tower); - auto transform = AddComponent(barrel); - transform->Position = glm::vec3(-0.012f, 0.4f, -0.75); - auto model = AddComponent(barrel); - model->ModelFile = "Models/Tank/tankBarrel.obj"; - auto barrelSteering = AddComponent(barrel); - barrelSteering->Axis = glm::vec3(1.f, 0.f, 0.f); - barrelSteering->TurnSpeed = glm::pi()/4.f; - barrelSteering->ShotSpeed = 70.f; - { - auto shot = CreateEntity(barrel); - auto transform = AddComponent(shot); - transform->Position = glm::vec3(0.35f, 0.f, -2.f); - transform->Orientation = glm::angleAxis(-glm::pi()/2.f, glm::vec3(1, 0, 0)); - transform->Scale = glm::vec3(3.f); - AddComponent(shot); - auto physics = AddComponent(shot); - physics->Mass = 25.f; - physics->Static = false; - physics->CollisionEvent = true; - auto modelComponent = AddComponent(shot); - modelComponent->ModelFile = "Models/Placeholders/rocket/Rocket.obj"; - auto tankShellComponent = AddComponent(shot); - tankShellComponent->Damage = 20.f; - tankShellComponent->ExplosionRadius = 30.f; - tankShellComponent->ExplosionStrength = 300000.f; - { - auto shape = CreateEntity(shot); - auto transform = AddComponent(shape); - auto boxShape = AddComponent(shape); - boxShape->Width = 0.5f; - boxShape->Height = 0.5f; - boxShape->Depth = 0.5f; - CommitEntity(shape); - } - CommitEntity(shot); - barrelSteering->ShotTemplate = shot; - } - CommitEntity(barrel); - tankSteering->Barrel = barrel; - } - CommitEntity(tower); - tankSteering->Turret = tower; - - - auto cameraTower = CreateEntity(tower); - { - auto transform = AddComponent(cameraTower); - transform->Position.z = 14.f; - transform->Position.y = 4.f; - //transform->Orientation = glm::quat(glm::vec3(glm::pi() / 8.f, 0.f, 0.f)); - auto cameraComp = AddComponent(cameraTower); - cameraComp->FarClip = 2000.f; - //auto freeSteering = AddComponent(cameraTower); - } - CommitEntity(cameraTower); - GetComponent(viewport1)->Camera = cameraTower; - } - - { - auto lightentity = CreateEntity(tank); - auto transform = AddComponent(lightentity); - transform->Position = glm::vec3(0, 0, 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); - /*light->ConstantAttenuation = 0.3f; - light->LinearAttenuation = 0.003f; - light->QuadraticAttenuation = 0.002f;*/ - } - -// auto wheelpair = CreateEntity(tank); -// SetProperty(wheelpair, "Name", "WheelPair"); -// AddComponent(wheelpair, "WheelPairThingy"); - #pragma region Wheels - //Create wheels - float wheelOffset = 0.4f; - float springLength = 0.3f; - float suspensionStrength = 15.f; - - { - auto wheel = CreateEntity(tank); - auto transform = AddComponent(wheel); - transform->Position = glm::vec3(1.68f, -0.83f - wheelOffset, -2.6f); - transform->Orientation = glm::angleAxis(glm::pi(), glm::vec3(0, 1, 0)); - auto model = AddComponent(wheel); - model->ModelFile = "Models/Tank/Fix/WheelPhysics.obj"; - auto Wheel = AddComponent(wheel); - Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); - Wheel->AxleID = 0; - Wheel->Mass = 2000; - Wheel->Radius = 0.6f; - Wheel->Steering = true; - Wheel->SuspensionStrength = suspensionStrength; - Wheel->Friction = 3.f; - Wheel->ConnectedToHandbrake = true; - Wheel->TorqueRatio = 0.125f; - Wheel->Width = 0.6f; - { - auto shape = CreateEntity(wheel); - 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(wheel); - } - { - auto wheel = CreateEntity(tank); - auto transform = AddComponent(wheel); - transform->Position = glm::vec3(1.68f, -0.83f - wheelOffset, -0.83f); - transform->Orientation = glm::angleAxis(glm::pi(), glm::vec3(0, 1, 0)); - auto model = AddComponent(wheel); - model->ModelFile = "Models/Tank/Fix/WheelPhysics.obj"; - auto Wheel = AddComponent(wheel); - Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); - Wheel->AxleID = 0; - Wheel->Mass = 2000; - Wheel->Radius = 0.6f; - Wheel->Steering = false; - Wheel->SuspensionStrength = suspensionStrength; - Wheel->Friction = 3.f; - Wheel->ConnectedToHandbrake = true; - Wheel->TorqueRatio = 0.125f; - Wheel->Width = 0.6f; - { - auto shape = CreateEntity(wheel); - 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(wheel); - } - - { - auto wheel = CreateEntity(tank); - auto transform = AddComponent(wheel); - transform->Position = glm::vec3(-1.68f, -0.83f - wheelOffset, -2.6f); - transform->Orientation = glm::angleAxis(glm::pi(), glm::vec3(0, 1, 0)); - auto model = AddComponent(wheel); - model->ModelFile = "Models/Tank/Fix/WheelPhysics.obj"; - auto Wheel = AddComponent(wheel); - Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); - Wheel->AxleID = 0; - Wheel->Mass = 2000; - Wheel->Radius = 0.6f; - Wheel->Steering = true; - Wheel->SuspensionStrength = suspensionStrength; - Wheel->Friction = 3.f; - Wheel->ConnectedToHandbrake = true; - Wheel->TorqueRatio = 0.125f; - Wheel->Width = 0.6f; - { - auto shape = CreateEntity(wheel); - 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(wheel); - } - { - auto wheel = CreateEntity(tank); - auto transform = AddComponent(wheel); - transform->Position = glm::vec3(-1.68f, -0.83f - wheelOffset, -0.83f); - transform->Orientation = glm::angleAxis(glm::pi(), glm::vec3(0, 1, 0)); - auto model = AddComponent(wheel); - model->ModelFile = "Models/Tank/Fix/WheelPhysics.obj"; - auto Wheel = AddComponent(wheel); - Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); - Wheel->AxleID = 0; - Wheel->Mass = 2000; - Wheel->Radius = 0.6f; - Wheel->Steering = true; - Wheel->SuspensionStrength = suspensionStrength; - Wheel->Friction = 3.f; - Wheel->ConnectedToHandbrake = true; - Wheel->TorqueRatio = 0.125f; - Wheel->Width = 0.6f; - { - auto shape = CreateEntity(wheel); - 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(wheel); - } - - - //Back - { - auto wheel = CreateEntity(tank); - auto transform = AddComponent(wheel); - transform->Position = glm::vec3(1.68f, -0.83f - wheelOffset, 1.f); - auto model = AddComponent(wheel); - model->ModelFile = "Models/Tank/Fix/WheelPhysics.obj"; - auto Wheel = AddComponent(wheel); - Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); - Wheel->AxleID = 1; - Wheel->Mass = 2000; - Wheel->Radius = 0.6f; - Wheel->Steering = false; - Wheel->SuspensionStrength = suspensionStrength; - Wheel->Friction = 3.f; - Wheel->ConnectedToHandbrake = true; - Wheel->TorqueRatio = 0.125f; - Wheel->Width = 0.6f; - { - auto shape = CreateEntity(wheel); - 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(wheel); - } - { - auto wheel = CreateEntity(tank); - auto transform = AddComponent(wheel); - transform->Position = glm::vec3(1.68f, -0.83f - wheelOffset, 2.95f); - auto model = AddComponent(wheel); - model->ModelFile = "Models/Tank/Fix/WheelPhysics.obj"; - auto Wheel = AddComponent(wheel); - Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); - Wheel->AxleID = 1; - Wheel->Mass = 2000; - Wheel->Radius = 0.6f; - Wheel->Steering = false; - Wheel->SuspensionStrength = suspensionStrength; - Wheel->Friction = 3.f; - Wheel->ConnectedToHandbrake = true; - Wheel->TorqueRatio = 0.125f; - Wheel->Width = 0.6f; - { - auto shape = CreateEntity(wheel); - 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(wheel); - - /*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 = 10; - emitterComponent->SpreadAngle = glm::pi(); - emitterComponent->UseGoalVelocity = false; - emitterComponent->LifeTime = 0.5; - emitterComponent->Speed = 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 wheel = CreateEntity(tank); - auto transform = AddComponent(wheel); - transform->Position = glm::vec3(-1.68f, -0.83f - wheelOffset, 1.f); - transform->Orientation = glm::angleAxis(glm::pi(), glm::vec3(0, 1, 0)); - auto model = AddComponent(wheel); - model->ModelFile = "Models/Tank/Fix/WheelPhysics.obj"; - auto Wheel = AddComponent(wheel); - Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); - Wheel->AxleID = 1; - Wheel->Mass = 2000; - Wheel->Radius = 0.6f; - Wheel->Steering = false; - Wheel->SuspensionStrength = suspensionStrength; - Wheel->Friction = 3.f; - Wheel->ConnectedToHandbrake = true; - Wheel->TorqueRatio = 0.125f; - Wheel->Width = 0.6f; - { - auto shape = CreateEntity(wheel); - 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(wheel); - } - { - auto wheel = CreateEntity(tank); - auto transform = AddComponent(wheel); - transform->Position = glm::vec3(-1.68f, -0.83f - wheelOffset, 2.95f); - auto model = AddComponent(wheel); - model->ModelFile = "Models/Tank/Fix/WheelPhysics.obj"; - auto Wheel = AddComponent(wheel); - Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); - Wheel->AxleID = 1; - Wheel->Mass = 2000; - Wheel->Radius = 0.6f; - Wheel->Steering = false; - Wheel->SuspensionStrength = suspensionStrength; - Wheel->Friction = 3.f; - Wheel->ConnectedToHandbrake = true; - Wheel->TorqueRatio = 0.125f; - Wheel->Width = 0.6f; - { - auto shape = CreateEntity(wheel); - 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(wheel); - - /*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->Speed = 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);*/ - } -#pragma endregion - - CommitEntity(tank); - } - - { - auto tank = CreateEntity(); - auto transform = AddComponent(tank); - transform->Position = glm::vec3(20, 5, 0); - //transform->Orientation = glm::angleAxis(0.f, glm::vec3(0, 1, 0)); - auto physics = AddComponent(tank); - physics->Mass = 63000 - 16000; - physics->Static = false; - physics->CollisionLayer = 3; - auto vehicle = AddComponent(tank); - vehicle->MaxTorque = 36000.f; - vehicle->MaxSteeringAngle = 90.f; - vehicle->MaxSpeedFullSteeringAngle = 4.f; - auto tankSteering = AddComponent(tank); - tankSteering->Player = player2; - AddComponent(tank); - auto health = AddComponent(tank); - health->health = 100; - { - auto shape = CreateEntity(tank); - auto transform = AddComponent(shape); - auto meshShape = AddComponent(shape); - meshShape->ResourceName = "Models/Tank/Fix/ChassiCollision.obj"; - CommitEntity(shape); - - // auto box = AddComponent(jeep); - // box->Width = 1.487f; - // box->Height = 0.727f; - // box->Depth = 2.594f; - - } - - { - auto chassis = CreateEntity(tank); - auto transform = AddComponent(chassis); - transform->Position = glm::vec3(0, 0, 0); - auto model = AddComponent(chassis); - model->ModelFile = "Models/Tank/tankBody.obj"; - } - { - auto tower = CreateEntity(tank); - SetProperty(tower, "Name", "tower"); - auto transform = AddComponent(tower); - transform->Position = glm::vec3(0.f, 0.68f, 0.9f); - auto model = AddComponent(tower); - model->ModelFile = "Models/Tank/tankTop.obj"; - auto towerSteering = AddComponent(tower); - towerSteering->Axis = glm::vec3(0.f, 1.f, 0.f); - towerSteering->TurnSpeed = glm::pi()/4.f; - { - auto barrel = CreateEntity(tower); - auto transform = AddComponent(barrel); - transform->Position = glm::vec3(-0.012f, 0.4f, -0.75); - auto model = AddComponent(barrel); - model->ModelFile = "Models/Tank/tankBarrel.obj"; - auto barrelSteering = AddComponent(barrel); - barrelSteering->Axis = glm::vec3(1.f, 0.f, 0.f); - barrelSteering->TurnSpeed = glm::pi()/4.f; - barrelSteering->ShotSpeed = 70.f; - { - auto shot = CreateEntity(barrel); - auto transform = AddComponent(shot); - transform->Position = glm::vec3(0.35f, 0.f, -2.f); - transform->Orientation = glm::angleAxis(-glm::pi()/2.f, glm::vec3(1, 0, 0)); - transform->Scale = glm::vec3(3.f); - AddComponent(shot); - auto physics = AddComponent(shot); - physics->Mass = 25.f; - physics->Static = false; - physics->CollisionEvent = true; - auto modelComponent = AddComponent(shot); - modelComponent->ModelFile = "Models/Placeholders/rocket/Rocket.obj"; - auto tankShellComponent = AddComponent(shot); - tankShellComponent->Damage = 20.f; - tankShellComponent->ExplosionRadius = 30.f; - tankShellComponent->ExplosionStrength = 300000.f; - { - auto shape = CreateEntity(shot); - auto transform = AddComponent(shape); - auto boxShape = AddComponent(shape); - boxShape->Width = 0.5f; - boxShape->Height = 0.5f; - boxShape->Depth = 0.5f; - CommitEntity(shape); - } - CommitEntity(shot); - barrelSteering->ShotTemplate = shot; - } - CommitEntity(barrel); - tankSteering->Barrel = barrel; - } - CommitEntity(tower); - tankSteering->Turret = tower; - - - auto cameraTower = CreateEntity(tower); - { - auto transform = AddComponent(cameraTower); - transform->Position.z = 14.f; - transform->Position.y = 4.f; - //transform->Orientation = glm::quat(glm::vec3(glm::pi() / 8.f, 0.f, 0.f)); - auto cameraComp = AddComponent(cameraTower); - cameraComp->FarClip = 2000.f; - //auto freeSteering = AddComponent(cameraTower); - } - CommitEntity(cameraTower); - GetComponent(viewport2)->Camera = cameraTower; - } - - { - auto lightentity = CreateEntity(tank); - auto transform = AddComponent(lightentity); - transform->Position = glm::vec3(0, 0, 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); - /*light->ConstantAttenuation = 0.3f; - light->LinearAttenuation = 0.003f; - light->QuadraticAttenuation = 0.002f;*/ - } - -// auto wheelpair = CreateEntity(tank); -// SetProperty(wheelpair, "Name", "WheelPair"); -// AddComponent(wheelpair, "WheelPairThingy"); - #pragma region Wheels - - //Create wheels - float wheelOffset = 0.4f; - float springLength = 0.3f; - float suspensionStrength = 15.f; - - { - auto wheel = CreateEntity(tank); - auto transform = AddComponent(wheel); - transform->Position = glm::vec3(1.68f, -0.83f - wheelOffset, -2.6f); - transform->Orientation = glm::angleAxis(glm::pi(), glm::vec3(0, 1, 0)); - auto model = AddComponent(wheel); - model->ModelFile = "Models/Tank/Fix/WheelPhysics.obj"; - auto Wheel = AddComponent(wheel); - Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); - Wheel->AxleID = 0; - Wheel->Mass = 2000; - Wheel->Radius = 0.6f; - Wheel->Steering = true; - Wheel->SuspensionStrength = suspensionStrength; - Wheel->Friction = 3.f; - Wheel->ConnectedToHandbrake = true; - Wheel->TorqueRatio = 0.125f; - Wheel->Width = 0.6f; - { - auto shape = CreateEntity(wheel); - 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(wheel); - } - { - auto wheel = CreateEntity(tank); - auto transform = AddComponent(wheel); - transform->Position = glm::vec3(1.68f, -0.83f - wheelOffset, -0.83f); - transform->Orientation = glm::angleAxis(glm::pi(), glm::vec3(0, 1, 0)); - auto model = AddComponent(wheel); - model->ModelFile = "Models/Tank/Fix/WheelPhysics.obj"; - auto Wheel = AddComponent(wheel); - Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); - Wheel->AxleID = 0; - Wheel->Mass = 2000; - Wheel->Radius = 0.6f; - Wheel->Steering = false; - Wheel->SuspensionStrength = suspensionStrength; - Wheel->Friction = 3.f; - Wheel->ConnectedToHandbrake = true; - Wheel->TorqueRatio = 0.125f; - Wheel->Width = 0.6f; - { - auto shape = CreateEntity(wheel); - 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(wheel); - } - - { - auto wheel = CreateEntity(tank); - auto transform = AddComponent(wheel); - transform->Position = glm::vec3(-1.68f, -0.83f - wheelOffset, -2.6f); - transform->Orientation = glm::angleAxis(glm::pi(), glm::vec3(0, 1, 0)); - auto model = AddComponent(wheel); - model->ModelFile = "Models/Tank/Fix/WheelPhysics.obj"; - auto Wheel = AddComponent(wheel); - Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); - Wheel->AxleID = 0; - Wheel->Mass = 2000; - Wheel->Radius = 0.6f; - Wheel->Steering = true; - Wheel->SuspensionStrength = suspensionStrength; - Wheel->Friction = 3.f; - Wheel->ConnectedToHandbrake = true; - Wheel->TorqueRatio = 0.125f; - Wheel->Width = 0.6f; - { - auto shape = CreateEntity(wheel); - 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(wheel); - } - { - auto wheel = CreateEntity(tank); - auto transform = AddComponent(wheel); - transform->Position = glm::vec3(-1.68f, -0.83f - wheelOffset, -0.83f); - transform->Orientation = glm::angleAxis(glm::pi(), glm::vec3(0, 1, 0)); - auto model = AddComponent(wheel); - model->ModelFile = "Models/Tank/Fix/WheelPhysics.obj"; - auto Wheel = AddComponent(wheel); - Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); - Wheel->AxleID = 0; - Wheel->Mass = 2000; - Wheel->Radius = 0.6f; - Wheel->Steering = true; - Wheel->SuspensionStrength = suspensionStrength; - Wheel->Friction = 3.f; - Wheel->ConnectedToHandbrake = true; - Wheel->TorqueRatio = 0.125f; - Wheel->Width = 0.6f; - { - auto shape = CreateEntity(wheel); - 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(wheel); - } - - - //Back - { - auto wheel = CreateEntity(tank); - auto transform = AddComponent(wheel); - transform->Position = glm::vec3(1.68f, -0.83f - wheelOffset, 1.f); - auto model = AddComponent(wheel); - model->ModelFile = "Models/Tank/Fix/WheelPhysics.obj"; - auto Wheel = AddComponent(wheel); - Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); - Wheel->AxleID = 1; - Wheel->Mass = 2000; - Wheel->Radius = 0.6f; - Wheel->Steering = false; - Wheel->SuspensionStrength = suspensionStrength; - Wheel->Friction = 3.f; - Wheel->ConnectedToHandbrake = true; - Wheel->TorqueRatio = 0.125f; - Wheel->Width = 0.6f; - { - auto shape = CreateEntity(wheel); - 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(wheel); - } - { - auto wheel = CreateEntity(tank); - auto transform = AddComponent(wheel); - transform->Position = glm::vec3(1.68f, -0.83f - wheelOffset, 2.95f); - auto model = AddComponent(wheel); - model->ModelFile = "Models/Tank/Fix/WheelPhysics.obj"; - auto Wheel = AddComponent(wheel); - Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); - Wheel->AxleID = 1; - Wheel->Mass = 2000; - Wheel->Radius = 0.6f; - Wheel->Steering = false; - Wheel->SuspensionStrength = suspensionStrength; - Wheel->Friction = 3.f; - Wheel->ConnectedToHandbrake = true; - Wheel->TorqueRatio = 0.125f; - Wheel->Width = 0.6f; - { - auto shape = CreateEntity(wheel); - 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(wheel); - - /*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 wheel = CreateEntity(tank); - auto transform = AddComponent(wheel); - transform->Position = glm::vec3(-1.68f, -0.83f - wheelOffset, 1.f); - transform->Orientation = glm::angleAxis(glm::pi(), glm::vec3(0, 1, 0)); - auto model = AddComponent(wheel); - model->ModelFile = "Models/Tank/Fix/WheelPhysics.obj"; - auto Wheel = AddComponent(wheel); - Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); - Wheel->AxleID = 1; - Wheel->Mass = 2000; - Wheel->Radius = 0.6f; - Wheel->Steering = false; - Wheel->SuspensionStrength = suspensionStrength; - Wheel->Friction = 3.f; - Wheel->ConnectedToHandbrake = true; - Wheel->TorqueRatio = 0.125f; - Wheel->Width = 0.6f; - { - auto shape = CreateEntity(wheel); - 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(wheel); - } - { - auto wheel = CreateEntity(tank); - auto transform = AddComponent(wheel); - transform->Position = glm::vec3(-1.68f, -0.83f - wheelOffset, 2.95f); - auto model = AddComponent(wheel); - model->ModelFile = "Models/Tank/Fix/WheelPhysics.obj"; - auto Wheel = AddComponent(wheel); - Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); - Wheel->AxleID = 1; - Wheel->Mass = 2000; - Wheel->Radius = 0.6f; - Wheel->Steering = false; - Wheel->SuspensionStrength = suspensionStrength; - Wheel->Friction = 3.f; - Wheel->ConnectedToHandbrake = true; - Wheel->TorqueRatio = 0.125f; - Wheel->Width = 0.6f; - { - auto shape = CreateEntity(wheel); - 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(wheel); - - /*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);*/ - } -#pragma endregion - CommitEntity(tank); - } - - /* - for(int i = 0; i < 10; i++) - { - auto entity = CreateEntity(); - auto transform = AddComponent(entity); - transform->Position = glm::vec3(30 + i*0.1f, 0 + i*0.1f, 10 + i*0.1f); - transform->Scale = glm::vec3(0); - transform->Orientation = glm::quat(glm::vec3(0.0f, 0.0f, 0.0f)); - - std::stringstream ss; - ss << "Models/Placeholders/ShatterTest/" << i+1 << ".obj"; - - auto model = AddComponent(entity); - model->ModelFile = ss.str(); - - auto physics = AddComponent(entity); - physics->Mass = 100; - physics->Static = true; - auto meshShape = AddComponent(entity); - meshShape->ResourceName = ss.str(); - - CommitEntity(entity); - }*/ - - - for (int y = 0; y < 5; y++) - { - auto cube = CreateEntity(); - auto transform = AddComponent(cube); - transform->Position = glm::vec3(0, 10*y, 50); - transform->Scale = glm::vec3(3); - transform->Orientation = glm::quat(glm::vec3(0.0f, 0.0f, 0.0f)); - auto model = AddComponent(cube); - model->ModelFile = "Models/Placeholders/PhysicsTest/Cube2.obj"; - - auto physics = AddComponent(cube); - physics->Mass = 100; - - { - auto shape = CreateEntity(cube); - auto transform = AddComponent(shape); - auto box = AddComponent(shape); - box->Width = 1.5f; - box->Height = 1.5f; - box->Depth = 1.5f; - CommitEntity(shape); - } - CommitEntity(cube); - } - - - - /*{ - auto entity = CreateEntity(); - AddComponent(entity, "Transform"); - auto emitter = AddComponent(entity); - emitter->Path = "Sounds/korvring.wav"; - emitter->Loop = true; - GetSystem("SoundSystem")->PlaySound(emitter); - CommitEntity(entity); - }*/ + //for (int i = 0; i < 500; i++) + //{ + // auto Light = CreateEntity(); + // auto transform = AddComponent(Light); + // transform->Position = glm::vec3((5 + (i*2.f))*cos(i*2.f), 3.f, (5 + (i*2.f))*sin(i*2.f)); + // auto light = AddComponent(Light); + // light->Specular = glm::vec3(0.5f, 0.5f, 0.5f); + // light->Diffuse = glm::vec3(0.5f, 0.5f, 0.5f); + // light->Radius = 15.f; + // light->specularExponent = 100.f; + // CommitEntity(Light); + // //auto model = AddComponent(Light, "Model"); + // //model->ModelFile = "Models/Placeholders/PhysicsTest/PointLight.obj"; + //} + } void GameWorld::Update(double dt) @@ -1201,27 +205,27 @@ void GameWorld::RegisterComponents() { m_ComponentFactory.Register([]() { return new Components::Transform(); }); m_ComponentFactory.Register([]() { return new Components::Template(); }); - m_ComponentFactory.Register([]() { return new Components::Player(); }); - m_ComponentFactory.Register([]() { return new Components::Flag(); }); + m_ComponentFactory.Register([]() { return new Components::Player(); }); + m_ComponentFactory.Register([]() { return new Components::Flag(); }); } void GameWorld::RegisterSystems() { - m_SystemFactory.Register([this]() { return new Systems::TimerSystem(this, m_EventBroker); }); - m_SystemFactory.Register([this]() { return new Systems::DamageSystem(this, m_EventBroker); }); - m_SystemFactory.Register([this]() { return new Systems::TransformSystem(this, m_EventBroker); }); + m_SystemFactory.Register([this]() { return new Systems::TimerSystem(this, EventBroker, ResourceManager); }); + m_SystemFactory.Register([this]() { return new Systems::DamageSystem(this, EventBroker, ResourceManager); }); + m_SystemFactory.Register([this]() { return new Systems::TransformSystem(this, EventBroker, ResourceManager); }); //m_SystemFactory.Register([this]() { return new Systems::LevelGenerationSystem(this); }); - m_SystemFactory.Register([this]() { return new Systems::InputSystem(this, m_EventBroker); }); - m_SystemFactory.Register([this]() { return new Systems::DebugSystem(this, m_EventBroker); }); + m_SystemFactory.Register([this]() { return new Systems::InputSystem(this, EventBroker, ResourceManager); }); + m_SystemFactory.Register([this]() { return new Systems::DebugSystem(this, EventBroker, ResourceManager); }); //m_SystemFactory.Register([this]() { return new Systems::CollisionSystem(this); }); - m_SystemFactory.Register([this]() { return new Systems::ParticleSystem(this, m_EventBroker); }); + m_SystemFactory.Register([this]() { return new Systems::ParticleSystem(this, EventBroker, ResourceManager); }); //m_SystemFactory.Register([this]() { return new Systems::PlayerSystem(this); }); - m_SystemFactory.Register([this]() { return new Systems::FreeSteeringSystem(this, m_EventBroker); }); - m_SystemFactory.Register([this]() { return new Systems::TankSteeringSystem(this, m_EventBroker); }); - m_SystemFactory.Register([this]() { return new Systems::SoundSystem(this, m_EventBroker); }); - m_SystemFactory.Register([this]() { return new Systems::PhysicsSystem(this, m_EventBroker); }); - m_SystemFactory.Register([this]() { return new Systems::TriggerSystem(this, m_EventBroker); }); - m_SystemFactory.Register([this]() { return new Systems::RenderSystem(this, m_EventBroker, m_Renderer); }); + m_SystemFactory.Register([this]() { return new Systems::FreeSteeringSystem(this, EventBroker, ResourceManager); }); + m_SystemFactory.Register([this]() { return new Systems::TankSteeringSystem(this, EventBroker, ResourceManager); }); + m_SystemFactory.Register([this]() { return new Systems::SoundSystem(this, EventBroker, ResourceManager); }); + m_SystemFactory.Register([this]() { return new Systems::PhysicsSystem(this, EventBroker, ResourceManager); }); + m_SystemFactory.Register([this]() { return new Systems::TriggerSystem(this, EventBroker, ResourceManager); }); + m_SystemFactory.Register([this]() { return new Systems::RenderSystem(this, EventBroker, ResourceManager); }); } void GameWorld::AddSystems() @@ -1249,7 +253,7 @@ void GameWorld::BindKey(int keyCode, std::string command, float value) e.KeyCode = keyCode; e.Command = command; e.Value = value; - m_EventBroker->Publish(e); + EventBroker->Publish(e); } void GameWorld::BindMouseButton(int button, std::string command, float value) @@ -1258,7 +262,7 @@ void GameWorld::BindMouseButton(int button, std::string command, float value) e.Button = button; e.Command = command; e.Value = value; - m_EventBroker->Publish(e); + EventBroker->Publish(e); } void GameWorld::BindGamepadAxis(Gamepad::Axis axis, std::string command, float value) @@ -1267,7 +271,7 @@ void GameWorld::BindGamepadAxis(Gamepad::Axis axis, std::string command, float v e.Axis = axis; e.Command = command; e.Value = value; - m_EventBroker->Publish(e); + EventBroker->Publish(e); } void GameWorld::BindGamepadButton(Gamepad::Button button, std::string command, float value) @@ -1276,5 +280,571 @@ void GameWorld::BindGamepadButton(Gamepad::Button button, std::string command, f e.Button = button; e.Command = command; e.Value = value; - m_EventBroker->Publish(e); + EventBroker->Publish(e); +} + +EntityID GameWorld::CreateTank(int playerID) +{ + auto playerEnt = CreateEntity(); + { + auto player = AddComponent(playerEnt); + player->ID = playerID; + } + + auto tank = CreateEntity(); + auto transform = AddComponent(tank); + transform->Position = glm::vec3(0, 5, 0); + //transform->Orientation = glm::angleAxis(0.f, glm::vec3(0, 1, 0)); + auto physics = AddComponent(tank); + physics->Mass = 63000 - 16000; + physics->Static = false; + auto vehicle = AddComponent(tank); + vehicle->MaxTorque = 36000.f; + vehicle->MaxSteeringAngle = 90.f; + vehicle->MaxSpeedFullSteeringAngle = 4.f; + auto player = AddComponent(tank); + player->ID = playerID; + auto tankSteering = AddComponent(tank); + tankSteering->Player = playerEnt; + AddComponent(tank); + auto health = AddComponent(tank); + health->Amount = 100.f; + + { + auto shape = CreateEntity(tank); + auto transform = AddComponent(shape); + auto meshShape = AddComponent(shape); + meshShape->ResourceName = "Models/Tank/Fix/ChassiCollision.obj"; + CommitEntity(shape); + + // auto box = AddComponent(jeep); + // box->Width = 1.487f; + // box->Height = 0.727f; + // box->Depth = 2.594f; + + } + + { + auto chassis = CreateEntity(tank); + auto transform = AddComponent(chassis); + transform->Position = glm::vec3(0, 0, 0); + auto model = AddComponent(chassis); + model->ModelFile = "Models/Tank/tankBody.obj"; + } + { + auto tower = CreateEntity(tank); + SetProperty(tower, "Name", "tower"); + auto transform = AddComponent(tower); + transform->Position = glm::vec3(0.f, 0.68f, 0.9f); + auto model = AddComponent(tower); + model->ModelFile = "Models/Tank/tankTop.obj"; + auto towerSteering = AddComponent(tower); + towerSteering->Axis = glm::vec3(0.f, 1.f, 0.f); + towerSteering->TurnSpeed = glm::pi() / 4.f; + { + auto barrel = CreateEntity(tower); + auto transform = AddComponent(barrel); + transform->Position = glm::vec3(-0.012f, 0.4f, -0.75); + auto model = AddComponent(barrel); + model->ModelFile = "Models/Tank/tankBarrel.obj"; + auto barrelSteering = AddComponent(barrel); + barrelSteering->Axis = glm::vec3(1.f, 0.f, 0.f); + barrelSteering->TurnSpeed = glm::pi() / 4.f; + barrelSteering->ShotSpeed = 70.f; + { + auto shot = CreateEntity(barrel); + auto transform = AddComponent(shot); + transform->Position = glm::vec3(0.35f, 0.f, -2.f); + transform->Orientation = glm::angleAxis(-glm::pi() / 2.f, glm::vec3(1, 0, 0)); + transform->Scale = glm::vec3(3.f); + AddComponent(shot); + auto physics = AddComponent(shot); + physics->Mass = 25.f; + physics->Static = false; + physics->CollisionEvent = true; + auto modelComponent = AddComponent(shot); + modelComponent->ModelFile = "Models/Placeholders/rocket/Rocket.obj"; + auto tankShellComponent = AddComponent(shot); + tankShellComponent->Damage = 20.f; + tankShellComponent->ExplosionRadius = 30.f; + tankShellComponent->ExplosionStrength = 300000.f; + { + auto shape = CreateEntity(shot); + auto transform = AddComponent(shape); + auto boxShape = AddComponent(shape); + boxShape->Width = 0.5f; + boxShape->Height = 0.5f; + boxShape->Depth = 0.5f; + CommitEntity(shape); + } + CommitEntity(shot); + barrelSteering->ShotTemplate = shot; + } + CommitEntity(barrel); + tankSteering->Barrel = barrel; + } + CommitEntity(tower); + tankSteering->Turret = tower; + + + auto cameraTower = CreateEntity(tower); + { + auto transform = AddComponent(cameraTower); + transform->Position.z = 16.f; + transform->Position.y = 4.f; + //transform->Orientation = glm::quat(glm::vec3(glm::pi() / 8.f, 0.f, 0.f)); + auto cameraComp = AddComponent(cameraTower); + cameraComp->FarClip = 2000.f; + //auto freeSteering = AddComponent(cameraTower); + } + + SetProperty(tank, "Camera", cameraTower); + } + + //{ + // auto lightentity = CreateEntity(tank); + // auto transform = AddComponent(lightentity); + // transform->Position = glm::vec3(0, 0, 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); + // /*light->ConstantAttenuation = 0.3f; + // light->LinearAttenuation = 0.003f; + // light->QuadraticAttenuation = 0.002f;*/ + //} + + // auto wheelpair = CreateEntity(tank); + // SetProperty(wheelpair, "Name", "WheelPair"); + // AddComponent(wheelpair, "WheelPairThingy"); + +#pragma region Wheels + //Create wheels + float wheelOffset = 0.4f; + float springLength = 0.3f; + float suspensionStrength = 15.f; + + { + auto wheel = CreateEntity(tank); + auto transform = AddComponent(wheel); + transform->Position = glm::vec3(1.68f, -0.83f - wheelOffset, -2.6f); + transform->Orientation = glm::angleAxis(glm::pi(), glm::vec3(0, 1, 0)); + auto model = AddComponent(wheel); + model->ModelFile = "Models/Tank/Fix/WheelPhysics.obj"; + auto Wheel = AddComponent(wheel); + Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); + Wheel->AxleID = 0; + Wheel->Mass = 2000; + Wheel->Radius = 0.6f; + Wheel->Steering = true; + Wheel->SuspensionStrength = suspensionStrength; + Wheel->Friction = 3.f; + Wheel->ConnectedToHandbrake = true; + Wheel->TorqueRatio = 0.125f; + Wheel->Width = 0.6f; + { + auto shape = CreateEntity(wheel); + 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(wheel); + } + { + auto wheel = CreateEntity(tank); + auto transform = AddComponent(wheel); + transform->Position = glm::vec3(1.68f, -0.83f - wheelOffset, -0.83f); + transform->Orientation = glm::angleAxis(glm::pi(), glm::vec3(0, 1, 0)); + auto model = AddComponent(wheel); + model->ModelFile = "Models/Tank/Fix/WheelPhysics.obj"; + auto Wheel = AddComponent(wheel); + Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); + Wheel->AxleID = 0; + Wheel->Mass = 2000; + Wheel->Radius = 0.6f; + Wheel->Steering = false; + Wheel->SuspensionStrength = suspensionStrength; + Wheel->Friction = 3.f; + Wheel->ConnectedToHandbrake = true; + Wheel->TorqueRatio = 0.125f; + Wheel->Width = 0.6f; + { + auto shape = CreateEntity(wheel); + 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(wheel); + } + + { + auto wheel = CreateEntity(tank); + auto transform = AddComponent(wheel); + transform->Position = glm::vec3(-1.68f, -0.83f - wheelOffset, -2.6f); + transform->Orientation = glm::angleAxis(glm::pi(), glm::vec3(0, 1, 0)); + auto model = AddComponent(wheel); + model->ModelFile = "Models/Tank/Fix/WheelPhysics.obj"; + auto Wheel = AddComponent(wheel); + Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); + Wheel->AxleID = 0; + Wheel->Mass = 2000; + Wheel->Radius = 0.6f; + Wheel->Steering = true; + Wheel->SuspensionStrength = suspensionStrength; + Wheel->Friction = 3.f; + Wheel->ConnectedToHandbrake = true; + Wheel->TorqueRatio = 0.125f; + Wheel->Width = 0.6f; + { + auto shape = CreateEntity(wheel); + 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(wheel); + } + { + auto wheel = CreateEntity(tank); + auto transform = AddComponent(wheel); + transform->Position = glm::vec3(-1.68f, -0.83f - wheelOffset, -0.83f); + transform->Orientation = glm::angleAxis(glm::pi(), glm::vec3(0, 1, 0)); + auto model = AddComponent(wheel); + model->ModelFile = "Models/Tank/Fix/WheelPhysics.obj"; + auto Wheel = AddComponent(wheel); + Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); + Wheel->AxleID = 0; + Wheel->Mass = 2000; + Wheel->Radius = 0.6f; + Wheel->Steering = true; + Wheel->SuspensionStrength = suspensionStrength; + Wheel->Friction = 3.f; + Wheel->ConnectedToHandbrake = true; + Wheel->TorqueRatio = 0.125f; + Wheel->Width = 0.6f; + { + auto shape = CreateEntity(wheel); + 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(wheel); + } + + + //Back + { + auto wheel = CreateEntity(tank); + auto transform = AddComponent(wheel); + transform->Position = glm::vec3(1.68f, -0.83f - wheelOffset, 1.f); + auto model = AddComponent(wheel); + model->ModelFile = "Models/Tank/Fix/WheelPhysics.obj"; + auto Wheel = AddComponent(wheel); + Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); + Wheel->AxleID = 1; + Wheel->Mass = 2000; + Wheel->Radius = 0.6f; + Wheel->Steering = false; + Wheel->SuspensionStrength = suspensionStrength; + Wheel->Friction = 3.f; + Wheel->ConnectedToHandbrake = true; + Wheel->TorqueRatio = 0.125f; + Wheel->Width = 0.6f; + { + auto shape = CreateEntity(wheel); + 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(wheel); + } + { + auto wheel = CreateEntity(tank); + auto transform = AddComponent(wheel); + transform->Position = glm::vec3(1.68f, -0.83f - wheelOffset, 2.95f); + auto model = AddComponent(wheel); + model->ModelFile = "Models/Tank/Fix/WheelPhysics.obj"; + auto Wheel = AddComponent(wheel); + Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); + Wheel->AxleID = 1; + Wheel->Mass = 2000; + Wheel->Radius = 0.6f; + Wheel->Steering = false; + Wheel->SuspensionStrength = suspensionStrength; + Wheel->Friction = 3.f; + Wheel->ConnectedToHandbrake = true; + Wheel->TorqueRatio = 0.125f; + Wheel->Width = 0.6f; + { + auto shape = CreateEntity(wheel); + 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(wheel); + + 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 wheel = CreateEntity(tank); + auto transform = AddComponent(wheel); + transform->Position = glm::vec3(-1.68f, -0.83f - wheelOffset, 1.f); + transform->Orientation = glm::angleAxis(glm::pi(), glm::vec3(0, 1, 0)); + auto model = AddComponent(wheel); + model->ModelFile = "Models/Tank/Fix/WheelPhysics.obj"; + auto Wheel = AddComponent(wheel); + Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); + Wheel->AxleID = 1; + Wheel->Mass = 2000; + Wheel->Radius = 0.6f; + Wheel->Steering = false; + Wheel->SuspensionStrength = suspensionStrength; + Wheel->Friction = 3.f; + Wheel->ConnectedToHandbrake = true; + Wheel->TorqueRatio = 0.125f; + Wheel->Width = 0.6f; + { + auto shape = CreateEntity(wheel); + 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(wheel); + } + { + auto wheel = CreateEntity(tank); + auto transform = AddComponent(wheel); + transform->Position = glm::vec3(-1.68f, -0.83f - wheelOffset, 2.95f); + auto model = AddComponent(wheel); + model->ModelFile = "Models/Tank/Fix/WheelPhysics.obj"; + auto Wheel = AddComponent(wheel); + Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); + Wheel->AxleID = 1; + Wheel->Mass = 2000; + Wheel->Radius = 0.6f; + Wheel->Steering = false; + Wheel->SuspensionStrength = suspensionStrength; + Wheel->Friction = 3.f; + Wheel->ConnectedToHandbrake = true; + Wheel->TorqueRatio = 0.125f; + Wheel->Width = 0.6f; + { + auto shape = CreateEntity(wheel); + 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(wheel); +#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); + } + + CommitEntity(tank); + + return tank; +} + +EntityID GameWorld::CreateJeep(int playerID) +{ + auto jeep = CreateEntity(); + auto transform = AddComponent(jeep); + transform->Position = glm::vec3(0, 5, 0); + transform->Orientation = glm::angleAxis(glm::pi() / 2, glm::vec3(0, 1, 0)); + auto physics = AddComponent(jeep); + physics->Mass = 1800; + physics->Static = false; + auto vehicle = AddComponent(jeep); + AddComponent(jeep); + + { + auto shape = CreateEntity(jeep); + auto transform = AddComponent(shape); + auto meshShape = AddComponent(shape); + meshShape->ResourceName = "Models/Jeep/Chassi/ChassiCollision.obj"; + CommitEntity(shape); + + // auto box = AddComponent(jeep); + // box->Width = 1.487f; + // box->Height = 0.727f; + // box->Depth = 2.594f; + + } + + { + auto chassis = CreateEntity(jeep); + auto transform = AddComponent(chassis); + transform->Position = glm::vec3(0, 0, 0); // 0.6577f + auto model = AddComponent(chassis); + 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; + float suspensionStrength = 35.f; + { + auto wheel = CreateEntity(jeep); + auto transform = AddComponent(wheel); + transform->Position = glm::vec3(1.9f, 0.5546f - wheelOffset, -0.9242f); + transform->Scale = glm::vec3(1.0f); + auto model = AddComponent(wheel); + model->ModelFile = "Models/Jeep/WheelFront/wheelFront.obj"; + auto Wheel = AddComponent(wheel); + Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); + Wheel->AxleID = 0; + Wheel->Mass = 50; + Wheel->Radius = 0.837f; + Wheel->Steering = true; + Wheel->SuspensionStrength = suspensionStrength; + Wheel->Friction = 4.f; + Wheel->ConnectedToHandbrake = true; + CommitEntity(wheel); + } + + { + auto wheel = CreateEntity(jeep); + auto transform = AddComponent(wheel); + transform->Position = glm::vec3(-1.9f, 0.5546f - wheelOffset, -0.9242f); + transform->Scale = glm::vec3(1.0f); + transform->Orientation = glm::angleAxis(glm::pi(), glm::vec3(0, 0, 1)); + auto model = AddComponent(wheel); + model->ModelFile = "Models/Jeep/WheelFront/wheelFront.obj"; + auto Wheel = AddComponent(wheel); + Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); + Wheel->AxleID = 0; + Wheel->Mass = 50; + Wheel->Radius = 0.837f; + Wheel->Steering = true; + Wheel->SuspensionStrength = suspensionStrength; + Wheel->Friction = 4.f; + Wheel->ConnectedToHandbrake = true; + CommitEntity(wheel); + } + + { + auto wheel = CreateEntity(jeep); + auto transform = AddComponent(wheel); + transform->Position = glm::vec3(0.2726f, 0.2805f - wheelOffset, 1.9307f); + auto model = AddComponent(wheel); + model->ModelFile = "Models/Jeep/WheelBack/wheelBack.obj"; + auto Wheel = AddComponent(wheel); + Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); + Wheel->AxleID = 1; + Wheel->Mass = 50; + Wheel->Radius = 0.737f; + Wheel->Steering = false; + Wheel->SuspensionStrength = suspensionStrength; + Wheel->Friction = 4.f; + Wheel->ConnectedToHandbrake = true; + CommitEntity(wheel); + } + + { + auto wheel = CreateEntity(jeep); + auto transform = AddComponent(wheel); + transform->Position = glm::vec3(-0.2726f, 0.2805f - wheelOffset, 1.9307f); + transform->Orientation = glm::angleAxis(glm::pi(), glm::vec3(0, 0, 1)); + auto model = AddComponent(wheel); + model->ModelFile = "Models/Jeep/WheelBack/wheelBack.obj"; + auto Wheel = AddComponent(wheel); + Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); + Wheel->AxleID = 1; + Wheel->Mass = 50; + Wheel->Radius = 0.737f; + Wheel->Steering = false; + Wheel->SuspensionStrength = suspensionStrength; + Wheel->Friction = 4.f; + Wheel->ConnectedToHandbrake = true; + CommitEntity(wheel); + } + + CommitEntity(jeep); + + return jeep; } diff --git a/src/GameWorld.h b/src/GameWorld.h index 039792d..b777975 100755 --- a/src/GameWorld.h +++ b/src/GameWorld.h @@ -51,11 +51,15 @@ class GameWorld : public World { public: - GameWorld(std::shared_ptr<::EventBroker> eventBroker, std::shared_ptr renderer) - : World(eventBroker), m_Renderer(renderer) { } + GameWorld(std::shared_ptr<::EventBroker> eventBroker, std::shared_ptr<::ResourceManager> resourceManager) + : World(eventBroker, resourceManager) + { } void Initialize(); + EntityID CreateTank(int playerID); + EntityID CreateJeep(int playerID); + void RegisterSystems() override; void AddSystems() override; void RegisterComponents() override; @@ -63,8 +67,6 @@ public: void Update(double dt); private: - std::shared_ptr m_Renderer; - void BindKey(int keyCode, std::string command, float value); void BindMouseButton(int button, std::string command, float value); void BindGamepadAxis(Gamepad::Axis axis, std::string command, float value); diff --git a/src/Model.cpp b/src/Model.cpp index c13a192..1186671 100755 --- a/src/Model.cpp +++ b/src/Model.cpp @@ -1,7 +1,7 @@ #include "PrecompiledHeader.h" #include "Model.h" -Model::Model(ResourceManager* rm, OBJ &obj) +Model::Model(std::shared_ptr rm, OBJ &obj) { OBJ::MaterialInfo* currentMaterial = nullptr; TextureGroup* currentTexGroup = nullptr; @@ -23,11 +23,23 @@ Model::Model(ResourceManager* rm, OBJ &obj) // TODO: Load normal map std::shared_ptr normalMap = nullptr; if (!currentMaterial->NormalMap.FileName.empty()) + { normalMap = std::shared_ptr(rm->Load("Texture", currentMaterial->NormalMap.FileName)); + } + else + { + normalMap = std::shared_ptr(rm->Load("Texture", "Textures/NeutralNormalMap.png")); + } // Load specular map std::shared_ptr specularMap = nullptr; if (!currentMaterial->SpecularMap.FileName.empty()) + { specularMap = std::shared_ptr(rm->Load("Texture", currentMaterial->SpecularMap.FileName)); + } + else + { + specularMap = std::shared_ptr(rm->Load("Texture", "Textures/NeutralSpecularMap.png")); + } // TODO: Load material parameters // Create new texture group (start index of new group is upcoming index) @@ -36,6 +48,21 @@ Model::Model(ResourceManager* rm, OBJ &obj) currentTexGroup = &TextureGroups.back(); } + /*std::unordered_map similarNormals; + std::unordered_map normalCount; + for (auto &faceDef : face.Definitions) + { + if (faceDef.NormalIndex == 0) + continue; + + + similarNormals[faceDef.VertexIndex - 1] += normal; + normalCount[faceDef.VertexIndex - 1]++; + int index = pair.first; + glm::vec3 averagedNormal = ; + Normals[] + }*/ + // Face definitions for (auto faceDef : face.Definitions) { @@ -178,7 +205,7 @@ void Model::CreateBuffers( std::vector vertices, std::vector resourceManager, OBJ &obj); struct TextureGroup { diff --git a/src/RenderQueue.h b/src/RenderQueue.h index 7439de2..a99535c 100644 --- a/src/RenderQueue.h +++ b/src/RenderQueue.h @@ -14,25 +14,10 @@ struct RenderJob { friend class RenderQueue; - unsigned int ViewportID; - unsigned int TextureID; - - GLuint DiffuseTexture; - GLuint NormalTexture; - GLuint SpecularTexture; - GLuint VAO; - unsigned int StartIndex; - unsigned int EndIndex; - glm::mat4 ModelMatrix; - protected: uint64_t Hash; - void CalculateHash() - { - Hash = ViewportID << 58 // 6 bits - | TextureID << 42; // 16 bits - } + virtual void CalculateHash() = 0; bool operator<(const RenderJob& rhs) { @@ -40,13 +25,49 @@ protected: } }; +struct ModelJob : RenderJob +{ + unsigned int ShaderID; + unsigned int TextureID; + + GLuint DiffuseTexture; + GLuint NormalTexture; + GLuint SpecularTexture; + glm::vec4 Color; + GLuint VAO; + unsigned int StartIndex; + unsigned int EndIndex; + glm::mat4 ModelMatrix; + + void CalculateHash() override + { + Hash = TextureID; + } +}; + +struct SpriteJob : RenderJob +{ + unsigned int ShaderID; + unsigned int TextureID; + + GLuint Texture; + glm::vec4 Color; + glm::mat4 ModelMatrix; + + void CalculateHash() override + { + Hash = TextureID; + } +}; + class RenderQueue { public: - void Add(RenderJob &job) + template + void Add(T &job) { job.CalculateHash(); - m_Jobs.push_front(job); + m_Jobs.push_front(std::shared_ptr(new T(job))); m_Jobs.sort(); } @@ -55,8 +76,18 @@ public: m_Jobs.clear(); } + std::forward_list>::const_iterator begin() + { + return m_Jobs.begin(); + } + + std::forward_list>::const_iterator end() + { + return m_Jobs.end(); + } + private: - std::forward_list m_Jobs; + std::forward_list> m_Jobs; }; #endif // RenderQueue_h__ diff --git a/src/Renderer.cpp b/src/Renderer.cpp index c2d0346..4769312 100755 --- a/src/Renderer.cpp +++ b/src/Renderer.cpp @@ -1,7 +1,8 @@ #include "PrecompiledHeader.h" #include "Renderer.h" -Renderer::Renderer() +Renderer::Renderer(std::shared_ptr<::ResourceManager> resourceManager) + : ResourceManager(resourceManager) { m_VSync = false; #ifdef DEBUG @@ -13,14 +14,17 @@ Renderer::Renderer() m_DrawWireframe = false; m_DrawBounds = false; #endif - Gamma = 2.2f; + Gamma = 0.85f; CAtt = 1.0f; LAtt = 0.0f; QAtt = 3.0f; - m_ShadowMapRes = 2048*6; - m_SunPosition = glm::vec3(0, 3.5f, 10); + m_ShadowMapRes = 2048*2; + m_SunPosition = glm::vec3(0.f, 1.0f, 0.5f); m_SunTarget = glm::vec3(0, 0, 0); - m_SunProjection = glm::ortho(10.f, -10.f, 10.f, -10.f, 10.f, -10.f); + m_SunProjection_height = glm::vec2(-40.f, 40.f); + m_SunProjection_width = glm::vec2(-40.f, 40.f); + m_SunProjection_length = glm::vec2(-500.f, 500.f); + m_SunProjection = glm::ortho(m_SunProjection_width.x, m_SunProjection_width.y, m_SunProjection_height.x, m_SunProjection_height.y, m_SunProjection_length.x, m_SunProjection_length.y); /* Lights = 0;*/ } @@ -66,13 +70,14 @@ void Renderer::Initialize() } // Create Camera - m_Camera = std::make_shared(45.f, (float)m_Width / m_Height, 0.01f, 1000.f); - m_Camera->Position(glm::vec3(0.0f, 0.0f, 2.f)); + m_Camera = std::make_shared(45.f, 0.01f, 1000.f); + 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(); } @@ -101,12 +106,22 @@ void Renderer::LoadContent() m_ShaderProgramDebugAABB.AddShader(standardVS); m_ShaderProgramDebugAABB.AddShader(std::shared_ptr(new FragmentShader("Shaders/AABB.frag.glsl"))); m_ShaderProgramDebugAABB.Compile(); - m_ShaderProgramDebugAABB.Link(); + m_ShaderProgramDebugAABB.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(); - m_ShaderProgramSkybox.Link();*/ + m_ShaderProgramSkybox.Link(); + + m_SunPassProgram.AddShader(std::shared_ptr(new VertexShader("Shaders/SunPass.vert.glsl"))); + m_SunPassProgram.AddShader(std::shared_ptr(new FragmentShader("Shaders/SunPass.frag.glsl"))); + m_SunPassProgram.Compile(); + m_SunPassProgram.Link(); + + 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(); + m_ForwardRendering.Link(); m_ShaderProgramShadows.AddShader(std::shared_ptr(new VertexShader("Shaders/ShadowMap.vert.glsl"))); m_ShaderProgramShadows.AddShader(std::shared_ptr(new FragmentShader("Shaders/ShadowMap.frag.glsl"))); @@ -139,6 +154,9 @@ void Renderer::LoadContent() m_ScreenQuad = CreateQuad(); CreateShadowMap(m_ShadowMapRes); FrameBufferTextures(); + + m_sphereModel = ResourceManager->Load("Model", "Models/Placeholders/PhysicsTest/Sphere.obj"); + m_Skybox = std::make_shared("Textures/Skybox/Sunset", "jpg"); } void Renderer::Draw(double dt) @@ -152,17 +170,44 @@ void Renderer::Draw(double dt) m_QuadView = true; } - if(glfwGetKey(m_Window, GLFW_KEY_KP_1)) + //if(glfwGetKey(m_Window, GLFW_KEY_KP_1)) + //{ + // Gamma -= 0.3f * dt; + // LOG_INFO("Gamma_UP: %f", Gamma); + //} + //if(glfwGetKey(m_Window, GLFW_KEY_KP_4)) + //{ + // Gamma += 0.3f * dt; + // LOG_INFO("Gamma_DOWN: %f", Gamma); + //} + + if(glfwGetKey(m_Window, GLFW_KEY_KP_7)) { - Gamma -= 0.3f * dt; - LOG_INFO("Gamma_UP: %f", Gamma); + m_SunProjection_height.x += 10.f * dt; + LOG_INFO("Heightx+: %f", m_SunProjection_height); + m_SunProjection = glm::ortho(m_SunProjection_width.x, m_SunProjection_width.y, m_SunProjection_height.x, m_SunProjection_height.y, m_SunProjection_length.x, m_SunProjection_length.y); } - if(glfwGetKey(m_Window, GLFW_KEY_KP_4)) + else if(glfwGetKey(m_Window, GLFW_KEY_KP_4)) { - Gamma += 0.3f * dt; - LOG_INFO("Gamma_DOWN: %f", Gamma); + m_SunProjection_height.x -= 10.f * dt; + LOG_INFO("Heightx-: %f", m_SunProjection_height); + m_SunProjection = glm::ortho(m_SunProjection_width.x, m_SunProjection_width.y, m_SunProjection_height.x, m_SunProjection_height.y, m_SunProjection_length.x, m_SunProjection_length.y); } + if(glfwGetKey(m_Window, GLFW_KEY_KP_8)) + { + m_SunProjection_height.y += 10.f * dt; + LOG_INFO("Heightx+: %f", m_SunProjection_height); + m_SunProjection = glm::ortho(m_SunProjection_width.x, m_SunProjection_width.y, m_SunProjection_height.x, m_SunProjection_height.y, m_SunProjection_length.x, m_SunProjection_length.y); + } + else if(glfwGetKey(m_Window, GLFW_KEY_KP_5)) + { + m_SunProjection_height.y -= 10.f * dt; + LOG_INFO("Heightx-: %f", m_SunProjection_height); + m_SunProjection = glm::ortho(m_SunProjection_width.x, m_SunProjection_width.y, m_SunProjection_height.x, m_SunProjection_height.y, m_SunProjection_length.x, m_SunProjection_length.y); + } + + if(glfwGetKey(m_Window, GLFW_KEY_1)) { if(glfwGetKey(m_Window, GLFW_KEY_KP_ADD)) @@ -211,16 +256,196 @@ void Renderer::Draw(double dt) glfwSwapBuffers(m_Window); } +void Renderer::DrawFrame(RenderQueue &rq) +{ + glBindFramebuffer(GL_FRAMEBUFFER, 0); + 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); + //glClearColor(0.0f, 0.5f, 0.0f, 1.0f); + + glDisable(GL_DEPTH_TEST); + glEnable(GL_CULL_FACE); + glCullFace(GL_BACK); + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + + //glm::mat4 cameraMatrix = m_Camera->ProjectionMatrix((float)m_Width / m_Height) * m_Camera->ViewMatrix(); + //glm::mat4 MVP; + + m_ForwardRendering.Bind(); + + //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(m_ForwardRendering.GetHandle(), "MVP"), 1, GL_FALSE, glm::value_ptr(MVP)); + // glUniformMatrix4fv(glGetUniformLocation(m_ForwardRendering.GetHandle(), "M"), 1, GL_FALSE, glm::value_ptr(modelMatrix)); + // glUniformMatrix4fv(glGetUniformLocation(m_ForwardRendering.GetHandle(), "V"), 1, GL_FALSE, glm::value_ptr(m_Camera->ViewMatrix())); + // glUniformMatrix4fv(glGetUniformLocation(m_ForwardRendering.GetHandle(), "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); + // } + //} + + for (auto &job : rq) + { + //auto modelJob = std::dynamic_pointer_cast(job); + //if (modelJob) + //{ + // glm::mat4 modelMatrix = modelJob->ModelMatrix; + + // MVP = cameraMatrix * modelMatrix; + // depthMVP = depthCameraMatrix * modelMatrix; + // glUniformMatrix4fv(glGetUniformLocation(m_FirstPassProgram.GetHandle(), "MVP"), 1, GL_FALSE, glm::value_ptr(MVP)); + // glUniformMatrix4fv(glGetUniformLocation(m_FirstPassProgram.GetHandle(), "DepthMVP"), 1, GL_FALSE, glm::value_ptr(depthMVP)); + // glUniformMatrix4fv(glGetUniformLocation(m_FirstPassProgram.GetHandle(), "M"), 1, GL_FALSE, glm::value_ptr(modelMatrix)); + // glUniformMatrix4fv(glGetUniformLocation(m_FirstPassProgram.GetHandle(), "V"), 1, GL_FALSE, glm::value_ptr(m_Camera->ViewMatrix())); + // glUniformMatrix4fv(glGetUniformLocation(m_FirstPassProgram.GetHandle(), "P"), 1, GL_FALSE, glm::value_ptr(cameraProjection)); + // //glUniform3fv(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "SunDirection_cameraspace"), 1, glm::value_ptr(sunDirection_cameraview)); + + // 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) + { + glUniformMatrix4fv(glGetUniformLocation(m_ForwardRendering.GetHandle(), "MVP"), 1, GL_FALSE, glm::value_ptr(glm::mat4())); + glUniformMatrix4fv(glGetUniformLocation(m_ForwardRendering.GetHandle(), "M"), 1, GL_FALSE, glm::value_ptr(glm::mat4())); + glUniformMatrix4fv(glGetUniformLocation(m_ForwardRendering.GetHandle(), "V"), 1, GL_FALSE, glm::value_ptr(glm::mat4())); + glUniformMatrix4fv(glGetUniformLocation(m_ForwardRendering.GetHandle(), "P"), 1, GL_FALSE, glm::value_ptr(glm::mat4())); + glUniform4fv(glGetUniformLocation(m_ForwardRendering.GetHandle(), "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; + } + } +} + +void Renderer::DrawWorld(RenderQueue &rq) +{ + glDisable(GL_BLEND); + + DrawShadowMap(rq); + + /* + Base pass + */ + 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); + //glViewport(0, 0, m_Width, m_Height); + + // Clear G-buffer + GLenum windowBuffClear[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_COLOR_ATTACHMENT3 }; + glDrawBuffers(4, windowBuffClear); + glClearColor(115.f / 255, 192.f / 255, 255.f / 255, 0.f); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + // Execute the first render stage which will fill out the internal buffers with data(??) + m_FirstPassProgram.Bind(); + GLenum windowBuffOpaque[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_COLOR_ATTACHMENT3 }; + glDrawBuffers(4, windowBuffOpaque); + + glCullFace(GL_BACK); + glEnable(GL_DEPTH_TEST); + DrawFBOScene(rq); + + /* + Lighting pass + */ + glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_fbLightingPass); + GLenum lightingPassAttachments[] = { GL_COLOR_ATTACHMENT0 }; + glDrawBuffers(1, lightingPassAttachments); + + glClearColor(0.f, 0.f, 0.f, 0.f); + glClear(GL_COLOR_BUFFER_BIT); + + m_SecondPassProgram.Bind(); + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, m_fPositionTexture); + glActiveTexture(GL_TEXTURE1); + glBindTexture(GL_TEXTURE_2D, m_fNormalsTexture); + glActiveTexture(GL_TEXTURE2); + glBindTexture(GL_TEXTURE_2D, m_fSpecularTexture); + + glCullFace(GL_FRONT); + DrawLightScene(rq); + DrawSunLightScene(); + + /* + Final pass + */ + 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); + glClear(GL_DEPTH_BUFFER_BIT); + + m_FinalPassProgram.Bind(); + + // Ambient light + glUniform3fv(glGetUniformLocation(m_FinalPassProgram.GetHandle(), "La"), 1, glm::value_ptr(glm::vec3(0.7f))); + glUniform1f(glGetUniformLocation(m_FinalPassProgram.GetHandle(), "Gamma"), Gamma); + + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, m_fDiffuseTexture); + glActiveTexture(GL_TEXTURE1); + glBindTexture(GL_TEXTURE_2D, m_fLightingTexture); + + glCullFace(GL_BACK); + glBindVertexArray(m_ScreenQuad); + glEnableVertexAttribArray(0); + glDrawArrays(GL_TRIANGLES, 0, 6); +} + +void Renderer::Swap() +{ + glfwSwapBuffers(m_Window); +} + #pragma region TempRegion void Renderer::DrawSkybox() { - glBindFramebuffer(GL_FRAMEBUFFER, 0); - glViewport(0, 0, m_Width, m_Height); + //glBindFramebuffer(GL_FRAMEBUFFER, 0); + //glViewport(0, 0, m_Width, m_Height); + //glScissor(0, 0, m_Width, m_Height); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); m_ShaderProgramSkybox.Bind(); - glm::mat4 cameraMatrix = m_Camera->ProjectionMatrix() * glm::toMat4(glm::inverse(m_Camera->Orientation())); + glm::mat4 cameraMatrix = m_Camera->ProjectionMatrix((float)m_Width / m_Height) * glm::toMat4(glm::inverse(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(); @@ -235,8 +460,8 @@ void Renderer::CreateShadowMap(int resolution) glGenTextures(1, &m_ShadowDepthTexture); glBindTexture(GL_TEXTURE_2D, m_ShadowDepthTexture); glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT16, resolution, resolution, 0, GL_DEPTH_COMPONENT, GL_FLOAT, nullptr); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER); @@ -253,21 +478,23 @@ void Renderer::CreateShadowMap(int resolution) } } -void Renderer::DrawShadowMap() +void Renderer::DrawShadowMap(RenderQueue &rq) { glEnable(GL_DEPTH_TEST);//Tests where objects are and display them correctly glEnable(GL_CULL_FACE); //removes triangles on the wrong side of the object - glCullFace(GL_BACK); //Make it so that only the back faces are rendered + glCullFace(GL_FRONT); //Make it so that only the back faces are rendered //Binds the FBO and sets the veiwport, witch in effect is how large the shadowmap is and what resolution it has. glBindFramebuffer(GL_FRAMEBUFFER, m_ShadowFrameBuffer); glViewport(0, 0, m_ShadowMapRes, m_ShadowMapRes); + glScissor(0, 0, m_ShadowMapRes, m_ShadowMapRes); glClear(GL_DEPTH_BUFFER_BIT); //glClearColor(0.0f, 0.0f, 0.0f, 0.0f); //Creates the "camera" for the shadowmap from the direction of the sun. - glm::mat4 depthViewMatrix = glm::lookAt(m_SunPosition, m_SunTarget, glm::vec3(0, 1, 0)); + glm::mat4 depthViewMatrix = glm::lookAt(m_SunPosition, m_SunTarget, glm::vec3(0, 1, 0)) * glm::translate(m_Camera->Position() * glm::vec3(1, 1, 1)); + //glm::mat4 depthViewMatrix = glm::lookAt(m_SunPosition, m_SunTarget, glm::vec3(0, 1, 0)) * glm::translate((-m_Camera->Position() + (glm::vec3(40.0) * -m_Camera->Forward())) * glm::vec3(1, 1, 1)); glm::mat4 depthCamera = m_SunProjection * depthViewMatrix; glm::mat4 MVP; @@ -275,26 +502,23 @@ void Renderer::DrawShadowMap() glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); //Draws filled polygons //For each model, render them to the shadowmap - for (auto tuple : ModelsToRender) + for (auto &job : rq) { - Model* model; - glm::mat4 modelMatrix; - bool shadow; - std::tie(model, modelMatrix, std::ignore, shadow) = tuple; - if (!shadow) - continue; - - MVP = depthCamera * modelMatrix; - glUniformMatrix4fv(glGetUniformLocation(m_ShaderProgramShadows.GetHandle(), "MVP"), 1, GL_FALSE, glm::value_ptr(MVP)); - - glBindVertexArray(model->VAO); - for (auto texGroup : model->TextureGroups) + auto modelJob = std::dynamic_pointer_cast(job); + if (modelJob) { - glDrawArrays(GL_TRIANGLES, texGroup.StartIndex, texGroup.EndIndex - texGroup.StartIndex + 1); + glm::mat4 modelMatrix = modelJob->ModelMatrix; + + MVP = depthCamera * modelMatrix; + glUniformMatrix4fv(glGetUniformLocation(m_ShaderProgramShadows.GetHandle(), "MVP"), 1, GL_FALSE, glm::value_ptr(MVP)); + //glUniform3fv(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "SunDirection_cameraspace"), 1, glm::value_ptr(sunDirection_cameraview)); + + glBindVertexArray(modelJob->VAO); + glDrawArrays(GL_TRIANGLES, modelJob->StartIndex, modelJob->EndIndex - modelJob->StartIndex + 1); + + continue; } } - - } void Renderer::DrawDebugShadowMap() @@ -355,19 +579,31 @@ void Renderer::AddModelToDraw(Model* model, glm::vec3 position, glm::quat orient void Renderer::AddTextureToDraw(Texture* texture, glm::vec3 position, glm::quat orientation, glm::vec3 scale) { - glm::mat4 modelMatrix = glm::translate(glm::mat4(), position) * glm::toMat4(orientation) * glm::scale(scale); + //glm::mat4 modelMatrix = glm::translate(glm::mat4(), position) * glm::toMat4(orientation) * glm::scale(scale); - TexturesToRender.push_back(std::make_tuple(texture, modelMatrix, position)); + //glm::vec3 camToParticle = glm::normalize(m_Camera->Position() - position); + //glm::vec3 up = glm::vec3(0,1,0); + //glm::vec3 rightVec = glm::normalize(glm::cross(up, camToParticle)); + //glm::vec3 up2 = glm::normalize(glm::cross(camToParticle, rightVec)); + // + //glm::mat4 billboardMatrix; + //billboardMatrix[0] = glm::vec4(rightVec, 0); + //billboardMatrix[1] = glm::vec4(up2, 0); + //billboardMatrix[2] = glm::vec4(camToParticle, 0); + ////billboardMatrix[3] = glm::vec4(position, 0); + + //TexturesToRender.push_back(std::make_tuple(texture, modelMatrix, billboardMatrix)); } void Renderer::AddPointLightToDraw( - glm::vec3 _position, - glm::vec3 _specular, - glm::vec3 _diffuse, - float _specularExponent, + glm::vec3 _position, + glm::vec3 _specular, + glm::vec3 _diffuse, + float _specularExponent, float _ConstantAttenuation, float _LinearAttenuation, - float _QuadraticAttenuation + float _QuadraticAttenuation, + float _radius ) { Light light; @@ -378,6 +614,7 @@ void Renderer::AddPointLightToDraw( light.ConstantAttenuation = _ConstantAttenuation; light.LinearAttenuation = _LinearAttenuation; light.QuadraticAttenuation = _QuadraticAttenuation; + light.Radius = _radius; light.SphereModelMatrix = CreateLightMatrix(light); Lights.push_back(light); } @@ -537,7 +774,7 @@ void Renderer::FrameBufferTextures() //Generate and bind diffuse texture glGenTextures(1, &m_fDiffuseTexture); glBindTexture(GL_TEXTURE_2D, m_fDiffuseTexture); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_Width, m_Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, m_Width, m_Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); @@ -546,7 +783,7 @@ void Renderer::FrameBufferTextures() //Generate and bind position texture glGenTextures(1, &m_fPositionTexture); glBindTexture(GL_TEXTURE_2D, m_fPositionTexture); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, m_Width, m_Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB16F, m_Width, m_Height, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); @@ -555,7 +792,7 @@ void Renderer::FrameBufferTextures() //Generate and bind normal texture glGenTextures(1, &m_fNormalsTexture); glBindTexture(GL_TEXTURE_2D, m_fNormalsTexture); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, m_Width, m_Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB16F, m_Width, m_Height, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); @@ -564,7 +801,7 @@ void Renderer::FrameBufferTextures() //Generate and bind normal texture glGenTextures(1, &m_fSpecularTexture); glBindTexture(GL_TEXTURE_2D, m_fSpecularTexture); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, m_Width, m_Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); + glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, m_Width, m_Height, 0, GL_RED, GL_UNSIGNED_BYTE, NULL); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); @@ -601,7 +838,7 @@ void Renderer::FrameBufferTextures() glGenTextures(1, &m_fLightingTexture); glBindTexture(GL_TEXTURE_2D, m_fLightingTexture); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_Width, m_Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, m_Width, m_Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); @@ -623,92 +860,101 @@ void Renderer::FrameBufferTextures() void Renderer::DrawFBO() { - DrawShadowMap(); + //DrawShadowMap(); - for (auto &pair : m_Viewports) - { - Viewport &viewport = pair.second; - if (!viewport.Camera) - continue; + //for (auto &pair : m_Viewports) + //{ + // Viewport &viewport = pair.second; + // if (!viewport.Camera) + // continue; - int x = viewport.Left * m_Width; - int y = viewport.Top * m_Height; - int width = (viewport.Right - viewport.Left) * m_Width; - int height = (viewport.Bottom - viewport.Top) * m_Height; - - /* - Base pass - */ - glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_fbBasePass); - glViewport(0, 0, m_Width, m_Height); + // int x = viewport.Left * m_Width; + // int y = viewport.Top * m_Height; + // int width = (viewport.Right - viewport.Left) * m_Width; + // int height = (viewport.Bottom - viewport.Top) * m_Height; + // + // /* + // Base pass + // */ + // glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_fbBasePass); + // glViewport(0, 0, m_Width, m_Height); - // Clear G-buffer - GLenum windowBuffClear[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2 }; - glDrawBuffers(3, windowBuffClear); - glClearColor(0.f, 0.f, 0.f, 0.f); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + // // Clear G-buffer + // GLenum windowBuffClear[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_COLOR_ATTACHMENT3 }; + // glDrawBuffers(4, windowBuffClear); + // glClearColor(0.0f, 0.3f, 0.7f, 0.f); + // glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - // Execute the first render stage which will fill out the internal buffers with data(??) - m_FirstPassProgram.Bind(); - GLenum windowBuffOpaque[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2 }; - glDrawBuffers(3, windowBuffOpaque); + // // Execute the first render stage which will fill out the internal buffers with data(??) + // m_FirstPassProgram.Bind(); + // GLenum windowBuffOpaque[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_COLOR_ATTACHMENT3 }; + // glDrawBuffers(4, windowBuffOpaque); - glCullFace(GL_BACK); - - DrawFBOScene(viewport); + // glCullFace(GL_BACK); + // + // DrawFBOScene(viewport); - /* - Lighting pass - */ - glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_fbLightingPass); - GLenum lightingPassAttachments[] = { GL_COLOR_ATTACHMENT0 }; - glDrawBuffers(1, lightingPassAttachments); + // /* + // Lighting pass + // */ + // glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_fbLightingPass); + // GLenum lightingPassAttachments[] = { GL_COLOR_ATTACHMENT0 }; + // glDrawBuffers(1, lightingPassAttachments); - glClearColor(0.f, 0.f, 0.f, 0.f); - glClear(GL_COLOR_BUFFER_BIT); + // glClearColor(0.f, 0.f, 0.f, 0.f); + // glClear(GL_COLOR_BUFFER_BIT); - m_SecondPassProgram.Bind(); - glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, m_fPositionTexture); - glActiveTexture(GL_TEXTURE1); - glBindTexture(GL_TEXTURE_2D, m_fNormalsTexture); + // m_SecondPassProgram.Bind(); + // glActiveTexture(GL_TEXTURE0); + // glBindTexture(GL_TEXTURE_2D, m_fPositionTexture); + // glActiveTexture(GL_TEXTURE1); + // glBindTexture(GL_TEXTURE_2D, m_fNormalsTexture); + // glActiveTexture(GL_TEXTURE2); + // glBindTexture(GL_TEXTURE_2D, m_fSpecularTexture); - glCullFace(GL_FRONT); - DrawLightScene(viewport); + // glCullFace(GL_FRONT); + // DrawLightScene(viewport); + DrawSunLightScene(); - /* - Final pass - */ - glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); - glViewport(x, y, width, height); - glClear(GL_DEPTH_BUFFER_BIT); + // /* + // Final pass + // */ + // glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); + // glViewport(x, y, width, height); + // glClear(GL_DEPTH_BUFFER_BIT); - m_FinalPassProgram.Bind(); + // m_FinalPassProgram.Bind(); - // Ambient light - glUniform3fv(glGetUniformLocation(m_FinalPassProgram.GetHandle(), "La"), 1, glm::value_ptr(glm::vec3(0.1f, 0.1f, 0.1f))); - glUniform1f(glGetUniformLocation(m_FinalPassProgram.GetHandle(), "Gamma"), Gamma); + // // Ambient light + // glUniform3fv(glGetUniformLocation(m_FinalPassProgram.GetHandle(), "La"), 1, glm::value_ptr(glm::vec3(0.7f))); + // glUniform1f(glGetUniformLocation(m_FinalPassProgram.GetHandle(), "Gamma"), Gamma); - glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, m_fDiffuseTexture); - glActiveTexture(GL_TEXTURE1); - glBindTexture(GL_TEXTURE_2D, m_fLightingTexture); + // glActiveTexture(GL_TEXTURE0); + // glBindTexture(GL_TEXTURE_2D, m_fDiffuseTexture); + // glActiveTexture(GL_TEXTURE1); + // glBindTexture(GL_TEXTURE_2D, m_fLightingTexture); - glCullFace(GL_BACK); - glBindVertexArray(m_ScreenQuad); - glEnableVertexAttribArray(0); - glDrawArrays(GL_TRIANGLES, 0, 6); - } + // glCullFace(GL_BACK); + // glBindVertexArray(m_ScreenQuad); + // glEnableVertexAttribArray(0); + // glDrawArrays(GL_TRIANGLES, 0, 6); + //} } -void Renderer::DrawFBOScene(Viewport &viewport) +void Renderer::DrawFBO2() +{ + ForwardRendering(); +} + +void Renderer::DrawFBOScene(RenderQueue &rq) { // glEnable(GL_DEPTH_TEST);//Tests where objects are and display them correctly // glEnable(GL_CULL_FACE); //removes triangles on the wrong side of the object // glCullFace(GL_BACK); //Make it so that only the back faces are rendered glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); //Draws filled polygons - glm::mat4 cameraMatrix = viewport.Camera->ProjectionMatrix() * viewport.Camera->ViewMatrix(); + glm::mat4 cameraProjection = m_Camera->ProjectionMatrix((float)m_Viewport.Width / m_Viewport.Height); + glm::mat4 cameraMatrix = cameraProjection * m_Camera->ViewMatrix(); glm::mat4 MVP; glm::mat4 biasMatrix( 0.5, 0.0, 0.0, 0.0, @@ -717,83 +963,84 @@ void Renderer::DrawFBOScene(Viewport &viewport) 0.5, 0.5, 0.5, 1.0 ); - glm::mat4 depthViewMatrix = glm::lookAt(m_SunPosition, m_SunTarget, glm::vec3(0, 1, 0)); + //glm::mat4 depthViewMatrix = glm::lookAt(m_SunPosition, m_SunTarget, glm::vec3(0, 1, 0)) * glm::translate((-m_Camera->Position() + (glm::vec3(40.0) * -m_Camera->Forward())) * glm::vec3(1, 1, 1)); + glm::mat4 depthViewMatrix = glm::lookAt(m_SunPosition, m_SunTarget, glm::vec3(0, 1, 0)) * glm::translate(-m_Camera->Position() * glm::vec3(1, 1, 1)); glm::mat4 depthCamera = m_SunProjection * depthViewMatrix; glm::mat4 depthCameraMatrix = biasMatrix * depthCamera; glm::mat4 depthMVP; + 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 tuple : ModelsToRender) + for (auto &job : rq) { - Model* model; - glm::mat4 modelMatrix; - bool visible; - std::tie(model, modelMatrix, visible, std::ignore) = tuple; - if (!visible) - continue; - - MVP = cameraMatrix * modelMatrix; - depthMVP = depthCameraMatrix * modelMatrix; - glUniformMatrix4fv(glGetUniformLocation(m_FirstPassProgram.GetHandle(), "MVP"), 1, GL_FALSE, glm::value_ptr(MVP)); - glUniformMatrix4fv(glGetUniformLocation(m_FirstPassProgram.GetHandle(), "DepthMVP"), 1, GL_FALSE, glm::value_ptr(depthMVP)); - glUniformMatrix4fv(glGetUniformLocation(m_FirstPassProgram.GetHandle(), "M"), 1, GL_FALSE, glm::value_ptr(modelMatrix)); - glUniformMatrix4fv(glGetUniformLocation(m_FirstPassProgram.GetHandle(), "V"), 1, GL_FALSE, glm::value_ptr(viewport.Camera->ViewMatrix())); - glUniformMatrix4fv(glGetUniformLocation(m_FirstPassProgram.GetHandle(), "P"), 1, GL_FALSE, glm::value_ptr(viewport.Camera->ProjectionMatrix())); - glBindVertexArray(model->VAO); - for (auto texGroup : model->TextureGroups) + auto modelJob = std::dynamic_pointer_cast(job); + if (modelJob) { + glm::mat4 modelMatrix = modelJob->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)); + + glBindVertexArray(modelJob->VAO); glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, *texGroup.Texture); - if (texGroup.NormalMap) + glBindTexture(GL_TEXTURE_2D, modelJob->DiffuseTexture); + if (modelJob->NormalTexture != 0) { glActiveTexture(GL_TEXTURE2); - glBindTexture(GL_TEXTURE_2D, *texGroup.NormalMap); + glBindTexture(GL_TEXTURE_2D, modelJob->NormalTexture); } - glDrawArrays(GL_TRIANGLES, texGroup.StartIndex, texGroup.EndIndex - texGroup.StartIndex + 1); + if (modelJob->SpecularTexture) + { + glActiveTexture(GL_TEXTURE3); + glBindTexture(GL_TEXTURE_2D, modelJob->SpecularTexture); + } + glDrawArrays(GL_TRIANGLES, modelJob->StartIndex, modelJob->EndIndex - modelJob->StartIndex + 1); + + continue; } - } - - for (auto tuple : TexturesToRender) - { - Texture* texture; - glm::mat4 modelMatrix; - glm::vec3 position; - std::tie(texture, modelMatrix, position) = tuple; - //MVP = cameraMatrix * glm::inverse(glm::toMat4(m_Camera->Orientation()) * modelMatrix ); + //auto spriteJob = std::dynamic_pointer_cast(job); + //if (spriteJob) + //{ + // Texture* texture; + // glm::mat4 modelMatrix; + // glm::mat4 billboardMatrix; + // std::tie(texture, modelMatrix, billboardMatrix) = tuple; - glm::vec3 camToParticle = glm::normalize(viewport.Camera->Position() - position); - glm::vec3 up = glm::vec3(0,1,0); - glm::vec3 rightVec = glm::normalize(glm::cross(up, camToParticle)); - glm::vec3 up2 = glm::normalize(glm::cross(camToParticle, rightVec)); + // //MVP = cameraMatrix * glm::inverse(glm::toMat4(m_Camera->Orientation()) * modelMatrix ); + // MVP = cameraMatrix * modelMatrix * billboardMatrix; - glm::mat4 billboardMatrix; - billboardMatrix[0] = glm::vec4(rightVec, 0); - billboardMatrix[1] = glm::vec4(up2, 0); - billboardMatrix[2] = glm::vec4(camToParticle, 0); - //billboardMatrix[3] = glm::vec4(position, 0); + // depthMVP = depthCameraMatrix * modelMatrix; + // glUniformMatrix4fv(glGetUniformLocation(m_FirstPassProgram.GetHandle(), "MVP"), 1, GL_FALSE, glm::value_ptr(MVP)); + // glUniformMatrix4fv(glGetUniformLocation(m_FirstPassProgram.GetHandle(), "DepthMVP"), 1, GL_FALSE, glm::value_ptr(depthMVP)); + // glUniformMatrix4fv(glGetUniformLocation(m_FirstPassProgram.GetHandle(), "M"), 1, GL_FALSE, glm::value_ptr(modelMatrix)); + // glUniformMatrix4fv(glGetUniformLocation(m_FirstPassProgram.GetHandle(), "V"), 1, GL_FALSE, glm::value_ptr(m_Camera->ViewMatrix())); + // glUniformMatrix4fv(glGetUniformLocation(m_FirstPassProgram.GetHandle(), "P"), 1, GL_FALSE, glm::value_ptr(m_Camera->ProjectionMatrix((float)m_Width / m_Height))); - MVP = cameraMatrix * modelMatrix * billboardMatrix; + // glActiveTexture(GL_TEXTURE0); + // glBindTexture(GL_TEXTURE_2D, *texture); + // glBindVertexArray(m_ScreenQuad); + // glDrawArrays(GL_TRIANGLES, 0, 6); - depthMVP = depthCameraMatrix * modelMatrix; - glUniformMatrix4fv(glGetUniformLocation(m_FirstPassProgram.GetHandle(), "MVP"), 1, GL_FALSE, glm::value_ptr(MVP)); - glUniformMatrix4fv(glGetUniformLocation(m_FirstPassProgram.GetHandle(), "DepthMVP"), 1, GL_FALSE, glm::value_ptr(depthMVP)); - glUniformMatrix4fv(glGetUniformLocation(m_FirstPassProgram.GetHandle(), "M"), 1, GL_FALSE, glm::value_ptr(modelMatrix)); - glUniformMatrix4fv(glGetUniformLocation(m_FirstPassProgram.GetHandle(), "V"), 1, GL_FALSE, glm::value_ptr(viewport.Camera->ViewMatrix())); - glUniformMatrix4fv(glGetUniformLocation(m_FirstPassProgram.GetHandle(), "P"), 1, GL_FALSE, glm::value_ptr(viewport.Camera->ProjectionMatrix())); - - glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, *texture); - glBindVertexArray(m_ScreenQuad); - glDrawArrays(GL_TRIANGLES, 0, 6); + // continue; + //} } } - - -void Renderer::DrawLightScene(Viewport &viewport) +void Renderer::DrawLightScene(RenderQueue &rq) { glEnable(GL_BLEND); glBlendEquation (GL_FUNC_ADD); @@ -803,32 +1050,72 @@ void Renderer::DrawLightScene(Viewport &viewport) glDepthMask (GL_FALSE); glBindVertexArray(m_sphereModel->VAO); - glm::mat4 cameraMatrix = viewport.Camera->ProjectionMatrix() * viewport.Camera->ViewMatrix(); + glm::mat4 cameraProjection = m_Camera->ProjectionMatrix((float)m_Viewport.Width / m_Viewport.Height); + glm::mat4 cameraMatrix = cameraProjection * m_Camera->ViewMatrix(); glm::mat4 MVP; + glm::vec3 sunDirection = m_SunTarget - m_SunPosition; + + m_SecondPassProgram.Bind(); + GLuint ShaderProgramHandle = m_SecondPassProgram.GetHandle(); for (auto &light : Lights) { MVP = cameraMatrix * light.SphereModelMatrix; - - glUniform2fv(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "ViewportSize"), 1,glm::value_ptr(glm::vec2(m_Width, m_Height))); - glUniformMatrix4fv(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "MVP"), 1, GL_FALSE, glm::value_ptr(MVP)); - glUniformMatrix4fv(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "V"), 1, GL_FALSE, glm::value_ptr(viewport.Camera->ViewMatrix())); - glUniformMatrix4fv(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "P"), 1, GL_FALSE, glm::value_ptr(viewport.Camera->ProjectionMatrix())); - glUniformMatrix4fv(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "M"), 1, GL_FALSE, glm::value_ptr(light.SphereModelMatrix)); - glUniform3fv(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "ls"), 1, glm::value_ptr(light.Specular)); - glUniform3fv(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "ld"), 1, glm::value_ptr(light.Diffuse)); - glUniform3fv(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "lp"), 1, glm::value_ptr(light.Position)); - glUniform3f(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "CameraPosition"), viewport.Camera->Position().x, viewport.Camera->Position().y, viewport.Camera->Position().z); - glUniform1f(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "specularExponent"), light.SpecularExponent); -// glUniform1f(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "ConstantAttenuation"), light.ConstantAttenuation); -// glUniform1f(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "LinearAttenuation"), light.LinearAttenuation); -// glUniform1f(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "QuadraticAttenuation"), light.QuadraticAttenuation); - glUniform1f(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "ConstantAttenuation"), CAtt); - glUniform1f(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "LinearAttenuation"), LAtt); - glUniform1f(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "QuadraticAttenuation"), QAtt); - glDrawArrays(GL_TRIANGLES, 0, m_sphereModel->Vertices.size()); + glUniform2fv(glGetUniformLocation(ShaderProgramHandle, "ViewportSize"), 1,glm::value_ptr(glm::vec2(m_Width, m_Height))); + glUniformMatrix4fv(glGetUniformLocation(ShaderProgramHandle, "MVP"), 1, GL_FALSE, glm::value_ptr(MVP)); + glUniformMatrix4fv(glGetUniformLocation(ShaderProgramHandle, "V"), 1, GL_FALSE, glm::value_ptr(m_Camera->ViewMatrix())); + glUniformMatrix4fv(glGetUniformLocation(ShaderProgramHandle, "P"), 1, GL_FALSE, glm::value_ptr(cameraProjection)); + glUniformMatrix4fv(glGetUniformLocation(ShaderProgramHandle, "M"), 1, GL_FALSE, glm::value_ptr(light.SphereModelMatrix)); + glUniform3fv(glGetUniformLocation(ShaderProgramHandle, "ls"), 1, glm::value_ptr(light.Specular)); + glUniform3fv(glGetUniformLocation(ShaderProgramHandle, "ld"), 1, glm::value_ptr(light.Diffuse)); + glUniform3fv(glGetUniformLocation(ShaderProgramHandle, "lp"), 1, glm::value_ptr(light.Position)); + glUniform3f(glGetUniformLocation(ShaderProgramHandle, "CameraPosition"), m_Camera->Position().x, m_Camera->Position().y, m_Camera->Position().z); + glUniform1f(glGetUniformLocation(ShaderProgramHandle, "specularExponent"), light.SpecularExponent); + //glUniform1f(glGetUniformLocation(ShaderProgramHandle, "ConstantAttenuation"), CAtt); + //glUniform1f(glGetUniformLocation(ShaderProgramHandle, "LinearAttenuation"), LAtt); + //glUniform1f(glGetUniformLocation(ShaderProgramHandle, "QuadraticAttenuation"), QAtt); + glUniform1f(glGetUniformLocation(ShaderProgramHandle, "LightRadius"), light.Radius); + glUniform3fv(glGetUniformLocation(ShaderProgramHandle, "directionToSun"), 1, glm::value_ptr(-sunDirection)); + + glDrawArrays(GL_TRIANGLES, 0, m_sphereModel->Vertices.size()); }; + glEnable(GL_DEPTH_TEST); + glDepthMask(GL_TRUE); + glDisable(GL_BLEND); +} + +void Renderer::DrawSunLightScene() +{ + glCullFace(GL_BACK); + + glEnable(GL_BLEND); + glBlendEquation (GL_FUNC_ADD); + glBlendFunc(GL_ONE,GL_ONE); + + glDisable (GL_DEPTH_TEST); + glDepthMask (GL_FALSE); + //glBindVertexArray(m_sphereModel->VAO); + + glm::mat4 cameraProjection = m_Camera->ProjectionMatrix((float)m_Viewport.Width / m_Viewport.Height); + glm::mat4 cameraMatrix = cameraProjection * m_Camera->ViewMatrix(); + glm::mat4 MVP; + + m_SunPassProgram.Bind(); + GLuint ShaderProgramHandle = m_SunPassProgram.GetHandle(); + + + glUniform2fv(glGetUniformLocation(ShaderProgramHandle, "ViewportSize"), 1, glm::value_ptr(glm::vec2(m_Width, m_Height))); + glUniformMatrix4fv(glGetUniformLocation(ShaderProgramHandle, "MVP"), 1, GL_FALSE, glm::value_ptr(MVP)); + glUniformMatrix4fv(glGetUniformLocation(ShaderProgramHandle, "V"), 1, GL_FALSE, glm::value_ptr(m_Camera->ViewMatrix())); + glUniformMatrix4fv(glGetUniformLocation(ShaderProgramHandle, "P"), 1, GL_FALSE, glm::value_ptr(cameraProjection)); + glUniform3f(glGetUniformLocation(ShaderProgramHandle, "CameraPosition"), m_Camera->Position().x, m_Camera->Position().y, m_Camera->Position().z); + glUniform3fv(glGetUniformLocation(ShaderProgramHandle, "directionToSun"), 1, glm::value_ptr(glm::normalize(m_SunPosition))); + + glBindVertexArray(m_ScreenQuad); + glEnableVertexAttribArray(0); + glDrawArrays(GL_TRIANGLES, 0, 6); + glEnable (GL_DEPTH_TEST); glDepthMask (GL_TRUE); glDisable (GL_BLEND); @@ -844,14 +1131,14 @@ glm::mat4 Renderer::CreateLightMatrix(Light &_light) // float c = _light.ConstantAttenuation; // float l = _light.LinearAttenuation; // float q = _light.QuadraticAttenuation; - float c = CAtt; - float l = LAtt; - float q = QAtt; - float cutOffRadius = abs(sqrt((-4*c*q) + pow(l, 2) + (1024*q) - l) / (2*q)); + //float c = CAtt; + //float l = LAtt; + //float q = QAtt; + //float cutOffRadius = abs(sqrt((-4*c*q) + pow(l, 2) + (1024*q) - l) / (2*q)); glm::mat4 model; model *= glm::translate(_light.Position); - model *= glm::scale(glm::vec3(cutOffRadius)); + model *= glm::scale(glm::vec3(_light.Radius*2)); return model; } @@ -869,7 +1156,7 @@ void Renderer::UpdateSunProjection() glm::vec3(1.f, 1.f, 1.f) }; - glm::mat4 inverseProjectionViewMatrix = glm::inverse(m_Camera->ViewMatrix()) * glm::inverse(m_Camera->ProjectionMatrix()); + glm::mat4 inverseProjectionViewMatrix = glm::inverse(m_Camera->ViewMatrix()) * glm::inverse(m_Camera->ProjectionMatrix((float)m_Width / m_Height)); //Also * with world matrix for light for(auto corner : NDCCube) @@ -882,35 +1169,72 @@ 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::RegisterViewport(int identifier, float left, float top, float right, float bottom) +void Renderer::ForwardRendering() { - Viewport v; - v.Left = left; - v.Top = top; - v.Right = right; - v.Bottom = bottom; - v.Camera = nullptr; - m_Viewports[identifier] = v; + 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, (float)m_Width / m_Height, nearClip, farClip); + m_Cameras[identifier] = std::make_shared(FOV, nearClip, farClip); } void Renderer::UpdateViewport(int viewportIdentifier, int cameraIdentifier) { - auto &viewport = m_Viewports[viewportIdentifier]; - auto camera = m_Cameras[cameraIdentifier]; - camera->AspectRatio(((viewport.Right - viewport.Left) * m_Width) / ((viewport.Bottom - viewport.Top) * m_Height)); - viewport.Camera = camera; + //auto &viewport = m_Viewports[viewportIdentifier]; + //auto camera = m_Cameras[cameraIdentifier]; + //camera->AspectRatio(((viewport.Right - viewport.Left) * m_Width) / ((viewport.Bottom - viewport.Top) * m_Height)); + //viewport.Camera = camera; } void Renderer::UpdateCamera(int cameraIdentifier, glm::vec3 position, glm::quat orientation, float FOV, float nearClip, float farClip) { - m_Cameras[cameraIdentifier]->Position(position); + /*m_Cameras[cameraIdentifier]->Position(position); m_Cameras[cameraIdentifier]->Orientation(orientation); m_Cameras[cameraIdentifier]->FOV(FOV); m_Cameras[cameraIdentifier]->NearClip(nearClip); - m_Cameras[cameraIdentifier]->FarClip(farClip); + m_Cameras[cameraIdentifier]->FarClip(farClip);*/ +} + +void Renderer::ClearPointLights() +{ + Lights.clear(); } diff --git a/src/Renderer.h b/src/Renderer.h index b2c2a91..ecdecfc 100755 --- a/src/Renderer.h +++ b/src/Renderer.h @@ -13,6 +13,8 @@ #include "Components/PointLight.h" #include "Skybox.h" #include "ResourceManager.h" +#include "Util/Rectangle.h" +#include "RenderQueue.h" class Renderer { @@ -29,7 +31,7 @@ public: std::list> TexturesToRender; std::list> AABBsToRender; - Renderer(); + Renderer(std::shared_ptr<::ResourceManager> resourceManager); void Initialize(); void Draw(double dt); @@ -40,6 +42,23 @@ public: void UpdateViewport(int viewportIdentifier, int cameraIdentifier); void UpdateCamera(int cameraIdentifier, glm::vec3 position, glm::quat orientation, float FOV, float nearClip, float farClip); +#pragma region NEWSTUFF + void SetViewport(const Rectangle &viewport) + { + m_Viewport = viewport; + } + + void SetCamera(std::shared_ptr camera) + { + m_Camera = camera; + } + + void DrawFrame(RenderQueue &rq); + void DrawWorld(RenderQueue &rq); + void Swap(); + +#pragma endregion + void AddModelToDraw(Model* model, glm::vec3 position, glm::quat orientation, glm::vec3 scale, bool visible, bool shadowCaster); void AddTextureToDraw(Texture* texture, glm::vec3 position, glm::quat orientation, glm::vec3 scale); void AddTextToDraw(); @@ -50,8 +69,11 @@ public: float _specularExponent, float _ConstantAttenuation, float _LinearAttenuation, - float _QuadraticAttenuation + float _QuadraticAttenuation, + float _radius ); + void ClearPointLights(); + void AddAABBToDraw(glm::vec3 origin, glm::vec3 volumeVector, bool colliding); void LoadContent(); @@ -70,6 +92,8 @@ public: void SetSphereModel(Model* _model); private: + std::shared_ptr<::ResourceManager> ResourceManager; + int m_Width, m_Height; struct Viewport @@ -84,6 +108,9 @@ private: std::unordered_map m_Viewports; std::unordered_map> m_Cameras; + Rectangle m_Viewport; + std::shared_ptr m_Camera; + struct Light { glm::vec3 Position; @@ -91,7 +118,7 @@ private: glm::vec3 Diffuse; float SpecularExponent; glm::mat4 SphereModelMatrix; - float ConstantAttenuation, LinearAttenuation, QuadraticAttenuation; + float ConstantAttenuation, LinearAttenuation, QuadraticAttenuation, Radius; }; float Gamma; @@ -113,6 +140,10 @@ private: glm::vec3 m_SunPosition; glm::vec3 m_SunTarget; glm::mat4 m_SunProjection; + glm::vec2 m_SunProjection_width; + glm::vec2 m_SunProjection_height; + glm::vec2 m_SunProjection_length; + GLuint m_DebugAABB; GLuint m_ShadowFrameBuffer; @@ -135,13 +166,13 @@ private: bool m_QuadView; - std::shared_ptr m_Camera; - ShaderProgram m_ShaderProgram; ShaderProgram m_FirstPassProgram; ShaderProgram m_SecondPassProgram; ShaderProgram m_SecondPassProgram_Debug; ShaderProgram m_FinalPassProgram; + ShaderProgram m_SunPassProgram; + ShaderProgram m_ForwardRendering; ShaderProgram m_ShaderProgramNormals; ShaderProgram m_ShaderProgramShadows; @@ -154,16 +185,19 @@ private: void ClearStuff(); void DrawScene(); void DrawModels(ShaderProgram &shader); - void DrawShadowMap(); + void DrawShadowMap(RenderQueue &rq); void CreateShadowMap(int resolution); void FrameBufferTextures(); void DrawFBO(); - void DrawFBOScene(Viewport &viewport); - void DrawLightScene(Viewport &viewport); + void DrawFBO2(); + void DrawFBOScene(RenderQueue &rq); + void DrawLightScene(RenderQueue &rq); + void DrawSunLightScene(); void BindFragDataLocation(); glm::mat4 CreateLightMatrix(Light &_light); void UpdateSunProjection(); void CreateNormalMapTangent(); + void ForwardRendering(); GLuint CreateQuad(); diff --git a/src/ShaderProgram.cpp b/src/ShaderProgram.cpp index bd67e90..6357149 100755 --- a/src/ShaderProgram.cpp +++ b/src/ShaderProgram.cpp @@ -155,4 +155,41 @@ void ShaderProgram::Bind() void ShaderProgram::Unbind() { glActiveShaderProgram(0, 0); -} \ No newline at end of file +} + +void ShaderProgram::LoadFromFolder(std::string folderPath) +{ + auto path = boost::filesystem::path(folderPath); + + if (!boost::filesystem::is_directory(path)) + { + LOG_ERROR("Failed to load shader program: \"%s\" is not a directory", folderPath.c_str()); + return; + } + + for (auto it = boost::filesystem::directory_iterator(path); it != boost::filesystem::directory_iterator(); it++) + { + std::string filename = it->path().filename().string(); + if (filename == "Vertex.glsl") + { + AddShader(std::shared_ptr(new VertexShader(filename))); + } + else if (filename == "Fragment.glsl") + { + AddShader(std::shared_ptr(new FragmentShader(filename))); + + } + else if (filename == "Geometry.glsl") + { + AddShader(std::shared_ptr(new GeometryShader(filename))); + } + } +} + +void ShaderProgram::BindFragDataLocation(int colorNumber, std::string name) +{ + if (m_ShaderProgramHandle == 0) + return; + + glBindFragDataLocation(m_ShaderProgramHandle, colorNumber, name.c_str()); +} diff --git a/src/ShaderProgram.h b/src/ShaderProgram.h index a27300c..f071d7f 100755 --- a/src/ShaderProgram.h +++ b/src/ShaderProgram.h @@ -6,6 +6,11 @@ #include #include +#include +#include + +#include "ResourceManager.h" + class Shader { public: @@ -57,23 +62,32 @@ public: : ShaderType(fileName) { } }; -class ShaderProgram +class ShaderProgram : public Resource { public: ShaderProgram() - : m_ShaderProgramHandle(0) { } + : m_ShaderProgramHandle(0) + { } + ShaderProgram(std::string folderPath) + : m_ShaderProgramHandle(0) + { } + ~ShaderProgram(); void AddShader(std::shared_ptr shader); + void BindFragDataLocation(int colorNumber, std::string name); void Compile(); GLuint Link(); GLuint GetHandle(); + operator GLuint() const { return m_ShaderProgramHandle; } void Bind(); void Unbind(); private: GLuint m_ShaderProgramHandle; std::vector> m_Shaders; + + void LoadFromFolder(std::string folderPath); }; #endif // ShaderProgram_h__ \ No newline at end of file diff --git a/src/Shaders/FinalPass.frag.glsl b/src/Shaders/FinalPass.frag.glsl index 0bdb1b1..26073a2 100644 --- a/src/Shaders/FinalPass.frag.glsl +++ b/src/Shaders/FinalPass.frag.glsl @@ -21,9 +21,10 @@ void main() vec4 LightingTexel = texture(LightingTexture, Input.TextureCoord); vec4 ShadowTexel = texture(ShadowTexture, Input.TextureCoord); - - vec4 _FragmentColor = DiffuseTexel * vec4(La, 1.0) + LightingTexel; - FragmentColor = vec4(pow(_FragmentColor.rgb, vec3(1.0 / Gamma)), _FragmentColor.a); + //FragmentColor = 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 new file mode 100644 index 0000000..b76f3aa --- /dev/null +++ b/src/Shaders/ForwardRendering.frag.glsl @@ -0,0 +1,20 @@ +#version 430 + +uniform vec4 Color; + +layout(binding=0) uniform sampler2D texture0; + +in VertexData { + vec3 Position; + vec3 Normal; + vec2 TextureCoord; +} Input; + +out vec4 fragmentColor; + +void main() { + // Texture + vec4 texel = texture(texture0, Input.TextureCoord); + + fragmentColor = texel * Color; +} \ No newline at end of file diff --git a/src/Shaders/ForwardRendering.vert.glsl b/src/Shaders/ForwardRendering.vert.glsl new file mode 100644 index 0000000..3a876d3 --- /dev/null +++ b/src/Shaders/ForwardRendering.vert.glsl @@ -0,0 +1,25 @@ +#version 430 + +uniform mat4 MVP; +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 vec2 TextureCoord; + +out VertexData { + vec3 Position; + vec3 Normal; + vec2 TextureCoord; +} Output; + +void main() +{ + gl_Position = MVP * vec4(Position, 1.0); + + Output.Position = Position; + Output.Normal = Normal; + Output.TextureCoord = TextureCoord; +} \ No newline at end of file diff --git a/src/Shaders/Fragment.glsl b/src/Shaders/Fragment.glsl index 47af3a6..51a2cf5 100755 --- a/src/Shaders/Fragment.glsl +++ b/src/Shaders/Fragment.glsl @@ -5,6 +5,15 @@ 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; in VertexData { @@ -19,16 +28,28 @@ in VertexData out vec4 frag_Diffuse; out vec4 frag_Position; out vec4 frag_Normal; -out vec4 frag_specular; +out vec4 frag_Specular; -float Shadow(vec4 ShadowCoord) +float Shadow(vec4 ShadowCoord, vec3 normal) { - //float cosTheta = clamp(dot(Input.Normal, 1.0), 0.0, 1.0); - float bias = 0.0005; // cosTheta is dot( n,l ), clamped between 0 and 1 - bias = clamp(bias, 0.0, 0.01); - if( texture(ShadowTexture, Input.ShadowCoord.xy).z < ShadowCoord.z - bias) + return 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.3; + return 0.6; } else { @@ -38,18 +59,38 @@ float Shadow(vec4 ShadowCoord) 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 + + - // Diffuse Texture - frag_Diffuse = texture(DiffuseTexture, Input.TextureCoord); // * Shadow(Input.ShadowCoord); // G-buffer Position frag_Position = vec4(Input.Position.xyz, 1.0); // G-buffer Normal - mat3 TBN = transpose(mat3(Input.Tangent, Input.BiTangent, Input.Normal)); + mat3 TBN = mat3(Input.Tangent, Input.BiTangent, Input.Normal); frag_Normal = normalize(vec4(TBN * vec3(texture(NormalMapTexture, Input.TextureCoord)), 0.0)); + //frag_Diffuse = normalize(vec4(TBN * vec3(texture(NormalMapTexture, Input.TextureCoord)), 0.0)); //frag_Normal = vec4(Input.Normal, 0.0); + // Diffuse Texture + //frag_Diffuse = tex; + frag_Diffuse = texture(DiffuseTexture, Input.TextureCoord) * Shadow(Input.ShadowCoord, vec3(frag_Normal)); + //G-buffer Specular - frag_specular = texture(SpecularMapTexture, Input.TextureCoord); + frag_Specular = texture(SpecularMapTexture, Input.TextureCoord); } \ No newline at end of file diff --git a/src/Shaders/Fragment2.glsl b/src/Shaders/Fragment2.glsl index 5ded0a2..27dcb76 100644 --- a/src/Shaders/Fragment2.glsl +++ b/src/Shaders/Fragment2.glsl @@ -2,6 +2,7 @@ layout (binding=0) uniform sampler2D PositionTexture; layout (binding=1) uniform sampler2D NormalsTexture; +layout (binding=2) uniform sampler2D SpecularTexture; uniform vec2 ViewportSize; uniform mat4 MVP; @@ -17,12 +18,14 @@ uniform vec3 CameraPosition; uniform float ConstantAttenuation; uniform float LinearAttenuation; uniform float QuadraticAttenuation; +uniform float LightRadius; const vec3 ks = vec3(1.0, 1.0, 1.0); const vec3 kd = vec3(1.0, 1.0, 1.0); const vec3 ka = vec3(1.0, 1.0, 1.0); const float kshine = 1.0; + in VertexData { vec3 Position; @@ -31,7 +34,7 @@ in VertexData out vec4 FragColor; -vec4 phong(vec3 position, vec3 normal) +vec4 phong(vec3 position, vec3 normal, vec3 specular) { // Diffuse vec3 lightPos = vec3(V * vec4(lp, 1.0)); @@ -46,32 +49,15 @@ vec4 phong(vec3 position, vec3 normal) vec3 surfaceToViewer = normalize(-position); vec3 halfWay = normalize(surfaceToViewer + directionToLight); float dotSpecular = max(dot(halfWay, normal), 0.0); - float specularFactor = pow(dotSpecular, specularExponent * 2.0); - vec3 Is = ks * ls * specularFactor; + float specularFactor = pow(dotSpecular, specularExponent); + vec3 Is = specular.r * ls * specularFactor; //Attenuation float dist = distance(lightPos, position); - //float attenuation = -log(min(1.0, dist / LightRadius)); - float attenuation = 1.0 / (ConstantAttenuation + (LinearAttenuation * dist) + (QuadraticAttenuation * dist * dist)); + float attenuation = pow(max(0.0f, 1.0 - (dist / LightRadius)), 2); - //float attenuation = 1.0 / (1.0 - 0.0001 * pow(dist, 2)); - - //float attenuation = clamp(0.0, 1.0, 1.0 / (0.001 + (0.001 * dist) + (0.001 * dist * dist))); - - //float attenuation = 1.0 / dot(directionToLight, directionToLight); - - //float att_s = 5; - //float attenuation = pow(dist, 2) / pow(5.0, 2); - //attenuation = 1.0 / (1.0 + attenuation * att_s); - //att_s = 1.0 / (1.0 + att_s); - //attenuation = attenuation / (1.0 - att_s); - - //float radius = 5.0; - //float alpha = dist / radius; - //float dampingFactor = 1.0 - pow(alpha, 3); - - return vec4((Id + Is) * attenuation, 1.0); + return vec4((Id) * attenuation, Is.r * attenuation); } void main() @@ -79,7 +65,8 @@ void main() vec2 TextureCoord = gl_FragCoord.xy / ViewportSize; vec4 PositionTexel = texture(PositionTexture, TextureCoord); vec4 NormalTexel = texture(NormalsTexture, TextureCoord); + vec4 SpecularTexel = texture(SpecularTexture, TextureCoord); - FragColor = phong(vec3(PositionTexel), vec3(NormalTexel)); + FragColor = phong(vec3(PositionTexel), vec3(NormalTexel), vec3(SpecularTexel)); //FragColor = NormalTexel; } \ No newline at end of file diff --git a/src/Shaders/SunPass.frag.glsl b/src/Shaders/SunPass.frag.glsl new file mode 100644 index 0000000..6f80c40 --- /dev/null +++ b/src/Shaders/SunPass.frag.glsl @@ -0,0 +1,61 @@ +#version 430 + +layout (binding=0) uniform sampler2D PositionTexture; +layout (binding=1) uniform sampler2D NormalsTexture; +layout (binding=2) uniform sampler2D SpecularTexture; + +uniform vec2 ViewportSize; +uniform mat4 MVP; +uniform mat4 M; +uniform mat4 V; +uniform mat4 P; +uniform vec3 CameraPosition; +uniform vec3 directionToSun; + +const vec3 ks = vec3(1.0, 1.0, 1.0); +const vec3 kd = vec3(1.0, 1.0, 1.0); +const vec3 ka = vec3(1.0, 1.0, 1.0); +const float kshine = 1.0; +const vec3 SunDiffuseLight = vec3(0.3, 0.3, 0.3); +const vec3 SunSpecularLight = vec3(1.0, 1.0, 1.0); +const vec3 SunPos = directionToSun*vec3(100); +const float specularExponent = 5; + + +in VertexData +{ + vec3 Position; + vec2 TextureCoord; +} Input; + +out vec4 FragColor; + +vec4 phong(vec3 position, vec3 normal, vec3 specular) +{ + + //Diffuse Sunlight + vec3 directionToLight = normalize(vec3(V * vec4(directionToSun, 0.0))); + float dotProdLight = dot(directionToLight, normal); + dotProdLight = max(dotProdLight, 0.0); + vec3 sId = kd * SunDiffuseLight * dotProdLight; + + //Specular Sunlight + vec3 surfaceToViewer = normalize(-position); + vec3 halfWay = normalize(surfaceToViewer + directionToLight); + float dotSpecular = max(dot(halfWay, normal), 0.0); + float specularFactorSun = pow(dotSpecular, specularExponent); + vec3 sIs = specular.r * SunSpecularLight * specularFactorSun; + + return vec4((sId), sIs.r); +} + +void main() +{ + vec2 TextureCoord = gl_FragCoord.xy / ViewportSize; + vec4 PositionTexel = texture(PositionTexture, TextureCoord); + vec4 NormalTexel = texture(NormalsTexture, TextureCoord); + vec4 SpecularTexel = texture(SpecularTexture, TextureCoord); + + FragColor = phong(vec3(PositionTexel), vec3(normalize(NormalTexel)), vec3(SpecularTexel)); + //FragColor = NormalTexel; +} \ No newline at end of file diff --git a/src/Shaders/SunPass.vert.glsl b/src/Shaders/SunPass.vert.glsl new file mode 100644 index 0000000..d743489 --- /dev/null +++ b/src/Shaders/SunPass.vert.glsl @@ -0,0 +1,21 @@ +#version 430 + +uniform mat4 MVP; + +layout (location = 0) in vec3 Position; +layout (location = 2) in vec2 TextureCoord; + +uniform mat4 depthBiasMVP; + +out VertexData +{ + vec3 Position; + vec2 TextureCoord; +} Output; + +void main() +{ + gl_Position = MVP * vec4(Position, 1.0); + Output.Position = Position; + Output.TextureCoord = (vec2(Position) + 1.0) / 2.0; +} \ No newline at end of file diff --git a/src/System.h b/src/System.h index f29aeb5..9e94dab 100755 --- a/src/System.h +++ b/src/System.h @@ -12,13 +12,15 @@ class World; class System { public: - System(World* world, std::shared_ptr eventBroker) + System(World* world, std::shared_ptr eventBroker, std::shared_ptr resourceManager) : m_World(world) - , EventBroker(eventBroker) { } + , EventBroker(eventBroker) + , ResourceManager(resourceManager) + { } virtual ~System() { } virtual void RegisterComponents(ComponentFactory* cf) { } - virtual void RegisterResourceTypes(ResourceManager* rm) { } + virtual void RegisterResourceTypes(std::shared_ptr<::ResourceManager> rm) { } virtual void Initialize() { } @@ -38,6 +40,7 @@ public: protected: World* m_World; std::shared_ptr EventBroker; + std::shared_ptr ResourceManager; }; class SystemFactory : public Factory { }; diff --git a/src/Systems/DamageSystem.cpp b/src/Systems/DamageSystem.cpp index 9ba2b82..55117dd 100644 --- a/src/Systems/DamageSystem.cpp +++ b/src/Systems/DamageSystem.cpp @@ -16,7 +16,7 @@ void Systems::DamageSystem::Initialize() bool Systems::DamageSystem::OnDamage( const Events::Damage &event ) { auto health = m_World->GetComponent(event.Entity); - health->health -= event.damage; - LOG_INFO("Damaged entity %i, Health left: %f", event.Entity, health->health); + health->Amount -= event.Amount; + LOG_INFO("Damaged entity %i, Health left: %f", event.Entity, health->Amount); return true; } \ No newline at end of file diff --git a/src/Systems/DamageSystem.h b/src/Systems/DamageSystem.h index 215b9df..5b81f17 100644 --- a/src/Systems/DamageSystem.h +++ b/src/Systems/DamageSystem.h @@ -12,8 +12,8 @@ namespace Systems { public: - DamageSystem(World* world, std::shared_ptr<::EventBroker> eventBroker) - : System(world, eventBroker) { } + DamageSystem(World* world, std::shared_ptr<::EventBroker> eventBroker, std::shared_ptr<::ResourceManager> resourceManager) + : System(world, eventBroker, resourceManager) { } void Initialize() override; diff --git a/src/Systems/DebugSystem.h b/src/Systems/DebugSystem.h index bbf7cd5..afdc091 100644 --- a/src/Systems/DebugSystem.h +++ b/src/Systems/DebugSystem.h @@ -12,8 +12,9 @@ namespace Systems class DebugSystem : public System { public: - DebugSystem(World* world, std::shared_ptr<::EventBroker> eventBroker) - : System(world, eventBroker) { } + DebugSystem(World* world, std::shared_ptr<::EventBroker> eventBroker, std::shared_ptr<::ResourceManager> resourceManager) + : System(world, eventBroker, resourceManager) + { } void Initialize() override; diff --git a/src/Systems/FreeSteeringSystem.cpp b/src/Systems/FreeSteeringSystem.cpp index efbfde1..61ef06d 100755 --- a/src/Systems/FreeSteeringSystem.cpp +++ b/src/Systems/FreeSteeringSystem.cpp @@ -40,6 +40,7 @@ void Systems::FreeSteeringSystem::UpdateEntity(double dt, EntityID entity, Entit glm::quat mouseOrientationPitch = glm::quat(m_InputController->MouseOrientation * glm::vec3(1, 0, 0)); glm::quat mouseOrientationYaw = glm::quat(m_InputController->MouseOrientation * glm::vec3(0, 1, 0)); + m_InputController->MouseOrientation = glm::vec3(0); glm::vec3 controllerOrientationEuler = m_InputController->ControllerOrientation * (float)dt; glm::quat controllerOrientationPitch = glm::quat(controllerOrientationEuler * glm::vec3(1, 0, 0)); @@ -53,8 +54,6 @@ void Systems::FreeSteeringSystem::UpdateEntity(double dt, EntityID entity, Entit //--------------------------------------------------------------------- // TOUCHING THIS CODE MIGHT CAUSE THE UNIVERSE TO IMPLODE, ALSO DRAGONS } - - m_InputController->MouseOrientation = glm::vec3(0); } bool Systems::FreeSteeringSystem::FreeSteeringInputController::OnCommand(const Events::InputCommand &event) @@ -80,7 +79,7 @@ bool Systems::FreeSteeringSystem::FreeSteeringInputController::OnCommand(const E } // Mouse click - else if (event.Command == "cam_attack") + else if (event.Command == "cam_lock") { OrientationActive = event.Value > 0; @@ -113,6 +112,7 @@ bool Systems::FreeSteeringSystem::FreeSteeringInputController::OnMouseMove(const if (OrientationActive) { MouseOrientation = -glm::vec3(event.DeltaY / 300.f, event.DeltaX / 300.f, 0.f); + LOG_DEBUG("Mouse DX: %f", event.DeltaX); } return true; diff --git a/src/Systems/FreeSteeringSystem.h b/src/Systems/FreeSteeringSystem.h index 61a453d..29347b0 100755 --- a/src/Systems/FreeSteeringSystem.h +++ b/src/Systems/FreeSteeringSystem.h @@ -12,8 +12,9 @@ namespace Systems class FreeSteeringSystem : public System { public: - FreeSteeringSystem(World* world, std::shared_ptr<::EventBroker> eventBroker) - : System(world, eventBroker) { } + FreeSteeringSystem(World* world, std::shared_ptr<::EventBroker> eventBroker, std::shared_ptr<::ResourceManager> resourceManager) + : System(world, eventBroker, resourceManager) + { } void RegisterComponents(ComponentFactory* cf) override; void Initialize() override; diff --git a/src/Systems/HelicopterSteeringSystem.h b/src/Systems/HelicopterSteeringSystem.h index 742ce70..e6261be 100644 --- a/src/Systems/HelicopterSteeringSystem.h +++ b/src/Systems/HelicopterSteeringSystem.h @@ -11,8 +11,9 @@ namespace Systems class HelicopterSteeringSystem : public System { public: - HelicopterSteeringSystem(World* world, std::shared_ptr<::EventBroker> eventBroker) - : System(world, eventBroker) { } + HelicopterSteeringSystem(World* world, std::shared_ptr<::EventBroker> eventBroker, std::shared_ptr<::ResourceManager> resourceManager) + : System(world, eventBroker, resourceManager) + { } void RegisterComponents(ComponentFactory* cf) override; void Initialize() override; diff --git a/src/Systems/InputSystem.h b/src/Systems/InputSystem.h index 9d51d2a..5a193a6 100755 --- a/src/Systems/InputSystem.h +++ b/src/Systems/InputSystem.h @@ -25,8 +25,9 @@ namespace Systems class InputSystem : public System { public: - InputSystem(World* world, std::shared_ptr<::EventBroker> eventBroker) - : System(world, eventBroker) { } + InputSystem(World* world, std::shared_ptr<::EventBroker> eventBroker, std::shared_ptr<::ResourceManager> resourceManager) + : System(world, eventBroker, resourceManager) + { } void RegisterComponents(ComponentFactory* cf) override; void Initialize() override; diff --git a/src/Systems/ParticleSystem.h b/src/Systems/ParticleSystem.h index fad8815..e11e73c 100644 --- a/src/Systems/ParticleSystem.h +++ b/src/Systems/ParticleSystem.h @@ -27,8 +27,9 @@ namespace Systems class ParticleSystem : public System { public: - ParticleSystem(World* world, std::shared_ptr<::EventBroker> eventBroker) - : System(world, eventBroker) { } + ParticleSystem(World* world, std::shared_ptr<::EventBroker> eventBroker, std::shared_ptr<::ResourceManager> resourceManager) + : System(world, eventBroker, resourceManager) + { } void RegisterComponents(ComponentFactory* cf) override; void Update(double dt) override; diff --git a/src/Systems/PhysicsSystem.cpp b/src/Systems/PhysicsSystem.cpp index 612355c..a9d4e76 100644 --- a/src/Systems/PhysicsSystem.cpp +++ b/src/Systems/PhysicsSystem.cpp @@ -614,7 +614,7 @@ void Systems::PhysicsSystem::OnEntityCommit( EntityID entity ) { std::vector* vertices = new std::vector; std::vector* vertexIndices = new std::vector; - auto meshShape = m_World->GetResourceManager()->Load("OBJ", meshShapeComponent->ResourceName); + auto meshShape = ResourceManager->Load("OBJ", meshShapeComponent->ResourceName); for (auto &vertex : meshShape->Vertices) { diff --git a/src/Systems/PhysicsSystem.h b/src/Systems/PhysicsSystem.h index b8138f4..8512017 100644 --- a/src/Systems/PhysicsSystem.h +++ b/src/Systems/PhysicsSystem.h @@ -147,8 +147,8 @@ public: }; friend class PhantomCallbackShape; - PhysicsSystem(World* world, std::shared_ptr<::EventBroker> eventBroker) - : System(world, eventBroker) { } + PhysicsSystem(World* world, std::shared_ptr<::EventBroker> eventBroker, std::shared_ptr<::ResourceManager> resourceManager) + : System(world, eventBroker, resourceManager) { } void RegisterComponents(ComponentFactory* cf) override; void Initialize() override; diff --git a/src/Systems/RenderSystem.cpp b/src/Systems/RenderSystem.cpp index 43268ce..e101f30 100755 --- a/src/Systems/RenderSystem.cpp +++ b/src/Systems/RenderSystem.cpp @@ -2,11 +2,9 @@ #include "RenderSystem.h" #include "World.h" -void Systems::RenderSystem::RegisterResourceTypes(ResourceManager* rm) +void Systems::RenderSystem::RegisterResourceTypes(std::shared_ptr<::ResourceManager> rm) { - rm->RegisterType("Model", [rm](std::string resourceName) { return new Model(rm, *rm->Load("OBJ", resourceName)); }); - rm->RegisterType("OBJ", [](std::string resourceName) { return new OBJ(resourceName); }); - rm->RegisterType("Texture", [](std::string resourceName) { return new Texture(resourceName); }); + rm->RegisterType("Shader", [](std::string resourceName) { return new ShaderProgram(resourceName); }); } void Systems::RenderSystem::RegisterComponents(ComponentFactory* cf) @@ -21,100 +19,99 @@ void Systems::RenderSystem::RegisterComponents(ComponentFactory* cf) void Systems::RenderSystem::OnEntityCommit(EntityID entity) { - auto transform = m_World->GetComponent(entity); + //auto transform = m_World->GetComponent(entity); - auto camera = m_World->GetComponent(entity); - if (transform && camera) - { - m_Renderer->RegisterCamera(entity, camera->FOV, camera->NearClip, camera->FarClip); - m_Renderer->UpdateCamera(entity, m_TransformSystem->AbsolutePosition(entity), m_TransformSystem->AbsoluteOrientation(entity), camera->FOV, camera->NearClip, camera->FarClip); - } + //auto camera = m_World->GetComponent(entity); + //if (transform && camera) + //{ + // m_Renderer->RegisterCamera(entity, camera->FOV, camera->NearClip, camera->FarClip); + // m_Renderer->UpdateCamera(entity, m_TransformSystem->AbsolutePosition(entity), m_TransformSystem->AbsoluteOrientation(entity), camera->FOV, camera->NearClip, camera->FarClip); + //} - auto viewport = m_World->GetComponent(entity); - if (viewport) - { - m_Renderer->RegisterViewport(entity, viewport->Left, viewport->Top, viewport->Right, viewport->Bottom); - if (viewport->Camera != 0) - { - m_Renderer->UpdateViewport(entity, viewport->Camera); - } - } + //auto viewport = m_World->GetComponent(entity); + //if (viewport) + //{ + // m_Renderer->RegisterViewport(entity, viewport->Left, viewport->Top, viewport->Right, viewport->Bottom); + // if (viewport->Camera != 0) + // { + // m_Renderer->UpdateViewport(entity, viewport->Camera); + // } + //} } void Systems::RenderSystem::UpdateEntity(double dt, EntityID entity, EntityID parent) { - auto templateComponent = m_World->GetComponent(entity); - if (templateComponent) - return; + //auto templateComponent = m_World->GetComponent(entity); + //if (templateComponent) + // return; - auto transformComponent = m_World->GetComponent(entity); + //auto transformComponent = m_World->GetComponent(entity); - // Draw models - auto modelComponent = m_World->GetComponent(entity); - if (transformComponent && modelComponent) - { - auto model = m_World->GetResourceManager()->Load("Model", modelComponent->ModelFile); - if (model) - { - /*glm::vec3 position = m_TransformSystem->AbsolutePosition(entity); - glm::quat orientation = m_TransformSystem->AbsoluteOrientation(entity); - glm::vec3 scale = m_TransformSystem->AbsoluteScale(entity);*/ - Components::Transform absoluteTransform = m_TransformSystem->AbsoluteTransform(entity); - m_Renderer->AddModelToDraw(model, absoluteTransform.Position, absoluteTransform.Orientation, absoluteTransform.Scale, modelComponent->Visible, modelComponent->ShadowCaster); - } - } + //// Draw models + //auto modelComponent = m_World->GetComponent(entity); + //if (transformComponent && modelComponent) + //{ + // auto model = m_World->ResourceManager->Load("Model", modelComponent->ModelFile); + // if (model) + // { + // /*glm::vec3 position = m_TransformSystem->AbsolutePosition(entity); + // glm::quat orientation = m_TransformSystem->AbsoluteOrientation(entity); + // glm::vec3 scale = m_TransformSystem->AbsoluteScale(entity);*/ + // Components::Transform absoluteTransform = m_TransformSystem->AbsoluteTransform(entity); + // m_Renderer->AddModelToDraw(model, absoluteTransform.Position, absoluteTransform.Orientation, absoluteTransform.Scale, modelComponent->Visible, modelComponent->ShadowCaster); + // } + //} - auto pointLightComponent = m_World->GetComponent(entity); - if (transformComponent && pointLightComponent) - { - glm::vec3 position = m_TransformSystem->AbsolutePosition(entity); - m_Renderer->AddPointLightToDraw( - position, - pointLightComponent->Specular, - pointLightComponent->Diffuse, - pointLightComponent->specularExponent, - pointLightComponent->ConstantAttenuation, - pointLightComponent->LinearAttenuation, - pointLightComponent->QuadraticAttenuation - ); - } + //auto pointLightComponent = m_World->GetComponent(entity); + //if (transformComponent && pointLightComponent) + //{ + // glm::vec3 position = m_TransformSystem->AbsolutePosition(entity); + // m_Renderer->AddPointLightToDraw( + // position, + // pointLightComponent->Specular, + // pointLightComponent->Diffuse, + // pointLightComponent->specularExponent, + // pointLightComponent->ConstantAttenuation, + // pointLightComponent->LinearAttenuation, + // pointLightComponent->QuadraticAttenuation + // ); + //} - auto cameraComponent = m_World->GetComponent(entity); - if (transformComponent && cameraComponent) - { - m_Renderer->UpdateCamera(entity - , m_TransformSystem->AbsolutePosition(entity) - , m_TransformSystem->AbsoluteOrientation(entity) - , cameraComponent->FOV - , cameraComponent->NearClip - , cameraComponent->FarClip); - } + //auto cameraComponent = m_World->GetComponent(entity); + //if (transformComponent && cameraComponent) + //{ + // m_Renderer->UpdateCamera(entity + // , m_TransformSystem->AbsolutePosition(entity) + // , m_TransformSystem->AbsoluteOrientation(entity) + // , cameraComponent->FOV + // , cameraComponent->NearClip + // , cameraComponent->FarClip); + //} - auto viewportComponent = m_World->GetComponent(entity); - if (viewportComponent) - { - if (viewportComponent->Camera != 0) - { - m_Renderer->UpdateViewport(entity, viewportComponent->Camera); - } - } + //auto viewportComponent = m_World->GetComponent(entity); + //if (viewportComponent) + //{ + // if (viewportComponent->Camera != 0) + // { + // m_Renderer->UpdateViewport(entity, viewportComponent->Camera); + // } + //} - auto spriteComponent = m_World->GetComponent(entity); - if (transformComponent && spriteComponent) - { - //TEMP - Texture* texture = m_World->GetResourceManager()->Load("Texture", spriteComponent->SpriteFile); - //glBindTexture(GL_TEXTURE_2D, texture); - auto transform = m_World->GetComponent(spriteComponent->Entity); - glm::quat orientation2D = glm::angleAxis(glm::eulerAngles(transform->Orientation).z, glm::vec3(0, 0, -1)); - m_Renderer->AddTextureToDraw(texture, transform->Position, orientation2D, transform->Scale); - } + //auto spriteComponent = m_World->GetComponent(entity); + //if (transformComponent && spriteComponent) + //{ + // //TEMP + // Texture* texture = m_World->ResourceManager->Load("Texture", spriteComponent->SpriteFile); + // //glBindTexture(GL_TEXTURE_2D, texture); + // auto transform = m_World->GetComponent(spriteComponent->Entity); + // glm::quat orientation2D = glm::angleAxis(glm::eulerAngles(transform->Orientation).z, glm::vec3(0, 0, -1)); + // m_Renderer->AddTextureToDraw(texture, transform->Position, orientation2D, transform->Scale); + //} } void Systems::RenderSystem::Initialize() { - m_TransformSystem = m_World->GetSystem(); - - m_Renderer->SetSphereModel(m_World->GetResourceManager()->Load("Model", "Models/Placeholders/PhysicsTest/Sphere.obj")); -} + //m_TransformSystem = m_World->GetSystem(); + //m_Renderer->SetSphereModel(m_World->ResourceManager->Load("Model", "Models/Placeholders/PhysicsTest/Sphere.obj")); +} \ No newline at end of file diff --git a/src/Systems/RenderSystem.h b/src/Systems/RenderSystem.h index 6268ed6..e6350db 100755 --- a/src/Systems/RenderSystem.h +++ b/src/Systems/RenderSystem.h @@ -5,6 +5,7 @@ #include "System.h" #include "Systems/TransformSystem.h" +#include "ShaderProgram.h" #include "Model.h" #include "Texture.h" #include "Components/Transform.h" @@ -14,10 +15,11 @@ #include "Components/PointLight.h" #include "Components/DirectionalLight.h" #include "Components/Viewport.h" - #include "Components/Template.h" #include "Components/Transform.h" #include "Renderer.h" +#include "RenderQueue.h" +#include "Events/SetViewportCamera.h" namespace Systems { @@ -25,12 +27,12 @@ namespace Systems class RenderSystem : public System { public: - RenderSystem(World* world, std::shared_ptr<::EventBroker> eventBroker, std::shared_ptr renderer) - : System(world, eventBroker) - , m_Renderer(renderer) { } + RenderSystem(World* world, std::shared_ptr<::EventBroker> eventBroker, std::shared_ptr<::ResourceManager> resourceManager) + : System(world, eventBroker, resourceManager) + { } void RegisterComponents(ComponentFactory* cf) override; - void RegisterResourceTypes(ResourceManager* rm) override; + void RegisterResourceTypes(std::shared_ptr<::ResourceManager> rm) override; void Initialize() override; std::unordered_map> m_CachedModels; @@ -38,12 +40,12 @@ public: void OnEntityCommit(EntityID entity) override; void UpdateEntity(double dt, EntityID entity, EntityID parent) override; - - - private: - std::shared_ptr m_Renderer; std::shared_ptr m_TransformSystem; + + void EnqueueModel(Model* model, glm::mat4 modelMatrix); + void EnqueueSprite(Texture* texture, glm::mat4 modelMatrix); + }; diff --git a/src/Systems/SoundSystem.cpp b/src/Systems/SoundSystem.cpp index ffb81b8..4ce23eb 100755 --- a/src/Systems/SoundSystem.cpp +++ b/src/Systems/SoundSystem.cpp @@ -32,7 +32,7 @@ void Systems::SoundSystem::RegisterComponents(ComponentFactory* cf) cf->Register([]() { return new Components::SoundEmitter(); }); } -void Systems::SoundSystem::RegisterResourceTypes(ResourceManager* rm) +void Systems::SoundSystem::RegisterResourceTypes(std::shared_ptr<::ResourceManager> rm) { rm->RegisterType("Sound", [](std::string resourceName) { return new Sound(resourceName); }); } @@ -94,7 +94,7 @@ void Systems::SoundSystem::PlaySound(Components::SoundEmitter* emitter, std::str if (m_Sources.find(emitter) == m_Sources.end()) return; - ALuint buffer = *m_World->GetResourceManager()->Load("Sound", fileName); + ALuint buffer = *ResourceManager->Load("Sound", fileName); if (buffer == 0) return; ALuint source = m_Sources[emitter]; @@ -104,7 +104,7 @@ void Systems::SoundSystem::PlaySound(Components::SoundEmitter* emitter, std::str void Systems::SoundSystem::PlaySound(std::shared_ptr emitter) { - ALuint buffer = *m_World->GetResourceManager()->Load("Sound", emitter->Path); + ALuint buffer = *ResourceManager->Load("Sound", emitter->Path); ALuint source = m_Sources[emitter.get()]; alSourcei(source, AL_BUFFER, buffer); alSourcePlay(m_Sources[emitter.get()]); @@ -151,7 +151,7 @@ bool Systems::SoundSystem::OnPlaySound(const Events::PlaySound &event) { LOG_DEBUG("Events::PlaySound.Resource = %s", event.Resource.c_str()); - ALuint buffer = *m_World->GetResourceManager()->Load("Sound", event.Resource); + ALuint buffer = *ResourceManager->Load("Sound", event.Resource); ALuint source = m_Sources.begin()->second; alSourcei(source, AL_BUFFER, buffer); alSourcePlay(source); diff --git a/src/Systems/SoundSystem.h b/src/Systems/SoundSystem.h index f885b60..b738a4b 100755 --- a/src/Systems/SoundSystem.h +++ b/src/Systems/SoundSystem.h @@ -16,11 +16,12 @@ namespace Systems class SoundSystem : public System { public: - SoundSystem(World* world, std::shared_ptr<::EventBroker> eventBroker) - : System(world, eventBroker) { } + SoundSystem(World* world, std::shared_ptr<::EventBroker> eventBroker, std::shared_ptr<::ResourceManager> resourceManager) + : System(world, eventBroker, resourceManager) + { } void RegisterComponents(ComponentFactory* cf) override; - void RegisterResourceTypes(ResourceManager* rm) override; + void RegisterResourceTypes(std::shared_ptr<::ResourceManager> rm) override; void Initialize() override; void Update(double dt) override; diff --git a/src/Systems/TankSteeringSystem.cpp b/src/Systems/TankSteeringSystem.cpp index bbe6948..2def044 100644 --- a/src/Systems/TankSteeringSystem.cpp +++ b/src/Systems/TankSteeringSystem.cpp @@ -150,7 +150,7 @@ bool Systems::TankSteeringSystem::OnCollision( const Events::Collision &e ) { Events::Damage d; d.Entity = physicsEntity; - d.damage = (1.f - pow(distance / radius, 2)) * shellComponent->Damage; + d.Amount = (1.f - pow(distance / radius, 2)) * shellComponent->Damage; EventBroker->Publish(d); } diff --git a/src/Systems/TankSteeringSystem.h b/src/Systems/TankSteeringSystem.h index 1ccb056..9a08e59 100644 --- a/src/Systems/TankSteeringSystem.h +++ b/src/Systems/TankSteeringSystem.h @@ -37,8 +37,9 @@ namespace Systems class TankSteeringSystem : public System { public: - TankSteeringSystem(World* world, std::shared_ptr<::EventBroker> eventBroker) - : System(world, eventBroker) { } + TankSteeringSystem(World* world, std::shared_ptr<::EventBroker> eventBroker, std::shared_ptr<::ResourceManager> resourceManager) + : System(world, eventBroker, resourceManager) + { } void RegisterComponents(ComponentFactory* cf) override; void Initialize() override; diff --git a/src/Systems/TimerSystem.h b/src/Systems/TimerSystem.h index 1e73c96..f025c4e 100644 --- a/src/Systems/TimerSystem.h +++ b/src/Systems/TimerSystem.h @@ -13,8 +13,8 @@ namespace Systems { public: - TimerSystem(World* world, std::shared_ptr<::EventBroker> eventBroker) - : System(world, eventBroker) { } + TimerSystem(World* world, std::shared_ptr<::EventBroker> eventBroker, std::shared_ptr<::ResourceManager> resourceManager) + : System(world, eventBroker, resourceManager) { } void RegisterComponents(ComponentFactory* cf) override; void UpdateEntity(double dt, EntityID entity, EntityID parent) override; diff --git a/src/Systems/TransformSystem.h b/src/Systems/TransformSystem.h index 7852628..993e9d2 100755 --- a/src/Systems/TransformSystem.h +++ b/src/Systems/TransformSystem.h @@ -10,8 +10,9 @@ namespace Systems class TransformSystem : public System { public: - TransformSystem(World* world, std::shared_ptr<::EventBroker> eventBroker) - : System(world, eventBroker) { } + TransformSystem(World* world, std::shared_ptr<::EventBroker> eventBroker, std::shared_ptr<::ResourceManager> resourceManager) + : System(world, eventBroker, resourceManager) + { } //void Update(double dt) override; //void UpdateEntity(double dt, EntityID entity, EntityID parent) override; diff --git a/src/Systems/TriggerSystem.h b/src/Systems/TriggerSystem.h index 0d248c6..dffeb11 100644 --- a/src/Systems/TriggerSystem.h +++ b/src/Systems/TriggerSystem.h @@ -22,8 +22,8 @@ namespace Systems { public: - TriggerSystem(World* world, std::shared_ptr<::EventBroker> eventBroker) - : System(world, eventBroker) { } + TriggerSystem(World* world, std::shared_ptr<::EventBroker> eventBroker, std::shared_ptr<::ResourceManager> resourceManager) + : System(world, eventBroker, resourceManager) { } void Initialize() override; void RegisterComponents(ComponentFactory* cf) override; diff --git a/src/Util/Rectangle.h b/src/Util/Rectangle.h index 0d0db32..ca7e59a 100644 --- a/src/Util/Rectangle.h +++ b/src/Util/Rectangle.h @@ -19,24 +19,24 @@ struct Rectangle int Width; int Height; - const int& GetLeft() const { return X; } + virtual int Left() const { return X; } void SetLeft(int left) { Width += X - left; X = left; } - int GetRight() const { return X + Width; } + virtual int Right() const { return X + Width; } void SetRight(int right) { Width = right - X; } - const int& GetTop() const { return Y; } + virtual int Top() const { return Y; } void SetTop(int top) { Height += Y - top; Y = top; } - int GetBottom() const { return Y + Height; } + virtual int Bottom() const { return Y + Height; } int SetBottom(int bottom) { Height = bottom - Y; @@ -44,15 +44,15 @@ struct Rectangle Rectangle& operator+=(const Rectangle &rhs) { - SetLeft(std::min(GetLeft(), rhs.GetLeft())); - SetRight(std::max(GetRight(), rhs.GetRight())); - SetTop(std::min(GetTop(), rhs.GetTop())); - SetBottom(std::max(GetBottom(), rhs.GetBottom())); + SetLeft(std::min(Left(), rhs.Left())); + SetRight(std::max(Right(), rhs.Right())); + SetTop(std::min(Top(), rhs.Top())); + SetBottom(std::max(Bottom(), rhs.Bottom())); } static bool Intersects(const Rectangle &r1, const Rectangle &r2) { - return !(r2.GetLeft() > r1.GetRight() || r2.GetRight() < r1.GetLeft() || r2.GetTop() > r1.GetBottom() || r2.GetBottom() < r1.GetTop()); + return !(r2.Left() > r1.Right() || r2.Right() < r1.Left() || r2.Top() > r1.Bottom() || r2.Bottom() < r1.Top()); } }; diff --git a/src/World.cpp b/src/World.cpp index 275e750..482cf35 100755 --- a/src/World.cpp +++ b/src/World.cpp @@ -38,7 +38,7 @@ void World::Update(double dt) { const std::string &type = pair.first; auto system = pair.second; - m_EventBroker->Process(type); + EventBroker->Process(type); system->Update(dt); RecursiveUpdate(system, dt, 0); } @@ -136,7 +136,7 @@ void World::Initialize() { auto system = pair.second; system->RegisterComponents(&m_ComponentFactory); - system->RegisterResourceTypes(&m_ResourceManager); + system->RegisterResourceTypes(ResourceManager); system->Initialize(); } } diff --git a/src/World.h b/src/World.h index 475a465..6e0f64f 100755 --- a/src/World.h +++ b/src/World.h @@ -22,8 +22,9 @@ class World { public: - World(std::shared_ptr<::EventBroker> eventBroker) - : m_EventBroker(eventBroker) + World(std::shared_ptr<::EventBroker> eventBroker, std::shared_ptr<::ResourceManager> resourceManager) + : EventBroker(eventBroker) + , ResourceManager(resourceManager) , m_LastEntityID(0) { } ~World() { } @@ -95,14 +96,12 @@ public: std::unordered_map* GetEntities() { return &m_EntityParents; } - ResourceManager* GetResourceManager() { return &m_ResourceManager; } - std::shared_ptr<::EventBroker> EventBroker() { return m_EventBroker; } - protected: - std::shared_ptr<::EventBroker> m_EventBroker; + std::shared_ptr<::EventBroker> EventBroker; + std::shared_ptr<::ResourceManager> ResourceManager; + SystemFactory m_SystemFactory; ComponentFactory m_ComponentFactory; - ResourceManager m_ResourceManager; std::unordered_map> m_Systems; diff --git a/src/main.cpp b/src/main.cpp index 03b63b4..87db645 100755 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,6 +4,7 @@ int main(int argc, char* argv[]) { Engine engine(argc, argv); + LOG_INFO("------------ Engine initialized ------------"); while (engine.Running()) engine.Tick(); diff --git a/vs11/Returngeance.sln b/vs11/Returngeance.sln index 10ce52b..734b64e 100644 --- a/vs11/Returngeance.sln +++ b/vs11/Returngeance.sln @@ -1,6 +1,8 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 2012 +# Visual Studio 2013 +VisualStudioVersion = 12.0.21005.1 +MinimumVisualStudioVersion = 10.0.40219.1 Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Returngeance", "Returngeance\Returngeance.vcxproj", "{E8B4A2A2-882B-402A-98A7-8B4F7233C8B3}" EndProject Project("{F088123C-0E9E-452A-89E6-6BA2F21D5CAC}") = "ModelingProject1", "ModelingProject1\ModelingProject1.modelproj", "{B35F204C-3377-457E-AC9E-D9606F421191}" diff --git a/vs11/Returngeance/Returngeance.vcxproj b/vs11/Returngeance/Returngeance.vcxproj index 7cb70e9..21e0951 100755 --- a/vs11/Returngeance/Returngeance.vcxproj +++ b/vs11/Returngeance/Returngeance.vcxproj @@ -187,12 +187,18 @@ + + + + + + @@ -231,6 +237,8 @@ + + @@ -240,6 +248,8 @@ + + diff --git a/vs11/Returngeance/Returngeance.vcxproj.filters b/vs11/Returngeance/Returngeance.vcxproj.filters index e8db376..2430d0c 100755 --- a/vs11/Returngeance/Returngeance.vcxproj.filters +++ b/vs11/Returngeance/Returngeance.vcxproj.filters @@ -170,6 +170,9 @@ {3c2ea0e5-41a1-4b11-a891-1d59ead7223c} + + {a025d51e-594d-4844-983b-f683726bf1bf} + {9702064a-02a2-4b3c-a2ab-47c23a9cf49c} @@ -391,6 +394,9 @@ Physics\Components + + Physics\Components + Input\Events @@ -454,6 +460,24 @@ Gameplay\Components + + GUI + + + GUI + + + Rendering\Events + + + GUI + + + GUI + + + GUI + @@ -504,5 +528,17 @@ Shaders + + Shaders + + + Shaders + + + Shaders + + + Shaders + \ No newline at end of file diff --git a/vs11/Returngeance1.psess b/vs11/Returngeance1.psess new file mode 100644 index 0000000..3dbec77 --- /dev/null +++ b/vs11/Returngeance1.psess @@ -0,0 +1,89 @@ + + + + Returngeance.sln + Sampling + None + true + true + Timestamp + Cycles + 10000000 + 10 + 10 + + false + + + + false + 500 + + \Memory\Pages/sec + \PhysicalDisk(_Total)\Avg. Disk Queue Length + \Processor(_Total)\% Processor Time + + + + true + false + false + + false + + + false + + + + bin\Debug\Returngeance.exe + 01/01/0001 00:00:00 + true + true + false + false + false + false + false + true + false + Executable + bin\Debug\Returngeance.exe + ..\bin\Debug + + + IIS + InternetExplorer + true + false + + false + + + false + + {E8B4A2A2-882B-402A-98A7-8B4F7233C8B3}|Returngeance\Returngeance.vcxproj + Returngeance\Returngeance.vcxproj + Returngeance + + + + + Returngeance140519.vsp + + + Returngeance140519(1).vsp + + + Returngeance140519(2).vsp + + + Returngeance140519(3).vsp + + + + + :PB:{E8B4A2A2-882B-402A-98A7-8B4F7233C8B3}|Returngeance\Returngeance.vcxproj + + + \ No newline at end of file diff --git a/vs11/Returngeance140519(1).vsp b/vs11/Returngeance140519(1).vsp new file mode 100644 index 0000000..256cb35 Binary files /dev/null and b/vs11/Returngeance140519(1).vsp differ diff --git a/vs11/Returngeance140519(2).vsp b/vs11/Returngeance140519(2).vsp new file mode 100644 index 0000000..90776c5 Binary files /dev/null and b/vs11/Returngeance140519(2).vsp differ diff --git a/vs11/Returngeance140519(3).vsp b/vs11/Returngeance140519(3).vsp new file mode 100644 index 0000000..f70580d Binary files /dev/null and b/vs11/Returngeance140519(3).vsp differ diff --git a/vs11/Returngeance140519.vsp b/vs11/Returngeance140519.vsp new file mode 100644 index 0000000..bd782b0 Binary files /dev/null and b/vs11/Returngeance140519.vsp differ