Game programming
Direct3D uses lights to approximate real-world lighting. It does this using a system of calculation that is much less time-consuming, and that can therefore be run in real-time. This system is built up of a set of types of lighting and types of light sources.
Types of Lighting
There are three types of lighting that can be used to approximate real-world light. They are called diffuse lighting, ambient lighting and specular lighting.
Diffuse lighting is light that more or less falls directly onto a surface. If you hold up your hand in a room with one light source, you will see that one side of your hand is lit, while the other side is not. That side of your hand is lit with what is called diffuse light. This can be used to create a sunlight effect, where the sunlight comes from one broad direction.
Ambient lighting consists of light that is everywhere. Generally speaking, it is quite dark (though not totally dark). When you hold your hand up in that room with one light source, the dark side of your hand is ambient lit, and the light that is reflected there is referred to as ambient light.
Specular lighting is often referred to as specular highlight or, even more commonly, reflection. (Wow, big word, huh?) When light reflects off an object, usually most of the light will go in one specific direction (rather than scatter everywhere evenly). The result is that each object has a little shine on it that comes from the light source.
Solution:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
namespace GP_P2
{
public partial class Form1 : Form
{
private Microsoft.DirectX.Direct3D.Device device;
private CustomVertex.PositionNormalColored[] vertex = new CustomVertex.PositionNormalColored[3];
public Form1()
{
InitializeComponent();
InitDevice();
}
public void InitDevice()
{
PresentParameters pp = new PresentParameters();
pp.Windowed = true;
pp.SwapEffect = SwapEffect.Discard;
device = new Device(0, DeviceType.Hardware, this, CreateFlags.HardwareVertexProcessing, pp);
device.Transform.Projection = Matrix.PerspectiveFovLH(3.14f / 4, device.Viewport.Width / device.Viewport.Height, 1f, 1000f);
device.Transform.View = Matrix.LookAtLH(new Vector3(0, 0, 10), new Vector3(), new Vector3(0, 1, 0));
device.RenderState.Lighting = false;
vertex[0] = new CustomVertex.PositionNormalColored(new Vector3(0, 1, 1), new Vector3(1, 0, 1), Color.Red.ToArgb());
vertex[1] = new CustomVertex.PositionNormalColored(new Vector3(-1, -1, 1), new Vector3(1, 0, 1), Color.Red.ToArgb());
vertex[2] = new CustomVertex.PositionNormalColored(new Vector3(1, -1, 1), new Vector3(-1, 0, 1), Color.Red.ToArgb());
device.RenderState.Lighting = true;
device.Lights[0].Type = LightType.Directional;
device.Lights[0].Diffuse = Color.Plum;
device.Lights[0].Direction = new Vector3(0.8f, 0, -1);
device.Lights[0].Enabled = true;
}
public void Render()
{
device.Clear(ClearFlags.Target, Color.CornflowerBlue, 1, 0);
device.BeginScene();
device.VertexFormat = CustomVertex.PositionNormalColored.Format;
device.DrawUserPrimitives(PrimitiveType.TriangleList, vertex.Length / 3, vertex);
device.EndScene();
device.Present();
T.Y.B.Sc.(C.S.) Game Programming
11
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Render();
}
}
}
Output:
Types of Lighting
There are three types of lighting that can be used to approximate real-world light. They are called diffuse lighting, ambient lighting and specular lighting.
Diffuse lighting is light that more or less falls directly onto a surface. If you hold up your hand in a room with one light source, you will see that one side of your hand is lit, while the other side is not. That side of your hand is lit with what is called diffuse light. This can be used to create a sunlight effect, where the sunlight comes from one broad direction.
Ambient lighting consists of light that is everywhere. Generally speaking, it is quite dark (though not totally dark). When you hold your hand up in that room with one light source, the dark side of your hand is ambient lit, and the light that is reflected there is referred to as ambient light.
Specular lighting is often referred to as specular highlight or, even more commonly, reflection. (Wow, big word, huh?) When light reflects off an object, usually most of the light will go in one specific direction (rather than scatter everywhere evenly). The result is that each object has a little shine on it that comes from the light source.
Solution:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
namespace GP_P2
{
public partial class Form1 : Form
{
private Microsoft.DirectX.Direct3D.Device device;
private CustomVertex.PositionNormalColored[] vertex = new CustomVertex.PositionNormalColored[3];
public Form1()
{
InitializeComponent();
InitDevice();
}
public void InitDevice()
{
PresentParameters pp = new PresentParameters();
pp.Windowed = true;
pp.SwapEffect = SwapEffect.Discard;
device = new Device(0, DeviceType.Hardware, this, CreateFlags.HardwareVertexProcessing, pp);
device.Transform.Projection = Matrix.PerspectiveFovLH(3.14f / 4, device.Viewport.Width / device.Viewport.Height, 1f, 1000f);
device.Transform.View = Matrix.LookAtLH(new Vector3(0, 0, 10), new Vector3(), new Vector3(0, 1, 0));
device.RenderState.Lighting = false;
vertex[0] = new CustomVertex.PositionNormalColored(new Vector3(0, 1, 1), new Vector3(1, 0, 1), Color.Red.ToArgb());
vertex[1] = new CustomVertex.PositionNormalColored(new Vector3(-1, -1, 1), new Vector3(1, 0, 1), Color.Red.ToArgb());
vertex[2] = new CustomVertex.PositionNormalColored(new Vector3(1, -1, 1), new Vector3(-1, 0, 1), Color.Red.ToArgb());
device.RenderState.Lighting = true;
device.Lights[0].Type = LightType.Directional;
device.Lights[0].Diffuse = Color.Plum;
device.Lights[0].Direction = new Vector3(0.8f, 0, -1);
device.Lights[0].Enabled = true;
}
public void Render()
{
device.Clear(ClearFlags.Target, Color.CornflowerBlue, 1, 0);
device.BeginScene();
device.VertexFormat = CustomVertex.PositionNormalColored.Format;
device.DrawUserPrimitives(PrimitiveType.TriangleList, vertex.Length / 3, vertex);
device.EndScene();
device.Present();
T.Y.B.Sc.(C.S.) Game Programming
11
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Render();
}
}
}
Output:

No comments
Post a Comment