Skip to main content
GameDev.net gamedev.net
🔒 Locked

TextureBuffer coordinates - Question

Started by INsanityDesign Jul 28, 2009 at 3:06 AM 4 replies 1.6k views
Original Post
INsanityDesign
INsanityDesign
Hi This is probably the dumbest question ever but I am not able to figure it out myself. The premiss is, I have OGL-ES and I use vertex buffers with indices. Everything worked fine as long as I do not want to map a texture on it. The prior model was a simple cube. Looked fine with every color. Now I just want to add a texture to each face. So, I thought I use texturebuffer, set the glTexCoordPointer and everything will be fine. But for me it won't work. I see the texture, so texture is loaded fine but somehow I cannot figure out how the texture coordinates have to be. I already reduced it to a square but still, I see a texture but it is mirrored for each of the triangles. Because of OGLES I use GL_TRIANGLES, tried strip already, but nothing works. At the moment I do not see anything more and just tested nearly every combination I can think of. So I enabled both and set my both buffers.

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
		
glVertexPointer(3, GL_FLOAT, 0, vertexBuffer);
glTexCoordPointer(2, GL_FLOAT, 0, textureBuffer);
I have four vertices (also tested all way rounds)

-1.0f, -1.0f, 1.0f, //v0
1.0f, -1.0f, 1.0f,  //v1
-1.0f, 1.0f, 1.0f,  //v2
1.0f, 1.0f, 1.0f,   //v3  
The indices

0,1,3, 0,3,2 //f1
And now the texture coordinates

1.0f, 1.0f,
0.0f, 1.0f,
0.0f, 0.0f,
1.0f, 0.0f,
0.0f, 1.0f,
1.0f, 1.0f
This is just the last version of my tests. I actually even do not get how OGLES uses them, how many etc. Tried 2, 4, 6, 100, all way round. The results are so different. Currently with these settings the texture is there but mirrored between the two triangles. Please excuse dumb errors but I just don't get it anymore. I draw on paper many figures, tried to do everything based on these, but nothing will work. I just do not understand it. I hope anyone can help me here. Many thanks in advance.
-----The next statement is not true!The previous statement is true!
Brother Bob
Brother Bob
Your texture coordinate array is larger than the vertex array, which immediately should raise a warning flag that something is wrong. In your case, you seems to think that, while vertices are indexed according to the index array, the texture coordinates are indexed linearly in the array.

Not the case; all arrays are indexed according to the index array. If you have any attribute array enabled, that attribute array will be indexed by the index array, and each vertex entry must have exactly one corresponding attribute. Seems like your texture coordinate array should look like this.
1.0f, 1.0f,0.0f, 1.0f,1.0f, 0.0f,0.0f, 0.0f,

Four elements, which is the same size as the vertex array, and each vertex have exactly one corresponding texture coordinate.

Since you mentioned a cube, I can as well say it right away. While the cube may have shared corners, the texture coordinates at these corners are not shared. Since you must have exactly one texture coordinate for each vertex, you cannot share the vertices in the corners. Thus, the cube have 24 vertices, not 8 as one might think.
Lord_Evil
Lord_Evil
Quote:

-1.0f, -1.0f, 1.0f, //v0

1.0f, -1.0f, 1.0f, //v1

-1.0f, 1.0f, 1.0f, //v2

1.0f, 1.0f, 1.0f, //v3


The resulting quad would look like this:
v2---v3 |    | |    |v0---v1


Thus I assume the texture coords should be:
texv0 = (0.0, 0.0);
texv1 = (1.0, 0.0);
texv2 = (0.0, 1.0);
texv3 = (1.0, 1.0);

i.e.
(0/1)---(1/1)  |       |  |       |(0/0)---(1/0)
If I was helpful, feel free to rate me up ;)If I wasn't and you feel to rate me down, please let me know why!
INsanityDesign
INsanityDesign
Quote:
Original post by Brother Bob
Since you mentioned a cube, I can as well say it right away. While the cube may have shared corners, the texture coordinates at these corners are not shared. Since you must have exactly one texture coordinate for each vertex, you cannot share the vertices in the corners. Thus, the cube have 24 vertices, not 8 as one might think.


Thank you both very much, and thank you VERY VERY MUCH for this important statement. That is what got me nearly mad. I tried it with 8 vertices, shared vertices as it worked perfectly without texture. I will now try it with the according amount of vertices.

One question: Is there are "better" solution (less vertices) for OGL-ES as it do not feature e.g. GL_QUADS???

-----The next statement is not true!The previous statement is true!
Brother Bob
Brother Bob
No matter how you twist it, the cube has 24 vertices. You can merge shared vertices to reduce the number, but there are no shared vertices, so 24 is the fundamental lowest limit.

Now, that is not really the complete truth for specific cases. There may be some handful of shared vertices, depending on how you orient your textures on the individual faces. But you have to look at that from your specific case. In general though, 24 vertices is the lowest number possible for a cube. It is not a limitation related to OpenGL ES, but a general limitation with how indexed geometry works; you don't have separate indices for separate attributes.
Lord_Evil
Lord_Evil
If GL_QUADS is not supported, you still can use GL_TRIANGLES with an index buffer/array. Thus you'd have 24 vertices and 36 indices (6 faces * 2 triangles * 3 indices).
If I was helpful, feel free to rate me up ;)If I wasn't and you feel to rate me down, please let me know why!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.