Advertisement
Guest User

Graphics3D.java

a guest
Oct 31st, 2013
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.88 KB | None | 0 0
  1. package de.skysoldier.graphics3D;
  2.  
  3. import java.awt.Graphics;
  4.  
  5. public class Graphics3D {
  6.  
  7.     private Matrix rotationX;
  8.     private Matrix rotationY;
  9.     private Matrix rotationZ;
  10.     private Matrix translation;
  11.    
  12.     public Graphics3D(){
  13.         createMatrices();
  14.     }
  15.    
  16.     public void drawLine(Point3D point1, Point3D point2, Graphics g){
  17.         g.drawLine((int) point1.getX(), (int) point1.getY(), (int) point2.getX(), (int) point2.getY());
  18.     }
  19.    
  20.     public void drawLine(Line3D line, Graphics g){
  21.         drawLine(line.getPointStart(), line.getPointEnd(), g);
  22.     }
  23.    
  24.     public Point3D renderPoint(Point3D point, Camera camera, Matrix... transformations){
  25.         Point3D copy = point.clone();
  26.         for(int i = 0; i < transformations.length; i++){
  27.             copy.transform(transformations[i]);
  28.         }
  29.         if(camera.active) copy.transform(camera.getProjectionMatrix());
  30.         return copy;
  31.     }
  32.    
  33.     public Point3D[] renderPoints(Point3D points[], Camera camera, Matrix... transformations){
  34.         Point3D copys[] = new Point3D[points.length];
  35.         for(int i = 0; i < copys.length; i++){
  36.             copys[i] = renderPoint(points[i], camera, transformations);
  37.         }
  38.         return copys;
  39.     }
  40.    
  41.     public Line3D renderLine(Line3D line, Camera camera, Matrix... transformations){
  42.         return new Line3D(renderPoint(line.getPointStart(), camera, transformations), renderPoint(line.getPointEnd(), camera, transformations));
  43.     }
  44.    
  45.     private void createMatrices(){
  46.         createRotationMatrices();
  47.         createStaticMatrices();
  48.     }
  49.    
  50.     private void createRotationMatrices(){
  51.         rotationX = new Matrix(4, 4);
  52.         rotationY = new Matrix(4, 4);
  53.         rotationZ = new Matrix(4, 4);
  54.         rotationX.setCellData(0, 0, 1);
  55.         rotationX.setCellData(0, 1, 0);
  56.         rotationX.setCellData(0, 2, 0);
  57.         rotationX.setCellData(0, 3, 0);
  58.         rotationX.setCellData(1, 0, 0);
  59.         rotationX.setCellData(1, 3, 0);
  60.         rotationX.setCellData(2, 0, 0);
  61.         rotationX.setCellData(2, 3, 0);
  62.         rotationX.setCellData(3, 0, 0);
  63.         rotationX.setCellData(3, 1, 0);
  64.         rotationX.setCellData(3, 2, 0);
  65.         rotationX.setCellData(3, 3, 1);
  66.         rotationY.setCellData(0, 1, 0);
  67.         rotationY.setCellData(0, 3, 0);
  68.         rotationY.setCellData(1, 0, 0);
  69.         rotationY.setCellData(1, 1, 1);
  70.         rotationY.setCellData(1, 2, 0);
  71.         rotationY.setCellData(1, 3, 0);
  72.         rotationY.setCellData(2, 1, 0);
  73.         rotationY.setCellData(2, 3, 0);
  74.         rotationY.setCellData(3, 0, 0);
  75.         rotationY.setCellData(3, 1, 0);
  76.         rotationY.setCellData(3, 2, 0);
  77.         rotationY.setCellData(3, 3, 1);
  78.         rotationZ.setCellData(0, 2, 0);
  79.         rotationZ.setCellData(0, 3, 0);
  80.         rotationZ.setCellData(1, 2, 0);
  81.         rotationZ.setCellData(1, 3, 0);
  82.         rotationZ.setCellData(2, 0, 0);
  83.         rotationZ.setCellData(2, 1, 0);
  84.         rotationZ.setCellData(2, 2, 1);
  85.         rotationZ.setCellData(2, 3, 0);
  86.         rotationZ.setCellData(3, 0, 0);
  87.         rotationZ.setCellData(3, 1, 0);
  88.         rotationZ.setCellData(3, 2, 0);
  89.         rotationZ.setCellData(3, 3, 1);
  90.         updateRotationDataX(0);
  91.         updateRotationDataY(0);
  92.         updateRotationDataZ(0);
  93.     }
  94.    
  95.     private void createStaticMatrices(){
  96.         translation = new Matrix(4, 4);
  97.         translation.setCellData(0, 0, 1);
  98.         translation.setCellData(0, 1, 0);
  99.         translation.setCellData(0, 2, 0);
  100.         translation.setCellData(1, 0, 0);
  101.         translation.setCellData(1, 1, 1);
  102.         translation.setCellData(1, 2, 0);
  103.         translation.setCellData(2, 0, 0);
  104.         translation.setCellData(2, 1, 0);
  105.         translation.setCellData(2, 2, 1);
  106.         translation.setCellData(3, 0, 0);
  107.         translation.setCellData(3, 1, 0);
  108.         translation.setCellData(3, 2, 0);
  109.         translation.setCellData(3, 3, 1);
  110.         updateTranslationData(0, 0, 0);
  111.     }
  112.    
  113.     public void updateRotationDataX(double angle){
  114.         angle = Math.toRadians(angle);
  115.         double cos = Math.cos(angle);
  116.         double sin = Math.sin(angle);
  117.         rotationX.setCellData(1, 1, cos);
  118.         rotationX.setCellData(1, 2, -sin);
  119.         rotationX.setCellData(2, 1, sin);
  120.         rotationX.setCellData(2, 2, cos);
  121.     }
  122.    
  123.     public void updateRotationDataY(double angle){
  124.         angle = Math.toRadians(angle);
  125.         double cos = Math.cos(angle);
  126.         double sin = Math.sin(angle);
  127.         rotationY.setCellData(0, 0, cos);
  128.         rotationY.setCellData(0, 2, sin);
  129.         rotationY.setCellData(2, 0, -sin);
  130.         rotationY.setCellData(2, 2, cos);
  131.     }
  132.  
  133.     public void updateRotationDataZ(double angle){
  134.         angle = Math.toRadians(angle);
  135.         double cos = Math.cos(angle);
  136.         double sin = Math.sin(angle);
  137.         rotationZ.setCellData(0, 0, cos);
  138.         rotationZ.setCellData(0, 1, -sin);
  139.         rotationZ.setCellData(1, 0, sin);
  140.         rotationZ.setCellData(1, 1, cos);
  141.     }
  142.    
  143.     public void updateTranslationData(double x, double y, double z){
  144.         translation.setCellData(0, 3, x);
  145.         translation.setCellData(1, 3, y);
  146.         translation.setCellData(2, 3, z);
  147.     }
  148.    
  149.     public Camera createCamera(Values3D cameraType){
  150.         return new Camera(cameraType);
  151.     }
  152.    
  153.     public Matrix getRotationDataX(){
  154.         return rotationX;
  155.     }
  156.  
  157.     public Matrix getRotationDataY(){
  158.         return rotationY;
  159.     }
  160.  
  161.     public Matrix getRotationDataZ(){
  162.         return rotationZ;
  163.     }
  164.    
  165.     public Matrix getTranslationData(){
  166.         return translation;
  167.     }
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement