Fixed issues with non-uniform scale and normals

dev
Shariq Shah 9 years ago
parent b826b66fae
commit 4d52269b68
  1. 2
      assets/shaders/blinn_phong.frag
  2. 5
      assets/shaders/commonVert.glsl
  3. 10
      src/game.c
  4. 10
      src/material.c
  5. 8
      src/model.c

@ -129,5 +129,5 @@ void main()
}
frag_color = (albedo_color * vec4(0.1, 0.1, 0.1, 1.0)) +
(albedo_color * vec4(light_contribution, 1.0));
(albedo_color * vec4(light_contribution, 1.0));
}

@ -12,6 +12,7 @@ out vec4 vertLightSpace;
// Common uniforms
uniform mat4 model_mat;
uniform mat4 inv_model_mat;
uniform mat4 view_mat;
uniform mat4 mvp;
uniform mat4 lightVPMat;
@ -25,10 +26,8 @@ void setOutputs()
{
uv = vUV;
//Normal and vertex sent to the fragment shader should be in the same space!
normal = vec4(model_mat * vec4(vNormal, 0.0)).xyz;
normal = vec4(transpose(inv_model_mat) * vec4(vNormal, 0.0)).xyz;
vertex = vec4(model_mat * vec4(vPosition, 1.0)).xyz;
// normal = vNormal;
// vertex = vPosition;
vertCamSpace = vec4(view_mat * vec4(vPosition, 1.0)).xyz;
vertLightSpace = vec4((lightVPMat * model_mat) * vec4(vPosition, 1.0));
}

@ -105,8 +105,10 @@ void scene_setup(void)
struct Transform* tran = entity_component_get(new_ent, C_TRANSFORM);
vec3 position = {0, 0, -5};
transform_translate(tran, &position, TS_WORLD);
struct Model* box_model = entity_component_add(new_ent, C_MODEL, "default.pamesh", "Blinn_Phong");
struct Model* box_model = entity_component_add(new_ent, C_MODEL, "torus.pamesh", "Blinn_Phong");
model_set_material_param(box_model, "diffuse_color", &color);
int tex = texture_create_from_file("white.tga", TU_DIFFUSE);
model_set_material_param(box_model, "diffuse_texture", &tex);
struct Transform* model_tran = entity_component_get(new_ent, C_TRANSFORM);
vec3 scale = {1, 1, 1};
transform_scale(model_tran, &scale);
@ -170,9 +172,9 @@ void scene_setup(void)
light_comp->intensity = 1.f;
}
/* struct Entity* sun = scene_add_new("Sun", NULL); */
/* struct Light* sun_light = entity_component_add(sun, C_LIGHT, LT_DIR); */
/* sun_light->intensity = 0.8f; */
struct Entity* sun = scene_add_new("Sun", NULL);
struct Light* sun_light = entity_component_add(sun, C_LIGHT, LT_DIR);
sun_light->intensity = 0.8f;
}
void debug(float dt)

@ -46,6 +46,11 @@ void material_init(void)
uniform->type = UT_MAT4;
uniform->location = shader_get_uniform_location(unshaded_mat->shader, uniform->name);
uniform = array_grow(unshaded_mat->pipeline_params, struct Uniform);
uniform->name = str_new("inv_model_mat");
uniform->type = UT_MAT4;
uniform->location = shader_get_uniform_location(unshaded_mat->shader, uniform->name);
/* Material params */
uniform = array_grow(unshaded_mat->model_params, struct Uniform);
uniform->name = str_new("diffuse_color");
@ -85,6 +90,11 @@ void material_init(void)
uniform->type = UT_MAT4;
uniform->location = shader_get_uniform_location(blinn_phong_mat->shader, uniform->name);
uniform = array_grow(blinn_phong_mat->pipeline_params, struct Uniform);
uniform->name = str_new("inv_model_mat");
uniform->type = UT_MAT4;
uniform->location = shader_get_uniform_location(blinn_phong_mat->shader, uniform->name);
/* Material params */
uniform = array_grow(blinn_phong_mat->model_params, struct Uniform);
uniform->name = str_new("diffuse_color");

@ -159,6 +159,14 @@ void model_render_all(struct Camera* camera)
shader_set_uniform(uniform->type, uniform->location, &camera->view_mat);
renderer_check_glerror("model:render_all:material_pipeline");
}
else if(strcmp(uniform->name, "inv_model_mat") == 0)
{
mat4 inv_mat;
mat4_identity(&inv_mat);
mat4_inverse(&inv_mat, &transform->trans_mat);
shader_set_uniform(uniform->type, uniform->location, &inv_mat);
renderer_check_glerror("model:render_all:material_pipeline");
}
}
if(material->lit) /* Set light information */

Loading…
Cancel
Save