hey!
since i'm about to learn shading, i'll post shader snippets for you to try out. you can of course comment them, i'm far away from being an professional so i'm grateful for any advice you may give me. all the snippets will be in hlsl and will need little to no adjustment to work with 3dgs, if so i'll state it in a comment. just save it in a .fx-file and load it. ok, here we go.
diffuse vertex lighting, beige coloured. only one light source, neither specular nor ambient light (i'll add that later on).
Code:
//------------------------------------
float4x4 matWVP : WorldViewProjection;
float4x4 matW : World;
float4 lightPos; // you need to set this to a proper light position
//------------------------------------
struct vertexInput {
float4 Position : POSITION0;
float3 Normal : NORMAL0;
};
struct vertexOutput {
float4 Position : POSITION0;
float4 Diffuse : COLOR0;
};
struct pixelOutput {
float4 Color : COLOR0;
};
//------------------------------------
void VS(in vertexInput a, out vertexOutput b)
{
// transform position
b.Position = mul(float4(a.Position.xyz , 1.0) , matWVP);
// get the position of the vertex in the world
float3 posWorld = mul(a.Position, matW).xyz;
// get normal
float3 normal = mul(a.Normal, matW).xyz;
// get light normal vector
float3 light = normalize(lightPos - posWorld);
b.Diffuse = float4(1, 0.9, 0.6, 1) * saturate(dot(light, normal));
}
void PS(in vertexOutput b, out pixelOutput c)
{
c.Color = b.Diffuse;
}
//-----------------------------------
technique simple
{
pass p0
{
VertexShader = compile vs_1_1 VS();
PixelShader = compile ps_1_4 PS();
}
}
diffuse per pixel lighting, everything else is the same as above:
Code:
//------------------------------------
float4x4 matWVP : WorldViewProjection;
float4x4 matW : World;
float4 lightPos; // you need to set this to a proper light position
//------------------------------------
struct vertexInput {
float4 Position : POSITION0;
float3 Normal : NORMAL0;
};
struct vertexOutput {
float4 Position : POSITION0;
float3 lightVec : TEXCOORD0;
float3 normalVec : TEXCOORD1;
};
struct pixelOutput {
float4 Color : COLOR0;
};
//------------------------------------
void VS(in vertexInput a, out vertexOutput b)
{
// transform position
b.Position = mul(float4(a.Position.xyz , 1.0) , matWVP);
//getting the position of the vertex in the world
float3 posWorld = mul(a.Position, matW).xyz;
// get normal
b.normalVec = mul(a.Normal, matW).xyz;
// get light normal vector
b.lightVec = normalize(lightPos - posWorld);
}
void PS(in vertexOutput b, out pixelOutput c)
{
float intensity = saturate(dot(b.normalVec, b.lightVec));
c.Color = float4(1, 0.9, 0.6, 1) * intensity;
}
//-----------------------------------
technique simple
{
pass p0
{
VertexShader = compile vs_1_1 VS();
PixelShader = compile ps_1_4 PS();
}
}
and the result:


per-pixel-lighting definately creates smoother shading.
joey.