Skip to content

Commit

Permalink
Camera movement + implemented deferred rendering and SSAO
Browse files Browse the repository at this point in the history
  • Loading branch information
RiscadoA committed Feb 27, 2021
1 parent 683a932 commit e3df71f
Show file tree
Hide file tree
Showing 14 changed files with 532 additions and 37 deletions.
4 changes: 3 additions & 1 deletion data/assets.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ language.portuguese = static text text/portuguese.csv

font.inconsolata = static font font/Inconsolata.otf

shader.lighting = static shader shader/screen_quad.vert shader/lighting.frag
shader.gbuffer = static shader shader/screen_quad.vert shader/gbuffer.frag
shader.ssao = static shader shader/screen_quad.vert shader/ssao.frag
shader.ssao_blur = static shader shader/screen_quad.vert shader/ssao_blur.frag
shader.model = static shader shader/model.vert shader/model.frag

model.chr_knight = dynamic model model/chr_knight.qb
Expand Down
42 changes: 42 additions & 0 deletions data/shader/gbuffer.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#version 330 core

#define PI 3.1415926535897932384626433832795

in vec2 frag_uv;

out vec4 frag_color;

uniform sampler2D albedo_tex;
uniform sampler2D position_tex;
uniform sampler2D normal_tex;
uniform sampler2D ssao_tex;

uniform vec3 sky_color;
uniform float z_far;

uniform mat4 view;

const vec3 world_light_dir = normalize(vec3(-0.7, 1.5, 0.5));

void main() {
vec3 albedo = texture(albedo_tex, frag_uv).rgb;
vec3 position = texture(position_tex, frag_uv).xyz;
vec3 normal = texture(normal_tex, frag_uv).xyz;
float ambient_occlusion = texture(ssao_tex, frag_uv).r;

vec3 lighting = albedo * ambient_occlusion * 0.3f;
vec3 light_dir = normalize(mat3(view) * world_light_dir);

vec3 diffuse = max(dot(normal, light_dir), 0.0f) * albedo * ambient_occlusion;
lighting += diffuse;

if (normal.x == 0.0f && normal.y == 0.0f && normal.z == 0.0f) {
// Sky
lighting = albedo;
}

// Fog
float depth = min(1.0f, length(position) / z_far);
float fog = depth * depth;
frag_color = vec4(mix(lighting, sky_color, fog), 1.0);
}
9 changes: 0 additions & 9 deletions data/shader/lighting.frag

This file was deleted.

10 changes: 8 additions & 2 deletions data/shader/model.frag
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,17 @@ layout (std140) uniform Palette {
Material palette[255];
};

in vec3 frag_position;
in vec3 frag_normal;
flat in uint frag_material;

out vec4 frag_color;
layout (location = 0) out vec3 albedo;
layout (location = 1) out vec3 position;
layout (location = 2) out vec3 normal;

void main() {
Material mat = palette[frag_material];
frag_color = vec4(mat.color, 1.0);
albedo = mat.color;
position = frag_position;
normal = normalize(frag_normal);
}
8 changes: 7 additions & 1 deletion data/shader/model.vert
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@ layout (location = 0) in vec3 vert_position;
layout (location = 1) in vec3 vert_normal;
layout (location = 2) in uint vert_material;

out vec3 frag_position;
out vec3 frag_normal;
flat out uint frag_material;

uniform mat4 model;
uniform mat4 view;
uniform mat4 proj;

void main() {
vec4 view_position = view * model * vec4(vert_position, 1.0f);
frag_position = view_position.xyz;
gl_Position = proj * view_position;
mat3 normal_matrix = transpose(inverse(mat3(view * model)));
frag_normal = normal_matrix * vert_normal;
frag_material = vert_material;
gl_Position = proj * view * model * vec4(vert_position, 1.0);
}
4 changes: 2 additions & 2 deletions data/shader/screen_quad.vert
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ const vec2[6] uvs = vec2[6](
vec2(0.0, 0.0)
);

out vec2 frag_uvs;
out vec2 frag_uv;

void main() {
frag_uvs = uvs[gl_VertexID];
frag_uv = uvs[gl_VertexID];
gl_Position = vec4(positions[gl_VertexID], 0.0, 1.0);
}
48 changes: 48 additions & 0 deletions data/shader/ssao.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#version 330 core

in vec2 frag_uv;

out float frag_color;

const int NUM_SAMPLES = 64;

uniform sampler2D position_tex;
uniform sampler2D normal_tex;
uniform sampler2D noise_tex;

uniform mat4 projection;
uniform vec2 noise_scale;
uniform vec3 samples[NUM_SAMPLES];

const float radius = 0.5;
const float bias = 0.025;
const float magnitude = 1.1;
const float constrast = 1.1;

void main() {
vec3 frag_pos = texture(position_tex, frag_uv).xyz;
vec3 normal = normalize(texture(normal_tex, frag_uv).xyz);
vec3 random_vec = normalize(texture(noise_tex, frag_uv * noise_scale).xyz);

vec3 tangent = normalize(random_vec - normal * dot(random_vec, normal));
vec3 bitangent = cross(normal, tangent);
mat3 tbn = mat3(tangent, bitangent, normal);

float occlusion = 0.0;
for (int i = 0; i < NUM_SAMPLES; ++i) {
vec3 sample_pos = tbn * samples[i];
sample_pos = frag_pos + sample_pos * radius;

vec4 offset = projection * vec4(sample_pos, 1.0);
offset.xy /= offset.w;
offset.xy = offset.xy * 0.5 + 0.5;

float sample_depth = texture(position_tex, offset.xy).z;
float range_check = smoothstep(0.0, 1.0, radius / abs(frag_pos.z - sample_depth));
occlusion += (sample_depth >= sample_pos.z + bias ? 1.0 : 0.0) * range_check;
}

occlusion = 1.0 - occlusion / NUM_SAMPLES;
occlusion = pow(occlusion, magnitude);
frag_color = constrast * (occlusion - 0.5) + 0.5;
}
19 changes: 19 additions & 0 deletions data/shader/ssao_blur.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#version 330 core

in vec2 frag_uv;

out float frag_color;

uniform sampler2D ssao_tex;

void main() {
vec2 texel_size = 1.0 / vec2(textureSize(ssao_tex, 0));
float result = 0.0;
for (int x = -2; x < 2; ++x) {
for (int y = -2; y < 2; ++y) {
vec2 offset = vec2(float(x), float(y)) * texel_size;
result += texture(ssao_tex, frag_uv + offset).r;
}
}
frag_color = result / (4.0 * 4.0);
}
10 changes: 10 additions & 0 deletions src/vpg/ecs/transform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ vpg::ecs::Transform::Transform(Entity parent) {
this->dirty = true;
}

void vpg::ecs::Transform::translate(const glm::vec3& translation) {
this->position += translation;
this->dirty = true;
}

void Transform::set_position(const glm::vec3& position) {
this->position = position;
this->dirty = true;
Expand Down Expand Up @@ -55,5 +60,10 @@ void Transform::update() {
this->local = glm::scale(this->local, this->scale);
this->local = glm::toMat4(this->get_rotation()) * this->local;
this->local = glm::translate(this->local, this->position);

this->forward = this->rotation * glm::vec3(0.0f, 0.0f, -1.0f);
this->up = this->rotation * glm::vec3(0.0f, 1.0f, 0.0f);
this->right = this->rotation * glm::vec3(1.0f, 0.0f, 0.0f);

this->dirty = false;
}
7 changes: 7 additions & 0 deletions src/vpg/ecs/transform.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,19 @@ namespace vpg::ecs {
public:
Transform(Entity parent = NullEntity);

void translate(const glm::vec3& translation);

void set_position(const glm::vec3& position);
void set_rotation(const glm::quat& rotation);
void set_scale(const glm::vec3& scale);

inline const glm::vec3& get_position() const { return this->position; }
inline const glm::quat& get_rotation() const { return this->rotation; }
inline const glm::vec3& get_scale() const { return this->scale; }

inline const glm::vec3& get_forward() const { return this->forward; }
inline const glm::vec3& get_right() const { return this->right; }
inline const glm::vec3& get_up() const { return this->up; }

glm::mat4 get_global();
const glm::mat4& get_local();
Expand All @@ -26,6 +32,7 @@ namespace vpg::ecs {
private:
Entity parent;
glm::vec3 position, scale;
glm::vec3 forward, right, up;
glm::quat rotation;
glm::mat4 local;
bool dirty;
Expand Down
Loading

0 comments on commit e3df71f

Please sign in to comment.