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:

float4 vPos = mul(Input.Pos,worldViewProj);
vPos.z = vPos.z * vPos.w / Far;
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:

D3DXMATRIX mProj;
D3DXMatrixPerspectiveFovLH(&mProj,fFov,fNear,fFar);
mProj._33/=fFar;
mProj._43/=fFar;
//...set to shader constant register or concatenate
//...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. HDU 1619 Unidirectional TSP(单向TSP + 路径打印)

    Unidirectional TSP Problem Description Problems that require minimum paths through some domain appea ...

  2. IOS 学习笔记 2015-04-08 OC-NSUserDefaults 持久化对象

    NSUserDefaults适合存储轻量级的本地数据,比如要保存一个登陆界面的数据,用户名.密码之类的; NSUserDefaults被设计用来存储设备和应用的配置信息,它通过一个工厂方法返回默认的. ...

  3. ASP.NET中的母版页

    添加一个"母版页",使用<asp:ContentPlaceHolder>挖坑,新建的母版页已经自动设置了两个ContentPlaceHolder创建使用母版页的具体页面 ...

  4. Graphviz使用简介(中文乱码的问题)

    Graphviz使用简介 graphviz是基于dot语言的绘图工具,可以画有向图.无向图.关系图.目录图.流程图等.具体作用可见它的官方网站 一些参考的网址: http://www.open-ope ...

  5. 忘记mysql的root密码

    如果忘记root密码或其他用户密码,不要急,按下面操作即可.1. 编辑mysql主配置文件 my.cnfvim /etc/my.cnf   在[mysqld]字段下添加参数  skip-grant   ...

  6. 【转】关于oracle with as用法

    原文链接:关于oracle with as用法 with as语法–针对一个别名with tmp as (select * from tb_name) –针对多个别名with   tmp as (se ...

  7. hbuider 中点击就显示出一个单选的列表 ,然后后台跨域向里面动态添加数据,注意里面的格式是json object

    jsp页面: <li class="mui-table-view-cell" onclick="showActionSheet()"> <di ...

  8. 搭建mysql主从复制---Mysql Replication

    主从复制原理 Mysql的Replication是一个异步的复制过程,从一个Mysql Instance(master)复制到另一个Mysql Instance(slave).中间需要三个线程slav ...

  9. 《C和指针》章节后编程练习解答参考——第10章

    10.1 #include <stdio.h> typedef struct { unsigned ]; unsigned ]; unsigned ]; }TelphoneNumber; ...

  10. 【ASP.NET】从服务器端注册客户端脚本

    一.在Asp.net 服务端处理脚本,一般都用 ClientScriptManager ,即web窗体服务端的this.ClientScript.该对象比较常用的方法: 1.RegisterArray ...