?? camera.java
字號:
//===========================================================================//=-------------------------------------------------------------------------=//= Module history: =//= - August 8 2005 - Oscar Chavarro: Original base version =//= - August 24 2005 - David Diaz / Cesar Bustacara: Design changes to =//= decouple JOGL from the Camera data model, extra utilitary methods =//= added =//= - August 25 2005 - Oscar Chavarro: English translation of comments =//= - September 12 2005 - Oscar Chavarro: generateRay updated =//= - November 15 2005 - Oscar Chavarro: generateRay updated (Bug?) =//= - November 23 2005 - Oscar Chavarro: updated methods for direct access =//= of coordinate base system and access maintaining ortoghonality. =//= - November 24 2005 - Oscar Chavarro: new generateRay algorithm, now =//= consistent with JOGL/OpenGL transformation interpretation. =//= - April 7 2006 - Oscar Chavarro: calculateUPlaneAtPixel, proyectPoint =//= - November 5 2006 - Oscar Chavarro: plane calculation methods updated =//= - November 5 2006 - Oscar Chavarro: added Cohen-Sutherland line =//= clipping functionality =//===========================================================================package vsdk.toolkit.environment;import vsdk.toolkit.common.VSDK;import vsdk.toolkit.common.Entity;import vsdk.toolkit.common.Vector3D;import vsdk.toolkit.common.Vector4D;import vsdk.toolkit.common.Ray;import vsdk.toolkit.common.Matrix4x4;import vsdk.toolkit.environment.geometry.Geometry;import vsdk.toolkit.environment.geometry.InfinitePlane;public class Camera extends Entity{ /// Check the general attribute description in superclass Entity. public static final long serialVersionUID = 20060502L; // Basic Camera Model private Vector3D up; private Vector3D front; private Vector3D left; private Vector3D eyePosition; private double focalDistance; private int projectionMode; private double fov; private double orthogonalZoom; private double nearPlaneDistance; private double farPlaneDistance; /// This string should be used for specific application defined /// functionality. Can be null. private String name; // Global constants public static final int OPCODE_FAR = (0x01 << 1); public static final int OPCODE_NEAR = (0x01 << 2); public static final int OPCODE_RIGHT = (0x01 << 3); public static final int OPCODE_LEFT = (0x01 << 4); public static final int OPCODE_UP = (0x01 << 5); public static final int OPCODE_DOWN = (0x01 << 6); public static final int STEREO_MODE_CENTER = 1; public static final int STEREO_MODE_LEFT_EYE = 2; public static final int STEREO_MODE_RIGHT_EYE = 3; public static final int PROJECTION_MODE_ORTHOGONAL = 4; public static final int PROJECTION_MODE_PERSPECTIVE = 5; /** Una `Camera` debe saber de qué tamaño es el viewport para el cual está generando una proyección, para poder modificar sus parámetros internos en función del ángulo de vision (`fov`) y de la actual proporción de ancho/alto del viewport. Las variables internas `viewportXSize` y `viewportYSize` representan el tamańo en pixels para el viewport, y son valores que solo pueden ser cambiados por el método `Camera::updateViewportResize`. Estos dos valores son para uso interno de la clase cámara y no pueden ser consultados (i.e. son una copia de la configuración del viewport, que debe ser administrado por la aplicación que use `Camera`s). */ private double viewportXSize; /// Check `viewportXSize`'s documentation private double viewportYSize; // Private values which are preprocessed to speed up calculations private Vector3D dx, dy, _dir, upWithScale, rightWithScale; private Matrix4x4 normalizingTransformation; public Camera() { eyePosition = new Vector3D(0,-5,1); up = new Vector3D(0,0,1); front=new Vector3D(0,1,0); left=new Vector3D(-1,0,0); fov = 60; viewportXSize = 320; viewportYSize = 320; projectionMode = PROJECTION_MODE_PERSPECTIVE; orthogonalZoom = 1; nearPlaneDistance = 0.05; farPlaneDistance = 100; focalDistance = 10; updateVectors(); } public Camera(Camera b) { eyePosition = new Vector3D(b.eyePosition); up = new Vector3D(b.up); front=new Vector3D(b.front); left=new Vector3D(b.left); fov = b.fov; viewportXSize = b.viewportXSize; viewportYSize = b.viewportYSize; projectionMode = b.projectionMode; orthogonalZoom = b.orthogonalZoom; nearPlaneDistance = b.nearPlaneDistance; farPlaneDistance = b.farPlaneDistance; focalDistance = b.focalDistance; updateVectors(); } public Matrix4x4 getNormalizingTransformation() { return normalizingTransformation; } public String getName() { return name; } public void setName(String n) { name = new String(n); } public double getViewportXSize() { return viewportXSize; } public double getViewportYSize() { return viewportYSize; } public Vector3D getPosition() { return eyePosition; } public void setPosition(Vector3D eyePosition) { this.eyePosition.clone(eyePosition); } public Vector3D getFocusedPosition() { Vector3D partial; Vector3D result; partial = front.multiply(focalDistance); result = eyePosition.add(partial); return result; } /** This method changes the `front` unit vector and the `focalDistance` based on `focusedPosition` parameter and current `eyePosition` value, WITHOUT changing any other vector. This method does NOT change the value of `up` of `left` vectors and can be used in advanced applications to directly access basic camera parameters. */ public void setFocusedPositionDirect(Vector3D focusedPosition) { Vector3D partial; partial = focusedPosition.substract(eyePosition); front.clone(partial); focalDistance = front.length(); front.normalize(); } /** This method changes the `front` unit vector, the `focalDistance`, the `left` vector and the `up` vector, based on: - `focusedPosition` parameter - current `eyePosition` value - current `up` vector (taken as a hint) This method CAN change the value of `up` and `left` vectors, to allow the user specify a left-handed orthogonal reference frame formed by the vectors <up, front, left>. The initial up vector is taken as a hint to specify a new up vector, similar to the original but forming a 90 degree angle with the front direction. The left vector is always changed to form a third orthogonal vectors to former ones. Note that the three resulting vectors are left normalized. \todo This method FAILS if the initial value of `up` is parallel to the `front` direction. Validation and exception handling are needed. */ public void setFocusedPositionMaintainingOrthogonality(Vector3D focusedPosition) { Vector3D partial1; partial1 = focusedPosition.substract(eyePosition); front.clone(partial1); focalDistance = front.length(); front.normalize(); left = up.crossProduct(front); left.normalize(); up = front.crossProduct(left); up.normalize(); } public Vector3D getUp() { return up; } public Vector3D getFront() { return front; } public Vector3D getLeft() { return left; } /** This method overwrites current `up` vector value, without considering the ill-case of getting `up` and `front` vectors pointing in the same direction. For non-advanced programmers, it is desireable to invoke the `setUpMaintainingOrthogonality` method instead of this one. */ public void setUpDirect(Vector3D up) { this.up.clone(up); } /** This method overwrites current `left` vector value, without considering the ill-case of getting `left` and `front` (or `up` vectors pointing in the same direction. For non-advanced programmers, it is desireable to invoke the `setLeftMaintainingOrthogonality` method instead of this one. */ public void setLeftDirect(Vector3D left) { this.left.clone(left); } /** * En este metodo no se tiene en cuenta si up y front quedan mirando para el mismo lado */ public void setUpMaintainingOrthogonality(Vector3D up) { up.normalize(); left = up.crossProduct(front); left.normalize(); this.up=front.crossProduct(left); this.up.normalize(); } public double getFov() { return fov; } public void setFov(double fov) { this.fov = fov; } public double getNearPlaneDistance() { return nearPlaneDistance; } public void setNearPlaneDistance(double nearPlaneDistance) { this.nearPlaneDistance = nearPlaneDistance; } public double getFarPlaneDistance() { return farPlaneDistance; } public void setFarPlaneDistance(double farPlaneDistance) { this.farPlaneDistance = farPlaneDistance; } public int getProjectionMode() { return projectionMode; } public void setProjectionMode(int projectionMode) { this.projectionMode = projectionMode; } public void updateViewportResize(int dx, int dy) { viewportXSize = dx; viewportYSize = dy; updateVectors(); } /** PRE: - focusedPosition y eyePosition tienen que ser diferentes! POST: - left queda normalizado - up queda normalizado @todo Document the way in which vectors are calculated, acording to the projection transformation. */ public void updateVectors() { up.normalize(); left.normalize(); front.normalize(); double fovFactor = viewportXSize / viewportYSize; _dir = front.multiply(0.5); upWithScale = up.multiply(Math.tan(Math.toRadians(fov/2))); rightWithScale = left.multiply(-fovFactor*Math.tan(Math.toRadians(fov/2))); //----------------------------------------------------------------- /* The normalizing transformation of current camera is such that transforms points in space to make it lie in the canonical view volume space, and it is calculated following the mechanism described on sections [FOLE1992].6.5.1 and [FOLE1992].6.5.2.
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -