Custom Scene Node, Shaders and Texture Blending...

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
Memorial76
Posts: 103
Joined: Mon Aug 10, 2009 8:22 pm
Location: France

Custom Scene Node, Shaders and Texture Blending...

Post by Memorial76 »

It seems to be quite an easy and usual problem but I can't find the solution:

I have created a CustomSceneNode on which I add 2 textures. (I chose to create a customSceneNode for learning purpose)
On the '0' layer I have a background texture and on the '1' layer I have a foreground texture with alpha channel.
Of course, I use the setMaterialTexture to do it.

Here are the code of the OnRegisterSceneNode(), render(), setMaterialTexture() functions of my CustomSceneNode

Code: Select all

 
void CIrrHexagoneSceneNode::OnRegisterSceneNode()
{
    if (IsVisible)
                SceneManager->registerNodeForRendering(this);
 
        ISceneNode::OnRegisterSceneNode();
             
}
 
void CIrrHexagoneSceneNode::render()
{
    u16 indices[] = { 0,1,2,3,4,5,6,1 };
    irr::video::IVideoDriver* driver = SceneManager->getVideoDriver();
 
    driver->setMaterial(Material);
    driver->setTransform(video::ETS_WORLD, AbsoluteTransformation);
    driver->drawIndexedTriangleFan(&Vertices[0], 7, &indices[0], 6);
}
 
void CIrrHexagoneSceneNode::setMaterialTexture(u32 textureLayer, video::ITexture* texture)
{
    Material.setTexture(textureLayer, texture);
}
 
The aim is to see the background through the foreground where it's transparent. Nothing special there.

As Irrlicht can't do this I had to learn Shaders to blend the textures.

This is the onSetConstant() of my callBack

Code: Select all

void CShaderCallBack::OnSetConstants(video::IMaterialRendererServices* pServices, s32 userData)
{
    
        // on récupère un pointeur sur VideoDriver
        video::IVideoDriver* pDriver = pServices->getVideoDriver();
 
        // Matrice WorldViewProj
        core::matrix4 mWorldViewProj;
        mWorldViewProj = pDriver->getTransform(video::ETS_PROJECTION);
        mWorldViewProj *= pDriver->getTransform(video::ETS_VIEW);
        mWorldViewProj *= pDriver->getTransform(video::ETS_WORLD);
        // on l'envoie au GPU
        pServices->setVertexShaderConstant("mWorldViewProj", mWorldViewProj.pointer(), 16);
    
}
And finally the shader itself

Code: Select all

uniform float4x4 mWorldViewProj;
 
// Vertex shader output structure
struct VS_OUTPUT
{
    float4 Position  : POSITION;   // vertex position 
    float2 TexCoord0 : TEXCOORD0;  // tex 0 coords
    float2 TexCoord1 : TEXCOORD1;  // tex 1 coords
};
 
 
VS_OUTPUT vertexMain( in float4 vPosition : POSITION, float2 texCoord0 : TEXCOORD0, float2 texCoord1 : TEXCOORD1 )
{
    VS_OUTPUT Output;
    
    Output.Position = mul(vPosition, mWorldViewProj);
    Output.TexCoord0 = texCoord0;
    Output.TexCoord1 = texCoord1;
    
    return Output;
}
 
 
 
uniform sampler2D texsol : register(s0);
uniform sampler2D texherbe : register(s1);
 
    
// Pixel shader output structure
struct PS_OUTPUT
{
    float4 RGBColor : COLOR0;  // Pixel color    
};
    
PS_OUTPUT pixelMain( VS_OUTPUT Input ) 
{ 
    PS_OUTPUT Output;
        
    float4 col0 = tex2D(texsol, Input.TexCoord0);
    float4 col1 = tex2D(texherbe, Input.TexCoord1); 
    
    Output.RGBColor.rgb = col0.rgb * (1.0f - col1.a) + col1.rgb * col1.a;
    Output.RGBColor.a =col0.a;
    
    return Output;
    
}
 
After many tries I finally managed to write something almost working exept that the texture 1 doesn't fit. It's only a single color (probably the first pixel of the texture)
I thonk the problems comes from my customSceneNode but I can't find it despite many help on another forum.

Maybe someone here can help me.
My Gui/Event receiver Architecture Pattern (GERAP):
http://irrgerap.sourceforge.net/

My Blog about my "ProjectOne"
http://memorial76-project-one.blogspot.fr/
smso
Posts: 246
Joined: Fri Jun 04, 2010 3:28 pm
Location: Hong Kong

Re: Custom Scene Node, Shaders and Texture Blending...

Post by smso »

Have you setup the material of the node like:

Code: Select all

node->getMaterial(0).TextureLayer[0].Texture = driver->getTexture("...");
node->getMaterial(0).TextureLayer[1].Texture = driver->getTexture("...");
If you doubt this:
I thonk the problems comes from my customSceneNode but I can't find it despite many help on another forum.
try testing the shader on the material of a simple irrlicht cube scene node.

Regards,
smso
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: Custom Scene Node, Shaders and Texture Blending...

Post by hendu »

If you're not sending texcoord1 properly, just use texcoord 0 for both textures?
Memorial76
Posts: 103
Joined: Mon Aug 10, 2009 8:22 pm
Location: France

Re: Custom Scene Node, Shaders and Texture Blending...

Post by Memorial76 »

Greetings,
thanks for your interest
smso wrote:Have you setup the material of the node like:

Code: Select all

node->getMaterial(0).TextureLayer[0].Texture = driver->getTexture("...");
node->getMaterial(0).TextureLayer[1].Texture = driver->getTexture("...");
Nope i use setMaterialTexture(0, "..."). Is this potentially a problem?
If you doubt this:
I thonk the problems comes from my customSceneNode but I can't find it despite many help on another forum.
try testing the shader on the material of a simple irrlicht cube scene node.

Regards,
smso
[/quote]

I did and it failed the same way.
If you're not sending texcoord1 properly, just use texcoord 0 for both textures?
because the shader returns and error code in that case (X4515)

Thanks a lot for your answer.
My Gui/Event receiver Architecture Pattern (GERAP):
http://irrgerap.sourceforge.net/

My Blog about my "ProjectOne"
http://memorial76-project-one.blogspot.fr/
Memorial76
Posts: 103
Joined: Mon Aug 10, 2009 8:22 pm
Location: France

Re: Custom Scene Node, Shaders and Texture Blending...

Post by Memorial76 »

I have some more question related to my problem:

1-When I create the vertex of my node, I set up UV coordiantes for each. Are these coordinates matching for each texture layer?

2-When I try to use the same texcoord in my shader, I have this error:
"<38> error X4515: cannot bind sampler to user specified stage. In ps_1_1, samplers must be bound to the same stage as their TEXCOORD"

And yet it works with someone else's programme doing almost the same thing.

Here is my CallBack::OnSetConstants() should it help:

Code: Select all

 
void CShaderCallBack::OnSetConstants(video::IMaterialRendererServices* pServices, s32 userData)
{
    
    // on récupère un pointeur sur VideoDriver
        video::IVideoDriver* pDriver = pServices->getVideoDriver();
 
        // Matrice WorldViewProj
        core::matrix4 mWorldViewProj;
        mWorldViewProj = pDriver->getTransform(video::ETS_PROJECTION);
        mWorldViewProj *= pDriver->getTransform(video::ETS_VIEW);
        mWorldViewProj *= pDriver->getTransform(video::ETS_WORLD);
        // on l'envoie au GPU
        pServices->setVertexShaderConstant("mWorldViewProj", mWorldViewProj.pointer(), 16);
    
}
 
I don't understand why the shader cant get the right coordinates for the second texture layer...
My Gui/Event receiver Architecture Pattern (GERAP):
http://irrgerap.sourceforge.net/

My Blog about my "ProjectOne"
http://memorial76-project-one.blogspot.fr/
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: Custom Scene Node, Shaders and Texture Blending...

Post by hendu »

That error is pretty clear. Not supported in ps1.1. Do you really need that low a level?
Memorial76
Posts: 103
Joined: Mon Aug 10, 2009 8:22 pm
Location: France

Re: Custom Scene Node, Shaders and Texture Blending...

Post by Memorial76 »

I don't know, I guess I don't...
Actually someone wrote this shader for me since I'm totally beginner. Could anyone write something working I would be gratefull...
My Gui/Event receiver Architecture Pattern (GERAP):
http://irrgerap.sourceforge.net/

My Blog about my "ProjectOne"
http://memorial76-project-one.blogspot.fr/
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: Custom Scene Node, Shaders and Texture Blending...

Post by hendu »

In the line where you load that shader, specify a higher level than ps1.1.
Memorial76
Posts: 103
Joined: Mon Aug 10, 2009 8:22 pm
Location: France

Re: Custom Scene Node, Shaders and Texture Blending...

Post by Memorial76 »

I tried PS1.2 and 1.3 ... but nothing works, I still have the error :(
My Gui/Event receiver Architecture Pattern (GERAP):
http://irrgerap.sourceforge.net/

My Blog about my "ProjectOne"
http://memorial76-project-one.blogspot.fr/
Memorial76
Posts: 103
Joined: Mon Aug 10, 2009 8:22 pm
Location: France

Re: Custom Scene Node, Shaders and Texture Blending...

Post by Memorial76 »

I'm quite sure the problem comes from one of these lines:

Code: Select all

uniform sampler2D texsol : register(s0);
uniform sampler2D texherbe : register(s1);

Code: Select all

float4 col0 = tex2D(texsol, Input.TexCoord0);
    float4 col1 = tex2D(texherbe, Input.TexCoord0);
But I don't see how and why...

I tried to have a look at the microsoft help to understand samplers. But I can't get it...
My Gui/Event receiver Architecture Pattern (GERAP):
http://irrgerap.sourceforge.net/

My Blog about my "ProjectOne"
http://memorial76-project-one.blogspot.fr/
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: Custom Scene Node, Shaders and Texture Blending...

Post by hendu »

All the 1.x versions are DX8. Use 2 or 3 really.

http://www.downloadatoz.com/howto/pixel ... 23960.html
Memorial76
Posts: 103
Joined: Mon Aug 10, 2009 8:22 pm
Location: France

Re: Custom Scene Node, Shaders and Texture Blending...

Post by Memorial76 »

:D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D
YOU GOT IT RIGHT !!!!!!!!!!!!!!!!!!
THANKS A LOT :!: :!: :!: :!: :!: :!: :!: :!: :!: :!: :!: :!: :!:
My Gui/Event receiver Architecture Pattern (GERAP):
http://irrgerap.sourceforge.net/

My Blog about my "ProjectOne"
http://memorial76-project-one.blogspot.fr/
Post Reply