Types of care
Environmental light: Reflected from other surfaces to reach objects
Diffuse light: propagates in a specific direction, reaching a surface and spreading uniformly in all directions
Mirror light: Light reflects in the same direction (can simulate bright spots)
Directx sets lighting through the following function:
Types of light sources
Point light source: has a fixed position and emits in all directions
Directional light: Without positional information, light propagates parallel to each other in a specific direction
Spotlight: With location information, emits conical light like a flashlight, propagating in a specific direction.
D3DXVECTOR3 dir(1.0f, -0.0f, 0.25f);
D3DXCOLOR color = d3d::WHITE;
D3DLIGHT9 light;
::ZeroMemory(&light, sizeof(light));
//set lighting properties (directional lighting)
light.Type = D3DLIGHT_DIRECTIONAL;
light.Ambient = *color * 0.6f;
light.Diffuse = *color;
light.Specular = *color * 0.6f;
light.Direction = *dir;
Device->SetLight(0, &light);
Device->LightEnable(0, true);
Device->SetRenderState(D3DRS_NORMALIZENORMALS, true);
The color of objects we see in the real world is determined by the color of the light reflected by the object. For example, a red object absorbs all non red light and reflects red light, so it is displayed as red. The SetMaterial function in directx can be used to set the material of the object.
Vertex normals describe the normals of the vertices that make up a polygon, which can be used to determine the angle of incidence at which light rays reach the surface. Assuming a face has three vertices p0, p1, p2, calculate the two vectors u ⃗61; of the plane; P0 − p1, v ⃗= P1- p2, then the normal vector of the face is n ⃗= U ⃗ × .
Texture Texture data can be read from image files and mapped to the triangles that make up the shape. Texture triangles are usually not the same size as screen triangles, and texture filters can be used to scale them to fit the screen. The texture filter directx is implemented using the following functions:
//creating textures
D3DXCreateTextureFromFile(
Device,
L"dx5_logo.bmp",
//set texture
Device->SetTexture(0, Tex);
//set texture filters
Device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
Device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
Device->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_POINT);
Fusion technology
Combine the color values of the elements being processed with the pixel color values stored in the background cache (to achieve a transparent effect). Firstly, draw objects that do not require fusion. Sort the objects to be fused according to their depth values relative to the camera, and draw them one by one in order from back to front. Set Outpixel as the fused pixel value:
Outpixel; Source Pixel * sourcefac+ DesPixel * defac
Source Pixelrepresents the pixel value to be fused, sourcefacrepresents the source pixel fusion factor, DesPixelrepresents the pixel color value in the background cache, Descrepresents the fusion factor of the target pixel. Directx can set the fusion factor through the following functions:
Device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
Device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
Use the Alpha component to set the transparency of an object, which can come from the texture's Alpha channel and diffuse color values.
//Alpha the component comes from the diffuse color value
Device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_DIFFUSE);
Device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
//Alpha the components come from the texture Alpha passageway
Device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
Device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);