please refer to http://www.mvps.org/directx/articles/linear_z/linearz.htm

When using a programmable vertex shader, we have direct control of the transformation process, and can implement our own.  Vertex position can be read from the input registers, manipulated however we like, then output as a 4D homogenous coordinate to the output position register.  However, there is one apparent problem at handling our linearity issue: the output from the shader is still homogenous, and will be divided in the same manner as the output from the fixed pipeline transformation would be.  So how do we handle this, if we can't eliminate the division operation?

The answer is actually pretty simple - just multiply Z by W prior to returning the result from the vertex shader.  The net effect is that Z*W/W = Z!  If we first divide Z by the far distance, to scale it to the range of 0.0 -> 1.0, we've got a linear result that will survive perspective division.  A simple HLSL implementation might look (in part) something like this:

  1. float4 vPos = mul(Input.Pos,worldViewProj);
  2. vPos.z = vPos.z * vPos.w / Far;
  3. Output.Pos = vPos;

To simplify this, instead of needing to divide by the far plane distance to scale Z, we could instead scale the values in the Z column of the projection matrix we use:

  1. D3DXMATRIX mProj;
  2. D3DXMatrixPerspectiveFovLH(&mProj,fFov,fNear,fFar);
  3. mProj._33/=fFar;
  4. mProj._43/=fFar;
  5. //...set to shader constant register or concatenate
  6. //...with world and view matrices first as needed

This reduces the vertex shader transformation to:

float4 vPos = mul(Input.Pos,worldViewProj);
vPos.z = vPos.z * vPos.w;
Output.Pos = vPos;

linearizing the depth in vertex shader的更多相关文章

  1. 学习笔记:GLSL Core Tutorial – Vertex Shader(内置变量说明)

    1.每个Vertex Shader都有用户定义的输入属性,例如:位置,法线向量和纹理坐标等.Vertex Shaders也接收一致变量(uniform variables). uniform vari ...

  2. GLSL写vertex shader和fragment shader

    0.一般来说vertex shader处理顶点坐标,然后向后传输,经过光栅化之后,传给fragment shader,其负责颜色.纹理.光照等等. 前者处理之后变成裁剪坐标系(三维),光栅化之后一般认 ...

  3. Stage3d 由浅到深理解AGAL的管线vertex shader和fragment shader || 简易教程 学习心得 AGAL 非常非常好的入门文章

    Everyday Stage3D (一) Everyday Stage3D (二) Triangle Everyday Stage3D (三) AGAL的基本概念 Everyday Stage3D ( ...

  4. vertex shader(4)

    Swizzling and Masking 如果你使用输入.常量.临时寄存器作为源寄存器,你可以彼此独立地swizzle .x,.y,.z,.w值.如果你使用输出.临时寄存器作为目标寄存器,你可以把. ...

  5. vertex shader(3)

    之前我们学习了如何声明顶点着色器.如何设置常量寄存器中的常量.接下来我们学习如何写和编译一个顶点着色器程序. 在我们编译一个顶点着色器之前,首先需要写一个. 有17种不同的指令(instruction ...

  6. vertex shader(2)

    一次只有一个vertex shader是活跃的.你可以有多个vertex shader,如果一个物体特殊的变换或者灯光,你可以选择合适的vertex shader来完成这个任务. 你可能想使用vert ...

  7. vertex shader(1)

    Vertex shader Architecture: 所有在vertex shader中的数据都用128-bit的quad-floats表示(4x32-bit). vertex shader线性地执 ...

  8. vertex shader must minimally write all four components of POSITION

    Though the POSITION semantic must be written out by the vertex shader, it cannot be read in by the p ...

  9. PlayCanvas PBR材质shader代码分析(vertex shader)

    顶点shader主要对顶点坐标变换,将顶点坐标从local->world->view->clip 空间变换 local空间:模型物体坐标系 world空间:世界空间坐标系 view空 ...

随机推荐

  1. C++ 引用(&)

    #include <iostream> void sort(int &a, int &b){ if (a>=b) { return; } if (a<b) { ...

  2. spring setter方法注入

    <bean id="dao" class="Dao"></bean> <bean id="service" c ...

  3. localstorage 使用

    localstorage作为HTML5的一个特殊属性,在发布时就备受关注:最近总结了其一些小的用法,希望可以抛砖引玉. 因HTML5本地存储只能存字符串,所以所有数据存储的话,都要转化成字符串:而js ...

  4. C#面向对象的学习笔记

    1.面向对象的3要素: 封装:将不需要显示的代码封装到一个方法中,只对外提供方法名,用户不需关心内部实现. 继承:子类继承父类,公用父类的代码,大大提高了代码的重用,贴近生活也符合人类的编程思想. 多 ...

  5. JavaScript学习总结【12】、JS AJAX应用

    1.AJAX 简介 AJAX(音译为:阿贾克斯) = Asynchronous JavaScript and XML(异步的 JavaScript 和 XML),是指一种创建交互式网页应用的网页开发技 ...

  6. Vim 中文件目录浏览插件——NERD tree

    说明 :vim的插件NERDTree用于使得vim窗口分左右窗口显示的用法说明.其中,左侧为目录的树形界面,简称为NERDTree界面,右则为vim界面. 一.配置步骤 下载地址: http://ww ...

  7. 自己编写的sublime text 3 插件

    一些小功能,比较杂. 具体的功能在这里查看 1.本地环境的php运行结果获取. 2.快捷打开常用的文件,文件夹,url.(ctrl+shift+a) 3.常用的缩进转换. 下边是网络爬虫代码. #py ...

  8. Maven构建灵活配置文件

    本文解决以下问题: Maven下面启动Main函数: 配置JDK版本 如何配置文件,在开发部署测试各个不同版本间无缝切换配置文件: 启动Main函数 Maven默认是不支持Main函数程序,需要在po ...

  9. 第 2 章 代理模式【Proxy Pattern】

    第 2 章 代理模式[Proxy Pattern] 以下内容出自:24种设计模式介绍与6大设计原则.pdf 什么是代理模式呢?我很忙,忙的没空理你,那你要找我呢就先找我的代理人吧,那代理人总要知道被代 ...

  10. bzoj 3052: [wc2013]糖果公园 带修改莫队

    3052: [wc2013]糖果公园 Time Limit: 250 Sec  Memory Limit: 512 MBSubmit: 506  Solved: 189[Submit][Status] ...