2 * Sixth 3D engine. Author: Svjatoslav Agejenko.
3 * This project is released under Creative Commons Zero (CC0) license.
8 package eu.svjatoslav.sixth.e3d.renderer.octree.raytracer;
10 import eu.svjatoslav.sixth.e3d.geometry.Point3D;
11 import eu.svjatoslav.sixth.e3d.gui.Avatar;
12 import eu.svjatoslav.sixth.e3d.math.Transform;
13 import eu.svjatoslav.sixth.e3d.renderer.raster.Color;
14 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.basic.line.LineAppearance;
15 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.composite.TexturedRectangle;
16 import eu.svjatoslav.sixth.e3d.renderer.raster.texture.Texture;
18 import javax.imageio.ImageIO;
20 import java.awt.image.BufferedImage;
21 import java.io.IOException;
24 public class Camera extends TexturedRectangle {
26 public static final int CAMERA_SIZE = 100;
27 public static final int IMAGE_SIZE = 500;
28 private final CameraView cameraView;
29 private Point3D camCenter;
31 public Camera(final Avatar avatar, final double zoom) {
32 super(new Transform(avatar.getLocation().clone()));
33 cameraView = new CameraView(avatar, zoom);
35 computeCameraCoordinates(avatar);
37 addWaitNotification(getTexture());
40 private void addWaitNotification(final Texture texture) {
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) {
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);
56 private void computeCameraCoordinates(final Avatar avatar) {
57 initialize(CAMERA_SIZE, CAMERA_SIZE, IMAGE_SIZE, IMAGE_SIZE, 3);
59 camCenter = new Point3D();
61 topLeft.setValues(camCenter.x, camCenter.y, camCenter.z + CAMERA_SIZE);
63 topRight.clone(topLeft);
64 bottomLeft.clone(topLeft);
65 bottomRight.clone(topLeft);
67 final float viewAngle = (float) .6;
69 topLeft.rotate(camCenter, -viewAngle, -viewAngle);
70 topRight.rotate(camCenter, viewAngle, -viewAngle);
71 bottomLeft.rotate(camCenter, -viewAngle, viewAngle);
72 bottomRight.rotate(camCenter, viewAngle, viewAngle);
74 topLeft.rotate(camCenter, -avatar.getAngleXZ(), -avatar.getAngleYZ());
75 topRight.rotate(camCenter, -avatar.getAngleXZ(), -avatar.getAngleYZ());
76 bottomLeft.rotate(camCenter, -avatar.getAngleXZ(), -avatar.getAngleYZ());
77 bottomRight.rotate(camCenter, -avatar.getAngleXZ(), -avatar.getAngleYZ());
79 final Color cameraColor = new Color(255, 255, 0, 255);
80 final LineAppearance appearance = new LineAppearance(2, cameraColor);
82 // addShape(appearance.getLine(camCenter, upLeft));
83 // addShape(appearance.getLine(camCenter, upRight));
84 // addShape(appearance.getLine(camCenter, downLeft));
85 // addShape(appearance.getLine(camCenter, downRight));
87 addShape(appearance.getLine(topLeft, topRight));
88 addShape(appearance.getLine(bottomLeft, bottomRight));
89 addShape(appearance.getLine(topLeft, bottomLeft));
90 addShape(appearance.getLine(topRight, bottomRight));
94 public CameraView getCameraView() {
98 public BufferedImage getSprite(final String ref) throws IOException {
99 final URL url = this.getClass().getClassLoader().getResource(ref);
100 return ImageIO.read(url);