From ad19848c1b547d7f14382487d18d3459574b2568 Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Mon, 12 May 2014 22:33:59 +0200 Subject: [PATCH 01/21] WIP ECS GUI --- src/Events/CastRay.h | 16 +++++++++++ src/Events/MousePress.h | 1 + src/Events/MouseRelease.h | 1 + src/Events/RayIntersection.h | 17 ++++++++++++ src/GUI/Frame.h | 27 +++++++++++++++++++ src/InputManager.cpp | 6 +++++ src/Systems/RaySystem.cpp | 25 ++++++++++++++++++ src/Systems/RaySystem.h | 35 ++++++++++++++++++++++++ vs11/Returngeance/EntityWrapper.h | 44 +++++++++++++++++++++++++++++++ 9 files changed, 172 insertions(+) create mode 100644 src/Events/CastRay.h create mode 100644 src/Events/RayIntersection.h create mode 100644 src/Systems/RaySystem.cpp create mode 100644 src/Systems/RaySystem.h create mode 100644 vs11/Returngeance/EntityWrapper.h diff --git a/src/Events/CastRay.h b/src/Events/CastRay.h new file mode 100644 index 0000000..adaba7f --- /dev/null +++ b/src/Events/CastRay.h @@ -0,0 +1,16 @@ +#ifndef Events_CastRay_h__ +#define Events_CastRay_h__ + +#include "EventBroker.h" + +namespace Events +{ + +struct CastRay : Event +{ + glm::vec3 Direction; +}; + +} + +#endif // Events_CastRay_h__ \ No newline at end of file diff --git a/src/Events/MousePress.h b/src/Events/MousePress.h index 3116edb..75b31d7 100644 --- a/src/Events/MousePress.h +++ b/src/Events/MousePress.h @@ -9,6 +9,7 @@ namespace Events struct MousePress : Event { int Button; + double X, Y; }; } diff --git a/src/Events/MouseRelease.h b/src/Events/MouseRelease.h index 7249dcd..588b06b 100644 --- a/src/Events/MouseRelease.h +++ b/src/Events/MouseRelease.h @@ -9,6 +9,7 @@ namespace Events struct MouseRelease : Event { int Button; + double X, Y; }; } diff --git a/src/Events/RayIntersection.h b/src/Events/RayIntersection.h new file mode 100644 index 0000000..9b9d74f --- /dev/null +++ b/src/Events/RayIntersection.h @@ -0,0 +1,17 @@ +#ifndef Events_RayIntersection_h__ +#define Events_RayIntersection_h__ + +#include "EventBroker.h" +#include "Entity.h" + +namespace Events +{ + +struct RayIntersection : Event +{ + EntityID Entity; +}; + +} + +#endif // Events_RayIntersection_h__ \ No newline at end of file diff --git a/src/GUI/Frame.h b/src/GUI/Frame.h index 19f534a..76bf98b 100644 --- a/src/GUI/Frame.h +++ b/src/GUI/Frame.h @@ -6,6 +6,9 @@ #include "Util/Rectangle.h" #include "EventBroker.h" +// HACK: Decouple renderer plz +#include "Renderer.h" + namespace GUI { @@ -31,17 +34,41 @@ public: { SetParent(parent); Initialize(); } virtual void Initialize() { } + std::shared_ptr Parent() const { return m_Parent; } void SetParent(std::shared_ptr parent) { + parent->AddChild(std::shared_ptr(this)); m_Parent = parent; EventBroker = parent->EventBroker; } + + void AddChild(std::shared_ptr child) + { + m_Children.push_back(child); + if (m_Parent != nullptr) + { + m_Parent->AddChild(child); + } + } + + typedef std::list>::const_iterator FrameChildrenIterator; + FrameChildrenIterator begin() + { + return m_Children.begin(); + } + FrameChildrenIterator end() + { + return m_Children.end(); + } + virtual void Update(double dt) { } + virtual void Draw(Renderer* renderer) { } protected: std::shared_ptr<::EventBroker> EventBroker; std::shared_ptr m_Parent; + std::list> m_Children; }; } diff --git a/src/InputManager.cpp b/src/InputManager.cpp index 9f09b1c..c5509c0 100644 --- a/src/InputManager.cpp +++ b/src/InputManager.cpp @@ -36,17 +36,23 @@ void InputManager::Update(double dt) m_CurrentMouseState[i] = glfwGetMouseButton(m_GLFWWindow, i); if (m_CurrentMouseState[i] != m_LastMouseState[i]) { + double x, y; + glfwGetCursorPos(m_GLFWWindow, &x, &y); // Publish mouse button events if (m_CurrentMouseState[i]) { Events::MousePress e; e.Button = i; + e.X = x; + e.Y = y; m_EventBroker->Publish(e); } else { Events::MouseRelease e; e.Button = i; + e.X = x; + e.Y = y; m_EventBroker->Publish(e); } } diff --git a/src/Systems/RaySystem.cpp b/src/Systems/RaySystem.cpp new file mode 100644 index 0000000..8f09fe0 --- /dev/null +++ b/src/Systems/RaySystem.cpp @@ -0,0 +1,25 @@ +#include "PrecompiledHeader.h" +#include "RaySystem.h" +#include "World.h" + +void Systems::RaySystem::Initialize() +{ + // Subscribe to events + EVENT_SUBSCRIBE_MEMBER(m_ECastRay, &Systems::RaySystem::OnCastRay); +} + +void Systems::RaySystem::Update(double dt) +{ + +} + +void Systems::RaySystem::UpdateEntity(double dt, EntityID entity, EntityID parent) +{ + +} + +bool Systems::RaySystem::OnCastRay(const Events::CastRay &event) +{ + Ray r; + return true; +} \ No newline at end of file diff --git a/src/Systems/RaySystem.h b/src/Systems/RaySystem.h new file mode 100644 index 0000000..51010d2 --- /dev/null +++ b/src/Systems/RaySystem.h @@ -0,0 +1,35 @@ +#ifndef DebugSystem_h__ +#define DebugSystem_h__ + +#include "System.h" +#include "Events/CastRay.h" + +namespace Systems +{ + +class RaySystem : public System +{ +public: + RaySystem(World* world, std::shared_ptr<::EventBroker> eventBroker) + : System(world, eventBroker) { } + + void Initialize() override; + + void Update(double dt) override; + void UpdateEntity(double dt, EntityID entity, EntityID parent) override; + +private: + struct Ray + { + glm::vec3 Direction; + }; + + EventRelay m_ECastRay; + bool OnCastRay(const Events::CastRay &event); + + std::list m_UnresolvedRays; + //void UpdateEntity(double dt, EntityID entity, EntityID parent) override; +}; + +} +#endif // DebugSystem_h__ \ No newline at end of file diff --git a/vs11/Returngeance/EntityWrapper.h b/vs11/Returngeance/EntityWrapper.h new file mode 100644 index 0000000..37e7ffb --- /dev/null +++ b/vs11/Returngeance/EntityWrapper.h @@ -0,0 +1,44 @@ +#include + +#include "Entity.h" +#include "World.h" +#include "EventBroker.h" + +// A slower wrapper class for EntityID to make +// creation of pre-defined entity hierarchies easier +class EntityGroup +{ +public: + EntityGroup(World* world, EntityID parent = 0) + : World(world) + , EventBroker(world->EventBroker()) + { + m_ID = World->CreateEntity(parent); + Initialize(); + World->CommitEntity(m_ID); + } + + ~EntityGroup() + { + World->RemoveEntity(m_ID); + } + + virtual void Initialize() { } + + template + std::shared_ptr AddComponent(std::string componentType) + { + return World->AddComponent(m_ID, componentType); + } + std::shared_ptr AddComponent(std::string componentType) + { + return World->AddComponent(m_ID, componentType); + } + + operator EntityID () const { return m_ID; } + +private: + EntityID m_ID; + ::World* World; + std::shared_ptr<::EventBroker> EventBroker; +}; \ No newline at end of file From 05757b6112ecfc98818ce8c523c4719084927a26 Mon Sep 17 00:00:00 2001 From: ViktorLjung Date: Thu, 15 May 2014 19:30:44 +0200 Subject: [PATCH 02/21] assets --- assets | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/assets b/assets index 15ac025..9a3fcc2 160000 --- a/assets +++ b/assets @@ -1 +1 @@ -Subproject commit 15ac02523ad374b4aaf60a16db89b1934de7f303 +Subproject commit 9a3fcc241854a9fa5665ec4885c70adb8f29faa0 From 9723d80742279b699d2d092ff3b2c8e9838cad4f Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Thu, 15 May 2014 21:44:33 +0200 Subject: [PATCH 03/21] And then Microsoft happened... --- src/GUI/Frame.h | 150 +++++++++++++++++----------------- src/InputManager.cpp | 5 +- src/InputManager.h | 17 ++-- src/Util/Rectangle.h | 190 +++++++++++++++++++++---------------------- 4 files changed, 179 insertions(+), 183 deletions(-) diff --git a/src/GUI/Frame.h b/src/GUI/Frame.h index d587bbe..628a650 100644 --- a/src/GUI/Frame.h +++ b/src/GUI/Frame.h @@ -1,75 +1,75 @@ -//#ifndef GUI_Frame_h__ -//#define GUI_Frame_h__ -// -//#include -// -//#include "Util/Rectangle.h" -//#include "EventBroker.h" -// -//// HACK: Decouple renderer plz -//#include "Renderer.h" -// -//namespace GUI -//{ -// -//class Frame : public Rectangle -//{ -//public: -// enum class Anchor -// { -// Left, -// Right, -// Top, -// Bottom -// }; -// -// // Set up a base frame with an event broker -// Frame(std::shared_ptr<::EventBroker> eventBroker) -// : EventBroker(eventBroker) -// , Rectangle() -// { Initialize(); } -// // Create a frame as a child -// Frame(std::shared_ptr parent) -// : Rectangle(static_cast(*parent)) // Clone parent rectangle using copy constructor -// { SetParent(parent); Initialize(); } -// -// virtual void Initialize() { } -// std::shared_ptr Parent() const { return m_Parent; } -// void SetParent(std::shared_ptr parent) -// { -// parent->AddChild(std::shared_ptr(this)); -// m_Parent = parent; -// EventBroker = parent->EventBroker; -// } -// -// void AddChild(std::shared_ptr child) -// { -// m_Children.push_back(child); -// if (m_Parent != nullptr) -// { -// m_Parent->AddChild(child); -// } -// } -// -// typedef std::list>::const_iterator FrameChildrenIterator; -// FrameChildrenIterator begin() -// { -// return m_Children.begin(); -// } -// FrameChildrenIterator end() -// { -// return m_Children.end(); -// } -// -// virtual void Update(double dt) { } -// virtual void Draw(Renderer* renderer) { } -// -//protected: -// std::shared_ptr<::EventBroker> EventBroker; -// std::shared_ptr m_Parent; -// std::list> m_Children; -//}; -// -//} -// -//#endif // GUI_Frame_h__ +#ifndef GUI_Frame_h__ +#define GUI_Frame_h__ + +#include + +#include "Util/Rectangle.h" +#include "EventBroker.h" + +// HACK: Decouple renderer plz +#include "Renderer.h" + +namespace GUI +{ + +class Frame : public Rectangle +{ +public: + enum class Anchor + { + Left, + Right, + Top, + Bottom + }; + + // Set up a base frame with an event broker + Frame(std::shared_ptr<::EventBroker> eventBroker) + : EventBroker(eventBroker) + , Rectangle() + { Initialize(); } + // Create a frame as a child + Frame(std::shared_ptr parent) + : Rectangle(static_cast(*parent)) // Clone parent rectangle using copy constructor + { SetParent(parent); Initialize(); } + + virtual void Initialize() { } + std::shared_ptr Parent() const { return m_Parent; } + void SetParent(std::shared_ptr parent) + { + parent->AddChild(std::shared_ptr(this)); + m_Parent = parent; + EventBroker = parent->EventBroker; + } + + void AddChild(std::shared_ptr child) + { + m_Children.push_back(child); + if (m_Parent != nullptr) + { + m_Parent->AddChild(child); + } + } + + typedef std::list>::const_iterator FrameChildrenIterator; + FrameChildrenIterator begin() + { + return m_Children.begin(); + } + FrameChildrenIterator end() + { + return m_Children.end(); + } + + virtual void Update(double dt) { } + virtual void Draw(Renderer* renderer) { } + +protected: + std::shared_ptr<::EventBroker> EventBroker; + std::shared_ptr m_Parent; + std::list> m_Children; +}; + +} + +#endif // GUI_Frame_h__ diff --git a/src/InputManager.cpp b/src/InputManager.cpp index ea22d96..00ae7be 100644 --- a/src/InputManager.cpp +++ b/src/InputManager.cpp @@ -1,5 +1,6 @@ #include "PrecompiledHeader.h" #include "InputManager.h" +#include void InputManager::Initialize() { @@ -97,9 +98,9 @@ void InputManager::Update(double dt) // } // Xbox360 controller - using namespace Windows; + //using namespace ; DWORD dwResult; - for (int i = 0; i < XUSER_MAX_COUNT; i++) + for (int i = 0; i < MAX_GAMEPADS; i++) { XINPUT_STATE state = { 0 }; // Simply get the state of the controller from XInput. diff --git a/src/InputManager.h b/src/InputManager.h index 189430f..104d2b4 100644 --- a/src/InputManager.h +++ b/src/InputManager.h @@ -3,13 +3,6 @@ #include -namespace Windows -{ -#include -#undef min -#undef max -} - #include "EventBroker.h" #include "Events/KeyDown.h" #include "Events/KeyUp.h" @@ -36,6 +29,8 @@ public: void Initialize(); + static const short MAX_GAMEPADS = 4; + void Update(double dt); private: @@ -47,11 +42,11 @@ private: std::array m_CurrentMouseState; std::array m_LastMouseState; typedef std::array(Gamepad::Axis::LAST) + 1> GamepadAxisState; - std::array m_CurrentGamepadAxisState; - std::array m_LastGamepadAxisState; + std::array m_CurrentGamepadAxisState; + std::array m_LastGamepadAxisState; typedef std::array(Gamepad::Button::LAST) + 1> GamepadButtonState; - std::array m_CurrentGamepadButtonState; - std::array m_LastGamepadButtonState; + std::array m_CurrentGamepadButtonState; + std::array m_LastGamepadButtonState; double m_CurrentMouseX, m_CurrentMouseY; double m_LastMouseX, m_LastMouseY; diff --git a/src/Util/Rectangle.h b/src/Util/Rectangle.h index e55e268..0d0db32 100644 --- a/src/Util/Rectangle.h +++ b/src/Util/Rectangle.h @@ -1,95 +1,95 @@ -//#ifndef Util_Rectangle_h__ -//#define Util_Rectangle_h__ -// -//#include -// -//struct Rectangle -//{ -// Rectangle() -// : X(0), Y(0), Width(0), Height(0) { } -// -// Rectangle(int x, int y, int width = 0, int height = 0) -// : X(x), Y(y), Width(width), Height(height) { } -// -// /*Rectangle(const Rectangle &rect) -// : X(rect.X), Y(rect.Y), Width(rect.Width), Height(rect.Height) { }*/ -// -// int X; -// int Y; -// int Width; -// int Height; -// -// const int& GetLeft() const { return X; } -// void SetLeft(int left) -// { -// Width += X - left; -// X = left; -// } -// int GetRight() const { return X + Width; } -// void SetRight(int right) -// { -// Width = right - X; -// } -// const int& GetTop() const { return Y; } -// void SetTop(int top) -// { -// Height += Y - top; -// Y = top; -// } -// int GetBottom() const { return Y + Height; } -// int SetBottom(int bottom) -// { -// Height = bottom - Y; -// } -// -// Rectangle& operator+=(const Rectangle &rhs) -// { -// SetLeft(std::min(GetLeft(), rhs.GetLeft())); -// SetRight(std::max(GetRight(), rhs.GetRight())); -// SetTop(std::min(GetTop(), rhs.GetTop())); -// SetBottom(std::max(GetBottom(), rhs.GetBottom())); -// } -// -// static bool Intersects(const Rectangle &r1, const Rectangle &r2) -// { -// return !(r2.GetLeft() > r1.GetRight() || r2.GetRight() < r1.GetLeft() || r2.GetTop() > r1.GetBottom() || r2.GetBottom() < r1.GetTop()); -// } -//}; -// -//inline bool operator==(const Rectangle &r1, const Rectangle &r2) -//{ -// return (r1.X == r2.X) && (r1.Y == r2.Y) && (r1.Width == r2.Width) && (r1.Height == r2.Height); -//} -// -//inline bool operator!=(const Rectangle &lhs, const Rectangle &rhs) -//{ -// return !(lhs == rhs); -//} -// -//inline bool operator<(const Rectangle &lhs, const Rectangle &rhs) -//{ -// return (lhs.Width < rhs.Width) && (lhs.Height < rhs.Height); -//} -// -//inline bool operator>(const Rectangle &lhs, const Rectangle &rhs) -//{ -// return rhs < lhs; -//} -// -//inline bool operator<=(const Rectangle &lhs, const Rectangle &rhs) -//{ -// return !(lhs > rhs); -//} -// -//inline bool operator>=(const Rectangle &lhs, const Rectangle &rhs) -//{ -// return !(lhs < rhs); -//} -// -//inline Rectangle operator+(Rectangle lhs, const Rectangle &rhs) -//{ -// lhs += rhs; -// return lhs; -//} -// -//#endif // Util_Rectangle_h__ +#ifndef Util_Rectangle_h__ +#define Util_Rectangle_h__ + +#include + +struct Rectangle +{ + Rectangle() + : X(0), Y(0), Width(0), Height(0) { } + + Rectangle(int x, int y, int width = 0, int height = 0) + : X(x), Y(y), Width(width), Height(height) { } + + /*Rectangle(const Rectangle &rect) + : X(rect.X), Y(rect.Y), Width(rect.Width), Height(rect.Height) { }*/ + + int X; + int Y; + int Width; + int Height; + + const int& GetLeft() const { return X; } + void SetLeft(int left) + { + Width += X - left; + X = left; + } + int GetRight() const { return X + Width; } + void SetRight(int right) + { + Width = right - X; + } + const int& GetTop() const { return Y; } + void SetTop(int top) + { + Height += Y - top; + Y = top; + } + int GetBottom() const { return Y + Height; } + int SetBottom(int bottom) + { + Height = bottom - Y; + } + + Rectangle& operator+=(const Rectangle &rhs) + { + SetLeft(std::min(GetLeft(), rhs.GetLeft())); + SetRight(std::max(GetRight(), rhs.GetRight())); + SetTop(std::min(GetTop(), rhs.GetTop())); + SetBottom(std::max(GetBottom(), rhs.GetBottom())); + } + + static bool Intersects(const Rectangle &r1, const Rectangle &r2) + { + return !(r2.GetLeft() > r1.GetRight() || r2.GetRight() < r1.GetLeft() || r2.GetTop() > r1.GetBottom() || r2.GetBottom() < r1.GetTop()); + } +}; + +inline bool operator==(const Rectangle &r1, const Rectangle &r2) +{ + return (r1.X == r2.X) && (r1.Y == r2.Y) && (r1.Width == r2.Width) && (r1.Height == r2.Height); +} + +inline bool operator!=(const Rectangle &lhs, const Rectangle &rhs) +{ + return !(lhs == rhs); +} + +inline bool operator<(const Rectangle &lhs, const Rectangle &rhs) +{ + return (lhs.Width < rhs.Width) && (lhs.Height < rhs.Height); +} + +inline bool operator>(const Rectangle &lhs, const Rectangle &rhs) +{ + return rhs < lhs; +} + +inline bool operator<=(const Rectangle &lhs, const Rectangle &rhs) +{ + return !(lhs > rhs); +} + +inline bool operator>=(const Rectangle &lhs, const Rectangle &rhs) +{ + return !(lhs < rhs); +} + +inline Rectangle operator+(Rectangle lhs, const Rectangle &rhs) +{ + lhs += rhs; + return lhs; +} + +#endif // Util_Rectangle_h__ From 44e376e8156790597f1999c36909901a8c54d678 Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Fri, 16 May 2014 02:13:28 +0200 Subject: [PATCH 04/21] Pls go away --- src/Util/defferedUtil.h | 28 ------------------- vs11/Returngeance/Returngeance.vcxproj | 1 - .../Returngeance/Returngeance.vcxproj.filters | 1 - 3 files changed, 30 deletions(-) delete mode 100644 src/Util/defferedUtil.h diff --git a/src/Util/defferedUtil.h b/src/Util/defferedUtil.h deleted file mode 100644 index d362fac..0000000 --- a/src/Util/defferedUtil.h +++ /dev/null @@ -1,28 +0,0 @@ -#ifndef UTIL_H -#define UTIL_H - -#include -#include -#include - -#define ZERO_MEM(a) memset(a, 0, sizeof(a)) - -#define ARRAY_SIZE_IN_ELEMENTS(a) (sizeof(a)/sizeof(a[0])) - -#define INVALID_OGL_VALUE 0xFFFFFFFF - -#define SAFE_DELETE(p) if (p) { delete p; p = NULL; } - -#define GLExitIfError() \ -{ \ - GLenum Error = glGetError(); \ - \ - if (Error != GL_NO_ERROR) { \ - printf("OpenGL error in %s:%d: 0x%x\n", __FILE__, __LINE__, Error); \ - exit(0); \ - } \ -} - -#define GLCheckError() (glGetError() == GL_NO_ERROR) - -#endif /* UTIL_H */ diff --git a/vs11/Returngeance/Returngeance.vcxproj b/vs11/Returngeance/Returngeance.vcxproj index 5226852..da9dfb3 100755 --- a/vs11/Returngeance/Returngeance.vcxproj +++ b/vs11/Returngeance/Returngeance.vcxproj @@ -195,7 +195,6 @@ - diff --git a/vs11/Returngeance/Returngeance.vcxproj.filters b/vs11/Returngeance/Returngeance.vcxproj.filters index f0252d5..2a89626 100755 --- a/vs11/Returngeance/Returngeance.vcxproj.filters +++ b/vs11/Returngeance/Returngeance.vcxproj.filters @@ -352,7 +352,6 @@ Input\Events - From 16d51c64c5a3bf68619e64e783acbf1fff525b82 Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Sat, 17 May 2014 00:56:38 +0200 Subject: [PATCH 05/21] Meh-ish helicopter physics --- src/Components/HelicopterSteering.h | 16 + src/Events/ApplyForce.h | 18 + src/GameWorld.cpp | 691 ++---------------- src/GameWorld.h | 1 + src/Systems/FreeSteeringSystem.cpp | 14 +- src/Systems/HelicopterSteeringSystem.cpp | 67 ++ src/Systems/HelicopterSteeringSystem.h | 54 ++ src/Systems/PhysicsSystem.cpp | 18 +- src/Systems/PhysicsSystem.h | 4 +- vs11/Returngeance/Returngeance.vcxproj | 4 + .../Returngeance/Returngeance.vcxproj.filters | 25 + 11 files changed, 259 insertions(+), 653 deletions(-) create mode 100644 src/Components/HelicopterSteering.h create mode 100644 src/Events/ApplyForce.h create mode 100644 src/Systems/HelicopterSteeringSystem.cpp create mode 100644 src/Systems/HelicopterSteeringSystem.h diff --git a/src/Components/HelicopterSteering.h b/src/Components/HelicopterSteering.h new file mode 100644 index 0000000..630ec67 --- /dev/null +++ b/src/Components/HelicopterSteering.h @@ -0,0 +1,16 @@ +#ifndef HelicopterSteering_h__ +#define HelicopterSteering_h__ + +#include "Component.h" + +namespace Components +{ + +struct HelicopterSteering : Component +{ + HelicopterSteering* Clone() const override { return new HelicopterSteering(*this); } +}; + +} + +#endif // HelicopterSteering_h__ \ No newline at end of file diff --git a/src/Events/ApplyForce.h b/src/Events/ApplyForce.h new file mode 100644 index 0000000..2008764 --- /dev/null +++ b/src/Events/ApplyForce.h @@ -0,0 +1,18 @@ +#ifndef Events_ApplyForce_h__ +#define Events_ApplyForce_h__ +#include "Entity.h" +#include "EventBroker.h" + +namespace Events +{ + +struct ApplyForce : Event +{ + EntityID Entity; + double DeltaTime; + glm::vec3 Force; +}; + +} + +#endif // Events_ApplyForce_h__ \ No newline at end of file diff --git a/src/GameWorld.cpp b/src/GameWorld.cpp index f0c5999..924922e 100755 --- a/src/GameWorld.cpp +++ b/src/GameWorld.cpp @@ -8,14 +8,25 @@ void GameWorld::Initialize() m_ResourceManager.Preload("Model", "Models/Placeholders/PhysicsTest/Plane.obj"); m_ResourceManager.Preload("Model", "Models/Placeholders/PhysicsTest/ArrowCube.obj"); - BindKey(GLFW_KEY_W, "vertical", 1.f); - BindKey(GLFW_KEY_S, "vertical", -1.f); - BindKey(GLFW_KEY_A, "horizontal", -1.f); - BindKey(GLFW_KEY_D, "horizontal", 1.f); + BindKey(GLFW_KEY_W, "cam_vertical", 1.f); + BindKey(GLFW_KEY_S, "cam_vertical", -1.f); + BindKey(GLFW_KEY_A, "cam_horizontal", -1.f); + BindKey(GLFW_KEY_D, "cam_horizontal", 1.f); BindGamepadAxis(Gamepad::Axis::LeftX, "horizontal", 1.f); BindGamepadAxis(Gamepad::Axis::LeftY, "vertical", 1.f); + BindGamepadAxis(Gamepad::Axis::RightX, "horizontal", 1.f); + BindGamepadAxis(Gamepad::Axis::RightY, "vertical", 1.f); + BindGamepadAxis(Gamepad::Axis::RightTrigger, "normal", 1.f); + BindGamepadAxis(Gamepad::Axis::LeftTrigger, "normal", -1.f); + BindKey(GLFW_KEY_SPACE, "cam_normal", 1.f); + BindKey(GLFW_KEY_LEFT_CONTROL, "cam_normal", -1.f); - BindKey(GLFW_KEY_UP, "barrel_rotation", 1.f); + BindKey(GLFW_KEY_LEFT_SHIFT, "cam_speed", 1.f); + BindKey(GLFW_KEY_LEFT_ALT, "cam_speed", -1.f); + + BindMouseButton(GLFW_MOUSE_BUTTON_1, "cam_attack", 1.f); + + /*BindKey(GLFW_KEY_UP, "barrel_rotation", 1.f); BindKey(GLFW_KEY_DOWN, "barrel_rotation", -1.f); BindKey(GLFW_KEY_LEFT, "tower_rotation", -1.f); BindKey(GLFW_KEY_RIGHT, "tower_rotation", 1.f); @@ -25,8 +36,7 @@ void GameWorld::Initialize() BindKey(GLFW_KEY_SPACE, "handbrake", 1.f); BindGamepadButton(Gamepad::Button::A, "handbrake", 1.f); - BindKey(GLFW_KEY_Z, "shoot", 1.f); - BindGamepadAxis(Gamepad::Axis::RightTrigger, "shoot", 1.f); + BindKey(GLFW_KEY_Z, "shoot", 1.f);*/ //BindGamepadButton(Gamepad::Button::Up, "Gamepad::Button::Up", 1.f); //BindGamepadButton(Gamepad::Button::Down, "Gamepad::Button::Down", 1.f); @@ -45,17 +55,7 @@ void GameWorld::Initialize() RegisterComponents(); - //{ - // auto camera = CreateEntity(); - // auto transform = AddComponent(camera, "Transform"); - // transform->Position.z = 20.f; - // transform->Position.y = 20.f; - // //transform->Orientation = glm::quat(glm::vec3(glm::pi() / 8.f, 0.f, 0.f)); - // auto cameraComp = AddComponent(camera, "Camera"); - // cameraComp->FarClip = 2000.f; - // auto freeSteering = AddComponent(camera, "FreeSteering"); - // CommitEntity(camera); - //} + { @@ -78,646 +78,55 @@ void GameWorld::Initialize() auto meshShape = AddComponent(groundshape, "MeshShape"); meshShape->ResourceName = "Models/Placeholders/Terrain/Terrain2.obj"; //meshShape->ResourceName = "Models/TestScene/testScene.obj"; - CommitEntity(groundshape); CommitEntity(ground); } - - - /*{ - auto jeep = CreateEntity(); - auto transform = AddComponent(jeep, "Transform"); - transform->Position = glm::vec3(0, 5, 0); - transform->Orientation = glm::angleAxis(glm::pi()/2, glm::vec3(0, 1, 0)); - auto physics = AddComponent(jeep, "Physics"); - physics->Mass = 1800; - physics->Static = false; - auto vehicle = AddComponent(jeep, "Vehicle"); - AddComponent(jeep, "Input"); - - { - auto shape = CreateEntity(jeep); - auto transform = AddComponent(shape, "Transform"); - auto meshShape = AddComponent(shape, "MeshShape"); - meshShape->ResourceName = "Models/Jeep/Chassi/ChassiCollision.obj"; - CommitEntity(shape); - - // auto box = AddComponent(jeep, "Box"); - // box->Width = 1.487f; - // box->Height = 0.727f; - // box->Depth = 2.594f; - - } - - { - auto chassis = CreateEntity(jeep); - auto transform = AddComponent(chassis, "Transform"); - transform->Position = glm::vec3(0, 0, 0); // 0.6577f - auto model = AddComponent(chassis, "Model"); - model->ModelFile = "Models/Jeep/Chassi/chassi.obj"; - } - - { - auto lightentity = CreateEntity(jeep); - auto transform = AddComponent(lightentity, "Transform"); - transform->Position = glm::vec3(0, 15, 0); - auto light = AddComponent(lightentity, "PointLight"); - light->Diffuse = glm::vec3(128.f/255.f, 172.f/255.f, 242.f/255.f); - light->Specular = glm::vec3(1.f); - light->constantAttenuation = 0.3f; - light->linearAttenuation = 0.003f; - light->quadraticAttenuation = 0.002f; - } - - - //Create wheels - float wheelOffset = 0.4f; - float springLength = 0.3f; - float suspensionStrength = 35.f; - { - auto wheel = CreateEntity(jeep); - auto transform = AddComponent(wheel, "Transform"); - transform->Position = glm::vec3(1.9f, 0.5546f - wheelOffset, -0.9242f); - transform->Scale = glm::vec3(1.0f); - auto model = AddComponent(wheel, "Model"); - model->ModelFile = "Models/Jeep/WheelFront/wheelFront.obj"; - auto Wheel = AddComponent(wheel, "Wheel"); - Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); - Wheel->AxleID = 0; - Wheel->Mass = 50; - Wheel->Radius = 0.837f; - Wheel->Steering = true; - Wheel->SuspensionStrength = suspensionStrength; - Wheel->Friction = 4.f; - Wheel->ConnectedToHandbrake = true; - CommitEntity(wheel); - } - - { - auto wheel = CreateEntity(jeep); - auto transform = AddComponent(wheel, "Transform"); - transform->Position = glm::vec3(-1.9f, 0.5546f - wheelOffset, -0.9242f); - transform->Scale = glm::vec3(1.0f); - transform->Orientation = glm::angleAxis(glm::pi(), glm::vec3(0, 0, 1)); - auto model = AddComponent(wheel, "Model"); - model->ModelFile = "Models/Jeep/WheelFront/wheelFront.obj"; - auto Wheel = AddComponent(wheel, "Wheel"); - Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); - Wheel->AxleID = 0; - Wheel->Mass = 50; - Wheel->Radius = 0.837f; - Wheel->Steering = true; - Wheel->SuspensionStrength = suspensionStrength; - Wheel->Friction = 4.f; - Wheel->ConnectedToHandbrake = true; - CommitEntity(wheel); - } - - { - auto wheel = CreateEntity(jeep); - auto transform = AddComponent(wheel, "Transform"); - transform->Position = glm::vec3(0.2726f, 0.2805f - wheelOffset, 1.9307f); - auto model = AddComponent(wheel, "Model"); - model->ModelFile = "Models/Jeep/WheelBack/wheelBack.obj"; - auto Wheel = AddComponent(wheel, "Wheel"); - Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); - Wheel->AxleID = 1; - Wheel->Mass = 50; - Wheel->Radius = 0.737f; - Wheel->Steering = false; - Wheel->SuspensionStrength = suspensionStrength; - Wheel->Friction = 4.f; - Wheel->ConnectedToHandbrake = true; - CommitEntity(wheel); - } - - { - auto wheel = CreateEntity(jeep); - auto transform = AddComponent(wheel, "Transform"); - transform->Position = glm::vec3(-0.2726f, 0.2805f - wheelOffset, 1.9307f); - transform->Orientation = glm::angleAxis(glm::pi(), glm::vec3(0, 0, 1)); - auto model = AddComponent(wheel, "Model"); - model->ModelFile = "Models/Jeep/WheelBack/wheelBack.obj"; - auto Wheel = AddComponent(wheel, "Wheel"); - Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); - Wheel->AxleID = 1; - Wheel->Mass = 50; - Wheel->Radius = 0.737f; - Wheel->Steering = false; - Wheel->SuspensionStrength = suspensionStrength; - Wheel->Friction = 4.f; - Wheel->ConnectedToHandbrake = true; - CommitEntity(wheel); - } - - CommitEntity(jeep); - }*/ - - + auto heli = CreateEntity(); { - auto tank = CreateEntity(); - auto transform = AddComponent(tank, "Transform"); - transform->Position = glm::vec3(0, 5, 0); - //transform->Orientation = glm::angleAxis(0.f, glm::vec3(0, 1, 0)); - auto physics = AddComponent(tank, "Physics"); - physics->Mass = 45000; - physics->Static = false; - auto vehicle = AddComponent(tank, "Vehicle"); - vehicle->MaxTorque = 5200.f; - AddComponent(tank, "TankSteering"); - AddComponent(tank, "Input"); - { - auto shape = CreateEntity(tank); - auto transform = AddComponent(shape, "Transform"); - auto meshShape = AddComponent(shape, "MeshShape"); - meshShape->ResourceName = "Models/Tank/Fix/ChassiCollision.obj"; - CommitEntity(shape); - - // auto box = AddComponent(jeep, "Box"); - // box->Width = 1.487f; - // box->Height = 0.727f; - // box->Depth = 2.594f; - + auto transform = AddComponent(heli, "Transform"); + transform->Position = glm::vec3(0.f, 3.f, 10.f); + auto physics = AddComponent(heli, "Physics"); + physics->Mass = 3000; + auto heliComponent = AddComponent(heli, "HelicopterSteering"); } + auto model = CreateEntity(heli); { - auto chassis = CreateEntity(tank); - auto transform = AddComponent(chassis, "Transform"); - transform->Position = glm::vec3(0, 0, 0); - auto model = AddComponent(chassis, "Model"); - model->ModelFile = "Models/Tank/Fix/Chassi.obj"; + auto transform = AddComponent(model, "Transform"); + transform->Orientation = glm::quat(glm::vec3(0, -glm::pi()/2.f, 0.f)); + auto modelComponent = AddComponent(model, "Model"); + modelComponent->ModelFile = "Models/Heli/Chassi/Chassi.obj"; } + CommitEntity(model); + + auto shape = CreateEntity(heli); { - auto tower = CreateEntity(tank); - SetProperty(tower, "Name", "tower"); - auto transform = AddComponent(tower, "Transform"); - transform->Position = glm::vec3(0.f, 1.2f, 1.8f); - auto model = AddComponent(tower, "Model"); - model->ModelFile = "Models/Tank/Fix/Top.obj"; - auto towerSteering = AddComponent(tower, "TowerSteering"); - towerSteering->Axis = glm::vec3(0.f, 1.f, 0.f); - towerSteering->TurnSpeed = glm::pi()/4.f; - { - auto barrel = CreateEntity(tower); - auto transform = AddComponent(barrel, "Transform"); - transform->Position = glm::vec3(-0.018f, -0.2, -1.3f); - auto model = AddComponent(barrel, "Model"); - model->ModelFile = "Models/Tank/Fix/Barrel.obj"; - auto barrelSteering = AddComponent(barrel, "BarrelSteering"); - barrelSteering->Axis = glm::vec3(1.f, 0.f, 0.f); - barrelSteering->TurnSpeed = glm::pi()/4.f; - barrelSteering->ShotSpeed = 70.f; - { - auto shot = CreateEntity(barrel); - auto transform = AddComponent(shot, "Transform"); - transform->Position = glm::vec3(0.35f, 0.f, -2.f); - transform->Orientation = glm::angleAxis(-glm::pi()/2.f, glm::vec3(1, 0, 0)); - transform->Scale = glm::vec3(3.f); - AddComponent(shot, "Template"); - auto physics = AddComponent(shot, "Physics"); - physics->Mass = 10.f; - physics->Static = false; - auto modelComponent = AddComponent(shot, "Model"); - modelComponent->ModelFile = "Models/Placeholders/rocket/Rocket.obj"; - - { - auto shape = CreateEntity(shot); - auto transform = AddComponent(shape, "Transform"); - auto boxShape = AddComponent(shape, "BoxShape"); - boxShape->Width = 0.5f; - boxShape->Height = 0.5f; - boxShape->Depth = 0.5f; - CommitEntity(shape); - } - CommitEntity(shot); - barrelSteering->ShotTemplate = shot; - } - CommitEntity(barrel); - } - - { - auto camera = CreateEntity(tower); - auto transform = AddComponent(camera, "Transform"); - transform->Position.z = 30.f; - transform->Position.y = 5.f; - //transform->Orientation = glm::quat(glm::vec3(-glm::pi() / 8.f, 0.f, 0.f)); - transform->Orientation = glm::angleAxis(glm::pi() / 100, glm::vec3(1, 0, 0)); - auto cameraComp = AddComponent(camera, "Camera"); - cameraComp->FarClip = 2000.f; - AddComponent(camera, "Input"); - //auto freeSteering = AddComponent(camera, "FreeSteering"); - CommitEntity(camera); - } + auto transform = AddComponent(shape, "Transform"); + auto box = AddComponent(shape, "BoxShape"); + box->Width = 5.f; + box->Height = 1.8f; + box->Depth = 7.f; } + CommitEntity(shape); - { - auto lightentity = CreateEntity(tank); - auto transform = AddComponent(lightentity, "Transform"); - transform->Position = glm::vec3(0, 0, 0); - auto light = AddComponent(lightentity, "PointLight"); - //light->Diffuse = glm::vec3(128.f/255.f, 172.f/255.f, 242.f/255.f); - //light->Specular = glm::vec3(1.f); - /*light->ConstantAttenuation = 0.3f; - light->LinearAttenuation = 0.003f; - light->QuadraticAttenuation = 0.002f;*/ - } - -// auto wheelpair = CreateEntity(tank); -// SetProperty(wheelpair, "Name", "WheelPair"); -// AddComponent(wheelpair, "WheelPairThingy"); - - //Create wheels - float wheelOffset = 0.4f; - float springLength = 0.3f; - float suspensionStrength = 25.f; - - { - auto wheel = CreateEntity(tank); - auto transform = AddComponent(wheel, "Transform"); - transform->Position = glm::vec3(1.68f, -0.83f - wheelOffset, -2.6f); - transform->Orientation = glm::angleAxis(glm::pi(), glm::vec3(0, 1, 0)); - auto model = AddComponent(wheel, "Model"); - model->ModelFile = "Models/Tank/Fix/WheelPhysics.obj"; - auto Wheel = AddComponent(wheel, "Wheel"); - Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); - Wheel->AxleID = 0; - Wheel->Mass = 2000; - Wheel->Radius = 0.6f; - Wheel->Steering = true; - Wheel->SuspensionStrength = suspensionStrength; - Wheel->Friction = 4.f; - Wheel->ConnectedToHandbrake = true; - Wheel->TorqueRatio = 0.125f; - Wheel->Width = 0.6f; - { - auto shape = CreateEntity(wheel); - auto shapetransform = AddComponent(shape, "Transform"); - shapetransform->Position = glm::vec3(0.f, 0.32f, 0.f) + transform->Position; - auto boxShape = AddComponent(shape, "BoxShape"); - boxShape->Width = 0.7f; - boxShape->Height = 0.34f; - boxShape->Depth = 0.7f; - CommitEntity(shape); - } - CommitEntity(wheel); - } - { - auto wheel = CreateEntity(tank); - auto transform = AddComponent(wheel, "Transform"); - transform->Position = glm::vec3(1.68f, -0.83f - wheelOffset, -0.83f); - transform->Orientation = glm::angleAxis(glm::pi(), glm::vec3(0, 1, 0)); - auto model = AddComponent(wheel, "Model"); - model->ModelFile = "Models/Tank/Fix/WheelPhysics.obj"; - auto Wheel = AddComponent(wheel, "Wheel"); - Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); - Wheel->AxleID = 0; - Wheel->Mass = 2000; - Wheel->Radius = 0.6f; - Wheel->Steering = false; - Wheel->SuspensionStrength = suspensionStrength; - Wheel->Friction = 4.f; - Wheel->ConnectedToHandbrake = true; - Wheel->TorqueRatio = 0.125f; - Wheel->Width = 0.6f; - { - auto shape = CreateEntity(wheel); - auto shapetransform = AddComponent(shape, "Transform"); - shapetransform->Position = glm::vec3(0.f, 0.32f, 0.f) + transform->Position; - auto boxShape = AddComponent(shape, "BoxShape"); - boxShape->Width = 0.7f; - boxShape->Height = 0.34f; - boxShape->Depth = 0.7f; - CommitEntity(shape); - } - CommitEntity(wheel); - } - - { - auto wheel = CreateEntity(tank); - auto transform = AddComponent(wheel, "Transform"); - transform->Position = glm::vec3(-1.68f, -0.83f - wheelOffset, -2.6f); - transform->Orientation = glm::angleAxis(glm::pi(), glm::vec3(0, 1, 0)); - auto model = AddComponent(wheel, "Model"); - model->ModelFile = "Models/Tank/Fix/WheelPhysics.obj"; - auto Wheel = AddComponent(wheel, "Wheel"); - Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); - Wheel->AxleID = 0; - Wheel->Mass = 2000; - Wheel->Radius = 0.6f; - Wheel->Steering = true; - Wheel->SuspensionStrength = suspensionStrength; - Wheel->Friction = 4.f; - Wheel->ConnectedToHandbrake = true; - Wheel->TorqueRatio = 0.125f; - Wheel->Width = 0.6f; - { - auto shape = CreateEntity(wheel); - auto shapetransform = AddComponent(shape, "Transform"); - shapetransform->Position = glm::vec3(0.f, 0.32f, 0.f) + transform->Position; - auto boxShape = AddComponent(shape, "BoxShape"); - boxShape->Width = 0.7f; - boxShape->Height = 0.34f; - boxShape->Depth = 0.7f; - CommitEntity(shape); - } - CommitEntity(wheel); - } - { - auto wheel = CreateEntity(tank); - auto transform = AddComponent(wheel, "Transform"); - transform->Position = glm::vec3(-1.68f, -0.83f - wheelOffset, -0.83f); - transform->Orientation = glm::angleAxis(glm::pi(), glm::vec3(0, 1, 0)); - auto model = AddComponent(wheel, "Model"); - model->ModelFile = "Models/Tank/Fix/WheelPhysics.obj"; - auto Wheel = AddComponent(wheel, "Wheel"); - Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); - Wheel->AxleID = 0; - Wheel->Mass = 2000; - Wheel->Radius = 0.6f; - Wheel->Steering = false; - Wheel->SuspensionStrength = suspensionStrength; - Wheel->Friction = 4.f; - Wheel->ConnectedToHandbrake = true; - Wheel->TorqueRatio = 0.125f; - Wheel->Width = 0.6f; - { - auto shape = CreateEntity(wheel); - auto shapetransform = AddComponent(shape, "Transform"); - shapetransform->Position = glm::vec3(0.f, 0.32f, 0.f) + transform->Position; - auto boxShape = AddComponent(shape, "BoxShape"); - boxShape->Width = 0.7f; - boxShape->Height = 0.34f; - boxShape->Depth = 0.7f; - CommitEntity(shape); - } - CommitEntity(wheel); - } - - - //Back - { - auto wheel = CreateEntity(tank); - auto transform = AddComponent(wheel, "Transform"); - transform->Position = glm::vec3(1.68f, -0.83f - wheelOffset, 1.f); - auto model = AddComponent(wheel, "Model"); - model->ModelFile = "Models/Tank/Fix/WheelPhysics.obj"; - auto Wheel = AddComponent(wheel, "Wheel"); - Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); - Wheel->AxleID = 1; - Wheel->Mass = 2000; - Wheel->Radius = 0.6f; - Wheel->Steering = false; - Wheel->SuspensionStrength = suspensionStrength; - Wheel->Friction = 4.f; - Wheel->ConnectedToHandbrake = true; - Wheel->TorqueRatio = 0.125f; - Wheel->Width = 0.6f; - { - auto shape = CreateEntity(wheel); - auto shapetransform = AddComponent(shape, "Transform"); - shapetransform->Position = glm::vec3(0.f, 0.32f, 0.f) + transform->Position; - auto boxShape = AddComponent(shape, "BoxShape"); - boxShape->Width = 0.7f; - boxShape->Height = 0.34f; - boxShape->Depth = 0.7f; - CommitEntity(shape); - } - CommitEntity(wheel); - } - { - auto wheel = CreateEntity(tank); - auto transform = AddComponent(wheel, "Transform"); - transform->Position = glm::vec3(1.68f, -0.83f - wheelOffset, 2.95f); - auto model = AddComponent(wheel, "Model"); - model->ModelFile = "Models/Tank/Fix/WheelPhysics.obj"; - auto Wheel = AddComponent(wheel, "Wheel"); - Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); - Wheel->AxleID = 1; - Wheel->Mass = 2000; - Wheel->Radius = 0.6f; - Wheel->Steering = false; - Wheel->SuspensionStrength = suspensionStrength; - Wheel->Friction = 4.f; - Wheel->ConnectedToHandbrake = true; - Wheel->TorqueRatio = 0.125f; - Wheel->Width = 0.6f; - { - auto shape = CreateEntity(wheel); - auto shapetransform = AddComponent(shape, "Transform"); - shapetransform->Position = glm::vec3(0.f, 0.32f, 0.f) + transform->Position; - auto boxShape = AddComponent(shape, "BoxShape"); - boxShape->Width = 0.7f; - boxShape->Height = 0.34f; - boxShape->Depth = 0.7f; - CommitEntity(shape); - } - CommitEntity(wheel); - - auto entity = CreateEntity(tank); - auto transformComponent = AddComponent(entity, "Transform"); - transformComponent->Position = glm::vec3(2,-1.7,2.0); - transformComponent->Scale = glm::vec3(3,3,3); - transformComponent->Orientation = glm::angleAxis(glm::pi()/2, glm::vec3(1,0,0)); - auto emitterComponent = AddComponent(entity, "ParticleEmitter"); - emitterComponent->SpawnCount = 2; - emitterComponent->SpawnFrequency = 0.005; - emitterComponent->SpreadAngle = glm::pi(); - emitterComponent->UseGoalVelocity = false; - emitterComponent->LifeTime = 0.5; - //emitterComponent->AngularVelocitySpectrum.push_back(glm::pi() / 100); - emitterComponent->ScaleSpectrum.push_back(glm::vec3(0.05)); - CommitEntity(entity); - - auto particleEntity = CreateEntity(entity); - auto TEMP = AddComponent(particleEntity, "Transform"); - TEMP->Scale = glm::vec3(0); - auto spriteComponent = AddComponent(particleEntity, "Sprite"); - spriteComponent->SpriteFile = "Models/Textures/Sprites/Dust.png"; - emitterComponent->ParticleTemplate = particleEntity; - - CommitEntity(particleEntity); - } - - { - auto wheel = CreateEntity(tank); - auto transform = AddComponent(wheel, "Transform"); - transform->Position = glm::vec3(-1.68f, -0.83f - wheelOffset, 1.f); - transform->Orientation = glm::angleAxis(glm::pi(), glm::vec3(0, 1, 0)); - auto model = AddComponent(wheel, "Model"); - model->ModelFile = "Models/Tank/Fix/WheelPhysics.obj"; - auto Wheel = AddComponent(wheel, "Wheel"); - Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); - Wheel->AxleID = 1; - Wheel->Mass = 2000; - Wheel->Radius = 0.6f; - Wheel->Steering = false; - Wheel->SuspensionStrength = suspensionStrength; - Wheel->Friction = 4.f; - Wheel->ConnectedToHandbrake = true; - Wheel->TorqueRatio = 0.125f; - Wheel->Width = 0.6f; - { - auto shape = CreateEntity(wheel); - auto shapetransform = AddComponent(shape, "Transform"); - shapetransform->Position = glm::vec3(0.f, 0.32f, 0.f) + transform->Position; - auto boxShape = AddComponent(shape, "BoxShape"); - boxShape->Width = 0.7f; - boxShape->Height = 0.34f; - boxShape->Depth = 0.7f; - CommitEntity(shape); - } - CommitEntity(wheel); - } - { - auto wheel = CreateEntity(tank); - auto transform = AddComponent(wheel, "Transform"); - transform->Position = glm::vec3(-1.68f, -0.83f - wheelOffset, 2.95f); - auto model = AddComponent(wheel, "Model"); - model->ModelFile = "Models/Tank/Fix/WheelPhysics.obj"; - auto Wheel = AddComponent(wheel, "Wheel"); - Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); - Wheel->AxleID = 1; - Wheel->Mass = 2000; - Wheel->Radius = 0.6f; - Wheel->Steering = false; - Wheel->SuspensionStrength = suspensionStrength; - Wheel->Friction = 4.f; - Wheel->ConnectedToHandbrake = true; - Wheel->TorqueRatio = 0.125f; - Wheel->Width = 0.6f; - { - auto shape = CreateEntity(wheel); - auto shapetransform = AddComponent(shape, "Transform"); - shapetransform->Position = glm::vec3(0.f, 0.32f, 0.f) + transform->Position; - auto boxShape = AddComponent(shape, "BoxShape"); - boxShape->Width = 0.7f; - boxShape->Height = 0.34f; - boxShape->Depth = 0.7f; - CommitEntity(shape); - } - CommitEntity(wheel); - - auto entity = CreateEntity(tank); - auto transformComponent = AddComponent(entity, "Transform"); - transformComponent->Position = glm::vec3(-2,-1.7,2.0); - transformComponent->Scale = glm::vec3(3,3,3); - transformComponent->Orientation = glm::angleAxis(glm::pi()/2, glm::vec3(1,0,0)); - auto emitterComponent = AddComponent(entity, "ParticleEmitter"); - emitterComponent->SpawnCount = 2; - emitterComponent->SpawnFrequency = 0.005; - emitterComponent->SpreadAngle = glm::pi(); - emitterComponent->UseGoalVelocity = false; - emitterComponent->LifeTime = 0.5; - //emitterComponent->AngularVelocitySpectrum.push_back(glm::pi() / 100); - emitterComponent->ScaleSpectrum.push_back(glm::vec3(0.05)); - CommitEntity(entity); - - auto particleEntity = CreateEntity(entity); - auto TEMP = AddComponent(particleEntity, "Transform"); - TEMP->Scale = glm::vec3(0); - auto spriteComponent = AddComponent(particleEntity, "Sprite"); - spriteComponent->SpriteFile = "Models/Textures/Sprites/Dust.png"; - emitterComponent->ParticleTemplate = particleEntity; - - CommitEntity(particleEntity); - } - - CommitEntity(tank); + } + CommitEntity(heli); - - - /* - for(int i = 0; i < 10; i++) - { - auto entity = CreateEntity(); - auto transform = AddComponent(entity, "Transform"); - transform->Position = glm::vec3(30 + i*0.1f, 0 + i*0.1f, 10 + i*0.1f); - transform->Scale = glm::vec3(0); - transform->Orientation = glm::quat(glm::vec3(0.0f, 0.0f, 0.0f)); - - std::stringstream ss; - ss << "Models/Placeholders/ShatterTest/" << i+1 << ".obj"; - - auto model = AddComponent(entity, "Model"); - model->ModelFile = ss.str(); - - auto physics = AddComponent(entity, "Physics"); - physics->Mass = 100; - physics->Static = true; - auto meshShape = AddComponent(entity, "MeshShape"); - meshShape->ResourceName = ss.str(); - - CommitEntity(entity); - }*/ - - for(int i = 0; i < 1; i++) + auto camera = CreateEntity(heli); { - for (int y = 0; y < 15; y++) - { - for (int x = -5; x < 5; x++) - { - auto brick = CreateEntity(); - auto transform = AddComponent(brick, "Transform"); - transform->Position = glm::vec3(x + 0.01f, y * 0.3f + 0.01f, -20); - transform->Position.x += (y % 2)*0.5f; - transform->Scale = glm::vec3(1, 0.3f, 0.4f); - transform->Orientation = glm::quat(glm::vec3(0.0f, 0.0f, 0.0f)); - auto model = AddComponent(brick, "Model"); - model->ModelFile = "Models/Placeholders/PhysicsTest/Cube2.obj"; - - auto physics = AddComponent(brick, "Physics"); - physics->Mass = 3; - - - - auto shape = CreateEntity(brick); - auto transformshape = AddComponent(shape, "Transform"); - auto box = AddComponent(shape, "BoxShape"); - box->Width = 0.5f; - box->Height = 0.15f; - box->Depth = 0.3f; - CommitEntity(shape); - CommitEntity(brick); - } - } + auto transform = AddComponent(camera, "Transform"); + transform->Position.z = 10.f; + transform->Position.y = 5.f; + //transform->Orientation = glm::quat(glm::vec3(glm::pi() / 8.f, 0.f, 0.f)); + auto cameraComp = AddComponent(camera, "Camera"); + cameraComp->FarClip = 2000.f; + auto freeSteering = AddComponent(camera, "FreeSteering"); + CommitEntity(camera); } - - /*for (int x = 0; x < 5; x++) - for (int y = 0; y < 5; y++) - { - auto cube = CreateEntity(); - auto transform = AddComponent(cube, "Transform"); - transform->Position = glm::vec3(3 * x + 0.1f + -20.f, 3 * y + 0.1f + 1.f, 0); - transform->Scale = glm::vec3(3); - transform->Orientation = glm::quat(glm::vec3(0.0f, 0.0f, 0.0f)); - auto model = AddComponent(cube, "Model"); - model->ModelFile = "Models/Placeholders/PhysicsTest/Cube2.obj"; - - auto physics = AddComponent(cube, "Physics"); - physics->Mass = 100; - auto box = AddComponent(cube, "BoxShape"); - box->Width = 1.5f; - box->Height = 1.5f; - box->Depth = 1.5f; - CommitEntity(cube); - } -*/ - - - /*{ - auto entity = CreateEntity(); - AddComponent(entity, "Transform"); - auto emitter = AddComponent(entity, "SoundEmitter"); - emitter->Path = "Sounds/korvring.wav"; - emitter->Loop = true; - GetSystem("SoundSystem")->PlaySound(emitter); - CommitEntity(entity); - }*/ - } void GameWorld::Update(double dt) @@ -742,6 +151,7 @@ void GameWorld::RegisterSystems() //m_SystemFactory.Register("PlayerSystem", [this]() { return new Systems::PlayerSystem(this); }); m_SystemFactory.Register("FreeSteeringSystem", [this]() { return new Systems::FreeSteeringSystem(this, m_EventBroker); }); m_SystemFactory.Register("TankSteeringSystem", [this]() { return new Systems::TankSteeringSystem(this, m_EventBroker); }); + m_SystemFactory.Register("HelicopterSteeringSystem", [this]() { return new Systems::HelicopterSteeringSystem(this, m_EventBroker); }); m_SystemFactory.Register("SoundSystem", [this]() { return new Systems::SoundSystem(this, m_EventBroker); }); m_SystemFactory.Register("PhysicsSystem", [this]() { return new Systems::PhysicsSystem(this, m_EventBroker); }); m_SystemFactory.Register("RenderSystem", [this]() { return new Systems::RenderSystem(this, m_EventBroker, m_Renderer); }); @@ -758,6 +168,7 @@ void GameWorld::AddSystems() //AddSystem("PlayerSystem"); AddSystem("FreeSteeringSystem"); AddSystem("TankSteeringSystem"); + AddSystem("HelicopterSteeringSystem"); AddSystem("SoundSystem"); AddSystem("PhysicsSystem"); AddSystem("RenderSystem"); diff --git a/src/GameWorld.h b/src/GameWorld.h index 7235e25..952f002 100755 --- a/src/GameWorld.h +++ b/src/GameWorld.h @@ -13,6 +13,7 @@ //#include "Systems/PlayerSystem.h" #include "Systems/FreeSteeringSystem.h" #include "Systems/TankSteeringSystem.h" +#include "Systems/HelicopterSteeringSystem.h" #include "Systems/RenderSystem.h" #include "Systems/SoundSystem.h" #include "Systems/PhysicsSystem.h" diff --git a/src/Systems/FreeSteeringSystem.cpp b/src/Systems/FreeSteeringSystem.cpp index edf24d0..c5ed953 100755 --- a/src/Systems/FreeSteeringSystem.cpp +++ b/src/Systems/FreeSteeringSystem.cpp @@ -60,27 +60,27 @@ void Systems::FreeSteeringSystem::UpdateEntity(double dt, EntityID entity, Entit bool Systems::FreeSteeringSystem::FreeSteeringInputController::OnCommand(const Events::InputCommand &event) { // Movement - if (event.Command == "vertical") + if (event.Command == "cam_vertical") { Movement.z = -event.Value; } - else if (event.Command == "horizontal") + else if (event.Command == "cam_horizontal") { Movement.x = event.Value; } - else if (event.Command == "normal") + else if (event.Command == "cam_normal") { Movement.y = event.Value; } // Speed - else if (event.Command == "speed") + else if (event.Command == "cam_speed") { SpeedMultiplier = event.Value; } // Mouse click - else if (event.Command == "attack") + else if (event.Command == "cam_attack") { OrientationActive = event.Value > 0; @@ -96,11 +96,11 @@ bool Systems::FreeSteeringSystem::FreeSteeringInputController::OnCommand(const E } } - else if (event.Command == "vertical2") + else if (event.Command == "cam_vertical2") { ControllerOrientation.x = event.Value; } - else if (event.Command == "horizontal2") + else if (event.Command == "cam_horizontal2") { ControllerOrientation.y = -event.Value; } diff --git a/src/Systems/HelicopterSteeringSystem.cpp b/src/Systems/HelicopterSteeringSystem.cpp new file mode 100644 index 0000000..7fd9661 --- /dev/null +++ b/src/Systems/HelicopterSteeringSystem.cpp @@ -0,0 +1,67 @@ +#include "PrecompiledHeader.h" +#include "HelicopterSteeringSystem.h" +#include "World.h" + +void Systems::HelicopterSteeringSystem::RegisterComponents(ComponentFactory* cf) +{ + cf->Register("HelicopterSteering", []() { return new Components::HelicopterSteering(); }); +} + +void Systems::HelicopterSteeringSystem::Initialize() +{ + m_InputController = std::unique_ptr(new HelicopterSteeringInputController(EventBroker)); +} + +void Systems::HelicopterSteeringSystem::Update(double dt) +{ + +} + +void Systems::HelicopterSteeringSystem::UpdateEntity(double dt, EntityID entity, EntityID parent) +{ + auto transform = m_World->GetComponent(entity, "Transform"); + if (!transform) + return; + + auto helicopterComponent = m_World->GetComponent(entity, "HelicopterSteering"); + if (helicopterComponent) + { + glm::vec3 controllerRotationEuler = m_InputController->Rotation * (float)dt; + transform->Orientation *= glm::quat(controllerRotationEuler); + + Events::ApplyForce e; + e.Entity = entity; + e.DeltaTime = dt; + e.Force = glm::normalize(transform->Orientation * glm::vec3(0, 1, 0)) * (m_InputController->Power * 3000.f * 9.82f * 8.f); + EventBroker->Publish(e); + } +} + +bool Systems::HelicopterSteeringSystem::HelicopterSteeringInputController::OnCommand(const Events::InputCommand &event) +{ + if (event.Command == "horizontal") + { + Rotation.z = -event.Value; + } + else if (event.Command == "vertical") + { + Rotation.x = -event.Value; + } + + else if (event.Command == "normal") + { + Power = event.Value; + } + + return true; +} + +bool Systems::HelicopterSteeringSystem::HelicopterSteeringInputController::OnMouseMove(const Events::MouseMove &event) +{ + return true; +} + +void Systems::HelicopterSteeringSystem::HelicopterSteeringInputController::Update(double dt) +{ + +} diff --git a/src/Systems/HelicopterSteeringSystem.h b/src/Systems/HelicopterSteeringSystem.h new file mode 100644 index 0000000..ed91096 --- /dev/null +++ b/src/Systems/HelicopterSteeringSystem.h @@ -0,0 +1,54 @@ +#include "System.h" +#include "Components/Transform.h" +#include "Components/HelicopterSteering.h" +#include "Events/SetVelocity.h" +#include "Events/ApplyForce.h" +#include "InputController.h" + +namespace Systems +{ + +class HelicopterSteeringSystem : public System +{ +public: + HelicopterSteeringSystem(World* world, std::shared_ptr<::EventBroker> eventBroker) + : System(world, eventBroker) { } + + void RegisterComponents(ComponentFactory* cf) override; + void Initialize() override; + + void Update(double dt) override; + void UpdateEntity(double dt, EntityID entity, EntityID parent) override; + +private: + class HelicopterSteeringInputController; + std::unique_ptr m_InputController; + + std::map m_TimeSinceLastShot; +}; + +class HelicopterSteeringSystem::HelicopterSteeringInputController : InputController +{ +public: + HelicopterSteeringInputController(std::shared_ptr<::EventBroker> eventBroker) + : InputController(eventBroker) + , Power(0.f) + { } + + float Power; + glm::vec3 Rotation; + + void Update(double dt); + +protected: + bool OnCommand(const Events::InputCommand &event) override; + bool OnMouseMove(const Events::MouseMove &event) override; + + +}; + + + + + +} \ No newline at end of file diff --git a/src/Systems/PhysicsSystem.cpp b/src/Systems/PhysicsSystem.cpp index 8efa4bb..e9bc69c 100644 --- a/src/Systems/PhysicsSystem.cpp +++ b/src/Systems/PhysicsSystem.cpp @@ -32,6 +32,7 @@ void Systems::PhysicsSystem::Initialize() // Events EVENT_SUBSCRIBE_MEMBER(m_ETankSteer, &Systems::PhysicsSystem::OnTankSteer); EVENT_SUBSCRIBE_MEMBER(m_ESetVelocity, &Systems::PhysicsSystem::OnSetVelocity); + EVENT_SUBSCRIBE_MEMBER(m_EApplyForce, &Systems::PhysicsSystem::OnApplyForce); hkMemorySystem::FrameInfo finfo(6000 * 1024); // Allocate 6MB of Physics solver buffer hkMemoryRouter* memoryRouter = hkMemoryInitUtil::initDefault(hkMallocAllocator::m_defaultMallocAllocator, finfo); @@ -160,7 +161,7 @@ void Systems::PhysicsSystem::Update(double dt) m_Accumulator += dt; while (m_Accumulator >= timestep) { - m_PhysicsWorld->stepMultithreaded(m_JobQueue, m_ThreadPool, timestep); + hkpStepResult stepresult = m_PhysicsWorld->stepMultithreaded(m_JobQueue, m_ThreadPool, timestep); //m_PhysicsWorld->stepDeltaTime(timestep); m_Accumulator -= timestep; @@ -172,10 +173,7 @@ void Systems::PhysicsSystem::Update(double dt) // Clear accumulated timer data in this thread and all slave threads hkMonitorStream::getInstance().reset(); m_ThreadPool->clearTimerData(); - } - - - + } } void Systems::PhysicsSystem::UpdateEntity(double dt, EntityID entity, EntityID parent) @@ -586,5 +584,15 @@ bool Systems::PhysicsSystem::OnSetVelocity( const Events::SetVelocity &event ) m_PhysicsWorld->markForWrite(); m_RigidBodies[event.Entity]->setLinearVelocity(ConvertPosition(event.Velocity)); m_PhysicsWorld->unmarkForWrite(); + + return true; +} + +bool Systems::PhysicsSystem::OnApplyForce(const Events::ApplyForce &event) +{ + m_PhysicsWorld->markForWrite(); + m_RigidBodies[event.Entity]->applyForce(event.DeltaTime, ConvertPosition(event.Force)); + m_PhysicsWorld->unmarkForWrite(); + return true; } diff --git a/src/Systems/PhysicsSystem.h b/src/Systems/PhysicsSystem.h index 7b2d259..8670cae 100644 --- a/src/Systems/PhysicsSystem.h +++ b/src/Systems/PhysicsSystem.h @@ -15,6 +15,7 @@ #include "Components/TowerSteering.h" #include "Events/TankSteer.h" #include "Events/SetVelocity.h" +#include "Events/ApplyForce.h" #include "OBJ.h" // Math and base include @@ -85,9 +86,10 @@ private: // Events EventRelay m_ETankSteer; bool OnTankSteer(const Events::TankSteer &event); - EventRelay m_ESetVelocity; bool OnSetVelocity(const Events::SetVelocity &event); + EventRelay m_EApplyForce; + bool OnApplyForce(const Events::ApplyForce &event); void SetUpPhysicsState(EntityID entity, EntityID parent); void TearDownPhysicsState(EntityID entity, EntityID parent); diff --git a/vs11/Returngeance/Returngeance.vcxproj b/vs11/Returngeance/Returngeance.vcxproj index 9849fc0..4681962 100755 --- a/vs11/Returngeance/Returngeance.vcxproj +++ b/vs11/Returngeance/Returngeance.vcxproj @@ -111,6 +111,7 @@ + @@ -131,6 +132,7 @@ + @@ -152,6 +154,7 @@ + @@ -188,6 +191,7 @@ + diff --git a/vs11/Returngeance/Returngeance.vcxproj.filters b/vs11/Returngeance/Returngeance.vcxproj.filters index 49cc263..22eb9ea 100755 --- a/vs11/Returngeance/Returngeance.vcxproj.filters +++ b/vs11/Returngeance/Returngeance.vcxproj.filters @@ -63,6 +63,7 @@ Physics\Systems + @@ -137,6 +138,21 @@ {42ae084f-ade8-402c-87ba-f03f6b846bc8} + + {5b992473-73e6-485e-8f13-17e0801fb2ca} + + + {8a78be3a-a3c5-4054-a2f1-1456f3fcbab0} + + + {8ccc0abe-5bdd-4656-ab11-5708a39c71ae} + + + {b2416d05-a49e-4fef-a5c4-cd03d8cc2efd} + + + {b34c0c95-a887-4cf4-af24-cf7c1f459aa8} + @@ -356,6 +372,15 @@ Input\Events + + Gameplay\Vehicles\Helicopter\Components + + + Gameplay\Vehicles\Helicopter\Systems + + + Physics\Events + From 67778f391a6730d9d373d6a0a4f47edfee7d08a9 Mon Sep 17 00:00:00 2001 From: Tleety Date: Sat, 17 May 2014 14:00:26 +0200 Subject: [PATCH 06/21] Working on shadows --- assets | 2 +- src/GameWorld.cpp | 6 +++--- src/Renderer.cpp | 33 +++++++++++++++++++++++++++++++-- src/Renderer.h | 2 ++ 4 files changed, 37 insertions(+), 6 deletions(-) diff --git a/assets b/assets index 672e8a2..cdee701 160000 --- a/assets +++ b/assets @@ -1 +1 @@ -Subproject commit 672e8a2b11ecaafc95a5b0286b5ec310c62438ad +Subproject commit cdee7015793193f0e78a2b75b25e3a2c07881632 diff --git a/src/GameWorld.cpp b/src/GameWorld.cpp index 19026b2..160498b 100755 --- a/src/GameWorld.cpp +++ b/src/GameWorld.cpp @@ -17,14 +17,14 @@ void GameWorld::Initialize() auto ground = CreateEntity(); auto transform = AddComponent(ground, "Transform"); transform->Position = glm::vec3(0, -5, 0); - transform->Scale = glm::vec3(400.0f, 10.0f, 400.0f); + transform->Scale = glm::vec3(800.0f, 10.0f, 800.0f); transform->Orientation = glm::quat(glm::vec3(0.0f, 0.0f, 0.0f)); auto model = AddComponent(ground, "Model"); model->ModelFile = "Models/Placeholders/PhysicsTest/Cube.obj"; auto box = AddComponent(ground, "Box"); - box->Width = 200; + box->Width = 400; box->Height = 5; - box->Depth = 200; + box->Depth = 400; auto physics = AddComponent(ground, "Physics"); physics->Mass = 10; diff --git a/src/Renderer.cpp b/src/Renderer.cpp index 2e781a0..d78f0b7 100755 --- a/src/Renderer.cpp +++ b/src/Renderer.cpp @@ -17,10 +17,10 @@ Renderer::Renderer() CAtt = 1.0f; LAtt = 0.0f; QAtt = 3.0f; - m_ShadowMapRes = 2048*8; + m_ShadowMapRes = 2048*16; m_SunPosition = glm::vec3(0, 3.5f, 10); m_SunTarget = glm::vec3(0, 0, 0); - m_SunProjection = glm::ortho(-200.f, 200.f, -200.f, 200.f, -100, 200); + m_SunProjection = glm::ortho(10.f, -10.f, 10.f, -10.f, 10.f, -10.f); /* Lights = 0;*/ } @@ -782,3 +782,32 @@ glm::mat4 Renderer::CreateLightMatrix(Light &_light) return model; } +void Renderer::UpdateSunProjection() +{ + glm::vec3 NDCCube[] = + { + glm::vec3(-1.f, -1.f, -1.f), + glm::vec3(1.f, -1.f, -1.f), + glm::vec3(-1.f, 1.f, -1.f), + glm::vec3(1.f, 1.f, -1.f), + glm::vec3(-1.f, -1.f, 1.f), + glm::vec3(1.f, -1.f, 1.f), + glm::vec3(-1.f, 1.f, 1.f), + glm::vec3(1.f, 1.f, 1.f) + }; + + glm::mat4 inverseProjectionViewMatrix = glm::inverse(m_Camera->ViewMatrix()) * glm::inverse(m_Camera->ProjectionMatrix()); + //Also * with world matrix for light + + for(auto corner : NDCCube) + { + //corner *= inverseProjectionViewMatrix; + } + + //Calculate the bounding box of the transformed frustum corners. This will be the view frustum for the shadow map. + + //Pass the bounding box's extents to glOrtho or similar to set up the orthographic projection matrix for the shadow map. +} + + + diff --git a/src/Renderer.h b/src/Renderer.h index e1a2aec..9053d92 100755 --- a/src/Renderer.h +++ b/src/Renderer.h @@ -141,6 +141,8 @@ private: void DrawLightScene(); void BindFragDataLocation(); glm::mat4 CreateLightMatrix(Light &_light); + void UpdateSunProjection(); + GLuint CreateQuad(); void DrawDebugShadowMap(); From 4d15d3e02ce1eb40de13c40c234a77b6ba81c09b Mon Sep 17 00:00:00 2001 From: ViktorLjung Date: Sat, 17 May 2014 18:06:56 +0200 Subject: [PATCH 07/21] Collision callbacks --- src/Components/Health.h | 21 ++++++++++++++++ src/GameWorld.cpp | 2 +- src/Systems/PhysicsSystem.cpp | 11 +++++++- src/Systems/PhysicsSystem.h | 25 ++++++++++++++++--- vs11/Returngeance/Returngeance.vcxproj | 1 + .../Returngeance/Returngeance.vcxproj.filters | 3 +++ 6 files changed, 58 insertions(+), 5 deletions(-) create mode 100644 src/Components/Health.h diff --git a/src/Components/Health.h b/src/Components/Health.h new file mode 100644 index 0000000..5100aaf --- /dev/null +++ b/src/Components/Health.h @@ -0,0 +1,21 @@ +#ifndef Components_Health_h__ +#define Components_Health_h__ + +#include "Component.h" + +namespace Components +{ + + struct Health : Component + { + Health() + : health(1.0f){ } + + float health; + + virtual Health* Clone() const override { return new Health(*this); } + }; + +} + +#endif // Components_Health_h__ diff --git a/src/GameWorld.cpp b/src/GameWorld.cpp index 9e48cf7..e9f5327 100755 --- a/src/GameWorld.cpp +++ b/src/GameWorld.cpp @@ -340,7 +340,7 @@ void GameWorld::Initialize() //Create wheels float wheelOffset = 0.4f; float springLength = 0.3f; - float suspensionStrength = 25.f; + float suspensionStrength = 15.f; { auto wheel = CreateEntity(tank); diff --git a/src/Systems/PhysicsSystem.cpp b/src/Systems/PhysicsSystem.cpp index 8efa4bb..51420d3 100644 --- a/src/Systems/PhysicsSystem.cpp +++ b/src/Systems/PhysicsSystem.cpp @@ -27,6 +27,8 @@ void Systems::PhysicsSystem::Initialize() { + + m_Accumulator = 0; // Events @@ -102,6 +104,8 @@ void Systems::PhysicsSystem::Initialize() SetupVisualDebugger(m_Context); m_PhysicsWorld->unmarkForWrite(); + + m_collisionResolution = new MyCollisionResolution; } } @@ -324,9 +328,11 @@ void Systems::PhysicsSystem::OnEntityCommit( EntityID entity ) m_PhysicsWorld->markForWrite(); vehicleSetup.buildVehicle(m_World, m_PhysicsWorld, *m_Vehicles[entity], entity, m_Wheels); // Add the vehicle's entities and phantoms to the world + rigidBody->addContactListener( m_collisionResolution ); m_Vehicles[entity]->addToWorld(m_PhysicsWorld); - m_RigidBodies[entity] = rigidBody; + m_collisionResolution->m_RigidBodies[rigidBody] = entity; + // The vehicle is an action m_PhysicsWorld->addAction(m_Vehicles[entity]); @@ -340,8 +346,10 @@ void Systems::PhysicsSystem::OnEntityCommit( EntityID entity ) else { m_PhysicsWorld->markForWrite(); + rigidBody->addContactListener( m_collisionResolution ); m_PhysicsWorld->addEntity(rigidBody); m_RigidBodies[entity] = rigidBody; + m_collisionResolution->m_RigidBodies[rigidBody] = entity; m_PhysicsWorld->unmarkForWrite(); shape->removeReference(); @@ -394,6 +402,7 @@ void Systems::PhysicsSystem::OnEntityCommit( EntityID entity ) m_PhysicsWorld->markForWrite(); m_PhysicsWorld->addEntity(rigidBody); m_RigidBodies[entity] = rigidBody; + m_collisionResolution->m_RigidBodies[rigidBody] = entity; m_PhysicsWorld->unmarkForWrite(); shape->removeReference(); diff --git a/src/Systems/PhysicsSystem.h b/src/Systems/PhysicsSystem.h index 7b2d259..ef7cb70 100644 --- a/src/Systems/PhysicsSystem.h +++ b/src/Systems/PhysicsSystem.h @@ -60,9 +60,27 @@ #include "Physics/VehicleSetup.h" #include + +#include + +class MyCollisionResolution: public hkReferencedObject, public hkpContactListener +{ +public: + std::unordered_map m_RigidBodies; + + virtual void contactPointCallback( const hkpContactPointEvent& event ) + { + + EntityID entity1 = m_RigidBodies[event.getBody(0)]; + EntityID entity2 = m_RigidBodies[event.getBody(1)]; + LOG_INFO("Entities colliding: %i, %i ", entity1, entity2); + + } +}; + + namespace Systems { - class PhysicsSystem : public System { public: @@ -77,7 +95,7 @@ public: void OnComponentCreated(std::string type, std::shared_ptr component) override; void OnComponentRemoved(std::string type, Component* component) override; void OnEntityCommit(EntityID entity) override; - + private: double m_Accumulator; hkpWorld* m_PhysicsWorld; @@ -145,8 +163,9 @@ private: hkpMoppBvTreeShape* MoppShape; }; std::unordered_map m_ExtendedMeshShapes; + + MyCollisionResolution* m_collisionResolution; }; } - #endif // PhysicsSystem_h__ diff --git a/vs11/Returngeance/Returngeance.vcxproj b/vs11/Returngeance/Returngeance.vcxproj index 6ccecc9..96c8e85 100755 --- a/vs11/Returngeance/Returngeance.vcxproj +++ b/vs11/Returngeance/Returngeance.vcxproj @@ -131,6 +131,7 @@ + diff --git a/vs11/Returngeance/Returngeance.vcxproj.filters b/vs11/Returngeance/Returngeance.vcxproj.filters index 641fa8f..70213c2 100755 --- a/vs11/Returngeance/Returngeance.vcxproj.filters +++ b/vs11/Returngeance/Returngeance.vcxproj.filters @@ -352,6 +352,9 @@ Input\Events + + Physics\Components + From a8ecacab11e3d60555738ae3c5fbb965d5fb8aa0 Mon Sep 17 00:00:00 2001 From: ViktorLjung Date: Sat, 17 May 2014 18:45:54 +0200 Subject: [PATCH 08/21] tank steering --- src/GameWorld.cpp | 3 ++- src/Physics/VehicleSetup.cpp | 2 +- src/Physics/VehicleSetup.h | 26 ++++++++++++++++++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/GameWorld.cpp b/src/GameWorld.cpp index e9f5327..5cc997c 100755 --- a/src/GameWorld.cpp +++ b/src/GameWorld.cpp @@ -248,6 +248,7 @@ void GameWorld::Initialize() physics->Static = false; auto vehicle = AddComponent(tank, "Vehicle"); vehicle->MaxTorque = 5200.f; + vehicle->MaxSteeringAngle = 90.f; AddComponent(tank, "TankSteering"); AddComponent(tank, "Input"); @@ -445,7 +446,7 @@ void GameWorld::Initialize() Wheel->AxleID = 0; Wheel->Mass = 2000; Wheel->Radius = 0.6f; - Wheel->Steering = false; + Wheel->Steering = true; Wheel->SuspensionStrength = suspensionStrength; Wheel->Friction = 4.f; Wheel->ConnectedToHandbrake = true; diff --git a/src/Physics/VehicleSetup.cpp b/src/Physics/VehicleSetup.cpp index 90e65dc..7d3610a 100644 --- a/src/Physics/VehicleSetup.cpp +++ b/src/Physics/VehicleSetup.cpp @@ -22,7 +22,7 @@ void VehicleSetup::buildVehicle(World *world, const hkpWorld* physicsWorld, hkpV // vehicle.m_data = new hkpVehicleData; vehicle.m_driverInput = new hkpVehicleDefaultAnalogDriverInput; - vehicle.m_steering = new hkpVehicleDefaultSteering; + vehicle.m_steering = new TankSteering; vehicle.m_engine = new hkpVehicleDefaultEngine; vehicle.m_transmission = new hkpVehicleDefaultTransmission; vehicle.m_brake = new hkpVehicleDefaultBrake; diff --git a/src/Physics/VehicleSetup.h b/src/Physics/VehicleSetup.h index 7f78204..bf75840 100644 --- a/src/Physics/VehicleSetup.h +++ b/src/Physics/VehicleSetup.h @@ -33,6 +33,32 @@ #include "Components/Wheel.h" #include "Components/Transform.h" + +/// Tank specific steering implementation. Rear wheels steer in opposite direction +/// to front wheels. +class TankSteering: public hkpVehicleDefaultSteering +{ +public: + virtual void calcSteering(const hkReal deltaTime, const hkpVehicleInstance* vehicle, const hkpVehicleDriverInput::FilteredDriverInputOutput& filteredInfoOutput, SteeringAnglesOutput& steeringOutput ) + { + hkpVehicleDefaultSteering::calcMainSteeringAngle( deltaTime, vehicle, filteredInfoOutput, steeringOutput ); + + // Wheels. + for (int w_it = 0; w_it < m_doesWheelSteer.getSize(); w_it++) + { + if ( m_doesWheelSteer[w_it] ) + { + steeringOutput.m_wheelsSteeringAngle [w_it] = steeringOutput.m_mainSteeringAngle; + } + else + { + // Steer with front and back wheels to simulate a tank. + steeringOutput.m_wheelsSteeringAngle [w_it] = -steeringOutput.m_mainSteeringAngle; + } + } + } +}; + class VehicleSetup { public: From fd0bb18bd33877d43c9f166a9cc1eb83992e49d5 Mon Sep 17 00:00:00 2001 From: ViktorLjung Date: Sun, 18 May 2014 21:07:03 +0200 Subject: [PATCH 09/21] tank stuff --- src/GameWorld.cpp | 3 ++- src/Systems/PhysicsSystem.h | 2 +- src/Systems/TankSteeringSystem.cpp | 1 + 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/GameWorld.cpp b/src/GameWorld.cpp index ca9dff4..fa338f2 100755 --- a/src/GameWorld.cpp +++ b/src/GameWorld.cpp @@ -222,11 +222,12 @@ void GameWorld::Initialize() transform->Position = glm::vec3(0, 5, 0); //transform->Orientation = glm::angleAxis(0.f, glm::vec3(0, 1, 0)); auto physics = AddComponent(tank); - physics->Mass = 45000; + physics->Mass = 25000; physics->Static = false; auto vehicle = AddComponent(tank); vehicle->MaxTorque = 5200.f; vehicle->MaxSteeringAngle = 90.f; + vehicle->MaxSpeedFullSteeringAngle = 4.f; AddComponent(tank); AddComponent(tank); diff --git a/src/Systems/PhysicsSystem.h b/src/Systems/PhysicsSystem.h index ef7cb70..0ff2bda 100644 --- a/src/Systems/PhysicsSystem.h +++ b/src/Systems/PhysicsSystem.h @@ -73,7 +73,7 @@ public: EntityID entity1 = m_RigidBodies[event.getBody(0)]; EntityID entity2 = m_RigidBodies[event.getBody(1)]; - LOG_INFO("Entities colliding: %i, %i ", entity1, entity2); + //LOG_INFO("Entities colliding: %i, %i ", entity1, entity2); } }; diff --git a/src/Systems/TankSteeringSystem.cpp b/src/Systems/TankSteeringSystem.cpp index dca3441..8a67039 100644 --- a/src/Systems/TankSteeringSystem.cpp +++ b/src/Systems/TankSteeringSystem.cpp @@ -87,6 +87,7 @@ bool Systems::TankSteeringSystem::TankSteeringInputController::OnCommand(const E if (event.Command == "horizontal") { m_Horizontal = val; + m_Vertical = -0.4f; } else if (event.Command == "vertical") { From 02243beceec2a5469965019c313e931e195c6723 Mon Sep 17 00:00:00 2001 From: ViktorLjung Date: Sun, 18 May 2014 23:54:01 +0200 Subject: [PATCH 10/21] Recoil while firing, clamp inputs, merge Helicopter --- src/Events/ApplyPointImpulse.h | 18 +++++++++++++++ src/GameWorld.cpp | 4 ++-- src/InputManager.cpp | 13 +++++++++++ src/Systems/HelicopterSteeringSystem.cpp | 6 ++--- src/Systems/PhysicsSystem.cpp | 12 +++++++++- src/Systems/PhysicsSystem.h | 3 +++ src/Systems/TankSteeringSystem.cpp | 22 ++++++++++++++++++- src/Systems/TankSteeringSystem.h | 3 +++ vs11/Returngeance/Returngeance.vcxproj | 3 ++- .../Returngeance/Returngeance.vcxproj.filters | 8 ++++++- 10 files changed, 83 insertions(+), 9 deletions(-) create mode 100644 src/Events/ApplyPointImpulse.h diff --git a/src/Events/ApplyPointImpulse.h b/src/Events/ApplyPointImpulse.h new file mode 100644 index 0000000..a02745b --- /dev/null +++ b/src/Events/ApplyPointImpulse.h @@ -0,0 +1,18 @@ +#ifndef Events_ApplyPointImpulse_h__ +#define Events_ApplyPointImpulse_h__ +#include "Entity.h" +#include "EventBroker.h" + +namespace Events +{ + + struct ApplyPointImpulse : Event + { + EntityID Entity; + glm::vec3 Position; + glm::vec3 Impulse; + }; + +} + +#endif // Events_ApplyPointImpulse_h__ \ No newline at end of file diff --git a/src/GameWorld.cpp b/src/GameWorld.cpp index fa338f2..f0484be 100755 --- a/src/GameWorld.cpp +++ b/src/GameWorld.cpp @@ -222,7 +222,7 @@ void GameWorld::Initialize() transform->Position = glm::vec3(0, 5, 0); //transform->Orientation = glm::angleAxis(0.f, glm::vec3(0, 1, 0)); auto physics = AddComponent(tank); - physics->Mass = 25000; + physics->Mass = 63000 - 16000; physics->Static = false; auto vehicle = AddComponent(tank); vehicle->MaxTorque = 5200.f; @@ -280,7 +280,7 @@ void GameWorld::Initialize() transform->Scale = glm::vec3(3.f); AddComponent(shot); auto physics = AddComponent(shot); - physics->Mass = 10.f; + physics->Mass = 25.f; physics->Static = false; auto modelComponent = AddComponent(shot); modelComponent->ModelFile = "Models/Placeholders/rocket/Rocket.obj"; diff --git a/src/InputManager.cpp b/src/InputManager.cpp index edbb736..f4e81e2 100644 --- a/src/InputManager.cpp +++ b/src/InputManager.cpp @@ -97,6 +97,19 @@ void InputManager::Update(double dt) dwResult = XInputGetState(i, &state); if (dwResult == 0) { + if(std::abs(state.Gamepad.sThumbLX) <= XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE) + state.Gamepad.sThumbLX = 0; + if(std::abs(state.Gamepad.sThumbLY) <= XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE) + state.Gamepad.sThumbLY = 0; + if(std::abs(state.Gamepad.sThumbRX) <= XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE) + state.Gamepad.sThumbRX = 0; + if(std::abs(state.Gamepad.sThumbRY) <= XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE) + state.Gamepad.sThumbRY = 0; + if(std::abs(state.Gamepad.bLeftTrigger) <= XINPUT_GAMEPAD_TRIGGER_THRESHOLD) + state.Gamepad.bLeftTrigger = 0; + if(std::abs(state.Gamepad.bRightTrigger) <= XINPUT_GAMEPAD_TRIGGER_THRESHOLD) + state.Gamepad.bRightTrigger = 0; + m_CurrentGamepadAxisState[i][static_cast(Gamepad::Axis::LeftX)] = state.Gamepad.sThumbLX / 32767.f; m_CurrentGamepadAxisState[i][static_cast(Gamepad::Axis::LeftY)] = state.Gamepad.sThumbLY / 32767.f; m_CurrentGamepadAxisState[i][static_cast(Gamepad::Axis::RightX)] = state.Gamepad.sThumbRX / 32767.f; diff --git a/src/Systems/HelicopterSteeringSystem.cpp b/src/Systems/HelicopterSteeringSystem.cpp index 7fd9661..94431c2 100644 --- a/src/Systems/HelicopterSteeringSystem.cpp +++ b/src/Systems/HelicopterSteeringSystem.cpp @@ -4,7 +4,7 @@ void Systems::HelicopterSteeringSystem::RegisterComponents(ComponentFactory* cf) { - cf->Register("HelicopterSteering", []() { return new Components::HelicopterSteering(); }); + cf->Register([]() { return new Components::HelicopterSteering(); }); } void Systems::HelicopterSteeringSystem::Initialize() @@ -19,11 +19,11 @@ void Systems::HelicopterSteeringSystem::Update(double dt) void Systems::HelicopterSteeringSystem::UpdateEntity(double dt, EntityID entity, EntityID parent) { - auto transform = m_World->GetComponent(entity, "Transform"); + auto transform = m_World->GetComponent(entity); if (!transform) return; - auto helicopterComponent = m_World->GetComponent(entity, "HelicopterSteering"); + auto helicopterComponent = m_World->GetComponent(entity); if (helicopterComponent) { glm::vec3 controllerRotationEuler = m_InputController->Rotation * (float)dt; diff --git a/src/Systems/PhysicsSystem.cpp b/src/Systems/PhysicsSystem.cpp index c8ccc6d..c8a899b 100644 --- a/src/Systems/PhysicsSystem.cpp +++ b/src/Systems/PhysicsSystem.cpp @@ -35,7 +35,8 @@ void Systems::PhysicsSystem::Initialize() EVENT_SUBSCRIBE_MEMBER(m_ETankSteer, &Systems::PhysicsSystem::OnTankSteer); EVENT_SUBSCRIBE_MEMBER(m_ESetVelocity, &Systems::PhysicsSystem::OnSetVelocity); EVENT_SUBSCRIBE_MEMBER(m_EApplyForce, &Systems::PhysicsSystem::OnApplyForce); - + EVENT_SUBSCRIBE_MEMBER(m_EApplyPointImpulse, &Systems::PhysicsSystem::OnApplyPointImpulse); + hkMemorySystem::FrameInfo finfo(6000 * 1024); // Allocate 6MB of Physics solver buffer hkMemoryRouter* memoryRouter = hkMemoryInitUtil::initDefault(hkMallocAllocator::m_defaultMallocAllocator, finfo); hkBaseSystem::init(memoryRouter, HavokErrorReport); @@ -605,3 +606,12 @@ bool Systems::PhysicsSystem::OnApplyForce(const Events::ApplyForce &event) return true; } + +bool Systems::PhysicsSystem::OnApplyPointImpulse( const Events::ApplyPointImpulse &event ) +{ + m_PhysicsWorld->markForWrite(); + m_RigidBodies[event.Entity]->applyPointImpulse(ConvertPosition(event.Impulse), ConvertPosition(event.Position)); + m_PhysicsWorld->unmarkForWrite(); + + return true; +} diff --git a/src/Systems/PhysicsSystem.h b/src/Systems/PhysicsSystem.h index 2e0230d..ac73310 100644 --- a/src/Systems/PhysicsSystem.h +++ b/src/Systems/PhysicsSystem.h @@ -16,6 +16,7 @@ #include "Events/TankSteer.h" #include "Events/SetVelocity.h" #include "Events/ApplyForce.h" +#include "Events/ApplyPointImpulse.h" #include "OBJ.h" // Math and base include @@ -108,6 +109,8 @@ private: bool OnSetVelocity(const Events::SetVelocity &event); EventRelay m_EApplyForce; bool OnApplyForce(const Events::ApplyForce &event); + EventRelay m_EApplyPointImpulse; + bool OnApplyPointImpulse(const Events::ApplyPointImpulse &event); void SetUpPhysicsState(EntityID entity, EntityID parent); void TearDownPhysicsState(EntityID entity, EntityID parent); diff --git a/src/Systems/TankSteeringSystem.cpp b/src/Systems/TankSteeringSystem.cpp index 8a67039..7755f1e 100644 --- a/src/Systems/TankSteeringSystem.cpp +++ b/src/Systems/TankSteeringSystem.cpp @@ -62,6 +62,17 @@ void Systems::TankSteeringSystem::UpdateEntity(double dt, EntityID entity, Entit e.Velocity = absoluteTransform.Orientation * (glm::vec3(0.f, 0.f, -1.f) * barrelSteeringComponent->ShotSpeed); EventBroker->Publish(e); m_TimeSinceLastShot[entity] = 0; + + + auto clonePhysicsComponent = m_World->GetComponent(clone); + //1,670m/s + //25kg + EntityID baseParent = m_World->GetEntityBaseParent(entity); + Events::ApplyPointImpulse ePointImpulse ; + ePointImpulse.Entity = baseParent; + ePointImpulse.Position = absoluteTransform.Position; + ePointImpulse.Impulse = glm::normalize(absoluteTransform.Orientation * glm::vec3(0, 0, 1)) * clonePhysicsComponent->Mass * 1670.f; + EventBroker->Publish(ePointImpulse); } m_TimeSinceLastShot[entity] += dt; @@ -83,7 +94,16 @@ void Systems::TankSteeringSystem::TowerSteeringInputController::Update( double d bool Systems::TankSteeringSystem::TankSteeringInputController::OnCommand(const Events::InputCommand &event) { - float val = event.Value; + float val; + if(abs(event.Value) < 0.3f) + { + val = 0.f; + } + else + { + val = event.Value; + } + if (event.Command == "horizontal") { m_Horizontal = val; diff --git a/src/Systems/TankSteeringSystem.h b/src/Systems/TankSteeringSystem.h index d264325..65c0bfc 100644 --- a/src/Systems/TankSteeringSystem.h +++ b/src/Systems/TankSteeringSystem.h @@ -3,10 +3,13 @@ #include "System.h" #include "Events/TankSteer.h" #include "Events/SetVelocity.h" +#include "Events/ApplyForce.h" +#include "Events/ApplyPointImpulse.h" #include "Components/Transform.h" #include "Components/TankSteering.h" #include "Components/TowerSteering.h" #include "Components/BarrelSteering.h" +#include "Components/Physics.h" #include "Components/Vehicle.h" #include "Systems/TransformSystem.h" #include "InputController.h" diff --git a/vs11/Returngeance/Returngeance.vcxproj b/vs11/Returngeance/Returngeance.vcxproj index 250e3db..e3cc2e4 100755 --- a/vs11/Returngeance/Returngeance.vcxproj +++ b/vs11/Returngeance/Returngeance.vcxproj @@ -60,7 +60,7 @@ false Default ProgramDatabase - MaxSpeed + Disabled true @@ -156,6 +156,7 @@ + diff --git a/vs11/Returngeance/Returngeance.vcxproj.filters b/vs11/Returngeance/Returngeance.vcxproj.filters index bdab530..3dd634b 100755 --- a/vs11/Returngeance/Returngeance.vcxproj.filters +++ b/vs11/Returngeance/Returngeance.vcxproj.filters @@ -63,7 +63,9 @@ Physics\Systems - + + Gameplay\Vehicles\Helicopter\Systems + @@ -383,6 +385,10 @@ Physics\Events + + + Physics\Events + From f14a83caa8a2c813140bec6494364edd32f3275a Mon Sep 17 00:00:00 2001 From: ViktorLjung Date: Sun, 18 May 2014 23:59:18 +0200 Subject: [PATCH 11/21] Assets --- assets | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/assets b/assets index 9a3fcc2..0a8ad2b 160000 --- a/assets +++ b/assets @@ -1 +1 @@ -Subproject commit 9a3fcc241854a9fa5665ec4885c70adb8f29faa0 +Subproject commit 0a8ad2bc62fd926d1e26eb20094474c59fd278f7 From 539196cd45d133dfbf7ba097ef2cfd00eeefd8f3 Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Mon, 19 May 2014 00:51:40 +0200 Subject: [PATCH 12/21] Viewport stuff --- src/Components/Camera.h | 3 ++- src/Components/Viewport.h | 26 +++++++++++++++++++ src/GameWorld.cpp | 15 +++++++++++ src/GameWorld.h | 1 + vs11/Returngeance/Returngeance.vcxproj | 1 + .../Returngeance/Returngeance.vcxproj.filters | 3 +++ 6 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 src/Components/Viewport.h diff --git a/src/Components/Camera.h b/src/Components/Camera.h index fb3f7ef..a672b85 100755 --- a/src/Components/Camera.h +++ b/src/Components/Camera.h @@ -2,6 +2,7 @@ #define Components_Camera_h__ #include "Component.h" +#include "Entity.h" namespace Components { @@ -13,7 +14,7 @@ struct Camera : Component , NearClip(0.1f) , FarClip(100.f) { } - std::string Viewport; + EntityID Viewport; float FOV; float NearClip; float FarClip; diff --git a/src/Components/Viewport.h b/src/Components/Viewport.h new file mode 100644 index 0000000..18f3126 --- /dev/null +++ b/src/Components/Viewport.h @@ -0,0 +1,26 @@ +#ifndef Components_Viewport_h__ +#define Components_Viewport_h__ + +#include "Component.h" + +namespace Components +{ + +struct Viewport : Component +{ + Viewport() + : Left(0.f) + , Top(0.f) + , Right(1.f) + , Bottom(1.f) { } + + float Left; + float Top; + float Right; + float Bottom; + + virtual Viewport* Clone() const override { return new Viewport(*this); } +}; + +} +#endif // Components_Viewport_h__ \ No newline at end of file diff --git a/src/GameWorld.cpp b/src/GameWorld.cpp index 151ad43..8748113 100755 --- a/src/GameWorld.cpp +++ b/src/GameWorld.cpp @@ -67,6 +67,20 @@ void GameWorld::Initialize() RegisterComponents(); + auto viewport1 = CreateEntity(); + { + auto viewport = AddComponent(viewport1, "Transform"); + viewport->Right = 0.5f; + } + CommitEntity(viewport1); + + auto viewport2 = CreateEntity(); + { + auto viewport = AddComponent(viewport1, "Transform"); + viewport->Left = 0.5f; + } + CommitEntity(viewport2); + { auto camera = CreateEntity(); auto transform = AddComponent(camera, "Transform"); @@ -74,6 +88,7 @@ void GameWorld::Initialize() transform->Position.y = 20.f; //transform->Orientation = glm::quat(glm::vec3(glm::pi() / 8.f, 0.f, 0.f)); auto cameraComp = AddComponent(camera, "Camera"); + cameraComp->Viewport = viewport1; cameraComp->FarClip = 2000.f; auto freeSteering = AddComponent(camera, "FreeSteering"); CommitEntity(camera); diff --git a/src/GameWorld.h b/src/GameWorld.h index 5e990bb..f0c83c8 100755 --- a/src/GameWorld.h +++ b/src/GameWorld.h @@ -28,6 +28,7 @@ #include "Components/Sprite.h" #include "Components/Template.h" #include "Components/Transform.h" +#include "Components/Viewport.h" #include "Components/Physics.h" #include "Components/SphereShape.h" diff --git a/vs11/Returngeance/Returngeance.vcxproj b/vs11/Returngeance/Returngeance.vcxproj index da9dfb3..f3e136e 100755 --- a/vs11/Returngeance/Returngeance.vcxproj +++ b/vs11/Returngeance/Returngeance.vcxproj @@ -147,6 +147,7 @@ + diff --git a/vs11/Returngeance/Returngeance.vcxproj.filters b/vs11/Returngeance/Returngeance.vcxproj.filters index 2a89626..078900d 100755 --- a/vs11/Returngeance/Returngeance.vcxproj.filters +++ b/vs11/Returngeance/Returngeance.vcxproj.filters @@ -352,6 +352,9 @@ Input\Events + + Rendering\Components + From 998ee3cfabd8b69b28694d6cd22a0619e92baa99 Mon Sep 17 00:00:00 2001 From: ViktorLjung Date: Mon, 19 May 2014 00:58:48 +0200 Subject: [PATCH 13/21] Brakes working --- assets | 2 +- src/Components/Wheel.h | 2 +- src/GameWorld.cpp | 18 +++++++++--------- src/Physics/VehicleSetup.cpp | 2 +- src/Systems/PhysicsSystem.cpp | 4 ++++ src/Systems/TankSteeringSystem.cpp | 11 ++--------- 6 files changed, 18 insertions(+), 21 deletions(-) diff --git a/assets b/assets index 0a8ad2b..9d3f71b 160000 --- a/assets +++ b/assets @@ -1 +1 @@ -Subproject commit 0a8ad2bc62fd926d1e26eb20094474c59fd278f7 +Subproject commit 9d3f71bee6e0a930a4bd265eca3f5fceac619709 diff --git a/src/Components/Wheel.h b/src/Components/Wheel.h index be3638b..4a6a66d 100644 --- a/src/Components/Wheel.h +++ b/src/Components/Wheel.h @@ -14,7 +14,7 @@ struct Wheel : Component Wheel() : AxleID(0), Radius(0), Width(0), Mass(0), Steering(false), DownDirection(glm::vec3(0, -1, 0)), Friction(1.5f), SlipAngle(0.0f), - MaxBreakingTorque(1500.0f), ConnectedToHandbrake(false), SuspensionStrength(50.0f), TorqueRatio(0.25f) { } + MaxBreakingTorque(50000.f), ConnectedToHandbrake(false), SuspensionStrength(50.0f), TorqueRatio(0.25f) { } // The Hardpoint MUST be positioned INSIDE the chassis. glm::vec3 Hardpoint; diff --git a/src/GameWorld.cpp b/src/GameWorld.cpp index f0484be..86eb104 100755 --- a/src/GameWorld.cpp +++ b/src/GameWorld.cpp @@ -225,7 +225,7 @@ void GameWorld::Initialize() physics->Mass = 63000 - 16000; physics->Static = false; auto vehicle = AddComponent(tank); - vehicle->MaxTorque = 5200.f; + vehicle->MaxTorque = 8000.f; vehicle->MaxSteeringAngle = 90.f; vehicle->MaxSpeedFullSteeringAngle = 4.f; AddComponent(tank); @@ -350,7 +350,7 @@ void GameWorld::Initialize() Wheel->Radius = 0.6f; Wheel->Steering = true; Wheel->SuspensionStrength = suspensionStrength; - Wheel->Friction = 4.f; + Wheel->Friction = 3.f; Wheel->ConnectedToHandbrake = true; Wheel->TorqueRatio = 0.125f; Wheel->Width = 0.6f; @@ -380,7 +380,7 @@ void GameWorld::Initialize() Wheel->Radius = 0.6f; Wheel->Steering = false; Wheel->SuspensionStrength = suspensionStrength; - Wheel->Friction = 4.f; + Wheel->Friction = 3.f; Wheel->ConnectedToHandbrake = true; Wheel->TorqueRatio = 0.125f; Wheel->Width = 0.6f; @@ -411,7 +411,7 @@ void GameWorld::Initialize() Wheel->Radius = 0.6f; Wheel->Steering = true; Wheel->SuspensionStrength = suspensionStrength; - Wheel->Friction = 4.f; + Wheel->Friction = 3.f; Wheel->ConnectedToHandbrake = true; Wheel->TorqueRatio = 0.125f; Wheel->Width = 0.6f; @@ -441,7 +441,7 @@ void GameWorld::Initialize() Wheel->Radius = 0.6f; Wheel->Steering = true; Wheel->SuspensionStrength = suspensionStrength; - Wheel->Friction = 4.f; + Wheel->Friction = 3.f; Wheel->ConnectedToHandbrake = true; Wheel->TorqueRatio = 0.125f; Wheel->Width = 0.6f; @@ -473,7 +473,7 @@ void GameWorld::Initialize() Wheel->Radius = 0.6f; Wheel->Steering = false; Wheel->SuspensionStrength = suspensionStrength; - Wheel->Friction = 4.f; + Wheel->Friction = 3.f; Wheel->ConnectedToHandbrake = true; Wheel->TorqueRatio = 0.125f; Wheel->Width = 0.6f; @@ -502,7 +502,7 @@ void GameWorld::Initialize() Wheel->Radius = 0.6f; Wheel->Steering = false; Wheel->SuspensionStrength = suspensionStrength; - Wheel->Friction = 4.f; + Wheel->Friction = 3.f; Wheel->ConnectedToHandbrake = true; Wheel->TorqueRatio = 0.125f; Wheel->Width = 0.6f; @@ -557,7 +557,7 @@ void GameWorld::Initialize() Wheel->Radius = 0.6f; Wheel->Steering = false; Wheel->SuspensionStrength = suspensionStrength; - Wheel->Friction = 4.f; + Wheel->Friction = 3.f; Wheel->ConnectedToHandbrake = true; Wheel->TorqueRatio = 0.125f; Wheel->Width = 0.6f; @@ -586,7 +586,7 @@ void GameWorld::Initialize() Wheel->Radius = 0.6f; Wheel->Steering = false; Wheel->SuspensionStrength = suspensionStrength; - Wheel->Friction = 4.f; + Wheel->Friction = 3.f; Wheel->ConnectedToHandbrake = true; Wheel->TorqueRatio = 0.125f; Wheel->Width = 0.6f; diff --git a/src/Physics/VehicleSetup.cpp b/src/Physics/VehicleSetup.cpp index 59c3f07..e319834 100644 --- a/src/Physics/VehicleSetup.cpp +++ b/src/Physics/VehicleSetup.cpp @@ -201,7 +201,7 @@ void VehicleSetup::setupComponent(const hkpVehicleData& data, hkpVehicleDefaultT transmission.m_upshiftRPM = 7000.0f; transmission.m_clutchDelayTime = 0.0f; - transmission.m_reverseGearRatio = 1.2f; + transmission.m_reverseGearRatio = 1.0f; transmission.m_gearsRatio[0] = 3.0f; transmission.m_gearsRatio[1] = 2.25f; transmission.m_gearsRatio[2] = 1.5f; diff --git a/src/Systems/PhysicsSystem.cpp b/src/Systems/PhysicsSystem.cpp index c8a899b..c5aa34a 100644 --- a/src/Systems/PhysicsSystem.cpp +++ b/src/Systems/PhysicsSystem.cpp @@ -582,6 +582,10 @@ bool Systems::PhysicsSystem::OnTankSteer(const Events::TankSteer &event) hkpVehicleDriverInputAnalogStatus* deviceStatus = (hkpVehicleDriverInputAnalogStatus*)m_Vehicles[event.Entity]->m_deviceStatus; deviceStatus->m_positionX = event.PositionX; deviceStatus->m_positionY = event.PositionY; + if(event.PositionY > 0) + { + deviceStatus->m_reverseButtonPressed = true; + } deviceStatus->m_handbrakeButtonPressed = event.Handbrake; m_PhysicsWorld->unmarkForWrite(); } diff --git a/src/Systems/TankSteeringSystem.cpp b/src/Systems/TankSteeringSystem.cpp index 7755f1e..99cd4bd 100644 --- a/src/Systems/TankSteeringSystem.cpp +++ b/src/Systems/TankSteeringSystem.cpp @@ -94,15 +94,8 @@ void Systems::TankSteeringSystem::TowerSteeringInputController::Update( double d bool Systems::TankSteeringSystem::TankSteeringInputController::OnCommand(const Events::InputCommand &event) { - float val; - if(abs(event.Value) < 0.3f) - { - val = 0.f; - } - else - { - val = event.Value; - } + + float val = event.Value; if (event.Command == "horizontal") { From 7455f4b8653e4a5cb55cd707faa8ba3f9ed1bcf3 Mon Sep 17 00:00:00 2001 From: Tleety Date: Mon, 19 May 2014 01:01:46 +0200 Subject: [PATCH 14/21] NormalMaps Working and started work on specular maps. --- src/Model.cpp | 118 +++++++++++++++++++++++++++++++- src/Model.h | 12 +++- src/Renderer.cpp | 17 ++++- src/Renderer.h | 2 + src/Shaders/FinalPass.frag.glsl | 2 +- src/Shaders/Fragment.glsl | 14 +++- src/Shaders/Fragment2.glsl | 1 + src/Shaders/Vertex.glsl | 6 ++ src/Texture.h | 1 + 9 files changed, 167 insertions(+), 6 deletions(-) diff --git a/src/Model.cpp b/src/Model.cpp index 547e940..ae99194 100755 --- a/src/Model.cpp +++ b/src/Model.cpp @@ -22,6 +22,8 @@ Model::Model(OBJ &obj, ResourceManager* rm) auto texture = std::shared_ptr(rm->Load("Texture", currentMaterial->DiffuseTexture.FileName)); // TODO: Load normal map std::shared_ptr normalMap = nullptr; + if (!currentMaterial->NormalMap.FileName.empty()) + normalMap = std::shared_ptr(rm->Load("Texture", currentMaterial->NormalMap.FileName)); // Load specular map std::shared_ptr specularMap = nullptr; if (!currentMaterial->SpecularMap.FileName.empty()) @@ -63,7 +65,9 @@ Model::Model(OBJ &obj, ResourceManager* rm) if (Vertices.size() > 0) { - CreateBuffers(Vertices, Normals, TextureCoords); + CreateTangents(); + getSimilarVertexIndex(); + CreateBuffers(Vertices, Normals, TangentNormals, BiTangentNormals, TextureCoords); } else { @@ -71,7 +75,7 @@ Model::Model(OBJ &obj, ResourceManager* rm) } } -void Model::CreateBuffers( std::vector vertices, std::vector normals, std::vectortextureCoords) +void Model::CreateBuffers( std::vector vertices, std::vector normals, std::vector tangents, std::vector biTangents, std::vectortextureCoords) { LOG_INFO("Generating VertexBuffer"); @@ -100,6 +104,32 @@ void Model::CreateBuffers( std::vector vertices, std::vector 0) + { + glBindBuffer(GL_ARRAY_BUFFER, TangentNormalsBuffer); + glBufferData(GL_ARRAY_BUFFER, tangents.size() * sizeof(glm::vec3), &tangents[0], GL_STATIC_DRAW); + GLERROR("GLEW: BufferFail, TangentNormalsBuffer"); + } + else + { + LOG_WARNING("Created empty tangent buffer!"); + } + + LOG_INFO("Generating BiTangentNormalsBuffer"); + glGenBuffers(1, &BiTangentNormalsBuffer); + if (biTangents.size() > 0) + { + glBindBuffer(GL_ARRAY_BUFFER, BiTangentNormalsBuffer); + glBufferData(GL_ARRAY_BUFFER, biTangents.size() * sizeof(glm::vec3), &biTangents[0], GL_STATIC_DRAW); + GLERROR("GLEW: BufferFail, BiTangentNormalsBuffer"); + } + else + { + LOG_WARNING("Created empty biTangent buffer!"); + } + LOG_INFO("Generating textureCoordBuffer"); glGenBuffers(1, &TextureCoordBuffer); @@ -130,10 +160,94 @@ void Model::CreateBuffers( std::vector vertices, std::vector Normals; + std::vector TangentNormals; + std::vector BiTangentNormals; std::vector TextureCoords; GLuint VertexBuffer; GLuint NormalBuffer; + GLuint TangentNormalsBuffer; + GLuint BiTangentNormalsBuffer; GLuint TextureCoordBuffer; bool Loadobj( @@ -53,9 +57,15 @@ private: void CreateBuffers( std::vector _Vertices, - std::vector _Normals, + std::vector _Normals, + std::vector _Tangents, + std::vector _BiTangents, std::vector_TextureCoords ); + void CreateTangents(); + bool IsNear(float v1, float v2); + void getSimilarVertexIndex(); + }; #endif // Model_h__ \ No newline at end of file diff --git a/src/Renderer.cpp b/src/Renderer.cpp index d78f0b7..c0878f1 100755 --- a/src/Renderer.cpp +++ b/src/Renderer.cpp @@ -17,7 +17,7 @@ Renderer::Renderer() CAtt = 1.0f; LAtt = 0.0f; QAtt = 3.0f; - m_ShadowMapRes = 2048*16; + m_ShadowMapRes = 2048*6; m_SunPosition = glm::vec3(0, 3.5f, 10); m_SunTarget = glm::vec3(0, 0, 0); m_SunProjection = glm::ortho(10.f, -10.f, 10.f, -10.f, 10.f, -10.f); @@ -551,6 +551,15 @@ void Renderer::FrameBufferTextures() glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); + //Generate and bind normal texture + glGenTextures(1, &m_fSpecularTexture); + glBindTexture(GL_TEXTURE_2D, m_fSpecularTexture); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, WIDTH, HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); + /*glGenTextures(1, &m_fShadowTexture); glBindTexture(GL_TEXTURE_2D, m_fShadowTexture); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, WIDTH, HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); @@ -567,6 +576,7 @@ void Renderer::FrameBufferTextures() glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_fDiffuseTexture, 0); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, m_fPositionTexture, 0); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, GL_TEXTURE_2D, m_fNormalsTexture, 0); + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT3, GL_TEXTURE_2D, m_fSpecularTexture, 0); //glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT3, GL_TEXTURE_2D, m_fShadowTexture, 0); GLenum fbStatus = glCheckFramebufferStatus(GL_FRAMEBUFFER); @@ -713,6 +723,11 @@ void Renderer::DrawFBOScene() { glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, *texGroup.Texture); + if (texGroup.NormalMap) + { + glActiveTexture(GL_TEXTURE2); + glBindTexture(GL_TEXTURE_2D, *texGroup.NormalMap); + } glDrawArrays(GL_TRIANGLES, texGroup.StartIndex, texGroup.EndIndex - texGroup.StartIndex + 1); } } diff --git a/src/Renderer.h b/src/Renderer.h index 9053d92..29635b4 100755 --- a/src/Renderer.h +++ b/src/Renderer.h @@ -102,6 +102,7 @@ private: GLuint m_fDiffuseTexture; GLuint m_fPositionTexture; GLuint m_fNormalsTexture; + GLuint m_fSpecularTexture; GLuint m_fBlendTexture; GLuint m_fbLightingPass; GLuint m_fLightingTexture; @@ -142,6 +143,7 @@ private: void BindFragDataLocation(); glm::mat4 CreateLightMatrix(Light &_light); void UpdateSunProjection(); + void CreateNormalMapTangent(); GLuint CreateQuad(); diff --git a/src/Shaders/FinalPass.frag.glsl b/src/Shaders/FinalPass.frag.glsl index 2a6cf4c..0bdb1b1 100644 --- a/src/Shaders/FinalPass.frag.glsl +++ b/src/Shaders/FinalPass.frag.glsl @@ -24,6 +24,6 @@ void main() vec4 _FragmentColor = DiffuseTexel * vec4(La, 1.0) + LightingTexel; FragmentColor = vec4(pow(_FragmentColor.rgb, vec3(1.0 / Gamma)), _FragmentColor.a); - //FragmentColor = ShadowTexel; + //FragmentColor = DiffuseTexel; } \ No newline at end of file diff --git a/src/Shaders/Fragment.glsl b/src/Shaders/Fragment.glsl index cc5370f..87a95fa 100755 --- a/src/Shaders/Fragment.glsl +++ b/src/Shaders/Fragment.glsl @@ -2,6 +2,9 @@ layout (binding=0) uniform sampler2D DiffuseTexture; layout (binding=1) uniform sampler2D ShadowTexture; +layout (binding=2) uniform sampler2D NormalMapTexture; +layout (binding=3) uniform sampler2D SpecularMapTexture; + in VertexData { @@ -9,11 +12,14 @@ in VertexData vec3 Normal; vec2 TextureCoord; vec4 ShadowCoord; + vec3 Tangent; + vec3 BiTangent; } Input; out vec4 frag_Diffuse; out vec4 frag_Position; out vec4 frag_Normal; +out vec4 frag_specular; float Shadow(vec4 ShadowCoord) { @@ -32,6 +38,7 @@ float Shadow(vec4 ShadowCoord) void main() { + // Diffuse Texture frag_Diffuse = texture(DiffuseTexture, Input.TextureCoord) * Shadow(Input.ShadowCoord); @@ -39,5 +46,10 @@ void main() frag_Position = vec4(Input.Position.xyz, 1.0); // G-buffer Normal - frag_Normal = vec4(Input.Normal, 0.0); + mat3 TBN = transpose(mat3(Input.Tangent, Input.BiTangent, Input.Normal)); + frag_Normal = normalize(vec4(TBN * vec3(texture(NormalMapTexture, Input.TextureCoord)), 0.0)); + //frag_Normal = vec4(Input.Normal, 0.0); + + //G-buffer Specular + frag_specular = texture(SpecularMapTexture, Input.TextureCoord); } \ No newline at end of file diff --git a/src/Shaders/Fragment2.glsl b/src/Shaders/Fragment2.glsl index f531f76..5ded0a2 100644 --- a/src/Shaders/Fragment2.glsl +++ b/src/Shaders/Fragment2.glsl @@ -81,4 +81,5 @@ void main() vec4 NormalTexel = texture(NormalsTexture, TextureCoord); FragColor = phong(vec3(PositionTexel), vec3(NormalTexel)); + //FragColor = NormalTexel; } \ No newline at end of file diff --git a/src/Shaders/Vertex.glsl b/src/Shaders/Vertex.glsl index 2f799ef..4c63e0d 100755 --- a/src/Shaders/Vertex.glsl +++ b/src/Shaders/Vertex.glsl @@ -9,6 +9,8 @@ uniform mat4 DepthMVP; layout (location = 0) in vec3 Position; layout (location = 1) in vec3 Normal; layout (location = 2) in vec2 TextureCoord; +layout (location = 3) in vec3 Tangent; +layout (location = 4) in vec3 BiTangent; out VertexData { @@ -16,6 +18,8 @@ out VertexData vec3 Normal; vec2 TextureCoord; vec4 ShadowCoord; + vec3 Tangent; + vec3 BiTangent; } Output; void main() @@ -26,4 +30,6 @@ void main() Output.Normal = normalize(vec3(inverse(transpose(V * M)) * vec4(Normal, 0.0))); Output.TextureCoord = TextureCoord; Output.ShadowCoord = DepthMVP * vec4(Position, 1.0); + Output.Tangent = normalize(vec3(inverse(transpose(V * M)) * vec4(Tangent, 0.0))); + Output.BiTangent = normalize(vec3(inverse(transpose(V * M)) * vec4(BiTangent, 0.0))); } \ No newline at end of file diff --git a/src/Texture.h b/src/Texture.h index c958213..a8c48bb 100755 --- a/src/Texture.h +++ b/src/Texture.h @@ -24,4 +24,5 @@ private: std::unordered_map m_TextureCache; }; + #endif // Texture_h__ From e07a0a2b1a7bdebea3291477dede0293aa5d46e6 Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Mon, 19 May 2014 04:29:30 +0200 Subject: [PATCH 15/21] More viewport stuff --- src/Renderer.cpp | 11 +++++++++++ src/Renderer.h | 11 +++++++++++ src/Systems/RenderSystem.cpp | 8 ++++---- src/Systems/RenderSystem.h | 3 ++- 4 files changed, 28 insertions(+), 5 deletions(-) diff --git a/src/Renderer.cpp b/src/Renderer.cpp index 8e0fe25..9dfe683 100755 --- a/src/Renderer.cpp +++ b/src/Renderer.cpp @@ -868,5 +868,16 @@ void Renderer::UpdateSunProjection() //Pass the bounding box's extents to glOrtho or similar to set up the orthographic projection matrix for the shadow map. } +void Renderer::CreateViewport(int identifier, float left, float top, float right, float bottom) +{ + Viewport v; + v.Left = left; + v.Top = top; + v.Right = right; + v.Bottom = bottom; + m_Viewports[identifier] = v; +} + + diff --git a/src/Renderer.h b/src/Renderer.h index e37d1a8..5bd326b 100755 --- a/src/Renderer.h +++ b/src/Renderer.h @@ -35,6 +35,7 @@ public: void Draw(double dt); void DrawText(); + void CreateViewport(int identifier, float left, float top, float right, float bottom); void AddModelToDraw(Model* model, glm::vec3 position, glm::quat orientation, glm::vec3 scale, bool visible, bool shadowCaster); void AddTextureToDraw(Texture* texture, glm::vec3 position, glm::quat orientation, glm::vec3 scale); void AddTextToDraw(); @@ -67,6 +68,16 @@ public: private: int m_Width, m_Height; + struct Viewport + { + float Left; + float Top; + float Right; + float Bottom; + }; + + std::unordered_map m_Viewports; + struct Light { glm::vec3 Position; diff --git a/src/Systems/RenderSystem.cpp b/src/Systems/RenderSystem.cpp index 4f0e727..ce388c9 100755 --- a/src/Systems/RenderSystem.cpp +++ b/src/Systems/RenderSystem.cpp @@ -2,11 +2,12 @@ #include "RenderSystem.h" #include "World.h" -void Systems::RenderSystem::OnComponentCreated(std::string type, std::shared_ptr component) +void Systems::RenderSystem::OnEntityCommit(EntityID entity) { - if(type == "Model") + auto viewport = m_World->GetComponent(entity, "Viewport"); + if (viewport) { - auto modelComponent = std::static_pointer_cast(component); + m_Renderer->CreateViewport(entity, viewport->Left, viewport->Top, viewport->Right, viewport->Bottom); } } @@ -95,4 +96,3 @@ void Systems::RenderSystem::RegisterResourceTypes(ResourceManager* rm) - diff --git a/src/Systems/RenderSystem.h b/src/Systems/RenderSystem.h index c3cd09a..ab0fa36 100755 --- a/src/Systems/RenderSystem.h +++ b/src/Systems/RenderSystem.h @@ -13,6 +13,7 @@ #include "Components/Sprite.h" #include "Components/PointLight.h" #include "Components/DirectionalLight.h" +#include "Components/Viewport.h" #include "Components/Template.h" #include "Components/Transform.h" @@ -34,7 +35,7 @@ public: std::unordered_map> m_CachedModels; - void OnComponentCreated(std::string type, std:: shared_ptr component) override; + void OnEntityCommit(EntityID entity); void UpdateEntity(double dt, EntityID entity, EntityID parent) override; From bd7ba73bca67fbf502f795384a257212a8eee87d Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Mon, 19 May 2014 17:20:43 +0200 Subject: [PATCH 16/21] Split screen working --- src/Components/Camera.h | 1 - src/Components/Viewport.h | 5 +- src/GameWorld.cpp | 46 +++++++---- src/Renderer.cpp | 150 +++++++++++++++++++++-------------- src/Renderer.h | 12 ++- src/Systems/RenderSystem.cpp | 83 +++++++++++-------- src/Systems/RenderSystem.h | 2 +- 7 files changed, 184 insertions(+), 115 deletions(-) diff --git a/src/Components/Camera.h b/src/Components/Camera.h index a672b85..cd992e8 100755 --- a/src/Components/Camera.h +++ b/src/Components/Camera.h @@ -14,7 +14,6 @@ struct Camera : Component , NearClip(0.1f) , FarClip(100.f) { } - EntityID Viewport; float FOV; float NearClip; float FarClip; diff --git a/src/Components/Viewport.h b/src/Components/Viewport.h index 18f3126..57692a6 100644 --- a/src/Components/Viewport.h +++ b/src/Components/Viewport.h @@ -12,13 +12,16 @@ struct Viewport : Component : Left(0.f) , Top(0.f) , Right(1.f) - , Bottom(1.f) { } + , Bottom(1.f) + , Camera(0) { } float Left; float Top; float Right; float Bottom; + EntityID Camera; + virtual Viewport* Clone() const override { return new Viewport(*this); } }; diff --git a/src/GameWorld.cpp b/src/GameWorld.cpp index 8748113..1daaaa9 100755 --- a/src/GameWorld.cpp +++ b/src/GameWorld.cpp @@ -67,33 +67,32 @@ void GameWorld::Initialize() RegisterComponents(); - auto viewport1 = CreateEntity(); + auto camera = CreateEntity(); { - auto viewport = AddComponent(viewport1, "Transform"); - viewport->Right = 0.5f; - } - CommitEntity(viewport1); - - auto viewport2 = CreateEntity(); - { - auto viewport = AddComponent(viewport1, "Transform"); - viewport->Left = 0.5f; - } - CommitEntity(viewport2); - - { - auto camera = CreateEntity(); auto transform = AddComponent(camera, "Transform"); transform->Position.z = 20.f; transform->Position.y = 20.f; //transform->Orientation = glm::quat(glm::vec3(glm::pi() / 8.f, 0.f, 0.f)); auto cameraComp = AddComponent(camera, "Camera"); - cameraComp->Viewport = viewport1; cameraComp->FarClip = 2000.f; auto freeSteering = AddComponent(camera, "FreeSteering"); - CommitEntity(camera); } + CommitEntity(camera); + auto viewport1 = CreateEntity(); + { + auto viewport = AddComponent(viewport1, "Viewport"); + viewport->Right = 0.5f; + viewport->Camera = camera; + } + CommitEntity(viewport1); + + auto viewport2 = CreateEntity(); + { + auto viewport = AddComponent(viewport2, "Viewport"); + viewport->Left = 0.5f; + } + CommitEntity(viewport2); { auto ground = CreateEntity(); @@ -334,6 +333,19 @@ void GameWorld::Initialize() } CommitEntity(barrel); } + + auto cameraTower = CreateEntity(tower); + { + auto transform = AddComponent(cameraTower, "Transform"); + transform->Position.z = 11.f; + transform->Position.y = 4.f; + //transform->Orientation = glm::quat(glm::vec3(glm::pi() / 8.f, 0.f, 0.f)); + auto cameraComp = AddComponent(cameraTower, "Camera"); + cameraComp->FarClip = 2000.f; + auto freeSteering = AddComponent(cameraTower, "FreeSteering"); + } + CommitEntity(cameraTower); + GetComponent(viewport2, "Viewport")->Camera = cameraTower; } { diff --git a/src/Renderer.cpp b/src/Renderer.cpp index 9dfe683..19c00f0 100755 --- a/src/Renderer.cpp +++ b/src/Renderer.cpp @@ -636,77 +636,90 @@ void Renderer::DrawFBO() { DrawShadowMap(); - /* + for (auto &pair : m_Viewports) + { + Viewport &viewport = pair.second; + if (!viewport.Camera) + continue; + + int x = viewport.Left * m_Width; + int y = viewport.Top * m_Height; + int width = (viewport.Right - viewport.Left) * m_Width; + int height = (viewport.Bottom - viewport.Top) * m_Height; + + /* Base pass - */ - glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_fbBasePass); + */ + glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_fbBasePass); + glViewport(0, 0, m_Width, m_Height); - // Clear G-buffer - GLenum windowBuffClear[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2 }; - glDrawBuffers(3, windowBuffClear); - glClearColor(0.f, 0.f, 0.f, 0.f); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + // Clear G-buffer + GLenum windowBuffClear[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2 }; + glDrawBuffers(3, windowBuffClear); + glClearColor(0.f, 0.f, 0.f, 0.f); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - // Execute the first render stage which will fill out the internal buffers with data(??) - m_FirstPassProgram.Bind(); - GLenum windowBuffOpaque[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2 }; - glDrawBuffers(3, windowBuffOpaque); + // Execute the first render stage which will fill out the internal buffers with data(??) + m_FirstPassProgram.Bind(); + GLenum windowBuffOpaque[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2 }; + glDrawBuffers(3, windowBuffOpaque); - glCullFace(GL_BACK); + glCullFace(GL_BACK); + + DrawFBOScene(viewport); - glViewport(0, 0, m_Width, m_Height); - DrawFBOScene(); - - /* + /* Lighting pass - */ - glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_fbLightingPass); - GLenum lightingPassAttachments[] = { GL_COLOR_ATTACHMENT0 }; - glDrawBuffers(1, lightingPassAttachments); + */ + glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_fbLightingPass); + GLenum lightingPassAttachments[] = { GL_COLOR_ATTACHMENT0 }; + glDrawBuffers(1, lightingPassAttachments); - glClearColor(0.f, 0.f, 0.f, 0.f); - glClear(GL_COLOR_BUFFER_BIT); + glClearColor(0.f, 0.f, 0.f, 0.f); + glClear(GL_COLOR_BUFFER_BIT); - m_SecondPassProgram.Bind(); - glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, m_fPositionTexture); - glActiveTexture(GL_TEXTURE1); - glBindTexture(GL_TEXTURE_2D, m_fNormalsTexture); + m_SecondPassProgram.Bind(); + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, m_fPositionTexture); + glActiveTexture(GL_TEXTURE1); + glBindTexture(GL_TEXTURE_2D, m_fNormalsTexture); - glCullFace(GL_FRONT); - DrawLightScene(); + glCullFace(GL_FRONT); + DrawLightScene(viewport); - /* + /* Final pass - */ - glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + */ + glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); + glViewport(x, y, width, height); + glClear(GL_DEPTH_BUFFER_BIT); - m_FinalPassProgram.Bind(); + m_FinalPassProgram.Bind(); - // Ambient light - glUniform3fv(glGetUniformLocation(m_FinalPassProgram.GetHandle(), "La"), 1, glm::value_ptr(glm::vec3(0.1f, 0.1f, 0.1f))); - glUniform1f(glGetUniformLocation(m_FinalPassProgram.GetHandle(), "Gamma"), Gamma); + // Ambient light + glUniform3fv(glGetUniformLocation(m_FinalPassProgram.GetHandle(), "La"), 1, glm::value_ptr(glm::vec3(0.1f, 0.1f, 0.1f))); + glUniform1f(glGetUniformLocation(m_FinalPassProgram.GetHandle(), "Gamma"), Gamma); - glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, m_fDiffuseTexture); - glActiveTexture(GL_TEXTURE1); - glBindTexture(GL_TEXTURE_2D, m_fLightingTexture); + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, m_fDiffuseTexture); + glActiveTexture(GL_TEXTURE1); + glBindTexture(GL_TEXTURE_2D, m_fLightingTexture); - glCullFace(GL_BACK); - glBindVertexArray(m_ScreenQuad); - glEnableVertexAttribArray(0); - glDrawArrays(GL_TRIANGLES, 0, 6); + glCullFace(GL_BACK); + glBindVertexArray(m_ScreenQuad); + glEnableVertexAttribArray(0); + glDrawArrays(GL_TRIANGLES, 0, 6); + } } -void Renderer::DrawFBOScene() +void Renderer::DrawFBOScene(Viewport &viewport) { // glEnable(GL_DEPTH_TEST);//Tests where objects are and display them correctly // glEnable(GL_CULL_FACE); //removes triangles on the wrong side of the object // glCullFace(GL_BACK); //Make it so that only the back faces are rendered glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); //Draws filled polygons - glm::mat4 cameraMatrix = m_Camera->ProjectionMatrix() * m_Camera->ViewMatrix(); + glm::mat4 cameraMatrix = viewport.Camera->ProjectionMatrix() * viewport.Camera->ViewMatrix(); glm::mat4 MVP; glm::mat4 biasMatrix( 0.5, 0.0, 0.0, 0.0, @@ -737,8 +750,8 @@ void Renderer::DrawFBOScene() 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(m_Camera->ProjectionMatrix())); + glUniformMatrix4fv(glGetUniformLocation(m_FirstPassProgram.GetHandle(), "V"), 1, GL_FALSE, glm::value_ptr(viewport.Camera->ViewMatrix())); + glUniformMatrix4fv(glGetUniformLocation(m_FirstPassProgram.GetHandle(), "P"), 1, GL_FALSE, glm::value_ptr(viewport.Camera->ProjectionMatrix())); glBindVertexArray(model->VAO); for (auto texGroup : model->TextureGroups) { @@ -767,8 +780,8 @@ void Renderer::DrawFBOScene() 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(m_Camera->ProjectionMatrix())); + glUniformMatrix4fv(glGetUniformLocation(m_FirstPassProgram.GetHandle(), "V"), 1, GL_FALSE, glm::value_ptr(viewport.Camera->ViewMatrix())); + glUniformMatrix4fv(glGetUniformLocation(m_FirstPassProgram.GetHandle(), "P"), 1, GL_FALSE, glm::value_ptr(viewport.Camera->ProjectionMatrix())); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, *texture); @@ -779,7 +792,7 @@ void Renderer::DrawFBOScene() -void Renderer::DrawLightScene() +void Renderer::DrawLightScene(Viewport &viewport) { glEnable(GL_BLEND); glBlendEquation (GL_FUNC_ADD); @@ -789,7 +802,7 @@ void Renderer::DrawLightScene() glDepthMask (GL_FALSE); glBindVertexArray(m_sphereModel->VAO); - glm::mat4 cameraMatrix = m_Camera->ProjectionMatrix() * m_Camera->ViewMatrix(); + glm::mat4 cameraMatrix = viewport.Camera->ProjectionMatrix() * viewport.Camera->ViewMatrix(); glm::mat4 MVP; for (auto &light : Lights) @@ -798,13 +811,13 @@ void Renderer::DrawLightScene() glUniform2fv(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "ViewportSize"), 1,glm::value_ptr(glm::vec2(m_Width, m_Height))); glUniformMatrix4fv(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "MVP"), 1, GL_FALSE, glm::value_ptr(MVP)); - glUniformMatrix4fv(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "V"), 1, GL_FALSE, glm::value_ptr(m_Camera->ViewMatrix())); - glUniformMatrix4fv(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "P"), 1, GL_FALSE, glm::value_ptr(m_Camera->ProjectionMatrix())); + glUniformMatrix4fv(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "V"), 1, GL_FALSE, glm::value_ptr(viewport.Camera->ViewMatrix())); + glUniformMatrix4fv(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "P"), 1, GL_FALSE, glm::value_ptr(viewport.Camera->ProjectionMatrix())); glUniformMatrix4fv(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "M"), 1, GL_FALSE, glm::value_ptr(light.SphereModelMatrix)); glUniform3fv(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "ls"), 1, glm::value_ptr(light.Specular)); glUniform3fv(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "ld"), 1, glm::value_ptr(light.Diffuse)); glUniform3fv(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "lp"), 1, glm::value_ptr(light.Position)); - glUniform3f(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "CameraPosition"), m_Camera->Position().x, m_Camera->Position().y, m_Camera->Position().z); + glUniform3f(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "CameraPosition"), viewport.Camera->Position().x, viewport.Camera->Position().y, viewport.Camera->Position().z); glUniform1f(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "specularExponent"), light.SpecularExponent); // glUniform1f(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "ConstantAttenuation"), light.ConstantAttenuation); // glUniform1f(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "LinearAttenuation"), light.LinearAttenuation); @@ -868,16 +881,35 @@ void Renderer::UpdateSunProjection() //Pass the bounding box's extents to glOrtho or similar to set up the orthographic projection matrix for the shadow map. } -void Renderer::CreateViewport(int identifier, float left, float top, float right, float bottom) +void Renderer::RegisterViewport(int identifier, float left, float top, float right, float bottom) { Viewport v; v.Left = left; v.Top = top; v.Right = right; v.Bottom = bottom; + v.Camera = nullptr; m_Viewports[identifier] = v; } +void Renderer::RegisterCamera(int identifier, float FOV, float nearClip, float farClip) +{ + m_Cameras[identifier] = std::make_shared(FOV, (float)m_Width / m_Height, nearClip, farClip); +} +void Renderer::UpdateViewport(int viewportIdentifier, int cameraIdentifier) +{ + auto &viewport = m_Viewports[viewportIdentifier]; + auto camera = m_Cameras[cameraIdentifier]; + camera->AspectRatio(((viewport.Right - viewport.Left) * m_Width) / ((viewport.Bottom - viewport.Top) * m_Height)); + viewport.Camera = camera; +} - +void Renderer::UpdateCamera(int cameraIdentifier, glm::vec3 position, glm::quat orientation, float FOV, float nearClip, float farClip) +{ + m_Cameras[cameraIdentifier]->Position(position); + m_Cameras[cameraIdentifier]->Orientation(orientation); + m_Cameras[cameraIdentifier]->FOV(FOV); + m_Cameras[cameraIdentifier]->NearClip(nearClip); + m_Cameras[cameraIdentifier]->FarClip(farClip); +} diff --git a/src/Renderer.h b/src/Renderer.h index 5bd326b..3bfa5ce 100755 --- a/src/Renderer.h +++ b/src/Renderer.h @@ -35,7 +35,11 @@ public: void Draw(double dt); void DrawText(); - void CreateViewport(int identifier, float left, float top, float right, float bottom); + void RegisterViewport(int identifier, float left, float top, float right, float bottom); + void RegisterCamera(int identifier, float FOV, float nearClip, float farClip); + void UpdateViewport(int viewportIdentifier, int cameraIdentifier); + void UpdateCamera(int cameraIdentifier, glm::vec3 position, glm::quat orientation, float FOV, float nearClip, float farClip); + void AddModelToDraw(Model* model, glm::vec3 position, glm::quat orientation, glm::vec3 scale, bool visible, bool shadowCaster); void AddTextureToDraw(Texture* texture, glm::vec3 position, glm::quat orientation, glm::vec3 scale); void AddTextToDraw(); @@ -74,9 +78,11 @@ private: float Top; float Right; float Bottom; + std::shared_ptr Camera; }; std::unordered_map m_Viewports; + std::unordered_map> m_Cameras; struct Light { @@ -152,8 +158,8 @@ private: void CreateShadowMap(int resolution); void FrameBufferTextures(); void DrawFBO(); - void DrawFBOScene(); - void DrawLightScene(); + void DrawFBOScene(Viewport &viewport); + void DrawLightScene(Viewport &viewport); void BindFragDataLocation(); glm::mat4 CreateLightMatrix(Light &_light); void UpdateSunProjection(); diff --git a/src/Systems/RenderSystem.cpp b/src/Systems/RenderSystem.cpp index ce388c9..c86849a 100755 --- a/src/Systems/RenderSystem.cpp +++ b/src/Systems/RenderSystem.cpp @@ -2,27 +2,55 @@ #include "RenderSystem.h" #include "World.h" +void Systems::RenderSystem::RegisterResourceTypes(ResourceManager* rm) +{ + rm->RegisterType("Model", [rm](std::string resourceName) { return new Model(rm, *rm->Load("OBJ", resourceName)); }); + rm->RegisterType("OBJ", [](std::string resourceName) { return new OBJ(resourceName); }); + rm->RegisterType("Texture", [](std::string resourceName) { return new Texture(resourceName); }); +} + +void Systems::RenderSystem::RegisterComponents(ComponentFactory* cf) +{ + cf->Register("Camera", []() { return new Components::Camera(); }); + cf->Register("Model", []() { return new Components::Model(); }); + cf->Register("Sprite", []() { return new Components::Sprite(); }); + cf->Register("PointLight", []() { return new Components::PointLight(); }); + cf->Register("DirectionalLight", []() { return new Components::DirectionalLight(); }); + cf->Register("Viewport", []() { return new Components::Viewport(); }); +} + void Systems::RenderSystem::OnEntityCommit(EntityID entity) { + auto transform = m_World->GetComponent(entity, "Transform"); + + auto camera = m_World->GetComponent(entity, "Camera"); + if (transform && camera) + { + m_Renderer->RegisterCamera(entity, camera->FOV, camera->NearClip, camera->FarClip); + m_Renderer->UpdateCamera(entity, m_TransformSystem->AbsolutePosition(entity), m_TransformSystem->AbsoluteOrientation(entity), camera->FOV, camera->NearClip, camera->FarClip); + } + auto viewport = m_World->GetComponent(entity, "Viewport"); if (viewport) { - m_Renderer->CreateViewport(entity, viewport->Left, viewport->Top, viewport->Right, viewport->Bottom); + m_Renderer->RegisterViewport(entity, viewport->Left, viewport->Top, viewport->Right, viewport->Bottom); + if (viewport->Camera != 0) + { + m_Renderer->UpdateViewport(entity, viewport->Camera); + } } } void Systems::RenderSystem::UpdateEntity(double dt, EntityID entity, EntityID parent) { auto transformComponent = m_World->GetComponent(entity, "Transform"); - if (transformComponent == nullptr) - return; // Draw models auto modelComponent = m_World->GetComponent(entity, "Model"); - if (modelComponent != nullptr) + if (transformComponent&& modelComponent) { auto model = m_World->GetResourceManager()->Load("Model", modelComponent->ModelFile); - if (model != nullptr) + if (model) { /*glm::vec3 position = m_TransformSystem->AbsolutePosition(entity); glm::quat orientation = m_TransformSystem->AbsoluteOrientation(entity); @@ -33,7 +61,7 @@ void Systems::RenderSystem::UpdateEntity(double dt, EntityID entity, EntityID pa } auto pointLightComponent = m_World->GetComponent(entity, "PointLight"); - if (pointLightComponent != nullptr) + if (transformComponent && pointLightComponent) { glm::vec3 position = m_TransformSystem->AbsolutePosition(entity); m_Renderer->AddPointLightToDraw( @@ -48,18 +76,27 @@ void Systems::RenderSystem::UpdateEntity(double dt, EntityID entity, EntityID pa } auto cameraComponent = m_World->GetComponent(entity, "Camera"); - if (cameraComponent != nullptr) + if (transformComponent && cameraComponent) { - m_Renderer->GetCamera()->Position(m_TransformSystem->AbsolutePosition(entity)); - m_Renderer->GetCamera()->Orientation(m_TransformSystem->AbsoluteOrientation(entity)); + m_Renderer->UpdateCamera(entity + , m_TransformSystem->AbsolutePosition(entity) + , m_TransformSystem->AbsoluteOrientation(entity) + , cameraComponent->FOV + , cameraComponent->NearClip + , cameraComponent->FarClip); + } - m_Renderer->GetCamera()->FOV(cameraComponent->FOV); - m_Renderer->GetCamera()->NearClip(cameraComponent->NearClip); - m_Renderer->GetCamera()->FarClip(cameraComponent->FarClip); + auto viewportComponent = m_World->GetComponent(entity, "Viewport"); + if (viewportComponent) + { + if (viewportComponent->Camera != 0) + { + m_Renderer->UpdateViewport(entity, viewportComponent->Camera); + } } auto spriteComponent = m_World->GetComponent(entity, "Sprite"); - if(spriteComponent != nullptr) + if (transformComponent && spriteComponent) { //TEMP Texture* texture = m_World->GetResourceManager()->Load("Texture", spriteComponent->SpriteFile); @@ -76,23 +113,3 @@ void Systems::RenderSystem::Initialize() m_Renderer->SetSphereModel(m_World->GetResourceManager()->Load("Model", "Models/Placeholders/PhysicsTest/Sphere.obj")); } - -void Systems::RenderSystem::RegisterComponents(ComponentFactory* cf) -{ - cf->Register("Camera", []() { return new Components::Camera(); }); - cf->Register("Model", []() { return new Components::Model(); }); - cf->Register("Sprite", []() { return new Components::Sprite(); }); - cf->Register("PointLight", []() { return new Components::PointLight(); }); - cf->Register("DirectionalLight", []() { return new Components::DirectionalLight(); }); -} - -void Systems::RenderSystem::RegisterResourceTypes(ResourceManager* rm) -{ - rm->RegisterType("Model", [rm](std::string resourceName) { return new Model(rm, *rm->Load("OBJ", resourceName)); }); - rm->RegisterType("OBJ", [](std::string resourceName) { return new OBJ(resourceName); }); - rm->RegisterType("Texture", [](std::string resourceName) { return new Texture(resourceName); }); -} - - - - diff --git a/src/Systems/RenderSystem.h b/src/Systems/RenderSystem.h index ab0fa36..6268ed6 100755 --- a/src/Systems/RenderSystem.h +++ b/src/Systems/RenderSystem.h @@ -35,7 +35,7 @@ public: std::unordered_map> m_CachedModels; - void OnEntityCommit(EntityID entity); + void OnEntityCommit(EntityID entity) override; void UpdateEntity(double dt, EntityID entity, EntityID parent) override; From 7f77db97a0466607094e7ba410a915966ecf8519 Mon Sep 17 00:00:00 2001 From: ViktorLjung Date: Mon, 19 May 2014 19:59:17 +0200 Subject: [PATCH 17/21] Convert functions working in debug --- src/Systems/PhysicsSystem.cpp | 85 ++++++++++------------------------- src/Systems/PhysicsSystem.h | 20 ++++----- 2 files changed, 34 insertions(+), 71 deletions(-) diff --git a/src/Systems/PhysicsSystem.cpp b/src/Systems/PhysicsSystem.cpp index c5aa34a..f4de197 100644 --- a/src/Systems/PhysicsSystem.cpp +++ b/src/Systems/PhysicsSystem.cpp @@ -146,13 +146,13 @@ void Systems::PhysicsSystem::Update(double dt) if (parent) { auto absoluteTransform = m_World->GetSystem()->AbsoluteTransform(entity); - position = ConvertPosition(absoluteTransform.Position); - rotation = ConvertRotation(absoluteTransform.Orientation); + position = GLMVEC3_TO_HKVECTOR4(absoluteTransform.Position); + rotation = GLMQUAT_TO_HKQUATERNION(absoluteTransform.Orientation); } else { - position = ConvertPosition(transformComponent->Position); - rotation = ConvertRotation(transformComponent->Orientation); + position = GLMVEC3_TO_HKVECTOR4(transformComponent->Position); + rotation = GLMQUAT_TO_HKQUATERNION(transformComponent->Orientation); } m_PhysicsWorld->markForWrite(); m_RigidBodies[entity]->setPositionAndRotation(position, rotation); @@ -166,8 +166,8 @@ void Systems::PhysicsSystem::Update(double dt) m_Accumulator += dt; while (m_Accumulator >= timestep) { - hkpStepResult stepresult = m_PhysicsWorld->stepMultithreaded(m_JobQueue, m_ThreadPool, timestep); - //m_PhysicsWorld->stepDeltaTime(timestep); + //hkpStepResult stepresult = m_PhysicsWorld->stepMultithreaded(m_JobQueue, m_ThreadPool, timestep); + m_PhysicsWorld->stepDeltaTime(timestep); m_Accumulator -= timestep; @@ -204,7 +204,7 @@ void Systems::PhysicsSystem::UpdateEntity(double dt, EntityID entity, EntityID p hkQuaternion steeringOrientation = m_Vehicles[car]->m_wheelsInfo[wheelComponent->ID].m_steeringOrientationChassisSpace; hkReal spinAngle = -m_Vehicles[car]->m_wheelsInfo[wheelComponent->ID].m_spinAngle; - glm::quat orientation = ConvertRotation(steeringOrientation) * glm::angleAxis(spinAngle, glm::vec3(1, 0, 0)); + glm::quat orientation = HKQUATERNION_TO_GLMQUAT(steeringOrientation) * glm::angleAxis(spinAngle, glm::vec3(1, 0, 0)); transformComponent->Orientation = orientation * wheelComponent->OriginalOrientation; m_PhysicsWorld->unmarkForWrite(); } @@ -213,8 +213,8 @@ void Systems::PhysicsSystem::UpdateEntity(double dt, EntityID entity, EntityID p { auto transformComponentParent = m_World->GetComponent(parent); - transformComponent->Position = ConvertPosition(m_RigidBodies[entity]->getPosition()); - transformComponent->Orientation = ConvertRotation(m_RigidBodies[entity]->getRotation()); + transformComponent->Position = HKVECTOR4_TO_GLMVEC3(m_RigidBodies[entity]->getPosition()); + transformComponent->Orientation = HKQUATERNION_TO_GLMQUAT(m_RigidBodies[entity]->getRotation()); // TODO: No support for Scale, MIGHT be possible if (transformComponentParent) @@ -277,13 +277,9 @@ void Systems::PhysicsSystem::OnEntityCommit( EntityID entity ) hkpListShape* listShape = new hkpListShape(shapeArray.begin(), shapeArray.getSize(), hkpShapeContainer::REFERENCE_POLICY_INCREMENT); // Save the listShape for further use m_ListShapes[entity] = listShape; - shape = listShape; - - ////////////////////////////////// - //******************************// - // Add a hkpBvShape // - //******************************// - ////////////////////////////////// + //shape = listShape; + hkpBoxShape* box = new hkpBoxShape(listShape->m_aabbHalfExtents, 0.0f); + shape = new hkpBvShape(listShape, box); // Clean up for less memory usage m_Shapes.erase(entity); @@ -296,8 +292,8 @@ void Systems::PhysicsSystem::OnEntityCommit( EntityID entity ) rigidBodyInfo.m_shape = shape; rigidBodyInfo.m_motionType = hkpMotion::MOTION_DYNAMIC; auto absoluteTransform = m_World->GetSystem()->AbsoluteTransform(entity); - hkVector4 position = ConvertPosition(absoluteTransform.Position); - hkQuaternion rotation = ConvertRotation(absoluteTransform.Orientation); + hkVector4 position = GLMVEC3_TO_HKVECTOR4(absoluteTransform.Position); + hkQuaternion rotation = GLMQUAT_TO_HKQUATERNION(absoluteTransform.Orientation); rigidBodyInfo.m_position.set(position(0), position(1), position(2), position(3)); rigidBodyInfo.m_rotation.set(rotation(0), rotation(1), rotation(2), rotation(3)); @@ -366,9 +362,9 @@ void Systems::PhysicsSystem::OnEntityCommit( EntityID entity ) auto childTransformComponent = m_World->GetComponent(shapeData.Entity); - hkVector4 position = ConvertPosition(childTransformComponent->Position); - hkQuaternion rotation = ConvertRotation(childTransformComponent->Orientation); - hkVector4 scale = ConvertScale(childTransformComponent->Scale); + hkVector4 position = GLMVEC3_TO_HKVECTOR4(childTransformComponent->Position); + hkQuaternion rotation = GLMQUAT_TO_HKQUATERNION(childTransformComponent->Orientation); + hkVector4 scale = GLMVEC3_TO_HKVECTOR4(childTransformComponent->Scale); hkQsTransform transform(position, rotation, scale); staticCompoundShape->addInstance(shapeData.Shape, transform); @@ -386,8 +382,8 @@ void Systems::PhysicsSystem::OnEntityCommit( EntityID entity ) rigidBodyInfo.m_shape = shape; rigidBodyInfo.m_motionType = hkpMotion::MOTION_FIXED; auto absoluteTransform = m_World->GetSystem()->AbsoluteTransform(entity); - hkVector4 position = ConvertPosition(absoluteTransform.Position); - hkQuaternion rotation = ConvertRotation(absoluteTransform.Orientation); + hkVector4 position = GLMVEC3_TO_HKVECTOR4(absoluteTransform.Position); + hkQuaternion rotation = GLMQUAT_TO_HKQUATERNION(absoluteTransform.Orientation); rigidBodyInfo.m_position.set(position(0), position(1), position(2), position(3)); rigidBodyInfo.m_rotation.set(rotation(0), rotation(1), rotation(2), rotation(3)); @@ -419,7 +415,7 @@ void Systems::PhysicsSystem::OnEntityCommit( EntityID entity ) { hkpSphereShape* sphereShape = new hkpSphereShape(sphereComponent->Radius); - hkQsTransform transform( ConvertPosition(transformComponent->Position), ConvertRotation(transformComponent->Orientation), ConvertScale(transformComponent->Scale)); + hkQsTransform transform( GLMVEC3_TO_HKVECTOR4(transformComponent->Position), GLMQUAT_TO_HKQUATERNION(transformComponent->Orientation), GLMVEC3_TO_HKVECTOR4(transformComponent->Scale)); hkpConvexTransformShape* transformedSphereShape = new hkpConvexTransformShape( sphereShape, transform ); m_Shapes[entityParent].push_back(ShapeArrayData(entity, transformedSphereShape)); @@ -432,7 +428,7 @@ void Systems::PhysicsSystem::OnEntityCommit( EntityID entity ) hkReal thickness = 0.05; hkpBoxShape* boxShape = new hkpBoxShape(hkVector4(boxComponent->Width- thickness, boxComponent->Height -thickness, boxComponent->Depth - thickness), thickness); - hkQsTransform transform( ConvertPosition(transformComponent->Position), ConvertRotation(transformComponent->Orientation), ConvertScale(transformComponent->Scale)); + hkQsTransform transform( GLMVEC3_TO_HKVECTOR4(transformComponent->Position), GLMQUAT_TO_HKQUATERNION(transformComponent->Orientation), GLMVEC3_TO_HKVECTOR4(transformComponent->Scale)); hkpConvexTransformShape* transformedBoxShape = new hkpConvexTransformShape( boxShape, transform ); m_Shapes[entityParent].push_back(ShapeArrayData(entity, transformedBoxShape)); boxShape->removeReference(); @@ -540,39 +536,6 @@ void HK_CALL Systems::PhysicsSystem::HavokErrorReport(const char* msg, void*) LOG_INFO("%s", msg); } - -glm::vec3 Systems::PhysicsSystem::ConvertPosition(const hkVector4 &hkPosition) -{ - return glm::vec3(hkPosition(0), hkPosition(1), hkPosition(2)); -} - -const hkVector4& Systems::PhysicsSystem::ConvertPosition(glm::vec3 glmPosition) -{ - return hkVector4( glmPosition.x, glmPosition.y, glmPosition.z); -} - -glm::quat Systems::PhysicsSystem::ConvertRotation(const hkQuaternion &hkRotation) -{ - return glm::quat(hkRotation(3), hkRotation(0), hkRotation(1), hkRotation(2)); -} - -const hkQuaternion& Systems::PhysicsSystem::ConvertRotation(glm::quat glmRotation) -{ - hkQuaternion quat = hkQuaternion(glmRotation.x, glmRotation.y, glmRotation.z, glmRotation.w); - quat.normalize(); - return quat; -} - -glm::vec3 Systems::PhysicsSystem::ConvertScale(const hkVector4 &hkScale) -{ - return glm::vec3(hkScale(0), hkScale(1), hkScale(2)); -} - -const hkVector4& Systems::PhysicsSystem::ConvertScale(glm::vec3 glmScale) -{ - return hkVector4(glmScale.x, glmScale.y, glmScale.z); -} - bool Systems::PhysicsSystem::OnTankSteer(const Events::TankSteer &event) { auto vehicleComponent = m_World->GetComponent(event.Entity); @@ -596,7 +559,7 @@ bool Systems::PhysicsSystem::OnTankSteer(const Events::TankSteer &event) bool Systems::PhysicsSystem::OnSetVelocity( const Events::SetVelocity &event ) { m_PhysicsWorld->markForWrite(); - m_RigidBodies[event.Entity]->setLinearVelocity(ConvertPosition(event.Velocity)); + m_RigidBodies[event.Entity]->setLinearVelocity(GLMVEC3_TO_HKVECTOR4(event.Velocity)); m_PhysicsWorld->unmarkForWrite(); return true; @@ -605,7 +568,7 @@ bool Systems::PhysicsSystem::OnSetVelocity( const Events::SetVelocity &event ) bool Systems::PhysicsSystem::OnApplyForce(const Events::ApplyForce &event) { m_PhysicsWorld->markForWrite(); - m_RigidBodies[event.Entity]->applyForce(event.DeltaTime, ConvertPosition(event.Force)); + m_RigidBodies[event.Entity]->applyForce(event.DeltaTime, GLMVEC3_TO_HKVECTOR4(event.Force)); m_PhysicsWorld->unmarkForWrite(); return true; @@ -614,7 +577,7 @@ bool Systems::PhysicsSystem::OnApplyForce(const Events::ApplyForce &event) bool Systems::PhysicsSystem::OnApplyPointImpulse( const Events::ApplyPointImpulse &event ) { m_PhysicsWorld->markForWrite(); - m_RigidBodies[event.Entity]->applyPointImpulse(ConvertPosition(event.Impulse), ConvertPosition(event.Position)); + m_RigidBodies[event.Entity]->applyPointImpulse(GLMVEC3_TO_HKVECTOR4(event.Impulse), GLMVEC3_TO_HKVECTOR4(event.Position)); m_PhysicsWorld->unmarkForWrite(); return true; diff --git a/src/Systems/PhysicsSystem.h b/src/Systems/PhysicsSystem.h index ac73310..54e61f7 100644 --- a/src/Systems/PhysicsSystem.h +++ b/src/Systems/PhysicsSystem.h @@ -1,6 +1,16 @@ #ifndef PhysicsSystem_h__ #define PhysicsSystem_h__ +#define HKVECTOR4_TO_GLMVEC3(hkvec) \ + glm::vec3(hkvec(0), hkvec(1), hkvec(2)) +#define GLMVEC3_TO_HKVECTOR4(glmvec) \ + hkVector4(glmvec.x, glmvec.y, glmvec.z) + +#define HKQUATERNION_TO_GLMQUAT(gkquat) \ + glm::quat(gkquat(3), gkquat(0), gkquat(1), gkquat(2)) +#define GLMQUAT_TO_HKQUATERNION(glmquat) \ + hkQuaternion(glmquat.x, glmquat.y, glmquat.z, glmquat.w) + #include "System.h" #include "Systems/TransformSystem.h" #include "Components/Transform.h" @@ -121,16 +131,6 @@ private: static void HK_CALL HavokErrorReport(const char* msg, void*); void SetupPhysics(hkpWorld* physicsWorld); - // Converterfunctions - glm::vec3 ConvertPosition(const hkVector4 &hkPosition); - const hkVector4& ConvertPosition(glm::vec3 glmPosition); - - glm::quat ConvertRotation(const hkQuaternion &hkRotation); - const hkQuaternion& ConvertRotation(glm::quat glmRotation); - - glm::vec3 ConvertScale(const hkVector4 &hkScale); - const hkVector4&ConvertScale(glm::vec3 glmScale); - std::unordered_map m_RigidBodies; hkJobThreadPool* m_ThreadPool; From ec74d700be55565beac8aa27dca5e96dc2b42d10 Mon Sep 17 00:00:00 2001 From: ViktorLjung Date: Mon, 19 May 2014 21:57:13 +0200 Subject: [PATCH 18/21] fix --- src/Systems/PhysicsSystem.cpp | 9 ++------- src/Systems/TankSteeringSystem.cpp | 11 +++++------ 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/src/Systems/PhysicsSystem.cpp b/src/Systems/PhysicsSystem.cpp index f4de197..03aade8 100644 --- a/src/Systems/PhysicsSystem.cpp +++ b/src/Systems/PhysicsSystem.cpp @@ -166,8 +166,8 @@ void Systems::PhysicsSystem::Update(double dt) m_Accumulator += dt; while (m_Accumulator >= timestep) { - //hkpStepResult stepresult = m_PhysicsWorld->stepMultithreaded(m_JobQueue, m_ThreadPool, timestep); - m_PhysicsWorld->stepDeltaTime(timestep); + m_PhysicsWorld->stepMultithreaded(m_JobQueue, m_ThreadPool, timestep); + //m_PhysicsWorld->stepDeltaTime(timestep); m_Accumulator -= timestep; @@ -541,7 +541,6 @@ bool Systems::PhysicsSystem::OnTankSteer(const Events::TankSteer &event) auto vehicleComponent = m_World->GetComponent(event.Entity); if (vehicleComponent && m_Vehicles.find(event.Entity) != m_Vehicles.end() && m_RigidBodies.find(event.Entity) != m_RigidBodies.end()) { - m_PhysicsWorld->markForWrite(); hkpVehicleDriverInputAnalogStatus* deviceStatus = (hkpVehicleDriverInputAnalogStatus*)m_Vehicles[event.Entity]->m_deviceStatus; deviceStatus->m_positionX = event.PositionX; deviceStatus->m_positionY = event.PositionY; @@ -550,7 +549,6 @@ bool Systems::PhysicsSystem::OnTankSteer(const Events::TankSteer &event) deviceStatus->m_reverseButtonPressed = true; } deviceStatus->m_handbrakeButtonPressed = event.Handbrake; - m_PhysicsWorld->unmarkForWrite(); } return true; @@ -561,7 +559,6 @@ bool Systems::PhysicsSystem::OnSetVelocity( const Events::SetVelocity &event ) m_PhysicsWorld->markForWrite(); m_RigidBodies[event.Entity]->setLinearVelocity(GLMVEC3_TO_HKVECTOR4(event.Velocity)); m_PhysicsWorld->unmarkForWrite(); - return true; } @@ -570,7 +567,6 @@ bool Systems::PhysicsSystem::OnApplyForce(const Events::ApplyForce &event) m_PhysicsWorld->markForWrite(); m_RigidBodies[event.Entity]->applyForce(event.DeltaTime, GLMVEC3_TO_HKVECTOR4(event.Force)); m_PhysicsWorld->unmarkForWrite(); - return true; } @@ -579,6 +575,5 @@ bool Systems::PhysicsSystem::OnApplyPointImpulse( const Events::ApplyPointImpuls m_PhysicsWorld->markForWrite(); m_RigidBodies[event.Entity]->applyPointImpulse(GLMVEC3_TO_HKVECTOR4(event.Impulse), GLMVEC3_TO_HKVECTOR4(event.Position)); m_PhysicsWorld->unmarkForWrite(); - return true; } diff --git a/src/Systems/TankSteeringSystem.cpp b/src/Systems/TankSteeringSystem.cpp index 99cd4bd..11c48fe 100644 --- a/src/Systems/TankSteeringSystem.cpp +++ b/src/Systems/TankSteeringSystem.cpp @@ -24,6 +24,9 @@ void Systems::TankSteeringSystem::Update(double dt) void Systems::TankSteeringSystem::UpdateEntity(double dt, EntityID entity, EntityID parent) { auto tankSteeringComponent = m_World->GetComponent(entity); + auto towerSteeringComponent = m_World->GetComponent(entity); + auto barrelSteeringComponent = m_World->GetComponent(entity); + if(tankSteeringComponent) { Events::TankSteer e; @@ -33,17 +36,13 @@ void Systems::TankSteeringSystem::UpdateEntity(double dt, EntityID entity, Entit e.Handbrake = m_TankInputController->Handbrake; EventBroker->Publish(e); } - - auto towerSteeringComponent = m_World->GetComponent(entity); - if(towerSteeringComponent) + else if(towerSteeringComponent) { auto transformComponent = m_World->GetComponent(entity); glm::quat orientation = glm::angleAxis(towerSteeringComponent->TurnSpeed * m_TowerInputController->TowerDirection * (float)dt, towerSteeringComponent->Axis); transformComponent->Orientation *= orientation; } - - auto barrelSteeringComponent = m_World->GetComponent(entity); - if(barrelSteeringComponent) + else if(barrelSteeringComponent) { auto transformComponent = m_World->GetComponent(entity); auto absoluteTransform = m_World->GetSystem()->AbsoluteTransform(entity); From 609687d6b40086d8a148b3ae187f5a6a4d82076f Mon Sep 17 00:00:00 2001 From: ViktorLjung Date: Tue, 20 May 2014 00:20:09 +0200 Subject: [PATCH 19/21] MULTIPLAYER --- assets | 2 +- src/Components/Input.h | 18 +- src/Components/Player.h | 21 + src/Components/TankSteering.h | 3 + src/Components/TowerSteering.h | 1 - src/GameWorld.cpp | 463 +++++++++++++++++- src/GameWorld.h | 1 + src/Model.cpp | 2 +- src/Systems/InputSystem.cpp | 8 +- src/Systems/PhysicsSystem.cpp | 2 +- src/Systems/RenderSystem.cpp | 6 +- src/Systems/TankSteeringSystem.cpp | 104 ++-- src/Systems/TankSteeringSystem.h | 49 +- vs11/Returngeance/Returngeance.vcxproj | 1 + .../Returngeance/Returngeance.vcxproj.filters | 11 +- 15 files changed, 575 insertions(+), 117 deletions(-) create mode 100644 src/Components/Player.h diff --git a/assets b/assets index 9d3f71b..6cc3858 160000 --- a/assets +++ b/assets @@ -1 +1 @@ -Subproject commit 9d3f71bee6e0a930a4bd265eca3f5fceac619709 +Subproject commit 6cc38589ed77dbb3b9c34c1168d52e0f86e1de46 diff --git a/src/Components/Input.h b/src/Components/Input.h index 15cb286..e3f043a 100755 --- a/src/Components/Input.h +++ b/src/Components/Input.h @@ -1,10 +1,6 @@ #ifndef Components_Input_h__ #define Components_Input_h__ -#include - -#include - #include "Component.h" namespace Components @@ -12,12 +8,14 @@ namespace Components struct Input : Component { - std::array KeyState; - std::array LastKeyState; - std::array MouseState; - std::array LastMouseState; - float dX, dY; - float WheelDelta; + /*Input() + : Keyboard(false) + , Mouse(false) + , GamepadID(0) { } + + bool Keyboard; + bool Mouse; + int GamepadID;*/ virtual Input* Clone() const override { return new Input(*this); } }; diff --git a/src/Components/Player.h b/src/Components/Player.h new file mode 100644 index 0000000..a5e926b --- /dev/null +++ b/src/Components/Player.h @@ -0,0 +1,21 @@ +#ifndef Player_h__ +#define Player_h__ + +#include "Component.h" + +namespace Components +{ + +struct Player : Component +{ + Player() + : ID(0) { } + + int ID; + + virtual Player* Clone() const override { return new Player(*this); } +}; + +} + +#endif // Player_h__ \ No newline at end of file diff --git a/src/Components/TankSteering.h b/src/Components/TankSteering.h index c9184b3..2c4b123 100644 --- a/src/Components/TankSteering.h +++ b/src/Components/TankSteering.h @@ -7,6 +7,9 @@ namespace Components { struct TankSteering : Component { + EntityID Player; + EntityID Turret; + EntityID Barrel; TankSteering* Clone() const override { return new TankSteering(*this); } }; } diff --git a/src/Components/TowerSteering.h b/src/Components/TowerSteering.h index f4f72b9..0b4a6c8 100644 --- a/src/Components/TowerSteering.h +++ b/src/Components/TowerSteering.h @@ -10,7 +10,6 @@ struct TowerSteering : Component { TowerSteering() : TurnSpeed(1.f), Axis(glm::vec3(0,1,0)){ } - float TurnSpeed; glm::vec3 Axis; virtual TowerSteering* Clone() const override { return new TowerSteering(*this); } diff --git a/src/GameWorld.cpp b/src/GameWorld.cpp index 63a6a9f..62fbc40 100755 --- a/src/GameWorld.cpp +++ b/src/GameWorld.cpp @@ -72,15 +72,28 @@ void GameWorld::Initialize() } CommitEntity(viewport2); + auto player1 = CreateEntity(); + { + auto player = AddComponent(player1); + player->ID = 1; + } + + auto player2 = CreateEntity(); + { + auto player = AddComponent(player2); + player->ID = 2; + } + + { auto ground = CreateEntity(); auto transform = AddComponent(ground); - transform->Position = glm::vec3(0, 0, 0); + transform->Position = glm::vec3(0, -50, 0); //transform->Scale = glm::vec3(400.0f, 10.0f, 400.0f); transform->Orientation = glm::quat(glm::vec3(0.0f, 0.0f, 0.0f)); auto model = AddComponent(ground); - //model->ModelFile = "Models/TestScene/testScene.obj"; - model->ModelFile = "Models/Placeholders/Terrain/Terrain2.obj"; + model->ModelFile = "Models/TestScene3/testScene.obj"; + //model->ModelFile = "Models/Placeholders/Terrain/Terrain2.obj"; auto physics = AddComponent(ground); physics->Mass = 10; @@ -90,8 +103,8 @@ void GameWorld::Initialize() auto groundshape = CreateEntity(ground); auto transformshape = AddComponent(groundshape); auto meshShape = AddComponent(groundshape); - meshShape->ResourceName = "Models/Placeholders/Terrain/Terrain2.obj"; - //meshShape->ResourceName = "Models/TestScene/testScene.obj"; + //meshShape->ResourceName = "Models/Placeholders/Terrain/Terrain2.obj"; + meshShape->ResourceName = "Models/TestScene3/testScene.obj"; CommitEntity(groundshape); @@ -239,10 +252,11 @@ void GameWorld::Initialize() physics->Mass = 63000 - 16000; physics->Static = false; auto vehicle = AddComponent(tank); - vehicle->MaxTorque = 8000.f; + vehicle->MaxTorque = 36000.f; vehicle->MaxSteeringAngle = 90.f; vehicle->MaxSpeedFullSteeringAngle = 4.f; - AddComponent(tank); + auto tankSteering = AddComponent(tank); + tankSteering->Player = player1; AddComponent(tank); { @@ -264,24 +278,24 @@ void GameWorld::Initialize() auto transform = AddComponent(chassis); transform->Position = glm::vec3(0, 0, 0); auto model = AddComponent(chassis); - model->ModelFile = "Models/Tank/Fix/Chassi.obj"; + model->ModelFile = "Models/Tank/tankBody.obj"; } { auto tower = CreateEntity(tank); SetProperty(tower, "Name", "tower"); auto transform = AddComponent(tower); - transform->Position = glm::vec3(0.f, 1.2f, 1.8f); + transform->Position = glm::vec3(0.f, 0.68f, 0.9f); auto model = AddComponent(tower); - model->ModelFile = "Models/Tank/Fix/Top.obj"; + model->ModelFile = "Models/Tank/tankTop.obj"; auto towerSteering = AddComponent(tower); towerSteering->Axis = glm::vec3(0.f, 1.f, 0.f); towerSteering->TurnSpeed = glm::pi()/4.f; { auto barrel = CreateEntity(tower); auto transform = AddComponent(barrel); - transform->Position = glm::vec3(-0.018f, -0.2, -1.3f); + transform->Position = glm::vec3(-0.012f, 0.4f, -0.75); auto model = AddComponent(barrel); - model->ModelFile = "Models/Tank/Fix/Barrel.obj"; + model->ModelFile = "Models/Tank/tankBarrel.obj"; auto barrelSteering = AddComponent(barrel); barrelSteering->Axis = glm::vec3(1.f, 0.f, 0.f); barrelSteering->TurnSpeed = glm::pi()/4.f; @@ -312,7 +326,429 @@ void GameWorld::Initialize() barrelSteering->ShotTemplate = shot; } CommitEntity(barrel); + tankSteering->Barrel = barrel; } + CommitEntity(tower); + tankSteering->Turret = tower; + + + auto cameraTower = CreateEntity(tower); + { + auto transform = AddComponent(cameraTower); + transform->Position.z = 11.f; + transform->Position.y = 4.f; + //transform->Orientation = glm::quat(glm::vec3(glm::pi() / 8.f, 0.f, 0.f)); + auto cameraComp = AddComponent(cameraTower); + cameraComp->FarClip = 2000.f; + //auto freeSteering = AddComponent(cameraTower); + } + CommitEntity(cameraTower); + GetComponent(viewport1)->Camera = cameraTower; + } + + { + auto lightentity = CreateEntity(tank); + auto transform = AddComponent(lightentity); + transform->Position = glm::vec3(0, 0, 0); + auto light = AddComponent(lightentity); + //light->Diffuse = glm::vec3(128.f/255.f, 172.f/255.f, 242.f/255.f); + //light->Specular = glm::vec3(1.f); + /*light->ConstantAttenuation = 0.3f; + light->LinearAttenuation = 0.003f; + light->QuadraticAttenuation = 0.002f;*/ + } + +// auto wheelpair = CreateEntity(tank); +// SetProperty(wheelpair, "Name", "WheelPair"); +// AddComponent(wheelpair, "WheelPairThingy"); + + //Create wheels + float wheelOffset = 0.4f; + float springLength = 0.3f; + float suspensionStrength = 15.f; + + { + auto wheel = CreateEntity(tank); + auto transform = AddComponent(wheel); + transform->Position = glm::vec3(1.68f, -0.83f - wheelOffset, -2.6f); + transform->Orientation = glm::angleAxis(glm::pi(), glm::vec3(0, 1, 0)); + auto model = AddComponent(wheel); + model->ModelFile = "Models/Tank/Fix/WheelPhysics.obj"; + auto Wheel = AddComponent(wheel); + Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); + Wheel->AxleID = 0; + Wheel->Mass = 2000; + Wheel->Radius = 0.6f; + Wheel->Steering = true; + Wheel->SuspensionStrength = suspensionStrength; + Wheel->Friction = 3.f; + Wheel->ConnectedToHandbrake = true; + Wheel->TorqueRatio = 0.125f; + Wheel->Width = 0.6f; + { + auto shape = CreateEntity(wheel); + auto shapetransform = AddComponent(shape); + shapetransform->Position = glm::vec3(0.f, 0.32f, 0.f) + transform->Position; + auto boxShape = AddComponent(shape); + boxShape->Width = 0.7f; + boxShape->Height = 0.34f; + boxShape->Depth = 0.7f; + CommitEntity(shape); + } + CommitEntity(wheel); + } + { + auto wheel = CreateEntity(tank); + auto transform = AddComponent(wheel); + transform->Position = glm::vec3(1.68f, -0.83f - wheelOffset, -0.83f); + transform->Orientation = glm::angleAxis(glm::pi(), glm::vec3(0, 1, 0)); + auto model = AddComponent(wheel); + model->ModelFile = "Models/Tank/Fix/WheelPhysics.obj"; + auto Wheel = AddComponent(wheel); + Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); + Wheel->AxleID = 0; + Wheel->Mass = 2000; + Wheel->Radius = 0.6f; + Wheel->Steering = false; + Wheel->SuspensionStrength = suspensionStrength; + Wheel->Friction = 3.f; + Wheel->ConnectedToHandbrake = true; + Wheel->TorqueRatio = 0.125f; + Wheel->Width = 0.6f; + { + auto shape = CreateEntity(wheel); + auto shapetransform = AddComponent(shape); + shapetransform->Position = glm::vec3(0.f, 0.32f, 0.f) + transform->Position; + auto boxShape = AddComponent(shape); + boxShape->Width = 0.7f; + boxShape->Height = 0.34f; + boxShape->Depth = 0.7f; + CommitEntity(shape); + } + CommitEntity(wheel); + } + + { + auto wheel = CreateEntity(tank); + auto transform = AddComponent(wheel); + transform->Position = glm::vec3(-1.68f, -0.83f - wheelOffset, -2.6f); + transform->Orientation = glm::angleAxis(glm::pi(), glm::vec3(0, 1, 0)); + auto model = AddComponent(wheel); + model->ModelFile = "Models/Tank/Fix/WheelPhysics.obj"; + auto Wheel = AddComponent(wheel); + Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); + Wheel->AxleID = 0; + Wheel->Mass = 2000; + Wheel->Radius = 0.6f; + Wheel->Steering = true; + Wheel->SuspensionStrength = suspensionStrength; + Wheel->Friction = 3.f; + Wheel->ConnectedToHandbrake = true; + Wheel->TorqueRatio = 0.125f; + Wheel->Width = 0.6f; + { + auto shape = CreateEntity(wheel); + auto shapetransform = AddComponent(shape); + shapetransform->Position = glm::vec3(0.f, 0.32f, 0.f) + transform->Position; + auto boxShape = AddComponent(shape); + boxShape->Width = 0.7f; + boxShape->Height = 0.34f; + boxShape->Depth = 0.7f; + CommitEntity(shape); + } + CommitEntity(wheel); + } + { + auto wheel = CreateEntity(tank); + auto transform = AddComponent(wheel); + transform->Position = glm::vec3(-1.68f, -0.83f - wheelOffset, -0.83f); + transform->Orientation = glm::angleAxis(glm::pi(), glm::vec3(0, 1, 0)); + auto model = AddComponent(wheel); + model->ModelFile = "Models/Tank/Fix/WheelPhysics.obj"; + auto Wheel = AddComponent(wheel); + Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); + Wheel->AxleID = 0; + Wheel->Mass = 2000; + Wheel->Radius = 0.6f; + Wheel->Steering = true; + Wheel->SuspensionStrength = suspensionStrength; + Wheel->Friction = 3.f; + Wheel->ConnectedToHandbrake = true; + Wheel->TorqueRatio = 0.125f; + Wheel->Width = 0.6f; + { + auto shape = CreateEntity(wheel); + auto shapetransform = AddComponent(shape); + shapetransform->Position = glm::vec3(0.f, 0.32f, 0.f) + transform->Position; + auto boxShape = AddComponent(shape); + boxShape->Width = 0.7f; + boxShape->Height = 0.34f; + boxShape->Depth = 0.7f; + CommitEntity(shape); + } + CommitEntity(wheel); + } + + + //Back + { + auto wheel = CreateEntity(tank); + auto transform = AddComponent(wheel); + transform->Position = glm::vec3(1.68f, -0.83f - wheelOffset, 1.f); + auto model = AddComponent(wheel); + model->ModelFile = "Models/Tank/Fix/WheelPhysics.obj"; + auto Wheel = AddComponent(wheel); + Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); + Wheel->AxleID = 1; + Wheel->Mass = 2000; + Wheel->Radius = 0.6f; + Wheel->Steering = false; + Wheel->SuspensionStrength = suspensionStrength; + Wheel->Friction = 3.f; + Wheel->ConnectedToHandbrake = true; + Wheel->TorqueRatio = 0.125f; + Wheel->Width = 0.6f; + { + auto shape = CreateEntity(wheel); + auto shapetransform = AddComponent(shape); + shapetransform->Position = glm::vec3(0.f, 0.32f, 0.f) + transform->Position; + auto boxShape = AddComponent(shape); + boxShape->Width = 0.7f; + boxShape->Height = 0.34f; + boxShape->Depth = 0.7f; + CommitEntity(shape); + } + CommitEntity(wheel); + } + { + auto wheel = CreateEntity(tank); + auto transform = AddComponent(wheel); + transform->Position = glm::vec3(1.68f, -0.83f - wheelOffset, 2.95f); + auto model = AddComponent(wheel); + model->ModelFile = "Models/Tank/Fix/WheelPhysics.obj"; + auto Wheel = AddComponent(wheel); + Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); + Wheel->AxleID = 1; + Wheel->Mass = 2000; + Wheel->Radius = 0.6f; + Wheel->Steering = false; + Wheel->SuspensionStrength = suspensionStrength; + Wheel->Friction = 3.f; + Wheel->ConnectedToHandbrake = true; + Wheel->TorqueRatio = 0.125f; + Wheel->Width = 0.6f; + { + auto shape = CreateEntity(wheel); + auto shapetransform = AddComponent(shape); + shapetransform->Position = glm::vec3(0.f, 0.32f, 0.f) + transform->Position; + auto boxShape = AddComponent(shape); + boxShape->Width = 0.7f; + boxShape->Height = 0.34f; + boxShape->Depth = 0.7f; + CommitEntity(shape); + } + CommitEntity(wheel); + + auto entity = CreateEntity(tank); + auto transformComponent = AddComponent(entity); + transformComponent->Position = glm::vec3(2,-1.7,2.0); + transformComponent->Scale = glm::vec3(3,3,3); + transformComponent->Orientation = glm::angleAxis(glm::pi()/2, glm::vec3(1,0,0)); + auto emitterComponent = AddComponent(entity); + emitterComponent->SpawnCount = 2; + emitterComponent->SpawnFrequency = 0.005; + emitterComponent->SpreadAngle = glm::pi(); + emitterComponent->UseGoalVelocity = false; + emitterComponent->LifeTime = 0.5; + //emitterComponent->AngularVelocitySpectrum.push_back(glm::pi() / 100); + emitterComponent->ScaleSpectrum.push_back(glm::vec3(0.05)); + CommitEntity(entity); + + auto particleEntity = CreateEntity(entity); + auto TEMP = AddComponent(particleEntity); + TEMP->Scale = glm::vec3(0); + auto spriteComponent = AddComponent(particleEntity); + spriteComponent->SpriteFile = "Models/Textures/Sprites/Dust.png"; + emitterComponent->ParticleTemplate = particleEntity; + + CommitEntity(particleEntity); + } + + { + auto wheel = CreateEntity(tank); + auto transform = AddComponent(wheel); + transform->Position = glm::vec3(-1.68f, -0.83f - wheelOffset, 1.f); + transform->Orientation = glm::angleAxis(glm::pi(), glm::vec3(0, 1, 0)); + auto model = AddComponent(wheel); + model->ModelFile = "Models/Tank/Fix/WheelPhysics.obj"; + auto Wheel = AddComponent(wheel); + Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); + Wheel->AxleID = 1; + Wheel->Mass = 2000; + Wheel->Radius = 0.6f; + Wheel->Steering = false; + Wheel->SuspensionStrength = suspensionStrength; + Wheel->Friction = 3.f; + Wheel->ConnectedToHandbrake = true; + Wheel->TorqueRatio = 0.125f; + Wheel->Width = 0.6f; + { + auto shape = CreateEntity(wheel); + auto shapetransform = AddComponent(shape); + shapetransform->Position = glm::vec3(0.f, 0.32f, 0.f) + transform->Position; + auto boxShape = AddComponent(shape); + boxShape->Width = 0.7f; + boxShape->Height = 0.34f; + boxShape->Depth = 0.7f; + CommitEntity(shape); + } + CommitEntity(wheel); + } + { + auto wheel = CreateEntity(tank); + auto transform = AddComponent(wheel); + transform->Position = glm::vec3(-1.68f, -0.83f - wheelOffset, 2.95f); + auto model = AddComponent(wheel); + model->ModelFile = "Models/Tank/Fix/WheelPhysics.obj"; + auto Wheel = AddComponent(wheel); + Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); + Wheel->AxleID = 1; + Wheel->Mass = 2000; + Wheel->Radius = 0.6f; + Wheel->Steering = false; + Wheel->SuspensionStrength = suspensionStrength; + Wheel->Friction = 3.f; + Wheel->ConnectedToHandbrake = true; + Wheel->TorqueRatio = 0.125f; + Wheel->Width = 0.6f; + { + auto shape = CreateEntity(wheel); + auto shapetransform = AddComponent(shape); + shapetransform->Position = glm::vec3(0.f, 0.32f, 0.f) + transform->Position; + auto boxShape = AddComponent(shape); + boxShape->Width = 0.7f; + boxShape->Height = 0.34f; + boxShape->Depth = 0.7f; + CommitEntity(shape); + } + CommitEntity(wheel); + + auto entity = CreateEntity(tank); + auto transformComponent = AddComponent(entity); + transformComponent->Position = glm::vec3(-2,-1.7,2.0); + transformComponent->Scale = glm::vec3(3,3,3); + transformComponent->Orientation = glm::angleAxis(glm::pi()/2, glm::vec3(1,0,0)); + auto emitterComponent = AddComponent(entity); + emitterComponent->SpawnCount = 2; + emitterComponent->SpawnFrequency = 0.005; + emitterComponent->SpreadAngle = glm::pi(); + emitterComponent->UseGoalVelocity = false; + emitterComponent->LifeTime = 0.5; + //emitterComponent->AngularVelocitySpectrum.push_back(glm::pi() / 100); + emitterComponent->ScaleSpectrum.push_back(glm::vec3(0.05)); + CommitEntity(entity); + + auto particleEntity = CreateEntity(entity); + auto TEMP = AddComponent(particleEntity); + TEMP->Scale = glm::vec3(0); + auto spriteComponent = AddComponent(particleEntity); + spriteComponent->SpriteFile = "Models/Textures/Sprites/Dust.png"; + emitterComponent->ParticleTemplate = particleEntity; + + CommitEntity(particleEntity); + } + + CommitEntity(tank); + } + + { + auto tank = CreateEntity(); + auto transform = AddComponent(tank); + transform->Position = glm::vec3(20, 5, 0); + //transform->Orientation = glm::angleAxis(0.f, glm::vec3(0, 1, 0)); + auto physics = AddComponent(tank); + physics->Mass = 63000 - 16000; + physics->Static = false; + auto vehicle = AddComponent(tank); + vehicle->MaxTorque = 36000.f; + vehicle->MaxSteeringAngle = 90.f; + vehicle->MaxSpeedFullSteeringAngle = 4.f; + auto tankSteering = AddComponent(tank); + tankSteering->Player = player2; + AddComponent(tank); + + { + auto shape = CreateEntity(tank); + auto transform = AddComponent(shape); + auto meshShape = AddComponent(shape); + meshShape->ResourceName = "Models/Tank/Fix/ChassiCollision.obj"; + CommitEntity(shape); + + // auto box = AddComponent(jeep); + // box->Width = 1.487f; + // box->Height = 0.727f; + // box->Depth = 2.594f; + + } + + { + auto chassis = CreateEntity(tank); + auto transform = AddComponent(chassis); + transform->Position = glm::vec3(0, 0, 0); + auto model = AddComponent(chassis); + model->ModelFile = "Models/Tank/tankBody.obj"; + } + { + auto tower = CreateEntity(tank); + SetProperty(tower, "Name", "tower"); + auto transform = AddComponent(tower); + transform->Position = glm::vec3(0.f, 0.68f, 0.9f); + auto model = AddComponent(tower); + model->ModelFile = "Models/Tank/tankTop.obj"; + auto towerSteering = AddComponent(tower); + towerSteering->Axis = glm::vec3(0.f, 1.f, 0.f); + towerSteering->TurnSpeed = glm::pi()/4.f; + { + auto barrel = CreateEntity(tower); + auto transform = AddComponent(barrel); + transform->Position = glm::vec3(-0.012f, 0.4f, -0.75); + auto model = AddComponent(barrel); + model->ModelFile = "Models/Tank/tankBarrel.obj"; + auto barrelSteering = AddComponent(barrel); + barrelSteering->Axis = glm::vec3(1.f, 0.f, 0.f); + barrelSteering->TurnSpeed = glm::pi()/4.f; + barrelSteering->ShotSpeed = 70.f; + { + auto shot = CreateEntity(barrel); + auto transform = AddComponent(shot); + transform->Position = glm::vec3(0.35f, 0.f, -2.f); + transform->Orientation = glm::angleAxis(-glm::pi()/2.f, glm::vec3(1, 0, 0)); + transform->Scale = glm::vec3(3.f); + AddComponent(shot); + auto physics = AddComponent(shot); + physics->Mass = 25.f; + physics->Static = false; + auto modelComponent = AddComponent(shot); + modelComponent->ModelFile = "Models/Placeholders/rocket/Rocket.obj"; + + { + auto shape = CreateEntity(shot); + auto transform = AddComponent(shape); + auto boxShape = AddComponent(shape); + boxShape->Width = 0.5f; + boxShape->Height = 0.5f; + boxShape->Depth = 0.5f; + CommitEntity(shape); + } + CommitEntity(shot); + barrelSteering->ShotTemplate = shot; + } + CommitEntity(barrel); + tankSteering->Barrel = barrel; + } + CommitEntity(tower); + tankSteering->Turret = tower; + auto cameraTower = CreateEntity(tower); { @@ -643,8 +1079,6 @@ void GameWorld::Initialize() CommitEntity(tank); } - - /* for(int i = 0; i < 10; i++) { @@ -744,6 +1178,7 @@ void GameWorld::RegisterComponents() { m_ComponentFactory.Register([]() { return new Components::Transform(); }); m_ComponentFactory.Register([]() { return new Components::Template(); }); + m_ComponentFactory.Register([]() { return new Components::Player(); }); } void GameWorld::RegisterSystems() diff --git a/src/GameWorld.h b/src/GameWorld.h index e4f7403..7ea100f 100755 --- a/src/GameWorld.h +++ b/src/GameWorld.h @@ -40,6 +40,7 @@ #include "Components/TankSteering.h" #include "Components/TowerSteering.h" #include "Components/BarrelSteering.h" +#include "Components/Player.h" class GameWorld : public World { diff --git a/src/Model.cpp b/src/Model.cpp index 4e1f937..c13a192 100755 --- a/src/Model.cpp +++ b/src/Model.cpp @@ -66,7 +66,7 @@ Model::Model(ResourceManager* rm, OBJ &obj) if (Vertices.size() > 0) { CreateTangents(); - getSimilarVertexIndex(); + //getSimilarVertexIndex(); CreateBuffers(Vertices, Normals, TangentNormals, BiTangentNormals, TextureCoords); } else diff --git a/src/Systems/InputSystem.cpp b/src/Systems/InputSystem.cpp index dbb9ba6..2f774d0 100755 --- a/src/Systems/InputSystem.cpp +++ b/src/Systems/InputSystem.cpp @@ -53,7 +53,7 @@ bool Systems::InputSystem::OnKeyDown(const Events::KeyDown &event) float value; std::tie(command, value) = bindingIt->second; m_CommandKeyboardValues[command][event.KeyCode] = value; - PublishCommand(0, command, GetCommandTotalValue(command)); + PublishCommand(1, command, GetCommandTotalValue(command)); } return true; @@ -68,7 +68,7 @@ bool Systems::InputSystem::OnKeyUp(const Events::KeyUp &event) float value; std::tie(command, value) = bindingIt->second; m_CommandKeyboardValues[command][event.KeyCode] = 0; - PublishCommand(0, command, GetCommandTotalValue(command));; + PublishCommand(1, command, GetCommandTotalValue(command));; } return true; @@ -83,7 +83,7 @@ bool Systems::InputSystem::OnMousePress(const Events::MousePress &event) float value; std::tie(command, value) = bindingIt->second; m_CommandMouseButtonValues[command][event.Button] = value; - PublishCommand(0, command, GetCommandTotalValue(command)); + PublishCommand(1, command, GetCommandTotalValue(command)); } return true; @@ -98,7 +98,7 @@ bool Systems::InputSystem::OnMouseRelease(const Events::MouseRelease &event) float value; std::tie(command, value) = bindingIt->second; m_CommandMouseButtonValues[command][event.Button] = 0; - PublishCommand(0, command, GetCommandTotalValue(command)); + PublishCommand(1, command, GetCommandTotalValue(command)); } return true; diff --git a/src/Systems/PhysicsSystem.cpp b/src/Systems/PhysicsSystem.cpp index 03aade8..3c3c1f6 100644 --- a/src/Systems/PhysicsSystem.cpp +++ b/src/Systems/PhysicsSystem.cpp @@ -75,7 +75,7 @@ void Systems::PhysicsSystem::Initialize() worldInfo.setupSolverInfo(hkpWorldCinfo::SOLVER_TYPE_4ITERS_MEDIUM); worldInfo.m_gravity = hkVector4(0.0f, -9.82f, 0.0f); - worldInfo.m_broadPhaseBorderBehaviour = hkpWorldCinfo::BROADPHASE_BORDER_FIX_ENTITY; // just fix the entity if the object falls off too far + worldInfo.m_broadPhaseBorderBehaviour = hkpWorldCinfo::BROADPHASE_BORDER_DO_NOTHING; // You must specify the size of the broad phase - objects should not be simulated outside this region worldInfo.setBroadPhaseWorldSize(1000.0f); diff --git a/src/Systems/RenderSystem.cpp b/src/Systems/RenderSystem.cpp index df48c92..43268ce 100755 --- a/src/Systems/RenderSystem.cpp +++ b/src/Systems/RenderSystem.cpp @@ -43,11 +43,15 @@ void Systems::RenderSystem::OnEntityCommit(EntityID entity) void Systems::RenderSystem::UpdateEntity(double dt, EntityID entity, EntityID parent) { + auto templateComponent = m_World->GetComponent(entity); + if (templateComponent) + return; + auto transformComponent = m_World->GetComponent(entity); // Draw models auto modelComponent = m_World->GetComponent(entity); - if (transformComponent&& modelComponent) + if (transformComponent && modelComponent) { auto model = m_World->GetResourceManager()->Load("Model", modelComponent->ModelFile); if (model) diff --git a/src/Systems/TankSteeringSystem.cpp b/src/Systems/TankSteeringSystem.cpp index 11c48fe..3d36977 100644 --- a/src/Systems/TankSteeringSystem.cpp +++ b/src/Systems/TankSteeringSystem.cpp @@ -11,70 +11,85 @@ void Systems::TankSteeringSystem::RegisterComponents( ComponentFactory* cf ) void Systems::TankSteeringSystem::Initialize() { - m_TankInputController = std::unique_ptr(new TankSteeringInputController(EventBroker)); - m_TowerInputController = std::unique_ptr(new TowerSteeringInputController(EventBroker)); + for (int i = 0; i < 4; i++) + { + m_TankInputControllers[i] = std::shared_ptr(new TankSteeringInputController(EventBroker, i + 1)); + } } void Systems::TankSteeringSystem::Update(double dt) { - m_TankInputController->Update(dt); - m_TowerInputController->Update(dt); + for (int i = 0; i < 4; i++) + { + m_TankInputControllers[i]->Update(dt); + } } void Systems::TankSteeringSystem::UpdateEntity(double dt, EntityID entity, EntityID parent) { auto tankSteeringComponent = m_World->GetComponent(entity); - auto towerSteeringComponent = m_World->GetComponent(entity); - auto barrelSteeringComponent = m_World->GetComponent(entity); + if(!tankSteeringComponent) + return; - if(tankSteeringComponent) + auto playerComponent = m_World->GetComponent(tankSteeringComponent->Player); + if (!playerComponent) + return; + + if (playerComponent->ID == 0) + return; + + auto inputController = m_TankInputControllers[playerComponent->ID - 1]; + + Events::TankSteer eSteering; + eSteering.Entity = entity; + eSteering.PositionX = inputController->PositionX; + eSteering.PositionY = inputController->PositionY; + eSteering.Handbrake = inputController->Handbrake; + EventBroker->Publish(eSteering); + + + auto towerSteeringComponent = m_World->GetComponent(tankSteeringComponent->Turret); + auto barrelSteeringComponent = m_World->GetComponent(tankSteeringComponent->Barrel); + + if(towerSteeringComponent) { - Events::TankSteer e; - e.Entity = entity; - e.PositionX = m_TankInputController->PositionX; - e.PositionY = m_TankInputController->PositionY; - e.Handbrake = m_TankInputController->Handbrake; - EventBroker->Publish(e); - } - else if(towerSteeringComponent) - { - auto transformComponent = m_World->GetComponent(entity); - glm::quat orientation = glm::angleAxis(towerSteeringComponent->TurnSpeed * m_TowerInputController->TowerDirection * (float)dt, towerSteeringComponent->Axis); + auto transformComponent = m_World->GetComponent(tankSteeringComponent->Turret); + glm::quat orientation = glm::angleAxis(towerSteeringComponent->TurnSpeed * inputController->TowerDirection * (float)dt, towerSteeringComponent->Axis); transformComponent->Orientation *= orientation; } - else if(barrelSteeringComponent) + + if(barrelSteeringComponent) { - auto transformComponent = m_World->GetComponent(entity); - auto absoluteTransform = m_World->GetSystem()->AbsoluteTransform(entity); - glm::quat orientation = glm::angleAxis(barrelSteeringComponent->TurnSpeed * m_TowerInputController->BarrelDirection * (float)dt, barrelSteeringComponent->Axis); + auto transformComponent = m_World->GetComponent(tankSteeringComponent->Barrel); + auto absoluteTransform = m_World->GetSystem()->AbsoluteTransform(tankSteeringComponent->Barrel); + glm::quat orientation = glm::angleAxis(barrelSteeringComponent->TurnSpeed * inputController->BarrelDirection * (float)dt, barrelSteeringComponent->Axis); transformComponent->Orientation *= orientation; - if(m_TowerInputController->Shoot && m_TimeSinceLastShot[entity] > 1.0) + if(inputController->Shoot && m_TimeSinceLastShot[tankSteeringComponent->Barrel] > 0.5) { EntityID clone = m_World->CloneEntity(barrelSteeringComponent->ShotTemplate); auto templateAbsoluteTransform = m_World->GetSystem()->AbsoluteTransform(barrelSteeringComponent->ShotTemplate); auto cloneTransform = m_World->GetComponent(clone); cloneTransform->Position = templateAbsoluteTransform.Position; cloneTransform->Orientation = absoluteTransform.Orientation * cloneTransform->Orientation; - Events::SetVelocity e; - e.Entity = clone; - e.Velocity = absoluteTransform.Orientation * (glm::vec3(0.f, 0.f, -1.f) * barrelSteeringComponent->ShotSpeed); - EventBroker->Publish(e); - m_TimeSinceLastShot[entity] = 0; + Events::SetVelocity eSetVelocity; + eSetVelocity.Entity = clone; + eSetVelocity.Velocity = absoluteTransform.Orientation * (glm::vec3(0.f, 0.f, -1.f) * barrelSteeringComponent->ShotSpeed); + EventBroker->Publish(eSetVelocity); + m_TimeSinceLastShot[tankSteeringComponent->Barrel] = 0; auto clonePhysicsComponent = m_World->GetComponent(clone); //1,670m/s //25kg - EntityID baseParent = m_World->GetEntityBaseParent(entity); Events::ApplyPointImpulse ePointImpulse ; - ePointImpulse.Entity = baseParent; + ePointImpulse.Entity = entity; ePointImpulse.Position = absoluteTransform.Position; ePointImpulse.Impulse = glm::normalize(absoluteTransform.Orientation * glm::vec3(0, 0, 1)) * clonePhysicsComponent->Mass * 1670.f; EventBroker->Publish(ePointImpulse); } - m_TimeSinceLastShot[entity] += dt; + m_TimeSinceLastShot[tankSteeringComponent->Barrel] += dt; } } @@ -82,10 +97,7 @@ void Systems::TankSteeringSystem::TankSteeringInputController::Update( double dt { PositionX = m_Horizontal; PositionY = m_Vertical; -} -void Systems::TankSteeringSystem::TowerSteeringInputController::Update( double dt ) -{ TowerDirection = m_TowerDirection; BarrelDirection = m_BarrelDirection; Shoot = m_Shoot; @@ -93,9 +105,12 @@ void Systems::TankSteeringSystem::TowerSteeringInputController::Update( double d bool Systems::TankSteeringSystem::TankSteeringInputController::OnCommand(const Events::InputCommand &event) { - + if (event.PlayerID != this->PlayerID) + return false; + float val = event.Value; + // Tank if (event.Command == "horizontal") { m_Horizontal = val; @@ -111,12 +126,7 @@ bool Systems::TankSteeringSystem::TankSteeringInputController::OnCommand(const E Handbrake = val > 0; } - return true; -} - -bool Systems::TankSteeringSystem::TowerSteeringInputController::OnCommand( const Events::InputCommand &event ) -{ - float val = event.Value; + // Turret if(event.Command == "tower_rotation") { m_TowerDirection = -val; @@ -130,19 +140,9 @@ bool Systems::TankSteeringSystem::TowerSteeringInputController::OnCommand( const { m_Shoot = val > 0; } + return true; } -bool Systems::TankSteeringSystem::TowerSteeringInputController::OnMouseMove( const Events::MouseMove &event ) -{ - return false; -} - -bool Systems::TankSteeringSystem::TankSteeringInputController::OnMouseMove( const Events::MouseMove &event ) -{ - return false; -} - - diff --git a/src/Systems/TankSteeringSystem.h b/src/Systems/TankSteeringSystem.h index 65c0bfc..370ba92 100644 --- a/src/Systems/TankSteeringSystem.h +++ b/src/Systems/TankSteeringSystem.h @@ -11,6 +11,7 @@ #include "Components/BarrelSteering.h" #include "Components/Physics.h" #include "Components/Vehicle.h" +#include "Components/Player.h" #include "Systems/TransformSystem.h" #include "InputController.h" @@ -31,9 +32,7 @@ namespace Systems private: class TankSteeringInputController; - std::unique_ptr m_TankInputController; - class TowerSteeringInputController; - std::unique_ptr m_TowerInputController; + std::array, 4> m_TankInputControllers; std::map m_TimeSinceLastShot; }; @@ -41,35 +40,17 @@ namespace Systems class TankSteeringSystem::TankSteeringInputController : InputController { public: - TankSteeringInputController(std::shared_ptr<::EventBroker> eventBroker) + TankSteeringInputController(std::shared_ptr<::EventBroker> eventBroker, int playerID) : InputController(eventBroker) { + PlayerID = playerID; + m_Horizontal = 0.f; m_Vertical = 0.f; PositionX = 0; PositionY = 0; Handbrake = false; - } - float PositionY; - float PositionX; - bool Handbrake; - void Update(double dt); - protected: - virtual bool OnCommand(const Events::InputCommand &event); - virtual bool OnMouseMove(const Events::MouseMove &event); - - private: - float m_Horizontal; - float m_Vertical; - }; - - class TankSteeringSystem::TowerSteeringInputController : InputController - { - public: - TowerSteeringInputController(std::shared_ptr<::EventBroker> eventBroker) - : InputController(eventBroker) - { m_TowerDirection = 0.f; m_BarrelDirection = 0.f; TowerDirection = 0.f; @@ -78,18 +59,28 @@ namespace Systems m_Shoot = false; } + int PlayerID; + + float PositionY; + float PositionX; + bool Handbrake; + float TowerDirection; float BarrelDirection; bool Shoot; + void Update(double dt); protected: virtual bool OnCommand(const Events::InputCommand &event); - virtual bool OnMouseMove(const Events::MouseMove &event); + //virtual bool OnMouseMove(const Events::MouseMove &event); private: - float m_TowerDirection; - float m_BarrelDirection; - bool m_Shoot; - }; + float m_Horizontal; + float m_Vertical; + float m_TowerDirection; + float m_BarrelDirection; + + bool m_Shoot; + }; } \ No newline at end of file diff --git a/vs11/Returngeance/Returngeance.vcxproj b/vs11/Returngeance/Returngeance.vcxproj index 9f7083a..6a99e47 100755 --- a/vs11/Returngeance/Returngeance.vcxproj +++ b/vs11/Returngeance/Returngeance.vcxproj @@ -141,6 +141,7 @@ + diff --git a/vs11/Returngeance/Returngeance.vcxproj.filters b/vs11/Returngeance/Returngeance.vcxproj.filters index d7a5449..2bf19bf 100755 --- a/vs11/Returngeance/Returngeance.vcxproj.filters +++ b/vs11/Returngeance/Returngeance.vcxproj.filters @@ -1,4 +1,4 @@ - + @@ -155,6 +155,9 @@ {b34c0c95-a887-4cf4-af24-cf7c1f459aa8} + + {cb06b441-90b8-46ed-b347-4190dac7185b} + @@ -371,7 +374,7 @@ Input\Events - Physics\Components + Physics\Components Physics\Components @@ -388,10 +391,12 @@ Physics\Events - Physics\Events + + Gameplay\Components + From 4102756e33fec672cd5bb594e16d4a7a5e69a096 Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Thu, 22 May 2014 22:04:54 +0200 Subject: [PATCH 20/21] Beautifully broken --- src/Engine.h | 1 + src/EventBroker.cpp | 55 ++++++++++++++++--- src/EventBroker.h | 74 ++++++++++++++++++++------ src/InputController.h | 5 +- src/InputManager.cpp | 2 + src/InputManager.h | 4 +- src/Systems/DebugSystem.h | 2 +- src/Systems/FreeSteeringSystem.h | 2 +- src/Systems/HelicopterSteeringSystem.h | 2 +- src/Systems/InputSystem.h | 22 ++++---- src/Systems/PhysicsSystem.h | 8 +-- src/Systems/SoundSystem.h | 2 +- src/Systems/TankSteeringSystem.h | 2 +- src/World.cpp | 2 + 14 files changed, 135 insertions(+), 48 deletions(-) diff --git a/src/Engine.h b/src/Engine.h index c063960..2d8215c 100755 --- a/src/Engine.h +++ b/src/Engine.h @@ -38,6 +38,7 @@ public: m_InputManager->Update(dt); m_World->Update(dt); m_Renderer->Draw(dt); + m_EventBroker->Clear(); glfwPollEvents(); } diff --git a/src/EventBroker.cpp b/src/EventBroker.cpp index 68354c9..29ce53c 100644 --- a/src/EventBroker.cpp +++ b/src/EventBroker.cpp @@ -1,5 +1,6 @@ #include "PrecompiledHeader.h" #include "EventBroker.h" +#include "Events/BindKey.h" BaseEventRelay::~BaseEventRelay() { @@ -11,19 +12,57 @@ BaseEventRelay::~BaseEventRelay() void EventBroker::Unsubscribe(BaseEventRelay &relay) // ? { - auto itpair = m_Subscribers.equal_range(relay.m_TypeName); + /*auto itpair = m_Subscribers.equal_range(relay.m_EventTypeName); for (auto it = itpair.first; it != itpair.second; ++it) { - if (it->second == &relay) - { - m_Subscribers.erase(it); - break; - } + if (it->second == &relay) + { + m_Subscribers.erase(it); + break; } + }*/ } void EventBroker::Subscribe(BaseEventRelay &relay) { relay.m_Broker = this; - m_Subscribers.insert(std::make_pair(relay.m_TypeName, &relay)); -} \ No newline at end of file + m_ContextSubscribers[relay.m_ContextTypeName][relay.m_EventTypeName] = &relay; +} + +int EventBroker::Process(std::string contextTypeName) +{ + auto it = m_ContextSubscribers.find(contextTypeName); + if (it == m_ContextSubscribers.end()) + return 0; + + int eventsProcessed = 0; + + EventRelays_t &relays = it->second; + for (auto &pair : *m_EventQueueRead) + { + std::string &eventTypeName = pair.first; + std::shared_ptr event = pair.second; + + /*if (eventTypeName == "struct Events::BindKey") + { + auto bindKey = static_cast(event.get()); + LOG_DEBUG("HsssEJ"); + }*/ + + auto it2 = relays.find(eventTypeName); + if (it2 == relays.end()) + continue; + + auto relay = it2->second; + relay->Receive(event); + eventsProcessed++; + } + + return eventsProcessed; +} + +void EventBroker::Clear() +{ + std::swap(m_EventQueueRead, m_EventQueueWrite); + m_EventQueueWrite->clear(); +} diff --git a/src/EventBroker.h b/src/EventBroker.h index f8877e8..cfd430c 100644 --- a/src/EventBroker.h +++ b/src/EventBroker.h @@ -23,19 +23,22 @@ class BaseEventRelay friend class EventBroker; protected: - BaseEventRelay(std::string typeName) - : m_TypeName(typeName), m_Broker(nullptr) { } + BaseEventRelay(std::string contextTypeName, std::string eventTypeName) + : m_ContextTypeName(contextTypeName) + , m_EventTypeName(eventTypeName) + , m_Broker(nullptr) { } ~BaseEventRelay(); public: - virtual bool Receive(const Event &event) = 0; + virtual bool Receive(std::shared_ptr event) = 0; protected: - std::string m_TypeName; + std::string m_ContextTypeName; + std::string m_EventTypeName; EventBroker* m_Broker; }; -template +template class EventRelay : public BaseEventRelay { public: @@ -43,24 +46,24 @@ public: EventRelay() : m_Callback(nullptr) - , BaseEventRelay(typeid(EventType).name()) { } + , BaseEventRelay(typeid(ContextType).name(), typeid(EventType).name()) { } EventRelay(CallbackType callback) : m_Callback(callback) - , BaseEventRelay(typeid(EventType).name()) { } + , BaseEventRelay(typeid(ContextType).name(), typeid(EventType).name()) { } protected: - bool Receive(const Event &event) override; + bool Receive(std::shared_ptr event) override; private: CallbackType m_Callback; }; -template -bool EventRelay::Receive(const Event &event) +template +bool EventRelay::Receive(std::shared_ptr event) { if (m_Callback != nullptr) { - return m_Callback(static_cast(event)); + return m_Callback(static_cast(*event.get())); } else { @@ -70,27 +73,66 @@ bool EventRelay::Receive(const Event &event) class EventBroker { -template friend class EventRelay; +template friend class EventRelay; public: + EventBroker() + { + m_EventQueueRead = std::make_shared(); + m_EventQueueWrite = std::make_shared(); + } + + void Subscribe(BaseEventRelay &relay); template void Publish(const EventType &event); - void Subscribe(BaseEventRelay &relay); + // Process all events no matter the context. + /*void Process() + { + + }*/ + /* + Process all events in a given context. + Returns: Number of events processed + */ + template + int Process(); + int Process(std::string contextTypeName); + void Clear(); void Unsubscribe(BaseEventRelay &relay); private: - std::unordered_multimap m_Subscribers; + typedef std::string ContextTypeName_t; // typeid(ContextType).name() + typedef std::string EventTypeName_t; // typeid(EventType).name() + + typedef std::unordered_map EventRelays_t; + typedef std::unordered_map ContextSubscribers_t; + ContextSubscribers_t m_ContextSubscribers; + + typedef std::list>> EventQueue_t; + std::shared_ptr m_EventQueueRead; + std::shared_ptr m_EventQueueWrite; }; template void EventBroker::Publish(const EventType &event) { - auto itpair = m_Subscribers.equal_range(typeid(EventType).name()); + /*auto itpair = m_Subscribers.equal_range(typeid(EventType).name()); for (auto it = itpair.first; it != itpair.second; ++it) { it->second->Receive(event); - } + }*/ + + m_EventQueueWrite->push_back(std::make_pair(typeid(EventType).name(), std::shared_ptr(new EventType(event)))); } +template +int EventBroker::Process() +{ + const std::string contextTypeName = typeid(ContextType).name(); + return Process(contextTypeName); +} + + + #endif // MessageRelay_h__ diff --git a/src/InputController.h b/src/InputController.h index 7afd5d5..39c26b1 100644 --- a/src/InputController.h +++ b/src/InputController.h @@ -7,6 +7,7 @@ #include "Events/InputCommand.h" #include "Events/MouseMove.h" +template class InputController { public: @@ -26,8 +27,8 @@ protected: std::shared_ptr<::EventBroker> EventBroker; private: - EventRelay m_EInputCommand; - EventRelay m_EMouseMove; + EventRelay m_EInputCommand; + EventRelay m_EMouseMove; }; #endif // InputController_h__ diff --git a/src/InputManager.cpp b/src/InputManager.cpp index 8c15fea..b20e072 100644 --- a/src/InputManager.cpp +++ b/src/InputManager.cpp @@ -13,6 +13,8 @@ void InputManager::Initialize() void InputManager::Update(double dt) { + EventBroker->Process(); + m_LastKeyState = m_CurrentKeyState; m_LastMouseState = m_CurrentMouseState; m_LastMouseX = m_CurrentMouseX; diff --git a/src/InputManager.h b/src/InputManager.h index 6b56c92..69827ba 100644 --- a/src/InputManager.h +++ b/src/InputManager.h @@ -39,9 +39,9 @@ private: GLFWwindow* m_GLFWWindow; std::shared_ptr<::EventBroker> EventBroker; - EventRelay m_ELockMouse; + EventRelay m_ELockMouse; bool OnLockMouse(const Events::LockMouse &event); - EventRelay m_EUnlockMouse; + EventRelay m_EUnlockMouse; bool OnUnlockMouse(const Events::UnlockMouse &event); std::array m_CurrentKeyState; diff --git a/src/Systems/DebugSystem.h b/src/Systems/DebugSystem.h index 9db4de6..bbf7cd5 100644 --- a/src/Systems/DebugSystem.h +++ b/src/Systems/DebugSystem.h @@ -19,7 +19,7 @@ public: void Update(double dt) override; - EventRelay m_EKeyDown; + EventRelay m_EKeyDown; bool OnKeyDown(const Events::KeyDown &event); //void UpdateEntity(double dt, EntityID entity, EntityID parent) override; diff --git a/src/Systems/FreeSteeringSystem.h b/src/Systems/FreeSteeringSystem.h index b94466b..61a453d 100755 --- a/src/Systems/FreeSteeringSystem.h +++ b/src/Systems/FreeSteeringSystem.h @@ -27,7 +27,7 @@ private: std::unique_ptr m_InputController; }; -class FreeSteeringSystem::FreeSteeringInputController : InputController +class FreeSteeringSystem::FreeSteeringInputController : InputController { public: FreeSteeringInputController(std::shared_ptr<::EventBroker> eventBroker) diff --git a/src/Systems/HelicopterSteeringSystem.h b/src/Systems/HelicopterSteeringSystem.h index ed91096..742ce70 100644 --- a/src/Systems/HelicopterSteeringSystem.h +++ b/src/Systems/HelicopterSteeringSystem.h @@ -27,7 +27,7 @@ private: std::map m_TimeSinceLastShot; }; -class HelicopterSteeringSystem::HelicopterSteeringInputController : InputController +class HelicopterSteeringSystem::HelicopterSteeringInputController : InputController { public: HelicopterSteeringInputController(std::shared_ptr<::EventBroker> eventBroker) diff --git a/src/Systems/InputSystem.h b/src/Systems/InputSystem.h index d13c249..9d51d2a 100755 --- a/src/Systems/InputSystem.h +++ b/src/Systems/InputSystem.h @@ -45,28 +45,28 @@ private: std::unordered_map> m_GamepadButtonBindings; // Gamepad::Button -> command string // Input events - EventRelay m_EKeyDown; + EventRelay m_EKeyDown; bool OnKeyDown(const Events::KeyDown &event); - EventRelay m_EKeyUp; + EventRelay m_EKeyUp; bool OnKeyUp(const Events::KeyUp &event); - EventRelay m_EMousePress; + EventRelay m_EMousePress; bool OnMousePress(const Events::MousePress &event); - EventRelay m_EMouseRelease; + EventRelay m_EMouseRelease; bool OnMouseRelease(const Events::MouseRelease &event); - EventRelay m_EGamepadAxis; + EventRelay m_EGamepadAxis; bool OnGamepadAxis(const Events::GamepadAxis &event); - EventRelay m_EGamepadButtonDown; + EventRelay m_EGamepadButtonDown; bool OnGamepadButtonDown(const Events::GamepadButtonDown &event); - EventRelay m_EGamepadButtonUp; + EventRelay m_EGamepadButtonUp; bool OnGamepadButtonUp(const Events::GamepadButtonUp &event); // Input binding events - EventRelay m_EBindKey; + EventRelay m_EBindKey; bool OnBindKey(const Events::BindKey &event); - EventRelay m_EBindMouseButton; + EventRelay m_EBindMouseButton; bool OnBindMouseButton(const Events::BindMouseButton &event); - EventRelay m_EBindGamepadAxis; + EventRelay m_EBindGamepadAxis; bool OnBindGamepadAxis(const Events::BindGamepadAxis &event); - EventRelay m_EBindGamepadButton; + EventRelay m_EBindGamepadButton; bool OnBindGamepadButton(const Events::BindGamepadButton &event); float GetCommandTotalValue(std::string command); diff --git a/src/Systems/PhysicsSystem.h b/src/Systems/PhysicsSystem.h index 54e61f7..029dd8e 100644 --- a/src/Systems/PhysicsSystem.h +++ b/src/Systems/PhysicsSystem.h @@ -113,13 +113,13 @@ private: hkpWorld* m_PhysicsWorld; // Events - EventRelay m_ETankSteer; + EventRelay m_ETankSteer; bool OnTankSteer(const Events::TankSteer &event); - EventRelay m_ESetVelocity; + EventRelay m_ESetVelocity; bool OnSetVelocity(const Events::SetVelocity &event); - EventRelay m_EApplyForce; + EventRelay m_EApplyForce; bool OnApplyForce(const Events::ApplyForce &event); - EventRelay m_EApplyPointImpulse; + EventRelay m_EApplyPointImpulse; bool OnApplyPointImpulse(const Events::ApplyPointImpulse &event); void SetUpPhysicsState(EntityID entity, EntityID parent); diff --git a/src/Systems/SoundSystem.h b/src/Systems/SoundSystem.h index 7fbc5aa..00ae091 100755 --- a/src/Systems/SoundSystem.h +++ b/src/Systems/SoundSystem.h @@ -44,7 +44,7 @@ private: //unsigned long dataSize; // Events - EventRelay m_EPlaySound; + EventRelay m_EPlaySound; bool OnPlaySound(const Events::PlaySound &event); std::map m_Sources; diff --git a/src/Systems/TankSteeringSystem.h b/src/Systems/TankSteeringSystem.h index 370ba92..ec96df8 100644 --- a/src/Systems/TankSteeringSystem.h +++ b/src/Systems/TankSteeringSystem.h @@ -37,7 +37,7 @@ namespace Systems std::map m_TimeSinceLastShot; }; - class TankSteeringSystem::TankSteeringInputController : InputController + class TankSteeringSystem::TankSteeringInputController : InputController { public: TankSteeringInputController(std::shared_ptr<::EventBroker> eventBroker, int playerID) diff --git a/src/World.cpp b/src/World.cpp index 2a25856..fe1f760 100755 --- a/src/World.cpp +++ b/src/World.cpp @@ -36,7 +36,9 @@ void World::Update(double dt) { for (auto pair : m_Systems) { + const std::string &type = pair.first; auto system = pair.second; + m_EventBroker->Process(type); system->Update(dt); RecursiveUpdate(system, dt, 0); } From e48df894cf15373ee57bd9f48f1d021b5ac2e96a Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Fri, 23 May 2014 23:31:24 +0200 Subject: [PATCH 21/21] Better events --- src/EventBroker.cpp | 46 ++++++++++++++++++++++----------------------- src/EventBroker.h | 31 ++++++++++++++++++++---------- 2 files changed, 44 insertions(+), 33 deletions(-) diff --git a/src/EventBroker.cpp b/src/EventBroker.cpp index 29ce53c..118eddf 100644 --- a/src/EventBroker.cpp +++ b/src/EventBroker.cpp @@ -12,50 +12,50 @@ BaseEventRelay::~BaseEventRelay() void EventBroker::Unsubscribe(BaseEventRelay &relay) // ? { - /*auto itpair = m_Subscribers.equal_range(relay.m_EventTypeName); + auto contextIt = m_ContextRelays.find(relay.m_ContextTypeName); + if (contextIt == m_ContextRelays.end()) + return; + + auto eventRelays = contextIt->second; + + auto itpair = eventRelays.equal_range(relay.m_EventTypeName); for (auto it = itpair.first; it != itpair.second; ++it) { - if (it->second == &relay) - { - m_Subscribers.erase(it); - break; + if (it->second == &relay) + { + eventRelays.erase(it); + break; + } } - }*/ } void EventBroker::Subscribe(BaseEventRelay &relay) { relay.m_Broker = this; - m_ContextSubscribers[relay.m_ContextTypeName][relay.m_EventTypeName] = &relay; + m_ContextRelays[relay.m_ContextTypeName].insert(std::make_pair(relay.m_EventTypeName, &relay)); } int EventBroker::Process(std::string contextTypeName) { - auto it = m_ContextSubscribers.find(contextTypeName); - if (it == m_ContextSubscribers.end()) + auto it = m_ContextRelays.find(contextTypeName); + if (it == m_ContextRelays.end()) return 0; - int eventsProcessed = 0; - EventRelays_t &relays = it->second; + + int eventsProcessed = 0; for (auto &pair : *m_EventQueueRead) { std::string &eventTypeName = pair.first; std::shared_ptr event = pair.second; - /*if (eventTypeName == "struct Events::BindKey") + auto itpair = relays.equal_range(eventTypeName); + for (auto it2 = itpair.first; it2 != itpair.second; ++it2) { - auto bindKey = static_cast(event.get()); - LOG_DEBUG("HsssEJ"); - }*/ - - auto it2 = relays.find(eventTypeName); - if (it2 == relays.end()) - continue; - - auto relay = it2->second; - relay->Receive(event); - eventsProcessed++; + auto relay = it2->second; + relay->Receive(event); + eventsProcessed++; + } } return eventsProcessed; diff --git a/src/EventBroker.h b/src/EventBroker.h index cfd430c..e29437a 100644 --- a/src/EventBroker.h +++ b/src/EventBroker.h @@ -3,6 +3,7 @@ #include #include +#include #include #include @@ -30,7 +31,7 @@ protected: ~BaseEventRelay(); public: - virtual bool Receive(std::shared_ptr event) = 0; + virtual bool Receive(const std::shared_ptr event) = 0; protected: std::string m_ContextTypeName; @@ -52,18 +53,18 @@ public: , BaseEventRelay(typeid(ContextType).name(), typeid(EventType).name()) { } protected: - bool Receive(std::shared_ptr event) override; + bool Receive(const std::shared_ptr event) override; private: CallbackType m_Callback; }; template -bool EventRelay::Receive(std::shared_ptr event) +bool EventRelay::Receive(const std::shared_ptr event) { if (m_Callback != nullptr) { - return m_Callback(static_cast(*event.get())); + return m_Callback(*static_cast(event.get())); } else { @@ -99,21 +100,22 @@ public: int Process(std::string contextTypeName); void Clear(); void Unsubscribe(BaseEventRelay &relay); + template + void UnsubscribeAll(); private: typedef std::string ContextTypeName_t; // typeid(ContextType).name() typedef std::string EventTypeName_t; // typeid(EventType).name() - typedef std::unordered_map EventRelays_t; - typedef std::unordered_map ContextSubscribers_t; - ContextSubscribers_t m_ContextSubscribers; + typedef std::unordered_multimap EventRelays_t; + typedef std::unordered_map ContextRelays_t; + ContextRelays_t m_ContextRelays; typedef std::list>> EventQueue_t; std::shared_ptr m_EventQueueRead; std::shared_ptr m_EventQueueWrite; }; - template void EventBroker::Publish(const EventType &event) { @@ -123,7 +125,7 @@ void EventBroker::Publish(const EventType &event) it->second->Receive(event); }*/ - m_EventQueueWrite->push_back(std::make_pair(typeid(EventType).name(), std::shared_ptr(new EventType(event)))); + m_EventQueueWrite->push_back(std::make_pair(typeid(EventType).name(), std::shared_ptr(new EventType(event)))); } template @@ -133,6 +135,15 @@ int EventBroker::Process() return Process(contextTypeName); } - +template +void EventBroker::UnsubscribeAll() +{ + const std::string contextTypeName = typeid(ContextType).name(); + auto contextIt = m_ContextRelays.find(contextTypeName); + if (contextIt != m_ContextRelays.end()) + { + m_ContextRelays.erase(contextIt); + } +} #endif // MessageRelay_h__