Game Development Reference
In-Depth Information
10.11
Some HLSL Examples
In this section we will present some examples of HLSL vertex and pixel
shaders that demonstrate many of the techniques discussed in previous
sections. These code snippets are well commented, as we intend for this
code to be read. We are giving examples in HLSL for the same reason that
we show code snippets in C: we expect that it will be applicable to a large
number of our readers, and although we know not every reader will be using
this specific language, we think the language is su ciently high level that
many of the basic principles can be conveyed and appreciated by almost
everyone.
HLSL is essentially the same language as the shader language developed by
NVIDIA known as “Cg.” HLSL is also very similar, although not identical,
to GLSL, the shading language used in OpenGL.
One aspect of HLSL that we realize introduces an unwanted impediment
to those not interested in real-time rendering is the division of labor between
vertex and pixel shaders. 24 Unfortunately, this is where some of the messy
guts cannot be fully hidden. This topic is not a book on HLSL, so we don't
fully explain these details, and some exposure to HLSL is helpful. However,
since the language uses the C syntax, it is relatively approachable, and our
examples should be readable. For those who are unfamiliar with HLSL, the
comments in the examples introduce the HLSL specifics as they come up.
Because these examples are all very basic, they were written targeting
shader model 2.0.
10.11.1
Decal Shading and HLSL Basics
We'll start with a very simple example to get warmed up and demonstrate
the basic mechanisms of HLSL for declaring constants and passing inter-
polated arguments. Perhaps the simplest type of shading is to just output
the color from a texture map directly, without any lighting at all. This is
sometimes called decal shading. The vertex shader in Listing 10.11 illus-
trates several of the basic mechanisms of HLSL, as explained by the source
comments.
24 For example, the RenderMan shading language does not have this property.
 
Search WWH ::




Custom Search