Skip to main content
GameDev.net gamedev.net
🔒 Locked 🎯 Unreal

GLSL - Uniform sampler2D problems (multi-textures/bump mapping)

Started by ClawHaMMer Sep 7, 2010 at 3:14 AM 1 replies 2.1k views
Original Post
ClawHaMMer
ClawHaMMer
I recently started learning GLSL but I'm have some problems with trying to use a normal map along side a texture for color aswell. The problem is, no matter what I specify through openGL (ie. glBindTexture() ) and no matter what I specify as the uniforms, the shader simply uses the last texture I loaded for its normal map and then for its texture.

I cannot get it to use seperate textures for the normals and then the color, it will always use the same one. here are my shaders:

Vertex:
varying vec3 normal, lightDir, eyeVec;void main(){		gl_FrontColor = gl_Color;	gl_TexCoord[0] = gl_MultiTexCoord0;	normal = gl_NormalMatrix * gl_Normal;	vec3 vVertex = vec3(gl_ModelViewMatrix * gl_Vertex);	lightDir = vec3(gl_LightSource[0].position.xyz - vVertex);	eyeVec = -vVertex;	gl_Position = ftransform();		}


fragment:
varying vec3 normal, lightDir, eyeVec;uniform sampler2D normalMap;uniform sampler2D colorMap;void main (){	vec4 base = texture2D( colorMap, gl_TexCoord[0].xy);	vec3 bump = normalize( texture2D(normalMap, gl_TexCoord[0].xy).xyz * 2.0 - 1.0);		vec4 final_color = 	(gl_FrontLightModelProduct.sceneColor * gl_FrontMaterial.ambient) + 	(gl_LightSource[0].ambient * gl_FrontMaterial.ambient);								vec3 N = normalize(normal*bump);	vec3 L = normalize(lightDir);		float lambertTerm = dot(N,L);		if(lambertTerm > 0.0)	{		final_color += gl_LightSource[0].diffuse * 		           	gl_FrontMaterial.diffuse * 					     lambertTerm *						base;//gl_Color;					vec3 E = normalize(eyeVec);		vec3 R = reflect(-L, N);		float specular = pow( max(dot(R, E), 0.0), 		                 gl_FrontMaterial.shininess );		final_color += gl_LightSource[0].specular * 		               gl_FrontMaterial.specular * 					   specular;		}	gl_FragColor = final_color;			}


and I'm loading/setting uniforms like this:

loadShader("Shaders/bump.vert.txt", "Shaders/bump.frag.txt", &shader[0]);			glUniform1i(glGetUniformLocation(shader[0], "normalMap"), texture[2]);		glUniform1i(glGetUniformLocation(shader[0], "colorMap"), texture[3]);		glUseProgram(shader[0]);


Any help would be appreciated, I just can't see why setting the uniforms seems to have no impact on the program at all, it always uses the last texture loaded.

Thanks
ClawHaMMer
ClawHaMMer
Think I've sorted it now, I wasn't using glActiveTexture() and binding them, I was handing a gl texture handle to the uniforms.

Strange problem has risen from this though, I cant render glutSolidTeapot after calling glewInit(), not sure why...
cgrant
cgrant
Another thing to look out for is when you call glUniform1i it the last argument is the texture unit the texture is going to be bound to for the sampler and NOT the texture id.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.