View Full Version : Beginner problem with VBOs

05-08-2011, 02:05 PM

I am new to opengl and trying to code some fractal scenes (like a sierpinski triangle http://en.wikipedia.org/wiki/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:

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)

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:

// 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:

//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 :)

05-09-2011, 12:12 AM
// 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.