为什么在调用glReadPixels之前需要打电话清除?

时间:2016-09-21 04:57:02

标签: opengl opengl-4

我目前正在尝试读回纹理数据以将该数据存储在物理存储中。

以下代码处理读取,然后将生成的浮动缓冲区传输到工作线程以进行压缩和最终存储。

    //Create file for storing data
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);

//Store required map data to recreate current map data
int currentFramebuffer = 0;
glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &currentFramebuffer);

glBindFramebuffer(GL_FRAMEBUFFER, _terrainFrameBuffer);
glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, _globalTerrainRenderMap, 0);
glReadBuffer(GL_COLOR_ATTACHMENT0);

//Check for valid framebuffer
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
{
    glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, 0, 0);
    glBindFramebuffer(GL_FRAMEBUFFER, currentFramebuffer);
    return false;
}

float *mapBuffer = new float[_terrainMapSize*_terrainMapSize * 4];
if (mapBuffer != nullptr)
{
    glReadPixels(0, 0, _terrainMapSize, _terrainMapSize, GL_RGBA, GL_FLOAT, (void *)mapBuffer);

    //Set OpenGL fence object
    GLsync gSync;
    gSync = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);

    //wait for OpenGL fence to process data
    if (gSync)
    {
        while (1)
        {
            GLenum waitReturn = glClientWaitSync(gSync, GL_SYNC_FLUSH_COMMANDS_BIT, 1);
            if (waitReturn == GL_ALREADY_SIGNALED || waitReturn == GL_CONDITION_SATISFIED)
                break;
        }
        glDeleteSync(gSync);
    }

    //Spawn compress and save thread to store file
    std::thread t=std::thread(CompressAndSaveMapFile, mapBuffer, _terrainMapSize, _terrainMapSize, mapFilename);
    t.detach();
}

glBindFramebuffer(GL_FRAMEBUFFER, currentFramebuffer);

在此代码的最开始,您将看到对glClearColor的调用,然后调用glClear()。如果省略这些行,则glReadPixels返回看似垃圾数据。但是使用这两行,它会返回正确的纹理数据。

现在我的问题是这个,为什么在设置read framebuffer并调用glReadPixels()之前我需要清除系统帧缓冲区?有任何想法吗?

系统可以完美地运行这两条线路,只是为什么这个看似多余的呼叫需要在那里有点奇怪。

0 个答案:

没有答案