Domanda

I'm having some problems with glDrawRangeElements(). Basically I keep every mesh in a given model in the same vertex/index buffer, and use glDrawRangeElements() to draw them selectively (changing material and such between each draw, if I have to).

Here's the function I use to draw a mesh within a model:

void Mesh::drawMesh(uint index)
{
    if (index >= subMesh.size()) return;

    va.drawRange(subMesh[index].start, subMesh[index].end);
}

Where va.drawRange() is:

void VertexArray::drawRange(unsigned int start, unsigned int end)
{
    if (ready == false)
        return;

    /* Bind our vertex array */
    bindArray();

    /* Draw it's contents */
    if (ibo == 0)
        glDrawArrays(prim, start, (end - start) + 1);
    else
        glDrawRangeElements(prim, start, end, (end - start) + 1, iformat, NULL);

    unbindArray();
}

subMesh[i].start is the first index of the submesh. subMesh[i].end is the last index. These values are determined while the mesh is being imported (with assimp):

subMesh[i].start = (uint)indices.size();

for (uint j = 0; j < mesh->mNumFaces; ++j)
{
    aiFace& face = mesh->mFaces[j];
    indices.push_back(face.mIndices[0] + vertexOffset);
    indices.push_back(face.mIndices[1] + vertexOffset);
    indices.push_back(face.mIndices[2] + vertexOffset);
}

subMesh[i].end = (uint)indices.size() -1;

vertexOffset += mesh->mNumVertices;

This code is run for every mesh in the scene that assimp imports.

The problem I'm having is that drawMesh(0) and drawMesh(1) draws the same thing, even though subMesh[0] and subMesh[1] contain different values (specifically, subMesh[0] has start = 0 and end = 35, subMesh[1] has start = 36 and end = 71). If I draw the entire mesh at once, using glDrawElements(), it draws both submeshes properly, so I know that the vertex and index data is correct.

What am I doing wrong?

(PS. I'm using OpenGL 3.3, core profile)

È stato utile?

Soluzione

You are not using this function correctly:

glDrawRangeElements(prim, start, end, (end - start) + 1, iformat, NULL);

The second and third parameters of glDrawRangeElements() are not for specifing a sub-range of your index array. They are to tell the GL in advance which vertex indices you will use during that draw call, so they are specifying a sub-range in your vertex arrays. You should specify the part of your index array you want to render just via the count and indices parameters.

You can simply use

glDrawElements(prim, (end - start) + 1, iformat, (GLvoid*) (sizeof(yourElementType)*start) );

If you want to use glDrawRangeElements(), you should additionaly track what range of the vertex array each object uses.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top