Game Development Reference
In-Depth Information
/ / C l e a r t h e f ra m e and depth b u f f e r s
f i l l F r a m e B u f f e r ( b a c k g r o u n d C o l o r ) ;
f i l l D e p t h B u f f e r ( i n f i n i t y ) ;
/ /
Outer
l o o p
i t e r a t e s
o v e r
a l l
t h e
p r i m i t i v e s
( u s u a l l y
t r i a n g l e s )
f o r
( each
g e o m e t r i c
p r i m i t i v e )
{
/ /
R a s t e r i z e
t h e
p r i m i t i v e
f o r
( each
p i x e l
x , y
i n
t h e
p r o j e c t i o n
o f
t h e
p r i m i t i v e )
{
/ /
T e s t
t h e
depth
b u f f e r ,
t o
s e e
i f
a
c l o s e r
p i x e l
has
/ /
a l r e a d y
been
w r i t t e n .
f l o a t
primDepth
=
g e t D e p t h O f P r i m i t i v e A t P i x e l ( x , y ) ;
i f
( primDepth > r e a d D e p t h B u f f e r ( x , y ) )
{
/ /
P i x e l
o f
t h i s
p r i m i t i v e
i s
obscured ,
d i s c a r d
i t
c o n t i n u e ;
}
/ /
Determine
p r i m i t i v e
c o l o r
a t
t h i s
p i x e l .
C o l o r
c
=
g e t C o l o r O f P r i m i t i v e A t P i x e l ( x , y ) ;
/ /
Update
t h e
c o l o r
and
depth
b u f f e r s
w r i t e F r a m e B u f f e r ( x , y ,
c ) ;
w r i t e D e p t h B u f f e r ( x , y ,
primDepth ) ;
}
}
Listing 10.2
Pseudocode for forward rendering using the depth buffer
in the depth buffer, sometimes known as the destination fragment. If the
source fragment we are currently rendering is farther away from the camera
than the existing value in the buffer, then whatever we rendered before this
is obscuring the surface we are now rendering (at least at this one pixel),
and we move on to the next pixel. However, if our depth value is closer
than the existing value in the depth buffer, then we know this is the closest
surface to the eye (at least of those rendered so far) and so we update the
depth buffer with this new, closer depth value. At this point we might
also proceed to step 2 of the rendering algorithm (at least for this pixel)
and update the frame buffer with the color of the light being emitted or
reflected from the surface that point. This is known as forward rendering,
and the basic idea is illustrated by Listing 10.2.
Opposed to forward rendering is deferred rendering, an old technique
that is becoming popular again due to the current location of bottlenecks
in the types of images we are producing and the hardware we are using to
produce them. A deferred renderer uses, in addition to the frame buffer
and the depth buffer, additional buffers, collectively known as the G-buffer
(short for “geometry” buffer), which holds extra information about the
surface closest to the eye at that location, such as the 3D location of the
surface, the surface normal, and material properties needed for lighting cal-
culations, such as the “color” of the object and how “shiny” it is at that
Search WWH ::




Custom Search