Graphics Reference
In-Depth Information
Under the OpenGL API, the outputs of a vertex shader are a set of attributes
and a vertex of the form ( x , y , a ,
z ) . That is, a homogeneous point for which the
perspective division has not yet been performed. The value a
/−
z will be used for
the depth test. We choose a = 1 so that the depth test is performed on
/
z , which
is a positive value for the negative z locations that will be visible to the camera.
We previously saw that any function that provides a consistent depth ordering can
be used for the depth test. We mentioned that distance along the eye ray,
1
z , and
1
/
z are common choices. Typically one scales the a value such that
a
/
z is in
the range [ 0, 1 ] or [
1, 1 ] , but for simplicity we'll omit that here. See Chapter 13
for the derivation of that transformation.
Note that we did not scale the output vertex to the dimensions of the image,
negate the y -axis, or translate the origin to the upper left in screen space, as we
did for the software renderer. That is because by convention, OpenGL considers
the upper-left corner of the screen to be at (
1, 1 ) and the lower-right corner at
( 1,
1 ) .
We choose the 3D position of the vertex and its normal as our attributes. The
hardware rasterizer will automatically interpolate these across the surface of the
triangle in a perspective-correct manner. We need to treat the vertex as an attribute
because OpenGL does not expose the 3D coordinates of the point being shaded.
Listings 15.33 and 15.34 give the pixel shader code for the shade routine,
which corresponds to the shade function from Listing 15.17, and helper functions
that correspond to the visible and BSDF::evaluateFiniteScatteringDensity
routines from the ray tracer and software rasterizer. The output of the shader
is in homogeneous space before the division operation. This corresponds to the
perspectiveProject function from Listing 15.24. The interpolated attributes
enter the shader as global variables Pinterp and ninterp . We then perform
shading in exactly the same manner as for the software renderers.
Listing 15.33: Pixel shader for computing the radiance scattered toward the
camera from one triangle illuminated by one light.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#version 130
// BSDF
uniform vec3
lambertian;
uniform vec3
glossy;
uniform float
glossySharpness;
// Light
uniform vec3
lightPosition;
uniform vec3
lightPower;
// Pre-rendered depth map from the light's position
uniform sampler2DShadow shadowMap;
// Point being shaded. OpenGL has automatically performed
// homogeneous division and perspective-correct interpolation for us.
in vec3
Pinterp;
in vec3
ninterp;
// Value we are computing
out vec3
radiance;
// Normalize the interpolated normal; OpenGL does not automatically
// renormalize for us.
vec3 n = normalize(ninterp);
 
Search WWH ::




Custom Search