Game Development Reference
In-Depth Information
is transformed using this matrix to resolve a 3D position in object space to a pixel in
screen space
—
a 2D X,Y coordinate and a depth measured into the screen. The sec-
ond matrix,
g_mWorld
, will be used to transform a normal vector into world space,
which will be used by the pixel shader to calculate lighting on a per-pixel basis.
In addition to transforming vertex positions into screen space, the vertex shader also
combines the defined object color or a piece of its texture with as many as eight
lights defined in the environment. To help with this, the vertex shader defines two
more constant buffers,
cbObjectColors
and
cbLights
.
cbObjectColors
stores
a diffuse and ambient color and whether there is a valid texture, which will be used
later in the pixel shader.
cbLights
stores the color and direction of up to eight
directional lights, an ambient light color, and the number of valid lights.
Notice the difference in
register
values for both
cbuffer
objects.
cbObject-
Colors
is set to slot one, and
cbLights
is set to slot two. This isn
'
t mandatory, as
the shader compiler would automatically set them to those values because of their
order of definition, but it
s good to have this example.
cbObjectColors
has a
bool
that is specifically put into
c2.x
, which is the first 32-bit member of the
third vector in the structure. The
cbLights
constant buffer has a similar issue with
the
g_nNumLights
member not being a full 4 vector, but the
packoffset
defini-
tions have been left off, so the shader compiler could set them as it wants.
Just as you might pack a structure in C++ to save space or create code that will
access it with more specificity than the defaults, you can use
packoffset
to over-
ride the shader compiler to create a very tightly defined structure. It
'
s completely up
to you and your needs. These shaders could have all forgone both
register
and
packoffset
keywords and used the defaults, since there isn
'
'
t anything really special
about them.
The next block of code defines the
VS_INPUT
structure. It stores a position, a nor-
mal, and a texture coordinate and maps to the
D3DVertex_UnlitTextured
struct
used in the previous chapter to create geometry. Here it is again:
struct D3D11Vertex_UnlitTextured
{
Vec3 Pos;
Vec3 Normal;
Vec2 Uv;
};
One syntax different from C is after the colon for each member:
POSITION
,
NORMAL
,
and
TEXCOORD0
. These are called semantics, and they provide a way to identify each
member so that each member can be linked to the data you define in C++.