Hello. I am creating a 3D world where I send matrices to a shader to create this. I am now trying to light up my world but it is causing me trouble. This is something I’ve been struggling for weeks on.
Here’s my fragment shader code:
#version 120
varying vec4 color;
varying vec4 normal;
varying vec4 vertex;
uniform mat4 modelMatrix;
uniform mat4 viewMatrix;
uniform mat4 projectionMatrix;
uniform mat4 normalMatrix;
void main() {
mat4 modelView = viewMatrix * modelMatrix;
vec3 vertexPos = (modelView * vertex).xyz;
vec3 surfaceNormal = normalize((normalMatrix*normal).xyz);
vec3 lightDirection = normalize(gl_LightSource[0].position.xyz - vertexPos);
float diffuseLightIntensity = max(0, dot(surfaceNormal, lightDirection));
gl_FragColor.rgb = diffuseLightIntensity * color.rgb;
gl_FragColor += gl_LightModel.ambient;
vec3 reflectionDirection = normalize(reflect(-lightDirection, surfaceNormal));
float specular = max(0.0, dot(surfaceNormal, reflectionDirection));
if (diffuseLightIntensity != 0) {
float fspecular = pow(specular, gl_FrontMaterial.shininess);
gl_FragColor += fspecular;
}
}
Here’s my vertex shader code:
#version 120
varying vec4 color;
varying vec4 normal;
varying vec4 vertex;
uniform mat4 modelMatrix;
uniform mat4 viewMatrix;
uniform mat4 projectionMatrix;
uniform mat4 normalMatrix;
void main() {
color = gl_FrontMaterial.diffuse;
normal = vec4(gl_Normal, 1.0);
vertex = gl_Vertex;
gl_Position = projectionMatrix * viewMatrix * modelMatrix * gl_Vertex;
}
I have my own maths classes that send the code to the shader and they work fine. Am I missing something with the code? I think I’m drawing the vertices correctly, by using a VBO. Cheers.
Here’s what the lighting looks like: http://i.imgur.com/BSEZ9qc.png