A gray tank! :D

This commit is contained in:
2014-04-05 01:24:19 +02:00
parent ed29297d00
commit ad2862bf84
15 changed files with 289 additions and 18 deletions
+2
View File
@@ -1,6 +1,8 @@
#ifndef Camera_h__
#define Camera_h__
//#include "PrecompiledHeader.h"
class Camera
{
public:
+14
View File
@@ -0,0 +1,14 @@
#ifndef FreeSteering_h__
#define FreeSteering_h__
#include "Component.h"
namespace Components
{
struct FreeSteering : Component
{
float Speed = 35;
};
}
#endif // FreeSteering_h__
+40 -7
View File
@@ -5,11 +5,41 @@ void GameWorld::Initialize()
{
World::Initialize();
auto terrain = CreateEntity();
auto transform = AddComponent<Components::Transform>(terrain, "Transform");
std::shared_ptr<Components::Transform> transform;
std::shared_ptr<Components::Model> model;
auto camera = CreateEntity();
transform = AddComponent<Components::Transform>(camera, "Transform");
transform->Position.z = 5.f;
auto cameraComp = AddComponent<Components::Camera>(camera, "Camera");
AddComponent(camera, "Input");
auto freeSteering = AddComponent<Components::FreeSteering>(camera, "FreeSteering");
/*auto terrain = CreateEntity();
transform = AddComponent<Components::Transform>(terrain, "Transform");
transform->Scale = glm::vec3(.0005f);
auto model = AddComponent<Components::Model>(terrain, "Model");
model->ModelFile = "Models/terrain/terrain.obj";
model = AddComponent<Components::Model>(terrain, "Model");
model->ModelFile = "Models/terrain/terrain.obj";*/
auto tank = CreateEntity();
transform = AddComponent<Components::Transform>(tank, "Transform");
transform->Scale = glm::vec3(.01f);
model = AddComponent<Components::Model>(tank, "Model");
model->ModelFile = "Models/tank/tank_base.obj";
{
auto tank_top = CreateEntity(tank);
transform = AddComponent<Components::Transform>(tank_top, "Transform");
//transform->Scale = glm::vec3(.0005f);
model = AddComponent<Components::Model>(tank_top, "Model");
model->ModelFile = "Models/tank/tank_top.obj";
{
auto tank_barrel = CreateEntity(tank_top);
transform = AddComponent<Components::Transform>(tank_barrel, "Transform");
//transform->Scale = glm::vec3(.0005f);
model = AddComponent<Components::Model>(tank_barrel, "Model");
model->ModelFile = "Models/tank/tank_barrel.obj";
}
}
}
void GameWorld::Update(double dt)
@@ -31,17 +61,19 @@ void GameWorld::RegisterComponents()
m_ComponentFactory.Register("Sprite", []() { return new Components::Sprite(); });
m_ComponentFactory.Register("Template", []() { return new Components::Template(); });
m_ComponentFactory.Register("Transform", []() { return new Components::Transform(); });
m_ComponentFactory.Register("FreeSteering", []() { return new Components::FreeSteering(); });
}
void GameWorld::RegisterSystems()
{
m_SystemFactory.Register("TransformSystem", [this]() { return new Systems::TransformSystem(this); });
//m_SystemFactory.Register("LevelGenerationSystem", [this]() { return new Systems::LevelGenerationSystem(this); });
//m_SystemFactory.Register("InputSystem", [this]() { return new Systems::InputSystem(this, m_Renderer); });
m_SystemFactory.Register("InputSystem", [this]() { return new Systems::InputSystem(this, m_Renderer); });
//m_SystemFactory.Register("CollisionSystem", [this]() { return new Systems::CollisionSystem(this); });
////m_SystemFactory.Register("ParticleSystem", [this]() { return new Systems::ParticleSystem(this); });
//m_SystemFactory.Register("PlayerSystem", [this]() { return new Systems::PlayerSystem(this); });
m_SystemFactory.Register("FreeSteeringSystem", [this]() { return new Systems::FreeSteeringSystem(this); });
//m_SystemFactory.Register("SoundSystem", [this]() { return new Systems::SoundSystem(this); });
m_SystemFactory.Register("TransformSystem", [this]() { return new Systems::TransformSystem(this); });
m_SystemFactory.Register("RenderSystem", [this]() { return new Systems::RenderSystem(this, m_Renderer); });
}
@@ -49,10 +81,11 @@ void GameWorld::AddSystems()
{
AddSystem("TransformSystem");
//AddSystem("LevelGenerationSystem");
//AddSystem("InputSystem");
AddSystem("InputSystem");
//AddSystem("CollisionSystem");
////AddSystem("ParticleSystem");
//AddSystem("PlayerSystem");
AddSystem("FreeSteeringSystem");
//AddSystem("SoundSystem");
AddSystem("RenderSystem");
}
+3 -2
View File
@@ -4,12 +4,13 @@
#include "World.h"
#include "Renderer.h"
//#include "Systems/TransformSystem.h"
#include "Systems/TransformSystem.h"
//#include "Systems/CollisionSystem.h"
//#include "Systems/InputSystem.h"
#include "Systems/InputSystem.h"
//#include "Systems/LevelGenerationSystem.h"
//#include "Systems/ParticleSystem.h"
//#include "Systems/PlayerSystem.h"
#include "Systems/FreeSteeringSystem.h"
#include "Systems/RenderSystem.h"
//#include "Systems/SoundSystem.h"
+5 -4
View File
@@ -14,7 +14,7 @@ Renderer::Renderer()
m_DrawBounds = false;
#endif
m_ShadowMapRes = 2048*8;
m_ShadowMapRes = 2048;
m_SunPosition = glm::vec3(0, 1.5f, 10);
m_SunTarget = glm::vec3(0, 0, 0);
m_SunProjection = glm::ortho<float>(-200, 200, -100, 400, -800, 600);
@@ -30,9 +30,10 @@ void Renderer::Initialize()
}
// Create a window
WIDTH = 1920;
HEIGHT = 1080;
glfwWindowHint(GLFW_SAMPLES, 16);
WIDTH = 1280;
HEIGHT = 720;
// Antialiasing
//glfwWindowHint(GLFW_SAMPLES, 16);
m_Window = glfwCreateWindow(WIDTH, HEIGHT, "OpenGL", nullptr, nullptr);
if (!m_Window) {
LOG_ERROR("GLFW: Failed to create window");
+61
View File
@@ -0,0 +1,61 @@
#include "PrecompiledHeader.h"
#include "FreeSteeringSystem.h"
#include "World.h"
Systems::FreeSteeringSystem::FreeSteeringSystem(World* world) : System(world)
{
}
void Systems::FreeSteeringSystem::Update(double dt)
{
}
void Systems::FreeSteeringSystem::UpdateEntity(double dt, EntityID entity, EntityID parent)
{
auto steering = m_World->GetComponent<Components::FreeSteering>(entity, "FreeSteering");
auto input = m_World->GetComponent<Components::Input>(entity, "Input");
if (steering && input) {
auto transform = m_World->GetComponent<Components::Transform>(entity, "Transform");
glm::vec3 Camera_Right = glm::vec3(glm::vec4(1, 0, 0, 0) * transform->Orientation);
glm::vec3 Camera_Forward = glm::vec3(glm::vec4(0, 0, 1, 0) * transform->Orientation);
float speed = steering->Speed;
if (input->KeyState[GLFW_KEY_LEFT_SHIFT]) {
speed *= 4.0f;
}
if (input->KeyState[GLFW_KEY_LEFT_ALT]) {
speed /= 4.0f;
}
if (input->KeyState[GLFW_KEY_A]) {
transform->Position -= Camera_Right * (float)dt * speed;
}
else if (input->KeyState[GLFW_KEY_D]) {
transform->Position += Camera_Right * (float)dt * speed;
}
if (input->KeyState[GLFW_KEY_W]) {
transform->Position -= Camera_Forward * (float)dt * speed;
}
if (input->KeyState[GLFW_KEY_S]) {
transform->Position += Camera_Forward * (float)dt * speed;
}
if (input->KeyState[GLFW_KEY_SPACE]) {
transform->Position += glm::vec3(0, 1, 0) * (float)dt * speed;
}
if (input->KeyState[GLFW_KEY_LEFT_CONTROL]) {
transform->Position -= glm::vec3(0, 1, 0) * (float)dt * speed;
}
if (input->MouseState[GLFW_MOUSE_BUTTON_LEFT]) {
// TOUCHING THIS CODE MIGHT COUSE THE UNIVERSE TO IMPLODE, ALSO DRAGONS
//---------------------------------------------------------------------
transform->Orientation = glm::angleAxis<float>(input->dY / 300.f, glm::vec3(1, 0, 0)) * transform->Orientation;
transform->Orientation = transform->Orientation * glm::angleAxis<float>(input->dX / 300.f, glm::vec3(0, 1, 0));
//---------------------------------------------------------------------
// TOUCHING THIS CODE MIGHT COUSE THE UNIVERSE TO IMPLODE, ALSO DRAGONS
}
}
}
+18
View File
@@ -0,0 +1,18 @@
#include <array>
#include "System.h"
#include "Components/Transform.h"
#include "Components/Input.h"
#include "Components/FreeSteering.h"
namespace Systems
{
class FreeSteeringSystem : public System
{
public:
FreeSteeringSystem(World* world);
void Update(double dt) override;
void UpdateEntity(double dt, EntityID entity, EntityID parent) override;
};
}
+73
View File
@@ -0,0 +1,73 @@
#include "PrecompiledHeader.h"
#include "InputSystem.h"
#include "World.h"
void Systems::InputSystem::Update(double dt)
{
m_LastKeyState = m_CurrentKeyState;
m_LastMouseState = m_CurrentMouseState;
// Keyboard input
for (int i = 0; i <= GLFW_KEY_LAST; ++i) {
m_CurrentKeyState[i] = glfwGetKey(m_Renderer->GetWindow(), i);
}
// Mouse buttons
for (int i = 0; i <= GLFW_MOUSE_BUTTON_LAST; ++i) {
m_CurrentMouseState[i] = glfwGetMouseButton(m_Renderer->GetWindow(), i);
}
// Cursor position
double xpos, ypos;
glfwGetCursorPos(m_Renderer->GetWindow(), &xpos, &ypos);
m_CurrentMouseDeltaX = xpos - m_LastMouseX;
m_CurrentMouseDeltaY = ypos - m_LastMouseY;
m_LastMouseX = xpos;
m_LastMouseY = ypos;
// Lock mouse while holding LMB
if (m_CurrentMouseState[GLFW_MOUSE_BUTTON_LEFT]) {
m_LastMouseX = m_Renderer->WIDTH / 2.f; // xpos;
m_LastMouseY = m_Renderer->HEIGHT / 2.f; // ypos;
glfwSetCursorPos(m_Renderer->GetWindow(), m_LastMouseX, m_LastMouseY);
}
// Hide/show cursor with LMB
if (m_CurrentMouseState[GLFW_MOUSE_BUTTON_LEFT] && !m_LastMouseState[GLFW_MOUSE_BUTTON_LEFT]) {
glfwSetInputMode(m_Renderer->GetWindow(), GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
}
if (!m_CurrentMouseState[GLFW_MOUSE_BUTTON_LEFT] && m_LastMouseState[GLFW_MOUSE_BUTTON_LEFT]) {
glfwSetInputMode(m_Renderer->GetWindow(), GLFW_CURSOR, GLFW_CURSOR_NORMAL);
}
#ifdef DEBUG
// Wireframe
if (m_CurrentKeyState[GLFW_KEY_F1] && !m_LastKeyState[GLFW_KEY_F1]) {
m_Renderer->DrawWireframe(!m_Renderer->DrawWireframe());
}
// Normals
if (m_CurrentKeyState[GLFW_KEY_F2] && !m_LastKeyState[GLFW_KEY_F2]) {
m_Renderer->DrawNormals(!m_Renderer->DrawNormals());
}
// Bounds
if (m_CurrentKeyState[GLFW_KEY_F3] && !m_LastKeyState[GLFW_KEY_F3]) {
m_Renderer->DrawBounds(!m_Renderer->DrawBounds());
}
#endif
}
void Systems::InputSystem::UpdateEntity(double dt, EntityID entity, EntityID parent)
{
auto input = m_World->GetComponent<Components::Input>(entity, "Input");
if (input == nullptr)
return;
input->KeyState = m_CurrentKeyState;
input->LastKeyState = m_LastKeyState;
input->MouseState = m_CurrentMouseState;
input->LastMouseState = m_LastMouseState;
input->dX = m_CurrentMouseDeltaX;
input->dY = m_CurrentMouseDeltaY;
}
std::array<int, GLFW_KEY_LAST+1> Systems::InputSystem::m_CurrentKeyState;
std::array<int, GLFW_KEY_LAST+1> Systems::InputSystem::m_LastKeyState;
+35
View File
@@ -0,0 +1,35 @@
#ifndef InputSystem_h__
#define InputSystem_h__
#include <array>
#include "System.h"
#include "Renderer.h"
#include "Components/Input.h"
namespace Systems
{
class InputSystem : public System
{
public:
InputSystem(World* world, std::shared_ptr<Renderer> renderer)
: System(world), m_Renderer(renderer) { }
void Update(double dt) override;
void UpdateEntity(double dt, EntityID entity, EntityID parent) override;
private:
std::shared_ptr<Renderer> m_Renderer;
static std::array<int, GLFW_KEY_LAST+1> m_CurrentKeyState;
static std::array<int, GLFW_KEY_LAST+1> m_LastKeyState;
std::array<int, GLFW_MOUSE_BUTTON_LAST+1> m_CurrentMouseState;
std::array<int, GLFW_MOUSE_BUTTON_LAST+1> m_LastMouseState;
float m_CurrentMouseDeltaX, m_CurrentMouseDeltaY;
float m_LastMouseX, m_LastMouseY;
};
}
#endif // InputSystem_h__
+2 -1
View File
@@ -25,7 +25,8 @@ void Systems::RenderSystem::UpdateEntity(double dt, EntityID entity, EntityID pa
auto model = m_CachedModels[modelComponent->ModelFile];
glm::vec3 position = m_TransformSystem->AbsolutePosition(entity);
glm::quat orientation = m_TransformSystem->AbsoluteOrientation(entity);
m_Renderer->AddModelToDraw(model, position, orientation, transformComponent->Scale, modelComponent->Visible, modelComponent->ShadowCaster);
glm::vec3 scale = m_TransformSystem->AbsoluteScale(entity);
m_Renderer->AddModelToDraw(model, position, orientation, scale, modelComponent->Visible, modelComponent->ShadowCaster);
}
// Debug draw bounds
+14
View File
@@ -46,3 +46,17 @@ glm::quat Systems::TransformSystem::AbsoluteOrientation(EntityID entity)
return absOrientation;
}
glm::vec3 Systems::TransformSystem::AbsoluteScale(EntityID entity)
{
glm::vec3 absScale(1);
do
{
auto transform = m_World->GetComponent<Components::Transform>(entity, "Transform");
absScale *= transform->Scale;
entity = m_World->GetEntityParent(entity);
} while (entity != 0);
return absScale;
}
+1
View File
@@ -18,6 +18,7 @@ namespace Systems
glm::vec3 AbsolutePosition(EntityID entity);
glm::quat AbsoluteOrientation(EntityID entity);
glm::vec3 AbsoluteScale(EntityID entity);
};
}
-3
View File
@@ -19,7 +19,4 @@ Global
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(Performance) = preSolution
HasPerformanceSessions = true
EndGlobalSection
EndGlobal
+5
View File
@@ -95,6 +95,8 @@
<ClCompile Include="..\..\src\Renderer.cpp" />
<ClCompile Include="..\..\src\ShaderProgram.cpp" />
<ClCompile Include="..\..\src\Skybox.cpp" />
<ClCompile Include="..\..\src\Systems\FreeSteeringSystem.cpp" />
<ClCompile Include="..\..\src\Systems\InputSystem.cpp" />
<ClCompile Include="..\..\src\Systems\RenderSystem.cpp" />
<ClCompile Include="..\..\src\Systems\TransformSystem.cpp" />
<ClCompile Include="..\..\src\Texture.cpp" />
@@ -108,6 +110,7 @@
<ClInclude Include="..\..\src\Components\Camera.h" />
<ClInclude Include="..\..\src\Components\Collision.h" />
<ClInclude Include="..\..\src\Components\DirectionalLight.h" />
<ClInclude Include="..\..\src\Components\FreeSteering.h" />
<ClInclude Include="..\..\src\Components\Input.h" />
<ClInclude Include="..\..\src\Components\Model.h" />
<ClInclude Include="..\..\src\Components\ParticleEmitter.h" />
@@ -130,6 +133,8 @@
<ClInclude Include="..\..\src\ShaderProgram.h" />
<ClInclude Include="..\..\src\Skybox.h" />
<ClInclude Include="..\..\src\System.h" />
<ClInclude Include="..\..\src\Systems\FreeSteeringSystem.h" />
<ClInclude Include="..\..\src\Systems\InputSystem.h" />
<ClInclude Include="..\..\src\Systems\RenderSystem.h" />
<ClInclude Include="..\..\src\Systems\TransformSystem.h" />
<ClInclude Include="..\..\src\Texture.h" />
@@ -19,6 +19,12 @@
<ClCompile Include="..\..\src\Systems\TransformSystem.cpp">
<Filter>Systems</Filter>
</ClCompile>
<ClCompile Include="..\..\src\Systems\InputSystem.cpp">
<Filter>Systems</Filter>
</ClCompile>
<ClCompile Include="..\..\src\Systems\FreeSteeringSystem.cpp">
<Filter>Systems</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<Filter Include="Util">
@@ -106,6 +112,15 @@
<ClInclude Include="..\..\src\Systems\TransformSystem.h">
<Filter>Systems</Filter>
</ClInclude>
<ClInclude Include="..\..\src\Systems\InputSystem.h">
<Filter>Systems</Filter>
</ClInclude>
<ClInclude Include="..\..\src\Systems\FreeSteeringSystem.h">
<Filter>Systems</Filter>
</ClInclude>
<ClInclude Include="..\..\src\Components\FreeSteering.h">
<Filter>Components</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="..\..\src\Shaders\AABB.frag.glsl">