Help setting up VBO

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, &id);
    }

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

    void addVertex(Vector3f &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) && 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
", 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

In function VertexBuffer::draw() you have


glVertexPointer(3, GL_FLOAT, 0, 0);

It should be:


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

I made that edit, and my screen is still black. I updated my original post to reflect the change.

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);

I figured out my problem. It is not an OpenGL issue, but an issue with the data I am sending with glBufferData. Thanks

hi,

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

cu
uwi2k2