前几天在shadertech网站上看到一个获金牌的效果,叫relief mapping,其中包含relief texture mapping和steep parallax mapping,效果非常好,于是开始琢磨着如何来实现其效果,由于steep parallax mapping需要ps3.0的支持,relief texture mapping需要二叉树的搜索,都对资源要求比较高,所以暂时实现了parallax mapping,发现效果非常好,比normal mapping还要好。
其实现方法主要是需要计算切线空间内的几个分量,需要normal, tangent, binormal这三个分量,一般来说,法线分量比较容易计算,而binormal分量则是通过tangent和normal的叉乘得到,而tangent的计算就相对复杂一些,以下是C++代码:
for ( DWORD i=0; i<dwTotalTri; i++ )
{
int nOffset = 3 * i;
int idxVertex0 = tri[nOffset];
int idxVertex1 = tri[nOffset + 1];
int idxVertex2 = tri[nOffset + 2];
D3DXVECTOR3 v0 = D3DXVECTOR3(vertex[idxVertex0]);
D3DXVECTOR3 v1 = D3DXVECTOR3(vertex[idxVertex1]);
D3DXVECTOR3 v2 = D3DXVECTOR3(vertex[idxVertex2]);
D3DXVECTOR2 w0 = D3DXVECTOR2(vertex[idxVertex0].u, vertex[idxVertex0].v);
D3DXVECTOR2 w1 = D3DXVECTOR2(vertex[idxVertex1].u, vertex[idxVertex1].v);
D3DXVECTOR2 w2 = D3DXVECTOR2(vertex[idxVertex2].u, vertex[idxVertex2].v);
float x1 = v1.x - v0.x;
float x2 = v2.x - v0.x;
float y1 = v1.y - v0.y;
float y2 = v2.y - v0.y;
float z1 = v1.z - v0.z;
float z2 = v2.z - v0.z;
float s1 = w1.x - w0.x;
float s2 = w2.x - w0.x;
float t1 = w1.y - w0.y;
float t2 = w2.y - w0.y;
float r = 1.0F / (s1 * t2 - s2 * t1);
D3DXVECTOR3 sdir((t2 * x1 - t1 * x2) * r, (t2 * y1 - t1 * y2) * r, (t2 * z1 - t1 * z2) * r);
D3DXVECTOR3 tdir((s1 * x2 - s2 * x1) * r, (s1 * y2 - s2 * y1) * r, (s1 * z2 - s2 * z1) * r);
tan1[idxVertex0] += sdir;
tan1[idxVertex1] += sdir;
tan1[idxVertex2] += sdir;
tan2[idxVertex0] += tdir;
tan2[idxVertex1] += tdir;
tan2[idxVertex2] += tdir;
}
for( i = 0; i < dwTotalVertex; i ++ )
{
D3DXVECTOR3 &N = vertex[i]]->normal;
D3DXVECTOR3 &T = tan1[i];
float a = N.x * T.x + N.y * T.y + N.z * T.z;
D3DXVec3Normalize(&vertex[i]->tangent, &(T - N * a));
D3DXVECTOR3 temp;
D3DXVec3Normalize(&vertex[i]->binormal, D3DXVec3Cross(&temp, &N, &vertex[i]->tangent));
}
这样就可以得到tangent和binormal以及normal三个分量,然后在VS里面将这三个分量分别点乘Light的xyz三个分量,得到一个新的float3,再讲这三个分量分别点乘vEye的xyz三个分量得到一个新的float3,这样做的目的是将vEye和vLight的位置转换到切线空间内。然后PS里再针对高度信息来计算bump的颜色,即可。