C ++ OpenGL闪烁问题

时间:2014-06-07 16:13:21

标签: c++ opengl flicker

我有一个问题,当我只渲染1个立方体时,它一切都没问题,但是如果我添加多个立方体,它们全部开始闪烁......可能是因为它们互相干扰但我使用双倍配音所以它不应该是的。这里是渲染方法的代码

for (std::vector<Entity *>::iterator iterator = entityArray->begin(); iterator != entityArray->end(); iterator++) {

    Entity *entity = *iterator;
    if (entity->getVertexBuffer() != NULL) {

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        glUseProgram(entity->getVertexBuffer()->getShader()->getProgramHandle());

        glLoadIdentity();
        gluLookAt(_currentCamera->getPosition().x,
            _currentCamera->getPosition().y,
            _currentCamera->getPosition().z,
            _currentCamera->getEyeVector().x,
            _currentCamera->getEyeVector().y,
            _currentCamera->getEyeVector().z,
            _currentCamera->getUpVector().x,
            _currentCamera->getUpVector().y,
            _currentCamera->getUpVector().z);

        glTranslatef(entity->getPosition().x, entity->getPosition().y, entity->getPosition().z);

        glRotatef(entity->getRotation().x, 0.0f, 0.0f, 1.0f);
        glRotatef(entity->getRotation().y, 0.0f, 1.0f, 0.0f);
        glRotatef(entity->getRotation().z, 1.0f, 0.0f, 0.0f);

        glScalef(entity->getScale().x, entity->getScale().y, entity->getScale().z);

        glUniform4f(entity->getVertexBuffer()->getShader()->get_uColor(),
            entity->getVertexBuffer()->getShaderData()->get_uColorValue().x,
            entity->getVertexBuffer()->getShaderData()->get_uColorValue().y,
            entity->getVertexBuffer()->getShaderData()->get_uColorValue().z,
            entity->getVertexBuffer()->getShaderData()->get_uColorValue().w);

        glUniform3f(entity->getVertexBuffer()->getShader()->get_uLightPosition(),
            entity->getVertexBuffer()->getShaderData()->get_uLightPosition().x,
            entity->getVertexBuffer()->getShaderData()->get_uLightPosition().y,
            entity->getVertexBuffer()->getShaderData()->get_uLightPosition().z);


        entity->getVertexBuffer()->configureVertexAttributes();
        entity->getVertexBuffer()->renderVertexBuffer();

        glfwSwapBuffers(_window);
        glfwPollEvents();

    }
}

2 个答案:

答案 0 :(得分:3)

首先清除,绘制所有立方体,然后交换,如下所示:

glClear();

for (cube in cubes) { draw(cube); }

glfwSwapBuffers(_window);
glfwPollEvents();

那应该解决它。

答案 1 :(得分:2)

你在内循环中调用glClear和glSwapBuffers。因此,对于您在内部循环中绘制的所有内容,它会清除图片,绘制事物(单个多维数据集)交换,然后对数组中的下一个元素执行相同操作。

任何不绘制立方体的东西都必须超出该循环。