GLSL實現HDR Rendering 【轉】

來源:互聯網
上載者:User

標籤:style   class   blog   http   tar   ext   

http://blog.csdn.net/a3070173/archive/2008/11/29/3408573.aspx

 

  1. HDR - 全稱High dynamic rang,是目前流行的3D特效技術.其基本原理是:雖然在電腦圖形中可以使用完全的浮點型來表
  2. 示顏色,但之前由於一直受到硬體的限制,從外部載入的紋理格式大多隻能以每種顏色成分用一個位元組來表示,也就是0-255,
  3. 當這個低範圍的顏色值轉換為浮點型之後就會導致一定量的顏色間隔,而HDR技術通過採用浮點型的外部紋理格式彌補了原來
  4. 整型紋理格式所導致的顏色間隔,從而增強了電腦圖形的顏色表現能力.當然這隻是籠統的說法,而且每個人對同一個概念
  5. 的理解也不盡相同,所以我在本文的最後列出了一些個人認為比較有用的參考資料,這裡只簡單描述一下我的HDR - OpenGL實
  6. 現,其實HDR的理念理解了之後就會比較簡單,但真要編碼實現的話還是有很多東西需要琢磨.
  7. 下面列出的是主要渲染流程:
  8. 備忘(FBO代表Frame Buffer Object)
  9. 1.渲染整個情境到FBO1
  10. 2.下採樣FBO1到尺寸為原來1/4的FBO2中
  11. 3.下採樣FBO2到尺寸為原來1/8的FBO3中
  12. DownSample片元著色器:
  13. uniform sampler2D g_SceneTexture;
  14. void main()
  15. {
  16.     gl_FragColor = texture2D(g_SceneTexture, gl_TexCoord[0].st);
  17. }
  18. 4.對經過兩次下採樣並儲存在FBO3中的內容進行高斯過濾,並將處理過後的映像
  19.   儲存在FBO4中(備忘:高斯過濾分為橫向過濾和縱向過濾兩個通道,所以需要一個FBO5進行過渡)
  20. 高斯過濾片元著色器:
  21. const int g_iWeightNumber = 17;
  22.                                     
  23. uniform sampler2D g_DecalTexture;
  24. uniform bool g_bFilterModel;
  25. uniform float g_aryWeight[g_iWeightNumber]; // Blur權重數組
  26. uniform vec2 g_aryVerticalOffset[g_iWeightNumber];  // 橫向Blur位移數組
  27. uniform vec2 g_aryHorizontalOffset[g_iWeightNumber];    // 縱向Blur位移數組
  28. void main()
  29. {
  30.     vec4 vec4Sum = vec4(0.0);
  31.     if (g_bFilterModel)
  32.     {
  33.         // 橫向過濾
  34.         for(int i = 0; i < g_iWeightNumber; ++i)
  35.         {
  36.             vec4Sum += texture2D(g_DecalTexture, gl_TexCoord[0].st + g_aryVerticalOffset[i])*g_aryWeight[i];
  37.         }
  38.     }
  39.     else
  40.     {   
  41.         // 縱向過濾
  42.         for(int i = 0; i < g_iWeightNumber; ++i)
  43.         {
  44.             vec4Sum += texture2D(g_DecalTexture, gl_TexCoord[0].st + g_aryHorizontalOffset[i])*g_aryWeight[i];
  45.         }
  46.     }
  47.     gl_FragColor = vec4Sum;
  48. }
  49. 5.FBO4中的內容與FBO1中的內容進行特效處理和ToneMapping以將高動態範圍的顏色值對應對低動態範圍以便於顯示.
  50. ToneMapping片元著色器:
  51. const float g_fGamma = 1.0/2.0;
  52. uniform float g_fBlurAmount;    // 模糊量
  53. uniform float g_fRadialEffectAmount;    // 放射式效果量
  54. uniform float g_fExposure;  // 暴光量
  55. uniform sampler2D g_SceneTexture;
  56. uniform sampler2D g_BlurTexture;
  57. // 計算放射式效果
  58. vec4 CaculateRadial(vec2 p_vec2TexCoord,int p_iSampleNumber,
  59.               float p_fStartScale = 1.0, float p_fScaleMul = 0.9)
  60. {
  61.     // 臨時變數
  62.     vec4 vec4TempColor = vec4(0.0);
  63.     float fCurrentScale = p_fStartScale;
  64.     vec2 vec2TempTexCoord = vec2(0.0);
  65.     
  66.     // 遍曆採樣
  67.     for(int i = 0; i < p_iSampleNumber; ++i) 
  68.     {
  69.         vec2TempTexCoord = (p_vec2TexCoord - 0.5)*fCurrentScale + 0.5;  // 採樣方式
  70.         vec4TempColor += texture2D(g_BlurTexture, vec2TempTexCoord);
  71.         fCurrentScale *= p_fScaleMul;
  72.     }
  73.     vec4TempColor /= float(p_iSampleNumber);
  74.     return vec4TempColor;
  75. }
  76. // 計算小插圖效果
  77. float CaculateVignette(vec2 p_vec2Position, float p_fInner, float p_fOuter)
  78. {
  79.     float L = length(p_vec2Position);
  80.     return ( 1.0 - smoothstep(p_fInner, p_fOuter, L) );
  81. }
  82. void main()
  83. {
  84.     vec4 vec4SceneColor = texture2D(g_SceneTexture, gl_TexCoord[0].st);     // 計算原始情境顏色
  85.     vec4 vec4BlurColor = texture2D(g_BlurTexture, gl_TexCoord[0].st);       // 計算經Blur後的情境顏色
  86.     vec4 vec4RadialEffectColor = CaculateRadial(gl_TexCoord[0].st, 30, 1.0, 0.95);  // 計算放射效果的顏色
  87.     
  88.     // 混合情境與Blur
  89.     vec4 vec4Temp = lerp(vec4SceneColor, vec4BlurColor, g_fBlurAmount);
  90.     // 添加放射性效果
  91.     vec4Temp += vec4RadialEffectColor*g_fRadialEffectAmount;
  92.     // 進行暴光
  93.     vec4Temp *= g_fExposure;
  94.     // 添加圓形擴散小插圖效果使得中間部分較亮而四個邊角逐漸層暗
  95.     vec4Temp *= CaculateVignette(gl_TexCoord[0].st*2.0 - 1.0, 0.7, 1.5);
  96.     // 使用Gamma校正規範會低範圍光照
  97.     vec4Temp.rgb = pow(vec4Temp.rgb, vec3(g_fGamma));
  98.     // 最終顏色
  99.     gl_FragColor = vec4Temp;
  100. }
  101. Demo效果:
  102. exe檔案:http://www.fileupyours.com/view/219112/GLSL/HDR%20Rendering.rar
  103. VC9運行庫:http://www.fileupyours.com/view/219112/GLSL/VC9RunningLib.rar
  104. 參考資料:1.DirectX SDK Sample - HDRLighting(備忘:這個可以從DirectX SDK中獲得)
  105.          2.Nvidia SDK 10.5 - HDR(備忘:Nvidia SDK可以在Nvidia的網站上免費下載)
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.