PDA

View Full Version : Basic lighting not working



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

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

glEnableVertexAttribArray(3);
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;
https://en.wikipedia.org/wiki/Phong_reflection_model
https://sites.google.com/site/john87connor/scene-light-material/3-directional-light

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

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