) Managed DirectX + C # Development (Getting Started) (7)

Source: Internet
Author: User
Chapter 6 mesh objects 1. What is mesh?We have already touched on the mesh class. Here we will introduce in detail that the mesh can be used to store any type of graphic data, but it is mainly used to encapsulate complicated models. The mesh class also has some ways to improve the rendering object performance. Mesh can be used to read 3D model files from external files, such as Maya files, so that models can be completed in 3D Max or Maya Software and then read into the program. Mesh objects also contain many geometric models. The mesh object package is located in the direct3d extension Library (d3dx direct3d extensions library. Note: Add a reference to the direct3dx. dll assembly. The mesh class has three constructors. The mesh class has several static methods used to create or load different models. All mesh objects contain a vertex buffer and an index cache. In addition, they also contain an attribute buffer ). Ii. Use MeshPreviously, the mesh object has been created using the following methods: Private mesh = NULL; mesh = mesh. box (device, 2.0f, 2.0f, 2.0f); then a cube with a length, width, and height of 2 is created. If you create a cylindrical, garden, or garden cone, the following is true: mesh = mesh. cylinder (device, 2.0f, 2.0f, 2.0f, 36,36); the second and third parameters radius1 and radius2 indicate the bottom and bottom radius of the cylinder, which must be non-negative; the fourth parameter length indicates the height of the cylinder in the z direction. The fifth parameter slices indicates the number of fragments along the central axis, and the sixth parameter stacks indicates the number of heaps along the main axis. (Similar to the number of blocks in the horizontal and vertical directions divided by longitude and weft. The method for creating a polygon is mesh = mesh. polygon (device, 2.0f, 8); The second parameter length indicates the length of each side of the polygon, and the third parameter sides indicates the number of edges mesh = mesh. sphere (device, 2.0f, 36, 36). The second parameter radius indicates the sphere radius. The third parameter slices and the fourth parameter stacks have the same meaning as cylinder. Mesh = mesh. TORUS (device, 0.5f, 2.0f, 36,18) the diameter of the second innerradius ring, the outer diameter of the third outterradius ring, and the number of sides on the fourth sides cross section, the fifth parameter is the number of rings on the cross section of rings. The first two values must be non-negative, and the last two values must be greater than or equal to three. If you create a teapot, the code is mesh1 = mesh. teapot (device); execution result: Now let's look at the render () function: Private void render () {If (device = NULL) return; device. clear (clearflags. target, system. drawing. color. black, 1.0f, 0); setupcamera (); device. beginscene (); material boxmaterial = new material (); boxmaterial. ambient = color. pink; boxmaterial. diffuse = color. white; device. material = boxmaterial; device. transform. world = matrix. translation (0, 50, 0); device. transform. world = matrix. identity; mesh1.drawsubset (0); device. endscene (); device. present ();} this time, unlike the previous one, we set a material for the scenario. The so-called material refers to how the object reflects the ambient light, scattered light, and high-gloss, you can also specify whether the object fully reflects the light. After setting, assign the material to the material attribute of the device, so that direct3d can know which material is used for rendering. Code mesh1.drawsubset (0); a parameter 0 is passed in the code. What does this mean? Mesh is divided into a series of subsets and rendered using the drawsubset () method. The normal elements created using the mesh class always have only one subset based on 0. So mesh. drawsubset (0) is used) 3. Read external filesUse mesh. formfile () and mesh. fromstream () can be read from external. X file, but the model created by 3dmax is not by default. X file. Therefore, you need to export it. X file, or use a dedicated tool for conversion. The stream method has more reloads to adapt to streams of different sizes. Mesh. the formfile () method has many overloaded versions. The following is one of them: public static mesh fromfile (string filename, // file name meshflags options, // how to load the data device, // render the device out graphicsstream adjacency of the mesh, // Save the three adjacent sides out extendedmaterial materials, // Save the normal direct3d material and a string loaded as a texture, it is usually the texture or resource file name out of processing tinstance effects // HLSL Material file and value used for mesh .); The adjacency parameter is an "out" parameter, indicating that after this method is completed, adjacency will be allocated and passed out, and adjacent information will be returned. The extendedmaterial class saves the normal direct3d material and a character string loaded as a texture. This string is usually the texture or resource file name used, because the texture is loaded by the program, it can also be a string provided by any user. The effectinstance parameter describes the HLSL material files and values used for mesh. You can select method overloading with different parameters as needed. In the program, if the. X file is read with a material or texture, you need to create an array based on the size of the material and texture to save the texture and texture. Because the mesh can have many different subsets, you need to create an array of materials and textures for each subset. Mesh will automatically align each subset with the texture of these subsets. You can use a loop to copy data in extenedmaterial to meshmaterials. If the subset also contains texture information, you can use textureloader. fromfile to create the texture. This method accepts two parameters, device, and the file name used as the texture. This method is much faster than the previously used system. Drawing. bitmap. The following code reads an external file: Private void loadmesh (string file) {extendedmaterial [] mtrl; // load mesh = mesh. fromfile (file, meshflags. managed, device, out mtrl); // if there are materials, save them; if (mtrl! = NULL) & (mtrl. length> 0) {meshmaterials = new material [mtrl. length]; meshtextures = new texture [mtrl. length]; // store each material and texture; For (INT I = 0; I <mtrl. length; I ++) {meshmaterials [I] = mtrl [I]. material3d; If (mtrl [I]. texturefilename! = NULL) & (mtrl [I]. texturefilename! = String. empty) {// if there is a texture, load it; meshtextures [I] = textureloader. fromfile (device ,@".. /.. /"+ mtrl [I]. texturefilename) ;}}} transferred from: dandancool http://blog.csdn.net/dandancool/archive/2007/06/26/1666574.aspx

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.