From 07c74ff9f586aa7ff304b1083c17581e535500e4 Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Sat, 24 May 2014 20:37:55 +0200 Subject: [PATCH] I'll create a GUI interface using Visual Basic, see if I can track an IP address --- src/Camera.cpp | 34 ++--- src/Camera.h | 26 +--- src/Engine.h | 9 +- src/Events/SetViewportCamera.h | 18 +++ src/GUI/Frame.h | 62 ++++++-- src/GUI/TextureFrame.h | 52 +++++++ src/GUI/Viewport.h | 58 ++++++- src/GUI/WorldFrame.h | 144 ++++++++++++++++++ src/RenderQueue.h | 51 +++++-- src/Renderer.cpp | 10 +- src/World.h | 3 +- vs11/Returngeance.sln | 4 +- vs11/Returngeance/Returngeance.vcxproj | 3 + .../Returngeance/Returngeance.vcxproj.filters | 12 ++ 14 files changed, 401 insertions(+), 85 deletions(-) create mode 100644 src/Events/SetViewportCamera.h create mode 100644 src/GUI/TextureFrame.h create mode 100644 src/GUI/WorldFrame.h diff --git a/src/Camera.cpp b/src/Camera.cpp index 1256e64..60b2cb0 100755 --- a/src/Camera.cpp +++ b/src/Camera.cpp @@ -9,10 +9,7 @@ Camera::Camera(float yFOV, float aspectRatio, float nearClip, float farClip) m_FarClip = farClip; m_Position = glm::vec3(0.0); - /*m_Pitch = 0.f; - m_Yaw = 0.f;*/ - UpdateProjectionMatrix(); UpdateViewMatrix(); } @@ -34,12 +31,6 @@ 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) { m_Position = val; @@ -65,16 +56,6 @@ 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); @@ -83,17 +64,24 @@ void Camera::UpdateViewMatrix() void Camera::FOV(float val) { m_FOV = val; - UpdateProjectionMatrix(); } void Camera::NearClip(float val) { m_NearClip = val; - UpdateProjectionMatrix(); } void Camera::FarClip(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/Engine.h b/src/Engine.h index 2d8215c..1d8a08f 100755 --- a/src/Engine.h +++ b/src/Engine.h @@ -1,7 +1,9 @@ #include #include +#include "ResourceManager.h" #include "EventBroker.h" +#include "RenderQueue.h" #include "Renderer.h" #include "InputManager.h" #include "GUI/Frame.h" @@ -12,6 +14,8 @@ class Engine public: Engine(int argc, char* argv[]) { + m_ResourceManager = std::make_shared(); + m_EventBroker = std::make_shared(); m_Renderer = std::make_shared(); @@ -19,7 +23,7 @@ public: m_InputManager = std::make_shared(m_Renderer->GetWindow(), m_EventBroker); - //m_UIParent = std::make_shared(m_EventBroker); + m_FrameStack = std::make_shared(m_EventBroker, m_Renderer); m_World = std::make_shared(m_EventBroker, m_Renderer); m_World->Initialize(); @@ -44,10 +48,11 @@ public: } 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; + std::shared_ptr m_FrameStack; // TODO: This should ultimately live in GameFrame std::shared_ptr m_World; 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..4051293 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,13 +26,16 @@ 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() + , ResourceManager(resourceManager) + , Rectangle() + , m_Name("UIParent") { Initialize(); } // Create a frame as a child - Frame(std::shared_ptr parent) + Frame(std::shared_ptr parent, std::string name) : Rectangle(static_cast(*parent)) // Clone parent rectangle using copy constructor + , m_Name(name) { SetParent(parent); Initialize(); } virtual void Initialize() { } @@ -40,18 +45,15 @@ public: 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_Parent->AddChild(child); - } + m_Children.push_back(std::make_pair(child->Name(), child)); } - typedef std::list>::const_iterator FrameChildrenIterator; + typedef std::map>::const_iterator FrameChildrenIterator; FrameChildrenIterator begin() { return m_Children.begin(); @@ -61,13 +63,45 @@ public: return m_Children.end(); } - virtual void Update(double dt) { } - virtual void Draw(Renderer* renderer) { } + std::string Name() const { return m_Name; } + void SetName(std::string val) { m_Name = val; } + + virtual void Update(double dt) + { + for (auto &pair : m_Children) + { + pair.second->Update(dt); + } + } + + void DrawLayered(std::shared_ptr renderer) + { + renderer->SetViewport(GetLeft(), GetTop(), GetRight(), GetBottom()); + this->Draw(renderer); + renderer->Draw(); + + for (auto &pair : m_Children) + { + pair.second->Draw(renderer); + } + } + + virtual void Draw(std::shared_ptr renderer) + { + renderer->SetViewport(GetLeft(), GetTop(), GetRight(), GetBottom()); + renderer->Draw(); + + + } protected: std::shared_ptr<::EventBroker> EventBroker; + std::shared_ptr<::ResourceManager> ResourceManager; + std::string m_Name; + std::shared_ptr m_Parent; - std::list> m_Children; + std::multimap> m_Children; // name -> frame + }; } diff --git a/src/GUI/TextureFrame.h b/src/GUI/TextureFrame.h new file mode 100644 index 0000000..b2dc815 --- /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(std::shared_ptr parent, std::string name) + : Frame(parent, name) { } + + void Update(double dt) override + { + Frame::Update(dt); + } + + void Draw(std::shared_ptr renderer) override + { + if (m_Texture == nullptr) + return; + + RenderQueue rq; + + auto textureAsset = ResourceManager->Load("Texture", m_Texture); + + SpriteJob job; + + 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(spriteQueue, textureAsset, modelMatrix); + } + + std::shared_ptr<::Texture> Texture() const { return m_Texture; } + void SetTexture(std::string resourceName) + { + m_Texture = std::shared_ptr<::Texture>(ResourceManager->Load<::Texture>("Texture", resourceName)); + } + +protected: + std::shared_ptr<::Texture> m_Texture; +}; + +} + +#endif // GUI_TextureFrame_h__ diff --git a/src/GUI/Viewport.h b/src/GUI/Viewport.h index b4568b5..726997b 100644 --- a/src/GUI/Viewport.h +++ b/src/GUI/Viewport.h @@ -4,6 +4,11 @@ #include #include "GUI/Frame.h" +#include "World.h" +#include "Components/Transform.h" +#include "Components/Camera.h" +#include "RenderQueue.h" +#include "Camera.h" namespace GUI { @@ -11,9 +16,56 @@ namespace GUI class Viewport : public Frame { public: - // Create a frame as a child - Viewport(std::shared_ptr parent) - : Frame(parent) { } + Viewport(std::shared_ptr 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 Initialize() override + { + + } + + void Update(double dt) override + { + auto transformComponent = m_World->GetComponent(m_CameraEntity); + if (!transformComponent) + return; + auto cameraComponent = m_World->GetComponent(m_CameraEntity); + if (!cameraComponent) + return; + + m_Camera->SetFOV(cameraComponent->FOV); + m_Camera->SetNearClip(cameraComponent->NearClip); + m_Camera->SetFarClip(cameraComponent->FarClip); + } + + void Draw(std::shared_ptr renderer) override + { + renderer->SetCamera(m_Camera); + + Frame::Draw(renderer); + } + +private: + std::shared_ptr m_World; + 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..a64b427 --- /dev/null +++ b/src/GUI/WorldFrame.h @@ -0,0 +1,144 @@ +#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(std::shared_ptr parent, std::string name, std::shared_ptr world) + : Frame(parent, name) + , m_World(world) + { } + + void Initialize() override + { + EVENT_SUBSCRIBE_MEMBER(m_ESetViewportCamera, &WorldFrame::OnSetViewportCamera); + + m_World->Initialize(); + } + + void Update(double dt) override + { + m_World->Update(dt); + m_TransformSystem = m_World->GetSystem(); + } + + void Draw(std::shared_ptr renderer) override + { + renderer->Flush(); + + RenderQueue modelQueue; + RenderQueue spriteQueue; + 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(modelQueue, 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(spriteQueue, textureAsset, modelMatrix); + } + } + + auto pointLightComponent = m_World->GetComponent(entity); + if (pointLightComponent) + { + // TODO: Push lights to renderer + // renderer->PushLight(...); + } + } + + renderer->PushQueue(rq); + + Frame::Draw(renderer); + } + + protected: + std::shared_ptr m_World; + + private: + EventRelay m_ESetViewportCamera; + bool OnSetViewportCamera(const Events::SetViewportCamera &event) + { + auto itpair = m_Children.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->SetCamera(event.CameraEntity); + } + } + + std::shared_ptr m_TransformSystem; + + void EnqueueModel(RenderQueue &renderQueue, 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; + job.SpecularTexture = *texGroup.SpecularMap; + job.VAO = model->VAO; + job.StartIndex = texGroup.StartIndex; + job.EndIndex = texGroup.EndIndex; + job.ModelMatrix = modelMatrix; + + renderQueue.Add(job); + } + } + + void EnqueueSprite(RenderQueue &renderQueue, Texture* texture, glm::mat4 modelMatrix) + { + SpriteJob job; + job.TextureID = texture->ResourceID; + job.Texture = *texture; + job.ModelMatrix = modelMatrix; + } + }; + +} + +#endif // GUI_WorldFrame_h__ diff --git a/src/RenderQueue.h b/src/RenderQueue.h index bac7349..a3bba08 100644 --- a/src/RenderQueue.h +++ b/src/RenderQueue.h @@ -12,24 +12,13 @@ class RenderQueue; struct RenderJob { - friend class RenderQueue; - - unsigned int Layer; - unsigned int ViewportID; - unsigned int TextureID; - - GLuint DiffuseTexture; - GLuint NormalTexture; - GLuint SpecularTexture; - GLuint VAO; - unsigned int StartIndex; - unsigned int EndIndex; - glm::mat4 ModelMatrix; + template + friend class RenderQueue; protected: uint64_t Hash; - void CalculateHash() + virtual void CalculateHash() = 0; { Hash = ViewportID << 58 // 6 bits | TextureID << 42; // 16 bits @@ -41,10 +30,40 @@ protected: } }; +struct ModelJob : RenderJob +{ + unsigned int TextureID; + GLuint DiffuseTexture; + GLuint NormalTexture; + GLuint SpecularTexture; + GLuint VAO; + unsigned int StartIndex; + unsigned int EndIndex; + glm::mat4 ModelMatrix; + + void CalculateHash() override + { + Hash = TextureID; + } +}; + +struct SpriteJob : RenderJob +{ + unsigned int TextureID; + GLuint Texture; + glm::mat4 ModelMatrix; + + void CalculateHash() override + { + Hash = TextureID; + } +}; + +template class RenderQueue { public: - void Add(RenderJob &job) + void Add(JobType &job) { job.CalculateHash(); m_Jobs.push_front(job); @@ -57,7 +76,7 @@ public: } 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 19c00f0..3d5f171 100755 --- a/src/Renderer.cpp +++ b/src/Renderer.cpp @@ -372,10 +372,10 @@ void Renderer::AddTextureToDraw(Texture* texture, glm::vec3 position, glm::quat } 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 @@ -826,7 +826,7 @@ void Renderer::DrawLightScene(Viewport &viewport) glUniform1f(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "LinearAttenuation"), LAtt); glUniform1f(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "QuadraticAttenuation"), QAtt); - glDrawArrays(GL_TRIANGLES, 0, m_sphereModel->Vertices.size()); + glDrawArrays(GL_TRIANGLES, 0, m_sphereModel->Vertices.size()); }; glEnable (GL_DEPTH_TEST); glDepthMask (GL_TRUE); diff --git a/src/World.h b/src/World.h index 87bfecf..022a272 100755 --- a/src/World.h +++ b/src/World.h @@ -88,14 +88,13 @@ public: std::unordered_map* GetEntities() { return &m_EntityParents; } - ResourceManager* GetResourceManager() { return &m_ResourceManager; } + //ResourceManager* GetResourceManager() { return &m_ResourceManager; } std::shared_ptr<::EventBroker> EventBroker() { return m_EventBroker; } protected: std::shared_ptr<::EventBroker> m_EventBroker; SystemFactory m_SystemFactory; ComponentFactory m_ComponentFactory; - ResourceManager m_ResourceManager; std::unordered_map> m_Systems; diff --git a/vs11/Returngeance.sln b/vs11/Returngeance.sln index 10ce52b..8bcc255 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.30110.0 +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 6a99e47..8b0579d 100755 --- a/vs11/Returngeance/Returngeance.vcxproj +++ b/vs11/Returngeance/Returngeance.vcxproj @@ -174,11 +174,14 @@ + + + diff --git a/vs11/Returngeance/Returngeance.vcxproj.filters b/vs11/Returngeance/Returngeance.vcxproj.filters index 2bf19bf..0f161c7 100755 --- a/vs11/Returngeance/Returngeance.vcxproj.filters +++ b/vs11/Returngeance/Returngeance.vcxproj.filters @@ -158,6 +158,9 @@ {cb06b441-90b8-46ed-b347-4190dac7185b} + + {a025d51e-594d-4844-983b-f683726bf1bf} + @@ -397,6 +400,15 @@ Gameplay\Components + + GUI + + + GUI + + + Rendering\Events +