How to Implement HDR Rendering using glsl]

Source: Internet
Author: User

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

 

  1. HDR, short for high dynamic rang, is a popular 3D special effect technology. Its basic principle is that although full floating-point tables can be used in computer graphics
  2. Color Display, but previously due to hardware restrictions, most of the texture formats loaded from the outside can only be expressed in one byte for each color component, that is, 0-255,
  3. When this low-range color value is converted to the floating point type, it will lead to a certain amount of color intervals, while the HDR technology uses the floating point type of external texture format to make up for the original
  4. The color interval caused by the integer texture format enhances the Color Performance of computer graphics. Of course, this is only a general statement, and everyone has the same concept.
  5. So I listed some useful references at the end of this Article. Here I will briefly describe my HDR-OpenGL implementation.
  6. Now, the concept of HDR is easy to understand, but there are still many things to consider if coding is really necessary.
  7. The following lists the main rendering processes:
  8. Remarks (FBO stands for Frame Buffer object)
  9. 1. rendering the entire scenario to fbo1
  10. 2. subsample fbo1 to fbo2 of the original size of 1/4
  11. 3. subsample fbo2 to the original fbo3 of 1/8 size
  12. Downsample Cell Coloring tool:
  13. Uniform sampler2d g_scenetexture;
  14. Void main ()
  15. {
  16. Gl_fragcolor = texture2d (g_scenetexture, gl_texcoord [0]. St );
  17. }
  18. 4. Perform Gaussian filtering on the content that has been sampled and stored in fbo3 twice, and then process the processed image.
  19. Saved in fbo4 (Note: Gaussian filter is divided into two channels: Horizontal Filter and vertical filter, so a fbo5 is required for transition)
  20. Gaussian filter element coloring device:
  21. Const int g_iweightnumber = 17;
  22. Uniform sampler2d g_decaltexture;
  23. Uniform bool g_bfiltermodel;
  24. Uniform float g_aryweight [g_iweightnumber]; // blur weight array
  25. Uniform vec2 g_aryverticaloffset [g_iweightnumber]; // array of horizontal blur Offsets
  26. Uniform vec2 g_aryhorizontaloffset [g_iweightnumber]; // array of vertical blur Offsets
  27. Void main ()
  28. {
  29. Vec4 vec4sum = vec4 (0.0 );
  30. If (g_bfiltermodel)
  31. {
  32. // Horizontal Filtering
  33. For (INT I = 0; I <g_iweightnumber; ++ I)
  34. {
  35. Vec4sum + = texture2d (g_decaltexture, gl_texcoord [0]. St + g_aryverticaloffset [I]) * g_aryweight [I];
  36. }
  37. }
  38. Else
  39. {
  40. // Vertical Filtering
  41. For (INT I = 0; I <g_iweightnumber; ++ I)
  42. {
  43. Vec4sum + = texture2d (g_decaltexture, gl_texcoord [0]. St + g_aryhorizontaloffset [I]) * g_aryweight [I];
  44. }
  45. }
  46. Gl_fragcolor = vec4sum;
  47. }
  48. 5. The content in fbo4 performs special effects and tonemapping with the content in fbo1 to map the color values in the high dynamic range to the low dynamic range for display.
  49. Tonemapping bitwise coloring tool:
  50. Const float g_fgamma = 1.0/2.0;
  51. Uniform float g_fbluramount; // fuzzy amount
  52. Uniform float g_fradialeffectamount; // radiation effect volume
  53. Uniform float g_fexposure; // amount of exposure
  54. Uniform sampler2d g_scenetexture;
  55. Uniform sampler2d g_blurtexture;
  56. // Calculate the radial effect
  57. Vec4 caculateradial (vec2 p_vec2texcoord, int p_isamplenumber,
  58. Float p_fstartscale = 1.0, float p_fscalemul = 0.9)
  59. {
  60. // Temporary Variable
  61. Vec4 vec4tempcolor = vec4 (0.0 );
  62. Float fcurrentscale = p_fstartscale;
  63. Vec2 vec2temptexcoord = vec2 (0.0 );
  64. // Traverse sampling
  65. For (INT I = 0; I <p_isamplenumber; ++ I)
  66. {
  67. Vec2temptexcoord = (p_vec2texcoord-0.5) * fcurrentscale + 0.5; // Sampling Method
  68. Vec4tempcolor + = texture2d (g_blurtexture, vec2temptexcoord );
  69. Fcurrentscale * = p_fscalemul;
  70. }
  71. Vec4tempcolor/= float (p_isamplenumber );
  72. Return vec4tempcolor;
  73. }
  74. // Calculate the effect of small illustrations
  75. Float caculatevignette (vec2 p_vec2position, float p_finner, float p_fouter)
  76. {
  77. Float L = length (p_vec2position );
  78. Return (1.0-smoothstep (p_finner, p_fouter, L ));
  79. }
  80. Void main ()
  81. {
  82. Vec4 vec4scenecolor = texture2d (g_scenetexture, gl_texcoord [0]. St); // calculate the color of the original scene
  83. Vec4 vec4blurcolor = texture2d (g_blurtexture, gl_texcoord [0]. St); // calculate the scene color after blur
  84. Vec4 vec4radialeffectcolor = caculateradial (gl_texcoord [0]. St, 30, 1.0, 0.95); // calculate the color of the Radiation Effect
  85. // Mixed scenario and blur
  86. Vec4 vec4temp = lerp (vec4scenecolor, vec4blurcolor, g_fbluramount );
  87. // Add the radioactive effect
  88. Vec4temp + = vec4radialpolictcolor * g_fradial?tamount;
  89. // Perform exposure
  90. Vec4temp * = g_fexposure;
  91. // Add a circular diffusion small illustration to make the middle part brighter and the four corners gradually become darker
  92. Vec4temp * = caculatevignette (gl_texcoord [0]. St * 2.0-1.0, 0.7, 1.5 );
  93. // Use the Gamma Correction specification for low-range illumination
  94. Vec4temp. RGB = POW (vec4temp. RGB, vec3 (g_fgamma ));
  95. // Final color
  96. Gl_fragcolor = vec4temp;
  97. }
  98. Demo effect:
  99. EXE file: http://www.fileupyours.com/view/219112/GLSL/HDR%20Rendering.rar
  100. Vc9 Runtime Library: http://www.fileupyours.com/view/219112/GLSL/VC9RunningLib.rar
  101. References: 1. DirectX SDK sample-hdrlighting (note: this can be obtained from DirectX SDK)
  102. 2. nvidia sdk 10.5-HDR (note: the nvidia sdk can be downloaded free of charge on the NVIDIA website)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.