Starting an attempt at shadow maps.

Creating this branch in order to spend more time on the 2D engine
stuff since I will probably be doing a 2D thing before I mess
around much with 3D and 3D lighting.
This commit is contained in:
elipzer 2018-10-07 21:05:25 -04:00
parent d196e42522
commit 6a04c835cf
11 changed files with 190 additions and 9 deletions

View File

@ -12,6 +12,7 @@ namespace charcoal
{ {
using namespace glm; using namespace glm;
typedef vec3 Direction;
typedef vec3 Position; typedef vec3 Position;
typedef vec3 Normal; typedef vec3 Normal;
typedef vec4 ColorRGBA; typedef vec4 ColorRGBA;
@ -51,6 +52,20 @@ namespace charcoal
Fade fade; Fade fade;
}; };
struct DirectionalLight
{
DirectionalLight(
const Direction& direction = Direction(0.0f, -0.7071067811865476f, 0.7071067811865476f),
const ColorRGB& color = ColorRGB(1.0f, 1.0f, 1.0f)
)
: direction(direction),
color(color)
{}
Direction direction;
ColorRGB color;
};
struct Material struct Material
{ {
Material( Material(

View File

@ -0,0 +1,6 @@
#version 430
void main()
{
// Pass through
}

View File

@ -0,0 +1,19 @@
#pragma once
#include "VertexFragmentShaderProgram.h"
namespace charcoal
{
namespace builtin
{
namespace litshadowed
{
// TODO: Add constants for the uniform and vertex attribute locations (for all shader programs)
class DirectionalMapShaderProgram : public VertexFragmentShaderProgram
{
public:
DirectionalMapShaderProgram() : VertexFragmentShaderProgram(SHADER_PATH "LitShadowedDirectionalMapVS.glsl", SHADER_PATH "LitShadowedDirectionalMapFS.glsl") {}
};
}
}
}

View File

@ -0,0 +1,12 @@
#version 430
layout(location = 0) in vec3 vertex_position;
layout(location = 3) in mat4 model_to_world;
layout(location = 0) uniform mat4 world_to_projection;
void main()
{
vec4 model_position = model_to_world * vec4(vertex_position, 1.0);
gl_Position = world_to_projection * model_position;
}

View File

@ -46,10 +46,21 @@ namespace charcoal
return m_lights.back(); return m_lights.back();
} }
DirectionalLight& set_directional_light(
const Direction& direction,
const ColorRGB& color
)
{
m_directional_light = DirectionalLight(direction, color);
return m_directional_light;
}
private: private:
ShaderProgram m_shader_program; ShaderProgram m_shader_program;
const Camera* m_p_camera = nullptr; const Camera* m_p_camera = nullptr;
std::vector<Light> m_lights; std::vector<Light> m_lights;
// TODO: Should this be a vector of directional lights?
DirectionalLight m_directional_light;
}; };
} }
} }

View File

@ -8,23 +8,27 @@ MyBuiltinLitShadowedScene::MyBuiltinLitShadowedScene(Application& application)
: litshadowed::Scene(application), : litshadowed::Scene(application),
m_cube( m_cube(
meshgenerator::set_material<litshadowed::Vertex, litshadowed::Index>( meshgenerator::set_material<litshadowed::Vertex, litshadowed::Index>(
meshgenerator::gen_cube_pn<litshadowed::Vertex, litshadowed::Index>(DRAW_TRIANGLES, 2.0f, 2.0f, 2.0f), meshgenerator::gen_cube_pn<litshadowed::Vertex, litshadowed::Index>(DRAW_TRIANGLES, 1.0f, 1.0f, 1.0f),
Material(1.0f, 1.0f, 0.2f, 1.0f) Material(1.0f, 1.0f, 0.2f, 1.0f)
), DrawMode::DRAW_TRIANGLES ), DrawMode::DRAW_TRIANGLES
), ),
m_plane( m_plane(
meshgenerator::set_material<litshadowed::Vertex, litshadowed::Index>( meshgenerator::set_material<litshadowed::Vertex, litshadowed::Index>(
meshgenerator::gen_plane_pn<litshadowed::Vertex, litshadowed::Index>(DRAW_TRIANGLES, 4.0f, 4.0f), meshgenerator::gen_plane_pn<litshadowed::Vertex, litshadowed::Index>(DRAW_TRIANGLES, 6.0f, 6.0f),
Material(1.0f, 1.0f, 0.2f, 1.0f) Material(1.0f, 1.0f, 0.2f, 1.0f)
), DrawMode::DRAW_TRIANGLES ), DrawMode::DRAW_TRIANGLES
), ),
m_camera((float)TAU_1_4, (float)m_screen_size.x / m_screen_size.y, 1.0f, 10.0f, vec3(0.0f, 0.0f, -5.0f)), m_camera((float)TAU_1_8, (float)m_screen_size.x / m_screen_size.y, 1.0f, 100.0f, vec3(0.0f, 0.0f, -10.0f)),
m_cube_batch(add_batch(&m_cube, 1)), m_cube_batch(add_batch(&m_cube, 1)),
m_plane_batch(add_batch(&m_plane, 1)) m_plane_batch(add_batch(&m_plane, 1))
{ {
add_prerenderable(&m_camera); add_prerenderable(&m_camera);
set_camera(&m_camera); set_camera(&m_camera);
// Use the default directional light by not setting the directional light.
m_plane_batch.get_pose(0).update_position(vec3(0.0f, -2.0f, 0.0f));
add_light( add_light(
Position(0.0f, 2.0f, -2.0f), Position(0.0f, 2.0f, -2.0f),
Light::Power(0.2f, 1.0f, 1.0f), Light::Power(0.2f, 1.0f, 1.0f),
@ -59,12 +63,12 @@ void MyBuiltinLitShadowedScene::update(float delta_time, clock_t clock)
vec3 camera_translation(0.0f, 0.0f, 0.0f); vec3 camera_translation(0.0f, 0.0f, 0.0f);
if (m_input_manager.is_key_down(GLFW_KEY_W)) camera_translation.y += 1; if (m_input_manager.is_key_down(GLFW_KEY_W)) camera_translation.z += 1;
if (m_input_manager.is_key_down(GLFW_KEY_S)) camera_translation.y -= 1; if (m_input_manager.is_key_down(GLFW_KEY_S)) camera_translation.z -= 1;
if (m_input_manager.is_key_down(GLFW_KEY_A)) camera_translation.x -= 1; if (m_input_manager.is_key_down(GLFW_KEY_A)) camera_translation.x += 1;
if (m_input_manager.is_key_down(GLFW_KEY_D)) camera_translation.x += 1; if (m_input_manager.is_key_down(GLFW_KEY_D)) camera_translation.x -= 1;
if (m_input_manager.is_key_down(GLFW_KEY_Q)) camera_translation.z -= 1; if (m_input_manager.is_key_down(GLFW_KEY_Q)) camera_translation.y -= 1;
if (m_input_manager.is_key_down(GLFW_KEY_E)) camera_translation.z += 1; if (m_input_manager.is_key_down(GLFW_KEY_E)) camera_translation.y += 1;
float camera_rotation = 0.0f; float camera_rotation = 0.0f;
if (m_input_manager.is_key_down(GLFW_KEY_Z)) camera_rotation += 1; if (m_input_manager.is_key_down(GLFW_KEY_Z)) camera_rotation += 1;

View File

@ -186,6 +186,7 @@
</ClCompile> </ClCompile>
<ClCompile Include="TexturedBatch.cpp" /> <ClCompile Include="TexturedBatch.cpp" />
<ClCompile Include="TexturedScene.cpp" /> <ClCompile Include="TexturedScene.cpp" />
<ClCompile Include="TextureFramebuffer.cpp" />
<ClCompile Include="Util.cpp" /> <ClCompile Include="Util.cpp" />
<ClCompile Include="VertexFragmentShaderProgram.cpp" /> <ClCompile Include="VertexFragmentShaderProgram.cpp" />
</ItemGroup> </ItemGroup>
@ -212,6 +213,7 @@
<ClInclude Include="LitScene.h" /> <ClInclude Include="LitScene.h" />
<ClInclude Include="LitShaderProgram.h" /> <ClInclude Include="LitShaderProgram.h" />
<ClInclude Include="LitShadowedBatch.h" /> <ClInclude Include="LitShadowedBatch.h" />
<ClInclude Include="LitShadowedDirectionalMapShaderProgram.h" />
<ClInclude Include="LitShadowedScene.h" /> <ClInclude Include="LitShadowedScene.h" />
<ClInclude Include="LitShadowedShaderProgram.h" /> <ClInclude Include="LitShadowedShaderProgram.h" />
<ClInclude Include="LitShadowedTypes.h" /> <ClInclude Include="LitShadowedTypes.h" />
@ -227,6 +229,7 @@
<ClInclude Include="TexturedBatch.h" /> <ClInclude Include="TexturedBatch.h" />
<ClInclude Include="TexturedTypes.h" /> <ClInclude Include="TexturedTypes.h" />
<ClInclude Include="TextureFactory.h" /> <ClInclude Include="TextureFactory.h" />
<ClInclude Include="TextureFramebuffer.h" />
<ClInclude Include="TextureGenerator.h" /> <ClInclude Include="TextureGenerator.h" />
<ClInclude Include="TextureRenderable.h" /> <ClInclude Include="TextureRenderable.h" />
<ClInclude Include="TexturedScene.h" /> <ClInclude Include="TexturedScene.h" />
@ -258,6 +261,8 @@
<ItemGroup> <ItemGroup>
<None Include="BasicFS.glsl" /> <None Include="BasicFS.glsl" />
<None Include="BasicVS.glsl" /> <None Include="BasicVS.glsl" />
<None Include="LitShadowedDirectionalMapFS.glsl" />
<None Include="LitShadowedDirectionalMapVS.glsl" />
<None Include="LitShadowedFS.glsl" /> <None Include="LitShadowedFS.glsl" />
<None Include="LitShadowedVS.glsl" /> <None Include="LitShadowedVS.glsl" />
<None Include="MySimpleFS.glsl" /> <None Include="MySimpleFS.glsl" />

View File

@ -207,6 +207,9 @@
<ClCompile Include="GLUtil.cpp"> <ClCompile Include="GLUtil.cpp">
<Filter>Source Files\Engine\builtin\General</Filter> <Filter>Source Files\Engine\builtin\General</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="TextureFramebuffer.cpp">
<Filter>Source Files\Engine\builtin\Scenes\LitShadowed</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="Application.h"> <ClInclude Include="Application.h">
@ -401,6 +404,12 @@
<ClInclude Include="TextureGenerator.h"> <ClInclude Include="TextureGenerator.h">
<Filter>Header Files\Engine\builtin\General</Filter> <Filter>Header Files\Engine\builtin\General</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="LitShadowedDirectionalMapShaderProgram.h">
<Filter>Header Files\Engine\builtin\Scenes\LitShadowed</Filter>
</ClInclude>
<ClInclude Include="TextureFramebuffer.h">
<Filter>Header Files\Engine\builtin\Scenes\LitShadowed</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="MySimpleVS.glsl"> <None Include="MySimpleVS.glsl">
@ -439,5 +448,11 @@
<None Include="LitShadowedVS.glsl"> <None Include="LitShadowedVS.glsl">
<Filter>Source Files\Engine\builtin\Scenes\Shaders</Filter> <Filter>Source Files\Engine\builtin\Scenes\Shaders</Filter>
</None> </None>
<None Include="LitShadowedDirectionalMapVS.glsl">
<Filter>Source Files\Engine\builtin\Scenes\Shaders</Filter>
</None>
<None Include="LitShadowedDirectionalMapFS.glsl">
<Filter>Source Files\Engine\builtin\Scenes\Shaders</Filter>
</None>
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -4,6 +4,8 @@
#include "Exception.h" #include "Exception.h"
#include "Sampler.h"
#include <vector> #include <vector>
namespace charcoal namespace charcoal
@ -185,6 +187,39 @@ namespace charcoal
glBindTexture((GLenum)texture_target, NULL); glBindTexture((GLenum)texture_target, NULL);
} }
template <typename T>
Texture(
Format data_format,
Type data_type,
unsigned long width,
unsigned long height,
const std::vector<T>& data,
InternalFormat internal_format,
Sampler::MinFilter min_filter,
Sampler::MagFilter mag_filter,
Sampler::Wrap wrap_s,
Sampler::Wrap wrap_t,
Target texture_target = Target::TEXTURE_2D
)
: width(width), height(height)
{
if (texture_target != Target::TEXTURE_2D)
throw EXCEPTION("Non 2D textures are not supported yet.");
glGenTextures(1, &m_texture);
glBindTexture((GLenum)texture_target, m_texture);
glTexImage2D((GLenum)texture_target, 0, (GLint)internal_format, width, height, 0, (GLenum)data_format, (GLenum)data_type, &data[0]);
glTexParameteri((GLenum)texture_target, GL_TEXTURE_MIN_FILTER, (GLenum)min_filter);
glTexParameteri((GLenum)texture_target, GL_TEXTURE_MAG_FILTER, (GLenum)mag_filter);
glTexParameteri((GLenum)texture_target, GL_TEXTURE_WRAP_S, (GLenum)wrap_s);
glTexParameteri((GLenum)texture_target, GL_TEXTURE_WRAP_T, (GLenum)wrap_t);
glBindTexture((GLenum)texture_target, NULL);
}
~Texture() ~Texture()
{ {
glDeleteTextures(1, &m_texture); glDeleteTextures(1, &m_texture);

View File

@ -0,0 +1,29 @@
#include "TextureFramebuffer.h"
namespace charcoal
{
namespace builtin
{
namespace litshadowed
{
TextureFramebuffer::TextureFramebuffer(unsigned int width, unsigned int height)
: m_texture(
Texture::Format::DEPTH_COMPONENT,
Texture::Type::FLOAT,
width, height,
std::vector<float>(),// Just an empty vector
Texture::InternalFormat::DEPTH_COMPONENT,
Sampler::MinFilter::LINEAR, Sampler::MagFilter::LINEAR,
Sampler::Wrap::CLAMP_TO_EDGE, Sampler::Wrap::CLAMP_TO_EDGE
)
{
glGenFramebuffers(1, &m_framebuffer);
}
TextureFramebuffer::~TextureFramebuffer()
{
glDeleteFramebuffers(1, &m_framebuffer);
}
}
}
}

View File

@ -0,0 +1,30 @@
#pragma once
#include "stdafx.h"
#include "Texture.h"
// TODO: Move this to the baseline or general builtin
namespace charcoal
{
namespace builtin
{
namespace litshadowed
{
class TextureFramebuffer
{
public:
TextureFramebuffer(unsigned int width = 1024, unsigned int height = 1024);
~TextureFramebuffer();
GLuint get_framebuffer() const { return m_framebuffer; }
GLuint get_texture() const { return m_texture.get_texture(); }
private:
GLuint m_framebuffer;
Texture m_texture;
};
}
}
}