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.

Slides:



Advertisements
Similar presentations
COMP 175 | COMPUTER GRAPHICS Remco Chang1/6103b – Shapes Lecture 03b: Shapes COMP 175: Computer Graphics February 3, 2015.
Advertisements

1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Building Models modified by Ray Wisman Ed Angel Professor of Computer Science,
1 Building Models. 2 Objectives Introduce simple data structures for building polygonal models ­Vertex lists ­Edge lists OpenGL vertex arrays.
Ray tracing. New Concepts The recursive ray tracing algorithm Generating eye rays Non Real-time rendering.
Computer Graphics Programming: Matrices and Transformations CSE 3451 Matt Boggus.
Chapter 11 File Systems and Directories. 2 File Systems File: A named collection of related data. File system: The logical view that an operating system.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 9 GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology/George Koutsogiannakis 1.
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
CS 4731: Computer Graphics Lecture 10: 3D Modeling: Polygonal Meshes
OBJ File Format 2005/10/03. CAIG Lab,NCTU2 OBJ File Format A text file format First character of each line specifies the type of command #: just some.
1 CMSC 132: Object-Oriented Programming II Java Constructs Department of Computer Science University of Maryland, College Park.
1 Cube & Sphere Chapter 5. 2 Draw Cube Cube has 8 vertices as the picture shows. Each vertex has color of Red, Blue, Green or Yellow. We need to draw.
Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:
1 High Level Shader Language (HLSL) Chapter What is HLSL? The previous vertex and pixel shader programming is to use assembly language to write.
1 Texture Chapter 6. 2 What is Texture? Texture is a D3D interface that can map a partial of a picture to a 3D space by using Texture coordinates. The.
Computer Graphics: Programming, Problem Solving, and Visual Communication Steve Cunningham California State University Stanislaus and Grinnell College.
Chapter 2: C Fundamentals Dr. Ameer Ali. Overview C Character set Identifiers and Keywords Data Types Constants Variables and Arrays Declarations Expressions.
1 Ring, Circle, Cone Chapter 4. 2 Triangle rotating We have a triangle in the following position We want to rotate it around line y =  2. For this purpose,
CSE 381 – Advanced Game Programming Basic 3D Graphics
Introduction to Programming Writing Java Beginning Java Programs.
Program structure Four different storage-class specifications: –Automatic (auto): local to a function, normally declared variables are automatic. Does.
CS 4363/6353 TEXTURE MAPPING PART II. WHAT WE KNOW We can open image files for reading We can load them into texture buffers We can link that texture.
IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education.
Image Synthesis Rabie A. Ramadan, PhD D Images.
Chapter 11 File Systems and Directories. 2 File Systems File: A named collection of related data. File system: The logical view that an operating system.
Introduction to XNA Graphics Programming Asst. Prof. Rujchai Ung-arunyawee COE, KKU.
1 Viewing & Projective Window Chapter 3. 2 D3D virtual viewpoint Definition: virtual viewpoint is like a camera that was positioned in some where and.
Pointers OVERVIEW.
DirectX Objects Finalised Paul Taylor Packing Your Objects
Representation. Objectives Introduce concepts such as dimension and basis Introduce coordinate systems for representing vectors spaces and frames for.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
1 Manage Mesh Data Chapter 8. 2 How to manage Mesh data The most important two objects of mesh are VertexBuffer and IndexBuffer, which can obtained by.
J2ME: M3G/11 Intro to J2ME. Prog. v Objectives: –to continue talking about "M3G Chapter 2. An Animated Model" u explain animation and the floor.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
3D Programming and DirectX API. Content Mathematics Mathematics Prepare to Write a 3D program Prepare to Write a 3D program Program Structure Program.
Computer Graphics Basic 3D Geometry CO2409 Computer Graphics Week 5-1.
Introduction to Programming Writing Java Beginning Java Programs.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
Solid Modeling Ref. Mantyla. Introduction Aim of modeling: The search of a media of communication.
Mesh data structure & file format
Game Programming 07 OGRE3D Mesh in Action 2010 년 2 학기 디지털콘텐츠전공.
A brief introduction to javadoc and doxygen. What’s in a program file? 1. Comments 2. Code.
Containers Jason Goffeney 2/23/2006. Containers The STL is a standard part of probably all C++ implementations It features implementations of most of.
1 OGRE Programming Intermediate Tutorial: Volume Selection.
Models & Hierarchies CSE167: Computer Graphics Instructor: Steve Rotenberg UCSD, Fall 2006.
 Learn how you can use the shader through OpenGL ES  Add texture on object and make the object have a different look!!
 Learn some important functions and process in OpenGL ES  Draw some triangles on the screen  Do some transformation on each triangle in each frame.
Mesh Skinning Sébastien Dominé. Agenda Introduction to Mesh Skinning 2 matrix skinning 4 matrix skinning with lighting Complex skinning for character.
3D Objects in WebGL Loading 3D model files. Announcement Midterm exam on 12/2 –No Internet connection. –Example code and exam questions will be on USB.
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
Computer Graphics I, Fall 2010 Building Models.
A study of efficiency INDEX BUFFERS JEFF CHASTINE 1.
CS 116 Object Oriented Programming II Lecture 9 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
CHAPTER 9 File Storage Shared Preferences SQLite.
Basic Data Types อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 4.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Layers in Adobe After Effect
Introduction to javadoc
Chapter 3 Introduction to Classes, Objects Methods and Strings
Topics Introduction to File Input and Output
OBJECT ORIENTED PROGRAMMING II LECTURE 9 GEORGE KOUTSOGIANNAKIS
Meshes.
Week 1: File Systems and Directories
Introduction to javadoc
03 | Creating, Texturing and Moving Objects
Topics Introduction to File Input and Output
Presentation transcript:

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 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 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 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 49 Standard Templates

6 Some Templates

7

8

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 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 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 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 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 Mesh Data in tiger.x

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

16 Mesh Data in cube.x // 24 vertices

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

18 // 24 Texture coordinates

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

20 ColorCube.x

21 Hierarchy in ColorCube.x Mesh MeshVertexColors

22 Cube3.x Multiple Meshes

23 Mesh data in Taurus.x

24 Hierarchy in taurus.x Mesh MeshMaterialList MeshNormals Material

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 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 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 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 Out put

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 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 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 Out put

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 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 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 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 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 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 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 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 Test Sphere.x

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.