My SceneNode for filter post processing shaders

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
ItisFree

Post by ItisFree »

well , the code is all in the top of the thread . It is in only one file

the definition of node is the part :

Code: Select all

class MyShaderCallBack : public video::IShaderConstantSetCallBack 
{ 
public: 

   float viewport_inv_width; 
   float viewport_inv_height; 

   virtual void OnSetConstants(video::IMaterialRendererServices* services) 
   { 
      video::IVideoDriver* driver = services->getVideoDriver(); 

      services->setVertexShaderConstant("viewport_inv_width", reinterpret_cast<f32*>(&viewport_inv_width), 1); 
      services->setVertexShaderConstant("viewport_inv_height", reinterpret_cast<f32*>(&viewport_inv_height), 1); 
             
   } 
}; 


class CBaseFilter : public scene::ISceneNode 
{ 

   core::aabbox3d<f32> Box; 
   video::S3DVertex Vertices[6]; 

public: 
   video::SMaterial Material; 
   c8* vsFileName; // filename for the vertex shader 
   c8* psFileName; // filename for the pixel shader 

   int newMaterialType1; 
   MyShaderCallBack* mc; 

   video::ITexture* rt0; 
   int alto; 
   int ancho; 

public: 

   CBaseFilter(scene::ISceneNode* parent, scene::ISceneManager* mgr, s32 id) 
      : scene::ISceneNode(parent, mgr, id) 
   { 
      Material.Wireframe = false; 
      Material.Lighting = false; 

      Vertices[0] = video::S3DVertex(-1.0f, -1.0f, 0.0f,1,1,0, video::SColor(255,0,255,255), 0.0f, 1.0f); 
      Vertices[1] = video::S3DVertex(-1.0f,  1.0f, 0.0f,1,1,0, video::SColor(255,0,255,255), 0.0f, 0.0f); 
      Vertices[2] = video::S3DVertex( 1.0f,  1.0f, 0.0f,1,1,0, video::SColor(255,0,255,255), 1.0f, 0.0f); 
      Vertices[3] = video::S3DVertex( 1.0f, -1.0f, 0.0f,1,1,0, video::SColor(255,0,255,255), 1.0f, 1.0f); 
      Vertices[4] = video::S3DVertex(-1.0f, -1.0f, 0.0f,1,1,0, video::SColor(255,0,255,255), 0.0f, 1.0f); 
      Vertices[5] = video::S3DVertex( 1.0f,  1.0f, 0.0f,1,1,0, video::SColor(255,0,255,255), 1.0f, 0.0f); 
    
      Box.reset(Vertices[0].Pos); 
      for (s32 i=1; i<4; ++i) 
         Box.addInternalPoint(Vertices[i].Pos); 

      mc = new MyShaderCallBack(); 

      video::IVideoDriver* driver = SceneManager->getVideoDriver(); 
      rt0 = driver->createRenderTargetTexture(core::dimension2d<s32>(640,480)); 
      setMaterialTexture(0, rt0); 
   } 


   virtual void OnPreRender() 
   { 
      if (IsVisible) 
         //SceneManager->registerNodeForRendering(this); 

      ISceneNode::OnPreRender(); 
   } 


   virtual void render() 
   { 
      u16 indices[] = {0,1,2,3,4,5}; 
      video::IVideoDriver* driver = SceneManager->getVideoDriver(); 

      driver->setMaterial(Material); 
      driver->setTransform(video::ETS_WORLD, AbsoluteTransformation); 
      driver->drawIndexedTriangleList(&Vertices[0], 6, &indices[0], 2); 
   } 


   virtual const core::aabbox3d<f32>& getBoundingBox() const 
   { 
      return Box; 
   } 

   virtual s32 getMaterialCount() 
   { 
      return 1; 
   } 

   virtual video::SMaterial& getMaterial(s32 i) 
   { 
      return Material; 
   }    
}; 
It is NOT necesary compile it, or other thing. You can put it in any file (.h , your own main.cpp, anyone...)

This node need file shader (hlsl,glsl,vs,ps etc , anyone) for the effect post-processing

In the code of top, i put three examples (three files shaders):

normal.hlsl ->it doesnt change anything
black&white.hlsl-> (no commnet)
blur.hlsl->(no comment)

(you can do your own shaders files and put it in this forum for all people)

it is VERY simple

You can put a lot of nodes-filter , but i recommend only two like maximun, for do loops

In the example of the top of thread i put only one filter black&white, like old TV

For declaration the filter in code main.cpp:

Code: Select all

//1)create of object 
   CBaseFilter *myFilter = new CBaseFilter(smgr->getRootSceneNode(), smgr, 666); 
    
   //2)select shader 
   myFilter->psFileName = "Black&White.hlsl"; 
   myFilter->vsFileName = "Black&White.hlsl"; 

   //3)create "new material shader" and associate with filter 
   myFilter->newMaterialType1 = gpu->addHighLevelShaderMaterialFromFiles( 
            myFilter->vsFileName,"vertexMain", video::EVST_VS_1_1, 
            myFilter->psFileName, "pixelMain", video::EPST_PS_1_1, 
            myFilter->mc, video::EMT_SOLID); 

   myFilter->setMaterialType((video::E_MATERIAL_TYPE)myFilter->newMaterialType1); 

   //4) pass the constant-variables to IShaderConstantSetCallBack 
   myFilter->mc->viewport_inv_height=1/480; 
   myFilter->mc->viewport_inv_width=1/640; 

   myFilter->drop(); 
and, the rest , it is like any program of irrlicht. you create your levels, models, billboard, light, etc.

BUT the DIFFERENT is in the RENDER SCENE
the node filter has a texture rrender target. you select that render target and render your scene nomal (example of main.cpp)

Code: Select all

while(device->run()) 
   { 
      driver->beginScene(true, true, video::SColor(0,100,100,100)); 

      driver->setRenderTarget(myFilter->rt0,true, true, video::SColor(0,0,0,255)); 
      smgr->drawAll(); 
now, the node has the image 2d result ( the scene)
Now you select the render target 0 (backbuffer) and render ONLY the node-filter :

Code: Select all

driver->setRenderTarget(0); 
      myFilter->render(); 
      driver->endScene(); 
   } 

   device->drop(); 
    
   return 0; 
} 
it paint the backbuffer working of filter shader post-processing of your node

Explication:
http://pruebalaboratorio.iespana.es/Dibujo.bmp
Examples:
http://pruebalaboratorio.iespana.es/1.bmp
http://pruebalaboratorio.iespana.es/2.bmp

NOTE (for the example of the top you need copy the texture "wall.bmp" , file "black&white.hlsl" and DIR "include" of irrlicht in the same DIR of the your main.cpp)

Other question is combinate various nodes doing loops. For example for doing complex blur. It is explicated more top

The idea for this node-filter-shader is the people can do your own filters-shader (files hlsl,glsls,vs or ps, and put it for all pepole of irrlicht)
Xaron
Posts: 310
Joined: Sun Oct 16, 2005 7:39 am
Location: Germany
Contact:

Post by Xaron »

Excellent! Thanks a lot! :)

Regards - Xaron
omaremad_

Post by omaremad_ »

Nice work man

It uses the same technique of putting a polgon infront of the camera (for my nightvision shader) but i experinece lag when the camera turns

the polygon lags behind the camera movement do u get the same

nice shaders any way ill download them if i get time

maybe we should make a shader thread for all of us
ItIsFree

Post by ItIsFree »

Very thanks all people for their comments, i am very happy

To omaremad : I know you did a thread with shader,you did a great work, i think if you want, we must work together for a nodes and shaders for irrlicht
I hope you will post me about this

(sorry, my english is very bad) :D

other thing : yeah, the technique is "a polgon infront of the camera" , but it is diferrent, because, if you look the polpon and the function "vs_main" in hlsl, i work with coordinates "screen" , not with coordinates model or view or project, only coordinates -1 to 1, for this reason , any polgon can be behind of camera

I have shader for :

NoFilter (dont anything)
Blur
NightVision
Black&Wihite

I think :

Alpha test
Mask test
DOF
blending
etc

Like you know better than me the world of irrlicht and his wiki web, you may desing it, and contact with me for work together.
If any people want work too , contat with us :P
Guest

Post by Guest »

pretty cool work

i meant to create a new forum section like beggineres and faq and opendiscusion

1 qustion how did u get the last frames texture for motion blur?
Xaron
Posts: 310
Joined: Sun Oct 16, 2005 7:39 am
Location: Germany
Contact:

Post by Xaron »

ItsFree I would be interested to work with you. As I'm a beginner with shaders I'm an expert in C++, which means nothing (of course) for shader programming. :lol:

Also I could provide a real fast ftp for (whatever) files. ;)

Regards - Xaron
sRc
Posts: 431
Joined: Thu Jul 28, 2005 1:44 am
Location: Salt Lake City, Utah
Contact:

Post by sRc »

hmmmm....

i must be missing something... i tried compiling the main.cpp in your first post as-is, and am getting the error

Code: Select all

main.cpp(63) : error C2259: 'MyShaderCallBack' : cannot instantiate abstract class
on this line (in bold):
code wrote: Box.reset(Vertices[0].Pos);
for (s32 i=1; i<4; ++i)
Box.addInternalPoint(Vertices.Pos);

mc = new MyShaderCallBack();

video::IVideoDriver* driver = SceneManager->getVideoDriver();
rt0 = driver->createRenderTargetTexture(core::dimension2d<s32>(640,480));
setMaterialTexture(0, rt0);


EDIT: hmmm.... just compiled it on my other machine (using 0.12 instead of 0.14) and it compiled fine... I'll have to look at this some more
The Bard sRc

Blog | Twitter
sRc
Posts: 431
Joined: Thu Jul 28, 2005 1:44 am
Location: Salt Lake City, Utah
Contact:

Post by sRc »

found the problem, OnSetConstants() was changed in 0.14 to take 2 paramaters now, instead of one as before.

changed

Code: Select all

class MyShaderCallBack : public video::IShaderConstantSetCallBack
{
public:

   float viewport_inv_width;
   float viewport_inv_height;

   virtual void OnSetConstants(video::IMaterialRendererServices* services)
   {
      video::IVideoDriver* driver = services->getVideoDriver();

      services->setVertexShaderConstant("viewport_inv_width", reinterpret_cast<f32*>(&viewport_inv_width), 1);
      services->setVertexShaderConstant("viewport_inv_height", reinterpret_cast<f32*>(&viewport_inv_height), 1);

   }
};
to

Code: Select all

class MyShaderCallBack : public video::IShaderConstantSetCallBack
{
public:

   float viewport_inv_width;
   float viewport_inv_height;

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

      services->setVertexShaderConstant("viewport_inv_width", reinterpret_cast<f32*>(&viewport_inv_width), 1);
      services->setVertexShaderConstant("viewport_inv_height", reinterpret_cast<f32*>(&viewport_inv_height), 1);

   }
};
and it works fine now :)
The Bard sRc

Blog | Twitter
ItIsFree

Post by ItIsFree »

Very thanks The Anaconda (español? ) . This is the good of open source, and the communities.

To Xaron: ok, very thanks, first we need talk with omaremad for we know what he thinks and after we can work in it

For shader , there is a great ebook : Shaders.for.Game.Programmers.and.Artists

the charpters 5 to 10 , talk about filters post processing, it can help us
Guest

Post by Guest »

btw im on a study leave till may so no programming for me just go ahead and do ur good shaders
Xaron
Posts: 310
Joined: Sun Oct 16, 2005 7:39 am
Location: Germany
Contact:

Post by Xaron »

Thanks for the hint, ItsFree! ;)

Regards - Xaron
ItIsFree

Post by ItIsFree »

Hello all!!!

I have done the filter "alpha testing" and filter "blending colour"

The new results :

http://pruebalaboratorio.iespana.es/dibujo2.bmp

I want to do a tutorial about shader post-procesing in irrlicht for explain the source codes for people can do your own shaders and effect

But my english is very bad, i think do it in spanish , and if someone want to translalate to english , the people of irrlicht will can read it

plz , if anyone want to do it, post in this thread and i will begin my work
Guest

Post by Guest »

Waiting for HDR :)
tip
Posts: 50
Joined: Fri Feb 13, 2004 8:53 am
Location: grenoble, France
Contact:

great work

Post by tip »

looks promising, i've never been looking too much at shaders; would be a good start of you can post some code.
Lenn

bloom?

Post by Lenn »

Hi, would you post the example with the HDR sort of bloom/guasian filter you have there?

This looks cool, thanks.
Post Reply