Fixed git clone URL
[sixth-3d.git] / raytracer / Camera.java
1 /*
2  * Sixth 3D engine. Copyright ©2012-2016, 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.geometry.Transform;
14 import eu.svjatoslav.sixth.e3d.gui.Avatar;
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 = Point3D.ZERO.clone();
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
79                 .rotate(camCenter, -avatar.getAngleXZ(), -avatar.getAngleYZ());
80         bottomRight.rotate(camCenter, -avatar.getAngleXZ(),
81                 -avatar.getAngleYZ());
82
83         final Color cameraColor = new Color(255, 255, 0, 255);
84         final LineAppearance appearance = new LineAppearance(2, cameraColor);
85
86         // addShape(appearance.getLine(camCenter, upLeft));
87         // addShape(appearance.getLine(camCenter, upRight));
88         // addShape(appearance.getLine(camCenter, downLeft));
89         // addShape(appearance.getLine(camCenter, downRight));
90
91         addShape(appearance.getLine(topLeft, topRight));
92         addShape(appearance.getLine(bottomLeft, bottomRight));
93         addShape(appearance.getLine(topLeft, bottomLeft));
94         addShape(appearance.getLine(topRight, bottomRight));
95
96     }
97
98     public CameraView getCameraView() {
99         return cameraView;
100     }
101
102     public BufferedImage getSprite(final String ref) throws IOException {
103         final URL url = this.getClass().getClassLoader().getResource(ref);
104         return ImageIO.read(url);
105     }
106
107 }