OpenGL - Associate Texture Coordinates Array With Index Array Rather Than Vertex Array?
Asked Answered
D

3

22

Whenever we use an index array to render textured polygons with glDraw*Elements*, we can provide an array of vertices and an array of texture coordinates. Then each index in the index array refers to a vertex at some position in the vertex array and the corresponding texture coordinate at the same position in the texture array. Now, if for instance several separate primitives (like QUADS) share one vertex, but require different texture coordinates for that vertex, we have to duplicate that vertex in our array as many times as we have different texture coordinates for it. Therefore, it would be much more convenient if the texture coordinate array could be associated with the positions in the index array. That way no vertex duplication would be necessary to associate one specific vertex with different texture coordinates.

Is this possible? If yes, what syntax to use?

Dmso answered 17/10, 2011 at 18:51 Comment(8)
How would you check for ambiguity? A vertex with a different uv coordinate is a different vertex.Boom
no ambiguity should arise, since different positions in the index array automatically create "different" vertices. That way one could upload a whole 3D map of vertices into the vertex array and use these discrete points in space to draw anything into it without ever to have to update the actual vertex data.Dmso
I have been thinking: You would need a uv-array that is as large as your index array. While else, your uv-array would be as long as your vertex array. Most of the time, I would assume that the vertex array is shorter than the index array. But yes, I would give you the right to do it, but it seems very likely that it isn't in opengl.Boom
The point is, if discrete vertices are used by several primitives very often, it would be much better to have only a longer index array, than to have longer index AND vertex arrays. I really hope that this is possible in OpenGL. Let's wait and see what the gurus have to say! =)Dmso
But if you would use this method, you would need a longer uv-array right? Suppose your index array is 1,1,1,1,1,1,1,0. Then you would need separate uv-coordinates for every entry, instead of just 0 and 1.Boom
@Marnix: If your index array is 1,1,1,1,1,1,1,0, then you're not drawing any triangles. All of those triangles have 0 area, so OpenGL will just cull them out.Boyett
@NicolBolas Haha, ah yes you're right. But still, if it would be 1111102, the same comment would stand.Boom
possible duplicate of Rendering meshes with multiple indicesBoyett
B
11

No. Not in a simple way.

You could use buffer textures and shader logic to implement it. But there is no simple API to make attributes index the way you want. All attributes are sampled from the same index (except when instanced array divisors are used, but that won't help you either).

Note that doing this will be a memory/performance tradeoff. Using buffer textures to access vertex data will take up less memory, but it will be significantly slower and more limiting than just using regular attributes. You won't have access to normalized vertex attributes, so compressing the vertex data will require explicit shader logic. And accessing buffer textures is just slower overall.

You should only do this if memory is at a premium.

Boyett answered 17/10, 2011 at 19:7 Comment(6)
Very unfortunate... :/ But thank you for your answer! I hope these buffer textures were introduced in OpenGL 3.0 or earlier! Otherwise, I'm screwed! =)Dmso
@Fejwin: The page I linked you to tell you exactly when buffer textures came into OpenGL. It's on the right-side of the page; I don't know how you missed it. Also, see my edit.Boyett
Yes you are right! I had not visited that link yet when I was writing. But more importantly, where can I submit improvement suggestions to the Architectural Review Board?Dmso
It seems the buffer textures approach does not pay off, thank you for the clarification.Dmso
@Fejwin: "where can I submit improvement suggestions to the Architectural Review Board" People have been asking for functionality like this for over a decade. It isn't happening; it's best to accept this and move on.Boyett
Eh, alright then. Guess these people know very well on their own what they should and what they should not do.Dmso
I
6

Now, if for instance several separate primitives (like QUADS) share one vertex, but require different texture coordinates for that vertex, we have to duplicate that vertex in our array as many times as we have different texture coordinates for it.

If the texture coordinates differ on primitives sharing a vertex position, then the vertices at a whole are not shared! A vertex is a single vector consisting of

  • position
  • normal
  • texture coordinate(s)
  • other attributes

You alter any of these, you end up with a different vertex. Because of that vertex sharing does not the way you thought.

Intrinsic answered 17/10, 2011 at 19:40 Comment(3)
That is correct in OpenGL, as it turns out. But mathematically two quads can very well share a vertex. Maybe that functionality will be available in the future...Dmso
Fejwin: This is the terminology of OpenGL and GPUs in general. Also it's very unlikely to change, since the way it's currently defined is the way, GPUs can digest the data the best. It's all about vertex caching and cache locality.Intrinsic
I see. So the reasons are more fundamental in terms of implementation. Good to know, thank you!Dmso
R
0

You can duplicate the vertices so that 1 has 1 texture coord & the other has the other. The only downfall of that is if you need to morph the surface - you may move 1 vertex but not both. Of course it is possible to do it "imperatively" - ie when you just run thru a loop & use different texture coord as you go - but that would not be VBO & much slower

Relly answered 15/1, 2016 at 20:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.