深度在GLSL与相机的距离飞机 [英] Depth as distance to camera plane in GLSL

查看:520
本文介绍了深度在GLSL与相机的距离飞机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一双GLSL着色器给我对象的深度映射在我的场景。我现在得到的是从每个像素相机的距离。我需要的是从像素摄像头面得到的距离。让我来举例说明一个小客厅

  * |  -  *
  / |
 / |
Ç----- * C ----- *
 \ |
  \ |
   * |  -  *
 

3星号是像素和C是相机。从星号的行是深度。在第一种情况下,我得到从像素到摄像机的距离。第二,我想从每个像素的平面获得的距离。

必须有办法通过一些投影矩阵要做到这一点,但我很为难。

下面是我使用的着色器。需要注意的是eyePosition是camera_position_object_space。

顶点着色器:

 无效的主要(){
    位置= gl_Vertex.xyz;
    GL_POSITION = gl_ModelViewProjectionMatrix * gl_Vertex;
}
 

像素着色器:

 统一VEC3 eyePosition;
不同VEC3位置;


无效的主要(无效){
        VEC3温度= VEC3(1.0,1.0,1.0);
        浮深度=(长度(eyePosition  - 位置*温度) -  1.0)/ 49.0;
        gl_FragColor = vec4(深度,深度,深度1.0);
}
 

解决方案

你真的想这样做了艰辛的道路。简单地从那里转换的东西到摄像机空间,和工作。

 变化的浮动distToCamera;

无效的主要()
{
    vec4 cs_position = glModelViewMatrix * gl_Vertex;
    distToCamera = -cs_position.z;
    GL_POSITION = gl_ProjectionMatrix * cs_position;
}
 

在摄像机空间(该空间的一切是相对于该位置的摄像机的/取向),该平面的距离,以一个顶点为Z的只是负坐标(较高负Z是推远)。

所以,你的片段着色器甚至不需要 eyePosition ; 深度是直接从顶点着色器。

I have a pair of GLSL shaders that give me the depth map of the objects in my scene. What I get now is the distance from each pixel to the camera. What I need is to get the distance from the pixel to the camera plane. Let me illustrate with a little drawing

   *          |--*
  /           |
 /            |
C-----*       C-----*
 \            |
  \           |
   *          |--*

The 3 asterisks are pixels and the C is the camera. The lines from the asterisks are the "depth". In the first case, I get the distance from the pixel to the camera. In the second, I wish to get the distance from each pixel to the plane.

There must be a way to do this by using some projection matrix, but I'm stumped.

Here are the shaders I'm using. Note that eyePosition is camera_position_object_space.

Vertex Shader:

void main() {
    position = gl_Vertex.xyz;
    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}

Pixel Shader:

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);
}

解决方案

You're really trying to do this the hard way. Simply transform things to camera space, and work from there.

varying float distToCamera;

void main()
{
    vec4 cs_position = glModelViewMatrix * gl_Vertex;
    distToCamera = -cs_position.z;
    gl_Position = gl_ProjectionMatrix * cs_position;
}

In camera space (the space where everything is relative to the position/orientation of the camera), the planar distance to a vertex is just the negative of the Z coordinate (higher negative Z is farther away).

So your fragment shader doesn't even need eyePosition; the "depth" comes directly from the vertex shader.

这篇关于深度在GLSL与相机的距离飞机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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