-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCamera.java
More file actions
112 lines (95 loc) · 3.29 KB
/
Camera.java
File metadata and controls
112 lines (95 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import javax.imageio.ImageIO;
import javax.swing.plaf.basic.BasicArrowButton;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
/**
* Class that represents the camera. Responsible for drawing scene
*
*
*/
public class Camera {
private Vertex position;
private Vector direction;
/**
* Constructor that takes in the direction and location of the camera and
* initializes the polygon.
*
* @param position
* The position of the camera.
* @param direction
* The vector showing the direction the camera is facing.
*/
public Camera(Vertex position, Vector direction) {
this.position = position;
this.direction = new Vector(direction.getX(), direction.getY(), direction.getZ());
}
/**
* Draws the given face to the given graphics 2d.
*
* @param g2d
* The graphics to draw on.
* @param objects
* - list of objects to draw in scene
*/
public void draw(Graphics2D g2d, ArrayList<Object3D> objects, Vertex lightSource) {
double height = 800;
double width = 1200;
Object3D closestObject = null;
/*
* SETUP FOR DRAWING RAYS THROUGH PIXEL CENTERS
*/
Vector t = new Vector(direction.getX(), direction.getY(), direction.getZ());
t.makeUnitVector();
Vector w = new Vector(0, 1, 0);
Vector b = VectorMath.crossProduct(w, t);
b.makeUnitVector();
Vector v = VectorMath.crossProduct(t, b);
double gx = Math.tan((Math.PI / 2) / 2);
double gy = gx * height / width;
Vector qx = VectorMath.multiply((2 * gx) / (width - 1), b);
Vector qy = VectorMath.multiply(-(2 * gy) / (height - 1), v); // seems like qy should be negative
Vector p1m = VectorMath.subtract(VectorMath.subtract(t, VectorMath.multiply(gx, b)),
VectorMath.multiply(gy, v));
/*
* SETUP FOR SPHERE TRACING
*/
Vector throughPixel;
Vertex point = new Vertex(position.getX(), position.getY(), position.getZ());
BufferedImage bf = new BufferedImage((int) width, (int) height, BufferedImage.TYPE_INT_RGB);
double counter = 0;
// iterating through rows
for (int i = 0; i < height; i++) {
// iterating through columns
for (int j = 0; j < width; j++) {
if(counter % (width*height/400) == 0) {
System.out.println(counter/(width*height)*100 +"/" + 100);
}
counter++;
// vector in the direction of the middle of pixel i,j from the camera
throughPixel = VectorMath.add(VectorMath.add(p1m, VectorMath.multiply(j, qx)),
VectorMath.multiply(i, qy));
throughPixel.makeUnitVector();
Vertex cameraPosition = new Vertex(position.getX(), position.getY(), position.getZ());
point = VectorMath.sphereTrace(cameraPosition, throughPixel, objects);
closestObject = VectorMath.closestObject(point, objects);
Color pixelColor = Color.BLACK;
if (closestObject.distanceFrom(point) == 0) {
pixelColor = closestObject.getColor(point, position, lightSource, objects);
}
bf.setRGB(j, i, pixelColor.getRGB());
}
}
g2d.drawImage(bf, 0, 0, (int) width, (int) height, new BasicArrowButton(0));
File outputfile = new File("outputImage.png");
try {
ImageIO.write(bf, "png", outputfile);
} catch (IOException e) {
System.out.println("failed to write image");
e.printStackTrace();
}
System.out.println("done");
}
}