Fixed git clone URL
[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 /**
22  * Camera that sees the raytraced scene.
23  * It is represented on the scene as a textured rectangle.
24  */
25 public class Camera extends TexturedRectangle {
26
27     public static final int SIZE = 100;
28     public static final int IMAGE_SIZE = 500;
29     private final CameraView cameraView;
30
31     public Camera(final Avatar avatar, final double zoom) {
32         super(new Transform(avatar.getLocation().clone()));
33         cameraView = new CameraView(avatar, zoom);
34
35         computeCameraCoordinates(avatar);
36
37         addWaitNotification(getTexture());
38     }
39
40     private void addWaitNotification(final Texture texture) {
41         // add hourglass icon
42         try {
43             final BufferedImage sprite = getSprite("eu/svjatoslav/sixth/e3d/examples/hourglass.png");
44             texture.graphics.drawImage(sprite, IMAGE_SIZE / 2,
45                     (IMAGE_SIZE / 2) - 30, null);
46         } catch (final Exception ignored) {
47         }
48
49         // add "Please wait..." message
50         texture.graphics.setColor(java.awt.Color.WHITE);
51         texture.graphics.setFont(new Font("Monospaced", Font.PLAIN, 10));
52         texture.graphics.drawString("Please wait...", (IMAGE_SIZE / 2) - 20,
53                 (IMAGE_SIZE / 2) + 30);
54     }
55
56     private void computeCameraCoordinates(final Avatar avatar) {
57         initialize(SIZE, SIZE, IMAGE_SIZE, IMAGE_SIZE, 3);
58
59         Point3D cameraCenter = new Point3D();
60
61         topLeft.setValues(cameraCenter.x, cameraCenter.y, cameraCenter.z + SIZE);
62         topRight.clone(topLeft);
63         bottomLeft.clone(topLeft);
64         bottomRight.clone(topLeft);
65
66         final float viewAngle = (float) .6;
67
68         topLeft.rotate(cameraCenter, -viewAngle, -viewAngle);
69         topRight.rotate(cameraCenter, viewAngle, -viewAngle);
70         bottomLeft.rotate(cameraCenter, -viewAngle, viewAngle);
71         bottomRight.rotate(cameraCenter, viewAngle, viewAngle);
72
73         topLeft.rotate(cameraCenter, -avatar.getAngleXZ(), -avatar.getAngleYZ());
74         topRight.rotate(cameraCenter, -avatar.getAngleXZ(), -avatar.getAngleYZ());
75         bottomLeft.rotate(cameraCenter, -avatar.getAngleXZ(), -avatar.getAngleYZ());
76         bottomRight.rotate(cameraCenter, -avatar.getAngleXZ(), -avatar.getAngleYZ());
77
78         final Color cameraColor = new Color(255, 255, 0, 255);
79         final LineAppearance appearance = new LineAppearance(2, cameraColor);
80
81         addShape(appearance.getLine(topLeft, topRight));
82         addShape(appearance.getLine(bottomLeft, bottomRight));
83         addShape(appearance.getLine(topLeft, bottomLeft));
84         addShape(appearance.getLine(topRight, bottomRight));
85
86     }
87
88     public CameraView getCameraView() {
89         return cameraView;
90     }
91
92     public BufferedImage getSprite(final String ref) throws IOException {
93         final URL url = this.getClass().getClassLoader().getResource(ref);
94         return ImageIO.read(url);
95     }
96
97 }