简体   繁体   中英

OpenGL glDrawElements

Given a set of Faces with each face containing the number of vertices and a pointer to a vertex in a vector std::Vector, i want to iterate over all faces and use glDrawElements to draw each face:

Edit: I just noticed i forgot to activate the vertex_array

for(std::vector<Face>::iterator it = faces.begin();it != faces.end();++it) {
    const Face &f = *it;

    std::Vector<GLint> indices;
    std::Vector<GLfloat> positions;

    for(int i=0;i<f.vcount;++i){
        const Vertex &v = vertices[f.vertices[i]];

        positions.push_back(v.x);
        positions.push_back(v.y);
        positions.push_back(v.z);

        indices.push_back(f.vertices[i]);
    }
    glEnableClientState(GL_VERTEX_ARRAY);
    glVertexPointer(3,GL_FLOAT,3*sizeof(GL_FLOAT),&positions[0]);                     

    glDrawElements(GL_POLYGON,indices.size(),GL_UNSIGNED_INT,&indices[0]);
    glDisableClientState(GL_VERTEX_ARRAY);
    positions.clear();
    indices.clear();
}

But apparently this does not work correctly and there is nothing displayed.

Edit: Enabling the GL_VERTEX_ARRAY draws something on the screen but not the model i tried to create. So there seems to be something wrong with the addressing.

Your index array doesn't make sense. The indices glDrawElements will use just refer to the vertex arrays you have set up - and you are setting up a new array for each separate polygon.

This means that

indices.push_back(f.vertices[i]);

should be conceptually just

indices.push_back(i);

which in the end means that you could skip the indices completely and just use

glDrawArrays(GL_POLYGON,0,f.vcount);

Note that what you are doing here is a very inefficent way to render the ojects. You would be much better if you would use a single draw call for the whole object. You could do that by manually triangulating the polygons into triangles as a pre-processing step.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM