Game Development Reference
In-Depth Information
void CPolygon::HorzLine(CCanvas &canvas, int x1, int x2, inty){
//Put x1 and x2 in the right order
int i;
if (x1>x2){ i=x1; x1=x2; x2=i; }
if (x1<0) x1=0;
i=canvas.GetWidth()-1;
if (x2>i) x2=i;
if (!ptr) return;//Off screen point
//24 bit only at the moment
for (i=x1;i<x2;i++) *ptr++=blue; *ptr++= green; *ptr++= red;
}
So far, the choice of colour is determined simply by a predetermined value.
More realistic computer graphics uses lighting. As the model moves
through the light, its on-screen colour changes. The simplest example of
this involves painting a single polygon. The most basic lighting model
varies the strength of a polygon's colour, based on its angle towards the
light. This is called Lambertian reflection. A light can be defined simply by
direction. This is a difficult concept for any photographers out there, since
intuitively they will think a light needs a position as well as a strength value.
Position is not necessary to create a convincing render. But this still leaves
the problem of angle. If we define a light by direction only, then we need to
track this direction. The angle of a polygon is described using its normal, as
we know from Chapter 1. We need to work out a value from the light vector
and each polygon normal that gives us a value for the current angle.
Following on from Chapter 1, we can determine the angle between two
vectors using the dot product of the two vectors.
L N
L N
cos(
)=
If each vector has magnitude 1, then this simplifies to
cos(
)= L•N
Using the inverse function acos , we can calculate the angle:
= acos( L•N )