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...

43
1 X file & Mesh Chapter 8

Transcript of 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...

Page 1: 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.

1

X file & Mesh

Chapter 8

Page 2: 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.

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 bexof 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.

Page 3: 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.

3

TemplatesAfter X declaration, there are list of Templates, which are like

template Game_Infor{

<C042EEE0-7983-4a41-BD17-A1FBB75F7754> // 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 […]

Page 4: 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.

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

Page 5: 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.

5

49 Standard TemplatesAnimation EffectParamString MeshTextureCoordsAnimationKey EffectString MeshVertexColorsAnimationOptions FaceAdjacency PatchAnimationSet FloatKeys PatchMeshAnimTicksPerSecond Frame PatchMesh9Boolean FrameTransformMatrix PMAttributeRangeBoolean2d FVFData PMInfoColorRGB Guid PMVSplitRecordColorRGBA IndexedColor SkinWeightsCompressedAnimationSet Material TextureFilenameCoords2d MaterialWrap TimedFloatKeysDeclData Matrix4x4 VectorEffectDWord Mesh VertexDuplicationIndicesEffectFloats MeshFace VertexElementEffectInstance MeshFaceWraps XSkinMeshHeaderEffectParamDWord MeshMaterialListEffectParamFloats MeshNormals

Page 6: 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.

6

Some Templatestemplate Header {

<3D82AB43-62DA-11cf-AB39-0020AF71E433>

WORD major; WORD minor; DWORD flags;

}

template Vector {

< 3D82AB5E-62DA-11cf-AB39-0020AF71E433 >

float x; float y; float z;

}

template ColorRGB{

< D3E16E81-7835-11cf-8F52-0040333594A3 >

float red; float green; float blue;

}

template ColorRGBA {

<35FF44E0-6C7C-11cf-8F52-0040333594A3>

float red; float green; float blue; float alpha;

}

Page 7: 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.

7

template Coords2d {

<F6F23F44-7686-11cf-8F52-0040333594A3>

float u; float v;

}

template Material {

<3D82AB4D-62DA-11cf-AB39-0020AF71E433>

ColorRGBA faceColor; float power;

ColorRGB specularColor; ColorRGB emissiveColor;

[...]

}

template MeshTextureCoords {

<F6F23F40-7686-11cf-8F52-0040333594A3>

DWORD nTextureCoords;

array Coords2d textureCoords[nTextureCoords];

}

template MeshMaterialList {

<F6F23F42-7686-11cf-8F52-0040333594A3>

DWORD nMaterials; DWORD nFaceI ndexes;

array DWORD faceIndexes[nFaceI ndexes];

[Material]

}

Page 8: 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.

8

template MeshNormals {

<F6F23F43-7686-11cf-8F52-0040333594A3>

DWORD nNormals; array Vector normals[nNormals];

DWORD nFaceNormals; array MeshFace faceNormals[nFaceNormals];

} template MeshVertexColors {

<1630B821-7842-11cf-8F52-0040333594A3>

DWORD nVertexColors; array I ndexedColor vertexColors[nVertexColors];

} template MeshFace {

<3D82AB5F-62DA-11cf-AB39-0020AF71E433>

DWORD nFaceVertexIndices; array DWORD faceVertexIndices[nFaceVertexIndices];

} template Mesh {

<3D82AB44-62DA-11cf-AB39-0020AF71E433>

DWORD nVertices; array Vector vertices[nVertices];

DWORD nFaces; array MeshFace faces[nFaces];

[...]

}

Page 9: 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.

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

Page 10: 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.

10

Template of Meshtemplate Mesh {

<3D82AB44-62DA-11cf-AB39-0020AF71E433> 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.

Page 11: 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.

11

Possible Templates in Mesh 1template MeshMaterialList {

<F6F23F42-7686-11cf-8F52-0040333594A3>

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 nth integer value is the material index of the nth Meshface.Note 3: If there is only one Material, we set 1, 1, 0 means all MeshFaces use the same color Material.

Page 12: 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.

12

Possible Templates in Mesh 2template MeshNormals {

<F6F23F43-7686-11cf-8F52-0040333594A3>

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 nth normal vector will belong to the nth vertex.Note 2: faceNormals is just the set of vertex indices. The program can automatically calculate the normal of Meshface.

Page 13: 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.

13

Possible Templates in Mesh 3template MeshTextureCoords {

<F6F23F40-7686-11cf-8F52-0040333594A3>

DWORD nTextureCoords; // number of coordinates

array Coords2d textureCoords[nTextureCoords];

// list of floating number pairs

}

Note: In array of vectors textureCoords , the nth 2 floating numbers is the Texture Coordinates of the nth vertex.

Page 14: 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.

14

xof 0302txt 0064 MeshMaterialList { 1;

Mesh Body1 { 1; 303; 0;; 0.000000;-0.061426;0.938846;, Material { ......... 0.694118;0.694118;0.694118;1.000000;; -0.176064;-0.153352;0.152359;, 50.000000; -0.213423;-0.066057;0.311063;; 1.000000;1.000000;1.000000;;

0.000000;0.000000;0.000000;; 599; TextureFilename { 3;300,301,302;, "tiger.bmp"; ........... } 4;152,157,156,153;, } 3;136,137,187;, } ............ MeshTextureCoords { 3;16,0,1;, 303; 4;17,6,1,18;, 0.190860;-0.544059;, 3;3,0,16;, .................. ........... 0.397574;-0.547381;; 3;300,298,203;; }

}

Mesh Data in tiger.x

Page 15: 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.

15

Hierarchy in tiger.x

Mesh

MeshMaterialList

MeshTextureCoords

Material

TextureFilename

Page 16: 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.

16

Mesh Data in cube.xHeader { 1.0; 1.0;-1.0;, 1; -1.0; 1.0;-1.0;, 0; 1.0;-1.0;-1.0;, 1; -1.0;-1.0;-1.0;,}

-1.0; 1.0;-1.0;,Mesh { -1.0; 1.0; 1.0;, 24; -1.0;-1.0;-1.0;, -1.0; 1.0; 1.0;, -1.0;-1.0; 1.0;, 1.0; 1.0; 1.0;, -1.0;-1.0; 1.0;, 1.0; 1.0; 1.0;, 1.0;-1.0; 1.0;, 1.0; 1.0;-1.0;,

1.0;-1.0; 1.0;, -1.0;-1.0; 1.0;, 1.0;-1.0;-1.0;, 1.0;-1.0; 1.0;, -1.0;-1.0;-1.0;, -1.0; 1.0;-1.0;, 1.0;-1.0;-1.0;, 1.0; 1.0;-1.0;,

-1.0; 1.0; 1.0;, 1.0; 1.0; 1.0;;

// 24 vertices

Page 17: 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.

17

6; MeshNormals { 4;0,2,3,1;, 6; 4;4,6,7,5;, 0.0;0.0;1.0;, 4;8,10,11,9;, 0.0;-1.0;0.0;, 4;12,14,15,13;, 0.0;0.0;-1.0;, 4;16,18,19,17;, -1.0;0.0;0.0;, 4;20,22,23,21;; 1.0;0.0;0.0;,

0.0;1.0;0.0;; MeshMaterialList { 1; 6; 1; 4;0,0,0,0;, 0;; 4;1,1,1,1;, Material { 4;2,2,2,2;, 1.000000;1.000000;1.000000;1.000000;; 4;3,3,3,3;, 0.000000; 4;4,4,4,4;, 0.000000;0.000000;0.000000;; 4;5,5,5,5;; 0.000000;0.000000;0.000000;; } TextureFilename { "cube.bmp"; } }}

// 6 faces // 6 vertex normal

// 6 mesh faces

Page 18: 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.

18

MeshTextureCoords { 24; 1.0;1.0;, 0.0;1.0;, 0.0;1.0;, 1.0;0.0;, 1.0;0.0;, 0.0;0.0;, 0.0;0.0;, 1.0;1.0;, 1.0;1.0;, 0.0;1.0;, 0.0;1.0;, 1.0;0.0;, 1.0;0.0;, 0.0;0.0;; 0.0;0.0;, } 1.0;1.0;, } 0.0;1.0;, 1.0;0.0;, 0.0;0.0;, 1.0;1.0;, 0.0;1.0;, 1.0;0.0;, 0.0;0.0;, 1.0;1.0;,

// 24 Texture coordinates

Page 19: 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.

19

Hierarchy in cube.x

Mesh

MeshMaterialList

MeshNormals

MeshTextureCoords

Material

child

child

child

child

TextureFilename

Page 20: 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.

20

xof 0302txt 0064 MeshVertexColors { 24;

Header { 0; 0.0; 0.0; 1.0; 1.0;;, 1; 1; 1.0; 0.0; 0.0; 1.0;;, 0; 2; 1.0; 1.0; 0.0; 1.0;;, 1; 3; 0.0; 1.0; 0.0; 1.0;;,} 4; 0.0; 0.0; 1.0; 1.0;;,

5; 1.0; 0.0; 0.0; 1.0;;,Mesh { 6; 1.0; 1.0; 0.0; 1.0;;, 24; 7; 0.0; 1.0; 0.0; 1.0;;, -1.0; 1.0; 1.0;, 8; 0.0; 0.0; 1.0; 1.0;;, 1.0; 1.0; 1.0;, 9; 1.0; 0.0; 0.0; 1.0;;, -1.0;-1.0; 1.0;, 10; 1.0; 1.0; 0.0; 1.0;;, 1.0;-1.0; 1.0;, 11; 0.0; 1.0; 0.0; 1.0;;, . . . . . . . . . 12; 0.0; 0.0; 1.0; 1.0;;, -1.0; 1.0;-1.0;, 13; 1.0; 0.0; 0.0; 1.0;;, 1.0; 1.0;-1.0;, 14; 1.0; 1.0; 0.0; 1.0;;, -1.0; 1.0; 1.0;, 15; 0.0; 1.0; 0.0; 1.0;;, 1.0; 1.0; 1.0;; 16; 0.0; 0.0; 1.0; 1.0;;,

17; 1.0; 0.0; 0.0; 1.0;;, 6; 18; 1.0; 1.0; 0.0; 1.0;;, 4;0,2,3,1;, 19; 0.0; 1.0; 0.0; 1.0;;, 4;4,6,7,5;, 20; 0.0; 0.0; 1.0; 1.0;;, 4;8,10,11,9;, 21; 1.0; 0.0; 0.0; 1.0;;, 4;12,14,15,13;, 22; 1.0; 1.0; 0.0; 1.0;;, 4;16,18,19,17;, 23; 0.0; 1.0; 0.0; 1.0;;; 4;20,22,23,21;; } } ColorCube.x

Page 21: 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.

21

Hierarchy in ColorCube.x

Mesh

MeshVertexColors

Page 22: 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.

22

xof 0302txt 0064 Mesh Unnamed_2 {. . . . . . . . .

Header { 1; } 0; 1; Mesh Unnamed_3 {} . . . . . . . . .

Mesh Unnamed_0 { }. . . . . . . . .

Mesh Unnamed_4 {} . . . . . . . . .

Mesh Unnamed_1 { }. . . . . . . . .

Mesh Unnamed_5 {} . . . . . . . . .

}

Cube3.x

Multiple Meshes

Page 23: 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.

23

xof 0303txt 0032

Header { Material { 1; 0.988235;0.105882;0.039216;1.000000;; 0; 17.000000; 1; 0.898039;0.898039;0.898039;;} 0.000000;0.000000;0.000000;;

}Mesh { } // end of MashMaterialList 288; 0.066036;9.024136;-0.000000;, MeshNormals { 0.066036;8.950999;-0.272951;, 288; . . . . . . -0.000000;1.000000;-0.000001;, -0.893286;8.624888;0.472764;, 0.000000;0.865777;-0.500429;, . . . . . . . . 576; -0.129297;0.482544;0.866275;, 3;0,12,13;, -0.224080;0.836277;0.500430;; 3;0,13,1;, 576; . . . . . . . . . 3;0,12,13;, 3;287,11,0;, 3;0,13,1;, 3;287,0,276;; . . . . . . . . .

3;287,11,0;, MeshMaterialList { 3;287,0,276;; 1; } // end of MashNormals 1; 0; }

Mesh data in Taurus.x

Page 24: 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.

24

Hierarchy in taurus.x

Mesh

MeshMaterialList

MeshNormals

Material

Page 25: 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.

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.

Page 26: 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.

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);

Page 27: 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.

27

Load Mesh to 3D programMesh mesh; // Main Mesh objectMaterial 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

Page 28: 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.

28

Draw Meshvoid 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

Page 29: 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.

29

Out put

Page 30: 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.

30

Multiple MaterialsMesh mesh; // Main Mesh objectMaterial [] 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];

Page 31: 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.

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.

Page 32: 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.

32

Draw Meshvoid 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();

. . . . . .

}

Page 33: 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.

33

Out put

Page 34: 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.

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:

Page 35: 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.

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

Page 36: 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.

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 application2) 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.

Page 37: 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.

37

Example1: Coding ColorCube.xprivate 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);

Page 38: 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.

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);

Page 39: 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.

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);

Page 40: 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.

40

Example2: Coding TexturedSphere.xprivate 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;

}

Page 41: 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.

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);

}

Page 42: 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.

42

Test Sphere.x

Page 43: 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.

43

DirectX3D Built-in MeshDirectX3D 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.