public Matrix3D GetTransformationMatrix() { //Matrix3D scaleMatrix = new Matrix3D(_scale.X, 0, 0, 0, 0, _scale.Y, 0, 0, 0, 0, _scale.Z, 0, 0, 0, 0, 1); //Matrix3D xRotationMatrix = new Matrix3D(1, 0, 0, 0, 0, (float)Math.Cos(_rotation.X), (float)-Math.Sin(_rotation.X), 0, 0, (float)Math.Sin(_rotation.X), (float)Math.Cos(_rotation.X), 0, 0, 0, 0, 1); //Matrix3D yRotationMatrix = new Matrix3D((float)Math.Cos(_rotation.Y), 0, (float)Math.Sin(_rotation.Y), 0, 0, 1, 0, 0, (float)-Math.Sin(_rotation.Y), 0, (float)Math.Cos(_rotation.Y), 0, 0, 0, 0, 1); //Matrix3D zRotationMatrix = new Matrix3D((float)Math.Cos(_rotation.Z), (float)-Math.Sin(_rotation.Z), 0, 0, (float)Math.Sin(_rotation.Z), (float)Math.Cos(_rotation.Z), 0, 0, 0, 0, 1, 0, 0, 0, 0, 1); //Matrix3D translateMatrix = new Matrix3D(1, 0, 0, _position.X, 0, 1, 0, _position.Y, 0, 0, 1, _position.Z, 0, 0, 0, 1); //Matrix3D transformationMatrix = scaleMatrix * xRotationMatrix * yRotationMatrix * zRotationMatrix * translateMatrix; //return transformationMatrix; Matrix3D scaleMatrix = Matrix3D.CreateScale(_scale); Matrix3D xRotationMatrix = Matrix3D.CreateRotationX(MathUtility.ToRadians(_rotation.X)); Matrix3D yRotationMatrix = Matrix3D.CreateRotationY(MathUtility.ToRadians(_rotation.Y)); Matrix3D zRotationMatrix = Matrix3D.CreateRotationZ(MathUtility.ToRadians(_rotation.Z)); Matrix3D translateMatrix = Matrix3D.CreateTranslation(_position.X, _position.Y, _position.Z); Matrix3D transformationMatrix = scaleMatrix * zRotationMatrix * yRotationMatrix * xRotationMatrix * translateMatrix; //Matrix3D transformationMatrix = scaleMatrix * xRotationMatrix * yRotationMatrix * zRotationMatrix * translateMatrix; //Matrix3D transformationMatrix = translateMatrix * zRotationMatrix * yRotationMatrix * xRotationMatrix * scaleMatrix; return(transformationMatrix); }
public override void Tessellate() { // First we loop around the main ring of the torus. for (int i = 0; i < TessellationLevel; i++) { float outerAngle = i * MathUtility.TWO_PI / TessellationLevel; // Create a transform matrix that will align geometry to // slice perpendicularly though the current ring position. Matrix3D transform = Matrix3D.CreateTranslation(_radius, 0, 0) * Matrix3D.CreateRotationY(outerAngle); // Now we loop along the other axis, around the side of the tube. for (int j = 0; j < TessellationLevel; j++) { float innerAngle = j * MathUtility.TWO_PI / TessellationLevel; float dx = (float)Math.Cos(innerAngle); float dy = (float)Math.Sin(innerAngle); // Create a vertex. Vector3D normal = new Vector3D(dx, dy, 0); Point3D position = (Point3D)(normal * _thickness / 2); position = Point3D.Transform(position, transform); normal = Vector3D.TransformNormal(normal, transform); AddVertex(position, normal); // And create indices for two triangles. int nextI = (i + 1) % TessellationLevel; int nextJ = (j + 1) % TessellationLevel; AddIndex(i * TessellationLevel + j); AddIndex(i * TessellationLevel + nextJ); AddIndex(nextI * TessellationLevel + j); AddIndex(i * TessellationLevel + nextJ); AddIndex(nextI * TessellationLevel + nextJ); AddIndex(nextI * TessellationLevel + j); } } }
private void UpdateBookTransform(Book3DViewModel bookVM, int iX, int iY) { var bounds3D = bookVM.GetBounds3D(); var margLeft = Config.Shelf3D.ShelfMarginLeft; var margTop = Config.Shelf3D.ShelfMarginTop; var margRight = Config.Shelf3D.ShelfMarginRight; var margBottom = Config.Shelf3D.ShelfMarginBottom; var shelfWidth = ActualSize.Width - margLeft - margRight; var shelfHeight = ActualSize.Height - margTop - margBottom; if (shelfWidth == 0 || shelfHeight == 0) { return; } var boardHeight = Config.Shelf3D.ShelfBoardHeight; double bookX = bounds3D.Min.X; double bookY = bounds3D.Min.Y; double bookWidth = bounds3D.Size.X; double bookHeight = bounds3D.Size.Y; if (bookWidth > 0 && bookHeight > 0) { var bookSize = new Size(bookWidth, bookHeight); var frameWidth = shelfWidth / HBS.ColumnCount; var frameHeight = shelfHeight / HBS.RowCount; frameWidth = Math.Max(0, frameWidth); frameHeight = Math.Max(0, frameHeight) - boardHeight; var frameX = frameWidth * iX + margLeft; var frameY = (frameHeight + boardHeight) * iY + margTop; var frameSize = new Size(frameWidth, frameHeight); var bookScale = MathHelper.ScaleSize(bookSize, frameWidth, frameHeight) * Config.Shelf3D.ShelfAdditionalScale; var scaledBookWidth = bookWidth * bookScale; var scaledBookHeight = bookHeight * bookScale; var x = frameX + (frameWidth - scaledBookWidth) * 0.5d + bookX; var y = frameY + (frameHeight - scaledBookHeight) + bookY; var translationX = x; //y gets inverted here since 3d origin is bottom left not top left var translationY = shelfHeight - scaledBookHeight - y; var m3D = Matrix3D.Identity; var rotY = Matrix3D.CreateRotationY(MathUtility.ToRadians((float)Config.Shelf3D.ShelfRotationY)); var rotX = Matrix3D.CreateRotationX(MathUtility.ToRadians((float)Config.Shelf3D.ShelfRotationX)); var scale = Matrix3D.CreateScale((float)bookScale); var translate = Matrix3D.CreateTranslation(new Point3D((float)translationX, (float)translationY, 0f)); var bounds2D = new Rect((float)x, (float)y, (float)scaledBookWidth, (float)scaledBookHeight); bookVM.Bounds2D = bounds2D; bookVM.TransformMatrix3D = m3D * rotY * rotX * scale * translate; } }
public void UpdateBookPlaneTransforms(double coverWidth = 0, double coverHeight = 0, double spineWidth = 0) { if (coverWidth == 0) { coverWidth = CoverWidth; } if (coverHeight == 0) { coverHeight = CoverHeight; } if (spineWidth == 0) { spineWidth = SpineWidth; } float x, y, rotX, rotY; Point3D rotCenter; Quaternion rotQuartY; Matrix3D rotMatrixX, rotMatrixY, translationMatrix; //front cover x = (float)spineWidth; rotY = (float)(OpenedProgress * HalfPi); rotMatrixY = Matrix3D.CreateRotationY(rotY); translationMatrix = Matrix3D.CreateTranslation(new Point3D(x, 0, 0)); FrontCover3D.TransformMatrix3D = rotMatrixY * translationMatrix; //spine Spine3D.TransformMatrix3D = Matrix3D.Identity; //back cover x = (float)-coverWidth; rotY = (float)(-OpenedProgress * HalfPi); rotQuartY = Quaternion.CreateFromAxisAngle(yAxis, rotY); rotCenter = new Point3D((float)coverWidth, 0, 0); rotMatrixY = Matrix3DHelper.CreateRotationMatrix(ref rotQuartY, ref rotCenter); translationMatrix = Matrix3D.CreateTranslation(new Point3D(x, 0, 0)); BackCover3D.TransformMatrix3D = rotMatrixY * translationMatrix; //top back cover x = (float)-coverWidth; y = (float)coverHeight; rotX = -HalfPi; rotY = (float)(-OpenedProgress * HalfPi); rotCenter = new Point3D((float)coverWidth, 0, 0); rotQuartY = Quaternion.CreateFromAxisAngle(yAxis, rotY); rotMatrixX = Matrix3D.CreateRotationX(rotX); rotMatrixY = Matrix3DHelper.CreateRotationMatrix(ref rotQuartY, ref rotCenter); translationMatrix = Matrix3D.CreateTranslation(new Point3D(x, y, 0)); TopBackCoverPages3D.TransformMatrix3D = rotMatrixX * rotMatrixY * translationMatrix; //top spine pages x = 0f; y = (float)(coverHeight - 0.1); rotX = -HalfPi; rotMatrixX = Matrix3D.CreateRotationX(rotX); translationMatrix = Matrix3D.CreateTranslation(new Point3D(x, y, 0)); TopSpinePages3D.TransformMatrix3D = rotMatrixX * translationMatrix; //top front cover pages x = (float)spineWidth; y = (float)coverHeight; rotX = -HalfPi; rotY = (float)(OpenedProgress * HalfPi); rotMatrixX = Matrix3D.CreateRotationX(rotX); rotMatrixY = Matrix3D.CreateRotationY(rotY); translationMatrix = Matrix3D.CreateTranslation(new Point3D(x, y, 0)); TopFrontCoverPages3D.TransformMatrix3D = rotMatrixX * rotMatrixY * translationMatrix; //inner page right x = (float)(spineWidth * 0.5d); rotY = Pi - (float)(OpenedProgress * HalfPi); rotMatrixY = Matrix3D.CreateRotationY(rotY); translationMatrix = Matrix3D.CreateTranslation(new Point3D(x, 0, 0)); InnerPageRight3D.TransformMatrix3D = rotMatrixY * translationMatrix; if (TransformsUpdated != null) { TransformsUpdated(this); } }