Graphics Reference
In-Depth Information
If the surface is analytic, but implicit rather than explicit,
so that the surface equation is given by S( x , y , z ) = 0, then its
normal vector is given by NS xSySz
= ∂∂∂∂∂∂ and
again, this needs to be normalized. The overall computation
is not otherwise significantly different.
On the other hand, if your surface or object is given
by simply seting vertex values, the normal cannot be
found analytically, and we must resort to computations
based on cross products of edges at vertices. This may be
made difficult, because the vertex shader can only access
coordinates of the vertex being processed; it cannot access
the coordinates of other vertices in the primitive. In case all
the vertex computations can be known without the actual
coordinates, this can be sidestepped. An example would be when a texture
map is used to compute vertex offsets, because adjacent texture coordinates
can be read from the vertex being processed, so the adjacent vertex coordinates
can be inferred.
But what if the computation does not let us infer coordinates? There
really is nothing the vertex shader can do in that case. However, geometry
shaders give you a way around this. In Chapter 12, we will show how you can
access the data from all the vertices in a primitive so you can compute a vertex
normal based on the cross product of edges, and at least get flat shading. If
your primitives also have adjacency information, you can get access to some
adjacent primitives as well, so you may be able to do just a bit beter than lat
shading. See Chapter 12 for more information.
Figure 7.8. Enneper's surface.
Summary
You can do several kinds of computation in either the vertex or fragment
shader. For example, you can calculate the direction from a vertex to a light
in a vertex shader and make it a variable to be interpolated in the fragment
shader, or you can use the interpolated pixel position and calculate the direc-
tion from that in a fragment shader. How do you decide exactly what you do
in the vertex shader and what you do in the fragment shader?
As you write vertex shaders, it can be tempting to write a separate shader
for each application. However, the lessons of code reuse suggest that you
should think about creating very general vertex shaders that can be used with
several different fragment shaders. This offers a very good place to include
appropriate #ifdef statements, so you can turn specific shader operations
Search WWH ::




Custom Search