0
\$\begingroup\$

I have an Android OpenGL ES 2 app that loads an .OBJ model using Assimp and renders the same.

The vertices data is stored in a object declared like this :

class CGameObject
{
public:
struct _Mesh {
    std::string Name;               // Name of Mesh

    std::vector<float> vertices;    // vertices of Mesh

    GLuint m_vbo;
    // TODO: Add TEXTURE and MATERIAL support
};
...
}

Set various Matrices :

cameraX = 3.0f; cameraY = 3.0f; cameraZ = 5.0f;

// Camera/View matrix
View = glm::lookAt(
    glm::vec3(cameraX, cameraY, cameraZ),     // Camera position
    glm::vec3(0, 0, 0),     // Pointing at
    glm::vec3(0, 1, 0)      // Angle
 );

aspect = (float) width / height;

// Projection Matrix
Projection = glm::perspective(45.0f, aspect, 0.1f, 10000.0f);

glm::mat4 Model = glm::translate(mat4(1.0), glm::vec3(0.0f, 0.0f, 0.0f));
MVP = Projection * View * Model;

Rendering function :

void GLEngine::DrawFrame() {
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

glUseProgram(programObject);
// Draw of VBO ---BEGIN
for (GameObjects::iterator i = gObjects.begin(); i != gObjects.end(); ++i)
{
    CGameObject* pObj = *i;
    /////////////////
    for (int x = 0; x < pObj->Meshes.size(); x++)
    {
        glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]);
        // bind VBO you want to draw
        glBindBuffer(GL_ARRAY_BUFFER, pObj->Meshes[x]->m_vbo);

        glEnableVertexAttribArray(gvPositionHandle);
        glVertexAttribPointer(gvPositionHandle, 3, GL_FLOAT, GL_FALSE, 0, 0);

        if (x == 0){
            glVertexAttrib3f(gvColorHandle, 0.0f, 0.0f, 1.0f);
            UcheckGLError("glVertexAttrib3f");

            }
        else {
            glVertexAttrib3f(gvColorHandle, 0.0f, 1.0f, 0.0f);
            UcheckGLError("glVertexAttrib3f");

            }

        //glEnableVertexAttribArray(gvColorHandle); // My app crashes if I uncomment this line . .. any reasons why ?
        //UcheckGLError("glEnableVertexAttribArray");

        glDrawArrays(GL_TRIANGLES, 0, pObj->Meshes[x]->vertices.size());
        // unbind VBO
        glBindBuffer(GL_ARRAY_BUFFER, 0);
    }
    /////////////////
}
// Draw of VBO ---END
// unbind program
glUseProgram(0);
}

source for shaders -

vertex shader :

attribute vec3 in_Position;

uniform mat4 MVP;

attribute vec3 in_Color;
varying vec4 fColor;

void main(void) {
    vec4 v = vec4(in_Position, 1.0);
    gl_Position = MVP * v;
    fColor = vec4(in_Color, 1.0);

}

Fragment shader :

varying vec4 fColor;

void main() {
    //gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
    gl_FragColor = fColor;
}

But the results is not what I expect. Expected : Expected results

What I get : enter image description here

Loading of the Assimp model is provided in this question : https://gamedev.stackexchange.com/questions/85808/problem-rendering-simple-obj-model-using-assimp-in-android-app

\$\endgroup\$
2
  • \$\begingroup\$ looks like you are rendering an object too many, I can see the 4 cubes you want and then some more objects appeared \$\endgroup\$ Commented Oct 15, 2014 at 10:27
  • \$\begingroup\$ Yes, but how come extra stuff is being drawn ? I have proper loop condition in place. \$\endgroup\$ Commented Oct 15, 2014 at 10:31

1 Answer 1

1
\$\begingroup\$

you problem is with the following call:

glDrawArrays(GL_TRIANGLES, 0, pObj->Meshes[x]->vertices.size());

you need to pass in the amount of vertices you are drawing aka pObj->Meshes[x]->vertices.size() / 3

\$\endgroup\$
1
  • \$\begingroup\$ You are right. It worked ! \$\endgroup\$ Commented Oct 15, 2014 at 10:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .