调用glBindBuffer后,具有奇怪顶点位置的简单openGL应用程序

时间:2012-11-15 12:26:41

标签: c opengl glew glfw

我写这个来测试如何用opengl缓冲区绘制,这里是初始化的函数:

#include "GL/glew.h"
#include "GL/glfw.h"

    int main ( int argc, char* argv[] )
    {
        //init glfw
        glfwInit();

        glfwOpenWindow ( 1024, 768, 8, 8, 8, 8, 24, 0, GLFW_WINDOW );

        //init extension
        glewInit();

        //i
        glGenBuffers ( 1, &buffID );
        //init test buffer
        for ( int i = 0 ; i < 15; i ++ ) {
            testBuffer[i][0] += 1 + i * i;
            testBuffer[i][1] += 1 + i + i * i;
            testBuffer[i][2] = 0;
        }

        do {
            render_loop();
        } while ( glfwGetWindowParam ( GLFW_OPENED ) );

        glfwTerminate();
    }

这是render_loop函数:

GLuint buffID;
static GLfloat testBuffer[15][3];

void render_loop()
{
    glClearColor ( .7, .1, .1, 1.0f );
    glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    glViewport ( 0, 0, 1024, 768 );
    glMatrixMode ( GL_PROJECTION );

    glLoadIdentity();
    glOrtho ( 0, 1024, 768, 0, 0, -1 );
    glMatrixMode ( GL_MODELVIEW );
    glLoadIdentity();

    //draw a test point
    glPointSize ( 10 );
    glBegin ( GL_POINTS );
    glColor4f ( 1, 1, 1, 1 );
    glVertex3f ( 512, 384, 0 );
    glEnd();

     glBindBuffer(GL_ARRAY_BUFFER,buffID);
     glBufferData(GL_ARRAY_BUFFER,3 * sizeof(GLfloat) * 15, testBuffer,GL_DYNAMIC_DRAW);

    glEnableClientState(GL_VERTEX_ARRAY);
    glVertexPointer(3,GL_FLOAT,0,testBuffer);
    glDrawArrays(GL_POINTS,0,15);
    glBindBuffer(GL_ARRAY_BUFFER,0);
    glDisableClientState(GL_VERTEX_ARRAY);

    glfwSwapBuffers();

}

所有15个顶点都在0,0位置,这是窗口的左上角,我可以看到白点,在我从循环中删除这3行之后:

glBindBuffer(GL_ARRAY_BUFFER,buffID);
glBufferData(GL_ARRAY_BUFFER,sizeof(GLfloat) * 15, testBuffer,GL_DYNAMIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER,0);

一切都按照我的预期运作,我有几个问题:

1.什么改变了顶点缓冲区的位置?

2.使用这些调用获得多少性能?

1 个答案:

答案 0 :(得分:1)

必须称之为:

glBindBuffer(GL_ARRAY_BUFFER,0);

glBufferData函数之后

我发现我需要改变这个:

//added this line before glVertexPointer:
glBindBuffer(GL_ARRAY_BUFFER,buffID);
//glVertexPointer(3,GL_FLOAT,0,testBuffer); change last param to 0:
glVertexPointer(3,GL_FLOAT,0,0);

补充说,一切似乎都运作良好。