Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 6 of 6

Thread: Help setting up VBO

  1. #1
    Junior Member Newbie
    Join Date
    Aug 2011
    Posts
    3

    Help setting up VBO

    Hello, I am trying to write a reusable VertexBuffer class. Here is my current code:

    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

  2. #2
    Intern Contributor uruk's Avatar
    Join Date
    Dec 2004
    Location
    acasa :) sau la munca :(
    Posts
    66

    Re: Help setting up VBO

    In function VertexBuffer::draw() you have
    Code :
    glVertexPointer(3, GL_FLOAT, 0, 0);
    It should be:
    Code :
    glVertexPointer(3, GL_FLOAT, 3*sizeof(float), 0);
    Stat rosa pristina nomine, nomina nuda tenemus.

  3. #3
    Junior Member Newbie
    Join Date
    Aug 2011
    Posts
    3

    Re: Help setting up VBO

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

  4. #4
    Junior Member Newbie
    Join Date
    Oct 2009
    Posts
    15

    Re: Help setting up VBO

    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
    Code :
    for (int i = 0; i < verts.size(); i += 3)
    should be
    Code :
    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:
    Code :
    glBufferData(GL_ARRAY_BUFFER, verts.size() * 3 * sizeof(float), data, GL_DYNAMIC_DRAW);

  5. #5
    Junior Member Newbie
    Join Date
    Aug 2011
    Posts
    3

    Re: Help setting up VBO

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

  6. #6
    Intern Contributor uwi2k2's Avatar
    Join Date
    Mar 2011
    Posts
    73

    Re: Help setting up VBO

    hi,

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

    cu
    uwi2k2
    uwi2k2 - OpenGL Trainer: www.opengl-trainer.com
    Free Programming Helpline: www.programming-hotline.com
    ---------------------------------------------------------
    A Friday Night Programmer: www.uwiworld.net
    CODE 2013 - Conference for Developers from Europe : http://www.code2013.eu - http://www.facebook.com/Code2013

Posting Permissions

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