0

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

}
genpfault
  • 51,148
  • 11
  • 85
  • 139
  • As always, are there any GL errors? The code looks fine to me. – thokra Oct 30 '13 at 12:04
  • the compiler (NVCC) doesn't give me any errors. When I launch the code appear a blank window for a fraction of a second. – flying teapot Oct 30 '13 at 12:11
  • What are the positions? Is you projection correct? What GUI framework? Are you using a double buffered context and don't swap? – thokra Oct 30 '13 at 12:13
  • I try with single and with double , when I use the double buffered I add glutSwapBuffers(); in the render code but still not work – flying teapot Oct 30 '13 at 12:17
  • 1
    Do you specify the positions in normalized device coordinates or do you project the triangle? Is the vertex winding correct, i.e. are you actually looking at a front-facing primitive? – thokra Oct 30 '13 at 12:19
  • data GLfloat data[] = {175.0, 25.0, 0.0, 1.0, 0.0, 0.0, 100.0, 325.0, 0.0,1.0,0.0, 250.0, 25.0, 0.0, 0.0, 0.0, 1.0}; – flying teapot Oct 30 '13 at 12:25
  • reshape function 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();} – flying teapot Oct 30 '13 at 12:26
  • I think even the reshape function is correct – flying teapot Oct 30 '13 at 12:27
  • Can you post the complete code somewhere? I'd like to have a look at it. – thokra Oct 30 '13 at 12:29
  • Where I can post the code? – flying teapot Oct 30 '13 at 12:34
  • I can't post the whole code here. – flying teapot Oct 30 '13 at 12:37
  • @flyingteapot just edit the question to add code, or post it e.g. at http://pastebin.org – keltar Oct 30 '13 at 12:43
  • I've edited the question, look at the code – flying teapot Oct 30 '13 at 12:52
  • Works for me. You've omited #include-commands, if you using some extension-loading library - this could be a problem. – keltar Oct 30 '13 at 12:59
  • I've only forgotten to copy the include command . I've use a NVCC compiler – flying teapot Oct 30 '13 at 13:05
  • I understand perfectly clear that you've forgotten them, but what exactly you're including is significant. Also, isn't `nvcc` for CUDA (and only for CUDA)? Last time i've checked, it seemed impossible to use it for general purpose like you do now. – keltar Oct 30 '13 at 13:17
  • I do a lttle research, I've to map the resource, now I'm readind about cuda and vbo. Thanks a lot keltar – flying teapot Oct 30 '13 at 13:56

1 Answers1

3

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();
}
genpfault
  • 51,148
  • 11
  • 85
  • 139