CH A PT ER 6 A DD I NG LI GHT T O YOUR SC ENE I N XN A 2.0 in Word

Maker Data Matrix 2d barcode in Word CH A PT ER 6 A DD I NG LI GHT T O YOUR SC ENE I N XN A 2.0

CH A PT ER 6 A DD I NG LI GHT T O YOUR SC ENE I N XN A 2.0
Drawing ECC200 In None
Using Barcode maker for Word Control to generate, create ECC200 image in Microsoft Word applications.
www.OnBarcode.com
Generate Code-39 In None
Using Barcode encoder for Office Word Control to generate, create Code 3/9 image in Word applications.
www.OnBarcode.com
The Code
Draw Code-128 In None
Using Barcode printer for Word Control to generate, create USS Code 128 image in Office Word applications.
www.OnBarcode.com
UPC-A Supplement 5 Creation In None
Using Barcode generation for Office Word Control to generate, create UPC-A image in Word applications.
www.OnBarcode.com
This XNA code renders multiple instances of your object. Because different World matrices are used, the objects will be rendered to different locations in your 3D scene. The final result is the same as in recipe 6-1, only this time obtained through your own HLSL effect: effect.CurrentTechnique = effect.Techniques["VertexShading"]; effect.Parameters["xView"].SetValue(fpsCam.ViewMatrix); effect.Parameters["xProjection"].SetValue(fpsCam.ProjectionMatrix); effect.Parameters["xLightDirection"].SetValue(new Vector3(1, 0, 0)); effect.Parameters["xAmbient"].SetValue(0.0f); for (int i = 0; i < 9; i++) { Matrix world = Matrix.CreateTranslation(4, 0, 0) * Matrix.CreateRotationZ((float)i * MathHelper.PiOver2 / 8.0f); effect.Parameters["xWorld"].SetValue(world); effect.Begin(); foreach (EffectPass pass in effect.CurrentTechnique.Passes) { pass.Begin(); device.VertexDeclaration = myVertexDeclaration; device.DrawUserPrimitives<VertexPositionNormalTexture> (PrimitiveType.TriangleList, vertices, 0, 2); pass.End(); } effect.End(); } This is the full content of your .fx file: float4x4 xWorld; float4x4 xView; float4x4 xProjection; float xAmbient; float3 xLightDirection; struct VSVertexToPixel { float4 Position : POSITION; float LightFactor : TEXCOORD0; }; struct VSPixelToFrame { float4 Color : COLOR0; };
Paint EAN13 In None
Using Barcode creation for Office Word Control to generate, create GTIN - 13 image in Office Word applications.
www.OnBarcode.com
Print PDF 417 In None
Using Barcode generator for Word Control to generate, create PDF417 image in Office Word applications.
www.OnBarcode.com
CHAPTER 6 ADDIN G LIGHT TO Y OUR SCE NE IN XNA 2.0
Generate Barcode In None
Using Barcode creation for Office Word Control to generate, create Barcode image in Office Word applications.
www.OnBarcode.com
Print Code 11 In None
Using Barcode printer for Microsoft Word Control to generate, create USD - 8 image in Office Word applications.
www.OnBarcode.com
//------- Technique: VertexShading -------VSVertexToPixel VSVertexShader(float4 inPos: POSITION0, float3 inNormal: NORMAL0) { VSVertexToPixel Output = (VSVertexToPixel)0; float4x4 preViewProjection = mul(xView, xProjection); float4x4 preWorldViewProjection = mul(xWorld, preViewProjection); Output.Position = mul(inPos, preWorldViewProjection); float3 normal = normalize(inNormal); float3x3 rotMatrix = (float3x3)xWorld; float3 rotNormal = mul(normal, rotMatrix); Output.LightFactor = dot(rotNormal, -xLightDirection); return Output; } VSPixelToFrame VSPixelShader(VSVertexToPixel PSIn) : COLOR0 { VSPixelToFrame Output = (VSPixelToFrame)0; float4 baseColor = float4(0,0,1,1); Output.Color = baseColor*(PSIn.LightFactor+xAmbient); return Output; } technique VertexShading { pass Pass0 { VertexShader = compile vs_2_0 VSVertexShader(); PixelShader = compile ps_2_0 VSPixelShader(); } }
DataMatrix Printer In Objective-C
Using Barcode generation for iPad Control to generate, create Data Matrix image in iPad applications.
www.OnBarcode.com
Create Data Matrix 2d Barcode In None
Using Barcode printer for Microsoft Excel Control to generate, create ECC200 image in Office Excel applications.
www.OnBarcode.com
6-6. Define a Point Light Using HLSL
QR Code Generator In Java
Using Barcode drawer for Eclipse BIRT Control to generate, create QR Code image in Eclipse BIRT applications.
www.OnBarcode.com
Encode Barcode In Objective-C
Using Barcode maker for iPhone Control to generate, create Barcode image in iPhone applications.
www.OnBarcode.com
The Problem
Paint Code 128 In Visual Basic .NET
Using Barcode encoder for .NET framework Control to generate, create Code 128B image in .NET framework applications.
www.OnBarcode.com
Code 39 Scanner In Visual Studio .NET
Using Barcode recognizer for .NET framework Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
Until now, you ve been using directional lights to light your scene, which can be useful to add sunlight to your 3D world. Often, you ll also need light coming from a single point, such as from a flashlight or an explosion. Such a light source is called a point light.
Print EAN / UCC - 13 In Java
Using Barcode printer for Android Control to generate, create GTIN - 13 image in Android applications.
www.OnBarcode.com
Linear Barcode Creator In C#.NET
Using Barcode maker for .NET Control to generate, create 1D Barcode image in .NET applications.
www.OnBarcode.com
CH A PT ER 6 A DD I NG LI GHT T O YOUR SC ENE I N XN A 2.0
Creating Barcode In Objective-C
Using Barcode encoder for iPhone Control to generate, create Barcode image in iPhone applications.
www.OnBarcode.com
Recognizing Barcode In Java
Using Barcode reader for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
The Solution
PDF417 Scanner In Java
Using Barcode decoder for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
Painting GS1 - 12 In None
Using Barcode creation for Office Excel Control to generate, create UPC A image in Microsoft Excel applications.
www.OnBarcode.com
Pass the 3D location of the point light from your XNA project to your XNA effect. For each vertex, you can calculate the direction from the light source toward the vertex and use this light direction. Once you know the light direction, you can continue as before.
How It Works
In your .fx file, replace the xLightDirection parameter with this line, allowing you to pass the 3D location of the light source from your XNA project to your HLSL effect: float3 xLightPosition; Next, for each vertex, you ll calculate the direction from the light going toward the vertex. As always, the direction from A to B is found by subtracting A from B. Keep in mind you need the final 3D position of the vertex for this, meaning the original 3D position transformed with the World matrix: float3 final3DPosition = mul(inPos, xWorld); float3 lightDirection = final3DPosition - xLightPosition; lightDirection = normalize(lightDirection); Output.LightFactor = dot(rotNormal, -lightDirection); Because the direction between the light source and vertex will probably be a lot larger than 1, you need to make sure its length becomes unity by normalizing it. Once you know this light direction, you re ready to proceed as in the previous recipe. When you run this code, in each vertex you will calculate the direction from the light source to that vertex, so this direction will be different for each vertex.
Distance Attenuation
To make things a bit more realistic, you may want to decrease the influence of a point light as the distance between the light source and the vertex becomes larger. To do this, find this distance by calling the length intrinsic on the lightDirection before you normalize it. Next, divide the LightFactor by this distance: float3 final3DPosition = mul(inPos, xWorld); float3 lightDirection = final3DPosition - xLightPosition; float distance = length(lightDirection); lightDirection = normalize(lightDirection); Output.LightFactor = dot(rotNormal, -lightDirection); Output.LightFactor /= distance;
CHAPTER 6 ADDIN G LIGHT TO Y OUR SCE NE IN XNA 2.0
The Code
In your XNA project, you can position your light source anywhere you want: effect.CurrentTechnique = effect.Techniques["VertexShading"]; effect.Parameters["xView"].SetValue(fpsCam.ViewMatrix); effect.Parameters["xProjection"].SetValue(fpsCam.ProjectionMatrix); effect.Parameters["xLightPosition"].SetValue(new Vector3(1, 5, 0)); effect.Parameters["xAmbient"].SetValue(0.0f); This is your complete vertex shader: VSVertexToPixel VSVertexShader(float4 inPos: POSITION0, float3 inNormal: NORMAL0) { VSVertexToPixel Output = (VSVertexToPixel)0; float4x4 preViewProjection = mul(xView, xProjection); float4x4 preWorldViewProjection = mul(xWorld, preViewProjection); Output.Position = mul(inPos, preWorldViewProjection); float3 normal = normalize(inNormal); float3x3 rotMatrix = (float3x3)xWorld; float3 rotNormal = mul(normal, rotMatrix); float3 final3DPosition = mul(inPos, xWorld); float3 lightDirection = final3DPosition - xLightPosition; lightDirection = normalize(lightDirection); Output.LightFactor = dot(rotNormal, -lightDirection); return Output; }
Copyright © OnBarcode.com . All rights reserved.