Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package de.skysoldier.graphics3D;
- import de.skysoldier.graphics3D.Values3D;
- public class Camera {
- private Matrix projection;
- private Values3D type;
- private double l, r, t, b, n, f;
- //only for debug...
- public boolean active = true;
- protected Camera(){
- this(Values3D.CAMERA_PERSPECTIVE);
- }
- protected Camera(Values3D type){
- this(type, -100, 100, 100, -100, 50, 500);
- }
- protected Camera(Values3D type, double l, double r, double t, double b, double n, double f){
- projection = new Matrix(4, 4, type);
- this.type = type;
- this.l = l;
- this.r = r;
- this.t = t;
- this.b = b;
- this.n = n;
- this.f = f;
- if(type == Values3D.CAMERA_PERSPECTIVE){
- projection.setCellData(0, 0, (2 * n) / (r - l));
- projection.setCellData(0, 1, 0);
- projection.setCellData(0, 2, (r + l) / (r - l));
- projection.setCellData(0, 3, 0);
- projection.setCellData(1, 0, 0);
- projection.setCellData(1, 1, (2 * n) / (t - b));
- projection.setCellData(1, 2, (t + b) / (t - b));
- projection.setCellData(1, 3, 0);
- projection.setCellData(2, 0, 0);
- projection.setCellData(2, 1, 0);
- projection.setCellData(2, 2, -(f + n) / (f - n));
- projection.setCellData(2, 3, -(2 * f * n) / (f - n));
- projection.setCellData(3, 0, 0);
- projection.setCellData(3, 1, 0);
- projection.setCellData(3, 2, -1);
- projection.setCellData(3, 3, 0);
- }
- else{
- throw new UnsupportedOperationException("Camera Type not supported.");
- }
- }
- public Matrix getProjectionMatrix(){
- return projection;
- }
- public void updateLeft(double l){
- this.l = l;
- if(type == Values3D.CAMERA_PERSPECTIVE){
- projection.setCellData(0, 0, (2 * n) / (r - l));
- projection.setCellData(0, 2, (r + l) / (r - l));
- }
- }
- public void updateRight(double r){
- this.r = r;
- if(type == Values3D.CAMERA_PERSPECTIVE){
- projection.setCellData(0, 0, (2 * n) / (r - l));
- projection.setCellData(0, 2, (r + l) / (r - l));
- }
- }
- public void updateTop(double t){
- this.t = t;
- if(type == Values3D.CAMERA_PERSPECTIVE){
- projection.setCellData(1, 1, (2 * n) / (t - b));
- projection.setCellData(1, 2, (t + b) / (t - b));
- }
- }
- public void updateBottom(double b){
- this.b = b;
- if(type == Values3D.CAMERA_PERSPECTIVE){
- projection.setCellData(1, 1, (2 * n) / (t - b));
- projection.setCellData(1, 2, (t + b) / (t - b));
- }
- }
- public void updateNear(double n){
- this.n = n;
- if(type == Values3D.CAMERA_PERSPECTIVE){
- projection.setCellData(0, 0, (2 * n) / (r - l));
- projection.setCellData(1, 1, (2 * n) / (t - b));
- projection.setCellData(2, 2, -(f + n) / (f - n));
- projection.setCellData(2, 3, -(2 * f * n) / (f - n));
- }
- }
- public void updateFar(double f){
- this.f = f;
- if(type == Values3D.CAMERA_PERSPECTIVE){
- projection.setCellData(2, 2, -(f + n) / (f - n));
- projection.setCellData(2, 3, -(2 * f * n) / (f - n));
- }
- }
- public void transform(Matrix... transformations){
- for(int i = 0; i < transformations.length; i++){
- projection = transformations[i].multiply(projection);
- }
- }
- public double getLeft(){
- return l;
- }
- public double getRight(){
- return r;
- }
- public double getTop(){
- return t;
- }
- public double getBottom(){
- return b;
- }
- public double getNear(){
- return n;
- }
- public double getFar(){
- return f;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement