简体   繁体   English

深度检查OpenGL(glsl)

[英]Depth Checking OpenGL(glsl)

我想知道是否有一种方法可以知道是否显示了特定的像素(例如,我想获取场景后方的所有位置,这意味着未在近平面中显示的所有背景对象)。

It's probably faster and easier to do some simple calculations yourself CPU side. 在CPU端自己进行一些简单的计算可​​能更快,更轻松。 For example... 例如...

clipSpacePoint = projectionMatrix * viewMatrix * modelMatrix * vec4f(0, 0, 0, 1); //using the model's origin and transform matrix
//or
clipSpacePoint = projectionMatrix * viewMatrix * vec4f(x, y, z, 1); //using the model's position

clipSpacePoint.xyz /= clipSpacePoint.w; //possible division by zero

//point is visible if clipSpacePoint.w is positive and clipSpacePoint.xyz are between -1 and 1

Alternatively, and to better answer your question, you can use the occlusion query to check how many fragments have been rendered during a draw call. 另外,为了更好地回答您的问题,您可以使用遮挡查询来检查在绘制调用期间渲染了多少片段。 An example application might be checking if a bright light is visible and drawing a lens flare if it is. 一个示例应用程序可能正在检查是否可以看到强光,如果可见,则绘制镜头光晕。 The occlusion query, like many OpenGL calls, run asynchronously so if you need the results right away you can stall the rendering pipeline. 像许多OpenGL调用一样,遮挡查询是异步运行的,因此如果您需要立即获得结果,则可以暂停渲染管线。 If you don't mind waiting a frame or two for the result it may run a bit faster. 如果您不介意等待一两帧以获得结果,则运行速度可能会更快一些。

This won't tell you where the pixels were drawn though. 但是,这不会告诉您像素在何处绘制。 Two ways to find the pixel locations come to mind... 我想到了两种查找像素位置的方法:

  1. You could write pixel coordinates in your fragment shader to a texture, use the histopyramid /stream compaction method to move them to the start of the texture, and read them back from the GPU. 您可以将片段着色器中的像素坐标写入纹理,使用histopyramid / stream压缩方法将其移至纹理的开头,然后从GPU读取它们。

  2. If you don't mind using recent GL features, ARB_atomic_counter can be used in the fragment shader to create a unique index which you could then use to write the fragment's coordinates to a buffer or texture with ARB_image_load_store. 如果您不介意使用最新的GL功能,则可以在片段着色器中使用ARB_atomic_counter创建唯一索引,然后将其用于使用ARB_image_load_store将片段的坐标写入缓冲区或纹理。 You'll probably also want to enable the early depth test for this too. 您可能还需要为此启用早期深度测试。

These are both far more complex than doing the point in box check or an occlusion query. 它们都比进行框内检查或遮挡查询要复杂得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM