Exemplo n.º 1
0
        Mesh CreateShape(CollisionShape shape)
        {
            uint[] indices;
            BulletSharp.Vector3[] vertices = CreateShape(shape, out indices);

            int  vertexCount = vertices.Length / 2;
            int  indexCount  = (indices != null) ? indices.Length : vertexCount;
            bool index32     = indexCount > 65536;

            Mesh mesh = new Mesh(device, indexCount / 3, vertexCount,
                                 MeshFlags.SystemMemory | (index32 ? MeshFlags.Use32Bit : 0), VertexFormat.Position | VertexFormat.Normal);

            DataStream vertexBuffer = mesh.LockVertexBuffer(LockFlags.Discard);

            vertexBuffer.WriteRange(vertices);
            mesh.UnlockVertexBuffer();

            DataStream indexBuffer = mesh.LockIndexBuffer(LockFlags.Discard);

            if (index32)
            {
                if (indices == null)
                {
                    indices = new uint[indexCount];
                    uint i = 0;
                    while (i < indexCount)
                    {
                        indices[i] = i;
                        i++;
                    }
                }
                indexBuffer.WriteRange(indices);
            }
            else
            {
                ushort[] indices_s;
                if (indices == null)
                {
                    indices_s = new ushort[indexCount];
                    ushort i = 0;
                    while (i < indexCount)
                    {
                        indices_s[i] = i;
                        i++;
                    }
                }
                else
                {
                    indices_s = CompactIndexBuffer(indices);
                }
                indexBuffer.WriteRange(indices_s);
            }
            mesh.UnlockIndexBuffer();

            shapes.Add(shape, mesh);
            return(mesh);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Converts mesh to DX-buffers.
        /// </summary>
        private static void MeshToBuffers(Device device,
                                          Mesh mesh,
                                          out SlimDX.Direct3D11.Buffer vertices,
                                          out SlimDX.Direct3D11.Buffer indices)
        {
            SlimDX.DataStream dsVertices = new SlimDX.DataStream(mesh.Vertices.Length * 6 * sizeof(float), true, true);
            SlimDX.DataStream dsIndices  = new SlimDX.DataStream(mesh.Indices.Length * 3 * sizeof(int), true, true);
            vertices = null;
            indices  = null;

            try
            {
                foreach (Vertex v in mesh.Vertices)
                {
                    v.WriteTo(dsVertices);
                }
                dsVertices.Position = 0;
                BufferDescription bdVertices = new BufferDescription();
                bdVertices.BindFlags      = BindFlags.VertexBuffer;
                bdVertices.CpuAccessFlags = CpuAccessFlags.None;
                bdVertices.OptionFlags    = ResourceOptionFlags.None;
                bdVertices.SizeInBytes    = mesh.Vertices.Length * 6 * sizeof(float);
                bdVertices.Usage          = ResourceUsage.Default;
                vertices = new SlimDX.Direct3D11.Buffer(device, dsVertices, bdVertices);

                dsIndices.WriteRange(mesh.Indices);
                dsIndices.Position = 0;
                BufferDescription bdIndices = new BufferDescription();
                bdIndices.BindFlags      = BindFlags.IndexBuffer;
                bdIndices.CpuAccessFlags = CpuAccessFlags.None;
                bdIndices.OptionFlags    = ResourceOptionFlags.None;
                bdIndices.SizeInBytes    = mesh.Indices.Length * 3 * sizeof(int);
                bdIndices.Usage          = ResourceUsage.Default;
                indices = new SlimDX.Direct3D11.Buffer(device, dsIndices, bdIndices);
            }
            catch (Exception)
            {
                if (vertices != null)
                {
                    vertices.Dispose();
                    vertices = null;
                }
                if (indices != null)
                {
                    indices.Dispose();
                    indices = null;
                }
                throw;
            }
            finally
            {
                dsVertices.Dispose();
                dsIndices.Dispose();
            }
        }
Exemplo n.º 3
0
        protected override DX11VertexGeometry GetGeom(DX11RenderContext device, int slice)
        {
            if (d2dFactory == null)
            {
                d2dFactory = new D2DFactory();
                dwFactory = new DWriteFactory(SharpDX.DirectWrite.FactoryType.Shared);
            }

            TextFormat fmt = new TextFormat(dwFactory, this.FFontInput[slice].Name, FFontSize[slice]);

            TextLayout tl = new TextLayout(dwFactory, FText[slice], fmt, 0.0f, 32.0f);
            tl.WordWrapping = WordWrapping.NoWrap;
            tl.TextAlignment = FHAlignment[slice];
            tl.ParagraphAlignment = FVAlignment[slice];

            OutlineRenderer renderer = new OutlineRenderer(d2dFactory);
            Extruder ex = new Extruder(d2dFactory);

            tl.Draw(renderer, 0.0f, 0.0f);

            var result = ex.GetVertices(renderer.GetGeometry(), this.FExtrude[slice]);

            Vector3 min = new Vector3(float.MaxValue);
            Vector3 max = new Vector3(float.MinValue);

            result.ForEach(pn =>
            {
                min.X = pn.Position.X < min.X ? pn.Position.X : min.X;
                min.Y = pn.Position.Y < min.Y ? pn.Position.Y : min.Y;
                min.Z = pn.Position.Z < min.Z ? pn.Position.Z : min.Z;

                max.X = pn.Position.X > max.X ? pn.Position.X : max.X;
                max.Y = pn.Position.Y > max.Y ? pn.Position.Y : max.Y;
                max.Z = pn.Position.Z > max.Z ? pn.Position.Z : max.Z;
            });

            SlimDX.DataStream ds = new SlimDX.DataStream(result.Count * Pos3Norm3VertexSDX.VertexSize, true, true);
            ds.Position = 0;

            ds.WriteRange(result.ToArray());

            ds.Position = 0;

            var vbuffer = new SlimDX.Direct3D11.Buffer(device.Device, ds, new SlimDX.Direct3D11.BufferDescription()
            {
                BindFlags = SlimDX.Direct3D11.BindFlags.VertexBuffer,
                CpuAccessFlags = SlimDX.Direct3D11.CpuAccessFlags.None,
                OptionFlags = SlimDX.Direct3D11.ResourceOptionFlags.None,
                SizeInBytes = (int)ds.Length,
                Usage = SlimDX.Direct3D11.ResourceUsage.Default
            });

            ds.Dispose();

            DX11VertexGeometry vg = new DX11VertexGeometry(device);
            vg.InputLayout = Pos3Norm3VertexSDX.Layout;
            vg.Topology = SlimDX.Direct3D11.PrimitiveTopology.TriangleList;
            vg.VertexBuffer = vbuffer;
            vg.VertexSize = Pos3Norm3VertexSDX.VertexSize;
            vg.VerticesCount = result.Count;
            vg.HasBoundingBox = true;
            vg.BoundingBox = new SlimDX.BoundingBox(new SlimDX.Vector3(min.X, min.Y, min.Z), new SlimDX.Vector3(max.X, max.Y, max.Z));

            return vg;
        }