public static IDX11Geometry CreateIndexedGeometry(DX11RenderContext context, List <Vector3> points, List <Vector3> normals, List <Vector2> tex, int[] indices) { var count = points.Count; Pos3Norm3Tex2Vertex[] vertices = new Pos3Norm3Tex2Vertex[count]; for (int i = 0; i < count; i++) { vertices[i] = new Pos3Norm3Tex2Vertex() { Position = points[i], Normals = normals[i % normals.Count], TexCoords = tex[i % tex.Count] }; } DataStream ds = new DataStream(vertices.Length * Pos3Norm3Tex2Vertex.VertexSize, true, true); ds.Position = 0; 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(); var indexstream = new DataStream(indices.Length * 4, true, true); indexstream.WriteRange(indices); indexstream.Position = 0; DX11IndexedGeometry geom = new DX11IndexedGeometry(context); geom.VertexBuffer = vbuffer; geom.IndexBuffer = new DX11IndexBuffer(context, indexstream, false, true); geom.InputLayout = Pos3Norm3Tex2Vertex.Layout; geom.Topology = PrimitiveTopology.TriangleList; geom.VerticesCount = count; geom.VertexSize = Pos3Norm3Tex2Vertex.VertexSize; geom.HasBoundingBox = false; return(geom); }
public void Update(IPluginIO pin, DX11RenderContext context) { for (int i = 0; i < this.FOutput.SliceCount; i++) { bool update = this.FInvalidate; DX11IndexedGeometry geom; if (!this.FOutput[i].Contains(context)) { geom = new DX11IndexedGeometry(context); geom.InputLayout = Pos3Norm3Tex2Vertex.Layout; geom.VertexSize = Pos3Norm3Tex2Vertex.VertexSize; geom.HasBoundingBox = false; geom.Topology = PrimitiveTopology.TriangleList; var indexstream = new DataStream(KinectRuntime.FACE_INDICES.Length * 4, true, true); indexstream.WriteRange(KinectRuntime.FACE_INDICES); indexstream.Position = 0; geom.IndexBuffer = new DX11IndexBuffer(context, indexstream, false, true); geom.VerticesCount = this.FInFrame[i].GetProjected3DShape().Count; var vbuffer = new SlimDX.Direct3D11.Buffer(context.Device, new BufferDescription() { BindFlags = BindFlags.VertexBuffer, CpuAccessFlags = CpuAccessFlags.Write, OptionFlags = ResourceOptionFlags.None, SizeInBytes = geom.VerticesCount * geom.VertexSize, Usage = ResourceUsage.Dynamic }); geom.VertexBuffer = vbuffer; this.FOutput[i][context] = geom; update = true; } else { geom = this.FOutput[i][context]; } if (update) { DataStream ds = geom.LockVertexBuffer(); ds.Position = 0; EnumIndexableCollection <FeaturePoint, PointF> pp = this.FInFrame[i].GetProjected3DShape(); EnumIndexableCollection <FeaturePoint, Vector3DF> p = this.FInFrame[i].Get3DShape(); Vector3[] norms = new Vector3[p.Count]; int[] inds = KinectRuntime.FACE_INDICES; int tricount = inds.Length / 3; //Compute smoothed normals for (int j = 0; j < tricount; j++) { int i1 = inds[j * 3]; int i2 = inds[j * 3 + 1]; int i3 = inds[j * 3 + 2]; Vector3 v1 = p[i1].SlimVector(); Vector3 v2 = p[i2].SlimVector(); Vector3 v3 = p[i3].SlimVector(); Vector3 faceEdgeA = v2 - v1; Vector3 faceEdgeB = v1 - v3; Vector3 norm = Vector3.Cross(faceEdgeB, faceEdgeA); norms[i1] += norm; norms[i2] += norm; norms[i3] += norm; } for (int j = 0; j < geom.VerticesCount; j++) { Pos3Norm3Tex2Vertex vertex = new Pos3Norm3Tex2Vertex(); Vector3DF v = p[j]; vertex.Position = new Vector3(v.X, v.Y, v.Z); vertex.Normals = Vector3.Normalize(norms[j]); vertex.TexCoords = new Vector2(0, 0); ds.Write <Pos3Norm3Tex2Vertex>(vertex); } geom.UnlockVertexBuffer(); } } }
public static IDX11Geometry Polygon2D(DX11RenderContext context, List <Vector2> points) { var count = points.Count; float cx = 0; float cy = 0; float x, y; float minx = float.MaxValue, miny = float.MaxValue; float maxx = float.MinValue, maxy = float.MinValue; Pos3Norm3Tex2Vertex[] vertices = new Pos3Norm3Tex2Vertex[count + 1]; for (int j = 0; j < count; j++) { var point = points[j]; x = point.X; y = point.Y; vertices[j + 1].Position = new Vector3(x, y, 0); vertices[j + 1].Normals = new Vector3(0, 0, -1); vertices[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; } } vertices[0].Position = new Vector3(cx / count, cy / count, 0); vertices[0].Normals = new Vector3(0, 0, -1); float w = maxx - minx; float h = maxy - miny; for (int j = 0; j <= count; j++) { vertices[j].TexCoords = new Vector2((vertices[j].Position.X - minx) / w, (vertices[j].Position.Y - miny) / h); } List <int> inds = new List <int>(); var indices = new int[count * 3]; var outerJ = 0; for (int j = 0; j < count - 1; j++) { outerJ = j * 3; indices[outerJ] = 0; indices[outerJ + 1] = j + 1; indices[outerJ + 2] = j + 2; } outerJ = count - 1; indices[outerJ] = 0; indices[outerJ + 1] = count - 1; indices[outerJ + 2] = 1; DataStream ds = new DataStream(vertices.Length * Pos3Norm3Tex2Vertex.VertexSize, true, true); ds.Position = 0; 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(); var indexstream = new DataStream(indices.Length * 4, true, true); indexstream.WriteRange(indices); indexstream.Position = 0; DX11IndexedGeometry geom = new DX11IndexedGeometry(context); geom.VertexBuffer = vbuffer; geom.IndexBuffer = new DX11IndexBuffer(context, indexstream, false, true); geom.InputLayout = Pos3Norm3Tex2Vertex.Layout; geom.Topology = PrimitiveTopology.TriangleList; geom.VerticesCount = count; geom.VertexSize = Pos3Norm3Tex2Vertex.VertexSize; geom.HasBoundingBox = false; return(geom); }