diff --git a/include/Core/Engine.h b/include/Core/Engine.h old mode 100755 new mode 100644 index 5c51b5f..527d9bb --- a/include/Core/Engine.h +++ b/include/Core/Engine.h @@ -190,7 +190,7 @@ public: auto ent = m_World->CreateEntity(); std::shared_ptr transform = m_World->AddComponent(ent); transform->Position = glm::vec3(0.f, -3.f, -10.f); - transform->Scale = glm::vec3(2.f, 0.5f, 1.f); + transform->Scale = glm::vec3(8.f, 0.5f, 1.f); transform->Orientation = glm::rotate(transform->Orientation, glm::radians(25.f), glm::vec3(0, 0, -1)); std::shared_ptr sprite = m_World->AddComponent(ent); diff --git a/include/Core/Engine.h.orig b/include/Core/Engine.h.orig deleted file mode 100755 index 819b19e..0000000 --- a/include/Core/Engine.h.orig +++ /dev/null @@ -1,417 +0,0 @@ -/* - 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 "Core/EventBroker.h" -#include "Rendering/CModel.h" -#include "Rendering/CSprite.h" -#include "CTemplate.h" -#include "Rendering/CPointLight.h" -#include "Transform/TransformSystem.h" -#include "Game/LevelSystem.h" -#include "Game/PadSystem.h" -#include "Game/CBall.h" -#include "Game/CBrick.h" -#include "Game/CPad.h" - -#include "Physics/PhysicsSystem.h" -#include "Physics/CPhysics.h" -#include "Physics/CBoxShape.h" -#include "Physics/ESetImpulse.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(); -// m_World->ComponentFactory.Register(); -// m_World->ComponentFactory.Register(); - - - m_World->ComponentFactory.Register(); - - m_World->ComponentFactory.Register(); - m_World->ComponentFactory.Register(); - m_World->ComponentFactory.Register(); - 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->SystemFactory.Register([this]() { return new Systems::LevelSystem(m_World.get(), m_EventBroker); }); - m_World->AddSystem(); - m_World->SystemFactory.Register([this]() { return new Systems::PadSystem(m_World.get(), m_EventBroker); }); - m_World->AddSystem(); - - m_World->ComponentFactory.Register(); - m_World->ComponentFactory.Register(); - m_World->ComponentFactory.Register(); - m_World->Initialize(); - - - //TODO: Remove tobias light-test code. - { - auto t_BrickWall = m_World->CreateEntity(); - auto transform = m_World->AddComponent(t_BrickWall); - transform->Position = glm::vec3(0.f, 0.f, -10.f); - auto sprite = m_World->AddComponent(t_BrickWall); - //TODO: Rename SpriteFile to DiffuseTexture or similar. - sprite->SpriteFile = "Textures/Test/Brick_Diffuse.png"; - sprite->NormalTexture = "Textures/Test/Brick_Normal.png"; - sprite->SpecularTexture = "Textures/Test/Brick_Specular.png"; - } - - - { - auto ent = m_World->CreateEntity(); - std::shared_ptr transform = m_World->AddComponent(ent); -<<<<<<< HEAD - transform->Position = glm::vec3(0.5f, 0.f, -10.f); - transform->Scale = glm::vec3(1.f, 1.f, 1.f); - - std::shared_ptr sprite = m_World->AddComponent(ent); - sprite->SpriteFile = "Textures/Ball.png"; -======= - transform->Position = glm::vec3(0.f, 0.f, -9.5f); - - //std::shared_ptr sprite = m_World->AddComponent(ent); - //sprite->SpriteFile = "Textures/Core/ErrorTexture.png"; ->>>>>>> GLrenderer - - std::shared_ptr circleShape = m_World->AddComponent(ent); - std::shared_ptr ball = m_World->AddComponent(ent); - - std::shared_ptr physics = m_World->AddComponent(ent); - physics->Static = false; - - auto plight = m_World->AddComponent(ent); - plight->Radius = 2.f; - - m_World->CommitEntity(ent); - - Events::SetImpulse e; - e.Entity = ent; - e.Impulse = glm::vec2(0.f, -7.f); - e.Point = glm::vec2(0.5f, 0.f); - m_EventBroker->Publish(e); - } - - { - auto topWall = m_World->CreateEntity(); - std::shared_ptr transform = m_World->AddComponent(topWall); - transform->Position = glm::vec3(0.f, 5.f, -10.f); - transform->Scale = glm::vec3(15.f, 0.5f, 1.f); - - std::shared_ptr sprite = m_World->AddComponent(topWall); - sprite->SpriteFile = "Textures/Core/ErrorTexture.png"; - - std::shared_ptr boxShape = m_World->AddComponent(topWall); - - std::shared_ptr physics = m_World->AddComponent(topWall); - physics->Static = true; - - m_World->CommitEntity(topWall); - } - { - 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 boxShape = m_World->AddComponent(leftWall); - - std::shared_ptr physics = m_World->AddComponent(leftWall); - physics->Static = true; - - m_World->CommitEntity(leftWall); - } - { - 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 boxShape = m_World->AddComponent(rightWall); - - std::shared_ptr physics = m_World->AddComponent(rightWall); - physics->Static = true; - - m_World->CommitEntity(rightWall); - } - - - /*{ - auto ent = m_World->CreateEntity(); - std::shared_ptr transform = m_World->AddComponent(ent); - transform->Position = glm::vec3(0.f, -3.f, -10.f); - transform->Scale = glm::vec3(2.f, 0.5f, 1.f); - transform->Orientation = glm::rotate(transform->Orientation, glm::radians(25.f), glm::vec3(0, 0, -1)); - - std::shared_ptr sprite = m_World->AddComponent(ent); - sprite->SpriteFile = "Textures/Core/ErrorTexture.png"; - - std::shared_ptr boxShape = m_World->AddComponent(ent); - - std::shared_ptr physics = m_World->AddComponent(ent); - physics->Static = true; - - m_World->CommitEntity(ent); - }*/ - - { - auto ent = m_World->CreateEntity(); - m_World->SetProperty(ent, "Name", "Pad"); - auto ctransform = m_World->AddComponent(ent); - ctransform->Position = glm::vec3(0.f, -5.f, -10.f); - ctransform->Scale = glm::vec3(3.2, 0.8, 0.); - auto rectangle = m_World->AddComponent(ent); - auto physics = m_World->AddComponent(ent); - auto csprite = m_World->AddComponent(ent); - auto pad = m_World->AddComponent(ent); - csprite->SpriteFile = "Textures/Pad.png"; - m_World->CommitEntity(ent); - } - - 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(); - - // 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(); - - // Render scene - //TODO send renderqueue to draw. -// m_Renderer->Draw(m_RendererQueue); - - 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(); - - // Render scene - //TODO send renderqueue to draw. - m_Renderer->Draw(m_RendererQueue); - - // Swap event queues - m_EventBroker->Clear(); - - glfwPollEvents(); - } - - std::shared_ptr m_TransformSystem; - std::shared_ptr m_LevelSystem; - - //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); - } - } - - //TODO: Add LightLoadShit - - auto pointLightComponent = m_World->GetComponent(entity); - if (pointLightComponent) - { - Components::Transform absoluteTransform = m_TransformSystem->AbsoluteTransform(entity); - EnqueuePointLight(absoluteTransform.Position, - pointLightComponent->Diffuse, - pointLightComponent->Specular, - pointLightComponent->Radius); - } - - - auto spriteComponent = m_World->GetComponent(entity); - if (spriteComponent) - { - - std::string normal = spriteComponent->NormalTexture; - std::string spec = spriteComponent->SpecularTexture; - - if (normal.empty()) { - normal = "Textures/Core/NeutralNormalMap.png"; - } - if (spec.empty()) { - spec = "Textures/Core/NeutralSpecularMap.png"; - } - auto texturediff = ResourceManager::Load(spriteComponent->SpriteFile); - auto texturenorm = ResourceManager::Load(normal); - auto texturespec = ResourceManager::Load(spec); - - - 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(texturediff, texturenorm, texturespec, 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, Texture* normalTexture, Texture* specularTexture, glm::mat4 modelMatrix, glm::vec4 color) - { - SpriteJob job; - job.TextureID = texture->ResourceID; - job.DiffuseTexture = *texture; - job.NormalTexture = *normalTexture; - job.SpecularTexture = *specularTexture; - job.ModelMatrix = modelMatrix; - job.Color = color; - - m_RendererQueue.Deferred.Add(job); - } - - void EnqueuePointLight(glm::vec3 position, glm::vec3 diffuseColor, glm::vec3 specularColor, float radius) - { - PointLightJob job; - job.Position = position; - job.DiffuseColor = diffuseColor; - job.SpecularColor = specularColor; - job.Radius = radius; - - m_RendererQueue.Lights.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; -}; - -} diff --git a/include/Physics/PhysicsSystem.h b/include/Physics/PhysicsSystem.h index 265e511..57bad34 100644 --- a/include/Physics/PhysicsSystem.h +++ b/include/Physics/PhysicsSystem.h @@ -55,6 +55,8 @@ private: b2Vec2 m_Gravity; b2World* m_PhysicsWorld; float m_TimeStep; + float m_Accumulator; + int m_VelocityIterations, m_PositionIterations; std::unordered_map m_EntitiesToBodies; diff --git a/src/game/Physics/PhysicsSystem.cpp b/src/game/Physics/PhysicsSystem.cpp index 5f8ec8d..3d244a7 100644 --- a/src/game/Physics/PhysicsSystem.cpp +++ b/src/game/Physics/PhysicsSystem.cpp @@ -16,9 +16,9 @@ void dd::Systems::PhysicsSystem::Initialize() m_PhysicsWorld = new b2World(m_Gravity); m_TimeStep = 1.f/60.f; - m_VelocityIterations = 8; - m_PositionIterations = 6; - + m_VelocityIterations = 6; + m_PositionIterations = 2; + m_Accumulator = 0.f; m_PhysicsWorld->SetContactListener(m_ContactListener); @@ -62,7 +62,23 @@ void dd::Systems::PhysicsSystem::Update(double dt) } } - m_PhysicsWorld->Step(m_TimeStep, m_VelocityIterations, m_PositionIterations); + + + + m_Accumulator += dt; + while(m_Accumulator >= m_TimeStep) + { + m_PhysicsWorld->Step(m_TimeStep, m_VelocityIterations, m_PositionIterations); + m_Accumulator -= dt; + } + + + + + + + + for (auto i : m_EntitiesToBodies) { EntityID entity = i.first;