Tangent and Binormal

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!
Post Reply
Matix522
Posts: 15
Joined: Wed Jul 02, 2014 11:48 am
Location: Poland

Tangent and Binormal

Post by Matix522 »

Hi,
I need a help I created my Normalmapping Shader so GPU need to get from vertex tangent and binormal
but i also need 2TCoords to correct texturing of my model (ITerrainSceneNode)
HLSL Shader Code
Vertex:

Code: Select all

uniform float4x4 WorldViewProj;
uniform float3 LightPosition;
struct VS_INPUT 
{ 
    float4 Position : POSITION0; 
    float2 alphamap : TEXCOORD0; 
    float2 tex : TEXCOORD1;
    float3 normal : NORMAL;
};
struct VS_OUTPUT
{ 
    float4 Position : POSITION0; 
    float2 alphamap : TEXCOORD0; 
    float2 tex : TEXCOORD1;
    float3 normal : TEXCOORD2;
    float4 Position2 : TEXCOORD3;
    float3 lightDir : TEXCOORD4;
}; 
VS_OUTPUT main(in VS_INPUT Input) 
{ 
    VS_OUTPUT Output; 
    float4 light = float4(LightPosition,1);
    float3 lightDir = normalize(light.xyz -  (Input.Position * light.w));
    Output.Position = mul( Input.Position, WorldViewProj );
    Output.alphamap = Input.alphamap; 
    Output.tex = Input.tex;
    Output.normal =Input.normal;
    Output.Position2= Output.Position;
    Output.lightDir = lightDir;
}
 
 
Pixel:

Code: Select all

uniform float4x4 World;
float3 DiffuseLightDirection = float3(1, 1, 1);
float4 DiffuseColor = float4(1, 1, 1, 1);
float DiffuseIntensity = 1.0;
                             
float Shininess = 200;
float4 SpecularColor = float4(1, 1, 1, 1);
float SpecularIntensity = 1;
float3 ViewVector = float3(1, 0, 0);
                             
sampler2D splatMap = sampler_state
{ 
    ADDRESSU = WRAP; 
    ADDRESSV = WRAP; 
    ADDRESSW = WRAP; 
};
sampler2D layer_red  = sampler_state
{ 
    MipFilter = LINEAR; 
    MinFilter = LINEAR; 
    MagFilter = LINEAR; 
    ADDRESSU = WRAP; 
    ADDRESSV = WRAP; 
    ADDRESSW = WRAP; 
};
                            
sampler2D layer_green = sampler_state
{ 
    MipFilter = LINEAR; 
    MinFilter = LINEAR; 
    MagFilter = LINEAR; 
    ADDRESSU = WRAP; 
    ADDRESSV = WRAP; 
    ADDRESSW = WRAP; 
};
                            
sampler2D layer_blue = sampler_state
{ 
    MipFilter = LINEAR; 
    MinFilter = LINEAR; 
    MagFilter = LINEAR; 
    ADDRESSU = WRAP; 
    ADDRESSV = WRAP; 
    ADDRESSW = WRAP; 
};
                            
                            
                            
sampler2D layer_Normal_red  = sampler_state
{ 
    MipFilter = LINEAR; 
    MinFilter = LINEAR; 
    MagFilter = LINEAR; 
    ADDRESSU = WRAP; 
    ADDRESSV = WRAP; 
    ADDRESSW = WRAP; 
};
                            
sampler2D layer_Normal_green = sampler_state
{ 
    MipFilter = LINEAR; 
    MinFilter = LINEAR; 
    MagFilter = LINEAR; 
    ADDRESSU = WRAP; 
    ADDRESSV = WRAP; 
    ADDRESSW = WRAP; 
}; 
 
sampler2D layer_Normal_blue = sampler_state
{ 
    MipFilter = LINEAR; 
    MinFilter = LINEAR; 
    MagFilter = LINEAR; 
    ADDRESSU = WRAP; 
    ADDRESSV = WRAP; 
    ADDRESSW = WRAP; 
};
struct VS_OUTPUT
{ 
    float4 Position : POSITION0; 
    float2 alphamap : TEXCOORD0; 
    float2 tex : TEXCOORD1;
    float3 normal : TEXCOORD2;
    float4 Position2 : TEXCOORD3;
    float3 lightDir : TEXCOORD4;
    }; 
struct PS_OUTPUT
{ 
    float4 diffuse : COLOR0; 
};
float3x3 generateTBN(float3 vertexPosition, float2 texCoord, float3 normal)
{
     float3 p_dx = ddx(vertexPosition);
     float3 p_dy = ddy(vertexPosition);
     
     float2 tc_dx = ddx(texCoord);
     float2 tc_dy = ddy(texCoord);
     
     float3 t = normalize( tc_dy.y * p_dx - tc_dx.y * p_dy );
     float3 b = normalize( tc_dy.x * p_dx - tc_dx.x * p_dy );
     
     float3 n = normalize(normal);
     float3 x = cross(b, t);
     t = cross(x, n);
     t = normalize(t);
     
     b = cross( t, n );
     b = normalize(b);
     return float3x3(t, b, n);
}
PS_OUTPUT main(in VS_OUTPUT input) 
{ 
     PS_OUTPUT output = (PS_OUTPUT)0; 
     
     float3x3 TBN = generateTBN(input.Position2,input.alphamap,input.normal);
     float3 LightDir = mul(TBN, input.lightDir);
     float4 splat = tex2D(splatMap, input.alphamap); 
     float4 red = tex2D(layer_red, input.tex);
     float4 green = tex2D(layer_green, input.tex); 
     float4 blue = tex2D(layer_blue, input.tex); 
     
     float4 bump = float4(splat.r*tex2D(layer_Normal_red, input.tex)+splat.g*tex2D(layer_Normal_green, input.tex)+splat.b*tex2D(layer_Normal_blue, input.tex))*float4(1,1,1,splat.a);
     
     float4 textureColor = float4(splat.r*red+splat.g*green+splat.b*blue)*float4(1,1,1,splat.a);
     float a = textureColor.a;
     output.diffuse = textureColor * dot(bump, LightDir);
     output.diffuse.a=a;
     return output;
}
This shader is improvement of that shader:
http://irrlicht.sourceforge.net/forum/v ... hp?t=38676
In pixel shader I can calculate tangent and binormal, but they arn't interpolated so this don't look good
Lightning
http://zapodaj.net/1a672fd1fdad5.jpg.html
Tanget
http://zapodaj.net/59927d85816bb.jpg.html
Binormal
http://zapodaj.net/7971fcfb18208.jpg.html
Is there any way to calculate them in vertex shade,
if not, can somebody tell me how to tell engine about additional fields in S3DVertex2TCoords/S3DVertexTangent
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: Tangent and Binormal

Post by hendu »

There is a technique for tangentless normal mapping, if you search with those words plus my username you should find the pdf link.
Matix522
Posts: 15
Joined: Wed Jul 02, 2014 11:48 am
Location: Poland

Re: Tangent and Binormal

Post by Matix522 »

i found this
http://irrlicht.sourceforge.net/forum/v ... start=1170
but i'm getting 404 error
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Tangent and Binormal

Post by CuteAlien »

Try clicking that link again - maybe there had been some server trouble. Link works here.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: Tangent and Binormal

Post by mongoose7 »

The SF link works, but the link in that document doesn't work. The OP is looking for the source document.
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: Tangent and Binormal

Post by hendu »

You need more google-fu training ;) in such situations you would google for the filename.
Mel
Competition winner
Posts: 2292
Joined: Wed May 07, 2008 11:40 am
Location: Granada, Spain

Re: Tangent and Binormal

Post by Mel »

If your secondary texture map coordinates are normalized (i.e. their range is -1,1 or 0,1, for a lightmap, for instance) it is easy to enconde them on the color channel of the vertices, and use a vertex format which already has the tangent space calculated.
"There is nothing truly useless, it always serves as a bad example". Arthur A. Schmitt
Post Reply