Here is the shader
#if OPENGL
#define SV_POSITION POSITION
#define VS_SHADERMODEL vs_3_0
#define PS_SHADERMODEL ps_3_0
#else
#define VS_SHADERMODEL vs_4_0_level_9_1
#define PS_SHADERMODEL ps_4_0_level_9_1
#endif
float4x4 World;
float4x4 View;
float4x4 Projection;
float4 AmbientColor = float4 (1, 1, 1, 1);
float AmbientIntensity = 0.1;
float4x4 WorldInverseTranspose;
float3 DiffuseLightDirection = float3(1, 0, 0);
float4 DiffuseColor = float4(1, 0, 1, 1);
float DiffuseIntensity = 1.0;
struct VertexShaderInput
{
float4 Position : POSITION;
float4 Normal : NORMAL;
};
struct VertexShaderOutput
{
float4 Position : POSITION;
float4 Color : COLOR0;
};
/*********************************************************************************************
Function Name : VertexShaderFunction
Description : this handles the vertices
*********************************************************************************************/
VertexShaderOutput VertexShaderFunction (VertexShaderInput input)
{
VertexShaderOutput output;
float4 worldPosition = mul (input.Position, World);
float4 viewPosition = mul (worldPosition, View);
output.Position = mul (viewPosition, Projection);
float4 normal = mul (input.Normal, WorldInverseTranspose);
float lightIntensity = dot (normal, DiffuseLightDirection);
output.Color = saturate (DiffuseColor * DiffuseIntensity * lightIntensity);
return output;
}
/*********************************************************************************************
Function Name : PixelShaderFunction
Description : this handles the pixels
**********************************************************************************************/
float4 PixelShaderFunction (VertexShaderOutput input) : COLOR0
{
return saturate(input.Color + AmbientColor * AmbientIntensity);
}
technique Diffuse
{
pass Pass1
{
VertexShader = compile VS_SHADERMODEL VertexShaderFunction();
PixelShader = compile PS_SHADERMODEL PixelShaderFunction();
}
};
and here is the call code
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;
namespace Whitakers_Shaders
{
/// <summary>
/// This is the main type for your game.
/// </summary>
///
/*
Matrix world = Matrix.CreateTranslation(0, 0, 0);
Matrix view = Matrix.CreateLookAt(new Vector3(0, 0, 10), new Vector3(0, 0, 0), new Vector3(0, 1, 0));
Matrix projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45), 1024f / 768f, 0.1f, 240f);
float angle = 0;
float distance = 85;
*/
public class Game1 : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Effect effect;
Matrix world = Matrix.CreateTranslation(0, 0, 0);
Matrix view = Matrix.CreateLookAt(new Vector3(0, 0, 10), new Vector3(0, 0, 0), new Vector3(0, 1, 0));
Matrix projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45), 1024f / 768f, 0.1f, 240f);
float angle = 0;
float distance = 85;
Model model;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
model = Content.Load<Model>("Models/Helicopter");
effect = Content.Load<Effect>("Effects/Diffuse");
}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed|| Keyboard.GetState().IsKeyDown(Keys.Escape))
this.Exit();
angle += 0.01f;
view = Matrix.CreateLookAt(distance * new Vector3((float)Math.Sin(angle), 0, (float)Math.Cos(angle)), new Vector3(0, 0, 0), new Vector3(0, 1, 0));
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
// DrawModel(model, world, view, projection);
DrawModelWithEffect(model, world, view, projection);
base.Draw(gameTime);
}
#region Regular Draw
private void DrawModel(Model model, Matrix world, Matrix view, Matrix projection)
{
foreach (ModelMesh mesh in model.Meshes)
{
foreach (BasicEffect effect in mesh.Effects)
{
effect.EnableDefaultLighting();
effect.PreferPerPixelLighting = true;
effect.World = world * mesh.ParentBone.Transform;
effect.View = view;
effect.Projection = projection;
}
mesh.Draw();
}
}
#endregion
private void DrawModelWithEffect(Model model, Matrix world, Matrix view, Matrix projection)
{
foreach (ModelMesh mesh in model.Meshes)
{
foreach (ModelMeshPart part in mesh.MeshParts)
{
part.Effect = effect;
effect.Parameters["World"].SetValue(world * mesh.ParentBone.Transform);
effect.Parameters["View"].SetValue(view);
effect.Parameters["Projection"].SetValue(projection);
// setting the ambient color
// effect.Parameters["AmbientColor"].SetValue(Color.Green.ToVector4());
// effect.Parameters["AmbientIntensity"].SetValue(0.5f);
Matrix worldInverseTransposeMatrix = Matrix.Transpose(Matrix.Invert(mesh.ParentBone.Transform * world));
effect.Parameters["WorldInverseTranspose"].SetValue(worldInverseTransposeMatrix);
}
mesh.Draw();
}
}
}
}
hope that helps