Example #1
0
        // A helper function, analogous to ProcessFaces.
        private void ProcessPoints(RenderingPassBufferStorage bufferStorage)
        {
            var points = bufferStorage.Points;

            if (points.Count == 0)
            {
                return;
            }

            // Edges are encoded as line segment primitives whose vertices contain only position information.
            bufferStorage.FormatBits = VertexFormatBits.PositionColored;

            //int edgeVertexBufferSizeInFloats = VertexPosition.GetSizeInFloats() * bufferStorage.VertexBufferCount;
            int vertexBufferSizeInFloats = VertexPositionColored.GetSizeInFloats() * bufferStorage.VertexBufferCount;

            int numPoints = points.Count;

            bufferStorage.VertexBuffer = new VertexBuffer(vertexBufferSizeInFloats);
            bufferStorage.VertexBuffer.Map(vertexBufferSizeInFloats);

            var vertexStream = bufferStorage.VertexBuffer.GetVertexStreamPositionColored();

            for (int i = 0; i < numPoints; i++)
            {
                var pointInfo = points[i];
                var vertex    = pointInfo.Vertex;
                vertexStream.AddVertex(new VertexPositionColored(vertex + pointInfo.Offset, pointInfo.Color));
            }

            bufferStorage.VertexBuffer.Unmap();

            bufferStorage.IndexBufferCount = bufferStorage.PrimitiveCount * IndexPoint.GetSizeInShortInts();
            int indexBufferSizeInShortInts = 1 * bufferStorage.IndexBufferCount;

            bufferStorage.IndexBuffer = new IndexBuffer(indexBufferSizeInShortInts);
            bufferStorage.IndexBuffer.Map(indexBufferSizeInShortInts);
            {
                IndexStreamPoint indexStream = bufferStorage.IndexBuffer.GetIndexStreamPoint();
                for (int i = 0; i < numPoints; i++)
                {
                    indexStream.AddPoint(new IndexPoint(i));
                }
            }
            bufferStorage.IndexBuffer.Unmap();

            bufferStorage.VertexFormat   = new VertexFormat(bufferStorage.FormatBits);
            bufferStorage.EffectInstance = new EffectInstance(bufferStorage.FormatBits);
        }
        public static int GetVertexSize(VertexFormatBits format)
        {
            switch (format)
            {
            case VertexFormatBits.Position: return(VertexPosition.GetSizeInFloats());

            case VertexFormatBits.PositionColored:
                return(VertexPositionColored.GetSizeInFloats());

            case VertexFormatBits.PositionNormal:
                return(VertexPositionNormal.GetSizeInFloats());

            case VertexFormatBits.PositionNormalColored:
                return(VertexPositionNormalColored.GetSizeInFloats());

            default: break;
            }
            return(VertexPosition.GetSizeInFloats());
        }
        protected static int ToPointsBuffer
        (
            Rhino.Geometry.PointCloud pointCloud,
            Primitive.Part part,
            out VertexFormatBits vertexFormatBits,
            out VertexBuffer vb, out int vertexCount,
            out IndexBuffer ib
        )
        {
            int pointsCount = part.VertexCount;
            int normalCount = pointCloud.ContainsNormals ? pointsCount : 0;
            int colorsCount = pointCloud.ContainsColors  ? pointsCount : 0;

            bool hasPoints  = pointsCount > 0;
            bool hasNormals = normalCount == pointsCount;
            bool hasColors  = colorsCount == pointsCount;

            if (hasPoints)
            {
                if (hasNormals)
                {
                    if (hasColors)
                    {
                        vertexFormatBits = VertexFormatBits.PositionNormalColored;
                        vb = new VertexBuffer(pointsCount * VertexPositionNormalColored.GetSizeInFloats());
                        vb.Map(pointsCount * VertexPositionNormalColored.GetSizeInFloats());

                        using (var vstream = vb.GetVertexStreamPositionNormalColored())
                        {
                            for (int p = part.StartVertexIndex; p < part.EndVertexIndex; ++p)
                            {
                                var point = pointCloud[p];
                                var c     = new ColorWithTransparency(point.Color.R, point.Color.G, point.Color.B, 255u - point.Color.A);
                                vstream.AddVertex(new VertexPositionNormalColored(RawEncoder.ToHost(point.Location), RawEncoder.ToHost(point.Normal), c));
                            }
                        }

                        vb.Unmap();
                    }
                    else
                    {
                        vertexFormatBits = VertexFormatBits.PositionNormal;
                        vb = new VertexBuffer(pointsCount * VertexPositionNormal.GetSizeInFloats());
                        vb.Map(pointsCount * VertexPositionNormal.GetSizeInFloats());

                        using (var vstream = vb.GetVertexStreamPositionNormal())
                        {
                            for (int p = part.StartVertexIndex; p < part.EndVertexIndex; ++p)
                            {
                                var point = pointCloud[p];
                                vstream.AddVertex(new VertexPositionNormal(RawEncoder.ToHost(point.Location), RawEncoder.ToHost(point.Normal)));
                            }
                        }

                        vb.Unmap();
                    }
                }
                else
                {
                    if (hasColors)
                    {
                        vertexFormatBits = VertexFormatBits.PositionColored;
                        vb = new VertexBuffer(pointsCount * VertexPositionColored.GetSizeInFloats());
                        vb.Map(pointsCount * VertexPositionColored.GetSizeInFloats());

                        using (var vstream = vb.GetVertexStreamPositionColored())
                        {
                            for (int p = part.StartVertexIndex; p < part.EndVertexIndex; ++p)
                            {
                                var point = pointCloud[p];
                                var c     = new ColorWithTransparency(point.Color.R, point.Color.G, point.Color.B, 255u - point.Color.A);
                                vstream.AddVertex(new VertexPositionColored(RawEncoder.ToHost(point.Location), c));
                            }
                        }

                        vb.Unmap();
                    }
                    else
                    {
                        vertexFormatBits = VertexFormatBits.Position;
                        vb = new VertexBuffer(pointsCount * VertexPosition.GetSizeInFloats());
                        vb.Map(pointsCount * VertexPosition.GetSizeInFloats());

                        using (var vstream = vb.GetVertexStreamPosition())
                        {
                            for (int p = part.StartVertexIndex; p < part.EndVertexIndex; ++p)
                            {
                                var point = pointCloud[p];
                                vstream.AddVertex(new VertexPosition(RawEncoder.ToHost(point.Location)));
                            }
                        }

                        vb.Unmap();
                    }
                }

                ib = IndexPointsBuffer(pointsCount);
            }
            else
            {
                vertexFormatBits = 0;
                vb = null;
                ib = null;
            }

            vertexCount = pointsCount;
            return(pointsCount);
        }
        protected static VertexBuffer ToVertexBuffer
        (
            Rhino.Geometry.Mesh mesh,
            Primitive.Part part,
            out VertexFormatBits vertexFormatBits,
            System.Drawing.Color color = default
        )
        {
            int verticesCount = part.EndVertexIndex - part.StartVertexIndex;
            int normalCount   = mesh.Normals.Count == mesh.Vertices.Count ? verticesCount : 0;
            int colorsCount   = color.IsEmpty ? (mesh.VertexColors.Count == mesh.Vertices.Count ? verticesCount : 0) : verticesCount;

            bool hasVertices = verticesCount > 0;
            bool hasNormals  = normalCount > 0;
            bool hasColors   = colorsCount > 0;

            if (hasVertices)
            {
                var vertices = mesh.Vertices;
                if (hasNormals)
                {
                    var normals = mesh.Normals;
                    if (hasColors)
                    {
                        vertexFormatBits = VertexFormatBits.PositionNormalColored;
                        var colors = mesh.VertexColors;
                        var vb     = new VertexBuffer(verticesCount * VertexPositionNormalColored.GetSizeInFloats());
                        vb.Map(verticesCount * VertexPositionNormalColored.GetSizeInFloats());
                        using (var stream = vb.GetVertexStreamPositionNormalColored())
                        {
                            for (int v = part.StartVertexIndex; v < part.EndVertexIndex; ++v)
                            {
                                var  c = !color.IsEmpty ? color : colors[v];
                                uint T = Math.Max(1, 255u - c.A);
                                stream.AddVertex(new VertexPositionNormalColored(RawEncoder.ToHost(vertices[v]), RawEncoder.ToHost(normals[v]), new ColorWithTransparency(c.R, c.G, c.B, T)));
                            }
                        }
                        vb.Unmap();
                        return(vb);
                    }
                    else
                    {
                        vertexFormatBits = VertexFormatBits.PositionNormal;
                        var vb = new VertexBuffer(verticesCount * VertexPositionNormal.GetSizeInFloats());
                        vb.Map(verticesCount * VertexPositionNormal.GetSizeInFloats());
                        using (var stream = vb.GetVertexStreamPositionNormal())
                        {
                            for (int v = part.StartVertexIndex; v < part.EndVertexIndex; ++v)
                            {
                                stream.AddVertex(new VertexPositionNormal(RawEncoder.ToHost(vertices[v]), RawEncoder.ToHost(normals[v])));
                            }
                        }
                        vb.Unmap();
                        return(vb);
                    }
                }
                else
                {
                    if (hasColors)
                    {
                        vertexFormatBits = VertexFormatBits.PositionColored;
                        var colors = mesh.VertexColors;
                        var vb     = new VertexBuffer(verticesCount * VertexPositionColored.GetSizeInFloats());
                        vb.Map(verticesCount * VertexPositionColored.GetSizeInFloats());
                        using (var stream = vb.GetVertexStreamPositionColored())
                        {
                            for (int v = part.StartVertexIndex; v < part.EndVertexIndex; ++v)
                            {
                                var  c = !color.IsEmpty ? color : colors[v];
                                uint T = Math.Max(1, 255u - c.A);
                                stream.AddVertex(new VertexPositionColored(RawEncoder.ToHost(vertices[v]), new ColorWithTransparency(c.R, c.G, c.B, T)));
                            }
                        }
                        vb.Unmap();
                        return(vb);
                    }
                    else
                    {
                        vertexFormatBits = VertexFormatBits.Position;
                        var vb = new VertexBuffer(verticesCount * VertexPosition.GetSizeInFloats());
                        vb.Map(verticesCount * VertexPosition.GetSizeInFloats());
                        using (var stream = vb.GetVertexStreamPosition())
                        {
                            for (int v = part.StartVertexIndex; v < part.EndVertexIndex; ++v)
                            {
                                stream.AddVertex(new VertexPosition(RawEncoder.ToHost(vertices[v])));
                            }
                        }
                        vb.Unmap();
                        return(vb);
                    }
                }
            }

            vertexFormatBits = 0;
            return(null);
        }
Example #5
0
        // A helper function, analogous to ProcessFaces.
        private void ProcessEdges(RenderingPassBufferStorage bufferStorage)
        {
            var edges = bufferStorage.Edges;

            if (edges.Count == 0)
            {
                return;
            }

            // Edges are encoded as line segment primitives whose vertices contain only position information.
            bufferStorage.FormatBits = VertexFormatBits.PositionColored;

            //int edgeVertexBufferSizeInFloats = VertexPosition.GetSizeInFloats() * bufferStorage.VertexBufferCount;
            int        edgeVertexBufferSizeInFloats = VertexPositionColored.GetSizeInFloats() * bufferStorage.VertexBufferCount;
            List <int> numVerticesInEdgesBefore     = new List <int>();

            numVerticesInEdgesBefore.Add(0);

            int numEdges = edges.Count;

            bufferStorage.VertexBuffer = new VertexBuffer(edgeVertexBufferSizeInFloats);
            bufferStorage.VertexBuffer.Map(edgeVertexBufferSizeInFloats);

            var vertexStream = bufferStorage.VertexBuffer.GetVertexStreamPositionColored();

            for (int i = 0; i < numEdges; i++)
            {
                var edgeInfo = edges[i];
                foreach (XYZ vertex in edgeInfo.Vertices)
                {
                    vertexStream.AddVertex(new VertexPositionColored(vertex + edgeInfo.Offset, edgeInfo.Color));
                }

                numVerticesInEdgesBefore.Add(numVerticesInEdgesBefore.Last() + edgeInfo.Vertices.Count);
            }

            bufferStorage.VertexBuffer.Unmap();

            int edgeNumber = 0;

            bufferStorage.IndexBufferCount = bufferStorage.PrimitiveCount * IndexLine.GetSizeInShortInts();
            int indexBufferSizeInShortInts = 1 * bufferStorage.IndexBufferCount;

            bufferStorage.IndexBuffer = new IndexBuffer(indexBufferSizeInShortInts);
            bufferStorage.IndexBuffer.Map(indexBufferSizeInShortInts);
            {
                IndexStreamLine indexStream = bufferStorage.IndexBuffer.GetIndexStreamLine();
                foreach (var edgeInfo in edges)
                {
                    var xyzs       = edgeInfo.Vertices;
                    int startIndex = numVerticesInEdgesBefore[edgeNumber];
                    for (int i = 1; i < xyzs.Count; i++)
                    {
                        // Add two indices that define a line segment.
                        indexStream.AddLine(new IndexLine((int)(startIndex + i - 1),
                                                          (int)(startIndex + i)));
                    }
                    edgeNumber++;
                }
            }
            bufferStorage.IndexBuffer.Unmap();

            bufferStorage.VertexFormat   = new VertexFormat(bufferStorage.FormatBits);
            bufferStorage.EffectInstance = new EffectInstance(bufferStorage.FormatBits);
        }
Example #6
0
        // Create and populate a pair of vertex and index buffers. Also update parameters associated with the format of the vertices.
        private void ProcessTriangles(RenderingPassBufferStorage bufferStorage)
        {
            List <TriangleInfo> triangles = bufferStorage.Triangles;

            if (triangles.Count == 0)
            {
                return;
            }

            bool useNormals = this.Inputs.EnableFaceNormal;

            // Vertex attributes are stored sequentially in vertex buffers. The attributes can include position, normal vector, and color.
            // All vertices within a vertex buffer must have the same format. Possible formats are enumerated by VertexFormatBits.
            // Vertex format also determines the type of rendering effect that can be used with the vertex buffer. In this sample,
            // the color is always encoded in the vertex attributes.

            bufferStorage.FormatBits = useNormals ? VertexFormatBits.PositionNormalColored : VertexFormatBits.PositionColored;

            // The format of the vertices determines the size of the vertex buffer.
            int vertexBufferSizeInFloats = (useNormals ? VertexPositionNormalColored.GetSizeInFloats() : VertexPositionColored.GetSizeInFloats()) *
                                           bufferStorage.VertexBufferCount;

            bufferStorage.VertexBuffer = new VertexBuffer(vertexBufferSizeInFloats);
            bufferStorage.VertexBuffer.Map(vertexBufferSizeInFloats);

            int numTriangles = triangles.Count;

            if (useNormals)
            {
                // A VertexStream is used to write data into a VertexBuffer.
                VertexStreamPositionNormalColored vertexStream = bufferStorage.VertexBuffer.GetVertexStreamPositionNormalColored();
                for (int i = 0; i < numTriangles; i++)
                {
                    var           triangleInfo = triangles[i];
                    g3.Triangle3d triangle     = triangleInfo.Triangle;
                    vertexStream.AddVertex(new VertexPositionNormalColored(triangle.V0.ToXYZ() + triangleInfo.Offset, triangleInfo.Normal, triangleInfo.ColorWithTransparency));
                    vertexStream.AddVertex(new VertexPositionNormalColored(triangle.V1.ToXYZ() + triangleInfo.Offset, triangleInfo.Normal, triangleInfo.ColorWithTransparency));
                    vertexStream.AddVertex(new VertexPositionNormalColored(triangle.V2.ToXYZ() + triangleInfo.Offset, triangleInfo.Normal, triangleInfo.ColorWithTransparency));
                }
            }
            else
            {
                // A VertexStream is used to write data into a VertexBuffer.
                VertexStreamPositionColored vertexStream = bufferStorage.VertexBuffer.GetVertexStreamPositionColored();
                for (int i = 0; i < numTriangles; i++)
                {
                    var           triangleInfo = triangles[i];
                    g3.Triangle3d triangle     = triangleInfo.Triangle;
                    // make the color of all faces white in HLR
                    ColorWithTransparency color = triangleInfo.ColorWithTransparency;
                    vertexStream.AddVertex(new VertexPositionColored(triangle.V0.ToXYZ() + triangleInfo.Offset, color));
                    vertexStream.AddVertex(new VertexPositionColored(triangle.V1.ToXYZ() + triangleInfo.Offset, color));
                    vertexStream.AddVertex(new VertexPositionColored(triangle.V2.ToXYZ() + triangleInfo.Offset, color));
                }
            }

            bufferStorage.VertexBuffer.Unmap();

            // Primitives are specified using a pair of vertex and index buffers. An index buffer contains a sequence of indices into
            // the associated vertex buffer, each index referencing a particular vertex.

            bufferStorage.IndexBufferCount = bufferStorage.PrimitiveCount * IndexTriangle.GetSizeInShortInts();
            int indexBufferSizeInShortInts = 1 * bufferStorage.IndexBufferCount;

            bufferStorage.IndexBuffer = new IndexBuffer(indexBufferSizeInShortInts);
            bufferStorage.IndexBuffer.Map(indexBufferSizeInShortInts);
            // An IndexStream is used to write data into an IndexBuffer.
            IndexStreamTriangle indexStream = bufferStorage.IndexBuffer.GetIndexStreamTriangle();
            int currIndex = 0;

            for (int i = 0; i < numTriangles; i++)
            {
                // Add three indices that define a triangle.
                indexStream.AddTriangle(new IndexTriangle(currIndex + 0, currIndex + 1, currIndex + 2));
                currIndex += 3;
            }
            bufferStorage.IndexBuffer.Unmap();

            // VertexFormat is a specification of the data that is associated with a vertex (e.g., position).
            bufferStorage.VertexFormat = new VertexFormat(bufferStorage.FormatBits);
            // Effect instance is a specification of the appearance of geometry. For example, it may be used to specify color, if there is no color information provided with the vertices.
            bufferStorage.EffectInstance = new EffectInstance(bufferStorage.FormatBits);
        }
Example #7
0
        // Create and populate a pair of vertex and index buffers. Also update parameters associated with the format of the vertices.
        private void ProcessFaces(RenderingPassBufferStorage bufferStorage)
        {
            List <MeshInfo> meshes = bufferStorage.Meshes;

            if (meshes.Count == 0)
            {
                return;
            }
            List <int> numVerticesInMeshesBefore = new List <int>();

            bool useNormals = this.Inputs.EnableFaceNormal;

            // Vertex attributes are stored sequentially in vertex buffers. The attributes can include position, normal vector, and color.
            // All vertices within a vertex buffer must have the same format. Possible formats are enumerated by VertexFormatBits.
            // Vertex format also determines the type of rendering effect that can be used with the vertex buffer. In this sample,
            // the color is always encoded in the vertex attributes.

            bufferStorage.FormatBits = useNormals ? VertexFormatBits.PositionNormalColored : VertexFormatBits.PositionColored;

            // The format of the vertices determines the size of the vertex buffer.
            int vertexBufferSizeInFloats = (useNormals ? VertexPositionNormalColored.GetSizeInFloats() : VertexPositionColored.GetSizeInFloats()) *
                                           bufferStorage.VertexBufferCount;

            numVerticesInMeshesBefore.Add(0);

            bufferStorage.VertexBuffer = new VertexBuffer(vertexBufferSizeInFloats);
            bufferStorage.VertexBuffer.Map(vertexBufferSizeInFloats);

            int numMeshes = meshes.Count;

            if (useNormals)
            {
                // A VertexStream is used to write data into a VertexBuffer.
                VertexStreamPositionNormalColored vertexStream = bufferStorage.VertexBuffer.GetVertexStreamPositionNormalColored();
                for (int i = 0; i < numMeshes; i++)
                {
                    var  meshInfo = meshes[i];
                    Mesh mesh     = meshInfo.Mesh;
                    foreach (XYZ vertex in mesh.Vertices)
                    {
                        vertexStream.AddVertex(new VertexPositionNormalColored(vertex + meshInfo.Offset, meshInfo.Normal, meshInfo.ColorWithTransparency));
                    }

                    numVerticesInMeshesBefore.Add(numVerticesInMeshesBefore.Last() + mesh.Vertices.Count);
                }
            }
            else
            {
                // A VertexStream is used to write data into a VertexBuffer.
                VertexStreamPositionColored vertexStream = bufferStorage.VertexBuffer.GetVertexStreamPositionColored();
                for (int i = 0; i < numMeshes; i++)
                {
                    var  meshInfo = meshes[i];
                    Mesh mesh     = meshInfo.Mesh;
                    // make the color of all faces white in HLR
                    ColorWithTransparency color = meshInfo.ColorWithTransparency;
                    foreach (XYZ vertex in mesh.Vertices)
                    {
                        vertexStream.AddVertex(new VertexPositionColored(vertex + meshInfo.Offset, color));
                    }

                    numVerticesInMeshesBefore.Add(numVerticesInMeshesBefore.Last() + mesh.Vertices.Count);
                }
            }

            bufferStorage.VertexBuffer.Unmap();

            // Primitives are specified using a pair of vertex and index buffers. An index buffer contains a sequence of indices into
            // the associated vertex buffer, each index referencing a particular vertex.

            int meshNumber = 0;

            bufferStorage.IndexBufferCount = bufferStorage.PrimitiveCount * IndexTriangle.GetSizeInShortInts();
            int indexBufferSizeInShortInts = 1 * bufferStorage.IndexBufferCount;

            bufferStorage.IndexBuffer = new IndexBuffer(indexBufferSizeInShortInts);
            bufferStorage.IndexBuffer.Map(indexBufferSizeInShortInts);
            {
                // An IndexStream is used to write data into an IndexBuffer.
                IndexStreamTriangle indexStream = bufferStorage.IndexBuffer.GetIndexStreamTriangle();
                foreach (MeshInfo meshInfo in meshes)
                {
                    Mesh mesh       = meshInfo.Mesh;
                    int  startIndex = numVerticesInMeshesBefore[meshNumber];
                    for (int i = 0; i < mesh.NumTriangles; i++)
                    {
                        MeshTriangle mt = mesh.get_Triangle(i);
                        // Add three indices that define a triangle.
                        indexStream.AddTriangle(new IndexTriangle((int)(startIndex + mt.get_Index(0)),
                                                                  (int)(startIndex + mt.get_Index(1)),
                                                                  (int)(startIndex + mt.get_Index(2))));
                    }
                    meshNumber++;
                }
            }
            bufferStorage.IndexBuffer.Unmap();

            // VertexFormat is a specification of the data that is associated with a vertex (e.g., position).
            bufferStorage.VertexFormat = new VertexFormat(bufferStorage.FormatBits);
            // Effect instance is a specification of the appearance of geometry. For example, it may be used to specify color, if there is no color information provided with the vertices.
            bufferStorage.EffectInstance = new EffectInstance(bufferStorage.FormatBits);
        }