0
votes

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.

1
glVertexPointer should have GL_FLOAT instead of GL_INT as second argument, and 3 * sizeof(GLfloat) as third argument. - Paweł Pela
Also, GL_POLYGON is not a valid drawing mode anymore. From the documentation: "Symbolic constants GL_POINTS, GL_LINE_STRIP, GL_LINE_LOOP, GL_LINES, GL_LINE_STRIP_ADJACENCY, GL_LINES_ADJACENCY, GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN, GL_TRIANGLES, GL_TRIANGLE_STRIP_ADJACENCY, GL_TRIANGLES_ADJACENCY and GL_PATCHES are accepted." - Paweł Pela
@poetryofruins: "Also, GL_POLYGON is not a valid drawing mode anymore" By that reasoning, glVertexPointer is also not available anymore in core profile OpenGL. So if he can use one, he can clearly use the other. - Nicol Bolas
I made the suggested changes but there is still nothing to be seen. - narain
Are you sure that your indices are correct? If they aren't coming in order from source data you have vertices in completely different positions than indices point to. Also you might be duplicating vertex data if (extremely likely) vertices is referenced multiple times in source data. - Pauli Nieminen

1 Answers

1
votes

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.