|
DirectX is immensely powerful library that supports multimedia
programming. The new version of this library, known as DirectX 9.0
provides multimedia functionality for managed applications. DirectX 9.0
supports managed languages like C#, Visual Basic.NET, VC++.NET and
JScript.NET. Components
DirectX 9.0 for Managed Code provides following
components
Direct3D – Used for 3-D graphics programming.
DirectDraw – Enables direct access to video
memory, specially used for high-speed rendering.
DirectInput – Used for programming with
various input devices.
DirectPlay – Used for creating network
enabled multiplayer games.
DirectSound – Used for capturing sounds from
input devices and playing sound through playback devices.
Audio Video Playback – Used for working with
audio-video files.
Direct3D Concepts
We will start our journey through DirectX 9.0 by
understanding the terms and concepts involved in it.
Direct3D uses a left-handed cartesion coordinate
system. The values on the x-axis increase from left to right. The values
on the y-axis increase from bottom to top and the values on the z-axis
increase from back to front. This is shown in following Figure.

Consider your left hand with the palm up as shown in
the above figure. Then the thumb would represent the z-direction, which is
traveling away from the body.
In the left handed coordinate system we can position
3D objects in one of the three different types of space coordinates.
Model Space:
Suppose we are drawing different 3D entities like a cube, a sphere, a
pyramid etc. Each such object can be called a model. In the model space
each such model has a different origin.
World Space:
In the world space there is a fixed origin. The world transformation
matrix changes coordinates from model space, where vertices are defined
relative to a model's local origin to world space where vertices are
defined relative to an origin common to all the objects in a scene. In
essence, the world transformation places a model into the world and hence
its name.

Camera Space
(View Space): This view of the world places the viewer (user) at the
origin and faces him or her in the direction of the positive z-axis. The
view matrix is used to transform vertices from the world space to the
Camera space. Above figure and following two figures depicts these three
spaces.


Let us now take an example and combine these three
spaces together. If we want to spin two cubes the first thing that we need
to do is create the tow cubes using the model coordinates. In the above
figure for depicting the model space the two cubes have their own
coordinate separate coordinate systems. We have placed both the cubes at
origin in their respective coordinate systems. Then using the world
transformation matrix we place the cubes into the world space. The origin
of the model space and world space may be same or different. (In the above
example the origin of the model space and the camera space are the same).
Then we have translated the cube 2 in the world space using
transformations because had we done that the two cubes would have got
overlapped. Note that the camera is placed at (-200,100) in the world
space. In the world space to rotate scale and translate the cube various
transformation matrices are used. Next to look at the cube we have to
indicate the position of the eye/camera in the world space. When we do so
the origin of the world space is mapped to the eye position (Camera space
origin). Due to this the camera or the eye is placed at the origin and the
two cubes are reoriented with respect to the camera. Now if we display the
cubes on the screen they would hardly appear like real cubes. To make the
cubes appear more realistic we need to make some more effort.
There are two facts involved when a human eye sees an
object
(a)
A human eye sees what falls in the conical volume as shown in
following figure.

(b)
That part of the object that is nearer to the eye appears bigger
than the part that is farther away from it. This way of looking at an
object is known as perspective projection.
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 upto a
particular distance. Hence instead of a pyramid of infinite length we need
to use a frustrum 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 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 direction ) 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.
1) 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.

2) 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 y-axis

The following transformation rotates the point around
the z-axis.

3) 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 a object
and then translate it to some location, we don't need to apply two
multiplication's. 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 following figure.
|