opengl VBO渲染无法正常工作

时间:2013-10-30 11:59:16

标签: c++ opengl vertex-buffer vertex-array

在我的代码开头我初始化了vbo:

GLuint VBO;

然后是我的顶点和颜色数组:

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

现在我创建并绑定vbo并分配空间:

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

最后是渲染功能:

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

这应绘制一个三角形,但我看不到任何形状。 也许glVertexPointer和glColorPointer的参数不正确。 我已经在opengl.or上阅读了有关VBO的文档,我已经按照以下几个教程进行了操作:https://www.youtube.com/watch?v=KIeExgOcmv0

我的代码和youtube链接上的代码类似,但第一个代码不起作用。我不明白为什么。你能救我吗?

这是整个代码:

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

}

1 个答案:

答案 0 :(得分:3)

似乎在这里工作:

screenshot

我为VBO支持添加了GLEW并稍微改组了一些东西:

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