Game Development BASICS (2)

Source: Internet
Author: User

 

Game Development Programming basics Chapter 2
Triangle Mesh is the basic unit for building object models.
Triangle elements in a grid:
Polygons, primitives, and mesh ry)
When describing a triangle unit, you must specify the positions of the three vertices of the triangle unit.
When describing an object, you must specify the list of triangle units that constitute the object.
After the vertex structure is defined, you need to use a combination of flexible vertex formats (flexible vertex format, fvf) to describe the vertex organization structure.
For example:
# Define fvf_color (d3dfvf_xyz | d3dfvf_diffuse) // location attribute, diffuse color
# Define fvf_normal_tex (d3dfvf_xyz | d3dfvf_normal | d3dfvf_tex1) // position, normal, texture
When defining a flexible vertex format, you must follow the convention that the specified sequence of the flexible vertex format tags must be consistent with the sequence of data definitions of the corresponding types in the vertex structure.
Triangle units are the basic components of 3D objects. To create an object, you must create a list of units that describe the contour shape and contour of the object.
The list of triangle units contains the data of each independent triangle we want to draw.
It is very important to specify the order of each vertex of a triangle. We call it a winding order)
The index concept is introduced to solve vertex coincidence.
We create a vertex list and an index list)
The vertex list contains all independent vertices. The index list contains the indexes pointing to the vertex list. These indexes specify the way in which each vertex is organized to build a triangle unit.
The camera specifies the visible part of the scene to the observer, which part of the 3D scene will be used to create a 2D image
In the world coordinate system, the camera has a certain position and direction, defining the visible space volume (in 3D graphics, it can also be referred to as the sight body or the sight body)
Objects that are not visible outside the horizon body should be discarded during further processing. The operation process of discarding such data is called cropping.

A 2D area in the projection window, where 3D ry in the horizon is mapped to the area by projection, a 2D representation of the 3D scene is created.
It is important that the minimum values of the rectangular projection window coordinates in the X and Y directions are min (-1,-1) and max)
Direct3d defines the projection plane as a plane Z = 1
To use direct3d to complete coordinate transformation, we must provide a transformation matrix that describes coordinate transformation.
To apply a transformation matrix, use the idirect3ddevice9-> settransfrom method,
The method has two parameters: 1. Description transformation type; 2. Description transformation matrix.

Assume that the center of a cube is located on the point (-3, 2, 6) in the world coordinate system, and the center of a sphere is located on the point (5, 0,-2:

Cube:
D3dxmatrix cubeworldmatrix;
D3dxmatrixtranslation (& cubeworldmatrix,-3.0f, 2.0f, 6.0f );
Device-> settransfrom (d3dts_world, & cubeworldmatrix );
Drawcube ();

Sphere:
D3dxmatrix sphereworldmatrix;
D3dxmatrixtranslation (& sphereworldmatrix, 5.0f, 0.0f,-2.0f)
Device-> settransfrom (d3dts_world, & sphereworldmatrix );
Drawsphere ();

To simplify the operation, we transform the camera to the origin of the world coordinate system and rotate it so that the camera's optical axis is consistent with the positive direction of the Z axis of the world coordinate system,
At the same time, all the geometries in the world space are transformed along with the camera to ensure the camera's field of view is constant. This kind of transformation is called landscape transformation. We call it the transformed geometric position.

In the observed coordinate system.
The scene transform matrix (observation matrix) can be obtained by the following d3dx function:
D3dxmatrix * d3dmatrixlookatlh (
D3dxmatrix * pout,
Const d3dxvector3 * peye, // The Position of the camera in the world coordinate system
Const d3dxvector3 * Pat, // The observed point of the camera in the world coordinate system
Const d3dxvector3 * pup // The vector in the "up" direction in the world coordinate system
);

For example, assume that the camera is located at (5, 3,-10) and its observation point is the origin of the world coordinate system (0, 0)

D3dxvector3 position (5.0f, 3.0f,-10.0f );
D3dxvector3 targetpoint (0.0f, 0.0f, 0.0f );
D3dxvector3 worolup (0.0f, 1.0f, 0.0f );

D3dxmatrix V;
D3dmatrixlookatlh (& V, & position, & targetpoint, & worldup );
// You need to use the idirect3ddevice9: settransform method to set the scene conversion. The parameter corresponding to the transformation type must be specified as d3dts_view:
Device-> settransform (d3dts_view, & V );

In order to remove the back surface, direct3d needs to distinguish which polygon are the front and which are the back orientation. By default, direct3d considers the order of vertices to be clockwise (observe coordinates

System) when the triangle unit is positive orientation, vertex arrangement order is clockwise (observed in the coordinate system) when the triangle unit back Orientation

Achieve the goal by modifying the rendering State d3drs_cullmode
Device-> setrenderstate (d3drs_cullmode,
Value // d3dcull_none disables backend hiding. // d3dcull_cw only disables clockwise round-robin triangle units. // d3dcull_ccw is the default value.

Value. Only the triangle units in the Counterclockwise round direction are hidden.
);

Projection Transformation defines the sight body and projects the ry in the sight body to the projection window. The projection matrix is complex. We will use the d3dx function:
// Its function is to create a projection matrix based on the description of the Field object.
D3dxmatrix * d3dxmatrixperspectivefovlh (
D3dxmatrix & pout, // point to the operation result matrix of the d3dxmatrix structure.
Float fovy, // The angle (radian) of the Y axis during observation, that is, the angle of the observed range
Float aspect, // Aspect Ratio
Float Zn, // The Z value of the position near the cropping surface.
Float ZF // The Z value of the remote cropping area.
);
The projection matrix application must be implemented using method: idirect3ddevice: settransform. The corresponding transformation type parameter must be specified as d3dts_projection.
Example:
D3dxmatrix proj;
D3dxmatrixperspectivefovlh (& proj, pI * 0.5f, (float) width/(float) height, 1.0f, 1000.0f );
Device-> settransform (d3dts_projection, & proj );

The task of using the viewport transform is to convert the vertex coordinates from the projection window to a rectangular area on the screen.
In direct3d, the view is represented by the d3dviewport9 structure.
Typedef struct d3dviewport9 {
Dword x;
DWORD y;
DWORD width;
DWORD height;
DWORD minz; // specify the minimum depth value in the depth cache.
DWORD maxz; // specify the maximum depth value in the depth cache.
} D3dviewport9;
The first four data members of this structure define the position and size of the window rectangle relative to its parent window.
Direct3d sets the depth range of the deep cache within the range [0, 1], so unless you want to pursue a special effect, minz and maxz should be limited within the specified range

Example:
D3dviewport9 Vp = {640,480 };
Device-> setviewport (& vp );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.