public void ComputeBoundingBox() { try { using (MeshBuffer b = meshObj.GetVertexBuffer(0)) { using (DataStream v = b.Map()) { v.Position = 0; bb = BoundingBox.FromPoints(v, meshObj.VertexCount, MeshInputElements10.GetStride(inputElements)); } } } catch (Exception ex) { } }
public int[] GetMeshIndices() { byte[] indices = null; int ibSizeInBytes; using (MeshBuffer b = meshObj.GetIndexBuffer()) { ibSizeInBytes = b.SizeInBytes; using (DataStream v = b.Map()) { v.Position = 0; indices = new byte[b.SizeInBytes]; v.Read(indices, 0, b.SizeInBytes); } } if (has32BitIndices) { int[] realIndices = new int[ibSizeInBytes / 4]; for (int i = 0, j = 0; i < indices.Length; i += 4, j++) { byte[] bytes = { indices[i], indices[i + 1], indices[i + 2], indices[i + 3] }; int n = BitConverter.ToInt32(bytes, 0); realIndices[j] = n; } return(realIndices); } else { int[] realIndices = new int[ibSizeInBytes / 2]; for (int i = 0, j = 0; i < indices.Length; i += 2, j++) { byte[] bytes = { indices[i], indices[i + 1] }; short n = BitConverter.ToInt16(bytes, 0); realIndices[j] = (short)n; } return(realIndices); } }
public Vector3[] GetMeshVertices() { MeshInputElements10.sNormalMesh[] meshData; using (MeshBuffer b = meshObj.GetVertexBuffer(0)) { using (DataStream v = b.Map()) { v.Position = 0; meshData = v.ReadRange <MeshInputElements10.sNormalMesh>(meshObj.VertexCount); } } List <Vector3> verts = new List <Vector3>(meshObj.VertexCount); foreach (MeshInputElements10.sNormalMesh n in meshData) { verts.Add(n.Position); } return(verts.ToArray()); }
public void GetObjectData(SerializationInfo info, StreamingContext context) { info.AddValue("VertexCount", meshObj.VertexCount); info.AddValue("FaceCount", meshObj.FaceCount); info.AddValue("NumAttributes", NumAttributes); info.AddValue("VertexBufferCount", meshObj.VertexBufferCount); for (int i = 0; i < meshObj.VertexBufferCount; i++) { using (MeshBuffer b = meshObj.GetVertexBuffer(i)) { using (DataStream v = b.Map()) { v.Position = 0; byte[] vertices = new byte[b.SizeInBytes]; v.Read(vertices, 0, b.SizeInBytes); info.AddValue("VertexBuffer_" + i, vertices, typeof(byte[])); info.AddValue("VertexBufferSizeInBytes_" + i, b.SizeInBytes); } } } using (MeshBuffer b = meshObj.GetIndexBuffer()) { using (DataStream v = b.Map()) { v.Position = 0; byte[] indicies = new byte[b.SizeInBytes]; v.Read(indicies, 0, b.SizeInBytes); info.AddValue("IndexBuffer", indicies, typeof(byte[])); info.AddValue("IndexBufferSizeInBytes", b.SizeInBytes); } } using (MeshBuffer b = meshObj.GetAttributeBuffer()) { using (DataStream v = b.Map()) { v.Position = 0; byte[] attr = new byte[b.SizeInBytes]; v.Read(attr, 0, b.SizeInBytes); info.AddValue("AttributeBuffer", attr, typeof(byte[])); info.AddValue("AttributeBufferSizeInBytes", b.SizeInBytes); } } for (int i = 0; i < attrTable.Length; i++) { info.AddValue("MeshAttribute_FaceCount_" + i, attrTable[i].FaceCount); info.AddValue("MeshAttribute_FaceStart_" + i, attrTable[i].FaceStart); info.AddValue("MeshAttribute_Id_" + i, attrTable[i].Id); info.AddValue("MeshAttribute_VertexCount_" + i, attrTable[i].VertexCount); info.AddValue("MeshAttribute_VertexStart_" + i, attrTable[i].VertexStart); } if (nodes != null) { for (int i = 0; i < nodes.Length; i++) { info.AddValue("MeshNodes_" + i, nodes[i]); } } if (materials != null) { for (int i = 0; i < materials.Length; i++) { info.AddValue("TextureFileName_" + i, materials[i]); } } if (animations != null && animations.Count > 0) { info.AddValue("Animations", animations); } }
public static void RenderNormals(Model gm, Matrix mWorld) { byte[] vertices = null; byte[] indices = null; int vbSizeInBytes, ibSizeInBytes; using (MeshBuffer b = gm.MeshObj.GetVertexBuffer(0)) { vbSizeInBytes = b.SizeInBytes; using (DataStream v = b.Map()) { v.Position = 0; vertices = new byte[b.SizeInBytes]; v.Read(vertices, 0, b.SizeInBytes); } } using (MeshBuffer b = gm.MeshObj.GetIndexBuffer()) { ibSizeInBytes = b.SizeInBytes; using (DataStream v = b.Map()) { v.Position = 0; indices = new byte[b.SizeInBytes]; v.Read(indices, 0, b.SizeInBytes); } } DataStream s = new DataStream(vbSizeInBytes, true, true); s.WriteRange(vertices); s.Position = 0; BufferDescription bufferDescription = new BufferDescription(); bufferDescription.BindFlags = BindFlags.VertexBuffer; bufferDescription.CpuAccessFlags = CpuAccessFlags.None; bufferDescription.OptionFlags = ResourceOptionFlags.None; bufferDescription.SizeInBytes = vbSizeInBytes; bufferDescription.Usage = ResourceUsage.Default; D3D10.Buffer verticesBuffer = new D3D10.Buffer(Game.Device, s, bufferDescription); s.Close(); s = new DataStream(ibSizeInBytes, true, true); s.WriteRange(indices); s.Position = 0; bufferDescription = new BufferDescription(); bufferDescription.BindFlags = BindFlags.IndexBuffer; bufferDescription.CpuAccessFlags = CpuAccessFlags.None; bufferDescription.OptionFlags = ResourceOptionFlags.None; bufferDescription.SizeInBytes = ibSizeInBytes; bufferDescription.Usage = ResourceUsage.Default; D3D10.Buffer indicesBuffer = new D3D10.Buffer(Game.Device, s, bufferDescription); s.Close(); Shader effect = WorldData.GetObject("debugger.fx") as Shader; EffectTechnique tech = effect.EffectObj.GetTechniqueByName("RenderNormals"); EffectPass pass = tech.GetPassByIndex(0); InputLayout layout = ShaderHelper.ConstructInputLayout(gm.Mesh3d.inputElements, pass.Description.Signature); Game.Device.InputAssembler.SetPrimitiveTopology(PrimitiveTopology.TriangleList); Game.Device.InputAssembler.SetIndexBuffer(indicesBuffer, Format.R16_UInt, 0); Game.Device.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(verticesBuffer, MeshInputElements10.GetStride(gm.Mesh3d.inputElements), 0)); Game.Device.InputAssembler.SetInputLayout(layout); effect.GetVar("WorldViewProj").AsMatrix().SetMatrix(mWorld * Camera.ViewMatrix * Camera.ProjectionMatrix); pass.Apply(); Game.Device.DrawIndexed(gm.MeshObj.FaceCount * 3, 0, 0); }