Coordinate system and basic elements (1)
Direct3d Basic Elements
Primitives is a basic graphic representation defined in direct3d. It is a set of vertices that constitute a single object. The simplest element is a set of multiple vertices in a three-dimensional coordinate system. It is called a point list ). A polygon is a polygon. A polygon is a closed graph composed of at least three edges. The simplest polygon is a triangle. direct3d uses a triangle to form most other polygon. This is because the three vertices of a triangle must be in the common surface, while the efficiency of rendering non-common vertices is relatively low. A combination of triangles can form larger and more complex polygon and mesh ).
Direct3d defines 6 basic elements.
Defines the primitives supported by direct3d.
typedef enum D3DPRIMITIVETYPE
{
D3DPT_POINTLIST = 1,
D3DPT_LINELIST = 2,
D3DPT_LINESTRIP = 3,
D3DPT_TRIANGLELIST = 4,
D3DPT_TRIANGLESTRIP = 5,
D3DPT_TRIANGLEFAN = 6,
D3DPT_FORCE_DWORD = 0x7fffffff,
} D3DPRIMITIVETYPE, *LPD3DPRIMITIVETYPE;
Constants
-
D3dpt_pointlist
-
Renders the vertices as a collection of isolated points. This value is unsupported for indexed primitives.
-
D3dpt_linelist
-
Renders the vertices as a list of isolated straight line segments.
-
D3dpt_linestrip
-
Renders the vertices as a single polyline.
-
D3dpt_trianglelist
Renders the specified vertices as a sequence of isolated triangles. Each group of three vertices defines a separate triangle.
-
Back-face culling is affected by the current winding-order render state.
-
D3dpt_trianglestrip
-
Renders the vertices as a triangle strip. The backface-culling flag is automatically flipped on even-numbered triangles.
-
D3dpt_trianglefan
-
Renders the vertices as a triangle fan.
-
D3dpt_force_dword
-
Forces this enumeration to compile to 32 bits in size. Without this value, Some compilers wocould allow this enumeration to compile to a size other than 32 bits. This value is not used.
Remarks
Using triangle strips (direct3d 9) or triangle fans (direct3d 9) is often more efficient than using triangle lists because fewer vertices are duplicated.
Point list (or vertex list) indicates that the graph to be drawn is an independent set. In a program, you can use the vertex list to represent the stars in the sky, or draw a line. Texture and material can also be applied to the point list elements, except that the color of the material or texture is only displayed at the position of the painting point, but not anywhere outside the point.
Drawprimitive () of idirect3ddevice9 is a method for drawing direct3d elements. The declaration of this method is as follows:
Renders a sequence of nonindexed, geometric primitives of the specified type from the current set of data input streams.
HRESULT DrawPrimitive(
D3DPRIMITIVETYPE PrimitiveType,
UINT StartVertex,
UINT PrimitiveCount
);
Parameters
-
Primitivetype
-
[In] member of the d3dprimitivetype enumerated type, describing the type of primitive to render.
-
Startvertex
-
[In] index of the first vertex to load. Beginning at startvertex the correct number of vertices will be read out of the vertex buffer.
-
Primitivecount
-
[In] Number of primitives to render. the maximum number of primitives allowed is determined by checking the maxprimitivecount Member of the d3dcaps9 structure. primitivecount is the number of primitives as determined by the primitive type. if it is a line list, each primitive has two vertices. if it is a triangle list, each primitive has three vertices.
Return values
If the method succeeds, the return value is d3d_ OK. If the method fails, the return value can be d3derr_invalidcall.
Remarks
When converting a legacy application to direct3d 9, you must add a call to either idirect3ddevice9: setfvf to use the fixed function pipeline, or idirect3ddevice9 :: setvertexdeclaration to use a vertex shader before you make any draw CILS.
A line list (or a line segment list) represents a group of independent line segments. A line segment set can be used to draw effects such as rain in a 3D scenario. The application creates a line segment set by filling a set of vertices. Note that the number of vertices must be an even number greater than or equal to 2. You can add a material or texture to a line segment set. By default, the color of a material or texture changes along the line segment, rather than a certain color on the line segment.
Line strip is an element composed of a group of connected lines. You can use line segment strip to create an unclosed polygon. A closed polygon is a polygon that connects the last vertex to the first vertex using a line segment. If a polygon is created using a line segment strip, the polygon may not share a surface, that is, it is not in a plane.
A triangle list is a series of independent triangles. They can be adjacent or not adjacent. The number of vertices in a triangle set must be at least 3, and the total number of vertices must be divisible by 3. The components of an object created using a set of triangles are not intersecting. For example, you can create a wall in a 3D Game by specifying a series of small triangles that are not connected to each other. Then add the visible materials and textures to these triangles, so that each triangle on the wall looks luminous. As there may be gaps between triangles, when players stare at scenes in the game, they may find that scenes Behind the wall become partially visible.
Triangle strips is a series of interconnected triangles. Because these triangles are interconnected, the application does not specify three vertices for each triangle. Objects in most 3D scenarios are composed of triangle strip, because the triangle strip can efficiently draw complex objects using memory and running time.
A triangle fan (Triangle fans) is similar to a triangle band. The difference is that a triangle fan shares a vertex.
Draw a graph using the vertex buffer
In direct3d, vertex buffer is the memory buffer used by direct3d to save vertex data, represented by the idirect3dvertexbuffer9 interface object. The vertex buffer can store any type of vertex data and perform coordinate transformation, illumination processing, cropping, and other operations on vertex data, the vertex data in the vertex buffer indicates the graph to be output to the screen.
Based on the graphic display needs, the vertex in the vertex buffer can contain vertex coordinates, color, normal direction, texture coordinates, and other attributes. What attributes does the vertex data contain, the flexible vertex format (fvf) can be used for description.
Flexible vertex format constants, or fvf codes, are used to describe the contents of vertices interleaved in a single data stream that will be processed by the fixed-function pipeline.
Vertex data flags
The following flags describe a vertex format. For information regarding vertex formats, see fixed function fvf codes (direct3d 9 ).
# Define |
Description |
Data Order and type |
D3dfvf_diffuse |
Vertex format into des a diffuse color component. |
DWORD in argb order. See d3dcolor_argb. |
D3dfvf_normal |
Vertex format must des a vertex normal vector. This flag cannot be used with the d3dfvf_xyzrhw flag. |
Float, float, float |
D3dfvf_psize |
Vertex format specified in point size. This size is expressed in camera space units for vertices that are not transformed and between, and in device-space units for transformed and beyond vertices. |
Float |
D3dfvf_specular |
Vertex format parameters des a specular color component. |
DWORD in argb order. SeeD3dcolor_argb. |
D3dfvf_xyz |
Vertex format parameters des the position of an untransformed vertex. This flag cannot be used with the d3dfvf_xyzrhw flag. |
Float, float, float. |
D3dfvf_xyzrhw |
Vertex format parameters des the position of a transformed vertex. This flag cannot be used with the d3dfvf_xyz or d3dfvf_normal flags. |
Float, float. |
D3dfvf_xyzb1 through d3dfvf_xyzb5 |
Vertex format contains position data, and a corresponding number of weighting (Beta) Values to use for multimatrix vertex blending operations. currently, direct3d can blend with up to three weighting values and four blending matrices. for more information about using blending matrices, see indexed vertex blending (direct3d 9 ). |
1, 2, or 3 floats. When d3dfvf_lastbeta_ubyte4 is used, the last blending weight is treated as a DWORD. |
D3dfvf_xyzw |
Vertex format contains transformed and clipped (X, Y, Z, W) data. processvertices does not invoke the Clipper, instead outputting data in clip coordinates. this constant is designed for, and can only be used with, the programmable vertex pipeline. |
Float, float |
Texture flags
The following flags describe texture flags used by the fixed-function pipeline.
# Define |
Description |
D3dfvf_tex0-d3dfvf_tex8 |
Number of texture coordinate sets for this vertex. The actual values for these flags are not sequential. |
D3dfvf_texcoordsizen (coordindex) |
Define a texture coordinate data set. N indicates the dimension of the texture coordinates. coordindex indicates texture coordinate index number. See d3dfvf_texcoordsizen and texture coordinates and texture stages. |
Mask flags
The following flags describe mask flags used by the fixed-function pipeline.
# Define |
Description |
D3dfvf_position_mask |
Mask for position bits. |
D3dfvf_reserved0, d3dfvf_reserved2 |
Mask values for reserved bits in the fvf. Do not use. |
D3dfvf_texcount_mask |
Mask value for texture flag bits. |
Miscellaneous flags
The following flags describe a variety of flags used by the fixed-function pipeline.
# Define |
Description |
D3dfvf_lastbeta_d3dcolor |
The last beta field in the vertex position data will be of Type d3dcolor. The data in the beta fields are used with matrix palette skinning to specify matrix indices. |
D3dfvf_lastbeta_ubyte4 |
The last beta field in the vertex position data will be of Type ubyte4. the data in the beta fields are used with matrix palette skinning to specify matrix indices.// Given the following vertex data definition: struct VERTEXPOSITION { float pos[3]; union { float beta[5]; struct { float weights[4]; DWORD MatrixIndices; // Used as UBYTEs } } };
Given the fvf is declared as: d3dfvf_xyzb5 | gradient. Weight and matrixindices are supported in Beta [5], where role says to interpret the last DWORD in Beta [5] as type ubyte4. |
D3dfvf_texcount_shift |
The number of BITs by which to shift an integer value that identifies the number of texture coordinates for a vertex. This value might be used as shown below.DWORD dwNumTextures = 1; // Vertex has only one set of coordinates.
// Shift the value for use when creating a // flexible vertex format (FVF) combination. dwFVF = dwNumTextures << D3DFVF_TEXCOUNT_SHIFT;
// Now, create an FVF combination using the shifted value.
|
Examples
The following examples show other common flag combinations.
// Untransformed vertex for lit, untextured, Gouraud-shaded content.
dwFVF = ( D3DFVF_XYZ | D3DFVF_DIFFUSE );
// Untransformed vertex for unlit, untextured, Gouraud-shaded
// content with diffuse material color specified per vertex.
dwFVF = ( D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_DIFFUSE );
// Untransformed vertex for light-map-based lighting.
dwFVF = ( D3DFVF_XYZ | D3DFVF_TEX2 );
// Transformed vertex for light-map-based lighting with shared rhw.
dwFVF = ( D3DFVF_XYZRHW | D3DFVF_TEX2 );
// Heavyweight vertex for unlit, colored content with two
// sets of texture coordinates.
dwFVF = ( D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_DIFFUSE | D3DFVF_SPECULAR | D3DFVF_TEX2 );
Coordinate system and basic elements (1 )~ Reprinted by tianxingjian, a gentleman who is striving for self-improvement