• Advertisement
Sign in to follow this  

obj doesn't draw

This topic is 1670 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello

I'm using a obj loader in my ios app but it doesn't draw.

 

Here's the code i use to bind the buffers

cub.import("coloredcube.obj");
    
    
    for(int i=0;i<cub.getNumberOfVertices();i++)
        NSLog(@"%f %f %f",cub.getVertexBuffer()[i].position[1],cub.getVertexBuffer()[i].position[0],cub.getVertexBuffer()[i].position[2]);
        
    
    for(int i=0;i<=cub.getNumberOfIndices();i+=3)
        NSLog(@"%d %d %d",cub.getIndexBuffer()[i],cub.getIndexBuffer()[i+1],cub.getIndexBuffer()[i+2]);
    
    
    glGenBuffers(1, &_vertexBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(cub.getVertexBuffer()), cub.getVertexBuffer(), GL_STATIC_DRAW);
    
    glGenBuffers(1, &_indexBuffer);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(cub.getIndexBuffer()), cub.getIndexBuffer(), GL_STATIC_DRAW);
    
    
    glEnableVertexAttribArray(GLKVertexAttribPosition);
    glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(ModelOBJ::Vertex), (const GLvoid *) offsetof(ModelOBJ::Vertex, position));

and to draw i use

 glBindVertexArrayOES(_vertexArray);  
 glDrawElements(GL_TRIANGLES, cub.getNumberOfIndices(), GL_UNSIGNED_INT, 0);

 

the the buffers returned by the ModelOBJ class are

 std::vector<Vertex> m_vertexBuffer;

 std::vector<GLuint> m_indexBuffer;

The loader seems to work as i output the vertexs in nslog

 

if i use some static coordinates binded to buffers it works

typedef struct {
    float Position[3];
    float Color[4];
} Vertex;

const Vertex Vertices[] = {
    {{1, -1, 0}, {1, 0, 0, 1}},
    {{1, 1, 0}, {0, 1, 0, 1}},
    {{-1, 1, 0}, {0, 0, 1, 1}},
    {{-1, -1, 0}, {0, 0, 0, 1}}
};

const GLubyte Indices[] = {
    0, 1, 2,
    2, 3, 0
};

What could be the problem?

Edited by adrian_diesel

Share this post


Link to post
Share on other sites
Advertisement

Probably because sizeof(cub.getVertexBuffer()) and sizeof(cub.getIndexBuffer()) don't do what you think they do. Since cub.getVertexBuffer() and cub.getIndexBuffer() return pointers, the sizeof() is giving you the size of a pointer, not the size of the buffer. You need to pass in the actual size of the buffers.

Share this post


Link to post
Share on other sites

Thank you , changed to

 glGenBuffers(1, &_vertexBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
    glBufferData(GL_ARRAY_BUFFER, cub.getVertexSize()*cub.getNumberOfVertices(), &cub.getVertexBuffer()[0], GL_STATIC_DRAW);
    
    glGenBuffers(1, &_indexBuffer);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, cub.getIndexSize()*cub.getNumberOfIndices(), &cub.getIndexBuffer()[0], GL_STATIC_DRAW);
  

 

and it works

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement