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

2

7

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;
}
Torres answered 15/9, 2013 at 16:55 Comment(0)
B
4

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;
}
Bismuthic answered 16/9, 2013 at 2:40 Comment(0)
R
4

A VBO is a buffer located somewhere in memory (almost always in dedicated GPU memory - VRAM) of a fixed size. You specify this size in glBufferData, and you also simultaneously give the GL a pointer to copy from. The key word here is copy. Everything you do to the vector after glBufferData isn't reflected in the VBO.

You should be binding and doing another glBufferData call after changing the vector. You will also probably get better performance from glBufferSubData or glMapBuffer if the VBO is already large enough to handle the new data, but in a small application like this the performance hit of calling glBufferData every time is basically non-existent.

Also, to address your other question about the values you need to pick out x, y, etc. The way your VBO is set up is that the values are interleaved. so in memory, your vertices will look like this:

+-------------------------------------------------
| x | y | u | v | r | g | b | x | y | u | v | ... 
+-------------------------------------------------

You tell OpenGL where your vertices and colors are with the glVertexPointer and glColorPointer functions respectively.

  • The size parameter specifies how many elements there are for each vertex. In this case, it's 2 for vertices, and 3 for colors.
  • The type parameter specifies what type each element is. In your case it's GL_FLOAT for both.
  • The stride parameter is how many bytes you need to skip from the start of one vertex to the start of the next. With an interleaved setup like yours, this is simply sizeof(vertex) for both.
  • The last parameter, pointer, isn't actually a pointer to your vector in this case. When a VBO is bound, pointer becomes a byte offset into the VBO. For vertices, this should be 0, since the first vertex starts at the very first byte of the VBO. For colors, this should be 4 * sizeof(float), since the first color is preceded by 4 floats.
Residential answered 16/9, 2013 at 2:39 Comment(0)
B
4

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;
}
Bismuthic answered 16/9, 2013 at 2:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.