Results 1 to 2 of 2

Thread: Beginner problem with VBOs

  1. #1
    Junior Member Newbie
    Join Date
    May 2011

    Beginner problem with VBOs


    I am new to opengl and trying to code some fractal scenes (like a sierpinski triangle That was fortunatly a success but I only managed to do this with the creation of each triangle for itself and redering it. As this costs very much performance I wanted to do this via vertex buffer arrays. Sadly it seems that I have a logic problem in my code and I hope you could help me to find it.

    I have a structure for colored vertices:

    Code :
    struct ColoredVertex
    	M3DVector3f m_Position;
    	M3DVector3f m_Color;

    In a recursive call I want to fill now the vertices into this structure (and colorize it that later a 3D tetraeder is created with colored sides)

    Code :
    ColoredVertex frontTriangle[3];
    // draw front triangle
    m3dLoadVector3(frontTriangle[0].m_Position, xB, yB, zB);
    m3dLoadVector3(frontTriangle[1].m_Position, xA, yA, zA);
    m3dLoadVector3(frontTriangle[2].m_Position, xD, yD, zD);
    m3dLoadVector3(frontTriangle[0].m_Color, 0.0f, 1.0f, 0.0f); // green
    m3dLoadVector3(frontTriangle[1].m_Color, 0.0f, 1.0f, 0.0f); // green
    m3dLoadVector3(frontTriangle[2].m_Color, 0.0f, 1.0f, 0.0f); // green

    Then I create the VBO array:

    Code :
    // Erzeuge das VBO
    glGenBuffers(m_Depth, &m_FrontTriangle);
    glBindBuffer(GL_ARRAY_BUFFER, m_FrontTriangle);
    //Kopiere die 3 * m_Depth + 1 vertices ins VBO
    glBufferData(GL_ARRAY_BUFFER, sizeof(ColoredVertex) * (3 * m_Depth), frontTriangle, GL_STATIC_DRAW);
    // erstes Shader-Attribut aktivieren (Vertex-Position)
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE,  sizeof(ColoredVertex),	NULL);
    // Zweites Shader-Attribut aktivieren (Farbe). 
    //Farbdaten stehen immer nach der Position, daher ein offset der Größe von M3DVector3f notwendig
    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE,  sizeof(ColoredVertex),	(const GLvoid *)(sizeof(M3DVector3f)));
    //VAO wieder unbinden

    In my render loop I try to draw these triangles:

    Code :
    //VBO aktivieren
    glBindBuffer(GL_ARRAY_BUFFER, m_FrontTriangle);
    // Zeiche einen Triangle
    glDrawArrays(GL_TRIANGLES, 0,3 * m_Depth));

    Well... it only draws a single triangle all the time regardless how I resize the vertex array. It seems like I dont write all necessary vertices into the vertex buffer, but I couldn't figure out how it should be done right.

    Does anyone have a hint or sees what I'm doing wrong?

    All help is welcome

  2. #2
    Senior Member Regular Contributor
    Join Date
    Mar 2015

    Re: Beginner problem with VBOs

    Quote Originally Posted by jonny2011
    Code :
    // Erzeuge das VBO
    glGenBuffers(m_Depth, &m_FrontTriangle);
    You are generating m_depth Buffers there, but you want only one. If m_FrontTriangle is a single GLuint, it will overwrite other variables !

    The same applies to the glGenVertexArrays statement.

    Also you don't need to Re-Bind the VBO after you bound the VAO. The VAO already stores all the Vertex Attribute Pointers and which Buffers they are bound to.

Similar Threads

  1. Problem using VBOs
    By myk45 in forum OpenGL: Basic Coding
    Replies: 4
    Last Post: 11-08-2012, 11:13 PM
  2. Replies: 2
    Last Post: 08-15-2012, 06:10 AM
  3. Problem with glDrawElements() via VBOs
    By Kip Warner in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 12-15-2009, 10:35 PM
  4. Problem with VBOs
    By Count Duckula in forum OpenGL: Basic Coding
    Replies: 21
    Last Post: 09-28-2006, 11:46 AM
  5. Problem with VBOs :)
    By OnTheRun in forum OpenGL: Basic Coding
    Replies: 6
    Last Post: 10-15-2004, 01:44 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
Proudly hosted by Digital Ocean