RenderStack 11.06.1
Public Member Functions | Properties
RenderStack.Mesh.GeometryMesh Class Reference

Converts Geometry to Mesh. More...

Inheritance diagram for RenderStack.Mesh.GeometryMesh:
RenderStack.Mesh.IMeshSource

List of all members.

Public Member Functions

 GeometryMesh (Geometry.Geometry geometry, NormalStyle normalStyle)
 GeometryMesh ()
 GeometryMesh (BufferUsageHint bufferUsageHint)
void Reset ()
void BuildMeshFromGeometry (NormalStyle normalStyle)

Properties

Geometry.Geometry Geometry [get, set]
Mesh GetMesh [get, set]

Detailed Description

Warning:
Does not currently optimize.
Todo:
Add vertex and index buffer optimizations.
Note:
Mostly stable. somewhat experimental.

Definition at line 46 of file GeometryMesh.cs.


Constructor & Destructor Documentation

RenderStack.Mesh.GeometryMesh.GeometryMesh ( Geometry.Geometry  geometry,
NormalStyle  normalStyle 
)

Definition at line 51 of file GeometryMesh.cs.

        {
            Geometry = geometry;
            GetMesh = new Mesh(BufferUsageHint.StaticDraw);
            BuildMeshFromGeometry(normalStyle);
        }
RenderStack.Mesh.GeometryMesh.GeometryMesh ( )

Definition at line 60 of file GeometryMesh.cs.

References RenderStack.Geometry.Geometry.Geometry().

        {
            Geometry = new Geometry.Geometry();
            GetMesh = new Mesh(BufferUsageHint.StaticDraw);
        }
RenderStack.Mesh.GeometryMesh.GeometryMesh ( BufferUsageHint  bufferUsageHint)

Definition at line 65 of file GeometryMesh.cs.

References RenderStack.Geometry.Geometry.Geometry().

        {
            Geometry = new Geometry.Geometry();
            GetMesh = new Mesh(bufferUsageHint);
        }

Member Function Documentation

void RenderStack.Mesh.GeometryMesh.Reset ( )

Definition at line 70 of file GeometryMesh.cs.

References RenderStack.Geometry.Geometry.Geometry().

        {
            Geometry = new Geometry.Geometry();
            GetMesh = new Mesh(BufferUsageHint.StaticDraw);
        }
void RenderStack.Mesh.GeometryMesh.BuildMeshFromGeometry ( NormalStyle  normalStyle)

Definition at line 75 of file GeometryMesh.cs.

References RenderStack.Geometry.Edge.A, RenderStack.Geometry.Edge.B, RenderStack.Graphics.Buffer.BeginEdit(), RenderStack.Geometry.Geometry.BuildEdges(), RenderStack.Geometry.Geometry.ComputePointNormals(), RenderStack.Geometry.Geometry.ComputePolygonCentroids(), RenderStack.Geometry.Geometry.ComputePolygonNormals(), RenderStack.Geometry.Geometry.CornerAttributes, RenderStack.Geometry.Point.Corners, RenderStack.Geometry.Polygon.Corners, RenderStack.Geometry.Geometry.Edges, RenderStack.Geometry.Corner.Point, RenderStack.Geometry.Geometry.PointAttributes, RenderStack.Geometry.Geometry.PolygonAttributes, RenderStack.Geometry.Geometry.Polygons, RenderStack.Geometry.Geometry.SmoothNormalize(), RenderStack.Math.Vector3.UnitY, RenderStack.Math.Vector3.Vector3FromUint(), RenderStack.Math.Vector3.X, RenderStack.Math.Vector3.Y, and RenderStack.Math.Vector3.Z.

        {
            GetMesh.ClearProgramAttributeBindings();

            if(Geometry.PolygonAttributes.Contains<Vector3>("polygon_normals") == false)
            {
                Geometry.ComputePolygonNormals();
            }
            Geometry.ComputePointNormals("point_normals_smooth");

            if(Geometry.PointAttributes.Contains<Vector3>("polygon_centroids") == false)
            {
                Geometry.ComputePolygonCentroids();
            }

            var polygonIdsVector3   = Geometry.PolygonAttributes.FindOrCreate<Vector3>("polygon_ids_vec3");
            var polygonIdsUInt32    = (RenderStack.Graphics.Configuration.useIntegerPolygonIDs)
                                    ? Geometry.PolygonAttributes.FindOrCreate<UInt32>("polygon_ids_uint")
                                    : null;

            Dictionary<Corner, Vector3> cornerNormals = null;
            Dictionary<Point, Vector3>  pointNormals  = null;
            Dictionary<Point, Vector3>  pointNormalsSmooth = Geometry.PointAttributes.Find<Vector3>("point_normals_smooth");

            bool normalsFound = false;
            if(Geometry.CornerAttributes.Contains<Vector3>("corner_normals"))
            {
                cornerNormals = Geometry.CornerAttributes.Find<Vector3>("corner_normals");
                normalsFound = true;
            }
            if(Geometry.PointAttributes.Contains<Vector3>("point_normals"))
            {
                pointNormals = Geometry.PointAttributes.Find<Vector3>("point_normals");
                normalsFound = true;
            }
            if(normalsFound == false)
            {
                //Geometry.ComputeCornerNormals(0.0f * (float)System.Math.PI);
                Geometry.SmoothNormalize("corner_normals", "polygon_normals", (0.0f * (float)System.Math.PI));
                cornerNormals = Geometry.CornerAttributes.Find<Vector3>("corner_normals");
            }

            Dictionary<Corner, Vector2> cornerTexcoords = null;
            Dictionary<Point, Vector2>  pointTexcoords  = null;

            if(Geometry.CornerAttributes.Contains<Vector2>("corner_texcoords"))
            {
                cornerTexcoords = Geometry.CornerAttributes.Find<Vector2>("corner_texcoords");
            }
            if(Geometry.PointAttributes.Contains<Vector2>("point_texcoords"))
            {
                pointTexcoords = Geometry.PointAttributes.Find<Vector2>("point_texcoords");
            }

            Dictionary<Corner, Vector4> cornerColors = null;
            Dictionary<Point, Vector4>  pointColors = null;
            if(Geometry.CornerAttributes.Contains<Vector4>("corner_colors"))
            {
                cornerColors = Geometry.CornerAttributes.Find<Vector4>("corner_colors");
            }
            if(Geometry.PointAttributes.Contains<Vector4>("point_colors"))
            {
                pointColors = Geometry.PointAttributes.Find<Vector4>("point_colors");
            }

            var polygonNormals   = Geometry.PolygonAttributes.Find<Vector3>("polygon_normals");
            var polygonCentroids = Geometry.PolygonAttributes.Find<Vector3>("polygon_centroids");
            var pointLocations   = Geometry.PointAttributes.Find<Vector3>("point_locations");
            var cornerIndices    = Geometry.CornerAttributes.FindOrCreate<uint>("corner_indices");

            #region prepare vertex format
            var attributePosition       = new Attribute(VertexUsage.Position, VertexAttribPointerType.Float, 0, 3);
            var attributeNormal         = new Attribute(VertexUsage.Normal,   VertexAttribPointerType.Float, 0, 3);   /*  content normals     */
            var attributeNormalFlat     = new Attribute(VertexUsage.Normal,   VertexAttribPointerType.Float, 1, 3);   /*  flat normals        */
            var attributeNormalSmooth   = new Attribute(VertexUsage.Normal,   VertexAttribPointerType.Float, 2, 3);   /*  smooth normals      */
            var attributeColor          = new Attribute(VertexUsage.Color,    VertexAttribPointerType.Float, 0, 4);
            var attributeIdVec3         = new Attribute(VertexUsage.Id,       VertexAttribPointerType.Float, 0, 3);
            var attributeIdUInt         = (RenderStack.Graphics.Configuration.useIntegerPolygonIDs) 
                                        ? new Attribute(VertexUsage.Id,       VertexAttribPointerType.UnsignedInt, 0, 1)
                                        : null;

            GetMesh.VertexBuffer.VertexFormat.Clear();
            GetMesh.VertexBuffer.VertexFormat.Add(attributePosition);
            GetMesh.VertexBuffer.VertexFormat.Add(attributeNormal);
            GetMesh.VertexBuffer.VertexFormat.Add(attributeNormalFlat);
            GetMesh.VertexBuffer.VertexFormat.Add(attributeNormalSmooth);
            if(cornerTexcoords != null || pointTexcoords != null)
            {
                var attributeTexcoord = new Attribute(VertexUsage.TexCoord, VertexAttribPointerType.Float, 0, 2);
                GetMesh.VertexBuffer.VertexFormat.Add(attributeTexcoord);
            }
            //if(cornerColors != null || pointColors != null)
            //{
                GetMesh.VertexBuffer.VertexFormat.Add(attributeColor);
            //}
            GetMesh.VertexBuffer.VertexFormat.Add(attributeIdVec3);
            if(RenderStack.Graphics.Configuration.useIntegerPolygonIDs)
            {
                GetMesh.VertexBuffer.VertexFormat.Add(attributeIdUInt);
            }
            #endregion prepare vertex format
            #region prepare index buffers
            Buffer polygonFillIndices = GetMesh.FindOrCreateIndexBuffer(
                MeshMode.PolygonFill, 
                BufferTarget.ElementArrayBuffer, 
                BufferUsageHint.StaticDraw, 
                DrawElementsType.UnsignedInt, 
                BeginMode.Triangles
            );
            Buffer edgeLineIndices = GetMesh.FindOrCreateIndexBuffer(
                MeshMode.EdgeLines,
                BufferTarget.ElementArrayBuffer,
                BufferUsageHint.StaticDraw,
                DrawElementsType.UnsignedInt,
                BeginMode.Lines
            );
            /*Buffer silhouetteLineIndices = GetMesh.FindOrCreateIndexBuffer(
                MeshMode.EdgeLines,
                BufferTarget.ElementArrayBuffer,
                BufferUsageHint.StaticDraw,
                DrawElementsType.UnsignedInt,
                BeginMode.Lines
            );*/
            Buffer cornerPointIndices = GetMesh.FindOrCreateIndexBuffer(
                MeshMode.CornerPoints, 
                BufferTarget.ElementArrayBuffer, 
                BufferUsageHint.StaticDraw, 
                DrawElementsType.UnsignedInt,
                BeginMode.Points
            );
            Buffer polygonCentroidIndices = GetMesh.FindOrCreateIndexBuffer(
                MeshMode.PolygonCentroids,
                BufferTarget.ElementArrayBuffer,
                BufferUsageHint.StaticDraw,
                DrawElementsType.UnsignedInt,
                BeginMode.Points
            );
            #endregion prepare index buffers

            GetMesh.VertexBuffer.BeginEdit();
            polygonFillIndices.BeginEdit();
            edgeLineIndices.BeginEdit();
            cornerPointIndices.BeginEdit();
            polygonCentroidIndices.BeginEdit();

            UInt32 polygonIndex = 0;

            #region polygons
            cornerIndices.Clear();
            foreach(Polygon polygon in Geometry.Polygons)
            {
                if(RenderStack.Graphics.Configuration.useIntegerPolygonIDs)
                {
                    polygonIdsUInt32[polygon] = polygonIndex;
                }
                polygonIdsVector3[polygon] = Vector3.Vector3FromUint(polygonIndex);

                Vector3 polygonNormal = Vector3.UnitY;

                if(polygon.Corners.Count > 2 && polygonNormals != null && polygonNormals.ContainsKey(polygon))
                {
                    polygonNormal = polygonNormals[polygon];
                }

                uint firstIndex    = GetMesh.VertexBuffer.CurrentIndex;
                uint previousIndex = firstIndex;

                //Corner firstCorner   = polygon.Corners[0];
                //Point  firstPoint    = firstCorner.Point;

                //Vector3 referenceLocation = pointLocations[firstPoint];

                #region corners
                foreach(Corner corner in polygon.Corners)
                {
                    //cornerVertexIndices[corner] = Mesh.VertexBuffer.CurrentIndex;

                    //  Position
                    GetMesh.VertexBuffer.Position(pointLocations[corner.Point]);

                    //  Normal
                    Vector3 normal = Vector3.UnitY;
                    if(
                        (cornerNormals != null) &&
                        (polygon.Corners.Count > 2) &&
                        (cornerNormals.ContainsKey(corner) == true)
                    )
                    {
                        normal = cornerNormals[corner];
                    }
                    else if(pointNormals != null && pointNormals.ContainsKey(corner.Point))
                    {
                        normal = pointNormals[corner.Point];
                    }
                    else if(pointNormalsSmooth != null && pointNormalsSmooth.ContainsKey(corner.Point))
                    {
                        normal = pointNormalsSmooth[corner.Point];
                    }
                    Vector3 pointNormal = Vector3.UnitY;
                    if(pointNormals != null && pointNormals.ContainsKey(corner.Point))
                    {
                        pointNormal = pointNormals[corner.Point];
                    }
                    else if(pointNormalsSmooth != null && pointNormalsSmooth.ContainsKey(corner.Point))
                    {
                        pointNormal = pointNormalsSmooth[corner.Point];
                    }

                    switch(normalStyle)
                    {
                        case NormalStyle.CornerNormals:     GetMesh.VertexBuffer.Normal(normal); break;
                        case NormalStyle.PointNormals:      GetMesh.VertexBuffer.Normal(pointNormal); break;
                        case NormalStyle.PolygonNormals:    GetMesh.VertexBuffer.Normal(polygonNormal); break;
                    }

                    GetMesh.VertexBuffer.Set(attributeNormalFlat, polygonNormal);
                    GetMesh.VertexBuffer.Set(attributeNormalSmooth, pointNormalsSmooth[corner.Point]);

                    //  Texcoord
                    if(
                        (cornerTexcoords != null) &&
                        (cornerTexcoords.ContainsKey(corner) == true)
                    )
                    {
                        GetMesh.VertexBuffer.TexCoord(cornerTexcoords[corner]);
                    }
                    else if(
                        (pointTexcoords != null) &&
                        (pointTexcoords.ContainsKey(corner.Point) == true)
                    )
                    {
                        GetMesh.VertexBuffer.TexCoord(pointTexcoords[corner.Point]);
                    }

                    //  Vertex Color
                    if(
                        (cornerColors != null) &&
                        (cornerColors.ContainsKey(corner) == true)
                    )
                    {
                        GetMesh.VertexBuffer.Set(attributeColor, cornerColors[corner]);
                    }
                    else if(
                        (pointColors != null) &&
                        (pointColors.ContainsKey(corner.Point) == true)
                    )
                    {
                        GetMesh.VertexBuffer.Set(attributeColor, pointColors[corner.Point]);
                    }
                    else
                    {
                        Vector3 color = new Vector3(1.0f, 1.0f, 1.0f); // + 0.5f * normal;
                        GetMesh.VertexBuffer.Set(attributeColor, color.X, color.Y, color.Z, 1.0f);
                    }

                    //  PolygonId
                    if(RenderStack.Graphics.Configuration.useIntegerPolygonIDs)
                    {
                        GetMesh.VertexBuffer.Set(attributeIdUInt, polygonIndex);
                    }
                    Vector3 v = Vector3.Vector3FromUint(polygonIndex);
                    GetMesh.VertexBuffer.Set(attributeIdVec3, v.X, v.Y, v.Z);

                    cornerPointIndices.Point(GetMesh.VertexBuffer.CurrentIndex);
                    cornerPointIndices.CurrentIndex++;

                    cornerIndices[corner] = GetMesh.VertexBuffer.CurrentIndex;

                    if(previousIndex != firstIndex)
                    {
                        polygonFillIndices.Triangle(firstIndex, GetMesh.VertexBuffer.CurrentIndex, previousIndex);
                        polygonFillIndices.CurrentIndex += 3;
                    }

                    previousIndex = GetMesh.VertexBuffer.CurrentIndex;

                    ++GetMesh.VertexBuffer.CurrentIndex;
                }
                #endregion corners

                ++polygonIndex;
            }
            #endregion polygons

            #region edges
            Geometry.BuildEdges();

            foreach(Edge edge in Geometry.Edges.Keys)
            {
                if(
                    cornerIndices.ContainsKey(edge.A.Corners[0]) && 
                    cornerIndices.ContainsKey(edge.B.Corners[0])
                )
                {
                    uint i0 = cornerIndices[edge.A.Corners[0]];
                    uint i1 = cornerIndices[edge.B.Corners[0]];
                    edgeLineIndices.Line(i0, i1);
                    edgeLineIndices.CurrentIndex += 2;
                }
            }
            #endregion edges

            #region polygon centroids
            foreach(Polygon polygon in Geometry.Polygons)
            {
                Vector3 normal;

                if(polygon.Corners.Count > 2)
                {
                    normal = polygonNormals[polygon];
                }
                else
                {
                    normal = new Vector3(0.0f, 1.0f, 0.0f);
                }

                GetMesh.VertexBuffer.Position(polygonCentroids[polygon]);
                GetMesh.VertexBuffer.Set     (attributeNormal, normal);
                GetMesh.VertexBuffer.Set     (attributeNormalFlat, normal);
                polygonCentroidIndices.Point(GetMesh.VertexBuffer.CurrentIndex);
                ++GetMesh.VertexBuffer.CurrentIndex;
                ++polygonCentroidIndices.CurrentIndex;
            }
            #endregion polygon centroids

            GetMesh.VertexBuffer.EndEdit();
            polygonFillIndices.EndEdit();
            edgeLineIndices.EndEdit();
            cornerPointIndices.EndEdit();
            polygonCentroidIndices.EndEdit();

        }

Property Documentation

Geometry.Geometry RenderStack.Mesh.GeometryMesh.Geometry [get, set]

Definition at line 48 of file GeometryMesh.cs.

Mesh RenderStack.Mesh.GeometryMesh.GetMesh [get, set]

The documentation for this class was generated from the following file: