Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package de.skysoldier.graphics3D;
- import java.awt.Graphics;
- public class Graphics3D {
- private Matrix rotationX;
- private Matrix rotationY;
- private Matrix rotationZ;
- private Matrix translation;
- public Graphics3D(){
- createMatrices();
- }
- public void drawLine(Point3D point1, Point3D point2, Graphics g){
- g.drawLine((int) point1.getX(), (int) point1.getY(), (int) point2.getX(), (int) point2.getY());
- }
- public void drawLine(Line3D line, Graphics g){
- drawLine(line.getPointStart(), line.getPointEnd(), g);
- }
- public Point3D renderPoint(Point3D point, Camera camera, Matrix... transformations){
- Point3D copy = point.clone();
- for(int i = 0; i < transformations.length; i++){
- copy.transform(transformations[i]);
- }
- if(camera.active) copy.transform(camera.getProjectionMatrix());
- return copy;
- }
- public Point3D[] renderPoints(Point3D points[], Camera camera, Matrix... transformations){
- Point3D copys[] = new Point3D[points.length];
- for(int i = 0; i < copys.length; i++){
- copys[i] = renderPoint(points[i], camera, transformations);
- }
- return copys;
- }
- public Line3D renderLine(Line3D line, Camera camera, Matrix... transformations){
- return new Line3D(renderPoint(line.getPointStart(), camera, transformations), renderPoint(line.getPointEnd(), camera, transformations));
- }
- private void createMatrices(){
- createRotationMatrices();
- createStaticMatrices();
- }
- private void createRotationMatrices(){
- rotationX = new Matrix(4, 4);
- rotationY = new Matrix(4, 4);
- rotationZ = new Matrix(4, 4);
- rotationX.setCellData(0, 0, 1);
- rotationX.setCellData(0, 1, 0);
- rotationX.setCellData(0, 2, 0);
- rotationX.setCellData(0, 3, 0);
- rotationX.setCellData(1, 0, 0);
- rotationX.setCellData(1, 3, 0);
- rotationX.setCellData(2, 0, 0);
- rotationX.setCellData(2, 3, 0);
- rotationX.setCellData(3, 0, 0);
- rotationX.setCellData(3, 1, 0);
- rotationX.setCellData(3, 2, 0);
- rotationX.setCellData(3, 3, 1);
- rotationY.setCellData(0, 1, 0);
- rotationY.setCellData(0, 3, 0);
- rotationY.setCellData(1, 0, 0);
- rotationY.setCellData(1, 1, 1);
- rotationY.setCellData(1, 2, 0);
- rotationY.setCellData(1, 3, 0);
- rotationY.setCellData(2, 1, 0);
- rotationY.setCellData(2, 3, 0);
- rotationY.setCellData(3, 0, 0);
- rotationY.setCellData(3, 1, 0);
- rotationY.setCellData(3, 2, 0);
- rotationY.setCellData(3, 3, 1);
- rotationZ.setCellData(0, 2, 0);
- rotationZ.setCellData(0, 3, 0);
- rotationZ.setCellData(1, 2, 0);
- rotationZ.setCellData(1, 3, 0);
- rotationZ.setCellData(2, 0, 0);
- rotationZ.setCellData(2, 1, 0);
- rotationZ.setCellData(2, 2, 1);
- rotationZ.setCellData(2, 3, 0);
- rotationZ.setCellData(3, 0, 0);
- rotationZ.setCellData(3, 1, 0);
- rotationZ.setCellData(3, 2, 0);
- rotationZ.setCellData(3, 3, 1);
- updateRotationDataX(0);
- updateRotationDataY(0);
- updateRotationDataZ(0);
- }
- private void createStaticMatrices(){
- translation = new Matrix(4, 4);
- translation.setCellData(0, 0, 1);
- translation.setCellData(0, 1, 0);
- translation.setCellData(0, 2, 0);
- translation.setCellData(1, 0, 0);
- translation.setCellData(1, 1, 1);
- translation.setCellData(1, 2, 0);
- translation.setCellData(2, 0, 0);
- translation.setCellData(2, 1, 0);
- translation.setCellData(2, 2, 1);
- translation.setCellData(3, 0, 0);
- translation.setCellData(3, 1, 0);
- translation.setCellData(3, 2, 0);
- translation.setCellData(3, 3, 1);
- updateTranslationData(0, 0, 0);
- }
- public void updateRotationDataX(double angle){
- angle = Math.toRadians(angle);
- double cos = Math.cos(angle);
- double sin = Math.sin(angle);
- rotationX.setCellData(1, 1, cos);
- rotationX.setCellData(1, 2, -sin);
- rotationX.setCellData(2, 1, sin);
- rotationX.setCellData(2, 2, cos);
- }
- public void updateRotationDataY(double angle){
- angle = Math.toRadians(angle);
- double cos = Math.cos(angle);
- double sin = Math.sin(angle);
- rotationY.setCellData(0, 0, cos);
- rotationY.setCellData(0, 2, sin);
- rotationY.setCellData(2, 0, -sin);
- rotationY.setCellData(2, 2, cos);
- }
- public void updateRotationDataZ(double angle){
- angle = Math.toRadians(angle);
- double cos = Math.cos(angle);
- double sin = Math.sin(angle);
- rotationZ.setCellData(0, 0, cos);
- rotationZ.setCellData(0, 1, -sin);
- rotationZ.setCellData(1, 0, sin);
- rotationZ.setCellData(1, 1, cos);
- }
- public void updateTranslationData(double x, double y, double z){
- translation.setCellData(0, 3, x);
- translation.setCellData(1, 3, y);
- translation.setCellData(2, 3, z);
- }
- public Camera createCamera(Values3D cameraType){
- return new Camera(cameraType);
- }
- public Matrix getRotationDataX(){
- return rotationX;
- }
- public Matrix getRotationDataY(){
- return rotationY;
- }
- public Matrix getRotationDataZ(){
- return rotationZ;
- }
- public Matrix getTranslationData(){
- return translation;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement