0

I've to render a scene that include various mesh with openGL. the meshes are defined like this:

struct Mesh {
frame3f         frame;      // frame
vector<vec3f>   pos;        // vertex position
vector<vec3f>   norm;       // vertex normal
vector<vec2f>   texcoord;   // vertex texcture coordinates
vector<vec3i>   triangle;   // triangle
vector<vec4i>   quad;       // quad
Material*       mat;        // material}

the mesh can be made of triangles and quads, I try to render the vertex with this code:

for (auto mesh : scene->meshes)
{
    // bind material kd, ks, n
    glVertexAttrib3f(material_kd_loc, mesh->mat->kd.x, mesh->mat->kd.y, mesh->mat->kd.z);
    glVertexAttrib3f(material_ks_loc, mesh->mat->ks.x, mesh->mat->ks.y, mesh->mat->ks.z);
    glVertexAttrib1f(material_n_loc, mesh->mat->n);

    // bind mesh frame - use frame_to_matrix
    mat4f mesh_mat = frame_to_matrix(mesh->frame);
    glUniformMatrix4fv(mesh_frame_loc, 1, GL_TRUE, &mesh_mat[0][0]);

    // enable vertex attributes arrays
    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(1);

    //and set up pointers to the mesh data
    glVertexAttribPointer(vertex_pos_loc, mesh->pos.size(), GL_FLOAT, GL_FALSE, 0, mesh->pos.data());
    glVertexAttribPointer(vertex_norm_loc, mesh->norm.size(), GL_FLOAT, GL_TRUE, 0, mesh->norm.data());

    // draw triangles and quads
    if (mesh->triangle.size() != 0){

        glDrawElements(GL_TRIANGLES, mesh->pos.size(), GL_UNSIGNED_INT, mesh->triangle.data());
    }
    if (mesh->quad.size() != 0){
        glDrawElements(GL_QUADS, mesh->pos.size(), GL_UNSIGNED_INT, mesh->quad.data());

    }
    // disable vertex attribute arrays
    glDisableVertexAttribArray(0);
    glDisableVertexAttribArray(1);
}

} this is what i draw (don't care about colors as i don't calculate them yet) this is what i draw (don't care about colors as i don't calculate them yet)

this is what i should see if my code is correct this is what i should see if my code is correct

anyone as an idea of where is the error?

Andon M. Coleman
  • 42,359
  • 2
  • 81
  • 106
jack_the_beast
  • 1,838
  • 4
  • 34
  • 67
  • Post your actual (taken from running program, not some input files) positions, triangle and quad arrays contents (they isn't large in given example). – keltar Oct 25 '13 at 11:07
  • camera is centered in 0,0,6 thise is the code that creates meshes: – jack_the_beast Oct 25 '13 at 11:17
  • new Mesh { frame3f{ {-offset, offset,0}, x3f, y3f, z3f }, { {0,1,0}, {-1,-1,0}, {1,-1,0} }, { z3f, z3f, z3f }, { zero2f, y2f, x2f }, { {0,1,2} }, {}, new Material{one3f,zero3f,100} }, ' – jack_the_beast Oct 25 '13 at 11:19
  • new Mesh { frame3f{ { offset, offset,0}, x3f, y3f, z3f }, { {1,1,0}, {-1, 1,0}, {-1,-1,0}, { 1,-1,0} }, { z3f, z3f, z3f, z3f }, { one2f, y2f, zero2f, x2f }, {}, { {0,1,2,3} }, new Material{one3f,zero3f,100} }, – jack_the_beast Oct 25 '13 at 11:19
  • new Mesh { identity_frame3f, { {-offset,1-offset,0}, {-1-offset,-1-offset,0}, {1-offset,-1-offset,0} }, { z3f, z3f, z3f }, { zero2f, y2f, x2f }, { {0,1,2} }, {}, new Material{one3f,zero3f,100} }, – jack_the_beast Oct 25 '13 at 11:20
  • new Mesh { identity_frame3f, { {1+offset,1-offset,0}, {-1+offset, 1-offset,0}, {-1+offset,-1-offset,0}, { 1+offset,-1-offset,0} }, { z3f, z3f, z3f, z3f }, { one2f, y2f, zero2f, x2f }, {}, { {0,1,2,3} }, new Material{one3f,zero3f,100} }, },' – jack_the_beast Oct 25 '13 at 11:20

2 Answers2

0
glDrawElements(GL_TRIANGLES, mesh->pos.size(), GL_UNSIGNED_INT, mesh->triangle.data());
glDrawElements(GL_QUADS, mesh->quad.size(), GL_UNSIGNED_INT, mesh->quad.data());

Take a look at second argument on both calls. It is incorrect for quads.

glVertexAttribPointer(vertex_pos_loc, mesh->pos.size(), GL_FLOAT, GL_FALSE, 0, mesh->pos.data());

Is incorrect too - second parameter is number of components per vertex, not size of array.

keltar
  • 17,711
  • 2
  • 37
  • 42
  • sorry i've paste an old version, now it's edited on the question however i've other (more complex) scenes to render and they are a complete mess with vertex floating around averywhere, so i guess it's only luck that i can render the two triangles correctly – jack_the_beast Oct 25 '13 at 10:56
  • thanks, now the first scene looks ok. but the other scenes looks weird: looks like some vertex are not drawed.you can see the target images and my images in this [photobucket album](http://s338.photobucket.com/user/jack_the_beast/library/graphic) note that i'm using a shader written by me, it does almost nothing but i can't say if this can cause a problem.... – jack_the_beast Oct 25 '13 at 12:00
0

look like the opengl function wants the total number of vertecx to be written and not just the number of different vertex. so:

glDrawElements(GL_TRIANGLES, mesh->triangle.size()*3, GL_UNSIGNED_INT, mesh->triangle.data());
glDrawElements(GL_QUADS, mesh->quad.size()*4, GL_UNSIGNED_INT, mesh->quad.data());
jack_the_beast
  • 1,838
  • 4
  • 34
  • 67