Beispiel #1
0
        void generateMultiple16bitMeshbuffers(int N)
        {
            Vertex3D[] vertices32bit;
            uint[]     indices32bit;
            generateVerticesAndIndices(N, out vertices32bit, out indices32bit);

            List <Vertex3D> verticesChunk = new List <Vertex3D>();
            List <ushort>   indicesChunk  = new List <ushort>();

            int totalCubes              = N * N * N;
            int indicesInCube           = indices32bit.Length / totalCubes;
            int verticesInCube          = vertices32bit.Length / totalCubes;
            int maximumVerticesPerChunk = ushort.MaxValue;             // must not be more than 0xffff (because we use 16-bit indices)
            int verticesIndexOffset     = 0;

            device.Logger.Log("Batching cubes into 16-bit meshbuffers...");

            for (int cubeIndex = 0; cubeIndex < totalCubes; cubeIndex++)
            {
                // add vertices
                for (int i = 0; i < verticesInCube; i++)
                {
                    verticesChunk.Add(vertices32bit[cubeIndex * verticesInCube + i]);
                }

                // add indices
                for (int i = 0; i < indicesInCube; i++)
                {
                    indicesChunk.Add((ushort)(indices32bit[cubeIndex * indicesInCube + i] - verticesIndexOffset));
                }

                if (verticesChunk.Count + verticesInCube > maximumVerticesPerChunk ||              // if this chunk is full
                    cubeIndex == totalCubes - 1)                        // or this is last cube
                {
                    // we create meshbuffer and add it to the main mesh
                    MeshBuffer mb = MeshBuffer.Create(VertexType.Standard, IndexType._16Bit);
                    mb.SetHardwareMappingHint(HardwareMappingHint.Static, HardwareBufferType.VertexAndIndex);
                    //mb.Append(verticesChunk.ToArray(), indicesChunk.ToArray());
                    mb.Append(verticesChunk.ToArray(), indicesChunk.ToArray());
                    mb.RecalculateBoundingBox();
                    mesh.AddMeshBuffer(mb);
                    mb.Drop();

                    // clean up vertex and index chunks
                    verticesIndexOffset += verticesChunk.Count;
                    verticesChunk.Clear();
                    indicesChunk.Clear();

                    device.Logger.Log(
                        (((cubeIndex + 1) * 100) / totalCubes) + "%: " +
                        mesh + ". ~" +
                        Program.MemUsageText);

                    GC.Collect();
                }
            }
        }
Beispiel #2
0
        void generateSingle32BitMeshbuffer(int N)
        {
            Vertex3D[] vertices32bit;
            uint[]     indices32bit;
            generateVerticesAndIndices(N, out vertices32bit, out indices32bit);

            MeshBuffer mb = MeshBuffer.Create(VertexType.Standard, IndexType._32Bit);

            mesh.AddMeshBuffer(mb);
            mb.Drop();

            device.Logger.Log("Appending " +
                              vertices32bit.Length + " vertices and " +
                              indices32bit.Length + " indices to 32-bit meshbuffer...");

            mb.Append(vertices32bit, indices32bit);
            mb.SetHardwareMappingHint(HardwareMappingHint.Static, HardwareBufferType.VertexAndIndex);
        }