Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 X file & Mesh Chapter 8. 2 What is X file? 1.X file is a file that is used to store D3D program's data. 2.Any X file has extension *.x. 3.X file could.

Similar presentations


Presentation on theme: "1 X file & Mesh Chapter 8. 2 What is X file? 1.X file is a file that is used to store D3D program's data. 2.Any X file has extension *.x. 3.X file could."— Presentation transcript:

1 1 X file & Mesh Chapter 8

2 2 What is X file? 1.X file is a file that is used to store D3D program's data. 2.Any X file has extension *.x. 3.X file could be text file or binary file. They are exactly same except one is in binary format for the purpose to keep their data in secrete. 4.The first line of X file (text version) must be xof 0302txt 0032 It is called X declaration. 5.X file consists of two parts. All template definitions are in Part I; all data bodies are in Part II. 6.Any kind of data can be stored in a X file. Such as vertex arrays. One template could have more than one data body. 7.All data stored in X file are in a tree hierarchy structure.

3 3 Templates After X declaration, there are list of Templates, which are like template Game_Infor{ // GUID STRING Game_name; STRING COMPANY; DWORD nVertices; array Vector vertices[nVertices]; […]//place holder for child template } Note: Not every template has child template place holder […]

4 4 Generate GUID C:\Program Files\Microsoft Visual Studio\Common\Tools\GUIDGEN.exe GUID is a128 bits number for registering the template class, which can be generated by GUIDGEN.exe

5 5 49 Standard Templates

6 6 Some Templates

7 7

8 8

9 9 What is Mesh? In DirectX3D, mesh is a special format to record the 3D model information data. Generally, a mesh data could have some of the following information. (1 & 2 are the must) 1. Vertices. 2. Faces, which are determined by 3 or 4 vertices index. 3. Material list. (important if Mesh has multiple materials) 4. Normal of each vertices and normal of each face. 5. Texture coordinates of each vertex in all surfaces. 6. Texture image file name. 7. Effectively Animations. We can record mesh data in a X file

10 10 Template of Mesh template Mesh { DWORD nVertices; // number of vertices array Vector vertices[nVertices]; DWORD nFaces; // number of faces array MeshFace faces[nFaces]; [...] } Note 1: The minimum of a Mesh is of vertices & faces. Note 2: Vector is a set of 3 float numbers. Note 3: MeshFace is a set 3 or 4 indices of the vertices.

11 11 Possible Templates in Mesh 1 template MeshMaterialList { WORD nMaterials; // number of material colors DWORD nFaceIndexes; // number of color faces array DWORD faceIndexes[nFaceIndexes]; // The indexes values of the following materials [Material] // list of Materials } Note 1: This is used to specify the color of each MeshFace. Note 2: In array of faceIndexes, the n th integer value is the material index of the n th Meshface. Note 3: If there is only one Material, we set 1, 1, 0 means all MeshFaces use the same color Material.

12 12 Possible Templates in Mesh 2 template MeshNormals { DWORD nNormals; // number of vertices normal array Vector normals[nNormals]; DWORD nFaceNormals; // number of face normal array MeshFace faceNormals[nFaceNormals]; } Note 1: In array of vectors normals, the n th normal vector will belong to the n th vertex. Note 2: faceNormals is just the set of vertex indices. The program can automatically calculate the normal of Meshface.

13 13 Possible Templates in Mesh 3 template MeshTextureCoords { DWORD nTextureCoords; // number of coordinates array Coords2d textureCoords[nTextureCoords]; // list of floating number pairs } Note: In array of vectors textureCoords, the n th 2 floating numbers is the T exture Coordinates of the n th vertex.

14 14 Mesh Data in tiger.x

15 15 Hierarchy in tiger.x Mesh MeshMaterialList MeshTextureCoords Material Texture Filename

16 16 Mesh Data in cube.x // 24 vertices

17 17 // 6 faces // 6 vertex normal // 6 mesh faces

18 18 // 24 Texture coordinates

19 19 Hierarchy in cube.x Mesh MeshMaterialList MeshNormals MeshTextureCoords Material child Texture Filename

20 20 ColorCube.x

21 21 Hierarchy in ColorCube.x Mesh MeshVertexColors

22 22 Cube3.x Multiple Meshes

23 23 Mesh data in Taurus.x

24 24 Hierarchy in taurus.x Mesh MeshMaterialList MeshNormals Material

25 25 Use DirectX Mesh Viewer The mesh model in a X file can be viewed by Mesh viewer Note: The texture image file must be in the same folder, otherwise the texture won’t be loaded.

26 26 Use Mesh to Draw 3D Model Mesh mesh = Mesh.FromFile(NameOfXFile, MeshFlags.SystemMemory, device); mesh.DrawSubset(0); Use Mesh to draw 3D Model is very simple, which only needs two lines of code, if without materials and textures. However, Mesh has materials and textures data, we need to use object ExtendedMaterial to collect materials and textures information. ExtendedMaterial[] materials = null; mesh = Mesh.FromFile("tiger.x", MeshFlags.SystemMemory, device, out materials);

27 27 Load Mesh to 3D program Mesh mesh;// Main Mesh object Material meshMaterial; // Mesh material Texture meshTexture; // Mesh Texture void InitMesh() { ExtendedMaterial[] materials = null; mesh = Mesh.FromFile("tiger.x", MeshFlags.SystemMemory, m_device, out materials); meshMaterial = materials[0].Material3D; meshMaterial.Ambient =meshMaterial.Diffuse; // if lighting on meshTexture = TextureLoader.FromFile(device, materials[0].TextureFilename) ; } Note: This is only for one material

28 28 Draw Mesh void Render() {......... device.BeginScene(); SetRotation(); device.Material = meshMaterial; device.SetTexture(0, meshTexture); mesh.DrawSubset(0);// must use index 0 device.EndScene(); device.Present();... } Note: tiger.x and tiger.bmp must in the same directory

29 29 Out put

30 30 Multiple Materials Mesh mesh;// Main Mesh object Material [] meshMaterials; // Mesh material array Texture [] meshTextures; // Mesh Texture array int total; void InitMesh() { ExtendedMaterial[] materials = null; mesh = Mesh.FromFile("tiger.x", MeshFlags.SystemMemory, device, out materials); total = materials.Length; meshTextures = new Texture[total]; meshMaterials = new Material[total];

31 31 for( int i=0; i<total; i++ ) { meshMaterials[i] = materials[i].Material3D; meshMaterials[i].Ambient = meshMaterials[i].Diffuse; // if lighting on meshTextures[i] = TextureLoader.FromFile(device, materials[i].TextureFilename); } Note: If there is no Texture in the Mesh, delete code related to the textures.

32 32 Draw Mesh void Render() {......... device.BeginScene(); SetRotation(); for( int i=0; i<total; i++ ) { device.Material = meshMaterials[i]; device.SetTexture(0, meshTextures[i]); mesh.DrawSubset(i) } device.EndScene(); device.Present();...... }

33 33 Out put

34 34 Write mesh to X file public Mesh( int numFaces, int numVertices, MeshFlags options, VertexFormats vertexFormat, Device device ); Step1: Use the following Constructor to create a Mesh object cube_mesh = new Mesh(12, 8, MeshFlags.Managed, CustomVertex.PositionColored.Format, device); Example:

35 35 Step2: Set VertexBuffer and IndexBuffer values of the Mesh. Define vertex array Verts and index array Indices. After set their data, write them into vertexBuffer and indexBuffer of mesh object CustomVertex.PositionColored [] Verts; short []Indices; using(VertexBuffer VB = mesh.VertexBuffer) { GraphicsStream data = VB.Lock (0, 0, LockFlags.None ); data.Write(Verts); VB.Unlock(); } using(IndexBuffer IB = mesh.IndexBuffer) { IB.SetData(Indices, 0, LockFlags.None); } Note: Index array must be of type short. One int will be split into 2 short index numbers

36 36 Step3: Save to an X file by the following function of Mesh public void Mesh::Save( string filename, int[] adjacency, ExtendedMaterial[] materials, EffectInstance[] effects, XFileFormat format ); Note: 1) It will save to the same directory of the excitable application 2) adjacency is array of three Int32 values per face that specify the three neighbors for each face in the mesh. Set to null if no. 3) materials cannot set to null, the array size is at least 1. 4) effects can set to null if no. 5) format set to FileFormat.TEXT.

37 37 Example1: Coding ColorCube.x private void MakeCubeMesh() { int numVerts = 8; // 8 points int numFaces = 12; // 12 triangles Mesh=new Mesh(numFaces, numVerts, MeshFlags.Managed, CustomVertex.PositionColored.Format, device); CustomVertex.PositionColored [] Verts = new CustomVertex.PositionColored[8]; Verts[0].Position = new Vector3(-1.0f, 1.0f, 1.0f); Verts[1].Position = new Vector3(-1.0f, -1.0f, 1.0f); Verts[2].Position = new Vector3( 1.0f, 1.0f, 1.0f); Verts[3].Position = new Vector3( 1.0f, -1.0f, 1.0f); Verts[4].Position = new Vector3(-1.0f, 1.0f, -1.0f); Verts[5].Position = new Vector3(1.0f, 1.0f, -1.0f); Verts[6].Position = new Vector3(-1.0f, -1.0f, -1.0f); Verts[7].Position = new Vector3(1.0f, -1.0f, -1.0f);

38 38 Verts[0].Color = Color.Red.ToArgb() ; Verts[1].Color = Color.Blue.ToArgb() ; Verts[2].Color = Color.Green.ToArgb() ; Verts[3].Color = Color.Yellow.ToArgb(); Verts[4].Color = Color.Yellow.ToArgb() ; Verts[5].Color = Color.Blue.ToArgb() ; Verts[6].Color = Color.Green.ToArgb() ; Verts[7].Color = Color.Red.ToArgb() ; using(VertexBuffer VB = mesh.VertexBuffer) { GraphicsStream data = VB.Lock(0, 0, LockFlags.None); data.Write(Verts); vb.Unlock(); } Or use mesh.SetVertexBufferData(Verts, LockFlags.None);

39 39 short[] indices = {// must use type short not type int 0,1,2, 1,3,2, // Front Face 4,5,6, 6,5,7, // Back Face 0,5,4, 0,2,5, // Top Face 1,6,7, 1,7,3, // Bottom Face 0,6,1, 4,6,0, // Left Face 2,3,7, 5,2,7 // Right Face }; using (IndexBuffer IB = mesh.IndexBuffer) { IB.SetData(indices, 0, LockFlags.None); } int [] adj= null; ExtendedMaterial[] mtrl = new ExtendedMaterial[1]; mesh.Save("cube.x", adj, mtrl, null, XFileFormat.Text); } Or use mesh.SetIndexBufferData(indices, LockFlags.None);

40 40 Example2: Coding TexturedSphere.x private void MakeTexturedSphereMesh(int m, int n, float r) { int numVerts = (m+1)*(n+1); int numFaces = 2*m*n; mesh=new Mesh(numFaces, numVerts, MeshFlags.Managed, CustomVertex.PositionColored.Format, device); CustomVertex.PositionNormalTextured[] Verts = new CustomVertex. PositionNormalTextured [(m+1)*(n+1)]; float a =2*(float)Math.PI/m; float t = (float)Math.PI/n for(int j=0; j<(m+1)*(n+1); j++) { int k=j/(m+1); int i=j%(m+1); float x = r*(float)Math.Sin(k*a)*(float)Math.Cos(i*t); float y = r*(float)Math.Sin(k*a)*(float)Math.Sin(i*t); float z = r*(float)Math.Cos(k*a); Verts[j].Position = new Vector3(x, y, z); Verts[j].Normal = Verts[j].Position ; Verts[j].Tu =(float)i/(float)m; Verts[j].Tv =(float)k/(float)n; }

41 41 mesh.SetVertexBufferData(Verts, LockFlags.None); int[] indices = new int[6*m*n]; for(int j=0;j<m*n;j++) { int k = j/m; int i = j%m; indices[j*6] =j+k; indices[1+j*6] = j+k+(m+2); indices[2+j*6] = j+k+(m+1); indices[3+j*6] = j+k; indices[4+j*6] = j+k+1; indices[5+j*6] = j+k+(m+2); } short [] index = new short [6*m*n]; for(int i=0; i<6*m*n ; i++) index[i] = (short)indices[i]; mesh.SetIndexBufferData(index, LockFlags.None ); int []adj= null; ExtendedMaterial[] mtrl = new ExtendedMaterial[1]; mtrl[0].TextureFilename = "earth.jpg"; mesh.Save("sphere.x", adj, mtrl,null, XFileFormat.Text); }

42 42 Test Sphere.x

43 43 DirectX3D Built-in Mesh DirectX3D has built-in the following several meshes: 1.Mesh.Teapot(device); 2.Mesh.Box(device, float, float, float); 3.Mesh.Taurus(device, float, float, int, int ); 4.Mesh.Cylinder(device, float, float,float, int, int); 5.Mesh.Sphere(device, float, int, int ); 6.Mesh.Polygon(device, float, int ); They can be directly used in 3D programs.


Download ppt "1 X file & Mesh Chapter 8. 2 What is X file? 1.X file is a file that is used to store D3D program's data. 2.Any X file has extension *.x. 3.X file could."

Similar presentations


Ads by Google