Switched to Blinn_Phong speculars

dev
Shariq Shah 9 years ago
parent b53dcf287e
commit b826b66fae
  1. 66
      assets/shaders/blinn_phong.frag
  2. 12
      src/game.c
  3. 2
      src/light.c
  4. 2
      src/light.h
  5. 4
      src/model.c

@ -8,7 +8,7 @@ struct Light
float inner_angle; float inner_angle;
float falloff; float falloff;
float intensity; float intensity;
vec4 color; vec3 color;
uint pcf_enabled; uint pcf_enabled;
int type; int type;
int radius; int radius;
@ -30,17 +30,16 @@ uniform int total_active_lights;
uniform float specular; uniform float specular;
uniform float diffuse; uniform float diffuse;
uniform float specular_strength; uniform float specular_strength;
out vec4 frag_color; 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); vec3 diffuse_comp = vec3(0.0);
vec4 specular_comp = vec4(0.0); vec3 specular_comp = vec3(0.0);
vec3 light_direction = vertex - point_light.position; vec3 light_direction = vertex - light.position;
float dist = abs(length(light_direction)); float dist = abs(length(light_direction));
if(dist <= point_light.radius) if(dist <= light.radius)
{ {
light_direction = normalize(light_direction); light_direction = normalize(light_direction);
vec3 normalized_normal = normalize(normal); vec3 normalized_normal = normalize(normal);
@ -49,58 +48,59 @@ vec4 calc_point_light(in Light point_light)
if(cos_ang_incidence > 0) 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 vertex_to_eye = normalize(camera_pos - vertex);
vec3 light_reflect = normalize(reflect(light_direction, normalized_normal)); vec3 halfway = normalize(light.direction + vertex_to_eye);
float specular_factor = max(0.0, dot(vertex_to_eye, light_reflect)); float specular_factor = max(0.0, dot(normalized_normal, halfway));
specular_factor = pow(specular_factor, specular_strength); 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); float attenuation = pow(max(0.0, (1.0 - (dist / light.radius))), light.falloff + 1.0f);
return (((diffuse_comp + specular_comp) * attenuation) * point_light.intensity); return (((diffuse_comp + specular_comp) * attenuation) * light.intensity);
} }
else 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); vec3 diffuse_comp = vec3(0.0);
vec4 specular_comp = vec4(0.0); vec3 specular_comp = vec3(0.0);
vec3 normalized_normal = normalize(normal); 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); cos_ang_incidence = clamp(cos_ang_incidence, 0, 1);
float shadow_factor = 1.0; float shadow_factor = 1.0;
if(cos_ang_incidence > 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 vertex_to_eye = normalize(camera_pos - vertex);
vec3 light_reflect = normalize(reflect(dir_light.direction, normalized_normal)); 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_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) // if(light.castShadow == 1)
// { // {
// shadow_factor = calcShadowFactor(vertLightSpace.xyz); // shadow_factor = calcShadowFactor(vertLightSpace.xyz);
// } // }
} }
//return (dir_light.intensity * (diffuse_comp + specular_comp)) * shadow_factor; //return (light.intensity * (diffuse_comp + specular_comp)) * shadow_factor;
return (dir_light.intensity * (diffuse_comp + specular_comp)); 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 color = vec3(0.0);
vec3 light_to_surface = vertex - spot_light.position; vec3 light_to_surface = vertex - light.position;
float angle = dot(spot_light.direction, normalize(light_to_surface)); float angle = dot(light.direction, normalize(light_to_surface));
if(acos(angle) < spot_light.outer_angle) if(acos(angle) < light.outer_angle)
{ {
color = calc_point_light(spot_light); color = calc_point_light(light);
color *= smoothstep(cos(spot_light.outer_angle), cos(spot_light.inner_angle), angle); color *= smoothstep(cos(light.outer_angle), cos(light.inner_angle), angle);
// if(light.cast_shadow != 0) // if(light.cast_shadow != 0)
// { // {
// float shadow_factor = calc_shadow_factor(vert_light_space.xyz / vert_light_space.w); // 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() void main()
{ {
vec4 albedo_color = diffuse_color * texture(diffuse_texture, uv); 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++) 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)) + frag_color = (albedo_color * vec4(0.1, 0.1, 0.1, 1.0)) +
(albedo_color * light_contribution); (albedo_color * vec4(light_contribution, 1.0));
} }

@ -123,6 +123,8 @@ void scene_setup(void)
struct Entity* suz = scene_add_as_child("Suzanne", NULL, parent_node); 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"); struct Model* suz_model = entity_component_add(suz, C_MODEL, "suzanne.pamesh", "Blinn_Phong");
model_set_material_param(suz_model, "diffuse_color", &color); 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); struct Transform* s_tran = entity_component_get(suz, C_TRANSFORM);
vec3 s_pos = {x, 5, z}; vec3 s_pos = {x, 5, z};
transform_translate(s_tran, &s_pos, TS_WORLD); 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); model_set_material_param(ground_model, "diffuse_color", &color);
int white_tex = texture_create_from_file("white.tga", TU_DIFFUSE); int white_tex = texture_create_from_file("white.tga", TU_DIFFUSE);
model_set_material_param(ground_model, "diffuse_texture", &white_tex); 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); struct Transform* ground_tran = entity_component_get(ground, C_TRANSFORM);
vec3 pos = {0, -15, 0}; vec3 pos = {0, -15, 0};
vec3 scale_ground = {200.f, 200.f, 200.f}; vec3 scale_ground = {200.f, 200.f, 200.f};
@ -159,10 +163,10 @@ void scene_setup(void)
x++; z++; x++; z++;
struct Entity* light_ent = scene_add_new("Light_Ent", NULL); struct Entity* light_ent = scene_add_new("Light_Ent", NULL);
struct Transform* light_tran = entity_component_get(light_ent, C_TRANSFORM); 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, &lt_pos); transform_set_position(light_tran, &lt_pos);
struct Light* light_comp = entity_component_add(light_ent, C_LIGHT, LT_SPOT); struct Light* light_comp = entity_component_add(light_ent, C_LIGHT, LT_POINT);
vec4_fill(&light_comp->color, 1.f / (float)x, 1.f / ((rand() % 10) + 1.f), 1.f / (float)z, 1); vec3_fill(&light_comp->color, 1.f / (float)x, 1.f / ((rand() % 10) + 1.f), 1.f / (float)z);
light_comp->intensity = 1.f; light_comp->intensity = 1.f;
} }
@ -289,7 +293,7 @@ void debug(float dt)
/* transform_rotate(mod_tran, &y_axis, 25.f * dt, TS_WORLD); */ /* transform_rotate(mod_tran, &y_axis, 25.f * dt, TS_WORLD); */
vec3 amount = {0, 0, 5 * dt}; vec3 amount = {0, 0, 5 * dt};
transform_translate(mod_tran, &amount, TS_LOCAL); transform_translate(mod_tran, &amount, TS_LOCAL);
} }
/* if(input_key_state_get(GLFW_KEY_C, GLFW_PRESS)) */ /* if(input_key_state_get(GLFW_KEY_C, GLFW_PRESS)) */
/* { */ /* { */

@ -65,7 +65,7 @@ int light_create(int node, int light_type)
new_light->node = node; new_light->node = node;
new_light->valid = 1; new_light->valid = 1;
new_light->cast_shadow = 0; 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->depth_bias = 0.0005f;
new_light->type = light_type; new_light->type = light_type;
new_light->pcf_enabled = 0; new_light->pcf_enabled = 0;

@ -19,7 +19,7 @@ struct Light
float inner_angle; float inner_angle;
float falloff; float falloff;
float intensity; float intensity;
vec4 color; vec3 color;
int32 node; int32 node;
uint8 cast_shadow; uint8 cast_shadow;
uint8 pcf_enabled; uint8 pcf_enabled;

@ -21,6 +21,8 @@
static struct Model* model_list; static struct Model* model_list;
static int* empty_indices; static int* empty_indices;
static int use_blinn = 1;
struct Model* model_get(int index) struct Model* model_get(int index)
{ {
struct Model* model = NULL; 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); 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); memset(uniform_name, '\0', max_name_len);
snprintf(uniform_name, max_name_len, "lights[%d].intensity", i); snprintf(uniform_name, max_name_len, "lights[%d].intensity", i);

Loading…
Cancel
Save