|
DirectX 3D Basics - II
The C # Column
(Continued from last week)
Let us now see how Direct3D uses these facts to generate realistic shapes. Instead
of using a cone Direct3D uses a close approximation of it a pyramid
to represent the viewing volume. In principle we can see an object, which is
present on any distance, even at infinity. But in reality we can see an object
present only up to a particular distance. Hence instead of a pyramid of infinite
length we need to use a frustum of a pyramid to represent a viewing volume.
This is shown in the following figure.
A
front and back clipping plane intersects the pyramid. The volume within the
pyramid between the front and back clipping planes is the viewing frustum. Objects
are visible only when they are in this volume. The viewing frustum is defined
by fov (field of view) and by the distances of the front and back clipping planes,
specified in z-coordinates. Once the viewing volume is specified we need to
create a projection matrix, which would apply the perspective projection to
the coordinates of the cube. These modified coordinates are then used to render
the shape on the screen (2D-plane). While actually rendering shapes using Direct3D
it relieves us of the burden of forming the projection matrix. we need to only
specify fov and the distances of the front and back plane from the eye. The
entire process is illustrated in the following figure.
Transformations
There are three basic transformations that can be applied to any 3D objects.
These are translation, rotation and scaling. These transformations are discussed
below. The Translation transformation involves movement of the object in the
x, y, or z direction. When we move the object in these directions the coordinates
of the object at the new position would be different from the original coordinates.
We can calculate the new coordinates using simple equations. However, from the
point of view of programming convenience if we store the coordinates in one
matrix and the distance by which the object is being translated ( in x, y, and
z directions ) in another and carry out simple matrix multiplication then the
resultant matrix would contain the new set of coordinates. Similar matrix operations
can be performed on Rotation and Scaling operations.
Translation
If x, y, and z are the coordinates of a point which is to be translated by a
distance Tx, Ty and Tz, then the matrix multiplication that would be carried
out is as under:
Here x, y and z are the new coordinates of the point.
Rotation
This
transformation involves rotation of an object around x, y or z axis. For example,
the following transformation rotates the point (x, y, z) around the x-axis,
producing a new point (x', y', z').
The following transformation rotates the point around the
z-axis.
Scaling
The scaling transformation involves increasing or decreasing the size of an
object.
The following transformation scales the point (x, y, z) by values Sx, Sy and
Sz in the x, y, and z directions to a new point (x', y', z').
Other than
programming convenience, using matrices for performing transformations offers
one more advantage. We can combine the effects of two or more transformation
matrices by multiplying the matrices representing these transformations. This
means that, to rotate an object and then translate it to some location, we dont
need to apply two multiplications. Instead, we can multiply the rotation and
translation matrices to produce a composite matrix that contains effects of
rotation and translation. This resultant matrix is then multiplied with the
coordinate matrix to yield new coordinates.
Direct3D provides
us methods to set up all the matrices for translation, rotation and scaling
as well as it provides a method that provides a matrix that represents the cumulative
effects of all the applied transformations. Thus Direct3D helps the user to
concentrate on the logical aspect of the game relieving him from the burden
of performing complex mathematical operations.
3D Objects
Let us have
a look at how a simple cube is formed in 3D. Before building the cube lets take
a look at the basic 3D primitives, which serve as the building blocks to create
a cube and more complex objects. A 3D primitive is a collection of vertices
(points in 3D space identified by x, y and z) that form any 3D entity.
Often, 3D primitives are polygons. A polygon is a closed 3D figure drawn by
connecting at least three vertices. The simplest polygon is a triangle. Direct3D
uses triangles to compose most of its polygons because all three vertices in
a triangle are guaranteed to be coplanar. If three points are connected they
always lie in the same plane.
Consider a pyramid. It has four points and when they are connected all the three
triangles do not lie in the same plane i.e. the vertices are non-coplanar. Rendering
of non-coplanar vertices is inefficient. Hence every 3D object be it a pyramid
or a sphere, is built out of triangles due to their coplanar nature. Below is
a sphere that is built from triangles.
Two triangles are used to create each face of the cube. A
cube has a total six faces i.e. 12 such triangles are used to compose the complete
cube. This is shown in the following figure.
|