diff --git a/assets b/assets index 66e2abf..6cc3858 160000 --- a/assets +++ b/assets @@ -1 +1 @@ -Subproject commit 66e2abf429f8d9a8c9fcab8ef81e717d5607aafa +Subproject commit 6cc38589ed77dbb3b9c34c1168d52e0f86e1de46 diff --git a/src/Components/BarrelSteering.h b/src/Components/BarrelSteering.h new file mode 100644 index 0000000..1cd71be --- /dev/null +++ b/src/Components/BarrelSteering.h @@ -0,0 +1,23 @@ +#ifndef BarrelSteering_h__ +#define BarrelSteering_h__ + +#include "Component.h" + +namespace Components +{ + + struct BarrelSteering : Component + { + BarrelSteering() + : TurnSpeed(1.f), Axis(glm::vec3(0,1,0)){ } + float TurnSpeed; + glm::vec3 Axis; + EntityID ShotTemplate; + float ShotSpeed; + + virtual BarrelSteering* Clone() const override { return new BarrelSteering(*this); } + }; + +} + +#endif // BarrelSteering_h__ \ No newline at end of file diff --git a/src/Components/Camera.h b/src/Components/Camera.h index fb3f7ef..cd992e8 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,6 @@ struct Camera : Component , NearClip(0.1f) , FarClip(100.f) { } - std::string Viewport; float FOV; float NearClip; float FarClip; 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/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/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/PointLight.h b/src/Components/PointLight.h index 248fec6..9037b89 100755 --- a/src/Components/PointLight.h +++ b/src/Components/PointLight.h @@ -9,13 +9,22 @@ namespace Components struct PointLight : Component { - float Intensity; - float MaxRange; + PointLight() + : Specular(1.0f, 1.0f, 1.0f) + , Diffuse(1.0f, 1.0f, 1.0f) + , specularExponent(50.0f) + , ConstantAttenuation(1.0f) + , LinearAttenuation(0.f) + , QuadraticAttenuation(3.f) + { } + + float ConstantAttenuation, LinearAttenuation, QuadraticAttenuation; + Color color; + glm::vec3 Specular; glm::vec3 Diffuse; - float constantAttenuation, linearAttenuation, quadraticAttenuation; - float spotExponent; - Color color; + float specularExponent; + float Scale; virtual PointLight* Clone() const override { return new PointLight(*this); } }; 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 new file mode 100644 index 0000000..0b4a6c8 --- /dev/null +++ b/src/Components/TowerSteering.h @@ -0,0 +1,20 @@ +#ifndef TowerSteering_h__ +#define TowerSteering_h__ + +#include "Component.h" + + namespace Components +{ + +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); } +}; + +} + +#endif // TowerSteering_h__ \ No newline at end of file diff --git a/src/Components/Vehicle.h b/src/Components/Vehicle.h index 69adce3..2957df5 100644 --- a/src/Components/Vehicle.h +++ b/src/Components/Vehicle.h @@ -11,7 +11,7 @@ struct Vehicle : Component { Vehicle() : MaxTorque(1000.0f), MinRPM(1000.0f), OptimalRPM(3000.0f), MaxRPM(4000.0f), MaxSteeringAngle(35), TopSpeed(130.0f), - MaxSpeedFullSteeringAngle(40.0f){ } + MaxSpeedFullSteeringAngle(40.0f), SpringDamping(1.f){ } float MaxTorque; float MinRPM; @@ -22,6 +22,7 @@ struct Vehicle : Component //TopSpeed not working fully yet float TopSpeed; float MaxSpeedFullSteeringAngle; + float SpringDamping; Vehicle* Clone() const override { return new Vehicle(*this); } }; diff --git a/src/Components/Viewport.h b/src/Components/Viewport.h new file mode 100644 index 0000000..57692a6 --- /dev/null +++ b/src/Components/Viewport.h @@ -0,0 +1,29 @@ +#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) + , Camera(0) { } + + float Left; + float Top; + float Right; + float Bottom; + + EntityID Camera; + + virtual Viewport* Clone() const override { return new Viewport(*this); } +}; + +} +#endif // Components_Viewport_h__ \ No newline at end of file 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/Engine.h b/src/Engine.h index 25337c4..2d8215c 100755 --- a/src/Engine.h +++ b/src/Engine.h @@ -19,7 +19,7 @@ public: m_InputManager = std::make_shared(m_Renderer->GetWindow(), m_EventBroker); - m_UIParent = std::make_shared(m_EventBroker); + //m_UIParent = std::make_shared(m_EventBroker); m_World = std::make_shared(m_EventBroker, m_Renderer); m_World->Initialize(); @@ -38,6 +38,7 @@ public: m_InputManager->Update(dt); m_World->Update(dt); m_Renderer->Draw(dt); + m_EventBroker->Clear(); glfwPollEvents(); } @@ -46,7 +47,7 @@ private: std::shared_ptr m_EventBroker; std::shared_ptr m_Renderer; std::shared_ptr m_InputManager; - std::shared_ptr m_UIParent; + //std::shared_ptr m_UIParent; // TODO: This should ultimately live in GameFrame std::shared_ptr m_World; diff --git a/src/EventBroker.cpp b/src/EventBroker.cpp index 68354c9..118eddf 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,12 +12,18 @@ BaseEventRelay::~BaseEventRelay() void EventBroker::Unsubscribe(BaseEventRelay &relay) // ? { - auto itpair = m_Subscribers.equal_range(relay.m_TypeName); + 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); + eventRelays.erase(it); break; } } @@ -25,5 +32,37 @@ void EventBroker::Unsubscribe(BaseEventRelay &relay) // ? 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_ContextRelays[relay.m_ContextTypeName].insert(std::make_pair(relay.m_EventTypeName, &relay)); +} + +int EventBroker::Process(std::string contextTypeName) +{ + auto it = m_ContextRelays.find(contextTypeName); + if (it == m_ContextRelays.end()) + return 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; + + auto itpair = relays.equal_range(eventTypeName); + for (auto it2 = itpair.first; it2 != itpair.second; ++it2) + { + 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..e29437a 100644 --- a/src/EventBroker.h +++ b/src/EventBroker.h @@ -3,6 +3,7 @@ #include #include +#include #include #include @@ -23,19 +24,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(const 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 +47,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(const std::shared_ptr event) override; private: CallbackType m_Callback; }; -template -bool EventRelay::Receive(const Event &event) +template +bool EventRelay::Receive(const std::shared_ptr event) { if (m_Callback != nullptr) { - return m_Callback(static_cast(event)); + return m_Callback(*static_cast(event.get())); } else { @@ -70,26 +74,75 @@ 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); + template + void UnsubscribeAll(); 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_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) { - 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); +} + +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); } } 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/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/Events/BindGamepadAxis.h b/src/Events/BindGamepadAxis.h new file mode 100644 index 0000000..b1e16f8 --- /dev/null +++ b/src/Events/BindGamepadAxis.h @@ -0,0 +1,21 @@ +#ifndef Events_BindGamepadAxis_h__ +#define Events_BindGamepadAxis_h__ + +#include + +#include "EventBroker.h" +#include "Events/GamepadAxis.h" + +namespace Events +{ + +struct BindGamepadAxis : Event +{ + Gamepad::Axis Axis; + std::string Command; + float Value; +}; + +} + +#endif // Events_BindGamepadAxis_h__ \ No newline at end of file diff --git a/src/Events/BindGamepadButton.h b/src/Events/BindGamepadButton.h new file mode 100644 index 0000000..72641d8 --- /dev/null +++ b/src/Events/BindGamepadButton.h @@ -0,0 +1,21 @@ +#ifndef Events_BindGamepadButton_h__ +#define Events_BindGamepadButton_h__ + +#include + +#include "EventBroker.h" +#include "Events/GamepadButton.h" + +namespace Events +{ + +struct BindGamepadButton : Event +{ + Gamepad::Button Button; + std::string Command; + float Value; +}; + +} + +#endif // Events_BindGamepadButton_h__ \ No newline at end of file diff --git a/src/Events/BindKey.h b/src/Events/BindKey.h index a774942..17c786f 100644 --- a/src/Events/BindKey.h +++ b/src/Events/BindKey.h @@ -1,6 +1,8 @@ #ifndef Events_BindKey_h__ #define Events_BindKey_h__ +#include + #include "EventBroker.h" namespace Events @@ -10,6 +12,7 @@ struct BindKey : Event { int KeyCode; std::string Command; + float Value; }; } diff --git a/src/Events/BindMouseButton.h b/src/Events/BindMouseButton.h index 1f46647..e7fb8a4 100644 --- a/src/Events/BindMouseButton.h +++ b/src/Events/BindMouseButton.h @@ -10,6 +10,7 @@ struct BindMouseButton : Event { int Button; std::string Command; + float Value; }; } 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/GamepadAxis.h b/src/Events/GamepadAxis.h new file mode 100644 index 0000000..3d47767 --- /dev/null +++ b/src/Events/GamepadAxis.h @@ -0,0 +1,32 @@ +#ifndef Events_GamepadAxis_h__ +#define Events_GamepadAxis_h__ + +#include "EventBroker.h" + +namespace Gamepad +{ + enum class Axis + { + LeftX, + LeftY, + RightX, + RightY, + LeftTrigger, + RightTrigger, + LAST = RightTrigger + }; +} + +namespace Events +{ + +struct GamepadAxis : Event +{ + int GamepadID; + Gamepad::Axis Axis; + float Value; +}; + +} + +#endif // Events_GamepadAxis_h__ \ No newline at end of file diff --git a/src/Events/GamepadButton.h b/src/Events/GamepadButton.h new file mode 100644 index 0000000..a99583c --- /dev/null +++ b/src/Events/GamepadButton.h @@ -0,0 +1,45 @@ +#ifndef Events_GamepadButton_h__ +#define Events_GamepadButton_h__ + +#include "EventBroker.h" + +namespace Gamepad +{ + enum class Button + { + Up, + Down, + Left, + Right, + Start, + Back, + LeftThumb, + RightThumb, + LeftShoulder, + RightShoulder, + A, + B, + X, + Y, + LAST = Y + }; +} + +namespace Events +{ + +struct GamepadButtonDown : Event +{ + int GamepadID; + Gamepad::Button Button; +}; + +struct GamepadButtonUp : Event +{ + int GamepadID; + Gamepad::Button Button; +}; + +} + +#endif // Events_GamepadButton_h__ \ No newline at end of file diff --git a/src/Events/InputCommand.h b/src/Events/InputCommand.h index bd18f6a..cde1dab 100644 --- a/src/Events/InputCommand.h +++ b/src/Events/InputCommand.h @@ -12,7 +12,7 @@ struct InputCommand : Event { unsigned int PlayerID; std::string Command; - boost::any Value; + float Value; }; } diff --git a/src/Events/LockMouse.h b/src/Events/LockMouse.h new file mode 100644 index 0000000..b045c55 --- /dev/null +++ b/src/Events/LockMouse.h @@ -0,0 +1,14 @@ +#ifndef Events_LockMouse_h__ +#define Events_LockMouse_h__ + +#include "EventBroker.h" + +namespace Events +{ + +struct LockMouse : Event { }; +struct UnlockMouse : Event { }; + +} + +#endif // Events_LockMouse_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/Events/SetVelocity.h b/src/Events/SetVelocity.h new file mode 100644 index 0000000..3bdb8a1 --- /dev/null +++ b/src/Events/SetVelocity.h @@ -0,0 +1,17 @@ +#ifndef Events_SetVelocity_h__ +#define Events_SetVelocity_h__ +#include "Entity.h" +#include "EventBroker.h" + +namespace Events +{ + + struct SetVelocity : Event + { + EntityID Entity; + glm::vec3 Velocity; + }; + +} + +#endif // Events_SetVelocity_h__ \ No newline at end of file diff --git a/src/Factory.h b/src/Factory.h index adcfbbf..80649ee 100755 --- a/src/Factory.h +++ b/src/Factory.h @@ -10,12 +10,18 @@ template class Factory { public: - void Register(std::string name, std::function factoryFunction) + /*void Register(std::string name, std::function factoryFunction) { m_FactoryFunctions[name] = factoryFunction; + }*/ + + template + void Register(std::function factoryFunction) + { + m_FactoryFunctions[typeid(T2).name()] = factoryFunction; } - T Create(std::string name) + /*T Create(std::string name) { auto it = m_FactoryFunctions.find(name); if (it != m_FactoryFunctions.end()) @@ -26,6 +32,20 @@ public: { return nullptr; } + }*/ + + template + T Create() + { + auto it = m_FactoryFunctions.find(typeid(T2).name()); + if (it != m_FactoryFunctions.end()) + { + return it->second(); + } + else + { + return nullptr; + } } private: diff --git a/src/GUI/Frame.h b/src/GUI/Frame.h index 19f534a..628a650 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 { @@ -34,14 +37,37 @@ public: 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/GameWorld.cpp b/src/GameWorld.cpp index 247204c..bcf46e5 100755 --- a/src/GameWorld.cpp +++ b/src/GameWorld.cpp @@ -8,61 +8,103 @@ 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, "+forward"); - BindKey(GLFW_KEY_S, "+backward"); - BindKey(GLFW_KEY_A, "+left"); - BindKey(GLFW_KEY_D, "+right"); - BindKey(GLFW_KEY_SPACE, "+handbrake"); + 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); + BindGamepadAxis(Gamepad::Axis::LeftX, "horizontal", 1.f); + BindGamepadAxis(Gamepad::Axis::LeftY, "vertical", 1.f); - BindKey(GLFW_KEY_Q, "+up"); - BindKey(GLFW_KEY_LEFT_CONTROL, "+down"); - BindKey(GLFW_KEY_LEFT_ALT, "+slow"); - BindKey(GLFW_KEY_LEFT_SHIFT, "+fast"); - BindMouseButton(GLFW_MOUSE_BUTTON_1, "+attack"); - BindMouseButton(GLFW_MOUSE_BUTTON_2, "+attack2"); - BindMouseButton(GLFW_MOUSE_BUTTON_3, "+attack3"); + 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); + BindGamepadAxis(Gamepad::Axis::RightX, "tower_rotation", 1.f); + BindGamepadAxis(Gamepad::Axis::RightY, "barrel_rotation", 1.f); + BindKey(GLFW_KEY_SPACE, "handbrake", 1.f); + BindGamepadButton(Gamepad::Button::A, "handbrake", 1.f); - BindKey(GLFW_KEY_UP, "+cam_forward"); - BindKey(GLFW_KEY_DOWN, "+cam_backward"); - BindKey(GLFW_KEY_LEFT, "+cam_right"); - BindKey(GLFW_KEY_RIGHT, "+cam_left"); + BindKey(GLFW_KEY_Z, "shoot", 1.f); + BindGamepadAxis(Gamepad::Axis::RightTrigger, "shoot", 1.f); + + //BindGamepadButton(Gamepad::Button::Up, "Gamepad::Button::Up", 1.f); + //BindGamepadButton(Gamepad::Button::Down, "Gamepad::Button::Down", 1.f); + //BindGamepadButton(Gamepad::Button::Left, "Gamepad::Button::Left", 1.f); + //BindGamepadButton(Gamepad::Button::Right, "Gamepad::Button::Right", 1.f); + //BindGamepadButton(Gamepad::Button::Start, "Gamepad::Button::Start", 1.f); + //BindGamepadButton(Gamepad::Button::Back, "Gamepad::Button::Back", 1.f); + //BindGamepadButton(Gamepad::Button::LeftThumb, "Gamepad::Button::LeftThumb", 1.f); + //BindGamepadButton(Gamepad::Button::RightThumb, "Gamepad::Button::RightThumb", 1.f); + //BindGamepadButton(Gamepad::Button::LeftShoulder, "Gamepad::Button::LeftShoulder", 1.f); + //BindGamepadButton(Gamepad::Button::RightShoulder, "Gamepad::Button::RightShoulder", 1.f); + //BindGamepadButton(Gamepad::Button::A, "Gamepad::Button::A", 1.f); + //BindGamepadButton(Gamepad::Button::B, "Gamepad::Button::B", 1.f); + //BindGamepadButton(Gamepad::Button::X, "Gamepad::Button::X", 1.f); + //BindGamepadButton(Gamepad::Button::Y, "Gamepad::Button::Y", 1.f); RegisterComponents(); + auto camera = CreateEntity(); { - auto camera = CreateEntity(); - auto transform = AddComponent(camera, "Transform"); + auto transform = AddComponent(camera); 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"); + auto cameraComp = AddComponent(camera); cameraComp->FarClip = 2000.f; - auto freeSteering = AddComponent(camera, "FreeSteering"); - CommitEntity(camera); + auto freeSteering = AddComponent(camera); + } + CommitEntity(camera); + + auto viewport1 = CreateEntity(); + { + auto viewport = AddComponent(viewport1); + viewport->Right = 0.5f; + viewport->Camera = camera; + } + CommitEntity(viewport1); + + auto viewport2 = CreateEntity(); + { + auto viewport = AddComponent(viewport2); + viewport->Left = 0.5f; + } + 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"); - transform->Position = glm::vec3(0, 0, 0); + auto transform = AddComponent(ground); + 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"); - //model->ModelFile = "Models/TestScene/testScene.obj"; - model->ModelFile = "Models/Placeholders/Terrain/Terrain2.obj"; + auto model = AddComponent(ground); + model->ModelFile = "Models/TestScene3/testScene.obj"; + //model->ModelFile = "Models/Placeholders/Terrain/Terrain2.obj"; - auto physics = AddComponent(ground, "Physics"); + auto physics = AddComponent(ground); physics->Mass = 10; physics->Static = true; auto groundshape = CreateEntity(ground); - auto transformshape = AddComponent(groundshape, "Transform"); - auto meshShape = AddComponent(groundshape, "MeshShape"); - meshShape->ResourceName = "Models/Placeholders/Terrain/Terrain2.obj"; - //meshShape->ResourceName = "Models/TestScene/testScene.obj"; + auto transformshape = AddComponent(groundshape); + auto meshShape = AddComponent(groundshape); + //meshShape->ResourceName = "Models/Placeholders/Terrain/Terrain2.obj"; + meshShape->ResourceName = "Models/TestScene3/testScene.obj"; CommitEntity(groundshape); @@ -73,23 +115,23 @@ void GameWorld::Initialize() /*{ auto jeep = CreateEntity(); - auto transform = AddComponent(jeep, "Transform"); + auto transform = AddComponent(jeep); transform->Position = glm::vec3(0, 5, 0); transform->Orientation = glm::angleAxis(glm::pi()/2, glm::vec3(0, 1, 0)); - auto physics = AddComponent(jeep, "Physics"); + auto physics = AddComponent(jeep); physics->Mass = 1800; physics->Static = false; - auto vehicle = AddComponent(jeep, "Vehicle"); - AddComponent(jeep, "Input"); + auto vehicle = AddComponent(jeep); + AddComponent(jeep); { auto shape = CreateEntity(jeep); - auto transform = AddComponent(shape, "Transform"); - auto meshShape = AddComponent(shape, "MeshShape"); + auto transform = AddComponent(shape); + auto meshShape = AddComponent(shape); meshShape->ResourceName = "Models/Jeep/Chassi/ChassiCollision.obj"; CommitEntity(shape); - // auto box = AddComponent(jeep, "Box"); + // auto box = AddComponent(jeep); // box->Width = 1.487f; // box->Height = 0.727f; // box->Depth = 2.594f; @@ -98,17 +140,17 @@ void GameWorld::Initialize() { auto chassis = CreateEntity(jeep); - auto transform = AddComponent(chassis, "Transform"); + auto transform = AddComponent(chassis); transform->Position = glm::vec3(0, 0, 0); // 0.6577f - auto model = AddComponent(chassis, "Model"); + auto model = AddComponent(chassis); model->ModelFile = "Models/Jeep/Chassi/chassi.obj"; } { auto lightentity = CreateEntity(jeep); - auto transform = AddComponent(lightentity, "Transform"); + auto transform = AddComponent(lightentity); transform->Position = glm::vec3(0, 15, 0); - auto light = AddComponent(lightentity, "PointLight"); + 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; @@ -123,12 +165,12 @@ void GameWorld::Initialize() float suspensionStrength = 35.f; { auto wheel = CreateEntity(jeep); - auto transform = AddComponent(wheel, "Transform"); + auto transform = AddComponent(wheel); transform->Position = glm::vec3(1.9f, 0.5546f - wheelOffset, -0.9242f); transform->Scale = glm::vec3(1.0f); - auto model = AddComponent(wheel, "Model"); + auto model = AddComponent(wheel); model->ModelFile = "Models/Jeep/WheelFront/wheelFront.obj"; - auto Wheel = AddComponent(wheel, "Wheel"); + auto Wheel = AddComponent(wheel); Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); Wheel->AxleID = 0; Wheel->Mass = 50; @@ -142,13 +184,13 @@ void GameWorld::Initialize() { auto wheel = CreateEntity(jeep); - auto transform = AddComponent(wheel, "Transform"); + auto transform = AddComponent(wheel); 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"); + auto model = AddComponent(wheel); model->ModelFile = "Models/Jeep/WheelFront/wheelFront.obj"; - auto Wheel = AddComponent(wheel, "Wheel"); + auto Wheel = AddComponent(wheel); Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); Wheel->AxleID = 0; Wheel->Mass = 50; @@ -162,11 +204,11 @@ void GameWorld::Initialize() { auto wheel = CreateEntity(jeep); - auto transform = AddComponent(wheel, "Transform"); + auto transform = AddComponent(wheel); transform->Position = glm::vec3(0.2726f, 0.2805f - wheelOffset, 1.9307f); - auto model = AddComponent(wheel, "Model"); + auto model = AddComponent(wheel); model->ModelFile = "Models/Jeep/WheelBack/wheelBack.obj"; - auto Wheel = AddComponent(wheel, "Wheel"); + auto Wheel = AddComponent(wheel); Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); Wheel->AxleID = 1; Wheel->Mass = 50; @@ -180,12 +222,12 @@ void GameWorld::Initialize() { auto wheel = CreateEntity(jeep); - auto transform = AddComponent(wheel, "Transform"); + auto transform = AddComponent(wheel); 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"); + auto model = AddComponent(wheel); model->ModelFile = "Models/Jeep/WheelBack/wheelBack.obj"; - auto Wheel = AddComponent(wheel, "Wheel"); + auto Wheel = AddComponent(wheel); Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); Wheel->AxleID = 1; Wheel->Mass = 50; @@ -203,25 +245,28 @@ void GameWorld::Initialize() { auto tank = CreateEntity(); - auto transform = AddComponent(tank, "Transform"); + auto transform = AddComponent(tank); transform->Position = glm::vec3(0, 5, 0); - transform->Orientation = glm::angleAxis(glm::pi()/2, glm::vec3(0, 1, 0)); - auto physics = AddComponent(tank, "Physics"); - physics->Mass = 45000; + //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"); - vehicle->MaxTorque = 5200.f; - AddComponent(tank, "TankSteering"); - AddComponent(tank, "Input"); + auto vehicle = AddComponent(tank); + vehicle->MaxTorque = 36000.f; + vehicle->MaxSteeringAngle = 90.f; + vehicle->MaxSpeedFullSteeringAngle = 4.f; + auto tankSteering = AddComponent(tank); + tankSteering->Player = player1; + AddComponent(tank); { auto shape = CreateEntity(tank); - auto transform = AddComponent(shape, "Transform"); - auto meshShape = AddComponent(shape, "MeshShape"); + auto transform = AddComponent(shape); + auto meshShape = AddComponent(shape); meshShape->ResourceName = "Models/Tank/Fix/ChassiCollision.obj"; CommitEntity(shape); - // auto box = AddComponent(jeep, "Box"); + // auto box = AddComponent(jeep); // box->Width = 1.487f; // box->Height = 0.727f; // box->Depth = 2.594f; @@ -230,37 +275,87 @@ void GameWorld::Initialize() { auto chassis = CreateEntity(tank); - auto transform = AddComponent(chassis, "Transform"); - transform->Position = glm::vec3(0, 0, 0); // 0.6577f - auto model = AddComponent(chassis, "Model"); - model->ModelFile = "Models/Tank/Fix/Chassi.obj"; + auto transform = AddComponent(chassis); + transform->Position = glm::vec3(0, 0, 0); + auto model = AddComponent(chassis); + model->ModelFile = "Models/Tank/tankBody.obj"; } { - auto top = CreateEntity(tank); - auto transform = AddComponent(top, "Transform"); - transform->Position = glm::vec3(0, 1.2, 1.95); // 0.6577f - auto model = AddComponent(top, "Model"); - model->ModelFile = "Models/Tank/Fix/Top.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 top = CreateEntity(tank); - auto transform = AddComponent(top, "Transform"); - transform->Position = glm::vec3(0, 1, 0.5); // 0.6577f - auto model = AddComponent(top, "Model"); - model->ModelFile = "Models/Tank/Fix/Barrel.obj"; + 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); + { + 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"); - 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; + 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); @@ -270,16 +365,16 @@ 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); - auto transform = AddComponent(wheel, "Transform"); - transform->Position = glm::vec3(1.88f, -0.83f - wheelOffset, -2.6f); + 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"); + auto model = AddComponent(wheel); model->ModelFile = "Models/Tank/Fix/WheelPhysics.obj"; - auto Wheel = AddComponent(wheel, "Wheel"); + auto Wheel = AddComponent(wheel); Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); Wheel->AxleID = 0; Wheel->Mass = 2000; @@ -289,16 +384,27 @@ void GameWorld::Initialize() 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"); - transform->Position = glm::vec3(1.88f, -0.83f - wheelOffset, -0.83f); + 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"); + auto model = AddComponent(wheel); model->ModelFile = "Models/Tank/Fix/WheelPhysics.obj"; - auto Wheel = AddComponent(wheel, "Wheel"); + auto Wheel = AddComponent(wheel); Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); Wheel->AxleID = 0; Wheel->Mass = 2000; @@ -308,17 +414,28 @@ void GameWorld::Initialize() 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"); - transform->Position = glm::vec3(-1.88f, -0.83f - wheelOffset, -2.6f); + 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"); + auto model = AddComponent(wheel); model->ModelFile = "Models/Tank/Fix/WheelPhysics.obj"; - auto Wheel = AddComponent(wheel, "Wheel"); + auto Wheel = AddComponent(wheel); Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); Wheel->AxleID = 0; Wheel->Mass = 2000; @@ -328,25 +445,47 @@ void GameWorld::Initialize() 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"); - transform->Position = glm::vec3(-1.88f, -0.83f - wheelOffset, -0.83f); + 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"); + auto model = AddComponent(wheel); model->ModelFile = "Models/Tank/Fix/WheelPhysics.obj"; - auto Wheel = AddComponent(wheel, "Wheel"); + 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->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); } @@ -354,11 +493,11 @@ void GameWorld::Initialize() //Back { auto wheel = CreateEntity(tank); - auto transform = AddComponent(wheel, "Transform"); - transform->Position = glm::vec3(1.88f, -0.83f - wheelOffset, 1.f); - auto model = AddComponent(wheel, "Model"); + 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"); + auto Wheel = AddComponent(wheel); Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); Wheel->AxleID = 1; Wheel->Mass = 2000; @@ -368,15 +507,26 @@ void GameWorld::Initialize() 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"); - transform->Position = glm::vec3(1.88f, -0.83f - wheelOffset, 2.95f); - auto model = AddComponent(wheel, "Model"); + 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"); + auto Wheel = AddComponent(wheel); Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); Wheel->AxleID = 1; Wheel->Mass = 2000; @@ -386,77 +536,25 @@ void GameWorld::Initialize() 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, "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 = 10; - emitterComponent->SpreadAngle = glm::pi(); - emitterComponent->UseGoalVelocity = false; - emitterComponent->LifeTime = 0.5; - emitterComponent->Speed = 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.88f, -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 = 3.f; - Wheel->ConnectedToHandbrake = true; - Wheel->TorqueRatio = 0.125f; - CommitEntity(wheel); - } - { - auto wheel = CreateEntity(tank); - auto transform = AddComponent(wheel, "Transform"); - transform->Position = glm::vec3(-1.88f, -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 = 3.f; - Wheel->ConnectedToHandbrake = true; - Wheel->TorqueRatio = 0.125f; - CommitEntity(wheel); - - auto entity = CreateEntity(tank); - auto transformComponent = AddComponent(entity, "Transform"); + 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, "ParticleEmitter"); + auto emitterComponent = AddComponent(entity); emitterComponent->SpawnCount = 2; emitterComponent->SpawnFrequency = 0.005; emitterComponent->SpreadAngle = glm::pi(); @@ -468,37 +566,524 @@ void GameWorld::Initialize() CommitEntity(entity); auto particleEntity = CreateEntity(entity); - auto TEMP = AddComponent(particleEntity, "Transform"); + auto TEMP = AddComponent(particleEntity); TEMP->Scale = glm::vec3(0); - auto spriteComponent = AddComponent(particleEntity, "Sprite"); + 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 camera = CreateEntity(tank); - auto transform = AddComponent(camera, "Transform"); - transform->Position.z = 20.f; - transform->Position.y = 7.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 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); + { + 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(viewport2)->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); + } /* for(int i = 0; i < 10; i++) { auto entity = CreateEntity(); - auto transform = AddComponent(entity, "Transform"); + auto transform = AddComponent(entity); 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)); @@ -506,13 +1091,13 @@ void GameWorld::Initialize() std::stringstream ss; ss << "Models/Placeholders/ShatterTest/" << i+1 << ".obj"; - auto model = AddComponent(entity, "Model"); + auto model = AddComponent(entity); model->ModelFile = ss.str(); - auto physics = AddComponent(entity, "Physics"); + auto physics = AddComponent(entity); physics->Mass = 100; physics->Static = true; - auto meshShape = AddComponent(entity, "MeshShape"); + auto meshShape = AddComponent(entity); meshShape->ResourceName = ss.str(); CommitEntity(entity); @@ -525,22 +1110,22 @@ void GameWorld::Initialize() for (int x = -5; x < 5; x++) { auto brick = CreateEntity(); - auto transform = AddComponent(brick, "Transform"); + auto transform = AddComponent(brick); 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"); + auto model = AddComponent(brick); model->ModelFile = "Models/Placeholders/PhysicsTest/Cube2.obj"; - auto physics = AddComponent(brick, "Physics"); + auto physics = AddComponent(brick); physics->Mass = 3; auto shape = CreateEntity(brick); - auto transformshape = AddComponent(shape, "Transform"); - auto box = AddComponent(shape, "BoxShape"); + auto transformshape = AddComponent(shape); + auto box = AddComponent(shape); box->Width = 0.5f; box->Height = 0.15f; box->Depth = 0.3f; @@ -554,16 +1139,16 @@ void GameWorld::Initialize() for (int y = 0; y < 5; y++) { auto cube = CreateEntity(); - auto transform = AddComponent(cube, "Transform"); + auto transform = AddComponent(cube); 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"); + auto model = AddComponent(cube); model->ModelFile = "Models/Placeholders/PhysicsTest/Cube2.obj"; - auto physics = AddComponent(cube, "Physics"); + auto physics = AddComponent(cube); physics->Mass = 100; - auto box = AddComponent(cube, "BoxShape"); + auto box = AddComponent(cube); box->Width = 1.5f; box->Height = 1.5f; box->Depth = 1.5f; @@ -575,7 +1160,7 @@ void GameWorld::Initialize() /*{ auto entity = CreateEntity(); AddComponent(entity, "Transform"); - auto emitter = AddComponent(entity, "SoundEmitter"); + auto emitter = AddComponent(entity); emitter->Path = "Sounds/korvring.wav"; emitter->Loop = true; GetSystem("SoundSystem")->PlaySound(emitter); @@ -591,54 +1176,75 @@ void GameWorld::Update(double dt) void GameWorld::RegisterComponents() { - m_ComponentFactory.Register("Transform", []() { return new Components::Transform(); }); - m_ComponentFactory.Register("Template", []() { return new Components::Template(); }); + m_ComponentFactory.Register([]() { return new Components::Transform(); }); + m_ComponentFactory.Register([]() { return new Components::Template(); }); + m_ComponentFactory.Register([]() { return new Components::Player(); }); } void GameWorld::RegisterSystems() { - m_SystemFactory.Register("TransformSystem", [this]() { return new Systems::TransformSystem(this, m_EventBroker); }); - //m_SystemFactory.Register("LevelGenerationSystem", [this]() { return new Systems::LevelGenerationSystem(this); }); - m_SystemFactory.Register("InputSystem", [this]() { return new Systems::InputSystem(this, m_EventBroker); }); - m_SystemFactory.Register("DebugSystem", [this]() { return new Systems::DebugSystem(this, m_EventBroker); }); - //m_SystemFactory.Register("CollisionSystem", [this]() { return new Systems::CollisionSystem(this); }); - m_SystemFactory.Register("ParticleSystem", [this]() { return new Systems::ParticleSystem(this, m_EventBroker); }); - //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("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); }); + m_SystemFactory.Register([this]() { return new Systems::TransformSystem(this, m_EventBroker); }); + //m_SystemFactory.Register([this]() { return new Systems::LevelGenerationSystem(this); }); + m_SystemFactory.Register([this]() { return new Systems::InputSystem(this, m_EventBroker); }); + m_SystemFactory.Register([this]() { return new Systems::DebugSystem(this, m_EventBroker); }); + //m_SystemFactory.Register([this]() { return new Systems::CollisionSystem(this); }); + m_SystemFactory.Register([this]() { return new Systems::ParticleSystem(this, m_EventBroker); }); + //m_SystemFactory.Register([this]() { return new Systems::PlayerSystem(this); }); + m_SystemFactory.Register([this]() { return new Systems::FreeSteeringSystem(this, m_EventBroker); }); + m_SystemFactory.Register([this]() { return new Systems::TankSteeringSystem(this, m_EventBroker); }); + m_SystemFactory.Register([this]() { return new Systems::SoundSystem(this, m_EventBroker); }); + m_SystemFactory.Register([this]() { return new Systems::PhysicsSystem(this, m_EventBroker); }); + m_SystemFactory.Register([this]() { return new Systems::RenderSystem(this, m_EventBroker, m_Renderer); }); } void GameWorld::AddSystems() { - AddSystem("TransformSystem"); - //AddSystem("LevelGenerationSystem"); - AddSystem("InputSystem"); - AddSystem("DebugSystem"); - //AddSystem("CollisionSystem"); - AddSystem("ParticleSystem"); - //AddSystem("PlayerSystem"); - AddSystem("FreeSteeringSystem"); - AddSystem("TankSteeringSystem"); - AddSystem("SoundSystem"); - AddSystem("PhysicsSystem"); - AddSystem("RenderSystem"); + AddSystem(); + //AddSystem(); + AddSystem(); + AddSystem(); + //AddSystem(); + AddSystem(); + //AddSystem(); + AddSystem(); + AddSystem(); + AddSystem(); + AddSystem(); + AddSystem(); } -void GameWorld::BindKey(int keyCode, std::string command) +void GameWorld::BindKey(int keyCode, std::string command, float value) { Events::BindKey e; e.KeyCode = keyCode; e.Command = command; + e.Value = value; m_EventBroker->Publish(e); } -void GameWorld::BindMouseButton(int button, std::string command) +void GameWorld::BindMouseButton(int button, std::string command, float value) { Events::BindMouseButton e; e.Button = button; e.Command = command; + e.Value = value; + m_EventBroker->Publish(e); +} + +void GameWorld::BindGamepadAxis(Gamepad::Axis axis, std::string command, float value) +{ + Events::BindGamepadAxis e; + e.Axis = axis; + e.Command = command; + e.Value = value; + m_EventBroker->Publish(e); +} + +void GameWorld::BindGamepadButton(Gamepad::Button button, std::string command, float value) +{ + Events::BindGamepadButton e; + e.Button = button; + e.Command = command; + e.Value = value; m_EventBroker->Publish(e); } diff --git a/src/GameWorld.h b/src/GameWorld.h index 5bb3cbd..7ea100f 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" @@ -28,6 +29,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" @@ -35,6 +37,10 @@ #include "Components/Vehicle.h" #include "Components/Wheel.h" #include "Components/HingeConstraint.h" +#include "Components/TankSteering.h" +#include "Components/TowerSteering.h" +#include "Components/BarrelSteering.h" +#include "Components/Player.h" class GameWorld : public World { @@ -53,8 +59,10 @@ public: private: std::shared_ptr m_Renderer; - void BindKey(int keyCode, std::string command); - void BindMouseButton(int button, std::string command); + void BindKey(int keyCode, std::string command, float value); + void BindMouseButton(int button, std::string command, float value); + void BindGamepadAxis(Gamepad::Axis axis, std::string command, float value); + void BindGamepadButton(Gamepad::Button button, std::string command, float value); }; #endif // GameWorld_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 9f09b1c..b20e072 100644 --- a/src/InputManager.cpp +++ b/src/InputManager.cpp @@ -1,8 +1,20 @@ #include "PrecompiledHeader.h" #include "InputManager.h" +#include + +void InputManager::Initialize() +{ + m_LastGamepadAxisState = std::array(); + m_LastGamepadButtonState = std::array(); + + EVENT_SUBSCRIBE_MEMBER(m_ELockMouse, &InputManager::OnLockMouse); + EVENT_SUBSCRIBE_MEMBER(m_EUnlockMouse, &InputManager::OnUnlockMouse); +} void InputManager::Update(double dt) { + EventBroker->Process(); + m_LastKeyState = m_CurrentKeyState; m_LastMouseState = m_CurrentMouseState; m_LastMouseX = m_CurrentMouseX; @@ -19,13 +31,13 @@ void InputManager::Update(double dt) { Events::KeyDown e; e.KeyCode = i; - m_EventBroker->Publish(e); + EventBroker->Publish(e); } else { Events::KeyUp e; e.KeyCode = i; - m_EventBroker->Publish(e); + EventBroker->Publish(e); } } } @@ -36,23 +48,29 @@ 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; - m_EventBroker->Publish(e); + e.X = x; + e.Y = y; + EventBroker->Publish(e); } else { Events::MouseRelease e; e.Button = i; - m_EventBroker->Publish(e); + e.X = x; + e.Y = y; + EventBroker->Publish(e); } } } - // Cursor position + // Mouse movement glfwGetCursorPos(m_GLFWWindow, &m_CurrentMouseX, &m_CurrentMouseY); m_CurrentMouseDeltaX = m_CurrentMouseX - m_LastMouseX; m_CurrentMouseDeltaY = m_CurrentMouseY - m_LastMouseY; @@ -64,7 +82,7 @@ void InputManager::Update(double dt) e.Y = m_CurrentMouseY; e.DeltaX = m_CurrentMouseDeltaX; e.DeltaY = m_CurrentMouseDeltaY; - m_EventBroker->Publish(e); + EventBroker->Publish(e); } // // Lock mouse while holding LMB @@ -83,4 +101,131 @@ void InputManager::Update(double dt) // { // glfwSetInputMode(m_GLFWWindow, GLFW_CURSOR, GLFW_CURSOR_NORMAL); // } + + // Xbox360 controller + //using namespace ; + DWORD dwResult; + for (int i = 0; i < MAX_GAMEPADS; i++) + { + XINPUT_STATE state = { 0 }; + // Simply get the state of the controller from XInput. + 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; + m_CurrentGamepadAxisState[i][static_cast(Gamepad::Axis::RightY)] = state.Gamepad.sThumbRY / 32767.f; + m_CurrentGamepadAxisState[i][static_cast(Gamepad::Axis::LeftTrigger)] = state.Gamepad.bLeftTrigger / 255.f; + m_CurrentGamepadAxisState[i][static_cast(Gamepad::Axis::RightTrigger)] = state.Gamepad.bRightTrigger / 255.f; + PublishGamepadAxisIfChanged(i, Gamepad::Axis::LeftX); + PublishGamepadAxisIfChanged(i, Gamepad::Axis::LeftY); + PublishGamepadAxisIfChanged(i, Gamepad::Axis::RightX); + PublishGamepadAxisIfChanged(i, Gamepad::Axis::RightY); + PublishGamepadAxisIfChanged(i, Gamepad::Axis::LeftTrigger); + PublishGamepadAxisIfChanged(i, Gamepad::Axis::RightTrigger); + + m_CurrentGamepadButtonState[i][static_cast(Gamepad::Button::Up)] = static_cast(state.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_UP); + m_CurrentGamepadButtonState[i][static_cast(Gamepad::Button::Down)] = static_cast(state.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_DOWN); + m_CurrentGamepadButtonState[i][static_cast(Gamepad::Button::Left)] = static_cast(state.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_LEFT); + m_CurrentGamepadButtonState[i][static_cast(Gamepad::Button::Right)] = static_cast(state.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_RIGHT); + m_CurrentGamepadButtonState[i][static_cast(Gamepad::Button::Start)] = static_cast(state.Gamepad.wButtons & XINPUT_GAMEPAD_START); + m_CurrentGamepadButtonState[i][static_cast(Gamepad::Button::Back)] = static_cast(state.Gamepad.wButtons & XINPUT_GAMEPAD_BACK); + m_CurrentGamepadButtonState[i][static_cast(Gamepad::Button::LeftThumb)] = static_cast(state.Gamepad.wButtons & XINPUT_GAMEPAD_LEFT_THUMB); + m_CurrentGamepadButtonState[i][static_cast(Gamepad::Button::RightThumb)] = static_cast(state.Gamepad.wButtons & XINPUT_GAMEPAD_RIGHT_THUMB); + m_CurrentGamepadButtonState[i][static_cast(Gamepad::Button::LeftShoulder)] = static_cast(state.Gamepad.wButtons & XINPUT_GAMEPAD_LEFT_SHOULDER); + m_CurrentGamepadButtonState[i][static_cast(Gamepad::Button::RightShoulder)] = static_cast(state.Gamepad.wButtons & XINPUT_GAMEPAD_RIGHT_SHOULDER); + m_CurrentGamepadButtonState[i][static_cast(Gamepad::Button::A)] = static_cast(state.Gamepad.wButtons & XINPUT_GAMEPAD_A); + m_CurrentGamepadButtonState[i][static_cast(Gamepad::Button::B)] = static_cast(state.Gamepad.wButtons & XINPUT_GAMEPAD_B); + m_CurrentGamepadButtonState[i][static_cast(Gamepad::Button::X)] = static_cast(state.Gamepad.wButtons & XINPUT_GAMEPAD_X); + m_CurrentGamepadButtonState[i][static_cast(Gamepad::Button::Y)] = static_cast(state.Gamepad.wButtons & XINPUT_GAMEPAD_Y); + PublishGamepadButtonIfChanged(i, Gamepad::Button::Up); + PublishGamepadButtonIfChanged(i, Gamepad::Button::Down); + PublishGamepadButtonIfChanged(i, Gamepad::Button::Left); + PublishGamepadButtonIfChanged(i, Gamepad::Button::Right); + PublishGamepadButtonIfChanged(i, Gamepad::Button::Start); + PublishGamepadButtonIfChanged(i, Gamepad::Button::Back); + PublishGamepadButtonIfChanged(i, Gamepad::Button::LeftThumb); + PublishGamepadButtonIfChanged(i, Gamepad::Button::RightThumb); + PublishGamepadButtonIfChanged(i, Gamepad::Button::LeftShoulder); + PublishGamepadButtonIfChanged(i, Gamepad::Button::RightShoulder); + PublishGamepadButtonIfChanged(i, Gamepad::Button::A); + PublishGamepadButtonIfChanged(i, Gamepad::Button::B); + PublishGamepadButtonIfChanged(i, Gamepad::Button::X); + PublishGamepadButtonIfChanged(i, Gamepad::Button::Y); + } + } + + m_LastKeyState = m_CurrentKeyState; + m_LastMouseState = m_CurrentMouseState; + m_LastMouseX = m_CurrentMouseX; + m_LastMouseY = m_CurrentMouseY; + m_LastGamepadAxisState = m_CurrentGamepadAxisState; + m_LastGamepadButtonState = m_CurrentGamepadButtonState; } + +void InputManager::PublishGamepadAxisIfChanged(int gamepadID, Gamepad::Axis axis) +{ + float currentValue = m_CurrentGamepadAxisState[gamepadID][static_cast(axis)]; + float lastValue = m_LastGamepadAxisState[gamepadID][static_cast(axis)]; + if (currentValue != lastValue) + { + Events::GamepadAxis e; + e.GamepadID = gamepadID; + e.Axis = axis; + e.Value = currentValue; + EventBroker->Publish(e); + } +} + +void InputManager::PublishGamepadButtonIfChanged(int gamepadID, Gamepad::Button button) +{ + bool currentState = m_CurrentGamepadButtonState[gamepadID][static_cast(button)]; + float lastState = m_LastGamepadButtonState[gamepadID][static_cast(button)]; + if (currentState != lastState) + { + if (currentState == true) + { + Events::GamepadButtonDown e; + e.GamepadID = gamepadID; + e.Button = button; + EventBroker->Publish(e); + } + else + { + Events::GamepadButtonUp e; + e.GamepadID = gamepadID; + e.Button = button; + EventBroker->Publish(e); + } + } +} + +bool InputManager::OnLockMouse(const Events::LockMouse &event) +{ + m_MouseLocked = true; + glfwSetInputMode(m_GLFWWindow, GLFW_CURSOR, GLFW_CURSOR_DISABLED); + + return true; +} + +bool InputManager::OnUnlockMouse(const Events::UnlockMouse &event) +{ + m_MouseLocked = false; + glfwSetInputMode(m_GLFWWindow, GLFW_CURSOR, GLFW_CURSOR_NORMAL); + + return true; +} \ No newline at end of file diff --git a/src/InputManager.h b/src/InputManager.h index 5acaaa2..69827ba 100644 --- a/src/InputManager.h +++ b/src/InputManager.h @@ -9,34 +9,59 @@ #include "Events/MousePress.h" #include "Events/MouseRelease.h" #include "Events/MouseMove.h" +#include "Events/LockMouse.h" +#include "Events/GamepadAxis.h" +#include "Events/GamepadButton.h" class InputManager { public: - InputManager(GLFWwindow* window, std::shared_ptr eventBroker) + InputManager(GLFWwindow* window, std::shared_ptr<::EventBroker> eventBroker) : m_GLFWWindow(window) - , m_EventBroker(eventBroker) + , EventBroker(eventBroker) , m_CurrentKeyState() , m_LastKeyState() , m_CurrentMouseState() , m_LastMouseState() , m_CurrentMouseX(0), m_CurrentMouseY(0) , m_LastMouseX(0), m_LastMouseY(0) - , m_CurrentMouseDeltaX(0), m_CurrentMouseDeltaY(0) { } + , m_CurrentMouseDeltaX(0), m_CurrentMouseDeltaY(0) + , m_MouseLocked(false) + { Initialize(); } + + void Initialize(); + + static const short MAX_GAMEPADS = 4; void Update(double dt); private: GLFWwindow* m_GLFWWindow; - std::shared_ptr m_EventBroker; - + std::shared_ptr<::EventBroker> EventBroker; + + EventRelay m_ELockMouse; + bool OnLockMouse(const Events::LockMouse &event); + EventRelay m_EUnlockMouse; + bool OnUnlockMouse(const Events::UnlockMouse &event); + std::array m_CurrentKeyState; std::array m_LastKeyState; std::array m_CurrentMouseState; std::array m_LastMouseState; + typedef std::array(Gamepad::Axis::LAST) + 1> GamepadAxisState; + std::array m_CurrentGamepadAxisState; + std::array m_LastGamepadAxisState; + typedef std::array(Gamepad::Button::LAST) + 1> GamepadButtonState; + std::array m_CurrentGamepadButtonState; + std::array m_LastGamepadButtonState; + double m_CurrentMouseX, m_CurrentMouseY; double m_LastMouseX, m_LastMouseY; double m_CurrentMouseDeltaX, m_CurrentMouseDeltaY; + bool m_MouseLocked; + + void PublishGamepadAxisIfChanged(int gamepadID, Gamepad::Axis axis); + void PublishGamepadButtonIfChanged(int gamepadID, Gamepad::Button button); }; #endif // InputManager_h__ diff --git a/src/Model.cpp b/src/Model.cpp index 753ec65..c13a192 100755 --- a/src/Model.cpp +++ b/src/Model.cpp @@ -22,6 +22,8 @@ Model::Model(ResourceManager* rm, OBJ &obj) 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(ResourceManager* rm, OBJ &obj) if (Vertices.size() > 0) { - CreateBuffers(Vertices, Normals, TextureCoords); + CreateTangents(); + //getSimilarVertexIndex(); + CreateBuffers(Vertices, Normals, TangentNormals, BiTangentNormals, TextureCoords); } else { @@ -71,7 +75,7 @@ Model::Model(ResourceManager* rm, OBJ &obj) } } -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/Physics/VehicleSetup.cpp b/src/Physics/VehicleSetup.cpp index 1babfcb..e319834 100644 --- a/src/Physics/VehicleSetup.cpp +++ b/src/Physics/VehicleSetup.cpp @@ -7,13 +7,13 @@ void VehicleSetup::buildVehicle(World *world, const hkpWorld* physicsWorld, hkpVehicleInstance& vehicle, EntityID vehicleEntity, std::vector wheelEntities) { - auto vehicleComponent = world->GetComponent(vehicleEntity, "Vehicle"); + auto vehicleComponent = world->GetComponent(vehicleEntity); WheelData wheelData; for (int i = 0; i < wheelEntities.size(); i++) { - wheelData.WheelComponent = world->GetComponent(wheelEntities[i], "Wheel"); - wheelData.TransformComponent = world->GetComponent(wheelEntities[i], "Transform"); + wheelData.WheelComponent = world->GetComponent(wheelEntities[i]); + wheelData.TransformComponent = world->GetComponent(wheelEntities[i]); m_Wheels.push_back(wheelData); } @@ -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; @@ -104,7 +104,7 @@ void VehicleSetup::setupVehicleData(const hkpWorld* world, hkpVehicleData& data data.m_torquePitchFactor = 0.5f; data.m_torqueYawFactor = 0.35f; - data.m_chassisUnitInertiaYaw = 1.0f; + data.m_chassisUnitInertiaYaw = 0.8f; data.m_chassisUnitInertiaRoll = 1.0f; data.m_chassisUnitInertiaPitch = 1.0f; @@ -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; @@ -246,9 +246,8 @@ void VehicleSetup::setupComponent(const hkpVehicleData& data, hkpVehicleDefaultS suspension.m_wheelParams[i].m_length = suspensionLength; suspension.m_wheelSpringParams[i].m_strength = m_Wheels[i].WheelComponent->SuspensionStrength; - const float wd = 3.0f; - suspension.m_wheelSpringParams[i].m_dampingCompression = wd; - suspension.m_wheelSpringParams[i].m_dampingRelaxation = wd; + suspension.m_wheelSpringParams[i].m_dampingCompression = vehicleComponent.SpringDamping; + suspension.m_wheelSpringParams[i].m_dampingRelaxation = vehicleComponent.SpringDamping; suspension.m_wheelParams[i].m_hardpointChassisSpace.set(m_Wheels[i].WheelComponent->Hardpoint.x, m_Wheels[i].WheelComponent->Hardpoint.y, m_Wheels[i].WheelComponent->Hardpoint.z); @@ -285,7 +284,7 @@ void VehicleSetup::setupComponent(const hkpVehicleData& data, hkpVehicleDefaultV // The threshold in m/s at which the algorithm switches from // using the normalSpinDamping to the collisionSpinDamping. - velocityDamper.m_collisionThreshold = 100.0f; + velocityDamper.m_collisionThreshold = 1.0f; } void VehicleSetup::setupWheelCollide(const hkpWorld* world, const hkpVehicleInstance& vehicle, hkpVehicleRayCastWheelCollide& wheelCollide) 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: diff --git a/src/Renderer.cpp b/src/Renderer.cpp index 2905891..19c00f0 100755 --- a/src/Renderer.cpp +++ b/src/Renderer.cpp @@ -13,12 +13,15 @@ Renderer::Renderer() m_DrawWireframe = false; m_DrawBounds = false; #endif - - m_ShadowMapRes = 2048; + Gamma = 2.2f; + CAtt = 1.0f; + LAtt = 0.0f; + QAtt = 3.0f; + m_ShadowMapRes = 2048*6; m_SunPosition = glm::vec3(0, 3.5f, 10); m_SunTarget = glm::vec3(0, 0, 0); - m_SunProjection = glm::ortho(-100, 100, -100, 100, -100, 100); - Lights = 0; + m_SunProjection = glm::ortho(10.f, -10.f, 10.f, -10.f, 10.f, -10.f); +/* Lights = 0;*/ } void Renderer::Initialize() @@ -76,7 +79,7 @@ void Renderer::Initialize() void Renderer::LoadContent() { - auto standardVS = std::shared_ptr(new VertexShader("Shaders/Vertex.glsl")); + /*auto standardVS = std::shared_ptr(new VertexShader("Shaders/Vertex.glsl")); auto standardFS = std::shared_ptr(new FragmentShader("Shaders/Fragment.glsl")); m_ShaderProgram.AddShader(standardVS); @@ -89,12 +92,7 @@ void Renderer::LoadContent() m_ShaderProgramNormals.AddShader(std::shared_ptr(new FragmentShader("Shaders/Normals.frag.glsl"))); m_ShaderProgramNormals.Compile(); m_ShaderProgramNormals.Link(); - - m_ShaderProgramShadows.AddShader(std::shared_ptr(new VertexShader("Shaders/ShadowMap.vert.glsl"))); - m_ShaderProgramShadows.AddShader(std::shared_ptr(new FragmentShader("Shaders/ShadowMap.frag.glsl"))); - m_ShaderProgramShadows.Compile(); - m_ShaderProgramShadows.Link(); - + m_ShaderProgramShadowsDrawDepth.AddShader(std::shared_ptr(new VertexShader("Shaders/VisualizeDepth.vert.glsl"))); m_ShaderProgramShadowsDrawDepth.AddShader(std::shared_ptr(new FragmentShader("Shaders/VisualizeDepth.frag.glsl"))); m_ShaderProgramShadowsDrawDepth.Compile(); @@ -108,13 +106,124 @@ void Renderer::LoadContent() m_ShaderProgramSkybox.AddShader(std::shared_ptr(new VertexShader("Shaders/Skybox.vert.glsl"))); m_ShaderProgramSkybox.AddShader(std::shared_ptr(new FragmentShader("Shaders/Skybox.frag.glsl"))); m_ShaderProgramSkybox.Compile(); - m_ShaderProgramSkybox.Link(); + m_ShaderProgramSkybox.Link();*/ - m_Skybox = std::make_shared("Textures/Skybox/Sunset", "jpg"); + m_ShaderProgramShadows.AddShader(std::shared_ptr(new VertexShader("Shaders/ShadowMap.vert.glsl"))); + m_ShaderProgramShadows.AddShader(std::shared_ptr(new FragmentShader("Shaders/ShadowMap.frag.glsl"))); + m_ShaderProgramShadows.Compile(); + m_ShaderProgramShadows.Link(); - m_DebugAABB = CreateAABB(); + m_FirstPassProgram.AddShader(std::shared_ptr(new VertexShader("Shaders/Vertex.glsl"))); + m_FirstPassProgram.AddShader(std::shared_ptr(new FragmentShader("Shaders/Fragment.glsl"))); + m_FirstPassProgram.Compile(); + + glBindFragDataLocation(m_FirstPassProgram.GetHandle(), 0, "frag_Diffuse"); + glBindFragDataLocation(m_FirstPassProgram.GetHandle(), 1, "frag_Position"); + glBindFragDataLocation(m_FirstPassProgram.GetHandle(), 2, "frag_Normal"); + m_FirstPassProgram.Link(); + + m_SecondPassProgram.AddShader(std::shared_ptr(new VertexShader("Shaders/Vertex2.glsl"))); + m_SecondPassProgram.AddShader(std::shared_ptr(new FragmentShader("Shaders/Fragment2.glsl"))); + m_SecondPassProgram.Compile(); + m_SecondPassProgram.Link(); + + m_SecondPassProgram_Debug.AddShader(std::shared_ptr(new VertexShader("Shaders/Vertex2.glsl"))); + m_SecondPassProgram_Debug.AddShader(std::shared_ptr(new FragmentShader("Shaders/Fragment2-Debug.glsl"))); + m_SecondPassProgram_Debug.Compile(); + m_SecondPassProgram_Debug.Link(); + + m_FinalPassProgram.AddShader(std::shared_ptr(new VertexShader("Shaders/FinalPass.vert.glsl"))); + m_FinalPassProgram.AddShader(std::shared_ptr(new FragmentShader("Shaders/FinalPass.frag.glsl"))); + m_FinalPassProgram.Compile(); + m_FinalPassProgram.Link(); m_ScreenQuad = CreateQuad(); CreateShadowMap(m_ShadowMapRes); + FrameBufferTextures(); +} + +void Renderer::Draw(double dt) +{ + if(glfwGetKey(m_Window, GLFW_KEY_F1)) + { + m_QuadView = false; + } + if(glfwGetKey(m_Window, GLFW_KEY_F2)) + { + m_QuadView = true; + } + + if(glfwGetKey(m_Window, GLFW_KEY_KP_1)) + { + Gamma -= 0.3f * dt; + LOG_INFO("Gamma_UP: %f", Gamma); + } + if(glfwGetKey(m_Window, GLFW_KEY_KP_4)) + { + Gamma += 0.3f * dt; + LOG_INFO("Gamma_DOWN: %f", Gamma); + } + + if(glfwGetKey(m_Window, GLFW_KEY_1)) + { + if(glfwGetKey(m_Window, GLFW_KEY_KP_ADD)) + { + CAtt += 0.5f * dt; + LOG_INFO("Const: %f", CAtt); + } + if(glfwGetKey(m_Window, GLFW_KEY_KP_SUBTRACT)) + { + CAtt -= 0.5f * dt; + LOG_INFO("Const: %f", CAtt); + } + } + if(glfwGetKey(m_Window, GLFW_KEY_2)) + { + if(glfwGetKey(m_Window, GLFW_KEY_KP_ADD)) + { + LAtt += 0.5f * dt; + LOG_INFO("Linear: %f", LAtt); + } + if(glfwGetKey(m_Window, GLFW_KEY_KP_SUBTRACT)) + { + LAtt -= 0.5f * dt; + LOG_INFO("Linear: %f", LAtt); + } + } + if(glfwGetKey(m_Window, GLFW_KEY_3)) + { + if(glfwGetKey(m_Window, GLFW_KEY_KP_ADD)) + { + QAtt += 0.5f * dt; + LOG_INFO("Quadratic: %f", QAtt); + } + if(glfwGetKey(m_Window, GLFW_KEY_KP_SUBTRACT)) + { + QAtt -= 0.5f * dt; + LOG_INFO("Quadratic: %f", QAtt); + } + } + + glDisable(GL_BLEND); + + DrawFBO(); + + ClearStuff(); + glfwSwapBuffers(m_Window); +} + +#pragma region TempRegion + +void Renderer::DrawSkybox() +{ + glBindFramebuffer(GL_FRAMEBUFFER, 0); + glViewport(0, 0, m_Width, m_Height); + + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + m_ShaderProgramSkybox.Bind(); + glm::mat4 cameraMatrix = m_Camera->ProjectionMatrix() * glm::toMat4(glm::inverse(m_Camera->Orientation())); + glUniformMatrix4fv(glGetUniformLocation(m_ShaderProgramSkybox.GetHandle(), "MVP"), 1, GL_FALSE, glm::value_ptr(cameraMatrix)); + glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); + m_Skybox->Draw(); } void Renderer::CreateShadowMap(int resolution) @@ -137,194 +246,35 @@ void Renderer::CreateShadowMap(int resolution) glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, m_ShadowDepthTexture, 0); glDrawBuffer(GL_NONE); - if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) - { + if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) { LOG_ERROR("Framebuffer incomplete!"); return; + } } -void Renderer::Draw(double dt) -{ - glDisable(GL_BLEND); - - DrawSkybox(); - DrawShadowMap(); - DrawScene(); - -#ifdef DEBUG - // Draw bounding boxes - if (m_DrawBounds) - { - glEnable(GL_BLEND); - glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ZERO); - m_ShaderProgramDebugAABB.Bind(); - for (auto tuple : AABBsToRender) - { - glm::mat4 modelMatrix; - bool colliding; - std::tie(modelMatrix, colliding) = tuple; - // Model matrix - glm::mat4 cameraMatrix = m_Camera->ProjectionMatrix() * m_Camera->ViewMatrix(); - glm::mat4 MVP = cameraMatrix * modelMatrix; - glUniformMatrix4fv(glGetUniformLocation(m_ShaderProgramDebugAABB.GetHandle(), "MVP"), 1, GL_FALSE, glm::value_ptr(MVP)); - // Color - glm::vec4 color(1.f, 1.f, 1.f, 0.f); - if (colliding) - color = glm::vec4(1.f, 0.f, 0.f, 0.f); - glUniform4fv(glGetUniformLocation(m_ShaderProgramDebugAABB.GetHandle(), "Color"), 1, glm::value_ptr(color)); - glBindVertexArray(m_DebugAABB); - glDrawArrays(GL_LINES, 0, 24); - } - } - - DrawDebugShadowMap(); -#endif - - ClearStuff(); - glfwSwapBuffers(m_Window); -} - -void Renderer::DrawSkybox() -{ - glBindFramebuffer(GL_FRAMEBUFFER, 0); - glViewport(0, 0, m_Width, m_Height); - - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - m_ShaderProgramSkybox.Bind(); - glm::mat4 cameraMatrix = m_Camera->ProjectionMatrix() * glm::toMat4(glm::inverse(m_Camera->Orientation())); - glUniformMatrix4fv(glGetUniformLocation(m_ShaderProgramSkybox.GetHandle(), "MVP"), 1, GL_FALSE, glm::value_ptr(cameraMatrix)); - glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); - m_Skybox->Draw(); -} - -void Renderer::DrawScene() -{ - glBindFramebuffer(GL_FRAMEBUFFER, 0); - glViewport(0, 0, m_Width, m_Height); - - glClear(GL_DEPTH_BUFFER_BIT); - //glClearColor(1.0f, 1.0f, 0.0f, 1.0f); - - glEnable(GL_DEPTH_TEST); - glEnable(GL_CULL_FACE); - glCullFace(GL_BACK); -#ifdef DEBUG - glDisable(GL_CULL_FACE); - glPolygonMode(GL_BACK, GL_LINE); -#endif - - // Draw models - glm::mat4 depthViewMatrix = glm::lookAt(m_SunPosition, m_SunTarget, glm::vec3(0, 1, 0)) * glm::translate(-m_Camera->Position() * glm::vec3(1, 0, 0)); - glm::mat4 depthCamera = m_SunProjection * depthViewMatrix; - glm::mat4 biasMatrix( - 0.5, 0.0, 0.0, 0.0, - 0.0, 0.5, 0.0, 0.0, - 0.0, 0.0, 0.5, 0.0, - 0.5, 0.5, 0.5, 1.0 - ); - - m_ShaderProgram.Bind(); - glUniform1i(glGetUniformLocation(m_ShaderProgram.GetHandle(), "numberOfLights"), Lights); - glUniform3fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "position"), Lights, Light_position.data()); - glUniform3fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "specular"), Lights, Light_specular.data()); - glUniform3fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "diffuse"), Lights, Light_diffuse.data()); - glUniform1fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "constantAttenuation"), Lights, Light_constantAttenuation.data()); - glUniform1fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "linearAttenuation"), Lights, Light_linearAttenuation.data()); - glUniform1fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "quadraticAttenuation"), Lights, Light_quadraticAttenuation.data()); - glUniform1fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "spotExponent"), Lights, Light_spotExponent.data()); - if (m_DrawWireframe) - { - glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); - } - glActiveTexture(GL_TEXTURE1); - glBindTexture(GL_TEXTURE_2D, m_ShadowDepthTexture); - //DrawModels(m_ShaderProgram); - glm::mat4 cameraMatrix = m_Camera->ProjectionMatrix() * m_Camera->ViewMatrix(); - glm::mat4 depthCameraMatrix = biasMatrix * depthCamera; - glm::mat4 MVP; - glm::mat4 depthMVP; - for (auto tuple : ModelsToRender) - { - Model* model; - glm::mat4 modelMatrix; - bool visible; - std::tie(model, modelMatrix, visible, std::ignore) = tuple; - if (!visible) - continue; - - MVP = cameraMatrix * modelMatrix; - depthMVP = depthCameraMatrix * modelMatrix; - glUniformMatrix4fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "MVP"), 1, GL_FALSE, glm::value_ptr(MVP)); - glUniformMatrix4fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "DepthMVP"), 1, GL_FALSE, glm::value_ptr(depthMVP)); - glUniformMatrix4fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "model"), 1, GL_FALSE, glm::value_ptr(modelMatrix)); - glUniformMatrix4fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "view"), 1, GL_FALSE, glm::value_ptr(m_Camera->ViewMatrix())); - glBindVertexArray(model->VAO); - for (auto texGroup : model->TextureGroups) - { - glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, *texGroup.Texture); - glDrawArrays(GL_TRIANGLES, texGroup.StartIndex, texGroup.EndIndex - texGroup.StartIndex + 1); - } - } - - for (auto tuple : TexturesToRender) - { - Texture* texture; - glm::mat4 modelMatrix; - glm::mat4 billboardMatrix; - std::tie(texture, modelMatrix, billboardMatrix) = tuple; - - //MVP = cameraMatrix * glm::inverse(glm::toMat4(m_Camera->Orientation()) * modelMatrix ); - MVP = cameraMatrix * modelMatrix * billboardMatrix; - - depthMVP = depthCameraMatrix * modelMatrix; - glUniformMatrix4fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "MVP"), 1, GL_FALSE, glm::value_ptr(MVP)); - glUniformMatrix4fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "DepthMVP"), 1, GL_FALSE, glm::value_ptr(depthMVP)); - glUniformMatrix4fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "model"), 1, GL_FALSE, glm::value_ptr(modelMatrix)); - glUniformMatrix4fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "view"), 1, GL_FALSE, glm::value_ptr(m_Camera->ViewMatrix())); - - glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, *texture); - glBindVertexArray(m_ScreenQuad); - glDrawArrays(GL_TRIANGLES, 0, 6); - } - - - - -#ifdef DEBUG - // Debug draw model normals - if (m_DrawNormals) - { - m_ShaderProgramNormals.Bind(); - glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); - DrawModels(m_ShaderProgramNormals); - } -#endif -} void Renderer::DrawShadowMap() { - glEnable(GL_DEPTH_TEST); - glEnable(GL_CULL_FACE); - glCullFace(GL_FRONT); + 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 + //Binds the FBO and sets the veiwport, witch in effect is how large the shadowmap is and what resolution it has. glBindFramebuffer(GL_FRAMEBUFFER, m_ShadowFrameBuffer); glViewport(0, 0, m_ShadowMapRes, m_ShadowMapRes); glClear(GL_DEPTH_BUFFER_BIT); //glClearColor(0.0f, 0.0f, 0.0f, 0.0f); - glm::mat4 depthViewMatrix = glm::lookAt(m_SunPosition, m_SunTarget, glm::vec3(0, 1, 0)) * glm::translate(-m_Camera->Position() * glm::vec3(1, 0, 0)); -// glm::mat4 depthViewMatrix = glm::lookAt(m_SunPosition, m_SunTarget, glm::vec3(0, 1, 0)); + //Creates the "camera" for the shadowmap from the direction of the sun. + glm::mat4 depthViewMatrix = glm::lookAt(m_SunPosition, m_SunTarget, glm::vec3(0, 1, 0)); glm::mat4 depthCamera = m_SunProjection * depthViewMatrix; - - //glm::mat4 cameraMatrix = depthProjectionMatrix * m_Camera->ViewMatrix(); - glm::mat4 MVP; m_ShaderProgramShadows.Bind(); - glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); + glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); //Draws filled polygons + + //For each model, render them to the shadowmap for (auto tuple : ModelsToRender) { Model* model; @@ -425,26 +375,22 @@ void Renderer::AddPointLightToDraw( glm::vec3 _position, glm::vec3 _specular, glm::vec3 _diffuse, - float _constantAttenuation, - float _linearAttenuation, - float _quadraticAttenuation, - float _spotExponent + float _specularExponent, + float _ConstantAttenuation, + float _LinearAttenuation, + float _QuadraticAttenuation ) { - Light_position.push_back(_position.x); - Light_position.push_back(_position.y); - Light_position.push_back(_position.z); - Light_specular.push_back(_specular.x); - Light_specular.push_back(_specular.y); - Light_specular.push_back(_specular.z); - Light_diffuse.push_back(_diffuse.x); - Light_diffuse.push_back(_diffuse.y); - Light_diffuse.push_back(_diffuse.z); - Light_constantAttenuation.push_back(_constantAttenuation); - Light_linearAttenuation.push_back(_linearAttenuation); - Light_quadraticAttenuation.push_back(_quadraticAttenuation); - Light_spotExponent.push_back(_spotExponent); - Lights = Light_constantAttenuation.size(); + Light light; + light.Position = _position; + light.Diffuse = _diffuse; + light.Specular = _specular; + light.SpecularExponent = _specularExponent; + light.ConstantAttenuation = _ConstantAttenuation; + light.LinearAttenuation = _LinearAttenuation; + light.QuadraticAttenuation = _QuadraticAttenuation; + light.SphereModelMatrix = CreateLightMatrix(light); + Lights.push_back(light); } void Renderer::AddAABBToDraw(glm::vec3 origin, glm::vec3 volumeVector, bool colliding) @@ -577,17 +523,393 @@ GLuint Renderer::CreateSkybox() return vao; } + void Renderer::ClearStuff() { AABBsToRender.clear(); ModelsToRender.clear(); TexturesToRender.clear(); - Light_position.clear(); - Light_specular.clear(); - Light_diffuse.clear(); - Light_constantAttenuation.clear(); - Light_linearAttenuation.clear(); - Light_quadraticAttenuation.clear(); - Light_spotExponent.clear(); - Lights = 0; -} \ No newline at end of file + Lights.clear(); +} + +#pragma endregion + +void Renderer::FrameBufferTextures() +{ + m_fbBasePass = 0; + m_fDepthBuffer = 0; + + glGenFramebuffers(1, &m_fbBasePass); + glGenRenderbuffers(1, &m_fDepthBuffer); + + glBindRenderbuffer(GL_RENDERBUFFER, m_fDepthBuffer); + glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, m_Width, m_Height); + + //Generate and bind diffuse texture + glGenTextures(1, &m_fDiffuseTexture); + glBindTexture(GL_TEXTURE_2D, m_fDiffuseTexture); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_Width, m_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); + + //Generate and bind position texture + glGenTextures(1, &m_fPositionTexture); + glBindTexture(GL_TEXTURE_2D, m_fPositionTexture); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, m_Width, m_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); + + //Generate and bind normal texture + glGenTextures(1, &m_fNormalsTexture); + glBindTexture(GL_TEXTURE_2D, m_fNormalsTexture); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, m_Width, m_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); + + //Generate and bind normal texture + glGenTextures(1, &m_fSpecularTexture); + glBindTexture(GL_TEXTURE_2D, m_fSpecularTexture); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, m_Width, m_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, m_Width, m_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);*/ + + //Bind fb + glBindFramebuffer(GL_FRAMEBUFFER, m_fbBasePass); + glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, m_fDepthBuffer); + + //Attach textures to the FB + 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); + if(fbStatus != GL_FRAMEBUFFER_COMPLETE) + { + LOG_ERROR("DeferredLighting:Init: m_fbBasePass incomplete: 0x%x\n", fbStatus); + //exit(1); + } + + m_fbLightingPass = 0; + glGenFramebuffers(1, &m_fbLightingPass); + + glGenTextures(1, &m_fLightingTexture); + glBindTexture(GL_TEXTURE_2D, m_fLightingTexture); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_Width, m_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); + + glBindFramebuffer(GL_FRAMEBUFFER, m_fbLightingPass); + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_fLightingTexture, 0); + + fbStatus = glCheckFramebufferStatus(GL_FRAMEBUFFER); + if(fbStatus != GL_FRAMEBUFFER_COMPLETE) + { + LOG_ERROR("DeferredLighting:Init: m_fbLightingPass incomplete: 0x%x\n", fbStatus); + //exit(1); + } + + + +} + +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); + 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); + + // 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); + + DrawFBOScene(viewport); + + /* + Lighting pass + */ + 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); + + 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(viewport); + + /* + Final pass + */ + glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); + glViewport(x, y, width, height); + glClear(GL_DEPTH_BUFFER_BIT); + + 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); + + 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); + } +} + +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 = viewport.Camera->ProjectionMatrix() * viewport.Camera->ViewMatrix(); + glm::mat4 MVP; + glm::mat4 biasMatrix( + 0.5, 0.0, 0.0, 0.0, + 0.0, 0.5, 0.0, 0.0, + 0.0, 0.0, 0.5, 0.0, + 0.5, 0.5, 0.5, 1.0 + ); + + glm::mat4 depthViewMatrix = glm::lookAt(m_SunPosition, m_SunTarget, glm::vec3(0, 1, 0)); + glm::mat4 depthCamera = m_SunProjection * depthViewMatrix; + glm::mat4 depthCameraMatrix = biasMatrix * depthCamera; + glm::mat4 depthMVP; + + glActiveTexture(GL_TEXTURE1); + glBindTexture(GL_TEXTURE_2D, m_ShadowDepthTexture); + + for (auto tuple : ModelsToRender) + { + Model* model; + glm::mat4 modelMatrix; + bool visible; + std::tie(model, modelMatrix, visible, std::ignore) = tuple; + if (!visible) + continue; + + MVP = cameraMatrix * modelMatrix; + depthMVP = depthCameraMatrix * modelMatrix; + glUniformMatrix4fv(glGetUniformLocation(m_FirstPassProgram.GetHandle(), "MVP"), 1, GL_FALSE, glm::value_ptr(MVP)); + glUniformMatrix4fv(glGetUniformLocation(m_FirstPassProgram.GetHandle(), "DepthMVP"), 1, GL_FALSE, glm::value_ptr(depthMVP)); + glUniformMatrix4fv(glGetUniformLocation(m_FirstPassProgram.GetHandle(), "M"), 1, GL_FALSE, glm::value_ptr(modelMatrix)); + glUniformMatrix4fv(glGetUniformLocation(m_FirstPassProgram.GetHandle(), "V"), 1, GL_FALSE, glm::value_ptr(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) + { + 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); + } + } + + for (auto tuple : TexturesToRender) + { + Texture* texture; + glm::mat4 modelMatrix; + glm::mat4 billboardMatrix; + std::tie(texture, modelMatrix, billboardMatrix) = tuple; + + //MVP = cameraMatrix * glm::inverse(glm::toMat4(m_Camera->Orientation()) * modelMatrix ); + MVP = cameraMatrix * modelMatrix * billboardMatrix; + + depthMVP = depthCameraMatrix * modelMatrix; + glUniformMatrix4fv(glGetUniformLocation(m_FirstPassProgram.GetHandle(), "MVP"), 1, GL_FALSE, glm::value_ptr(MVP)); + glUniformMatrix4fv(glGetUniformLocation(m_FirstPassProgram.GetHandle(), "DepthMVP"), 1, GL_FALSE, glm::value_ptr(depthMVP)); + glUniformMatrix4fv(glGetUniformLocation(m_FirstPassProgram.GetHandle(), "M"), 1, GL_FALSE, glm::value_ptr(modelMatrix)); + glUniformMatrix4fv(glGetUniformLocation(m_FirstPassProgram.GetHandle(), "V"), 1, GL_FALSE, glm::value_ptr(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); + glBindVertexArray(m_ScreenQuad); + glDrawArrays(GL_TRIANGLES, 0, 6); + } +} + + + +void Renderer::DrawLightScene(Viewport &viewport) +{ + glEnable(GL_BLEND); + glBlendEquation (GL_FUNC_ADD); + glBlendFunc(GL_ONE,GL_ONE); + + glDisable (GL_DEPTH_TEST); + glDepthMask (GL_FALSE); + glBindVertexArray(m_sphereModel->VAO); + + glm::mat4 cameraMatrix = viewport.Camera->ProjectionMatrix() * viewport.Camera->ViewMatrix(); + glm::mat4 MVP; + + for (auto &light : Lights) + { + MVP = cameraMatrix * light.SphereModelMatrix; + + 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(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"), 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); +// glUniform1f(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "QuadraticAttenuation"), light.QuadraticAttenuation); + glUniform1f(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "ConstantAttenuation"), CAtt); + glUniform1f(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "LinearAttenuation"), LAtt); + glUniform1f(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "QuadraticAttenuation"), QAtt); + + glDrawArrays(GL_TRIANGLES, 0, m_sphereModel->Vertices.size()); + }; + glEnable (GL_DEPTH_TEST); + glDepthMask (GL_TRUE); + glDisable (GL_BLEND); +} + +void Renderer::SetSphereModel( Model* _model ) +{ + m_sphereModel = _model; +} + +glm::mat4 Renderer::CreateLightMatrix(Light &_light) +{ +// float c = _light.ConstantAttenuation; +// float l = _light.LinearAttenuation; +// float q = _light.QuadraticAttenuation; + float c = CAtt; + float l = LAtt; + float q = QAtt; + float cutOffRadius = abs(sqrt((-4*c*q) + pow(l, 2) + (1024*q) - l) / (2*q)); + + glm::mat4 model; + model *= glm::translate(_light.Position); + model *= glm::scale(glm::vec3(cutOffRadius)); + 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. +} + +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 26891d6..3bfa5ce 100755 --- a/src/Renderer.h +++ b/src/Renderer.h @@ -12,6 +12,7 @@ #include "Model.h" #include "Components/PointLight.h" #include "Skybox.h" +#include "ResourceManager.h" class Renderer { @@ -26,14 +27,6 @@ public: std::list> ModelsToRender; std::list> TexturesToRender; - int Lights; - std::vector Light_position; - std::vector Light_specular; - std::vector Light_diffuse; - std::vector Light_constantAttenuation; - std::vector Light_linearAttenuation; - std::vector Light_quadraticAttenuation; - std::vector Light_spotExponent; std::list> AABBsToRender; Renderer(); @@ -42,6 +35,11 @@ public: void Draw(double dt); void DrawText(); + 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(); @@ -49,10 +47,10 @@ public: glm::vec3 _position, glm::vec3 _specular, glm::vec3 _diffuse, - float _constantAttenuation, - float _linearAttenuation, - float _quadraticAttenuation, - float _spotExponent + float _specularExponent, + float _ConstantAttenuation, + float _LinearAttenuation, + float _QuadraticAttenuation ); void AddAABBToDraw(glm::vec3 origin, glm::vec3 volumeVector, bool colliding); @@ -69,8 +67,37 @@ public: void DrawBounds(bool val) { m_DrawBounds = val; } void DrawSkybox(); + void SetSphereModel(Model* _model); + private: int m_Width, m_Height; + + struct Viewport + { + float Left; + float Top; + float Right; + float Bottom; + std::shared_ptr Camera; + }; + + std::unordered_map m_Viewports; + std::unordered_map> m_Cameras; + + struct Light + { + glm::vec3 Position; + glm::vec3 Specular; + glm::vec3 Diffuse; + float SpecularExponent; + glm::mat4 SphereModelMatrix; + float ConstantAttenuation, LinearAttenuation, QuadraticAttenuation; + }; + + float Gamma; + + std::list Lights; + GLFWwindow* m_Window; GLint m_glVersion[2]; GLchar* m_glVendor; @@ -78,6 +105,7 @@ private: bool m_DrawNormals; bool m_DrawWireframe; bool m_DrawBounds; + float CAtt, LAtt, QAtt; std::shared_ptr m_Skybox; @@ -87,24 +115,57 @@ private: glm::mat4 m_SunProjection; GLuint m_DebugAABB; - GLuint m_ScreenQuad; GLuint m_ShadowFrameBuffer; GLuint m_ShadowDepthTexture; + GLuint m_fbBasePass; + GLuint m_fDiffuseTexture; + GLuint m_fPositionTexture; + GLuint m_fNormalsTexture; + GLuint m_fSpecularTexture; + GLuint m_fBlendTexture; + GLuint m_fbLightingPass; + GLuint m_fLightingTexture; + GLuint m_fShadowTexture; + + GLuint m_fDepthBuffer; + GLenum draw_bufs[2]; + GLuint m_ScreenQuad; + Model* m_sphereModel; + + bool m_QuadView; + std::shared_ptr m_Camera; ShaderProgram m_ShaderProgram; + ShaderProgram m_FirstPassProgram; + ShaderProgram m_SecondPassProgram; + ShaderProgram m_SecondPassProgram_Debug; + ShaderProgram m_FinalPassProgram; + ShaderProgram m_ShaderProgramNormals; ShaderProgram m_ShaderProgramShadows; ShaderProgram m_ShaderProgramShadowsDrawDepth; ShaderProgram m_ShaderProgramDebugAABB; ShaderProgram m_ShaderProgramSkybox; + + void ClearStuff(); void DrawScene(); void DrawModels(ShaderProgram &shader); void DrawShadowMap(); void CreateShadowMap(int resolution); + void FrameBufferTextures(); + void DrawFBO(); + void DrawFBOScene(Viewport &viewport); + void DrawLightScene(Viewport &viewport); + void BindFragDataLocation(); + glm::mat4 CreateLightMatrix(Light &_light); + void UpdateSunProjection(); + void CreateNormalMapTangent(); + + GLuint CreateQuad(); void DrawDebugShadowMap(); GLuint CreateAABB(); diff --git a/src/ShaderProgram.cpp b/src/ShaderProgram.cpp index e9bd864..bd67e90 100755 --- a/src/ShaderProgram.cpp +++ b/src/ShaderProgram.cpp @@ -103,6 +103,11 @@ void ShaderProgram::AddShader(std::shared_ptr shader) void ShaderProgram::Compile() { + if (m_ShaderProgramHandle == 0) + { + m_ShaderProgramHandle = glCreateProgram(); + } + for (auto &shader : m_Shaders) { if (!shader->IsCompiled()) @@ -121,7 +126,7 @@ GLuint ShaderProgram::Link() } LOG_INFO("Linking shader program"); - m_ShaderProgramHandle = glCreateProgram(); + for (auto &shader : m_Shaders) { glAttachShader(m_ShaderProgramHandle, shader->GetHandle()); diff --git a/src/ShaderProgram.h b/src/ShaderProgram.h index 07dbf9b..a27300c 100755 --- a/src/ShaderProgram.h +++ b/src/ShaderProgram.h @@ -61,7 +61,7 @@ class ShaderProgram { public: ShaderProgram() - : m_ShaderProgramHandle(0) { } + : m_ShaderProgramHandle(0) { } ~ShaderProgram(); void AddShader(std::shared_ptr shader); diff --git a/src/Shaders/FinalPass.frag.glsl b/src/Shaders/FinalPass.frag.glsl new file mode 100644 index 0000000..0bdb1b1 --- /dev/null +++ b/src/Shaders/FinalPass.frag.glsl @@ -0,0 +1,29 @@ +#version 430 + +uniform vec3 La; +uniform float Gamma; + +layout (binding=0) uniform sampler2D DiffuseTexture; +layout (binding=1) uniform sampler2D LightingTexture; +layout (binding=2) uniform sampler2D ShadowTexture; + +in VertexData +{ + vec3 Position; + vec2 TextureCoord; +} Input; + +out vec4 FragmentColor; + +void main() +{ + vec4 DiffuseTexel = texture(DiffuseTexture, Input.TextureCoord); + vec4 LightingTexel = texture(LightingTexture, Input.TextureCoord); + vec4 ShadowTexel = texture(ShadowTexture, Input.TextureCoord); + + + vec4 _FragmentColor = DiffuseTexel * vec4(La, 1.0) + LightingTexel; + FragmentColor = vec4(pow(_FragmentColor.rgb, vec3(1.0 / Gamma)), _FragmentColor.a); + //FragmentColor = DiffuseTexel; + +} \ No newline at end of file diff --git a/src/Shaders/FinalPass.vert.glsl b/src/Shaders/FinalPass.vert.glsl new file mode 100644 index 0000000..05deece --- /dev/null +++ b/src/Shaders/FinalPass.vert.glsl @@ -0,0 +1,16 @@ +#version 430 + +layout(location = 0) in vec3 Position; + +out VertexData +{ + vec3 Position; + vec2 TextureCoord; +} Output; + +void main() +{ + gl_Position = vec4(Position, 1.0); + Output.Position = Position; + Output.TextureCoord = (vec2(Position) + 1) / 2; +} \ No newline at end of file diff --git a/src/Shaders/Fragment.glsl b/src/Shaders/Fragment.glsl index 1909e84..87a95fa 100755 --- a/src/Shaders/Fragment.glsl +++ b/src/Shaders/Fragment.glsl @@ -1,113 +1,55 @@ #version 430 -uniform mat4 model; -uniform mat4 view; +layout (binding=0) uniform sampler2D DiffuseTexture; +layout (binding=1) uniform sampler2D ShadowTexture; +layout (binding=2) uniform sampler2D NormalMapTexture; +layout (binding=3) uniform sampler2D SpecularMapTexture; -layout(binding=0) uniform sampler2D texture0; -layout(binding=1) uniform sampler2D shadowMap; - -const int maxNumberOfLights = 82; -uniform int numberOfLights; -uniform vec3 position[maxNumberOfLights]; -uniform vec3 specular[maxNumberOfLights]; -uniform vec3 diffuse[maxNumberOfLights]; -uniform float constantAttenuation[maxNumberOfLights]; -uniform float linearAttenuation[maxNumberOfLights]; -uniform float quadraticAttenuation[maxNumberOfLights]; -uniform float spotExponent[maxNumberOfLights]; in VertexData { vec3 Position; vec3 Normal; vec2 TextureCoord; - vec3 ShadowCoord; + vec4 ShadowCoord; + vec3 Tangent; + vec3 BiTangent; } Input; -vec3 scene_ambient = vec3(0.5, 0.5, 0.5); +out vec4 frag_Diffuse; +out vec4 frag_Position; +out vec4 frag_Normal; +out vec4 frag_specular; -out vec4 fragmentColor; +float Shadow(vec4 ShadowCoord) +{ + //float cosTheta = clamp(dot(Input.Normal, 1.0), 0.0, 1.0); + float bias = 0.0005; // cosTheta is dot( n,l ), clamped between 0 and 1 + bias = clamp(bias, 0.0, 0.01); + if( texture(ShadowTexture, Input.ShadowCoord.xy).z < ShadowCoord.z - bias) + { + return 0.3; + } + else + { + return 1.0; + } +} void main() { + + // Diffuse Texture + frag_Diffuse = texture(DiffuseTexture, Input.TextureCoord) * Shadow(Input.ShadowCoord); - // Texture - vec4 texel = texture2D(texture0, Input.TextureCoord); - //vec4 texel = (blend.x * texel0) + (blend.y * texel1) + (blend.z * texel2); + // G-buffer Position + frag_Position = vec4(Input.Position.xyz, 1.0); - // - // Phong shading - // + // G-buffer Normal + 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); - // Ambient light - vec3 La = scene_ambient; // Ambient light - vec3 Ks = vec3(0.3, 0.3, 0.3); // Specular reflectance - vec3 Kd = vec3(1.0, 1.0, 1.0); // Diffuse reflectance - vec3 Ka = vec3(1.0, 1.0, 1.0); // Ambient reflectance - vec3 Is; - vec3 Id; - - // Shadows - //float cosTheta = clamp(dot(Input.Normal, vec3(0, 1, 0)), 0.0, 1.0); - //float bias = 0.001 * tan(acos(cosTheta)); // cosTheta is dot( n,l ), clamped between 0 and 1 - //bias = clamp(bias, 0.0, 0.01); - float visibility = 1.0; - /*if (Input.ShadowCoord.x >= 0.0 && Input.ShadowCoord.x <= 1.0 && Input.ShadowCoord.y >= 0.0 && Input.ShadowCoord.y <= 1.0) - { - float bias = 0.00005; - vec4 shadowMapValue = texture(shadowMap, Input.ShadowCoord.xy); - if (shadowMapValue.z < clamp(Input.ShadowCoord.z - bias, 0, 1)) - { - visibility = 0.3; - } - }*/ - - vec3 totalLighting = La * Ka * visibility; - - float attenuation; - - for(int i = 0; i < numberOfLights && i < maxNumberOfLights; i++) - { - // Light - //vec3 lightPosition = vec3(0, 0, 2); - vec3 Ls = specular[i]; // Specular light - vec3 Ld = diffuse[i]; // Diffuse light - - vec3 lightPosView = vec3(view * vec4(position[i], 1.0)); - vec3 surfacePosition = vec3(model * vec4(Input.Position, 1.0)); - vec3 surfacePosView = vec3(view * vec4(surfacePosition, 1.0)); - vec3 surfaceToLight = normalize(lightPosView - surfacePosView); - mat3 normalMatrix = transpose(inverse(mat3(view * model))); - vec3 surfaceNormal = normalize(normalMatrix * Input.Normal); - - float dist = length(position[i] - surfacePosition); - - attenuation = 1.0 / (constantAttenuation[i] - + linearAttenuation[i] * dist - + quadraticAttenuation[i] * pow(dist, 2.0)); - //attenuation = attenuation * pow(clampedCosine, spotExponent[i]); - - // Diffuse light - float dotProd = dot(surfaceToLight, surfaceNormal); - dotProd = max(dotProd, 0.0); - - Id = Ld * Kd * abs(dotProd) * attenuation; - - // Specular light - vec3 reflection = reflect(-surfaceToLight, surfaceNormal); - float dotSpecular = dot(reflection, normalize(-surfacePosView)); - dotSpecular = max(dotSpecular, 0.0); - float specularFactor = pow(dotSpecular, 30.0); // Specular factor - - Is = attenuation * Ls * Ks * specularFactor; - - totalLighting = totalLighting + Id + Is; - } - - fragmentColor = vec4(totalLighting, 1.0) * texel; - - - //fragmentColor = vec4(Id, 1.0) * texel; - - //fragmentColor = texel; + //G-buffer Specular + frag_specular = texture(SpecularMapTexture, Input.TextureCoord); } \ No newline at end of file diff --git a/src/Shaders/Fragment2-Debug.glsl b/src/Shaders/Fragment2-Debug.glsl new file mode 100644 index 0000000..db35d6e --- /dev/null +++ b/src/Shaders/Fragment2-Debug.glsl @@ -0,0 +1,38 @@ +#version 430 + +layout (binding=0) uniform sampler2D DiffuseTexture; +layout (binding=1) uniform sampler2D PositionTexture; +layout (binding=2) uniform sampler2D NormalTexture; + +in VertexData +{ + vec3 Position; + vec3 Normal; + vec2 TextureCoord; +} Input; + +out vec4 FragColor; + +void DrawQuadrant(vec4 texel, vec2 quadrant) +{ + if (-quadrant.x * Input.Position.x < 0 && -quadrant.y * Input.Position.y < 0) + { + FragColor = texel; + } +} + +void main() +{ + vec4 DiffuseTexel = texture2D(DiffuseTexture, Input.TextureCoord); + vec4 PositionTexel = texture2D(PositionTexture, Input.TextureCoord); + vec4 NormalTexel = texture2D(NormalTexture, Input.TextureCoord); + + //FragColor = texture2D(DiffuseTexture, Input.TextureCoord * 2 + vec2(0, -1)); + DrawQuadrant(texture2D(DiffuseTexture, Input.TextureCoord * 2), vec2(-1, 1)); + DrawQuadrant(texture2D(PositionTexture, Input.TextureCoord * 2), vec2(1, 1)); + DrawQuadrant(texture2D(NormalTexture, Input.TextureCoord * 2), vec2(-1, -1)); + + vec4 AllTexel = texture2D(DiffuseTexture, Input.TextureCoord*2)*texture2D(PositionTexture, Input.TextureCoord*2)*texture2D(NormalTexture, Input.TextureCoord*2); + DrawQuadrant(AllTexel, vec2(1, -1)); +} + diff --git a/src/Shaders/Fragment2.glsl b/src/Shaders/Fragment2.glsl new file mode 100644 index 0000000..5ded0a2 --- /dev/null +++ b/src/Shaders/Fragment2.glsl @@ -0,0 +1,85 @@ +#version 430 + +layout (binding=0) uniform sampler2D PositionTexture; +layout (binding=1) uniform sampler2D NormalsTexture; + +uniform vec2 ViewportSize; +uniform mat4 MVP; +uniform mat4 M; +uniform mat4 V; +uniform mat4 P; +uniform vec3 la; +uniform vec3 ls; +uniform vec3 ld; +uniform vec3 lp; +uniform float specularExponent; +uniform vec3 CameraPosition; +uniform float ConstantAttenuation; +uniform float LinearAttenuation; +uniform float QuadraticAttenuation; + +const vec3 ks = vec3(1.0, 1.0, 1.0); +const vec3 kd = vec3(1.0, 1.0, 1.0); +const vec3 ka = vec3(1.0, 1.0, 1.0); +const float kshine = 1.0; + +in VertexData +{ + vec3 Position; + vec2 TextureCoord; +} Input; + +out vec4 FragColor; + +vec4 phong(vec3 position, vec3 normal) +{ + // Diffuse + vec3 lightPos = vec3(V * vec4(lp, 1.0)); + vec3 distanceToLight = lightPos - position; + vec3 directionToLight = normalize(distanceToLight); + float dotProd = dot(directionToLight, normal); + dotProd = max(dotProd, 0.0); + vec3 Id = kd * ld * dotProd; + + // Specular + //vec3 reflection = reflect(-directionToLight, normal); + vec3 surfaceToViewer = normalize(-position); + vec3 halfWay = normalize(surfaceToViewer + directionToLight); + float dotSpecular = max(dot(halfWay, normal), 0.0); + float specularFactor = pow(dotSpecular, specularExponent * 2.0); + vec3 Is = ks * ls * specularFactor; + + //Attenuation + float dist = distance(lightPos, position); + //float attenuation = -log(min(1.0, dist / LightRadius)); + + float attenuation = 1.0 / (ConstantAttenuation + (LinearAttenuation * dist) + (QuadraticAttenuation * dist * dist)); + + //float attenuation = 1.0 / (1.0 - 0.0001 * pow(dist, 2)); + + //float attenuation = clamp(0.0, 1.0, 1.0 / (0.001 + (0.001 * dist) + (0.001 * dist * dist))); + + //float attenuation = 1.0 / dot(directionToLight, directionToLight); + + //float att_s = 5; + //float attenuation = pow(dist, 2) / pow(5.0, 2); + //attenuation = 1.0 / (1.0 + attenuation * att_s); + //att_s = 1.0 / (1.0 + att_s); + //attenuation = attenuation / (1.0 - att_s); + + //float radius = 5.0; + //float alpha = dist / radius; + //float dampingFactor = 1.0 - pow(alpha, 3); + + return vec4((Id + Is) * attenuation, 1.0); +} + +void main() +{ + vec2 TextureCoord = gl_FragCoord.xy / ViewportSize; + vec4 PositionTexel = texture(PositionTexture, TextureCoord); + 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 295d523..4c63e0d 100755 --- a/src/Shaders/Vertex.glsl +++ b/src/Shaders/Vertex.glsl @@ -1,26 +1,35 @@ #version 430 uniform mat4 MVP; +uniform mat4 M; +uniform mat4 V; +uniform mat4 P; uniform mat4 DepthMVP; -layout(location = 0) in vec3 Position; -layout(location = 1) in vec3 Normal; -layout(location = 2) in vec2 TextureCoord; +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 { vec3 Position; vec3 Normal; vec2 TextureCoord; - vec3 ShadowCoord; + vec4 ShadowCoord; + vec3 Tangent; + vec3 BiTangent; } Output; void main() { gl_Position = MVP * vec4(Position, 1.0); - Output.Position = Position; - Output.Normal = Normal; + Output.Position = vec3(V * M * vec4(Position, 1.0)); + Output.Normal = normalize(vec3(inverse(transpose(V * M)) * vec4(Normal, 0.0))); Output.TextureCoord = TextureCoord; - Output.ShadowCoord = vec3(DepthMVP * vec4(Position, 1.0)); + 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/Shaders/Vertex2.glsl b/src/Shaders/Vertex2.glsl new file mode 100644 index 0000000..e617846 --- /dev/null +++ b/src/Shaders/Vertex2.glsl @@ -0,0 +1,22 @@ +#version 430 + +uniform mat4 MVP; + +layout (location = 0) in vec3 Position; +layout (location = 2) in vec2 TextureCoord; + +uniform mat4 depthBiasMVP; + +out VertexData +{ + vec3 Position; + vec2 TextureCoord; +} Output; + +void main() +{ + gl_Position = MVP * vec4(Position, 1.0); + Output.Position = Position; + Output.TextureCoord = (vec2(Position) + 1.0) / 2.0; + +} \ No newline at end of file diff --git a/src/Shaders/geometry_pass.frag.glsl b/src/Shaders/geometry_pass.frag.glsl deleted file mode 100644 index 4fe34ba..0000000 --- a/src/Shaders/geometry_pass.frag.glsl +++ /dev/null @@ -1,20 +0,0 @@ -#version 430 - -in vec2 TexCoord0; -in vec3 Normal0; -in vec3 WorldPos0; - -layout (location = 0) out vec3 WorldPosOut; -layout (location = 1) out vec3 DiffuseOut; -layout (location = 2) out vec3 NormalOut; -layout (location = 3) out vec3 TexCoordOut; - -uniform sampler2D gColorMap; - -void main() -{ - WorldPosOut = WorldPos0; - DiffuseOut = texture(gColorMap, TexCoord0).xyz; - NormalOut = normalize(Normal0); - TexCoordOut = vec3(TexCoord0, 0.0); -} \ No newline at end of file diff --git a/src/Shaders/geometry_pass.vert.glsl b/src/Shaders/geometry_pass.vert.glsl deleted file mode 100644 index c431ba9..0000000 --- a/src/Shaders/geometry_pass.vert.glsl +++ /dev/null @@ -1,20 +0,0 @@ -#version 430 - -layout (location = 0) in vec3 Position; -layout (location = 1) in vec2 TexCoord; -layout (location = 2) in vec3 Normal; - -uniform mat4 gWVP; -uniform mat4 gWorld; - -out vec2 TexCoord0; -out vec3 Normal0; -out vec3 WorldPos0; - -void main() -{ - gl_Position = gWVP * vec4(Position, 1.0); - TexCoord0 = TexCoord; - Normal0 = (gWorld * vec4(Normal, 0.0)).xyz; - WorldPos0 = (gWorld * vec4(Position, 1.0)).xyz; -} \ No newline at end of file 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.cpp b/src/Systems/FreeSteeringSystem.cpp index d59b475..efbfde1 100755 --- a/src/Systems/FreeSteeringSystem.cpp +++ b/src/Systems/FreeSteeringSystem.cpp @@ -4,7 +4,7 @@ void Systems::FreeSteeringSystem::RegisterComponents(ComponentFactory* cf) { - cf->Register("FreeSteering", []() { return new Components::FreeSteering(); }); + cf->Register([]() { return new Components::FreeSteering(); }); } void Systems::FreeSteeringSystem::Initialize() @@ -19,100 +19,90 @@ void Systems::FreeSteeringSystem::Update(double dt) void Systems::FreeSteeringSystem::UpdateEntity(double dt, EntityID entity, EntityID parent) { - auto steering = m_World->GetComponent(entity, "FreeSteering"); + auto steering = m_World->GetComponent(entity); if (steering) { - auto transform = m_World->GetComponent(entity, "Transform"); + auto transform = m_World->GetComponent(entity); - glm::vec3 cameraRight = glm::vec3(m_InputController->Orientation * glm::vec4(1, 0, 0, 0)); - glm::vec3 cameraForward = glm::vec3(m_InputController->Orientation * glm::vec4(0, 0, -1, 0)); + glm::vec3 cameraRight = glm::vec3(transform->Orientation * glm::vec4(1, 0, 0, 0)); + glm::vec3 cameraForward = glm::vec3(transform->Orientation * glm::vec4(0, 0, -1, 0)); glm::vec3 movement; movement += cameraRight * m_InputController->Movement.x; movement.y += m_InputController->Movement.y; movement += cameraForward * -m_InputController->Movement.z; - transform->Position += movement * steering->Speed * m_InputController->SpeedMultiplier * (float)dt; - transform->Orientation = m_InputController->Orientation; + float speedMultiplier = 1.f; + if (m_InputController->SpeedMultiplier > 0) + speedMultiplier *= 4; + else if (m_InputController->SpeedMultiplier < 0) + speedMultiplier /= 4; + + transform->Position += movement * steering->Speed * speedMultiplier * (float)dt; + + glm::quat mouseOrientationPitch = glm::quat(m_InputController->MouseOrientation * glm::vec3(1, 0, 0)); + glm::quat mouseOrientationYaw = glm::quat(m_InputController->MouseOrientation * glm::vec3(0, 1, 0)); + + glm::vec3 controllerOrientationEuler = m_InputController->ControllerOrientation * (float)dt; + glm::quat controllerOrientationPitch = glm::quat(controllerOrientationEuler * glm::vec3(1, 0, 0)); + glm::quat controllerOrientationYaw = glm::quat(controllerOrientationEuler * glm::vec3(0, 1, 0)); + + // TOUCHING THIS CODE MIGHT CAUSE THE UNIVERSE TO IMPLODE, ALSO DRAGONS + //--------------------------------------------------------------------- + transform->Orientation = (mouseOrientationYaw * controllerOrientationYaw) + * transform->Orientation + * (mouseOrientationPitch * controllerOrientationPitch); + //--------------------------------------------------------------------- + // TOUCHING THIS CODE MIGHT CAUSE THE UNIVERSE TO IMPLODE, ALSO DRAGONS } + + m_InputController->MouseOrientation = glm::vec3(0); } bool Systems::FreeSteeringSystem::FreeSteeringInputController::OnCommand(const Events::InputCommand &event) { // Movement - if (event.Command == "+cam_forward") + if (event.Command == "cam_vertical") { - Movement.z += -1.f; + Movement.z = -event.Value; } - else if (event.Command == "-cam_forward") + else if (event.Command == "cam_horizontal") { - Movement.z -= -1.f; + Movement.x = event.Value; } - else if (event.Command == "+cam_backward") + else if (event.Command == "cam_normal") { - Movement.z += 1.f; - } - else if (event.Command == "-cam_backward") - { - Movement.z -= 1.f; - } - else if (event.Command == "+cam_right") - { - Movement.x -= 1.f; - } - else if (event.Command == "-cam_right") - { - Movement.x += 1.f; - } - else if (event.Command == "+cam_left") - { - Movement.x -= -1.f; - } - else if (event.Command == "-cam_left") - { - Movement.x += -1.f; - } - else if (event.Command == "+up") - { - Movement.y += 1.f; - } - else if (event.Command == "-up") - { - Movement.y -= 1.f; - } - else if (event.Command == "+down") - { - Movement.y += -1.f; - } - else if (event.Command == "-down") - { - Movement.y -= -1.f; + Movement.y = event.Value; } // Speed - else if (event.Command == "+fast") + else if (event.Command == "cam_speed") { - SpeedMultiplier *= 4.f; - } - else if (event.Command == "-fast") - { - SpeedMultiplier /= 4.f; - } - else if (event.Command == "+slow") - { - SpeedMultiplier /= 4.f; - } - else if (event.Command == "-slow") - { - SpeedMultiplier *= 4.f; + SpeedMultiplier = event.Value; } // Mouse click - else if (event.Command == "+attack") + else if (event.Command == "cam_attack") { - OrientationActive = true; + OrientationActive = event.Value > 0; + + if (OrientationActive) + { + Events::LockMouse e; + EventBroker->Publish(e); + } + else + { + Events::UnlockMouse e; + EventBroker->Publish(e); + } } - else if (event.Command == "-attack") + + else if (event.Command == "cam_vertical2") { - OrientationActive = false; + ControllerOrientation.x = event.Value; + } + else if (event.Command == "cam_horizontal2") + { + ControllerOrientation.y = -event.Value; } return true; @@ -122,11 +112,7 @@ bool Systems::FreeSteeringSystem::FreeSteeringInputController::OnMouseMove(const { if (OrientationActive) { - // TOUCHING THIS CODE MIGHT CAUSE THE UNIVERSE TO IMPLODE, ALSO DRAGONS - //--------------------------------------------------------------------- - Orientation = glm::angleAxis(event.DeltaX / 300.f, glm::vec3(0, -1, 0)) * Orientation * glm::angleAxis(event.DeltaY / 300.f, glm::vec3(-1, 0, 0)); - //--------------------------------------------------------------------- - // TOUCHING THIS CODE MIGHT CAUSE THE UNIVERSE TO IMPLODE, ALSO DRAGONS + MouseOrientation = -glm::vec3(event.DeltaY / 300.f, event.DeltaX / 300.f, 0.f); } return true; diff --git a/src/Systems/FreeSteeringSystem.h b/src/Systems/FreeSteeringSystem.h index 4535cbb..61a453d 100755 --- a/src/Systems/FreeSteeringSystem.h +++ b/src/Systems/FreeSteeringSystem.h @@ -4,6 +4,7 @@ #include "Components/Transform.h" #include "Components/FreeSteering.h" #include "InputController.h" +#include "Events/LockMouse.h" namespace Systems { @@ -26,16 +27,17 @@ private: std::unique_ptr m_InputController; }; -class FreeSteeringSystem::FreeSteeringInputController : InputController +class FreeSteeringSystem::FreeSteeringInputController : InputController { public: FreeSteeringInputController(std::shared_ptr<::EventBroker> eventBroker) : InputController(eventBroker) - , SpeedMultiplier(1.f) + , SpeedMultiplier(0.f) , OrientationActive(false) { } glm::vec3 Movement; - glm::quat Orientation; + glm::vec3 MouseOrientation; + glm::vec3 ControllerOrientation; float SpeedMultiplier; bool OrientationActive; diff --git a/src/Systems/HelicopterSteeringSystem.cpp b/src/Systems/HelicopterSteeringSystem.cpp new file mode 100644 index 0000000..94431c2 --- /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([]() { 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); + if (!transform) + return; + + auto helicopterComponent = m_World->GetComponent(entity); + 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..742ce70 --- /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/InputSystem.cpp b/src/Systems/InputSystem.cpp index 18a21aa..2f774d0 100755 --- a/src/Systems/InputSystem.cpp +++ b/src/Systems/InputSystem.cpp @@ -4,18 +4,23 @@ void Systems::InputSystem::RegisterComponents(ComponentFactory* cf) { - cf->Register("Input", []() { return new Components::Input(); }); + cf->Register([]() { return new Components::Input(); }); } void Systems::InputSystem::Initialize() { // Subscribe to events - EVENT_SUBSCRIBE_MEMBER(m_EKeyDown, &Systems::InputSystem::OnKeyDown) - EVENT_SUBSCRIBE_MEMBER(m_EKeyUp, &Systems::InputSystem::OnKeyUp) - EVENT_SUBSCRIBE_MEMBER(m_EMousePress, &Systems::InputSystem::OnMousePress) - EVENT_SUBSCRIBE_MEMBER(m_EMouseRelease, &Systems::InputSystem::OnMouseRelease) - EVENT_SUBSCRIBE_MEMBER(m_EBindKey, &Systems::InputSystem::OnBindKey) - EVENT_SUBSCRIBE_MEMBER(m_EBindMouseButton, &Systems::InputSystem::OnBindMouseButton) + EVENT_SUBSCRIBE_MEMBER(m_EKeyDown, &Systems::InputSystem::OnKeyDown); + EVENT_SUBSCRIBE_MEMBER(m_EKeyUp, &Systems::InputSystem::OnKeyUp); + EVENT_SUBSCRIBE_MEMBER(m_EMousePress, &Systems::InputSystem::OnMousePress); + EVENT_SUBSCRIBE_MEMBER(m_EMouseRelease, &Systems::InputSystem::OnMouseRelease); + EVENT_SUBSCRIBE_MEMBER(m_EGamepadAxis, &Systems::InputSystem::OnGamepadAxis); + EVENT_SUBSCRIBE_MEMBER(m_EGamepadButtonDown, &Systems::InputSystem::OnGamepadButtonDown); + EVENT_SUBSCRIBE_MEMBER(m_EGamepadButtonUp, &Systems::InputSystem::OnGamepadButtonUp); + EVENT_SUBSCRIBE_MEMBER(m_EBindKey, &Systems::InputSystem::OnBindKey); + EVENT_SUBSCRIBE_MEMBER(m_EBindMouseButton, &Systems::InputSystem::OnBindMouseButton); + EVENT_SUBSCRIBE_MEMBER(m_EBindGamepadAxis, &Systems::InputSystem::OnBindGamepadAxis); + EVENT_SUBSCRIBE_MEMBER(m_EBindGamepadButton, &Systems::InputSystem::OnBindGamepadButton); } void Systems::InputSystem::Update(double dt) @@ -44,7 +49,11 @@ bool Systems::InputSystem::OnKeyDown(const Events::KeyDown &event) auto bindingIt = m_KeyBindings.find(event.KeyCode); if (bindingIt != m_KeyBindings.end()) { - PublishCommand(0, bindingIt->second, 1.f, false); + std::string command; + float value; + std::tie(command, value) = bindingIt->second; + m_CommandKeyboardValues[command][event.KeyCode] = value; + PublishCommand(1, command, GetCommandTotalValue(command)); } return true; @@ -55,7 +64,11 @@ bool Systems::InputSystem::OnKeyUp(const Events::KeyUp &event) auto bindingIt = m_KeyBindings.find(event.KeyCode); if (bindingIt != m_KeyBindings.end()) { - PublishCommand(0, bindingIt->second, 1.f, true); + std::string command; + float value; + std::tie(command, value) = bindingIt->second; + m_CommandKeyboardValues[command][event.KeyCode] = 0; + PublishCommand(1, command, GetCommandTotalValue(command));; } return true; @@ -66,7 +79,11 @@ bool Systems::InputSystem::OnMousePress(const Events::MousePress &event) auto bindingIt = m_MouseButtonBindings.find(event.Button); if (bindingIt != m_MouseButtonBindings.end()) { - PublishCommand(0, bindingIt->second, 1.f, false); + std::string command; + float value; + std::tie(command, value) = bindingIt->second; + m_CommandMouseButtonValues[command][event.Button] = value; + PublishCommand(1, command, GetCommandTotalValue(command)); } return true; @@ -77,12 +94,62 @@ bool Systems::InputSystem::OnMouseRelease(const Events::MouseRelease &event) auto bindingIt = m_MouseButtonBindings.find(event.Button); if (bindingIt != m_MouseButtonBindings.end()) { - PublishCommand(0, bindingIt->second, 1.f, true); + std::string command; + float value; + std::tie(command, value) = bindingIt->second; + m_CommandMouseButtonValues[command][event.Button] = 0; + PublishCommand(1, command, GetCommandTotalValue(command)); } return true; } +bool Systems::InputSystem::OnGamepadAxis(const Events::GamepadAxis &event) +{ + auto bindingIt = m_GamepadAxisBindings.find(event.Axis); + if (bindingIt != m_GamepadAxisBindings.end()) + { + std::string command; + float value; + std::tie(command, value) = bindingIt->second; + m_CommandGamepadAxisValues[command][event.Axis] = event.Value * value; + PublishCommand(event.GamepadID + 1, command, GetCommandTotalValue(command)); + } + + return true; +} + +bool Systems::InputSystem::OnGamepadButtonDown(const Events::GamepadButtonDown &event) +{ + auto bindingIt = m_GamepadButtonBindings.find(event.Button); + if (bindingIt != m_GamepadButtonBindings.end()) + { + std::string command; + float value; + std::tie(command, value) = bindingIt->second; + m_CommandGamepadButtonValues[command][event.Button] = value; + PublishCommand(event.GamepadID + 1, command, GetCommandTotalValue(command)); + } + + return true; +} + +bool Systems::InputSystem::OnGamepadButtonUp(const Events::GamepadButtonUp &event) +{ + auto bindingIt = m_GamepadButtonBindings.find(event.Button); + if (bindingIt != m_GamepadButtonBindings.end()) + { + std::string command; + float value; + std::tie(command, value) = bindingIt->second; + m_CommandGamepadButtonValues[command][event.Button] = 0; + PublishCommand(event.GamepadID + 1, command, GetCommandTotalValue(command)); + } + + return true; +} + + bool Systems::InputSystem::OnBindKey(const Events::BindKey &event) { if (event.Command.empty()) @@ -91,7 +158,7 @@ bool Systems::InputSystem::OnBindKey(const Events::BindKey &event) } else { - m_KeyBindings[event.KeyCode] = event.Command; + m_KeyBindings[event.KeyCode] = std::make_tuple(event.Command, event.Value); LOG_DEBUG("Input: Bound key %c to %s", (char)event.KeyCode, event.Command.c_str()); } @@ -106,25 +173,93 @@ bool Systems::InputSystem::OnBindMouseButton(const Events::BindMouseButton &even } else { - m_MouseButtonBindings[event.Button] = event.Command; + m_MouseButtonBindings[event.Button] = std::make_tuple(event.Command, event.Value); LOG_DEBUG("Input: Bound mouse button %i to %s", event.Button, event.Command.c_str()); } return true; } -void Systems::InputSystem::PublishCommand(int playerID, std::string command, float value, bool release /*= false*/) +bool Systems::InputSystem::OnBindGamepadAxis(const Events::BindGamepadAxis &event) { - if (release && command.at(0) == '+') + if (event.Command.empty()) { - command[0] = '-'; + m_GamepadAxisBindings.erase(event.Axis); + } + else + { + m_GamepadAxisBindings[event.Axis] = std::make_tuple(event.Command, event.Value); + LOG_DEBUG("Input: Bound gamepad axis %i to %s", event.Axis, event.Command.c_str()); } + return true; +} + +bool Systems::InputSystem::OnBindGamepadButton(const Events::BindGamepadButton &event) +{ + if (event.Command.empty()) + { + m_GamepadButtonBindings.erase(event.Button); + } + else + { + m_GamepadButtonBindings[event.Button] = std::make_tuple(event.Command, event.Value); + LOG_DEBUG("Input: Bound gamepad axis %i to %s", event.Button, event.Command.c_str()); + } + + return true; +} + +float Systems::InputSystem::GetCommandTotalValue(std::string command) +{ + float value = 0.f; + + auto keyboardIt = m_CommandKeyboardValues.find(command); + if (keyboardIt != m_CommandKeyboardValues.end()) + { + for (auto &key : keyboardIt->second) + { + value += key.second; + } + } + + auto mouseButtonIt = m_CommandMouseButtonValues.find(command); + if (mouseButtonIt != m_CommandMouseButtonValues.end()) + { + for (auto &button : mouseButtonIt->second) + { + value += button.second; + } + } + + auto gamepadAxisIt = m_CommandGamepadAxisValues.find(command); + if (gamepadAxisIt != m_CommandGamepadAxisValues.end()) + { + for (auto &axis : gamepadAxisIt->second) + { + value += axis.second; + } + } + + auto gamepadButtonIt = m_CommandGamepadButtonValues.find(command); + if (gamepadButtonIt != m_CommandGamepadButtonValues.end()) + { + for (auto &button : gamepadButtonIt->second) + { + value += button.second; + } + } + + return std::max(-1.f, std::min(value, 1.f)); +} + +void Systems::InputSystem::PublishCommand(int playerID, std::string command, float value) +{ Events::InputCommand e; e.PlayerID = playerID; e.Command = command; e.Value = value; EventBroker->Publish(e); - LOG_DEBUG("Input: Published command %s for player %i", e.Command.c_str(), playerID); + LOG_DEBUG("Input: Published command %s=%f for player %i", e.Command.c_str(), e.Value, playerID); } diff --git a/src/Systems/InputSystem.h b/src/Systems/InputSystem.h index f912de0..9d51d2a 100755 --- a/src/Systems/InputSystem.h +++ b/src/Systems/InputSystem.h @@ -3,6 +3,7 @@ #include #include +#include #include "System.h" #include "Components/Input.h" @@ -10,8 +11,12 @@ #include "Events/KeyDown.h" #include "Events/MousePress.h" #include "Events/MouseRelease.h" +#include "Events/GamepadAxis.h" +#include "Events/GamepadButton.h" #include "Events/BindKey.h" #include "Events/BindMouseButton.h" +#include "Events/BindGamepadAxis.h" +#include "Events/BindGamepadButton.h" #include "Events/InputCommand.h" namespace Systems @@ -29,26 +34,43 @@ public: void Update(double dt) override; private: + std::unordered_map> m_CommandKeyboardValues; // command string -> keyboard key value for command + std::unordered_map> m_CommandMouseButtonValues; // command string -> mouse button value for command + std::unordered_map> m_CommandGamepadAxisValues; // command string -> gamepad axis value for command + std::unordered_map> m_CommandGamepadButtonValues; // command string -> gamepad button value for command // Input binding tables - std::unordered_map m_KeyBindings; // GLFW_KEY... -> command string - std::unordered_map m_MouseButtonBindings; // GLFW_MOUSE_BUTTON... -> command string + std::unordered_map> m_KeyBindings; // GLFW_KEY... -> command string & value + std::unordered_map> m_MouseButtonBindings; // GLFW_MOUSE_BUTTON... -> command string + std::unordered_map> m_GamepadAxisBindings; // Gamepad::Axis -> command string & value + 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; + bool OnGamepadAxis(const Events::GamepadAxis &event); + EventRelay m_EGamepadButtonDown; + bool OnGamepadButtonDown(const Events::GamepadButtonDown &event); + 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; + bool OnBindGamepadAxis(const Events::BindGamepadAxis &event); + EventRelay m_EBindGamepadButton; + bool OnBindGamepadButton(const Events::BindGamepadButton &event); - void PublishCommand(int playerID, std::string command, float value, bool release = false); + float GetCommandTotalValue(std::string command); + void PublishCommand(int playerID, std::string command, float value); }; } diff --git a/src/Systems/ParticleSystem.cpp b/src/Systems/ParticleSystem.cpp index fac2f91..9b094d3 100644 --- a/src/Systems/ParticleSystem.cpp +++ b/src/Systems/ParticleSystem.cpp @@ -6,9 +6,9 @@ void Systems::ParticleSystem::Initialize() { - m_TransformSystem = m_World->GetSystem("TransformSystem"); + m_TransformSystem = m_World->GetSystem(); tempSpawnedExplosions = false; - EVENT_SUBSCRIBE_MEMBER(m_EKeyUp, &Systems::ParticleSystem::OnKeyUp); + EVENT_SUBSCRIBE_MEMBER(m_EKeyUp, &ParticleSystem::OnKeyUp); } void Systems::ParticleSystem::Update(double dt) @@ -20,7 +20,7 @@ void Systems::ParticleSystem::Update(double dt) double spawnTime = it->second; double timeLived = glfwGetTime() - spawnTime; - auto eComp = m_World->GetComponent(explosionID, "ParticleEmitter"); + auto eComp = m_World->GetComponent(explosionID); if(timeLived > eComp->LifeTime) { @@ -37,15 +37,15 @@ void Systems::ParticleSystem::Update(double dt) void Systems::ParticleSystem::UpdateEntity(double dt, EntityID entity, EntityID parent) { - auto transformComponent = m_World->GetComponent(entity, "Transform"); + auto transformComponent = m_World->GetComponent(entity); if(!transformComponent) return; - auto emitterComponent = m_World->GetComponent(entity, "ParticleEmitter"); + auto emitterComponent = m_World->GetComponent(entity); if(emitterComponent) { emitterComponent->TimeSinceLastSpawn += dt; - auto emitterTransformComponent = m_World->GetComponent(entity, "Transform"); + auto emitterTransformComponent = m_World->GetComponent(entity); if(emitterComponent->TimeSinceLastSpawn > emitterComponent->SpawnFrequency) { SpawnParticles(entity); @@ -56,8 +56,8 @@ void Systems::ParticleSystem::UpdateEntity(double dt, EntityID entity, EntityID for(it = m_ParticleEmitter[entity].begin(); it != m_ParticleEmitter[entity].end();) { EntityID particleID = (it)->ParticleID; - auto transformComponent = m_World->GetComponent(particleID, "Transform"); - auto particleComponent = m_World->GetComponent(particleID, "Particle"); + auto transformComponent = m_World->GetComponent(particleID); + auto particleComponent = m_World->GetComponent(particleID); double timeLived = glfwGetTime() - it->SpawnTime; if(timeLived > particleComponent->LifeTime) @@ -115,24 +115,24 @@ void Systems::ParticleSystem::UpdateEntity(double dt, EntityID entity, EntityID void Systems::ParticleSystem::RegisterComponents(ComponentFactory* cf) { - cf->Register("ParticleEmitter", []() { return new Components::ParticleEmitter(); }); - cf->Register("Particle", []() { return new Components::Particle(); }); + cf->Register([]() { return new Components::ParticleEmitter(); }); + cf->Register([]() { return new Components::Particle(); }); } void Systems::ParticleSystem::SpawnParticles(EntityID emitterID) { - auto eComponent = m_World->GetComponent(emitterID, "ParticleEmitter"); - auto eTransform = m_World->GetComponent(emitterID, "Transform"); + auto eComponent = m_World->GetComponent(emitterID); + auto eTransform = m_World->GetComponent(emitterID); glm::vec3 ePosition = m_TransformSystem->AbsolutePosition(emitterID); glm::quat eOrientation = eTransform->Orientation; glm::vec3 paticleSpeed = glm::vec3(eComponent->Speed); for(int i = 0; i < eComponent->SpawnCount; i++) { - auto particleEntity = m_World->CloneEntity(eComponent->ParticleTemplate); + auto ent = m_World->CloneEntity(eComponent->ParticleTemplate); - auto particleTransform = m_World->GetComponent(particleEntity, "Transform"); + auto particleTransform = m_World->GetComponent(ent); particleTransform->Position = ePosition; particleTransform->Orientation = eOrientation; @@ -143,16 +143,16 @@ void Systems::ParticleSystem::SpawnParticles(EntityID emitterID) glm::normalize(glm::angleAxis(RandomizeAngle(spreadAngle), glm::vec3(0, 1, 0))) * glm::normalize(glm::angleAxis(RandomizeAngle(spreadAngle), glm::vec3(0, 0, 1))); - auto particleComponent = m_World->AddComponent(particleEntity, "Particle"); - particleComponent->LifeTime = eComponent->LifeTime; - particleComponent->ScaleSpectrum = eComponent->ScaleSpectrum; - particleComponent->VelocitySpectrum.push_back(particleTransform->Velocity); + auto particle = m_World->AddComponent(ent); + particle->LifeTime = eComponent->LifeTime; + particle->ScaleSpectrum = eComponent->ScaleSpectrum; + particle->VelocitySpectrum.push_back(particleTransform->Velocity); if (eComponent->ScaleSpectrum.size() > 0) { if (eComponent->ScaleSpectrum.size() > 1) { - particleComponent->ScaleSpectrum = eComponent->ScaleSpectrum; + particle->ScaleSpectrum = eComponent->ScaleSpectrum; } else { @@ -165,20 +165,20 @@ void Systems::ParticleSystem::SpawnParticles(EntityID emitterID) } if(eComponent->UseGoalVelocity) - particleComponent->VelocitySpectrum.push_back(eComponent->GoalVelocity); - particleComponent->OrientationSpectrum = eComponent->OrientationSpectrum; - if(particleComponent->OrientationSpectrum.size() != 0) - particleTransform->Orientation = glm::angleAxis(0.f, particleComponent->OrientationSpectrum[0]); - particleComponent->AngularVelocitySpectrum = eComponent->AngularVelocitySpectrum; + particle->VelocitySpectrum.push_back(eComponent->GoalVelocity); + particle->OrientationSpectrum = eComponent->OrientationSpectrum; + if(particle->OrientationSpectrum.size() != 0) + particleTransform->Orientation = glm::angleAxis(0.f, particle->OrientationSpectrum[0]); + particle->AngularVelocitySpectrum = eComponent->AngularVelocitySpectrum; ParticleData data; - data.ParticleID = particleEntity; + data.ParticleID = ent; data.SpawnTime = glfwGetTime(); - if (particleComponent->AngularVelocitySpectrum.size() != 0) - data.AngularVelocity = particleComponent->AngularVelocitySpectrum[0]; - if (particleComponent->OrientationSpectrum.size() != 0) - data.Orientation = particleComponent->OrientationSpectrum[0]; + if (particle->AngularVelocitySpectrum.size() != 0) + data.AngularVelocity = particle->AngularVelocitySpectrum[0]; + if (particle->OrientationSpectrum.size() != 0) + data.Orientation = particle->OrientationSpectrum[0]; else data.Orientation = eOrientation * glm::vec3(0,0,-1); m_ParticleEmitter[emitterID].push_back(data); } @@ -228,7 +228,7 @@ void Systems::ParticleSystem::ScalarInterpolation(double timeProgress, std::vect void Systems::ParticleSystem::CreateExplosion(glm::vec3 _pos, double _lifeTime, int _particlesToSpawn, std::string _spritePath, glm::quat _relativeUpOri, float _speed, float _spreadAngle, float _particleScale) { auto explosion = m_World->CreateEntity(); - auto emitter = m_World->AddComponent(explosion, "ParticleEmitter"); + auto emitter = m_World->AddComponent(explosion); emitter->LifeTime = _lifeTime; emitter->SpawnCount = _particlesToSpawn; emitter->Speed = _speed; @@ -239,14 +239,14 @@ void Systems::ParticleSystem::CreateExplosion(glm::vec3 _pos, double _lifeTime, m_World->CommitEntity(explosion); auto particleEnt = m_World->CreateEntity(); - auto TEMP = m_World->AddComponent(particleEnt, "Transform"); + auto TEMP = m_World->AddComponent(particleEnt); TEMP->Scale = glm::vec3(0); - auto spriteComponent = m_World->AddComponent(particleEnt, "Sprite"); + auto spriteComponent = m_World->AddComponent(particleEnt); spriteComponent->SpriteFile = _spritePath; m_World->CommitEntity(particleEnt); emitter->ParticleTemplate = particleEnt; - auto transform = m_World->AddComponent(explosion, "Transform"); + auto transform = m_World->AddComponent(explosion); transform->Position = _pos; transform->Orientation = _relativeUpOri; diff --git a/src/Systems/ParticleSystem.h b/src/Systems/ParticleSystem.h index 31945c8..fad8815 100644 --- a/src/Systems/ParticleSystem.h +++ b/src/Systems/ParticleSystem.h @@ -54,7 +54,7 @@ private: bool tempSpawnedExplosions; - EventRelay m_EKeyUp; + EventRelay m_EKeyUp; bool OnKeyUp(const Events::KeyUp &e); }; diff --git a/src/Systems/PhysicsSystem.cpp b/src/Systems/PhysicsSystem.cpp index 5fad34c..3c3c1f6 100644 --- a/src/Systems/PhysicsSystem.cpp +++ b/src/Systems/PhysicsSystem.cpp @@ -27,10 +27,15 @@ void Systems::PhysicsSystem::Initialize() { + + m_Accumulator = 0; // 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); + 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); @@ -70,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); @@ -101,21 +106,22 @@ void Systems::PhysicsSystem::Initialize() SetupVisualDebugger(m_Context); m_PhysicsWorld->unmarkForWrite(); + + m_collisionResolution = new MyCollisionResolution; } } void Systems::PhysicsSystem::RegisterComponents(ComponentFactory* cf) { - cf->Register("Physics", []() { return new Components::Physics(); }); - cf->Register("BoxShape", []() { return new Components::BoxShape(); }); - cf->Register("SphereShape", []() { return new Components::SphereShape(); }); - cf->Register("Vehicle", []() { return new Components::Vehicle(); }); - cf->Register("Wheel", []() { return new Components::Wheel(); }); - cf->Register("MeshShape", []() { return new Components::MeshShape(); }); - cf->Register("HingeConstraint", []() { return new Components::HingeConstraint(); }); - cf->Register("WheelPair", []() { return new Components::WheelPair(); }); - + cf->Register([]() { return new Components::Physics(); }); + cf->Register([]() { return new Components::BoxShape(); }); + cf->Register([]() { return new Components::SphereShape(); }); + cf->Register([]() { return new Components::Vehicle(); }); + cf->Register([]() { return new Components::Wheel(); }); + cf->Register([]() { return new Components::MeshShape(); }); + cf->Register([]() { return new Components::HingeConstraint(); }); + cf->Register([]() { return new Components::WheelPair(); }); } void Systems::PhysicsSystem::Update(double dt) @@ -128,7 +134,7 @@ void Systems::PhysicsSystem::Update(double dt) if (m_RigidBodies.find(entity) == m_RigidBodies.end()) continue; - auto transformComponent = m_World->GetComponent(entity, "Transform"); + auto transformComponent = m_World->GetComponent(entity); if (!transformComponent) continue; @@ -139,14 +145,14 @@ void Systems::PhysicsSystem::Update(double dt) if (parent) { - auto absoluteTransform = m_World->GetSystem("TransformSystem")->AbsoluteTransform(entity); - position = ConvertPosition(absoluteTransform.Position); - rotation = ConvertRotation(absoluteTransform.Orientation); + auto absoluteTransform = m_World->GetSystem()->AbsoluteTransform(entity); + 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); @@ -172,19 +178,16 @@ 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) { - auto transformComponent = m_World->GetComponent(entity, "Transform"); + auto transformComponent = m_World->GetComponent(entity); if (!transformComponent) return; - auto wheelComponent = m_World->GetComponent(entity, "Wheel"); + auto wheelComponent = m_World->GetComponent(entity); if (wheelComponent) { EntityID car = m_World->GetEntityParent(entity); @@ -201,17 +204,17 @@ 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(); } } else if(m_RigidBodies.find(entity) != m_RigidBodies.end()) { - auto transformComponentParent = m_World->GetComponent(parent, "Transform"); + 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) @@ -227,11 +230,11 @@ void Systems::PhysicsSystem::UpdateEntity(double dt, EntityID entity, EntityID p void Systems::PhysicsSystem::OnEntityCommit( EntityID entity ) { - auto transformComponent = m_World->GetComponent(entity, "Transform"); + auto transformComponent = m_World->GetComponent(entity); if (!transformComponent) return; - auto wheelComponent = m_World->GetComponent(entity, "Wheel"); + auto wheelComponent = m_World->GetComponent(entity); if (wheelComponent) { wheelComponent->ID = m_Wheels.size(); @@ -241,9 +244,9 @@ void Systems::PhysicsSystem::OnEntityCommit( EntityID entity ) EntityID entityParent = m_World->GetEntityBaseParent(entity); - auto sphereComponent = m_World->GetComponent(entity, "SphereShape"); - auto boxComponent = m_World->GetComponent(entity, "BoxShape"); - auto meshShapeComponent = m_World->GetComponent(entity, "MeshShape"); + auto sphereComponent = m_World->GetComponent(entity); + auto boxComponent = m_World->GetComponent(entity); + auto meshShapeComponent = m_World->GetComponent(entity); if(entityParent == entity && (sphereComponent || boxComponent || meshShapeComponent)) { @@ -251,7 +254,7 @@ void Systems::PhysicsSystem::OnEntityCommit( EntityID entity ) return; } - auto physicsComponent = m_World->GetComponent(entity, "Physics"); + auto physicsComponent = m_World->GetComponent(entity); if (physicsComponent) { hkpShape* shape; @@ -274,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); @@ -292,9 +291,9 @@ void Systems::PhysicsSystem::OnEntityCommit( EntityID entity ) { rigidBodyInfo.m_shape = shape; rigidBodyInfo.m_motionType = hkpMotion::MOTION_DYNAMIC; - auto absoluteTransform = m_World->GetSystem("TransformSystem")->AbsoluteTransform(entity); - hkVector4 position = ConvertPosition(absoluteTransform.Position); - hkQuaternion rotation = ConvertRotation(absoluteTransform.Orientation); + auto absoluteTransform = m_World->GetSystem()->AbsoluteTransform(entity); + 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)); @@ -305,7 +304,7 @@ void Systems::PhysicsSystem::OnEntityCommit( EntityID entity ) // Create RigidBody hkpRigidBody* rigidBody = new hkpRigidBody(rigidBodyInfo); - auto vehicleComponent = m_World->GetComponent(entity, "Vehicle"); + auto vehicleComponent = m_World->GetComponent(entity); if (vehicleComponent && m_Vehicles.find(entity) == m_Vehicles.end()) { for (int i = 0; i < m_Wheels.size(); i++) @@ -324,9 +323,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 +341,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(); @@ -357,11 +360,11 @@ void Systems::PhysicsSystem::OnEntityCommit( EntityID entity ) for (auto &shapeData : m_Shapes[entity]) { - auto childTransformComponent = m_World->GetComponent(shapeData.Entity, "Transform"); + 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); @@ -378,9 +381,9 @@ void Systems::PhysicsSystem::OnEntityCommit( EntityID entity ) { rigidBodyInfo.m_shape = shape; rigidBodyInfo.m_motionType = hkpMotion::MOTION_FIXED; - auto absoluteTransform = m_World->GetSystem("TransformSystem")->AbsoluteTransform(entity); - hkVector4 position = ConvertPosition(absoluteTransform.Position); - hkQuaternion rotation = ConvertRotation(absoluteTransform.Orientation); + auto absoluteTransform = m_World->GetSystem()->AbsoluteTransform(entity); + 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)); @@ -394,6 +397,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(); @@ -411,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)); @@ -424,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(); @@ -532,51 +536,44 @@ 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) -{ - return hkQuaternion(glmRotation.x, glmRotation.y, glmRotation.z, glmRotation.w); -} - -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, "Vehicle"); - auto inputComponent = m_World->GetComponent(event.Entity, "Input"); - if (vehicleComponent && inputComponent && m_Vehicles.find(event.Entity) != m_Vehicles.end() && m_RigidBodies.find(event.Entity) != m_RigidBodies.end()) + 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; + if(event.PositionY > 0) + { + deviceStatus->m_reverseButtonPressed = true; + } deviceStatus->m_handbrakeButtonPressed = event.Handbrake; - m_PhysicsWorld->unmarkForWrite(); } return true; } +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; +} + +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; +} + +bool Systems::PhysicsSystem::OnApplyPointImpulse( const Events::ApplyPointImpulse &event ) +{ + 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/PhysicsSystem.h b/src/Systems/PhysicsSystem.h index b8fc262..029dd8e 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" @@ -12,7 +22,11 @@ #include "Components/MeshShape.h" #include "Components/HingeConstraint.h" #include "Components/WheelPair.h" +#include "Components/TowerSteering.h" #include "Events/TankSteer.h" +#include "Events/SetVelocity.h" +#include "Events/ApplyForce.h" +#include "Events/ApplyPointImpulse.h" #include "OBJ.h" // Math and base include @@ -58,9 +72,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: @@ -75,14 +107,20 @@ 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; // Events - EventRelay m_ETankSteer; + 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); + EventRelay m_EApplyPointImpulse; + bool OnApplyPointImpulse(const Events::ApplyPointImpulse &event); void SetUpPhysicsState(EntityID entity, EntityID parent); void TearDownPhysicsState(EntityID entity, EntityID parent); @@ -93,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; @@ -140,8 +168,9 @@ private: hkpMoppBvTreeShape* MoppShape; }; std::unordered_map m_ExtendedMeshShapes; + + MyCollisionResolution* m_collisionResolution; }; } - #endif // PhysicsSystem_h__ 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/src/Systems/RenderSystem.cpp b/src/Systems/RenderSystem.cpp index 35a57d8..43268ce 100755 --- a/src/Systems/RenderSystem.cpp +++ b/src/Systems/RenderSystem.cpp @@ -2,26 +2,59 @@ #include "RenderSystem.h" #include "World.h" -void Systems::RenderSystem::OnComponentCreated(std::string type, std::shared_ptr component) +void Systems::RenderSystem::RegisterResourceTypes(ResourceManager* rm) { - if(type == "Model") + 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([]() { return new Components::Camera(); }); + cf->Register([]() { return new Components::Model(); }); + cf->Register([]() { return new Components::Sprite(); }); + cf->Register([]() { return new Components::PointLight(); }); + cf->Register([]() { return new Components::DirectionalLight(); }); + cf->Register([]() { return new Components::Viewport(); }); +} + +void Systems::RenderSystem::OnEntityCommit(EntityID entity) +{ + auto transform = m_World->GetComponent(entity); + + auto camera = m_World->GetComponent(entity); + if (transform && camera) { - auto modelComponent = std::static_pointer_cast(component); + 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); + if (viewport) + { + 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) + auto templateComponent = m_World->GetComponent(entity); + if (templateComponent) return; + auto transformComponent = m_World->GetComponent(entity); + // Draw models - auto modelComponent = m_World->GetComponent(entity, "Model"); - if (modelComponent != nullptr) + auto modelComponent = m_World->GetComponent(entity); + 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); @@ -31,38 +64,48 @@ void Systems::RenderSystem::UpdateEntity(double dt, EntityID entity, EntityID pa } } - auto pointLightComponent = m_World->GetComponent(entity, "PointLight"); - if (pointLightComponent != nullptr) + auto pointLightComponent = m_World->GetComponent(entity); + if (transformComponent && pointLightComponent) { glm::vec3 position = m_TransformSystem->AbsolutePosition(entity); m_Renderer->AddPointLightToDraw( position, pointLightComponent->Specular, pointLightComponent->Diffuse, - pointLightComponent->constantAttenuation, - pointLightComponent->linearAttenuation, - pointLightComponent->quadraticAttenuation, - pointLightComponent->spotExponent); + pointLightComponent->specularExponent, + pointLightComponent->ConstantAttenuation, + pointLightComponent->LinearAttenuation, + pointLightComponent->QuadraticAttenuation + ); } - auto cameraComponent = m_World->GetComponent(entity, "Camera"); - if (cameraComponent != nullptr) + auto cameraComponent = m_World->GetComponent(entity); + if (transformComponent && cameraComponent) { - m_Renderer->GetCamera()->Position(m_TransformSystem->AbsolutePosition(entity)); - m_Renderer->GetCamera()->Orientation(m_TransformSystem->AbsoluteOrientation(entity)); - - m_Renderer->GetCamera()->FOV(cameraComponent->FOV); - m_Renderer->GetCamera()->NearClip(cameraComponent->NearClip); - m_Renderer->GetCamera()->FarClip(cameraComponent->FarClip); + m_Renderer->UpdateCamera(entity + , m_TransformSystem->AbsolutePosition(entity) + , m_TransformSystem->AbsoluteOrientation(entity) + , cameraComponent->FOV + , cameraComponent->NearClip + , cameraComponent->FarClip); } - auto spriteComponent = m_World->GetComponent(entity, "Sprite"); - if(spriteComponent != nullptr) + auto viewportComponent = m_World->GetComponent(entity); + if (viewportComponent) + { + if (viewportComponent->Camera != 0) + { + m_Renderer->UpdateViewport(entity, viewportComponent->Camera); + } + } + + auto spriteComponent = m_World->GetComponent(entity); + if (transformComponent && spriteComponent) { //TEMP Texture* texture = m_World->GetResourceManager()->Load("Texture", spriteComponent->SpriteFile); //glBindTexture(GL_TEXTURE_2D, texture); - auto transform = m_World->GetComponent(spriteComponent->Entity, "Transform"); + auto transform = m_World->GetComponent(spriteComponent->Entity); glm::quat orientation2D = glm::angleAxis(glm::eulerAngles(transform->Orientation).z, glm::vec3(0, 0, -1)); m_Renderer->AddTextureToDraw(texture, transform->Position, orientation2D, transform->Scale); } @@ -70,26 +113,8 @@ void Systems::RenderSystem::UpdateEntity(double dt, EntityID entity, EntityID pa void Systems::RenderSystem::Initialize() { - m_TransformSystem = m_World->GetSystem("TransformSystem"); + m_TransformSystem = m_World->GetSystem(); + + 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 c3cd09a..6268ed6 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) override; void UpdateEntity(double dt, EntityID entity, EntityID parent) override; diff --git a/src/Systems/SoundSystem.cpp b/src/Systems/SoundSystem.cpp index 02a665e..b36bf47 100755 --- a/src/Systems/SoundSystem.cpp +++ b/src/Systems/SoundSystem.cpp @@ -29,7 +29,7 @@ void Systems::SoundSystem::Initialize() void Systems::SoundSystem::RegisterComponents(ComponentFactory* cf) { - cf->Register("SoundEmitter", []() { return new Components::SoundEmitter(); }); + cf->Register([]() { return new Components::SoundEmitter(); }); } void Systems::SoundSystem::RegisterResourceTypes(ResourceManager* rm) @@ -44,7 +44,7 @@ void Systems::SoundSystem::Update(double dt) void Systems::SoundSystem::UpdateEntity(double dt, EntityID entity, EntityID parent) { - auto transformComponent = m_World->GetComponent(entity, "Transform"); + auto transformComponent = m_World->GetComponent(entity); if (transformComponent == nullptr) return; @@ -68,7 +68,7 @@ void Systems::SoundSystem::UpdateEntity(double dt, EntityID entity, EntityID par alListenerfv(AL_ORIENTATION, listenerOri); } - auto soundEmitter = m_World->GetComponent(entity, "SoundEmitter"); + auto soundEmitter = m_World->GetComponent(entity); if(soundEmitter != nullptr) { ALuint source = m_Sources[soundEmitter]; 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.cpp b/src/Systems/TankSteeringSystem.cpp index f0a4e30..3d36977 100644 --- a/src/Systems/TankSteeringSystem.cpp +++ b/src/Systems/TankSteeringSystem.cpp @@ -4,84 +4,145 @@ void Systems::TankSteeringSystem::RegisterComponents( ComponentFactory* cf ) { - cf->Register("TankSteering", []() { return new Components::TankSteering(); }); + cf->Register([]() { return new Components::TankSteering(); }); + cf->Register([]() { return new Components::TowerSteering(); }); + cf->Register([]() { return new Components::BarrelSteering(); }); } void Systems::TankSteeringSystem::Initialize() { - m_InputController = std::unique_ptr(new TankSteeringInputController(EventBroker)); - m_InputController->PositionX = 0; - m_InputController->PositionY = 0; - m_InputController->Handbrake = false; + for (int i = 0; i < 4; i++) + { + m_TankInputControllers[i] = std::shared_ptr(new TankSteeringInputController(EventBroker, i + 1)); + } } void Systems::TankSteeringSystem::Update(double 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, "TankSteering"); - if(tankSteeringComponent) + auto tankSteeringComponent = m_World->GetComponent(entity); + if(!tankSteeringComponent) + return; + + 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_InputController->PositionX; - e.PositionY = m_InputController->PositionY; - e.Handbrake = m_InputController->Handbrake; - EventBroker->Publish(e); + auto transformComponent = m_World->GetComponent(tankSteeringComponent->Turret); + glm::quat orientation = glm::angleAxis(towerSteeringComponent->TurnSpeed * inputController->TowerDirection * (float)dt, towerSteeringComponent->Axis); + transformComponent->Orientation *= orientation; } + + if(barrelSteeringComponent) + { + 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(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 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 + Events::ApplyPointImpulse ePointImpulse ; + 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[tankSteeringComponent->Barrel] += dt; + } +} + +void Systems::TankSteeringSystem::TankSteeringInputController::Update( double dt ) +{ + PositionX = m_Horizontal; + PositionY = m_Vertical; + + TowerDirection = m_TowerDirection; + BarrelDirection = m_BarrelDirection; + Shoot = m_Shoot; } bool Systems::TankSteeringSystem::TankSteeringInputController::OnCommand(const Events::InputCommand &event) { - float val = boost::any_cast(event.Value); - if (event.Command == "+right") + if (event.PlayerID != this->PlayerID) + return false; + + float val = event.Value; + + // Tank + if (event.Command == "horizontal") { - PositionX += val; + m_Horizontal = val; + m_Vertical = -0.4f; } - else if (event.Command == "-right") + else if (event.Command == "vertical") { - PositionX -= val; + m_Vertical = -val; } - else if (event.Command == "+left") + + else if (event.Command == "handbrake") { - PositionX += -val; - } - else if (event.Command == "-left") - { - PositionX -= -val; - } - else if (event.Command == "+forward") - { - PositionY += -val; - } - else if (event.Command == "-forward") - { - PositionY -= -val; - } - else if (event.Command == "+backward") - { - PositionY += val; - } - else if (event.Command == "-backward") - { - PositionY -= val; + Handbrake = val > 0; } - else if (event.Command == "+handbrake") + // Turret + if(event.Command == "tower_rotation") { - Handbrake = true; + m_TowerDirection = -val; } - else if (event.Command == "-handbrake") + else if(event.Command == "barrel_rotation") { - Handbrake = false; + m_BarrelDirection = val; } + + else if (event.Command == "shoot") + { + m_Shoot = val > 0; + } + return true; } -bool Systems::TankSteeringSystem::TankSteeringInputController::OnMouseMove( const Events::MouseMove &event ) -{ - return false; -} + + diff --git a/src/Systems/TankSteeringSystem.h b/src/Systems/TankSteeringSystem.h index 5c7a02e..ec96df8 100644 --- a/src/Systems/TankSteeringSystem.h +++ b/src/Systems/TankSteeringSystem.h @@ -2,9 +2,17 @@ #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 "Components/Player.h" +#include "Systems/TransformSystem.h" #include "InputController.h" namespace Systems @@ -24,22 +32,55 @@ namespace Systems private: class TankSteeringInputController; - std::unique_ptr m_InputController; + std::array, 4> m_TankInputControllers; + + std::map m_TimeSinceLastShot; }; - class TankSteeringSystem::TankSteeringInputController : InputController + class TankSteeringSystem::TankSteeringInputController : InputController { public: - TankSteeringInputController(std::shared_ptr<::EventBroker> eventBroker) - : InputController(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; + + m_TowerDirection = 0.f; + m_BarrelDirection = 0.f; + TowerDirection = 0.f; + BarrelDirection = 0.f; + + 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_Horizontal; + float m_Vertical; + + float m_TowerDirection; + float m_BarrelDirection; + + bool m_Shoot; + }; } \ No newline at end of file diff --git a/src/Systems/TransformSystem.cpp b/src/Systems/TransformSystem.cpp index 383c123..69ec6fb 100755 --- a/src/Systems/TransformSystem.cpp +++ b/src/Systems/TransformSystem.cpp @@ -7,8 +7,8 @@ // if (parent == 0) // return; // -// auto transform = m_World->GetComponent(entity, "Transform"); -// auto parentTransform = m_World->GetComponent(parent, "Transform"); +// auto transform = m_World->GetComponent(entity); +// auto parentTransform = m_World->GetComponent(parent); // // transform->Position = parentTransform->Position + transform->RelativePosition; //} @@ -20,14 +20,14 @@ glm::vec3 Systems::TransformSystem::AbsolutePosition(EntityID entity) do { - auto transform = m_World->GetComponent(entity, "Transform"); + auto transform = m_World->GetComponent(entity); //absPosition += transform->Position; entity = m_World->GetEntityParent(entity); - auto transform2 = m_World->GetComponent(entity, "Transform"); - if (entity != 0) - absPosition += transform2->Orientation * transform->Position; - else + auto transform2 = m_World->GetComponent(entity); + if (entity == 0) absPosition += transform->Position; + else + absPosition = transform2->Orientation * (absPosition + transform->Position); } while (entity != 0); return absPosition * accumulativeOrientation; @@ -39,7 +39,7 @@ glm::quat Systems::TransformSystem::AbsoluteOrientation(EntityID entity) do { - auto transform = m_World->GetComponent(entity, "Transform"); + auto transform = m_World->GetComponent(entity); absOrientation = transform->Orientation * absOrientation; entity = m_World->GetEntityParent(entity); } while (entity != 0); @@ -53,7 +53,7 @@ glm::vec3 Systems::TransformSystem::AbsoluteScale(EntityID entity) do { - auto transform = m_World->GetComponent(entity, "Transform"); + auto transform = m_World->GetComponent(entity); absScale *= transform->Scale; entity = m_World->GetEntityParent(entity); } while (entity != 0); @@ -69,15 +69,15 @@ Components::Transform Systems::TransformSystem::AbsoluteTransform(EntityID entit do { - auto transform = m_World->GetComponent(entity, "Transform"); + auto transform = m_World->GetComponent(entity); entity = m_World->GetEntityParent(entity); - auto transform2 = m_World->GetComponent(entity, "Transform"); + auto transform2 = m_World->GetComponent(entity); // Position - if (entity != 0) - absPosition += transform2->Orientation * transform->Position; - else + if (entity == 0) absPosition += transform->Position; + else + absPosition = transform2->Orientation * (absPosition + transform->Position); // Orientation absOrientation = transform->Orientation * absOrientation; // Scale 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__ diff --git a/src/World.cpp b/src/World.cpp index 01713ab..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); } @@ -132,11 +134,6 @@ void World::Initialize() } } -std::shared_ptr World::AddComponent(EntityID entity, std::string componentType) -{ - return AddComponent(entity, componentType); -} - void World::CommitEntity(EntityID entity) { for (auto pair : m_Systems) @@ -158,11 +155,6 @@ void World::AddComponent(EntityID entity, std::string componentType, std::shared } } -void World::AddSystem(std::string systemType) -{ - m_Systems[systemType] = std::shared_ptr(m_SystemFactory.Create(systemType)); -} - EntityID World::CloneEntity(EntityID entity, EntityID parent /* = 0 */) { int clone = CreateEntity(parent); @@ -170,6 +162,8 @@ EntityID World::CloneEntity(EntityID entity, EntityID parent /* = 0 */) for (auto pair : m_EntityComponents[entity]) { auto type = pair.first; + if (type == typeid(Components::Template).name()) + continue; auto component = std::shared_ptr(pair.second->Clone()); if (component != nullptr) { @@ -186,6 +180,8 @@ EntityID World::CloneEntity(EntityID entity, EntityID parent /* = 0 */) } } + CommitEntity(clone); + return clone; } diff --git a/src/World.h b/src/World.h index 5f430b2..87bfecf 100755 --- a/src/World.h +++ b/src/World.h @@ -13,6 +13,7 @@ #include "Factory.h" #include "Entity.h" #include "Component.h" +#include "Components/Template.h" #include "System.h" #include "EventBroker.h" #include "ResourceManager.h" @@ -31,10 +32,14 @@ public: virtual void AddSystems() = 0; virtual void RegisterComponents() = 0; + template + void AddSystem() + { + m_Systems[typeid(T).name()] = std::shared_ptr(m_SystemFactory.Create()); + } - void AddSystem(std::string systemType); - template - std::shared_ptr GetSystem(std::string systemType); + template + std::shared_ptr GetSystem(); EntityID CreateEntity(EntityID parent = 0); EntityID CloneEntity(EntityID entity, EntityID parent = 0); @@ -63,11 +68,15 @@ public: m_EntityProperties[entity][property] = value; } + void SetProperty(EntityID entity, std::string property, char* value) + { + m_EntityProperties[entity][property] = std::string(value); + } + template - std::shared_ptr AddComponent(EntityID entity, std::string componentType); - std::shared_ptr AddComponent(EntityID entity, std::string componentType); + std::shared_ptr AddComponent(EntityID entity); template - T* GetComponent(EntityID entity, std::string componentType); + T* GetComponent(EntityID entity); // Triggers commit events in systems void CommitEntity(EntityID entity); @@ -112,11 +121,13 @@ protected: }; template -std::shared_ptr World::GetSystem(std::string systemType) +std::shared_ptr World::GetSystem() { + const char* systemType = typeid(T).name(); + if (m_Systems.find(systemType) == m_Systems.end()) { - LOG_WARNING("Tried to get pointer to unregistered system \"%s\"!", systemType.c_str()); + LOG_WARNING("Tried to get pointer to unregistered system \"%s\"!", systemType); return nullptr; } @@ -124,12 +135,14 @@ std::shared_ptr World::GetSystem(std::string systemType) } template -std::shared_ptr World::AddComponent(EntityID entity, std::string componentType) +std::shared_ptr World::AddComponent(EntityID entity) { - std::shared_ptr component = std::shared_ptr(static_cast(m_ComponentFactory.Create(componentType))); + const char* componentType = typeid(T).name(); + + std::shared_ptr component = std::shared_ptr(static_cast(m_ComponentFactory.Create())); if (component == nullptr) { - LOG_ERROR("Failed to attach invalid component \"%s\" to entity #%i", componentType.c_str(), entity); + LOG_ERROR("Failed to attach invalid component \"%s\" to entity #%i", componentType, entity); return nullptr; } @@ -140,17 +153,11 @@ std::shared_ptr World::AddComponent(EntityID entity, std::string componentTyp template -T* World::GetComponent(EntityID entity, std::string componentType) +T* World::GetComponent(EntityID entity) { - - /*auto it0 = m_EntityComponents.find(entity); - - if (it0 == m_EntityComponents.end()) - return nullptr;*/ - auto components = m_EntityComponents[entity]; - auto it = components.find(componentType); + auto it = components.find(typeid(T).name()); if (it != components.end()) { return static_cast(it->second.get()); diff --git a/src/gBuffer.cpp b/src/gBuffer.cpp deleted file mode 100644 index 5acf690..0000000 --- a/src/gBuffer.cpp +++ /dev/null @@ -1,40 +0,0 @@ -#include "PrecompiledHeader.h" -#include "gBuffer.h" - -bool GBuffer::Init(unsigned int WindowWidth, unsigned int WindowHeight) -{ - // Create the FBO - glGenFramebuffers(1, &m_fbo); - glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_fbo); - - // Create the gbuffer textures - glGenTextures(ARRAY_SIZE_IN_ELEMENTS(m_textures), m_textures); - glGenTextures(1, &m_depthTexture); - - for (unsigned int i = 0 ; i < ARRAY_SIZE_IN_ELEMENTS(m_textures) ; i++) { - glBindTexture(GL_TEXTURE_2D, m_textures[i]); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F, WindowWidth, WindowHeight, 0, GL_RGB, GL_FLOAT, NULL); - glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, GL_TEXTURE_2D, m_textures[i], 0); - } - - // depth - glBindTexture(GL_TEXTURE_2D, m_depthTexture); - glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32F, WindowWidth, WindowHeight, 0, GL_DEPTH_COMPONENT, GL_FLOAT, - NULL); - glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, m_depthTexture, 0); - - GLenum DrawBuffers[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_COLOR_ATTACHMENT3 }; - glDrawBuffers(ARRAY_SIZE_IN_ELEMENTS(DrawBuffers), DrawBuffers); - - GLenum Status = glCheckFramebufferStatus(GL_FRAMEBUFFER); - - if (Status != GL_FRAMEBUFFER_COMPLETE) { - printf("FB error, status: 0x%x\n", Status); - return false; - } - - // restore default FBO - glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); - - return true; -} \ No newline at end of file diff --git a/src/gBuffer.h b/src/gBuffer.h deleted file mode 100644 index 721eb15..0000000 --- a/src/gBuffer.h +++ /dev/null @@ -1,35 +0,0 @@ -#ifndef gBuffer_h__ -#define gBuffer_h__ - -#include - -class GBuffer -{ -public: - - enum GBUFFER_TEXTURE_TYPE { - GBUFFER_TEXTURE_TYPE_POSITION, - GBUFFER_TEXTURE_TYPE_DIFFUSE, - GBUFFER_TEXTURE_TYPE_NORMAL, - GBUFFER_TEXTURE_TYPE_TEXCOORD, - GBUFFER_NUM_TEXTURES - }; - - GBuffer(); - - ~GBuffer(); - - bool Init(unsigned int WindowWidth, unsigned int WindowHeight); - - void BindForWriting(); - - void BindForReading(); - -private: - - GLuint m_fbo; - GLuint m_textures[GBUFFER_NUM_TEXTURES]; - GLuint m_depthTexture; -}; - -#endif //gBuffer_h__ \ No newline at end of file diff --git a/vs11/Returngeance.psess b/vs11/Returngeance.psess new file mode 100644 index 0000000..1912c4c --- /dev/null +++ b/vs11/Returngeance.psess @@ -0,0 +1,83 @@ + + + + Returngeance.sln + Sampling + None + true + true + Timestamp + Cycles + 10000000 + 10 + 10 + + false + + + + false + 500 + + \Memory\Pages/sec + \PhysicalDisk(_Total)\Avg. Disk Queue Length + \Processor(_Total)\% Processor Time + + + + true + false + false + + false + + + false + + + + bin\Debug\Returngeance.exe + 01/01/0001 00:00:00 + true + true + false + false + false + false + false + true + false + Executable + bin\Debug\Returngeance.exe + ..\bin\Debug + + + IIS + InternetExplorer + true + false + + false + + + false + + {E8B4A2A2-882B-402A-98A7-8B4F7233C8B3}|Returngeance\Returngeance.vcxproj + Returngeance\Returngeance.vcxproj + Returngeance + + + + + Returngeance140427.vsp + + + Returngeance140427(1).vsp + + + + + :PB:{E8B4A2A2-882B-402A-98A7-8B4F7233C8B3}|Returngeance\Returngeance.vcxproj + + + \ 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 diff --git a/vs11/Returngeance/Returngeance.vcxproj b/vs11/Returngeance/Returngeance.vcxproj index 63c4d5a..6a99e47 100755 --- a/vs11/Returngeance/Returngeance.vcxproj +++ b/vs11/Returngeance/Returngeance.vcxproj @@ -39,33 +39,33 @@ - $(HAVOK_2013_1_0_r1_ROOT)\Source;$(BOOST_1_55_0_ROOT);$(SolutionDir)\..\src;$(SolutionDir)\..\libs;$(SolutionDir)\..\libs\glew-1.10.0\include;$(SolutionDir)\..\libs\glfw-3.0.4\include;$(SolutionDir)\..\libs\glm-0.9.5.3;$(SolutionDir)\..\libs\openal-soft-1.15.1\include;$(SolutionDir)\..\libs\bullet-2.82-r2704\src;$(SolutionDir)\..\libs\SOIL\src;$(IncludePath) - $(HAVOK_2013_1_0_r1_ROOT)\Lib\win32_vs2012_win7\debug_dll;$(BOOST_1_55_0_ROOT)\lib32-msvc-11.0;$(SolutionDir)\..\libs\glew-1.10.0\lib\Debug\Win32;$(SolutionDir)\..\libs\glfw-3.0.4\lib\Debug;$(SolutionDir)\..\libs\openal-soft-1.15.1\lib\Win32\Debug;$(SolutionDir)\..\libs\bullet-2.82-r2704\lib\Debug;$(SolutionDir)\..\libs\SOIL\lib\Debug;$(LibraryPath) + $(HAVOK_2013_1_0_r1_ROOT)\Source;$(BOOST_1_55_0_ROOT);$(SolutionDir)\..\src;$(SolutionDir)\..\libs;$(SolutionDir)\..\libs\glew-1.10.0\include;$(SolutionDir)\..\libs\glfw-3.0.4\include;$(SolutionDir)\..\libs\glm-0.9.5.3;$(SolutionDir)\..\libs\openal-soft-1.15.1\include;$(SolutionDir)\..\libs\bullet-2.82-r2704\src;$(SolutionDir)\..\libs\SOIL\src;$(DXSDK_DIR)\Include;$(IncludePath) + $(HAVOK_2013_1_0_r1_ROOT)\Lib\win32_vs2012_win7\debug_dll;$(BOOST_1_55_0_ROOT)\lib32-msvc-11.0;$(SolutionDir)\..\libs\glew-1.10.0\lib\Debug\Win32;$(SolutionDir)\..\libs\glfw-3.0.4\lib\Debug;$(SolutionDir)\..\libs\openal-soft-1.15.1\lib\Win32\Debug;$(SolutionDir)\..\libs\bullet-2.82-r2704\lib\Debug;$(SolutionDir)\..\libs\SOIL\lib\Debug;$(LibraryPath);$(DXSDK_DIR)\Lib\x86 $(SolutionDir)\..\bin\$(Configuration)\ $(SolutionDir)\..\obj\$(Configuration)\ - $(HAVOK_2013_1_0_r1_ROOT)\Source;$(BOOST_1_55_0_ROOT);$(SolutionDir)\..\src;$(SolutionDir)\..\libs;$(SolutionDir)\..\libs\glew-1.10.0\include;$(SolutionDir)\..\libs\glfw-3.0.4\include;$(SolutionDir)\..\libs\glm-0.9.5.3;$(SolutionDir)\..\libs\openal-soft-1.15.1\include;$(SolutionDir)\..\libs\bullet-2.82-r2704\src;$(SolutionDir)\..\libs\SOIL\src;$(IncludePath) - $(HAVOK_2013_1_0_r1_ROOT)\Lib\win32_vs2012_win7\release_dll;$(BOOST_1_55_0_ROOT)\lib32-msvc-11.0;$(SolutionDir)\..\libs\glfw-3.0.4\lib\Release;$(SolutionDir)\..\libs\glew-1.10.0\lib\Release\Win32;$(SolutionDir)\..\libs\openal-soft-1.15.1\lib\Win32\Release;$(SolutionDir)\..\libs\bullet-2.82-r2704\lib\Release;$(SolutionDir)\..\libs\SOIL\lib\Release;$(LibraryPath) + $(HAVOK_2013_1_0_r1_ROOT)\Source;$(BOOST_1_55_0_ROOT);$(SolutionDir)\..\src;$(SolutionDir)\..\libs;$(SolutionDir)\..\libs\glew-1.10.0\include;$(SolutionDir)\..\libs\glfw-3.0.4\include;$(SolutionDir)\..\libs\glm-0.9.5.3;$(SolutionDir)\..\libs\openal-soft-1.15.1\include;$(SolutionDir)\..\libs\bullet-2.82-r2704\src;$(SolutionDir)\..\libs\SOIL\src;$(DXSDK_DIR)\Include;$(IncludePath) + $(HAVOK_2013_1_0_r1_ROOT)\Lib\win32_vs2012_win7\release_dll;$(BOOST_1_55_0_ROOT)\lib32-msvc-11.0;$(SolutionDir)\..\libs\glfw-3.0.4\lib\Release;$(SolutionDir)\..\libs\glew-1.10.0\lib\Release\Win32;$(SolutionDir)\..\libs\openal-soft-1.15.1\lib\Win32\Release;$(SolutionDir)\..\libs\bullet-2.82-r2704\lib\Release;$(SolutionDir)\..\libs\SOIL\lib\Release;$(LibraryPath);$(DXSDK_DIR)\Lib\x86 $(SolutionDir)\..\bin\$(Configuration)\ $(SolutionDir)\..\obj\$(Configuration)\ Level3 - _WINDOWS;WIN32;_WIN32;_DEBUG;HK_DEBUG;HK_DEBUG_SLOW;_XT_STATICLINK;_CONSOLE;_ALLOW_ITERATOR_DEBUG_LEVEL_MISMATCH;HK_CONFIG_SIMD=1;DEBUG;_CRT_SECURE_NO_WARNINGS;_MBCS;%(PreprocessorDefinitions) + _X86_;_WINDOWS;WIN32;_WIN32;_DEBUG;HK_DEBUG;HK_DEBUG_SLOW;_XT_STATICLINK;_CONSOLE;_ALLOW_ITERATOR_DEBUG_LEVEL_MISMATCH;HK_CONFIG_SIMD=1;DEBUG;_CRT_SECURE_NO_WARNINGS;_MBCS;%(PreprocessorDefinitions) Create PrecompiledHeader.h MultiThreadedDebugDLL false Default ProgramDatabase - MaxSpeed + Disabled true true - OpenAL32.lib;opengl32.lib;glu32.lib;SOIL.lib;glew32d.lib;glfw3.lib;hkBase.lib;hkVisualize.lib;hkInternal.lib;hkSerialize.lib;hkGeometryUtilities.lib;hkcdInternal.lib;hkcdCollide.lib;hkpCollide.lib;hkpConstraint.lib;hkpConstraintSolver.lib;hkpDynamics.lib;hkpInternal.lib;hkpUtilities.lib;hkSceneData.lib;hkpVehicle.lib;%(AdditionalDependencies) + OpenAL32.lib;opengl32.lib;glu32.lib;SOIL.lib;XInput9_1_0.lib;glew32d.lib;glfw3.lib;hkBase.lib;hkVisualize.lib;hkInternal.lib;hkSerialize.lib;hkGeometryUtilities.lib;hkcdInternal.lib;hkcdCollide.lib;hkpCollide.lib;hkpConstraint.lib;hkpConstraintSolver.lib;hkpDynamics.lib;hkpInternal.lib;hkpUtilities.lib;hkSceneData.lib;hkpVehicle.lib;%(AdditionalDependencies) /ignore:4221 @@ -80,7 +80,7 @@ true true true - _CRT_SECURE_NO_WARNINGS;_MBCS;HK_CONFIG_SIMD=1;%(PreprocessorDefinitions) + _X86_;_CRT_SECURE_NO_WARNINGS;_MBCS;HK_CONFIG_SIMD=1;%(PreprocessorDefinitions) Create PrecompiledHeader.h StreamingSIMDExtensions2 @@ -89,7 +89,7 @@ true true true - OpenAL32.lib;opengl32.lib;glu32.lib;SOIL.lib;glew32.lib;glfw3.lib;hkBase.lib;hkVisualize.lib;hkInternal.lib;hkSerialize.lib;hkGeometryUtilities.lib;hkcdInternal.lib;hkcdCollide.lib;hkpCollide.lib;hkpConstraint.lib;hkpConstraintSolver.lib;hkpDynamics.lib;hkpInternal.lib;hkpUtilities.lib;hkSceneData.lib;hkpVehicle.lib;%(AdditionalDependencies) + OpenAL32.lib;opengl32.lib;glu32.lib;SOIL.lib;XInput9_1_0.lib;glew32.lib;glfw3.lib;hkBase.lib;hkVisualize.lib;hkInternal.lib;hkSerialize.lib;hkGeometryUtilities.lib;hkcdInternal.lib;hkcdCollide.lib;hkpCollide.lib;hkpConstraint.lib;hkpConstraintSolver.lib;hkpDynamics.lib;hkpInternal.lib;hkpUtilities.lib;hkSceneData.lib;hkpVehicle.lib;%(AdditionalDependencies) @@ -111,6 +111,7 @@ + @@ -125,11 +126,14 @@ + + + @@ -137,28 +141,39 @@ + + + + + + + + + + + @@ -180,6 +195,7 @@ + @@ -196,7 +212,11 @@ + + + + @@ -204,6 +224,7 @@ + diff --git a/vs11/Returngeance/Returngeance.vcxproj.filters b/vs11/Returngeance/Returngeance.vcxproj.filters index cd6fb6b..2bf19bf 100755 --- a/vs11/Returngeance/Returngeance.vcxproj.filters +++ b/vs11/Returngeance/Returngeance.vcxproj.filters @@ -63,6 +63,9 @@ Physics\Systems + + Gameplay\Vehicles\Helicopter\Systems + @@ -137,6 +140,24 @@ {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} + + + {cb06b441-90b8-46ed-b347-4190dac7185b} + @@ -331,12 +352,54 @@ Physics\Systems + + Physics\Components + + + Physics\Components + + + Physics\Events + + + Input\Events + + + Input\Events + + + Input\Events + + + Input\Events + + + Physics\Components + + + Physics\Components + + + Input\Events + + + Gameplay\Vehicles\Helicopter\Components + + + Gameplay\Vehicles\Helicopter\Systems + + + Physics\Events + + + Physics\Events + + + Gameplay\Components + - - Shaders - - + Shaders @@ -360,11 +423,29 @@ Shaders + + Shaders + Shaders Shaders + + Shaders + + + Shaders + + + Shaders + + + Shaders + + + Shaders + \ No newline at end of file