// Find apropriate vertex buffer and create it if doesn't exist
        public NVRVertexBuffer GetVertexBuffer(int vertexToAddCount, NVRVertexType type)
        {
            foreach (NVRVertexBuffer buffer in VertexBuffers)
            {
                if (buffer.Type == type && buffer.Vertices.Count < (UInt16.MaxValue - vertexToAddCount))
                {
                    return(buffer);
                }
            }
            NVRVertexBuffer created = new NVRVertexBuffer(type);

            VertexBuffers.Add(created);
            return(created);
        }
        // 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);
        }