Home Articles Tutorials Resources About

Tutorial 5 - Lights and Materials


By Pieter Germishuys, January 4 2006

Long time no see, How has everyone been doing? Today we are going to look at a very interesting and exciting topic. But you are probably thinking hey, why does this screenshot look exactly like the previous tutorial's shot? It's cause today we are going to enable light and material and this screenshot has light in it.

Let's get into the theory. We are going to introduce 3 new components today. They are Normals, Lights and Materials. Let's see what the definition is for each one of them.
Normal
"A surface normal, or just normal to a flat surface is a three-dimensional vector which is perpendicular to that surface. A normal to a non-flat surface at a point p on the surface is a vector which is perpendicular to the tangent plane to that surface at p."



In this image above I have pointed out what normals are. Normals are unit vectors, A vector that has the length (or magnitude) of one unit eg: (0.0f, 1.0f, 0.0f). This is the normal point upwards for the object in the figure.

Light
"Form of radiant energy that acts upon the retina of the eye, optic nerve, etc., making sight possible. This energy is transmitted at a velocity of about 186,000 miles per second by wavelike or vibrational motion. 2. A form of radiant energy similar to this, but not acting on the normal retina, such as ultraviolet and infrared radiation. Interplay between light rays and the atmosphere cause us to see the sky as blue, and can result in such phenomena as glows, halos, arcs, flashes, and streamers."

There are three types of lights available in Direct3D: point lights, directional lights, and spotlights. We will be working with a directional light as it's simple to setup and for tutorial purposes keeps math out of the equation.

Material
"A material defines the color that is reflected off the surface of a geometric object when a light hits it."


This means that the material is like a costume that the object puts on. This material defines how the light will interact with it. Look at some examples in life. Chrome, Wood, Fur... all these materials handles light in a different way. Some absorb more light than others.

Now that we have some theory covered let's look at some code.

public Core()
{
???? window = new Window(); //instantiate our window
???? //we are going to use the same triangle as before but we going to extend it to a 4 vertex quad using ?????//CustomVertex.PositionNormalTextured that.
?????CustomVertex.PositionNormalTextured[] triangle = new CustomVertex.PositionNormalTextured[4];
?????//bottom left.

?????triangle[0] = new CustomVertex.PositionNormalTextured(-1.0f, 0.0f, 0.0f, 0.0f, 0.0f,-1.0f, 0.0f, 1.0f);
?????//top left

?????triangle[1] = new CustomVertex.PositionNormalTextured(-1.0f, 2.0f, 0.0f, 0.0f, 0.0f,-1.0f, 0.0f, 0.0f);
?????//bottom right

?????triangle[2] = new CustomVertex.PositionNormalTextured( 1.0f, 0.0f, 0.0f, 0.0f, 0.0f,-1.0f, 1.0f, 1.0f);
?????//top right

?????triangle[3] = new CustomVertex.PositionNormalTextured( 1.0f, 2.0f, 0.0f, 0.0f, 0.0f,-1.0f, 1.0f, 0.0f);

?????vb = new VertexBuffer(typeof(CustomVertex.PositionNormalTextured), 4, window.D3DDevice, Usage.WriteOnly, ?????CustomVertex.PositionNormalTextured.Format, Pool.Managed);
???? vb.SetData(triangle, 0, LockFlags.None);

???? texture = TextureLoader.FromFile(window.D3DDevice, "texture.jpg"); //load the texture from a file

???? window.D3DDevice.RenderState.Lighting = true; //enable lighting
?????window.D3DDevice.RenderState.CullMode = Cull.None; //Cull nothing (render everything)
?????window.D3DDevice.Lights[0].Type = LightType.Directional; //set the light type (Directional)
?????window.D3DDevice.Lights[0].Direction = new Vector3(0.0f, 0.0f, 1.0f); //we point the light into the scene
?????window.D3DDevice.Lights[0].Enabled = true; //enable this light
????
????
material = new Material(); //create a new material (very simple)
?????material.Diffuse = Color.White; //set the diffuse color of the material to white.
?????/*What is diffuse light?
??? ?The essential color of an object. This is the color that the object reveals under pure white light. It is perceived as the color of ??? ?the object itself rather than a reflection of the light. You can set the diffuse color separately from the ambient and specular ????? ?????colors. Diffuse color is distinguished from the color of specular reflection (highlights) off an object, which are generally the ?????color of the light source itself. See also Ambient and Specular.*/

}?


We have created all of our resources. The only thing left is to set them and render the quad.

private void Render()
{
????? window.BeginRender(Color.Black);
??????//set the material (everything will be rendered with this material until it is set to null or another material

????? window.D3DDevice.Material = material;
??????//set the texture (everything will be rendered with this textureuntil it is set to null or another texture

????? window.D3DDevice.SetTexture(0, texture);
????? window.D3DDevice.Transform.World = matWorld;
????? window.D3DDevice.SetStreamSource(0, vb, 0);
????? window.D3DDevice.VertexFormat = CustomVertex.PositionNormalTextured.Format;
????? window.D3DDevice.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2);
????? window.FinishRender();
}




Files for this tutorial

Filename Size
? tut5.rar 200.4 KB
?
MDX info is an initiative by vector4. All content is copyright ? 2005-2006 by its respective authors | About MDX info | Terms of Use |
Coming soon!