flying teapot
flying teapot

Reputation: 23

opengl VBO rendering doesn't work properly

At the beginning of my code I have initialized the vbo:

GLuint VBO;

then my vertex and color array:

GL float vertandcol[]={x1,y1, z1, r1,g1,b1, ...........,x3, y3, z3, r3,g3,b3};

Now I create and bind the vbo and allocate the space:

glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertandcol),vertandcol, GL_STATIC_DRAW);

and finally the render function:

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glVertexPointer(3, GL_FLOAT,  6* sizeof(GLfloat), 0);
glColorPointer(3, GL_FLOAT, 6* sizeof(GLfloat), (void*)(3 * sizeof(GLfloat)));
glDrawArrays(GL_TRIANGLES, 0,3);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, 0);

This should draw a triangle but I can't see anythig. Maybe the arguments of glVertexPointer and glColorPointer are incorrect. I have read the documentation about VBO on opengl.or and I've follow several tutorials, like this: https://www.youtube.com/watch?v=KIeExgOcmv0

My code and the code on the youtube link are similar but the first one doesn't work. I can't understand why. Can you help me?

here is the whole code:

GLuint vbo;
GLfloat vertandcolor[] = {175.0, 25.0, 0.0, 1.0, 0.0, 0.0, 
    100.0, 325.0, 0.0, 0.0,1.0,0.0, 250.0, 25.0, 0.0, 0.0, 0.0, 1.0};



void init()
{
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertandcolor),vertandcolor, GL_STATIC_DRAW);

}



void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


glBindBuffer(GL_ARRAY_BUFFER, vbo);

glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT,  6* sizeof(GLfloat), 0);
glEnableClientState(GL_COLOR_ARRAY);
glColorPointer(3, GL_FLOAT, 6* sizeof(GLfloat), (void*)(3 * sizeof(GLfloat)));
glDrawArrays(GL_TRIANGLES, 0,3);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glutSwapBuffers();
glFlush();
}

void reshape(int w, int h){
glClearColor(0.5,0.2,0.5,1);
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, (GLsizei) w, 0, (GLsizei) h, -1,1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();}


int main(int argc, char** argv){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(350, 350);
glutInitWindowPosition(100, 100);
glutCreateWindow("VBO");
init();
glutDisplayFunc(&display);
glutReshapeFunc(reshape);

glutMainLoop();

}

Upvotes: 0

Views: 844

Answers (1)

genpfault
genpfault

Reputation: 52157

Seems to work here:

screenshot

I added GLEW for VBO support and shuffled things around a bit:

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

GLuint vbo = 0;
void init()
{
    GLfloat verts[] = 
    {
        175.0, 25.0, 0.0,   1.0, 0.0, 0.0, 
        100.0, 325.0, 0.0,  0.0,1.0,0.0, 
        250.0, 25.0, 0.0,   0.0, 0.0, 1.0
    };

    glGenBuffers(1, &vbo);
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW);
}

void display()
{
    glClearColor(0.5,0.2,0.5,1);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    int w = glutGet( GLUT_WINDOW_WIDTH );
    int h = glutGet( GLUT_WINDOW_HEIGHT );
    glViewport(0, 0, w, h);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, w, 0, h, -1, 1);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glBindBuffer(GL_ARRAY_BUFFER, vbo);

    glEnableClientState(GL_VERTEX_ARRAY);
    glVertexPointer(3, GL_FLOAT, 6* sizeof(GLfloat), 0);

    glEnableClientState(GL_COLOR_ARRAY);
    glColorPointer(3, GL_FLOAT, 6* sizeof(GLfloat), (void*)(3 * sizeof(GLfloat)));

    glDrawArrays(GL_TRIANGLES, 0, 3);

    glDisableClientState(GL_COLOR_ARRAY);
    glDisableClientState(GL_VERTEX_ARRAY);

    glBindBuffer(GL_ARRAY_BUFFER, 0);

    glutSwapBuffers();
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
    glutInitWindowSize(350, 350);
    glutInitWindowPosition(100, 100);
    glutCreateWindow("VBO");
    glewInit();

    init();

    glutDisplayFunc(display);
    glutMainLoop();
}

Upvotes: 3

Related Questions