2 * Sixth 3D engine. Author: Svjatoslav Agejenko.
3 * This project is released under Creative Commons Zero (CC0) license.
5 package eu.svjatoslav.sixth.e3d.renderer.octree.raytracer;
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;
15 import javax.imageio.ImageIO;
17 import java.awt.image.BufferedImage;
18 import java.io.IOException;
21 public class Camera extends TexturedRectangle {
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;
28 public Camera(final Avatar avatar, final double zoom) {
29 super(new Transform(avatar.getLocation().clone()));
30 cameraView = new CameraView(avatar, zoom);
32 computeCameraCoordinates(avatar);
34 addWaitNotification(getTexture());
37 private void addWaitNotification(final Texture texture) {
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) {
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);
53 private void computeCameraCoordinates(final Avatar avatar) {
54 initialize(CAMERA_SIZE, CAMERA_SIZE, IMAGE_SIZE, IMAGE_SIZE, 3);
56 camCenter = new Point3D();
58 topLeft.setValues(camCenter.x, camCenter.y, camCenter.z + CAMERA_SIZE);
60 topRight.clone(topLeft);
61 bottomLeft.clone(topLeft);
62 bottomRight.clone(topLeft);
64 final float viewAngle = (float) .6;
66 topLeft.rotate(camCenter, -viewAngle, -viewAngle);
67 topRight.rotate(camCenter, viewAngle, -viewAngle);
68 bottomLeft.rotate(camCenter, -viewAngle, viewAngle);
69 bottomRight.rotate(camCenter, viewAngle, viewAngle);
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());
76 final Color cameraColor = new Color(255, 255, 0, 255);
77 final LineAppearance appearance = new LineAppearance(2, cameraColor);
79 // addShape(appearance.getLine(camCenter, upLeft));
80 // addShape(appearance.getLine(camCenter, upRight));
81 // addShape(appearance.getLine(camCenter, downLeft));
82 // addShape(appearance.getLine(camCenter, downRight));
84 addShape(appearance.getLine(topLeft, topRight));
85 addShape(appearance.getLine(bottomLeft, bottomRight));
86 addShape(appearance.getLine(topLeft, bottomLeft));
87 addShape(appearance.getLine(topRight, bottomRight));
91 public CameraView getCameraView() {
95 public BufferedImage getSprite(final String ref) throws IOException {
96 final URL url = this.getClass().getClassLoader().getResource(ref);
97 return ImageIO.read(url);