Shaders and Textures

Ive managed to get my shaders working with normals, color and basic positioning, but textures dont want to work so far. Ive stripped the shaders down to just the texture parts and posted them below.

The result Im getting is just black- Ive seen that it is getting through the whole bitmap loading process, and I know the bitmap file itself works fine with Opengl. Ive used the same bmp with the same loading code in another opengl program without shaders. I dont know that the texture coordinates are correct but I tried them as color to check that they are indeed holding varied information so I think they are correct. The solid black suggests to me that the texture information isnt getting through though…


texture = TextureLoad(“rock.bmp”, GL_FALSE, GL_LINEAR, GL_LINEAR, GL_REPEAT);
int texture_location = glGetUniformLocation(p, “myTexture”);
glUniform1i(texture_location, texture);

// vertex shader

void main()
{
gl_TexCoord[0] = gl_MultiTexCoord0;
gl_Position = ftransform();
}

//Fragment shader

uniform sampler2D myTexture;
void main()
{
//Texture
gl_FragColor = texture2D(myTexture, gl_TexCoord[0].st);
}

Is there any problem with these sections?

Oh… if anyone could direct me to a simple but complete project that uses textures and shaders that would also help tremendously. Im finding far too many tutorials with incomplete code- if I could simply compile working code on this, I could take it apart and find what Im missing.

glUniform1i(texture_location, texture);
This should specify the Texture unit not the texture ID.
So for example, binding the texture to Texture unit GL_TEXTURE3 you would use glUniform1i(texture_location, 3);

Huge thanks! Im still a little vague I guess on what that part is doing, but that was the problem… Once I clean it up a bit I can continue with the rest of the project finally =)

Texture units are attachment points to which you bind your textures. With the fixed function pipeline there are a maximum of 8 texture units and with shaders it’s unto 32. These limits are actually implementation specific ( you have to query GL to get the actual limits)
This means you can simultaneously mix 32 textures in one shader !
The trick however, is to link your shader to each texture unit so that GL knows which texture unit is mapped to which shader uniform. Within each shader program the slot to which each uniform is bound is queried and used as the argument to glUniform. The second argument is the texture unit holding your texture.

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