Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Page 1 of 4 123 ... LastLast
Results 1 to 10 of 37

Thread: VBO and IBO

  1. #1
    Intern Contributor
    Join Date
    Jun 2009
    Posts
    90

    VBO and IBO

    glEnableClientState(GL_VERTEX_ARRAY);

    GLuint VertexVBOID[2];
    glGenBuffers(1, VertexVBOID);
    glBindBuffer(GL_ARRAY_BUFFER, VertexVBOID[0]);
    glBufferData(GL_ARRAY_BUFFER, ((VertexTotal*3) * sizeof(float)), electrodexyz, GL_STATIC_DRAW);

    GLuint IndexVBOID;
    glGenBuffers(1, &IndexVBOID);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IndexVBOID);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, (VertexTotal) * sizeof(unsigned int), indices, GL_STATIC_DRAW);

    glVertexPointer(3, GL_FLOAT, 0, 0);
    glDrawElements(GL_TRIANGLES, (VertexTotal), GL_UNSIGNED_INT, 0);
    I'm currently using this to render a model of 50,000 vertices. The problem is that when rendering I get this output, rather than a brain model. I believe the indices must be wrong, but I can't determine how. When importing the model I add each x,y,z coord to an array of 3 times the number of vertices (for x,y,z).



    Then for each vertex I add count++ to the value of the indices, example array[0] = 1; array[1] = 2; array[n] = n+1;

    Am I approaching using VBO's and IBO's wrong?

    Help is greatly appreciated

  2. #2
    Advanced Member Frequent Contributor
    Join Date
    Apr 2003
    Posts
    666

    Re: VBO and IBO

    glBufferData(GL_ELEMENT_ARRAY_BUFFER, (VertexTotal) * sizeof(unsigned int), indices, GL_STATIC_DRAW);
    VertexTotal sounds wrong to me in this line. Shouldn't that be something like "numFaces*3" or similar?

  3. #3
    Intern Contributor
    Join Date
    Jun 2009
    Posts
    90

    Re: VBO and IBO

    VertexTotal actually is computing the amount of polygons on the model. I mistakenly, originally, named it VertexTotal.

    Should my bufferdata parameters of the VBO be different?

  4. #4
    Senior Member OpenGL Pro dletozeun's Avatar
    Join Date
    Jan 2006
    Location
    FRANCE
    Posts
    1,367

    Re: VBO and IBO

    VertexTotal actually is computing the amount of polygons on the model. I mistakenly, originally, named it VertexTotal.

    Should my bufferdata parameters of the VBO be different?
    Please give correct names to your variable a post your code again if the same problem occurs so that it is clear to us .

    You have to give the buffer size in bytes to glBufferData and the number of indices to glDrawElements.

  5. #5
    Intern Contributor
    Join Date
    Jun 2009
    Posts
    90

    Re: VBO and IBO

    glEnableClientState(GL_VERTEX_ARRAY);

    GLuint VertexVBOID[2];
    glGenBuffers(1, VertexVBOID);
    glBindBuffer(GL_ARRAY_BUFFER, VertexVBOID[0]);
    glBufferData(GL_ARRAY_BUFFER, ((TriangleTotal*3) * sizeof(float)), electrodexyz, GL_STATIC_DRAW);

    GLuint IndexVBOID;
    glGenBuffers(1, &IndexVBOID);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IndexVBOID);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, (TriangleTotal) * sizeof(unsigned int), indices, GL_STATIC_DRAW);

    glVertexPointer(3, GL_FLOAT, 0, 0);
    glDrawElements(GL_TRIANGLES, (TriangleTotal), GL_UNSIGNED_INT, 0);
    And to Dynamically Allocate the Size of the Arrays for Indices and the Vertices

    electrodexyz = new float[TriangleTotal*3];
    indices = new unsigned int[TriangleTotal];
    I think I am allocate the memory correctly? I pass the amount of Triangles to the VBO multiplied by 3 to include every point. I then pass the amount of triangles to the index buffer so there is 1 index per triangle? Am I approaching this incorrectly??

  6. #6
    Advanced Member Frequent Contributor
    Join Date
    Apr 2003
    Posts
    666

    Re: VBO and IBO

    glBufferData(GL_ARRAY_BUFFER, ((TriangleTotal*3) * sizeof(float)), electrodexyz, GL_STATIC_DRAW);
    That won't work. If this code should reserve space for 3 individual vertices (each _3_ floats) per triangle, you'll need
    (TriangleTotal*3)*3*sizeof(float) bytes.


    Am I approaching this incorrectly??
    yes. This is wrong. You should have 3 indices per triangle instead of only one.

    As far as I can guess, you seem to have 3 individual vertices per triangles anyway (no vertex sharing among triangles?!). In this case, using glDrawArrays seems more appropriate and you don't need the indices at all.

  7. #7
    Senior Member OpenGL Pro dletozeun's Avatar
    Join Date
    Jan 2006
    Location
    FRANCE
    Posts
    1,367

    Re: VBO and IBO

    glBufferData(GL_ARRAY_BUFFER, ((TriangleTotal*3) * sizeof(float)), electrodexyz, GL_STATIC_DRAW);
    I think it should be: 3 * ((TriangleTotal*3) * sizeof(float))

    since there are 3 vertices per triangles and 3 coordinates per vertex.

    And:

    glBufferData(GL_ELEMENT_ARRAY_BUFFER, (TriangleTotal) * sizeof(unsigned int), indices, GL_STATIC_DRAW);
    3 * (TriangleTotal) * sizeof(unsigned int) since there are 3 indexes per triangle.

    Anyway, something is weird in your vertex data creation. You should not have as many vertices as indices. In your code you do not separate the number of indices and the number of vertices.
    The purpose of indexed buffers id to use less vertices in your BO by not duplicate them.

  8. #8
    Intern Contributor
    Join Date
    Jun 2009
    Posts
    90

    Re: VBO and IBO

    That won't work. If this code should reserve space for 3 individual vertices (each _3_ floats) per triangle, you'll need
    (TriangleTotal*3)*3*sizeof(float) bytes.
    I go through my model import and make a count for each time a triangle is rendered. Hence the 50,000 TriangleTotal.

    I then allocated space for each array, TriangleTotal*3, for the array of vertices. 50,000 Tri's and 3 floats per Tri. Then I allocated TriangleTotal for the Indices, I thought one index per triangle.

    Using that formula I attempted to allocated TriangleTotal*3 for the total vertices in the array of vertices? For the array I input array[0]=x1,array[1]=y1,array[2]=z1; array[3]=x2,array[4]=array[5]=y2,array[6]=z2, etc..

    yes. This is wrong. You should have 3 indices per triangle instead of only one.
    Why is it I should have three indices per Triangle? Sorry if this is a simple question, I am completely new to VBO's and IBO's, though I have tried to read about them before posting.

    As far as I can guess, you seem to have 3 individual vertices per triangles anyway (no vertex sharing among triangles?!). In this case, using glDrawArrays seems more appropriate and you don't need the indices at all.
    I assume there is vertex sharing. I am not certain since I import the coordinates from a ASCII file. It is a high-res 50,000 poly brain model, vertex sharing seems necessary. If I use glDrawArrays, how would this optimize the rendering and leave out the IBO's?

  9. #9
    Intern Contributor
    Join Date
    Jun 2009
    Posts
    90

    Re: VBO and IBO

    dletozeun,

    I changed the bufferdata to the suggested size. I now get a "unhandled win32 exception" "access violation reading location". If my memory is going out of bounds, I really am at a loss for what I'm doing wrong here.

    To create the indices array, for each triangle that is rendered, in this order I add 1 to the array of indices. So indices[0] = 1; indices[1] = 2, etc...
    And the positions, pos[0] = x1, pos[1] = y1, pos[2] = z1, etc..


    *Update*

    I believe the error is not passing in an Interleave array with the glNormalPointer call? Without the normals, I'm sure this would create a mess of a rendering.

    2 Questions:
    1) Do I use the same data array of vertices for the normals? And how would I go about rendering the normals using a VBO?

    2) As previously suggested, is glDrawArrays() going to compromise rendering speed? If I don't use IBO's will my program suffer?

    Thanks everyone, especially dletozeun and skynet

  10. #10
    Senior Member OpenGL Pro dletozeun's Avatar
    Join Date
    Jan 2006
    Location
    FRANCE
    Posts
    1,367

    Re: VBO and IBO

    Hum, I think you do not understand the idea behing indexed buffers. Some interesting lecture:

    It is d3d documentation but it is quite general:
    http://msdn.microsoft.com/en-us/library/bb147325(VS.85).aspx

    OpenGL wiki:

    http://www.opengl.org/wiki/Vertex_Formats
    http://www.opengl.org/wiki/VBO

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •