使用 glReadPixels 和 gluUnProject 读取透明平面的深度值
我正在尝试创建台球模拟,并一直使用 glReadPixels 和 gluUnProject 将鼠标指针投影到场景中。
如果鼠标指向场景中的某个对象(例如桌子),则效果很好,但当它指向背景时,由于 glReadPixels 调用返回 1.0,它会弄乱 gluUnProject。
我试图弄清楚如何在桌子的同一水平面上绘制一个透明平面,这样无论我将鼠标指向场景中的何处,它都会获得深度,就好像它指向与桌子相同的平面上一样。
如果我绘制一个透明的四边形而不使用 glAlphaFunc(GL_GREATER, 0.01f);它将把四边形绘制为白色,并且深度测试将按我的计划进行,但是当我添加对 alphaFunc 的调用以使四边形变得透明时,深度会恢复到之前的状态。据我所知, glReadPixels 从帧缓冲区读取像素,所以这是有道理的,我只是想知道如何解决这个问题。
我还尝试反转四边形上的缠绕,以便从上方看不到它,但这与 glReadPixels 从帧缓冲区获取测量结果存在相同的问题。
简而言之,如何让 glReadPixels 从对象中获取其深度分量,而不将该对象绘制到屏幕上?
以下是对 glReadPixels 和 gluUnProject 的调用:
winX = (float)x;
winY = (float)viewport[3] - (float)y;
glReadPixels(x, (int) winY, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ);
gluUnProject(winX, winY, winZ, modelview, projection, viewport, &posX, &posY, &posZ);
I am trying to create a billiards simulation and have been using glReadPixels along with gluUnProject to project my mouse pointer into the scene.
This works fine if the mouse is pointing at an object in the scene (the table for instance) but when it points to the background it messes up the gluUnProject due to the glReadPixels call returning 1.0.
I'm trying to figure out how to draw a transparent plane at the same level of the table so that no matter where I point the mouse in my scene, it will get the depth as if it were pointing onto the same plane as the table.
If I draw a transparent quad without glAlphaFunc(GL_GREATER, 0.01f); it will draw the quad as white and the depth testing will work as I planned, but when I add in the call to alphaFunc to get the quad to be transparent, the depth goes back to what it was before. From what I've seen, glReadPixels reads the pixels from the frame buffer, so this makes sense, I'm just wondering how I can work around this.
I've also tried reversing the winding on the quad so that it wouldn't be visible from above, but this has the same problem of glReadPixels taking it's measurements from the framebuffer.
In short, how do I get glReadPixels to get it's depth component from an object without drawing that object to the screen?
Here's the calls to glReadPixels and gluUnProject:
winX = (float)x;
winY = (float)viewport[3] - (float)y;
glReadPixels(x, (int) winY, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ);
gluUnProject(winX, winY, winZ, modelview, projection, viewport, &posX, &posY, &posZ);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是因为 Alpha 测试完全丢弃未通过测试的像素。
然而 gluUnProject 不应该因深度缓冲值 1.0 而被阻塞;对于深度缓冲区值 1.0,您应该得到的值是远裁剪平面的距离。
That is because alpha testing fully discards pixels that don't pass the test.
However gluUnProject should not choke on a depth buffer value of 1.0; what you should get as value for a depth buffer value of 1.0 is the distance of the far clipping plane.
您可以使用“如果在此之后绘制某些内容,它仍然会进行深度测试(如果已打开)”来完全禁用对颜色缓冲区的写入
,并且其深度值仍将被写入深度缓冲区(如果已打开),但没有它的片段将在颜色缓冲区中可见。
You can completely disable writes to the color buffer using
If you draw something after this, it will still get depth tested (if that's turned on) and its depth values will still be written to the depth buffer (if that's turned on) but none of its fragments will be visible in the color buffer.