Game Development Reference
In-Depth Information
'
shaders, but those are beyond the scope of this topic. Worry not, there
s a set of
further reading at the end of this chapter.
The Vertex Shader and Shader Syntax
A vertex shader is just a program
in fact, very similar to a C program. Its job is to
take vertices from the game, process them, and send them along to the pixel shader.
By process, the thing you see most often in a vertex shader is transformation and
lighting. But you can do much more; any operation supported by the shader syntax
is fair game, even moving the vertices around before you transform them. Vertex
shaders do their work and then send the output on to the pixel shader, which you
'
ll
see next.
The shader below is relatively simple and doesn
t look completely different from a C
program. Variables, data structures, and functions have a familiar look
'
but as you
would expect, there are differences. Many of these differences come from the fact that
the shader runs on the GPU in a video card, and that hardware expects data to be
presented in very specific ways. Second, the syntax helps
the shader with
data sent to it from C++. This chapter will present the shader first and then the C++
code to send it the data it needs to do its work.
The simple vertex shader presented below does the following three things:
hook up
n Transforms the position of a vertex from object space into screen space.
n Transforms the normal vector from object space to world space.
n Passes the texture coordinate through as-is.
//—————————————————————————————————————————————————————————————
// File: GameCode4_VSMain_VS.hlsl
// The vertex shader file for the GameCode4.
//
—————————————————————————————————————————————————————————————
// Globals
//
—————————————————————————————————————————————————————————————
cbuffer cbMatrices : register( b0 )
{
matrix
g_mWorldViewProjection : packoffset( c0 );
matrix
g_mWorld
: packoffset( c4 );
};
cbuffer cbLights : register( b1 )
{
float4 g_LightDiffuse[8];
float4 g_LightDir[8];
 
 
Search WWH ::




Custom Search