コード例 #1
0
        public DX11IndexedGeometry LoadFromMesh(Assimp.Mesh mesh, AssimpLoadInformation loadInfo, bool allowRawView = false)
        {
            uint[] inds = mesh.GetIndices();

            if (inds.Length > 0 && mesh.VertexCount > 0)
            {
                int vertexsize;
                var layout = mesh.InputLayout(loadInfo, out vertexsize);

                BoundingBox bb;
                DataStream  ds = mesh.LoadVertices(loadInfo, vertexsize, out bb);

                DX11IndexedGeometry geom = new DX11IndexedGeometry(device)
                {
                    HasBoundingBox = true,
                    BoundingBox    = bb,
                    IndexBuffer    = DX11IndexBuffer.CreateImmutable(device, inds, allowRawView),
                    InputLayout    = layout,
                    PrimitiveType  = "AssimpModel",
                    Tag            = null,
                    Topology       = SharpDX.Direct3D.PrimitiveTopology.TriangleList,
                    VertexBuffer   = DX11VertexBuffer.CreateImmutable(device, mesh.VertexCount, vertexsize, ds, allowRawView)
                };

                ds.Dispose();
                return(geom);
            }

            return(null);
        }
コード例 #2
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="context">Render context</param>
        /// <param name="vertexSize">Vertex size</param>
        /// <param name="vertexCount">Vertex Count</param>
        /// <param name="inputElements">Input elements</param>
        public StreamOutputBufferWithRawSupport(DX11RenderContext context, int vertexSize, int vertexCount, OutputDrawMode outputDrawMode, bool allowIndexBuffer, InputElement[] inputElements)
        {
            this.innerBuffer = BufferHelper.CreateStreamOutBuffer(context, vertexSize, vertexCount, true, allowIndexBuffer);

            //Copy a new Vertex buffer with stream out
            this.vertexGeometry           = new DX11VertexGeometry(context);
            vertexGeometry.HasBoundingBox = false;
            vertexGeometry.InputLayout    = inputElements;
            vertexGeometry.Topology       = PrimitiveTopology.TriangleList;
            vertexGeometry.VertexBuffer   = innerBuffer;
            vertexGeometry.VertexSize     = vertexSize;
            vertexGeometry.VerticesCount  = vertexCount;

            if (outputDrawMode == OutputDrawMode.Auto)
            {
                vertexGeometry.AssignDrawer(new DX11VertexAutoDrawer());
            }

            if (context.ComputeShaderSupport)
            {
                this.rawBuffer = new DX11RawBuffer(context, innerBuffer);
            }

            if (allowIndexBuffer)
            {
                this.indexBuffer = DX11IndexBuffer.FromReference(context, this.innerBuffer, vertexCount);
            }
        }
コード例 #3
0
        public void CreateImmutableShort()
        {
            short[]         ud  = new short[] { 0, 1, 2, 3, 4, 5, 6 };
            DX11IndexBuffer ibo = DX11IndexBuffer.CreateImmutable(Device, ud);

            Assert.IsNotNull(ibo.Buffer, "Buffer Is Null");
            ibo.Dispose();
        }
コード例 #4
0
        private DX11IndexedGeometry FromAppender(AbstractPrimitiveDescriptor descriptor, ListGeometryAppender appender, PrimitiveInfo info)
        {
            DX11IndexedGeometry geom = new DX11IndexedGeometry(device);

            geom.Tag            = descriptor;
            geom.PrimitiveType  = descriptor.PrimitiveType;
            geom.VertexBuffer   = DX11VertexBuffer.CreateImmutable(device, appender.Vertices.ToArray());
            geom.IndexBuffer    = DX11IndexBuffer.CreateImmutable(device, appender.Indices.ToArray());
            geom.InputLayout    = Pos4Norm3Tex2Vertex.Layout;
            geom.Topology       = PrimitiveTopology.TriangleList;
            geom.HasBoundingBox = info.IsBoundingBoxKnown;
            geom.BoundingBox    = info.BoundingBox;
            return(geom);
        }
コード例 #5
0
        public DX11IndexedGeometry RoundRect(RoundRect settings)
        {
            Vector2 inner = settings.InnerRadius;
            float   outer = settings.OuterRadius;
            int     ires  = settings.CornerResolution;

            DX11IndexedGeometry geom = new DX11IndexedGeometry(device);

            geom.PrimitiveType = settings.PrimitiveType;
            geom.Tag           = settings;
            List <Pos4Norm3Tex2Vertex> vl = new List <Pos4Norm3Tex2Vertex>();
            List <int> il = new List <int>();

            int idx = 0;

            float ucy = Convert.ToSingle(inner.Y + outer);
            float ucx = Convert.ToSingle(inner.X + outer);

            float mx = ucx * 2.0f;
            float my = ucy * 2.0f;

            //Need 1 quad for center
            if (settings.EnableCenter)
            {
                idx = SetQuad(vl, il, 0.0f, 0.0f, inner.X, inner.Y, idx, mx, my);
            }

            //Need 2 quads up/down
            idx = SetQuad(vl, il, 0.0f, ucy, inner.X, (float)outer, idx, mx, my);
            idx = SetQuad(vl, il, 0.0f, -ucy, inner.X, (float)outer, idx, mx, my);

            //Need 2 quads left/right
            idx = SetQuad(vl, il, -ucx, 0.0f, (float)outer, inner.Y, idx, mx, my);
            idx = SetQuad(vl, il, ucx, 0.0f, (float)outer, inner.Y, idx, mx, my);

            float radius = (float)outer * 2.0f;

            //Add the 4 corners
            idx = SetSegment(vl, il, inner.X, inner.Y, 0.0f, radius, ires, idx, mx, my);
            idx = SetSegment(vl, il, -inner.X, inner.Y, 0.25f, radius, ires, idx, mx, my);
            idx = SetSegment(vl, il, -inner.X, -inner.Y, 0.5f, radius, ires, idx, mx, my);
            idx = SetSegment(vl, il, inner.X, -inner.Y, 0.75f, radius, ires, idx, mx, my);

            geom.VertexBuffer   = DX11VertexBuffer.CreateImmutable <Pos4Norm3Tex2Vertex>(device, vl.ToArray());
            geom.IndexBuffer    = DX11IndexBuffer.CreateImmutable(device, il.ToArray());
            geom.InputLayout    = Pos4Norm3Tex2Vertex.Layout;
            geom.Topology       = PrimitiveTopology.TriangleList;
            geom.HasBoundingBox = false;
            return(geom);
        }
コード例 #6
0
        public void Update(IPluginIO pin, DX11RenderContext context)
        {
            if (this.ibo == null)
            {
                uint[] faceIndices = faceModel.TriangleIndices.ToArray();

                fixed(uint *uPtr = &faceIndices[0])
                {
                    DataStream ds = new DataStream(new IntPtr(uPtr), faceIndices.Length * 4, true, true);

                    this.ibo = new DX11IndexBuffer(context, ds, false, false);
                }

                this.FOutGeom[0][context]             = new DX11IndexOnlyGeometry(context);
                this.FOutGeom[0][context].IndexBuffer = this.ibo;
            }

            if (this.faceVertexBuffer == null)
            {
                this.faceVertexBuffer             = new DX11DynamicStructuredBuffer <Vector3>(context, (int)FaceModel.VertexCount);
                this.FOutFaceVertices[0][context] = this.faceVertexBuffer;

                this.faceUVBuffer           = new DX11DynamicStructuredBuffer <Vector2>(context, (int)FaceModel.VertexCount);
                this.FOutFaceUV[0][context] = this.faceUVBuffer;
            }

            if (this.FInvalidate)
            {
                fixed(CameraSpacePoint *cPtr = &this.cameraPoints[0])
                {
                    this.faceVertexBuffer.WriteData(new IntPtr(cPtr));
                }

                fixed(ColorSpacePoint *cPtr = &this.colorPoints[0])
                {
                    this.faceUVBuffer.WriteData(new IntPtr(cPtr));
                }

                this.FInvalidate = false;
            }
        }
コード例 #7
0
        private DX11IndexedGeometry QuadTextured()
        {
            DX11IndexedGeometry geom = new DX11IndexedGeometry(this.device);
            float sx = 1.0f;
            float sy = 1.0f;

            Pos4Tex2Vertex[] vertices = new Pos4Tex2Vertex[]
            {
                new Pos4Tex2Vertex()
                {
                    Position  = new Vector4(-sx, sy, 0.0f, 1.0f),
                    TexCoords = new Vector2(0, 0)
                },
                new Pos4Tex2Vertex()
                {
                    Position  = new Vector4(sx, sy, 0.0f, 1.0f),
                    TexCoords = new Vector2(1, 0)
                },
                new Pos4Tex2Vertex()
                {
                    Position  = new Vector4(-sx, -sy, 0.0f, 1.0f),
                    TexCoords = new Vector2(0, 1)
                },
                new Pos4Tex2Vertex()
                {
                    Position  = new Vector4(sx, -sy, 0.0f, 1.0f),
                    TexCoords = new Vector2(1, 1)
                },
            };
            int[] indices = new int[] { 0, 1, 3, 3, 2, 0 };

            geom.VertexBuffer             = DX11VertexBuffer.CreateImmutable(device, vertices);;
            geom.IndexBuffer              = DX11IndexBuffer.CreateImmutable(device, indices);
            geom.InputLayout              = Pos4Tex2Vertex.Layout;
            geom.VertexBuffer.InputLayout = geom.InputLayout;
            geom.Topology       = PrimitiveTopology.TriangleList;
            geom.HasBoundingBox = true;
            geom.BoundingBox    = new BoundingBox(new Vector3(-sx, -sy, 0.0f), new Vector3(sx, sy, 0.0f));

            return(geom);
        }
コード例 #8
0
        public DX11IndexedGeometry Box(Box settings)
        {
            DX11IndexedGeometry geom = new DX11IndexedGeometry(device);

            geom.Tag           = settings;
            geom.PrimitiveType = settings.PrimitiveType;

            DataStream vertexstream = new DataStream(24 * Pos4Norm3Tex2Vertex.VertexSize, true, true);

            vertexstream.Position = 0;

            var indexstream = new DataStream(36 * 4, true, true);

            float sx = 0.5f * settings.Size.X;
            float sy = 0.5f * settings.Size.Y;
            float sz = 0.5f * settings.Size.Z;

            Vector3 s3   = new Vector3(sx, sy, sz);
            Vector4 size = new Vector4(sx, sy, sz, 1.0f);

            this.WriteFrontFace(vertexstream, indexstream, size);
            this.WriteBackFace(vertexstream, indexstream, size);
            this.WriteRightFace(vertexstream, indexstream, size);
            this.WriteLeftFace(vertexstream, indexstream, size);
            this.WriteTopFace(vertexstream, indexstream, size);
            this.WriteBottomFace(vertexstream, indexstream, size);

            geom.VertexBuffer             = DX11VertexBuffer.CreateImmutable(device, 24, Pos4Norm3Tex2Vertex.VertexSize, vertexstream);
            geom.IndexBuffer              = DX11IndexBuffer.CreateImmutable(device, 36, indexstream, true);
            geom.InputLayout              = Pos4Norm3Tex2Vertex.Layout;
            geom.VertexBuffer.InputLayout = geom.InputLayout;
            geom.Topology       = PrimitiveTopology.TriangleList;
            geom.HasBoundingBox = true;
            geom.BoundingBox    = new BoundingBox(-s3, s3);

            vertexstream.Dispose();
            indexstream.Dispose();

            return(geom);
        }
コード例 #9
0
        public void StructuredCopyTest()
        {
            uint[]          ud  = new uint[] { 0, 1, 2, 3, 4, 5, 6 };
            DX11IndexBuffer ibo = DX11IndexBuffer.CreateImmutable(Device, ud);

            DX11StructuredBuffer sb      = ibo.CopyToStructuredBuffer(this.RenderContext);
            DX11StructuredBuffer staging = sb.AsStaging();

            this.RenderContext.Context.CopyResource(sb.Buffer, staging.Buffer);

            uint[] data = staging.ReadData <uint>(this.RenderContext);

            ibo.Dispose();
            sb.Dispose();
            staging.Dispose();

            Assert.AreEqual(ud.Length, data.Length, "Resources are or different Size");
            for (int i = 0; i < ud.Length; i++)
            {
                Assert.AreEqual(ud[i], data[i], "Data Mismatch");
            }
        }
コード例 #10
0
        public DX11IndexedGeometry Segment(Segment settings)
        {
            SegmentBuilder       builder  = new SegmentBuilder();
            ListGeometryAppender appender = new ListGeometryAppender();
            PrimitiveInfo        info     = builder.GetPrimitiveInfo(settings);

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

            builder.Construct(settings, (v, n, u) =>
                              { appender.AppendVertex(v, n, u); min = Vector3.Min(min, v); max = Vector3.Max(max, v); }, appender.AppendIndex);

            DX11IndexedGeometry geom = new DX11IndexedGeometry(device);

            geom.Tag            = settings;
            geom.PrimitiveType  = settings.PrimitiveType;
            geom.VertexBuffer   = DX11VertexBuffer.CreateImmutable(device, appender.Vertices.ToArray());
            geom.IndexBuffer    = DX11IndexBuffer.CreateImmutable(device, appender.Indices.ToArray());
            geom.InputLayout    = Pos4Norm3Tex2Vertex.Layout;
            geom.Topology       = PrimitiveTopology.TriangleList;
            geom.HasBoundingBox = true;
            geom.BoundingBox    = new BoundingBox(min, max);
            return(geom);
        }
コード例 #11
0
ファイル: KinectFusionNode.cs プロジェクト: kratter/dx11-vvvv
        public void Update(DX11RenderContext context)
        {
            if (!this.FTextureOutput[0].Contains(context))
            {
                this.FTextureOutput[0][context] = new DX11DynamicTexture2D(context, this.width, this.height, SlimDX.DXGI.Format.R8G8B8A8_UNorm);
                this.FPCOut[0][context]         = new DX11DynamicStructuredBuffer <float>(context, 640 * 480 * 6);
            }

            if (this.FInvalidate)
            {
                fixed(int *f = &this.pic[0])
                {
                    IntPtr ptr = new IntPtr(f);

                    this.FTextureOutput[0][context].WriteData(ptr, this.width * this.height * 4);
                }

                /*fixed (float* f = &this.piccloud[0])
                 * {*
                 *  IntPtr ptr = new IntPtr(f);*/

                DX11DynamicStructuredBuffer <float> db = (DX11DynamicStructuredBuffer <float>) this.FPCOut[0][context];

                db.WriteData(this.piccloud);
                //}

                this.FInvalidate = false;
            }

            if (this.FInVoxels[0])
            {
                if (this.FOutVoxels[0].Contains(context))
                {
                    this.FOutVoxels[0].Dispose(context);
                }

                short[] data = new short[this.VoxelResolutionX * this.VoxelResolutionY * this.VoxelResolutionZ];

                this.volume.ExportVolumeBlock(0, 0, 0, this.VoxelResolutionX, this.VoxelResolutionY, this.VoxelResolutionZ, 1, data);

                DX11DynamicStructuredBuffer <int> b = new DX11DynamicStructuredBuffer <int>(context, this.VoxelResolutionX * this.VoxelResolutionY * this.VoxelResolutionZ);

                int[] idata = new int[this.VoxelResolutionX * this.VoxelResolutionY * this.VoxelResolutionZ];

                for (int i = 0; i < this.VoxelResolutionX * this.VoxelResolutionY * this.VoxelResolutionZ; i++)
                {
                    idata[i] = data[i];
                }

                b.WriteData(idata);

                this.FOutVoxels[0][context] = b;
            }

            if (this.FInExport[0])
            {
                if (this.FGeomOut[0].Contains(context))
                {
                    this.FGeomOut[0].Dispose(context);
                }

                if (this.volume != null)
                {
                    Mesh m = this.volume.CalculateMesh(this.FInGeomVoxelStep[0]);

                    DX11IndexedGeometry geom = new DX11IndexedGeometry(context);

                    ReadOnlyCollection <int> inds = m.GetTriangleIndexes();

                    DataStream ds = new DataStream(inds.Count * 4, true, true);
                    ds.WriteRange <int>(inds.ToArray());
                    ds.Position = 0;

                    DX11IndexBuffer ibo = new DX11IndexBuffer(context, ds, false, true);

                    ReadOnlyCollection <Microsoft.Kinect.Fusion.Vector3> pos  = m.GetVertices();
                    ReadOnlyCollection <Microsoft.Kinect.Fusion.Vector3> norm = m.GetNormals();
                    //ReadOnlyCollection<int> col = m.GetColors();

                    DataStream dsv = new DataStream(Pos3Norm3Vertex.VertexSize * pos.Count, true, true);

                    SlimDX.Vector3 bmin = new SlimDX.Vector3(float.MaxValue, float.MaxValue, float.MaxValue);
                    SlimDX.Vector3 bmax = new SlimDX.Vector3(float.MinValue, float.MinValue, float.MinValue);

                    for (int i = 0; i < pos.Count; i++)
                    {
                        Microsoft.Kinect.Fusion.Vector3 p = pos[i];
                        Microsoft.Kinect.Fusion.Vector3 n = norm[i];

                        dsv.Write <Microsoft.Kinect.Fusion.Vector3>(p);
                        dsv.Write <Microsoft.Kinect.Fusion.Vector3>(n);
                        //dsv.Write<int>(col[i]);

                        if (p.X < bmin.X)
                        {
                            bmin.X = p.X;
                        }
                        if (p.Y < bmin.Y)
                        {
                            bmin.Y = p.Y;
                        }
                        if (p.Z < bmin.Z)
                        {
                            bmin.Z = p.Z;
                        }

                        if (p.X > bmax.X)
                        {
                            bmax.X = p.X;
                        }
                        if (p.Y > bmax.Y)
                        {
                            bmax.Y = p.Y;
                        }
                        if (p.Z > bmax.Z)
                        {
                            bmax.Z = p.Z;
                        }
                    }

                    geom.IndexBuffer    = ibo;
                    geom.HasBoundingBox = true;
                    geom.InputLayout    = Pos3Norm3Vertex.Layout;     // FusionColoredVertex.Layout;
                    geom.Topology       = SlimDX.Direct3D11.PrimitiveTopology.TriangleList;
                    geom.VertexSize     = Pos3Norm3Vertex.VertexSize; // FusionColoredVertex.VertexSize;
                    geom.VertexBuffer   = BufferHelper.CreateVertexBuffer(context, dsv, false, true);
                    geom.VerticesCount  = pos.Count;
                    geom.BoundingBox    = new BoundingBox(bmin, bmax);


                    this.FGeomOut[0][context] = geom;

                    m.Dispose();
                }
            }
        }
コード例 #12
0
        public void Update(IPluginIO pin, DX11RenderContext context)
        {
            for (int i = 0; i < this.FOutput.SliceCount; i++)
            {
                if (this.FOutput[i].Contains(context))
                {
                    this.FOutput[i].Dispose(context);
                }
            }

            if (this.FBodies.SliceCount > 0)
            {
                int cnt = this.FBodies.SliceCount;

                for (int i = 0; i < cnt; i++)
                {
                    SoftBody body = this.FBodies[i];

                    SoftBodyCustomData sc = (SoftBodyCustomData)body.UserObject;

                    AlignedFaceArray faces = body.Faces;

                    if (FValid[i])
                    {
                        if (body.Faces.Count > 0)
                        {
                            #region Build from Faces
                            DX11IndexedGeometry geom = new DX11IndexedGeometry(context);

                            geom.VerticesCount = faces.Count * 3;


                            if (sc.HasUV)
                            {
                                geom.InputLayout = Pos3Norm3Tex2Vertex.Layout;
                                geom.VertexSize  = Pos3Norm3Tex2Vertex.VertexSize;
                            }
                            else
                            {
                                geom.InputLayout = Pos3Norm3Vertex.Layout;
                                geom.VertexSize  = Pos3Norm3Vertex.VertexSize;
                            }

                            //Mesh mesh = new Mesh(OnDevice, faces.Count, faces.Count * 3, MeshFlags.SystemMemory | MeshFlags.Use32Bit, decl);

                            SlimDX.DataStream verts   = new SlimDX.DataStream(geom.VerticesCount * geom.VertexSize * 3, false, true);
                            SlimDX.DataStream indices = new SlimDX.DataStream(faces.Count * sizeof(int) * 3, false, true);

                            int j;
                            int uvcnt = 0;
                            for (j = 0; j < faces.Count; j++)
                            {
                                NodePtrArray nodes = faces[j].N;
                                verts.Write(nodes[0].X);
                                verts.Write(nodes[0].Normal);
                                //verts.Position += 12;

                                if (sc.HasUV)
                                {
                                    verts.Write(sc.UV[uvcnt]);
                                    uvcnt++;
                                    verts.Write(sc.UV[uvcnt]);
                                    uvcnt++;
                                }

                                verts.Write(nodes[1].X);
                                verts.Write(nodes[1].Normal);

                                //verts.Position += 12;

                                if (sc.HasUV)
                                {
                                    verts.Write(sc.UV[uvcnt]);
                                    uvcnt++;
                                    verts.Write(sc.UV[uvcnt]);
                                    uvcnt++;
                                }

                                verts.Write(nodes[2].X);
                                verts.Write(nodes[2].Normal);
                                //verts.Position += 12;

                                if (sc.HasUV)
                                {
                                    verts.Write(sc.UV[uvcnt]);
                                    uvcnt++;
                                    verts.Write(sc.UV[uvcnt]);
                                    uvcnt++;
                                }

                                indices.Write(j * 3);
                                indices.Write(j * 3 + 1);
                                indices.Write(j * 3 + 2);
                            }

                            geom.VertexBuffer = BufferHelper.CreateVertexBuffer(context, verts, false, true);

                            geom.HasBoundingBox = false;

                            DX11IndexBuffer ibo = new DX11IndexBuffer(context, indices, false, true);
                            geom.IndexBuffer         = ibo;
                            this.FOutput[i][context] = geom;
                            #endregion
                        }
                    }
                }
            }
        }
コード例 #13
0
        public DX11IndexedGeometry SegmentZ(SegmentZ settings)
        {
            int   res    = settings.Resolution;
            float cycles = settings.Cycles;
            float phase  = settings.Phase;
            float inner  = settings.InnerRadius;
            float z      = settings.Z;

            DX11IndexedGeometry geom = new DX11IndexedGeometry(device);

            geom.Tag           = settings;
            geom.PrimitiveType = settings.PrimitiveType;

            int vcount = res * 2;
            int icount = (res - 1) * 6;

            float inc = Convert.ToSingle((Math.PI * 2.0 * cycles) / (res - 1.0));
            float phi = Convert.ToSingle(phase * (Math.PI * 2.0));

            List <Pos4Norm3Tex2Vertex> vlist = new List <Pos4Norm3Tex2Vertex>();
            List <int> ilist = new List <int>();

            Pos4Norm3Tex2Vertex innerv = new Pos4Norm3Tex2Vertex();

            innerv.Normals = new Vector3(0.0f, 0.0f, 1.0f);

            Pos4Norm3Tex2Vertex outerv = new Pos4Norm3Tex2Vertex();

            outerv.Normals = new Vector3(0.0f, 0.0f, 1.0f);

            Pos4Norm3Tex2Vertex[] vertices = new Pos4Norm3Tex2Vertex[res * 2];

            #region Append front face
            for (int i = 0; i < res; i++)
            {
                float x = Convert.ToSingle(0.5 * inner * Math.Cos(phi));
                float y = Convert.ToSingle(0.5 * inner * Math.Sin(phi));

                innerv.Position = new Vector4(x, y, z, 1.0f);

                x = Convert.ToSingle(0.5 * Math.Cos(phi));
                y = Convert.ToSingle(0.5 * Math.Sin(phi));

                outerv.Position = new Vector4(x, y, z, 1.0f);

                vertices[i]       = innerv;
                vertices[i + res] = outerv;
                phi += inc;
            }

            int   indstep = 0;
            int[] indices = new int[icount];
            for (int i = 0; i < res - 1; i++)
            {
                //Triangle from low to high
                indices[indstep]     = i;
                indices[indstep + 1] = res + i;
                indices[indstep + 2] = i + 1;


                //Triangle from high to low
                indices[indstep + 3] = i + 1;
                indices[indstep + 4] = res + i;
                indices[indstep + 5] = res + i + 1;

                indstep += 6;
            }

            vlist.AddRange(vertices);
            ilist.AddRange(indices);
            #endregion

            #region Append Back Face
            //Second layer just has Z inverted
            for (int i = 0; i < res * 2; i++)
            {
                vertices[i].Position.Z = -vertices[i].Position.Z;
                vertices[i].Normals.Z  = -vertices[i].Normals.Z;
                phi += inc;
            }

            //Here we also flip triangles for cull
            indstep = 0;
            int offset = res * 2;
            for (int i = offset; i < offset + res - 1; i++)
            {
                //Triangle from low to high
                indices[indstep]     = i;
                indices[indstep + 2] = res + i;
                indices[indstep + 1] = i + 1;


                //Triangle from high to low
                indices[indstep + 3] = i + 1;
                indices[indstep + 5] = res + i;
                indices[indstep + 4] = res + i + 1;

                indstep += 6;
            }

            vlist.AddRange(vertices);
            ilist.AddRange(indices);
            #endregion


            //We need to append new set of indices, as we want nice normals
            #region Append Outer
            phi = Convert.ToSingle(phase * (Math.PI * 2.0));
            for (int i = 0; i < res; i++)
            {
                float x = Convert.ToSingle(0.5 * Math.Cos(phi));
                float y = Convert.ToSingle(0.5 * Math.Sin(phi));

                innerv.Position = new Vector4(x, y, z, 1.0f);
                outerv.Position = new Vector4(x, y, -z, 1.0f);
                innerv.Normals  = Vector3.Normalize(new Vector3(innerv.Position.X, innerv.Position.Y, 0.0f));
                outerv.Normals  = Vector3.Normalize(new Vector3(innerv.Position.X, innerv.Position.Y, 0.0f));

                vertices[i]       = innerv;
                vertices[i + res] = outerv;
                phi += inc;
            }

            indstep = 0;
            offset += (res * 2);
            for (int i = offset; i < offset + res - 1; i++)
            {
                //Triangle from low to high
                indices[indstep]     = i;
                indices[indstep + 1] = res + i;
                indices[indstep + 2] = i + 1;


                //Triangle from high to low
                indices[indstep + 3] = i + 1;
                indices[indstep + 4] = res + i;
                indices[indstep + 5] = res + i + 1;

                indstep += 6;
            }

            vlist.AddRange(vertices);
            ilist.AddRange(indices);
            #endregion

            #region Append Inner
            phi = Convert.ToSingle(phase * (Math.PI * 2.0));
            for (int i = 0; i < res; i++)
            {
                float x = Convert.ToSingle(0.5 * inner * Math.Cos(phi));
                float y = Convert.ToSingle(0.5 * inner * Math.Sin(phi));

                innerv.Position = new Vector4(x, y, z, 1.0f);
                outerv.Position = new Vector4(x, y, -z, 1.0f);
                innerv.Normals  = -Vector3.Normalize(new Vector3(innerv.Position.X, innerv.Position.Y, 0.0f));
                outerv.Normals  = -Vector3.Normalize(new Vector3(innerv.Position.X, innerv.Position.Y, 0.0f));

                vertices[i]       = innerv;
                vertices[i + res] = outerv;
                phi += inc;
            }

            indstep = 0;
            offset += (res * 2);
            for (int i = offset; i < offset + res - 1; i++)
            {
                //Triangle from low to high
                indices[indstep]     = i;
                indices[indstep + 2] = res + i;
                indices[indstep + 1] = i + 1;


                //Triangle from high to low
                indices[indstep + 3] = i + 1;
                indices[indstep + 5] = res + i;
                indices[indstep + 4] = res + i + 1;

                indstep += 6;
            }

            vlist.AddRange(vertices);
            ilist.AddRange(indices);
            #endregion

            #region Append Border

            //Append border low (quad)
            phi = Convert.ToSingle(phase * (Math.PI * 2.0));
            float x2 = Convert.ToSingle(0.5 * inner * Math.Cos(phi));
            float y2 = Convert.ToSingle(0.5 * inner * Math.Sin(phi));

            float x3 = Convert.ToSingle(0.5 * Math.Cos(phi));
            float y3 = Convert.ToSingle(0.5 * Math.Sin(phi));

            Pos4Norm3Tex2Vertex q1 = new Pos4Norm3Tex2Vertex();
            Pos4Norm3Tex2Vertex q2 = new Pos4Norm3Tex2Vertex();
            Pos4Norm3Tex2Vertex q3 = new Pos4Norm3Tex2Vertex();
            Pos4Norm3Tex2Vertex q4 = new Pos4Norm3Tex2Vertex();

            q1.Position = new Vector4(x2, y2, z, 1.0f);
            q2.Position = new Vector4(x2, y2, -z, 1.0f);
            q3.Position = new Vector4(x3, y3, z, 1.0f);
            q4.Position = new Vector4(x3, y3, -z, 1.0f);

            Vector3 e1 = new Vector3(q2.Position.X - q1.Position.X,
                                     q2.Position.Y - q1.Position.Y,
                                     q2.Position.Z - q1.Position.Z);

            Vector3 e2 = new Vector3(q3.Position.X - q2.Position.X,
                                     q3.Position.Y - q2.Position.Y,
                                     q3.Position.Z - q2.Position.Z);

            Vector3 n = Vector3.Cross(e1, e2);
            q1.Normals = n;
            q2.Normals = n;
            q3.Normals = n;
            q4.Normals = n;

            vlist.Add(q1); vlist.Add(q2); vlist.Add(q3); vlist.Add(q4);

            offset += (res * 2);
            ilist.Add(offset); ilist.Add(offset + 1); ilist.Add(offset + 2);
            ilist.Add(offset + 2); ilist.Add(offset + 1); ilist.Add(offset + 3);


            offset += 4;

            //Totally crapply unoptimized, but phi can be negative
            phi = Convert.ToSingle(phase * (Math.PI * 2.0));
            for (int i = 0; i < res - 1; i++)
            {
                phi += inc;
            }

            x2 = Convert.ToSingle(0.5 * inner * Math.Cos(phi));
            y2 = Convert.ToSingle(0.5 * inner * Math.Sin(phi));

            x3 = Convert.ToSingle(0.5 * Math.Cos(phi));
            y3 = Convert.ToSingle(0.5 * Math.Sin(phi));

            q1.Position = new Vector4(x2, y2, z, 1.0f);
            q2.Position = new Vector4(x2, y2, -z, 1.0f);
            q3.Position = new Vector4(x3, y3, z, 1.0f);
            q4.Position = new Vector4(x3, y3, -z, 1.0f);

            e1 = new Vector3(q2.Position.X - q1.Position.X,
                             q2.Position.Y - q1.Position.Y,
                             q2.Position.Z - q1.Position.Z);

            e2 = new Vector3(q3.Position.X - q2.Position.X,
                             q3.Position.Y - q2.Position.Y,
                             q3.Position.Z - q2.Position.Z);

            n          = Vector3.Cross(e2, e1);
            q1.Normals = n;
            q2.Normals = n;
            q3.Normals = n;
            q4.Normals = n;

            vlist.Add(q1); vlist.Add(q2); vlist.Add(q3); vlist.Add(q4);

            ilist.Add(offset); ilist.Add(offset + 2); ilist.Add(offset + 1);
            ilist.Add(offset + 2); ilist.Add(offset + 3); ilist.Add(offset + 1);



            #endregion

            float minx = float.MaxValue, miny = float.MaxValue, minz = float.MaxValue;
            float maxx = float.MinValue, maxy = float.MinValue, maxz = float.MinValue;

            foreach (Pos4Norm3Tex2Vertex v in vlist)
            {
                minx = v.Position.X < minx ? v.Position.X : minx;
                miny = v.Position.Y < miny ? v.Position.Y : miny;
                minz = v.Position.Z < minz ? v.Position.Z : minz;

                maxx = v.Position.X > maxx ? v.Position.X : maxx;
                maxy = v.Position.Y > maxy ? v.Position.Y : maxy;
                maxz = v.Position.Z > maxz ? v.Position.Z : maxz;
            }

            geom.VertexBuffer = DX11VertexBuffer.CreateImmutable <Pos4Norm3Tex2Vertex>(device, vlist.ToArray());
            geom.IndexBuffer  = DX11IndexBuffer.CreateImmutable(device, ilist.ToArray());
            geom.InputLayout  = Pos4Norm3Tex2Vertex.Layout;
            geom.Topology     = PrimitiveTopology.TriangleList;

            geom.HasBoundingBox = true;
            geom.BoundingBox    = new BoundingBox(new Vector3(minx, miny, minz), new Vector3(maxx, maxy, maxz));

            return(geom);
        }
コード例 #14
0
        public DX11IndexedGeometry Polygon2d(Polygon2d settings)
        {
            DX11IndexedGeometry geom = new DX11IndexedGeometry(device);

            geom.Tag           = settings;
            geom.PrimitiveType = settings.PrimitiveType;

            int count = settings.Vertices.Length;

            Pos4Norm3Tex2Vertex[] verts = new Pos4Norm3Tex2Vertex[count + 1];

            float cx = 0;
            float cy = 0;
            float x = 0, y = 0;

            float minx = float.MaxValue, miny = float.MaxValue;
            float maxx = float.MinValue, maxy = float.MinValue;

            for (int j = 0; j < count; j++)
            {
                verts[j + 1].Position  = new Vector4(settings.Vertices[j].X, settings.Vertices[j].Y, 0.0f, 1.0f);
                verts[j + 1].Normals   = new Vector3(0, 0, 1);
                verts[j + 1].TexCoords = new Vector2(0.0f, 0.0f);
                cx += x;
                cy += y;

                if (x < minx)
                {
                    minx = x;
                }
                if (x > maxx)
                {
                    maxx = x;
                }
                if (y < miny)
                {
                    miny = y;
                }
                if (y > maxy)
                {
                    maxy = y;
                }
            }

            verts[0].Position  = new Vector4(cx / (float)count, cy / (float)count, 0.0f, 1.0f);
            verts[0].Normals   = new Vector3(0, 0, 1);
            verts[0].TexCoords = new Vector2(0.5f, 0.5f);

            float w = maxx - minx;
            float h = maxy - miny;

            for (int j = 0; j < count; j++)
            {
                verts[0].TexCoords = new Vector2((verts[j + 1].Position.X - minx) / w, (verts[j + 1].Position.Y - miny) / h);
            }

            List <int> inds = new List <int>();

            for (int j = 0; j < count - 1; j++)
            {
                inds.Add(0);
                inds.Add(j + 1);
                inds.Add(j + 2);
            }

            inds.Add(0);
            inds.Add(verts.Length - 1);
            inds.Add(1);

            geom.VertexBuffer = DX11VertexBuffer.CreateImmutable(device, verts);
            geom.IndexBuffer  = DX11IndexBuffer.CreateImmutable(device, inds.ToArray());
            geom.InputLayout  = Pos4Norm3Tex2Vertex.Layout;
            geom.Topology     = PrimitiveTopology.TriangleList;


            geom.HasBoundingBox = true;
            geom.BoundingBox    = new BoundingBox()
            {
                Minimum = new Vector3(minx, miny, 0.0f),
                Maximum = new Vector3(maxx, maxy, 0.0f)
            };

            return(geom);
        }