Hi, in order to enable basic lighting (diffuse) effects, the vertex and fragment shaders must be modified as follows:
static const char* cubeMeshVertexShader = " \
attribute vec4 vertexPosition; \
attribute vec4 vertexNormal; \
attribute vec2 vertexTexCoord; \
varying vec2 texCoord; \
varying vec4 normal; \
uniform mat4 modelViewMatrix; \
uniform mat4 modelViewProjectionMatrix; \
void main() { \
gl_Position = modelViewProjectionMatrix * vertexPosition; \
normal = modelViewMatrix * vec4(vertexNormal.xyz, 0.0); \
texCoord = vertexTexCoord; \
}";
static const char* cubeFragmentShader = " \
precision mediump float; \
varying vec2 texCoord; \
varying vec4 normal; \
uniform sampler2D texSampler2D; \
void main() { \
vec3 n = normalize(normal.xyz); \
vec3 lightDir = normalize(vec3(0.0, 0.0, -1.0)); \
vec3 ambientLight = vec3(0.5, 0.5, 0.5); \
vec3 ambientMaterial = vec3(0.3, 0.3, 0.3); \
vec3 ambient = ambientMaterial * ambientLight; \
vec3 diffuseLight = vec3(1.0, 1.0, 1.0); \
vec3 diffuseMaterial = vec3(0.9, 0.9, 0.9); \
float diffuseFactor = max(0.0, dot(n, lightDir)); \
vec3 diffuse = diffuseFactor * diffuseMaterial * diffuseLight; \
vec3 shadedColor = ambient + diffuse; \
gl_FragColor = vec4(shadedColor, 1.0); \
}";
It is important to note that some of the variables defined in the fragment shaders, such as lightDir, ambientLight, diffuseLight and diffuseMaterial, would be better declared as uniform parameters, so that their values could be set from outside the shader (i.e. controlled from the application code); for simplicity reasons, this has not been done in the code above.
Also, a new uniform matrix, namely the “modelViewMatrix”, has been introduced in the vertex shader code; like for any other uniform parameters, the modelViewMatrix values must be set from the application code; this can be done in ImageTargets.cpp by simply adding the following line of code in the initRendering function:
modelViewMatrixHandle = glGetUniformLocation(shaderProgramID,
"modelViewMatrix");
as well as adding the following line in the renderFrame function (just before calling glDrawElements):
glUniformMatrix4fv(modelViewMatrixHandle, 1, GL_FALSE,
(GLfloat*)&modelViewMatrix.data[0] );
Let me know if something is not clear.
after changed the .obj to .h file, you will find there is no XXXIndices variables in the code