Exemplo n.º 1
0
        // transform input point pt1, (rotate "aX, aZ" and move to "center")
        public static void Transform(Mesh3D model, Point3D center, double aX, double aZ)
        {
            double angleX = 3.1415926f * aX / 180;
            double angleZ = 3.1415926f * aZ / 180;

            int nVertNo = model.GetVertexNo();

            for (int i = 0; i < nVertNo; i++)
            {
                Point3D pt1 = model.GetPoint(i);
                // rotate from z-axis
                double x2 = pt1.X * Math.Cos(angleZ) + pt1.Z * Math.Sin(angleZ);
                double y2 = pt1.Y;
                double z2 = -pt1.X * Math.Sin(angleZ) + pt1.Z * Math.Cos(angleZ);

                double x3 = center.X + x2 * Math.Cos(angleX) - y2 * Math.Sin(angleX);
                double y3 = center.Y + x2 * Math.Sin(angleX) + y2 * Math.Cos(angleX);
                double z3 = center.Z + z2;

                model.SetPoint(i, x3, y3, z3);
            }
        }
        // set this ModelVisual3D object from a array of mesh3D objects
        private void SetModel(ArrayList meshs, Material backMaterial)
        {
            int nMeshNo = meshs.Count;

            if (nMeshNo == 0)
            {
                return;
            }

            MeshGeometry3D triangleMesh = new MeshGeometry3D();
            int            nTotalVertNo = 0;

            for (int j = 0; j < nMeshNo; j++)
            {
                Mesh3D mesh    = (Mesh3D)meshs[j];
                int    nVertNo = mesh.GetVertexNo();
                int    nTriNo  = mesh.GetTriangleNo();
                if ((nVertNo <= 0) || (nTriNo <= 0))
                {
                    continue;
                }

                double[] vx = new double[nVertNo];
                double[] vy = new double[nVertNo];
                double[] vz = new double[nVertNo];
                for (int i = 0; i < nVertNo; i++)
                {
                    vx[i] = vy[i] = vz[i] = 0;
                }

                // get normal of each vertex
                for (int i = 0; i < nTriNo; i++)
                {
                    Triangle3D tri = mesh.GetTriangle(i);
                    Vector3D   vN  = mesh.GetTriangleNormal(i);
                    int        n0  = tri.n0;
                    int        n1  = tri.n1;
                    int        n2  = tri.n2;

                    vx[n0] += vN.X;
                    vy[n0] += vN.Y;
                    vz[n0] += vN.Z;
                    vx[n1] += vN.X;
                    vy[n1] += vN.Y;
                    vz[n1] += vN.Z;
                    vx[n2] += vN.X;
                    vy[n2] += vN.Y;
                    vz[n2] += vN.Z;
                }
                for (int i = 0; i < nVertNo; i++)
                {
                    double length = Math.Sqrt(vx[i] * vx[i] + vy[i] * vy[i] + vz[i] * vz[i]);
                    if (length > 1e-20)
                    {
                        vx[i] /= length;
                        vy[i] /= length;
                        vz[i] /= length;
                    }
                    triangleMesh.Positions.Add(mesh.GetPoint(i));
                    Color color = mesh.GetColor(i);
                    Point mapPt = m_mapping.GetMappingPosition(color);
                    triangleMesh.TextureCoordinates.Add(new System.Windows.Point(mapPt.X, mapPt.Y));
                    triangleMesh.Normals.Add(new Vector3D(vx[i], vy[i], vz[i]));
                }

                for (int i = 0; i < nTriNo; i++)
                {
                    Triangle3D tri = mesh.GetTriangle(i);
                    int        n0  = tri.n0;
                    int        n1  = tri.n1;
                    int        n2  = tri.n2;

                    triangleMesh.TriangleIndices.Add(nTotalVertNo + n0);
                    triangleMesh.TriangleIndices.Add(nTotalVertNo + n1);
                    triangleMesh.TriangleIndices.Add(nTotalVertNo + n2);
                }
                nTotalVertNo += nVertNo;
            }
            //Material material = new DiffuseMaterial(new SolidColorBrush(Colors.Red));
            Material material = m_mapping.m_material;

            GeometryModel3D triangleModel = new GeometryModel3D(triangleMesh, material);

            triangleModel.Transform = new Transform3DGroup();
            if (backMaterial != null)
            {
                triangleModel.BackMaterial = backMaterial;
            }

            Content = triangleModel;
        }