Friday, February 9, 2007

关于Shadow Map


Shadow Map技术最早由Lance Williams于1978在一篇题为"Casting Curved Shadow on Curved Surfaces"的论文中提出,其基本思想是(摘录):


  1. A view of the scene is constructed from the point of view of the light source. Only the Z values and not the shading values need be computed and stored.

  2. A view of the scene is then constructed from the point of view of the observer's eye. A linear transformation exists which maps X, Y, Z points in the observer's view into X, Y, Z coordinates in the light source view. As each point is generated in the observer's view, it is transformed into the computed view in the light source before computing its shading value. If the point is not visible to the light source, it is in shadow and is shaded accordingly.
Shadow Map可以对付复杂的场景,其cost是线性增加的,这是它的优点。它最大的缺点就是aliasing,如果要追求完美的画面,那些锯齿将是你不得不面对的难题。因此接下来的算法改进,很多功夫都下在反锯齿(antialiasing)上。还有另外两个改进就是边缘模糊(Soft Shadow Map)和硬件加速。硬件加速实际上与Shadow Map本身算法改进没有太大关系,但是高性能硬件的出现,使得后续改进算法的实现变得轻松写意。

反锯齿第一役,PCF(Percentage Closer Filtering),来自于Pixar的William T. Reeves, David H. Salesin 和 Robert L. Cook的一篇名为"Rendering Antialiased Shadows with Depth Maps"的论文。既然不能对Shadow Map做Filtering,那么就对比较结果做Filtering是PCF的精髓所在(摘录):

Our solution reverses the order of the filtering and comparison steps. The z values of the depth map across the entire region are first compared against the depth of the surface being rendered. This sample transformation converts the depth map under the region into a binary image, which is then filtered to give the proportion of the region in shadow. The resulting shadows have soft, antialiased edges.

值得一提的是,PCF可以产生soft shadow,虽然并不是真实的soft shadow,因为它与surface和光源的距离无关。

反锯齿第二役,Perspective Shadow Maps,来自于Marc Stamminger和George Drettakis。该方法的创新之处在于Shadow Map在normalized device coordinate空间生成,如此就:providing high resolution for near objects, and decreased resolution as we move away from the viewpoint.

反锯齿第三役,Variance Shadow Maps,来自于William Donnelly和Andrew Lauritzen。

VSM的实现相当简单,虽然它的原理涉及到一些统计学的东西,其思路是(摘录):


Instead of storing a single depth value, we store the mean and mean squared of a distribution of depths, from which we can efficiently compute the variance over any filter region. Using the variance, we derive an upper bound on the fraction of a shaded fragment that is occlueded. We show that this bound often provedes a good approximation to the true occlusion, and can be used as an approximate value for rendering.


另外文中提到的Mipmapping和Gaussian blur技术,是anti-aliasing和soften shadow很有效的手段,并且mipmapping对提高效率也有很大帮助。目前的shader可以很容易将上述技术与Variance Shadow Map很好的结合,产生更加逼真的shadow。


在Xbox360的XDK的samples中,有很好的Mipmapping和Variance Shadow Map的例子。


关于Shadow Map,还有很多相关技术;这里主要是针对游戏行业进行的选择。

Tuesday, February 6, 2007

呼应

公司同事儿辞职,另一同事儿觊觎其靠窗座位;辞职同事儿遂恶作剧,在座位前的窗玻璃上贴出标语(被恶作剧者为男性):




第二日,公司对面office的窗玻璃上,回应标语赫然醒目,正所谓人间自有真情在,有情人终成眷属。

求反平方根(reciprocal square root)的快速实用算法

float InvSqrt(float x)
{
        float xhalf = 0.5f*x;
        int i = *(int*)&x;
        i = 0x5f3759df - (i>>1);
        x = *(float*)&i;
        x = x*(1.5f - xhalf*x*x);
        return x;
}

欲知算法详情,请看这里,希望你有耐性看完整篇推导。

Monday, February 5, 2007

看看Efren Reyes是怎么打球的

以下视频来自http://www.youtube.com

1995年的一场比赛,由Reyes对Earl Strickland

1996年,Reyes对Jim Rempe

关于Continuous Integration

推荐一篇Continuous Integration的文章,作者是Martin Fowler,请看这里

Martin 对Continuous Integration的定义如下:
Continuous Integration is a software development practice where members of a team integrate their work frequently, usually each person integrates at least daily - leading to multiple integrations per day. Each integration is verified by an automated build (including test) to detect integration errors as quickly as possible. Many teams find that this approach leads to significantly reduced integration problems and allows a team to develop cohesive software more rapidly.

我们公司用CruiseControl.NET来做Continuous Integration,效果不错。关于CruiseControl.NET的配置和应用,请直接google。至于Continuous Integration是否适用于你的项目,请大声阅读Martin的定义直至领悟。