Hi there! I’m slowly learning GLSL, and I’m currently following a tutorial and creating a Phong light shader.
The illumination is working, so if I create a light node and attach it to the root node, the model is illuminated. However, I have hit a stumbling block with camera movement.
When I rotate the default camera (the one you start with in the Basic Game template) - akin to looking around - the light on the model shifts ever so slightly. I have compared it to jME’s Lighting shader and it does not exhibit the same issue.
I am not sure whether this is the way it is supposed to work, and I would appreciate it if anyone could shed some light on this. Thank you!
For reference, the code is below:
.j3md file:
MaterialDef Solid {
//This is the complete list of user defined uniforms to be used in the
//shaders
MaterialParameters {
Vector3 Color
}
Technique {
LightMode MultiPass
//This is where the vertex and fragment shader files are
//specified
VertexShader GLSL100: Shaders/solid.vert
FragmentShader GLSL100: Shaders/solid.frag
//This is where you specify which global uniform you need for your
//shaders
WorldParameters {
NormalMatrix
WorldViewMatrix
WorldViewProjectionMatrix
}
}
}
.vert file:
//the global uniform World view projection matrix
//(more on global uniforms below)
uniform mat3 g_NormalMatrix;
uniform mat4 g_WorldViewMatrix;
uniform mat4 g_WorldViewProjectionMatrix;
//The attribute inPosition is the Object space position of the vertex
attribute vec3 inPosition;
attribute vec3 inNormal;
varying vec3 outNormal;
varying vec4 outEye;
varying vec4 copy;
void main(){
//Transformation of the object space coordinate to projection space
//coordinates.
//- gl_Position is the standard GLSL variable holding projection space
//position. It must be filled in the vertex shader
//- To convert position we multiply the worldViewProjectionMatrix by
//by the position vector.
//The multiplication must be done in this order.
outNormal = normalize(g_NormalMatrix * inNormal);
outEye = -normalize(g_WorldViewProjectionMatrix * vec4(inPosition, 0.0));
gl_Position = g_WorldViewProjectionMatrix * vec4(inPosition, 1.0);
}
.frag file:
uniform vec3 m_Color;
uniform vec4 g_LightPosition;
uniform vec4 g_AmbientLightColor;
uniform mat4 g_WorldViewMatrix;
varying vec3 outNormal;
varying vec4 outEye;
void main(){
vec4 diffuse = vec4(m_Color, 1.0);
vec4 ambient = g_AmbientLightColor;
vec4 specular = vec4(0.25, 0.25, 0.25, 1.0);
float shininess = 10.0;
vec4 light = normalize(g_LightPosition);
vec3 direction = -normalize(light.xyz);
// set the specular term to black
vec4 spec = vec4(0.0, 0.0, 0.0, 0.0);
// normalize both input vectors
vec3 n = normalize(outNormal);
vec3 e = normalize(vec3(outEye));
float intensity = max(dot(n, direction), 0.0);
// if the vertex is lit compute the specular color
if (intensity > 0.0) {
// compute the half vector
vec3 h = normalize(direction + e);
// compute the specular term into spec
float intSpec = max(dot(h, n), 0.0);
spec = specular * pow(intSpec, shininess);
}
gl_FragColor = max(pow(intensity * diffuse + spec, 1), ambient);
}
Images for reference:
Before
After
Notice how on the right side of the object, the shadow is much more prevalent in the second picture. That’s my problem