Graphics Reference
In-Depth Information
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
wsInterpolatedNormal =
normalize(g3d_ObjectToWorldNormalMatrix * gl_Normal);
wsInterpolatedEye = wsEyePosition -
(g3d_ObjectToWorldMatrix * gl_Vertex).xyz;
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
dist = sqrt(dot(wsInterpolatedEye, wsInterpolatedEye));
}
... Fragment Shader ...
varying vec3 wsInterpolatedNormal;
varying vec3 wsInterpolatedEye;
varying float dist;
void main() {
vec3 wsNormal = normalize(wsInterpolatedNormal);
vec3 wsEye = normalize(wsInterpolatedEye);
vec2 selector; // index into texture map
selector.x = (1.0 + dot(wsNormal, wsEye))/2.0; // in [0 1]
selector.y = dist/2; // scaled to account for size of teapot
gl_FragColor.rgb = texture2D(xtoonMap, selector).rgb;
}
33.9 Discussion and Further Reading
As we discussed in this chapter and what we should include as examples, one of
us said, “I worry that what you guys call shaders are what I call graphics!” His
point was a good one: Phong shading involves the same computation whether you
do it on the CPU or on the GPU. The choice of where to implement a particular
aspect of your graphics program is a matter of engineering: What works best for
your particular situation? Since GPUs are becoming increasingly parallelized, and
branching tends to damage throughput, a rough guideline is that branch-intensive
code should run on the CPU and straight-line code on the GPU. But with tricks
like hiding an if statement by adding arithmetic, as in using
x=(u==1) * y + (u != 1) * z
as a replacement for
if (u == 1) then x = y else x = z
you can see that there's no hard-and-fast rule. The factors that may weigh in the
decision are software development costs, bandwidth to/from the GPU, and the
amount of data that must be passed between various shaders on the GPU.
New topics of shader tricks are being published all the time. Many of the
tricks described in these topics are ways to get around the limitations of current
GPU hardware or software architecture, and they tend to be out of date almost
as soon as the topics are published. Others have longer-term value, demonstrating
how the work in some algorithm is best partitioned among various shader stages.
33.10 Exercises
For all these exercises, you'll need a GL wrapper like G3D, or else a shader devel-
opment tool like RenderMonkey [AMD12], to experiment with.
Exercise 33.1: Write a vertex shader that alters the x -coordinate of every point
on an object as a sinusoidal function of its y -coordinate.
 
 
 
 
Search WWH ::




Custom Search