BrownBot Logo BrownBot Head

1a in XNA with Anti-aliasing and Shaders

9:39 am Filed under: BrownBot

Brownbot 1a’s back again, this time he’s rendered in XNA with a nice custom shader featuring:

  • Per pixel lighting
  • Separate Ambient, Diffuse and Specular values

Plus 2x anti aliasing, now it’s time to add a texture and move him into some interesting poses.

Here are some code snippets for the XNA guys out there.

To turn on full scene anti-aliasing in XNA (otherwise called multi sampling) you simply add the following.

graphics = new GraphicsDeviceManager(this);
graphics.PreferMultiSampling = true;

The following is my fx file for super simple per pixel rendering (note: the camera is the light source).

string description = "Basic Vertex Lighting with a Texture";

//------------------------------------
float4x4 worldViewProj : WorldViewProjection;
float4x4 world   : World;

texture diffuseTexture : Diffuse;

//------------------------------------
struct vertexInput {
    float4 Position				: POSITION;
    float3 Normal				: NORMAL;
    float4 TexCoord				: TEXCOORD0;
};

struct vertexOutput {
    float4 Position     : POSITION;
    float2 TexCoords    : TEXCOORD0;
    float3 Normal        : TEXCOORD1;
    float3 Position3D    : TEXCOORD2;
};

//------------------------------------
vertexOutput VS_TransformAndTexture(vertexInput IN)
{
    vertexOutput Output;
    Output.Position = mul(IN.Position , worldViewProj);
    Output.Normal = normalize(mul(IN.Normal, (float3x3)world));
	Output.Position3D = mul(IN.Position, world);
    Output.TexCoords = IN.TexCoord;
    return Output;
}

//------------------------------------
sampler TextureSampler = sampler_state
{
    texture = ;
    AddressU  = CLAMP;
    AddressV  = CLAMP;
    AddressW  = CLAMP;
    MIPFILTER = LINEAR;
    MINFILTER = LINEAR;
    MAGFILTER = LINEAR;
};

float4 Ambient = float4(0.12, 0.12, 0.10, 1.0);
float4 Diffuse = float4(0.80, 0.80, 0.80, 1.0);
float4 Specular = float4(0.25, 0.20, 0.20, 1.0);
float SpecularPower = 10;

float4 lightPos : LightPosition = {0.0f, 50.0f, 500.0f, 1.0f};

float4 color = float4( 0.36, 0.26, 0.10, 1.00 );

//-----------------------------------
float4 PS_Textured( vertexOutput IN): COLOR
{
    float3 LightDirection = normalize(lightPos - IN.Position3D);
    float NDotL = dot(IN.Normal, LightDirection);
    float4 TotalDiffuse = saturate(Diffuse * NDotL);
    float3 Reflection = normalize((2.0 * IN.Normal * NDotL) - LightDirection);
    float RDotV = max(0.0, dot(Reflection, LightDirection));
    float4 TotalSpecular = saturate(Specular * pow(RDotV, SpecularPower)); 

    return saturate(Ambient + (color * TotalDiffuse)+ TotalSpecular);
}

//-----------------------------------
technique textured
{
    pass p0
    {
		VertexShader = compile vs_1_1 VS_TransformAndTexture();
		PixelShader  = compile ps_2_0 PS_Textured();
    }
}

We’ve got a long weekend starting with Australia day tomorrow, looks like we’ve got a few social events lined up I should have some time to get some more interesting stuff up here.

1 Comment »

  1. [...] BrownBot released the .Fx file for his per-pixel-lighted brown robot. He also updated Eraser to have an online score board (no code). [...]

    Pingback by XNAtutorial.com » Weekly Update - Competition in February — February 1, 2007 @ 1:05 pm

RSS feed for comments on this post. TrackBack URI

Leave a comment

Powered by WordPress