Strange Shader Behaviour

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)
		glBindBufferARB(GL_ARRAY_BUFFER_ARB, Mesh.VBO);         // for vertex coordinates


		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);




	void Update(SceneData* Scene)
		glBindTexture(GL_TEXTURE_2D, m_DiffuseMapID);
		glUniform1iARB(m_DiffuseMapLoc, 1);	

		glBindTexture(GL_TEXTURE_2D, m_HeightMapID);
		glUniform1iARB(m_HeightMapLoc, 0);

		glBindTexture(GL_TEXTURE_2D, m_LightMapID);
		glUniform1iARB(m_LightMapLoc, 2);

		glBindTexture(GL_TEXTURE_2D, m_NormalMapID);
		glUniform1iARB(m_NormalMapLoc, 3);

		glBindTexture(GL_TEXTURE_2D, m_SpecularMapID);
		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;
   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 = * Intensity;
   Colour += * dot(Normal,-fvLightDirection);
   Specular = ( * 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  = - (gl_ModelViewMatrix * (gl_Vertex + PositionChange)).xyz;
   vec3 Normal = texture2D(NormalMap, vTexCoord).xyz;

   gl_Position += (gl_ModelViewProjectionMatrix * vec4((gl_Normal *,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.

Well, it’s probably obvious to you what’s likely going on here. For some reason, your app is rendering the height map (texture unit 0) as an intensity texture rather than using it to displace the vertices.

One possibility is that your shader is not really active though you think it is. Check for GL errors, or use gDEBugger or similar.

Another possibility: let’s see the code that determines m_HeightMapLoc, m_HeightMapID, and friends. Maybe you’re binding the wrong textures to the wrong uniforms.

Also, these directives:


are no-ops when you’re using shaders (specifically a fragment shader). Change these to glDisable’s. If you see any difference in the output, you’re not rendering with your shader – you’re rendering with the fixed function pipeline. In any case, you should remove these. Legacy stuff for controlling the fixed function pipe.

Also, change your frag shader to always output red (1,0,0,1), or change your vertex shader to output a wildly different position – basic kick-the-tires test to see if your shader program is properly active and being executed.

One thing that is definitely not good:


and within Update, uniforms are set. Since the program is not active, those functions will not affect the uniforms of Shader->m_program.

There are 2 different ways to fix it:

change the above block to:



Use glProgramUniform in Update() to directly set the values of the uniforms (these functions take as argument the name of the program as well).

Also, I would strongly suggest not using the ARB_shader_objects functions. Just use the core OpenGL 2.0 functions. The reason is that there were some non-trivial changes between ARB_shader_objects and core OpenGL 2.0.

Thank you all for the help, I am currently at work but I will try and apply these changes once I get back.

I am slightly confused to why ARB_Shader_objects are still provided (and much easier to find out about through google) if OpenGL 2.0 already had the functions built in?

I am slightly confused to why ARB_Shader_objects are still provided (and much easier to find out about through google) if OpenGL 2.0 already had the functions built in?

I don’t know what Google searches you’re using to find ARB_shader_objects, but there were at least some applications released between the time ARB_shader_objects came out and OpenGL 2.0. OpenGL implementations therefore must expose ARB_shader_objects in order to support that software.

The code to update textures and variables is:

	virtual void Update()
		CTerrainShader* Ter = (CTerrainShader*)Shader;
		Ter->m_HeightMapID = m_Height;
		Ter->m_LightMapID = m_Light;
		Ter->m_NormalMapID = m_Normal;
		Ter->m_SpecularMapID = m_Specular;
		Ter->m_HeightScale = m_HeightScale;

Switching these two around:


and switching to ‘glDisable(GL_TEXTURE_2D)’ has caused it to become the same as rendermonkey except all white. So I think the vertex shader must be working ok.

I am looking into seeing if it is just my lighting that may be causing that.

It is a problem with lighting washing it out. I think the main problem has been sorted, just need to tidy the code and switch it around a bit and sort out my lighting. Thank you all

Really didnt pay attention to my lighting…

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.