diff --git a/include/Core/Engine.h b/include/Core/Engine.h index 359c230..87cb918 100755 --- a/include/Core/Engine.h +++ b/include/Core/Engine.h @@ -43,6 +43,7 @@ #include "Game/CPad.h" #include "GUI/Frame.h" +#include "GUI/GameFrame.h" #include "Physics/PhysicsSystem.h" #include "Physics/CPhysics.h" @@ -103,7 +104,7 @@ public: m_FrameStack->Width = 1920; m_FrameStack->Height = 1080; - + new dd::GameFrame(m_FrameStack, "GameFrame"); //TODO: Remove tobias light-test code. @@ -159,14 +160,15 @@ public: sprite->SpecularTexture = "Textures/Test/Brick_Specular.png"; } + // Top Wall { auto topWall = m_World->CreateEntity(); std::shared_ptr transform = m_World->AddComponent(topWall); transform->Position = glm::vec3(0.f, 6.f, -10.f); transform->Scale = glm::vec3(18.f, 0.5f, 1.f); - std::shared_ptr sprite = m_World->AddComponent(topWall); - sprite->SpriteFile = "Textures/Core/ErrorTexture.png"; + //std::shared_ptr sprite = m_World->AddComponent(topWall); + //sprite->SpriteFile = "Textures/Core/ErrorTexture.png"; std::shared_ptr boxShape = m_World->AddComponent(topWall); @@ -175,14 +177,15 @@ public: m_World->CommitEntity(topWall); } + // Left Wall { auto leftWall = m_World->CreateEntity(); std::shared_ptr transform = m_World->AddComponent(leftWall); transform->Position = glm::vec3(-9.f, 1.f, -10.f); transform->Scale = glm::vec3(0.5f, 10.f, 1.f); - std::shared_ptr sprite = m_World->AddComponent(leftWall); - sprite->SpriteFile = "Textures/Core/ErrorTexture.png"; + //std::shared_ptr sprite = m_World->AddComponent(leftWall); + //sprite->SpriteFile = "Textures/Core/ErrorTexture.png"; std::shared_ptr boxShape = m_World->AddComponent(leftWall); @@ -191,14 +194,15 @@ public: m_World->CommitEntity(leftWall); } + // Right Wall. { auto rightWall = m_World->CreateEntity(); std::shared_ptr transform = m_World->AddComponent(rightWall); transform->Position = glm::vec3(9.f, 1.f, -10.f); transform->Scale = glm::vec3(0.5f, 10.f, 1.f); - std::shared_ptr sprite = m_World->AddComponent(rightWall); - sprite->SpriteFile = "Textures/Core/ErrorTexture.png"; + //std::shared_ptr sprite = m_World->AddComponent(rightWall); + //sprite->SpriteFile = "Textures/Core/ErrorTexture.png"; std::shared_ptr boxShape = m_World->AddComponent(rightWall); diff --git a/include/Core/RenderQueue.h b/include/Core/RenderQueue.h index ccf1a3a..64c587e 100755 --- a/include/Core/RenderQueue.h +++ b/include/Core/RenderQueue.h @@ -88,6 +88,10 @@ struct SpriteJob : RenderJob unsigned int ShaderID; unsigned int TextureID; + //Niklas is trying to get Frames to work. + GLuint Texture; + //Niklas is trying to get Frames to work./end + glm::mat4 ModelMatrix; GLuint DiffuseTexture; GLuint NormalTexture; @@ -167,6 +171,23 @@ struct RenderQueueCollection } }; +struct RenderQueuePair + +{ + RenderQueue Deferred; + RenderQueue Forward; + + void Clear() { + Deferred.Clear(); + Forward.Clear(); + } + + void Sort() { + Deferred.Sort(); + Forward.Sort(); + } +}; + } #endif // RenderQueue_h__ \ No newline at end of file diff --git a/include/Core/Renderer.h b/include/Core/Renderer.h index 241366d..f1e1510 100755 --- a/include/Core/Renderer.h +++ b/include/Core/Renderer.h @@ -51,6 +51,32 @@ public: } } + + + //Niklas is trying to implement Frames. + std::shared_ptr GetCamera() const { return m_FrameCamera; } + void SetCamera(const std::shared_ptr camera) + { + m_FrameCamera = camera; + } + + void DrawFrame(RenderQueuePair &rq); + void DrawWorld(RenderQueuePair &rq); + + void SetViewport(const Rectangle &viewport) + { + m_Viewport = viewport; + } + + void SetScissor(const Rectangle &scissor) + { + m_Scissor = scissor; + } + + int Width() const { return m_Width; } + int Height() const { return m_Height; } + //Niklas is trying to implement Frames. + void Initialize(); void Draw(RenderQueueCollection& rq); @@ -61,8 +87,17 @@ private: //Rectangle m_Viewport; //Rectangle m_Scissor; std::unique_ptr m_DefaultCamera = nullptr; + const dd::Camera* m_Camera = nullptr; + //Niklas is trying to implement Frames. + std::shared_ptr m_FrameCamera; + Rectangle m_Viewport; + Rectangle m_Scissor; + ShaderProgram m_ForwardRendering; + int m_Width = 1980, m_Height = 1080; + //Niklas is trying to implement Frames./end + int m_GLVersion[2]; std::string m_GLVendor; GLFWwindow* m_Window = nullptr; diff --git a/include/Core/ResourceManager.h b/include/Core/ResourceManager.h index b16099c..6ea29db 100644 --- a/include/Core/ResourceManager.h +++ b/include/Core/ResourceManager.h @@ -107,6 +107,12 @@ public: @tparam T Resource type. @param resourceName Fully qualified name of the resource to fetch. */ + + //Niklas is trying to get frames to work. + template + T* Load(std::string resourceType, std::string resourceName); + //Niklas is trying to get frames to work./end + template static T* Fetch(std::string resourceName); @@ -142,6 +148,7 @@ private: // Internal: Create a resource and cache it template static T* CreateResource(std::string resourceName, Resource* parent); + Resource* CreateResource(std::string resourceType, std::string resourceName); }; template @@ -161,6 +168,25 @@ T* ResourceManager::Load(std::string resourceName, Resource* parent /* = nullptr return CreateResource(resourceName, parent); } +//Niklas is trying to get frames to work. +template +T* ResourceManager::Load(std::string resourceType, std::string resourceName) +{ + auto it = m_ResourceCache.find(std::make_pair(resourceType, resourceName)); + if (it != m_ResourceCache.end()) + return static_cast(it->second); + if (m_Preloading) { + LOG_INFO("Preloading resource \"%s\"", resourceName.c_str()); + } + else { + LOG_WARNING("Hot-loading resource \"%s\"", resourceName.c_str()); + } + return static_cast(CreateResource(resourceType, resourceName)); +} + + + //Niklas is trying to get frames to work./end + template T* ResourceManager::Fetch(std::string resourceName) { diff --git a/include/GUI/Frame.h b/include/GUI/Frame.h index 9a15351..f90801f 100644 --- a/include/GUI/Frame.h +++ b/include/GUI/Frame.h @@ -11,7 +11,7 @@ namespace dd #include #include -#include "Util/Rectangle.h" +#include "Core/Util/Rectangle.h" #include "Core/EventBroker.h" #include "Core/EKeyDown.h" #include "Core/EKeyUp.h" diff --git a/include/GUI/GameFrame.h b/include/GUI/GameFrame.h index 0163d1a..1556fe3 100644 --- a/include/GUI/GameFrame.h +++ b/include/GUI/GameFrame.h @@ -8,13 +8,13 @@ #include "GUI/Frame.h" #include "GUI/MainMenuFrame.h" //#include "GUI/WorldFrame.h" -//#include "GUI/Viewport.h" +#include "GUI/Viewport.h" #include "GUI/TextureFrame.h" //#include "GUI/PlayerHUD.h" //#include "GameWorld.h" #include "Game/EGameStart.h" -//#include "Events/GameOver.h" +#include "Game/EGameOver.h" //#include "Events/Dead.h" namespace dd @@ -27,8 +27,8 @@ namespace dd : Frame(parent, name) , m_GameOver(false) { - EVENT_SUBSCRIBE_MEMBER(m_EGameStart, &GUI::GameFrame::OnGameStart); - EVENT_SUBSCRIBE_MEMBER(m_EGameOver, &GUI::GameFrame::OnGameOver); + EVENT_SUBSCRIBE_MEMBER(m_EGameStart, &dd::GameFrame::OnGameStart); + EVENT_SUBSCRIBE_MEMBER(m_EGameOver, &dd::GameFrame::OnGameOver); //m_World = std::make_shared(EventBroker, ResourceManager); /*m_WorldFrame = new WorldFrame(this, "GameWorldFrame", m_World); @@ -46,8 +46,8 @@ namespace dd m_FreeCamViewport = new Viewport(m_WorldFrame, "ViewportFreeCam", m_World); m_FreeCamViewport->Hide(); }*/ - m_WorldFrame->Hide(); - m_World->Initialize(); + //m_WorldFrame->Hide(); + //m_World->Initialize(); m_MainMenuFrame = new MainMenuFrame(this, "MainMenu"); } @@ -56,11 +56,11 @@ namespace dd EventRelay m_EGameStart; bool OnGameStart(const dd::Events::GameStart &event) { - m_WorldFrame->Show(); + //m_WorldFrame->Show(); return true; } - /*EventRelay m_EGameOver; + EventRelay m_EGameOver; bool OnGameOver(const Events::GameOver &event) { m_GameOver = true; @@ -68,15 +68,15 @@ namespace dd return true; } - std::shared_ptr m_World; + //std::shared_ptr m_World; MainMenuFrame* m_MainMenuFrame; - WorldFrame* m_WorldFrame; - Viewport* vp1; - Viewport* vp2; - Viewport* m_FreeCamViewport; + //WorldFrame* m_WorldFrame; + //Viewport* vp1; + //Viewport* vp2; + //Viewport* m_FreeCamViewport; - bool m_GameOver;*/ + bool m_GameOver; bool OnKeyUp(const dd::Events::KeyUp &event) override { diff --git a/include/GUI/TextureFrame.h b/include/GUI/TextureFrame.h index d7bd6cd..98a8e71 100644 --- a/include/GUI/TextureFrame.h +++ b/include/GUI/TextureFrame.h @@ -49,7 +49,7 @@ public: Texture *Texture() const { return m_Texture; } void SetTexture(std::string resourceName) { - m_Texture = ResourceManager->Load<::Texture>("Texture", resourceName); + m_Texture = ResourceManager->Load("Texture", resourceName); } void FadeToTexture(std::string resourceName, double duration) { @@ -75,8 +75,8 @@ public: void SetColor(glm::vec4 val) { m_Color = val; } protected: - Texture *m_Texture; - Texture *m_FadeTexture; + dd::Texture *m_Texture; + dd::Texture *m_FadeTexture; glm::vec4 m_Color; float m_FadeDuration; float m_CurrentFade; diff --git a/include/GUI/Viewport.h b/include/GUI/Viewport.h index 8c49323..129f0ce 100644 --- a/include/GUI/Viewport.h +++ b/include/GUI/Viewport.h @@ -15,70 +15,66 @@ #include "Core/RenderQueue.h" #include "Core/Camera.h" -namespace GUI +namespace dd { - class Viewport : public Frame - { - public: - Viewport(Frame* parent, std::string name, std::shared_ptr world) - : Frame(parent, name) - , m_World(world) - { } +class Viewport : public Frame +{ +public: + 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; + EntityID CameraEntity() const { return m_CameraEntity; } - m_Camera = std::make_shared(cameraComponent->FOV, cameraComponent->NearClip, cameraComponent->FarClip); - } + 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_FrameCamera = std::make_shared(cameraComponent->FOV, cameraComponent->NearClip, cameraComponent->FarClip); + } - void Update(double dt) override - { - if (!m_TransformSystem) - m_TransformSystem = m_World->GetSystem(); + void Update(double dt) override { + if (!m_TransformSystem) + m_TransformSystem = m_World->GetSystem(); - auto transformComponent = m_World->GetComponent(m_CameraEntity); - if (!transformComponent) - return; + auto transformComponent = m_World->GetComponent(m_CameraEntity); + if (!transformComponent) + return; - auto cameraComponent = m_World->GetComponent(m_CameraEntity); - if (!cameraComponent) - return; + auto cameraComponent = m_World->GetComponent(m_CameraEntity); + if (!cameraComponent) + return; - Components::Transform absoluteTransform = m_TransformSystem->AbsoluteTransform(m_CameraEntity); + dd::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); - } + m_FrameCamera->SetFOV(cameraComponent->FOV); + m_FrameCamera->SetNearClip(cameraComponent->NearClip); + m_FrameCamera->SetFarClip(cameraComponent->FarClip); + m_FrameCamera->SetPosition(absoluteTransform.Position); + m_FrameCamera->SetOrientation(absoluteTransform.Orientation); + } - void Draw(std::shared_ptr renderer) override - { - if (!m_Camera) - return; + void Draw(std::shared_ptr renderer) override { + if (!m_FrameCamera) + return; - renderer->SetCamera(m_Camera); - renderer->SetViewport(AbsoluteRectangle()); - renderer->DrawWorld(m_Parent->RenderQueue); - } + renderer->SetCamera(m_FrameCamera); + renderer->SetViewport(AbsoluteRectangle()); + renderer->DrawWorld(m_Parent->RenderQueue); + } - private: - std::shared_ptr m_World; - std::shared_ptr m_TransformSystem; - std::shared_ptr m_Camera; - EntityID m_CameraEntity; - }; +private: + std::shared_ptr m_World; + std::shared_ptr m_TransformSystem; + std::shared_ptr m_FrameCamera; + EntityID m_CameraEntity; +}; } diff --git a/include/Game/EGameOver.h b/include/Game/EGameOver.h new file mode 100644 index 0000000..f2ff41f --- /dev/null +++ b/include/Game/EGameOver.h @@ -0,0 +1,21 @@ +// +// Created by Administrator on 2015-09-17. +// + +#ifndef DAYDREAM_EGAMEOVER_H +#define DAYDREAM_EGAMEOVER_H + +#include "Core/EventBroker.h" + +namespace dd +{ +namespace Events +{ +struct GameOver : Event +{ + +}; +} +} + +#endif //DAYDREAM_EGAMEOVER_H diff --git a/src/game/CMakeLists.txt b/src/game/CMakeLists.txt index 363006a..1e862fd 100755 --- a/src/game/CMakeLists.txt +++ b/src/game/CMakeLists.txt @@ -75,7 +75,7 @@ set(SOURCE_FILES ${SOURCE_FILES_Rendering} ${SOURCE_FILES_Transform} ${SOURCE_FILES_Physics} - ${SOURCE_FILES_Game} ../../include/GUI/Viewport.h) + ${SOURCE_FILES_Game} ../../include/Game/EGameOver.h) if(CMAKE_COMPILER_IS_GNUCXX) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread") diff --git a/src/game/Core/Renderer.cpp b/src/game/Core/Renderer.cpp index 4864bc6..e0efbe3 100755 --- a/src/game/Core/Renderer.cpp +++ b/src/game/Core/Renderer.cpp @@ -463,3 +463,104 @@ void dd::Renderer::DebugKeys() m_CurrentScreenBuffer = m_tLighting; } } + +//Niklas is trying to get frames to work. +void dd::Renderer::DrawFrame(RenderQueuePair &rq) +{ + glBindFramebuffer(GL_FRAMEBUFFER, 0); + glViewport(m_Viewport.X, m_Height - m_Viewport.Y - m_Viewport.Height, m_Viewport.Width, m_Viewport.Height); + glScissor(m_Scissor.X, m_Height - m_Scissor.Y - m_Scissor.Height, m_Scissor.Width, m_Scissor.Height); + + //glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + //glClearColor(0.0f, 0.5f, 0.0f, 1.0f); + + glEnable(GL_SCISSOR_TEST); + 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.Forward) + { + //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; + } + } +} +//Niklas is trying to get frames to work./end + diff --git a/src/game/Core/ResourceManager.cpp b/src/game/Core/ResourceManager.cpp index 2d25e1d..a77074d 100644 --- a/src/game/Core/ResourceManager.cpp +++ b/src/game/Core/ResourceManager.cpp @@ -82,4 +82,26 @@ void dd::ResourceManager::fileWatcherCallback(std::string path, FileWatcher::Fil void dd::ResourceManager::Update() { m_FileWatcher.Check(); -} \ No newline at end of file +} + +//Niklas is trying to get frames to work. +dd::Resource* dd::ResourceManager::CreateResource(std::string resourceType, std::string resourceName) +{ + auto facIt = m_FactoryFunctions.find(resourceType); + if (facIt == m_FactoryFunctions.end()) + { + LOG_ERROR("Failed to load resource \"%s\" of type \"%s\": Type not registered", resourceName.c_str(), resourceType.c_str()); + return nullptr; + } + + // Call the factory function + Resource* resource = facIt->second(resourceName); + // Store IDs + resource->TypeID = GetTypeID(resourceType); + resource->ResourceID = GetNewResourceID(resource->TypeID); + // Cache + m_ResourceCache[std::make_pair(resourceType, resourceName)] = resource; + + return resource; +} +//Niklas is trying to get frames to work./end