linearizing the depth in vertex shader
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的更多相关文章
- 学习笔记:GLSL Core Tutorial – Vertex Shader(内置变量说明)
1.每个Vertex Shader都有用户定义的输入属性,例如:位置,法线向量和纹理坐标等.Vertex Shaders也接收一致变量(uniform variables). uniform vari ...
- GLSL写vertex shader和fragment shader
0.一般来说vertex shader处理顶点坐标,然后向后传输,经过光栅化之后,传给fragment shader,其负责颜色.纹理.光照等等. 前者处理之后变成裁剪坐标系(三维),光栅化之后一般认 ...
- Stage3d 由浅到深理解AGAL的管线vertex shader和fragment shader || 简易教程 学习心得 AGAL 非常非常好的入门文章
Everyday Stage3D (一) Everyday Stage3D (二) Triangle Everyday Stage3D (三) AGAL的基本概念 Everyday Stage3D ( ...
- vertex shader(4)
Swizzling and Masking 如果你使用输入.常量.临时寄存器作为源寄存器,你可以彼此独立地swizzle .x,.y,.z,.w值.如果你使用输出.临时寄存器作为目标寄存器,你可以把. ...
- vertex shader(3)
之前我们学习了如何声明顶点着色器.如何设置常量寄存器中的常量.接下来我们学习如何写和编译一个顶点着色器程序. 在我们编译一个顶点着色器之前,首先需要写一个. 有17种不同的指令(instruction ...
- vertex shader(2)
一次只有一个vertex shader是活跃的.你可以有多个vertex shader,如果一个物体特殊的变换或者灯光,你可以选择合适的vertex shader来完成这个任务. 你可能想使用vert ...
- vertex shader(1)
Vertex shader Architecture: 所有在vertex shader中的数据都用128-bit的quad-floats表示(4x32-bit). vertex shader线性地执 ...
- 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 ...
- PlayCanvas PBR材质shader代码分析(vertex shader)
顶点shader主要对顶点坐标变换,将顶点坐标从local->world->view->clip 空间变换 local空间:模型物体坐标系 world空间:世界空间坐标系 view空 ...
随机推荐
- Ambiguous handler methods mapped for HTTP path
一.问题:映射重复导致的错误 java代码如下: @RequestMapping(value = "/info/{remove}/{id}", method = RequestMe ...
- DAG模型——嵌套矩阵
有向无环图上的动态规划是学习动态规划的基础,很多问题都可以转化为DAG上的最长路.最短路或路径计数问题. 嵌套矩阵 有n个矩阵,每个矩阵可以用两个整数a,b描述,表示它的长和宽.矩阵X(a,b)可以嵌 ...
- c语言中继承和多态的简单实现
C语言本身是不支持继承和多态的,但其实在 C 的世界里,有一套非常有名的面向对象的框架,用的也非常广,那就是 GObject,它是整个图形界面开发库 GTK 的基石,在IBM developerWor ...
- git: command not found
在使用git时,出现“git: command not found”的情况,于是使用yum安装: [root@localhost ~]# yum install -y gitLoaded plugin ...
- 获取本机IP、mac地址、计算机名
python获取本机IP.mac地址.计算机名 在python中获取ip地址和在php中有很大不同,我们先来看一下python 获得本机MAC地址: >>> import uuid ...
- Oracle安装时忘记解锁scott用户的解决方案
最近笔者开始学习Oracle,对于笔者同学安装过程出现各种问题而导致重做系统表示默哀. (1)问题1:64位操作系统安装完以后选择java.exe文件后.sqldeveloper闪退,原因是64位操作 ...
- 由 OR 引起的死循环
在客商迁移测试时,程序一旦开始执行就不能自动停止.只能通过手动中断应用服务器的进程来停止.检查迁移的一个表,这个表迁移前没有数据,迁移最多会插入3w条左右数据,但是迁移过程执行2个多小时候再看,已经有 ...
- 对ARM9哈佛结构的认识
书本上都说ARM是哈佛结构,但是我总感觉好像看不出来.后来针对S3C2440的ARM9核进行分析,我有了自己的见解. 我的结论是“ARM9被称为是哈佛结构是从它拥有指令cache和数据cache”来说 ...
- linux禁止root用户直接登录sshd并修改默认端口
linux最高权限用户root,默认可以直接登录sshd.为了提高服务器的安全度,需要对它进行禁止,使得攻击者无法通过暴力破解来获取root权限. 1,新建一个用户: #useradd xxx (xx ...
- delphi xe5 android 开发数据访问手机端(一)
上几片文章我们把供手机端调用的web服务完成,接下来实现手机端调用webservices获取数据 1.新建firemonkey mobile application 2.选择blank applica ...