| 導購 | 订阅 | 在线投稿
分享
 
 
 

教你一招:視點替用特效技術介紹

來源:互聯網  2008-05-19 00:45:18  評論

公告板及精靈的本質特點就是可以很容易的完成渲染,但在實時3D幾何圖形處理中它們是否還具有優勢…很好,實時3D。這有沒有可能找到一種在兩個世界中最佳的方法呢。

Billboards or sprites have the advantage of being very cheap to render, but real 3D geometry has the advantage of looking like it's... Well, real 3D. Wouldn't it be great if it were somehow possible to have the best of both worlds?

當然,在我沒有告訴你那些准許你得到兩個世界中最合理的那種技術之前我是不會這麽問你的。這種技術的就叫做「視點替用特效(光源替用特效)」。

Of course I wouldn't ask you this if I wasn't about to tell you that there is a technique that allows you to get the best of both worlds. This technique is called "impostoring".

1、引言(Say what?)

視點替用特效本質就是精靈 ― 可替代實時3D模型的2D圖像。但請把握住精靈是由美工手繪完成的,而視點替用特效則是由3D引擎本身實時進行渲染。

An impostor is essentially a sprite - a 2D image that replaces a real 3D model. The catch is that while sprites are usually drawn by artists, impostors are rendered at run-time, by the 3D engine itself.

讓我們假設你的場景中包含了一棵用視點替用特效表現的樹。3D引擎渲染的首先步驟是給3D樹模型加入紋理,然後把你在場景中寄予的紋理當做一個公告板。只要照相機不移動太大,視點替用特效就能這樣被保留。當照相機顯著地移動時,引擎也只不過重新渲染下視點替用特效,並且,你可以很好的去獲得其它的幀。

Let's say your scene contains a tree that will be represented by an impostor. The 3D engine first renders the 3D tree model to a texture, then places that texture in your scene as a billboard. As long as the camera doesn't move too much, the impostor can be kept as is. When the camera does move significantly, the engine simply re-renders the impostor, and you're good to go for another couple of frames.

你正在做什麽,事實是,渲染對象一旦進行,接下來的重新使用,對于隨後渲染的幾幀圖像而言幾乎就是零成本。它顯然能減少你龐大的多邊形數量。當在使用「傳統」公告板或精靈的時候,因爲引擎需要頻繁的對紋理進行更新,性能相比之下一定受到影響,而視點替用特效的更新僅僅與你的照相機位置發生聯系,因此它們更爲象實時3D對象。

What you're doing, in fact, is rendering an object once, and then reusing it in several subsequent frames at almost zero cost. This can obviously reduce your polygon counts quite a lot. While performance is a bit lower than using "traditional" billboards or sprites because of the frequent texture updates, impostors are updated to match your camera position, so they do look like real 3D objects.

2、創建視點替用特效(Creating an impostor)

當開始應用時,需爲你欲要使用視點替用特效的每個對象創建一個新的紋理對象。當使用視點替用特效圖像的時間到來時,請測定你3D對象的屏幕空間的包圍矩形。這可保證對象包圍盒投影到屏幕及指定的包圍矩形內。最快的解決方案是使用包圍球,如此一來你僅需投影兩個點,當然你也可以使用包圍盒。

When your application starts up,you'll need to create a new texture object for every object that you want to impostor. When the time comes to render the impostor image,you determine the screen-space bounding rectangle of your 3D object.This can be done by projecting the object's bounding volume to the screen and taking the bounding rectangle of the projection. The fastest solution is to use a bounding sphere,as you'd only need to project two points, but you can also use bounding boxes.

在你弄明白包圍矩形時,就可利用當前照相機的位置對3D對象進行渲染了。而後你可用glCopyTexSubImage2D()複制包圍矩形內的視點替用特效的紋理內容。

When you've figured out the bounding rectangle, you render the 3D object, using your current camera position. You can then use glCopyTexSubImage2D() to copy the contents of the bounding rectangle to the impostor's texture.

王朝网络

圖1

紋理圖像的分辨率應該取決于屏幕上3D對象的大小。假如對象當前占用了30x30的象素,你就不應該使用256x256的紋理來實現視點替用特效。相反的,你應該動態地調整紋理分辨率以最好的適應對象的需要。就保證紋理大小保持在2的冪,以滿足放大或縮小時的需要。縮放紋理的大小應該調用glCopyTexImage2D()而不是glTexSubImage2D()函數。

The texture image's resolution should depend on the size of the 3D object on screen. If the object occupies 30x30 pixels, you shouldn't use a 256x256 texture to store the impostor. Instead, you can dynamically adjust the texture resolution to best fit the object. Just make sure that the texture size remains a power of two, and that you enlarge or shrink it when needed. Resizing the texture can be done by calling glCopyTexImage2D() instead of glTexSubImage2D().

3、渲染視點替用特效(Rendering the impostor)

既然你已爲使用視點替用特效紋理做了必要的准備,在你3D對象的位置渲染一個對齊視窗的精靈應是件很簡單的事。你需要做的唯一的事情就是確保視點替用特效不時地進行更新。

Now that you have the impostor texture ready for use, rendering it is simply a matter of placing a viewport-aligned sprite where your 3D object used to be. The only thing you need to do is make sure the impostor gets updated every now and then.

王朝网络

圖2

第一種情形是當你屏幕空間的包圍矩形改變大小的時候,需要重新創建視點替用特效紋理。其意味著照相機到對象之間距離的改變,因此視點替用特效的分辨率也可能發生適當的改變。對距離變化的回應是,你可維持視點替用特效紋理的紋素之間的一個幾乎恒定的比率 ― 比率的最佳理想值應該是1:1。

A first situation in which you should recreate your impostor texture is if the screen-space bounding rectangle changes size. This means that the camera's distance to the object is changing, so the impostor's resolution may need to change as well. By responding to changes in distance, you can maintain an almost constant ratio between texels in the impostor image - ideally this ratio would be 1:1.

第二個需要更新的情形是,當對象與照相機觀察矢量之間的角度發生大範圍變化時。這顯而易見的:如果你從一個不同的角度觀察對象時,視點替用特效需要改變以反映你新的視點。

The second situation in which the impostor needs to be updated is when the angle between the object and the camera view vector changes by more than a few degrees. This is obvious: if you view the object from a different angle, the impostor needs to change to reflect your new viewpoint.

這裏還有一個發現:如果照相機很靠近視點替用特效對象時,用戶就可得知,其3D對象事實上就是一個精靈。當對象的屏幕尺寸達到某個阈值時,你可以依靠渲染一個真實的3D對象來代替視點替用特效以避免問題的發生。這不僅可防止用戶察覺你正在欺騙他們,也保證了視點替用特效紋理的大小限制在一定的尺寸內。

There's one catch: if the camera comes very close to the impostor, the user will be able to tell that the 3D object is actually a sprite. You can avoid this by rendering the real 3D object instead of the impostor when the object's screen size exceeds a certain threshold. This not only prevents the user from finding out that you're cheating, it also keeps the impostor texture sizes within reasonable limits.

4、問題(Issues)

在你進而實現視點替用特效時,你可能會遇到一些問題。首先,你可能對視點替用特效的阿爾法通道感到疑惑。歸根結底,這技巧的運作狀況良好,視點替用特效需要帶有透明背景。

There are a couple of problems that you might run into when implementing impostors. First of all, you're probably wondering about the impostor's alpha channel. After all, in order for this trick to work, the impostor has to have a transparent background!

假如你需要一個32位的顔色緩沖區,緩沖區內含阿爾法通道,命名爲「destination alpha」。在正常的情形下,阿爾法目的單元格是從不用到的。當你調用glCopyTexImage2D(),及設置RGBA格式時,另一方面,OpenGL也將阿爾法目的單元格的內容複制到紋理上。將glClearColor()的參數alpha設以零可清除阿爾法目的單元格的信息。如果你接下進行視點替用特效渲染,你將在透明背景上得到一張不透明的圖像,其正是你需要的。

If you reque

  公告板及精靈的本質特點就是可以很容易的完成渲染,但在實時3D幾何圖形處理中它們是否還具有優勢…很好,實時3D。這有沒有可能找到一種在兩個世界中最佳的方法呢。   Billboards or sprites have the advantage of being very cheap to render, but real 3D geometry has the advantage of looking like it's... Well, real 3D. Wouldn't it be great if it were somehow possible to have the best of both worlds?   當然,在我沒有告訴你那些准許你得到兩個世界中最合理的那種技術之前我是不會這麽問你的。這種技術的就叫做「視點替用特效(光源替用特效)」。   Of course I wouldn't ask you this if I wasn't about to tell you that there is a technique that allows you to get the best of both worlds. This technique is called "impostoring".   1、引言(Say what?)   視點替用特效本質就是精靈 ― 可替代實時3D模型的2D圖像。但請把握住精靈是由美工手繪完成的,而視點替用特效則是由3D引擎本身實時進行渲染。   An impostor is essentially a sprite - a 2D image that replaces a real 3D model. The catch is that while sprites are usually drawn by artists, impostors are rendered at run-time, by the 3D engine itself.   讓我們假設你的場景中包含了一棵用視點替用特效表現的樹。3D引擎渲染的首先步驟是給3D樹模型加入紋理,然後把你在場景中寄予的紋理當做一個公告板。只要照相機不移動太大,視點替用特效就能這樣被保留。當照相機顯著地移動時,引擎也只不過重新渲染下視點替用特效,並且,你可以很好的去獲得其它的幀。   Let's say your scene contains a tree that will be represented by an impostor. The 3D engine first renders the 3D tree model to a texture, then places that texture in your scene as a billboard. As long as the camera doesn't move too much, the impostor can be kept as is. When the camera does move significantly, the engine simply re-renders the impostor, and you're good to go for another couple of frames.   你正在做什麽,事實是,渲染對象一旦進行,接下來的重新使用,對于隨後渲染的幾幀圖像而言幾乎就是零成本。它顯然能減少你龐大的多邊形數量。當在使用「傳統」公告板或精靈的時候,因爲引擎需要頻繁的對紋理進行更新,性能相比之下一定受到影響,而視點替用特效的更新僅僅與你的照相機位置發生聯系,因此它們更爲象實時3D對象。   What you're doing, in fact, is rendering an object once, and then reusing it in several subsequent frames at almost zero cost. This can obviously reduce your polygon counts quite a lot. While performance is a bit lower than using "traditional" billboards or sprites because of the frequent texture updates, impostors are updated to match your camera position, so they do look like real 3D objects.   2、創建視點替用特效(Creating an impostor)   當開始應用時,需爲你欲要使用視點替用特效的每個對象創建一個新的紋理對象。當使用視點替用特效圖像的時間到來時,請測定你3D對象的屏幕空間的包圍矩形。這可保證對象包圍盒投影到屏幕及指定的包圍矩形內。最快的解決方案是使用包圍球,如此一來你僅需投影兩個點,當然你也可以使用包圍盒。   When your application starts up,you'll need to create a new texture object for every object that you want to impostor. When the time comes to render the impostor image,you determine the screen-space bounding rectangle of your 3D object.This can be done by projecting the object's bounding volume to the screen and taking the bounding rectangle of the projection. The fastest solution is to use a bounding sphere,as you'd only need to project two points, but you can also use bounding boxes.   在你弄明白包圍矩形時,就可利用當前照相機的位置對3D對象進行渲染了。而後你可用glCopyTexSubImage2D()複制包圍矩形內的視點替用特效的紋理內容。   When you've figured out the bounding rectangle, you render the 3D object, using your current camera position. You can then use glCopyTexSubImage2D() to copy the contents of the bounding rectangle to the impostor's texture.      [url=/bbs/detail_1440379.html][img]http://images.wangchao.net.cn/images/upload/images/lsdn/1211129118445.jpg[/img][/url]   圖1   紋理圖像的分辨率應該取決于屏幕上3D對象的大小。假如對象當前占用了30x30的象素,你就不應該使用256x256的紋理來實現視點替用特效。相反的,你應該動態地調整紋理分辨率以最好的適應對象的需要。就保證紋理大小保持在2的冪,以滿足放大或縮小時的需要。縮放紋理的大小應該調用glCopyTexImage2D()而不是glTexSubImage2D()函數。   The texture image's resolution should depend on the size of the 3D object on screen. If the object occupies 30x30 pixels, you shouldn't use a 256x256 texture to store the impostor. Instead, you can dynamically adjust the texture resolution to best fit the object. Just make sure that the texture size remains a power of two, and that you enlarge or shrink it when needed. Resizing the texture can be done by calling glCopyTexImage2D() instead of glTexSubImage2D().   3、渲染視點替用特效(Rendering the impostor)   既然你已爲使用視點替用特效紋理做了必要的准備,在你3D對象的位置渲染一個對齊視窗的精靈應是件很簡單的事。你需要做的唯一的事情就是確保視點替用特效不時地進行更新。   Now that you have the impostor texture ready for use, rendering it is simply a matter of placing a viewport-aligned sprite where your 3D object used to be. The only thing you need to do is make sure the impostor gets updated every now and then.    [url=/bbs/detail_1440379.html][img]http://images.wangchao.net.cn/images/upload/images/lsdn/1211129118648.jpg[/img][/url]   圖2   第一種情形是當你屏幕空間的包圍矩形改變大小的時候,需要重新創建視點替用特效紋理。其意味著照相機到對象之間距離的改變,因此視點替用特效的分辨率也可能發生適當的改變。對距離變化的回應是,你可維持視點替用特效紋理的紋素之間的一個幾乎恒定的比率 ― 比率的最佳理想值應該是1:1。   A first situation in which you should recreate your impostor texture is if the screen-space bounding rectangle changes size. This means that the camera's distance to the object is changing, so the impostor's resolution may need to change as well. By responding to changes in distance, you can maintain an almost constant ratio between texels in the impostor image - ideally this ratio would be 1:1.   第二個需要更新的情形是,當對象與照相機觀察矢量之間的角度發生大範圍變化時。這顯而易見的:如果你從一個不同的角度觀察對象時,視點替用特效需要改變以反映你新的視點。   The second situation in which the impostor needs to be updated is when the angle between the object and the camera view vector changes by more than a few degrees. This is obvious: if you view the object from a different angle, the impostor needs to change to reflect your new viewpoint.   這裏還有一個發現:如果照相機很靠近視點替用特效對象時,用戶就可得知,其3D對象事實上就是一個精靈。當對象的屏幕尺寸達到某個阈值時,你可以依靠渲染一個真實的3D對象來代替視點替用特效以避免問題的發生。這不僅可防止用戶察覺你正在欺騙他們,也保證了視點替用特效紋理的大小限制在一定的尺寸內。   There's one catch: if the camera comes very close to the impostor, the user will be able to tell that the 3D object is actually a sprite. You can avoid this by rendering the real 3D object instead of the impostor when the object's screen size exceeds a certain threshold. This not only prevents the user from finding out that you're cheating, it also keeps the impostor texture sizes within reasonable limits.   4、問題(Issues)   在你進而實現視點替用特效時,你可能會遇到一些問題。首先,你可能對視點替用特效的阿爾法通道感到疑惑。歸根結底,這技巧的運作狀況良好,視點替用特效需要帶有透明背景。   There are a couple of problems that you might run into when implementing impostors. First of all, you're probably wondering about the impostor's alpha channel. After all, in order for this trick to work, the impostor has to have a transparent background!   假如你需要一個32位的顔色緩沖區,緩沖區內含阿爾法通道,命名爲「destination alpha」。在正常的情形下,阿爾法目的單元格是從不用到的。當你調用glCopyTexImage2D(),及設置RGBA格式時,另一方面,OpenGL也將阿爾法目的單元格的內容複制到紋理上。將glClearColor()的參數alpha設以零可清除阿爾法目的單元格的信息。如果你接下進行視點替用特效渲染,你將在透明背景上得到一張不透明的圖像,其正是你需要的。   If you reque
󰈣󰈤
王朝萬家燈火計劃
期待原創作者加盟
 
 
 
>>返回首頁<<
 
 
 
 
 熱帖排行
 
 
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有