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

Conflicts:
	Escape-the-Dawn/Systems/LevelGenerationSystem.cpp
	Escape-the-Dawn/Systems/PlayerSystem.cpp
This commit is contained in:
ViktorLjung
2014-03-13 19:36:03 +01:00
23 changed files with 304 additions and 166 deletions
+3
View File
@@ -10,7 +10,10 @@ namespace Components
struct Collision : Component
{
Collision() : Phantom(false), Interested(false) { }
bool Phantom;
bool Interested;
std::vector<EntityID> CollidingEntities;
};
+1 -1
View File
@@ -11,7 +11,7 @@ namespace Components
struct SoundEmitter : Component
{
float Gain;
float MaxDistance;
//float MaxDistance;
float ReferenceDistance;
float Pitch;
bool Loop;
+3 -3
View File
@@ -82,12 +82,12 @@
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>GLEW_STATIC;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>GLEW_STATIC;DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<DebugInformationFormat>None</DebugInformationFormat>
<DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
<MinimalRebuild>true</MinimalRebuild>
</ClCompile>
<Link>
<GenerateDebugInformation>false</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<AdditionalDependencies>OpenAL32.lib;opengl32.lib;glu32.lib;SOIL.lib;glew32s.lib;glfw3.lib;%(AdditionalDependencies)</AdditionalDependencies>
+37 -33
View File
@@ -21,31 +21,16 @@ void GameWorld::Initialize()
std::shared_ptr<Components::Collision> collision;
EntityID ent;
// Camera
entcamera = CreateEntity();
SetProperty(entcamera, "Name", std::string("Camera"));
transform = AddComponent<Components::Transform>(entcamera, "Transform");
AddComponent<Components::Input>(entcamera, "Input");
transform->Position = glm::vec3(0.f, 10.f, 14.f);
camera = AddComponent<Components::Camera>(entcamera, "Camera");
camera->FOV = 45.f;
camera->FarClip = 1000.f;
camera->NearClip = 0.01f;
transform->Orientation = glm::angleAxis<float>(glm::radians(15.0f),glm::vec3(1,0,0));
collision = AddComponent<Components::Collision>(entcamera, "Collision");
collision->Phantom = false;
bounds = AddComponent<Components::Bounds>(entcamera, "Bounds");
bounds->VolumeVector = glm::vec3(0.1f,0.1f,0.1f);
// Fucking lights
ent = CreateEntity();
transform = AddComponent<Components::Transform>(ent, "Transform");
transform->Position = glm::vec3(0.f, 4.f, 0.f);
transform->Position = glm::vec3(10.f, 2.f, 5.f);
transform->Position = glm::vec3(0.f, 7.f, 0.f);
pointLight = AddComponent<Components::PointLight>(ent, "PointLight");
pointLight->Specular = glm::vec3(1.0, 1.0, 1.0);
pointLight->Diffuse = glm::vec3(1.0, 1.0, 1.0);
pointLight->Diffuse = glm::vec3(1.0, 1.0, 5.0);
pointLight->Diffuse = glm::vec3(0.0, 0.0, 5.0);
pointLight->constantAttenuation = 0.f;
pointLight->linearAttenuation = 1.f;
pointLight->quadraticAttenuation = 0.f;
@@ -54,13 +39,10 @@ void GameWorld::Initialize()
model->ModelFile = "Models/sphere.obj";
ent = CreateEntity();
transform = AddComponent<Components::Transform>(ent, "Transform");
transform->Position = glm::vec3(10.f, 4.f, 0.f);
transform->Position = glm::vec3(0.f, 2.f, 5.f);
transform->Position = glm::vec3(10.f, 7.f, 0.f);
pointLight = AddComponent<Components::PointLight>(ent, "PointLight");
pointLight->Specular = glm::vec3(1.0, 1.0, 1.0);
pointLight->Diffuse = glm::vec3(1.0, 1.0, 1.0);
pointLight->Specular = glm::vec3(3.0, 3.0, 3.0);
pointLight->Diffuse = glm::vec3(3.0, 0.0, 0.0);
pointLight->Diffuse = glm::vec3(0.0, 5.0, 0.0);
pointLight->constantAttenuation = 0.f;
pointLight->linearAttenuation = 1.f;
pointLight->quadraticAttenuation = 0.f;
@@ -69,12 +51,10 @@ void GameWorld::Initialize()
model->ModelFile = "Models/sphere.obj";
ent = CreateEntity();
transform = AddComponent<Components::Transform>(ent, "Transform");
transform->Position = glm::vec3(0.f, -4.f, 0.f);
transform->Position = glm::vec3(-10.f, 2.f, 5.f);
transform->Position = glm::vec3(-10.f, 7.f, 0.f);
pointLight = AddComponent<Components::PointLight>(ent, "PointLight");
pointLight->Specular = glm::vec3(1.0, 1.0, 1.0);
pointLight->Diffuse = glm::vec3(1.0, 1.0, 1.0);
pointLight->Diffuse = glm::vec3(0.0, 3.0, 0.0);
pointLight->Diffuse = glm::vec3(5.0, 0.0, 0.0);
pointLight->constantAttenuation = 0.f;
pointLight->linearAttenuation = 1.f;
pointLight->quadraticAttenuation = 0.f;
@@ -93,18 +73,42 @@ void GameWorld::Initialize()
m_Player = CreateEntity();
SetProperty(m_Player, "Name", std::string("PlayerShip"));
transform = AddComponent<Components::Transform>(m_Player, "Transform");
transform->Position = glm::vec3(0.f, 4.f, -5.f);
transform->Position = glm::vec3(0.f, 2.f, -5.f);
transform->Scale = glm::vec3(1.0f);
model = AddComponent<Components::Model>(m_Player, "Model");
model->ModelFile = "Models/ship.obj";
pointLight = AddComponent<Components::PointLight>(m_Player, "PointLight");
pointLight->Specular = glm::vec3(1.0, 1.0, 1.0);
pointLight->Diffuse = glm::vec3(0.4, 0.4, 1.0);
pointLight->constantAttenuation = 0.f;
pointLight->linearAttenuation = 1.f;
pointLight->quadraticAttenuation = 0.f;
pointLight->spotExponent = 0.0f;
AddComponent<Components::Input>(m_Player, "Input");
collision = AddComponent<Components::Collision>(m_Player, "Collision");
collision->Phantom = false;
collision->Interested = true;
bounds = AddComponent<Components::Bounds>(m_Player, "Bounds");
bounds->Origin = glm::vec3(transform->Position.x, transform->Position.y - 4, transform->Position.z + 3);
bounds->VolumeVector = glm::vec3(4.f,0.7f,4);
bounds->Origin = glm::vec3(0, 0, 2.f);
bounds->VolumeVector = glm::vec3(4.f, 0.7f, 1);
ent = CreateEntity();
// Camera
entcamera = CreateEntity(m_Player);
SetProperty(entcamera, "Name", std::string("Camera"));
transform = AddComponent<Components::Transform>(entcamera, "Transform");
AddComponent<Components::Input>(entcamera, "Input");
transform->Position = glm::vec3(0.f, 10.f, 14.f);
camera = AddComponent<Components::Camera>(entcamera, "Camera");
camera->FOV = 45.f;
camera->FarClip = 1000.f;
camera->NearClip = 0.01f;
transform->Orientation = glm::angleAxis<float>(glm::radians(15.0f),glm::vec3(1,0,0));
/*ent = CreateEntity();
transform = AddComponent<Components::Transform>(ent, "Transform");
transform->Position = glm::vec3(10.f, 4.f, 0.f);
model = AddComponent<Components::Model>(ent, "Model");
@@ -113,7 +117,7 @@ void GameWorld::Initialize()
collision->Phantom = false;
bounds = AddComponent<Components::Bounds>(player2, "Bounds");
bounds->Origin = transform->Position;
bounds->VolumeVector = glm::vec3(2.f,2.f,2.f);
bounds->VolumeVector = glm::vec3(2.f,2.f,2.f);*/
}
void GameWorld::Update(double dt)
+12
View File
@@ -0,0 +1,12 @@
# Blender MTL File: 'powerup.blend'
# Material Count: 1
newmtl Material.001
Ns 96.078431
Ka 0.000000 0.000000 0.000000
Kd 0.640000 0.640000 0.640000
Ks 0.500000 0.500000 0.500000
Ni 1.000000
d 1.000000
illum 2
map_Kd powerupTexture.png
Binary file not shown.

After

Width:  |  Height:  |  Size: 6.5 KiB

+97 -52
View File
@@ -3,12 +3,21 @@
Renderer::Renderer()
{
m_VSync = false;
#ifdef DEBUG
m_DrawNormals = false;
m_DrawWireframe = false;
m_DrawBounds = true;
#else
m_DrawNormals = false;
m_DrawWireframe = false;
m_DrawBounds = false;
#endif
m_SunPosition = glm::vec3(0, 10, 10);
m_ShadowMapRes = 2048*2;
m_SunPosition = glm::vec3(0, 0.3f, 10);
m_SunTarget = glm::vec3(0, 0, 0);
m_SunProjection = glm::ortho<float>(-20, 20, -20, 20, -10, 20);
m_SunProjection = glm::ortho<float>(-200, 200, -10, 100, -800, 400);
Lights = 0;
}
void Renderer::Initialize()
@@ -92,7 +101,7 @@ void Renderer::LoadContent()
m_DebugAABB = CreateAABB();
m_ScreenQuad = CreateQuad();
CreateShadowMap(2048*2);
CreateShadowMap(m_ShadowMapRes);
}
void Renderer::CreateShadowMap(int resolution)
@@ -128,31 +137,33 @@ void Renderer::Draw(double dt)
DrawScene();
#ifdef DEBUG
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);
// 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);
}
}
AABBsToRender.clear();
//DrawDebugShadowMap();
#endif
DrawDebugShadowMap();
ModelsToRender.clear();
ClearStuff();
glfwSwapBuffers(m_Window);
}
@@ -173,33 +184,51 @@ void Renderer::DrawScene()
#endif
// Draw models
glm::mat4 depthViewMatrix = glm::lookAt(m_SunPosition, m_SunTarget, glm::vec3(0, 1, 0));
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 depthMVP = depthCamera;
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 depthBiasMVP = biasMatrix * depthMVP;
m_ShaderProgram.Bind();
glUniformMatrix4fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "DepthMVP"), 1, GL_FALSE, glm::value_ptr(depthBiasMVP));
glUniform3fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "position"), 3, Light_position.data());
glUniform3fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "specular"), 3, Light_specular.data());
glUniform3fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "diffuse"), 3, Light_diffuse.data());
glUniform1fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "constantAttenuation"), 3, Light_constantAttenuation.data());
glUniform1fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "linearAttenuation"), 3, Light_linearAttenuation.data());
glUniform1fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "quadraticAttenuation"), 3, Light_quadraticAttenuation.data());
glUniform1fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "spotExponent"), 3, Light_spotExponent.data());
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);
//DrawModels(m_ShaderProgram);
glm::mat4 cameraMatrix = m_Camera->ProjectionMatrix() * m_Camera->ViewMatrix();
glm::mat4 MVP;
for (auto tuple : ModelsToRender)
{
Model* model;
glm::mat4 modelMatrix;
std::tie(model, modelMatrix) = tuple;
MVP = cameraMatrix * modelMatrix;
glm::mat4 depthMVP = biasMatrix * depthCamera * 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->texture);
glDrawArrays(GL_TRIANGLES, texGroup.StartIndex, texGroup.EndIndex - texGroup.StartIndex + 1);
}
}
#ifdef DEBUG
// Debug draw model normals
if (m_DrawNormals) {
@@ -217,12 +246,13 @@ void Renderer::DrawShadowMap()
glCullFace(GL_BACK);
glBindFramebuffer(GL_FRAMEBUFFER, m_ShadowFrameBuffer);
glViewport(0, 0, 2048*2, 2048*2);
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::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));
glm::mat4 depthCamera = m_SunProjection * depthViewMatrix;
//glm::mat4 cameraMatrix = depthProjectionMatrix * m_Camera->ViewMatrix();
@@ -231,12 +261,13 @@ void Renderer::DrawShadowMap()
m_ShaderProgramShadows.Bind();
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
for (int i = 0; i < ModelsToRender.size(); i++)
for (auto tuple : ModelsToRender)
{
ModelData* modelData = ModelsToRender.at(i);
auto model = modelData->model;
Model* model;
glm::mat4 modelMatrix;
std::tie(model, modelMatrix) = tuple;
MVP = depthCamera * modelData->ModelMatrix;
MVP = depthCamera * modelMatrix;
glUniformMatrix4fv(glGetUniformLocation(m_ShaderProgramShadows.GetHandle(), "MVP"), 1, GL_FALSE, glm::value_ptr(MVP));
glBindVertexArray(model->VAO);
@@ -266,14 +297,15 @@ void Renderer::DrawModels(ShaderProgram &shader)
glm::mat4 cameraMatrix = m_Camera->ProjectionMatrix() * m_Camera->ViewMatrix();
glm::mat4 MVP;
for (int i = 0; i < ModelsToRender.size(); i++)
for (auto tuple : ModelsToRender)
{
ModelData* modelData = ModelsToRender.at(i);
auto model = modelData->model;
Model* model;
glm::mat4 modelMatrix;
std::tie(model, modelMatrix) = tuple;
MVP = cameraMatrix * modelData->ModelMatrix;
MVP = cameraMatrix * modelMatrix;
glUniformMatrix4fv(glGetUniformLocation(shader.GetHandle(), "MVP"), 1, GL_FALSE, glm::value_ptr(MVP));
glUniformMatrix4fv(glGetUniformLocation(shader.GetHandle(), "model"), 1, GL_FALSE, glm::value_ptr(modelData->ModelMatrix));
glUniformMatrix4fv(glGetUniformLocation(shader.GetHandle(), "model"), 1, GL_FALSE, glm::value_ptr(modelMatrix));
glUniformMatrix4fv(glGetUniformLocation(shader.GetHandle(), "view"), 1, GL_FALSE, glm::value_ptr(m_Camera->ViewMatrix()));
glBindVertexArray(model->VAO);
for (auto texGroup : model->TextureGroups) {
@@ -296,9 +328,9 @@ void Renderer::AddTextToDraw()
void Renderer::AddModelToDraw(std::shared_ptr<Model> model, glm::vec3 position, glm::quat orientation, glm::vec3 scale)
{
glm::mat4 ModelMatrix = glm::translate(glm::mat4(), position) * glm::toMat4(orientation) * glm::scale(scale);
glm::mat4 modelMatrix = glm::translate(glm::mat4(), position) * glm::toMat4(orientation) * glm::scale(scale);
// You can now use ModelMatrix to build the MVP matrix
ModelsToRender.push_back(new ModelData(model, ModelMatrix));
ModelsToRender.push_back(std::make_tuple(model.get(), modelMatrix));
}
void Renderer::AddPointLightToDraw(
@@ -324,7 +356,7 @@ void Renderer::AddPointLightToDraw(
Light_linearAttenuation.push_back(_linearAttenuation);
Light_quadraticAttenuation.push_back(_quadraticAttenuation);
Light_spotExponent.push_back(_spotExponent);
Lights = Light_constantAttenuation.size();
}
void Renderer::AddAABBToDraw(glm::vec3 origin, glm::vec3 volumeVector, bool colliding)
@@ -426,3 +458,16 @@ GLuint Renderer::CreateAABB()
return vao;
}
void Renderer::ClearStuff()
{
AABBsToRender.clear();
ModelsToRender.clear();
Light_position.clear();
Light_specular.clear();
Light_diffuse.clear();
Light_constantAttenuation.clear();
Light_linearAttenuation.clear();
Light_quadraticAttenuation.clear();
Light_spotExponent.clear();
}
+9 -11
View File
@@ -30,15 +30,8 @@ public:
int HEIGHT, WIDTH;
struct ModelData
{
std::shared_ptr<Model> model;
glm::mat4 ModelMatrix;
ModelData(std::shared_ptr<Model> _model, glm::mat4 _modelMatrix)
: model(_model), ModelMatrix(_modelMatrix) {}
};
std::vector<ModelData*> ModelsToRender;
std::list<std::tuple<Model*, glm::mat4>> ModelsToRender;
int Lights;
std::vector<float> Light_position;
std::vector<float> Light_specular;
std::vector<float> Light_diffuse;
@@ -46,7 +39,7 @@ public:
std::vector<float> Light_linearAttenuation;
std::vector<float> Light_quadraticAttenuation;
std::vector<float> Light_spotExponent;
std::vector<std::tuple<glm::mat4, bool>> AABBsToRender;
std::list<std::tuple<glm::mat4, bool>> AABBsToRender;
Renderer();
@@ -76,6 +69,8 @@ public:
void DrawNormals(bool val) { m_DrawNormals = val; }
bool DrawWireframe() const { return m_DrawWireframe; }
void DrawWireframe(bool val) { m_DrawWireframe = val; }
bool DrawBounds() const { return m_DrawBounds; }
void DrawBounds(bool val) { m_DrawBounds = val; }
private:
GLFWwindow* m_Window;
@@ -84,7 +79,9 @@ private:
bool m_VSync;
bool m_DrawNormals;
bool m_DrawWireframe;
bool m_DrawBounds;
int m_ShadowMapRes;
glm::vec3 m_SunPosition;
glm::vec3 m_SunTarget;
glm::mat4 m_SunProjection;
@@ -101,7 +98,8 @@ private:
ShaderProgram m_ShaderProgramShadows;
ShaderProgram m_ShaderProgramShadowsDrawDepth;
ShaderProgram m_ShaderProgramDebugAABB;
void ClearStuff();
void DrawScene();
void DrawModels(ShaderProgram &shader);
void DrawShadowMap();
+17 -15
View File
@@ -6,15 +6,15 @@ uniform mat4 view;
layout(binding=0) uniform sampler2D texture0;
layout(binding=1) uniform sampler2D shadowMap;
const int numberOfLights = 3;
uniform vec3 position[numberOfLights];
uniform vec3 specular[numberOfLights];
uniform vec3 diffuse[numberOfLights];
uniform float constantAttenuation[numberOfLights];
uniform float linearAttenuation[numberOfLights];
uniform float quadraticAttenuation[numberOfLights];
uniform float spotExponent[numberOfLights];
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;
@@ -23,7 +23,7 @@ in VertexData {
vec3 ShadowCoord;
} Input;
vec3 scene_ambient = vec3(0.2);
vec3 scene_ambient = vec3(0.3);
out vec4 fragmentColor;
@@ -49,18 +49,20 @@ void main() {
//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 bias = 0.003;
float visibility = 1.0;
vec4 shadowMapValue = texture(shadowMap, Input.ShadowCoord.xy);
if (shadowMapValue.z < Input.ShadowCoord.z - bias) {
visibility = 0.5;
if (Input.ShadowCoord.x >= 0.0 && Input.ShadowCoord.x <= 1.0 && Input.ShadowCoord.y >= 0.0 && Input.ShadowCoord.y <= 1.0) {
float bias = 0.003;
vec4 shadowMapValue = texture(shadowMap, Input.ShadowCoord.xy);
if (shadowMapValue.z < Input.ShadowCoord.z - bias) {
visibility = 0.5;
}
}
vec3 totalLighting = La * Ka * visibility;
float attenuation;
for(int i = 0; i < numberOfLights; i++)
for(int i = 0; i < numberOfLights && i < maxNumberOfLights; i++)
{
// Light
//vec3 lightPosition = vec3(0, 0, 2);
@@ -12,7 +12,7 @@ out vec4 FragmentColor;
float LinearizeDepth(float z)
{
float n = 0.1; // camera z near
float f = 10.0; // camera z far
float f = 800.0; // camera z far
return (2.0 * n) / (f + n - z * (f - n));
}
Binary file not shown.
Binary file not shown.
+3 -1
View File
@@ -19,7 +19,9 @@ public:
virtual void UpdateEntity(double dt, EntityID entity, EntityID parent) { }
// Called when a component is created
virtual void OnComponentCreated(std::string type, std:: shared_ptr<Component> component) { }
virtual void OnComponentCreated(std::string type, std::shared_ptr<Component> component) { }
// Called when a component is removed
virtual void OnComponentRemoved(std::string type, Component* component) { }
protected:
World* m_World;
+6 -1
View File
@@ -20,6 +20,10 @@ void Systems::CollisionSystem::UpdateEntity(double dt, EntityID entity, EntityID
// Clear old collisions
collisionComponent->CollidingEntities.clear();
// Quit out if we're not interested in collision events
if (!collisionComponent->Interested)
return;
auto entities = m_World->GetEntities();
for (auto pair : *entities) {
@@ -43,7 +47,8 @@ void Systems::CollisionSystem::UpdateEntity(double dt, EntityID entity, EntityID
//pair.first, pair.second;
if(entity == entity2)
break;
continue;
Intersects(entity, entity2);
}
}
+4
View File
@@ -47,6 +47,10 @@ void Systems::InputSystem::Update(double dt)
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
}
@@ -5,14 +5,14 @@ Systems::LevelGenerationSystem::LevelGenerationSystem( World* world )
{
elapsedtime = 0;
velocity = 0;
startx = 0;
startyz = glm::vec2(0, -1000);
}
void Systems::LevelGenerationSystem::SpawnObstacle()
{
typeRandom = 0 + (rand() % 3);
typeRandom = 0 + (rand() % 10);
EntityID ent;
@@ -23,45 +23,64 @@ void Systems::LevelGenerationSystem::SpawnObstacle()
bounds = m_World->AddComponent<Components::Bounds>(ent, "Bounds");
collision = m_World->AddComponent<Components::Collision>(ent, "Collision");
model = m_World->AddComponent<Components::Model>(ent, "Model");
auto sound = m_World->AddComponent<Components::SoundEmitter>(ent, "SoundEmitter");
positionRandom = -500 + startx + (rand() % 1000);
transform->Position = glm::vec3(positionRandom, startyz);
//transform->Velocity = glm::vec3(0.f, 10.f, 100.f);
switch (typeRandom)
{
case 0: // Mountain stuff :D
sound->Loop = true;
sound->Gain = 1.f;
sound->ReferenceDistance = 15.f;
m_World->GetSystem<Systems::SoundSystem>("SoundSystem")->PlaySound(sound, "Sounds/hum.wav");
if(typeRandom >= 0 && typeRandom < 2) // Mountain stuff :D
{
transform->Position = glm::vec3( positionRandom, startyz); // fix position
float scale = (float)(rand() % 1000) / 250;
transform->Scale = glm::vec3(scale);
bounds->VolumeVector = glm::vec3(9, 12, 7);
bounds->Origin = glm::vec3(1,11,-1);
model->ModelFile = "Models/obstacle_mountain_1.obj";
}
break;
case 1: // Single Mountain :D
else if(typeRandom >= 2 && typeRandom < 5) // Single Mountain :D
{
transform->Position = glm::vec3( positionRandom, startyz); // fix position
float scale = (float)(rand() % 1000) / 200;
transform->Scale = glm::vec3(scale);
bounds->VolumeVector = glm::vec3(3.5f, 7.5f, 3.5f);
bounds->Origin = glm::vec3(0,7.5f,0);
model->ModelFile = "Models/obstacle_mountain_2.obj";
}
break;
case 2:
else if(typeRandom >= 5 && typeRandom < 8)
{
transform->Position = glm::vec3( positionRandom, startyz); // fix position
float scale = (float)(rand() % 1000) / 100;
transform->Scale = glm::vec3(scale);
bounds->VolumeVector = glm::vec3(1, 1, 1);
bounds->Origin = glm::vec3(0,1,0);
model->ModelFile = "Models/obstacle_cube_1.obj";
}
break;
default:
if(typeRandom >= 8 && typeRandom < 10)
{
float scale = (float)(rand() % 1000) / 100;
bounds->VolumeVector = glm::vec3(4, 4, 4);
bounds->Origin = glm::vec3(0,4,0);
pointLight = m_World->AddComponent<Components::PointLight>(ent, "PointLight");
pointLight->Specular = glm::vec3(1.0, 1.0, 1.0);
pointLight->Diffuse = glm::vec3(0.3, 1.0, 0.3);
pointLight->constantAttenuation = 0.f;
pointLight->linearAttenuation = 1.f;
pointLight->quadraticAttenuation = 0.f;
pointLight->spotExponent = 0.0f;
break;
}
powerUp = m_World->AddComponent<Components::PowerUp>(ent, "PowerUp");
powerUp->Speed = 10.f;
model->ModelFile = "Models/powerup.obj";
}
// Put it below ground level
transform->Position.y -= bounds->VolumeVector.y * 2.f;
}
@@ -75,14 +94,22 @@ void Systems::LevelGenerationSystem::Update( double dt )
elapsedtime = 0;
}
std::vector<EntityID> removethis;
std::list<EntityID> removethis;
for(auto ent : obstacles)
{
auto transformComponent = m_World->GetComponent<Components::Transform>(ent, "Transform");
transformComponent->Position.z += 100.f * dt;
auto transform = m_World->GetComponent<Components::Transform>(ent, "Transform");
if(transformComponent->Position.z > 100)
transform->Velocity.z = -velocity;
transform->Position += transform->Velocity * (float)dt;
// Stop raising obstacles when they reach ground level
if (transform->Velocity.y > 0 && transform->Position.y >= 0) {
transform->Position.y = 0;
transform->Velocity.y = 0;
}
if(transform->Position.z > 800)
{
removethis.push_back(ent);
}
@@ -104,6 +131,7 @@ void Systems::LevelGenerationSystem::UpdateEntity( double dt, EntityID entity, E
auto transformComponent = m_World->GetComponent<Components::Transform>(entity, "Transform");
startyz = glm::vec2(0, transformComponent->Position.z - 1000);
startx = transformComponent->Position.x;
velocity = transformComponent->Velocity.z;
}
}
@@ -2,11 +2,16 @@
#define LevelGenerationSystem_h__
#include "System.h"
#include "Systems/SoundSystem.h"
#include "World.h"
#include "Components/Transform.h"
#include "Components/Bounds.h"
#include "Components/Collision.h"
#include "Components/Model.h"
#include "Components/PointLight.h"
#include "Components/SoundEmitter.h"
#include "Components/PowerUp.h"
namespace Systems
{
@@ -30,12 +35,14 @@ namespace Systems
double elapsedtime;
std::list<EntityID> obstacles;
float velocity;
std::shared_ptr<Components::Transform> transform;
std::shared_ptr<Components::Bounds> bounds;
std::shared_ptr<Components::Collision> collision;
std::shared_ptr<Components::Model> model;
std::shared_ptr<Components::PointLight> pointLight;
std::shared_ptr<Components::PowerUp> powerUp;
};
}
+16 -14
View File
@@ -3,6 +3,8 @@
Systems::PlayerSystem::PlayerSystem( World* world ) : System(world)
{
m_PlayerSpeed = 20;
m_PlayerOriginalBounds = glm::vec3(0);
}
void Systems::PlayerSystem::Update(double dt)
@@ -18,14 +20,12 @@ void Systems::PlayerSystem::UpdateEntity(double dt, EntityID entity, EntityID pa
if (input == nullptr)
return;
float speed = 50.0f;
auto name = m_World->GetProperty<std::string>(entity, "Name");
if (name == "Camera") {
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 = m_PlayerSpeed;
if(input->KeyState[GLFW_KEY_LEFT_SHIFT]) {
speed *= 4.0f;
}
@@ -59,19 +59,15 @@ void Systems::PlayerSystem::UpdateEntity(double dt, EntityID entity, EntityID pa
//---------------------------------------------------------------------
// TOUCHING THIS CODE MIGHT COUSE THE UNIVERSE TO IMPLODE, ALSO DRAGONS
}
auto collision = m_World->GetComponent<Components::Collision>(entity, "Collision");
if(collision->CollidingEntities.size() > 0) {
// DO STUFF
}
}
name = m_World->GetProperty<std::string>(entity, "Name");
if (name == "PlayerShip")
{
auto bounds = m_World->GetComponent<Components::Bounds>(entity, "Bounds");
if (bounds && m_PlayerOriginalBounds == glm::vec3(0)) {
m_PlayerOriginalBounds = bounds->VolumeVector;
}
glm::vec3 Ship_Right = glm::vec3(glm::vec4(1, 0, 0, 0));
glm::vec3 Ship_Forward = glm::vec3(glm::vec4(0, 0, 1, 0));
@@ -79,7 +75,7 @@ void Systems::PlayerSystem::UpdateEntity(double dt, EntityID entity, EntityID pa
glm::vec3 Euler = glm::eulerAngles(transform->Orientation);
if(input->KeyState[GLFW_KEY_LEFT]) {
transform->Position -= Ship_Right * (float)dt * speed;
transform->Position -= Ship_Right * (float)dt * m_PlayerSpeed;
if(Euler.z < 10.f)
{
@@ -95,7 +91,7 @@ void Systems::PlayerSystem::UpdateEntity(double dt, EntityID entity, EntityID pa
}
}
else if(input->KeyState[GLFW_KEY_RIGHT]) {
transform->Position += Ship_Right * (float)dt * speed;
transform->Position += Ship_Right * (float)dt * m_PlayerSpeed;
if(Euler.z > -10.f)
{
@@ -119,6 +115,12 @@ void Systems::PlayerSystem::UpdateEntity(double dt, EntityID entity, EntityID pa
else if(Euler.z > 0.f)
transform->Orientation = transform->Orientation * glm::angleAxis<float>((float)dt,glm::vec3(0,0,-1));
}
// Update player bounds based on rotation
if (bounds) {
Euler = glm::eulerAngles(transform->Orientation);
bounds->VolumeVector.x = m_PlayerOriginalBounds.x * glm::cos(glm::radians(Euler.z));
}
}
}
+3 -1
View File
@@ -7,6 +7,7 @@
#include "Components/Transform.h"
#include "Components/Input.h"
#include "Components/Collision.h"
#include "Components/Bounds.h"
#include "logging.h"
@@ -23,7 +24,8 @@ namespace Systems
void UpdateEntity(double dt, EntityID entity, EntityID parent) override;
private:
float m_PlayerSpeed;
glm::vec3 m_PlayerOriginalBounds;
};
}
+2 -1
View File
@@ -20,6 +20,7 @@ public:
void Update(double dt) override;
void UpdateEntity(double dt, EntityID entity, EntityID parent) override;
void OnComponentCreated(std::string type, std::shared_ptr<Component> component) override;
void OnComponentRemoved(std::string type, Component* component) override;
void PlaySound(std::shared_ptr<Components::SoundEmitter> emitter, std::string fileName);
private:
@@ -35,7 +36,7 @@ private:
unsigned long dataSize;
std::map<Component*, ALuint> m_Source;
std::map<Component*, ALuint> m_Sources;
std::map<std::string, ALuint> m_BufferCache; // string = fileName
};
+23 -7
View File
@@ -18,6 +18,9 @@ Systems::SoundSystem::SoundSystem(World* world)
}
alGetError();
alSpeedOfSound(340.29f); // Speed of sound
alDistanceModel(AL_INVERSE_DISTANCE);
}
void Systems::SoundSystem::Update(double dt)
@@ -54,10 +57,10 @@ void Systems::SoundSystem::UpdateEntity(double dt, EntityID entity, EntityID par
auto soundEmitter = m_World->GetComponent<Components::SoundEmitter>(entity, "SoundEmitter");
if(soundEmitter != nullptr)
{
ALuint source = m_Source[soundEmitter.get()];
alSourcei(source, AL_GAIN, soundEmitter->Gain);
alSourcei(source, AL_MAX_DISTANCE, soundEmitter->MaxDistance);
alSourcei(source, AL_REFERENCE_DISTANCE, soundEmitter->ReferenceDistance);
ALuint source = m_Sources[soundEmitter];
alSourcef(source, AL_GAIN, soundEmitter->Gain);
//alSourcef(source, AL_MAX_DISTANCE, soundEmitter->MaxDistance);
alSourcef(source, AL_REFERENCE_DISTANCE, soundEmitter->ReferenceDistance);
alSourcef(source, AL_PITCH, soundEmitter->Pitch);
alSourcei(source, AL_LOOPING, soundEmitter->Loop);
@@ -74,17 +77,30 @@ void Systems::SoundSystem::UpdateEntity(double dt, EntityID entity, EntityID par
void Systems::SoundSystem::PlaySound(std::shared_ptr<Components::SoundEmitter> emitter, std::string fileName)
{
if (m_Sources.find(emitter.get()) == m_Sources.end())
return;
ALuint buffer = LoadFile(fileName);
ALuint source = m_Source[emitter.get()];
ALuint source = m_Sources[emitter.get()];
alSourcei(source, AL_BUFFER, buffer);
alSourcePlay(m_Source[emitter.get()]);
alSourcePlay(m_Sources[emitter.get()]);
}
void Systems::SoundSystem::OnComponentCreated(std::string type, std::shared_ptr<Component> component)
{
if(type == "SoundEmitter") {
ALuint source = CreateSource();
m_Source[component.get()] = source;
m_Sources[component.get()] = source;
}
}
void Systems::SoundSystem::OnComponentRemoved(std::string type, Component* component)
{
if(type == "SoundEmitter") {
if (m_Sources.find(component) != m_Sources.end()) {
ALuint source = m_Sources[component];
alDeleteSources(1, &source);
}
}
}
+8 -1
View File
@@ -61,12 +61,19 @@ bool World::ValidEntity(EntityID entity)
void World::RemoveEntity(EntityID entity)
{
m_EntityParents.erase(entity);
m_EntityComponents.erase(entity);
// Remove components
for (auto pair : m_EntityComponents[entity]) {
auto type = pair.first;
auto component = pair.second;
// Trigger events
for (auto pair : m_Systems) {
auto system = pair.second;
system->OnComponentRemoved(type, component.get());
}
m_ComponentsOfType[type].remove(component);
}
m_EntityComponents.erase(entity);
RecycleEntityID(entity);
}
+3 -3
View File
@@ -60,7 +60,7 @@ public:
std::shared_ptr<T> AddComponent(EntityID entity, std::string componentType);
std::shared_ptr<Component> AddComponent(EntityID entity, std::string componentType);
template <class T>
std::shared_ptr<T> GetComponent(EntityID entity, std::string componentType);
T* GetComponent(EntityID entity, std::string componentType);
/*std::vector<EntityID> GetEntityChildren(EntityID entity);*/
@@ -117,9 +117,9 @@ std::shared_ptr<T> World::AddComponent(EntityID entity, std::string componentTyp
template <class T>
std::shared_ptr<T> World::GetComponent(EntityID entity, std::string componentType)
T* World::GetComponent(EntityID entity, std::string componentType)
{
return std::static_pointer_cast<T>(m_EntityComponents[entity][componentType]);
return (T*)m_EntityComponents[entity][componentType].get();
}
#endif // World_h__