I am having a strange problem in which the behavior of my shader in rendermonkey is different than in OpenGL.
I am trying to create a simple terrain shader. In Rendermonkey this is displayed as a full terrain, where as in OpenGL it is just the first loaded image textured on the flat plane I am using. It looks like fixed function drawing.
Code to call and draw shader
virtual void Draw(SceneData* Scene)
{
Shader->Update(Scene);
glUseProgramObjectARB(Shader->m_Program);
glBindBufferARB(GL_ARRAY_BUFFER_ARB, Mesh.VBO); // for vertex coordinates
glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, Mesh.IBO);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
glTexCoordPointer(2, GL_FLOAT, sizeof(VBOVertex),(char *)NULL + sizeof(point) * 2);
glNormalPointer(GL_FLOAT, sizeof(VBOVertex),(char *)NULL + sizeof(point));
glVertexPointer(3, GL_FLOAT, sizeof(VBOVertex), (char *)NULL + 0);
glDrawElements(GL_TRIANGLES, Mesh.VertexCount, GL_UNSIGNED_INT,(char *)NULL + 0);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
glUseProgramObjectARB(0);
}
Shader->Update
void Update(SceneData* Scene)
{
glActiveTextureARB(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, m_DiffuseMapID);
glEnable(GL_TEXTURE_2D);
glUniform1iARB(m_DiffuseMapLoc, 1);
glActiveTextureARB(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, m_HeightMapID);
glEnable(GL_TEXTURE_2D);
glUniform1iARB(m_HeightMapLoc, 0);
glActiveTextureARB(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, m_LightMapID);
glEnable(GL_TEXTURE_2D);
glUniform1iARB(m_LightMapLoc, 2);
glActiveTextureARB(GL_TEXTURE3);
glBindTexture(GL_TEXTURE_2D, m_NormalMapID);
glEnable(GL_TEXTURE_2D);
glUniform1iARB(m_NormalMapLoc, 3);
glActiveTextureARB(GL_TEXTURE4);
glBindTexture(GL_TEXTURE_2D, m_SpecularMapID);
glEnable(GL_TEXTURE_2D);
glUniform1iARB(m_SpecularMapLoc, 4);
glUniform1fARB(m_HeightScaleLoc, m_HeightScale);
glUniform3fARB(m_AmbientLoc, Scene->AmbientVector[0].x, Scene->AmbientVector[0].y, Scene->AmbientVector[0].z);
glUniform4fARB(m_EyePositionLoc, Scene->CameraVector[0].Position.x, Scene->CameraVector[0].Position.y, Scene->CameraVector[0].Position.z, 0);
glUniform3fARB(m_LightVectorLoc, -Scene->LightVector[0].Position.x, -Scene->LightVector[0].Position.y, -Scene->LightVector[0].Position.z);
}
Fragment Shader
uniform sampler2D LightMap;
uniform sampler2D NormalMap;
uniform sampler2D DiffuseMap;
uniform sampler2D SpecularMap;
uniform vec3 LightVector;
uniform vec3 Ambient;
varying vec2 vTexCoord;
varying vec3 ViewDirection;
float saturate( float inValue)
{
return clamp(inValue, 0.0, 1.0);
}
void main(void)
{
vec3 Normal = texture2D(NormalMap, vTexCoord).xyz;
normalize(Normal);
normalize(LightVector);
vec3 fvLightDirection = normalize( LightVector );
vec4 temp = texture2D(SpecularMap, vTexCoord);
float transparent = (temp.x + temp.y + temp.z)/3;
float SpecularIntensity = 0.4 * transparent;
float fvTotalSpecular = pow(saturate(dot(reflect(normalize(ViewDirection), Normal), fvLightDirection)), 45.0 );//temp * ( pow( fRDotV, fSpecularPower ) );
vec3 Specular = SpecularIntensity * vec3(fvTotalSpecular, fvTotalSpecular,fvTotalSpecular);
vec4 temp1 = texture2D(LightMap, vTexCoord);
float Intensity = (temp1.x + temp1.y + temp1.z)/3;
vec4 rgb = texture2D(DiffuseMap, vTexCoord);
vec3 Colour = rgb.xyz * Intensity;
Colour += rgb.xyz * dot(Normal,-fvLightDirection);
Specular = (rgb.xyz * Specular) * dot(Normal,-fvLightDirection);
gl_FragColor = vec4((Ambient + Colour + Specular),1);
}
Vertex Shader
uniform sampler2D HeightMap;
uniform float HeightScale;
uniform vec4 EyePosition;
uniform sampler2D NormalMap;
varying vec2 vTexCoord;
varying vec3 ViewDirection;
void main(void)
{
vTexCoord = vec2(gl_MultiTexCoord0);
gl_Position = ftransform();
vec4 PositionChange = vec4(texture2D(HeightMap, vTexCoord).xyz, 0);
PositionChange.y *= HeightScale;
ViewDirection = EyePosition.xyz - (gl_ModelViewMatrix * (gl_Vertex + PositionChange)).xyz;
vec3 Normal = texture2D(NormalMap, vTexCoord).xyz;
gl_Position += (gl_ModelViewProjectionMatrix * vec4((gl_Normal * PositionChange.xyz),1));
}
I find this strange as I have other simper shaders working fine with textures.
I am using a ATi 4870HD and have checked the caps to make sure I am not over the texture limit.
Since this is my first time implementing shaders, I think I may have made a mistake when using the uniforms, which may have caused it to fall back on fixed function, but for all my looking I cannot see what would cause this not to work and my other shaders to work.
Any help or advice would be greatly appreciated.