Example #1
0
 public void Parse(string Input)
 {
     double AllX = 0, AllY = 0, AllZ = 0;
       double X, Y, Z;
       string[] Arr = Input.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
       int Length = Arr.Length;
       string[] Row;
       for (int i = 0; i < Length; i++)
       {
     if (Arr[i] != "" && Arr[i][0] != '#')
       switch (Arr[i].Substring(0, 2))
       {
     case "v ":
       Row = Arr[i].Split(null as string[], StringSplitOptions.RemoveEmptyEntries);
       Array.Resize(ref Points, ++PLength);
       X = double.Parse(Row[1], System.Globalization.CultureInfo.InvariantCulture);
       Y = double.Parse(Row[2], System.Globalization.CultureInfo.InvariantCulture);
       Z = double.Parse(Row[3], System.Globalization.CultureInfo.InvariantCulture);
       AllX += X;
       AllY += Y;
       AllZ += Z;
       Points[PLength - 1] = new Math3D.Point3D(X, Y, Z);
       break;
     case "f ":
       Row = Arr[i].Split(' ');
       string[] Temp;
       int[] F = new int[3];
       for (int k = 1; k <= 3; k++)
       {
         Temp = Row[k].Split('/');
         F[k - 1] = int.Parse(Temp[0]);
       }
       Array.Resize(ref Polygons, ++RLength);
       Polygons[RLength - 1] = new Polygon3D(F[0], F[1], F[2]);
       Array.Resize(ref Edges, ELength + 3);
       ELength += 3;
       Edges[ELength - 3] = new Edge3D(F[0], F[1]);
       Edges[ELength - 2] = new Edge3D(F[1], F[2]);
       Edges[ELength - 1] = new Edge3D(F[2], F[0]);
       break;
       }
       }
       Origin = new Math3D.Point3D(AllX / PLength, AllY / PLength, AllZ / PLength);
 }
Example #2
0
        public Bitmap drawCube(Point drawOrigin)
        {
            //FRONT FACE
              //Top Left - 7
              //Top Right - 4
              //Bottom Left - 6
              //Bottom Right - 5

              //Vars
              PointF[] point3D = new PointF[PLength]; //Will be actual 2D drawing points
              Point tmpOrigin = new Point(0, 0);

              Math3D.Point3D point0 = new Math3D.Point3D(0, 0, 0); //Used for reference

              //Calculate the camera Z position to stay constant despite rotation
              Math3D.Point3D anchorPoint = Points[4]; //anchor point
              double cameraZ = -(((anchorPoint.X - Origin.X) * Mathes.Zoom) / Origin.X) + anchorPoint.Z;
              Cam.Position = new Math3D.Point3D(Origin.X, Origin.Y, cameraZ);

              //Apply Rotations, moving the cube to a corner then back to middle
              Points = Math3D.Translate(Points, Origin, point0);
              Points = Math3D.RotateX(Points, xRotation); //The order of these
              Points = Math3D.RotateY(Points, yRotation); //rotations is the source
              Points = Math3D.RotateZ(Points, zRotation); //of Gimbal Lock
              Points = Math3D.Translate(Points, point0, Origin);

              //Convert 3D Points to 2D
              Math3D.Point3D vec;
              for (int i = 0; i < PLength; i++)
              {
            vec = Points[i];
            if (vec.Z - Cam.Position.Z >= 0)
            {
              point3D[i].X = (int)((double)-(vec.X - Cam.Position.X) / (-0.1f) * Mathes.Zoom) + drawOrigin.X;
              point3D[i].Y = (int)((double)(vec.Y - Cam.Position.Y) / (-0.1f) * Mathes.Zoom) + drawOrigin.Y;
            }
            else
            {
              tmpOrigin.X = (int)((double)(Origin.X - Cam.Position.X) / (double)(Origin.Z - Cam.Position.Z) * Mathes.Zoom) + drawOrigin.X;
              tmpOrigin.Y = (int)((double)-(Origin.Y - Cam.Position.Y) / (double)(Origin.Z - Cam.Position.Z) * Mathes.Zoom) + drawOrigin.Y;

              point3D[i].X = (float)((vec.X - Cam.Position.X) / (vec.Z - Cam.Position.Z) * Mathes.Zoom + drawOrigin.X);
              point3D[i].Y = (float)(-(vec.Y - Cam.Position.Y) / (vec.Z - Cam.Position.Z) * Mathes.Zoom + drawOrigin.Y);

              point3D[i].X = (int)point3D[i].X;
              point3D[i].Y = (int)point3D[i].Y;
            }
              }

              //Now to plot out the points
              /*Rectangle bounds = getBounds(point3D);
              bounds.Width += drawOrigin.X;
              bounds.Height += drawOrigin.Y;*/

              Bitmap tmpBmp = new Bitmap(Width, Height);
              Graphics g = Graphics.FromImage(tmpBmp);

              for (int i = 0; i < ELength; i++)
              {
            g.DrawLine(Pens.White, point3D[Edges[i].F - 1], point3D[Edges[i].S - 1]);
              }
              g.Dispose(); //Clean-up

              return tmpBmp;
        }