The glass is sometimes very similar to Glow shader on wiki:
http://www.ogre3d.org/tikiwiki/Glow&highlight=Glow
depth must be considered as well or you could shift uv on things that are in front of glass.
This shader is much easier, and doesn't use depth, because the shifts are too little, and I mustn't know normals and depth of a surface.
With big texture shifts, it doesn't look good.
The main problem of the shader is that sometimes shifts are getting screen tex-coords out of the glass surface, and it looks like glass mesh is not a primitive, such as a scaled box(it's rough on corners)! But again, with little shifts it looks ok.
Take a look:
C++ code
Code: Select all
if (StringConverter::parseBool(cf.getSetting("enableGlass","","true")))
{
LogManager::getSingleton().logMessage("Glass is now enabled!");
Camera* mCamera = global::getSingleton().getCamera();
CompositorManager::getSingleton().addCompositor(mCamera->getViewport(), "Run3Glass");
CompositorManager::getSingleton().setCompositorEnabled(mCamera->getViewport(), "Run3Glass", true);
if (gml!=0)
{
gml = new GlowMaterialListener();
Ogre::MaterialManager::getSingleton().addListener(gml);
}
}
By the way, my GlowMaterialListener is from ogre wiki, and controls both glow and glass.
Code: Select all
#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 == "Scheme/Glow")
{
LogManager::getSingleton().logMessage(">> adding glow to material: "+mat->getName());
return mBlackMat->getTechnique(0);
}*/
if (schemeName == "glow")
{
return mBlackMat->getTechnique(0);
}
if (schemeName == "GlassObj")
{
return mBlackMat->getTechnique(0);
}
return NULL;
}
};
#endif //GLOWMATERIALLISTENER_H__
glassFilter.cg
Code: Select all
float4 GlassFilterFP
(
float2 uv: TEXCOORD0,
uniform sampler2D scene: register(s0),
uniform sampler2D glass: register(s1),
uniform sampler2D n: register(s2)
) : COLOR
{
float4 colour = tex2D(glass,uv);
if(colour.xyz!=float3(0))
{
float4 normal = 2 * (tex2D(n,uv) - 0.5);
//return tex2D(scene, uv + float2(sin(uv[0]*6.28f*2)/50+0.02f,sin(uv[1]*6.28f*2)/50+0.02f));
return tex2D(scene, uv + normal.xy * 0.01);
}
else
{
return tex2D(scene, uv);
}
}
Fragment and Material scripts(uses texture from ogre media,can be changed to anything else more interesting):
Code: Select all
fragment_program GlassFilter_fp cg
{
source glassFilter.cg
entry_point GlassFilterFP
profiles ps_2_x arbfp1
}
material Run3/GlassFilter
{
technique
{
pass
{
cull_hardware none
cull_software none
depth_func always_pass
fragment_program_ref GlassFilter_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
}
texture_unit glassMap
{
tex_coord_set 1
tex_address_mode clamp
filtering trilinear
texture WaterNormal1.tga
}
}
}
}
Glass Compositor:
Code: Select all
compositor Run3Glass
{
technique
{
texture rt0 target_width target_height PF_A8R8G8B8
texture glassMap target_width target_height PF_A8R8G8B8
//Fetch scene contents.
target rt0
{
input previous
}
//Get scene rendered with 'GlassObj' scheme
target glassMap
{
input none
material_scheme GlassObj
pass clear
{
}
pass render_scene
{
}
}
target_output
{
input none
pass clear
{
}
pass render_quad
{
material Run3/GlassFilter
input 0 rt0
input 1 glassMap
}
}
}
}
Example glass material:
(cubemap must be changed)
Code: Select all
material EXAMPLE
{
technique
{
scheme GlassObj
pass
{
lighting off
scene_blend alpha_blend
depth_write off
texture_unit
{
cubic_texture glass01.png combinedUVW
tex_address_mode clamp
env_map cubic_reflection
}
}
}
}