Skip to content

Commit

Permalink
Merge branch 'master' into collisions
Browse files Browse the repository at this point in the history
  • Loading branch information
MStachowicz committed Dec 22, 2023
2 parents 084fbfd + b092f64 commit 673639b
Show file tree
Hide file tree
Showing 11 changed files with 329 additions and 29 deletions.
12 changes: 12 additions & 0 deletions source/Component/Texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,19 @@ namespace Component
: m_diffuse{}
, m_specular{}
, m_shininess{32.f}
, m_colour{glm::vec4(1.f)}
{}
Texture::Texture(const TextureRef& m_diffuse) noexcept
: m_diffuse{m_diffuse}
, m_specular{}
, m_shininess{32.f}
, m_colour{glm::vec4(1.f)}
{}
Texture::Texture(const glm::vec4& p_colour) noexcept
: m_diffuse{}
, m_specular{}
, m_shininess{32.f}
, m_colour{p_colour}
{}

void Texture::draw_UI(System::TextureSystem& p_texture_system)
Expand All @@ -48,6 +56,10 @@ namespace Component
m_specular = p_texture_system.getTexture(availableTextures[selected]);
ImGui::Slider("Shininess", m_shininess, 1.f, 512.f, "%.1f");

ImGui::ColorEdit4("Colour", &m_colour[0]);
ImGui::SameLine();
ImGui::Text("Used if no textures are specified.");

ImGui::TreePop();
}
}
Expand Down
5 changes: 5 additions & 0 deletions source/Component/Texture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "Utility/ResourceManager.hpp"
#include "Utility/File.hpp"

#include <glm/vec4.hpp>
#include <filesystem>

namespace Data
Expand Down Expand Up @@ -41,8 +42,12 @@ namespace Component
TextureRef m_specular;
float m_shininess;

glm::vec4 m_colour;

Texture() noexcept;
Texture(const TextureRef& m_diffuse) noexcept;
Texture(const glm::vec4& p_colour) noexcept;

void draw_UI(System::TextureSystem& p_texture_system);
};
}; // namespace Component
File renamed without changes.
File renamed without changes.
186 changes: 186 additions & 0 deletions source/OpenGL/GLSL/phong-uColour.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
#version 430 core

uniform vec3 view_position;
uniform float shininess;
uniform sampler2D diffuse;
uniform sampler2D specular;

uniform sampler2D shadow_map;
uniform float PCF_bias;
uniform vec4 uColour;

struct DirectionalLight
{
vec3 direction;

vec3 ambient;
vec3 diffuse;
vec3 specular;
};
buffer DirectionalLightsBuffer
{
uint number_of_directional_lights;
DirectionalLight directional_lights[];
};
vec4 directional_light_contribution(DirectionalLight p_light, vec3 p_frag_normal, vec3 p_view_direction);


struct PointLight
{
vec3 position;

float constant;
float linear;
float quadratic;

vec3 ambient;
vec3 diffuse;
vec3 specular;
};
buffer PointLightsBuffer
{
uint number_of_point_lights;
PointLight point_lights[];
};
vec4 point_light_contribution(PointLight p_light, vec3 p_frag_normal, vec3 p_frag_pos, vec3 p_view_direction);


struct SpotLight
{
vec3 position;

vec3 direction;
float cutoff;
float outer_cutoff;

float constant;
float linear;
float quadratic;

vec3 ambient;
vec3 diffuse;
vec3 specular;
};
buffer SpotLightsBuffer
{
uint number_of_spot_lights;
SpotLight spot_lights[];
};
vec4 spot_light_contribution(SpotLight p_light, vec3 p_frag_normal, vec3 p_frag_pos, vec3 p_view_direction);


float shadow_calculation(vec4 frag_position_light_space);

in VS_OUT {
vec3 position;
vec3 normal;
vec4 position_light_space;
} fs_in;
out vec4 Colour;

void main()
{
Colour = vec4(0.0, 0.0, 0.0, 1.0);

vec3 frag_normal = normalize(fs_in.normal);
vec3 view_direction = normalize(view_position - fs_in.position);

for (uint i = 0; i < number_of_point_lights; i++)
{
Colour += point_light_contribution(point_lights[i], frag_normal, fs_in.position, view_direction);
}
for (uint i = 0; i < number_of_spot_lights; i++)
{
Colour += spot_light_contribution(spot_lights[i], frag_normal, fs_in.position, view_direction);
}
for (uint i = 0; i < number_of_directional_lights; i++)
{
Colour += directional_light_contribution(directional_lights[i], frag_normal, view_direction);
}
}

vec4 directional_light_contribution(DirectionalLight p_light, vec3 p_frag_normal, vec3 p_view_direction)
{
vec3 light_direction = normalize(-p_light.direction);

// diffuse shading
float diff = max(dot(p_frag_normal, light_direction), 0.0);

// specular shading
vec3 reflect_direction = reflect(-light_direction, p_frag_normal);
float spec = pow(max(dot(p_view_direction, reflect_direction), 0.0), shininess);

// combine results
vec3 ambient = p_light.ambient * vec3(uColour.rgb);
vec3 diffuse = p_light.diffuse * diff * vec3(uColour.rgb);
vec3 specular = p_light.specular * spec * vec3(uColour.rgb);

float shadow = shadow_calculation(fs_in.position_light_space);
//shadow = 0.0;
return vec4((ambient + (shadow * (diffuse + specular))).xyz, 1.0);
//return vec4((ambient + diffuse + specular).xyz, 1.0);
}
vec4 point_light_contribution(PointLight p_light, vec3 p_frag_normal, vec3 p_frag_pos, vec3 p_view_direction)
{
vec3 light_direction = normalize(p_light.position - p_frag_pos);

// diffuse shading
float diff = max(dot(p_frag_normal, light_direction), 0.0);

// specular shading
vec3 reflect_direction = reflect(-light_direction, p_frag_normal);
float spec = pow(max(dot(p_view_direction, reflect_direction), 0.0), shininess);

// attenuation
float distance = length(p_light.position - p_frag_pos);
float attenuation = 1.0 / (p_light.constant + p_light.linear * distance + p_light.quadratic * (distance * distance));

// combine results
vec3 ambient = p_light.ambient * vec3(uColour.rgb) * attenuation;
vec3 diffuse = p_light.diffuse * diff * vec3(uColour.rgb) * attenuation;
vec3 specular = p_light.specular * spec * vec3(uColour.rgb) * attenuation;

return vec4((ambient + diffuse + specular).xyz, 1.0);
}
vec4 spot_light_contribution(SpotLight p_light, vec3 p_frag_normal, vec3 p_frag_pos, vec3 p_view_direction)
{
vec3 light_direction = normalize(p_light.position - p_frag_pos);

// diffuse shading
float diff = max(dot(p_frag_normal, light_direction), 0.0);

// specular shading
vec3 reflect_direction = reflect(-light_direction, p_frag_normal);
float spec = pow(max(dot(p_view_direction, reflect_direction), 0.0), shininess);

// attenuation
float distance = length(p_light.position - p_frag_pos);
float attenuation = 1.0 / (p_light.constant + p_light.linear * distance + p_light.quadratic * (distance * distance));

// spotlight intensity
float theta = dot(light_direction, normalize(-p_light.direction));
float epsilon = p_light.cutoff - p_light.outer_cutoff;
float intensity = clamp((theta - p_light.outer_cutoff) / epsilon, 0.0, 1.0);

// combine results
vec3 ambient = p_light.ambient * vec3(uColour.rgb) * attenuation * intensity;
vec3 diffuse = p_light.diffuse * diff * vec3(uColour.rgb) * attenuation * intensity;
vec3 specular = p_light.specular * spec * vec3(uColour.rgb) * attenuation * intensity;

return vec4((ambient + diffuse + specular).xyz, 1.0);
}

float shadow_calculation(vec4 frag_position_light_space)
{
// If the depth of the frag_position_light_space is larger than the closest depth from the light perspective.
// The fragment being rendered is ocluded.

// perform perspective divide + transfrom to [0,1] texture space
vec3 projected_coords = ((frag_position_light_space.xyz / frag_position_light_space.w) * 0.5) + 0.5;
// get depth of current fragment from light's perspective
float frag_depth_light_space = projected_coords.z;
// get closest depth value from light's perspective (using [0,1] range fragPosLight as coords)
float closest_depth = texture(shadow_map, projected_coords.xy).r;
// check whether current frag pos is in shadow
return frag_depth_light_space + PCF_bias > closest_depth ? 0.5 : 1.0;
}
27 changes: 27 additions & 0 deletions source/OpenGL/GLSL/phong-uColour.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#version 430 core

layout (location = 0) in vec3 VertexPosition;
layout (location = 1) in vec3 VertexNormal;

uniform mat4 model;
uniform mat4 light_proj_view;

layout(shared) uniform ViewProperties
{
mat4 view;
mat4 projection;
} viewProperties;

out VS_OUT {
vec3 position;
vec3 normal;
vec4 position_light_space;
} vs_out;

void main()
{
vs_out.position = vec3(model * vec4(VertexPosition, 1.0));
vs_out.normal = mat3(transpose(inverse(model))) * VertexNormal;
vs_out.position_light_space = light_proj_view * vec4(vs_out.position, 1.0);
gl_Position = viewProperties.projection * viewProperties.view * model * vec4(VertexPosition, 1.0);
}
42 changes: 29 additions & 13 deletions source/OpenGL/OpenGLRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,20 +96,36 @@ namespace OpenGL

scene.foreach([&](ECS::Entity& p_entity, Component::Transform& p_transform, Component::Mesh& mesh_comp)
{
if (scene.has_components<Component::Texture>(p_entity))
if (mesh_comp.m_mesh)
{
auto& texComponent = scene.get_component<Component::Texture>(p_entity);
if (scene.has_components<Component::Texture>(p_entity))
{
auto& texComponent = scene.get_component<Component::Texture>(p_entity);

if (texComponent.m_diffuse.has_value())
{
DrawCall dc;
dc.set_uniform("view_position", m_view_information.m_view_position);
dc.set_uniform("model", p_transform.m_model);
dc.set_uniform("shininess", texComponent.m_shininess);
dc.set_texture("diffuse", texComponent.m_diffuse);
dc.set_texture("specular", texComponent.m_specular.has_value() ? texComponent.m_specular : m_blank_texture);
dc.submit(m_phong_renderer.get_texture_shader(), mesh_comp.m_mesh);
}
else // Has a Mesh and Texture but no diffuse texture. Use the colour instead.
{
DrawCall dc;
dc.set_uniform("view_position", m_view_information.m_view_position);
dc.set_uniform("model", p_transform.m_model);
dc.set_uniform("shininess", texComponent.m_shininess);
dc.set_uniform("uColour", texComponent.m_colour);
dc.submit(m_phong_renderer.get_uniform_colour_shader(), mesh_comp.m_mesh);
}

return;
}

DrawCall dc;
dc.set_uniform("view_position", m_view_information.m_view_position);
dc.set_uniform("model", p_transform.m_model);
dc.set_uniform("shininess", texComponent.m_shininess);
dc.set_texture("diffuse", texComponent.m_diffuse.has_value() ? texComponent.m_diffuse : m_missing_texture);
dc.set_texture("specular", texComponent.m_specular.has_value() ? texComponent.m_specular : m_blank_texture);
dc.submit(m_phong_renderer.get_shader(), mesh_comp.m_mesh);
}
else
{
// Fallback to rendering using default colour and no lighting.
DrawCall dc;
dc.set_uniform("model", p_transform.m_model);
dc.set_uniform("colour", glm::vec4(0.06f, 0.44f, 0.81f, 1.f));
Expand All @@ -126,7 +142,7 @@ namespace OpenGL
dc.set_uniform("shininess", 64.f);
dc.set_texture("diffuse", p_terrain.m_texture.has_value() ? p_terrain.m_texture : m_missing_texture);
dc.set_texture("specular", m_blank_texture);
dc.submit(m_phong_renderer.get_shader(), p_terrain.m_mesh);
dc.submit(m_phong_renderer.get_texture_shader(), p_terrain.m_mesh);
});
}

Expand Down
Loading

0 comments on commit 673639b

Please sign in to comment.