View Full Version : Problem using glUniform1iARB to set texture in GLSL?

03-16-2006, 02:38 AM

I'm trying to create a simple shader that applies a texture onto a plane surface... I've verified that the shader is compiled and linked correctly (and works as expected in ShaderDesigner).
I think my problem is using the glUniform1iARB funktion, or simply transfering the data correctly.
It seem like the texture data is read as zero in the shader.

void setShader()
// compile and link shader (works correctly)

int error;
textureLocation = glGetUniformLocationARB(shaderProgram, "texture");
error = glGetError();

// Init texture data
GLubyte *pTexture = new GLubyte[TEXTURE_HEIGHT*TEXTURE_WIDTH*3];

// Init texture
for(int i=0; i<TEXTURE_WIDTH*TEXTURE_HEIGHT; i++) {
pTexture[i*3] = 0; // R
pTexture[i*3+1] = 255; // G
pTexture[i*3+2] = 255; // B

// Create and Bind texture on Graphics Card
glGenTextures(1, &amp;texture);
glBindTexture(GL_TEXTURE_2D, texture); // Binds the texture
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, TEXTURE_WIDTH, TEXTURE_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, pTexture); // inits the texture
glBindTexture(GL_TEXTURE_2D, 0);
delete pTexture;

int DrawGLScene(GLvoid)
enableShader(); // Enables my shader
if(textureLocation != -1) // check if variable exists in linket shader program
glUniform1iARB(textureLocation, texture);

glTranslatef(-1.2f,0.0f,0.0f); // Move to the left
glBegin(GL_QUADS); // Draw A Quad
glTexCoord2f( 0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 0.0f);
glTexCoord2f( 0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 0.0f);
glTexCoord2f( 1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 0.0f);
glTexCoord2f( 1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 0.0f);
return TRUE;

// VS
void main()
gl_TexCoord[0] = gl_MultiTexCoord0;
gl_Position = ftransform();

// FS
uniform sampler2D texture;
void main()
vec4 texelColor0 = texture2D(texture, gl_TexCoord[0].xy);
gl_FragColor = texelColor0;
}Can anybody help me with this (maybe simple problem)... Oh, my graphics card is an ATI X600 Mobility


03-16-2006, 03:51 AM
Your problem is in glUniform1iAR(textureLocation, texture). You should not specify id of texture object here. The value for sampler uniform represents index of texture unit to which desired texture is bound.

03-16-2006, 11:37 PM
I see my problem... Thanks!

04-02-2006, 04:32 PM
if you use an only texture, glUniform is not necessary to set a sampler uniform, when call glBindTexture(GL_TEXTURE_2D, texture); OpenGL knows the current texture in use. For using multiple textures use glActiveTexture for change textures and avoid calling glUniform within your rendering function