From b826b66fae1f2ade7ef126a22495bbc8403e4aaa Mon Sep 17 00:00:00 2001 From: Shariq Shah Date: Mon, 1 Aug 2016 12:30:50 +0500 Subject: [PATCH] Switched to Blinn_Phong speculars --- assets/shaders/blinn_phong.frag | 66 ++++++++++++++++----------------- src/game.c | 12 ++++-- src/light.c | 2 +- src/light.h | 2 +- src/model.c | 4 +- 5 files changed, 46 insertions(+), 40 deletions(-) diff --git a/assets/shaders/blinn_phong.frag b/assets/shaders/blinn_phong.frag index 2f5416e..a855ca5 100644 --- a/assets/shaders/blinn_phong.frag +++ b/assets/shaders/blinn_phong.frag @@ -8,7 +8,7 @@ struct Light float inner_angle; float falloff; float intensity; - vec4 color; + vec3 color; uint pcf_enabled; int type; int radius; @@ -30,17 +30,16 @@ uniform int total_active_lights; uniform float specular; uniform float diffuse; uniform float specular_strength; - out vec4 frag_color; -vec4 calc_point_light(in Light point_light) +vec3 calc_point_light(in Light light) { - vec4 diffuse_comp = vec4(0.0); - vec4 specular_comp = vec4(0.0); - vec3 light_direction = vertex - point_light.position; + vec3 diffuse_comp = vec3(0.0); + vec3 specular_comp = vec3(0.0); + vec3 light_direction = vertex - light.position; float dist = abs(length(light_direction)); - if(dist <= point_light.radius) + if(dist <= light.radius) { light_direction = normalize(light_direction); vec3 normalized_normal = normalize(normal); @@ -49,58 +48,59 @@ vec4 calc_point_light(in Light point_light) if(cos_ang_incidence > 0) { - diffuse_comp = point_light.color * diffuse * cos_ang_incidence; + diffuse_comp = light.color * diffuse * cos_ang_incidence; vec3 vertex_to_eye = normalize(camera_pos - vertex); - vec3 light_reflect = normalize(reflect(light_direction, normalized_normal)); - float specular_factor = max(0.0, dot(vertex_to_eye, light_reflect)); + vec3 halfway = normalize(light.direction + vertex_to_eye); + float specular_factor = max(0.0, dot(normalized_normal, halfway)); specular_factor = pow(specular_factor, specular_strength); - specular_comp = point_light.color * specular * specular_factor; + specular_comp = light.color * specular * specular_factor; } - float attenuation = pow(max(0.0, (1.0 - (dist / point_light.radius))), point_light.falloff + 1.0f); - return (((diffuse_comp + specular_comp) * attenuation) * point_light.intensity); + float attenuation = pow(max(0.0, (1.0 - (dist / light.radius))), light.falloff + 1.0f); + return (((diffuse_comp + specular_comp) * attenuation) * light.intensity); } else { - return vec4(0.0); + return vec3(0.0); } } -vec4 calc_dir_light(in Light dir_light) +vec3 calc_dir_light(in Light light) { - vec4 diffuse_comp = vec4(0.0); - vec4 specular_comp = vec4(0.0); + vec3 diffuse_comp = vec3(0.0); + vec3 specular_comp = vec3(0.0); vec3 normalized_normal = normalize(normal); - float cos_ang_incidence = dot(normalized_normal, -dir_light.direction); + float cos_ang_incidence = dot(normalized_normal, -light.direction); cos_ang_incidence = clamp(cos_ang_incidence, 0, 1); float shadow_factor = 1.0; if(cos_ang_incidence > 0) { - diffuse_comp = dir_light.color * diffuse * cos_ang_incidence; + diffuse_comp = light.color * diffuse * cos_ang_incidence; vec3 vertex_to_eye = normalize(camera_pos - vertex); - vec3 light_reflect = normalize(reflect(dir_light.direction, normalized_normal)); - float specular_factor = max(0.0, dot(vertex_to_eye, light_reflect)); + vec3 light_reflect = normalize(reflect(light.direction, normalized_normal)); + vec3 halfway = normalize(light.direction + vertex_to_eye); + float specular_factor = max(0.0, dot(normalized_normal, halfway)); specular_factor = pow(specular_factor, specular_strength); - specular_comp = dir_light.color * specular * specular_factor; + specular_comp = light.color * specular * specular_factor; // if(light.castShadow == 1) // { // shadow_factor = calcShadowFactor(vertLightSpace.xyz); // } } - //return (dir_light.intensity * (diffuse_comp + specular_comp)) * shadow_factor; - return (dir_light.intensity * (diffuse_comp + specular_comp)); + //return (light.intensity * (diffuse_comp + specular_comp)) * shadow_factor; + return (light.intensity * (diffuse_comp + specular_comp)); } -vec4 calc_spot_light(in Light spot_light) +vec3 calc_spot_light(in Light light) { - vec4 color = vec4(0.0); - vec3 light_to_surface = vertex - spot_light.position; - float angle = dot(spot_light.direction, normalize(light_to_surface)); - if(acos(angle) < spot_light.outer_angle) + vec3 color = vec3(0.0); + vec3 light_to_surface = vertex - light.position; + float angle = dot(light.direction, normalize(light_to_surface)); + if(acos(angle) < light.outer_angle) { - color = calc_point_light(spot_light); - color *= smoothstep(cos(spot_light.outer_angle), cos(spot_light.inner_angle), angle); + color = calc_point_light(light); + color *= smoothstep(cos(light.outer_angle), cos(light.inner_angle), angle); // if(light.cast_shadow != 0) // { // float shadow_factor = calc_shadow_factor(vert_light_space.xyz / vert_light_space.w); @@ -114,7 +114,7 @@ vec4 calc_spot_light(in Light spot_light) void main() { vec4 albedo_color = diffuse_color * texture(diffuse_texture, uv); - vec4 light_contribution = vec4(0.0, 0.0, 0.0, 1.0); + vec3 light_contribution = vec3(0.0, 0.0, 0.0); for(int i = 0; i < total_active_lights; i++) { @@ -129,5 +129,5 @@ void main() } frag_color = (albedo_color * vec4(0.1, 0.1, 0.1, 1.0)) + - (albedo_color * light_contribution); + (albedo_color * vec4(light_contribution, 1.0)); } diff --git a/src/game.c b/src/game.c index 5b3994c..e907720 100644 --- a/src/game.c +++ b/src/game.c @@ -123,6 +123,8 @@ void scene_setup(void) struct Entity* suz = scene_add_as_child("Suzanne", NULL, parent_node); struct Model* suz_model = entity_component_add(suz, C_MODEL, "suzanne.pamesh", "Blinn_Phong"); model_set_material_param(suz_model, "diffuse_color", &color); + float spec_str = 80.f; + model_set_material_param(suz_model, "specular_strength", &spec_str); struct Transform* s_tran = entity_component_get(suz, C_TRANSFORM); vec3 s_pos = {x, 5, z}; transform_translate(s_tran, &s_pos, TS_WORLD); @@ -134,6 +136,8 @@ void scene_setup(void) model_set_material_param(ground_model, "diffuse_color", &color); int white_tex = texture_create_from_file("white.tga", TU_DIFFUSE); model_set_material_param(ground_model, "diffuse_texture", &white_tex); + float spec_str = 80.f; + model_set_material_param(ground_model, "specular_strength", &spec_str); struct Transform* ground_tran = entity_component_get(ground, C_TRANSFORM); vec3 pos = {0, -15, 0}; vec3 scale_ground = {200.f, 200.f, 200.f}; @@ -159,10 +163,10 @@ void scene_setup(void) x++; z++; struct Entity* light_ent = scene_add_new("Light_Ent", NULL); struct Transform* light_tran = entity_component_get(light_ent, C_TRANSFORM); - vec3 lt_pos = {x * 10, 3, z * 10}; + vec3 lt_pos = {x * 20, 0, z * 20}; transform_set_position(light_tran, <_pos); - struct Light* light_comp = entity_component_add(light_ent, C_LIGHT, LT_SPOT); - vec4_fill(&light_comp->color, 1.f / (float)x, 1.f / ((rand() % 10) + 1.f), 1.f / (float)z, 1); + struct Light* light_comp = entity_component_add(light_ent, C_LIGHT, LT_POINT); + vec3_fill(&light_comp->color, 1.f / (float)x, 1.f / ((rand() % 10) + 1.f), 1.f / (float)z); light_comp->intensity = 1.f; } @@ -289,7 +293,7 @@ void debug(float dt) /* transform_rotate(mod_tran, &y_axis, 25.f * dt, TS_WORLD); */ vec3 amount = {0, 0, 5 * dt}; transform_translate(mod_tran, &amount, TS_LOCAL); - } + } /* if(input_key_state_get(GLFW_KEY_C, GLFW_PRESS)) */ /* { */ diff --git a/src/light.c b/src/light.c index a33ffe0..fbd6cae 100644 --- a/src/light.c +++ b/src/light.c @@ -65,7 +65,7 @@ int light_create(int node, int light_type) new_light->node = node; new_light->valid = 1; new_light->cast_shadow = 0; - vec4_fill(&new_light->color, 1.f, 1.f, 1.f, 1.f); + vec3_fill(&new_light->color, 1.f, 1.f, 1.f); new_light->depth_bias = 0.0005f; new_light->type = light_type; new_light->pcf_enabled = 0; diff --git a/src/light.h b/src/light.h index ea28063..754e47a 100644 --- a/src/light.h +++ b/src/light.h @@ -19,7 +19,7 @@ struct Light float inner_angle; float falloff; float intensity; - vec4 color; + vec3 color; int32 node; uint8 cast_shadow; uint8 pcf_enabled; diff --git a/src/model.c b/src/model.c index 39f4957..1b02bae 100644 --- a/src/model.c +++ b/src/model.c @@ -21,6 +21,8 @@ static struct Model* model_list; static int* empty_indices; +static int use_blinn = 1; + struct Model* model_get(int index) { struct Model* model = NULL; @@ -208,7 +210,7 @@ void model_render_all(struct Camera* camera) } snprintf(uniform_name, max_name_len, "lights[%d].color", i); - shader_set_uniform_vec4(material->shader, uniform_name, &light->color); + shader_set_uniform_vec3(material->shader, uniform_name, &light->color); memset(uniform_name, '\0', max_name_len); snprintf(uniform_name, max_name_len, "lights[%d].intensity", i);