67 lines
1.8 KiB
Java
67 lines
1.8 KiB
Java
|
package renderSuite;
|
||
|
|
||
|
import renderSuite.Vec3;
|
||
|
import renderSuite.Ray;
|
||
|
|
||
|
import java.awt.image.BufferedImage;
|
||
|
import javax.imageio.ImageIO;
|
||
|
|
||
|
import java.io.File;
|
||
|
|
||
|
public class Camera {
|
||
|
private Vec3 p, f;
|
||
|
private double fov;
|
||
|
private int sizeX, sizeY;
|
||
|
|
||
|
public Camera(Vec3 position, Vec3 facing, double fov, int sizeX, int sizeY) {
|
||
|
this.p = position;
|
||
|
this.f = facing;
|
||
|
this.fov = fov;
|
||
|
this.sizeX = sizeX;
|
||
|
this.sizeY = sizeY;
|
||
|
}
|
||
|
|
||
|
public void main() {
|
||
|
|
||
|
}
|
||
|
|
||
|
public void renderImageTo(String filename, Surface[] surfs) {
|
||
|
BufferedImage bi = new BufferedImage(this.sizeX, this.sizeY, BufferedImage.TYPE_INT_RGB);
|
||
|
|
||
|
for (int x = 0; x < this.sizeX; x++) {
|
||
|
for (int y = 0; y < this.sizeY; y++) {
|
||
|
Vec3 result = probe(x, y);
|
||
|
int color = ( (int)result.x ) << 16 | ( (int)result.y ) << 8 | ( (int)result.z );
|
||
|
bi.setRGB(x, y, color);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
File f = new File(filename + ".png");
|
||
|
ImageIO.write(bi, "PNG", f);
|
||
|
}
|
||
|
|
||
|
public Vec3 probe(int x, int y) {
|
||
|
Vec3 result = new Vec3(0, 0, 0);
|
||
|
|
||
|
// creating ray to use as a probing object
|
||
|
Ray ray = new Ray(this.p.copy(), this.f.copy().get_norm());
|
||
|
|
||
|
// adjust ray facing
|
||
|
double offsetX = ((x/this.sizeX) - 0.5) * 2;
|
||
|
ray.rotate('y', offsetX * this.fov);
|
||
|
|
||
|
double offsetY = ((y/this.sizeY) - 0.5) * 2;
|
||
|
ray.rotate('x', offsetY * this.fov);
|
||
|
|
||
|
// check collisions with every object on scene
|
||
|
for (int i = 0; i < surfs.length; i++) {
|
||
|
if (surfs[i].intersect(ray)) {
|
||
|
return surfs[i].color;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// if no collision detected, return black pixel
|
||
|
return new Vec3(0, 0, 0);
|
||
|
}
|
||
|
}
|