enmaniac
11-05-2005, 09:30 AM
Hi! I wrote vertex and fragment shader to texture a planet in the way that bright side is of another texture than dark one. The light is at the same position all the time.
The planet rotates around its axis. Everything is fine except that bright side rotates with the planet!!!! I am not sure where is the problem.
Here is a screen shot to illustrate the problem:
http://www.republika.pl/enmaniac/images/textureproblem0.JPG
The shaders are:
// VERTEX SHADER
struct VertexIn
{
float4 position : POSITION;
float4 color : COLOR;
float3 vNormal : NORMAL;
float2 texCoord : TEXCOORD0;
const uniform float4x4 modelViewProjMatrix;
const uniform float3 vLightPosition;
};
struct VertexOut
{
float4 position : POSITION;
float4 color : COLOR;
float2 texCoord : TEXCOORD0;
float3 vLightVector : TEXCOORD1;
float3 vNormalVector : TEXCOORD2;
};
VertexOut main(VertexIn In)
{
VertexOut Out;
Out.position = mul( In.modelViewProjMatrix, In.position );
Out.color = In.color;
Out.texCoord = In.texCoord;
Out.vLightVector = normalize( In.vLightPosition-Out.position.xyz );
Out.vNormalVector = In.vNormal;
return Out;
}
// PIXEL SHADER
struct FragmentIn
{
float4 color : COLOR;
float3 vLightVector : TEXCOORD1;
float3 vNormalVector : TEXCOORD2;
float2 texCoord : TEXCOORD0;
uniform sampler2D textureLight : TEXUNIT0;
uniform sampler2D textureDark : TEXUNIT1;
};
struct FragmentOut
{
float4 color : COLOR;
};
FragmentOut main(FragmentIn In)
{
FragmentOut Out;
float lightCoeff = dot( In.vNormalVector, In.vLightVector );
float4 brightSample = tex2D( In.textureLight, In.texCoord );
float4 darkSample = tex2D( In.textureDark, In.texCoord );
Out.color = lerp (darkSample, brightSample * max(lightCoeff, 0), lightCoeff * 0.5 + 0.5);
return Out;
}
The planet rotates around its axis. Everything is fine except that bright side rotates with the planet!!!! I am not sure where is the problem.
Here is a screen shot to illustrate the problem:
http://www.republika.pl/enmaniac/images/textureproblem0.JPG
The shaders are:
// VERTEX SHADER
struct VertexIn
{
float4 position : POSITION;
float4 color : COLOR;
float3 vNormal : NORMAL;
float2 texCoord : TEXCOORD0;
const uniform float4x4 modelViewProjMatrix;
const uniform float3 vLightPosition;
};
struct VertexOut
{
float4 position : POSITION;
float4 color : COLOR;
float2 texCoord : TEXCOORD0;
float3 vLightVector : TEXCOORD1;
float3 vNormalVector : TEXCOORD2;
};
VertexOut main(VertexIn In)
{
VertexOut Out;
Out.position = mul( In.modelViewProjMatrix, In.position );
Out.color = In.color;
Out.texCoord = In.texCoord;
Out.vLightVector = normalize( In.vLightPosition-Out.position.xyz );
Out.vNormalVector = In.vNormal;
return Out;
}
// PIXEL SHADER
struct FragmentIn
{
float4 color : COLOR;
float3 vLightVector : TEXCOORD1;
float3 vNormalVector : TEXCOORD2;
float2 texCoord : TEXCOORD0;
uniform sampler2D textureLight : TEXUNIT0;
uniform sampler2D textureDark : TEXUNIT1;
};
struct FragmentOut
{
float4 color : COLOR;
};
FragmentOut main(FragmentIn In)
{
FragmentOut Out;
float lightCoeff = dot( In.vNormalVector, In.vLightVector );
float4 brightSample = tex2D( In.textureLight, In.texCoord );
float4 darkSample = tex2D( In.textureDark, In.texCoord );
Out.color = lerp (darkSample, brightSample * max(lightCoeff, 0), lightCoeff * 0.5 + 0.5);
return Out;
}