drawing model issue

Started by
1 comment, last by AbandonedAccount 10 years, 1 month ago

The problem I have kinda has to do with glDrawArrays. Basically, if I have a number of vertices that isn't divisible by 3 it won't work. It works based on triangles. I need it to work generically. If I have 4 verts I need it to draw a quad if I have 3 it needs to draw a triangle.

This is my drawModel function right now:


 
void Model::drawModel(glm::mat4& v2s_matrix, glm::mat4& w2v_matrix){



    glm::mat4 mvp = v2s_matrix * w2v_matrix * m2w_matrix;

    GLuint mvploc = glGetUniformLocation(data.file_header.shader_program, "mvp");

    glUniformMatrix4fv(mvploc, 1, GL_FALSE, &mvp[0][0]);



    glUseProgram(data.file_header.shader_program);



    for(int x = 0; x < data.file_header.object_count; x++){

        glBindBuffer(GL_ARRAY_BUFFER,vbo_list.at(x));

        if(data.model_object.at(x).object_header.coord_flag != 0){

            glEnableVertexAttribArray(0);

            glVertexAttribPointer(0,4,GL_FLOAT,GL_FALSE,sizeof(ModelVertex),(void*)COORD_OFFSET);

        }



        if(data.model_object.at(x).object_header.color_flag != 0){

            glEnableVertexAttribArray(1);

            glVertexAttribPointer(1,4,GL_FLOAT,GL_FALSE,sizeof(ModelVertex),(void*)COLOR_OFFSET);

        }



        glDrawArrays(GL_TRIANGLES,0,data.model_object.at(x).object_header.vertex_count);

        glDisableVertexAttribArray(0);

        glDisableVertexAttribArray(1);

    }



    return;

}
 

I have two basic models. A square and a triangle I am testing. 4 verts and 3 verts. This works for the triangle. For the square, it draws a triangle not including the last point.

Any suggestions would be greatly appreciated. Thanks.

Advertisement

Just convert quads to triangles when you're loading the model.

Derp

Use GL_TRIANGLE_STRIP instead of GL_TRIANGLES. This does assume your model data is set up properly for triangle strips, but it probably already is if you're using a library to import the data.

This topic is closed to new replies.

Advertisement