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