void Init() { device = new Device(this.panel2.Width, this.panel2.Height); mesh = new Mesh("Cube", 8,12); mesh.Vertices[0] = new SharpDX.Vector3(-1, 1, 1); mesh.Vertices[1] = new SharpDX.Vector3(1, 1, 1); mesh.Vertices[2] = new SharpDX.Vector3(-1, -1, 1); mesh.Vertices[3] = new SharpDX.Vector3(1, -1, 1); mesh.Vertices[4] = new SharpDX.Vector3(-1, 1, -1); mesh.Vertices[5] = new SharpDX.Vector3(1, 1, -1); mesh.Vertices[6] = new SharpDX.Vector3(1, -1, -1); mesh.Vertices[7] = new SharpDX.Vector3(-1, -1, -1); mesh.Faces[0] = new Face { A = 0, B = 1, C = 2 }; mesh.Faces[1] = new Face { A = 1, B = 2, C = 3 }; mesh.Faces[2] = new Face { A = 1, B = 3, C = 6 }; mesh.Faces[3] = new Face { A = 1, B = 5, C = 6 }; mesh.Faces[4] = new Face { A = 0, B = 1, C = 4 }; mesh.Faces[5] = new Face { A = 1, B = 4, C = 5 }; mesh.Faces[6] = new Face { A = 2, B = 3, C = 7 }; mesh.Faces[7] = new Face { A = 3, B = 6, C = 7 }; mesh.Faces[8] = new Face { A = 0, B = 2, C = 7 }; mesh.Faces[9] = new Face { A = 0, B = 4, C = 7 }; mesh.Faces[10] = new Face { A = 4, B = 5, C = 6 }; mesh.Faces[11] = new Face { A = 4, B = 6, C = 7 }; camera = new Camera(); camera.Postion = new SharpDX.Vector3(0, 0, 10f); camera.Target = SharpDX.Vector3.Zero; }
// Loading the JSON file in an asynchronous manner public async Task<Mesh[]> LoadJSONFileAsync(string fileName) { var meshes = new List<Mesh>(); var file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(fileName); var data = await Windows.Storage.FileIO.ReadTextAsync(file); dynamic jsonObject = Newtonsoft.Json.JsonConvert.DeserializeObject(data); for (var meshIndex = 0; meshIndex < jsonObject.meshes.Count; meshIndex++) { var verticesArray = jsonObject.meshes[meshIndex].vertices; // Faces var indicesArray = jsonObject.meshes[meshIndex].indices; var uvCount = jsonObject.meshes[meshIndex].uvCount.Value; var verticesStep = 1; // Depending of the number of texture's coordinates per vertex // we're jumping in the vertices array by 6, 8 & 10 windows frame switch ((int)uvCount) { case 0: verticesStep = 6; break; case 1: verticesStep = 8; break; case 2: verticesStep = 10; break; } // the number of interesting vertices information for us var verticesCount = verticesArray.Count / verticesStep; // number of faces is logically the size of the array divided by 3 (A, B, C) var facesCount = indicesArray.Count / 3; var mesh = new Mesh(jsonObject.meshes[meshIndex].name.Value, verticesCount, facesCount); // Filling the Vertices array of our mesh first for (var index = 0; index < verticesCount; index++) { var x = (float)verticesArray[index * verticesStep].Value; var y = (float)verticesArray[index * verticesStep + 1].Value; var z = (float)verticesArray[index * verticesStep + 2].Value; // Loading the vertex normal exported by Blender var nx = (float)verticesArray[index * verticesStep + 3].Value; var ny = (float)verticesArray[index * verticesStep + 4].Value; var nz = (float)verticesArray[index * verticesStep + 5].Value; mesh.Vertices[index] = new Vertex { Coordinates = new Vector3(x, y, z), Normal = new Vector3(nx, ny, nz) }; } // Then filling the Faces array for (var index = 0; index < facesCount; index++) { var a = (int)indicesArray[index * 3].Value; var b = (int)indicesArray[index * 3 + 1].Value; var c = (int)indicesArray[index * 3 + 2].Value; mesh.Faces[index] = new Face { A = a, B = b, C = c }; } // Getting the position you've set in Blender var position = jsonObject.meshes[meshIndex].position; mesh.Position = new Vector3((float)position[0].Value, (float)position[1].Value, (float)position[2].Value); meshes.Add(mesh); } return meshes.ToArray(); }