Add specular lighting

This commit is contained in:
SeanOMik 2021-12-09 16:24:26 -05:00
parent 18f50d5420
commit 576d4030b8
5 changed files with 28 additions and 9 deletions

View File

@ -5,14 +5,18 @@ in mat4 vs_transform;
in vec2 vs_texcoord; in vec2 vs_texcoord;
in vec3 vs_normal; in vec3 vs_normal;
in vec3 vs_to_light; in vec3 vs_to_light;
in vec3 vs_to_camera;
uniform bool texture_is_set; uniform bool texture_is_set;
uniform sampler2D vs_texture; uniform sampler2D vs_texture;
uniform vec3 light_color; uniform vec3 light_color;
uniform float shine_damper;
uniform float reflectivity;
out vec4 fs_color; out vec4 fs_color;
void main() { void main() {
// Lighting
vec3 unit_normal = normalize(vs_normal); vec3 unit_normal = normalize(vs_normal);
vec3 unit_light_vector = normalize(vs_to_light); vec3 unit_light_vector = normalize(vs_to_light);
@ -20,9 +24,23 @@ void main() {
float brightness = max(dot_prod, 0.f); float brightness = max(dot_prod, 0.f);
vec3 diffuse = brightness * light_color; vec3 diffuse = brightness * light_color;
// Specular lighting
// only do all this math is reflectivity is > 0
vec3 final_specular = vec3(0.f);
if (reflectivity > 0) {
vec3 unit_vector_to_camera = normalize(vs_to_camera);
vec3 light_direction = -unit_vector_to_camera;
vec3 reflected_light_dir = reflect(light_direction, unit_normal);
float specular_factor = dot(reflected_light_dir, unit_vector_to_camera);
specular_factor = max(specular_factor, 0.f);
float damped_specular = pow(specular_factor, shine_damper);
final_specular = damped_specular * reflectivity * light_color;
}
if (texture_is_set) { if (texture_is_set) {
fs_color = vec4(diffuse, 1.f) * texture(vs_texture, vs_texcoord); //fs_color = vec4(0.5 * unit_normal + vec3(0.5), 1.f); // Visualize normals
fs_color = vec4(diffuse, 1.f) * texture(vs_texture, vs_texcoord) + vec4(final_specular, 1.f);
} else { } else {
fs_color = vec4(diffuse, 1.f); fs_color = vec4(diffuse, 1.f) + vec4(final_specular, 1.f);
} }
} }

View File

@ -9,6 +9,7 @@ out vec2 vs_texcoord;
out mat4 vs_transform; out mat4 vs_transform;
out vec3 vs_normal; out vec3 vs_normal;
out vec3 vs_to_light; out vec3 vs_to_light;
out vec3 vs_to_camera;
uniform mat4 transform_matrix; uniform mat4 transform_matrix;
uniform mat4 view_matrix; uniform mat4 view_matrix;
@ -26,11 +27,5 @@ void main() {
vs_normal = (transform_matrix * vec4(vertex_normal, 0.f)).xyz; vs_normal = (transform_matrix * vec4(vertex_normal, 0.f)).xyz;
vs_to_light = light_position - world_pos.xyz; vs_to_light = light_position - world_pos.xyz;
vs_to_camera = (inverse(view_matrix) * vec4(0.f, 0.f, 0.f, 1.f)).xyz - world_pos.xyz;
/* vs_position = vec4(transform_matrix * vec4(vertex_position, 1.f)).xyz;
vs_transform = transform_matrix;
vs_color = vertex_color;
vs_texcoord = vertex_texcoord;
gl_Position = projection_matrix * view_matrix * transform_matrix * vec4(vertex_position, 1.0f); */
} }

View File

@ -52,6 +52,8 @@ int main(int argc, char *argv[]) {
game.add_event(light); game.add_event(light);
simpleengine::gfx::Texture white_texture("resources/white_texture.png"); simpleengine::gfx::Texture white_texture("resources/white_texture.png");
/* white_texture.shine_damper = 10;
white_texture.reflectivity = 1; */
auto dragon = std::make_shared<simpleengine::objects_3d::ObjModel>(game.get_window(), core_shader, white_texture, "resources/dragon.obj"); auto dragon = std::make_shared<simpleengine::objects_3d::ObjModel>(game.get_window(), core_shader, white_texture, "resources/dragon.obj");
dragon->translate(0.f, -5.f, -25.f); dragon->translate(0.f, -5.f, -25.f);
game.add_event(dragon); game.add_event(dragon);

View File

@ -29,6 +29,8 @@ namespace simpleengine::gfx {
int height; int height;
int width; int width;
int channels; int channels;
float shine_damper = 1.f;
float reflectivity = 0.f;
/** /**
* @brief Construct a new Texture object from a path. * @brief Construct a new Texture object from a path.

View File

@ -19,6 +19,8 @@ namespace simpleengine::gfx {
void TexturedModel::render(GLFWwindow* target) { void TexturedModel::render(GLFWwindow* target) {
shader.use(); shader.use();
shader.set_uniform_matrix_4f("transform_matrix", transform_matrix, false); shader.set_uniform_matrix_4f("transform_matrix", transform_matrix, false);
shader.set_uniform_float("shine_damper", texture.shine_damper, false);
shader.set_uniform_float("reflectivity", texture.reflectivity, false);
// When binding to the texture, tell the shader if the texture is set or not. // When binding to the texture, tell the shader if the texture is set or not.
shader.set_uniform_int("texture_is_set", (GLint) true, false); shader.set_uniform_int("texture_is_set", (GLint) true, false);