Using a VBO to draw lines from a vector of points in OpenGL

openglvertex-buffer

I have a simple OpenGL program which I am trying to utilize Vertex Buffer Objects for rendering instead of the old glBegin() – glEnd(). Basically the user clicks on the window indicating a starting point, and then presses a key to generate subsequent points which OpenGL draws as a line.

I've implemented this using glBegin() and glEnd() but have not been successful using a VBO. I am wondering if the problem is that after I initialize the VBO, I'm adding more vertices which it doesn't have memory allocated for, and thus doesn't display them.

Edit: Also, I'm a bit confused as to how it knows exactly which values in the vertex struct to use for x and y, as well as for r, g, b. I haven't been able to find a clear example of this.

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <Math.h>
#include <iostream>
#include <vector>
#include <GL/glew.h>
#include <GL/glut.h>

struct vertex {
    float x, y, u, v, r, g, b;
};

const int D = 10;   // distance
const int A = 10;   // angle
const int WINDOW_WIDTH = 500, WINDOW_HEIGHT = 500;

std::vector<vertex> vertices;
boolean start = false;
GLuint vboId;

void update_line_point() {
    vertex temp;
    temp.x = vertices.back().x + D * vertices.back().u;
    temp.y = vertices.back().y + D * vertices.back().v;
    temp.u = vertices.back().u;
    temp.v = vertices.back().v;
    vertices.push_back(temp);
}

void update_line_angle() {
    float u_prime, v_prime;
    u_prime = vertices.back().u * cos(A) - vertices.back().v * sin(A);
    v_prime = vertices.back().u * sin(A) + vertices.back().v * cos(A);
    vertices.back().u = u_prime;
    vertices.back().v = v_prime;
}

void initVertexBuffer() {
    glGenBuffers(1, &vboId);
    glBindBuffer(GL_ARRAY_BUFFER, vboId);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertex) * vertices.size(), &vertices[0], GL_STATIC_DRAW);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
}

void displayCB() {
    glClear(GL_COLOR_BUFFER_BIT);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0, WINDOW_WIDTH, 0, WINDOW_HEIGHT);

    if (start) {
        glBindBuffer(GL_ARRAY_BUFFER, vboId);
        glEnableClientState(GL_VERTEX_ARRAY);
        glEnableClientState(GL_COLOR_ARRAY);
        glVertexPointer(2, GL_FLOAT, sizeof(vertex), &vertices[0]);
        glColorPointer(3, GL_FLOAT, sizeof(vertex), &vertices[0]);
        glDrawArrays(GL_LINE_STRIP, 0, vertices.size());
        glDisableClientState(GL_VERTEX_ARRAY);
        glDisableClientState(GL_COLOR_ARRAY);
        glBindBuffer(GL_ARRAY_BUFFER, 0);
    }

    /***** this is what I'm trying to achieve
    glColor3f(1, 0, 0);
    glBegin(GL_LINE_STRIP);
    for (std::vector<vertex>::size_type i = 0; i < vertices.size(); i++) {
            glVertex2f(vertices[i].x, vertices[i].y);
    }
    glEnd();
    *****/

    glFlush();
    glutSwapBuffers();
}


void mouseCB(int button, int state, int x, int y) {
    if (state == GLUT_DOWN) {
        vertices.clear();
        vertex temp = {x, WINDOW_HEIGHT - y, 1, 0, 1, 0, 0};  // default red color
        vertices.push_back(temp);
        start = true;
        initVertexBuffer();
    }
    glutPostRedisplay();
}

void keyboardCB(unsigned char key, int x, int y) {
    switch(key) {
    case 'f':
        if (start) {
            update_line_point();
        }
        break;
    case 't':
        if (start) {
            update_line_angle();
        }
        break;
    }

    glutPostRedisplay();
}

void initCallbackFunc() {
    glutDisplayFunc(displayCB);
    glutMouseFunc(mouseCB);
    glutKeyboardFunc(keyboardCB);
}

int main(int argc, char** argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);
    glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);
    glutInitWindowPosition(100, 100);
    glutCreateWindow("Test");
    initCallbackFunc();

    // initialize glew
    GLenum glewInitResult;
    glewExperimental = GL_TRUE;
    glewInitResult = glewInit();
    if (GLEW_OK != glewInitResult) {
        std::cerr << "Error initializing glew." << std::endl;
        return 1;
    }
    glClearColor(1, 1, 1, 0);
    glutMainLoop();

    return 0;
}

Best Answer

If you have a VBO bound then the pointer argument to the gl*Pointer() calls is interpreted as a byte offset from the beginning of the VBO, not an actual pointer. Your usage is consistent with vertex array usage though.

So for your vertex struct x starts at byte zero and r starts at byte sizeof(float) * 4.

Also, your mouse callback reset your vertex vector on every call so you would never be able have more than one vertex in it at any given time. It also leaked VBO names via the glGenBuffers() in initVertexBuffer().

Give this a shot:

#include <GL/glew.h>
#include <GL/glut.h>
#include <iostream>
#include <vector>

struct vertex 
{
    float x, y;
    float u, v;
    float r, g, b;
};

GLuint vboId;
std::vector<vertex> vertices;
void mouseCB(int button, int state, int x, int y) 
{
    y = glutGet( GLUT_WINDOW_HEIGHT ) - y;
    if (state == GLUT_DOWN) 
    {
        vertex temp = {x, y, 1, 0, 1, 0, 0};  // default red color
        vertices.push_back(temp);
        glBindBuffer(GL_ARRAY_BUFFER, vboId);
        glBufferData(GL_ARRAY_BUFFER, sizeof(vertex) * vertices.size(), &vertices[0], GL_STATIC_DRAW);
        glBindBuffer(GL_ARRAY_BUFFER, 0);
    }
    glutPostRedisplay();
}

void displayCB()
{
    glClearColor(1, 1, 1, 0);
    glClear(GL_COLOR_BUFFER_BIT);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    double w = glutGet( GLUT_WINDOW_WIDTH );
    double h = glutGet( GLUT_WINDOW_HEIGHT );
    glOrtho( 0, w, 0, h, -1, 1 );

    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();

    if ( vertices.size() > 1 ) 
    {
        glBindBuffer(GL_ARRAY_BUFFER, vboId);
        glEnableClientState(GL_VERTEX_ARRAY);
        glEnableClientState(GL_COLOR_ARRAY);
        glVertexPointer(2, GL_FLOAT, sizeof(vertex), (void*)(sizeof( float ) * 0));
        glColorPointer(3, GL_FLOAT, sizeof(vertex), (void*)(sizeof( float ) * 4));
        glDrawArrays(GL_LINE_STRIP, 0, vertices.size());
        glDisableClientState(GL_VERTEX_ARRAY);
        glDisableClientState(GL_COLOR_ARRAY);
        glBindBuffer(GL_ARRAY_BUFFER, 0);
    }

    glutSwapBuffers();
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);
    glutInitWindowSize(500, 500);
    glutInitWindowPosition(100, 100);
    glutCreateWindow("Test");

    // initialize glew
    glewExperimental = GL_TRUE;
    GLenum glewInitResult = glewInit();
    if (GLEW_OK != glewInitResult) {
        std::cerr << "Error initializing glew." << std::endl;
        return 1;
    }

    glGenBuffers(1, &vboId);

    glutDisplayFunc(displayCB);
    glutMouseFunc(mouseCB);
    glutMainLoop();
    return 0;
}
Related Topic