设向量为V, 平面normal为N, 其在平面上的投影向量为V'。
Normalize V和N。
v = normlize(V);
n = normaize(N);
计算v和n的夹角cos值,如果是负值则翻转n和cos值,保证所取的normal和V夹角在180以内。
cosVN = dot(v, n);
if(cosVN < 0)
{
cosVN = -cosVN;
n = -n;
}
根据V的长度(V_L)和V和N夹角的cos(cosVN) 计算N'长度(N'_L)和N'。N'满足其终点和V终点连线和平面平行。
N'_L = V_L * cosVN';
N' = N'_L * n;
最后算出V'。
V' = V - N';
2011年2月17日星期四
点到面的距离公式推导
求点P(x0,y0,z0)到平面Q: Ax + By + Cz + D = 0的距离d。
平面Q的normalized normal为N = (A, B, C) / sqrt(A^2 + B^2 + C^2)。这样,必存在一scalor s,使得点P - sN在平面上(s可正可负)。将P - sN带入平面Q的方程, 求得s = (Ax0 + By0 + Czo + D) / sqrt(A^2 + B^2 + C^2)。
将s取绝对值即为d。
d = | s | = | Ax0 + By0 + Czo + D | / sqrt(A^2 + B^2 + C^2)
平面Q的normalized normal为N = (A, B, C) / sqrt(A^2 + B^2 + C^2)。这样,必存在一scalor s,使得点P - sN在平面上(s可正可负)。将P - sN带入平面Q的方程, 求得s = (Ax0 + By0 + Czo + D) / sqrt(A^2 + B^2 + C^2)。
将s取绝对值即为d。
d = | s | = | Ax0 + By0 + Czo + D | / sqrt(A^2 + B^2 + C^2)
2010年10月14日星期四
Bump Mapping [转]
Bump mapping is very much like Texture Mapping. However, where Texture Mapping added colour to a polygon, Bump Mapping adds, what appears to be surface roughness. This can have a dramatic effect on the look of a polygonal object. Bump Mapping can add minute detail to an object which would otherwise require a large number of polygons. Note that the polygon is still physically flat, but appears to a be bumpy.
The theory behind Bump Mapping
What is a Bump Map
So how's it done
x_gradient = pixel(x-1, y) - pixel(x+1, y) y_gradient = pixel(x, y-1) - pixel(x, y+1)
New_Normal = Normal + (U * x_gradient) + (V * y_gradient)
From: http://freespace.virgin.net/hugo.elias/graphics/x_polybm.htm
2010年9月17日星期五
Texture Address Mode (Clamp, Wrap)
1. Clamp
这个寻址模式让所有的纹理坐标截取到[0,1]范围内,所有小于0的坐标截取到0,大于1的截取到1。
结果是,超出[0,1] 范围的纹理坐标的所有像素的颜色为纹理边缘的颜色。
2. Wrap
使用这个模式,显卡会从坐标加上或减去1直到坐标仍回到[0,1]范围。
这会导致原始纹理被复制。
这个寻址模式让所有的纹理坐标截取到[0,1]范围内,所有小于0的坐标截取到0,大于1的截取到1。
结果是,超出[0,1] 范围的纹理坐标的所有像素的颜色为纹理边缘的颜色。
2. Wrap
使用这个模式,显卡会从坐标加上或减去1直到坐标仍回到[0,1]范围。
这会导致原始纹理被复制。
下图是两种addressing mode的比较,左为Clamp, 右为Wrap。
纹理过滤模式中的Bilinear、Trilinear以及Anistropic Filtering <转>
1、 为什么在纹理采样时需要texture filter(纹理过滤)。


我们的纹理是要贴到三维图形表面的,而三维图形上的pixel中心和纹理上的texel中心并不一至(pixel不一定对应texture上的采样中心texel),大小也不一定一至。当纹理大于三维图形表面时,导至一个像素被映射到许多纹理像素上;当维理小于三维图形表面时,许多个象素都映射到同一纹理。
当这些情况发生时,贴图就会变得模糊或发生错位,马赛克。要解决此类问题,必须通过技术平滑texel和pixel之间的对应。这种技术就是纹理滤波。
不同的过滤模式,计算复杂度不一样,会得到不同的效果。过滤模式由简单到复杂包括:Nearest Point Sampling(最近点采样),Bilinear(双线性过滤)、Trilinear(三线性过滤)、Anisotropic Filtering(各向异性过滤)。
在了解这些之前,有必要了解什么是MipMap和什么时各向同性,各向异性。
2、 什么是MipMap?
Mipmap由Lance Williams 在1983的一篇文章“Pyramidal parametrics”中提出。Wiki中有很详细的介绍( http://en.wikipedia.org/wiki/Mipmap ) . 比如一张256X256的图,在长和宽方向每次减少一倍,生成:128X128,64X64,32X32,16X16,8X8,4X4,2X2,1X1,八张图,组成MipMap,如下图示。
Mipmap早已被硬件支持,硬件会自动为创建的Texture生成mipmap的各级。在D3D的API:CreateTexture中有一个参数levels,就是用于指定生成mipmap到哪个级别,当不指定时就一直生成到1X1。
3、 什么是各向同性和各向异性?
当需要贴图的三维表面平行于屏幕(viewport),则是各向同性的。当要贴图的三维表面与屏幕有一定角度的倾斜,则是各向异性的。也可以这样理解,当一个texture贴到三维表面上从Camera看来没有变形,投射到屏幕空间中后U方向和V方向比例仍然是一样的,便可以理解成各向同性。反之则认为是各向异性。
4、 Nearest Point Sampling(最近点采样)
这个最简单,每个像素的纹理坐标,并不是刚好对应Texture上的一个采样点texel,怎么办呢?最近点采样取最接近的texel进行采样。
当纹理的大小与贴图的三维图形的大小差不多时,这种方法非常有效和快捷。如果大小不同,纹理就需要进行放大或缩小,这样,结果就会变得矮胖、变形或模糊。
5、 Bilinear(双线性过滤)
双线性过滤以pixel对应的纹理坐标为中心,采该纹理坐标周围4个texel的像素,再取平均,以平均值作为采样值。
双线性过滤像素之间的过渡更加平滑,但是它只作用于一个MipMap Level,它选取texel和pixel之间大小最接近的那一层MipMap进行采样。当和pixel大小匹配的texel大小在两层Mipmap level之间时,双线性过滤在有些情况效果就不太好。于是就有了三线性过滤。
6、 Trilinear(三线性过滤)
三线性过滤以双线性过滤为基础。会对pixel大小与texel大小最接近的两层Mipmap level分别进行双线性过滤,然后再对两层得到的结果进生线性插值。
三线性过滤在一般情况下效果非常理想了。但是到目前为止,我们均是假设是texture投射到屏幕空间是各向同性的。但是当各向异性的情况时,效果仍然不理想,于是产生了Anisotropic Filtering(各向异性过滤)。
7、 Anisotropic Filtering(各向异性过滤)
先看效果,左边的图采用三线性过滤,右边的图采用各向异性过滤。
各向同性的过滤在采样的时候,是对正方形区域里行采样。各向异性过滤把纹理与屏幕空间的角度这个因素考虑时去。简单地说,它会考滤一个pixel(x:y=1:1)对应到纹理空间中在u和v方向上u和v的比例关系,当u:v不是1:1时,将会按比例在各方向上采样不同数量的点来计算最终的结果(这时采样就有可能是长方形区域)。
我们一般指的Anisotropic Filtering(AF)均是基于三线过滤的Anisotropic Filtering,因此当u:v不为1:1时,则Anisotropic Filtering比Trilinear需要采样更多的点,具体要采多少,取决于是多少X的AF,现在的显卡最多技持到16X AF。
当开启16X AF的时候,硬件并不是对所有的texture采样都用16X AF,而是需要先计算屏幕空间与纹理空间的夹角(量化后便是上面所说的u:v),只有当夹角大到需要16X时,才会真正使用16X.
如果想了解AF的实现原理,可以查阅此篇Paper: “Implementing an anisotropic texture filter”. 现在AF都是硬件实现,因此只有少数人才清楚AF就尽是怎样实现了(其实细节我也没搞清楚),其实完全可以由Pixel Shader来实现AF,当然性能和由硬件做是没得比的。
8、 各过滤模式性能比较。
下表是各种过滤模式采一个pixel需要sample的次数:
| Sample Number | |
| Nearest Point Sampling | 1 |
| Bilinear | 4 |
| Trilinear | 8 |
| Anisotropic Filtering 4X | 32 |
| Anisotropic Filtering 16X | 128 |
Anisotropic Filtering 16X效果最好,但是显卡Performance会下降很多,当然也是测试你手中显卡Texture Unit的好方法。如果你觉得你的显卡够牛,那么就把AA和AF都打到最高再试试吧:)
2010年7月1日星期四
[转] Phong lighting model
From wikipedia:
Phong reflection is an empirical model of local illumination. It describes the way a surface reflects light as a combination of the diffuse reflection of rough surfaces with the specular reflection of shiny surfaces. It is based on Bui Tuong Phong's informal observation that shiny surfaces have small intense specular highlights, while dull surfaces have large highlights that fall off more gradually. The reflection model also includes an ambient term to account for the small amount of light that is scattered about the entire scene.
For each light source in the scene, we define the components is and id as the intensities (often as RGB values) of the specular and diffuse components of the light sources respectively. A single term ia controls the ambient lighting; it is sometimes computed as a sum of contributions from all light sources.
For each material in the scene, we define:
ks: specular reflection constant, the ratio of reflection of the specular term of incoming light
Note L, N, R, V are all unit vecters.
Then the Phong reflection model provides an equation for computing the shading value of each surface point Ip:
Although the above formulation is the common way of presenting the Phong model, a particular term in the sum should only be included if it is positive, i.e. the equation is formally incorrect.
Therefore, in the above fomulation, (Lm·N) should be exactly max(Lm·N, 0) and similarly (Rm·V) should be max(Rm·V, 0).
Phong reflection is an empirical model of local illumination. It describes the way a surface reflects light as a combination of the diffuse reflection of rough surfaces with the specular reflection of shiny surfaces. It is based on Bui Tuong Phong's informal observation that shiny surfaces have small intense specular highlights, while dull surfaces have large highlights that fall off more gradually. The reflection model also includes an ambient term to account for the small amount of light that is scattered about the entire scene.
For each light source in the scene, we define the components is and id as the intensities (often as RGB values) of the specular and diffuse components of the light sources respectively. A single term ia controls the ambient lighting; it is sometimes computed as a sum of contributions from all light sources.
For each material in the scene, we define:
ks: specular reflection constant, the ratio of reflection of the specular term of incoming light
kd: diffuse reflection constant, the ratio of reflection of the diffuse term of incoming light (Lambertian reflectance)
ka: ambient reflection constant, the ratio of reflection of the ambient term present in all points in the scene rendered
α: is a shininess constant for this material, which is larger for surfaces that are smoother and more mirror-like. When this constant is large the specular highlight is small.
We further define lights as the set of all light sources, L as the direction vector from the point on the surface toward each light source, N as the normal at this point on the surface, R as the direction that a perfectly reflected ray of light would take from this point on the surface, and V as the direction pointing towards the viewer (such as a virtual camera).
Note L, N, R, V are all unit vecters.
Then the Phong reflection model provides an equation for computing the shading value of each surface point Ip:
The diffuse term is not affected by the viewer direction (V). The specular term is large only when the viewer direction (V) is aligned with the reflection direction R. Their alignment is measured by the α power of the cosine of the angle between them. The cosine of the angle between the normalized vectors R and V is equal to their dot product. When α is large, in the case of a nearly mirror-like reflection, the specular highlight will be small, because any viewpoint not aligned with the reflection will have a cosine less than one which rapidly approaches zero when raised to a high power.
When we have color representations as RGB values, this equation will typically be calculated separately for R, G and B intensities.Although the above formulation is the common way of presenting the Phong model, a particular term in the sum should only be included if it is positive, i.e. the equation is formally incorrect.
Therefore, in the above fomulation, (Lm·N) should be exactly max(Lm·N, 0) and similarly (Rm·V) should be max(Rm·V, 0).
2010年6月25日星期五
Projective Texture Mapping
Projective Texture Mapping是将texture用投影的方法投射到物体上的一种texture mapping方法。从顶点到texture坐标的变换如下图右所示:
在OpenGL里,实现projective mapping可以用两种texture生成方法, object linear和eye linear, 其变换矩阵分别如下图:
Object Linear Texgen左乘的对象是顶点在object space的坐标,Eye Linear Texgen左乘的对象则是顶点在view space的坐标,一般使用Eye Linear Texgen来实现不同object coordinate system的物体的统一投影效果(如shadow mapping)。用OpenGL的实现Eye Linear Texgen,如果用R表示 [0,1] range transformation matrix,则需要将R*Pp*Vp的四个row vector分别设为s,t,r,q的eye plane。
最后发两张NV教学文档里的效果图:

订阅:
博文 (Atom)




