OpenGL的渲染成紋理技術(轉)

post0發表於2007-08-12
OpenGL的渲染成紋理技術(轉)[@more@]

  1、什麼是動態紋理(What is Dynamic Texturing)

  

  這是適用處理實時的“飛於空中”的紋理貼圖。

  The creation of texture maps "on the fly" for use in real time.

  

  簡化觀點(Simplified view):

  

  迴圈(Loop):

  1)渲染影像。

  Render an image.

  

  2)建立源於影像中的紋理。

  Create a texture from that image.

  

  3)使用紋理時你應該使用靜態紋理。

  Use the texture as you would a static texture.

  2、動態紋理的應用(Applications of Dynamic Texturing)

  

  光源替用特效(Impostors)

  

  反饋效果(Feedback Effects)

  

  可產生動態立方體/環境貼圖(Dynamic Cube / Environment map generation)

  

  可產生動態法向量圖(Dynamic Normal map generation)

  

  動態體積霧化(Dynamic Volumetric Fog)

  

  程式紋理(Procedural Texturing)

  動態影像的處理(Dynamic Image Processing)

  

  物理模擬(Physical (PDE) Simulation)

  
[[The No.1 Picture.]]

  3、挑戰動態紋理帖圖(Challenge of Dynamic Texturing)

  

  效能的瓶頸

  Performance Bottlenecks

  

  簡化觀點(Simplified view):

  

  迴圈(Loop):

  1)渲染影像。

  Render an image.

  

  2)建立源於影像中的紋理。

  Create a texture from that image.

  

  3)使用紋理時你應該用靜態紋理。

  Use the texture as you would a static texture.

  第二步是主要瓶頸,但一、三步還可以很好的工作。

  Step 2 is primary bottleneck but 1 and 3 can be relevant as well.

  

  4、建立紋理的方法(Methods for Creating the Texture)

  如何把紋理渲染到影像中?

  How to get rendered image into a texture?

  

  glReadPixels() -> glTexImage*() ?

  緩慢(Slow)

  

  glCopyTexImage*()

  比較好(Better)

  

  glCopyTexSubImage*()

  甚至更好(Even Better)

  

  Render Directly to Texture

  消除“紋理複製” ― 潛在地理想方法。

  Eliminates "texture copy" - potentially optimal.

  

  5、直接渲染紋理(Rendering Directly to a Texture)

  其不是OpenGL 1.3的核心部分,但ARB擴充套件指令可讓它可以在大多數的圖形處理器上實現。

  Not a core part of OpenGL 1.3, but ARB extensions make this possible on most GPUs.

  

  必須的擴充套件指令(Required Extensions):

  

  WGL_ARB_extensions_string

  WGL_ARB_render_texture

  WGL_ARG_pbuffer

  WGL_ARB_pixel_format

  NVIDIA公司目前GeForce以上的產品(必須28.40版以上的驅動程式)都可以使用它們。

  Available on all NVIDIA products since (requires 28.40 driver)

  

  5.1 縱覽(Rendering Directly to a Texture: An Overview)

  基本思路:允許P快取作為一個紋理而進行限制。

  Basic Idea: Allow a p-buffer to be bound as a texture.

  

  建立紋理物件。

  Create a texture object.

  

  建立“渲染紋理”(即P快取)。

  Create a "Render Texture" (i.e. the pbuffer)

  

  作必要迴圈:

  Loop as necessary:

  

  建立當前渲染物件的P快取

  Make the pbuffer the current rendering target

  

  渲染影像

  Render an image

  

  建立當前渲染物件的視窗

  Make the window the current rendering target

  

  將P快取混合到紋理物件中

  Bind the pbuffer to the texture object

  

  當你需要使用紋理物件的時候

  Use the texture object as you would any other

  

  釋放紋理物件所佔用的P快取

  Release the pbuffer from the texture object

  

  清理

  Clean Up

  5.2 建立紋理物件(Creating the Texture Object)

  是否需要建立一個常規的紋理 ― 不,沒有必要指定實際的紋理資料。

  Just as you would for a regular texture -- no need to specify the actual texture data.

  

  // Create a render texture object

  glGenTextures( 1, &render_texture );

  glBindTexture( GL_TEXTURE_2D, render_texture );

  glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,

  GL_LINEAR_MIPMAP_LINEAR );

  glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,

  GL_LINEAR );

  glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,

  GL_CLAMP_TO_EDGE );

  glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,

  GL_CLAMP_TO_EDGE );

  

  5.3 建立P快取(Creating the Pbuffer)

  

  快速預覽(Quick Overview):

  5.3.1 獲得有效裝置的上下文(Get a valid device context)

  

  HDC hdc = wglGetCurrentDC();

  

  5.3 2 選擇象素格式(Choose a pixel format)

  指定一套最低屬性:

  Specify a set of minimum attributes

  

  色彩位、深度位、模板位、單精/雙精度等。

  Color bits, Depth bits, Stencil bits, single/double, etc.

  

  可用WGL_DRAW_TO_PBUFFER、WGL_BIND_TO_TEXTURE_RGB_ARB或WGL_BIND_TO_TEXTURE_RGBA_ARB中任意一條。

  Must also specify WGL_DRAW_TO_PBUFFER and either WGL_BIND_TO_TEXTURE_RGB_ARB or WGL_BIND_TO_TEXTURE_RGBA_ARB as TRUE.

  

  呼叫wglChoosePixelFormat():

  Then call wglChoosePixelFormat()

  

  返回最低要求的格式列表。

  Returns a list of formats which meet minimum requirements.

  

  fid = 在列表裡揀選任意一種格式。

  fid = pick any format in the list.

  5.3.3 建P快取(Create the pbuffer)

  

  HPBUFFER hbuf = wglCreatePbufferARB( hdc, fid, width, height, attr );

  

  width、height為P快取的尺度引數。

  width and height are dimensions of the pbuffer

  

  “attr”是P快取其它屬性的列表。

  "attr" is a list of other properties for your pbuffer.

  

  設定(Set)WGL_TEXTURE_FORMAT_ARB:

  

  WGL_TEXTURE_RGB_ARB or WGL_TEXTURE_RGBA_ARB

  

  設定(Set)WGL_TEXTURE_TARGET_ARB:

  

  WGL_TEXTURE_1D_ARB, WGL_TEXTURE_2D_ARB, or WGL_TEXTURE_CUBE_MAP_ARB

  

  設定WGL_MIPMAP_TEXTURE_ARB需求紋理細化的空間值,其為非零。

  Set WGL_MIPMAP_TEXTURE_ARB to non-zero value to request space for mipmaps.

  

  設定WGL_PBUFFER_LARGEST_ARB以獲取更多可用的P快取空間,其為非零。

  Set WGL_PBUFFER_LARGEST_ARB to non-zero value to obtain largest possible pbuffer.

  5.3.4 獲取P快取裝置的上下文(Get the device context for the pbuffer)

  

  hpbufdc = wglGetPbufferDCARB( hbuf );

  

  5.3.5 獲取渲染P快取的上下文(Get a rendering context for the pbuffer)

  

  建立一個新的 ― P快取獲取GL狀態集:

  Create a new one - pbuffer gets its own GL state:

  

  hpbufglrc = wglCreateContext( hpbufdc );

  5.3.6 決定建立的P快取的實際尺寸(Determine the actual dimension of the created pbuffer)

  

  wglQueryPbufferARB( hbuf, WGL_PBUFFER_WIDTH_ARB, width );

  wglQueryPbufferARB( hbuf, WGL_PBUFFER_HEIGHT_ARB, height );

  

  5.3.7 對紋理進行渲染(Rendering to the Texture)

  

  建立P快取一旦完成。

  Can be done anytime once the creation of the pbuffer is complete

  

  初始化函式、顯示迴圈、每次十幀等:

  Initialization function, display loop, every 10 frames, etc.

  

  可使用wglMakeCurrent建立當前P快取的渲染上下文:

  Must make the rendering context for the pbuffer current using wglMakeCurrent:

  

  wglMakeCurrent( hpbufdc, hpbufglrc );

  

  發出OpenGL繪製指令。

  Issue OpenGL drawing commands.

  

  wglMakeCurrent( hwindc, hwinglrc );

  

  向紋理渲染的機制允許我們對紋理的特定區域進行渲染操作:

  The render to texture mechanism allows for rendering to specific regions of a texture:

  

  紋理細化的細節層次

  A specific level of a mipmapped texture

  

  立方圖紋理的細節面

  A specific face of a cube map texture

  

  立方圖紋理的細節面的細節細化層次

  A specific mip level of a specific face of a cube map texture

  

  可使用wglSetPbufferAttribARB()選擇渲染的立方圖的面或紋理細化層次。

  Can use wglSetPbufferAttribARB() to choose which cube map face o

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/8225414/viewspace-952125/,如需轉載,請註明出處,否則將追究法律責任。

相關文章