View Full Version : Basic lighting not working

05-09-2017, 01:19 AM
Again, after few more attempts, I've been able to resolve this problem as well (lol). Sorry for yet another resolved post.

05-09-2017, 01:44 AM
your VAO "normals" is not connected to the correct vertex shader attibute location:

glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, 0, 0);

your vertex shader transforms the normals not correctly, you need to transform the position into 2 different coordinate systems: "world space" to do th lighting calculation, and "clip space" for gl_Position

gl_Position = MVP * vec4(in_position, 1);
vec3 transformed_position = (M * vec4(in_position, 1)).xyz;
vec3 transformed_normal = (M * vec4(in_normal, 0)).xyz;

then you need to pass transformed_position and transformed_normal to the fragmentshader

what your fragmentshader does isnt actually clear. try to write is more clearly to understand, like:

// get basic fragment color
vec3 Kd = vertexcolor * texture(tex1, texcoord);

// basic light parts
vec3 Iambient = vec3(0.2, 0.2, 0.2); // for example
vec3 Idiffuse = vec3(0, 0, 0);
vec3 Ispecular = vec3(0, 0, 0);

// do some calculations here ...

// final fragment color
finalcolor = Iambient * Ka + Idiffuse * Kd + Ispecular * Ks;

some arent distinguishing between Ka and Kd and instead use simply Kd like that:
// final fragment color

finalcolor = (Iambient + Idiffuse) * Kd + Ispecular * Ks;