diff --git a/Escape-the-Dawn/Camera.cpp b/Escape-the-Dawn/Camera.cpp index 6deeed0..542864d 100644 --- a/Escape-the-Dawn/Camera.cpp +++ b/Escape-the-Dawn/Camera.cpp @@ -8,30 +8,30 @@ Camera::Camera(float yFOV, float aspectRatio, float nearClip, float farClip) m_FarClip = farClip; m_Position = glm::vec3(0.0); - m_Pitch = 0.f; - m_Yaw = 0.f; + /*m_Pitch = 0.f; + m_Yaw = 0.f;*/ UpdateProjectionMatrix(); UpdateViewMatrix(); } -glm::vec3 Camera::Forward() -{ - return glm::rotate(glm::vec3(0.f, 0.f, -1.f), -m_Yaw, glm::vec3(0.f, 1.f, 0.f)); -} +//glm::vec3 Camera::Forward() +//{ +// return glm::rotate(glm::vec3(0.f, 0.f, -1.f), -m_Yaw, glm::vec3(0.f, 1.f, 0.f)); +//} +// +//glm::vec3 Camera::Right() +//{ +// return glm::rotate(glm::vec3(1.f, 0.f, 0.f), -m_Yaw, glm::vec3(0.f, 1.f, 0.f)); +//} -glm::vec3 Camera::Right() -{ - return glm::rotate(glm::vec3(1.f, 0.f, 0.f), -m_Yaw, glm::vec3(0.f, 1.f, 0.f)); -} - -glm::mat4 Camera::Orientation() -{ - glm::mat4 orientation(1.f); - orientation = glm::rotate(orientation, m_Pitch, glm::vec3(1.f, 0.f, 0.f)); - orientation = glm::rotate(orientation, m_Yaw, glm::vec3(0.f, 1.f, 0.f)); - return orientation; -} +//glm::mat4 Camera::Orientation() +//{ +// glm::mat4 orientation(1.f); +// orientation = glm::rotate(orientation, m_Pitch, glm::vec3(1.f, 0.f, 0.f)); +// orientation = glm::rotate(orientation, m_Yaw, glm::vec3(0.f, 1.f, 0.f)); +// return orientation; +//} void Camera::AspectRatio(float val) { @@ -46,17 +46,23 @@ void Camera::Position(glm::vec3 val) } -void Camera::Pitch(float val) +void Camera::Orientation(glm::quat val) { - m_Pitch = val; + m_Orientation = val; UpdateViewMatrix(); } -void Camera::Yaw(float val) -{ - m_Yaw = val; - UpdateViewMatrix(); -} +//void Camera::Pitch(float val) +//{ +// m_Pitch = val; +// UpdateViewMatrix(); +//} +// +//void Camera::Yaw(float val) +//{ +// m_Yaw = val; +// UpdateViewMatrix(); +//} void Camera::UpdateProjectionMatrix() { @@ -70,5 +76,5 @@ void Camera::UpdateProjectionMatrix() void Camera::UpdateViewMatrix() { - m_ViewMatrix = glm::translate(Orientation(), -m_Position); + m_ViewMatrix = glm::translate(glm::toMat4(m_Orientation), -m_Position); } diff --git a/Escape-the-Dawn/Camera.h b/Escape-the-Dawn/Camera.h index 0b6ecb5..baea8a5 100644 --- a/Escape-the-Dawn/Camera.h +++ b/Escape-the-Dawn/Camera.h @@ -6,6 +6,8 @@ #include #include #include +#include +#include class Camera { @@ -14,7 +16,7 @@ public: glm::vec3 Forward(); glm::vec3 Right(); - glm::mat4 Orientation(); + glm::quat Orientation(); float AspectRatio() const { return m_AspectRatio; } void AspectRatio(float val); @@ -22,16 +24,28 @@ public: glm::vec3 Position() const { return m_Position; } void Position(glm::vec3 val); - float Pitch() const { return m_Pitch; } + glm::quat Orientation() const { return m_Orientation; } + void Orientation(glm::quat val); + + /*float Pitch() const { return m_Pitch; } void Pitch(float val); float Yaw() const { return m_Yaw; } - void Yaw(float val); + void Yaw(float val);*/ glm::mat4 ProjectionMatrix() const { return m_ProjectionMatrix; } void ProjectionMatrix(glm::mat4 val) { m_ProjectionMatrix = val; } glm::mat4 ViewMatrix() const { return m_ViewMatrix; } void ViewMatrix(glm::mat4 val) { m_ViewMatrix = val; } + + float FOV() const { return m_FOV; } + void FOV(float val) { m_FOV = val; } + + float NearClip() const { return m_NearClip; } + void NearClip(float val) { m_NearClip = val; } + + float FarClip() const { return m_FarClip; } + void FarClip(float val) { m_FarClip = val; } private: void UpdateProjectionMatrix(); @@ -41,10 +55,12 @@ private: float m_AspectRatio; float m_NearClip; float m_FarClip; + glm::vec3 m_Position; - float m_Pitch; - float m_Yaw; + glm::quat m_Orientation; + //float m_Pitch; + //float m_Yaw; glm::mat4 m_ProjectionMatrix; glm::mat4 m_ViewMatrix; diff --git a/Escape-the-Dawn/Components/Camera.h b/Escape-the-Dawn/Components/Camera.h index c836df4..62d6905 100644 --- a/Escape-the-Dawn/Components/Camera.h +++ b/Escape-the-Dawn/Components/Camera.h @@ -8,9 +8,9 @@ namespace Components struct Camera : Component { - int FOV; - int NearClip; - int FarClip; + float FOV; + float NearClip; + float FarClip; }; } diff --git a/Escape-the-Dawn/GameWorld.h b/Escape-the-Dawn/GameWorld.h index 43d871b..afe8dc8 100644 --- a/Escape-the-Dawn/GameWorld.h +++ b/Escape-the-Dawn/GameWorld.h @@ -56,15 +56,31 @@ void GameWorld::Initialize() AddSystem("InputSystem"); //AddSystem("CollisionSystem"); //AddSystem("ParticleSystem"); - //AddSystem("PlayerSystem"); + AddSystem("PlayerSystem"); //AddSystem("SoundSystem"); AddSystem("RenderSystem"); - EntityID ent = CreateEntity(); - auto transform = AddComponent(ent, "Transform"); + std::shared_ptr transform; + std::shared_ptr model; + std::shared_ptr camera; + EntityID ent; + + ent = CreateEntity(); + transform = AddComponent(ent, "Transform"); transform->Position = glm::vec3(0.f, 0.f, 0.f); - auto model = AddComponent(ent, "Model"); + model = AddComponent(ent, "Model"); model->ModelFile = "ship.obj"; + + ent = CreateEntity(); + SetProperty(ent, "Name", std::string("Camera")); + transform = AddComponent(ent, "Transform"); + AddComponent(ent, "Input"); + transform->Position = glm::vec3(0.f, 2.f, 10.f); + camera = AddComponent(ent, "Camera"); + camera->FOV = 45.f; + camera->FarClip = 1000.f; + camera->NearClip = 0.01f; + transform->Orientation = glm::angleAxis(glm::radians(25.0f),glm::vec3(1,0,0)); } void GameWorld::RegisterSystems() @@ -73,7 +89,7 @@ void GameWorld::RegisterSystems() 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("PlayerSystem", [this]() { return new Systems::PlayerSystem(this); }); //m_SystemFactory.Register("SoundSystem", [this]() { return new Systems::SoundSystem(this); }); m_SystemFactory.Register("RenderSystem", [this]() { return new Systems::RenderSystem(this, m_Renderer); }); } diff --git a/Escape-the-Dawn/Renderer.cpp b/Escape-the-Dawn/Renderer.cpp index 4ed238f..3268949 100644 --- a/Escape-the-Dawn/Renderer.cpp +++ b/Escape-the-Dawn/Renderer.cpp @@ -42,6 +42,7 @@ void Renderer::Initialize() glEnable(GL_DEPTH_TEST); // Create Camera + m_Camera = std::make_shared(45.f, (float)WIDTH / HEIGHT, 0.01f, 1000.f); m_Camera->Position(glm::vec3(0.0f, 0.0f, 2.f)); @@ -64,9 +65,15 @@ void Renderer::Draw(double _dt) glBindTexture(GL_TEXTURE_2D, ModelsToRender[i]->model->texture[0]->texture); MVP = cameraMatrix * ModelsToRender[i]->ModelMatrix; glUniformMatrix4fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "MVP"), 1, GL_FALSE, glm::value_ptr(MVP)); + glUniformMatrix4fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "model"), 1, GL_FALSE, glm::value_ptr(ModelsToRender[i]->ModelMatrix)); + glUniformMatrix4fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "view"), 1, GL_FALSE, glm::value_ptr(m_Camera->ViewMatrix())); + glUniform3fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "lightPosition"), 1, glm::value_ptr(glm::vec3(2.0f, 4.0f, 1.0f))); + glUniform1f(glGetUniformLocation(m_ShaderProgram.GetHandle(), "constantAttenuation"), 1.5f); + glUniform1f(glGetUniformLocation(m_ShaderProgram.GetHandle(), "linearAttenuation"), 0.0f); + glUniform1f(glGetUniformLocation(m_ShaderProgram.GetHandle(), "quadraticAttenuation"), 0.0f); glBindVertexArray(ModelsToRender[i]->model->VAO); glDrawArrays(GL_TRIANGLES, 0, ModelsToRender[i]->model->Vertices.size()); - GLERROR("DRAW"); + } ModelsToRender.clear(); diff --git a/Escape-the-Dawn/Shaders/Fragment.glsl b/Escape-the-Dawn/Shaders/Fragment.glsl index 2efef52..52c1792 100644 --- a/Escape-the-Dawn/Shaders/Fragment.glsl +++ b/Escape-the-Dawn/Shaders/Fragment.glsl @@ -1,7 +1,14 @@ #version 430 -uniform mat4 MVP; -uniform sampler2D texture0; +uniform mat4 model; +uniform mat4 view; + +layout(binding=0) uniform sampler2D texture; + +uniform vec3 lightPosition; +uniform float constantAttenuation; +uniform float linearAttenuation; +uniform float quadraticAttenuation; in VertexData { vec3 Position; @@ -9,10 +16,62 @@ in VertexData { vec2 TextureCoord; } Input; -out vec4 FragmentColor; +out vec4 fragmentColor; -void main() -{ - vec4 texel = texture2D(texture0, Input.TextureCoord); - FragmentColor = texel; +void main() { + // Texture + vec4 texel = texture2D(texture, Input.TextureCoord); + //vec4 texel1 = texture2D(texture1, Input.TextureCoord * 32.0); + //vec4 texel2 = texture2D(texture2, Input.TextureCoord * 100.0); + //vec4 blend = texture2D(blendmap, Input.TextureCoord); + + //vec4 texel = (blend.x * texel0) + (blend.y * texel1) + (blend.z * texel2); + + // + // Phong shading + // + + // Light + //vec3 lightPosition = vec3(0, 0, 2); + vec3 Ls = vec3(1.0, 1.0, 0.9); // Specular light + vec3 Ld = vec3(1.0, 1.0, 0.9); // Diffuse light + vec3 La = vec3(0.6, 0.6, 0.6); // Ambient light + + // Object + 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 lightPosView = vec3(view * vec4(lightPosition, 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); + + // 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 + vec3 Is = Ls * Ks * specularFactor; + + // Diffuse light + float dotProd = dot(surfaceToLight, surfaceNormal); + dotProd = max(dotProd, 0.0); + vec3 Id = Ld * Kd * abs(dotProd); + + // Ambient light + vec3 Ia = La * Ka; + + float dist = length(lightPosition - surfacePosition); + float attenuation = 1.0 / (constantAttenuation + + linearAttenuation * dist + + quadraticAttenuation * pow(dist, 2.0)); + + fragmentColor = vec4((Is + Id) * attenuation + Ia, 1.0) * texel; + + //fragmentColor = vec4(Id, 1.0) * texel; + + //fragmentColor = texel; } \ No newline at end of file diff --git a/Escape-the-Dawn/Systems/InputSystem.cpp b/Escape-the-Dawn/Systems/InputSystem.cpp index fc29849..b6937f7 100644 --- a/Escape-the-Dawn/Systems/InputSystem.cpp +++ b/Escape-the-Dawn/Systems/InputSystem.cpp @@ -21,8 +21,9 @@ void Systems::InputSystem::Update(double dt) glfwGetCursorPos(m_Renderer->GetWindow(), &xpos, &ypos); m_CurrentMouseDeltaX = xpos - m_LastMouseX; m_CurrentMouseDeltaY = ypos - m_LastMouseY; - m_LastMouseX = xpos; - m_LastMouseY = ypos; + m_LastMouseX = m_Renderer->WIDTH / 2.f; // xpos; + m_LastMouseY = m_Renderer->HEIGHT / 2.f; // ypos; + glfwSetCursorPos(m_Renderer->GetWindow(), m_LastMouseX, m_LastMouseY); } void Systems::InputSystem::UpdateEntity(double dt, EntityID entity, EntityID parent) diff --git a/Escape-the-Dawn/Systems/PlayerSystem.cpp b/Escape-the-Dawn/Systems/PlayerSystem.cpp index e69de29..a237a8f 100644 --- a/Escape-the-Dawn/Systems/PlayerSystem.cpp +++ b/Escape-the-Dawn/Systems/PlayerSystem.cpp @@ -0,0 +1,52 @@ +#include "PlayerSystem.h" +#include "World.h" + + + +void Systems::PlayerSystem::Update(double dt) +{ + +} + +void Systems::PlayerSystem::UpdateEntity(double dt, EntityID entity, EntityID parent) +{ + auto transform = m_World->GetComponent(entity, "Transform"); + if (transform == nullptr) + return; + auto input = m_World->GetComponent(entity, "Input"); + if (input == nullptr) + return; + + auto name = m_World->GetProperty(entity, "Name"); + if (name == "Camera") { + + glm::vec3 Camera_Right = glm::rotate(transform->Orientation, glm::vec3(1, 0, 0)); + glm::vec3 Camera_Forward = glm::rotate(transform->Orientation, glm::vec3(0, 0, 1)); + + + float speed = 4.0f; + if(input->KeyState[GLFW_KEY_LEFT_SHIFT] == GLFW_PRESS) + { + speed = 1.0f; + } + if(input->KeyState[GLFW_KEY_A] == GLFW_PRESS) + { + transform->Position -= Camera_Right * (float)dt*2.f * speed; + } + if(input->KeyState[GLFW_KEY_D] == GLFW_PRESS) + { + transform->Position += Camera_Right * (float)dt*2.f * speed; + } + if(input->KeyState[GLFW_KEY_W] == GLFW_PRESS) + { + transform->Position -= Camera_Forward * (float)dt*2.f * speed; + } + if(input->KeyState[GLFW_KEY_S] == GLFW_PRESS) + { + transform->Position += Camera_Forward * (float)dt*2.f * speed; + } + + transform->Orientation = transform->Orientation * glm::angleAxis(-(input->dX/300.f),glm::vec3(0,1,0)); + transform->Orientation = transform->Orientation * glm::angleAxis(-(input->dY/300.f),glm::vec3(1,0,0)); + } +} diff --git a/Escape-the-Dawn/Systems/PlayerSystem.h b/Escape-the-Dawn/Systems/PlayerSystem.h index e69de29..99efd8b 100644 --- a/Escape-the-Dawn/Systems/PlayerSystem.h +++ b/Escape-the-Dawn/Systems/PlayerSystem.h @@ -0,0 +1,30 @@ +#ifndef PlayerSystem_h__ +#define PlayerSystem_h__ + +#include + +#include "System.h" +#include "Renderer.h" +#include "Components/Transform.h" +#include "Components/Input.h" +#include "logging.h" + +namespace Systems +{ + + class PlayerSystem : public System + { + public: + PlayerSystem(World* world) + : System(world) { } + + void Update(double dt) override; + void UpdateEntity(double dt, EntityID entity, EntityID parent) override; + + private: + + }; + +} + +#endif // InputSystem_h__ diff --git a/Escape-the-Dawn/Systems/RenderSystem.cpp b/Escape-the-Dawn/Systems/RenderSystem.cpp index 2be19c1..1db226c 100644 --- a/Escape-the-Dawn/Systems/RenderSystem.cpp +++ b/Escape-the-Dawn/Systems/RenderSystem.cpp @@ -12,18 +12,31 @@ void Systems::RenderSystem::OnComponentCreated( std::string type, std:: shared_p void Systems::RenderSystem::UpdateEntity( double dt, EntityID entity, EntityID parent ) { - auto modelComponent = m_World->GetComponent(entity, "Model"); - if (modelComponent == nullptr) - return; auto transformComponent = m_World->GetComponent(entity, "Transform"); if (transformComponent == nullptr) return; + auto modelComponent = m_World->GetComponent(entity, "Model"); + if (modelComponent != nullptr) + { + if (m_CachedModels.find(modelComponent->ModelFile) == m_CachedModels.end()){ + m_CachedModels[modelComponent->ModelFile] = new Model(modelComponent->ModelFile); + } - if (m_CachedModels.find(modelComponent->ModelFile) == m_CachedModels.end()) { - m_CachedModels[modelComponent->ModelFile] = new Model(modelComponent->ModelFile); + Model* model = m_CachedModels[modelComponent->ModelFile]; + m_Renderer->AddModelToDraw(model, transformComponent->Position, transformComponent->Orientation); } - Model* model = m_CachedModels[modelComponent->ModelFile]; - m_Renderer->AddModelToDraw(model, transformComponent->Position, transformComponent->Orientation); + auto cameraComponent = m_World->GetComponent(entity, "Camera"); + if (cameraComponent != nullptr) + { + m_Renderer->GetCamera()->Position(transformComponent->Position); + m_Renderer->GetCamera()->Orientation(transformComponent->Orientation); + + m_Renderer->GetCamera()->FOV(cameraComponent->FOV); + m_Renderer->GetCamera()->NearClip(cameraComponent->NearClip); + m_Renderer->GetCamera()->FarClip(cameraComponent->FarClip); + } + + } diff --git a/Escape-the-Dawn/Systems/RenderSystem.cpp.orig b/Escape-the-Dawn/Systems/RenderSystem.cpp.orig new file mode 100644 index 0000000..27bfc77 --- /dev/null +++ b/Escape-the-Dawn/Systems/RenderSystem.cpp.orig @@ -0,0 +1,37 @@ +#include "RenderSystem.h" +#include "World.h" + +void Systems::RenderSystem::OnComponentCreated( std::string type, std:: shared_ptr component ) +{ + if(type == "Model") + { + auto modelComponent = std::static_pointer_cast(component); + +<<<<<<< HEAD + if (m_CachedModels.find(modelComponent->ModelFile) != m_CachedModels.end()) { + m_CachedModels[modelComponent->ModelFile] = new Model(modelComponent->ModelFile); + } +======= +>>>>>>> ce6b570520794087eb98447a571578585a705ded + } +} + +void Systems::RenderSystem::UpdateEntity( double dt, EntityID entity, EntityID parent ) +{ + auto modelComponent = m_World->GetComponent(entity, "Model"); + if (modelComponent == nullptr) + return; + auto transformComponent = m_World->GetComponent(entity, "Transform"); + if (transformComponent == nullptr) + return; + + if (m_CachedModels.find(modelComponent->ModelFile) == m_CachedModels.end()) { + m_CachedModels[modelComponent->ModelFile] = new Model(modelComponent->ModelFile); + } + Model* model = m_CachedModels[modelComponent->ModelFile]; + m_Renderer->AddModelToDraw(model, transformComponent->Position, transformComponent->Orientation); +} + + + + diff --git a/Escape-the-Dawn/Systems/RenderSystem.h b/Escape-the-Dawn/Systems/RenderSystem.h index 539cd83..fab25ea 100644 --- a/Escape-the-Dawn/Systems/RenderSystem.h +++ b/Escape-the-Dawn/Systems/RenderSystem.h @@ -6,6 +6,7 @@ #include "Texture.h" #include "Components/Model.h" #include "Components/Transform.h" +#include "Components/Camera.h" #include "Renderer.h" #include diff --git a/Escape-the-Dawn/World.h b/Escape-the-Dawn/World.h index 7f46c7f..0e1a652 100644 --- a/Escape-the-Dawn/World.h +++ b/Escape-the-Dawn/World.h @@ -42,6 +42,11 @@ public: template T GetProperty(EntityID entity, std::string property) { + if(m_EntityProperties.find(entity) == m_EntityProperties.end()) + return T(); + if(m_EntityProperties[entity].find(property) == m_EntityProperties[entity].end()) + return T(); + return boost::any_cast(m_EntityProperties[entity][property]); } diff --git a/Escape-the-Dawn/shiptexture.png b/Escape-the-Dawn/shiptexture.png index 5b56a63..ed6b9e4 100644 Binary files a/Escape-the-Dawn/shiptexture.png and b/Escape-the-Dawn/shiptexture.png differ