简体   繁体   中英

OpenGL Vertex Buffer Object doesn't work

I've got a problem with Vertex Buffer Object, it seems it doesn't work properly. It doesn't show anything on screen.

Here is my Code:

void gl::glRecti(int x,int y,int w,int h,glColor *color)
{
    GLuint vbo = 0;

    GLfloat verts[] = 
    {
        x,y,
        x,y + h,
        x + w,y + h,
        x + w,y,
    };

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

    glEnableClientState(GL_VERTEX_ARRAY);

        glBindBuffer(GL_ARRAY_BUFFER,vbo);
            glVertexPointer( 4 , GL_FLOAT , sizeof(float) * 8, NULL );
            glDrawArrays(GL_QUADS,0,4);
        glBindBuffer(GL_ARRAY_BUFFER, 0);


    glDisableClientState(GL_VERTEX_ARRAY);

}

PS: I'm very new in OpenGL programming. Any help would be appreciated.

Your vertex pointer does not make sense:

 glVertexPointer( 4 , GL_FLOAT , sizeof(float) * 8, NULL ); 

You are telling the GL that each vertex position is specified as a 4-dimensional vector, and that the offset between two consecutive vertices is 8 floats.

What you supply is a tighlty packed array of 2-dimensional positions, so you should use 2 as the size parameter, and 2*sizeof(float) for the stride (or 0, which is a shortcut for thigly packed arrays).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM