예제 #1
0
        private INode CreateCabin(Vector3 offset)
        {
            TriangleMesh mesh = new TriangleMesh();

            TriangleMeshFactory.CreateSphere(mesh, 0.2f, 20);
            TriangleMeshNode node = new TriangleMeshNode(mesh);

            TriangleMesh windowMesh = new TriangleMesh();

            TriangleMeshFactory.CreateSphere(windowMesh, 0.17f, 20);
            TriangleMeshNode windowNode = new TriangleMeshNode(windowMesh);

            windowNode.SetColor(Color.LightBlue);
            TranslationNode windowTranslation = new TranslationNode(new Vector3(0, 0.01f, -0.05f));

            windowTranslation.AddChild(windowNode);

            TranslationNode cabin = new TranslationNode(offset);

            cabin.AddChild(node);
            cabin.AddChild(windowTranslation);
            return(cabin);
        }
예제 #2
0
        private ITriangleMesh CreateBoundary(ITriangleMesh mesh, float height, float borderHeightTop, float borderHeightBottom, Vector3 offset, Vector2[] cornerTexCoords)
        {
            List <TriangleEdge> border       = mesh.GetBorder();
            int           vertsPerBorderEdge = 3;
            ITriangleMesh borderMesh         = new TriangleMesh();

            for (int i = 0; i < border.Count; i++)
            {
                borderMesh.AddVertex(mesh.GetVertex(border[i].A));
            }
            for (int i = 0; i < border.Count; i++)
            {
                TriangleEdge borderEdge = border[i];
                TriangleEdge prevEdge   = border[(i + border.Count - 1) % border.Count];
                Vector3      v          = borderMesh.GetVertex(i);

                // v0
                Vector3 edgeNormal     = ComputeBorderEdgeNormal(borderEdge, mesh);
                Vector3 prevEdgeNormal = ComputeBorderEdgeNormal(prevEdge, mesh);
                Vector3 borderNormal   = Vector3.Add(edgeNormal, prevEdgeNormal);
                borderNormal.Normalize();
                borderNormal = Vector3.Multiply(borderNormal, Math.Max(borderHeightTop, borderHeightBottom) / (float)Math.Sin(30.0 / 180.0 * Math.PI));
                Vector3 v0 = Vector3.Add(Vector3.Add(v, borderNormal), new Vector3(0, 0, -borderHeightTop));
                borderMesh.AddVertex(v0);

                // v1
                Vector3 v1 = Vector3.Add(v0, Vector3.Multiply(Vector3.UnitZ, -(height - (borderHeightTop + borderHeightBottom))));
                borderMesh.AddVertex(v1);

                // v2
                Vector3 v2 = Vector3.Add(v, Vector3.Multiply(Vector3.UnitZ, -height));
                borderMesh.AddVertex(v2);
            }

            float alpha = (float)(borderHeightTop * Math.Sqrt(2) / (borderHeightTop * Math.Sqrt(2) + borderHeightBottom * Math.Sqrt(2) + height));
            float beta  = 1 - (float)(borderHeightBottom * Math.Sqrt(2) / (borderHeightTop * Math.Sqrt(2) + borderHeightBottom * Math.Sqrt(2) + height));
            int   ti0   = borderMesh.AddTextureCoordinate(cornerTexCoords[0]);
            int   ti1   = borderMesh.AddTextureCoordinate(Vector2.Add(Vector2.Multiply(cornerTexCoords[0], 1 - alpha), Vector2.Multiply(cornerTexCoords[1], alpha)));
            int   ti2   = borderMesh.AddTextureCoordinate(Vector2.Add(Vector2.Multiply(cornerTexCoords[0], 1 - beta), Vector2.Multiply(cornerTexCoords[1], beta)));
            int   ti3   = borderMesh.AddTextureCoordinate(cornerTexCoords[1]);
            int   ti_0  = borderMesh.AddTextureCoordinate(cornerTexCoords[2]);
            int   ti_1  = borderMesh.AddTextureCoordinate(Vector2.Add(Vector2.Multiply(cornerTexCoords[2], 1 - alpha), Vector2.Multiply(cornerTexCoords[3], alpha)));
            int   ti_2  = borderMesh.AddTextureCoordinate(Vector2.Add(Vector2.Multiply(cornerTexCoords[2], 1 - beta), Vector2.Multiply(cornerTexCoords[3], beta)));
            int   ti_3  = borderMesh.AddTextureCoordinate(cornerTexCoords[3]);

            // Triangles
            for (int i = 0; i < border.Count; i++)
            {
                TriangleEdge borderEdge = border[i];
                int          vi0        = border.Count + vertsPerBorderEdge * i + 0;
                int          vi1        = border.Count + vertsPerBorderEdge * i + 1;
                int          vi2        = border.Count + vertsPerBorderEdge * i + 2;
                int          vi_0       = border.Count + vertsPerBorderEdge * ((i + 1) % border.Count) + 0;
                int          vi_1       = border.Count + vertsPerBorderEdge * ((i + 1) % border.Count) + 1;
                int          vi_2       = border.Count + vertsPerBorderEdge * ((i + 1) % border.Count) + 2;
                // A
                Triangle t = new Triangle(i, vi0, (i + 1) % border.Count, ti0, ti1, ti_1);
                borderMesh.AddTriangle(t);
                // B
                t = new Triangle((i + 1) % border.Count, vi0, vi_0, ti_0, ti1, ti_1);
                borderMesh.AddTriangle(t);
                // C
                t = new Triangle(vi0, vi1, vi_0, ti1, ti2, ti_1);
                borderMesh.AddTriangle(t);
                // D
                t = new Triangle(vi1, vi_1, vi_0, ti2, ti_2, ti_1);
                borderMesh.AddTriangle(t);
                // E
                t = new Triangle(vi1, vi2, vi_1, ti2, ti3, ti_2);
                borderMesh.AddTriangle(t);
                // F
                t = new Triangle(vi2, vi_2, vi_1, ti3, ti_3, ti_2);
                borderMesh.AddTriangle(t);
            }

            return(borderMesh);
        }