Formatting update
[sixth-3d.git] / src / main / java / eu / svjatoslav / sixth / e3d / renderer / octree / raytracer / Camera.java
1 /*
2  * Sixth 3D engine. Author: Svjatoslav Agejenko. 
3  * This project is released under Creative Commons Zero (CC0) license.
4  */
5 package eu.svjatoslav.sixth.e3d.renderer.octree.raytracer;
6
7 import eu.svjatoslav.sixth.e3d.geometry.Point3D;
8 import eu.svjatoslav.sixth.e3d.gui.Avatar;
9 import eu.svjatoslav.sixth.e3d.math.Transform;
10 import eu.svjatoslav.sixth.e3d.renderer.raster.Color;
11 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.basic.line.LineAppearance;
12 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.composite.TexturedRectangle;
13 import eu.svjatoslav.sixth.e3d.renderer.raster.texture.Texture;
14
15 import javax.imageio.ImageIO;
16 import java.awt.*;
17 import java.awt.image.BufferedImage;
18 import java.io.IOException;
19 import java.net.URL;
20
21 public class Camera extends TexturedRectangle {
22
23     public static final int CAMERA_SIZE = 100;
24     public static final int IMAGE_SIZE = 500;
25     private final CameraView cameraView;
26     private Point3D camCenter;
27
28     public Camera(final Avatar avatar, final double zoom) {
29         super(new Transform(avatar.getLocation().clone()));
30         cameraView = new CameraView(avatar, zoom);
31
32         computeCameraCoordinates(avatar);
33
34         addWaitNotification(getTexture());
35     }
36
37     private void addWaitNotification(final Texture texture) {
38         // add hourglass icon
39         try {
40             final BufferedImage sprite = getSprite("eu/svjatoslav/sixth/e3d/examples/hourglass.png");
41             texture.graphics.drawImage(sprite, IMAGE_SIZE / 2,
42                     (IMAGE_SIZE / 2) - 30, null);
43         } catch (final Exception ignored) {
44         }
45
46         // add "Please wait..." message
47         texture.graphics.setColor(java.awt.Color.WHITE);
48         texture.graphics.setFont(new Font("Monospaced", Font.PLAIN, 10));
49         texture.graphics.drawString("Please wait...", (IMAGE_SIZE / 2) - 20,
50                 (IMAGE_SIZE / 2) + 30);
51     }
52
53     private void computeCameraCoordinates(final Avatar avatar) {
54         initialize(CAMERA_SIZE, CAMERA_SIZE, IMAGE_SIZE, IMAGE_SIZE, 3);
55
56         camCenter = new Point3D();
57
58         topLeft.setValues(camCenter.x, camCenter.y, camCenter.z + CAMERA_SIZE);
59
60         topRight.clone(topLeft);
61         bottomLeft.clone(topLeft);
62         bottomRight.clone(topLeft);
63
64         final float viewAngle = (float) .6;
65
66         topLeft.rotate(camCenter, -viewAngle, -viewAngle);
67         topRight.rotate(camCenter, viewAngle, -viewAngle);
68         bottomLeft.rotate(camCenter, -viewAngle, viewAngle);
69         bottomRight.rotate(camCenter, viewAngle, viewAngle);
70
71         topLeft.rotate(camCenter, -avatar.getAngleXZ(), -avatar.getAngleYZ());
72         topRight.rotate(camCenter, -avatar.getAngleXZ(), -avatar.getAngleYZ());
73         bottomLeft.rotate(camCenter, -avatar.getAngleXZ(), -avatar.getAngleYZ());
74         bottomRight.rotate(camCenter, -avatar.getAngleXZ(), -avatar.getAngleYZ());
75
76         final Color cameraColor = new Color(255, 255, 0, 255);
77         final LineAppearance appearance = new LineAppearance(2, cameraColor);
78
79         // addShape(appearance.getLine(camCenter, upLeft));
80         // addShape(appearance.getLine(camCenter, upRight));
81         // addShape(appearance.getLine(camCenter, downLeft));
82         // addShape(appearance.getLine(camCenter, downRight));
83
84         addShape(appearance.getLine(topLeft, topRight));
85         addShape(appearance.getLine(bottomLeft, bottomRight));
86         addShape(appearance.getLine(topLeft, bottomLeft));
87         addShape(appearance.getLine(topRight, bottomRight));
88
89     }
90
91     public CameraView getCameraView() {
92         return cameraView;
93     }
94
95     public BufferedImage getSprite(final String ref) throws IOException {
96         final URL url = this.getClass().getClassLoader().getResource(ref);
97         return ImageIO.read(url);
98     }
99
100 }