public void Write(Stream stream) { using (BinaryWriter bw = new BinaryWriter(stream)) { NVRBuffers buffers = this.GenerateBuffers(); bw.Write(ASCIIEncoding.ASCII.GetBytes("NVR\0")); bw.Write(this.MajorVersion); bw.Write(this.MinorVersion); bw.Write(buffers.Materials.Count); bw.Write(buffers.VertexBuffers.Count); bw.Write(buffers.IndexBuffers.Count); bw.Write(buffers.Meshes.Count); bw.Write(buffers.Nodes.Count); foreach (NVRMaterial material in buffers.Materials) { material.Write(bw); } foreach (NVRVertexBuffer vertBuffer in buffers.VertexBuffers) { vertBuffer.Write(bw); } foreach (NVRIndexBuffer indBuffer in buffers.IndexBuffers) { indBuffer.Write(bw); } foreach (NVRMesh mesh in buffers.Meshes) { mesh.Write(bw); } foreach (NVRNode node in buffers.Nodes) { node.Write(bw); } } }
public NVRMesh(BinaryReader br, NVRBuffers buffers, bool readOld) { this.QualityLevel = (NVRMeshQuality)br.ReadInt32(); if (!readOld) { this.Flag = br.ReadInt32(); } this.BoundingSphere = new R3DSphere(br); this.BoundingBox = new R3DBox(br); this.Material = buffers.Materials[br.ReadInt32()]; this.IndexedPrimitives[0] = new NVRDrawIndexedPrimitive(br, buffers, this, true); this.IndexedPrimitives[1] = new NVRDrawIndexedPrimitive(br, buffers, this, false); }
public NVRNode(BinaryReader br, NVRBuffers buffers) { this.BoundingBox = new R3DBox(br); this.FirstMesh = br.ReadInt32(); this.MeshCount = br.ReadInt32(); this.FirstChildNode = br.ReadInt32(); this.ChildNodeCount = br.ReadInt32(); if (this.FirstChildNode == -1) { for (int i = this.FirstMesh; i < this.FirstMesh + this.MeshCount; i++) { this.Meshes.Add(buffers.Meshes[i]); } } }
public NVRFile(Stream stream) { using (BinaryReader br = new BinaryReader(stream)) { //Reading magic and version string magic = ASCIIEncoding.ASCII.GetString(br.ReadBytes(4)); if (magic != "NVR\0") { throw new InvalidFileSignatureException(); } this.MajorVersion = br.ReadInt16(); this.MinorVersion = br.ReadInt16(); //Reading the counts int materialsCount = br.ReadInt32(); int vertexBufferCount = br.ReadInt32(); int indexBufferCount = br.ReadInt32(); int meshesCount = br.ReadInt32(); int nodesCount = br.ReadInt32(); //Parse content NVRBuffers buffers = new NVRBuffers(); for (int i = 0; i < materialsCount; i++) { buffers.Materials.Add(new NVRMaterial(br, (MajorVersion == 8 && MinorVersion == 1 ? true : false))); } for (int i = 0; i < vertexBufferCount; i++) { buffers.VertexBuffers.Add(new NVRVertexBuffer(br)); } for (int i = 0; i < indexBufferCount; i++) { buffers.IndexBuffers.Add(new NVRIndexBuffer(br)); } for (int i = 0; i < meshesCount; i++) { buffers.Meshes.Add(new NVRMesh(br, buffers, (MajorVersion == 8 && MinorVersion == 1 ? true : false))); } // Unused for (int i = 0; i < nodesCount; i++) { buffers.Nodes.Add(new NVRNode(br, buffers)); } this.Meshes = buffers.Meshes; } }
public NVRDrawIndexedPrimitive(BinaryReader br, NVRBuffers buffers, NVRMesh mesh, bool isComplex) { this.Parent = mesh; // Read vertices this.VertexBuffer = br.ReadInt32(); this.FirstVertex = br.ReadInt32(); this.VertexCount = br.ReadInt32(); long currentOffset = br.BaseStream.Position; // Find vertex type int vertexSize = 12; if (isComplex) { this.VertexType = NVRVertex.GetVertexTypeFromMaterial(mesh.Material); switch (VertexType) { case NVRVertexType.NVRVERTEX_4: vertexSize = NVRVertex4.Size; break; case NVRVertexType.NVRVERTEX_8: vertexSize = NVRVertex8.Size; break; case NVRVertexType.NVRVERTEX_12: vertexSize = NVRVertex12.Size; break; } } //Parse vertices br.BaseStream.Seek(buffers.VertexBuffers[VertexBuffer].Offset + FirstVertex * vertexSize, SeekOrigin.Begin); for (int i = 0; i < VertexCount; i++) { NVRVertex newVertex; switch (VertexType) { case NVRVertexType.NVRVERTEX_4: newVertex = new NVRVertex4(br); break; case NVRVertexType.NVRVERTEX_8: newVertex = new NVRVertex8(br); break; case NVRVertexType.NVRVERTEX_12: newVertex = new NVRVertex12(br); break; default: newVertex = new NVRVertex(br); break; } this.Vertices.Add(newVertex); } // Store indices br.BaseStream.Seek(currentOffset, SeekOrigin.Begin); this.IndexBuffer = br.ReadInt32(); this.FirstIndex = br.ReadInt32(); this.IndexCount = br.ReadInt32(); for (int i = FirstIndex; i < FirstIndex + IndexCount; i++) { this.Indices.Add(buffers.IndexBuffers[IndexBuffer].Indices[i]); } // Fix indices int indicesMin = FindMin(this.Indices); for (int i = 0; i < this.Indices.Count; i++) { this.Indices[i] -= indicesMin; } }
// Generate buffers for writing private NVRBuffers GenerateBuffers() { NVRBuffers buffers = new NVRBuffers(); // Material buffer foreach (NVRMesh mesh in Meshes) { if (!buffers.Materials.Contains(mesh.Material)) { buffers.Materials.Add(mesh.Material); } } // Creating complex buffers first foreach (NVRMesh mesh in Meshes) { NVRDrawIndexedPrimitive complexMesh = mesh.IndexedPrimitives[0]; NVRVertexType type = complexMesh.Vertices[0].GetVertexType(); NVRVertexBuffer vertBuffer = buffers.GetVertexBuffer(complexMesh.Vertices.Count, type); int bufferIndex = buffers.VertexBuffers.IndexOf(vertBuffer); NVRIndexBuffer indBuffer = buffers.GetIndexBuffer(bufferIndex); complexMesh.IndexBuffer = bufferIndex; complexMesh.VertexBuffer = bufferIndex; complexMesh.FirstVertex = vertBuffer.Vertices.Count; complexMesh.FirstIndex = indBuffer.Indices.Count; complexMesh.IndexCount = complexMesh.Indices.Count; complexMesh.VertexCount = complexMesh.Vertices.Count; vertBuffer.Vertices.AddRange(complexMesh.Vertices); int indBufferMax = indBuffer.CurrentMax + 1; foreach (int index in complexMesh.Indices) { indBuffer.AddIndex(index + indBufferMax); } } // Then do simple ones foreach (NVRMesh mesh in Meshes) { NVRDrawIndexedPrimitive simpleMesh = mesh.IndexedPrimitives[1]; NVRVertexType type = simpleMesh.Vertices[0].GetVertexType(); NVRVertexBuffer vertBuffer = buffers.GetVertexBuffer(simpleMesh.Vertices.Count, type); int bufferIndex = buffers.VertexBuffers.IndexOf(vertBuffer); NVRIndexBuffer indBuffer = buffers.GetIndexBuffer(bufferIndex); simpleMesh.IndexBuffer = bufferIndex; simpleMesh.VertexBuffer = bufferIndex; simpleMesh.FirstVertex = vertBuffer.Vertices.Count; simpleMesh.FirstIndex = indBuffer.Indices.Count; simpleMesh.IndexCount = simpleMesh.Indices.Count; simpleMesh.VertexCount = simpleMesh.Vertices.Count; vertBuffer.Vertices.AddRange(simpleMesh.Vertices); int indBufferMax = indBuffer.CurrentMax + 1; foreach (int index in simpleMesh.Indices) { indBuffer.AddIndex(index + indBufferMax); } } NVRNode parentNode = this.CreateRootNode(); // Making mesh buffer buffers.GenerateMeshBuffer(parentNode); foreach (NVRMesh mesh in buffers.Meshes) { mesh.MaterialIndex = buffers.Materials.IndexOf(mesh.Material); } // Making node buffer buffers.Nodes.Add(parentNode); buffers.GenerateNodeBuffer(parentNode); foreach (NVRNode node in buffers.Nodes) { if (node.Children.Count > 0) { node.FirstChildNode = buffers.Nodes.IndexOf(node.Children[0]); } else { node.FirstChildNode = -1; } } return(buffers); }