在display()JOGL中调用glReadPixels

时间:2014-05-10 20:50:30

标签: java opengl jogl glreadpixels picking

我正在尝试实施对象选择。我有代码将对象渲染为纯色,不亮的颜色,然后我读取屏幕缓冲区中的像素。我解释glReadPixels()的读数来确定光标当前所在的对象。最后,我重新渲染所有点亮,纹理和彩色的东西。

    gl.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    gl.glMatrixMode(GL_MODELVIEW);
    gl.glLoadIdentity();
    gl.glPushMatrix();

    //Picking render goes here...

    gl.glPopMatrix();



    //The following code reads the current pixel color at the center of the screen
    FloatBuffer buffer = FloatBuffer.allocate(4);

    gl.glReadBuffer(GL_FRONT);
    gl.glReadPixels(drawable.getWidth() / 2, drawable.getHeight() / 2, 1, 1, GL_RGBA, GL_FLOAT, buffer);
    float[] pixels = new float[3];
    pixels = buffer.array();
    float red = pixels[0];
    float green = pixels[1];
    float blue = pixels[2];
    System.out.println(red + ", " + green + ", " + blue);



    gl.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    gl.glMatrixMode(GL_MODELVIEW);
    gl.glLoadIdentity();
    gl.glPushMatrix();

    //Final render goes here...

    gl.glPopMatrix();

问题是对glReadPixels()的调用会返回FINAL渲染的像素颜色,而不是拾取渲染。我将非常感谢如何阅读拾取渲染像素。

我知道我可以使用OpenGL名称堆栈,但坦率地说我发现它不方便且荒谬。

1 个答案:

答案 0 :(得分:1)

问题是您渲染到后台缓冲区,但是您的glReadPixels()调用从前台缓冲区读取。如果渲染到双缓冲曲面(使用OpenGL时几乎总是需要),则默认情况下渲染将​​转到后台缓冲区。您在代码中进行了此次调用:

gl.glReadBuffer(GL_FRONT);

这告诉以下glReadPixels()调用从前缓冲区读取,这是绘制的缓冲区。

您应该能够简单地删除glReadBuffer()调用,因为GL_BACK是双缓冲默认渲染表面的默认读缓冲区。