SharkCop
SharkCop

Reputation: 1120

In OpenGL, how do you specify indices with a stride?

To draw in OpenGL, you specify an array of vertices, and then indices that connect together the array of vertices into a sensical order of primitives. You can do this with glDrawElements. To specify vertices, you can use glVertexPointer, in which you can specify a parameter "stride" - the gaps between contiguous elements in your array (i.e. if you store a vertex in a struct containing other data, you stride to jump past the other data).

This is great, but now I am using Assimp, which specifies its indices in a face struct.

struct aiFace {
    unsigned int* indices;
    unsigned int numIndices; 
}

Presumably, this is to support a mesh with different sized faces (a mesh with triangles and quads). Assimp is nice enough to have the option to triangulate all meshes, so I can guarantee all faces to be the same primitive.

Thus, what I really want is to be able to stride my indices, as such:

gl/*...uh...*/ElementPointer(
    3, 
    GL_UNSIGNED_INT, 
    sizeof(unsigned int) /* skip past the num_indices field*/, 
    &faces[0]);

But I can't figure out how to do that. glDrawElements assumes that indices is a contiguous array. Is there a way to do this in OpenGL?

Upvotes: 4

Views: 1658

Answers (2)

jozxyqk
jozxyqk

Reputation: 17396

Your best bet is to triangulate the mesh, so you can draw the whole thing in one go with GL_TRIANGLES (or as much as possible considering swapping materials/textures etc).

If you really want different sized polygons, it is possible. I doubt it'd be very fast and I think you'd have to use GL_TRIANGLE_FAN to do the triangulation which doesn't always give great results (eg splitting a quad along either diagonal). Anyway, the answer:

glPrimitiveRestartIndex will probably work OK with glDrawElements and GL_TRIANGLE_FAN, but really triangulating yourself is best.

glMultiDrawElements is also possible (actually very similar to your "gl...ElementPointer" idea) but probably slower than glPrimitiveRestartIndex.

EDIT Another alternative is glDrawElementsIndirect, but at first glance this may have more overhead than glPrimitiveRestartIndex.

Upvotes: 1

datenwolf
datenwolf

Reputation: 162317

Sorry, there's no support for what you're looking for built into OpenGL. However since you should copy the data as loaded by Assimp into VBO's anyway, both the vertex data and the elements array, you can do any necessary rearrangements in that step.

BTW: the stride tells the offset between start of elements, not offset between end of element to start of next element.

Upvotes: 3

Related Questions