0

我写这个是为了测试如何用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.顺便说一句,使用这些调用可以获得多少性能?

4

1 回答 1

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

添加后,一切似乎都运行良好。

于 2012-11-15T13:32:34.677 回答