PDA

View Full Version : Help setting up VBO



tnecniv
08-14-2011, 11:09 AM
Hello, I am trying to write a reusable VertexBuffer class. Here is my current code:




#include <GL/glew.h>
#include <GL/glfw.h>
#include <vector>
#include <stdio.h>

struct Vector3f
{
float x, y, z;

Vector3f();
Vector3f(float x, float y, float z) : x(x), y(y), z(z) {}
};

class VertexBuffer
{
protected:
GLuint id;
std::vector<Vector3f> verts;

void updateVertexBuffer()
{
float data[verts.size() * 3];

for (int i = 0; i < verts.size(); i += 3)
{
data[i] = verts.at(i).x;
data[i + 1] = verts.at(i).y;
data[i + 2] = verts.at(i).z;
}

glBindBuffer(GL_ARRAY_BUFFER, id);
glBufferData(GL_ARRAY_BUFFER, verts.size() * 3, data, GL_DYNAMIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}

public:
VertexBuffer()
{
glGenBuffers(1, &amp;id);
}

~VertexBuffer()
{
glDeleteBuffers(1, &amp;id);
}

void addVertex(Vector3f &amp;v)
{
verts.push_back(v);
updateVertexBuffer();
}

void addVertex(float x, float y, float z)
{
Vector3f v(x, y, z);
addVertex(v);
}

void draw()
{
glBindBuffer(GL_ARRAY_BUFFER, id);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 3 * sizeof(float), 0);
//glDrawElements(GL_QUADS, 24, GL_UNSIGNED_BYTE, 0);
glDrawArrays(GL_QUADS, 0, verts.size());
glDisableClientState(GL_VERTEX_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
};

int main()
{
glfwInit();
glfwOpenWindow(800, 600, 24, 24, 24, 24, 24, 24, GLFW_WINDOW);
glewInit();

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
float aspect_ratio = 800.0f / 600.0f;
gluPerspective(90.0f, aspect_ratio, 0.1f, 100.0f);
glMatrixMode(GL_MODELVIEW);

VertexBuffer vbuf;
vbuf.addVertex(0.0f, 0.0f, 0.0f);
vbuf.addVertex(1.0f, 0.0f, 0.0f);
vbuf.addVertex(1.0f, 1.0f, 0.0f);
vbuf.addVertex(0.0f, 1.0f, 0.0f);

while (!glfwGetKey(GLFW_KEY_ESC) &amp;&amp; glfwGetWindowParam(GLFW_OPENED))
{
GLenum e = 0;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(0.0f, 0.0f, -2.0f);
vbuf.draw();
if ((e = glGetError()) != GL_NO_ERROR)
printf("%d\n", e);
glPopMatrix();
glfwSwapBuffers();
}

glfwTerminate();
return 0;
}


Right now, I just get a black screen. If I use glBegin/glEnd, I get a proper square. After drawing, glGetError returns GL_NO_ERROR.

Thank you

uruk
08-14-2011, 11:45 AM
In function VertexBuffer::draw() you have


glVertexPointer(3, GL_FLOAT, 0, 0);

It should be:


glVertexPointer(3, GL_FLOAT, 3*sizeof(float), 0);

tnecniv
08-14-2011, 12:13 PM
I made that edit, and my screen is still black. I updated my original post to reflect the change.

robmx
08-14-2011, 01:46 PM
I think you need to review the updateVertexBuffer function.

The loop seems to be wrong as it is not copying all the data from the vector:

instead of


for (int i = 0; i < verts.size(); i += 3)

should be


for (int i = 0; i < verts.size()*3; i += 3)


Also the call to glBufferData is missing taking into account the size of float. Should be something like:


glBufferData(GL_ARRAY_BUFFER, verts.size() * 3 * sizeof(float), data, GL_DYNAMIC_DRAW);

tnecniv
08-14-2011, 01:50 PM
I figured out my problem. It is not an OpenGL issue, but an issue with the data I am sending with glBufferData. Thanks

uwi2k2
08-15-2011, 01:26 AM
hi,

i really would like to know what was the issu.
may be a good hint for other people as well..

cu
uwi2k2