.NET Tip



The Buffer Class...

The term buffer is related to memory, the Buffer class works on direct memory. This class can be used for arrays of primitive types and does not apply to objects. This class provides methods to copy bytes from one array of primitive types to another array of primitive types. 

In case of System.Array class, it carries an index to index copy. But in the Buffer class, it's from offset to offset. The Buffer class copies a specified number of bytes from a source array starting at a particular offset to a destination array starting at a particular offset.

In the System.Buffer class we have the BlockCopy( ) member to copy from one array to another. We can also get a byte from an array, set a byte in an array, and obtain the length of an array. This class provides better performance for manipulating primitive types as comapred to those in the System.Array class. 

Top


Device Drivers Tip



The Device Node...

The device node consists of a upper filter driver, a function driver and a lower filter driver. The device node also contains a bus driver and its associated bus filter driver. The device object created by the upper filter driver is known as FiDo whereas the device object created by the Bus driver is known as a physical device object or PDO.

Top


VC++ Tip



How do I write code to right justify the entries displayed in columns of list control?

Ans:
To right justify the text in columns of list control use CListCtrl::InsertColumn( ) function as shown below:

m_list.InsertColumn ( 0, "Name", LVCFMT_RIGHT, 100 ) ; // where m_list is a control var. of list control

Top


C++ Tip



Do templates provide reusability?

Ans: Yes, but of a different type. Templates provide a way to reuse the source code. Templates allow us to write one function or class that works for different data types. This significantly reduces source code size and increases code flexibility.

Top


C Tip



How many bytes of memory would the following code receive?

#include "alloc.h"
main( )
{

int *p ;
p = ( int * ) malloc ( 256 * 256 ) ;
if ( p == NULL )

printf ( "Allocation failed" ) ;

}

Ans: It would fail to allocate any memory because 256 * 256 is 65536 which when passed to malloc( ) would become a negative number since the formal argument of malloc( ), an unsigned int, can accommodate numbers only upto 65535.

Top


Article – C# - DirectX 3-D Basics


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.

Top


Joke



While the pope was visiting the USA, he told the driver of his limo that he has the sudden urge to drive. The driver was a good Catholic man, and would not ever dream of questioning the pope's authority. So the pope sat at the wheel, while his driver got in the back.

They were traveling down the road doing between 70 and 80 mph, when a policeman happened to see them. As he pulled them over, he called in to headquarters reporting a speeding limo, with a VIP inside it.

The chief asked: "Who is in the limo, the mayor?"
The policeman told him: "No, someone more important than the mayor."

Then the chief asked "Is it the governor?"
The policeman answered: "No, someone more important than the governor."

The chief finally asked: "Is it the President?"
The policeman answered: "No, someone even more important than the President."

This made the chief very angry and he bellowed: "Now who is more important than the President?!"
The policeman calmly whispered: "I'll put it to you this way chief. I don't know who is this guy, but he has the pope as his chauffeur." 

Top


Different Strokes



Top