小编Luc*_*ian的帖子

深度作为GLSL中相机平面的距离

我有一对GLSL着色器,它为我提供了场景中对象的深度贴图.我现在得到的是每个像素到相机的距离.我需要的是获得从像素到相机平面的距离.让我用一点画来说明

   *          |--*
  /           |
 /            |
C-----*       C-----*
 \            |
  \           |
   *          |--*
Run Code Online (Sandbox Code Playgroud)

3个星号是像素,C是相机.星号的线条是"深度".在第一种情况下,我得到像素到相机的距离.在第二个中,我希望得到每个像素到平面的距离.

必须有一种方法可以通过使用一些投影矩阵来做到这一点,但我很难过.

这是我正在使用的着色器.请注意,eyePosition是camera_position_object_space.

顶点着色器:

void main() {
    position = gl_Vertex.xyz;
    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}
Run Code Online (Sandbox Code Playgroud)

像素着色器:

uniform vec3 eyePosition;
varying vec3 position;


void main(void) {
        vec3 temp = vec3(1.0,1.0,1.0);
        float depth = (length(eyePosition - position*temp) - 1.0) / 49.0;
        gl_FragColor = vec4(depth, depth, depth, 1.0);
}
Run Code Online (Sandbox Code Playgroud)

opengl 3d shader glsl depth

8
推荐指数
1
解决办法
1万
查看次数

标签 统计

3d ×1

depth ×1

glsl ×1

opengl ×1

shader ×1