开发者

Unable to display VBO in OpenGL

开发者 https://www.devze.com 2023-03-29 02:41 出处:网络
I have a VBO and an IBO in OpenGL, but am unable to draw them properly. Could you please let me know what I could have forgotten in the frame display function ?

I have a VBO and an IBO in OpenGL, but am unable to draw them properly. Could you please let me know what I could have forgotten in the frame display function ? - struct Point3D is a struct with 3 floats inside (x,y,z). - nbVertex is the amount of vertexes in the glVertex array. - nbVBOInd is the amount of indices in the VBOInd array.

    glGenBuffers(1, &VertexVBOID);
    glBindBuffer(GL_ARRAY_BUFFER, VertexVBOID);
    glBufferData(GL_ARRAY_BUFFER, sizeof(struct Point3D)*nbVertex, glVertex, GL_STATIC_DRAW);

    glGenBuffers(1, &IndexVBOID);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IndexVBOID);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(int)*nbVBOInd, VBOInd, GL_STATIC_DRAW);

    glBindBuffer(GL_ARRAY_BUFFER, VertexVBOID);
    glEnableClientState(GL_VERTEX_ARRAY);
    glVertexPointer(3, GL_FLOAT, sizeof(glVertex), BUFFER_OFFSET(0));   //The starting point of the VBO, for the vertices

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IndexVBOID);
    glDrawElements(GL_TRIANGLES, 3,开发者_运维技巧 GL_UNSIGNED_SHORT, BUFFER_OFFSET(0));   //The starting point of the IBO

Thanks !


I see the same problem as rodrigo - you have data type mismatch, as you can see here:

glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(int)*nbVBOInd, VBOInd, GL_STATIC_DRAW);

sizeof(int) - using integer type

glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_SHORT, BUFFER_OFFSET(0));   

GL_UNSIGNED_SHORT - using short type

according to openGL specification, there are only unsigned data types possible for glDrawElements. To fix this you need:

  • change VBOInd to unsigned type in declaration like:

    unsigned int* VBOInd = new unsigned int[nbVBOInd]

  • replace 6th call with

    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int)*nbVBOInd, VBOInd, GL_STATIC_DRAW);

  • replace 11th (last) call with

    glDrawElements(GL_TRIANGLES, nbVBOInd, GL_UNSIGNED_INT, BUFFER_OFFSET(0));

Anyway I believe that the problem is hidden in pointer setup, change 9th call to:

glVertexPointer(3, GL_FLOAT, 0, BUFFER_OFFSET(0));

If that doesn't work, please show us how glVertex and VBOInd is declared and filled with data. Maybe you're using std::vector? You need to call these data containers like:

glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int)*nbVBOInd, &VBOInd[0], GL_STATIC_DRAW);

If something's unclear, just ask in comments..


Try changing the last line to:

glDrawElements(GL_TRIANGLES, nbVBOInd, GL_UNSIGNED_INT, BUFFER_OFFSET(0));

Unless your data in the IndexVBOID are really short, but then, the sizeof(int) above would be wrong.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号