[1.0] Texture manipulation and post effects

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
TheMaverick
Posts: 4
Joined: Sun May 28, 2006 4:13 pm

[1.0] Texture manipulation and post effects

Post by TheMaverick »

Hi folks!

Ok, I tried to add post effects as described http://irrlicht.sourceforge.net/phpBB2/ ... hp?t=13437 here. I now have it but I want to add post effects object based. So I created two RTT textures and rendered the effects I want with the object I want on that two different textures (name it itex1 and itex2 :) ). So far, so good (Btw can the above done a different, e.g. more elegant or faster way?).

So now I want to add these two textures itex1+itex2, since I found no function for that I wrote one on my own.

Code: Select all


	static void AddTextures(Irrlicht::Video::ITexture* a, Irrlicht::Video::ITexture* b)
	{
		irr::video::ITexture* aa = a->get_NativeTexture();
		irr::video::ITexture* bb = b->get_NativeTexture();

		// aa will be the output texture

		// assume both textures have the same format! color and size!
		irr::core::dimension2d<irr::s32> dim = aa->getOriginalSize();
		irr::s32 pitch = aa->getPitch() / 2;
		irr::s16 *pa = (irr::s16*) aa->lock();
		irr::s16 *pb = (irr::s16*) bb->lock();
		
		for(int x = 0; x < dim.Width; x++)
		{
			for(int y = 0; y < dim.Height; y++)
			{
				pa[y* pitch + x] = pa[y*pitch+x] + pb[y*pitch+x];
			}
		}
		aa->unlock();
		bb->unlock();
	}
(Note that is code in the Irrlicht.Net wrapper, but as you see the irrlicht textures get converted in native c++ ones =) )

Which doesn't function, because it can not get lock on textures whiche are created with the createRenterTargetTexture function. But after applying the patch mentioned here http://irrlicht.sourceforge.net/phpBB2/ ... hp?t=10154, Irrlicht can get lock on the both textures.

So now my two problems:
- aa->getPitch() returns zero, anybody know why? (texture created with the createRenterTargetTexture function)
- The image looks bad (see below). I set the size of the itex1 and itex2 textures the same size as the output window does have.

Image

(Rendered with DirectX9)
TheMaverick
Posts: 4
Joined: Sun May 28, 2006 4:13 pm

Post by TheMaverick »

Noone a idea?
Sorry for whining.
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

Blending the textures on the CPU will be very slow. It might be faster and easier if you did the blending on the GPU using a shader.

The topic that you linked to doesn't appear to be about post processing effects. I'm assuming that you are already rendering the scene to texture, applying a shader, and then rendering that as a full screen quad.

If that is what you are doing, you can just apply a shader to combine the your two rtts. The following hlsl shader will average the colors sampled from two textures.

Code: Select all

float4x4 mWorldViewProj;  // World * View * Projection transformation

struct VS_OUTPUT
{
   float4 Position   : POSITION;   // vertex position 
   float2 TexCoord0   : TEXCOORD0;  // tex coords
   float2 TexCoord1   : TEXCOORD1;  // tex coords
};

struct PS_OUTPUT
{
    float4 RGBColor : COLOR0;  // Pixel color    
};

sampler2D tex0;
sampler2D tex1;


VS_OUTPUT vs_main( in float4 Position : POSITION,
                   float2 TexCoord    : TEXCOORD0 )
{
   VS_OUTPUT Output;

   Output.Position = mul(Position, mWorldViewProj);
	
   Output.TexCoord0 = TexCoord;
   Output.TexCoord1 = TexCoord;
	
   return Output;
}
	
PS_OUTPUT ps_main( float4 Position : POSITION,
                   float2 TexCoord0 : TEXCOORD0,
                   float2 TexCoord1 : TEXCOORD1 ) 
{ 
   PS_OUTPUT Output;

   float4 col0 = tex2D( tex0, TexCoord0 );
   float4 col1 = tex2D( tex1, TexCoord1 );
	
   Output.RGBColor = (col0 + col1) * .5;

   return Output;
}
You can use it like this...

Code: Select all

class MyShaderConstantCallback : public video::IShaderConstantSetCallBack
{
public:
   virtual void OnSetConstants(video::IMaterialRendererServices* services, s32 userData)
   {
      video::IVideoDriver* driver = services->getVideoDriver();

      core::matrix4 worldViewProj;
      worldViewProj  = driver->getTransform(video::ETS_PROJECTION);        
      worldViewProj *= driver->getTransform(video::ETS_VIEW);
      worldViewProj *= driver->getTransform(video::ETS_WORLD);
      services->setVertexShaderConstant("mWorldViewProj", worldViewProj.M, 16);
   }
};

   /// 
   /// code omitted...
   /// 

   MyShaderConstantCallback scc;

   s32 newMaterialType = driver->getGPUProgrammingServices()->addHighLevelShaderMaterialFromFiles(
"../../media/test.hlsl", "vs_main", video::EVST_VS_1_1,
"../../media/test.hlsl", "ps_main", video::EPST_PS_1_1, &scc);
   node->setMaterialType((video::E_MATERIAL_TYPE)newMaterialType);
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Shouldn't the fixed pipeline be able to do this as well, so just use the correct material or define your own.
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

Of course, but Irrlicht doesn't provide a material type for blending the two textures, and he is most likely already using shaders for the post processing effects.

Travis
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

Actually Irrlicht does provide a material type for this. You just need to adjust the vertex alpha to get it to work...

Code: Select all

   scene::IAnimatedMesh* dwarf = smgr->getMesh("../../media/dwarf.x");
   smgr->getMeshManipulator()->setVertexColorAlpha(dwarf->getMesh(0), 127);

   scene::IAnimatedMeshSceneNode* node = smgr->addAnimatedMeshSceneNode(dwarf);
   node->setMaterialTexture(0, driver->getTexture("../../media/t1.bmp"));
   node->setMaterialTexture(1, driver->getTexture("../../media/faerie2.bmp"));
   node->setMaterialFlag(video::EMF_LIGHTING, false);
   node->setMaterialType(video::EMT_SOLID_2_LAYER);
TheMaverick
Posts: 4
Joined: Sun May 28, 2006 4:13 pm

Post by TheMaverick »

First a big Thanks for your replies.
I will try that. =)

Hm, actually I thougt I tried EMT_SOLID_2_LAYER ...
Post Reply