Home Articles Tutorials Resources About

Tutorial 8 - Simple Shadows


By Pieter Germishuys, January 4 2006

For ultimate realism what better way to produce this then actually implementing shadows. This is definitely not the best method but it's a good hack.
What is a Shadow?
A shadow is a dark shape, e.g. on the ground or a wall, caused by an object (or person, etc.) blocking light.

What do we need to know?
Now that we know what a shadow is, how do we produce one?
Since this is a beginner tutorial I am not going to be using shaders nor am I going to use any difficult tricks.
The theory is simple for this exercise.
1) Create a plane that will define the normal of the object
2) Use the Matrix.Shadow method to create a transformation that will flatten the geometry to a plane.
3) Render your Solid object with a the world matrix
4) Render your Solid object again, this time with the shadow Matrix.

The code
Declaring your objects.

private Box box;
private Floor floor;
private Plane shadowPlane;
private Matrix matShadow;
private Matrix matFloor;
private Matrix matBox;
private Material mtrlBox;
private Material mtrlFloor;
private Material mtrlShadow;


The constructor that will initialize our objects and load our mesh.
public Core()
{
    window = new Window();

    //setup our point light
    window.D3DDevice.Lights[0].Type = LightType.Point;
    window.D3DDevice.Lights[0].Position = new Vector3(20.0f, 30.0f, 0.0f);
    window.D3DDevice.Lights[0].Diffuse = Color.White;
    window.D3DDevice.Lights[0].Attenuation0 = 1.0f;
    window.D3DDevice.Lights[0].Range = 50.0f;
    window.D3DDevice.Lights[0].Enabled = true;

    //enable lighting in our engine
    window.D3DDevice.RenderState.Lighting = true;

    //I created a simple floor and box class that will just instantiate our box and floor
    box = new Box(window.D3DDevice);
    floor = new Floor(window.D3DDevice);

    //The floor material
    mtrlFloor = new Material();
    mtrlFloor.Diffuse = Color.White;
    //The box material
    mtrlBox = new Material();
    mtrlBox.Diffuse = Color.White;
    mtrlBox.Emissive = Color.FromArgb(100, 100, 100);
    //The shadow material
    mtrlShadow = new Material();
    mtrlShadow.Diffuse = Color.FromArgb(30, 30, 30);

    //The plane of the shadow
    //The first 3 parameters are the normals of the plane (nx, ny, nz)
    //the last parameter is the distance

    shadowPlane = new Plane(0.0f, 1.0f, 0.0f, 0.0f);
    matShadow.Shadow(new Vector4(window.D3DDevice.Lights[0].Position.X,
    window.D3DDevice.Lights[0].Position.Y,
    window.D3DDevice.Lights[0].Position.Z,
    0.0f),
    shadowPlane); //the plane of the shadow
}

Update your scene.
private void Update()
{
    //set the box at 10 units above the floor
    matBox = Matrix.Translation(0.0f, 10.0f, 0.0f);
    //set the floor 1.0 units lower so that we can see the shadow
    matFloor = Matrix.Translation(0.0f, -1.0f, 0.0f);
}
Render your scene.
private void Render()
{
    window.BeginRender(Color.Black);
    //Render the floor with the position and material
    window.D3DDevice.Material = mtrlFloor;
    window.D3DDevice.Transform.World = matFloor;
    floor.Render(window.D3DDevice);
    //Render the box with the position and material
    window.D3DDevice.Material = mtrlBox;
    window.D3DDevice.Transform.World = matBox;
    box.Render(window.D3DDevice);
    //Render the shadow box with the position and material
    window.D3DDevice.Material = mtrlShadow;
    window.D3DDevice.Transform.World = matShadow;
    box.Render(window.D3DDevice);
    window.FinishRender();
}



Files for this tutorial

Filename Size
  tut8.rar 273.0 KB
 
MDX info is an initiative by vector4. All content is copyright © 2005-2008 by its respective authors | About MDX info | Terms of Use | RSS feed
Coming soon!