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空 ...
随机推荐
- HDU 4294 A Famous Equation(DP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4249 题目大意:给一个a+b=c的表达式,但是a.b.c中部分位的数字丢失,并用?代替,问有多少种方案 ...
- leetcode169——Majority Element (C++)
Given an array of size n, find the majority element. The majority element is the element that appear ...
- IOS 学习笔记 2015-04-15 Xcode 工程模板分类
一 Application类型 我们大部分呢的开发工作都是使用Application类型的模板创建IOS程序开始的,该类型包括5个模板1 Master-Detail-Application ...
- Python没有执行__init__
疑惑 提出问题 前天同事问我一个问题,为什么这个脚本中的没有调用A 的__init__.脚本如下: class A(object): def __init__(self, *args, **kwarg ...
- Hack--兼容性测试
CSS hack由于不同的浏览器,比如Internet Explorer 6,Internet Explorer 7,Mozilla Firefox等,对CSS的解析认识不一样,因此会导致生成的页面效 ...
- 『奇葩问题集锦』Cannot find module 'webpack/lib/node/NodeTemplatePlugin'
第一步:npm config get prefix ,获取输出path“C:\Users\jaxGu\AppData\Roaming\npm”加上"\node_modules"用于 ...
- 浅谈JavaScript词法分析步骤
JavaScript代码运行前有一个类似编译的过程即词法分析,词法分析主要有三个步骤: 分析参数 再分析变量的声明 分析函数声明 具体步骤如下: 函数在运行的瞬间,生成一个活动对象(Active Ob ...
- Castle ActiveRecord配置文件中连接字符串解密
使用Castle ActiveRecord通常都是使用配置文件进行数据库连接配置.然后采用如下方式初始化: IConfigurationSource source = ConfigurationMan ...
- ACM俱乐部算法基础练习赛(1)
A: 水题 代码: #include<cstdio> #include<algorithm> using namespace std; ]; int n,m,c; int ma ...
- hdu 4435
一道枚举+搜索题: 很容易看出这道题目要求尽量不在大的城市里面建加油站: 所以从最大的城市开始枚举! 代码: #include<cstdio> #include<cmath> ...