Skip to main content

History: Glow

Source of version: 5

Copy to clipboard
            This page explains how to add glow to your scene. As example of what we mean some before/after pictures are below.

If you are looking for stencil glow, have a look at this topic: http://www.ogre3d.org/forums/viewtopic.php?f=11&t=27477

!!Examples

{img fileId="1588" alt="Glow_overview.jpg"}

a=texture, b=glow mask, c=result

{img fileId="1589" alt="Glow_off.png"}

Glow (and HDR) disabled

{img fileId="1590" alt="Glow_on.png"}

Glow and HDR on

!!Files
You will need to add these files to your resources:

!!!glow.compositor

{CODE(wrap="1", colors="c++")}compositor Glow
{
	technique
	{
		texture rt0 target_width target_height PF_A8R8G8B8
		texture glowMap target_width target_height PF_A8R8G8B8
		texture AtoB target_width target_height PF_A8R8G8B8

		//Fetch scene contents.
		target rt0
		{
			input previous
		}

		//Get scene rendered with 'Glow' scheme
		target glowMap
		{
			input none
			material_scheme glow

			pass clear
			{
			}

			pass render_scene
			{
			}
		}

		//Blur it along X.
		target AtoB
		{
			input none

			pass clear
			{
			}

			pass render_quad
			{
				material GlowCompositorMat/GlowA
				input 0 glowMap
			}
		}

		//Blur along Y, add to original, and output.
		target_output
		{
			input none
			pass render_quad
			{
				material GlowCompositorMat/GlowB
				input 0 rt0
				input 1 AtoB
			}
		}
	}
}{CODE}
=== glow.cg ===

{CODE(wrap="1", colors="c++")}//A.
float4 GlowA_fp
	(
		float2 uv: TEXCOORD0,

		uniform sampler2D scene: register(s0),
		uniform float4 invTexSize
		//uniform float time
	) : COLOR
#define RENDER_SCENE 1
#define BLUR_RADIUS 2
{
	float4 colour = float4(0);
	float blurSize = BLUR_RADIUS * invTexSize.x;

	//X-blur.
	colour += tex2D(scene, float2(uv.x - 4.0*blurSize, uv.y)) * 1.0/25.0;
	colour += tex2D(scene, float2(uv.x - 3.0*blurSize, uv.y)) * 2.0/25.0;
	colour += tex2D(scene, float2(uv.x - 2.0*blurSize, uv.y)) * 3.0/25.0;
	colour += tex2D(scene, float2(uv.x - blurSize, uv.y)) * 4.0/25.0;
	colour += tex2D(scene, float2(uv.x, uv.y)) * 5.0/25.0;
	colour += tex2D(scene, float2(uv.x + blurSize, uv.y)) * 4.0/25.0;
	colour += tex2D(scene, float2(uv.x + 2.0*blurSize, uv.y)) * 3.0/25.0;
	colour += tex2D(scene, float2(uv.x + 3.0*blurSize, uv.y)) * 2.0/25.0;
	colour += tex2D(scene, float2(uv.x + 4.0*blurSize, uv.y)) * 1.0/25.0;

	return colour;
}

//B.
float4 GlowB_fp
	(
		float2 uv: TEXCOORD0,

		uniform sampler2D scene: register(s0),
		uniform sampler2D blurX: register(s1),
		uniform float4 invTexSize,
		uniform float time
	) : COLOR
{
	float4 colour = float4(0);
	float blurSize = BLUR_RADIUS * invTexSize.y;

	//Y-blur.
	colour += tex2D(blurX, float2(uv.x, uv.y - 4.0*blurSize)) * 1.0/25.0;
	colour += tex2D(blurX, float2(uv.x, uv.y - 3.0*blurSize)) * 2.0/25.0;
	colour += tex2D(blurX, float2(uv.x, uv.y - 2.0*blurSize)) * 3.0/25.0;
	colour += tex2D(blurX, float2(uv.x, uv.y - blurSize)) * 4.0/25.0;
	colour += tex2D(blurX, float2(uv.x, uv.y)) * 5.0/25.0;
	colour += tex2D(blurX, float2(uv.x, uv.y + blurSize)) * 4.0/25.0;
	colour += tex2D(blurX, float2(uv.x, uv.y + 2.0*blurSize)) * 3.0/25.0;
	colour += tex2D(blurX, float2(uv.x, uv.y + 3.0*blurSize)) * 2.0/25.0;
	colour += tex2D(blurX, float2(uv.x, uv.y + 4.0*blurSize)) * 1.0/25.0;

	//Add this to original, return.
	return
#if RENDER_SCENE
		tex2D(scene, uv) +
#endif
		colour * 4;
}{CODE}

=== glow.program ===

{CODE(wrap="1", colors="c++")}fragment_program GlowA_fp cg
{
	source glow.cg
	entry_point GlowA_fp

		default_params
		{
			param_named_auto invTexSize inverse_texture_size 0
			//param_named_auto time time_0_2pi 1
		}

	profiles ps_2_0 arbfp1
}

material GlowCompositorMat/GlowA
{
	technique
	{
		pass
		{
			cull_hardware none
			cull_software none
			depth_func always_pass

			fragment_program_ref GlowA_fp
			{
			}

			texture_unit map
			{
					tex_coord_set 0
					tex_address_mode clamp
					filtering trilinear
			}
		}
	}
}

fragment_program GlowB_fp cg
{
	source glow.cg
	entry_point GlowB_fp

		default_params
		{
			param_named_auto invTexSize inverse_texture_size 0
			param_named_auto time time_0_2pi 4
		}

	profiles ps_2_0 arbfp1
}

material GlowCompositorMat/GlowB
{
	technique
	{
		pass
		{
			cull_hardware none
			cull_software none
			depth_func always_pass

			fragment_program_ref GlowB_fp
			{
			}

			texture_unit scene
			{
					tex_coord_set 0
					tex_address_mode clamp
					filtering trilinear
			}

			texture_unit map
			{
					tex_coord_set 0
					tex_address_mode clamp
					filtering trilinear
			}
		}
	}
}{CODE}

!!!GlowMaterialListener.h (add yo your sources)

{CODE(wrap="1", colors="c++")}#ifndef GLOWMATERIALLISTENER_H__
#define GLOWMATERIALLISTENER_H__

#include <Ogre.h>
#include <OgreMaterialManager.h>

class GlowMaterialListener : public Ogre::MaterialManager::Listener
{
protected:
	Ogre::MaterialPtr mBlackMat;
public:
	GlowMaterialListener()
	{
		mBlackMat = Ogre::MaterialManager::getSingleton().create("mGlowBlack", "Internal");
		mBlackMat->getTechnique(0)->getPass(0)->setDiffuse(0,0,0,0);
		mBlackMat->getTechnique(0)->getPass(0)->setSpecular(0,0,0,0);
		mBlackMat->getTechnique(0)->getPass(0)->setAmbient(0,0,0);
		mBlackMat->getTechnique(0)->getPass(0)->setSelfIllumination(0,0,0);
	}

	Ogre::Technique *handleSchemeNotFound(unsigned short, const Ogre::String& schemeName, Ogre::Material*mat, unsigned short, const Ogre::Renderable*)
	{
		if (schemeName == "glow")
		{
			//LogManager::getSingleton().logMessage(">> adding glow to material: "+mat->getName());
			return mBlackMat->getTechnique(0);
		}
		return NULL;
	}
};

#endif //GLOWMATERIALLISTENER_H__{CODE}

!!Load the compositor in your code

{CODE(wrap="1", colors="c++")}#include "GlowMaterialListener.h"
...
	{
		CompositorManager::getSingleton().addCompositor(mCamera->getViewport(), "Glow");
		CompositorManager::getSingleton().setCompositorEnabled(mCamera->getViewport(), "Glow", true);
		GlowMaterialListener *gml = new GlowMaterialListener();
		Ogre::MaterialManager::getSingleton().addListener(gml);
	}{CODE}

!!Add materials that glow
Add a new glow scheme technique to your material.

Original material: 

{CODE(wrap="1", colors="c++")}material AC3D/Box5W900/Mat004_NoTex
{
	technique
	{
		pass
		{
			emissive 1 1 1 0.3
			texture_unit
			{
				anim_texture W900_headlight_material.png 2 0
			}
		}
	}
}{CODE}

New material:

{CODE(wrap="1", colors="c++")}material AC3D/Box5W900/Mat004_NoTex
{
	technique
	{
		pass
		{
			emissive 1 1 1 0.3
			texture_unit
			{
				anim_texture W900_headlight_material.png 2 0
			}
		}
	}
	technique
	{
		scheme glow
		pass
		{
			texture_unit
			{
			texture W900_headlight_material_1.png
			}
			ambient 1 1 1
			diffuse 1 1 1
			specular 0 0 0 1
			emissive 0 0 0
		}
	}
}{CODE}

Please note that you can change the amount of the glow by modifying the ambient and diffuse values. (also during runtime)

!!Debugging
You can change "#define RENDER_SCENE 1" to "#define RENDER_SCENE 0" in glow.cg in order to just display glowing objects. Non-glowing objects will be black.

        

History

Information Version
Sat 01 of May, 2010 09:22 GMT-0000 jacmoe 9
Sat 01 of May, 2010 09:19 GMT-0000 jacmoe 8
Sat 02 of Jan, 2010 07:34 GMT-0000 jacmoe 7
Thu 31 of Dec, 2009 16:28 GMT-0000 jacmoe 6
Thu 31 of Dec, 2009 16:24 GMT-0000 jacmoe 5
Thu 31 of Dec, 2009 16:23 GMT-0000 jacmoe 4
Thu 31 of Dec, 2009 16:22 GMT-0000 jacmoe 3
Thu 31 of Dec, 2009 16:21 GMT-0000 jacmoe 2
Thu 31 of Dec, 2009 16:17 GMT-0000 jacmoe 1