/*
This file is part of Daydream Engine.
Copyright 2014 Adam Byléhn, Tobias Dahl, Simon Holmberg, Viktor Ljung
Daydream Engine is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Daydream Engine is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with Daydream Engine. If not, see .
*/
#include
#include
#include "ResourceManager.h"
#include "OBJ.h"
#include "Model.h"
#include "Texture.h"
#include "EventBroker.h"
#include "RenderQueue.h"
#include "Renderer.h"
#include "InputManager.h"
//TODO: Remove includes that are only here for the temporary draw solution.
#include "World.h"
#include "CTransform.h"
#include "Rendering/CModel.h"
#include "Rendering/CSprite.h"
#include "CTemplate.h"
#include "Transform/TransformSystem.h"
#include "Physics/PhysicsSystem.h"
#include "Physics/CPhysics.h"
#include "CBoxShape.h"
namespace dd
{
class Engine
{
public:
Engine(int argc, char* argv[])
{
m_EventBroker = std::make_shared();
m_Renderer = std::make_shared();
m_Renderer->SetFullscreen(false);
m_Renderer->SetResolution(Rectangle(0, 0, 1920, 1080));
m_Renderer->Initialize();
m_InputManager = std::make_shared(m_Renderer->Window(), m_EventBroker);
m_World = std::make_shared(m_EventBroker);
//TODO: Move this out of engine.h
m_World->ComponentFactory.Register();
m_World->SystemFactory.Register([this]() { return new Systems::TransformSystem(m_World.get(), m_EventBroker); });
m_World->AddSystem();
<<<<<<< HEAD
m_World->ComponentFactory.Register();
m_World->ComponentFactory.Register();
m_World->SystemFactory.Register([this]() { return new Systems::PhysicsSystem(m_World.get(), m_EventBroker); });
m_World->AddSystem();
m_World->ComponentFactory.Register();
m_World->ComponentFactory.Register();
m_World->Initialize();
auto ent = m_World->CreateEntity();
std::shared_ptr transform = m_World->AddComponent(ent);
transform->Position = glm::vec3(0.f, 0.f, -10.f);
std::shared_ptr model = m_World->AddComponent(ent);
model->ModelFile = "Models/Core/UnitSphere.obj";
std::shared_ptr boxShape = m_World->AddComponent(ent);
boxShape->x = 5.f;
boxShape->y = 5.f;
std::shared_ptr physics = m_World->AddComponent(ent);
physics->Static = true;
m_World->CommitEntity(ent);
=======
m_World->ComponentFactory.Register();
m_World->ComponentFactory.Register();
m_World->ComponentFactory.Register();
m_World->Initialize();
// {
// auto ent = m_World->CreateEntity();
// std::shared_ptr transform = m_World->AddComponent(ent);
// transform->Position = glm::vec3(0.f, 0.f, -10.f);
// auto sprite = m_World->AddComponent(ent);
// sprite->SpriteFile = "Textures/Core/ErrorTexture.png";
// }
>>>>>>> origin/master
m_LastTime = glfwGetTime();
}
bool Running() const { return !glfwWindowShouldClose(m_Renderer->Window()); }
void Tick()
{
double currentTime = glfwGetTime();
double dt = currentTime - m_LastTime;
m_LastTime = currentTime;
ResourceManager::Update();
<<<<<<< HEAD
// // Update input
m_InputManager->Update(dt);
m_World->Update(dt);
//
// if (glfwGetKey(m_Renderer->Window(), GLFW_KEY_R)) {
// ResourceManager::Reload("Shaders/Deferred/3/Fragment.glsl");
// }
//TODO Fill up the renderQueue with models (Temp fix)
// TEMPAddToRenderQueue();
=======
// Update input
m_InputManager->Update(dt);
m_World->Update(dt);
if (glfwGetKey(m_Renderer->Window(), GLFW_KEY_R)) {
ResourceManager::Reload("Shaders/Deferred/3/Fragment.glsl");
}
//TODO Fill up the renderQueue with models (Temp fix)
TEMPAddToRenderQueue();
>>>>>>> origin/master
// Render scene
//TODO send renderqueue to draw.
m_Renderer->Draw(m_RendererQueue);
// Swap event queues
m_EventBroker->Clear();
glfwPollEvents();
}
std::shared_ptr m_TransformSystem;
//TODO: Get this out of engine.h
void TEMPAddToRenderQueue()
{
if (!m_TransformSystem)
m_TransformSystem = m_World->GetSystem();
m_RendererQueue.Clear();
for (auto &pair : *m_World->GetEntities())
{
EntityID entity = pair.first;
auto templateComponent = m_World->GetComponent(entity);
if (templateComponent)
continue;
auto transform = m_World->GetComponent(entity);
if (!transform)
continue;
auto modelComponent = m_World->GetComponent(entity);
if (modelComponent)
{
Model* modelAsset = nullptr;
modelAsset = ResourceManager::Load(modelComponent->ModelFile);
if (modelAsset)
{
Components::Transform absoluteTransform = m_TransformSystem->AbsoluteTransform(entity);
glm::mat4 modelMatrix = glm::translate(glm::mat4(), absoluteTransform.Position)
* glm::toMat4(absoluteTransform.Orientation)
* glm::scale(absoluteTransform.Scale);
EnqueueModel(modelAsset, modelMatrix, modelComponent->Transparent, modelComponent->Color);
}
}
auto spriteComponent = m_World->GetComponent(entity);
if (spriteComponent)
{
auto textureAsset = ResourceManager::Load(spriteComponent->SpriteFile);
if (textureAsset)
{
Components::Transform absoluteTransform = m_TransformSystem->AbsoluteTransform(entity);
glm::quat orientation2D = glm::angleAxis(glm::eulerAngles(absoluteTransform.Orientation).z, glm::vec3(0, 0, -1));
glm::mat4 modelMatrix = glm::translate(absoluteTransform.Position)
* glm::toMat4(orientation2D)
* glm::scale(absoluteTransform.Scale);
EnqueueSprite(textureAsset, modelMatrix, spriteComponent->Color);
}
}
}
m_RendererQueue.Sort();
}
//TODO: Get this out of engine.h
void EnqueueModel(Model* model, glm::mat4 modelMatrix, float transparent, glm::vec4 color)
{
for (auto texGroup : model->TextureGroups)
{
ModelJob job;
job.TextureID = (texGroup.Texture) ? texGroup.Texture->ResourceID : 0;
job.DiffuseTexture = (texGroup.Texture) ? *texGroup.Texture : 0;
job.NormalTexture = (texGroup.NormalMap) ? *texGroup.NormalMap : 0;
job.SpecularTexture = (texGroup.SpecularMap) ? *texGroup.SpecularMap : 0;
job.VAO = model->VAO;
job.ElementBuffer = model->ElementBuffer;
job.StartIndex = texGroup.StartIndex;
job.EndIndex = texGroup.EndIndex;
job.ModelMatrix = modelMatrix;
job.Color = color;
m_RendererQueue.Deferred.Add(job);
}
}
// TODO: Get this out of engine.h
void EnqueueSprite(Texture* texture, glm::mat4 modelMatrix, glm::vec4 color)
{
SpriteJob job;
job.TextureID = texture->ResourceID;
job.Texture = *texture;
job.ModelMatrix = modelMatrix;
job.Color = color;
m_RendererQueue.Forward.Add(job);
}
private:
std::shared_ptr m_ResourceManager;
std::shared_ptr m_EventBroker;
std::shared_ptr m_Renderer;
RenderQueueCollection m_RendererQueue;
std::shared_ptr m_InputManager;
std::shared_ptr m_World;
double m_LastTime;
};
}