[SOLVED] Texturing VBOs

I don’t seem to understand how texturing works for VBOs. It was easy back then when I used immediate mode (glTexCoord2f …) but you can of course not apply the same to VBOs.

I do know that you need to create own buffers / add the texture coordinates to a buffer but I can not really figure out how these coordinates actually work / look like. When I used immediate mode it was a little bit like this:

glBindTexture( ... );

glTexCoord2f(1f,0);
glVertex2d(x + 50, y);
// ...

I guess that I can also use TEXTURE_2D in this case.

This is what I got in my vertex buffer (just the normal buffer which contains the coordinates of my quad):

-0.5f,-0.5f, 0f,	0.5f,-0.5f,0f,		0.5f,0.5f,0f,	-0.5f,0.5f,0f

(Yes, the quads are visible so there shouldn’t be any problem)

My questions now are:

[o] How would the texture coordinates for this quad look like(This all happens in 3D - space)?
[o] At which place do I have to call glBindTexture( … )?

I do know that you need following piece of code to enable texture coordinates:


glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glBindBufferARB(GL_ARRAY_BUFFER_ARB, vboTextureHandle);
glTexCoordPointer(3, GL_FLOAT, 0, 0);

Thanks in advance.


Note:

I have used the search function but I couldn’t find anything that helped me out.

Same as texture coords in immediate mode: two-dimensional, between 0.0 and 1.0 inclusively, assuming you have a 2d texture.

Any time before you draw it.

Silly question: you did enable textures, right? And you don’t have a fragment shader otherwise taking over texturing?

Maybe your problem is, that you don’t quite understand, how to “store” these Texture coords.

There are 2 ways:

  • use 2 VBOs, one for Vertices and one for Texture coords (good for starting up with em):
    call glTextureCoordPointer just like the vertex pointer, but with the other VBO and buffer.

  • use 1 “Interleaved” VBO (like written in the bottom of the article on the lwjgl wiki), where the buffer looks like this:

(vertices) -0.5, 0.5, 0.5 (textureCoords) 0.0, 0.0 (vertices) 0.0, 0.0, 0.0 (textureCoords) 1.0, 0.0 ...

Then you would need to operate with the offsets and the “stride”.

I do understand how to use them and that I have to store them in a VBO. And yes, I did enable TEXTURE_2D. My main problem however is that I don’t exactly understand what the float values ranging from 0.0 - 1.0 mean.

Would the coordinates for this quad

-0.5f,-0.5f, 0f,   0.5f,-0.5f,0f,      0.5f,0.5f,0f,   -0.5f,0.5f,0f

look like this?:

0f,0f,    1f, 0f,      1f,1f      0f, 1f

I have just found the problem. The size of the TexCoordPointer was 3 instead of 2. Stupid mistake I guess.
Anyway, thanks for the quick help!

If you’re making a 2D game, it would be easier to specify vertex positions in 2D orthographic screen space, for e.g. (0, 0) being top left, rather than world space.

You might like to read up on texturing a bit, as it’s not always as simple as just using 0.0 or 1.0. (i.e. for sprite atlases, or non-power-of-two images packed in a power-of-two-texture, or what have you).
http://www.arcsynthesis.org/gltut/Texturing/Texturing.html

And since you’re already learning modern techniques, IMO you should try learning pure OpenGL 3+ ways of doing things (i.e. custom GLSL attributes and glVertexAttribPointer instead of glTexCoordPointer).

I am actually playing around a little bit with 3D games. Thanks for the link, I kinda knew that it can’t always just be 1 or 0, eventhough it works now.

About using OpenGL 3+: I heard that it is not supported by most of the older computers, what is of course not what I want. If I create games, I want them to be available for a wide range of players. Anyway, GLSL seems to be quite interesting and I will for sure take a look at them later, but currently I want to ‘fix’ my knowledge with normal VBOs.

If your target supports VBOs, there is a very good chance it will also support shaders. The code for binding and setting up a shader looks the same; the only difference is that you’re using custom attributes (“a_Texcoord0”) instead of builtins (“gl_MultiTexCoord0”). The best way to “fix” your knowledge of VBOs is to learn how to use them without relying on deprecated code. :slight_smile:

Oh, I didn’t know that. I will take a close look into them asap. Thanks for clearing me up!