Jump to content
  • Advertisement
Sign in to follow this  
Mr.A

glDrawElements (SEGAULT)

This topic is 1335 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,

So I wrote a simple .obj models parser, and attempted to render a simple cube model with indices. Everything seems to be working fine, until I come to render the cube with a glDrawElements() call.. it crashes returning -1073741819. I've been struggling with this for a day now, so I'd really appreciate if you'd help me with this.

Here is my code:

 

Loading the stuff:

    vector<Vertex> CubeModel, CubeNormals;
    vector<GLuint> CubeIndices;
    vector<TexCoord> CubeTexCoords;
    LoadObjModel("simple cube.obj", CubeModel, CubeTexCoords, CubeNormals, CubeIndices); 

    GLuint CubeId = InitializeVBOs(&CubeModel, 8);
    GLuint CubeIndicesId = InitializeIBOs( &CubeIndices, 36);

The InitializeVBOs/IBOs functions:

GLuint InitializeVBOs(std::vector<Vertex> * vertices, int number)
{
    GLuint VBOid;
    glGenBuffers(1, &VBOid);
    glBindBuffer(GL_ARRAY_BUFFER, VBOid);
    glBufferData(GL_ARRAY_BUFFER, number*sizeof(Vertex), vertices, GL_STATIC_DRAW);
    return VBOid;
}

GLuint InitializeIBOs(vector<GLuint> * indices, int number)
{
    GLuint IBOid;
    glGenBuffers(1, &IBOid);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBOid);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, number*sizeof(GLuint), indices, GL_STATIC_DRAW);
    return IBOid;
}

The drawing part:

        glEnableClientState(GL_VERTEX_ARRAY);

            glBindBuffer(GL_ARRAY_BUFFER, CubeId);
            glVertexPointer(3, GL_FLOAT, 0, NULL);
            glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, CubeIndicesId);

            glDrawElements( GL_TRIANGLES, 36, GL_UNSIGNED_INT, NULL ); //there are 36 indices; 3 for each triangle making half a face

        glDisableClientState(GL_VERTEX_ARRAY);
        glBindBuffer(GL_ARRAY_BUFFER, 0);
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

Here is the .obj file content:

http://pastebin.com/zcQpy7sR

 

I am pretty sure the .obj parser works correctly. Feel free to compare the values with the watch values of the arrays (I am aware the indices values are subtracted by 1):

Content of "CubeModel" (vertices): http://prntscr.com/68emn7

Content of "CubeIndices": http://prntscr.com/68emcn

 

If I am missing any other info, let me know and I will provide them immediately. Thanks in advance!

Edited by Mr.A

Share this post


Link to post
Share on other sites
Advertisement

You're passing pointers to the vectors to glBufferData, not the pointer to the data the vector contains. Also, pass the vectors by reference instead and skip the size parameter; the vector know its own size.

GLuint InitializeVBOs(std::vector<Vertex> &vertices)
{
    GLuint VBOid;
    glGenBuffers(1, &VBOid);
    glBindBuffer(GL_ARRAY_BUFFER, VBOid);
    glBufferData(GL_ARRAY_BUFFER, vertices.size()*sizeof(Vertex), vertices.data(), GL_STATIC_DRAW);
    return VBOid;
}

Share this post


Link to post
Share on other sites

It worked! Still not sure why it worked though.

https://www.opengl.org/sdk/docs/man3/xhtml/glBufferData.xml

According to that page, we should pass a pointer to the function (?).

 

Edit: So you're saying the pointer to the vector is different from the pointer to the first element in the vector per se? If so, then what data does it point to?

 

Thanks!

Edited by Mr.A

Share this post


Link to post
Share on other sites

The vector object itself is just a small class that stores a few pointers, such as a pointer to the actual data you put into it, and some internal book-keeping information for things such as how much memory has been allocated, how much is actually used, and so on. You want the pointer to the data stored by the vector, not a pointer to the vector object itself. A pointer to the vector object just points to its internal private data.

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!