diff --git a/assets/shaders/blinn_phong.frag b/assets/shaders/blinn_phong.frag index a855ca5..1f7592d 100644 --- a/assets/shaders/blinn_phong.frag +++ b/assets/shaders/blinn_phong.frag @@ -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)); } diff --git a/assets/shaders/commonVert.glsl b/assets/shaders/commonVert.glsl index be16844..87a0b41 100644 --- a/assets/shaders/commonVert.glsl +++ b/assets/shaders/commonVert.glsl @@ -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)); } diff --git a/src/game.c b/src/game.c index e907720..7bcb3fa 100644 --- a/src/game.c +++ b/src/game.c @@ -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) diff --git a/src/material.c b/src/material.c index 9390483..c14ff1f 100644 --- a/src/material.c +++ b/src/material.c @@ -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"); diff --git a/src/model.c b/src/model.c index 1b02bae..20dbd5d 100644 --- a/src/model.c +++ b/src/model.c @@ -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 */