Game Engine Architecture

(Ben Green) #1

458 10. The Rendering Engine


VtxOut vshaderMain(VtxIn in) // in maps to input
// registers
{
VtxOut out;
// ...
return out; // out maps to output registers
}
z Uniform declaration. To gain access to the data supplied by the applica-
tion via the constant registers, we can declare a variable with the key-
word uniform. For example, the model-view matrix could be passed to
a vertex shader as follows:
VtxOut vshaderMain(VtxIn in,
uniform float4x4 modelViewMatrix)
{
VtxOut out;
// ...
return out;
}
Arithmetic operations can be performed by invoking C-style operators,
or by calling intrinsic functions as appropriate. For example, to multiply the
input vertex position by the model-view matrix, we could write:
VtxOut vshaderMain(VtxIn in,
uniform float4x4 modelViewMatrix)
{
VtxOut out;
out.pos = mul(modelViewMatrix,in.pos);
out.color = float4(0, 1, 0, 1); // RGBA green
return out;
}
Data is obtained from textures by calling special intrinsic functions that
read the value of the texels at a specifi ed texture coordinate. A number of vari-
ants are available for reading one-, two- and three-dimensional textures in
various formats, with and without fi ltering. Special texture addressing modes
are also available for accessing cube maps and shadow maps. References to
the texture maps themselves are declared using a special data type known as
a texture sampler declaration. For example, the data type sampler2D repre-
sents a reference to a typical two-dimensional texture. The following simple
Cg pixel shader applies a diff use texture to a triangle:
struct FragmentOut
{
float4 color : COLOR;
};
Free download pdf