Passing a texture

I’m not entirely sure if I’m doing everything correct…
All I want is to load a texture using GLSL and apply it on a surface of points. Shouldn’t be too difficult right?
This is what I have:


Vertex shader:

varying vec2 texture_coordinate;
void main()
{
    texture_coordinate = gl_MultiTexCoord0.xy;
    gl_Position = ftransform();
}

Fragment shader:

varying vec2 texture_coordinate;
uniform sampler2D tex;

void main (void)
{
    gl_FragColor = texture2D(tex, texture_coordinate);
}

Opengl:

...
unsigned char *bitmap_data = NULL;
int my_sampler_uniform_location = glGetUniformLocation(ProgramObject, "tex");
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, buffers[0]);
loadBitmap("picture.bmp", bitmap_data);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, bitmap_data);
glUniform1iARB(my_sampler_uniform_location, 0);


Assume I’ve used genTextures on buffers beforehand, that the shaders have been compiled properly, and the loadBitmap method has been tested and works fine.

The result I get is the texture does not render, instead it turns out all white. Have I missed something out?

Have you actually enabled the shader for use again after compiling etc.?
It’s needs to be active and enabled when you bind the Uniform and the texture.
Other than that it looks ok to me.

You might have to setup sampler states for the texture I think:

(do this after you use glTexImage2D)

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

And perhaps other sampler states such as:
GL_TEXTURE_MAG_FILTER
GL_TEXTURE_WRAP_S
GL_TEXTURE_WRAP_T
(I think these have default values that work though)

Check the following:
-glGetError() after each line: there is probably error somewhere
-activation of a shader before drawing
-have the shaders linked properly
-‘my_sampler_uniform_location’ is a valid uniform ID
-passing correct vertex attributes: position & texcoord
-setting correct matrices: modelview & projection
-clear color & depth, depth check & write

Thanks for the replies.

Gone through the points, this is what I’ve got:

  1. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); <- no difference
  2. glGetError(); <- no errors
  3. shaders work properly. I can for e.g. have gl_FragColor = vec4(0,1,0) and my “terrain” will turn green. This means - shaders have been activated, & shaders are linked properly.
  4. my_sampler_uniform_location <- from my code in the first post, does it look right? :stuck_out_tongue:
  5. The position & texcoords are most probably wrong. I need the steps for this…do I require glTexCoord2f when using shaders?
  6. Matrices look fine, I can see the terrain.
  7. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); <- done
  1. On each instruction? all right then
  2. You test the shader linkage by modifying ti and see the result? hah, it’s not the right way. You need to call glGetObjectParameteriv followed by glGetInfoLog and print this log some where to be sure that your original shaders are linked properly.
  3. Your code is correct, but I’m asking about actual returned value of glGetUniformLocation.
  4. For immediate mode - yes, you need to call glTexCoord*. I prefer VBO way (which may be a little more difficult to setup, if you didn’t use it)
  5. Ok
  6. Ok
  1. Oh alright…forgot about that
  2. What should I expect? An address? One sec…
  3. I’m using multiple clipmaps stored in separate VBOs, so it may be a bit troublesome.

Thanks heaps :smiley:

Post your code here to show us whats going on on your end.

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