我正在尝试实现对象拾取。我有代码将对象渲染为纯色、未点亮的颜色,然后读取屏幕缓冲区中的像素。我解释来自 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()
的调用返回最终渲染的像素颜色,而不是拾取渲染的像素颜色。我希望能够解释如何读取拾取渲染像素。
我知道我可以使用 OpenGL 名称堆栈,但坦率地说,我发现它不方便且荒谬。
最佳答案
问题是您渲染到后台缓冲区,但您的 glReadPixels()
调用从前台缓冲区读取。如果渲染到双缓冲表面(使用 OpenGL 时几乎总是需要的),默认情况下渲染将进入后台缓冲区。您的代码中有此调用:
gl.glReadBuffer(GL_FRONT);
这告诉下面的 glReadPixels()
调用从前端缓冲区读取,这是您不绘制到的缓冲区。
您应该能够简单地删除 glReadBuffer()
调用,因为 GL_BACK
是双缓冲默认渲染表面的默认读取缓冲区。
关于java - 在 display() JOGL 中调用 glReadPixels,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23586181/