1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212:
|
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Media.Media3D; using System.Windows.Media; using System.Windows; using System.Windows.Media.Imaging; using System.IO;
namespace Cube3DLib { public class Cube : ModelVisual3D { Model3DGroup meshGroup; RotateTransform3D myXRotateTransform3D; RotateTransform3D myYRotateTransform3D; RotateTransform3D myZRotateTransform3D; AxisAngleRotation3D myXAxisAngleRotation3d; AxisAngleRotation3D myYAxisAngleRotation3d; AxisAngleRotation3D myZAxisAngleRotation3d; Point3D center;
public Cube(Point3D center, string imagePath) { Name = "cube1"; meshGroup = new Model3DGroup(); this.center = center; CreateCube(imagePath);
myXRotateTransform3D = new RotateTransform3D(); myXAxisAngleRotation3d = new AxisAngleRotation3D(); myXAxisAngleRotation3d.Angle = 40; myXRotateTransform3D.Rotation = myXAxisAngleRotation3d;
myYRotateTransform3D = new RotateTransform3D(); myYAxisAngleRotation3d = new AxisAngleRotation3D(); myYAxisAngleRotation3d.Angle = 0; myYRotateTransform3D.Rotation = myYAxisAngleRotation3d;
myZRotateTransform3D = new RotateTransform3D(); myZAxisAngleRotation3d = new AxisAngleRotation3D(); myZAxisAngleRotation3d.Angle = 0; myZRotateTransform3D.Rotation = myZAxisAngleRotation3d;
Transform3DGroup rotate = new Transform3DGroup(); rotate.Children.Add(myXRotateTransform3D); rotate.Children.Add(myYRotateTransform3D); rotate.Children.Add(myZRotateTransform3D); meshGroup.Transform = rotate;
}
public void RotateX() { myXAxisAngleRotation3d.Axis = new Vector3D(0, 3, 0); myXAxisAngleRotation3d.Angle = myXAxisAngleRotation3d.Angle + 15; }
public void RotateY() { myYAxisAngleRotation3d.Axis = new Vector3D(3, 0, 0); myYAxisAngleRotation3d.Angle = myYAxisAngleRotation3d.Angle + 15; }
public void RotateZ() { myZAxisAngleRotation3d.Axis = new Vector3D(0, 0, 3); myZAxisAngleRotation3d.Angle = myZAxisAngleRotation3d.Angle + 15; } public string Name { get; set; }
private void CreateCube(string imagePath) { Point3D a = new Point3D(center.X - 1, center.Y - 1, center.Z + 1); Point3D b = new Point3D(center.X + 1, center.Y - 1, center.Z + 1); Point3D c = new Point3D(center.X + 1, center.Y + 1, center.Z + 1); Point3D d = new Point3D(center.X - 1, center.Y + 1, center.Z + 1); Point3D e = new Point3D(center.X + 1, center.Y - 1, center.Z - 1); Point3D f = new Point3D(center.X - 1, center.Y - 1, center.Z - 1); Point3D g = new Point3D(center.X - 1, center.Y + 1, center.Z - 1); Point3D h = new Point3D(center.X + 1, center.Y + 1, center.Z - 1);
GeometryModel3D front = new GeometryModel3D(BuildRectangle(a, b, c, d, new Vector3D(0, 0, +1)), new DiffuseMaterial(new ImageBrush(new BitmapImage( new Uri(Path.Combine(imagePath, "1.jpg"), UriKind.Absolute)))));
front.BackMaterial = new DiffuseMaterial(new ImageBrush(new BitmapImage( new Uri(Path.Combine(imagePath, "1.jpg"), UriKind.Absolute))));
GeometryModel3D back = new GeometryModel3D(BuildRectangle(e, f, g, h, new Vector3D(0, 0, -1)), new DiffuseMaterial(new ImageBrush(new BitmapImage( new Uri(Path.Combine(imagePath, "2.Jpg"), UriKind.Absolute)))));
back.BackMaterial = new DiffuseMaterial(new ImageBrush(new BitmapImage( new Uri(Path.Combine(imagePath, "2.jpg"), UriKind.Absolute))));
GeometryModel3D right = new GeometryModel3D(BuildRectangle(b, e, h, c, new Vector3D(1, 0, 0)), new DiffuseMaterial(new ImageBrush(new BitmapImage( new Uri(Path.Combine(imagePath, "3.jpg"), UriKind.Absolute)))));
right.BackMaterial = new DiffuseMaterial(new ImageBrush(new BitmapImage( new Uri(Path.Combine(imagePath, "3.jpg"), UriKind.Absolute))));
GeometryModel3D left = new GeometryModel3D(BuildRectangle(f, a, d, g, new Vector3D(-1, 0, 0)), new DiffuseMaterial(new ImageBrush(new BitmapImage( new Uri(Path.Combine(imagePath, "4.jpg"), UriKind.Absolute)))));
left.BackMaterial = new DiffuseMaterial(new ImageBrush(new BitmapImage( new Uri(Path.Combine(imagePath, "4.jpg"), UriKind.Absolute))));
GeometryModel3D bottom = new GeometryModel3D(BuildRectangle(e, b, a, f, new Vector3D(0, -1, 0)), new DiffuseMaterial(new ImageBrush(new BitmapImage( new Uri(Path.Combine(imagePath, "5.jpg"), UriKind.Absolute)))));
bottom.BackMaterial = new DiffuseMaterial(new ImageBrush(new BitmapImage( new Uri(Path.Combine(imagePath, "5.jpg"), UriKind.Absolute))));
GeometryModel3D top = new GeometryModel3D(BuildRectangle(d, c, h, g, new Vector3D(0, 1, 0)), new DiffuseMaterial(new ImageBrush(new BitmapImage( new Uri(Path.Combine(imagePath, "6.jpg"), UriKind.Absolute)))));
top.BackMaterial = new DiffuseMaterial(new ImageBrush(new BitmapImage( new Uri(Path.Combine(imagePath, "6.jpg"), UriKind.Absolute)))); meshGroup.Children.Add(front); meshGroup.Children.Add(back); meshGroup.Children.Add(right); meshGroup.Children.Add(left); meshGroup.Children.Add(bottom); meshGroup.Children.Add(top);
this.Content = meshGroup; }
private MeshGeometry3D BuildRectangle(Point3D a, Point3D b, Point3D c, Point3D d, Vector3D normal) { MeshGeometry3D geometry = new MeshGeometry3D(); int baseIndex = geometry.Positions.Count;
geometry.Positions.Add(a); geometry.Positions.Add(b); geometry.Positions.Add(c); geometry.Positions.Add(d);
geometry.TextureCoordinates.Add(new Point(0, 1)); geometry.TextureCoordinates.Add(new Point(1, 1)); geometry.TextureCoordinates.Add(new Point(1, 0)); geometry.TextureCoordinates.Add(new Point(0, 0));
geometry.Normals.Add(normal); geometry.Normals.Add(normal); geometry.Normals.Add(normal); geometry.Normals.Add(normal);
geometry.TriangleIndices.Add(baseIndex + 0); geometry.TriangleIndices.Add(baseIndex + 1); geometry.TriangleIndices.Add(baseIndex + 3); geometry.TriangleIndices.Add(baseIndex + 1); geometry.TriangleIndices.Add(baseIndex + 2); geometry.TriangleIndices.Add(baseIndex + 3);
return geometry; } } } |