/// <summary> /// Constructor /// </summary> public Cube3D() { // initialize cube object FullCube = new Cube(); MovableFaceArray = new BlockFace3D[Cube.MovableFaces]; for (int BlockNo = 0; BlockNo < Cube.BlocksPerCube; BlockNo++) { // create block Block3D Block = new Block3D(BlockNo); // add box to Cube3D VisualMedia3D Children.Add(Block); // center block (block no 13) if (Block.BlockFaceArray == null) { continue; } // loop forall 6 faces of each block foreach (BlockFace3D Face in Block.BlockFaceArray) { // save movable faces array if (Face.FaceNo >= 0 && Face.FaceNo < Cube.MovableFaces) { MovableFaceArray[Face.FaceNo] = Face; } } } // create double index array of all 6 faces and for each face all blocks CubeFaceBlockArray = new Block3D[Cube.FaceColors][]; // loop for all colors for (int ColorIndex = 0; ColorIndex < Cube.FaceColors; ColorIndex++) { // for each color there are 9 blocks CubeFaceBlockArray[ColorIndex] = new Block3D[Cube.BlocksPerFace]; // loop for all blocks of one face color for (int BlockIndex = 0; BlockIndex < Cube.BlocksPerFace; BlockIndex++) { CubeFaceBlockArray[ColorIndex][BlockIndex] = (Block3D)Children[BlockNoOfOneFace[ColorIndex, BlockIndex]]; } } return; }
/// <summary> /// Block face constructor /// </summary> /// <param name="Block">Parent Block</param> /// <param name="FaceNo">Face number</param> /// <param name="FaceColor">Initial face color</param> public BlockFace3D ( Block3D Block, int FaceNo, int FaceColor ) { // save face number this.FaceNo = FaceNo; // set current color CurrentColor = FaceColor; // initialize some geometric variables Point3D Point0 = new Point3D(); Point3D Point1 = new Point3D(); Point3D Point2 = new Point3D(); Point3D Point3 = new Point3D(); Vector3D Normal = new Vector3D(); switch (FaceColor) { case Cube.WhiteFace: Point0 = new Point3D(Block.OrigX, Block.OrigY, Block.OrigZ); Point1 = new Point3D(Block.OrigX, Block.OrigY + Cube3D.BlockWidth, Block.OrigZ); Point2 = new Point3D(Block.OrigX + Cube3D.BlockWidth, Block.OrigY + Cube3D.BlockWidth, Block.OrigZ); Point3 = new Point3D(Block.OrigX + Cube3D.BlockWidth, Block.OrigY, Block.OrigZ); Normal = new Vector3D(0, 0, -1); break; case Cube.BlueFace: Point0 = new Point3D(Block.OrigX, Block.OrigY + Cube3D.BlockWidth, Block.OrigZ + Cube3D.BlockWidth); Point1 = new Point3D(Block.OrigX, Block.OrigY + Cube3D.BlockWidth, Block.OrigZ); Point2 = new Point3D(Block.OrigX, Block.OrigY, Block.OrigZ); Point3 = new Point3D(Block.OrigX, Block.OrigY, Block.OrigZ + Cube3D.BlockWidth); Normal = new Vector3D(-1, 0, 0); break; case Cube.RedFace: Point0 = new Point3D(Block.OrigX, Block.OrigY, Block.OrigZ + Cube3D.BlockWidth); Point1 = new Point3D(Block.OrigX, Block.OrigY, Block.OrigZ); Point2 = new Point3D(Block.OrigX + Cube3D.BlockWidth, Block.OrigY, Block.OrigZ); Point3 = new Point3D(Block.OrigX + Cube3D.BlockWidth, Block.OrigY, Block.OrigZ + Cube3D.BlockWidth); Normal = new Vector3D(0, -1, 0); break; case Cube.GreenFace: Point0 = new Point3D(Block.OrigX + Cube3D.BlockWidth, Block.OrigY, Block.OrigZ + Cube3D.BlockWidth); Point1 = new Point3D(Block.OrigX + Cube3D.BlockWidth, Block.OrigY, Block.OrigZ); Point2 = new Point3D(Block.OrigX + Cube3D.BlockWidth, Block.OrigY + Cube3D.BlockWidth, Block.OrigZ); Point3 = new Point3D(Block.OrigX + Cube3D.BlockWidth, Block.OrigY + Cube3D.BlockWidth, Block.OrigZ + Cube3D.BlockWidth); Normal = new Vector3D(1, 0, 0); break; case Cube.OrangeFace: Point0 = new Point3D(Block.OrigX + Cube3D.BlockWidth, Block.OrigY + Cube3D.BlockWidth, Block.OrigZ + Cube3D.BlockWidth); Point1 = new Point3D(Block.OrigX + Cube3D.BlockWidth, Block.OrigY + Cube3D.BlockWidth, Block.OrigZ); Point2 = new Point3D(Block.OrigX, Block.OrigY + Cube3D.BlockWidth, Block.OrigZ); Point3 = new Point3D(Block.OrigX, Block.OrigY + Cube3D.BlockWidth, Block.OrigZ + Cube3D.BlockWidth); Normal = new Vector3D(0, 1, 0); break; case Cube.YellowFace: Point0 = new Point3D(Block.OrigX, Block.OrigY + Cube3D.BlockWidth, Block.OrigZ + Cube3D.BlockWidth); Point1 = new Point3D(Block.OrigX, Block.OrigY, Block.OrigZ + Cube3D.BlockWidth); Point2 = new Point3D(Block.OrigX + Cube3D.BlockWidth, Block.OrigY, Block.OrigZ + Cube3D.BlockWidth); Point3 = new Point3D(Block.OrigX + Cube3D.BlockWidth, Block.OrigY + Cube3D.BlockWidth, Block.OrigZ + Cube3D.BlockWidth); Normal = new Vector3D(0, 0, 1); break; } // hidden black faces if (FaceNo < 0) { DiffuseMaterial BlackMaterial = new DiffuseMaterial(Brushes.Black); Block.Children.Add(CreateTriangle(Point0, Point1, Point2, Normal, BlackMaterial)); Block.Children.Add(CreateTriangle(Point0, Point2, Point3, Normal, BlackMaterial)); return; } // calculate points to separate block edge and block face Vector3D Diag02 = Point3D.Subtract(Point2, Point0); Point3D Point4 = Point3D.Add(Point0, Vector3D.Multiply(0.04, Diag02)); Point3D Point6 = Point3D.Add(Point0, Vector3D.Multiply(0.96, Diag02)); Vector3D Diag13 = Point3D.Subtract(Point3, Point1); Point3D Point5 = Point3D.Add(Point1, Vector3D.Multiply(0.04, Diag13)); Point3D Point7 = Point3D.Add(Point1, Vector3D.Multiply(0.96, Diag13)); // gray edge DiffuseMaterial GrayMaterial = new DiffuseMaterial(Brushes.DarkGray); Block.Children.Add(CreateTriangle(Point0, Point1, Point5, Normal, GrayMaterial)); Block.Children.Add(CreateTriangle(Point0, Point5, Point4, Normal, GrayMaterial)); Block.Children.Add(CreateTriangle(Point1, Point2, Point6, Normal, GrayMaterial)); Block.Children.Add(CreateTriangle(Point1, Point6, Point5, Normal, GrayMaterial)); Block.Children.Add(CreateTriangle(Point2, Point3, Point7, Normal, GrayMaterial)); Block.Children.Add(CreateTriangle(Point2, Point7, Point6, Normal, GrayMaterial)); Block.Children.Add(CreateTriangle(Point3, Point0, Point4, Normal, GrayMaterial)); Block.Children.Add(CreateTriangle(Point3, Point4, Point7, Normal, GrayMaterial)); // block face color DiffuseMaterial ColorMaterial = Cube3D.Material[FaceColor]; Block.Children.Add(CreateTriangle(Point4, Point5, Point6, Normal, ColorMaterial, 1)); Block.Children.Add(CreateTriangle(Point4, Point6, Point7, Normal, ColorMaterial, 2)); return; }