Http://blog.csdn.net/a3070173/archive/2008/11/29/3408573.aspx
- 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
- 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,
- 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
- 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.
- So I listed some useful references at the end of this Article. Here I will briefly describe my HDR-OpenGL implementation.
- Now, the concept of HDR is easy to understand, but there are still many things to consider if coding is really necessary.
- The following lists the main rendering processes:
- Remarks (FBO stands for Frame Buffer object)
- 1. rendering the entire scenario to fbo1
- 2. subsample fbo1 to fbo2 of the original size of 1/4
- 3. subsample fbo2 to the original fbo3 of 1/8 size
- Downsample Cell Coloring tool:
- Uniform sampler2d g_scenetexture;
- Void main ()
- {
- Gl_fragcolor = texture2d (g_scenetexture, gl_texcoord [0]. St );
- }
- 4. Perform Gaussian filtering on the content that has been sampled and stored in fbo3 twice, and then process the processed image.
- Saved in fbo4 (Note: Gaussian filter is divided into two channels: Horizontal Filter and vertical filter, so a fbo5 is required for transition)
- Gaussian filter element coloring device:
- Const int g_iweightnumber = 17;
- Uniform sampler2d g_decaltexture;
- Uniform bool g_bfiltermodel;
- Uniform float g_aryweight [g_iweightnumber]; // blur weight array
- Uniform vec2 g_aryverticaloffset [g_iweightnumber]; // array of horizontal blur Offsets
- Uniform vec2 g_aryhorizontaloffset [g_iweightnumber]; // array of vertical blur Offsets
- Void main ()
- {
- Vec4 vec4sum = vec4 (0.0 );
- If (g_bfiltermodel)
- {
- // Horizontal Filtering
- For (INT I = 0; I <g_iweightnumber; ++ I)
- {
- Vec4sum + = texture2d (g_decaltexture, gl_texcoord [0]. St + g_aryverticaloffset [I]) * g_aryweight [I];
- }
- }
- Else
- {
- // Vertical Filtering
- For (INT I = 0; I <g_iweightnumber; ++ I)
- {
- Vec4sum + = texture2d (g_decaltexture, gl_texcoord [0]. St + g_aryhorizontaloffset [I]) * g_aryweight [I];
- }
- }
- Gl_fragcolor = vec4sum;
- }
- 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.
- Tonemapping bitwise coloring tool:
- Const float g_fgamma = 1.0/2.0;
- Uniform float g_fbluramount; // fuzzy amount
- Uniform float g_fradialeffectamount; // radiation effect volume
- Uniform float g_fexposure; // amount of exposure
- Uniform sampler2d g_scenetexture;
- Uniform sampler2d g_blurtexture;
- // Calculate the radial effect
- Vec4 caculateradial (vec2 p_vec2texcoord, int p_isamplenumber,
- Float p_fstartscale = 1.0, float p_fscalemul = 0.9)
- {
- // Temporary Variable
- Vec4 vec4tempcolor = vec4 (0.0 );
- Float fcurrentscale = p_fstartscale;
- Vec2 vec2temptexcoord = vec2 (0.0 );
- // Traverse sampling
- For (INT I = 0; I <p_isamplenumber; ++ I)
- {
- Vec2temptexcoord = (p_vec2texcoord-0.5) * fcurrentscale + 0.5; // Sampling Method
- Vec4tempcolor + = texture2d (g_blurtexture, vec2temptexcoord );
- Fcurrentscale * = p_fscalemul;
- }
- Vec4tempcolor/= float (p_isamplenumber );
- Return vec4tempcolor;
- }
- // Calculate the effect of small illustrations
- Float caculatevignette (vec2 p_vec2position, float p_finner, float p_fouter)
- {
- Float L = length (p_vec2position );
- Return (1.0-smoothstep (p_finner, p_fouter, L ));
- }
- Void main ()
- {
- Vec4 vec4scenecolor = texture2d (g_scenetexture, gl_texcoord [0]. St); // calculate the color of the original scene
- Vec4 vec4blurcolor = texture2d (g_blurtexture, gl_texcoord [0]. St); // calculate the scene color after blur
- Vec4 vec4radialeffectcolor = caculateradial (gl_texcoord [0]. St, 30, 1.0, 0.95); // calculate the color of the Radiation Effect
- // Mixed scenario and blur
- Vec4 vec4temp = lerp (vec4scenecolor, vec4blurcolor, g_fbluramount );
- // Add the radioactive effect
- Vec4temp + = vec4radialpolictcolor * g_fradial?tamount;
- // Perform exposure
- Vec4temp * = g_fexposure;
- // Add a circular diffusion small illustration to make the middle part brighter and the four corners gradually become darker
- Vec4temp * = caculatevignette (gl_texcoord [0]. St * 2.0-1.0, 0.7, 1.5 );
- // Use the Gamma Correction specification for low-range illumination
- Vec4temp. RGB = POW (vec4temp. RGB, vec3 (g_fgamma ));
- // Final color
- Gl_fragcolor = vec4temp;
- }
- Demo effect:
- EXE file: http://www.fileupyours.com/view/219112/GLSL/HDR%20Rendering.rar
- Vc9 Runtime Library: http://www.fileupyours.com/view/219112/GLSL/VC9RunningLib.rar
- References: 1. DirectX SDK sample-hdrlighting (note: this can be obtained from DirectX SDK)
- 2. nvidia sdk 10.5-HDR (note: the nvidia sdk can be downloaded free of charge on the NVIDIA website)