DirectX BASICS (3)

Source: Internet
Author: User
Direct3d uses a texture coordinate system, which consists of a horizontal U axis and a vertical v axis. The elements in the texture are determined by the U and V coordinates. It is called Texel . Note that the V axis is down.

In direct3d, you can set eight textures (which are, correctly, eight-layer textures), which can be combined to create images with more details. This is also called multi-texture.
That is, the first parameter of idevice3ddevice9: settexture can specify the texture (layer) to be set ).
To destroy a texture, we can set the second parameter to null.

Textures are mapped to triangles on the screen. Generally, texture triangles are different from screen triangles. When the texture triangle is smaller than the screen triangle, the texture triangle is enlarged properly. When the texture triangle is larger than the screen triangle, the texture triangle is reduced as appropriate. In both cases, deformation will occur. Filtering is a kind of direct3d technology that helps smooth deformation.
Direct3d provides three different filters; each provides a different quality level. The better the quality, the slower the quality. Therefore, you must strike a balance between quality and speed. The texture filter is set using the idirect3ddevice9: setsamplerstate method:
D3dsamp_magfilter
D3dsamp_minfilter
D3dsamp_mipfilter
(Generally, it is better to use the default value)

we specify that the texture coordinates must be in the range of [0, 1. Technically, this is not true; they are beyond this range. Texture coordinates can also be defined outside the range of [0, 1] through the addressing mode of direct3d. There are four addressing modes: surround texture addressing mode, border color texture addressing mode, captured texture addressing mode, Image Texture addressing mode
// set WRAP address mode
device-> setsamplerstate (0, d3dsamp_addressu, callback);
device-> setsamplerstate (0, d3dsamp_addressv, d3dtaddress_wrap);
// set border color address mode
device-> setsamplerstate (0, parameters, d3dtaddress_border);
device-> setsamplerstate (0, d3dsamp_addressv, d3dtaddress_border);
device-> setsamplerstate (0, d3dsamp_bordercolor, 0x000000ff );
// set clamp address mode
device-> setsamplerstate (0, failed, d3dtaddress_clamp);
device-> setsamplerstate (0, d3dsamp_addressv, d3dtaddress_clamp );
// set mirror address mode
device-> setsamplerstate (0, failed, d3dtaddress_mirror);
device-> setsamplerstate (0, d3dsamp_addressv, d3dtaddress_mirror );

When you use a mix (usually alpha transparency), the following rules are followed:
Rule: First, do not use a mixture to draw objects. Then, sort objects by mixing them based on the distance between the objects and the camera. This is very effective. If the objects are in view coordinates, you can simply pick objects by using the Z component. Finally, draw objects in a mix of sequence from the back to the front.
By default, the hybrid mode is disabled. You can enable it by setting the d3drs_alphablendenable rendering status to true:
Device-> setrenderstate (d3drs_alphablendenable, true );
Source (usually calculated pixels, which are mixed by pixels in the back cache, d3drs_srcblend) and destination (pixels in the back cache, d3drs_destblend) the default values are d3dblend_srcalpha and d3dblend_invsrcalpha.

In d3d, by default, when a texture with an alpha channel is set, the Alpha value is obtained from the alpha channel. If there is no alpha channel, the Alpha value is obtained by the vertex color.
However, you can specify which resource to use through the following rendering status:
// Compute Alpha from diffuse colors during Shading
Device-> settexturestagestate (0, d3dtss_alph1_g1, d3dta_diffuse );
Device-> settexturestagestate (0, d3dtss_alphaop, d3dtop_selectarg1 );
// Take Alpha from Alpha Channel
Device-> settexturestagestate (0, d3dtss_alph1_g1, d3dta_texture );
Device-> settexturestagestate (0, d3dtss_alphaop, d3dtop_selectarg1 );

Direct3d supports a maximum of eight layers of textures. That is to say, the surface of a 3D object can have 1 ~ 8 different texture maps. Direct3d can mix these texture colors in sequence during a rendering process and render them to the surface of the same object. Each texture layer corresponds to 0 ~ Index Number 7, Multi-Layer Texture ing can simulate a more real 3D world. For example, to display a smooth marble floor with a reflection of the surrounding scenes, you can set the marble floor texture to texture layer 0 and texture layer 1 with a reflection of the surrounding scenes, then, by setting the direct3d Multi-Layer Texture mixing operation, the texture layer 0 and the texture layer 1 are mixed. Then, the drawn 3D object has both the texture color of the marble floor and the reflection of the scene. You can use direct3d to display a variety of images in the graphic display system by blending up to eight layers of textures.
These 8-layer textures are mixed layer by layer and then output. That is to say, a maximum of 8 stages are required to complete texture ing, and each stage is independent, you must set the corresponding color and Alpha mixing method for each stage. Therefore, a texture layer is equivalent to a texture stage. Multi-Layer Texture ing is also called multi-phase texture mixing. Whether to apply the texture layer 0 ~ 7, that is, whether 0 ~ 7. Operations in the texture phase can be performed by applications Program But their selection must be sequential. That is to say, layer n + 1 cannot be used without layer n.
By default, the default operation in the first texture phase (phase 0) is d3dtop_modulate, and the default operation in other texture operations is d3dtop_disable. That is, by default, direct3d uses a Single Layer Texture ing to draw a graph. Except for the texture layer 0, the texture layer 1 ~ 7 is disabled.
To map a multi-layer texture to an object's surface, you need to specify the texture coordinates used for each texture layer. The texture layers can use the same texture coordinates or different texture coordinates. Texture coordinates are included in vertex data. If different texture coordinates are required, multiple texture coordinates must be included in the vertex data, then, specify the texture coordinates used for each layer texture through the index:
For example:
Pd3ddevice-> settexturestagestate (0, d3dtss_texcoordindex, 0 );
Pd3ddevice-> settexturestagestate (1, d3dtss_texcoordindex, 1 );
Truct scustomvertex
{
Float x, y, z;
DWORD color;
Float U0, V0;
Float u1, V1;
};
# Define d3dfvf_custom_vertex (d3dfvf_xyz | d3dfvf_diffuse | d3dfvf_tex2)
// Declare d3dfvf_tex2 to indicate that there are two sets of texture coordinates in fvf.
Scustomvertex vertices [] =
{
{-3.0f,-3.0f, 0.0f, 0 xffffffff, 0.0f, 1.0f, 0.0f, 1.0f },
{-3.0f, 3.0f, 0.0f, 0 xffffffff, 0.0f, 0.0f, 0.0f, 0.0f },
{3.0f,-3.0f, 0.0f, 0 xffffffff, 1.0f, 1.0f, 1.0f, 1.0f },
{3.0f, 3.0f, 0.0f, 0 xffffffff, 1.0f, 0.0f, 1.0f, 0.0f}
};
The two sets of texture coordinate values are the same. You can change any of these coordinates so that two layers of texture use different texture coordinates.
Set the texture layer Blending Method Code As follows:
// Set color blend operation and texture coordinate index for texture stage 0
Pd3ddevice-> settexture (0, g_texture_0 );
Pd3ddevice-> settexturestagestate (0, d3dtss_colodrop, d3dtop_modulate );
Pd3ddevice-> settexturestagestate (0, d3dtss_colorarg1, d3dta_texture );
Pd3ddevice-> settexturestagestate (0, d3dtss_colorarg2, d3dta_diffuse );
Pd3ddevice-> settexturestagestate (0, d3dtss_alphaop, d3dtop_disable );
Pd3ddevice-> settexturestagestate (0, d3dtss_texcoordindex, 0); // U0 and V0 are automatically found.
Pd3ddevice-> setsamplerstate (0, d3dsamp_magfilter, d3dtexf_linear );
Pd3ddevice-> setsamplerstate (0, d3dsamp_minfilter, d3dtexf_linear );
// Set color blend operation and texture coordinate index for texture Stage 1
Pd3ddevice-> settexture (1, g_texture_1 );
Pd3ddevice-> settexturestagestate (1, d3dtss_colodrop, d3dtop_add );
Pd3ddevice-> settexturestagestate (1, d3dtss_colorarg1, d3dta_texture );
Pd3ddevice-> settexturestagestate (1, d3dtss_colorarg2, d3dta_current );
Pd3ddevice-> settexturestagestate (1, d3dtss_alphaop, d3dtop_disable );
Pd3ddevice-> settexturestagestate (1, d3dtss_texcoordindex, 1); // U1 and V1 are automatically found
Pd3ddevice-> setsamplerstate (1, d3dsamp_magfilter, d3dtexf_linear );
Pd3ddevice-> setsamplerstate (1, d3dsamp_minfilter, d3dtexf_linear );
Here, the diffuse color of the object is multiplied by the texture color of texture layer 0. The result is then added to the texture color of texture layer 1 and then output.

Texture ing is essentially to get the color value from the texture and apply it to the surface of the object. In essence, Multi-Layer Texture ing is to mix the color of Multi-Layer Texture and then apply it to the surface of the object. For processing convenience, direct3d processes the RGB and Alpha channels of colors respectively (this is probably the reason for the difference between d3dtss_colodrop and d3dtss_alphaop) the specific operation method is set through the texture stage status.
The code for setting the texture color mixing operation is roughly as follows:
// I indicates the serial number of the texture phase
Pd3ddevice-> settexturestagestate (I, d3dtss_colorarg1, arg1 );
Pd3ddevice-> settexturestagestate (I, d3dtss_colorarg2, arg2 );
Pd3ddevice-> settexturestagestate (I, d3dtss_colorop, OP );
Generally, d3dtss_colorarg1 is used to specify the color of the current texture layer. d3dtss_colorarg2 is used to specify the color of all texture layers before the color mixing process. d3dtss_colodrop is used to specify the blending mode. Direct3d uses the following method for texture mixing:
Colorstage = d3dtss_colodrop (d3dtss_colorarg1, d3dtss_colorarg2)

In the Coordinate Transformation and illumination assembly line of direct3d, the illumination effect is calculated based on the so-called "per-vertex" method, that is, the actual number is calculated for each vertex of the triangle, rather than for each pixel. Sometimes this may cause some obvious visual errors. For example, a large triangle has a light source near its surface. When the light source approaches a vertex of the triangle, then we can see the effect of the triangle's light. When the light source approaches the center of gravity of the triangle, the effect of the triangle's light will gradually disappear. The worst case is that when the light source is located in the center of the triangle, the entire triangle is only subject to a very small amount of light, and there will be a bright spot in the center of the triangle. Therefore, if the vertex is not illuminated, the correct triangle surface color cannot be calculated. To solve this problem, pixel-based illumination calculation can be used. However, pixel-based illumination calculation requires a large amount of computing. Texture pasters are usually used to simulate pixel-by-pixel illumination effects, the texture content is the result of the desired type of light source shining on a dark surface.
Texture ing is used to simulate the pixel-by-pixel illumination effect. Generally, the first texture is set to the original surface texture of the object, and the second texture is set to the illumination texture, and then multiply the colors of the two textures, so sometimes the colors of the two textures are multiplied by light ing ). This technique is often used to dark ing a texture ). The sample code is as follows:
Pd3ddevice-> settexture (0, g_base_texture );
Pd3ddevice-> settexturestagestate (0, d3dtss_texcoordindex, 0 );
Pd3ddevice-> settexturestagestate (0, d3dtss_colorarg1, d3dta_texture );
Pd3ddevice-> settexturestagestate (0, d3dtss_colodrop, d3dtop_selectarg1 );
Pd3ddevice-> settexture (1, g_dark_texture );
Pd3ddevice-> settexturestagestate (1, d3dtss_texcoordindex, 0 );
Pd3ddevice-> settexturestagestate (1, d3dtss_colorarg1, d3dta_texture );
Pd3ddevice-> settexturestagestate (1, d3dtss_colorarg2, d3dta_current );
Pd3ddevice-> settexturestagestate (1, d3dtss_colodrop, d3dtop_modulate );

When rendering a scene, direct3d can combine color information from several sources: vertex, current material, texture, and color information previously written to the rendering target, then combine some of the colors. You can also use Alpha to specify the weight of direct3d to mix these colors. Alpha information can be stored in vertices, materials, and textures. If the Alpha value is 0, the image is completely transparent. If the Alpha value is 1, the image is not transparent ~ The value between 1 indicates transparency to different degrees.
To obtain the Alpha value from a texture, use d3dta_texture as the Alpha parameter.
To use the Alpha value from the vertex, use d3dta_diffuse as the Alpha parameter and make sure that the rendering status d3drs_diffusematerialsource is set to d3dmcs_color1 (this is also the default status ).
To use the Alpha value from the material, use d3dta_diffuse as the Alpha parameter and make sure that the rendering status d3drs_diffusematerialsource is set to d3dmcs_material.
If the d3drs_diffusematerialsource parameter is not set using setrenderstate (), the diffuse color is obtained from the default source (vertex.

In direct3d, you can not only specify the texture coordinates of objects in the model loading or rendering phase, but also automatically generate texture coordinates through the direct3d rendering engine, it is used for special visual effects such as environment ing. Compared with manually setting texture coordinates, texture coordinates are automatically generated in the direct3d Coordinate Transformation and illumination assembly line, and the execution speed is faster.
The direct3d system can use the transformed camera space vertex coordinates and normal information to generate texture coordinates. If texture coordinates are automatically generated, texture coordinate data does not need to be included in the vertex, which reduces the amount of data transmitted during rendering. Automatic texture coordinate generation is mainly used to produce some special effects. In most cases, manually specify texture coordinates for each vertex.
Call settexturestagestate () and set the second parameter to d3dtss_texcoordindex to control how the direct3d system automatically generates texture coordinates.

Direct3d provides the coordinate transformation function for the generated texture coordinates. Similar to the vertex coordinate transformation, you can specify a 4x4 texture coordinate transformation matrix and multiply it by the generated texture coordinates, then, output the transformed texture coordinates to the direct3d rendering pipeline. Texture coordinate transformation can be used to perform 3D transformations, such as translation, rotation, and scaling, on texture coordinates. Texture coordinate transformation is very useful for generating some special effects. It does not need to directly modify the texture coordinates of vertices. For example, a simple translation matrix can be used to transform texture coordinates, so that the texture on the object surface can be changed continuously to produce animation effects. Environment ing is the most widely used automatic texture coordinate generation in 3D graphics programs.
You can use the idirect3ddevice9: settransform () function to set the 4x4 texture coordinate transformation matrix, which uses d3dts_texture0 ~ D3dts_texture7 is the first parameter, indicating that the texture layer 0 ~ 7.

Objects in a 3D scenario are not only affected by illumination, but also affected by the surrounding environment, and can map images in the surrounding environment. Environment ing is a technique used to simulate smooth object surface ing to the surrounding environment. In fact, it is to paste a texture containing the scene of the object's surrounding environment to the object's surface, so that we can simulate the ing of the object to the surrounding environment to a certain extent, without the need to use like Ray trackingAlgorithmThis complex computing technology.

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.