天天看點

Surface Normal Averaging

摘要abstract:正确設定網格面上點的法向,對幾何體在光照等情況下顯示得更真實,這樣就可以減少頂點數量,提高渲染速度。本文通過将opencascade中的形狀離散成網格資料後在openscenegraph中顯示,及使用osg的快速法向osgutil::smoothingvisitor優化與使用opencascade來計算正确的法向的結果的對比,說明面法向量的重要性。    關鍵字key words:opencascade, openscenegraph, normal averaging, triangulation mesh

surface normal averaging

[email protected]

摘要abstract:正确設定網格面上點的法向,對幾何體在光照等情況下顯示得更真實,這樣就可以減少頂點數量,提高渲染速度。本文通過将opencascade中的形狀離散成網格資料後在openscenegraph中顯示,及使用osg的快速法向osgutil::smoothingvisitor優化與使用opencascade來計算正确的法向的結果的對比,說明面法向量的重要性。 

關鍵字key words:opencascade, openscenegraph, normal averaging, triangulation mesh 

opengl中的頂點(vertex)不是一個值,而由其空間坐标值、法向、顔色坐标、紋理坐标、霧坐标等所組成的一個集合。一個最基本的幾何體對象至少需要設定一個合法的頂點數組,并記錄頂點資料;如有必要,還可以設定顔色數組、法線數組、紋理坐标數組等多種資訊。 

在很多應用中,網格上的各點都需要一個表面法向量,它的作用非常廣泛。例如可用來計算光照、背面剔除、模拟粒子系統在表面的“彈跳”效果、通過隻需要正面而加速碰撞檢測等。 

Surface Normal Averaging

figure 1.1 lighting on a surface 

Surface Normal Averaging

figure 1.2 light is reflected off objects at specific angles 

如上圖所示,物體在光照情況下的反射光等的計算是與法向n有關的。 

在opencascade中可以将拓樸形狀轉換成stl格式的檔案進行模型的資料交換。其中stl結構中隻儲存了三角網格的頂點坐标和三角面的法向量。為了将拓樸資料轉換成stl的網格資料,先将拓樸形狀進行三角剖分,再将剖分的網格儲存成stl即可。其中每個三角面的法向計算也是直接根據兩個向量的叉乘得來。 

Surface Normal Averaging

figure 2.1 a normal vector as cross product of two vectors 

實作檔案是rwstl.cxx,其中計算法向的程式代碼如下所示: 

這種方式渲染的圖形效果如下圖所示: 

Surface Normal Averaging

figure 2.2 a typical sphere made up of triangles 

上面的球面是由三角形組成,由opencascade的三角剖分算法生成。如果将每個三角面的法向作為每個頂點的法向,則渲染效果如下圖所示: 

Surface Normal Averaging

figure 2.3 specific the triangle face normal as the vertex normal of the trangle 

如上圖所示,在光照效果下每個三角面界限分明,感覺不是很光滑,面之間的過渡很生硬。 

直接将網格頂點的法向設定成三角面的法向産生的效果不是很理想,通過改變頂點法向的方向可以讓曲面更滑,這種技術稱為法向平均(normal averaging)。利用法向平均技術可以産生一些有意思的視覺效果。如果有個面像下面圖所示: 

Surface Normal Averaging

figure 3.1 jagged surface with the usual surface normals 

當我們考慮兩個相連面的頂點處的法向為兩相連面的法向的平均值時,那麼這兩個相連表面的連接配接處在opengl中渲染時看上去就不那麼棱角分明了,如下圖所示: 

Surface Normal Averaging

figure 3.2 averaging the normals will make sharp corners appear softer 

對于球面或更一般的自由曲面,法向平均的算法也是适用的。如下圖所示: 

Surface Normal Averaging

figure 3.3 an approximation with normals perpendicular to each face 

Surface Normal Averaging

figure 3.4 each normal is perpendicular to the surface itself 

球面的法向計算還是相當簡單的。但是對于一般的曲面就不是那麼容易了。這種情況下需要計算多邊形面片相連處的頂點的法向,将相連接配接處的頂點的法向設定為各相鄰面的平均法向後,視覺效果還是很棒的,光滑。 

the actual normal you assign to that vertex is the average of these normals. the visual effect is a nice, smooth, regular surface, even though it is actually composed of numerous small, flat segments. 

在openscenegraph中生成頂點法向量的類是osgutil::smoothingvisitor,它使用了visitor的模式,通過周遊場景中的幾何體,生成頂點的法向量。對于上面同一個球的網格,使用osgutil::smoothingvisitor生成法向後在光照下的顯示效果如下圖所示: 

Surface Normal Averaging

figure 3.5 use osgutil::smoothingvisitor to generate normals for the sphere 

不管是stl中三角面的法向還是使用osgutil::smoothingvisitor來生成面的法向都是無奈之舉,因為都是在離散的三角網格上找出法向,不精确,在光照下渲染效果都不是很理想。但是opencascade作為幾何造型核心,提供了計算曲面法向的功能,是以有能力計算出頂點處的法向的精确值。 

當計算網格曲面頂點的法向時,共享頂點處的法向最好設定為頂點各相連面的法向的平均值。對于參數化的曲面,是可以直接計算出每個頂點處的法向,就不需要再求法向平均值了,因為已經有了曲面法向數學定義的值。是以在opencascade中計算出來曲面中某個頂點的法向就是數學定義上面的法向。計算方法如下: 

對頂點處的參數u,v分别求一階導數,得出頂點處在u,v方向的切向量,如下圖所示: 

Surface Normal Averaging

figure 4.1 derivatives with respect to u and v  

Surface Normal Averaging

figure 4.1 tangents on a surface 

将u和v方向的切向量叉乘就得到了該頂點處的法向,計算方法如下所示: 

Surface Normal Averaging

叉乘後頂點處的法向如下圖所示: 

Surface Normal Averaging

figure 4.2 normal on a surface 

opencascade中計算曲面表面屬性的類是breplprop_slprops,計算法向部分程式如下所示: 

此類的使用方法如下所示: 

計算法向後渲染效果如下圖所示: 

Surface Normal Averaging

figure 4.3 sphere vertex normals computed by breplprop_slprops 

由圖可知,opencascade計算的面的法向在渲染時效果很好。 

将這三種情況産生的渲染效果放在一起來比較,程式代碼如下所示: 

生成效果圖如下所示: 

Surface Normal Averaging

figure 5.1 same sphere triangulation mesh 

Surface Normal Averaging

figure 5.2 same sphere mesh with different vertex normals 

由上圖可知,相同的球面網格,當頂點的法向為三角面的法向時,在有光照的情況下,渲染效果最差。使用osgutil::smoothingvisitor法向生成算法生成的頂點法向與使用類breplprop_slprops計算出的法向,在光照情況下顯示效果相當。 

Surface Normal Averaging

figure 5.3 pipe and equipments with correct vertex normals 

Surface Normal Averaging

正确設定網格面頂點的法向可以在光照環境中看上去更光滑真實。利用法向平均算法或使用曲面的參數方程求解曲面頂點上法向,可以在滿足顯示效果基本相同的條件下減少網格頂點的數量,可以提高渲染速度。 

1. waite group press, opengl super bible(1st), macmillan computer publishing, 1996 

2. richard s. wright jr., benjamin lipchak, opengl superbible(3rd), sams publishing, 2004 

3. vsocc.cpp in netgen 

4. kelly dempski, focus on curves and surfaces, premier press, 2003 

5. 王銳,錢學雷,openscenegraph三維渲染引擎設計與實踐,清華大學出版社 

6. 肖鵬,劉更代,徐明亮,openscenegraph三維渲染引擎程式設計指南,清華大學出版社 

pdf version: surface normal averaging