Use a Mesh rendering complex model (call .x file)

xiaoxiao2021-03-06  111

In the front, we learned a teapot using Mesh in a simple class library, but in practice, our model is not just a teapot so simple? In fact, there are many complex 3D models, such as 3Dmax, etc. manufactured by modeling software. D3D support .x model file, .x files include model data, such as VertexBuffer, IndexBuffer, and materials. Moreover, material information may also include information such as texture. While getting these files, you can also get the HIGHLEVEL SHADER LANGUAGE (HLSL) file (HLSL is a high-level topic, not learning, later explained later). By using the modeling software generated .x file, make our 3D programming easier and easier. Here's how simple LOAD is a .x file.

1. First of all, introduce the most simple static method call .x file: Mesh.FromFile (string filename, Microsoft.DirectX.Direct3D.MeshFlags options ,, Microsoft.DirectX.Direct3D.Device device, out Microsoft.DirectX.Direct3D. ExtendedMaterial [] Materials)

Parameters say: FileName: means the path of the .x file options: is Meshflags, which is used to specify where the read data is put, how to put it. Such as: Meshflags.dyNAMIC refers to the use of usage.dynamic and usage.dynamic for IndexBuffer and VertexBuffer. There are still many meshflags, please see MSDN.DEIVCE: Al don't say it. Materials: This is an array of outputs that contain material information. Below we compile a way of loadMesh (String FileName). FileName, of course, the path to the .x files of LOAD Private void loadmesh (String file) {extendedMaterial [] mtrl; mesh = mesh.fromfile (file, meshflags.managed, device, out mtrl);

IF ((mtrl! = null) && (mtrl.Length> 0)) {meshmaterials = new material [mtrl.Length]; // The Material array created in advance is used to store material information read from .x files Meshtextures. = New Texture [mtrl.length]; // The texture array established in advance is used to store texture information from the .x file.

For (int i = 0; i

In the method, the overall idea is, first adjusting the construction method LOAD .X file, then judge the input ExtendedMaterial array. If there is data existence, store the data to the pre-defined Material array and the Texture array. Since Texture data is not necessarily presented, it is determined separately. Through the above method. The materials, vertices, and texture information in our .x file are extracted. The following can be rendered.

2. Just extract texture information, material information to two arrays, which is of course let the device know what material and texture information. For each texture and material: for (int i = 0; i

Through the above processing, the image can be displayed, however, you have to add a light to see clearly. Plus light: device.lights [0] .diffuse = system.drawing.color.white; device.lights [0] .enable.lights [0] .Nabled = true; device.lights [0] .direction = new vector3 (0.0F, 1.0F, 0.0 f); // Take the origin as the starting point device.lights [0] .type = lighttype.directional;

It is only introduced above. Of course, you can call these methods in the program, and prefer it to be instantiated in Device, call the loadmesh method, when rendering, add the above For statement to your rendering method, it will be. Also, be sure to set the view matrix and Projection matrix according to the actual situation. For my example, I am Tiny.x in the calling SDK. The view and Projection matrix I use as follows: device.transform.view = Matrix.lookatlh (New Vector3 (0.0F, -1000.0F, 200.0F), New Vector3 (0.0F, 0.0F, 0.0F), New Vector3 (0.0 f, 1.0f, 0.0f); device.transform.Projection = Matrix.PerspectiveFovLH (FLOAT) Math.pi / 4.0F, this.width / this.Height, 1.0F, 10000.0F);

knock off! Don't know if my narrative is correct? ? Please correct correctly.

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

New Post(0)