|
|
|
|
Transforming vertices to texture space using Projective Texturing |
Now we have a Shadow Map, it’s time to draw the scene as seen by our camera, and check whether the pixels should be shadowed or lit. This can be decided by sampling our Shadow Map at the correct location. As goal for this chapter, we’ll be drawing the scene from our camera’s point of view, with the color of each pixel being the color stored in the Shadow Map for that pixel.
First of all, this means we’ll be drawing our scene using 2 different techniques: the first one will generate our Shadow Map which we store in a texture, the second one will draw the scene to the screen again. You can already add this second technique to your HLSL file:
technique ShadowedScene { pass Pass0 { VertexShader = compile vs_2_0 ShadowedSceneVertexShader(); PixelShader = compile ps_2_0 ShadowedScenePixelShader(); } }
Because we need 2 brand new shader methods, it’s good practice to define new structs that hold the output data of both methods:
struct SSceneVertexToPixel { float4 Position : POSITION; float4 ShadowMapSamplingPos : TEXCOORD0; };
struct SScenePixelToFrame { float4 Color : COLOR0; };
You can see the vertex shader will pass the (required) 2D position to the pixel shader, as well as the coordinate that will indicate which position of the Shadow Map corresponds to the current pixel that is being drawn. The pixel shader will again only output the pixel’s color.
Our pixel shader will be using the Shadow Map, which can be treated as a standard texture:
Texture xShadowMap;
sampler ShadowMapSampler = sampler_state { texture = <xShadowMap>
; magfilter = LINEAR; minfilter = LINEAR; mipfilter=LINEAR; AddressU = clamp; AddressV = clamp;};
Let’s start by coding the vertex shader. A quick reminder: this second technique has to draw the scene, as seen by the camera. When this second technique is being called, the first technique has already finished drawing the shadow map. Important: this shadow map was drawn as seen by the headlights, at the bottom left of our scene. So now, for every pixel seen by the camera, we have to find which part of shadow map corresponds to this pixel. In other words, we need the 2D screen coordinates of the shadow map that correspond with every pixel being drawn. Because this can be difficult to grasp, I’ve added a small picture below:

I’ve indicated the points of interest with a red dot. Say we want to find the color of the dotted pixel of the wall. What we want to know is the 2D coordinate of the corresponding pixel in our Shadow Map.
The first step would be to project the 3D coordinates of the red-dotted pixel of the wall to 2D camera space of the light. This can be done by our vertex shader and will be stored in the ShadowMapSamplingPos variable:
SSceneVertexToPixel ShadowedSceneVertexShader( float4 inPos : POSITION) { SSceneVertexToPixel Output = (SSceneVertexToPixel)0; Output.Position = mul(inPos, xWorldViewProjection); Output.ShadowMapSamplingPos = mul(inPos, xLightWorldViewProjection); return Output; }
Now our pixel shader has access to the homogeneous screen coordinates of our light. Quite a difficult line. The ‘homogeneous’ part means we need to divide the X,Y and Z components by the W component. If you’re interested why, you can check out my articles on matrices you can find in the ‘Extra Reading’ section at the left of the site. For now, remember we need to divide by the W component. Because screen coordinates are 2D, we’re not using the Z component. So the difficult line reduces to: ‘divide X and Y by W’.
When we do this, we would get a 2D coordinate, with the X and Y component between the [-1, 1] region. The coord (-1,-1) would represent the upper left corner, whereas coord (0,1) would represent the pixel in the middle of the right.
Remember texture coordinates have to be between the [0, 1] region, so we need a simple remap: point (-1,-1) has to become (0,0) and point (1,1) has to stay (1,1). This is what we need:

Most people use the ‘Projective matrix’ to do this, sometimes because they don’t know what it’s doing :) For a change, we’re simply going to code this formula in our pixel shader:
SScenePixelToFrame ShadowedScenePixelShader(SSceneVertexToPixel PSIn) { SScenePixelToFrame Output = (SScenePixelToFrame)0;
float2 ProjectedTexCoords; ProjectedTexCoords[0] = PSIn.ShadowMapSamplingPos.x/PSIn.ShadowMapSamplingPos.w/2.0f +0.5f; ProjectedTexCoords[1] = -PSIn.ShadowMapSamplingPos.y/PSIn.ShadowMapSamplingPos.w/2.0f +0.5f; Output.Color = tex2D(ShadowMapSampler, ProjectedTexCoords);
return Output; }
You see we receive the homogeneous coordinates as input, and derive the 2D texture coordinates from them. With these coordinates, we sample the color of our Shadow map at that position and set this as output color for the pixel. Because this all is not that trivial, you can find some extra discussing with sample code in the forum of this chapter.
That’s it for the HLSL code, now we need to add some very easy DirectX code. First add this method:
private void RenderShadowedScene() { effect.Technique = "ShadowedScene"; effect.SetValue("xShadowMap", RenderTexture); int numpasses = effect.Begin(0); for (int i = 0; i < numpasses; i++) { effect.BeginPass(i); DrawScene(); effect.EndPass(); } effect.End(); }
Which loads our second technique, passes in the Shadow map and draws the scene using this technique. Now we only need to call this method from within our OnPaint method, somewhere between the calls to device.BeginScene and device.EndScene:
RenderShadowedScene();
Now when you run the code, you should see something like the image below. In the top-left corner, we still have our shadow map, displayed as a Sprite. The other part of the window is used the normal way, except for the colors: these are taken from the Shadow map. When you look at a lamppost, you see it has the same color as in the Shadow map. But when you look at the part of the wall behind the lamppost, you can see it has the same color as the lamppost! Because of this, next chapter we’ll be able to detect the shadowed areas.

Click here to go to the forum on this chapter!
Or click on one of the topics on this chapter to go there: ProjectedTexCoords? first of all thanks for your tutorials i learned a...No Light? I'm finally getting through the C# with C++. But ...lost code Hi Riemer,
sorry, but the dreaded LT and GT cha...
This Projective texturing technique is the last mathematical part of the Series, so there’s no reason to start panicking. But with the explanation found in this chapter, you should at least know why/what you are doing, and with my articles on matrices in the Extra Reading section you should even be able to understand the maths behind it. Remember you can always post questions in the forum for further explanation.
The HLSL code, containing our 2 techniques:
struct SMapVertexToPixel { float4 Position : POSITION; float3 Position2D : TEXCOORD0; };
struct SMapPixelToFrame { float4 Color : COLOR0; };
struct SSceneVertexToPixel { float4 Position : POSITION; float4 ShadowMapSamplingPos : TEXCOORD0; };
struct SScenePixelToFrame { float4 Color : COLOR0; };
//------- Constants --------
float4x4 xWorldViewProjection; float4x4 xLightWorldViewProjection; float4x4 xRot; float4 xLightPos; float xLightPower; float xMaxDepth;
//------- Texture Samplers --------
Texture xColoredTexture;
sampler ColoredTextureSampler = sampler_state { texture = <xColoredTexture>
; magfilter = LINEAR; minfilter = LINEAR; mipfilter=LINEAR; AddressU = mirror; AddressV = mirror;};
Texture xShadowMap;
sampler ShadowMapSampler = sampler_state { texture = <xShadowMap>
; magfilter = LINEAR; minfilter = LINEAR; mipfilter=LINEAR; AddressU = clamp; AddressV = clamp;};
//------- Vertex Shaders --------
SMapVertexToPixel ShadowMapVertexShader( float4 inPos : POSITION) { SMapVertexToPixel Output = (SMapVertexToPixel)0; Output.Position = mul(inPos, xLightWorldViewProjection); Output.Position2D = Output.Position; return Output; }
SSceneVertexToPixel ShadowedSceneVertexShader( float4 inPos : POSITION) { SSceneVertexToPixel Output = (SSceneVertexToPixel)0; Output.Position = mul(inPos, xWorldViewProjection); Output.ShadowMapSamplingPos = mul(inPos, xLightWorldViewProjection); return Output; }
//------- Pixel Shaders --------
SMapPixelToFrame ShadowMapPixelShader(SMapVertexToPixel PSIn) { SMapPixelToFrame Output = (SMapPixelToFrame)0;
Output.Color = PSIn.Position2D.z/xMaxDepth;
return Output; }
SScenePixelToFrame ShadowedScenePixelShader(SSceneVertexToPixel PSIn) { SScenePixelToFrame Output = (SScenePixelToFrame)0; float2 ProjectedTexCoords; ProjectedTexCoords[0] = PSIn.ShadowMapSamplingPos.x/PSIn.ShadowMapSamplingPos.w/2.0f +0.5f; ProjectedTexCoords[1] = -PSIn.ShadowMapSamplingPos.y/PSIn.ShadowMapSamplingPos.w/2.0f +0.5f; Output.Color = tex2D(ShadowMapSampler, ProjectedTexCoords); return Output; }
//------- Techniques --------
technique ShadowMap { pass Pass0 { VertexShader = compile vs_2_0 ShadowMapVertexShader(); PixelShader = compile ps_2_0 ShadowMapPixelShader(); } }
technique ShadowedScene { pass Pass0 { VertexShader = compile vs_2_0 ShadowedSceneVertexShader(); PixelShader = compile ps_2_0 ShadowedScenePixelShader(); } }
Our DirectX part hasn’t changed a lot, but here it is:
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using Microsoft.DirectX; using Microsoft.DirectX.Direct3D; using D3D = Microsoft.DirectX.Direct3D; namespace DirectX_Tutorial { struct myownvertexformat { public Vector3 Pos; public Vector3 Normal; public Vector2 TexCoord; public myownvertexformat(Vector3 _Pos, Vector3 _Normal, float texx, float texy) { Pos = _Pos; Normal = _Normal; TexCoord.X = texx; TexCoord.Y = texy; } } public class WinForm : System.Windows.Forms.Form { private System.ComponentModel.Container components = null; private D3D.Device device; private VertexBuffer vb; private Vector3 CameraPos; private VertexDeclaration vd; private Effect effect; private Texture StreetTexture; private Mesh Lamppost; private Material[] LamppostMaterials; private Texture[] LamppostTextures; private Mesh Car; private Material[] CarMaterials; private Texture[] CarTextures; private Matrix matView; private Matrix matProjection; private Matrix LightViewProjection; private int LastTickCount = 1; private int Frames = 0; private float LastFrameRate = 0; private D3D.Font text; private RenderToSurface RtsHelper; private Texture RenderTexture; private Surface RenderSurface; private int RenderSurfaceSize = 512; public WinForm() { InitializeComponent(); this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true); } public void InitializeDevice() { PresentParameters presentParams = new PresentParameters(); presentParams.Windowed = true; presentParams.SwapEffect = SwapEffect.Discard; presentParams.AutoDepthStencilFormat = DepthFormat.D16; presentParams.EnableAutoDepthStencil = true; Caps DevCaps = D3D.Manager.GetDeviceCaps(0, D3D.DeviceType.Hardware); D3D.DeviceType DevType = D3D.DeviceType.Reference; CreateFlags DevFlags = CreateFlags.SoftwareVertexProcessing; if ((DevCaps.VertexShaderVersion >= new Version(2, 0)) && (DevCaps.PixelShaderVersion >= new Version(2, 0))) { DevType = D3D.DeviceType.Hardware; if (DevCaps.DeviceCaps.SupportsHardwareTransformAndLight) { DevFlags = CreateFlags.HardwareVertexProcessing; if (DevCaps.DeviceCaps.SupportsPureDevice) { DevFlags |= CreateFlags.PureDevice; } } } device = new D3D.Device(0, DevType, this, DevFlags, presentParams); device.DeviceReset += new EventHandler(this.HandleDeviceReset); } private void HandleDeviceReset(object sender, EventArgs e) { FillResources(); } private void AllocateResources() { vb = new VertexBuffer(typeof(myownvertexformat), 18, device, Usage.WriteOnly, VertexFormats.Position | VertexFormats.Normal | VertexFormats.Texture0, Pool.Managed); InitializeFont(); effect = D3D.Effect.FromFile(device, @"../../OurHLSLFile.fx", null, null, ShaderFlags.None, null); } private void FillResources() { myownvertexformat[] vertices = new myownvertexformat[18]; vertices[0] = new myownvertexformat(new Vector3(20, -10, 0), new Vector3(0, 0, 1), -0.25f, 25.0f); vertices[1] = new myownvertexformat(new Vector3(20, 100, 0), new Vector3(0, 0, 1), -0.25f, 0.0f); vertices[2] = new myownvertexformat(new Vector3(-2, -10, 0), new Vector3(0, 0, 1), 0.25f, 25.0f); vertices[3] = new myownvertexformat(new Vector3(-2, 100, 0), new Vector3(0, 0, 1), 0.25f, 0.0f); vertices[4] = new myownvertexformat(new Vector3(-2, -10, 0), new Vector3(1, 0, 0), 0.25f, 25.0f); vertices[5] = new myownvertexformat(new Vector3(-2, 100, 0), new Vector3(1, 0, 0), 0.25f, 0.0f); vertices[6] = new myownvertexformat(new Vector3(-2, -10, 1), new Vector3(1, 0, 0), 0.375f, 25.0f); vertices[7] = new myownvertexformat(new Vector3(-2, 100, 1), new Vector3(1, 0, 0), 0.375f, 0.0f); vertices[8] = new myownvertexformat(new Vector3(-2, -10, 1), new Vector3(0, 0, 1), 0.375f, 25.0f); vertices[9] = new myownvertexformat(new Vector3(-2, 100, 1), new Vector3(0, 0, 1), 0.375f, 0.0f); vertices[10] = new myownvertexformat(new Vector3(-3, -10, 1), new Vector3(0, 0, 1), 0.5f, 25.0f); vertices[11] = new myownvertexformat(new Vector3(-3, 100, 1), new Vector3(0, 0, 1), 0.5f, 0.0f); vertices[12] = new myownvertexformat(new Vector3(-13, -10, 1), new Vector3(0, 0, 1), 0.75f, 25.0f); vertices[13] = new myownvertexformat(new Vector3(-13, 100, 1), new Vector3(0, 0, 1), 0.75f, 0.0f); vertices[14] = new myownvertexformat(new Vector3(-13, -10, 1), new Vector3(1, 0, 0), 0.75f, 25.0f); vertices[15] = new myownvertexformat(new Vector3(-13, 100, 1), new Vector3(1, 0, 0), 0.75f, 0.0f); vertices[16] = new myownvertexformat(new Vector3(-13, -10, 21), new Vector3(1, 0, 0), 1.25f, 25.0f); vertices[17] = new myownvertexformat(new Vector3(-13, 100, 21), new Vector3(1, 0, 0), 1.25f, 0.0f); vb.SetData(vertices, 0, LockFlags.None); SetUpCamera(); VertexElement[] velements = new VertexElement[] { new VertexElement(0, 0, DeclarationType.Float3, DeclarationMethod.Default, DeclarationUsage.Position, 0), new VertexElement(0, 12, DeclarationType.Float3, DeclarationMethod.Default, DeclarationUsage.Normal, 0), new VertexElement(0, 24, DeclarationType.Float2, DeclarationMethod.Default, DeclarationUsage.TextureCoordinate, 0), VertexElement.VertexDeclarationEnd }; vd = new VertexDeclaration(device, velements); StreetTexture = TextureLoader.FromFile(device, "streettexture.jpg"); LoadMesh("lamppost.x", ref Lamppost, ref LamppostMaterials, ref LamppostTextures); LoadMesh("car.x", ref Car, ref CarMaterials, ref CarTextures); RtsHelper = new RenderToSurface(device, RenderSurfaceSize, RenderSurfaceSize, Format.X8R8G8B8, true, DepthFormat.D16); RenderTexture = new Texture(device, RenderSurfaceSize, RenderSurfaceSize, 1, Usage.RenderTarget, Format.X8R8G8B8, Pool.Default); RenderSurface = RenderTexture.GetSurfaceLevel(0); } private void InitializeFont() { System.Drawing.Font systemfont = new System.Drawing.Font("Arial", 12f, FontStyle.Regular); text = new D3D.Font(device, systemfont); } private void DrawMesh(Mesh mesh, Material[] meshmaterials, Texture[] meshtextures) { for (int i = 0; i < meshmaterials.Length; i++) { if (meshtextures.Length > 3) effect.SetValue("xColoredTexture", meshtextures[i]); effect.CommitChanges(); mesh.DrawSubset(i); } } protected override void OnPaint(System.Windows.Forms.PaintEventArgs e) { UpdateLightData(); GenerateShadowMap(); device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0); device.BeginScene();
RenderShadowedScene();
using (Sprite spriteobject = new Sprite(device)) { spriteobject.Begin(SpriteFlags.DoNotSaveState); spriteobject.Transform = Matrix.Scaling(0.25f, 0.25f, 0.25f); spriteobject.Draw(RenderTexture, new Rectangle(0, 0, RenderSurfaceSize, RenderSurfaceSize), new Vector3(0, 0, 0), new Vector3(0, 0, 0), Color.White); spriteobject.End(); } UpdateFramerate(); device.EndScene(); device.Present(); this.Invalidate(); } private void GenerateShadowMap() { RtsHelper.BeginScene(RenderSurface); device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0); effect.Technique = "ShadowMap"; effect.SetValue("xMaxDepth", 60); int numpasses = effect.Begin(0); for (int i = 0; i < numpasses; i++) { effect.BeginPass(i); DrawScene(); effect.EndPass(); } effect.End(); RtsHelper.EndScene(Filter.None); }
private void RenderShadowedScene() { effect.Technique = "ShadowedScene"; effect.SetValue("xShadowMap", RenderTexture); int numpasses = effect.Begin(0); for (int i = 0; i < numpasses; i++) { effect.BeginPass(i); DrawScene(); effect.EndPass(); } effect.End(); }
private void UpdateLightData() { Vector3 LightPos = new Vector3(18, 2, 5); float LightPower = 0.7f; LightViewProjection = Matrix.LookAtLH(LightPos, new Vector3(2, 10, -3), new Vector3(0, 0, 1)) * Matrix.PerspectiveFovLH((float)Math.PI / 2, this.Width / this.Height, 1f, 100f); effect.SetValue("xLightPos", new Vector4(LightPos.X, LightPos.Y, LightPos.Z, 1)); effect.SetValue("xLightPower", LightPower); } private void DrawScene() { effect.SetValue("xWorldViewProjection", Matrix.Identity * matView * matProjection); effect.SetValue("xLightWorldViewProjection", Matrix.Identity * LightViewProjection); effect.SetValue("xRot", Matrix.Identity); effect.CommitChanges(); device.SetStreamSource(0, vb, 0); device.VertexDeclaration = vd; device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 16); Matrix LamppostWorld = Matrix.Scaling(0.05f, 0.05f, 0.05f) * Matrix.RotationX((float)Math.PI / 2) * Matrix.Translation(-4.0f, 5, 1); effect.SetValue("xWorldViewProjection", LamppostWorld * matView * matProjection); effect.SetValue("xLightWorldViewProjection", LamppostWorld * LightViewProjection); effect.SetValue("xRot", Matrix.RotationX((float)Math.PI / 2)); DrawMesh(Lamppost, LamppostMaterials, LamppostTextures); LamppostWorld = Matrix.Scaling(0.05f, 0.05f, 0.05f) * Matrix.RotationX((float)Math.PI / 2) * Matrix.Translation(-4.0f, 35, 1); effect.SetValue("xWorldViewProjection", LamppostWorld * matView * matProjection); effect.SetValue("xLightWorldViewProjection", LamppostWorld * LightViewProjection); effect.SetValue("xRot", Matrix.RotationX((float)Math.PI / 2)); DrawMesh(Lamppost, LamppostMaterials, LamppostTextures); Matrix CarWorld = Matrix.Scaling(4f, 4f, 4f) * Matrix.RotationYawPitchRoll((float)Math.PI / 2, (float)Math.PI / 2, (float)Math.PI / 2) * Matrix.Translation(3, 15, 0f); effect.SetValue("xWorldViewProjection", CarWorld * matView * matProjection); effect.SetValue("xLightWorldViewProjection", CarWorld * LightViewProjection); effect.SetValue("xRot", Matrix.RotationYawPitchRoll((float)Math.PI / 2, (float)Math.PI / 2, (float)Math.PI / 2) * Matrix.Translation(3, 15, 0f)); DrawMesh(Car, CarMaterials, CarTextures); CarWorld = Matrix.Scaling(4f, 4f, 4f) * Matrix.RotationYawPitchRoll((float)Math.PI / 2, (float)Math.PI / 8, (float)Math.PI / 2) * Matrix.Translation(28, -1.9f, 0f); effect.SetValue("xWorldViewProjection", CarWorld * matView * matProjection); effect.SetValue("xLightWorldViewProjection", CarWorld * LightViewProjection); effect.SetValue("xRot", Matrix.RotationYawPitchRoll((float)Math.PI / 2, (float)Math.PI / 2, (float)Math.PI / 2) * Matrix.Translation(3, 15, 0f)); DrawMesh(Car, CarMaterials, CarTextures); } private void UpdateFramerate() { Frames++; if (Math.Abs(Environment.TickCount - LastTickCount) > 1000) { LastFrameRate = (float)Frames * 1000 / Math.Abs(Environment.TickCount - LastTickCount); LastTickCount = Environment.TickCount; Frames = 0; } text.DrawText(null, string.Format("Framerate : {0:0.00} fps", LastFrameRate), new Point(10, 430), Color.Red); } private void SetUpCamera() { CameraPos = new Vector3(25, -18, 13); matProjection = Matrix.PerspectiveFovLH((float)Math.PI / 4, this.Width / this.Height, 0.3f, 200f); matView = Matrix.LookAtLH(CameraPos, new Vector3(0, 12, 2), new Vector3(0, 0, 1)); } private void LoadMesh(string filename, ref Mesh mesh, ref Material[] meshmaterials, ref Texture[] meshtextures) { ExtendedMaterial[] materialarray; GraphicsStream adj = null; mesh = Mesh.FromFile(filename, MeshFlags.Managed, device, out adj, out materialarray); if ((materialarray != null) && (materialarray.Length > 0)) { meshmaterials = new Material[materialarray.Length]; meshtextures = new Texture[materialarray.Length]; for (int i = 0; i < materialarray.Length; i++) { meshmaterials[i] = materialarray[i].Material3D; meshmaterials[i].Ambient = meshmaterials[i].Diffuse; if ((materialarray[i].TextureFilename != null) && (materialarray[i].TextureFilename != string.Empty)) { meshtextures[i] = TextureLoader.FromFile(device, materialarray[i].TextureFilename); } } } mesh = mesh.Clone(mesh.Options.Value, CustomVertex.PositionNormalTextured.Format, device); mesh.ComputeNormals(); } protected override void Dispose(bool disposing) { if (disposing) { if (components != null) { components.Dispose(); } } base.Dispose(disposing); } private void InitializeComponent() { this.components = new System.ComponentModel.Container(); this.Size = new System.Drawing.Size(500, 500); this.Text = "Riemer's DirectX & HLSL Tutorial using C# -- Season 3"; } static void Main() { using (WinForm our_directx_form = new WinForm()) { our_directx_form.InitializeDevice(); our_directx_form.AllocateResources(); our_directx_form.FillResources(); Application.Run(our_directx_form); } } } }
- Website design & XNA + DirectX code : Riemer Grootjans - ©2003 - 2011 Riemer Grootjans
|
|
|
|
|