Reputation: 371
I'm trying to draw a string in OpenGL with LWJGL from a texture atlas containing all chars next to another.
Now I'm adding for each char three vertices to the mesh, which is going to be rendered by
GL11.glDrawArrays(GL11.GL_TRIANGLE_STRIP, 0, text.getModel().getRawModel().getVertexCount());
For example I'm tyring to render the string "Thomas", that end's up as the following (with added offset to see the problem better):
As you can see, there is a strange "quad" between every char and i don't know why :-( I think that I'm adding to many vertices per char, but i got no idea to do it better...
Vertices (2D):
-0.3333333, 0.5875,
-0.24800003, 0.5875,
-0.3333333, 0.25,
-0.24800003, 0.25,
-0.11466664, 0.5875,
-0.029333353, 0.5875,
-0.11466664, 0.25,
-0.029333353, 0.25,
0.10399997, 0.5875,
0.18933332, 0.5875,
0.10399997, 0.25,
0.18933332, 0.25,
0.32266665, 0.5875,
0.408, 0.5875,
0.32266665, 0.25,
0.408, 0.25,
0.5413333, 0.5875,
0.62666667, 0.5875,
0.5413333, 0.25,
0.62666667, 0.25,
0.76, 0.5875,
0.84533334, 0.5875,
0.76, 0.25,
0.84533334, 0.25,
Texture Coords (2D):
0.23318386, 1.0,
0.23766816, 1.0,
0.23318386, 0.0,
0.23766816, 0.0,
0.32286996, 1.0,
0.32735425, 1.0,
0.32286996, 0.0,
0.32735425, 0.0,
0.3542601, 1.0,
0.35874438, 1.0,
0.3542601, 0.0,
0.35874438, 0.0,
0.34529147, 1.0,
0.3497758, 1.0,
0.34529147, 0.0,
0.3497758, 0.0,
0.29147983, 1.0,
0.29596412, 1.0,
0.29147983, 0.0,
0.29596412, 0.0,
0.3721973, 1.0,
0.37668163, 1.0,
0.3721973, 0.0,
0.37668163, 0.0
Upvotes: 4
Views: 4610
Reputation: 396
If you want to render disjoint primatives with TRIANGLE_STRIP
, you need to include degenerate (zero-area) triangles between your disjoint prims. You can do this by repeating the vertices you want to make disjoint:
-0.3333333, 0.5875,
-0.24800003, 0.5875,
-0.3333333, 0.25, // real triangle
-0.24800003, 0.25, // real triangle
-0.24800003, 0.25, // ignored degenerate triangle
-0.11466664, 0.5875, // ignored degenerate triangle
-0.11466664, 0.5875, // ignored degenerate triangle
-0.029333353, 0.5875, // ignored degenerate triangle
-0.11466664, 0.25, // real triangle
-0.029333353, 0.25, // real triangle
-0.029333353, 0.25, // ignored degenerate triangle
...
Degenrate prims are optimized away well enough that you can disregard them.
Note however that has basically no advantage for rendering disjoint quads, since you only save two vertices total over just using TRIANGLES
, regardless of number of quads.
This technique is more useful when disjoint 'jumps' are less common, such as for tri-strip-optimized meshes.
Upvotes: 1
Reputation: 3078
Looking at how you laid out your vertices, it looks like GL_TRIANGLE_STRIP
is not going to work. Let's label your vertices with the numbers 0 through 24. The triangle strip topology will draw triangles like this:
draw triangle 0 1 2
draw triangle 1 2 3
draw triangle 2 3 4
...
draw triangle 20 21 22
draw triangle 21 22 23
What you probably want is this:
draw triangle 0 1 2
draw triangle 1 2 3
draw triangle 4 5 6
draw triangle 5 6 7
draw triangle 8 9 10
draw triangle 9 10 11
...
...
draw triangle 20 21 22
draw triangle 21 22 23
This can be accomplished with the GL_TRIANGLES
topology and using an indices buffer like this:
[ 0 1 2 1 2 3 4 5 6 5 6 7 ... 20 21 22 21 22 23 ]
Upvotes: 2