Send an image to a GLSL shader

Started by Estraven, September 09, 2007, 20:10:25

Previous topic - Next topic

Estraven

Hi,
i need to acces a normal map to do some bump mapping in a Fragment Shader.

Declaration of texture in GLSL fragment shader :
Quote
...
uniform sampler2D colorMap;
uniform sampler2D normalMap;
...

The colorMap is filled during the texture binding.
Quote
...
   glBindTexture(GL_TEXTURE_2D, textureID);
...

but i don't know how to fill the normalMap.

I tried this :
Quote
   int location = glUniformLocation("normalMap", shaderId);
   ARBShaderObjects.glUniform1iARB(location, textureID);      

but it does not work.

Any idea ?

Thanks.
Estraven

ndhb

Hi Estraven. You almost got it right but there's a bit of misunderstanding going on here too:

1) The location you supply to your shader with glUniform is a "Texture Unit" identifier, NOT the "Texture Object Name" (what you call textureID).

2) To use sample from multiple textures in your shader you have to use multitexturing. This means enabling more than one "Texture Unit". You use the call glActiveTexture for this (http://www.opengl.org/sdk/docs/man/xhtml/glActiveTexture.xml). For each texture you want to sample from in your shader, you change texture unit, then bind a texture using the familiar glBindTexture call.

In other words:

glActiveTexture(GL_TEXTURE0) // change active texture unit to number 0
glBindTexture(GL_TEXTURE_2D, colorMapTextureID); // bind the colorMap texture to the active texture unit (which is now 0)

glActiveTexture(GL_TEXTURE1) // change active texture unit to number 1
glBindTexture(GL_TEXTURE_2D, normalMapTextureID); // bind the normalMap texture to the active texture unit (which is now 1)

Now you supply the relevant "Texture Unit Identifiers" to your shader program with:

int locationColorMap = glUniformLocation("colorMap", shaderId);
ARBShaderObjects.glUniform1iARB(locationColorMap , 0); // Notice the 0 to indicate you want to sample from the texture bound to Texture Unit GL_TEXTURE0.

int locationNormalMap = glUniformLocation("normalMap", shaderId);
ARBShaderObjects.glUniform1iARB(locationNormalMap , 1); // Notice the 1 to indicate you want to sample from the texture bound to Texture Unit GL_TEXTURE1.

... and that should do it. Remember that the minimum number of Texture Units is 2 so you might want to check the current implementation at runtime before referencing GL_TEXTURE2 or higher.

kind regards,
Nicolai de Haan Brøgger



Estraven

Thanks,

I did figure it out a couple of days ago, and it works perfectly.
Still, thanks to you for those explanations, I anderstand what i did much better now.

Estraven.