diff --git a/include/Engine/Core/Transform.h b/include/Engine/Core/Transform.h new file mode 100644 index 00000000..c43e4386 --- /dev/null +++ b/include/Engine/Core/Transform.h @@ -0,0 +1,62 @@ +#ifndef Transform_h__ +#define Transform_h__ + +#include "../GLM.h" +#include "World.h" + +static class Transform +{ +public: + static glm::vec3 AbsolutePosition(World* world, EntityID entity) + { + glm::vec3 position; + + while (entity != EntityID_Invalid) { + ComponentWrapper transform = world->GetComponent(entity, "Transform"); + EntityID parent = world->GetParent(entity); + position += AbsoluteOrientation(world, parent) * (glm::vec3)transform["Position"]; + entity = parent; + } + + return position; + }; + + static glm::quat AbsoluteOrientation(World* world, EntityID entity) + { + glm::quat orientation; + + while (entity != EntityID_Invalid) { + ComponentWrapper transform = world->GetComponent(entity, "Transform"); + orientation = glm::quat((glm::vec3)transform["Orientation"]) * orientation; + entity = world->GetParent(entity); + } + + return orientation; + }; + + static glm::vec3 AbsoluteScale(World* world, EntityID entity) + { + glm::vec3 scale(1.f); + + while (entity != EntityID_Invalid) { + ComponentWrapper transform = world->GetComponent(entity, "Transform"); + scale *= (glm::vec3)transform["Scale"]; + entity = world->GetParent(entity); + } + + return scale; + }; + + static glm::mat4 ModelMatrix(EntityID entity, World* world) + { + glm::vec3 position = Transform::AbsolutePosition(world, entity); + glm::quat orientation = Transform::AbsoluteOrientation(world, entity); + glm::vec3 scale = Transform::AbsoluteScale(world, entity); + + glm::mat4 modelMatrix = glm::translate(glm::mat4(), position) * glm::toMat4(orientation) * glm::scale(scale); + return modelMatrix; + } + +}; + +#endif \ No newline at end of file diff --git a/include/Engine/Editor/EditorSystem.h b/include/Engine/Editor/EditorSystem.h index 6b9b7a21..05e106d9 100644 --- a/include/Engine/Editor/EditorSystem.h +++ b/include/Engine/Editor/EditorSystem.h @@ -9,9 +9,8 @@ #include "../Core/ConfigFile.h" #include "../Input/EInputCommand.h" #include "../Rendering/IRenderer.h" -#include "../Rendering/EPicking.h" +#include "../Core/Transform.h" #include "../Core/EFileDropped.h" -#include "../Rendering/RenderQueueFactory.h" #include "../Core/EntityFilePreprocessor.h" #include "../Core/EntityFileParser.h" #include "../Core/EntityFileWriter.h" @@ -26,6 +25,7 @@ public: private: IRenderer* m_Renderer; World* m_World = nullptr; + Camera* m_Camera = nullptr; bool m_Enabled; bool m_Visible; @@ -57,6 +57,7 @@ private: EntityID m_WidgetOrigin = EntityID_Invalid; glm::vec3 m_WidgetCurrentAxis; float m_WidgetPickingDepth = 0.f; + glm::vec3 m_WidgetPickingPosition = glm::vec3(0); EntityID m_Selection = EntityID_Invalid; EntityID m_LastSelection = EntityID_Invalid; @@ -75,11 +76,10 @@ private: bool OnMousePress(const Events::MousePress& e); EventRelay m_EMouseMove; bool OnMouseMove(const Events::MouseMove& e); - EventRelay m_EPicking; - bool OnPicking(const Events::Picking& e); EventRelay m_EFileDropped; bool OnFileDropped(const Events::FileDropped& e); - + + void Picking(); void createWidget(); void updateWidget(); void setWidgetMode(WidgetMode newMode); diff --git a/include/Engine/GUI/Button.h b/include/Engine/GUI/Button.h index 91bd9782..80845cb7 100644 --- a/include/Engine/GUI/Button.h +++ b/include/Engine/GUI/Button.h @@ -40,7 +40,7 @@ public: m_TexturePressed = resourceName; } - void Draw(RenderQueueCollection& rq) override + void Draw(RenderScene& rq) override { if (m_Texture == nullptr && !m_TextureReleased.empty()) { SetTexture(m_TextureReleased); diff --git a/include/Engine/GUI/Frame.h b/include/Engine/GUI/Frame.h index 416b8117..4c5f2eb9 100644 --- a/include/Engine/GUI/Frame.h +++ b/include/Engine/GUI/Frame.h @@ -212,7 +212,7 @@ public: virtual void Update(double dt) { } - void DrawLayered(RenderQueueCollection& rq) + void DrawLayered(RenderScene& rq) { if (this->Hidden()) return; @@ -232,7 +232,7 @@ public: } } - virtual void Draw(RenderQueueCollection& rq) { } + virtual void Draw(RenderScene& rq) { } protected: ::EventBroker* m_EventBroker; diff --git a/include/Engine/GUI/TextureFrame.h b/include/Engine/GUI/TextureFrame.h index f02285ca..2c6d34dd 100644 --- a/include/Engine/GUI/TextureFrame.h +++ b/include/Engine/GUI/TextureFrame.h @@ -16,7 +16,7 @@ public: void EnableScissor() { m_ScissorEnabled = true; } void DisableScissor() { m_ScissorEnabled = false; } - void Draw(RenderQueueCollection& rq) override + void Draw(RenderScene& rq) override { if (m_Texture == nullptr) return; diff --git a/include/Engine/Rendering/Camera.h b/include/Engine/Rendering/Camera.h index 660dfd49..2b863448 100644 --- a/include/Engine/Rendering/Camera.h +++ b/include/Engine/Rendering/Camera.h @@ -26,13 +26,12 @@ public: glm::quat Orientation() const { return m_Orientation; } 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 SetProjectionMatrix(glm::mat4 val); + glm::mat4 ViewMatrix() const { return m_ViewMatrix; } + void SetViewMatrix(glm::mat4 val); + float AspectRatio() const { return m_AspectRatio; } void SetAspectRatio(float val); @@ -46,11 +45,10 @@ public: float FarClip() const { return m_FarClip; } void SetFarClip(float val); - + void UpdateViewMatrix(); + void UpdateProjectionMatrix(); private: - void UpdateViewMatrix(); - void UpdateProjectionMatrix(); glm::vec3 m_Position; glm::quat m_Orientation; diff --git a/include/Engine/Rendering/DebugCameraInputController.h b/include/Engine/Rendering/DebugCameraInputController.h index 614b071c..4d74e288 100644 --- a/include/Engine/Rendering/DebugCameraInputController.h +++ b/include/Engine/Rendering/DebugCameraInputController.h @@ -9,6 +9,9 @@ public: : FirstPersonInputController(eventBroker, playerID) { } + void SetPosition(const glm::vec3 position) { m_Position = position; } + void SetOrientation(const glm::quat orientation) { m_Orientation = orientation; } + const glm::vec3 Position() const { return m_Position; } void SetBaseSpeed(float speed) { m_BaseSpeed = speed; } diff --git a/include/Engine/Rendering/DrawScenePass.h b/include/Engine/Rendering/DrawScenePass.h index 782c5a49..ca2463cf 100644 --- a/include/Engine/Rendering/DrawScenePass.h +++ b/include/Engine/Rendering/DrawScenePass.h @@ -17,7 +17,7 @@ public: void InitializeFrameBuffers(); void InitializeShaderPrograms(); - void Draw(RenderQueueCollection& rq); + void Draw(RenderScene& scene); //Getters @@ -25,12 +25,18 @@ public: private: void GenerateTexture(GLuint* texture, GLenum wrapping, GLenum filtering, glm::vec2 dimensions, GLint internalFormat, GLint format, GLenum type) const; + static bool DepthSort(const std::shared_ptr &i, const std::shared_ptr &j) + { + return (i->Depth < j->Depth); + }; + Texture* m_WhiteTexture; const IRenderer* m_Renderer; ShaderProgram* m_BasicForwardProgram; + }; #endif \ No newline at end of file diff --git a/include/Engine/Rendering/DummyRenderer.h b/include/Engine/Rendering/DummyRenderer.h index 56790fa1..176c5834 100644 --- a/include/Engine/Rendering/DummyRenderer.h +++ b/include/Engine/Rendering/DummyRenderer.h @@ -9,7 +9,7 @@ class DummyRenderer : public IRenderer { public: virtual void Initialize() override; - virtual void Draw(RenderQueueCollection& rq) override; + virtual void Draw(RenderFrame& rq) override; }; #endif \ No newline at end of file diff --git a/include/Engine/Rendering/EPicking.h b/include/Engine/Rendering/EPicking.h deleted file mode 100644 index 8ee252f0..00000000 --- a/include/Engine/Rendering/EPicking.h +++ /dev/null @@ -1,74 +0,0 @@ -#ifndef Events_Picking_h__ -#define Events_Picking_h__ - -#include "../OpenGL.h" -#include "../GLM.h" - -#include "../Core/EventBroker.h" -#include "Util/ScreenCoords.h" -#include "FrameBuffer.h" -#include "../Core/Entity.h" -#include "Util/UnorderedMapVec2.h" - -namespace Events -{ - -/** Thrown Every frame, use functions to pick*/ -struct Picking : Event -{ -public: - Picking(FrameBuffer* pickingBuffer, GLuint* depthBuffer, glm::mat4 projectionMatrix, glm::mat4 viewMatrix, Rectangle resolution, const std::unordered_map* pickingColorsToEntity) - : PickingBuffer(pickingBuffer) - , DepthBuffer(depthBuffer) - , ProjectionMatrix(projectionMatrix) - , ViewMatrix(viewMatrix) - , Resolution(resolution) - , PickingColorsToEntity(pickingColorsToEntity) - { } - - - - struct PickData - { - //Picked Entity - EntityID Entity; - //World position of the "pick" - glm::vec3 Position; - // Depth - float Depth; - }; - - PickData Pick(glm::vec2 screenCoord) const - { - PickData pickData; - - // Invert screen y coordinate - screenCoord.y = Resolution.Height - screenCoord.y; - ScreenCoords::PixelData data = ScreenCoords::ToPixelData(screenCoord, PickingBuffer, *DepthBuffer); - pickData.Depth = data.Depth; - - auto it = PickingColorsToEntity->find(glm::vec2(data.Color[0], data.Color[1])); - if (it != PickingColorsToEntity->end()) { - pickData.Entity = it->second; - } else { - pickData.Entity = EntityID_Invalid; - } - pickData.Position = ScreenCoords::ToWorldPos(screenCoord.x, screenCoord.y, data.Depth, Resolution, ProjectionMatrix, ViewMatrix); - - return pickData; - } - - -private: - FrameBuffer* PickingBuffer; - GLuint* DepthBuffer; - const glm::mat4 ProjectionMatrix; - const glm::mat4 ViewMatrix; - const Rectangle Resolution; - const std::unordered_map* PickingColorsToEntity; - -}; - -} - -#endif diff --git a/include/Engine/Rendering/ESetCamera.h b/include/Engine/Rendering/ESetCamera.h new file mode 100644 index 00000000..650f3b12 --- /dev/null +++ b/include/Engine/Rendering/ESetCamera.h @@ -0,0 +1,23 @@ +#ifndef Events_SetCamera_h__ +#define Events_SetCamera_h__ + +#include "../Core/EventBroker.h" +#include "../Core/Entity.h" +#include + +namespace Events +{ + +struct SetCamera : Event +{ +public: + SetCamera() { }; + std::string Name; + +private: + +}; + +} + +#endif diff --git a/include/Engine/Rendering/IRenderer.h b/include/Engine/Rendering/IRenderer.h index 5dce14c7..773da732 100644 --- a/include/Engine/Rendering/IRenderer.h +++ b/include/Engine/Rendering/IRenderer.h @@ -10,6 +10,15 @@ #include "RenderQueue.h" #include "Model.h" +struct PickData +{ + EntityID Entity; + glm::vec3 Position; //World position + float Depth; + ::Camera* Camera; + const ::World* World; +}; + class IRenderer { public: @@ -20,19 +29,19 @@ public: void SetFullscreen(bool fullscreen) { m_Fullscreen = fullscreen; } bool VSYNC() const { return m_VSYNC; } void SetVSYNC(bool vsync) { m_VSYNC = vsync; } - ::Camera* Camera() const { return m_Camera; } - void SetCamera(::Camera* camera) - { - if (camera == nullptr) { - m_Camera = m_DefaultCamera; - } else { - m_Camera = camera; - } - } - + ::Camera* Camera() const { return m_Camera; } + void SetCamera(::Camera* camera) + { + if (camera == nullptr) { + m_Camera = m_DefaultCamera; + } else { + m_Camera = camera; + } + } virtual void Initialize() = 0; virtual void Update(double dt) = 0; - virtual void Draw(RenderQueueCollection& rq) = 0; + virtual void Draw(RenderFrame& rq) = 0; + virtual PickData Pick(glm::vec2 screenCord) = 0; protected: Rectangle m_Resolution = Rectangle::Rectangle(1280, 720); @@ -40,9 +49,9 @@ protected: bool m_VSYNC = false; int m_GLVersion[2]; std::string m_GLVendor; - ::Camera* m_DefaultCamera; - ::Camera* m_Camera = nullptr; GLFWwindow* m_Window = nullptr; + ::Camera* m_DefaultCamera; + ::Camera* m_Camera = nullptr; }; #endif // Renderer_h__ diff --git a/include/Engine/Rendering/ModelJob.h b/include/Engine/Rendering/ModelJob.h new file mode 100644 index 00000000..d598766f --- /dev/null +++ b/include/Engine/Rendering/ModelJob.h @@ -0,0 +1,55 @@ +#ifndef ModelJob_h__ +#define ModelJob_h__ + +#include + +#include "../Common.h" +#include "../GLM.h" +#include "../Core/ComponentWrapper.h" +#include "Texture.h" +#include "Model.h" +#include "RenderJob.h" +#include "../Core/ResourceManager.h" +#include "Camera.h" +#include "../Core/World.h" + +struct ModelJob : RenderJob +{ + ModelJob(Model* model, Camera* camera, glm::mat4 matrix, ::Model::MaterialGroup texGroup, ComponentWrapper modelComponent, World* world) + : RenderJob() + { + Model = model; + TextureID = (texGroup.Texture) ? texGroup.Texture->ResourceID : 0; + DiffuseTexture = texGroup.Texture.get(); + NormalTexture = texGroup.NormalMap.get(); + SpecularTexture = texGroup.SpecularMap.get(); + StartIndex = texGroup.StartIndex; + EndIndex = texGroup.EndIndex; + Matrix = matrix; + Color = modelComponent["Color"]; + Entity = modelComponent.EntityID; + World = world; + }; + + unsigned int TextureID; + unsigned int ShaderID; + + EntityID Entity; + glm::mat4 Matrix; + const Texture* DiffuseTexture; + const Texture* NormalTexture; + const Texture* SpecularTexture; + float Shininess = 0.f; + glm::vec4 Color; + const ::Model* Model = nullptr; + unsigned int StartIndex = 0; + unsigned int EndIndex = 0; + const World* World; + + void CalculateHash() override + { + Hash = TextureID; + } +}; + +#endif \ No newline at end of file diff --git a/include/Engine/Rendering/PickingPass.h b/include/Engine/Rendering/PickingPass.h index e1bc42db..523c7d65 100644 --- a/include/Engine/Rendering/PickingPass.h +++ b/include/Engine/Rendering/PickingPass.h @@ -5,9 +5,9 @@ #include "PickingPassState.h" #include "FrameBuffer.h" #include "ShaderProgram.h" -#include "Util/UnorderedMapVec2.h" +#include "Util/UnorderedMapiVec2.h" #include "../Core/EventBroker.h" -#include "EPicking.h" +#include "../Core/World.h" class PickingPass { @@ -18,16 +18,19 @@ public: void InitializeFrameBuffers(); void InitializeShaderPrograms(); - void Draw(RenderQueueCollection& rq); + void Draw(RenderScene& scene); + void ClearPicking(); //Getters const ShaderProgram& PickingProgram() const { return *m_PickingProgram; } - const std::unordered_map& PickingColorsToEntity() const { return m_PickingColorsToEntity; } + //const std::unordered_map& PickingColorsToEntity() const { return m_PickingColorsToEntity; } GLuint PickingTexture() const { return m_PickingTexture; } GLuint DepthBuffer() const { return m_DepthBuffer; } const FrameBuffer& PickingBuffer() const { return m_PickingBuffer; } + + PickData Pick(glm::vec2 screenCoord); private: void GenerateTexture(GLuint* texture, GLenum wrapping, GLenum filtering, glm::vec2 dimensions, GLint internalFormat, GLint format, GLenum type) const; @@ -37,13 +40,24 @@ private: const IRenderer* m_Renderer; ShaderProgram* m_PickingProgram; + Camera* m_Camera; - std::unordered_map m_PickingColorsToEntity; + struct PickingInfo + { + EntityID Entity; + const ::World* World; + ::Camera* Camera; + }; + + std::unordered_map m_PickingColorsToEntity; GLuint m_PickingTexture; GLuint m_DepthBuffer; FrameBuffer m_PickingBuffer; + + int m_ColorCounter[2]; + std::map, glm::ivec2> m_EntityColors; }; #endif \ No newline at end of file diff --git a/include/Engine/Rendering/RawModel.h b/include/Engine/Rendering/RawModel.h index c8226168..d63e46bb 100644 --- a/include/Engine/Rendering/RawModel.h +++ b/include/Engine/Rendering/RawModel.h @@ -46,6 +46,7 @@ public: struct MaterialGroup { float Shininess; + float Transparency; std::shared_ptr<::Texture> Texture; std::shared_ptr<::Texture> NormalMap; std::shared_ptr<::Texture> SpecularMap; diff --git a/include/Engine/Rendering/RenderJob.h b/include/Engine/Rendering/RenderJob.h new file mode 100644 index 00000000..4afe0386 --- /dev/null +++ b/include/Engine/Rendering/RenderJob.h @@ -0,0 +1,32 @@ +#ifndef RenderJob_h__ +#define RenderJob_h__ + +#include + +#include "../Common.h" +#include "../GLM.h" +#include "../Core/ComponentWrapper.h" +#include "RenderQueue.h" + + +struct RenderJob +{ + friend class RenderQueue; + +public: + + float Depth; + +protected: + uint64_t Hash; + + virtual void CalculateHash() = 0; + + bool operator<(const RenderJob& rhs) + { + return this->Hash < rhs.Hash; + } + +}; + +#endif \ No newline at end of file diff --git a/include/Engine/Rendering/RenderQueue.h b/include/Engine/Rendering/RenderQueue.h index 2942c743..dba34c3e 100644 --- a/include/Engine/Rendering/RenderQueue.h +++ b/include/Engine/Rendering/RenderQueue.h @@ -8,61 +8,13 @@ #include "../GLM.h" #include "../Core/Util/Rectangle.h" #include "../Core/Entity.h" +#include "Camera.h" +#include "RenderJob.h" +#include "ModelJob.h" -class Model; -class Skeleton; -class Texture; -class RenderQueue; -//TODO: Render: Remove obsolete RenderJobs and fix standard values on variables. - -struct RenderJob -{ - friend class RenderQueue; - - float Depth; - -protected: - uint64_t Hash; - - virtual void CalculateHash() = 0; - - bool operator<(const RenderJob& rhs) - { - return this->Hash < rhs.Hash; - } -}; - -struct ModelJob : RenderJob -{ - unsigned int ShaderID = 0; - unsigned int TextureID = 0; - - //TODO: RENDERER: Not sure if the best solution for pickingColor to entity link is this - EntityID Entity; - - glm::mat4 ModelMatrix; - const Texture* DiffuseTexture; - const Texture* NormalTexture; - const Texture* SpecularTexture; - float Shininess = 0.f; - glm::vec4 Color; - const Model* Model = nullptr; - unsigned int StartIndex = 0; - unsigned int EndIndex = 0; - - // Animation - Skeleton* Skeleton = nullptr; - bool NoRootMotion = true; - std::string AnimationName; - double AnimationTime = 0; - - void CalculateHash() override - { - Hash = TextureID; - } -}; +/* struct SpriteJob : RenderJob { unsigned int ShaderID = 0; @@ -93,62 +45,53 @@ struct PointLightJob : RenderJob Hash = 0; } }; +*/ -class RenderQueue +struct RenderScene { -public: - template - void Add(T &job) - { - job.CalculateHash(); - Jobs.push_back(std::shared_ptr(new T(job))); - m_Size++; - } - - void Sort() - { - Jobs.sort(); - } + ::Camera* Camera; + std::list> ForwardJobs; + std::list> LightJobs; + Rectangle Viewport; void Clear() { - Jobs.clear(); - m_Size = 0; + ForwardJobs.clear(); + LightJobs.clear(); } - - int Size() const { return m_Size; } - std::list>::const_iterator begin() - { - return Jobs.begin(); - } - - std::list>::const_iterator end() - { - return Jobs.end(); - } - - std::list> Jobs; - -private: - int m_Size = 0; }; -struct RenderQueueCollection +struct RenderFrame { - RenderQueue Forward; - RenderQueue Lights; +public: - void Clear() - { - Forward.Clear(); - Lights.Clear(); - } + void Add(RenderScene &scene) + { + RenderScenes.push_back(std::shared_ptr(new RenderScene(scene))); + m_Size++; + } - void Sort() - { - Forward.Sort(); - Lights.Sort(); - } + void Clear() + { + RenderScenes.clear(); + m_Size = 0; + } + + int Size() const { return m_Size; } + std::list>::const_iterator begin() + { + return RenderScenes.begin(); + } + + std::list>::const_iterator end() + { + return RenderScenes.end(); + } + + std::list> RenderScenes; + +private: + int m_Size = 0; }; #endif \ No newline at end of file diff --git a/include/Engine/Rendering/RenderQueueFactory.h b/include/Engine/Rendering/RenderQueueFactory.h deleted file mode 100644 index be2c55ca..00000000 --- a/include/Engine/Rendering/RenderQueueFactory.h +++ /dev/null @@ -1,31 +0,0 @@ -#ifndef RenderQueueFactory_h__ -#define RenderQueueFactory_h__ - -#include "../Core/World.h" -#include "RenderQueue.h" -#include "../Core/ResourceManager.h" -#include "Model.h" -#include "../GLM.h" - -class RenderQueueFactory -{ -public: - RenderQueueFactory(); - void Update(World* world); - - RenderQueueCollection RenderQueues() const { return m_RenderQueues; } - - static glm::vec3 AbsolutePosition(World* world, EntityID entity); - static glm::quat AbsoluteOrientation(World* world, EntityID entity); - static glm::vec3 AbsoluteScale(World* world, EntityID entity); - -private: - RenderQueueCollection m_RenderQueues; - - void FillModels(World* world, RenderQueue* renderQueue); - void FillLights(World* world, RenderQueue* renderQueue); - - glm::mat4 ModelMatrix(World* world, EntityID entity); -}; - -#endif \ No newline at end of file diff --git a/include/Engine/Rendering/RenderSystem.h b/include/Engine/Rendering/RenderSystem.h new file mode 100644 index 00000000..93e7233f --- /dev/null +++ b/include/Engine/Rendering/RenderSystem.h @@ -0,0 +1,53 @@ +#ifndef RenderSystem_h__ +#define RenderSystem_h__ + +#include "../Core/System.h" +#include "RenderQueue.h" +#include "../GLM.h" +#include "../OpenGL.h" +#include "../Core/ResourceManager.h" +#include "ESetCamera.h" +#include "Model.h" +#include "../Core/EKeyDown.h" +#include "../Input/EInputCommand.h" +#include "Camera.h" +#include "ModelJob.h" +#include "Renderer.h" +#include "../Core/Transform.h" + +class RenderSystem : public ImpureSystem +{ +public: + RenderSystem(EventBroker* eventBrokerer, const IRenderer* renderer, RenderFrame* renderFrame); + + virtual void Update(World* world, double dt) override; + +private: + World* m_World = nullptr; + const IRenderer* m_Renderer = nullptr; + + RenderFrame* m_RenderFrame; + bool m_SwitchCamera = false; + Camera* m_Camera = nullptr; + Camera* m_DefaultCamera = nullptr; + + std::list m_CameraComponents; + + EventRelay m_ESetCamera; + bool OnSetCamera(const Events::SetCamera &event); + EntityID m_CurrentCamera = EntityID_Invalid; + + void switchCamera(EntityID entity); + + void updateCamera(World* world, double dt); + void updateProjectionMatrix(ComponentWrapper& cameraComponent); + glm::mat4 m_ViewMatrix; + glm::mat4 m_ProjectionMatrix; + + void fillModels(std::list>& jobs, World* world); + + EventRelay m_EInputCommand; + bool OnInputCommand(const Events::InputCommand& e); +}; + +#endif \ No newline at end of file diff --git a/include/Engine/Rendering/Renderer.h b/include/Engine/Rendering/Renderer.h index 19b48e1a..6dd7e250 100644 --- a/include/Engine/Rendering/Renderer.h +++ b/include/Engine/Rendering/Renderer.h @@ -12,45 +12,31 @@ #include "../Core/World.h" #include "PickingPass.h" #include "DrawScenePass.h" -#include "DebugCameraInputController.h" - - -#define TILE_SIZE 16 -#define NUM_LIGHTS 3 - - -enum lightType -{ - Point, - Spot, - Directional, - Area -}; - #include "../Core/EventBroker.h" -#include "EPicking.h" #include "ImGuiRenderPass.h" +#include "Camera.h" class Renderer : public IRenderer { public: - Renderer(EventBroker* eventBroker) + Renderer(EventBroker* eventBroker, World* world) : m_EventBroker(eventBroker) + , m_World(world) { } virtual void Initialize() override; virtual void Update(double dt) override; - virtual void Draw(RenderQueueCollection& rq) override; + virtual void Draw(RenderFrame& frame) override; + + virtual PickData Pick(glm::vec2 screenCoord) override; private: //----------------------Variables----------------------// EventBroker* m_EventBroker; - - std::shared_ptr> m_DebugCameraInputController; + World* m_World; Texture* m_ErrorTexture; Texture* m_WhiteTexture; - float m_CameraMoveSpeed; Model* m_ScreenQuad; Model* m_UnitQuad; @@ -71,56 +57,10 @@ private: //void PickingPass(RenderQueueCollection& rq); void DrawScreenQuad(GLuint textureToDraw); - //----------------------Forward+-----------------------// - void CalculateFrustum(); - void CullLights(); - //Frustum - struct Plane { - glm::vec3 Normal; - float d; - }; - struct Frustum { - Plane Planes[4]; - }; - Frustum m_Frustums[80*45]; //TODO: Renderer: Make this change with resolution - - //Lights - void TEMPCreateLights(); - //TODO: Renderer: Add Directionllights, spotlights and area lights to this as type. - struct PointLight { - glm::vec4 Position = glm::vec4(0.f); - glm::vec4 Color = glm::vec4(1.f); - float Radius = 5.f; - float Intensity = 0.8f; - float Falloff = 0.3f; - float Padding = 1337; - }; - PointLight m_PointLights[NUM_LIGHTS]; - - struct LightGrid { - int Amount; - int Start; - glm::vec2 Padding; - }; - LightGrid m_LightGrid[80*45]; - - int m_LightOffset = 0; - - int m_LightIndex[80*45*200]; - - //-------------------------SSBO------------------------// - GLuint m_FrustumSSBO = 0; - GLuint m_LightSSBO = 1; - GLuint m_LightGridSSBO = 2; - GLuint m_LightOffsetSSBO = 3; - GLuint m_LightIndexSSBO = 4; - void GenerateTexture(GLuint* texture, GLenum wrapping, GLenum filtering, glm::vec2 dimensions, GLint internalFormat, GLint format, GLenum type); //--------------------ShaderPrograms-------------------// ShaderProgram* m_BasicForwardProgram; ShaderProgram* m_DrawScreenQuadProgram; - ShaderProgram* m_CalculateFrustumProgram; - ShaderProgram* m_LightCullProgram; }; diff --git a/include/Engine/Rendering/Util/UnorderedMapiVec2.h b/include/Engine/Rendering/Util/UnorderedMapiVec2.h new file mode 100644 index 00000000..9797046e --- /dev/null +++ b/include/Engine/Rendering/Util/UnorderedMapiVec2.h @@ -0,0 +1,24 @@ +#pragma once +#ifndef UnorderedMapiVec2_h__ +#define UnorderedMapiVec2_h__ + +#include +#include +#include + +template<> +struct std::hash +{ + inline std::size_t operator()(const glm::ivec2 &v) const + { + return boost::hash()(v.x) ^ boost::hash()(v.y); + } + + inline bool operator()(const glm::ivec2& a, const glm::ivec2& b)const + { + return a.x == b.x && a.y == b.y; + } + +}; + +#endif \ No newline at end of file diff --git a/include/Game/Game.h b/include/Game/Game.h index 728070f3..a3be292a 100644 --- a/include/Game/Game.h +++ b/include/Game/Game.h @@ -8,7 +8,6 @@ #include "Core/InputManager.h" #include "GUI/Frame.h" #include "Core/World.h" -#include "Rendering/RenderQueueFactory.h" #include "Input/InputProxy.h" #include "Input/KeyboardInputHandler.h" #include "Input/MouseInputHandler.h" @@ -17,6 +16,7 @@ #include "Core/SystemPipeline.h" #include "Editor/EditorSystem.h" #include "Core/EntityFile.h" +#include "Rendering/RenderSystem.h" #include "Core/EntityFileParser.h" #include "Core/Octree.h" @@ -48,7 +48,7 @@ private: Octree* m_OctreeCollision; Octree* m_OctreeFrustrumCulling; SystemPipeline* m_SystemPipeline; - RenderQueueFactory* m_RenderQueueFactory; + RenderFrame* m_RenderFrame; // Network variables boost::thread m_NetworkThread; diff --git a/resources/Schema/Components.xsd b/resources/Schema/Components.xsd index e1440b69..7d323df6 100644 --- a/resources/Schema/Components.xsd +++ b/resources/Schema/Components.xsd @@ -7,6 +7,7 @@ + diff --git a/resources/Schema/Components/Camera.xml b/resources/Schema/Components/Camera.xml new file mode 100644 index 00000000..92225dde --- /dev/null +++ b/resources/Schema/Components/Camera.xml @@ -0,0 +1,6 @@ + + cam + 60.0 + 0.01 + 5000 + \ No newline at end of file diff --git a/resources/Schema/Components/Camera.xsd b/resources/Schema/Components/Camera.xsd new file mode 100644 index 00000000..4f02deb0 --- /dev/null +++ b/resources/Schema/Components/Camera.xsd @@ -0,0 +1,19 @@ + + + + + + + + It's a camera thingy! + + + + + + + + + + + \ No newline at end of file diff --git a/resources/Schema/Entities/RenderingWorld.xml b/resources/Schema/Entities/RenderingWorld.xml new file mode 100644 index 00000000..86a3090a --- /dev/null +++ b/resources/Schema/Entities/RenderingWorld.xml @@ -0,0 +1,58 @@ + + + + + + + + + + + + + + + + Models/Camera.obj + + + MainCamera + + + + + + + + + + Models/Camera.obj + + + ActionCamera + + + + + + + + + + + Models/Core/UnitPlane.obj + + + + + + + + + + An error + + + + + \ No newline at end of file diff --git a/resources/Schema/Entities/Test.xml b/resources/Schema/Entities/Test.xml index 528c7e95..d2838ae9 100644 --- a/resources/Schema/Entities/Test.xml +++ b/resources/Schema/Entities/Test.xml @@ -9,114 +9,19 @@ Models/DummyScene.obj + - - - - - - - - Models/ScaleWidget.obj - - - - - - - - - - - Models/RotationWidget.obj - - - - - - - + + + + Models/Camera.obj + - - - - - - - - - Models/Core/UnitRaptor.obj - - - - - - - - - - - - 20 - - - - - - - - - - - - - Models/Core/UnitCube.obj - - - - - - - - - - - - - Models/Core/UnitCube.obj - - - - - - - - - - \ No newline at end of file diff --git a/resources/Shaders/BasicForward.frag.glsl b/resources/Shaders/BasicForward.frag.glsl index 274ec8d7..dc04f59f 100644 --- a/resources/Shaders/BasicForward.frag.glsl +++ b/resources/Shaders/BasicForward.frag.glsl @@ -1,8 +1,5 @@ #version 430 -uniform mat4 M; -uniform mat4 V; -uniform mat4 P; uniform vec4 Color; uniform sampler2D texture0; diff --git a/resources/Shaders/BasicForward.vert.glsl b/resources/Shaders/BasicForward.vert.glsl index 20ab9051..96f081f4 100644 --- a/resources/Shaders/BasicForward.vert.glsl +++ b/resources/Shaders/BasicForward.vert.glsl @@ -25,7 +25,7 @@ out VertexData{ void main() { - gl_Position = P*V*M * vec4(Position, 1.0); + gl_Position = P * V * M * vec4(Position, 1.0); Output.Position = Position; Output.TextureCoordinate = TextureCoords; diff --git a/resources/Shaders/Picking.frag.glsl b/resources/Shaders/Picking.frag.glsl index 8c95ead3..59f761f9 100644 --- a/resources/Shaders/Picking.frag.glsl +++ b/resources/Shaders/Picking.frag.glsl @@ -1,8 +1,5 @@ #version 430 -uniform mat4 M; -uniform mat4 V; -uniform mat4 P; uniform vec2 PickingColor; in VertexData{ diff --git a/resources/Shaders/Picking.vert.glsl b/resources/Shaders/Picking.vert.glsl index 47c0ecd7..b2857cea 100644 --- a/resources/Shaders/Picking.vert.glsl +++ b/resources/Shaders/Picking.vert.glsl @@ -22,7 +22,7 @@ out VertexData{ void main() { - gl_Position = P*V*M * vec4(Position, 1.0); + gl_Position = P * V* M * vec4(Position, 1.0); Output.Position = Position; } \ No newline at end of file diff --git a/src/Engine/Editor/EditorSystem.cpp b/src/Engine/Editor/EditorSystem.cpp index b5e527f0..714d3d10 100644 --- a/src/Engine/Editor/EditorSystem.cpp +++ b/src/Engine/Editor/EditorSystem.cpp @@ -20,7 +20,6 @@ EditorSystem::EditorSystem(EventBroker* eventBroker, IRenderer* renderer) EVENT_SUBSCRIBE_MEMBER(m_EMousePress, &EditorSystem::OnMousePress); EVENT_SUBSCRIBE_MEMBER(m_EMouseRelease, &EditorSystem::OnMouseRelease); EVENT_SUBSCRIBE_MEMBER(m_EMouseMove, &EditorSystem::OnMouseMove); - EVENT_SUBSCRIBE_MEMBER(m_EPicking, &EditorSystem::OnPicking); EVENT_SUBSCRIBE_MEMBER(m_EFileDropped, &EditorSystem::OnFileDropped); } @@ -35,7 +34,7 @@ void EditorSystem::Update(World* world, double dt) if (!m_Visible) { return; } - + Picking(); updateWidget(); drawUI(world, dt); @@ -129,7 +128,7 @@ bool EditorSystem::OnMouseMove(const Events::MouseMove& e) auto widgetTransform = m_World->GetComponent(m_Widget, "Transform"); glm::vec3 widgetOrientation = widgetTransform["Orientation"]; - glm::quat totalOrientation = m_Renderer->Camera()->Orientation() * glm::inverse(glm::quat(widgetOrientation)); + glm::quat totalOrientation = m_Camera->Orientation() * glm::inverse(glm::quat(widgetOrientation)); int width; int height; @@ -141,14 +140,14 @@ bool EditorSystem::OnMouseMove(const Events::MouseMove& e) delta2, m_WidgetPickingDepth, res, - m_Renderer->Camera()->ProjectionMatrix(), + m_Camera->ProjectionMatrix(), glm::toMat4(glm::inverse(totalOrientation)) ); glm::vec3 origin = ScreenCoords::ToWorldPos( glm::vec2(res.Width / 2.f, res.Height / 2.f), m_WidgetPickingDepth, res, - m_Renderer->Camera()->ProjectionMatrix(), + m_Camera->ProjectionMatrix(), glm::toMat4(glm::inverse(totalOrientation)) ); deltaWorld = deltaWorld - origin; @@ -161,7 +160,7 @@ bool EditorSystem::OnMouseMove(const Events::MouseMove& e) EntityID parent = m_World->GetParent(m_Selection); glm::quat inverseParentOrientation; //if (parent != 0) { - inverseParentOrientation = glm::inverse(RenderQueueFactory::AbsoluteOrientation(m_World, parent)); + inverseParentOrientation = glm::inverse(Transform::AbsoluteOrientation(m_World, parent)); //} (glm::vec3&)m_World->GetComponent(m_Selection, "Transform")["Position"] += inverseParentOrientation * movement; } else if (m_WidgetSpace == WidgetSpace::Local) { @@ -177,10 +176,10 @@ bool EditorSystem::OnMouseMove(const Events::MouseMove& e) EntityID parent = m_World->GetParent(m_Selection); glm::quat parentOrientation; //if (parent != 0) { - // parentOrientation = RenderQueueFactory::AbsoluteOrientation(m_World, parent); + // parentOrientation = RenderSystem::AbsoluteOrientation(m_World, parent); //} glm::vec3& selectionOrientation = m_World->GetComponent(m_Selection, "Transform")["Orientation"]; - glm::quat currentOrientation = RenderQueueFactory::AbsoluteOrientation(m_World, m_Selection); + glm::quat currentOrientation = Transform::AbsoluteOrientation(m_World, m_Selection); //glm::quat currentOrientation = parentOrientation * glm::quat(selectionOrientation); glm::quat deltaOrientation(finalMovement); selectionOrientation = glm::eulerAngles(glm::inverse(parentOrientation) * (deltaOrientation * currentOrientation)); @@ -236,10 +235,10 @@ bool EditorSystem::OnMouseRelease(const Events::MouseRelease& e) return true; } -bool EditorSystem::OnPicking(const Events::Picking& e) +void EditorSystem::Picking() { for (auto& pos : m_PickingQueue) { - auto result = e.Pick(pos); + auto result = m_Renderer->Pick(pos); EntityID entity = result.Entity; if (glm::length2(m_WidgetCurrentAxis) > 0.f) { // ??? @@ -247,6 +246,7 @@ bool EditorSystem::OnPicking(const Events::Picking& e) LOG_INFO("Selected %i", entity); if (entity != EntityID_Invalid) { EntityID parent = m_World->GetParent(entity); + m_Camera = result.Camera; if (parent == m_Widget) { m_WidgetCurrentAxis = glm::vec3( (entity == m_WidgetX) || (entity == m_WidgetOrigin) || (entity == m_WidgetPlaneY || entity == m_WidgetPlaneZ), @@ -254,7 +254,6 @@ bool EditorSystem::OnPicking(const Events::Picking& e) (entity == m_WidgetZ) || (entity == m_WidgetOrigin) || (entity == m_WidgetPlaneX || entity == m_WidgetPlaneY) ); m_WidgetPickingDepth = result.Depth; - //auto widgetTransform = m_World->GetComponent(m_Widget, "Transform"); //auto selectionTransform = m_World->GetComponent(m_Selection, "Transform"); //widgetTransform["Position"] = (glm::vec3)selectionTransform["Position"]; @@ -270,7 +269,6 @@ bool EditorSystem::OnPicking(const Events::Picking& e) } } m_PickingQueue.clear(); - return true; }; bool EditorSystem::OnFileDropped(const Events::FileDropped& e) @@ -324,10 +322,10 @@ void EditorSystem::updateWidget() if (m_Selection != EntityID_Invalid) { auto widgetTransform = m_World->GetComponent(m_Widget, "Transform"); - glm::vec3 selectionPosition = RenderQueueFactory::AbsolutePosition(m_World, m_Selection); + glm::vec3 selectionPosition = Transform::AbsolutePosition(m_World, m_Selection); widgetTransform["Position"] = selectionPosition; if (m_WidgetSpace == WidgetSpace::Local) { - widgetTransform["Orientation"] = glm::eulerAngles(RenderQueueFactory::AbsoluteOrientation(m_World, m_Selection)); + widgetTransform["Orientation"] = glm::eulerAngles(Transform::AbsoluteOrientation(m_World, m_Selection)); } } } @@ -362,7 +360,7 @@ void EditorSystem::setWidgetMode(WidgetMode newMode) if (m_Selection != EntityID_Invalid) { if (m_WidgetSpace == WidgetSpace::Local) { auto selectionTransform = m_World->GetComponent(m_Selection, "Transform"); - widgetTransform["Orientation"] = glm::eulerAngles(RenderQueueFactory::AbsoluteOrientation(m_World, m_Selection)); + widgetTransform["Orientation"] = glm::eulerAngles(Transform::AbsoluteOrientation(m_World, m_Selection)); } } } else if (newMode == WidgetMode::Scale) { @@ -373,7 +371,7 @@ void EditorSystem::setWidgetMode(WidgetMode newMode) m_World->GetComponent(m_WidgetOrigin, "Model")["Resource"] = "Models/ScaleWidgetOrigin.obj"; if (m_Selection != EntityID_Invalid) { auto selectionTransform = m_World->GetComponent(m_Selection, "Transform"); - widgetTransform["Orientation"] = glm::eulerAngles(RenderQueueFactory::AbsoluteOrientation(m_World, m_Selection)); + widgetTransform["Orientation"] = glm::eulerAngles(Transform::AbsoluteOrientation(m_World, m_Selection)); } } else if (newMode == WidgetMode::Rotate) { m_World->GetComponent(m_WidgetX, "Model")["Resource"] = "Models/RotationWidgetX.obj"; @@ -382,7 +380,7 @@ void EditorSystem::setWidgetMode(WidgetMode newMode) if (m_Selection != EntityID_Invalid) { auto selectionTransform = m_World->GetComponent(m_Selection, "Transform"); if (m_WidgetSpace == WidgetSpace::Local) { - widgetTransform["Orientation"] = glm::eulerAngles(RenderQueueFactory::AbsoluteOrientation(m_World, m_Selection)); + widgetTransform["Orientation"] = glm::eulerAngles(Transform::AbsoluteOrientation(m_World, m_Selection)); } } } diff --git a/src/Engine/Rendering/Camera.cpp b/src/Engine/Rendering/Camera.cpp index 6ddf1c6c..5a774c34 100644 --- a/src/Engine/Rendering/Camera.cpp +++ b/src/Engine/Rendering/Camera.cpp @@ -50,6 +50,18 @@ void Camera::SetOrientation(glm::quat val) UpdateViewMatrix(); } + +void Camera::SetProjectionMatrix(glm::mat4 val) +{ + m_ProjectionMatrix = val; +} + + +void Camera::SetViewMatrix(glm::mat4 val) +{ + m_ViewMatrix = val; +} + //void Camera::Pitch(float val) //{ // m_Pitch = val; @@ -64,15 +76,6 @@ void Camera::SetOrientation(glm::quat val) void Camera::UpdateProjectionMatrix() { -// m_ProjectionMatrix = glm::ortho( -// -16.f, -// 16.f, -// -9.f, -// 9.f, -// m_NearClip, -// m_FarClip -// ); - m_ProjectionMatrix = glm::perspective(m_FOV, m_AspectRatio, m_NearClip, m_FarClip); } diff --git a/src/Engine/Rendering/DrawScenePass.cpp b/src/Engine/Rendering/DrawScenePass.cpp index 559a0f58..8fe6d827 100644 --- a/src/Engine/Rendering/DrawScenePass.cpp +++ b/src/Engine/Rendering/DrawScenePass.cpp @@ -21,29 +21,25 @@ void DrawScenePass::InitializeShaderPrograms() m_BasicForwardProgram->AddShader(std::shared_ptr(new FragmentShader("Shaders/BasicForward.frag.glsl"))); m_BasicForwardProgram->Compile(); m_BasicForwardProgram->Link(); - - } -void DrawScenePass::Draw(RenderQueueCollection& rq) +void DrawScenePass::Draw(RenderScene& scene) { //glBindFramebuffer(GL_FRAMEBUFFER, 0); GLERROR("Renderer::Draw PickingPass"); - DrawScenePassState state; + DrawScenePassState state = DrawScenePassState(); - - //TODO: Render: Add code for more jobs than modeljobs. - for (auto &job : rq.Forward) { + for (auto &job : scene.ForwardJobs) { auto modelJob = std::dynamic_pointer_cast(job); if (modelJob) { GLuint ShaderHandle = m_BasicForwardProgram->GetHandle(); m_BasicForwardProgram->Bind(); //TODO: Kolla upp "header/include/common" shader saken så man slipper skicka in asmycket uniforms - glUniformMatrix4fv(glGetUniformLocation(ShaderHandle, "M"), 1, GL_FALSE, glm::value_ptr(modelJob->ModelMatrix)); - glUniformMatrix4fv(glGetUniformLocation(ShaderHandle, "V"), 1, GL_FALSE, glm::value_ptr(m_Renderer->Camera()->ViewMatrix())); - glUniformMatrix4fv(glGetUniformLocation(ShaderHandle, "P"), 1, GL_FALSE, glm::value_ptr(m_Renderer->Camera()->ProjectionMatrix())); + glUniformMatrix4fv(glGetUniformLocation(ShaderHandle, "M"), 1, GL_FALSE, glm::value_ptr(modelJob->Matrix)); + glUniformMatrix4fv(glGetUniformLocation(ShaderHandle, "V"), 1, GL_FALSE, glm::value_ptr(scene.Camera->ViewMatrix())); + glUniformMatrix4fv(glGetUniformLocation(ShaderHandle, "P"), 1, GL_FALSE, glm::value_ptr(scene.Camera->ProjectionMatrix())); glUniform4fv(glGetUniformLocation(ShaderHandle, "Color"), 1, glm::value_ptr(modelJob->Color)); //TODO: Renderer: bättre textur felhantering samt fler texturer stöd @@ -59,8 +55,9 @@ void DrawScenePass::Draw(RenderQueueCollection& rq) glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, modelJob->Model->ElementBuffer); glDrawElementsBaseVertex(GL_TRIANGLES, modelJob->EndIndex - modelJob->StartIndex + 1, GL_UNSIGNED_INT, 0, modelJob->StartIndex); - continue; + //continue; } } + GLERROR("DrawScene Error"); } diff --git a/src/Engine/Rendering/DrawScenePassState.cpp b/src/Engine/Rendering/DrawScenePassState.cpp index 9e7497a3..2d643697 100644 --- a/src/Engine/Rendering/DrawScenePassState.cpp +++ b/src/Engine/Rendering/DrawScenePassState.cpp @@ -8,8 +8,10 @@ DrawScenePassState::DrawScenePassState() GLERROR("---"); Enable(GL_DEPTH_TEST); Enable(GL_CULL_FACE); - ClearColor(glm::vec4(255.f / 255, 163.f / 255, 176.f / 255, 0.f)); - Clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + Enable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + // ClearColor(glm::vec4(255.f / 255, 163.f / 255, 176.f / 255, 0.f)); + // Clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); } DrawScenePassState::~DrawScenePassState() diff --git a/src/Engine/Rendering/DummyRenderer.cpp b/src/Engine/Rendering/DummyRenderer.cpp index 4956a87a..ab529315 100644 --- a/src/Engine/Rendering/DummyRenderer.cpp +++ b/src/Engine/Rendering/DummyRenderer.cpp @@ -39,17 +39,10 @@ void DummyRenderer::Initialize() exit(EXIT_FAILURE); } - // Create default camera - m_DefaultCamera = new ::Camera((float)m_Resolution.Width / m_Resolution.Height, glm::radians(45.f), 0.01f, 5000.f); - m_DefaultCamera->SetPosition(glm::vec3(0, 0, 0)); - if (m_Camera == nullptr) { - m_Camera = m_DefaultCamera; - } - glfwSwapInterval(m_VSYNC); } -void DummyRenderer::Draw(RenderQueueCollection& rq) +void DummyRenderer::Draw(RenderFrame& rq) { glClearColor(255.f / 255, 163.f / 255, 176.f / 255, 0.f); glClear(GL_COLOR_BUFFER_BIT); diff --git a/src/Engine/Rendering/PickingPass.cpp b/src/Engine/Rendering/PickingPass.cpp index 148b272c..aa42a961 100644 --- a/src/Engine/Rendering/PickingPass.cpp +++ b/src/Engine/Rendering/PickingPass.cpp @@ -43,70 +43,109 @@ void PickingPass::InitializeShaderPrograms() m_PickingProgram->Link(); } -void PickingPass::Draw(RenderQueueCollection& rq) +void PickingPass::Draw(RenderScene& scene) { - m_PickingColorsToEntity.clear(); PickingPassState* state = new PickingPassState(m_PickingBuffer.GetHandle()); - int r = 0; - int g = 0; + //TODO: Render: Add code for more jobs than modeljobs. GLuint ShaderHandle = m_PickingProgram->GetHandle(); m_PickingProgram->Bind(); - std::map entityColors; + - for (auto &job : rq.Forward) { - auto modelJob = std::dynamic_pointer_cast(job); + m_Camera = scene.Camera; - if (modelJob) { - int pickColor[2] = { r, g }; - auto color = entityColors.find(modelJob->Entity); - if (color != entityColors.end()) { - pickColor[0] = color->second[0]; - pickColor[1] = color->second[1]; - } else { - entityColors[modelJob->Entity] = glm::vec2(pickColor[0], pickColor[1]); - if (r + 10 > 255) { - r = 0; - g += 1; + for (auto &job : scene.ForwardJobs) { + auto modelJob = std::dynamic_pointer_cast(job); + + if (modelJob) { + int pickColor[2] = { m_ColorCounter[0], m_ColorCounter[1] }; + + PickingInfo pickInfo; + pickInfo.Entity = modelJob->Entity; + pickInfo.World = modelJob->World; + pickInfo.Camera = scene.Camera; + + auto color = m_EntityColors.find(std::make_tuple(pickInfo.Entity, pickInfo.World, pickInfo.Camera)); + if (color != m_EntityColors.end()) { + pickColor[0] = color->second[0]; + pickColor[1] = color->second[1]; } else { - r += 1; + m_EntityColors[std::make_tuple(pickInfo.Entity, pickInfo.World, pickInfo.Camera)] = glm::ivec2(pickColor[0], pickColor[1]); + if (m_ColorCounter[0] > 255) { + m_ColorCounter[0] = 0; + m_ColorCounter[1]++;; + } else { + m_ColorCounter[0]++;; + } } - } - m_PickingColorsToEntity[glm::vec2(pickColor[0], pickColor[1])] = modelJob->Entity; - - //Render picking stuff - //TODO: Kolla upp "header/include/common" shader saken så man slipper skicka in asmycket uniforms - glUniformMatrix4fv(glGetUniformLocation(ShaderHandle, "M"), 1, GL_FALSE, glm::value_ptr(modelJob->ModelMatrix)); - glUniformMatrix4fv(glGetUniformLocation(ShaderHandle, "V"), 1, GL_FALSE, glm::value_ptr(m_Renderer->Camera()->ViewMatrix())); - glUniformMatrix4fv(glGetUniformLocation(ShaderHandle, "P"), 1, GL_FALSE, glm::value_ptr(m_Renderer->Camera()->ProjectionMatrix())); - glUniform2fv(glGetUniformLocation(ShaderHandle, "PickingColor"), 1, glm::value_ptr(glm::vec2(pickColor[0], pickColor[1]))); - glBindVertexArray(modelJob->Model->VAO); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, modelJob->Model->ElementBuffer); - glDrawElementsBaseVertex(GL_TRIANGLES, modelJob->EndIndex - modelJob->StartIndex + 1, GL_UNSIGNED_INT, nullptr, modelJob->StartIndex); + m_PickingColorsToEntity[glm::ivec2(pickColor[0], pickColor[1])] = pickInfo; + + glUniformMatrix4fv(glGetUniformLocation(ShaderHandle, "M"), 1, GL_FALSE, glm::value_ptr(modelJob->Matrix)); + glUniformMatrix4fv(glGetUniformLocation(ShaderHandle, "V"), 1, GL_FALSE, glm::value_ptr(scene.Camera->ViewMatrix())); + glUniformMatrix4fv(glGetUniformLocation(ShaderHandle, "P"), 1, GL_FALSE, glm::value_ptr(scene.Camera->ProjectionMatrix())); + glUniform2fv(glGetUniformLocation(ShaderHandle, "PickingColor"), 1, glm::value_ptr(glm::vec2(pickColor[0], pickColor[1]))); + + glBindVertexArray(modelJob->Model->VAO); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, modelJob->Model->ElementBuffer); + glDrawElementsBaseVertex(GL_TRIANGLES, modelJob->EndIndex - modelJob->StartIndex + 1, GL_UNSIGNED_INT, nullptr, modelJob->StartIndex); + } } - } + + m_PickingBuffer.Unbind(); GLERROR("PickingPass Error"); - //Publish pick event every frame with the pick data that can be picked by the event + + delete state; +} + + + +void PickingPass::ClearPicking() +{ + m_PickingColorsToEntity.clear(); + m_EntityColors.clear(); + m_ColorCounter[0] = 0; + m_ColorCounter[1] = 0; + + m_PickingBuffer.Bind(); + glClearColor(0.f, 0.f, 0.f, 0.f); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + m_PickingBuffer.Unbind(); +} + +PickData PickingPass::Pick(glm::vec2 screenCoord) +{ int fbWidth; int fbHeight; glfwGetFramebufferSize(m_Renderer->Window(), &fbWidth, &fbHeight); - Events::Picking pickEvent = Events::Picking( - &m_PickingBuffer, - &m_DepthBuffer, - m_Renderer->Camera()->ProjectionMatrix(), - m_Renderer->Camera()->ViewMatrix(), - Rectangle(fbWidth, fbHeight), - &m_PickingColorsToEntity); - m_EventBroker->Publish(pickEvent); + Rectangle resolution = Rectangle(fbWidth, fbHeight); + PickData pickData; + // Invert screen y coordinate + screenCoord.y = resolution.Height - screenCoord.y; + ScreenCoords::PixelData data = ScreenCoords::ToPixelData(screenCoord, &m_PickingBuffer, m_DepthBuffer); + pickData.Depth = data.Depth; - delete state; + PickingInfo pickInfo; + + auto it = m_PickingColorsToEntity.find(glm::ivec2(data.Color[0], data.Color[1])); + if (it != m_PickingColorsToEntity.end()) { + pickInfo = it->second; + } else { + pickData.Entity = EntityID_Invalid; + } + pickData.Position = ScreenCoords::ToWorldPos(screenCoord.x, screenCoord.y, data.Depth, resolution, pickInfo.Camera->ProjectionMatrix(), pickInfo.Camera->ViewMatrix()); + + + pickData.Entity = pickInfo.Entity; + pickData.Camera = pickInfo.Camera; + pickData.World = pickInfo.World; + return pickData; } void PickingPass::GenerateTexture(GLuint* texture, GLenum wrapping, GLenum filtering, glm::vec2 dimensions, GLint internalFormat, GLint format, GLenum type) const diff --git a/src/Engine/Rendering/PickingPassState.cpp b/src/Engine/Rendering/PickingPassState.cpp index 1e28ea66..2b4f30c4 100644 --- a/src/Engine/Rendering/PickingPassState.cpp +++ b/src/Engine/Rendering/PickingPassState.cpp @@ -10,8 +10,8 @@ PickingPassState::PickingPassState(GLuint frameBuffer) Enable(GL_CULL_FACE); glm::vec4 clearColor = glm::vec4(0.f); - ClearColor(clearColor); - Clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + //ClearColor(clearColor); + //Clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); } PickingPassState::~PickingPassState() diff --git a/src/Engine/Rendering/RawModel.cpp b/src/Engine/Rendering/RawModel.cpp index 95a75a15..d159f271 100644 --- a/src/Engine/Rendering/RawModel.cpp +++ b/src/Engine/Rendering/RawModel.cpp @@ -81,6 +81,9 @@ RawModel::RawModel(std::string fileName) float opacity; material->Get(AI_MATKEY_OPACITY, opacity); desc.DiffuseVertexColor = glm::vec4(diffuse.r, diffuse.g, diffuse.b, opacity); + + desc.DiffuseVertexColor = glm::vec4(diffuse.r, diffuse.g, diffuse.b, opacity); + // Material specular color aiColor3D specular; material->Get(AI_MATKEY_COLOR_SPECULAR, specular); @@ -134,6 +137,7 @@ RawModel::RawModel(std::string fileName) matGroup.EndIndex = m_Indices.size() - 1; // Material shininess material->Get(AI_MATKEY_SHININESS, matGroup.Shininess); + material->Get(AI_MATKEY_OPACITY, matGroup.Transparency); //LOG_DEBUG("Shininess: %f", matGroup.Shininess); // Diffuse texture //LOG_DEBUG("%i diffuse textures found", material->GetTextureCount(aiTextureType_DIFFUSE)); diff --git a/src/Engine/Rendering/RenderQueueFactory.cpp b/src/Engine/Rendering/RenderQueueFactory.cpp deleted file mode 100644 index 14e55320..00000000 --- a/src/Engine/Rendering/RenderQueueFactory.cpp +++ /dev/null @@ -1,116 +0,0 @@ -#include "Rendering/RenderQueueFactory.h" - - -RenderQueueFactory::RenderQueueFactory() -{ - m_RenderQueues = RenderQueueCollection(); -} - -void RenderQueueFactory::Update(World* world) -{ - m_RenderQueues.Clear(); - FillModels(world, &m_RenderQueues.Forward); - FillLights(world, &m_RenderQueues.Lights); -} - -glm::mat4 RenderQueueFactory::ModelMatrix(World* world, EntityID entity) -{ - glm::vec3 position = AbsolutePosition(world, entity); - glm::quat orientation = AbsoluteOrientation(world, entity); - glm::vec3 scale = AbsoluteScale(world, entity); - - glm::mat4 modelMatrix = glm::translate(glm::mat4(), position) * glm::toMat4(orientation) * glm::scale(scale); - return modelMatrix; -} - -glm::vec3 RenderQueueFactory::AbsolutePosition(World* world, EntityID entity) -{ - glm::vec3 position; - - while (entity != EntityID_Invalid) { - ComponentWrapper transform = world->GetComponent(entity, "Transform"); - EntityID parent = world->GetParent(entity); - //if (parent != EntityID_Invalid) { - position += AbsoluteOrientation(world, parent) * (glm::vec3)transform["Position"]; - //} else { - // position += (glm::vec3)transform["Position"]; - //} - entity = parent; - } - - return position; -} - -glm::quat RenderQueueFactory::AbsoluteOrientation(World* world, EntityID entity) -{ - glm::quat orientation; - - while (entity != EntityID_Invalid) { - ComponentWrapper transform = world->GetComponent(entity, "Transform"); - orientation = glm::quat((glm::vec3)transform["Orientation"]) * orientation; - entity = world->GetParent(entity); - } - - return orientation; -} - -glm::vec3 RenderQueueFactory::AbsoluteScale(World* world, EntityID entity) -{ - glm::vec3 scale(1.f); - - while (entity != EntityID_Invalid) { - ComponentWrapper transform = world->GetComponent(entity, "Transform"); - scale *= (glm::vec3)transform["Scale"]; - entity = world->GetParent(entity); - } - - return scale; -} - -void RenderQueueFactory::FillModels(World* world, RenderQueue* renderQueue) -{ - auto models = world->GetComponents("Model"); - if (models == nullptr) { - return; - } - - for (auto& modelC : *models) { - bool visible = modelC["Visible"]; - if (!visible) { - continue; - } - std::string resource = modelC["Resource"]; - if (resource.empty()) { - continue; - } - glm::vec4 color = modelC["Color"]; - Model* model = ResourceManager::Load(resource); - if (model == nullptr) { - model = ResourceManager::Load("Models/Core/Error.obj"); - } - - for (auto texGroup : model->TextureGroups) { - ModelJob job; - job.TextureID = (texGroup.Texture) ? texGroup.Texture->ResourceID : 0; - job.DiffuseTexture = texGroup.Texture.get(); - job.NormalTexture = texGroup.NormalMap.get(); - job.SpecularTexture = texGroup.SpecularMap.get(); - job.Model = model; - job.StartIndex = texGroup.StartIndex; - job.EndIndex = texGroup.EndIndex; - job.ModelMatrix = model->m_Matrix * ModelMatrix(world, modelC.EntityID); - job.Color = color; - - //TODO: RENDERER: Not sure if the best solution for pickingColor to entity link is this - job.Entity = modelC.EntityID; - - renderQueue->Add(job); - } - } -} - -void RenderQueueFactory::FillLights(World* world, RenderQueue* renderQueue) -{ - -} - diff --git a/src/Engine/Rendering/RenderSystem.cpp b/src/Engine/Rendering/RenderSystem.cpp new file mode 100644 index 00000000..d6cdb9b3 --- /dev/null +++ b/src/Engine/Rendering/RenderSystem.cpp @@ -0,0 +1,194 @@ +#include "Rendering/RenderSystem.h" +#include "Rendering/DebugCameraInputController.h" + +RenderSystem::RenderSystem(EventBroker* eventBrokerer, const IRenderer* renderer, RenderFrame* renderFrame) :ImpureSystem(eventBrokerer) +{ + m_Renderer = renderer; + m_RenderFrame = renderFrame; + EVENT_SUBSCRIBE_MEMBER(m_ESetCamera, &RenderSystem::OnSetCamera); + EVENT_SUBSCRIBE_MEMBER(m_EInputCommand, &RenderSystem::OnInputCommand); + + m_DefaultCamera = new Camera((float)m_Renderer->Resolution().Width / m_Renderer->Resolution().Height, glm::radians(45.f), 0.01f, 5000.f); + m_DefaultCamera->SetPosition(glm::vec3(0, 0, 10)); + if (m_Camera == nullptr) { + m_Camera = m_DefaultCamera; + } +} + +bool RenderSystem::OnSetCamera(const Events::SetCamera &event) +{ + auto cameras = m_World->GetComponents("Camera"); + + if (cameras != nullptr) { + for (auto it = cameras->begin(); it != cameras->end(); it++) { + if ((std::string)(*it)["Name"] == event.Name) { + switchCamera((*it).EntityID); + } + } + } + return true; +} + +void RenderSystem::switchCamera(EntityID entity) +{ + if(m_World->HasComponent(entity, "Camera")) { + + if (m_CurrentCamera != EntityID_Invalid) { + if (m_World->HasComponent(m_CurrentCamera, "Model")) { + m_World->GetComponent(m_CurrentCamera, "Model")["Visible"] = true; + } + } + + if (m_World->HasComponent(entity, "Model")) { + m_World->GetComponent(entity, "Model")["Visible"] = false; + } + m_CurrentCamera = entity; + m_SwitchCamera = false; + + } else { + LOG_ERROR("Entity %i does not have a CameraComponent", entity); + m_SwitchCamera = false; + } +} + +void RenderSystem::updateProjectionMatrix(ComponentWrapper& cameraComponent) +{ + double fov = cameraComponent["FOV"]; + double aspectRatio = m_Renderer->Resolution().Width / m_Renderer->Resolution().Height; + double nearClip = cameraComponent["NearClip"]; + double farClip = cameraComponent["FarClip"]; + + double fovY = atan(tan(glm::radians(fov)/2.0) * aspectRatio) * 2.0; + m_ProjectionMatrix = glm::perspective(fovY, aspectRatio, nearClip, farClip); + + m_Camera->SetFOV(fovY); + m_Camera->SetAspectRatio(aspectRatio); + m_Camera->SetNearClip(nearClip); + m_Camera->SetFarClip(farClip); + m_Camera->UpdateProjectionMatrix(); +} + +void RenderSystem::fillModels(std::list>& jobs, World* world) +{ + auto models = world->GetComponents("Model"); + if (models == nullptr) { + return; + } + + for (auto& modelComponent : *models) { + bool visible = modelComponent["Visible"]; + if (!visible) { + continue; + } + std::string resource = modelComponent["Resource"]; + if (resource.empty()) { + continue; + } + + Model* model = ResourceManager::Load<::Model>(resource); + if (model == nullptr) { + model = ResourceManager::Load<::Model>("Models/Core/Error.obj"); + } + + glm::mat4 modelMatrix = Transform::ModelMatrix(modelComponent.EntityID, world); + + for (auto texGroup : model->TextureGroups) { + std::shared_ptr modelJob = std::shared_ptr(new ModelJob(model, m_Camera, modelMatrix, texGroup, modelComponent, world)); + jobs.push_back(modelJob); + } + } +} + +bool RenderSystem::OnInputCommand(const Events::InputCommand& e) +{ + if (e.Command == "SwitchCamera" && e.Value > 0) { + m_SwitchCamera = true; + return true; + } else { + return false; + } +} + +void RenderSystem::Update(World* world, double dt) +{ + m_World = world; + m_EventBroker->Process(); + + updateCamera(world, dt); + + //Only supports opaque geometry atm + m_RenderFrame->Clear(); + + RenderScene rs; + rs.Camera = m_Camera; + rs.Viewport = Rectangle(1280, 720); + fillModels(rs.ForwardJobs, world); + m_RenderFrame->Add(rs); + +} + +void RenderSystem::updateCamera(World* world, double dt) +{ + + static DebugCameraInputController firstPersonInputController(m_EventBroker, -1); + + if (m_SwitchCamera) { + auto cameras = world->GetComponents("Camera"); + for (auto it = cameras->begin(); it != cameras->end(); it++) { + if ((*it).EntityID == m_CurrentCamera) { + it++; + if (it != cameras->end()) { + switchCamera((*it).EntityID); + } else { + switchCamera((*cameras->begin()).EntityID); + } + break; + } + } + ComponentWrapper& cameraComponent = world->GetComponent(m_CurrentCamera, "Camera"); + ComponentWrapper& cameraTransform = world->GetComponent(m_CurrentCamera, "Transform"); + + firstPersonInputController.SetOrientation(glm::quat((glm::vec3)cameraTransform["Orientation"])); + firstPersonInputController.SetPosition(cameraTransform["Position"]); + + } + + if (m_World->ValidEntity(m_CurrentCamera)) { + if (world->HasComponent(m_CurrentCamera, "Camera") && world->HasComponent(m_CurrentCamera, "Transform")) { + ComponentWrapper& cameraComponent = world->GetComponent(m_CurrentCamera, "Camera"); + ComponentWrapper& cameraTransform = world->GetComponent(m_CurrentCamera, "Transform"); + + firstPersonInputController.Update(dt); + (glm::vec3&)cameraTransform["Orientation"] = glm::eulerAngles(firstPersonInputController.Orientation()); + (glm::vec3&)cameraTransform["Position"] = firstPersonInputController.Position(); + + glm::vec3 position = Transform::AbsolutePosition(world, m_CurrentCamera); + glm::quat orientation = Transform::AbsoluteOrientation(world, m_CurrentCamera); + + m_Camera->SetPosition(position); + m_Camera->SetOrientation(orientation); + + updateProjectionMatrix(cameraComponent); + + } + } else { + m_Camera = m_DefaultCamera; + + auto cameras = world->GetComponents("Camera"); + if (cameras != nullptr) { + if (cameras->begin() != cameras->end()) { + ComponentWrapper& cameraC = *cameras->begin(); + switchCamera(cameraC.EntityID); + + ComponentWrapper& cameraComponent = world->GetComponent(m_CurrentCamera, "Camera"); + ComponentWrapper& cameraTransform = world->GetComponent(m_CurrentCamera, "Transform"); + + firstPersonInputController.SetOrientation(glm::quat((glm::vec3)cameraTransform["Orientation"])); + firstPersonInputController.SetPosition(cameraTransform["Position"]); + } + } + } + + m_Camera->UpdateViewMatrix(); +} + diff --git a/src/Engine/Rendering/Renderer.cpp b/src/Engine/Rendering/Renderer.cpp index 208f339b..51546f5b 100644 --- a/src/Engine/Rendering/Renderer.cpp +++ b/src/Engine/Rendering/Renderer.cpp @@ -3,27 +3,27 @@ void Renderer::Initialize() { InitializeWindow(); - // Create default camera - m_DefaultCamera = new ::Camera((float)m_Resolution.Width / m_Resolution.Height, glm::radians(45.f), 0.01f, 5000.f); - m_DefaultCamera->SetPosition(glm::vec3(0, 0, 10)); - if (m_Camera == nullptr) { - m_Camera = m_DefaultCamera; - } - m_DebugCameraInputController = std::make_shared>(m_EventBroker, -1); - TEMPCreateLights(); + InitializeRenderPasses(); glfwSwapInterval(m_VSYNC); InitializeShaders(); InitializeTextures(); - InitializeSSBOs(); - //CalculateFrustum(); m_ScreenQuad = ResourceManager::Load("Models/Core/ScreenQuad.obj"); m_UnitQuad = ResourceManager::Load("Models/Core/UnitQuad.obj"); m_UnitSphere = ResourceManager::Load("Models/Core/UnitSphere.obj"); m_ImGuiRenderPass = new ImGuiRenderPass(this, m_EventBroker); + + + // Create default camera + m_DefaultCamera = new ::Camera((float)m_Resolution.Width / m_Resolution.Height, glm::radians(45.f), 0.01f, 5000.f); + m_DefaultCamera->SetPosition(glm::vec3(0, 0, 10)); + if (m_Camera == nullptr) { + m_Camera = m_DefaultCamera; + } + } void Renderer::InitializeWindow() @@ -75,52 +75,11 @@ void Renderer::InitializeShaders() m_DrawScreenQuadProgram->AddShader(std::shared_ptr(new FragmentShader("Shaders/DrawScreenQuad.frag.glsl"))); m_DrawScreenQuadProgram->Compile(); m_DrawScreenQuadProgram->Link(); - - //m_CalculateFrustumProgram = ResourceManager::Load("#CalculateFrustumProgram"); - //m_CalculateFrustumProgram.AddShader(std::shared_ptr(new ComputeShader("Shaders/GridFrustum.comp.glsl"))); - //m_CalculateFrustumProgram.Compile(); - //m_CalculateFrustumProgram.Link(); - - //m_LightCullProgram = ResourceManager::Load("#LightCullProgram"); - //m_LightCullProgram.AddShader(std::shared_ptr(new ComputeShader("Shaders/cullLights.comp.glsl"))); - //m_LightCullProgram.Compile(); - //m_LightCullProgram.Link(); } void Renderer::InputUpdate(double dt) { - glm::vec3 m_Position = m_Camera->Position(); - if (glfwGetKey(m_Window, GLFW_KEY_O) == GLFW_PRESS) - { - m_Position = glm::vec3(0.f, 0.f, 5.f); - } - if (glfwGetKey(m_Window, GLFW_KEY_W) == GLFW_PRESS) - { - m_Position += m_Camera->Forward() * m_CameraMoveSpeed * (float)dt; - } - if (glfwGetKey(m_Window, GLFW_KEY_S) == GLFW_PRESS) - { - m_Position -= m_Camera->Forward() * m_CameraMoveSpeed * (float)dt; - } - if (glfwGetKey(m_Window, GLFW_KEY_D) == GLFW_PRESS) - { - m_Position += m_Camera->Right() * m_CameraMoveSpeed * (float)dt; - } - if (glfwGetKey(m_Window, GLFW_KEY_A) == GLFW_PRESS) - { - m_Position -= m_Camera->Right() * m_CameraMoveSpeed * (float)dt; - } - if (glfwGetKey(m_Window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS) - { - m_CameraMoveSpeed = 5.f; - } - else { - m_CameraMoveSpeed = 0.5f; - } - - m_DebugCameraInputController->Update(dt); - m_Camera->SetOrientation(m_DebugCameraInputController->Orientation()); - m_Camera->SetPosition(m_DebugCameraInputController->Position()); + } void Renderer::Update(double dt) @@ -130,20 +89,31 @@ void Renderer::Update(double dt) m_ImGuiRenderPass->Update(dt); } -void Renderer::Draw(RenderQueueCollection& rq) +void Renderer::Draw(RenderFrame& frame) { - m_PickingPass->Draw(rq); - //DrawScreenQuad(m_PickingPass->PickingTexture()); - //CullLights(); - - glClearColor(255.f / 255, 163.f / 255, 176.f / 255, 1.f); + glClearColor(255.f / 255, 163.f / 255, 176.f / 255, 0.f); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + m_PickingPass->ClearPicking(); + for (auto scene : frame.RenderScenes){ + m_Camera = scene->Camera; // remove renderer camera when Editor uses the render scene cameras. + m_PickingPass->Draw(*scene); + + + + m_DrawScenePass->Draw(*scene); + GLERROR("Renderer::Draw m_DrawScenePass->Draw"); + } - m_DrawScenePass->Draw(rq); - GLERROR("Renderer::Draw m_DrawScenePass->Draw"); m_ImGuiRenderPass->Draw(); glfwSwapBuffers(m_Window); } +PickData Renderer::Pick(glm::vec2 screenCoord) +{ + return m_PickingPass->Pick(screenCoord); +} + void Renderer::DrawScreenQuad(GLuint textureToDraw) { glBindFramebuffer(GL_FRAMEBUFFER, 0); @@ -183,95 +153,8 @@ void Renderer::GenerateTexture(GLuint* texture, GLenum wrapping, GLenum filterin GLERROR("Texture initialization failed"); } -void Renderer::InitializeSSBOs() -{ - printf("Size: %i\n", sizeof(m_Frustums)); - glGenBuffers(1, &m_FrustumSSBO); - glBindBuffer(GL_SHADER_STORAGE_BUFFER, m_FrustumSSBO); - glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(m_Frustums), &m_Frustums, GL_DYNAMIC_COPY); - glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, m_FrustumSSBO); - glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0); - - GLERROR("m_FrustumSSBO"); - - glGenBuffers(1, &m_LightSSBO); - glBindBuffer(GL_SHADER_STORAGE_BUFFER, m_LightSSBO); - glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(m_PointLights), &m_PointLights, GL_DYNAMIC_COPY); - glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, m_LightSSBO); - glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0); - - GLERROR("m_LightSSBO"); - - - glGenBuffers(1, &m_LightGridSSBO); - glBindBuffer(GL_SHADER_STORAGE_BUFFER, m_LightGridSSBO); - glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(m_LightGrid), &m_LightGrid, GL_DYNAMIC_COPY); - glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, m_LightGridSSBO); - glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0); - - GLERROR("m_LightGridSSBO"); - - - glGenBuffers(1, &m_LightOffsetSSBO); - glBindBuffer(GL_SHADER_STORAGE_BUFFER, m_LightOffsetSSBO); - glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(m_LightOffset), &m_LightOffset, GL_DYNAMIC_COPY); - glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 3, m_LightOffsetSSBO); - glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0); - - GLERROR("m_LightOffsetSSBO"); - - - glGenBuffers(1, &m_LightIndexSSBO); - glBindBuffer(GL_SHADER_STORAGE_BUFFER, m_LightIndexSSBO); - glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(m_LightIndex), &m_LightIndex, GL_DYNAMIC_COPY); - glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 4, m_LightIndexSSBO); - glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0); - - GLERROR("m_LightIndexSSBO"); - -} - void Renderer::InitializeRenderPasses() { m_DrawScenePass = new DrawScenePass(this); m_PickingPass = new PickingPass(this, m_EventBroker); } - -void Renderer::CalculateFrustum() -{ - GLERROR("CalculateFrustum Error-1"); - m_CalculateFrustumProgram->Bind(); - - GLERROR("CalculateFrustum Error1"); - glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, m_FrustumSSBO); - GLERROR("CalculateFrustum Error2"); - glUniformMatrix4fv(glGetUniformLocation(m_CalculateFrustumProgram->GetHandle(), "P"), 1, false, glm::value_ptr(m_Camera->ProjectionMatrix())); - GLERROR("CalculateFrustum Error3"); - glUniform2f(glGetUniformLocation(m_CalculateFrustumProgram->GetHandle(), "ScreenDimensions"), m_Resolution.Width, m_Resolution.Height); - GLERROR("CalculateFrustum Error4"); - glDispatchCompute(5, 3, 1); - GLERROR("CalculateFrustum Error5"); - -} - -void Renderer::TEMPCreateLights() -{ - for (int i = 0; i < NUM_LIGHTS; i++) { - m_PointLights[i].Position = glm::vec4(i, 0.f, 0.f, 0.f); - m_PointLights[i].Color = glm::vec4(1.f, 0.5f, 0.f + i*0.1f, 1.f); - } -} - -void Renderer::CullLights() -{ - m_LightCullProgram->Bind(); - glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, m_FrustumSSBO); - glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, m_LightSSBO); - glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, m_LightGridSSBO); - glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 3, m_LightOffsetSSBO); - glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 4, m_LightIndexSSBO); - glDispatchCompute(m_Resolution.Width / TILE_SIZE, m_Resolution.Height / TILE_SIZE, 1); - GLERROR("CullLights Error"); - -} - diff --git a/src/Game/Game.cpp b/src/Game/Game.cpp index 29b9316b..2be1cb28 100644 --- a/src/Game/Game.cpp +++ b/src/Game/Game.cpp @@ -22,10 +22,9 @@ Game::Game(int argc, char* argv[]) // Create the core event broker m_EventBroker = new EventBroker(); - m_RenderQueueFactory = new RenderQueueFactory(); // Create the renderer - m_Renderer = new Renderer(m_EventBroker); + m_Renderer = new Renderer(m_EventBroker, m_World); m_Renderer->SetFullscreen(m_Config->Get("Video.Fullscreen", false)); m_Renderer->SetVSYNC(m_Config->Get("Video.VSYNC", false)); m_Renderer->SetResolution(Rectangle::Rectangle( @@ -35,7 +34,7 @@ Game::Game(int argc, char* argv[]) m_Config->Get("Video.Height", 720) )); m_Renderer->Initialize(); - m_Renderer->Camera()->SetFOV(glm::radians(m_Config->Get("Video.FOV", 90.f))); + //m_Renderer->Camera()->SetFOV(glm::radians(m_Config->Get("Video.FOV", 90.f))); // Create input manager m_InputManager = new InputManager(m_Renderer->Window(), m_EventBroker); @@ -60,12 +59,15 @@ Game::Game(int argc, char* argv[]) fp.MergeEntities(m_World); } + m_RenderFrame = new RenderFrame(); + // Create Octrees m_OctreeCollision = new Octree(AABB(glm::vec3(-100), glm::vec3(100)), 4); m_OctreeFrustrumCulling = new Octree(AABB(glm::vec3(-100), glm::vec3(100)), 4); // Create system pipeline m_SystemPipeline = new SystemPipeline(m_EventBroker); + // All systems with orderlevel 0 will be updated first. unsigned int updateOrderLevel = 0; m_SystemPipeline->AddSystem(updateOrderLevel); @@ -81,6 +83,9 @@ Game::Game(int argc, char* argv[]) m_SystemPipeline->AddSystem(updateOrderLevel, m_OctreeCollision); m_SystemPipeline->AddSystem(updateOrderLevel, m_OctreeCollision); + ++updateOrderLevel; + m_SystemPipeline->AddSystem(updateOrderLevel, m_Renderer, m_RenderFrame); + // Invoke network if (m_Config->Get("Networking.StartNetwork", false)) { //boost::thread workerThread(&Game::networkFunction, this); @@ -98,8 +103,8 @@ Game::~Game() delete m_FrameStack; delete m_InputProxy; delete m_InputManager; + delete m_RenderFrame; delete m_Renderer; - delete m_RenderQueueFactory; delete m_EventBroker; } @@ -130,9 +135,8 @@ void Game::Tick() m_Renderer->Update(dt); m_EventBroker->Process(); - m_RenderQueueFactory->Update(m_World); GLERROR("Game::Tick m_RenderQueueFactory->Update"); - m_Renderer->Draw(m_RenderQueueFactory->RenderQueues()); + m_Renderer->Draw(*m_RenderFrame); GLERROR("Game::Tick m_Renderer->Draw"); m_EventBroker->Swap(); m_EventBroker->Clear(); diff --git a/src/Tests/ResourceManagerTest.cpp b/src/Tests/ResourceManagerTest.cpp index a3edb7b8..9d62fa93 100644 --- a/src/Tests/ResourceManagerTest.cpp +++ b/src/Tests/ResourceManagerTest.cpp @@ -7,7 +7,6 @@ #include "Core/ResourceManager.h" #include "Core/ConfigFile.h" #include "Rendering/Renderer.h" -#include "Core/EntityXMLFile.h" #include "Engine\Rendering\Texture.h" BOOST_AUTO_TEST_SUITE(resourceManagerTests)