From 6a04c835cfff92cee578b6e4970176e1312f3051 Mon Sep 17 00:00:00 2001 From: elipzer Date: Sun, 7 Oct 2018 21:05:25 -0400 Subject: [PATCH] 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. --- OpenGLEngine/BuiltinTypes.h | 15 ++++++++ OpenGLEngine/LitShadowedDirectionalMapFS.glsl | 6 ++++ .../LitShadowedDirectionalMapShaderProgram.h | 19 ++++++++++ OpenGLEngine/LitShadowedDirectionalMapVS.glsl | 12 +++++++ OpenGLEngine/LitShadowedScene.h | 11 ++++++ OpenGLEngine/MyBuiltinLitShadowedScene.cpp | 22 +++++++----- OpenGLEngine/OpenGLEngine.vcxproj | 5 +++ OpenGLEngine/OpenGLEngine.vcxproj.filters | 15 ++++++++ OpenGLEngine/Texture.h | 35 +++++++++++++++++++ OpenGLEngine/TextureFramebuffer.cpp | 29 +++++++++++++++ OpenGLEngine/TextureFramebuffer.h | 30 ++++++++++++++++ 11 files changed, 190 insertions(+), 9 deletions(-) create mode 100644 OpenGLEngine/LitShadowedDirectionalMapFS.glsl create mode 100644 OpenGLEngine/LitShadowedDirectionalMapShaderProgram.h create mode 100644 OpenGLEngine/LitShadowedDirectionalMapVS.glsl create mode 100644 OpenGLEngine/TextureFramebuffer.cpp create mode 100644 OpenGLEngine/TextureFramebuffer.h diff --git a/OpenGLEngine/BuiltinTypes.h b/OpenGLEngine/BuiltinTypes.h index 76d65e3..1b09004 100644 --- a/OpenGLEngine/BuiltinTypes.h +++ b/OpenGLEngine/BuiltinTypes.h @@ -12,6 +12,7 @@ namespace charcoal { using namespace glm; + typedef vec3 Direction; typedef vec3 Position; typedef vec3 Normal; typedef vec4 ColorRGBA; @@ -51,6 +52,20 @@ namespace charcoal 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 { Material( diff --git a/OpenGLEngine/LitShadowedDirectionalMapFS.glsl b/OpenGLEngine/LitShadowedDirectionalMapFS.glsl new file mode 100644 index 0000000..ec6cea6 --- /dev/null +++ b/OpenGLEngine/LitShadowedDirectionalMapFS.glsl @@ -0,0 +1,6 @@ +#version 430 + +void main() +{ + // Pass through +} \ No newline at end of file diff --git a/OpenGLEngine/LitShadowedDirectionalMapShaderProgram.h b/OpenGLEngine/LitShadowedDirectionalMapShaderProgram.h new file mode 100644 index 0000000..3004e9e --- /dev/null +++ b/OpenGLEngine/LitShadowedDirectionalMapShaderProgram.h @@ -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") {} + }; + } + } +} diff --git a/OpenGLEngine/LitShadowedDirectionalMapVS.glsl b/OpenGLEngine/LitShadowedDirectionalMapVS.glsl new file mode 100644 index 0000000..5dbe837 --- /dev/null +++ b/OpenGLEngine/LitShadowedDirectionalMapVS.glsl @@ -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; +} \ No newline at end of file diff --git a/OpenGLEngine/LitShadowedScene.h b/OpenGLEngine/LitShadowedScene.h index aad3a6f..032dbdf 100644 --- a/OpenGLEngine/LitShadowedScene.h +++ b/OpenGLEngine/LitShadowedScene.h @@ -46,10 +46,21 @@ namespace charcoal 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: ShaderProgram m_shader_program; const Camera* m_p_camera = nullptr; std::vector m_lights; + // TODO: Should this be a vector of directional lights? + DirectionalLight m_directional_light; }; } } diff --git a/OpenGLEngine/MyBuiltinLitShadowedScene.cpp b/OpenGLEngine/MyBuiltinLitShadowedScene.cpp index d5ea760..b04b162 100644 --- a/OpenGLEngine/MyBuiltinLitShadowedScene.cpp +++ b/OpenGLEngine/MyBuiltinLitShadowedScene.cpp @@ -8,23 +8,27 @@ MyBuiltinLitShadowedScene::MyBuiltinLitShadowedScene(Application& application) : litshadowed::Scene(application), m_cube( meshgenerator::set_material( - meshgenerator::gen_cube_pn(DRAW_TRIANGLES, 2.0f, 2.0f, 2.0f), + meshgenerator::gen_cube_pn(DRAW_TRIANGLES, 1.0f, 1.0f, 1.0f), Material(1.0f, 1.0f, 0.2f, 1.0f) ), DrawMode::DRAW_TRIANGLES ), m_plane( meshgenerator::set_material( - meshgenerator::gen_plane_pn(DRAW_TRIANGLES, 4.0f, 4.0f), + meshgenerator::gen_plane_pn(DRAW_TRIANGLES, 6.0f, 6.0f), Material(1.0f, 1.0f, 0.2f, 1.0f) ), 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_plane_batch(add_batch(&m_plane, 1)) { add_prerenderable(&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( Position(0.0f, 2.0f, -2.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); - if (m_input_manager.is_key_down(GLFW_KEY_W)) camera_translation.y += 1; - if (m_input_manager.is_key_down(GLFW_KEY_S)) camera_translation.y -= 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_Q)) camera_translation.z -= 1; - if (m_input_manager.is_key_down(GLFW_KEY_E)) camera_translation.z += 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.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_D)) camera_translation.x -= 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.y += 1; float camera_rotation = 0.0f; if (m_input_manager.is_key_down(GLFW_KEY_Z)) camera_rotation += 1; diff --git a/OpenGLEngine/OpenGLEngine.vcxproj b/OpenGLEngine/OpenGLEngine.vcxproj index cc6cedc..3b8395d 100644 --- a/OpenGLEngine/OpenGLEngine.vcxproj +++ b/OpenGLEngine/OpenGLEngine.vcxproj @@ -186,6 +186,7 @@ + @@ -212,6 +213,7 @@ + @@ -227,6 +229,7 @@ + @@ -258,6 +261,8 @@ + + diff --git a/OpenGLEngine/OpenGLEngine.vcxproj.filters b/OpenGLEngine/OpenGLEngine.vcxproj.filters index 7e969a6..667c529 100644 --- a/OpenGLEngine/OpenGLEngine.vcxproj.filters +++ b/OpenGLEngine/OpenGLEngine.vcxproj.filters @@ -207,6 +207,9 @@ Source Files\Engine\builtin\General + + Source Files\Engine\builtin\Scenes\LitShadowed + @@ -401,6 +404,12 @@ Header Files\Engine\builtin\General + + Header Files\Engine\builtin\Scenes\LitShadowed + + + Header Files\Engine\builtin\Scenes\LitShadowed + @@ -439,5 +448,11 @@ Source Files\Engine\builtin\Scenes\Shaders + + Source Files\Engine\builtin\Scenes\Shaders + + + Source Files\Engine\builtin\Scenes\Shaders + \ No newline at end of file diff --git a/OpenGLEngine/Texture.h b/OpenGLEngine/Texture.h index b8416cd..b302c4b 100644 --- a/OpenGLEngine/Texture.h +++ b/OpenGLEngine/Texture.h @@ -4,6 +4,8 @@ #include "Exception.h" +#include "Sampler.h" + #include namespace charcoal @@ -185,6 +187,39 @@ namespace charcoal glBindTexture((GLenum)texture_target, NULL); } + template + Texture( + Format data_format, + Type data_type, + unsigned long width, + unsigned long height, + const std::vector& 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() { glDeleteTextures(1, &m_texture); diff --git a/OpenGLEngine/TextureFramebuffer.cpp b/OpenGLEngine/TextureFramebuffer.cpp new file mode 100644 index 0000000..a220806 --- /dev/null +++ b/OpenGLEngine/TextureFramebuffer.cpp @@ -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(),// 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); + } + } + } +} \ No newline at end of file diff --git a/OpenGLEngine/TextureFramebuffer.h b/OpenGLEngine/TextureFramebuffer.h new file mode 100644 index 0000000..390323f --- /dev/null +++ b/OpenGLEngine/TextureFramebuffer.h @@ -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; + }; + } + } +} +