使用着色器在opengl中绘制深度值 [英] draw the depth value in opengl using shaders

查看:265
本文介绍了使用着色器在opengl中绘制深度值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在片段着色器中绘制深度缓冲区,我这样做:

I want to draw the depth buffer in the fragment shader, I do this:

顶点着色器:

varying vec4 position_;

gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
position_ = gl_ModelViewProjectionMatrix * gl_Vertex;

片段着色器:

float depth = ((position_.z / position_.w) + 1.0) * 0.5;

gl_FragColor = vec4(depth, depth, depth, 1.0);

但是我打印的全部是白色的,我在做什么错了?

But all I print is white, what am I doing wrong?

推荐答案

您想在哪个空间绘制深度?如果要绘制窗口空间深度,可以执行以下操作:

In what space do you want to draw the depth? If you want to draw the window-space depth, you can do this:

gl_FragColor = vec4(gl_FragCoord.z);

但是,这并不是特别有用,因为大多数数字将非常接近1.0.只有非常接近的物体才可见.这就是使用标准透视投影的深度缓冲区的深度值分布的本质.

However, this will not be particularly useful, since most of the numbers will be very close to 1.0. Only extremely close objects will be visible. This is the nature of the distribution of depth values for a depth buffer using a standard perspective projection.

或者,换句话说,这就是为什么你变白的原因.

Or, to put it another way, that's why you're getting white.

如果要将这些值放在线性空间中,则需要执行以下操作:

If you want these values in a linear space, you will need to do something like the following:

float ndcDepth = ndcPos.z =
    (2.0 * gl_FragCoord.z - gl_DepthRange.near - gl_DepthRange.far) /
    (gl_DepthRange.far - gl_DepthRange.near);
float clipDepth = ndcDepth / gl_FragCoord.w;
gl_FragColor = vec4((clipDepth * 0.5) + 0.5); 

这篇关于使用着色器在opengl中绘制深度值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆