OpenGL VBO内存泄漏

时间:2012-10-21 02:48:35

标签: c++ opengl vbo

我在使用一个非常简单的OpenGL应用程序时遇到了一些内存问题,我写这篇文章是为了学习VBO。它使用的内存容易增加打开的时间。下面的代码重复我使用Windows任务管理器看到的内容:

int main() {

sf::RenderWindow window(sf::VideoMode(800, 600, 32), "Test");

///Setup the scene, materials, lighting
Scene scene;
scene.resize(800,600);

glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glColorMaterial(GL_FRONT_AND_BACK, GL_EMISSION);
glEnable(GL_COLOR_MATERIAL);
glShadeModel(GL_FLAT);
glEnable(GL_LIGHT0);
float XL = .5, YL = .1, ZL = 1;
GLfloat ambientLight[] = { 0.2f, 0.2f, 0.2f, 1.0f };
GLfloat diffuseLight[] = { 0.8f, 0.8f, 0.8, 1.0f };
GLfloat specularLight[] = { 0.5f, 0.5f, 0.5f, 1.0f };
GLfloat lightpos[] = {XL, YL, ZL, 0.};
glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
glLightfv(GL_LIGHT0, GL_SPECULAR, specularLight);
glLightfv(GL_LIGHT0, GL_POSITION, lightpos);

glGenBuffersARB = (PFNGLGENBUFFERSARBPROC)wglGetProcAddress("glGenBuffersARB");
glBindBufferARB = (PFNGLBINDBUFFERARBPROC)wglGetProcAddress("glBindBufferARB");
glBufferDataARB = (PFNGLBUFFERDATAARBPROC)wglGetProcAddress("glBufferDataARB");
glBufferSubDataARB = (PFNGLBUFFERSUBDATAARBPROC)wglGetProcAddress("glBufferSubDataARB");
glDeleteBuffersARB = (PFNGLDELETEBUFFERSARBPROC)wglGetProcAddress("glDeleteBuffersARB");
glGetBufferParameterivARB = (PFNGLGETBUFFERPARAMETERIVARBPROC)wglGetProcAddress("glGetBufferParameterivARB");
glMapBufferARB = (PFNGLMAPBUFFERARBPROC)wglGetProcAddress("glMapBufferARB");
glUnmapBufferARB = (PFNGLUNMAPBUFFERARBPROC)wglGetProcAddress("glUnmapBufferARB");

GLfloat vertices[] = { .5, .5, .5,  -.5, .5, .5,  -.5,-.5, .5};

GLuint VBOID;
glGenBuffersARB(1, &VBOID);
glBindBufferARB(GL_ARRAY_BUFFER_ARB, VBOID);
glBufferDataARB(GL_ARRAY_BUFFER_ARB, 108, vertices, GL_STATIC_DRAW_ARB);

///Start loop
cout << "Starting" << endl;
while( window.isOpen() ) {
    sf::Event event;
    while( window.pollEvent( event ) ) {
        if( event.type == sf::Event::Closed )
            window.close();
    }

    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(50.0, 1.0, 1.0, 50);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(5, 5, 5, 0, 0, 0, 0, 1, 0);

    glBindBufferARB(GL_ARRAY_BUFFER_ARB, VBOID);
    glEnableClientState(GL_VERTEX_ARRAY);

    glVertexPointer(3, GL_FLOAT, 0, 0);
    glDrawArrays(GL_TRIANGLES, 0, 3);

    glDisableClientState(GL_VERTEX_ARRAY); 

    ///Reset env settings for SFML
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

    window.display();
}

return 1;

}

编辑:

评论所有事件轮询没有任何变化。如果我使用window.clear()或opengl的clear函数,窗口清除似乎没有区别。

随着进一步评论,我能够发现一行代码window.display()导致我的内存泄漏。我看看SFML论坛上的人是否可以解决这个问题;)

1 个答案:

答案 0 :(得分:0)

我在绘制循环中看不到任何会导致此行为的内容。

我认为您应该尝试SFML论坛:http://en.sfml-dev.org/forums/

关于这个问题有几个主题:

开发人员大多声称这是一个驱动程序问题。您可以尝试禁用所有绘制代码​​,看看是否有任何改变。使用window.clear()代替?删除事件轮询?