I Can't Believe It's Not Shadows!®

This commit is contained in:
2014-03-13 16:29:34 +01:00
parent a711ea7db8
commit 4905c17ebf
11 changed files with 81 additions and 41 deletions
-3
View File
@@ -17,7 +17,4 @@ Global
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE
EndGlobalSection EndGlobalSection
GlobalSection(Performance) = preSolution
HasPerformanceSessions = true
EndGlobalSection
EndGlobal EndGlobal
+3 -3
View File
@@ -54,7 +54,6 @@
<OutDir>$(SolutionDir)bin\</OutDir> <OutDir>$(SolutionDir)bin\</OutDir>
<IntDir>$(SolutionDir)obj\$(ProjectName)\</IntDir> <IntDir>$(SolutionDir)obj\$(ProjectName)\</IntDir>
<TargetName>$(ProjectName)-$(Configuration)</TargetName> <TargetName>$(ProjectName)-$(Configuration)</TargetName>
<LinkIncremental>true</LinkIncremental>
</PropertyGroup> </PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile> <ClCompile>
@@ -83,9 +82,10 @@
<Optimization>MaxSpeed</Optimization> <Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking> <FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions> <IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>GLEW_STATIC;%(PreprocessorDefinitions)</PreprocessorDefinitions> <PreprocessorDefinitions>GLEW_STATIC;DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PrecompiledHeader>NotUsing</PrecompiledHeader> <PrecompiledHeader>NotUsing</PrecompiledHeader>
<DebugInformationFormat>None</DebugInformationFormat> <DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
<MinimalRebuild>true</MinimalRebuild>
</ClCompile> </ClCompile>
<Link> <Link>
<EnableCOMDATFolding>true</EnableCOMDATFolding> <EnableCOMDATFolding>true</EnableCOMDATFolding>
+5 -5
View File
@@ -86,7 +86,7 @@ void GameWorld::Initialize()
m_Player = CreateEntity(); m_Player = CreateEntity();
SetProperty(m_Player, "Name", std::string("PlayerShip")); SetProperty(m_Player, "Name", std::string("PlayerShip"));
transform = AddComponent<Components::Transform>(m_Player, "Transform"); 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); transform->Scale = glm::vec3(1.0f);
model = AddComponent<Components::Model>(m_Player, "Model"); model = AddComponent<Components::Model>(m_Player, "Model");
model->ModelFile = "Models/ship.obj"; model->ModelFile = "Models/ship.obj";
@@ -102,10 +102,10 @@ void GameWorld::Initialize()
collision->Phantom = false; collision->Phantom = false;
collision->Interested = true; collision->Interested = true;
bounds = AddComponent<Components::Bounds>(m_Player, "Bounds"); bounds = AddComponent<Components::Bounds>(m_Player, "Bounds");
bounds->Origin = glm::vec3(transform->Position.x, transform->Position.y - 4, transform->Position.z + 3); bounds->Origin = glm::vec3(0, 0, 2.f);
bounds->VolumeVector = glm::vec3(4.f,0.7f,4); bounds->VolumeVector = glm::vec3(4.f, 0.7f, 1);
ent = CreateEntity(); /*ent = CreateEntity();
transform = AddComponent<Components::Transform>(ent, "Transform"); transform = AddComponent<Components::Transform>(ent, "Transform");
transform->Position = glm::vec3(10.f, 4.f, 0.f); transform->Position = glm::vec3(10.f, 4.f, 0.f);
model = AddComponent<Components::Model>(ent, "Model"); model = AddComponent<Components::Model>(ent, "Model");
@@ -114,7 +114,7 @@ void GameWorld::Initialize()
collision->Phantom = false; collision->Phantom = false;
bounds = AddComponent<Components::Bounds>(player2, "Bounds"); bounds = AddComponent<Components::Bounds>(player2, "Bounds");
bounds->Origin = transform->Position; 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) void GameWorld::Update(double dt)
+31 -12
View File
@@ -6,9 +6,10 @@ Renderer::Renderer()
m_DrawNormals = false; m_DrawNormals = false;
m_DrawWireframe = false; m_DrawWireframe = false;
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_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; Lights = 0;
} }
@@ -93,7 +94,7 @@ void Renderer::LoadContent()
m_DebugAABB = CreateAABB(); m_DebugAABB = CreateAABB();
m_ScreenQuad = CreateQuad(); m_ScreenQuad = CreateQuad();
CreateShadowMap(2048*2); CreateShadowMap(m_ShadowMapRes);
} }
void Renderer::CreateShadowMap(int resolution) void Renderer::CreateShadowMap(int resolution)
@@ -129,6 +130,7 @@ void Renderer::Draw(double dt)
DrawScene(); DrawScene();
#ifdef DEBUG #ifdef DEBUG
// Draw bounding boxes
glEnable(GL_BLEND); glEnable(GL_BLEND);
glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ZERO); glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ZERO);
m_ShaderProgramDebugAABB.Bind(); m_ShaderProgramDebugAABB.Bind();
@@ -149,9 +151,8 @@ void Renderer::Draw(double dt)
glDrawArrays(GL_LINES, 0, 24); glDrawArrays(GL_LINES, 0, 24);
} }
#endif
DrawDebugShadowMap(); DrawDebugShadowMap();
#endif
ClearStuff(); ClearStuff();
glfwSwapBuffers(m_Window); glfwSwapBuffers(m_Window);
@@ -174,19 +175,16 @@ void Renderer::DrawScene()
#endif #endif
// Draw models // 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 depthCamera = m_SunProjection * depthViewMatrix;
glm::mat4 depthMVP = depthCamera;
glm::mat4 biasMatrix( glm::mat4 biasMatrix(
0.5, 0.0, 0.0, 0.0, 0.5, 0.0, 0.0, 0.0,
0.0, 0.5, 0.0, 0.0, 0.0, 0.5, 0.0, 0.0,
0.0, 0.0, 0.5, 0.0, 0.0, 0.0, 0.5, 0.0,
0.5, 0.5, 0.5, 1.0 0.5, 0.5, 0.5, 1.0
); );
glm::mat4 depthBiasMVP = biasMatrix * depthMVP;
m_ShaderProgram.Bind(); m_ShaderProgram.Bind();
glUniformMatrix4fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "DepthMVP"), 1, GL_FALSE, glm::value_ptr(depthBiasMVP));
glUniform1i(glGetUniformLocation(m_ShaderProgram.GetHandle(), "numberOfLights"), Lights); glUniform1i(glGetUniformLocation(m_ShaderProgram.GetHandle(), "numberOfLights"), Lights);
glUniform3fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "position"), Lights, Light_position.data()); 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(), "specular"), Lights, Light_specular.data());
@@ -200,7 +198,27 @@ void Renderer::DrawScene()
} }
glActiveTexture(GL_TEXTURE1); glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, m_ShadowDepthTexture); 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 #ifdef DEBUG
// Debug draw model normals // Debug draw model normals
@@ -219,12 +237,13 @@ void Renderer::DrawShadowMap()
glCullFace(GL_BACK); glCullFace(GL_BACK);
glBindFramebuffer(GL_FRAMEBUFFER, m_ShadowFrameBuffer); glBindFramebuffer(GL_FRAMEBUFFER, m_ShadowFrameBuffer);
glViewport(0, 0, 2048*2, 2048*2); glViewport(0, 0, m_ShadowMapRes, m_ShadowMapRes);
glClear(GL_DEPTH_BUFFER_BIT); glClear(GL_DEPTH_BUFFER_BIT);
//glClearColor(0.0f, 0.0f, 0.0f, 0.0f); //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 depthCamera = m_SunProjection * depthViewMatrix;
//glm::mat4 cameraMatrix = depthProjectionMatrix * m_Camera->ViewMatrix(); //glm::mat4 cameraMatrix = depthProjectionMatrix * m_Camera->ViewMatrix();
+1
View File
@@ -78,6 +78,7 @@ private:
bool m_DrawNormals; bool m_DrawNormals;
bool m_DrawWireframe; bool m_DrawWireframe;
int m_ShadowMapRes;
glm::vec3 m_SunPosition; glm::vec3 m_SunPosition;
glm::vec3 m_SunTarget; glm::vec3 m_SunTarget;
glm::mat4 m_SunProjection; glm::mat4 m_SunProjection;
+8 -6
View File
@@ -23,7 +23,7 @@ in VertexData {
vec3 ShadowCoord; vec3 ShadowCoord;
} Input; } Input;
vec3 scene_ambient = vec3(0.2); vec3 scene_ambient = vec3(0.3);
out vec4 fragmentColor; out vec4 fragmentColor;
@@ -49,18 +49,20 @@ void main() {
//float cosTheta = clamp(dot(Input.Normal, vec3(0, 1, 0)), 0.0, 1.0); //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 //float bias = 0.001 * tan(acos(cosTheta)); // cosTheta is dot( n,l ), clamped between 0 and 1
//bias = clamp(bias, 0.0, 0.01); //bias = clamp(bias, 0.0, 0.01);
float bias = 0.003;
float visibility = 1.0; float visibility = 1.0;
vec4 shadowMapValue = texture(shadowMap, Input.ShadowCoord.xy); if (Input.ShadowCoord.x >= 0.0 && Input.ShadowCoord.x <= 1.0 && Input.ShadowCoord.y >= 0.0 && Input.ShadowCoord.y <= 1.0) {
if (shadowMapValue.z < Input.ShadowCoord.z - bias) { float bias = 0.003;
visibility = 0.5; vec4 shadowMapValue = texture(shadowMap, Input.ShadowCoord.xy);
if (shadowMapValue.z < Input.ShadowCoord.z - bias) {
visibility = 0.5;
}
} }
vec3 totalLighting = La * Ka * visibility; vec3 totalLighting = La * Ka * visibility;
float attenuation; float attenuation;
for(int i = 0; i < numberOfLights; i++) for(int i = 0; i < numberOfLights && i < maxNumberOfLights; i++)
{ {
// Light // Light
//vec3 lightPosition = vec3(0, 0, 2); //vec3 lightPosition = vec3(0, 0, 2);
@@ -12,7 +12,7 @@ out vec4 FragmentColor;
float LinearizeDepth(float z) float LinearizeDepth(float z)
{ {
float n = 0.1; // camera z near 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)); return (2.0 * n) / (f + n - z * (f - n));
} }
+3 -1
View File
@@ -19,7 +19,9 @@ public:
virtual void UpdateEntity(double dt, EntityID entity, EntityID parent) { } virtual void UpdateEntity(double dt, EntityID entity, EntityID parent) { }
// Called when a component is created // 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: protected:
World* m_World; World* m_World;
+16 -6
View File
@@ -3,7 +3,8 @@
Systems::PlayerSystem::PlayerSystem( World* world ) : System(world) Systems::PlayerSystem::PlayerSystem( World* world ) : System(world)
{ {
speed = 20; m_PlayerSpeed = 20;
m_PlayerOriginalBounds = glm::vec3(0);
} }
void Systems::PlayerSystem::Update(double dt) void Systems::PlayerSystem::Update(double dt)
@@ -21,10 +22,10 @@ void Systems::PlayerSystem::UpdateEntity(double dt, EntityID entity, EntityID pa
auto name = m_World->GetProperty<std::string>(entity, "Name"); auto name = m_World->GetProperty<std::string>(entity, "Name");
if (name == "Camera") { if (name == "Camera") {
glm::vec3 Camera_Right = glm::vec3(glm::vec4(1, 0, 0, 0) * transform->Orientation); 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); 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]) { if(input->KeyState[GLFW_KEY_LEFT_SHIFT]) {
speed *= 4.0f; speed *= 4.0f;
} }
@@ -65,11 +66,14 @@ void Systems::PlayerSystem::UpdateEntity(double dt, EntityID entity, EntityID pa
} }
} }
name = m_World->GetProperty<std::string>(entity, "Name"); name = m_World->GetProperty<std::string>(entity, "Name");
if (name == "PlayerShip") 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_Right = glm::vec3(glm::vec4(1, 0, 0, 0));
glm::vec3 Ship_Forward = glm::vec3(glm::vec4(0, 0, 1, 0)); glm::vec3 Ship_Forward = glm::vec3(glm::vec4(0, 0, 1, 0));
@@ -77,7 +81,7 @@ void Systems::PlayerSystem::UpdateEntity(double dt, EntityID entity, EntityID pa
glm::vec3 Euler = glm::eulerAngles(transform->Orientation); glm::vec3 Euler = glm::eulerAngles(transform->Orientation);
if(input->KeyState[GLFW_KEY_LEFT]) { 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) if(Euler.z < 10.f)
{ {
@@ -93,7 +97,7 @@ void Systems::PlayerSystem::UpdateEntity(double dt, EntityID entity, EntityID pa
} }
} }
else if(input->KeyState[GLFW_KEY_RIGHT]) { 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) if(Euler.z > -10.f)
{ {
@@ -117,6 +121,12 @@ void Systems::PlayerSystem::UpdateEntity(double dt, EntityID entity, EntityID pa
else if(Euler.z > 0.f) else if(Euler.z > 0.f)
transform->Orientation = transform->Orientation * glm::angleAxis<float>((float)dt,glm::vec3(0,0,-1)); 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/Transform.h"
#include "Components/Input.h" #include "Components/Input.h"
#include "Components/Collision.h" #include "Components/Collision.h"
#include "Components/Bounds.h"
#include "logging.h" #include "logging.h"
@@ -23,7 +24,8 @@ namespace Systems
void UpdateEntity(double dt, EntityID entity, EntityID parent) override; void UpdateEntity(double dt, EntityID entity, EntityID parent) override;
private: private:
float speed; float m_PlayerSpeed;
glm::vec3 m_PlayerOriginalBounds;
}; };
} }
+8 -1
View File
@@ -61,12 +61,19 @@ bool World::ValidEntity(EntityID entity)
void World::RemoveEntity(EntityID entity) void World::RemoveEntity(EntityID entity)
{ {
m_EntityParents.erase(entity); m_EntityParents.erase(entity);
m_EntityComponents.erase(entity); // Remove components
for (auto pair : m_EntityComponents[entity]) { for (auto pair : m_EntityComponents[entity]) {
auto type = pair.first; auto type = pair.first;
auto component = pair.second; 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_ComponentsOfType[type].remove(component);
} }
m_EntityComponents.erase(entity);
RecycleEntityID(entity); RecycleEntityID(entity);
} }