1 //--------------------------------------------------------------------------------------
\r
2 // File: BasicHLSL.fx
\r
4 // The effect file for the BasicHLSL sample.
\r
6 // Copyright (c) Microsoft Corporation. All rights reserved.
\r
7 //--------------------------------------------------------------------------------------
\r
10 //--------------------------------------------------------------------------------------
\r
12 //--------------------------------------------------------------------------------------
\r
13 float4 g_MaterialAmbientColor; // Material's ambient color
\r
14 float4 g_MaterialDiffuseColor; // Material's diffuse color
\r
17 float3 g_LightDir; // Light's direction in world space
\r
18 float4 g_LightDiffuse; // Light's diffuse color
\r
19 float4 g_LightAmbient; // Light's ambient color
\r
21 texture g_MeshTexture; // Color texture for mesh
\r
23 float g_fTime; // App's time in seconds
\r
24 float4x4 g_mWorld; // World matrix for object
\r
25 float4x4 g_mWorldViewProjection; // World * View * Projection matrix
\r
29 //--------------------------------------------------------------------------------------
\r
31 //--------------------------------------------------------------------------------------
\r
32 sampler MeshTextureSampler =
\r
35 Texture = <g_MeshTexture>;
\r
42 //--------------------------------------------------------------------------------------
\r
43 // Vertex shader output structure
\r
44 //--------------------------------------------------------------------------------------
\r
47 float4 Position : POSITION; // vertex position
\r
48 float4 Diffuse : COLOR0; // vertex diffuse color (note that COLOR0 is clamped from 0..1)
\r
49 float2 TextureUV : TEXCOORD0; // vertex texture coords
\r
53 //--------------------------------------------------------------------------------------
\r
54 // This shader computes standard transform and lighting
\r
55 //--------------------------------------------------------------------------------------
\r
56 VS_OUTPUT RenderSceneVS( float4 vPos : POSITION,
\r
57 float3 vNormal : NORMAL,
\r
58 float2 vTexCoord0 : TEXCOORD0,
\r
59 uniform int nNumLights,
\r
60 uniform bool bTexture,
\r
61 uniform bool bAnimate )
\r
65 float3 vNormalWorldSpace;
\r
67 // Transform the position from object space to homogeneous projection space
\r
68 Output.Position = mul(vPos, g_mWorldViewProjection);
\r
70 // Transform the normal from object space to world space
\r
71 vNormalWorldSpace = normalize(mul(vNormal, (float3x3)g_mWorld)); // normal (world space)
\r
73 // Compute simple directional lighting equation
\r
74 float3 vTotalLightDiffuse = float3(0,0,0);
\r
75 for(int i=0; i<nNumLights; i++ )
\r
76 vTotalLightDiffuse += g_LightDiffuse * max(0,dot(vNormalWorldSpace, g_LightDir));
\r
78 Output.Diffuse.rgb = g_MaterialDiffuseColor * vTotalLightDiffuse +
\r
79 g_MaterialAmbientColor * g_LightAmbient;
\r
80 Output.Diffuse.a = 1.0f;
\r
82 // Just copy the texture coordinate through
\r
84 Output.TextureUV = vTexCoord0;
\r
86 Output.TextureUV = 0;
\r
92 //--------------------------------------------------------------------------------------
\r
93 // Pixel shader output structure
\r
94 //--------------------------------------------------------------------------------------
\r
97 float4 RGBColor : COLOR0; // Pixel color
\r
101 //--------------------------------------------------------------------------------------
\r
102 // This shader outputs the pixel's color by modulating the texture's
\r
103 // color with diffuse material color
\r
104 //--------------------------------------------------------------------------------------
\r
105 PS_OUTPUT RenderScenePS( VS_OUTPUT In,
\r
106 uniform bool bTexture )
\r
110 // Lookup mesh texture and modulate it with diffuse
\r
112 Output.RGBColor = tex2D(MeshTextureSampler, In.TextureUV) * In.Diffuse;
\r
114 Output.RGBColor = In.Diffuse;
\r
120 //--------------------------------------------------------------------------------------
\r
121 // Renders scene to render target
\r
122 //--------------------------------------------------------------------------------------
\r
123 technique RenderSceneWithTexture1Light
\r
127 VertexShader = compile vs_2_0 RenderSceneVS( 1, true, true );
\r
128 PixelShader = compile ps_2_0 RenderScenePS( true ); // trivial pixel shader (could use FF instead if desired)
\r
132 technique RenderSceneWithTexture2Light
\r
136 VertexShader = compile vs_2_0 RenderSceneVS( 2, true, true );
\r
137 PixelShader = compile ps_2_0 RenderScenePS( true ); // trivial pixel shader (could use FF instead if desired)
\r
141 technique RenderSceneWithTexture3Light
\r
145 VertexShader = compile vs_2_0 RenderSceneVS( 3, true, true );
\r
146 PixelShader = compile ps_2_0 RenderScenePS( true ); // trivial pixel shader (could use FF instead if desired)
\r
150 technique RenderSceneNoTexture
\r
154 VertexShader = compile vs_2_0 RenderSceneVS( 1, false, false );
\r
155 PixelShader = compile ps_2_0 RenderScenePS( false ); // trivial pixel shader (could use FF instead if desired)
\r