Add texture to cubes

xiaoxiao2021-03-06  115

Today, I saw "Managed DirectX 9 Kick Start: Graphics and Game Programming" about the texture, below I talked about how to simply add texture to the cube. If you are wrong, please refer to you, thank you. First of all, I change the vertices of the original positionColored type to POSITIONTEXTURED, because it is obvious. By the way, there is 2 large vertex types in CustomvertEx. One is transformed, one is POSITION, for 2D coordinates and 3D coordinates, each of which is more than [Normal] [color] [textured], meaning The following: [Normal]: It is a bit like defined the normal direction. When using Lights, the vertex method with NORMAL should be used [colored]: for the vertex in color [teXTURED]: add texture

When you change the top point, you should also change the and device.vertexFormat in the VertexBuffer constructor.

Since it is a PositionTextureT type vertex, and the purpose is to add textures to the rectangle, it is to set Tu, TV, and texture coordinates for each vertex. The value of Tu, TV is between [0, 1], the coordinate of the upper left corner of the rectangular texture is set to (0, 0), the lower right corner (1, 1), the upper right corner (1, 0), the lower left corner (0, 1), a bit like a matrix coordinate ... then put the top of the texture corresponding to the rectangular vertices to be rendered, respectively ... such as: VERTS [10] .x = -1.0f; VERTS [10] .Y = 1.0f; VERTS [10] .z = -1.0F; VERTS [10] .Tu = 0.0F; VERTS [10] .tv = 1.0f; VERTS [11] .x = -1.0f; VERTS [ 11] .y = 1.0f; VERTS [11] .z = 1.0f; VERTS [11] .Tu = 0.0f; VERTS [11] .tv = 0.0f; VERTS [12] .x = 1.0f; VERTS [ 12] .y = 1.0f; VERTS [12] .z = -1.0F; VERTS [12] .TU = 1.0f; VERTS [12] .tv = 1.0f; VERTS [13] .x = 1.0f; VERTS [13] .y = 1.0f; VERTS [13] .z = 1.0f; VERTS [13] .Tu = 1.0f; VERTS [13] .tv = 0.0f;

The above code is the top floor of the cube.

The corresponding relationship between the vertices and textures is set, now you have to get the texture picture, the picture should be a bitmap, the bitmap is wide, high as the N times power sizes, such as 16x16, 32x32, 64x64. .. Wait, this can speed up the rendering speed, of course, other wide can be.

Texture Tex = New Texture (tool, new bitmap (this.gettype (), "texture.bmp"), 0, pool.managed; // method, there are other methods,

Texture.BMP is imported in the project, Solution Explore-> Your Solution Name-> Right-click -> Add EXIXG ITEM ...

Finally, set Device.SetTexture before calling Device.DrawPrimitives, such as: Device.Settexture (0, TEX), where the first parameter is the texture stage, read the book, now not very clear, see behind, I will explain again Let's, the second parameter is the texture that just set up. Come.

转载请注明原文地址:https://www.9cbs.com/read-102182.html

New Post(0)