package matrix; // ViewTrans for making transformations // 3D to 2D transformations - easy and fun. // A ViewTrans is the combination of three transformations: // a view (3D -> 3D), a perspective (3D -> 2D), and // a screen (2D -> 2D) transformation combined as // screen * perspective * view (think of as operating // on a vector from right to left). // The view part of the transformation can be // transformed with the applyToView() to re-orient // the viewpoint without messing up the perspective. // The perspective part of the transformation can be // changed without messing up the screen transformation // with setEyeToScreenDist(). public class ViewTrans extends Matrix3Dh { // store the two components (view and perspective) // of the ViewTrans matrix to make ViewTrans // manipulation easier. private Matrix3Dh view; private Matrix3Dh perspective; private Matrix2Dh screen; public ViewTrans(Vector3D eye, Vector3D lookat, Vector3D up, double eyeToScreenDist, Vector2D center, Vector2D scale) { view = Matrix3Dh.view(eye, lookat, up); //view.print(System.out); //System.out.println("_"); perspective = Matrix3Dh.perspective(eyeToScreenDist); //perspective.print(System.out); //System.out.println("_"); screen = Matrix2Dh.translate(center).applyOn(Matrix2D.scale(scale)); //(perspective.applyOn(view)).print(System.out); //System.out.println("_"); setViewTrans(); //{{INIT_CONTROLS //}} } public ViewTrans(Matrix3Dh viewT, double eyeToScreenDist, Vector2D center, Vector2D scale) { this.view = new Matrix3Dh(); this.view.copy(viewT); perspective = Matrix3Dh.perspective(eyeToScreenDist); screen = Matrix2Dh.translate(center).applyOn(Matrix2D.scale(scale)); setViewTrans(); } public ViewTrans(Vector3D eye, Vector3D lookat, Vector3D up) { view = Matrix3Dh.view(eye, lookat, up); perspective = new Matrix3Dh(); // identity screen = new Matrix2Dh(); // identity setViewTrans(); } // straight projection public ViewTrans(Matrix3Dh viewT, Matrix2Dh screenT) { this.view = new Matrix3Dh(); this.view.copy(viewT); this.perspective = new Matrix3Dh(); // identity this.screen = new Matrix2Dh(); this.screen.copy(screenT); setViewTrans(); } // apply a transformation to view and then re-obtain // the ViewTrans accordingly: // this <- perspective * M * view public void applyToView(Matrix3Dh T) { view = T.applyOn(view); setViewTrans(); } public void setView(Matrix3Dh viewT) { this.view = new Matrix3Dh(); this.view.copy(viewT); setViewTrans(); } public void setEyeToScreenDist(double eyeToScreenDist) { perspective = Matrix3Dh.perspective(eyeToScreenDist); setViewTrans(); } public void noPerspective() { perspective = new Matrix3Dh(); setViewTrans(); } public void setScreenTransform(Vector2D center, Vector2D scale) { screen = Matrix2Dh.translate(center).applyOn(Matrix2D.scale(scale)); setViewTrans(); } public void setScreenTransform(Matrix2Dh S) { this.screen = new Matrix2Dh(); this.screen.copy(S); setViewTrans(); } private void setViewTrans() { super.copy(screen.applyOn(perspective.applyOn(view))); } //{{DECLARE_CONTROLS //}} }