Glsl glow effect [conversion]

Source: Internet
Author: User

Http://blog.csdn.net/a3070173/archive/2008/11/04/3220940.aspx

 

Glow, that is, the glow effect, has now become a compelling special effect in 3D graphics. This article mainly introduces how to use glsl to implement a typical glow effect.
Implementation steps: 1. render the entire scenario to a swap Buffer
2. Draw the second FBO texture a from the objects that need glow processing in the scenario.
3. perform horizontal and vertical "Gaussian" filtering between FBO texture A and B
4. Use glblendfunc (gl_one, gl_one) to mix the scene image in the FBO texture a after glow processing and the scene image in the SWAp buffer.

Glsl file features:
Fullscreen. vert-used to draw a quadrilateral that overwrites the entire viewport for Gaussian filtering of glow Effect
Filter. frag-used for horizontal and vertical Gaussian filtering
Blend. frag-used to mix processed glowfbo textures with original scene images

To directly generate glow results, we assume that the program has correctly initialized OpenGL and glsl.

Void renderorigionalscene ()
{
If (g_busefillrender)
{
Glpolygonmode (gl_front, gl_fill );
}
Else
{
Glpolygonmode (gl_front, gl_line );
}
Renderobject ();
}
First, let's plot the original scenario. Because this demo does not plot other things except the glow object, we will plot the objects with the glow effect directly here.

Void renderglowobject ()
{
// Set the viewport
Glviewport (0, 0, g_uitexturewidth, g_uitextureheight );
// Draw the original scenario to the second FBO
Glbindframebufferext (gl_framebuffer_ext, g_uifbocolorone );
// Clear the first FBO color and depth Cache
Glclear (gl_color_buffer_bit | gl_depth_buffer_bit );
// Draw a glow object
Renderobject ();
}
Then set the draw target to FBO texture A and draw the objects to be processed by glow. Note that you must set
The viewport makes it as big as the FBO texture so that the object can be accurately mapped to the entire FBO texture. It is necessary to clear the FBO color buffer and draw the depth buffer, because
The image in the FBO texture is different each time.

Void filterglowobject ()
{
Glpolygonmode (gl_front, gl_fill );
// Draw the second fbo for the horizontally filtered image
Glbindframebufferext (gl_framebuffer_ext, g_uifbocolortwo );
// Clear the second FBO color
Glclear (gl_color_buffer_bit );
// Re-set the Cell Coloring tool.
Gluseprogram (g_programobjectone );
// Set the horizontal filter flag
Glint iuniformindex = glgetuniformlocation (g_programobjectone, "g_bfitermode ");
Gluniform1i (iuniformindex, 1 );
// Set the texture
Glbindtexture (gl_texture_2d, g_uiidone );
// Draw
Renderfullscreen ();

// Draw the first FBO image after vertical Filtering
Glbindframebufferext (gl_framebuffer_ext, g_uifbocolorone );
// Clear the first FBO color and depth Cache
Glclear (gl_color_buffer_bit | gl_depth_buffer_bit );
// Set the vertical filter flag
Iuniformindex = glgetuniformlocation (g_programobjectone, "g_bfitermode ");
Gluniform1i (iuniformindex, 0 );
// Set the texture
Glbindtexture (gl_texture_2d, g_uiidtwo );
// Draw
Renderfullscreen ();
}
The following is the highlight of glow effect processing. The key to generating a perfect glow effect lies in the processing of this step. However, it is also very simple, mainly for the filter coloring device.
Configure the logo and target for horizontal and vertical filtering, and then draw the full-view quadrilateral. The rest of the filtering is the responsibility of the glsl Gaussian filter coloring device.

Void rendertoscreen ()
{
// Restore the view
Glviewport (0, 0, g_uicurrent1_wwidth, g_uicurrentwindowheight );
// Restore the rendering target to the swap Buffer
Glbindframebufferext (gl_framebuffer_ext, null );
// Start the Hybrid Operation.
Glable (gl_blend );
Glblendfunc (gl_one, gl_one );
// Bind texture
Glbindtexture (gl_texture_2d, g_uiidone );
// Re-set the Cell Coloring tool.
Gluseprogram (g_programobjecttwo );
// Draw
Renderfullscreen ();
// Restore the fixed function pipeline
Gluseprogram (0 );
// Disable the Hybrid Operation.
Gldisable (gl_blend );
}
The last step is to mix the filtered glow texture with the original scene image. Of course, the OpenGL fixed function pipeline or glsl can be easily implemented.
You must set the viewport back to the original state.

The following is the code of the glsl shader for Gaussian filtering. paste it here for your convenience.
Vertex shader:
Void main ()
{
Gl_texcoord [0] = gl_multitexcoord0;
Gl_position = gl_vertex;
}
Gaussian filter coloring tool:
Const int g_ifiltertime = 9; // times of Filtering
Const float g_fgene = (1.0/(1.0 + 2.0*(0.93 + 0.8 + 0.7 + 0.6 + 0.5 + 0.4 + 0.3 + 0.2); // Attenuation Factor

Uniform sampler2d g_decal;
Uniform bool g_bfitermode;
Uniform float g_fglowgene;
Uniform vec2 g_vec2horizontaldir; // Horizontal Filtering direction
Uniform vec2 g_vec2verticaldir; // vertical filter direction
Uniform float g_ffilteroffset; // filter the offset.

Void main ()
{
Float aryattenuation [g_ifiltertime];
Aryattenuation [0] = 0.93;
Aryattenuation [1] = 0.8;
Aryattenuation [2] = 0.7;
Aryattenuation [3] = 0.6;
Aryattenuation [4] = 0.5;
Aryattenuation [5] = 0.4;
Aryattenuation [6] = 0.3;
Aryattenuation [7] = 0.2;
Aryattenuation [8] = 0.1;

// Sample the original color
Vec2 vec2tex0 = gl_texcoord [0]. St;
Vec4 vec4color = texture2d (g_decal, vec2tex0) * g_fgene;

// Calculate the filtering direction
Vec2 vec2filterdir = g_vec2horizontaldir + vec2 (g_ffilteroffset, 0.0); // Horizontal Filtering
If (! G_bfitermode)
{
Vec2filterdir = g_vec2verticaldir + vec2 (0.0, g_ffilteroffset); // vertical Filter
}

// Filter
Vec2 vec2step = vec2filterdir;
For (INT I = 0; I <g_ifiltertime; ++ I)
{
Vec4color + = texture2d (g_decal, vec2tex0 + vec2step) * aryattenuation [I] * g_fgene;
Vec4color + = texture2d (g_decal, vec2tex0-vec2step) * aryattenuation [I] * g_fgene;
Vec2step + = vec2filterdir;
}

If (g_bfitermode)
{
Gl_fragcolor = vec4color * g_fglowgene;
}
Else
{
Gl_fragcolor = vec4color;
}
}

Hybrid coloring Er:
Uniform sampler2d g_decal;

Void main ()
{
Gl_fragcolor = texture2d (g_decal, gl_texcoord [0]. St );
}

Demo:

 


 

Reference: NVIDIA OpenGL SDK 10.5 simple glow
EXE file: http://www.fileupyours.com/view/219112/GLSL/Glow%20Demo.rar

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.