I am using this cg shader for my scene node.
Code: Select all
void main_v(float4 Position: POSITION,
float3 Normal: NORMAL,
float2 Uv: TEXCOORD0,
float3 Tangent: TEXCOORD1,
float3 Binormal: TEXCOORD2,
out float4 oPosition: POSITION,
out float2 oUv: TEXCOORD0,
out float3 oLightDir: TEXCOORD1,
out float3 oEyeDir: TEXCOORD2,
out float3 oHalfAngle: TEXCOORD3,
out float2 oUv_env: TEXCOORD4,
uniform float4 lightPosition,
uniform float3 eyePosition,
uniform float4x4 WorldViewProj)
{
oPosition = mul(WorldViewProj, Position);
oUv = Uv;
float3 LightDir = normalize(lightPosition.xyz - (Position * lightPosition.w));
float3 EyeDir = eyePosition - Position.xyz;
float3x3 TBN = float3x3(Tangent, Binormal, Normal);
// Transformacja do Tangent Space
LightDir = normalize(mul(TBN, LightDir));
EyeDir = normalize(mul(TBN, EyeDir));
oLightDir = LightDir;
oEyeDir = EyeDir;
oHalfAngle = normalize(EyeDir + LightDir);
//float dis = -200;
//float4 tran_Pos = float4(Position.x+Normal.x*dis, Position.y+Normal.y*dis, Position.z+Normal.z*dis, 1);
float4 Proj_tran_pos = mul(WorldViewProj, Position);
oUv_env = Proj_tran_pos/Proj_tran_pos.w;
}
void main_f(float2 Uv: TEXCOORD0,
float3 LightDir : TEXCOORD1,
float3 EyeDir : TEXCOORD2,
float3 HalfAngle : TEXCOORD3,
float2 Uv_env: TEXCOORD4,
uniform float bumpness,
uniform float behind,
uniform float specularPow,
uniform float envPow,
uniform float3 LightDiffuse,
uniform float3 LightAmbient,
uniform sampler2D DecalMap : register(s0),
uniform sampler2D NormalMap : register(s1),
uniform sampler2D SpecularMap : register(s2),
uniform sampler2D EnvMap : register(s3),
out float4 oColor : COLOR)
{
float3 smooth = { 0.5f, 0.5f, 1.0f };
float3 Normal = tex2D (NormalMap, Uv).rgb;
Normal = lerp( smooth, Normal, bumpness );
Normal = normalize (Normal*2.0 -1.0);
float3 Diffuse = max (dot (LightDir, Normal), 0.0) * LightDiffuse;
float3 DecalColor = tex2D (DecalMap, Uv).rgb;
float3 AmbientColor = tex2D(DecalMap, Uv).rgb * LightAmbient;
float3 SpecularColor = tex2D(SpecularMap, Uv).rgb;
float Specular = max (dot (HalfAngle, Normal), 0.0);
Specular = pow (Specular, specularPow);
float2 projCoord = Uv_env;
projCoord += float2(1.0, 1.0);
projCoord *= 0.5;
projCoord = clamp(projCoord, 0.001, 0.999);
float3 EnvColor = tex2D(EnvMap, projCoord).rgb;
if(behind == 1.0)
envPow = 0;
oColor = float4 (DecalColor * Diffuse + Specular * SpecularColor + AmbientColor + EnvColor*envPow, 1.0);
}
The problem is when I get close to the reflection plane. the reflection effect got corrupted. When I move away and adjust the viewing angle of the camera. The reflection gets back to normal again. Here is a image of the effect when getting close to the reflection plane.
When moving away... adjusting viewing angle facing more to the plane...
I also checked out sio's PerpixelLighting_WithWater using HLSL shader, it has the same problem.
Corrupted
Moving away... adjusting viewing angle, etc.
However, when I switch from EDT_DIRECT3D9 to EDT_OPENGL, everything is rendered properly. There is no reflection corruption issue.
Has anyone faced a similar problem? I noticed the giroom demo from sio's website has aligning problem when moving camera. But I cannot figure out the real reason for this.
Is it related to the shader problem for calculate the envMap UV coordinates?
Guys, I really need your help on this. It is driving me mad...
My development env is: irrlicht 1.5, vs_2_0, ps_2_0.
My graphic card is: NVIDIA GeForce 9800 GT
[/code]