I have a triangle with vertices having positions as :
(-1,0,1) , (0,2,1) , (1,0,1)
The vertex normal for all 3 vertices is (0,0,-1).
I assigned diffuse color to the 3 vertices, used a texture and wrote vertex and pixel shaders which work just fine.
Vertex shader :
[SOURCE]
// Vertex shader input structure
struct VS_INPUT
{
float4 Position : POSITION;
float2 Texture : TEXCOORD0;
float4 DiffuseColor : COLOR0;
};
// Vertex shader output structure
struct VS_OUTPUT
{
float4 Position : POSITION;
float2 Texture : TEXCOORD0;
float4 DiffuseColor : COLOR0;
};
// Global variables
float4x4 WorldViewProj;
// Name: Simple Vertex Shader
// Type: Vertex shader
// Desc: Vertex transformation and texture coord pass-through
//
VS_OUTPUT vs_main( in VS_INPUT In )
{
VS_OUTPUT Out; //create an output vertex
Out.Position = mul(In.Position,
WorldViewProj); //apply vertex transformation
Out.Texture = In.Texture; //copy original texcoords
Out.DiffuseColor = In.DiffuseColor;
return Out; //return output vertex
}
[/SOURCE]
[/source]
Pixel shader
[SOURCE]
// Pixel shader input structure
struct PS_INPUT
{
float4 Position : POSITION;
float2 Texture : TEXCOORD0;
float4 DiffuseColor : COLOR0;
};
// Pixel shader output structure
struct PS_OUTPUT
{
float4 Color : COLOR0;
};
// Global variables
sampler2D Tex0;
float blendRatio;
// Name: Simple Pixel Shader
// Type: Pixel shader
// Desc: Fetch texture and blend with constant color
//
PS_OUTPUT ps_main( in PS_INPUT In )
{
PS_OUTPUT Out; //create an output pixel
Out.Color = tex2D(Tex0, In.Texture); //do a texture lookup
Out.Color *= In.DiffuseColor*blendRatio; //do a simple effect
return Out; //return output pixel
}
[/SOURCE]
[/source]
Now, instead of using the diffuse color i want to try simulating a light source.
So I modified the vertex shader :
[SOURCE]
// Vertex shader input structure
struct VS_INPUT
{
float4 Position : POSITION;
float2 Texture : TEXCOORD0;
float4 DiffuseColor : COLOR0;
float3 Normal : NORMAL;
};
// Vertex shader output structure
struct VS_OUTPUT
{
float4 Position : POSITION;
float2 Texture : TEXCOORD0;
float4 DiffuseColor : COLOR0;
};
// Global variables
float4x4 WorldViewProj;
float3 LightPosition;
float4 LightColor = {0.0f, 0.0f, 1.0f, 1.0f};
// Name: Simple Vertex Shader
// Type: Vertex shader
// Desc: Vertex transformation and texture coord pass-through
//
VS_OUTPUT vs_main( in VS_INPUT In )
{
VS_OUTPUT Out; //create an output vertex
float3 lightDirection = normalize (In.Position - LightPosition);
float lightContibution = dot (lightDirection, In.Normal);
float4 colorContibutedbyLight = 0;
colorContibutedbyLight = LightColor * lightContibution;
Out.Position = mul(In.Position,
WorldViewProj); //apply vertex transformation
Out.Texture = In.Texture; //copy original texcoords
Out.DiffuseColor = colorContibutedbyLight;
return Out; //return output vertex
}
[/SOURCE]
[/source]
The new uniform variable here is 'LightPosition' which i set as (0,0.5,-1)
But i do not get black triangle. Any suggestions ?
[Edited by - mishal153 on April 16, 2010 1:06:57 AM]