Skip to content

Commit

Permalink
Changing shadow class to contain an array of shadowmaps since shadow …
Browse files Browse the repository at this point in the history
…algorithms like variance shadow mapping and cascade shadow mapping need multiple shadow maps. Also, the shadow maps can contain a number of framebuffers if the shadows need to apply post processing. Next, the shadow class has a virtual function for applying post processing to shadows since algorithms like variance shadow mapping need bluring to work correctly. For one thing, the texture class suffered a total refactor so repetitive code like creating or loading the texture has been modularized. On the other hand, the Framebuffer class now better fits the needs of the system. Finally, the light classes have been change to match the Shadow class.
  • Loading branch information
rxwp5657 committed Jun 5, 2020
1 parent fe85d31 commit 70221cf
Show file tree
Hide file tree
Showing 25 changed files with 668 additions and 406 deletions.
1 change: 1 addition & 0 deletions include/core/directional_light.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ namespace nitro
void Draw(const graphics::Shader& shader, bool default_framebuffer = true) override;
void Erase() override;

void PostProcess(const std::map<std::string, graphics::Shader>& shaders) override;
void DrawShadows(const std::map<std::string, graphics::Shader>& shaders,
const std::vector<std::shared_ptr<Actor>>& actors) override;

Expand Down
26 changes: 26 additions & 0 deletions include/core/gaussian_blur.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#ifndef GAUSSIAN_BLUR
#define GAUSSIAN_BLUR

#include <string>
#include "../graphics/framebuffer.hpp"
#include "../graphics/post_process.hpp"

namespace nitro
{
namespace core
{
class GaussianBlur : public graphics::PostProcess
{
public:

GaussianBlur(int width, int height);
void Process(const graphics::Shader& shader, graphics::Texture& in, graphics::Texture& out) override;

private:
graphics::Framebuffer framebuffer_;
void Subprocess(const graphics::Shader& shader, graphics::Texture& in, graphics::Texture& out, bool vertical);
};
}
}

#endif
2 changes: 2 additions & 0 deletions include/core/point_light.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ namespace nitro
void Draw(const graphics::Shader& shader, bool default_framebuffer = true) override;
void Erase() override;

void PostProcess(const std::map<std::string, graphics::Shader>& shaders) override;
void DrawShadows(const std::map<std::string, graphics::Shader>& shaders,
const std::vector<std::shared_ptr<Actor>>& actors) override;

void SetupShadows() override;

private:
Expand Down
4 changes: 3 additions & 1 deletion include/core/shadows.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "../graphics/shader.hpp"
#include "../graphics/texture.hpp"
#include "../graphics/framebuffer.hpp"
#include "./actor.hpp"

namespace nitro
Expand All @@ -25,14 +26,15 @@ namespace nitro
graphics::Texture ShadowTexture(int index) const;

virtual void SetupShadows() = 0;
virtual void PostProcess(const std::map<std::string, graphics::Shader>& shaders) = 0;
virtual void DrawShadows(const std::map<std::string, graphics::Shader>& shaders,
const std::vector<std::shared_ptr<Actor>>& actors) = 0;

protected:
bool cast_shadows_;
bool set_up_;
int pcf_;
unsigned int framebuffer_;
std::vector<graphics::Framebuffer> framebuffers_;
std::vector<graphics::Texture> shadow_maps_;
};
}
Expand Down
4 changes: 3 additions & 1 deletion include/core/spot_light.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "./constants.hpp"
#include "./shadows.hpp"
#include "./actor.hpp"
#include "./gaussian_blur.hpp"

namespace nitro
{
Expand Down Expand Up @@ -45,8 +46,9 @@ namespace nitro
void Draw(const graphics::Shader& shader, bool default_framebuffer = true) override;
void Erase() override;

void PostProcess(const std::map<std::string, graphics::Shader>& shaders) override;
void DrawShadows(const std::map<std::string, graphics::Shader>& shaders,
const std::vector<std::shared_ptr<Actor>>& actors) override;
const std::vector<std::shared_ptr<Actor>>& actors) override;
void SetupShadows() override;

private:
Expand Down
37 changes: 16 additions & 21 deletions include/graphics/framebuffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,24 @@ namespace nitro
class Framebuffer
{
public:

Framebuffer();
Framebuffer(unsigned int viewport_width,
unsigned int viewport_height,
bool color = false,
bool depht = false,
bool stencil = false);

void AttachTexture(const GLenum type, const GLuint texture, bool is_cubemap = false) const;
void AttachRenderBuffer(const GLenum type, const GLuint) const;
void WindowViewPort(const bool is_window) const;
void Bind() const;
void Unbind() const;
GLuint GenRenderBuffer(GLenum format);
Framebuffer(int width, int height);

void AttachTexture(const Texture& texture, GLenum attachment_t);
//void AttachRenderBuffer(const RenderBuffer& renderBuffer);

private:
unsigned int viewport_width_;
unsigned int viewport_height_;
bool is_window_;
GLuint framebuffer_;
GLuint depht_render_buffer_;
GLuint color_render_buffer_;
GLuint stencil_render_buffer_;
bool Complete();

void Bind();
void Unbind();

void Size(int width, int height);
void DeleteColorBuffers();

private:
unsigned int framebuffer_;
int width_;
int height_;
};
}
}
Expand Down
19 changes: 19 additions & 0 deletions include/graphics/post_process.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef POST_PROCESS
#define POST_PROCESS

#include "./shader.hpp"
#include "./texture.hpp"

namespace nitro
{
namespace graphics
{
struct PostProcess
{
virtual ~PostProcess() = default;
virtual void Process(const Shader& shader, Texture& in, Texture& out) = 0;
};
}
}

#endif
2 changes: 2 additions & 0 deletions include/graphics/shader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ namespace nitro
void SetUniformBlock(const std::string& name, int index) const;

utils::StatusInfo Status() const;

void Use() const;
void Disable() const;

private:
GLint program_;
Expand Down
67 changes: 49 additions & 18 deletions include/graphics/texture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ namespace nitro
{
namespace graphics
{

struct TextureFile
{
int width;
int height;
unsigned int format;
unsigned char *data;
};

class Texture : public Drawable
{
public:
Expand All @@ -33,42 +42,64 @@ namespace nitro
Texture(const std::string& name,
int width,
int height,
GLenum target,
GLenum internal_format,
GLenum format,
GLenum type);
unsigned int target,
unsigned int internal_format,
unsigned int format,
unsigned int data_type,
unsigned int filter);

~Texture();

Texture& operator = (const Texture& texture);
Texture& operator = (Texture&& texture) noexcept;

std::string get_path() const;
std::string Name() const;

GLuint TextureReference() const;
int Width() const;
int Height() const;
int Unit() const;
std::string Path() const;
std::string Name() const;
unsigned int TextureReference() const;
unsigned int Type() const;
unsigned int InternalFormat() const;
unsigned int Format() const;
unsigned int DataType() const;

void TextureUnit(GLint texture_unit, int uniform_location, int texture_number);

void PostProcessing(const Shader& shader) const;
void TextureUnit(GLint texture_unit, int uniform_location, const std::string& name);

void DrawQuad() const;

void Erase() override;
void Draw(const Shader& shader, bool default_framebuffer = true) override;

private:
GLuint texture_;
GLint texture_unit_;
GLuint type_;
unsigned int texture_;
unsigned int type_;
unsigned int internal_format_;
unsigned int format_;
unsigned int data_type_;

unsigned int texture_unit_;
int uniform_location_;
int widht_;
int height_;
bool has_num_;

std::string name_;
std::string path_;
int uniform_location_;
bool has_num_;
GLuint quad_vba_;
GLuint quad_vbo_;

unsigned int quad_vba_;
unsigned int quad_vbo_;
std::vector<float> quad_vertices_;

void Setup(const Shader& shader) override;
void InitQuad();

TextureFile LoadTextureFile(const std::string& path);

void Create2DTexture(int width, int height, unsigned int internal_format, unsigned int format, unsigned int data_type, unsigned char *data, unsigned int filter, bool mipmap);
void CreateCubeMap(int width, int height, unsigned int internal_format, unsigned int format, unsigned int data_type);
void CreateCubeMap(const std::vector<TextureFile>& file_data, unsigned int internal_format, unsigned int data_type ,unsigned int filter);
};
}
}
Expand Down
3 changes: 2 additions & 1 deletion resources/shaders/debug.frag
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ uniform sampler2D shadow_map;
void main()
{
float depthValue = texture(shadow_map, TexCoords).r;
FragColor = vec4(vec3(depthValue), 1.0);
//FragColor = vec4(vec3(depthValue), 1.0);
FragColor = texture(shadow_map, TexCoords);
}
5 changes: 3 additions & 2 deletions resources/shaders/debug.vert
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#version 410 core
in vec3 aPosition;
in vec2 aTexCoord;

layout (location = 0) in vec3 aPosition;
layout (location = 1) in vec2 aTexCoord;

out vec2 TexCoords;

Expand Down
27 changes: 26 additions & 1 deletion resources/shaders/gaussian.frag
Original file line number Diff line number Diff line change
@@ -1 +1,26 @@
#version 410 core
#version 330 core
out vec4 FragColor;

in vec2 TexCoords;

uniform sampler2D image;
uniform bool vertical;


void main()
{
float kernel[5] = float[](0.2270270270, 0.1945945946, 0.1216216216, 0.0540540541, 0.0162162162);

//texel size
vec2 texel_size = 1.0 / textureSize(image, 0);
vec3 result = texture(image, TexCoords).rgb * kernel[0];
vec2 offset = vertical ? vec2(0, 1) : vec2(1, 0);

for(int i = 1; i < 5; i++)
{
result += texture(image, TexCoords + offset * i * texel_size).rgb * kernel[i];
result += texture(image, TexCoords - offset * i * texel_size).rgb * kernel[i];
}

FragColor = vec4(result, 1.0);
}
11 changes: 11 additions & 0 deletions resources/shaders/gaussian.vert
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
#version 410 core

layout (location = 0) in vec3 aPosition;
layout (location = 1) in vec2 aTexCoord;

out vec2 TexCoords;

void main()
{
TexCoords = aTexCoord;
gl_Position = vec4(aPosition, 1.0);
}
35 changes: 12 additions & 23 deletions resources/shaders/spot_light.frag
Original file line number Diff line number Diff line change
Expand Up @@ -38,30 +38,19 @@ uniform sampler2D shadow_map;

float shadow(vec4 frag_pos, float bias)
{
// Perspective Divide
vec3 coord = frag_pos.xyz / frag_pos.w;
// Mapt to range [0, 1]
// Map to range [0, 1]
coord = coord * 0.5 + 0.5;

// Get texel size by getting the reciprocal of the texture size.
vec2 shadow_offset = 1.0 / textureSize(shadow_map,0);

float light_depth = texture(shadow_map, coord.xy).r;
float current_depht = coord.z;

int offset = uPCF / 2;
float shadow = 0.0;

for(int x = -offset; x <= offset; x++)
{
for(int y = -offset; y <= offset; y++)
{
float neighbor = texture(shadow_map, coord.xy + shadow_offset * vec2(x,y)).r;
shadow += current_depht - bias > neighbor ? 1.0 : 0.0;
}
}

return shadow / (uPCF * uPCF);

vec2 moments = texture(shadow_map, coord.xy).xy;
float m1 = moments.x;
float m2 = moments.y;
float variance = m2 - m1 * m1;
float diff = coord.z - m1;
float p_max = variance / (variance + diff * diff);

return coord.z < moments.x ? 1.0 : p_max ;
}

// Distance attenuation/falloff
Expand Down Expand Up @@ -94,9 +83,9 @@ vec4 blinn(vec3 FragPos, vec3 Normal, vec3 View, vec3 L, vec3 light_color)
vec3 diffuse = surface_color * light_color * max(0.0, dot(N,L));
vec3 specular = surface_spec * light_color * pow(max(0.0, dot(R, V)), 50) * 0.9;

float shadow_val = (uCastsShadow) ? shadow(fs_in.FragPosL, 0.005) : 0.0;
float shadow_val = (uCastsShadow) ? shadow(fs_in.FragPosL, 0.005) : 1.0;

return vec4(ambient + (1.0 - shadow_val) * (diffuse + specular), 1.0);
return vec4(ambient + (shadow_val) * (diffuse + specular), 1.0);
}

void main()
Expand Down
Loading

0 comments on commit 70221cf

Please sign in to comment.