Game Development Reference
In-Depth Information
The VS_OUTPUT structure is defined similarly, except there is a special semantic,
SV_POSITION .Thisisasystemvaluesemantic,andittellstheshadercompilerthat
this value will be interpreted as a pixel location on the display. The entire VS_OUTPUT
structure will be sent to the pixel shader.
The next block of code defines a function, GameCode4_VS_Main . This function
accepts VS_INPUT and returns VS_OUTPUT . The syntax is similar to C, and just like
C there are over 100 intrinsic functions you can call upon. The one you see first, mul ,
multiplies or concatenates two matrices. Other intrinsic functions perform data type
conversions, vector operations like a dot product, trigonometric functions like cosine,
and even functions to compute high-precision partial derivatives. For now, we
'
ll stick
to the simple stuff.
The vertex shader function uses mul to transform the vPosition member in
VS_INPUT from object space to screen space, and to transform the vNormal mem-
ber from object space to world space. The vTexcoord member is just sent along as
is. With all the members of VS_OUTPUT assigned, it is returned, and the shader func-
tion exits.
If there are lights to worry about, the shader enters a loop. Another shader-intrinsic
function, dot , calculates the dot product between the light
s direction vector and the
normal vector sent in from the vertex shader. The dot product calculates an angle if
you recall, and if that angle is 90 degrees, the dot product is 0.0f. The max intrinsic
function makes sure that the dot product doesn
'
'
t contribute a negative value to the
light calculation. The light
'
s diffuse color is multiplied by the dot product to scale the
light
s contribution down to zero if the normal is at right angles to the light direction.
The light
'
s contribution, stored in dottedLightColor , is accumulated in the com-
bined contributions of all the lights, vTotalLightDiffuse .
After the lights are accumulated, the final result is combined with the object
'
s mate-
rial and stored in Output.vDiffuse . This is one of the simplest lighting models,
where the lights are accumulated for each vertex and combined with the object
'
s
material, which results in a color for each vertex. The pixel shader, which you will
see shortly, interpolates this color value for each pixel and, if it exists, combines
that with a value sampled from the texture.
'
Compiling the Vertex Shader
The shader is typically stored in an HLSL text file. To use it, it must be loaded and
compiled. It is possible to precompile shaders, saving some loading time, but it can
be useful to compile them at runtime so that different levels of shaders on the target
machine can be supported.
 
 
Search WWH ::




Custom Search