├── .gitattributes ├── 3D - Tutorial 01 └── DDDTutorial.java ├── 3D - Tutorial 02 ├── DDDTutorial.java └── Screen.java ├── 3D - Tutorial 03 ├── DDDTutorial.java ├── PolygonObject.java └── Screen.java ├── 3D - Tutorial 04 ├── DDDTutorial.java ├── DPolygon.java ├── PolygonObject.java └── Screen.java ├── 3D - Tutorial 05 ├── Calculator.java ├── DDDTutorial.java ├── DPolygon.java ├── PolygonObject.java └── Screen.java ├── 3D - Tutorial 06 ├── Calculator.java ├── DDDTutorial.java ├── DPolygon.java ├── PolygonObject.java ├── Screen.java └── Vector.java ├── 3D - Tutorial 07 ├── Calculator.java ├── DDDTutorial.java ├── DPolygon.java ├── PolygonObject.java ├── Screen.java └── Vector.java ├── 3D - Tutorial 08 ├── Calculator.java ├── DDDTutorial.java ├── DPolygon.java ├── PolygonObject.java ├── Screen.java └── Vector.java ├── 3D - Tutorial 09 ├── Calculator.java ├── DDDTutorial.java ├── DPolygon.java ├── PolygonObject.java ├── Screen.java └── Vector.java ├── 3D - Tutorial 10 ├── Calculator.java ├── DDDTutorial.java ├── DPolygon.java ├── PolygonObject.java ├── Screen.java └── Vector.java ├── 3D - Tutorial 11 ├── Calculator.java ├── DDDTutorial.java ├── DPolygon.java ├── PolygonObject.java ├── Screen.java └── Vector.java ├── 3D - Tutorial 12 ├── Calculator.java ├── DDDTutorial.java ├── DPolygon.java ├── PolygonObject.java ├── Screen.java └── Vector.java ├── 3D - Tutorial 13 ├── Calculator.java ├── DDDTutorial.java ├── DPolygon.java ├── PolygonObject.java ├── Screen.java └── Vector.java ├── 3D - Tutorial 14 ├── Calculator.java ├── DDDTutorial.java ├── DPolygon.java ├── PolygonObject.java ├── Screen.java └── Vector.java └── 3D Engine - 13-06-15 ├── Calculator.java ├── Cube.java ├── DDDTutorial.java ├── DPolygon.java ├── GenerateTerrain.java ├── Plane.java ├── PolygonObject.java ├── Prism.java ├── Pyramid.java ├── Screen.java └── Vector.java /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | 7 | # Standard to msysgit 8 | *.doc diff=astextplain 9 | *.DOC diff=astextplain 10 | *.docx diff=astextplain 11 | *.DOCX diff=astextplain 12 | *.dot diff=astextplain 13 | *.DOT diff=astextplain 14 | *.pdf diff=astextplain 15 | *.PDF diff=astextplain 16 | *.rtf diff=astextplain 17 | *.RTF diff=astextplain 18 | -------------------------------------------------------------------------------- /3D - Tutorial 01/DDDTutorial.java: -------------------------------------------------------------------------------- 1 | import java.awt.Toolkit; 2 | import javax.swing.JFrame; 3 | 4 | public class DDDTutorial extends JFrame{ 5 | static JFrame F = new DDDTutorial(); 6 | 7 | public DDDTutorial() 8 | { 9 | setUndecorated(true); 10 | setSize(Toolkit.getDefaultToolkit().getScreenSize()); 11 | setVisible(true); 12 | } 13 | 14 | public static void main(String[] args) 15 | { 16 | 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /3D - Tutorial 02/DDDTutorial.java: -------------------------------------------------------------------------------- 1 | import java.awt.Toolkit; 2 | import javax.swing.JFrame; 3 | 4 | public class DDDTutorial extends JFrame{ 5 | static JFrame F = new DDDTutorial(); 6 | Screen ScreenObject = new Screen(); 7 | 8 | public DDDTutorial() 9 | { 10 | add(ScreenObject); 11 | setUndecorated(true); 12 | setSize(Toolkit.getDefaultToolkit().getScreenSize()); 13 | setVisible(true); 14 | } 15 | 16 | public static void main(String[] args) 17 | { 18 | 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /3D - Tutorial 02/Screen.java: -------------------------------------------------------------------------------- 1 | import java.awt.Graphics; 2 | import javax.swing.JPanel; 3 | 4 | public class Screen extends JPanel{ 5 | public void paintComponent(Graphics g) 6 | { 7 | g.fillOval(10, 10, 500, 500); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /3D - Tutorial 03/DDDTutorial.java: -------------------------------------------------------------------------------- 1 | import java.awt.Toolkit; 2 | import javax.swing.JFrame; 3 | 4 | public class DDDTutorial extends JFrame{ 5 | static JFrame F = new DDDTutorial(); 6 | Screen ScreenObject = new Screen(); 7 | 8 | public DDDTutorial() 9 | { 10 | add(ScreenObject); 11 | setUndecorated(true); 12 | setSize(Toolkit.getDefaultToolkit().getScreenSize()); 13 | setVisible(true); 14 | } 15 | 16 | public static void main(String[] args) 17 | { 18 | 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /3D - Tutorial 03/PolygonObject.java: -------------------------------------------------------------------------------- 1 | import java.awt.Color; 2 | import java.awt.Graphics; 3 | import java.awt.Polygon; 4 | 5 | 6 | public class PolygonObject { 7 | Polygon P; 8 | Color c; 9 | 10 | public PolygonObject(int[] x, int[] y, Color c) 11 | { 12 | P = new Polygon(); 13 | P.xpoints = x; 14 | P.ypoints = y; 15 | P.npoints = x.length; 16 | this.c = c; 17 | } 18 | 19 | void drawPolygon(Graphics g) 20 | { 21 | g.setColor(c); 22 | g.drawPolygon(P); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /3D - Tutorial 03/Screen.java: -------------------------------------------------------------------------------- 1 | import java.awt.Color; 2 | import java.awt.Graphics; 3 | 4 | import javax.swing.JPanel; 5 | 6 | public class Screen extends JPanel{ 7 | PolygonObject Poly1; 8 | 9 | public Screen() 10 | { 11 | Poly1 = new PolygonObject(new int[]{10, 200, 10}, new int[]{10, 200, 400}, Color.black); 12 | } 13 | 14 | public void paintComponent(Graphics g) 15 | { 16 | Poly1.drawPolygon(g); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /3D - Tutorial 04/DDDTutorial.java: -------------------------------------------------------------------------------- 1 | import java.awt.Toolkit; 2 | import javax.swing.JFrame; 3 | 4 | public class DDDTutorial extends JFrame{ 5 | static JFrame F = new DDDTutorial(); 6 | Screen ScreenObject = new Screen(); 7 | 8 | public DDDTutorial() 9 | { 10 | add(ScreenObject); 11 | setUndecorated(true); 12 | setSize(Toolkit.getDefaultToolkit().getScreenSize()); 13 | setVisible(true); 14 | } 15 | 16 | public static void main(String[] args) 17 | { 18 | 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /3D - Tutorial 04/DPolygon.java: -------------------------------------------------------------------------------- 1 | import java.awt.Color; 2 | import java.awt.Polygon; 3 | 4 | 5 | public class DPolygon { 6 | Color c; 7 | int[] x; 8 | int[] y; 9 | int[] z; 10 | 11 | public DPolygon(int[] x, int[] y, int[] z, Color c) 12 | { 13 | this.x = x; 14 | this.y = y; 15 | this.z = z; 16 | this.c = c; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /3D - Tutorial 04/PolygonObject.java: -------------------------------------------------------------------------------- 1 | import java.awt.Color; 2 | import java.awt.Graphics; 3 | import java.awt.Polygon; 4 | 5 | 6 | public class PolygonObject { 7 | Polygon P; 8 | Color c; 9 | 10 | public PolygonObject(int[] x, int[] y, Color c) 11 | { 12 | P = new Polygon(); 13 | P.xpoints = x; 14 | P.ypoints = y; 15 | P.npoints = x.length; 16 | this.c = c; 17 | } 18 | 19 | void drawPolygon(Graphics g) 20 | { 21 | g.setColor(c); 22 | g.drawPolygon(P); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /3D - Tutorial 04/Screen.java: -------------------------------------------------------------------------------- 1 | import java.awt.Color; 2 | import java.awt.Graphics; 3 | 4 | import javax.swing.JPanel; 5 | 6 | public class Screen extends JPanel{ 7 | double[] ViewFrom = new double[] {10, 10, 10}; 8 | double[] ViewTo = new double[] {0, 0, 0}; 9 | 10 | DPolygon DPoly1 = new DPolygon(new int[]{2, 4, 2}, new int[]{2, 4, 6}, new int[]{5, 5, 5}, Color.black); 11 | public Screen() 12 | { 13 | 14 | } 15 | 16 | public void paintComponent(Graphics g) 17 | { 18 | 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /3D - Tutorial 05/Calculator.java: -------------------------------------------------------------------------------- 1 | 2 | public class Calculator { 3 | void CalculatePosition(double[] ViewFrom, double[] ViewTo, double[] x, double[] y, double[] z) 4 | { 5 | 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /3D - Tutorial 05/DDDTutorial.java: -------------------------------------------------------------------------------- 1 | import java.awt.Toolkit; 2 | import javax.swing.JFrame; 3 | 4 | public class DDDTutorial extends JFrame{ 5 | static JFrame F = new DDDTutorial(); 6 | Screen ScreenObject = new Screen(); 7 | 8 | public DDDTutorial() 9 | { 10 | add(ScreenObject); 11 | setUndecorated(true); 12 | setSize(Toolkit.getDefaultToolkit().getScreenSize()); 13 | setVisible(true); 14 | } 15 | 16 | public static void main(String[] args) 17 | { 18 | 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /3D - Tutorial 05/DPolygon.java: -------------------------------------------------------------------------------- 1 | import java.awt.Color; 2 | import java.awt.Polygon; 3 | 4 | 5 | public class DPolygon { 6 | Color c; 7 | double[] x, y, z; 8 | 9 | public DPolygon(double[] x, double[] y, double[] z, Color c) 10 | { 11 | this.x = x; 12 | this.y = y; 13 | this.z = z; 14 | this.c = c; 15 | createPolygon(); 16 | } 17 | 18 | void createPolygon() 19 | { 20 | Screen.DrawablePolygons[Screen.NumberOfPolygons] = new PolygonObject(x, y, c); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /3D - Tutorial 05/PolygonObject.java: -------------------------------------------------------------------------------- 1 | import java.awt.Color; 2 | import java.awt.Graphics; 3 | import java.awt.Polygon; 4 | 5 | 6 | public class PolygonObject { 7 | Polygon P; 8 | Color c; 9 | 10 | public PolygonObject(double[] x, double[] y, Color c) 11 | { 12 | Screen.NumberOfPolygons++; 13 | P = new Polygon(); 14 | for(int i = 0; i < x.length; i++) 15 | P.addPoint((int)x[i], (int)y[i]); 16 | this.c = c; 17 | } 18 | 19 | void drawPolygon(Graphics g) 20 | { 21 | g.setColor(c); 22 | g.drawPolygon(P); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /3D - Tutorial 05/Screen.java: -------------------------------------------------------------------------------- 1 | import java.awt.Color; 2 | import java.awt.Graphics; 3 | 4 | import javax.swing.JPanel; 5 | 6 | public class Screen extends JPanel{ 7 | double[] ViewFrom = new double[] {10, 10, 10}; 8 | double[] ViewTo = new double[] {0, 0, 0}; 9 | static int NumberOfPolygons = 0; 10 | static PolygonObject[] DrawablePolygons = new PolygonObject[100]; 11 | 12 | DPolygon DPoly1 = new DPolygon(new double[]{2, 4, 2}, new double[]{2, 4, 6}, new double[]{5, 5, 5}, Color.black); 13 | public Screen() 14 | { 15 | 16 | } 17 | 18 | public void paintComponent(Graphics g) 19 | { 20 | for(int i = 0; i < NumberOfPolygons; i++) 21 | DrawablePolygons[i].drawPolygon(g); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /3D - Tutorial 06/Calculator.java: -------------------------------------------------------------------------------- 1 | 2 | public class Calculator { 3 | static double CalculatePositionX(double[] ViewFrom, double[] ViewTo, double x, double y, double z) 4 | { 5 | return 0; 6 | } 7 | 8 | static double CalculatePositionY(double[] ViewFrom, double[] ViewTo, double x, double y, double z) 9 | { 10 | setStuff(ViewFrom, ViewTo); 11 | return 0; 12 | } 13 | 14 | static void setStuff(double[] ViewFrom, double[] ViewTo) 15 | { 16 | Vector ViewVector = new Vector(ViewTo[0] - ViewFrom[0], ViewTo[1] - ViewFrom[1], ViewTo[2] - ViewFrom[2]); 17 | 18 | 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /3D - Tutorial 06/DDDTutorial.java: -------------------------------------------------------------------------------- 1 | import java.awt.Toolkit; 2 | import javax.swing.JFrame; 3 | 4 | public class DDDTutorial extends JFrame{ 5 | static JFrame F = new DDDTutorial(); 6 | Screen ScreenObject = new Screen(); 7 | 8 | public DDDTutorial() 9 | { 10 | add(ScreenObject); 11 | setUndecorated(true); 12 | setSize(Toolkit.getDefaultToolkit().getScreenSize()); 13 | setVisible(true); 14 | } 15 | 16 | public static void main(String[] args) 17 | { 18 | 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /3D - Tutorial 06/DPolygon.java: -------------------------------------------------------------------------------- 1 | import java.awt.Color; 2 | import java.awt.Polygon; 3 | 4 | 5 | public class DPolygon { 6 | Color c; 7 | double[] x, y, z; 8 | 9 | public DPolygon(double[] x, double[] y, double[] z, Color c) 10 | { 11 | this.x = x; 12 | this.y = y; 13 | this.z = z; 14 | this.c = c; 15 | createPolygon(); 16 | } 17 | 18 | void createPolygon() 19 | { 20 | double[] newX = new double[x.length]; 21 | double [] newY = new double[x.length]; 22 | 23 | for(int i = 0; i < x.length; i++) 24 | { 25 | newX[i] = Calculator.CalculatePositionX(Screen.ViewFrom, Screen.ViewTo, x[i], y[i], z[i]); 26 | newY[i] = Calculator.CalculatePositionY(Screen.ViewFrom, Screen.ViewTo, x[i], y[i], z[i]); 27 | } 28 | 29 | Screen.DrawablePolygons[Screen.NumberOfPolygons] = new PolygonObject(newX, newY, c); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /3D - Tutorial 06/PolygonObject.java: -------------------------------------------------------------------------------- 1 | import java.awt.Color; 2 | import java.awt.Graphics; 3 | import java.awt.Polygon; 4 | 5 | 6 | public class PolygonObject { 7 | Polygon P; 8 | Color c; 9 | 10 | public PolygonObject(double[] x, double[] y, Color c) 11 | { 12 | Screen.NumberOfPolygons++; 13 | P = new Polygon(); 14 | for(int i = 0; i < x.length; i++) 15 | P.addPoint((int)x[i], (int)y[i]); 16 | this.c = c; 17 | } 18 | 19 | void drawPolygon(Graphics g) 20 | { 21 | g.setColor(c); 22 | g.drawPolygon(P); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /3D - Tutorial 06/Screen.java: -------------------------------------------------------------------------------- 1 | import java.awt.Color; 2 | import java.awt.Graphics; 3 | 4 | import javax.swing.JPanel; 5 | 6 | public class Screen extends JPanel{ 7 | static double[] ViewFrom = new double[] {10, 10, 10}; 8 | static double[] ViewTo = new double[] {0, 0, 0}; 9 | static int NumberOfPolygons = 0; 10 | static PolygonObject[] DrawablePolygons = new PolygonObject[100]; 11 | 12 | DPolygon DPoly1 = new DPolygon(new double[]{2, 4, 2}, new double[]{2, 4, 6}, new double[]{5, 5, 5}, Color.black); 13 | public Screen() 14 | { 15 | 16 | } 17 | 18 | public void paintComponent(Graphics g) 19 | { 20 | for(int i = 0; i < NumberOfPolygons; i++) 21 | DrawablePolygons[i].drawPolygon(g); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /3D - Tutorial 06/Vector.java: -------------------------------------------------------------------------------- 1 | 2 | public class Vector { 3 | double x, y, z; 4 | public Vector(double x, double y, double z) 5 | { 6 | this.x = x; 7 | this.y = y; 8 | this.z = z; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /3D - Tutorial 07/Calculator.java: -------------------------------------------------------------------------------- 1 | 2 | public class Calculator { 3 | static double CalculatePositionX(double[] ViewFrom, double[] ViewTo, double x, double y, double z) 4 | { 5 | return 0; 6 | } 7 | 8 | static double CalculatePositionY(double[] ViewFrom, double[] ViewTo, double x, double y, double z) 9 | { 10 | setStuff(ViewFrom, ViewTo); 11 | return 0; 12 | } 13 | 14 | static void setStuff(double[] ViewFrom, double[] ViewTo) 15 | { 16 | Vector ViewVector = new Vector(ViewTo[0] - ViewFrom[0], ViewTo[1] - ViewFrom[1], ViewTo[2] - ViewFrom[2]); 17 | Vector DirectionVector = new Vector(1, 1, 1); 18 | Vector PlaneVector1 = ViewVector.CrossProduct(DirectionVector); 19 | Vector PlaneVector2 = ViewVector.CrossProduct(PlaneVector1); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /3D - Tutorial 07/DDDTutorial.java: -------------------------------------------------------------------------------- 1 | import java.awt.Toolkit; 2 | import javax.swing.JFrame; 3 | 4 | public class DDDTutorial extends JFrame{ 5 | static JFrame F = new DDDTutorial(); 6 | Screen ScreenObject = new Screen(); 7 | 8 | public DDDTutorial() 9 | { 10 | add(ScreenObject); 11 | setUndecorated(true); 12 | setSize(Toolkit.getDefaultToolkit().getScreenSize()); 13 | setVisible(true); 14 | } 15 | 16 | public static void main(String[] args) 17 | { 18 | 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /3D - Tutorial 07/DPolygon.java: -------------------------------------------------------------------------------- 1 | import java.awt.Color; 2 | import java.awt.Polygon; 3 | 4 | 5 | public class DPolygon { 6 | Color c; 7 | double[] x, y, z; 8 | 9 | public DPolygon(double[] x, double[] y, double[] z, Color c) 10 | { 11 | this.x = x; 12 | this.y = y; 13 | this.z = z; 14 | this.c = c; 15 | createPolygon(); 16 | } 17 | 18 | void createPolygon() 19 | { 20 | double[] newX = new double[x.length]; 21 | double [] newY = new double[x.length]; 22 | 23 | for(int i = 0; i < x.length; i++) 24 | { 25 | newX[i] = Calculator.CalculatePositionX(Screen.ViewFrom, Screen.ViewTo, x[i], y[i], z[i]); 26 | newY[i] = Calculator.CalculatePositionY(Screen.ViewFrom, Screen.ViewTo, x[i], y[i], z[i]); 27 | } 28 | 29 | Screen.DrawablePolygons[Screen.NumberOfPolygons] = new PolygonObject(newX, newY, c); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /3D - Tutorial 07/PolygonObject.java: -------------------------------------------------------------------------------- 1 | import java.awt.Color; 2 | import java.awt.Graphics; 3 | import java.awt.Polygon; 4 | 5 | 6 | public class PolygonObject { 7 | Polygon P; 8 | Color c; 9 | 10 | public PolygonObject(double[] x, double[] y, Color c) 11 | { 12 | Screen.NumberOfPolygons++; 13 | P = new Polygon(); 14 | for(int i = 0; i < x.length; i++) 15 | P.addPoint((int)x[i], (int)y[i]); 16 | this.c = c; 17 | } 18 | 19 | void drawPolygon(Graphics g) 20 | { 21 | g.setColor(c); 22 | g.drawPolygon(P); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /3D - Tutorial 07/Screen.java: -------------------------------------------------------------------------------- 1 | import java.awt.Color; 2 | import java.awt.Graphics; 3 | 4 | import javax.swing.JPanel; 5 | 6 | public class Screen extends JPanel{ 7 | static double[] ViewFrom = new double[] {10, 10, 10}; 8 | static double[] ViewTo = new double[] {0, 0, 0}; 9 | static int NumberOfPolygons = 0; 10 | static PolygonObject[] DrawablePolygons = new PolygonObject[100]; 11 | 12 | DPolygon DPoly1 = new DPolygon(new double[]{2, 4, 2}, new double[]{2, 4, 6}, new double[]{5, 5, 5}, Color.black); 13 | public Screen() 14 | { 15 | 16 | } 17 | 18 | public void paintComponent(Graphics g) 19 | { 20 | for(int i = 0; i < NumberOfPolygons; i++) 21 | DrawablePolygons[i].drawPolygon(g); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /3D - Tutorial 07/Vector.java: -------------------------------------------------------------------------------- 1 | 2 | public class Vector { 3 | double x, y, z; 4 | public Vector(double x, double y, double z) 5 | { 6 | double length = Math.sqrt(x * x + y * y + z * z); 7 | this.x = x/length; 8 | this.y = y/length; 9 | this.z = z/length; 10 | } 11 | 12 | Vector CrossProduct(Vector V) 13 | { 14 | Vector CrossVector = new Vector( 15 | y * V.z - z * V.y, 16 | z * V.x - x * V.z, 17 | x * V.y - y * V.x); 18 | return CrossVector; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /3D - Tutorial 08/Calculator.java: -------------------------------------------------------------------------------- 1 | 2 | public class Calculator { 3 | static double DrawX = 0, DrawY = 0; 4 | static double CalculatePositionX(double[] ViewFrom, double[] ViewTo, double x, double y, double z) 5 | { 6 | setStuff(ViewFrom, ViewTo, x, y, z); 7 | return DrawX; 8 | } 9 | 10 | static double CalculatePositionY(double[] ViewFrom, double[] ViewTo, double x, double y, double z) 11 | { 12 | setStuff(ViewFrom, ViewTo, x, y, z); 13 | return DrawY; 14 | } 15 | 16 | static void setStuff(double[] ViewFrom, double[] ViewTo, double x, double y, double z) 17 | { 18 | Vector ViewVector = new Vector(ViewTo[0] - ViewFrom[0], ViewTo[1] - ViewFrom[1], ViewTo[2] - ViewFrom[2]); 19 | Vector DirectionVector = new Vector(1, 1, 1); 20 | Vector PlaneVector1 = ViewVector.CrossProduct(DirectionVector); 21 | Vector PlaneVector2 = ViewVector.CrossProduct(PlaneVector1); 22 | 23 | Vector ViewToPoint = new Vector(x - ViewFrom[0], y - ViewFrom[1], z - ViewFrom[2]); 24 | 25 | double t = (ViewVector.x * ViewTo[0] + ViewVector.y*ViewTo[1] + ViewVector.z*ViewTo[2] 26 | - (ViewVector.x * ViewFrom[0] + ViewVector.y*ViewFrom[1] + ViewVector.z*ViewFrom[2])) 27 | / (ViewVector.x * ViewToPoint.x + ViewVector.y*ViewToPoint.y + ViewVector.z*ViewToPoint.z); 28 | 29 | x = ViewFrom[0] + ViewToPoint.x * t; 30 | y = ViewFrom[1] + ViewToPoint.y * t; 31 | z = ViewFrom[2] + ViewToPoint.z * t; 32 | 33 | if(t > 0) 34 | { 35 | DrawX = PlaneVector2.x * x + PlaneVector2.y * y + PlaneVector2.z * z; 36 | DrawY = PlaneVector1.x * x + PlaneVector1.y * y + PlaneVector1.z * z; 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /3D - Tutorial 08/DDDTutorial.java: -------------------------------------------------------------------------------- 1 | import java.awt.Toolkit; 2 | import javax.swing.JFrame; 3 | 4 | public class DDDTutorial extends JFrame{ 5 | static JFrame F = new DDDTutorial(); 6 | Screen ScreenObject = new Screen(); 7 | 8 | public DDDTutorial() 9 | { 10 | add(ScreenObject); 11 | setUndecorated(true); 12 | setSize(Toolkit.getDefaultToolkit().getScreenSize()); 13 | setVisible(true); 14 | } 15 | 16 | public static void main(String[] args) 17 | { 18 | 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /3D - Tutorial 08/DPolygon.java: -------------------------------------------------------------------------------- 1 | import java.awt.Color; 2 | import java.awt.Polygon; 3 | 4 | 5 | public class DPolygon { 6 | Color c; 7 | double[] x, y, z; 8 | 9 | public DPolygon(double[] x, double[] y, double[] z, Color c) 10 | { 11 | this.x = x; 12 | this.y = y; 13 | this.z = z; 14 | this.c = c; 15 | createPolygon(); 16 | } 17 | 18 | void createPolygon() 19 | { 20 | double[] newX = new double[x.length]; 21 | double [] newY = new double[x.length]; 22 | 23 | for(int i = 0; i < x.length; i++) 24 | { 25 | newX[i] = 200 * Calculator.CalculatePositionX(Screen.ViewFrom, Screen.ViewTo, x[i], y[i], z[i]); 26 | newY[i] = 200 * Calculator.CalculatePositionY(Screen.ViewFrom, Screen.ViewTo, x[i], y[i], z[i]); 27 | } 28 | 29 | Screen.DrawablePolygons[Screen.NumberOfPolygons] = new PolygonObject(newX, newY, c); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /3D - Tutorial 08/PolygonObject.java: -------------------------------------------------------------------------------- 1 | import java.awt.Color; 2 | import java.awt.Graphics; 3 | import java.awt.Polygon; 4 | 5 | 6 | public class PolygonObject { 7 | Polygon P; 8 | Color c; 9 | 10 | public PolygonObject(double[] x, double[] y, Color c) 11 | { 12 | Screen.NumberOfPolygons++; 13 | P = new Polygon(); 14 | for(int i = 0; i < x.length; i++) 15 | P.addPoint((int)x[i], (int)y[i]); 16 | this.c = c; 17 | } 18 | 19 | void drawPolygon(Graphics g) 20 | { 21 | g.setColor(c); 22 | g.fillPolygon(P); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /3D - Tutorial 08/Screen.java: -------------------------------------------------------------------------------- 1 | import java.awt.Color; 2 | import java.awt.Graphics; 3 | 4 | import javax.swing.JPanel; 5 | 6 | public class Screen extends JPanel{ 7 | static double[] ViewFrom = new double[] {10, 10, 10}; 8 | static double[] ViewTo = new double[] {5, 0, 0}; 9 | static int NumberOfPolygons = 0; 10 | static PolygonObject[] DrawablePolygons = new PolygonObject[100]; 11 | 12 | DPolygon DPoly1 = new DPolygon(new double[]{2, 4, 2}, new double[]{2, 4, 6}, new double[]{5, 5, 5}, Color.black); 13 | public Screen() 14 | { 15 | 16 | } 17 | 18 | public void paintComponent(Graphics g) 19 | { 20 | for(int i = 0; i < NumberOfPolygons; i++) 21 | DrawablePolygons[i].drawPolygon(g); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /3D - Tutorial 08/Vector.java: -------------------------------------------------------------------------------- 1 | 2 | public class Vector { 3 | double x = 0, y = 0, z = 0; 4 | public Vector(double x, double y, double z) 5 | { 6 | double length = Math.sqrt(x * x + y * y + z * z); 7 | if(length>0) 8 | { 9 | this.x = x/length; 10 | this.y = y/length; 11 | this.z = z/length; 12 | } 13 | } 14 | 15 | Vector CrossProduct(Vector V) 16 | { 17 | Vector CrossVector = new Vector( 18 | y * V.z - z * V.y, 19 | z * V.x - x * V.z, 20 | x * V.y - y * V.x); 21 | return CrossVector; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /3D - Tutorial 09/Calculator.java: -------------------------------------------------------------------------------- 1 | 2 | public class Calculator { 3 | static double DrawX = 0, DrawY = 0; 4 | static double CalculatePositionX(double[] ViewFrom, double[] ViewTo, double x, double y, double z) 5 | { 6 | setStuff(ViewFrom, ViewTo, x, y, z); 7 | return DrawX; 8 | } 9 | 10 | static double CalculatePositionY(double[] ViewFrom, double[] ViewTo, double x, double y, double z) 11 | { 12 | setStuff(ViewFrom, ViewTo, x, y, z); 13 | return DrawY; 14 | } 15 | 16 | static void setStuff(double[] ViewFrom, double[] ViewTo, double x, double y, double z) 17 | { 18 | Vector ViewVector = new Vector(ViewTo[0] - ViewFrom[0], ViewTo[1] - ViewFrom[1], ViewTo[2] - ViewFrom[2]); 19 | Vector DirectionVector = new Vector(1, 1, 1); 20 | Vector PlaneVector1 = ViewVector.CrossProduct(DirectionVector); 21 | Vector PlaneVector2 = ViewVector.CrossProduct(PlaneVector1); 22 | 23 | Vector ViewToPoint = new Vector(x - ViewFrom[0], y - ViewFrom[1], z - ViewFrom[2]); 24 | 25 | double t = (ViewVector.x * ViewTo[0] + ViewVector.y*ViewTo[1] + ViewVector.z*ViewTo[2] 26 | - (ViewVector.x * ViewFrom[0] + ViewVector.y*ViewFrom[1] + ViewVector.z*ViewFrom[2])) 27 | / (ViewVector.x * ViewToPoint.x + ViewVector.y*ViewToPoint.y + ViewVector.z*ViewToPoint.z); 28 | 29 | x = ViewFrom[0] + ViewToPoint.x * t; 30 | y = ViewFrom[1] + ViewToPoint.y * t; 31 | z = ViewFrom[2] + ViewToPoint.z * t; 32 | 33 | if(t > 0) 34 | { 35 | DrawX = PlaneVector2.x * x + PlaneVector2.y * y + PlaneVector2.z * z; 36 | DrawY = PlaneVector1.x * x + PlaneVector1.y * y + PlaneVector1.z * z; 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /3D - Tutorial 09/DDDTutorial.java: -------------------------------------------------------------------------------- 1 | import java.awt.Toolkit; 2 | import javax.swing.JFrame; 3 | 4 | public class DDDTutorial extends JFrame{ 5 | static JFrame F = new DDDTutorial(); 6 | Screen ScreenObject = new Screen(); 7 | 8 | public DDDTutorial() 9 | { 10 | add(ScreenObject); 11 | setUndecorated(true); 12 | setSize(Toolkit.getDefaultToolkit().getScreenSize()); 13 | setVisible(true); 14 | } 15 | 16 | public static void main(String[] args) 17 | { 18 | 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /3D - Tutorial 09/DPolygon.java: -------------------------------------------------------------------------------- 1 | import java.awt.Color; 2 | import java.awt.Polygon; 3 | 4 | 5 | public class DPolygon { 6 | Color c; 7 | double[] x, y, z; 8 | 9 | public DPolygon(double[] x, double[] y, double[] z, Color c) 10 | { 11 | this.x = x; 12 | this.y = y; 13 | this.z = z; 14 | this.c = c; 15 | createPolygon(); 16 | } 17 | 18 | void createPolygon() 19 | { 20 | double[] newX = new double[x.length]; 21 | double [] newY = new double[x.length]; 22 | 23 | for(int i = 0; i < x.length; i++) 24 | { 25 | newX[i] = 200 * Calculator.CalculatePositionX(Screen.ViewFrom, Screen.ViewTo, x[i], y[i], z[i]); 26 | newY[i] = 200 * Calculator.CalculatePositionY(Screen.ViewFrom, Screen.ViewTo, x[i], y[i], z[i]); 27 | } 28 | 29 | Screen.DrawablePolygons[Screen.NumberOfPolygons] = new PolygonObject(newX, newY, c); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /3D - Tutorial 09/PolygonObject.java: -------------------------------------------------------------------------------- 1 | import java.awt.Color; 2 | import java.awt.Graphics; 3 | import java.awt.Polygon; 4 | 5 | 6 | public class PolygonObject { 7 | Polygon P; 8 | Color c; 9 | 10 | public PolygonObject(double[] x, double[] y, Color c) 11 | { 12 | Screen.NumberOfPolygons++; 13 | P = new Polygon(); 14 | for(int i = 0; i < x.length; i++) 15 | P.addPoint((int)x[i], (int)y[i]); 16 | this.c = c; 17 | } 18 | 19 | void drawPolygon(Graphics g) 20 | { 21 | g.setColor(c); 22 | g.fillPolygon(P); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /3D - Tutorial 09/Screen.java: -------------------------------------------------------------------------------- 1 | import java.awt.Color; 2 | import java.awt.Graphics; 3 | 4 | import javax.swing.JPanel; 5 | 6 | public class Screen extends JPanel{ 7 | double SleepTime = 1000/30, lastRefresh = 0; 8 | static double[] ViewFrom = new double[] {10, 10, 10}; 9 | static double[] ViewTo = new double[] {5, 0, 0}; 10 | static int NumberOfPolygons = 0; 11 | static PolygonObject[] DrawablePolygons = new PolygonObject[100]; 12 | 13 | DPolygon DPoly1 = new DPolygon(new double[]{2, 4, 2}, new double[]{2, 4, 6}, new double[]{5, 5, 5}, Color.black); 14 | public Screen() 15 | { 16 | 17 | } 18 | 19 | public void paintComponent(Graphics g) 20 | { 21 | g.clearRect(0, 0, 2000, 1200); 22 | g.drawString(System.currentTimeMillis() + "", 20, 20); 23 | for(int i = 0; i < NumberOfPolygons; i++) 24 | DrawablePolygons[i].drawPolygon(g); 25 | SleepAndRefresh(); 26 | } 27 | 28 | void SleepAndRefresh() 29 | { 30 | while(true) 31 | { 32 | if(System.currentTimeMillis() - lastRefresh > SleepTime) 33 | { 34 | lastRefresh = System.currentTimeMillis(); 35 | repaint(); 36 | break; 37 | } 38 | else 39 | { 40 | try 41 | { 42 | Thread.sleep((long)(System.currentTimeMillis() - lastRefresh)); 43 | } 44 | catch (Exception e) 45 | { 46 | 47 | } 48 | } 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /3D - Tutorial 09/Vector.java: -------------------------------------------------------------------------------- 1 | 2 | public class Vector { 3 | double x = 0, y = 0, z = 0; 4 | public Vector(double x, double y, double z) 5 | { 6 | double length = Math.sqrt(x * x + y * y + z * z); 7 | if(length>0) 8 | { 9 | this.x = x/length; 10 | this.y = y/length; 11 | this.z = z/length; 12 | } 13 | } 14 | 15 | Vector CrossProduct(Vector V) 16 | { 17 | Vector CrossVector = new Vector( 18 | y * V.z - z * V.y, 19 | z * V.x - x * V.z, 20 | x * V.y - y * V.x); 21 | return CrossVector; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /3D - Tutorial 10/Calculator.java: -------------------------------------------------------------------------------- 1 | 2 | public class Calculator { 3 | static double DrawX = 0, DrawY = 0; 4 | static double CalculatePositionX(double[] ViewFrom, double[] ViewTo, double x, double y, double z) 5 | { 6 | setStuff(ViewFrom, ViewTo, x, y, z); 7 | return DrawX; 8 | } 9 | 10 | static double CalculatePositionY(double[] ViewFrom, double[] ViewTo, double x, double y, double z) 11 | { 12 | setStuff(ViewFrom, ViewTo, x, y, z); 13 | return DrawY; 14 | } 15 | 16 | static void setStuff(double[] ViewFrom, double[] ViewTo, double x, double y, double z) 17 | { 18 | Vector ViewVector = new Vector(ViewTo[0] - ViewFrom[0], ViewTo[1] - ViewFrom[1], ViewTo[2] - ViewFrom[2]); 19 | Vector DirectionVector = new Vector(1, 1, 1); 20 | Vector PlaneVector1 = ViewVector.CrossProduct(DirectionVector); 21 | Vector PlaneVector2 = ViewVector.CrossProduct(PlaneVector1); 22 | 23 | Vector ViewToPoint = new Vector(x - ViewFrom[0], y - ViewFrom[1], z - ViewFrom[2]); 24 | 25 | double t = (ViewVector.x * ViewTo[0] + ViewVector.y*ViewTo[1] + ViewVector.z*ViewTo[2] 26 | - (ViewVector.x * ViewFrom[0] + ViewVector.y*ViewFrom[1] + ViewVector.z*ViewFrom[2])) 27 | / (ViewVector.x * ViewToPoint.x + ViewVector.y*ViewToPoint.y + ViewVector.z*ViewToPoint.z); 28 | 29 | x = ViewFrom[0] + ViewToPoint.x * t; 30 | y = ViewFrom[1] + ViewToPoint.y * t; 31 | z = ViewFrom[2] + ViewToPoint.z * t; 32 | 33 | if(t > 0) 34 | { 35 | DrawX = PlaneVector2.x * x + PlaneVector2.y * y + PlaneVector2.z * z; 36 | DrawY = PlaneVector1.x * x + PlaneVector1.y * y + PlaneVector1.z * z; 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /3D - Tutorial 10/DDDTutorial.java: -------------------------------------------------------------------------------- 1 | import java.awt.Toolkit; 2 | import javax.swing.JFrame; 3 | 4 | public class DDDTutorial extends JFrame{ 5 | static JFrame F = new DDDTutorial(); 6 | Screen ScreenObject = new Screen(); 7 | 8 | public DDDTutorial() 9 | { 10 | add(ScreenObject); 11 | setUndecorated(true); 12 | setSize(Toolkit.getDefaultToolkit().getScreenSize()); 13 | setVisible(true); 14 | } 15 | 16 | public static void main(String[] args) 17 | { 18 | 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /3D - Tutorial 10/DPolygon.java: -------------------------------------------------------------------------------- 1 | import java.awt.Color; 2 | import java.awt.Polygon; 3 | 4 | 5 | public class DPolygon { 6 | Color c; 7 | double[] x, y, z; 8 | int poly = 0; 9 | 10 | public DPolygon(double[] x, double[] y, double[] z, Color c) 11 | { 12 | Screen.NumberOf3DPolygons++; 13 | this.x = x; 14 | this.y = y; 15 | this.z = z; 16 | this.c = c; 17 | createPolygon(); 18 | } 19 | 20 | void createPolygon() 21 | { 22 | double[] newX = new double[x.length]; 23 | double [] newY = new double[x.length]; 24 | 25 | for(int i = 0; i < x.length; i++) 26 | { 27 | newX[i] = 500 + 50 * Calculator.CalculatePositionX(Screen.ViewFrom, Screen.ViewTo, x[i], y[i], z[i]); 28 | newY[i] = 500 + 50 * Calculator.CalculatePositionY(Screen.ViewFrom, Screen.ViewTo, x[i], y[i], z[i]); 29 | } 30 | 31 | poly = Screen.NumberOfPolygons; 32 | Screen.DrawablePolygons[Screen.NumberOfPolygons] = new PolygonObject(newX, newY, c); 33 | } 34 | 35 | void updatePolygon() 36 | { 37 | double[] newX = new double[x.length]; 38 | double [] newY = new double[x.length]; 39 | 40 | for(int i = 0; i < x.length; i++) 41 | { 42 | newX[i] = 500 + 50 * Calculator.CalculatePositionX(Screen.ViewFrom, Screen.ViewTo, x[i], y[i], z[i]); 43 | newY[i] = 500 + 50 * Calculator.CalculatePositionY(Screen.ViewFrom, Screen.ViewTo, x[i], y[i], z[i]); 44 | } 45 | 46 | Screen.DrawablePolygons[poly] = new PolygonObject(newX, newY, c); 47 | Screen.NumberOfPolygons --; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /3D - Tutorial 10/PolygonObject.java: -------------------------------------------------------------------------------- 1 | import java.awt.Color; 2 | import java.awt.Graphics; 3 | import java.awt.Polygon; 4 | 5 | 6 | public class PolygonObject { 7 | Polygon P; 8 | Color c; 9 | 10 | public PolygonObject(double[] x, double[] y, Color c) 11 | { 12 | Screen.NumberOfPolygons++; 13 | P = new Polygon(); 14 | for(int i = 0; i < x.length; i++) 15 | P.addPoint((int)x[i], (int)y[i]); 16 | this.c = c; 17 | } 18 | 19 | void drawPolygon(Graphics g) 20 | { 21 | g.setColor(c); 22 | g.fillPolygon(P); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /3D - Tutorial 10/Screen.java: -------------------------------------------------------------------------------- 1 | import java.awt.Color; 2 | import java.awt.Graphics; 3 | import java.awt.event.KeyEvent; 4 | import java.awt.event.KeyListener; 5 | 6 | import javax.swing.JPanel; 7 | 8 | public class Screen extends JPanel implements KeyListener{ 9 | double SleepTime = 1000/30, lastRefresh = 0; 10 | static double[] ViewFrom = new double[] {10, 10, 10}; 11 | static double[] ViewTo = new double[] {5, 0, 0}; 12 | static int NumberOfPolygons = 0, NumberOf3DPolygons = 0; 13 | static PolygonObject[] DrawablePolygons = new PolygonObject[100]; 14 | static DPolygon[] DPolygons = new DPolygon[100]; 15 | 16 | public Screen() 17 | { 18 | addKeyListener(this); 19 | setFocusable(true); 20 | DPolygons[0] = new DPolygon(new double[]{0, 2, 2, 0}, new double[]{0, 0, 2, 2}, new double[]{0, 0, 0, 0}, Color.black); 21 | DPolygons[1] = new DPolygon(new double[]{0, 2, 2, 0}, new double[]{0, 0, 2, 2}, new double[]{3, 3, 3, 3 }, Color.black); 22 | } 23 | 24 | public void paintComponent(Graphics g) 25 | { 26 | g.clearRect(0, 0, 2000, 1200); 27 | g.drawString(System.currentTimeMillis() + "", 20, 20); 28 | 29 | for(int i = 0; i < NumberOf3DPolygons; i++) 30 | DPolygons[i].updatePolygon(); 31 | 32 | for(int i = 0; i < NumberOfPolygons; i++) 33 | DrawablePolygons[i].drawPolygon(g); 34 | SleepAndRefresh(); 35 | } 36 | 37 | void SleepAndRefresh() 38 | { 39 | while(true) 40 | { 41 | if(System.currentTimeMillis() - lastRefresh > SleepTime) 42 | { 43 | lastRefresh = System.currentTimeMillis(); 44 | repaint(); 45 | break; 46 | } 47 | else 48 | { 49 | try 50 | { 51 | Thread.sleep((long)(System.currentTimeMillis() - lastRefresh)); 52 | } 53 | catch (Exception e) 54 | { 55 | 56 | } 57 | } 58 | } 59 | } 60 | 61 | public void keyPressed(KeyEvent e) { 62 | if(e.getKeyCode() == KeyEvent.VK_LEFT) 63 | ViewFrom[0] --; 64 | if(e.getKeyCode() == KeyEvent.VK_RIGHT) 65 | ViewFrom[0] ++; 66 | if(e.getKeyCode() == KeyEvent.VK_UP) 67 | ViewFrom[1] --; 68 | if(e.getKeyCode() == KeyEvent.VK_DOWN) 69 | ViewFrom[0] ++; 70 | } 71 | 72 | public void keyReleased(KeyEvent e) { 73 | } 74 | 75 | public void keyTyped(KeyEvent e) { 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /3D - Tutorial 10/Vector.java: -------------------------------------------------------------------------------- 1 | 2 | public class Vector { 3 | double x = 0, y = 0, z = 0; 4 | public Vector(double x, double y, double z) 5 | { 6 | double length = Math.sqrt(x * x + y * y + z * z); 7 | if(length>0) 8 | { 9 | this.x = x/length; 10 | this.y = y/length; 11 | this.z = z/length; 12 | } 13 | } 14 | 15 | Vector CrossProduct(Vector V) 16 | { 17 | Vector CrossVector = new Vector( 18 | y * V.z - z * V.y, 19 | z * V.x - x * V.z, 20 | x * V.y - y * V.x); 21 | return CrossVector; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /3D - Tutorial 11/Calculator.java: -------------------------------------------------------------------------------- 1 | 2 | public class Calculator { 3 | static double DrawX = 0, DrawY = 0, t; 4 | static double CalculatePositionX(double[] ViewFrom, double[] ViewTo, double x, double y, double z) 5 | { 6 | setStuff(ViewFrom, ViewTo, x, y, z); 7 | return DrawX; 8 | } 9 | 10 | static double CalculatePositionY(double[] ViewFrom, double[] ViewTo, double x, double y, double z) 11 | { 12 | setStuff(ViewFrom, ViewTo, x, y, z); 13 | return DrawY; 14 | } 15 | 16 | static void setStuff(double[] ViewFrom, double[] ViewTo, double x, double y, double z) 17 | { 18 | Vector ViewVector = new Vector(ViewTo[0] - ViewFrom[0], ViewTo[1] - ViewFrom[1], ViewTo[2] - ViewFrom[2]); 19 | Vector DirectionVector = new Vector(1, 1, 1); 20 | Vector PlaneVector1 = ViewVector.CrossProduct(DirectionVector); 21 | Vector PlaneVector2 = ViewVector.CrossProduct(PlaneVector1); 22 | 23 | Vector ViewToPoint = new Vector(x - ViewFrom[0], y - ViewFrom[1], z - ViewFrom[2]); 24 | 25 | t = (ViewVector.x * ViewTo[0] + ViewVector.y*ViewTo[1] + ViewVector.z*ViewTo[2] 26 | - (ViewVector.x * ViewFrom[0] + ViewVector.y*ViewFrom[1] + ViewVector.z*ViewFrom[2])) 27 | / (ViewVector.x * ViewToPoint.x + ViewVector.y*ViewToPoint.y + ViewVector.z*ViewToPoint.z); 28 | 29 | x = ViewFrom[0] + ViewToPoint.x * t; 30 | y = ViewFrom[1] + ViewToPoint.y * t; 31 | z = ViewFrom[2] + ViewToPoint.z * t; 32 | 33 | if(t > 0) 34 | { 35 | DrawX = PlaneVector2.x * x + PlaneVector2.y * y + PlaneVector2.z * z; 36 | DrawY = PlaneVector1.x * x + PlaneVector1.y * y + PlaneVector1.z * z; 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /3D - Tutorial 11/DDDTutorial.java: -------------------------------------------------------------------------------- 1 | import java.awt.Toolkit; 2 | import javax.swing.JFrame; 3 | 4 | public class DDDTutorial extends JFrame{ 5 | static JFrame F = new DDDTutorial(); 6 | Screen ScreenObject = new Screen(); 7 | 8 | public DDDTutorial() 9 | { 10 | add(ScreenObject); 11 | setUndecorated(true); 12 | setSize(Toolkit.getDefaultToolkit().getScreenSize()); 13 | setVisible(true); 14 | } 15 | 16 | public static void main(String[] args) 17 | { 18 | 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /3D - Tutorial 11/DPolygon.java: -------------------------------------------------------------------------------- 1 | import java.awt.Color; 2 | import java.awt.Polygon; 3 | 4 | 5 | public class DPolygon { 6 | Color c; 7 | double[] x, y, z; 8 | int poly = 0; 9 | 10 | public DPolygon(double[] x, double[] y, double[] z, Color c) 11 | { 12 | Screen.NumberOf3DPolygons++; 13 | this.x = x; 14 | this.y = y; 15 | this.z = z; 16 | this.c = c; 17 | createPolygon(); 18 | } 19 | 20 | void createPolygon() 21 | { 22 | double total = 0; 23 | double[] newX = new double[x.length]; 24 | double [] newY = new double[x.length]; 25 | 26 | for(int i = 0; i < x.length; i++) 27 | { 28 | newX[i] = 500 + 50 * Calculator.CalculatePositionX(Screen.ViewFrom, Screen.ViewTo, x[i], y[i], z[i]); 29 | newY[i] = 500 + 50 * Calculator.CalculatePositionY(Screen.ViewFrom, Screen.ViewTo, x[i], y[i], z[i]); 30 | total += Calculator.t; 31 | } 32 | 33 | poly = Screen.NumberOfPolygons; 34 | Screen.DrawablePolygons[Screen.NumberOfPolygons] = new PolygonObject(newX, newY, c); 35 | Screen.DrawablePolygons[poly].AvgDist = total / x.length; 36 | } 37 | 38 | void updatePolygon() 39 | { 40 | double[] newX = new double[x.length]; 41 | double [] newY = new double[x.length]; 42 | 43 | for(int i = 0; i < x.length; i++) 44 | { 45 | newX[i] = 500 + 50 * Calculator.CalculatePositionX(Screen.ViewFrom, Screen.ViewTo, x[i], y[i], z[i]); 46 | newY[i] = 500 + 50 * Calculator.CalculatePositionY(Screen.ViewFrom, Screen.ViewTo, x[i], y[i], z[i]); 47 | } 48 | 49 | Screen.DrawablePolygons[poly] = new PolygonObject(newX, newY, c); 50 | Screen.NumberOfPolygons --; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /3D - Tutorial 11/PolygonObject.java: -------------------------------------------------------------------------------- 1 | import java.awt.Color; 2 | import java.awt.Graphics; 3 | import java.awt.Polygon; 4 | 5 | 6 | public class PolygonObject { 7 | Polygon P; 8 | Color c; 9 | double AvgDist = 0; 10 | 11 | public PolygonObject(double[] x, double[] y, Color c) 12 | { 13 | Screen.NumberOfPolygons++; 14 | P = new Polygon(); 15 | for(int i = 0; i < x.length; i++) 16 | P.addPoint((int)x[i], (int)y[i]); 17 | this.c = c; 18 | } 19 | 20 | void drawPolygon(Graphics g) 21 | { 22 | g.setColor(c); 23 | g.fillPolygon(P); 24 | g.setColor(Color.black); 25 | g.drawPolygon(P); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /3D - Tutorial 11/Screen.java: -------------------------------------------------------------------------------- 1 | import java.awt.Color; 2 | import java.awt.Graphics; 3 | import java.awt.event.KeyEvent; 4 | import java.awt.event.KeyListener; 5 | 6 | import javax.swing.JPanel; 7 | 8 | public class Screen extends JPanel implements KeyListener{ 9 | double SleepTime = 1000/30, lastRefresh = 0; 10 | static double[] ViewFrom = new double[] {10, 10, 10}; 11 | static double[] ViewTo = new double[] {5, 0, 0}; 12 | static int NumberOfPolygons = 0, NumberOf3DPolygons = 0; 13 | static PolygonObject[] DrawablePolygons = new PolygonObject[100]; 14 | static DPolygon[] DPolygons = new DPolygon[100]; 15 | int[] NewOrder; 16 | 17 | public Screen() 18 | { 19 | addKeyListener(this); 20 | setFocusable(true); 21 | DPolygons[NumberOf3DPolygons] = new DPolygon(new double[]{0, 2, 2, 0}, new double[]{0, 0, 2, 2}, new double[]{0, 0, 0, 0}, Color.gray); 22 | DPolygons[NumberOf3DPolygons] = new DPolygon(new double[]{0, 2, 2, 0}, new double[]{0, 0, 2, 2}, new double[]{3, 3, 3, 3}, Color.gray); 23 | DPolygons[NumberOf3DPolygons] = new DPolygon(new double[]{0, 2, 2, 0}, new double[]{0, 0, 0, 0}, new double[]{0, 0, 3, 3}, Color.gray); 24 | DPolygons[NumberOf3DPolygons] = new DPolygon(new double[]{0, 2, 2, 0}, new double[]{2, 2, 2, 2}, new double[]{0, 0, 3, 3}, Color.gray); 25 | DPolygons[NumberOf3DPolygons] = new DPolygon(new double[]{0, 0, 0, 0}, new double[]{0, 2, 2, 0}, new double[]{0, 0, 3, 3}, Color.gray); 26 | DPolygons[NumberOf3DPolygons] = new DPolygon(new double[]{2, 2, 2, 2}, new double[]{0, 2, 2, 0}, new double[]{0, 0, 3, 3}, Color.gray); 27 | } 28 | 29 | public void paintComponent(Graphics g) 30 | { 31 | g.clearRect(0, 0, 2000, 1200); 32 | g.drawString(System.currentTimeMillis() + "", 20, 20); 33 | 34 | for(int i = 0; i < NumberOf3DPolygons; i++) 35 | DPolygons[i].updatePolygon(); 36 | 37 | setOrder(); 38 | 39 | for(int i = 0; i < NumberOfPolygons; i++) 40 | DrawablePolygons[NewOrder[i]].drawPolygon(g); 41 | SleepAndRefresh(); 42 | } 43 | 44 | void setOrder() 45 | { 46 | double[] k = new double[NumberOfPolygons]; 47 | NewOrder = new int[NumberOfPolygons]; 48 | 49 | for(int i = 0; i < NumberOfPolygons; i++) 50 | { 51 | k[i] = DrawablePolygons[i].AvgDist; 52 | NewOrder[i] = i; 53 | } 54 | 55 | double temp; 56 | int tempr; 57 | for (int a = 0; a < k.length-1; a++) 58 | for (int b = 0; b < k.length-1; b++) 59 | if(k[b] < k[b + 1]) 60 | { 61 | temp = k[b]; 62 | tempr = NewOrder[b]; 63 | NewOrder[b] = NewOrder[b + 1]; 64 | k[b] = k[b + 1]; 65 | 66 | NewOrder[b + 1] = tempr; 67 | k[b + 1] = temp; 68 | } 69 | } 70 | 71 | void SleepAndRefresh() 72 | { 73 | while(true) 74 | { 75 | if(System.currentTimeMillis() - lastRefresh > SleepTime) 76 | { 77 | lastRefresh = System.currentTimeMillis(); 78 | repaint(); 79 | break; 80 | } 81 | else 82 | { 83 | try 84 | { 85 | Thread.sleep((long)(System.currentTimeMillis() - lastRefresh)); 86 | } 87 | catch (Exception e) 88 | { 89 | 90 | } 91 | } 92 | } 93 | } 94 | 95 | public void keyPressed(KeyEvent e) { 96 | if(e.getKeyCode() == KeyEvent.VK_LEFT) 97 | ViewFrom[0] --; 98 | if(e.getKeyCode() == KeyEvent.VK_RIGHT) 99 | ViewFrom[0] ++; 100 | if(e.getKeyCode() == KeyEvent.VK_UP) 101 | ViewFrom[1] --; 102 | if(e.getKeyCode() == KeyEvent.VK_DOWN) 103 | ViewFrom[0] ++; 104 | } 105 | 106 | public void keyReleased(KeyEvent e) { 107 | } 108 | 109 | public void keyTyped(KeyEvent e) { 110 | } 111 | } 112 | -------------------------------------------------------------------------------- /3D - Tutorial 11/Vector.java: -------------------------------------------------------------------------------- 1 | 2 | public class Vector { 3 | double x = 0, y = 0, z = 0; 4 | public Vector(double x, double y, double z) 5 | { 6 | double length = Math.sqrt(x * x + y * y + z * z); 7 | if(length>0) 8 | { 9 | this.x = x/length; 10 | this.y = y/length; 11 | this.z = z/length; 12 | } 13 | } 14 | 15 | Vector CrossProduct(Vector V) 16 | { 17 | Vector CrossVector = new Vector( 18 | y * V.z - z * V.y, 19 | z * V.x - x * V.z, 20 | x * V.y - y * V.x); 21 | return CrossVector; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /3D - Tutorial 12/Calculator.java: -------------------------------------------------------------------------------- 1 | 2 | public class Calculator { 3 | static double DrawX = 0, DrawY = 0; 4 | static double CalculatePositionX(double[] ViewFrom, double[] ViewTo, double x, double y, double z) 5 | { 6 | setStuff(ViewFrom, ViewTo, x, y, z); 7 | return DrawX; 8 | } 9 | 10 | static double CalculatePositionY(double[] ViewFrom, double[] ViewTo, double x, double y, double z) 11 | { 12 | setStuff(ViewFrom, ViewTo, x, y, z); 13 | return DrawY; 14 | } 15 | 16 | static void setStuff(double[] ViewFrom, double[] ViewTo, double x, double y, double z) 17 | { 18 | Vector ViewVector = new Vector(ViewTo[0] - ViewFrom[0], ViewTo[1] - ViewFrom[1], ViewTo[2] - ViewFrom[2]); 19 | Vector DirectionVector = new Vector(1, 1, 1); 20 | Vector PlaneVector1 = ViewVector.CrossProduct(DirectionVector); 21 | Vector PlaneVector2 = ViewVector.CrossProduct(PlaneVector1); 22 | 23 | Vector ViewToPoint = new Vector(x - ViewFrom[0], y - ViewFrom[1], z - ViewFrom[2]); 24 | 25 | double t = (ViewVector.x * ViewTo[0] + ViewVector.y*ViewTo[1] + ViewVector.z*ViewTo[2] 26 | - (ViewVector.x * ViewFrom[0] + ViewVector.y*ViewFrom[1] + ViewVector.z*ViewFrom[2])) 27 | / (ViewVector.x * ViewToPoint.x + ViewVector.y*ViewToPoint.y + ViewVector.z*ViewToPoint.z); 28 | 29 | x = ViewFrom[0] + ViewToPoint.x * t; 30 | y = ViewFrom[1] + ViewToPoint.y * t; 31 | z = ViewFrom[2] + ViewToPoint.z * t; 32 | 33 | if(t > 0) 34 | { 35 | DrawX = PlaneVector2.x * x + PlaneVector2.y * y + PlaneVector2.z * z; 36 | DrawY = PlaneVector1.x * x + PlaneVector1.y * y + PlaneVector1.z * z; 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /3D - Tutorial 12/DDDTutorial.java: -------------------------------------------------------------------------------- 1 | import java.awt.Toolkit; 2 | import javax.swing.JFrame; 3 | 4 | public class DDDTutorial extends JFrame{ 5 | static JFrame F = new DDDTutorial(); 6 | Screen ScreenObject = new Screen(); 7 | 8 | public DDDTutorial() 9 | { 10 | add(ScreenObject); 11 | setUndecorated(true); 12 | setSize(Toolkit.getDefaultToolkit().getScreenSize()); 13 | setVisible(true); 14 | } 15 | 16 | public static void main(String[] args) 17 | { 18 | 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /3D - Tutorial 12/DPolygon.java: -------------------------------------------------------------------------------- 1 | import java.awt.Color; 2 | import java.awt.Polygon; 3 | 4 | 5 | public class DPolygon { 6 | Color c; 7 | double[] x, y, z; 8 | int poly = 0; 9 | 10 | public DPolygon(double[] x, double[] y, double[] z, Color c) 11 | { 12 | Screen.NumberOf3DPolygons++; 13 | this.x = x; 14 | this.y = y; 15 | this.z = z; 16 | this.c = c; 17 | createPolygon(); 18 | } 19 | 20 | void createPolygon() 21 | { 22 | double[] newX = new double[x.length]; 23 | double [] newY = new double[x.length]; 24 | 25 | for(int i = 0; i < x.length; i++) 26 | { 27 | newX[i] = 500 + 50 * Calculator.CalculatePositionX(Screen.ViewFrom, Screen.ViewTo, x[i], y[i], z[i]); 28 | newY[i] = 500 + 50 * Calculator.CalculatePositionY(Screen.ViewFrom, Screen.ViewTo, x[i], y[i], z[i]); 29 | } 30 | 31 | poly = Screen.NumberOfPolygons; 32 | Screen.DrawablePolygons[Screen.NumberOfPolygons] = new PolygonObject(newX, newY, c); 33 | Screen.DrawablePolygons[poly].AvgDist = GetDist(); 34 | } 35 | 36 | void updatePolygon() 37 | { 38 | double[] newX = new double[x.length]; 39 | double [] newY = new double[x.length]; 40 | 41 | for(int i = 0; i < x.length; i++) 42 | { 43 | newX[i] = 500 + 50 * Calculator.CalculatePositionX(Screen.ViewFrom, Screen.ViewTo, x[i], y[i], z[i]); 44 | newY[i] = 500 + 50 * Calculator.CalculatePositionY(Screen.ViewFrom, Screen.ViewTo, x[i], y[i], z[i]); 45 | } 46 | 47 | Screen.DrawablePolygons[poly] = new PolygonObject(newX, newY, c); 48 | Screen.DrawablePolygons[poly].AvgDist = GetDist(); 49 | Screen.NumberOfPolygons --; 50 | } 51 | 52 | double GetDist() 53 | { 54 | double total = 0; 55 | for(int i = 0; i < x.length; i++) 56 | total += GetDistanceToP(i); 57 | return total / x.length; 58 | } 59 | 60 | double GetDistanceToP(int i) 61 | { 62 | 63 | return Math.sqrt( 64 | (Screen.ViewFrom[0] - x[i])*(Screen.ViewFrom[0] - x[i]) + 65 | (Screen.ViewFrom[1] - y[i])*(Screen.ViewFrom[1] - y[i]) + 66 | (Screen.ViewFrom[2] - z[i])*(Screen.ViewFrom[2] - z[i])); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /3D - Tutorial 12/PolygonObject.java: -------------------------------------------------------------------------------- 1 | import java.awt.Color; 2 | import java.awt.Graphics; 3 | import java.awt.Polygon; 4 | 5 | 6 | public class PolygonObject { 7 | Polygon P; 8 | Color c; 9 | double AvgDist = 0; 10 | 11 | public PolygonObject(double[] x, double[] y, Color c) 12 | { 13 | Screen.NumberOfPolygons++; 14 | P = new Polygon(); 15 | for(int i = 0; i < x.length; i++) 16 | P.addPoint((int)x[i], (int)y[i]); 17 | this.c = c; 18 | } 19 | 20 | void drawPolygon(Graphics g) 21 | { 22 | g.setColor(c); 23 | g.fillPolygon(P); 24 | g.setColor(Color.black); 25 | g.drawPolygon(P); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /3D - Tutorial 12/Screen.java: -------------------------------------------------------------------------------- 1 | import java.awt.Color; 2 | import java.awt.Graphics; 3 | import java.awt.event.KeyEvent; 4 | import java.awt.event.KeyListener; 5 | 6 | import javax.swing.JPanel; 7 | 8 | public class Screen extends JPanel implements KeyListener{ 9 | double SleepTime = 1000/30, lastRefresh = 0; 10 | static double[] ViewFrom = new double[] {10, 10, 10}; 11 | static double[] ViewTo = new double[] {5, 0, 0}; 12 | static int NumberOfPolygons = 0, NumberOf3DPolygons = 0; 13 | static PolygonObject[] DrawablePolygons = new PolygonObject[100]; 14 | static DPolygon[] DPolygons = new DPolygon[100]; 15 | int[] NewOrder; 16 | 17 | public Screen() 18 | { 19 | addKeyListener(this); 20 | setFocusable(true); 21 | DPolygons[NumberOf3DPolygons] = new DPolygon(new double[]{0, 2, 2, 0}, new double[]{0, 0, 2, 2}, new double[]{0, 0, 0, 0}, Color.gray); 22 | DPolygons[NumberOf3DPolygons] = new DPolygon(new double[]{0, 2, 2, 0}, new double[]{0, 0, 2, 2}, new double[]{3, 3, 3, 3}, Color.gray); 23 | DPolygons[NumberOf3DPolygons] = new DPolygon(new double[]{0, 2, 2, 0}, new double[]{0, 0, 0, 0}, new double[]{0, 0, 3, 3}, Color.gray); 24 | DPolygons[NumberOf3DPolygons] = new DPolygon(new double[]{0, 2, 2, 0}, new double[]{2, 2, 2, 2}, new double[]{0, 0, 3, 3}, Color.gray); 25 | DPolygons[NumberOf3DPolygons] = new DPolygon(new double[]{0, 0, 0, 0}, new double[]{0, 2, 2, 0}, new double[]{0, 0, 3, 3}, Color.gray); 26 | DPolygons[NumberOf3DPolygons] = new DPolygon(new double[]{2, 2, 2, 2}, new double[]{0, 2, 2, 0}, new double[]{0, 0, 3, 3}, Color.gray); 27 | } 28 | 29 | public void paintComponent(Graphics g) 30 | { 31 | g.clearRect(0, 0, 2000, 1200); 32 | g.drawString(System.currentTimeMillis() + "", 20, 20); 33 | 34 | for(int i = 0; i < NumberOf3DPolygons; i++) 35 | DPolygons[i].updatePolygon(); 36 | 37 | setOrder(); 38 | 39 | for(int i = 0; i < NumberOfPolygons; i++) 40 | DrawablePolygons[NewOrder[i]].drawPolygon(g); 41 | SleepAndRefresh(); 42 | } 43 | 44 | void setOrder() 45 | { 46 | double[] k = new double[NumberOfPolygons]; 47 | NewOrder = new int[NumberOfPolygons]; 48 | 49 | for(int i = 0; i < NumberOfPolygons; i++) 50 | { 51 | k[i] = DrawablePolygons[i].AvgDist; 52 | NewOrder[i] = i; 53 | } 54 | 55 | double temp; 56 | int tempr; 57 | for (int a = 0; a < k.length-1; a++) 58 | for (int b = 0; b < k.length-1; b++) 59 | if(k[b] < k[b + 1]) 60 | { 61 | temp = k[b]; 62 | tempr = NewOrder[b]; 63 | NewOrder[b] = NewOrder[b + 1]; 64 | k[b] = k[b + 1]; 65 | 66 | NewOrder[b + 1] = tempr; 67 | k[b + 1] = temp; 68 | } 69 | } 70 | 71 | void SleepAndRefresh() 72 | { 73 | while(true) 74 | { 75 | if(System.currentTimeMillis() - lastRefresh > SleepTime) 76 | { 77 | lastRefresh = System.currentTimeMillis(); 78 | repaint(); 79 | break; 80 | } 81 | else 82 | { 83 | try 84 | { 85 | Thread.sleep((long)(System.currentTimeMillis() - lastRefresh)); 86 | } 87 | catch (Exception e) 88 | { 89 | 90 | } 91 | } 92 | } 93 | } 94 | 95 | public void keyPressed(KeyEvent e) { 96 | if(e.getKeyCode() == KeyEvent.VK_LEFT) 97 | ViewFrom[0] --; 98 | if(e.getKeyCode() == KeyEvent.VK_RIGHT) 99 | ViewFrom[0] ++; 100 | if(e.getKeyCode() == KeyEvent.VK_UP) 101 | ViewFrom[1] --; 102 | if(e.getKeyCode() == KeyEvent.VK_DOWN) 103 | ViewFrom[0] ++; 104 | } 105 | 106 | public void keyReleased(KeyEvent e) { 107 | } 108 | 109 | public void keyTyped(KeyEvent e) { 110 | } 111 | } 112 | -------------------------------------------------------------------------------- /3D - Tutorial 12/Vector.java: -------------------------------------------------------------------------------- 1 | 2 | public class Vector { 3 | double x = 0, y = 0, z = 0; 4 | public Vector(double x, double y, double z) 5 | { 6 | double length = Math.sqrt(x * x + y * y + z * z); 7 | if(length>0) 8 | { 9 | this.x = x/length; 10 | this.y = y/length; 11 | this.z = z/length; 12 | } 13 | } 14 | 15 | Vector CrossProduct(Vector V) 16 | { 17 | Vector CrossVector = new Vector( 18 | y * V.z - z * V.y, 19 | z * V.x - x * V.z, 20 | x * V.y - y * V.x); 21 | return CrossVector; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /3D - Tutorial 13/Calculator.java: -------------------------------------------------------------------------------- 1 | 2 | public class Calculator { 3 | static double DrawX = 0, DrawY = 0; 4 | static double CalculatePositionX(double[] ViewFrom, double[] ViewTo, double x, double y, double z) 5 | { 6 | setStuff(ViewFrom, ViewTo, x, y, z); 7 | return DrawX; 8 | } 9 | 10 | static double CalculatePositionY(double[] ViewFrom, double[] ViewTo, double x, double y, double z) 11 | { 12 | setStuff(ViewFrom, ViewTo, x, y, z); 13 | return DrawY; 14 | } 15 | 16 | static void setStuff(double[] ViewFrom, double[] ViewTo, double x, double y, double z) 17 | { 18 | Vector ViewVector = new Vector(ViewTo[0] - ViewFrom[0], ViewTo[1] - ViewFrom[1], ViewTo[2] - ViewFrom[2]); 19 | Vector DirectionVector = new Vector(1, 1, 1); 20 | Vector PlaneVector1 = ViewVector.CrossProduct(DirectionVector); 21 | Vector PlaneVector2 = ViewVector.CrossProduct(PlaneVector1); 22 | 23 | Vector RotationVector = GetRotationVector(ViewFrom, ViewTo); 24 | Vector WeirdVector1 = ViewVector.CrossProduct(RotationVector); 25 | Vector WeirdVector2 = ViewVector.CrossProduct(WeirdVector1); 26 | 27 | Vector ViewToPoint = new Vector(x - ViewFrom[0], y - ViewFrom[1], z - ViewFrom[2]); 28 | 29 | double t = (ViewVector.x * ViewTo[0] + ViewVector.y*ViewTo[1] + ViewVector.z*ViewTo[2] 30 | - (ViewVector.x * ViewFrom[0] + ViewVector.y*ViewFrom[1] + ViewVector.z*ViewFrom[2])) 31 | / (ViewVector.x * ViewToPoint.x + ViewVector.y*ViewToPoint.y + ViewVector.z*ViewToPoint.z); 32 | 33 | x = ViewFrom[0] + ViewToPoint.x * t; 34 | y = ViewFrom[1] + ViewToPoint.y * t; 35 | z = ViewFrom[2] + ViewToPoint.z * t; 36 | 37 | if(t > 0) 38 | { 39 | DrawX = WeirdVector2.x * x + WeirdVector2.y * y + WeirdVector2.z * z; 40 | DrawY = WeirdVector1.x * x + WeirdVector1.y * y + WeirdVector1.z * z; 41 | } 42 | } 43 | 44 | static Vector GetRotationVector(double[] ViewFrom, double[] ViewTo) 45 | { 46 | double dx = Math.abs(ViewFrom[0]-ViewTo[0]); 47 | double dy = Math.abs(ViewFrom[1]-ViewTo[1]); 48 | double xRot, yRot; 49 | 50 | xRot=dy/(dx+dy); 51 | yRot=dx/(dx+dy); 52 | 53 | if(ViewFrom[1]>ViewTo[1]) 54 | xRot = -xRot; 55 | if(ViewFrom[0] SleepTime) 76 | { 77 | lastRefresh = System.currentTimeMillis(); 78 | repaint(); 79 | break; 80 | } 81 | else 82 | { 83 | try 84 | { 85 | Thread.sleep((long)(System.currentTimeMillis() - lastRefresh)); 86 | } 87 | catch (Exception e) 88 | { 89 | 90 | } 91 | } 92 | } 93 | } 94 | 95 | public void keyPressed(KeyEvent e) { 96 | if(e.getKeyCode() == KeyEvent.VK_LEFT) 97 | ViewFrom[0] --; 98 | if(e.getKeyCode() == KeyEvent.VK_RIGHT) 99 | ViewFrom[0] ++; 100 | if(e.getKeyCode() == KeyEvent.VK_UP) 101 | ViewFrom[1] --; 102 | if(e.getKeyCode() == KeyEvent.VK_DOWN) 103 | ViewFrom[0] ++; 104 | } 105 | 106 | public void keyReleased(KeyEvent e) { 107 | } 108 | 109 | public void keyTyped(KeyEvent e) { 110 | } 111 | } 112 | -------------------------------------------------------------------------------- /3D - Tutorial 13/Vector.java: -------------------------------------------------------------------------------- 1 | 2 | public class Vector { 3 | double x = 0, y = 0, z = 0; 4 | public Vector(double x, double y, double z) 5 | { 6 | double length = Math.sqrt(x * x + y * y + z * z); 7 | if(length>0) 8 | { 9 | this.x = x/length; 10 | this.y = y/length; 11 | this.z = z/length; 12 | } 13 | } 14 | 15 | Vector CrossProduct(Vector V) 16 | { 17 | Vector CrossVector = new Vector( 18 | y * V.z - z * V.y, 19 | z * V.x - x * V.z, 20 | x * V.y - y * V.x); 21 | return CrossVector; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /3D - Tutorial 14/Calculator.java: -------------------------------------------------------------------------------- 1 | 2 | public class Calculator { 3 | static double DrawX = 0, DrawY = 0; 4 | static double CalculatePositionX(double[] ViewFrom, double[] ViewTo, double x, double y, double z) 5 | { 6 | setStuff(ViewFrom, ViewTo, x, y, z); 7 | return DrawX; 8 | } 9 | 10 | static double CalculatePositionY(double[] ViewFrom, double[] ViewTo, double x, double y, double z) 11 | { 12 | setStuff(ViewFrom, ViewTo, x, y, z); 13 | return DrawY; 14 | } 15 | 16 | static void setStuff(double[] ViewFrom, double[] ViewTo, double x, double y, double z) 17 | { 18 | Vector ViewVector = new Vector(ViewTo[0] - ViewFrom[0], ViewTo[1] - ViewFrom[1], ViewTo[2] - ViewFrom[2]); 19 | Vector DirectionVector = new Vector(1, 1, 1); 20 | Vector PlaneVector1 = ViewVector.CrossProduct(DirectionVector); 21 | Vector PlaneVector2 = ViewVector.CrossProduct(PlaneVector1); 22 | 23 | Vector RotationVector = GetRotationVector(ViewFrom, ViewTo); 24 | Vector WeirdVector1 = ViewVector.CrossProduct(RotationVector); 25 | Vector WeirdVector2 = ViewVector.CrossProduct(WeirdVector1); 26 | 27 | Vector ViewToPoint = new Vector(x - ViewFrom[0], y - ViewFrom[1], z - ViewFrom[2]); 28 | 29 | double t = (ViewVector.x * ViewTo[0] + ViewVector.y*ViewTo[1] + ViewVector.z*ViewTo[2] 30 | - (ViewVector.x * ViewFrom[0] + ViewVector.y*ViewFrom[1] + ViewVector.z*ViewFrom[2])) 31 | / (ViewVector.x * ViewToPoint.x + ViewVector.y*ViewToPoint.y + ViewVector.z*ViewToPoint.z); 32 | 33 | x = ViewFrom[0] + ViewToPoint.x * t; 34 | y = ViewFrom[1] + ViewToPoint.y * t; 35 | z = ViewFrom[2] + ViewToPoint.z * t; 36 | 37 | if(t > 0) 38 | { 39 | DrawX = WeirdVector2.x * x + WeirdVector2.y * y + WeirdVector2.z * z; 40 | DrawY = WeirdVector1.x * x + WeirdVector1.y * y + WeirdVector1.z * z; 41 | } 42 | } 43 | 44 | static Vector GetRotationVector(double[] ViewFrom, double[] ViewTo) 45 | { 46 | double dx = Math.abs(ViewFrom[0]-ViewTo[0]); 47 | double dy = Math.abs(ViewFrom[1]-ViewTo[1]); 48 | double xRot, yRot; 49 | 50 | xRot=dy/(dx+dy); 51 | yRot=dx/(dx+dy); 52 | 53 | if(ViewFrom[1]>ViewTo[1]) 54 | xRot = -xRot; 55 | if(ViewFrom[0] SleepTime) 81 | { 82 | lastRefresh = System.currentTimeMillis(); 83 | repaint(); 84 | break; 85 | } 86 | else 87 | { 88 | try 89 | { 90 | Thread.sleep((long)(System.currentTimeMillis() - lastRefresh)); 91 | } 92 | catch (Exception e) 93 | { 94 | 95 | } 96 | } 97 | } 98 | } 99 | 100 | void Controls() 101 | { 102 | Vector ViewVector = new Vector(ViewTo[0] - ViewFrom[0], ViewTo[1] - ViewFrom[1], ViewTo[2] - ViewFrom[2]); 103 | if(Keys[4]) 104 | { 105 | ViewFrom[0] += ViewVector.x; 106 | ViewFrom[1] += ViewVector.y; 107 | ViewFrom[2] += ViewVector.z; 108 | ViewTo[0] += ViewVector.x; 109 | ViewTo[1] += ViewVector.y; 110 | ViewTo[2] += ViewVector.z; 111 | } 112 | 113 | if(Keys[6]) 114 | { 115 | ViewFrom[0] -= ViewVector.x; 116 | ViewFrom[1] -= ViewVector.y; 117 | ViewFrom[2] -= ViewVector.z; 118 | ViewTo[0] -= ViewVector.x; 119 | ViewTo[1] -= ViewVector.y; 120 | ViewTo[2] -= ViewVector.z; 121 | } 122 | 123 | Vector VerticalVector = new Vector(0, 0, 1); 124 | Vector SideViewVector = ViewVector.CrossProduct(VerticalVector); 125 | 126 | if(Keys[5]) 127 | { 128 | ViewFrom[0] += SideViewVector.x; 129 | ViewFrom[1] += SideViewVector.y; 130 | ViewFrom[2] += SideViewVector.z; 131 | ViewTo[0] += SideViewVector.x; 132 | ViewTo[1] += SideViewVector.y; 133 | ViewTo[2] += SideViewVector.z; 134 | } 135 | 136 | if(Keys[7]) 137 | { 138 | ViewFrom[0] -= SideViewVector.x; 139 | ViewFrom[1] -= SideViewVector.y; 140 | ViewFrom[2] -= SideViewVector.z; 141 | ViewTo[0] -= SideViewVector.x; 142 | ViewTo[1] -= SideViewVector.y; 143 | ViewTo[2] -= SideViewVector.z; 144 | } 145 | } 146 | 147 | public void keyPressed(KeyEvent e) { 148 | if(e.getKeyCode() == KeyEvent.VK_LEFT) 149 | Keys[0] = true; 150 | if(e.getKeyCode() == KeyEvent.VK_RIGHT) 151 | Keys[1] = true; 152 | if(e.getKeyCode() == KeyEvent.VK_UP) 153 | Keys[2] = true; 154 | if(e.getKeyCode() == KeyEvent.VK_DOWN) 155 | Keys[3] = true; 156 | if(e.getKeyCode() == KeyEvent.VK_W) 157 | Keys[4] = true; 158 | if(e.getKeyCode() == KeyEvent.VK_A) 159 | Keys[5] = true; 160 | if(e.getKeyCode() == KeyEvent.VK_S) 161 | Keys[6] = true; 162 | if(e.getKeyCode() == KeyEvent.VK_D) 163 | Keys[7] = true; 164 | } 165 | 166 | public void keyReleased(KeyEvent e) { 167 | if(e.getKeyCode() == KeyEvent.VK_LEFT) 168 | Keys[0] = false; 169 | if(e.getKeyCode() == KeyEvent.VK_RIGHT) 170 | Keys[1] = false; 171 | if(e.getKeyCode() == KeyEvent.VK_UP) 172 | Keys[2] = false; 173 | if(e.getKeyCode() == KeyEvent.VK_DOWN) 174 | Keys[3] = false; 175 | if(e.getKeyCode() == KeyEvent.VK_W) 176 | Keys[4] = false; 177 | if(e.getKeyCode() == KeyEvent.VK_A) 178 | Keys[5] = false; 179 | if(e.getKeyCode() == KeyEvent.VK_S) 180 | Keys[6] = false; 181 | if(e.getKeyCode() == KeyEvent.VK_D) 182 | Keys[7] = false; 183 | 184 | } 185 | 186 | public void keyTyped(KeyEvent e) { 187 | } 188 | } 189 | -------------------------------------------------------------------------------- /3D - Tutorial 14/Vector.java: -------------------------------------------------------------------------------- 1 | 2 | public class Vector { 3 | double x = 0, y = 0, z = 0; 4 | public Vector(double x, double y, double z) 5 | { 6 | double length = Math.sqrt(x * x + y * y + z * z); 7 | if(length>0) 8 | { 9 | this.x = x/length; 10 | this.y = y/length; 11 | this.z = z/length; 12 | } 13 | } 14 | 15 | Vector CrossProduct(Vector V) 16 | { 17 | Vector CrossVector = new Vector( 18 | y * V.z - z * V.y, 19 | z * V.x - x * V.z, 20 | x * V.y - y * V.x); 21 | return CrossVector; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /3D Engine - 13-06-15/Calculator.java: -------------------------------------------------------------------------------- 1 | public class Calculator { 2 | static double t = 0; 3 | static Vector W1, W2, ViewVector, RotationVector, DirectionVector, PlaneVector1, PlaneVector2; 4 | static Plane P; 5 | static double[] CalcFocusPos = new double[2]; 6 | 7 | static double[] CalculatePositionP(double[] ViewFrom, double[] ViewTo, double x, double y, double z) 8 | { 9 | double[] projP = getProj(ViewFrom, ViewTo, x, y, z, P); 10 | double[] drawP = getDrawP(projP[0], projP[1], projP[2]); 11 | return drawP; 12 | } 13 | 14 | static double[] getProj(double[] ViewFrom, double[] ViewTo, double x, double y, double z, Plane P) 15 | { 16 | Vector ViewToPoint = new Vector(x - ViewFrom[0], y - ViewFrom[1], z - ViewFrom[2]); 17 | 18 | t = (P.NV.x*P.P[0] + P.NV.y*P.P[1] + P.NV.z*P.P[2] 19 | - (P.NV.x*ViewFrom[0] + P.NV.y*ViewFrom[1] + P.NV.z*ViewFrom[2])) 20 | / (P.NV.x*ViewToPoint.x + P.NV.y*ViewToPoint.y + P.NV.z*ViewToPoint.z); 21 | 22 | x = ViewFrom[0] + ViewToPoint.x * t; 23 | y = ViewFrom[1] + ViewToPoint.y * t; 24 | z = ViewFrom[2] + ViewToPoint.z * t; 25 | 26 | return new double[] {x, y, z}; 27 | } 28 | 29 | static double[] getDrawP(double x, double y, double z) 30 | { 31 | double DrawX = W2.x * x + W2.y * y + W2.z * z; 32 | double DrawY = W1.x * x + W1.y * y + W1.z * z; 33 | return new double[]{DrawX, DrawY}; 34 | } 35 | 36 | static Vector getRotationVector(double[] ViewFrom, double[] ViewTo) 37 | { 38 | double dx = Math.abs(ViewFrom[0]-ViewTo[0]); 39 | double dy = Math.abs(ViewFrom[1]-ViewTo[1]); 40 | double xRot, yRot; 41 | xRot=dy/(dx+dy); 42 | yRot=dx/(dx+dy); 43 | 44 | if(ViewFrom[1]>ViewTo[1]) 45 | xRot = -xRot; 46 | if(ViewFrom[0] 1) 57 | DrawablePolygon.lighting = 1; 58 | if(DrawablePolygon.lighting < 0) 59 | DrawablePolygon.lighting = 0; 60 | } 61 | 62 | double GetDist() 63 | { 64 | double total = 0; 65 | for(int i=0; i DPolygons = new ArrayList(); 24 | 25 | static ArrayList Cubes = new ArrayList(); 26 | static ArrayList Prisms = new ArrayList(); 27 | static ArrayList Pyramids = new ArrayList(); 28 | 29 | //The polygon that the mouse is currently over 30 | static PolygonObject PolygonOver = null; 31 | 32 | //Used for keeping mouse in center 33 | Robot r; 34 | 35 | static double[] ViewFrom = new double[] { 15, 5, 10}, 36 | ViewTo = new double[] {0, 0, 0}, 37 | LightDir = new double[] {1, 1, 1}; 38 | 39 | 40 | //The smaller the zoom the more zoomed out you are and visa versa, although altering too far from 1000 will make it look pretty weird 41 | static double zoom = 1000, MinZoom = 500, MaxZoom = 2500, MouseX = 0, MouseY = 0, MovementSpeed = 0.5; 42 | 43 | //FPS is a bit primitive, you can set the MaxFPS as high as u want 44 | double drawFPS = 0, MaxFPS = 1000, SleepTime = 1000.0/MaxFPS, LastRefresh = 0, StartTime = System.currentTimeMillis(), LastFPSCheck = 0, Checks = 0; 45 | //VertLook goes from 0.999 to -0.999, minus being looking down and + looking up, HorLook takes any number and goes round in radians 46 | //aimSight changes the size of the center-cross. The lower HorRotSpeed or VertRotSpeed, the faster the camera will rotate in those directions 47 | double VertLook = -0.9, HorLook = 0, aimSight = 4, HorRotSpeed = 900, VertRotSpeed = 2200, SunPos = 0; 48 | 49 | //will hold the order that the polygons in the ArrayList DPolygon should be drawn meaning DPolygon.get(NewOrder[0]) gets drawn first 50 | int[] NewOrder; 51 | 52 | static boolean OutLines = true; 53 | boolean[] Keys = new boolean[4]; 54 | 55 | long repaintTime = 0; 56 | 57 | public Screen() 58 | { 59 | this.addKeyListener(this); 60 | setFocusable(true); 61 | 62 | this.addMouseListener(this); 63 | this.addMouseMotionListener(this); 64 | this.addMouseWheelListener(this); 65 | 66 | invisibleMouse(); 67 | new GenerateTerrain(); 68 | Cubes.add(new Cube(0, -5, 0, 2, 2, 2, Color.red)); 69 | Prisms.add(new Prism(6, -5, 0, 2, 2, 2, Color.green)); 70 | Pyramids.add(new Pyramid(12, -5, 0, 2, 2, 2, Color.blue)); 71 | Cubes.add(new Cube(18, -5, 0, 2, 2, 2, Color.red)); 72 | Cubes.add(new Cube(20, -5, 0, 2, 2, 2, Color.red)); 73 | Cubes.add(new Cube(22, -5, 0, 2, 2, 2, Color.red)); 74 | Cubes.add(new Cube(20, -5, 2, 2, 2, 2, Color.red)); 75 | Prisms.add(new Prism(18, -5, 2, 2, 2, 2, Color.green)); 76 | Prisms.add(new Prism(22, -5, 2, 2, 2, 2, Color.green)); 77 | Pyramids.add(new Pyramid(20, -5, 4, 2, 2, 2, Color.blue)); 78 | } 79 | 80 | public void paintComponent(Graphics g) 81 | { 82 | //Clear screen and draw background color 83 | g.setColor(new Color(140, 180, 180)); 84 | g.fillRect(0, 0, (int)DDDTutorial.ScreenSize.getWidth(), (int)DDDTutorial.ScreenSize.getHeight()); 85 | 86 | CameraMovement(); 87 | 88 | //Calculated all that is general for this camera position 89 | Calculator.SetPrederterminedInfo(); 90 | 91 | ControlSunAndLight(); 92 | 93 | //Updates each polygon for this camera position 94 | for(int i = 0; i < DPolygons.size(); i++) 95 | DPolygons.get(i).updatePolygon(); 96 | 97 | //rotate and update shape examples 98 | Cubes.get(0).rotation+=.01; 99 | Cubes.get(0).updatePoly(); 100 | 101 | Prisms.get(0).rotation+=.01; 102 | Prisms.get(0).updatePoly(); 103 | 104 | Pyramids.get(0).rotation+=.01; 105 | Pyramids.get(0).updatePoly(); 106 | 107 | //Set drawing order so closest polygons gets drawn last 108 | setOrder(); 109 | 110 | //Set the polygon that the mouse is currently over 111 | setPolygonOver(); 112 | 113 | //draw polygons in the Order that is set by the 'setOrder' function 114 | for(int i = 0; i < NewOrder.length; i++) 115 | DPolygons.get(NewOrder[i]).DrawablePolygon.drawPolygon(g); 116 | 117 | //draw the cross in the center of the screen 118 | drawMouseAim(g); 119 | 120 | //FPS display 121 | g.drawString("FPS: " + (int)drawFPS + " (Benchmark)", 40, 40); 122 | 123 | // repaintTime = System.currentTimeMillis() - repaintTime; 124 | // System.out.println(repaintTime); 125 | SleepAndRefresh(); 126 | } 127 | 128 | void setOrder() 129 | { 130 | double[] k = new double[DPolygons.size()]; 131 | NewOrder = new int[DPolygons.size()]; 132 | 133 | for(int i=0; i= 15) 176 | { 177 | drawFPS = Checks/((System.currentTimeMillis() - LastFPSCheck)/1000.0); 178 | LastFPSCheck = System.currentTimeMillis(); 179 | Checks = 0; 180 | } 181 | 182 | if(timeSLU < 1000.0/MaxFPS) 183 | { 184 | try { 185 | Thread.sleep((long) (1000.0/MaxFPS - timeSLU)); 186 | } catch (InterruptedException e) { 187 | e.printStackTrace(); 188 | } 189 | } 190 | 191 | LastRefresh = System.currentTimeMillis(); 192 | 193 | repaint(); 194 | } 195 | 196 | void ControlSunAndLight() 197 | { 198 | SunPos += 0.005; 199 | double mapSize = GenerateTerrain.mapSize * GenerateTerrain.Size; 200 | LightDir[0] = mapSize/2 - (mapSize/2 + Math.cos(SunPos) * mapSize * 10); 201 | LightDir[1] = mapSize/2 - (mapSize/2 + Math.sin(SunPos) * mapSize * 10); 202 | LightDir[2] = -200; 203 | } 204 | 205 | void CameraMovement() 206 | { 207 | Vector ViewVector = new Vector(ViewTo[0] - ViewFrom[0], ViewTo[1] - ViewFrom[1], ViewTo[2] - ViewFrom[2]); 208 | double xMove = 0, yMove = 0, zMove = 0; 209 | Vector VerticalVector = new Vector (0, 0, 1); 210 | Vector SideViewVector = ViewVector.CrossProduct(VerticalVector); 211 | 212 | if(Keys[0]) 213 | { 214 | xMove += ViewVector.x ; 215 | yMove += ViewVector.y ; 216 | zMove += ViewVector.z ; 217 | } 218 | 219 | if(Keys[2]) 220 | { 221 | xMove -= ViewVector.x ; 222 | yMove -= ViewVector.y ; 223 | zMove -= ViewVector.z ; 224 | } 225 | 226 | if(Keys[1]) 227 | { 228 | xMove += SideViewVector.x ; 229 | yMove += SideViewVector.y ; 230 | zMove += SideViewVector.z ; 231 | } 232 | 233 | if(Keys[3]) 234 | { 235 | xMove -= SideViewVector.x ; 236 | yMove -= SideViewVector.y ; 237 | zMove -= SideViewVector.z ; 238 | } 239 | 240 | Vector MoveVector = new Vector(xMove, yMove, zMove); 241 | MoveTo(ViewFrom[0] + MoveVector.x * MovementSpeed, ViewFrom[1] + MoveVector.y * MovementSpeed, ViewFrom[2] + MoveVector.z * MovementSpeed); 242 | } 243 | 244 | void MoveTo(double x, double y, double z) 245 | { 246 | ViewFrom[0] = x; 247 | ViewFrom[1] = y; 248 | ViewFrom[2] = z; 249 | updateView(); 250 | } 251 | 252 | void setPolygonOver() 253 | { 254 | PolygonOver = null; 255 | for(int i = NewOrder.length-1; i >= 0; i--) 256 | if(DPolygons.get(NewOrder[i]).DrawablePolygon.MouseOver() && DPolygons.get(NewOrder[i]).draw 257 | && DPolygons.get(NewOrder[i]).DrawablePolygon.visible) 258 | { 259 | PolygonOver = DPolygons.get(NewOrder[i]).DrawablePolygon; 260 | break; 261 | } 262 | } 263 | 264 | void MouseMovement(double NewMouseX, double NewMouseY) 265 | { 266 | double difX = (NewMouseX - DDDTutorial.ScreenSize.getWidth()/2); 267 | double difY = (NewMouseY - DDDTutorial.ScreenSize.getHeight()/2); 268 | difY *= 6 - Math.abs(VertLook) * 5; 269 | VertLook -= difY / VertRotSpeed; 270 | HorLook += difX / HorRotSpeed; 271 | 272 | if(VertLook>0.999) 273 | VertLook = 0.999; 274 | 275 | if(VertLook<-0.999) 276 | VertLook = -0.999; 277 | 278 | updateView(); 279 | } 280 | 281 | void updateView() 282 | { 283 | double r = Math.sqrt(1 - (VertLook * VertLook)); 284 | ViewTo[0] = ViewFrom[0] + r * Math.cos(HorLook); 285 | ViewTo[1] = ViewFrom[1] + r * Math.sin(HorLook); 286 | ViewTo[2] = ViewFrom[2] + VertLook; 287 | } 288 | 289 | void CenterMouse() 290 | { 291 | try { 292 | r = new Robot(); 293 | r.mouseMove((int)DDDTutorial.ScreenSize.getWidth()/2, (int)DDDTutorial.ScreenSize.getHeight()/2); 294 | } catch (AWTException e) { 295 | e.printStackTrace(); 296 | } 297 | } 298 | 299 | public void keyPressed(KeyEvent e) { 300 | if(e.getKeyCode() == KeyEvent.VK_W) 301 | Keys[0] = true; 302 | if(e.getKeyCode() == KeyEvent.VK_A) 303 | Keys[1] = true; 304 | if(e.getKeyCode() == KeyEvent.VK_S) 305 | Keys[2] = true; 306 | if(e.getKeyCode() == KeyEvent.VK_D) 307 | Keys[3] = true; 308 | if(e.getKeyCode() == KeyEvent.VK_O) 309 | OutLines = !OutLines; 310 | if(e.getKeyCode() == KeyEvent.VK_ESCAPE) 311 | System.exit(0); 312 | } 313 | 314 | public void keyReleased(KeyEvent e) { 315 | if(e.getKeyCode() == KeyEvent.VK_W) 316 | Keys[0] = false; 317 | if(e.getKeyCode() == KeyEvent.VK_A) 318 | Keys[1] = false; 319 | if(e.getKeyCode() == KeyEvent.VK_S) 320 | Keys[2] = false; 321 | if(e.getKeyCode() == KeyEvent.VK_D) 322 | Keys[3] = false; 323 | } 324 | 325 | public void keyTyped(KeyEvent e) { 326 | } 327 | 328 | public void mouseDragged(MouseEvent arg0) { 329 | MouseMovement(arg0.getX(), arg0.getY()); 330 | MouseX = arg0.getX(); 331 | MouseY = arg0.getY(); 332 | CenterMouse(); 333 | } 334 | 335 | public void mouseMoved(MouseEvent arg0) { 336 | MouseMovement(arg0.getX(), arg0.getY()); 337 | MouseX = arg0.getX(); 338 | MouseY = arg0.getY(); 339 | CenterMouse(); 340 | } 341 | 342 | public void mouseClicked(MouseEvent arg0) { 343 | } 344 | 345 | public void mouseEntered(MouseEvent arg0) { 346 | } 347 | 348 | public void mouseExited(MouseEvent arg0) { 349 | } 350 | 351 | public void mousePressed(MouseEvent arg0) { 352 | if(arg0.getButton() == MouseEvent.BUTTON1) 353 | if(PolygonOver != null) 354 | PolygonOver.seeThrough = false; 355 | 356 | if(arg0.getButton() == MouseEvent.BUTTON3) 357 | if(PolygonOver != null) 358 | PolygonOver.seeThrough = true; 359 | } 360 | 361 | public void mouseReleased(MouseEvent arg0) { 362 | } 363 | 364 | public void mouseWheelMoved(MouseWheelEvent arg0) { 365 | if(arg0.getUnitsToScroll()>0) 366 | { 367 | if(zoom > MinZoom) 368 | zoom -= 25 * arg0.getUnitsToScroll(); 369 | } 370 | else 371 | { 372 | if(zoom < MaxZoom) 373 | zoom -= 25 * arg0.getUnitsToScroll(); 374 | } 375 | } 376 | } 377 | -------------------------------------------------------------------------------- /3D Engine - 13-06-15/Vector.java: -------------------------------------------------------------------------------- 1 | 2 | public class Vector { 3 | double x, y, z; 4 | public Vector(double x, double y, double z) 5 | { 6 | double Length = Math.sqrt(x*x + y*y + z*z); 7 | 8 | if(Length>0) 9 | { 10 | this.x = x/Length; 11 | this.y = y/Length; 12 | this.z = z/Length; 13 | } 14 | 15 | } 16 | 17 | Vector CrossProduct(Vector V) 18 | { 19 | Vector CrossVector = new Vector( 20 | y * V.z - z * V.y, 21 | z * V.x - x * V.z, 22 | x * V.y - y * V.x); 23 | return CrossVector; 24 | } 25 | } 26 | --------------------------------------------------------------------------------