# Thread: Beginner problem with VBOs

1. ## Beginner problem with VBOs

Hello.

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:

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_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, &amp;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);

glGenVertexArrays(m_Depth,&amp;m_vaoFrontTriangle);
glBindVertexArray(m_vaoFrontTriangle);

glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE,  sizeof(ColoredVertex),	NULL);

//Farbdaten stehen immer nach der Position, daher ein offset der Größe von M3DVector3f notwendig
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE,  sizeof(ColoredVertex),	(const GLvoid *)(sizeof(M3DVector3f)));

//VAO wieder unbinden
glBindVertexArray(0);```

In my render loop I try to draw these triangles:

Code :
```//VBO aktivieren
glBindVertexArray(m_vaoFrontTriangle);
glBindBuffer(GL_ARRAY_BUFFER, m_FrontTriangle);
// Zeiche einen Triangle

glDrawArrays(GL_TRIANGLES, 0,3 * m_Depth));

//Aufräumen
glBindVertexArray(0);```

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. ## Re: Beginner problem with VBOs

Originally Posted by jonny2011
Code :
```// Erzeuge das VBO
glGenBuffers(m_Depth, &amp;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.

#### Posting Permissions

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