Merge branch 'master' of github.com:sippeangelo/Escape-the-Dawn

Conflicts:
	Escape-the-Dawn/Systems/SoundsSystem.cpp
This commit is contained in:
Adam
2014-03-10 01:59:48 +01:00
14 changed files with 299 additions and 56 deletions
+32 -26
View File
@@ -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);
}
+21 -5
View File
@@ -6,6 +6,8 @@
#include <glm/gtc/constants.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtx/rotate_vector.hpp>
#include <glm/gtc/quaternion.hpp>
#include <glm/gtx/quaternion.hpp>
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;
+3 -3
View File
@@ -8,9 +8,9 @@ namespace Components
struct Camera : Component
{
int FOV;
int NearClip;
int FarClip;
float FOV;
float NearClip;
float FarClip;
};
}
+21 -5
View File
@@ -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<Components::Transform>(ent, "Transform");
std::shared_ptr<Components::Transform> transform;
std::shared_ptr<Components::Model> model;
std::shared_ptr<Components::Camera> camera;
EntityID ent;
ent = CreateEntity();
transform = AddComponent<Components::Transform>(ent, "Transform");
transform->Position = glm::vec3(0.f, 0.f, 0.f);
auto model = AddComponent<Components::Model>(ent, "Model");
model = AddComponent<Components::Model>(ent, "Model");
model->ModelFile = "ship.obj";
ent = CreateEntity();
SetProperty(ent, "Name", std::string("Camera"));
transform = AddComponent<Components::Transform>(ent, "Transform");
AddComponent<Components::Input>(ent, "Input");
transform->Position = glm::vec3(0.f, 2.f, 10.f);
camera = AddComponent<Components::Camera>(ent, "Camera");
camera->FOV = 45.f;
camera->FarClip = 1000.f;
camera->NearClip = 0.01f;
transform->Orientation = glm::angleAxis<float>(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); });
}
+8 -1
View File
@@ -42,6 +42,7 @@ void Renderer::Initialize()
glEnable(GL_DEPTH_TEST);
// Create Camera
m_Camera = std::make_shared<Camera>(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();
+66 -7
View File
@@ -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;
}
+3 -2
View File
@@ -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)
+52
View File
@@ -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<Components::Transform>(entity, "Transform");
if (transform == nullptr)
return;
auto input = m_World->GetComponent<Components::Input>(entity, "Input");
if (input == nullptr)
return;
auto name = m_World->GetProperty<std::string>(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<float>(-(input->dX/300.f),glm::vec3(0,1,0));
transform->Orientation = transform->Orientation * glm::angleAxis<float>(-(input->dY/300.f),glm::vec3(1,0,0));
}
}
+30
View File
@@ -0,0 +1,30 @@
#ifndef PlayerSystem_h__
#define PlayerSystem_h__
#include <array>
#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__
+20 -7
View File
@@ -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<Components::Model>(entity, "Model");
if (modelComponent == nullptr)
return;
auto transformComponent = m_World->GetComponent<Components::Transform>(entity, "Transform");
if (transformComponent == nullptr)
return;
auto modelComponent = m_World->GetComponent<Components::Model>(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<Components::Camera>(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);
}
}
@@ -0,0 +1,37 @@
#include "RenderSystem.h"
#include "World.h"
void Systems::RenderSystem::OnComponentCreated( std::string type, std:: shared_ptr<Component> component )
{
if(type == "Model")
{
auto modelComponent = std::static_pointer_cast<Components::Model>(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<Components::Model>(entity, "Model");
if (modelComponent == nullptr)
return;
auto transformComponent = m_World->GetComponent<Components::Transform>(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);
}
+1
View File
@@ -6,6 +6,7 @@
#include "Texture.h"
#include "Components/Model.h"
#include "Components/Transform.h"
#include "Components/Camera.h"
#include "Renderer.h"
#include <unordered_map>
+5
View File
@@ -42,6 +42,11 @@ public:
template <class T>
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<T>(m_EntityProperties[entity][property]);
}
Binary file not shown.

Before

Width:  |  Height:  |  Size: 879 KiB

After

Width:  |  Height:  |  Size: 69 KiB