Beispiel #1
0
        private int SetQuad(List <Pos4Norm3Tex2Vertex> verts, List <int> inds, float cx, float cy, float sx, float sy, int lastindex, float mx, float my)
        {
            Pos4Norm3Tex2Vertex innerv = new Pos4Norm3Tex2Vertex();

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

            innerv.Position = new Vector4(cx - sx, cy - sy, 0.0f, 1.0f);
            innerv          = TexCoord(innerv, mx, my);
            verts.Add(innerv);

            innerv.Position = new Vector4(cx + sx, cy + sy, 0.0f, 1.0f);
            innerv          = TexCoord(innerv, mx, my);
            verts.Add(innerv);

            innerv.Position = new Vector4(cx - sx, cy + sy, 0.0f, 1.0f);
            innerv          = TexCoord(innerv, mx, my);
            verts.Add(innerv);

            innerv.Position = new Vector4(cx + sx, cy - sy, 0.0f, 1.0f);
            innerv          = TexCoord(innerv, mx, my);
            verts.Add(innerv);

            int[] idxarray = new int[] { 0, 2, 1, 0, 1, 3 };
            for (int i = 0; i < idxarray.Length; i++)
            {
                idxarray[i] += lastindex;
            }
            inds.AddRange(idxarray);

            return(lastindex + 4);
        }
        private int SetQuad(List<Pos4Norm3Tex2Vertex> verts, List<int> inds, float cx, float cy, float sx, float sy, int lastindex, float mx, float my)
        {
            Pos4Norm3Tex2Vertex innerv = new Pos4Norm3Tex2Vertex();
            innerv.Normals = new Vector3(0.0f, 0.0f, 1.0f);

            innerv.Position = new Vector4(cx - sx, cy - sy, 0.0f, 1.0f);
            innerv = TexCoord(innerv, mx, my);
            verts.Add(innerv);

            innerv.Position = new Vector4(cx + sx, cy + sy, 0.0f, 1.0f);
            innerv = TexCoord(innerv, mx, my);
            verts.Add(innerv);

            innerv.Position = new Vector4(cx - sx, cy + sy, 0.0f, 1.0f);
            innerv = TexCoord(innerv, mx, my);
            verts.Add(innerv);

            innerv.Position = new Vector4(cx + sx, cy - sy, 0.0f, 1.0f);
            innerv = TexCoord(innerv, mx, my);
            verts.Add(innerv);

            int[] idxarray = new int[] { 0, 2, 1, 0, 1, 3 };
            for (int i = 0; i < idxarray.Length; i++) { idxarray[i] += lastindex; }
            inds.AddRange(idxarray);

            return lastindex + 4;
        }
 public void TransformVertices(Func <Pos4Norm3Tex2Vertex, Pos4Norm3Tex2Vertex> transformFunction)
 {
     for (int i = 0; i < this.vertices.Count; i++)
     {
         Pos4Norm3Tex2Vertex v = vertices[i];
         vertices[i] = transformFunction(v);
     }
 }
Beispiel #4
0
        public DX11IndexedGeometry QuadNormals(Quad settings)
        {
            DX11IndexedGeometry geom = new DX11IndexedGeometry(context);

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

            Vector2 size = settings.Size;

            DataStream ds = new DataStream(4 * Pos4Norm3Tex2Vertex.VertexSize, true, true);

            ds.Position = 0;

            float sx = 0.5f * size.X;
            float sy = 0.5f * size.Y;

            Pos4Norm3Tex2Vertex v = new Pos4Norm3Tex2Vertex();

            v.Position  = new Vector4(-sx, sy, 0.0f, 1.0f);
            v.Normals   = new Vector3(0, 0, 1);
            v.TexCoords = new Vector2(0, 0);
            ds.Write <Pos4Norm3Tex2Vertex>(v);

            v.Position  = new Vector4(sx, sy, 0.0f, 1.0f);
            v.TexCoords = new Vector2(1, 0);
            ds.Write <Pos4Norm3Tex2Vertex>(v);

            v.Position  = new Vector4(-sx, -sy, 0.0f, 1.0f);
            v.TexCoords = new Vector2(0, 1);
            ds.Write <Pos4Norm3Tex2Vertex>(v);

            v.Position  = new Vector4(sx, -sy, 0.0f, 1.0f);
            v.TexCoords = new Vector2(1, 1);
            ds.Write <Pos4Norm3Tex2Vertex>(v);

            var vertices = BufferHelper.CreateVertexBuffer(context, ds, true);

            var indexstream = new DataStream(24, true, true);

            indexstream.WriteRange(new int[] { 0, 1, 3, 3, 2, 0 });


            geom.VertexBuffer  = vertices;
            geom.IndexBuffer   = new DX11IndexBuffer(context, indexstream, false, true);
            geom.InputLayout   = Pos4Norm3Tex2Vertex.Layout;
            geom.Topology      = PrimitiveTopology.TriangleList;
            geom.VerticesCount = 4;
            geom.VertexSize    = Pos4Norm3Tex2Vertex.VertexSize;

            geom.HasBoundingBox = true;
            geom.BoundingBox    = new BoundingBox(new Vector3(-sx, -sy, 0.0f), new Vector3(sx, sy, 0.0f));

            return(geom);
        }
        public void AppendVertex(Vector3 position, Vector3 normal, Vector2 uv)
        {
            Pos4Norm3Tex2Vertex v = new Pos4Norm3Tex2Vertex()
            {
                Position  = new Vector4(position.X, position.Y, position.Z, 1.0f),
                Normals   = normal,
                TexCoords = uv
            };

            this.vertices.Add(v);
        }
Beispiel #6
0
        public DX11IndexedGeometry QuadNormals(Quad settings)
        {
            DX11IndexedGeometry geom = new DX11IndexedGeometry(context);
            geom.Tag = settings;
            geom.PrimitiveType = settings.PrimitiveType;

            Vector2 size = settings.Size;

            DataStream ds = new DataStream(4 * Pos4Norm3Tex2Vertex.VertexSize, true, true);
            ds.Position = 0;

            float sx = 0.5f * size.X;
            float sy = 0.5f * size.Y;

            Pos4Norm3Tex2Vertex v = new Pos4Norm3Tex2Vertex();
            v.Position = new Vector4(-sx, sy, 0.0f, 1.0f);
            v.Normals = new Vector3(0, 0, 1);
            v.TexCoords = new Vector2(0, 0);
            ds.Write<Pos4Norm3Tex2Vertex>(v);

            v.Position = new Vector4(sx, sy, 0.0f, 1.0f);
            v.TexCoords = new Vector2(1, 0);
            ds.Write<Pos4Norm3Tex2Vertex>(v);

            v.Position = new Vector4(-sx, -sy, 0.0f, 1.0f);
            v.TexCoords = new Vector2(0, 1);
            ds.Write<Pos4Norm3Tex2Vertex>(v);

            v.Position = new Vector4(sx, -sy, 0.0f, 1.0f);
            v.TexCoords = new Vector2(1, 1);
            ds.Write<Pos4Norm3Tex2Vertex>(v);

            var vertices = BufferHelper.CreateVertexBuffer(context, ds, true);

            var indexstream = new DataStream(24, true, true);
            indexstream.WriteRange(new int[] { 0, 1, 3, 3, 2, 0 });


            geom.VertexBuffer = vertices;
            geom.IndexBuffer = new DX11IndexBuffer(context, indexstream, false, true);
            geom.InputLayout = Pos4Norm3Tex2Vertex.Layout;
            geom.Topology = PrimitiveTopology.TriangleList;
            geom.VerticesCount = 4;
            geom.VertexSize = Pos4Norm3Tex2Vertex.VertexSize;

            geom.HasBoundingBox = true;
            geom.BoundingBox = new BoundingBox(new Vector3(-sx, -sy, 0.0f), new Vector3(sx, sy, 0.0f));

            return geom;
        }
Beispiel #7
0
        public void Evaluate(int SpreadMax)
        {
            this.FInvalidate = false;

            if (FUpdate != null && FUpdate[0])
            {
                this.FVertex.Clear();
                this.FIndices.Clear();

                FVertexPin.Sync();
                FNormalsPin.Sync();
                FTexPin.Sync();

                FIndexPin.Sync();

                int meshCount = Math.Max(FVertexPin.SliceCount, FTexPin.SliceCount);
                meshCount = Math.Max(meshCount, FNormalsPin.SliceCount);
                meshCount = Math.Max(meshCount, FIndexPin.SliceCount);

                for (int i = 0; i < meshCount; i++)
                {

                    int vertexCount = Math.Max(FVertexPin[i].SliceCount, FTexPin[i].SliceCount);

                    Pos4Norm3Tex2Vertex[] verts = new Pos4Norm3Tex2Vertex[Convert.ToInt32(vertexCount)];

                    for (int j = 0; j < vertexCount; j++)
                    {
                        verts[j].Position = new Vector4(FVertexPin[i][j], 1.0f);
                        verts[j].Normals = FNormalsPin[i][j];
                        verts[j].TexCoords =FTexPin[i][j];
                    }
                    this.FVertex.Add(verts);

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

                    for (int j = 0; j < FIndexPin[i].SliceCount; j++)
                    {
                        Vector3 triangle = FIndexPin[i][j];
                        inds.Add((int)triangle.X);
                        inds.Add((int)triangle.Y);
                        inds.Add((int)triangle.Z);
                    }
                    this.FIndices.Add(inds.ToArray());
                }
                this.InvalidateMesh(meshCount);
            }
        }
Beispiel #8
0
        private int SetSegment(List <Pos4Norm3Tex2Vertex> verts, List <int> inds, float cx, float cy,
                               float phase, float radius, int ires, int lastindex, float mx, float my)
        {
            //Center vertex
            Pos4Norm3Tex2Vertex innerv = new Pos4Norm3Tex2Vertex();

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

            innerv.Position = new Vector4(cx, cy, 0.0f, 1.0f);
            innerv          = TexCoord(innerv, mx, my);
            verts.Add(innerv);

            double inc = (Math.PI / 2.0) / (double)ires;
            double phi = phase * (Math.PI * 2.0);

            //Build triangle strip here
            for (int i = 0; i < ires + 1; i++)
            {
                float x = Convert.ToSingle(cx + radius * Math.Cos(phi));
                float y = Convert.ToSingle(cy + radius * Math.Sin(phi));

                innerv.Position = new Vector4(x, y, 0.0f, 1.0f);
                innerv          = TexCoord(innerv, mx, my);
                verts.Add(innerv);

                phi += inc;
            }

            for (int i = 0; i < ires; i++)
            {
                inds.Add(lastindex);
                inds.Add(lastindex + i + 2);
                inds.Add(lastindex + i + 1);
            }

            return(lastindex + ires + 2);
        }
        public DX11IndexedGeometry IcoGrid(IcoGrid grid)
        {
            Vector2 size = grid.Size;
            int resX = grid.ResolutionX;
            int resY = grid.ResolutionY;

            float fdispx = -0.5f;
            float fdispy = (float)Math.Sqrt(0.75);
            List<Pos4Norm3Tex2Vertex> verts = new List<Pos4Norm3Tex2Vertex>();

            Pos4Norm3Tex2Vertex v = new Pos4Norm3Tex2Vertex();
            v.Normals = new Vector3(0, 0, -1);
            v.TexCoords = new Vector2(0, 0);

            float maxx = float.MinValue;
            float maxy = float.MinValue;

            bool bdisp = true;
            float y = 0.0f;
            for (int i = 0; i < resX; i++)
            {
                bdisp = !bdisp;

                int cnt = resY;

                for (int j = 0; j < cnt; j++)
                {
                    if (bdisp)
                    {
                        v.Position = new Vector4((float)j - 0.5f, y, 0.0f, 1.0f);

                        if (v.Position.X > maxx)
                        {
                            maxx = v.Position.X;
                        }
                        if (v.Position.Y > maxy)
                        {
                            maxy = v.Position.Y;
                        }
                    }
                    else
                    {
                        v.Position = new Vector4((float)j, y, 0.0f, 1.0f);

                        if (v.Position.X > maxx)
                        {
                            maxx = v.Position.X;
                        }
                        if (v.Position.Y > maxy)
                        {
                            maxy = v.Position.Y;
                        }
                    }
                    verts.Add(v);
                }
                y += fdispy;
            }

            float invmx = 1.0f / maxx;
            float invmy = 1.0f / maxy;
            for (int i = 0; i < verts.Count; i++)
            {
                Pos4Norm3Tex2Vertex v2 = verts[i];
                v2.Position.X *= invmx;
                v2.Position.X -= 0.5f;
                v2.Position.Y *= invmy;
                v2.Position.Y -= 0.5f;
                verts[i] = v2;
            }

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

            bool bflip = true;
            int nextrow = resY;
            int a = 1;
            for (int i = 0; i < resX - 1; i++)
            {
                bflip = !bflip;
                for (int j = 0; j < resY - 1; j++)
                {
                    int linestart = i * resY;
                    int lineup = (i + 1) * resY;
                    if (!bflip)
                    {
                        inds.Add(lineup + j);
                        inds.Add(linestart + j);
                        inds.Add(lineup + j + 1);

                        inds.Add(linestart + j);
                        inds.Add(linestart + j + 1);
                        inds.Add(lineup + j + 1);
                    }
                    else
                    {
                        inds.Add(linestart + j);
                        inds.Add(linestart + j + 1);
                        inds.Add(lineup + j);

                        inds.Add(lineup + j);
                        inds.Add(linestart + j + 1);
                        inds.Add(lineup + j + 1);
                    }
                }
                a += nextrow;
            }

            DX11IndexedGeometry geom = new DX11IndexedGeometry(context);
            geom.Tag = grid;
            geom.PrimitiveType = grid.PrimitiveType;

            DataStream vertexstream = new DataStream(verts.Count * Pos4Norm3Tex2Vertex.VertexSize, true, true);
            vertexstream.Position = 0;
            vertexstream.WriteRange(verts.ToArray());
            vertexstream.Position = 0;

            var vertices = BufferHelper.CreateDynamicVertexBuffer(context, vertexstream, true);

            var indexstream = new DataStream(inds.Count * 4, true, true);
            indexstream.WriteRange(inds.ToArray());
            indexstream.Position = 0;

            geom.VertexBuffer = vertices;
            geom.IndexBuffer = new DX11IndexBuffer(context, indexstream, false, true);
            geom.InputLayout = Pos4Norm3Tex2Vertex.Layout;
            geom.Topology = PrimitiveTopology.TriangleList;
            geom.VerticesCount = verts.Count;
            geom.VertexSize = Pos4Norm3Tex2Vertex.VertexSize;

            geom.HasBoundingBox = false;

            return geom;
        }
        public DX11IndexedGeometry Segment(Segment settings)
        {
            float phase  = settings.Phase;
            float cycles = settings.Cycles;
            float inner  = settings.InnerRadius;
            int   res    = settings.Resolution;
            bool  flat   = settings.Flat;

            DX11IndexedGeometry geom = new DX11IndexedGeometry(context);

            geom.Tag           = settings;
            geom.PrimitiveType = settings.PrimitiveType;
            int vcount = res * 2;
            int icount = (res - 1) * 6;

            DataStream ds = new DataStream(vcount * Pos4Norm3Tex2Vertex.VertexSize, true, true);

            ds.Position = 0;

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

            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];

            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, 0.0f, 1.0f);

                if (flat)
                {
                    innerv.TexCoords = new Vector2(0.5f - x, 0.5f - y);
                }
                else
                {
                    innerv.TexCoords = new Vector2((1.0f * (float)i) / ((float)res - 1.0f), 0.0f);
                }

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

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

                if (flat)
                {
                    outerv.TexCoords = new Vector2(0.5f - x, 0.5f - y);
                }
                else
                {
                    outerv.TexCoords = new Vector2((1.0f * (float)i) / ((float)res - 1.0f), 1.0f);
                }

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

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

            foreach (Pos4Norm3Tex2Vertex v in vertices)
            {
                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;
            }

            ds.WriteRange(vertices);

            ds.Position = 0;

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

            ds.Dispose();

            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 + 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;
            }

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

            indexstream.WriteRange(indices);
            indexstream.Position = 0;

            geom.VertexBuffer  = vbuffer;
            geom.IndexBuffer   = new DX11IndexBuffer(context, indexstream, false, true);
            geom.InputLayout   = Pos4Norm3Tex2Vertex.Layout;
            geom.Topology      = PrimitiveTopology.TriangleList;
            geom.VerticesCount = vcount;
            geom.VertexSize    = Pos4Norm3Tex2Vertex.VertexSize;

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

            return(geom);
        }
        public DX11IndexedGeometry Cylinder(Cylinder settings)
        {
            float radius1 = settings.Radius1;
            float radius2 = settings.Radius2;
            float cycles = settings.Cycles;
            float length = settings.Length;
            int resX = settings.ResolutionX;
            int resY = settings.ResolutionY;
            bool caps = settings.Caps;

            DX11IndexedGeometry geom = new DX11IndexedGeometry(context);
            geom.Tag = settings;
            geom.PrimitiveType = settings.PrimitiveType;

            int vcount = resX * (resY + 1);
            int icount = vcount * 6;

            if (caps)
            {
                //Add vertices (+1 for center)
                vcount += (resX + 1 * 2);

                //Add Triangles (on each side
                icount += (resX * 6);
            }

            float lenstart = -length * 0.5f; //Start at half bottom
            float lenstep = (float)length / (float)resY;

            float y = lenstart;

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

            float phi = 0.0f;
            float inc = Convert.ToSingle((Math.PI * 2.0 * cycles) / (double)resX);

            float fres = Convert.ToSingle(resY);

            #region Add Vertices tube
            for (int i = 0; i < resY + 1; i++)
            {

                float ystep = (float)i / fres;

                float radius = Map(ystep, 0, 1, radius1, radius2);

                for (int j = 0; j < resX; j++)
                {
                    float x = Convert.ToSingle(radius1 * Math.Cos(phi)) * radius;
                    float z = Convert.ToSingle(radius1 * Math.Sin(phi)) * radius;

                    Pos4Norm3Tex2Vertex v = new Pos4Norm3Tex2Vertex();
                    v.Position = new Vector4(x, y, z, 1.0f);
                    v.Normals = new Vector3(x, 0.0f, z);
                    v.Normals.Normalize();

                    verts.Add(v);

                    phi += inc;
                }
                y += lenstep;
                phi = 0.0f;
            }
            #endregion

            #region Add Indices Tube
            int indstart;
            for (int i = 0; i < resY; i++)
            {
                indstart = resX * i;
                int j;
                for (j = 0; j < resX - 1; j++)
                {
                    inds.Add(indstart + j);
                    inds.Add(indstart + resX + j);
                    inds.Add(indstart + j + 1);


                    inds.Add(indstart + j + 1);
                    inds.Add(indstart + j + resX);
                    inds.Add(indstart + j + resX + 1);

                }

                inds.Add(indstart + j);
                inds.Add(indstart + resX + j);
                inds.Add(indstart);

                inds.Add(indstart + j + resX);
                inds.Add(indstart + resX);
                inds.Add(indstart);
            }

            if (caps)
            {
                indstart = verts.Count;
                y = -length * 0.5f;

                Pos4Norm3Tex2Vertex v = new Pos4Norm3Tex2Vertex();
                v.Position = new Vector4(0, y, 0, 1.0f);
                v.Normals = new Vector3(0.0f, -1.0f, 0.0f);
                v.Normals.Normalize();
                verts.Add(v);

                phi = 0.0f;

                for (int j = 0; j < resX; j++)
                {
                    float x = Convert.ToSingle(radius1 * Math.Cos(phi)) * radius1;
                    float z = Convert.ToSingle(radius1 * Math.Sin(phi)) * radius1;

                    v.Position = new Vector4(x, y, z, 1.0f);
                    verts.Add(v);

                    phi += inc;
                }

                for (int j = 1; j < resX + 1; j++)
                {
                    inds.Add(indstart);
                    inds.Add(indstart + j);

                    if (j == resX)
                    {
                        inds.Add(indstart + 1);
                    }
                    else
                    {
                        inds.Add(indstart + j + 1);
                    }
                }

                indstart += (resX + 1);
                y = length * 0.5f;

                v = new Pos4Norm3Tex2Vertex();
                v.Position = new Vector4(0, y, 0, 1.0f);
                v.Normals = new Vector3(0.0f, 1.0f, 0.0f);
                v.Normals.Normalize();
                verts.Add(v);

                phi = 0.0f;

                for (int j = 0; j < resX; j++)
                {
                    float x = Convert.ToSingle(radius1 * Math.Cos(phi)) * radius2;
                    float z = Convert.ToSingle(radius1 * Math.Sin(phi)) * radius2;

                    v.Position = new Vector4(x, y, z, 1.0f);
                    verts.Add(v);

                    phi += inc;
                }

                for (int j = 1; j < resX + 1; j++)
                {
                    inds.Add(indstart + j);
                    inds.Add(indstart);


                    if (j == resX)
                    {
                        inds.Add(indstart + 1);
                    }
                    else
                    {
                        inds.Add(indstart + j + 1);
                    }
                }
            }
            #endregion

            DataStream ds = new DataStream(verts.Count * Pos4Norm3Tex2Vertex.VertexSize, true, true);
            ds.Position = 0;
            ds.WriteRange(verts.ToArray());
            ds.Position = 0;


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

            ds.Dispose();

            var indexstream = new DataStream(inds.Count * 4, true, true);
            indexstream.WriteRange(inds.ToArray());
            indexstream.Position = 0;

            geom.VertexBuffer = vbuffer;
            geom.IndexBuffer = new DX11IndexBuffer(context, indexstream, false, true);
            geom.InputLayout = Pos4Norm3Tex2Vertex.Layout;
            geom.Topology = PrimitiveTopology.TriangleList;
            geom.VerticesCount = vcount;
            geom.VertexSize = Pos4Norm3Tex2Vertex.VertexSize;

            //Since cylinder can be a cone, max box is max of radius
            float maxrad = radius1 > radius2 ? radius1 : radius2;

            geom.HasBoundingBox = true;
            geom.BoundingBox = new BoundingBox(new Vector3(-maxrad, -lenstart, -maxrad), new Vector3(maxrad, lenstart, maxrad));

            return geom;
        }
        public DX11IndexedGeometry Cylinder(Cylinder settings)
        {
            float radius1 = settings.Radius1;
            float radius2 = settings.Radius2;
            float cycles  = settings.Cycles;
            float length  = settings.Length;
            int   resX    = settings.ResolutionX;
            int   resY    = settings.ResolutionY;
            bool  caps    = settings.Caps;

            DX11IndexedGeometry geom = new DX11IndexedGeometry(context);

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

            int vcount = resX * (resY + 1);
            int icount = vcount * 6;

            if (caps)
            {
                //Add vertices (+1 for center)
                vcount += (resX + 1 * 2);

                //Add Triangles (on each side
                icount += (resX * 6);
            }

            float lenstart = settings.CenterY ? -length * 0.5f : 0.0f; //Start at half bottom if centered
            float lenstep  = (float)length / (float)resY;

            float y = lenstart;

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

            float phi = 0.0f;
            float inc = Convert.ToSingle((Math.PI * 2.0 * cycles) / (double)resX);

            float fres = Convert.ToSingle(resY);

            float fy = resY;

            float resMax = (float)resY;

            #region Add Vertices tube
            for (int i = 0; i < resY + 1; i++)
            {
                float ystep = (float)i / (fres + 1.0f);

                float radius = Map((float)i, 0.0f, resMax, radius1, radius2);

                for (int j = 0; j < resX; j++)
                {
                    float x = Convert.ToSingle(Math.Cos(phi)) * radius;
                    float z = Convert.ToSingle(Math.Sin(phi)) * radius;

                    Pos4Norm3Tex2Vertex v = new Pos4Norm3Tex2Vertex();
                    v.Position = new Vector4(x, y, z, 1.0f);
                    v.Normals  = new Vector3(x, 0.0f, z);
                    v.Normals.Normalize();
                    v.TexCoords = new Vector2((float)j / (float)resX, 1.0f - (float)i / fy);

                    verts.Add(v);

                    phi += inc;
                }
                y  += lenstep;
                phi = 0.0f;
            }
            #endregion

            #region Add Indices Tube
            int indstart;
            for (int i = 0; i < resY; i++)
            {
                indstart = resX * i;
                int j;
                for (j = 0; j < resX - 1; j++)
                {
                    inds.Add(indstart + j);
                    inds.Add(indstart + resX + j);
                    inds.Add(indstart + j + 1);


                    inds.Add(indstart + j + 1);
                    inds.Add(indstart + j + resX);
                    inds.Add(indstart + j + resX + 1);
                }

                inds.Add(indstart + j);
                inds.Add(indstart + resX + j);
                inds.Add(indstart);

                inds.Add(indstart + j + resX);
                inds.Add(indstart + resX);
                inds.Add(indstart);
            }

            if (caps)
            {
                indstart = verts.Count;
                y        = lenstart;

                Pos4Norm3Tex2Vertex v = new Pos4Norm3Tex2Vertex();
                v.Position = new Vector4(0, y, 0, 1.0f);
                v.Normals  = new Vector3(0.0f, -1.0f, 0.0f);
                v.Normals.Normalize();
                verts.Add(v);

                phi = 0.0f;

                for (int j = 0; j < resX; j++)
                {
                    float x = Convert.ToSingle(Math.Cos(phi)) * radius1;
                    float z = Convert.ToSingle(Math.Sin(phi)) * radius1;

                    v.Position = new Vector4(x, y, z, 1.0f);
                    verts.Add(v);

                    phi += inc;
                }

                for (int j = 1; j < resX + 1; j++)
                {
                    inds.Add(indstart);
                    inds.Add(indstart + j);

                    if (j == resX)
                    {
                        inds.Add(indstart + 1);
                    }
                    else
                    {
                        inds.Add(indstart + j + 1);
                    }
                }

                indstart += (resX + 1);
                y         = lenstart + length;

                v          = new Pos4Norm3Tex2Vertex();
                v.Position = new Vector4(0, y, 0, 1.0f);
                v.Normals  = new Vector3(0.0f, 1.0f, 0.0f);
                v.Normals.Normalize();
                verts.Add(v);

                phi = 0.0f;

                for (int j = 0; j < resX; j++)
                {
                    float x = Convert.ToSingle(Math.Cos(phi)) * radius2;
                    float z = Convert.ToSingle(Math.Sin(phi)) * radius2;

                    v.Position = new Vector4(x, y, z, 1.0f);
                    verts.Add(v);

                    phi += inc;
                }

                for (int j = 1; j < resX + 1; j++)
                {
                    inds.Add(indstart + j);
                    inds.Add(indstart);


                    if (j == resX)
                    {
                        inds.Add(indstart + 1);
                    }
                    else
                    {
                        inds.Add(indstart + j + 1);
                    }
                }
            }
            #endregion

            DataStream ds = new DataStream(verts.Count * Pos4Norm3Tex2Vertex.VertexSize, true, true);
            ds.Position = 0;
            ds.WriteRange(verts.ToArray());
            ds.Position = 0;


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

            ds.Dispose();

            var indexstream = new DataStream(inds.Count * 4, true, true);
            indexstream.WriteRange(inds.ToArray());
            indexstream.Position = 0;

            geom.VertexBuffer  = vbuffer;
            geom.IndexBuffer   = new DX11IndexBuffer(context, indexstream, false, true);
            geom.InputLayout   = Pos4Norm3Tex2Vertex.Layout;
            geom.Topology      = PrimitiveTopology.TriangleList;
            geom.VerticesCount = vcount;
            geom.VertexSize    = Pos4Norm3Tex2Vertex.VertexSize;

            //Since cylinder can be a cone, max box is max of radius
            float maxrad = radius1 > radius2 ? radius1 : radius2;

            geom.HasBoundingBox = true;
            geom.BoundingBox    = new BoundingBox(new Vector3(-maxrad, -lenstart, -maxrad), new Vector3(maxrad, lenstart, maxrad));

            return(geom);
        }
        public void Evaluate(int SpreadMax)
        {
            this.FInvalidate = false;

            if (this.FPInInCtrlPts.PinIsChanged || this.FPInInRes.PinIsChanged
                || this.FPinInMeshCount.PinIsChanged || this.FPinInCtrlRes.PinIsChanged)
            {
                this.FVertex.Clear();
                this.FIndices.Clear();

                double dblx, dbly;

                double mc;
                this.FPinInMeshCount.GetValue(0, out mc);

                int patchcnt = (int)mc;

                int ctrlidx = 0;

                for (int pc = 0; pc < patchcnt; pc++)
                {

                    this.FPInInRes.GetValue2D(pc, out dblx, out dbly);

                    int resX = Convert.ToInt32(dblx);
                    int resY = Convert.ToInt32(dbly);

                    this.FPinInCtrlRes.GetValue2D(pc, out dblx, out dbly);
                    int CresX = Convert.ToInt32(dblx);
                    int CresY = Convert.ToInt32(dbly);

                    List<Pos4Norm3Tex2Vertex> verts = new List<Pos4Norm3Tex2Vertex>();

                    float sx = 0.5f;
                    float sy = 0.5f;

                    float ix = (sx / Convert.ToSingle(resX - 1)) * 2.0f;
                    float iy = (sy / Convert.ToSingle(resY - 1)) * 2.0f;

                    float y = -sy;

                    Pos4Norm3Tex2Vertex v = new Pos4Norm3Tex2Vertex();
                    v.Normals = new Vector3(0, 0, -1.0f);

                    List<Vector3> ctrls = new List<Vector3>();

                    double cx, cy,cz;
                    for (int ct = 0; ct < CresX * CresY; ct++)
                    {
                        this.FPInInCtrlPts.GetValue3D(ct + ctrlidx, out cx, out cy, out cz);

                        ctrls.Add(new Vector3((float)cx, (float)cy, (float)cz));
                    }

                    Vector3[] carr = new Vector3[ctrls.Count];

                    for (int i = 0; i < resY; i++)
                    {
                        float x = -sx;
                        for (int j = 0; j < resX; j++)
                        {
                            float tu1 = Convert.ToSingle(VMath.Map(j, 0, resX - 1, 0.0, 1.0, TMapMode.Clamp));
                            float tv1 = Convert.ToSingle(VMath.Map(i, 0, resY - 1, 1.0, 0.0, TMapMode.Clamp));
                            v.TexCoords = new Vector2(tu1, tv1);

                            float[] bu = BernsteinBasis.ComputeBasis(CresX -1,tu1);
                            float[] bv = BernsteinBasis.ComputeBasis(CresY -1,tv1);

                            for (int ck = 0; ck < ctrls.Count; ck++)
                            {
                                carr[ck].X = x + ctrls[ck].X;
                                carr[ck].Y = y + ctrls[ck].Y;
                                carr[ck].Z = ctrls[ck].Z;
                            }

                            Vector3 vp = this.EvaluateBezier(carr, bu, bv, CresX, CresY);

                            v.Position = new Vector4(vp.X, vp.Y, vp.Z,1.0f);
                            x += ix;

                            //ds.Write<PosNormTexVertex>(v);
                            verts.Add(v);
                        }
                        y += iy;
                    }

                    this.FVertex.Add(verts.ToArray());

                    List<int> indlist = new List<int>();
                    for (int j = 0; j < resY - 1; j++)
                    {
                        int rowlow = (j * resX);
                        int rowup = ((j + 1) * resX);
                        for (int i = 0; i < resX - 1; i++)
                        {

                            int col = i * (resX - 1);

                            indlist.Add(0 + rowlow + i);
                            indlist.Add(0 + rowup + i);
                            indlist.Add(1 + rowlow + i);

                            indlist.Add(1 + rowup + i);
                            indlist.Add(1 + rowlow + i);
                            indlist.Add(0 + rowup + i);
                        }
                    }

                    this.FIndices.Add(indlist.ToArray());

                    ctrlidx += CresX * CresY;
                }
                this.InvalidateMesh(patchcnt);
            }
        }
Beispiel #14
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(context);

            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;
            }

            DataStream ds = new DataStream(vlist.Count * Pos4Norm3Tex2Vertex.VertexSize, true, true);
            ds.Position = 0;
            ds.WriteRange(vlist.ToArray());
            ds.Position = 0;

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

            ds.Dispose();

            var indexstream = new DataStream(ilist.Count * 4, true, true);
            indexstream.WriteRange(ilist.ToArray());
            indexstream.Position = 0;

            geom.VertexBuffer  = vbuffer;
            geom.IndexBuffer   = new DX11IndexBuffer(context, indexstream, false, true);
            geom.InputLayout   = Pos4Norm3Tex2Vertex.Layout;
            geom.Topology      = PrimitiveTopology.TriangleList;
            geom.VerticesCount = vlist.Count;
            geom.VertexSize    = Pos4Norm3Tex2Vertex.VertexSize;

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

            return(geom);
        }
        public void Evaluate(int SpreadMax)
        {
            this.FInvalidate = false;

            if (this.FPinInVertices.PinIsChanged || this.FPinInVerticesCount.PinIsChanged)
            {
                this.FVertex.Clear();
                this.FIndices.Clear();
                int cnt = 0;
                for (int i = 0; i < this.FPinInVerticesCount.SliceCount; i++)
                {
                    double dblcount;
                    this.FPinInVerticesCount.GetValue(i, out dblcount);

                    if (dblcount >= 3)
                    {
                        double cx = 0;
                        double cy = 0;
                        double x, y;

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

                        Pos4Norm3Tex2Vertex[] verts = new Pos4Norm3Tex2Vertex[Convert.ToInt32(dblcount) + 1];

                        for (int j = 0; j < dblcount; j++)
                        {
                            this.FPinInVertices.GetValue2D(cnt,out x,out y);
                            verts[j + 1].Position = new Vector4(Convert.ToSingle(x), Convert.ToSingle(y), 0,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; }

                            cnt++;
                        }

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

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

                        this.FVertex.Add(verts);

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

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

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

                        this.FIndices.Add(inds.ToArray());
                    }
                }
                this.InvalidateMesh(this.FVertex.Count);
            }
        }
Beispiel #16
0
 private Pos4Norm3Tex2Vertex TexCoord(Pos4Norm3Tex2Vertex v, float mx, float my)
 {
     v.TexCoords = new Vector2(0.5f + (v.Position.X / (mx * 2.0f)), 0.5f + (v.Position.Y / (my * 2.0f)));
     return(v);
 }
Beispiel #17
0
        public void Construct(Segment settings, Action <Vector3, Vector3, Vector2> appendVertex, Action <Int3> appendIndex)
        {
            float phase  = settings.Phase;
            float cycles = settings.Cycles;
            float inner  = settings.InnerRadius;
            int   res    = settings.Resolution;
            bool  flat   = settings.Flat;

            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));

            Pos3Norm3Tex2Vertex innerv = new Pos3Norm3Tex2Vertex();

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

            Pos3Norm3Tex2Vertex outerv = new Pos3Norm3Tex2Vertex();

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

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

            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 Vector3(x, y, 0.0f);
                if (flat)
                {
                    innerv.TexCoords = new Vector2(0.5f - x, 0.5f - y);
                }
                else
                {
                    innerv.TexCoords = new Vector2((1.0f * (float)i) / ((float)res - 1.0f), 0.0f);
                }
                appendVertex(innerv.Position, innerv.Normals, innerv.TexCoords);
                phi += inc;
            }

            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));
                outerv.Position = new Vector3(x, y, 0.0f);

                if (flat)
                {
                    outerv.TexCoords = new Vector2(0.5f - x, 0.5f - y);
                }
                else
                {
                    outerv.TexCoords = new Vector2((1.0f * (float)i) / ((float)res - 1.0f), 1.0f);
                }

                appendVertex(outerv.Position, outerv.Normals, outerv.TexCoords);
                phi += inc;
            }



            for (int i = 0; i < res - 1; i++)
            {
                appendIndex(new Int3(i, i + 1, res + i));
                appendIndex(new Int3(i + 1, res + i + 1, res + i));
            }
        }
Beispiel #18
0
        public DX11IndexedGeometry Sphere(Sphere settings)
        {
            float radius = settings.Radius;
            int   resX   = settings.ResX;
            int   resY   = settings.ResY;
            float cx     = settings.CyclesX;
            float cy     = settings.CyclesY;

            List <Pos4Norm3Tex2Vertex> vertices = new List <Pos4Norm3Tex2Vertex>();
            List <int> indices = new List <int>();

            float pi     = (float)Math.PI;
            float pidiv2 = pi * 0.5f;
            float twopi  = pi * 2.0f;



            for (int i = 0; i <= resY; i++)
            {
                float v = 1 - (float)i / resY;

                float latitude = (i * pi / resY) - pidiv2;

                float dy  = (float)Math.Sin(latitude * cy);
                float dxz = (float)Math.Cos(latitude * cy);

                // Create a single ring of vertices at this latitude.
                for (int j = 0; j <= resX; j++)
                {
                    float u = (float)j / resX;

                    float longitude = j * twopi / resX;

                    float dx = (float)Math.Sin(longitude * cx);
                    float dz = (float)Math.Cos(longitude * cx);

                    dx *= dxz;
                    dz *= dxz;

                    Pos4Norm3Tex2Vertex vertex = new Pos4Norm3Tex2Vertex();
                    vertex.Position  = new Vector4(dx * radius, dy * radius, dz * radius, 1.0f);
                    vertex.Normals   = Vector3.Normalize(new Vector3(vertex.Position.X, vertex.Position.Y, vertex.Position.Z));
                    vertex.TexCoords = new Vector2(1.0f - u, v);

                    vertices.Add(vertex);
                }
            }

            int stride = resX + 1;

            for (int i = 0; i < resY; i++)
            {
                for (int j = 0; j <= resX; j++)
                {
                    int nextI = i + 1;
                    int nextJ = (j + 1) % stride;

                    indices.Add(i * stride + j);
                    indices.Add(i * stride + nextJ);
                    indices.Add(nextI * stride + j);

                    indices.Add(i * stride + nextJ);
                    indices.Add(nextI * stride + nextJ);
                    indices.Add(nextI * stride + j);
                }
            }

            DX11IndexedGeometry geom = DX11IndexedGeometry.CreateFrom <Pos4Norm3Tex2Vertex>(context, vertices.ToArray(), indices.ToArray(), Pos4Norm3Tex2Vertex.Layout);

            geom.HasBoundingBox = true;
            geom.BoundingBox    = new BoundingBox(new Vector3(-radius), new Vector3(radius));
            geom.Tag            = settings;
            geom.PrimitiveType  = settings.PrimitiveType;

            return(geom);
        }
        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(context);
            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;
            }

            DataStream ds = new DataStream(vlist.Count * Pos4Norm3Tex2Vertex.VertexSize, true, true);
            ds.Position = 0;
            ds.WriteRange(vlist.ToArray());
            ds.Position = 0;

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

            ds.Dispose();

            var indexstream = new DataStream(ilist.Count * 4, true, true);
            indexstream.WriteRange(ilist.ToArray());
            indexstream.Position = 0;

            geom.VertexBuffer = vbuffer;
            geom.IndexBuffer = new DX11IndexBuffer(context, indexstream, false, true);
            geom.InputLayout = Pos4Norm3Tex2Vertex.Layout;
            geom.Topology = PrimitiveTopology.TriangleList;
            geom.VerticesCount = vlist.Count;
            geom.VertexSize = Pos4Norm3Tex2Vertex.VertexSize;

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

            return geom;
        }
Beispiel #20
0
        public DX11IndexedGeometry IcoGrid(IcoGrid grid)
        {
            Vector2 size = grid.Size;
            int     resX = grid.ResolutionX;
            int     resY = grid.ResolutionY;

            float fdispx = -0.5f;
            float fdispy = (float)Math.Sqrt(0.75);
            List <Pos4Norm3Tex2Vertex> verts = new List <Pos4Norm3Tex2Vertex>();

            Pos4Norm3Tex2Vertex v = new Pos4Norm3Tex2Vertex();

            v.Normals   = new Vector3(0, 0, -1);
            v.TexCoords = new Vector2(0, 0);

            float maxx = float.MinValue;
            float maxy = float.MinValue;

            bool  bdisp = true;
            float y     = 0.0f;

            for (int i = 0; i < resX; i++)
            {
                bdisp = !bdisp;

                int cnt = resY;

                for (int j = 0; j < cnt; j++)
                {
                    if (bdisp)
                    {
                        v.Position = new Vector4((float)j - 0.5f, y, 0.0f, 1.0f);

                        if (v.Position.X > maxx)
                        {
                            maxx = v.Position.X;
                        }
                        if (v.Position.Y > maxy)
                        {
                            maxy = v.Position.Y;
                        }
                    }
                    else
                    {
                        v.Position = new Vector4((float)j, y, 0.0f, 1.0f);

                        if (v.Position.X > maxx)
                        {
                            maxx = v.Position.X;
                        }
                        if (v.Position.Y > maxy)
                        {
                            maxy = v.Position.Y;
                        }
                    }
                    verts.Add(v);
                }
                y += fdispy;
            }

            float invmx = 1.0f / maxx;
            float invmy = 1.0f / maxy;

            for (int i = 0; i < verts.Count; i++)
            {
                Pos4Norm3Tex2Vertex v2 = verts[i];
                v2.Position.X *= invmx;
                v2.Position.X -= 0.5f;
                v2.Position.Y *= invmy;
                v2.Position.Y -= 0.5f;
                verts[i]       = v2;
            }

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

            bool bflip   = true;
            int  nextrow = resY;
            int  a       = 1;

            for (int i = 0; i < resX - 1; i++)
            {
                bflip = !bflip;
                for (int j = 0; j < resY - 1; j++)
                {
                    int linestart = i * resY;
                    int lineup    = (i + 1) * resY;
                    if (!bflip)
                    {
                        inds.Add(lineup + j);
                        inds.Add(linestart + j);
                        inds.Add(lineup + j + 1);

                        inds.Add(linestart + j);
                        inds.Add(linestart + j + 1);
                        inds.Add(lineup + j + 1);
                    }
                    else
                    {
                        inds.Add(linestart + j);
                        inds.Add(linestart + j + 1);
                        inds.Add(lineup + j);

                        inds.Add(lineup + j);
                        inds.Add(linestart + j + 1);
                        inds.Add(lineup + j + 1);
                    }
                }
                a += nextrow;
            }

            DX11IndexedGeometry geom = new DX11IndexedGeometry(context);

            geom.Tag           = grid;
            geom.PrimitiveType = grid.PrimitiveType;

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

            vertexstream.Position = 0;
            vertexstream.WriteRange(verts.ToArray());
            vertexstream.Position = 0;

            var vertices = BufferHelper.CreateDynamicVertexBuffer(context, vertexstream, true);

            var indexstream = new DataStream(inds.Count * 4, true, true);

            indexstream.WriteRange(inds.ToArray());
            indexstream.Position = 0;

            geom.VertexBuffer  = vertices;
            geom.IndexBuffer   = new DX11IndexBuffer(context, indexstream, false, true);
            geom.InputLayout   = Pos4Norm3Tex2Vertex.Layout;
            geom.Topology      = PrimitiveTopology.TriangleList;
            geom.VerticesCount = verts.Count;
            geom.VertexSize    = Pos4Norm3Tex2Vertex.VertexSize;

            geom.HasBoundingBox = false;

            return(geom);
        }
        public DX11IndexedGeometry Segment(float phase, float cycles, float inner, int res, bool flat)
        {
            DX11IndexedGeometry geom = new DX11IndexedGeometry(context);
            int vcount = res * 2;
            int icount = (res - 1) * 6;

            DataStream ds = new DataStream(vcount * Pos4Norm3Tex2Vertex.VertexSize, true, true);
            ds.Position = 0;

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

            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];

            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, 0.0f, 1.0f);

                if (flat)
                {
                    innerv.TexCoords = new Vector2(0.5f - x, 0.5f - y);
                }
                else
                {
                    innerv.TexCoords = new Vector2((1.0f * (float)i) / ((float)res - 1.0f), 0.0f);
                }

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

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

                if (flat)
                {
                    outerv.TexCoords = new Vector2(0.5f - x, 0.5f - y);
                }
                else
                {
                    outerv.TexCoords = new Vector2((1.0f * (float)i) / ((float)res - 1.0f), 1.0f);
                }

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

            ds.WriteRange(vertices);

            ds.Position = 0;

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

            ds.Dispose();

            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 + 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;
            }

            var indexstream = new DataStream(icount * 4, true, true);
            indexstream.WriteRange(indices);
            indexstream.Position = 0;

            geom.VertexBuffer = vbuffer;
            geom.IndexBuffer = new DX11IndexBuffer(context, indexstream, false, true);
            geom.InputLayout = Pos4Norm3Tex2Vertex.Layout;
            geom.Topology = PrimitiveTopology.TriangleList;
            geom.VerticesCount = vcount;
            geom.VertexSize = Pos4Norm3Tex2Vertex.VertexSize;

            geom.HasBoundingBox = false;

            return geom;
        }
        public DX11IndexedGeometry Segment(Segment settings)
        {
            float phase = settings.Phase;
            float cycles = settings.Cycles; 
            float inner = settings.InnerRadius; 
            int res = settings.Resolution;
            bool flat = settings.Flat;

            DX11IndexedGeometry geom = new DX11IndexedGeometry(context);
            geom.Tag = settings;
            geom.PrimitiveType = settings.PrimitiveType;
            int vcount = res * 2;
            int icount = (res - 1) * 6;

            DataStream ds = new DataStream(vcount * Pos4Norm3Tex2Vertex.VertexSize, true, true);
            ds.Position = 0;

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

            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];

            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, 0.0f, 1.0f);

                if (flat)
                {
                    innerv.TexCoords = new Vector2(0.5f - x, 0.5f - y);
                }
                else
                {
                    innerv.TexCoords = new Vector2((1.0f * (float)i) / ((float)res - 1.0f), 0.0f);
                }

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

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

                if (flat)
                {
                    outerv.TexCoords = new Vector2(0.5f - x, 0.5f - y);
                }
                else
                {
                    outerv.TexCoords = new Vector2((1.0f * (float)i) / ((float)res - 1.0f), 1.0f);
                }

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

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

            foreach (Pos4Norm3Tex2Vertex v in vertices)
            {
                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;
            }

            ds.WriteRange(vertices);

            ds.Position = 0;

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

            ds.Dispose();

            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 + 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;
            }

            var indexstream = new DataStream(icount * 4, true, true);
            indexstream.WriteRange(indices);
            indexstream.Position = 0;

            geom.VertexBuffer = vbuffer;
            geom.IndexBuffer = new DX11IndexBuffer(context, indexstream, false, true);
            geom.InputLayout = Pos4Norm3Tex2Vertex.Layout;
            geom.Topology = PrimitiveTopology.TriangleList;
            geom.VerticesCount = vcount;
            geom.VertexSize = Pos4Norm3Tex2Vertex.VertexSize;

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

            return geom;
        }
        public DX11IndexedGeometry Torus(int resX, int resY, float radius, float thick, float phasey,float phasex,float rot, float cy)
        {
            List<Pos4Norm3Tex2Vertex> vertices = new List<Pos4Norm3Tex2Vertex>();
            List<int> indices = new List<int>();

            float pi = (float)Math.PI;
            float pidiv2 = pi * 0.5f;
            float twopi = pi * 2.0f;

            int stride = resY + 1;

            for (int i = 0; i <= resY; i++)
            {
                float u = (float)i / (float)resY;
                //u *= cy;

                float outerAngle = (float)i * twopi / (float)resY - pidiv2;

                Matrix transform = Matrix.Translation(radius, 0, 0) * Matrix.RotationY(outerAngle*rot);

                for (int j = 0; j <= resX; j++)
                {
                    float fj = (float)j * cy;

                    float v = 1 - (float)j / resX;

                    float innerAngle = fj * twopi / (float)resX + pi;
                    float dy = (float)Math.Sin(innerAngle)*phasey;
                    float dx = (float)Math.Cos(innerAngle)*phasex;

                    Pos4Norm3Tex2Vertex vertex = new Pos4Norm3Tex2Vertex();

                    Vector3 normal = new Vector3(dx, dy, 0);
                    Vector3 position = normal * thick / 2;
                    vertex.TexCoords = new Vector2(u, v);
                    vertex.Normals = Vector3.TransformNormal(normal, transform);

                    position = Vector3.TransformCoordinate(position, transform);
                    vertex.Position = new Vector4(position.X, position.Y, position.Z, 1.0f);

                    vertices.Add(vertex);

                    int nextI = (i + 1) % stride;
                    int nextJ = (j + 1) % stride;

                    indices.Add(j * stride + i);
                    indices.Add(nextJ * stride + i);
                    indices.Add(j * stride + nextI);

                    indices.Add(j * stride + nextI);
                    indices.Add(nextJ * stride + i);
                    indices.Add(nextJ * stride + nextI);
                }
            }

            DX11IndexedGeometry geom = DX11IndexedGeometry.CreateFrom<Pos4Norm3Tex2Vertex>(context, vertices.ToArray(), indices.ToArray(), Pos4Norm3Tex2Vertex.Layout);
            geom.HasBoundingBox = false;
            geom.BoundingBox = new BoundingBox(new Vector3(-radius), new Vector3(radius));

            return geom;
        }
        public DX11IndexedGeometry Sphere(Sphere settings)
        {
            float radius = settings.Radius;
            int resX = settings.ResX;
            int resY = settings.ResY;
            float cx = settings.CyclesX;
            float cy = settings.CyclesY;

            List<Pos4Norm3Tex2Vertex> vertices = new List<Pos4Norm3Tex2Vertex>();
            List<int> indices = new List<int>();

            float pi = (float)Math.PI;
            float pidiv2 = pi * 0.5f;
            float twopi = pi * 2.0f;

            

            for (int i = 0; i <= resY; i++)
            {
                float v = 1 - (float)i / resY;

                float latitude = (i * pi / resY) - pidiv2;

                float dy = (float)Math.Sin(latitude * cy);
                float dxz = (float)Math.Cos(latitude * cy);

                // Create a single ring of vertices at this latitude.
                for (int j = 0; j <= resX; j++)
                {
                    float u = (float)j / resX;

                    float longitude = j * twopi / resX;

                    float dx = (float)Math.Sin(longitude * cx);
                    float dz = (float)Math.Cos(longitude * cx);

                    dx *= dxz;
                    dz *= dxz;

                    Pos4Norm3Tex2Vertex vertex = new Pos4Norm3Tex2Vertex();
                    vertex.Position = new Vector4(dx * radius, dy * radius, dz * radius, 1.0f);
                    vertex.Normals = Vector3.Normalize(new Vector3(vertex.Position.X, vertex.Position.Y, vertex.Position.Z));
                    vertex.TexCoords = new Vector2(1.0f-u,v);

                    vertices.Add(vertex);
                }
            }

            int stride = resX + 1;

            for (int i = 0; i < resY; i++)
            {
                for (int j = 0; j <= resX; j++)
                {
                    int nextI = i + 1;
                    int nextJ = (j + 1) % stride;

                    indices.Add(i * stride + j);
                    indices.Add(i * stride + nextJ);
                    indices.Add(nextI * stride + j);

                    indices.Add(i * stride + nextJ);
                    indices.Add(nextI * stride + nextJ);
                    indices.Add(nextI * stride + j);
                }
            }

            DX11IndexedGeometry geom = DX11IndexedGeometry.CreateFrom<Pos4Norm3Tex2Vertex>(context, vertices.ToArray(), indices.ToArray(), Pos4Norm3Tex2Vertex.Layout);               
            geom.HasBoundingBox = true;
            geom.BoundingBox = new BoundingBox(new Vector3(-radius), new Vector3(radius));
            geom.Tag = settings;
            geom.PrimitiveType = settings.PrimitiveType;

            return geom;
        }
Beispiel #25
0
        public DX11IndexedGeometry Torus(Torus settings)
        {
            int   resX   = settings.ResolutionX;
            int   resY   = settings.ResolutionY;
            float radius = settings.Radius;
            float thick  = settings.Thickness;
            float phasey = settings.PhaseY;
            float phasex = settings.PhaseX;
            float rot    = settings.Rotation;
            float cy     = settings.CY;

            List <Pos4Norm3Tex2Vertex> vertices = new List <Pos4Norm3Tex2Vertex>();
            List <int> indices = new List <int>();

            float pi     = (float)Math.PI;
            float pidiv2 = pi * 0.5f;
            float twopi  = pi * 2.0f;

            int stride = resY + 1;

            for (int i = 0; i <= resY; i++)
            {
                float u = (float)i / (float)resY;
                //u *= cy;

                float outerAngle = (float)i * twopi / (float)resY - pidiv2;

                Matrix transform = Matrix.Translation(radius, 0, 0) * Matrix.RotationY(outerAngle * rot);


                for (int j = 0; j <= resX; j++)
                {
                    float fj = (float)j * cy;

                    float v = 1 - (float)j / resX;

                    float innerAngle = fj * twopi / (float)resX + pi;
                    float dy         = (float)Math.Sin(innerAngle) * phasey;
                    float dx         = (float)Math.Cos(innerAngle) * phasex;

                    Pos4Norm3Tex2Vertex vertex = new Pos4Norm3Tex2Vertex();

                    Vector3 normal   = new Vector3(dx, dy, 0);
                    Vector3 position = normal * thick / 2;
                    vertex.TexCoords = new Vector2(u, v);
                    vertex.Normals   = Vector3.TransformNormal(normal, transform);

                    position        = Vector3.TransformCoordinate(position, transform);
                    vertex.Position = new Vector4(position.X, position.Y, position.Z, 1.0f);

                    vertices.Add(vertex);

                    int nextI = (i + 1) % stride;
                    int nextJ = (j + 1) % stride;

                    indices.Add(j * stride + i);
                    indices.Add(nextJ * stride + i);
                    indices.Add(j * stride + nextI);

                    indices.Add(j * stride + nextI);
                    indices.Add(nextJ * stride + i);
                    indices.Add(nextJ * stride + nextI);
                }
            }

            DX11IndexedGeometry geom = DX11IndexedGeometry.CreateFrom <Pos4Norm3Tex2Vertex>(context, vertices.ToArray(), indices.ToArray(), Pos4Norm3Tex2Vertex.Layout);

            geom.Tag            = settings;
            geom.PrimitiveType  = settings.PrimitiveType;
            geom.HasBoundingBox = false;
            geom.BoundingBox    = new BoundingBox(new Vector3(-radius), new Vector3(radius));

            return(geom);
        }
Beispiel #26
0
        public void Evaluate(int SpreadMax)
        {
            this.FInvalidate = false;

            if (this.FPInInCtrlPts.PinIsChanged || this.FPInInRes.PinIsChanged
                || this.FPinInMeshCount.PinIsChanged || this.FPinInCtrlRes.PinIsChanged)
            {
                this.FVertex.Clear();
                this.FIndices.Clear();

                List<double> hx = new List<double>();
                List<double> hy = new List<double>();
                List<int> pid = new List<int>();

                double dblx, dbly;

                double mc;
                this.FPinInMeshCount.GetValue(0, out mc);

                int patchcnt = (int)mc;

                int ctrlidx = 0;

                for (int pc = 0; pc < patchcnt; pc++)
                {

                    this.FPInInRes.GetValue2D(pc, out dblx, out dbly);

                    int resX = Convert.ToInt32(dblx);
                    int resY = Convert.ToInt32(dbly);

                    this.FPinInCtrlRes.GetValue2D(pc, out dblx, out dbly);
                    int CresX = Convert.ToInt32(dblx);
                    int CresY = Convert.ToInt32(dbly);

                    List<Pos4Norm3Tex2Vertex> verts = new List<Pos4Norm3Tex2Vertex>();

                    float sx = 0.5f;
                    float sy = 0.5f;

                    float ix = (sx / Convert.ToSingle(resX - 1)) * 2.0f;
                    float iy = (sy / Convert.ToSingle(resY - 1)) * 2.0f;

                    float y = -sy;

                    Pos4Norm3Tex2Vertex v = new Pos4Norm3Tex2Vertex();
                    v.Normals = new Vector3(0, 0, -1.0f);

                    List<Vector3> ctrls = new List<Vector3>();

                    float mx = -0.5f;
                    float my = 0.5f;

                    float incx = 1.0f / ((float)CresX - 1.0f);
                    float incy = 1.0f / ((float)CresY - 1.0f);

                    int inch = 0;

                    double cx, cy;
                    for (int ct = 0; ct < CresX * CresY; ct++)
                    {
                        this.FPInInCtrlPts.GetValue2D(ct + ctrlidx, out cx, out cy);

                        ctrls.Add(new Vector3((float)cx, (float)cy, 0.0f));
                    }

                    Matrix4x4 mat;
                    this.FPinInTransform.GetMatrix(pc, out mat);

                    for (int ct = 0; ct < CresX * CresY; ct++)
                    {
                        this.FPInInCtrlPts.GetValue2D(ct + ctrlidx, out cx, out cy);

                        ctrls.Add(new Vector3((float)cx, (float)cy, 0.0f));

                        Vector2D vd = new Vector2D(cx + mx, cy + my);
                        Vector3D v2 = mat * vd;

                        hx.Add(v2.x);
                        hy.Add(v2.y);

                        mx += incx;

                        inch++;
                        if (inch == CresY)
                        {
                            inch = 0;
                            mx = -0.5f;
                            my -= incy;
                        }

                        pid.Add(pc);
                    }

                    Vector3[] carr = new Vector3[ctrls.Count];

                    for (int i = 0; i < resY; i++)
                    {
                        float x = -sx;
                        for (int j = 0; j < resX; j++)
                        {
                            //v.Position = new Vector4(x, y, 0.0f, 1.0f);
                            float tu1 = Convert.ToSingle(VMath.Map(j, 0, resX - 1, 0.0, 1.0, TMapMode.Clamp));
                            float tv1 = Convert.ToSingle(VMath.Map(i, 0, resY - 1, 1.0, 0.0, TMapMode.Clamp));
                            v.TexCoords = new Vector2(tu1, tv1);

                            float[] bu = BernsteinBasis.ComputeBasis(CresX -1,tu1);
                            float[] bv = BernsteinBasis.ComputeBasis(CresY -1,tv1);

                            for (int ck = 0; ck < ctrls.Count; ck++)
                            {
                                carr[ck].X = x + ctrls[ck].X;
                                carr[ck].Y = y + ctrls[ck].Y;
                            }

                            Vector3 vp = this.EvaluateBezier(carr, bu, bv, CresX, CresY);

                            v.Position = new Vector4(vp.X, vp.Y, 0.0f,1.0f);
                            x += ix;

                            //ds.Write<PosNormTexVertex>(v);
                            verts.Add(v);
                        }
                        y += iy;
                    }

                    this.FVertex.Add(verts.ToArray());

                    List<int> indlist = new List<int>();
                    for (int j = 0; j < resY - 1; j++)
                    {
                        int rowlow = (j * resX);
                        int rowup = ((j + 1) * resX);
                        for (int i = 0; i < resX - 1; i++)
                        {

                            int col = i * (resX - 1);

                            indlist.Add(0 + rowlow + i);
                            indlist.Add(0 + rowup + i);
                            indlist.Add(1 + rowlow + i);

                            indlist.Add(1 + rowup + i);
                            indlist.Add(1 + rowlow + i);
                            indlist.Add(0 + rowup + i);
                        }
                    }

                    this.FIndices.Add(indlist.ToArray());

                    ctrlidx += CresX * CresY;
                }

                this.FPinOutHelpers.SliceCount = hx.Count;

                for (int i = 0; i < hx.Count; i++)
                {
                    this.FPinOutHelpers.SetValue2D(i, hx[i], hy[i]);
                }

                this.FOutPatchId.SliceCount = pid.Count;
                for (int i = 0; i < pid.Count; i++)
                {
                    this.FOutPatchId.SetValue(i, pid[i]);
                }

                this.InvalidateMesh(patchcnt);
            }
        }
Beispiel #27
0
        public DX11IndexedGeometry Grid(Grid settings)
        {
            Vector2 size = settings.Size;
            int     resX = settings.ResolutionX;
            int     resY = settings.ResolutionY;

            DX11IndexedGeometry geom = new DX11IndexedGeometry(context);

            geom.Tag           = settings;
            geom.PrimitiveType = "Grid";

            DataStream ds = new DataStream(resX * resY * Pos4Norm3Tex2Vertex.VertexSize, true, true);

            ds.Position = 0;

            float sx = 0.5f * size.X;
            float sy = 0.5f * size.Y;

            float ix = (sx / Convert.ToSingle(resX - 1)) * 2.0f;
            float iy = (sy / Convert.ToSingle(resY - 1)) * 2.0f;


            float y = -sy;


            Pos4Norm3Tex2Vertex v = new Pos4Norm3Tex2Vertex();

            v.Normals = new Vector3(0, 0, -1.0f);

            for (int i = 0; i < resY; i++)
            {
                float x = -sx;
                for (int j = 0; j < resX; j++)
                {
                    v.Position    = new Vector4(x, y, 0.0f, 1.0f);
                    v.TexCoords.X = Convert.ToSingle(Map((float)j, 0.0f, resX - 1, 0.0f, 1.0f));
                    v.TexCoords.Y = Convert.ToSingle(Map((float)i, 0.0f, resY - 1, 1.0f, 0.0f));
                    x            += ix;

                    ds.Write <Pos4Norm3Tex2Vertex>(v);
                }
                y += iy;
            }

            ds.Position = 0;

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

            ds.Dispose();

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

            for (int j = 0; j < resY - 1; j++)
            {
                int rowlow = (j * resX);
                int rowup  = ((j + 1) * resX);
                for (int i = 0; i < resX - 1; i++)
                {
                    int col = i * (resX - 1);

                    indlist.Add(0 + rowlow + i);
                    indlist.Add(0 + rowup + i);
                    indlist.Add(1 + rowlow + i);

                    indlist.Add(1 + rowlow + i);
                    indlist.Add(0 + rowup + i);
                    indlist.Add(1 + rowup + i);
                }
            }

            var indexstream = new DataStream(indlist.Count * 4, true, true);

            indexstream.WriteRange(indlist.ToArray());
            indexstream.Position = 0;



            geom.VertexBuffer  = vertices;
            geom.IndexBuffer   = new DX11IndexBuffer(context, indexstream, false, true);
            geom.InputLayout   = Pos4Norm3Tex2Vertex.Layout;
            geom.Topology      = PrimitiveTopology.TriangleList;
            geom.VerticesCount = resX * resY;
            geom.VertexSize    = Pos4Norm3Tex2Vertex.VertexSize;

            geom.HasBoundingBox = true;
            geom.BoundingBox    = new BoundingBox(new Vector3(-sx, -sy, 0.0f), new Vector3(sx, sy, 0.0f));

            return(geom);
        }
        private int SetSegment(List<Pos4Norm3Tex2Vertex> verts, List<int> inds, float cx, float cy,
            float phase, float radius, int ires, int lastindex, float mx, float my)
        {
            //Center vertex
            Pos4Norm3Tex2Vertex innerv = new Pos4Norm3Tex2Vertex();
            innerv.Normals = new Vector3(0.0f, 0.0f, 1.0f);

            innerv.Position = new Vector4(cx, cy, 0.0f, 1.0f);
            innerv = TexCoord(innerv, mx, my);
            verts.Add(innerv);

            double inc = (Math.PI / 2.0) / (double)ires;
            double phi = phase * (Math.PI * 2.0);

            //Build triangle strip here
            for (int i = 0; i < ires + 1; i++)
            {
                float x = Convert.ToSingle(cx + radius * Math.Cos(phi));
                float y = Convert.ToSingle(cy + radius * Math.Sin(phi));

                innerv.Position = new Vector4(x, y, 0.0f, 1.0f);
                innerv = TexCoord(innerv, mx, my);
                verts.Add(innerv);

                phi += inc;
            }

            for (int i = 0; i < ires; i++)
            {
                inds.Add(lastindex);
                inds.Add(lastindex + i + 2);
                inds.Add(lastindex + i + 1);
            }

            return lastindex + ires + 2;
        }
 private Pos4Norm3Tex2Vertex TexCoord(Pos4Norm3Tex2Vertex v, float mx, float my)
 {
     v.TexCoords = new Vector2(0.5f + (v.Position.X / (mx * 2.0f)), 0.5f + (v.Position.Y / (my * 2.0f)));
     return v;
 }
        public DX11IndexedGeometry Grid(Vector2 size, int resX, int resY)
        {
            DX11IndexedGeometry geom = new DX11IndexedGeometry(context);

            DataStream ds = new DataStream(resX * resY * Pos4Norm3Tex2Vertex.VertexSize, true, true);
            ds.Position = 0;

            float sx = 0.5f * size.X;
            float sy = 0.5f * size.Y;

            float ix = (sx / Convert.ToSingle(resX - 1)) * 2.0f;
            float iy = (sy / Convert.ToSingle(resY - 1)) * 2.0f;

            float y = -sy;

            Pos4Norm3Tex2Vertex v = new Pos4Norm3Tex2Vertex();
            v.Normals = new Vector3(0, 0, -1.0f);

            for (int i = 0; i < resY; i++)
            {
                float x = -sx;
                for (int j = 0; j < resX; j++)
                {
                    v.Position = new Vector4(x, y, 0.0f, 1.0f);
                    v.TexCoords.X = Convert.ToSingle(Map((float)j, 0.0f, resX - 1, 0.0f, 1.0f));
                    v.TexCoords.Y = Convert.ToSingle(Map((float)i, 0.0f, resY - 1, 1.0f, 0.0f));
                    x += ix;

                    ds.Write<Pos4Norm3Tex2Vertex>(v);
                }
                y += iy;
            }

            ds.Position = 0;

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

            ds.Dispose();

            List<int> indlist = new List<int>();
            for (int j = 0; j < resY - 1; j++)
            {
                int rowlow = (j * resX);
                int rowup = ((j + 1) * resX);
                for (int i = 0; i < resX - 1; i++)
                {

                    int col = i * (resX - 1);

                    indlist.Add(0 + rowlow + i);
                    indlist.Add(0 + rowup + i);
                    indlist.Add(1 + rowlow + i);

                    indlist.Add(1 + rowlow + i);
                    indlist.Add(0 + rowup + i);
                    indlist.Add(1 + rowup + i);
                }
            }

            var indexstream = new DataStream(indlist.Count * 4, true, true);
            indexstream.WriteRange(indlist.ToArray());
            indexstream.Position = 0;

            geom.VertexBuffer = vertices;
            geom.IndexBuffer = new DX11IndexBuffer(context, indexstream, false, true);
            geom.InputLayout = Pos4Norm3Tex2Vertex.Layout;
            geom.Topology = PrimitiveTopology.TriangleList;
            geom.VerticesCount = resX * resY;
            geom.VertexSize = Pos4Norm3Tex2Vertex.VertexSize;

            geom.HasBoundingBox = true;
            geom.BoundingBox = new BoundingBox(new Vector3(-sx, -sy, 0.0f), new Vector3(sx, sy, 0.0f));

            return geom;
        }
        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);
        }