OpenGL后退和前置缓冲区 - 在显示到屏幕之前计数和调整红色像素的数量

时间:2017-02-16 06:01:05

标签: opengl buffer framebuffer

我正在编写一个函数来读取屏幕左右两半的红色像素数,它会在红色像素数之间的差异超出阈值时进行调整。我在使用缓冲区(后端和前端)时出现问题,因为当我最终交换到前端缓冲区时,它会动态地绘制我在后台缓冲区中所做的所有更改(就像更改已排队)但我只是想把最后一个画到屏幕上。在伪代码中,

do {
draw scene to back buffer
read number of left, right red pixels
if (abs(left-right)>threshold) {
//some function to jitter objects in the scene
}
else done=true,
}while(!done);
draw to front buffer

这是一些C ++ / OpenGL代码:

do{     
glDrawBuffer(GL_BACK_LEFT);
display(0);

glDrawBuffer(GL_BACK_RIGHT);
display(1);

 redsleft = 0;
 redsright = 0;

glReadBuffer(GL_BACK_LEFT);

glReadPixels(0, 0, resolution.first, resolution.second, GL_RGBA, GL_UNSIGNED_BYTE, frameBuffer);
for (int j = 0; j < resolution.first*resolution.second * 4; j += 4){
    if (frameBuffer[j]>0 && frameBuffer[j + 1] == 0 && frameBuffer[j + 2] == 0 && frameBuffer[j + 3] > 0)
            {


                if (j % 4 == 0 && i == 0)

                {

                    int redX = (j / 4) % resolution.first;

                    int redY = j / 4 / resolution.first;

                    if (redX < resolution.first / 2)
                        redsleft++;


                    else
                        redsright++;

                }

            }


        }
        //if abs redsleft-redsright is above threshold, call function to adjust 
        //else done

    }   
}

}while (!done);

glDrawBuffer(GL_BACK_LEFT);
display(0);

glDrawBuffer(GL_BACK_RIGHT);
display(1);

glutSwapBuffers();
glutPostRedisplay();

有什么想法吗? 我一直在尝试像glFlush(),glClear()这样的东西,但我没有运气。

0 个答案:

没有答案
相关问题