Skip to content

Commit

Permalink
Fix tabulation
Browse files Browse the repository at this point in the history
  • Loading branch information
VxDxK committed Feb 19, 2023
1 parent e09213c commit 76cb3c5
Show file tree
Hide file tree
Showing 60 changed files with 254 additions and 237 deletions.
7 changes: 3 additions & 4 deletions src/main/java/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@
import image.ImageWriter;
import raytracer.RayTracer;
import raytracer.RayTracerConfig;
import raytracer.RayTracerImpl;
import raytracer.RayTracerLights;
import raytracer.scene.BallsScene;
import raytracer.scene.LightsScene;
import util.Dimension;

import java.nio.file.Paths;
import java.util.concurrent.TimeUnit;

public class App {
public static void main(String[] args) throws Exception{
public static void main(String[] args) throws Exception {
ImageWriter writer = new ImageWriter(Math::sqrt);
// RayTracer rayTracer = new RayTracerImpl(new RayTracerConfig(new Dimension(400, 16d/9d), 50, 50), new BallsScene());
RayTracer rayTracer = new RayTracerLights(new RayTracerConfig(new Dimension(400, 16d/9d), 50, 50), new LightsScene());
RayTracer rayTracer = new RayTracerLights(new RayTracerConfig(new Dimension(400, 16d / 9d), 50, 50), new LightsScene());
long st = System.currentTimeMillis();
Image image = rayTracer.render();
System.out.printf("Processing time: %d seconds\n", TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis() - st));
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/Convertor.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public static void main(String[] args) {
final String destFilename = "image.png";

// try to get hold of the file.
try(BufferedReader reader = new BufferedReader(new FileReader(sourceFilename))) {
try (BufferedReader reader = new BufferedReader(new FileReader(sourceFilename))) {
// validate file header
final String FileHeader = "P3";
String line = reader.readLine();
Expand Down
18 changes: 8 additions & 10 deletions src/main/java/Perlin.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,29 @@
import math.Points;
import util.PerlinNoise;

import javax.imageio.ImageIO;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.function.Function;

import static util.Util.clamp;

public class Perlin {
public static void main(String[] args) throws Exception{
public static void main(String[] args) throws Exception {
try (OutputStream stream = Files.newOutputStream(Paths.get("image.ppm"));
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(stream);
BufferedWriter writer = new BufferedWriter(outputStreamWriter)){
BufferedWriter writer = new BufferedWriter(outputStreamWriter)) {
long start = System.currentTimeMillis();
new Perlin(writer);
System.out.println("Processing time: " + (((double)(System.currentTimeMillis() - start))/1000));
System.out.println("Processing time: " + (((double) (System.currentTimeMillis() - start)) / 1000));
}
Convertor.main(args);
}

Perlin(BufferedWriter writer) throws Exception{
Perlin(BufferedWriter writer) throws Exception {
int width = 100;
int height = 100;

Expand All @@ -37,9 +35,9 @@ public static void main(String[] args) throws Exception{
PerlinNoise b = new PerlinNoise();

writer.write("P3\n" + width + " " + height + "\n255\n");
for(int j = 0; j < height; j++){
for (int j = 0; j < height; j++) {
System.out.print("Lines remaining " + (height - j) + "\r");
for(int i = 0; i < width; i++){
for (int i = 0; i < width; i++) {
Color red = new Color(1, 0, 0).scale(15 * r.turbulence(Points.scale(new Point(i, 0, j), 0.1), 1));
Color green = new Color(0, 1, 0).scale(10 * g.turbulence(Points.scale(new Point(i, 0, j), 0.1), 1));
Color blue = new Color(0, 0, 1).scale(10 * b.turbulence(Points.scale(new Point(i, 0, j), 0.1), 1));
Expand All @@ -49,7 +47,7 @@ public static void main(String[] args) throws Exception{
Color mixed = new Color(start.getRed() + red.getRed() + green.getRed() + blue.getRed(),
start.getGreen() + red.getGreen() + green.getGreen() + blue.getGreen(),
start.getBlue() + red.getBlue() + green.getBlue() + blue.getBlue());
mixed = mixed.scale((1d/4d));
mixed = mixed.scale((1d / 4d));
double scaling = perlinNoise.noise(Points.scale(new Point(i, 0, j), 0.1));
Color pixelColor = new Color(1, 1, 1).scale(scaling);
writeColor(writer, mixed, x -> x);
Expand All @@ -62,7 +60,7 @@ private void writeColor(BufferedWriter writer, Color color, Function<Double, Dou
double r = gammaCorrectionFunction.apply(color.getRed());
double g = gammaCorrectionFunction.apply(color.getGreen());
double b = gammaCorrectionFunction.apply(color.getBlue());
writer.write((int)(256 * clamp(r, 0d, 0.999)) + " " + (int)(256 * clamp(g, 0d, 0.999)) + " " + (int)(256 * clamp(b, 0d, 0.999)) + '\n');
writer.write((int) (256 * clamp(r, 0d, 0.999)) + " " + (int) (256 * clamp(g, 0d, 0.999)) + " " + (int) (256 * clamp(b, 0d, 0.999)) + '\n');
}

}
9 changes: 5 additions & 4 deletions src/main/java/camera/BlurCamera.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ public class BlurCamera implements Camera {
private final Vector vertical;
private final double lensRadius;
private final Vector w, u, v;
public BlurCamera(Point lookFrom, Point lookAt, Vector worldNormal, double verticalFov, double aspectRatio, double aperture, double focusDist){

public BlurCamera(Point lookFrom, Point lookAt, Vector worldNormal, double verticalFov, double aspectRatio, double aperture, double focusDist) {
double theta = Math.toRadians(verticalFov);
double h = Math.tan(theta/2);
double h = Math.tan(theta / 2);
double viewportHeight = 2.0 * h;
double viewportWidth = aspectRatio * viewportHeight;

Expand All @@ -29,11 +30,11 @@ public BlurCamera(Point lookFrom, Point lookAt, Vector worldNormal, double verti
this.vertical = v.multiply(viewportHeight).multiply(focusDist);
this.lowerLeftCorner = origin.move(vertical.negate().divide(2)).move(horizontal.negate().divide(2)).move(w.negate().multiply(focusDist));

lensRadius = aperture/2;
lensRadius = aperture / 2;
}

@Override
public Ray getRay(double s, double t){
public Ray getRay(double s, double t) {
Vector rd = Vectors.randomInUnitSphere().multiply(lensRadius);
Vector offset = u.multiply(rd.getX()).add(v.multiply(rd.getY()));

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/camera/ClearCamera.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
import math.Vectors;


public class ClearCamera implements Camera{
public class ClearCamera implements Camera {
private final Point origin;
private final Point lowerLeftCorner;
private final Vector horizontal;
private final Vector vertical;

public ClearCamera(Point lookFrom, Point lookAt, Vector worldNormal, double verticalFov, double aspectRatio) {
double theta = Math.toRadians(verticalFov);
double h = Math.tan(theta/2);
double h = Math.tan(theta / 2);
double viewportHeight = 2.0 * h;
double viewportWidth = aspectRatio * viewportHeight;

Expand Down Expand Up @@ -51,5 +51,5 @@ public Vector getHorizontal() {
public Vector getVertical() {
return vertical;
}

}
4 changes: 2 additions & 2 deletions src/main/java/camera/MotionBlurCamera.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

import math.Ray;

public class MotionBlurCamera implements Camera{
public class MotionBlurCamera implements Camera {
private final Camera camera;
private final double shutterOpeningMoment;
private final double shutterClosingMoment;

public MotionBlurCamera(Camera camera, double shutterOpeningMoment, double shutterClosingMoment) {
if(shutterClosingMoment < shutterOpeningMoment){
if (shutterClosingMoment < shutterOpeningMoment) {
throw new IllegalArgumentException("shutterClosingMoment can`t be less then shutterOpeningMoment");
}
this.camera = camera;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/image/ArrayImage.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import java.util.*;

public class ArrayImage implements Image{
public class ArrayImage implements Image {
private final int xLen;
private final int yLen;
private final Color[][] colors;
Expand All @@ -29,7 +29,7 @@ public Pair<Integer, Integer> getSize() {

@Override
public Color getColor(int x, int y) {
if(x >= xLen || y >= yLen){
if (x >= xLen || y >= yLen) {
throw new IllegalArgumentException("Coordinate out of image size");
}
return colors[x][y];
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/image/ColorImage.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import java.util.Iterator;
import java.util.List;

public class ColorImage implements Image{
public class ColorImage implements Image {
private final int x;
private final int y;
private final Color color;
Expand Down Expand Up @@ -35,7 +35,7 @@ public Color getColor(int x, int y) {
@Override
public Iterator<Pixel> iterator() {
List<Pixel> pixels = new ArrayList<>();
for (int i = 0; i < x; i++){
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
pixels.add(new Pixel(i, j, color));
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/image/Image.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import math.Color;
import util.Pair;

public interface Image extends Iterable<Pixel>{
public interface Image extends Iterable<Pixel> {
Pair<Integer, Integer> getSize();

Color getColor(int x, int y);
}
4 changes: 2 additions & 2 deletions src/main/java/image/ImageWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public ImageWriter(Function<Double, Double> gammaCorrection) {
}

public void writeToBuffer(BufferedImage imageBuf, Image image) {
for (Pixel px: image) {
for (Pixel px : image) {
Color now = px.getColor();
Color corrected = new Color(gammaCorrection.apply(now.getRed()), gammaCorrection.apply(now.getGreen()), gammaCorrection.apply(now.getBlue()));
imageBuf.setRGB(px.getX(), px.getY(), corrected.toRGB());
Expand All @@ -41,7 +41,7 @@ public void writeToFile(Path file, BufferedImage bufferedImage) throws IOExcepti
writeToFile(file, bufferedImage, OutputFormat.PNG);
}

public void writeToFile(Path file, Image image) throws IOException{
public void writeToFile(Path file, Image image) throws IOException {
writeToFile(file, write(image));
}

Expand Down
8 changes: 4 additions & 4 deletions src/main/java/image/ListImage.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import java.util.LinkedList;
import java.util.List;

public class ListImage implements Image{
public class ListImage implements Image {
private final int xLen;
private final int yLen;
private final List<Pixel> list = new LinkedList<>();
Expand All @@ -30,11 +30,11 @@ public Pair<Integer, Integer> getSize() {

@Override
public Color getColor(int x, int y) {
if(x >= xLen || y >= yLen){
if (x >= xLen || y >= yLen) {
throw new IllegalArgumentException("Coordinate out of image size");
}
for (Pixel px: list) {
if(px.getX() == x && px.getY() == y){
for (Pixel px : list) {
if (px.getX() == x && px.getY() == y) {
return px.getColor();
}
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/image/OutputFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public enum OutputFormat {
BMP("bmp");

public final String type;

OutputFormat(String type) {
this.type = type;
}
Expand Down
19 changes: 10 additions & 9 deletions src/main/java/material/Dielectric.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
import math.Color;
import math.HitRecord;

public class Dielectric implements Material{
public class Dielectric implements Material {
private final double refractionIndex;
private final Color albedo;

public Dielectric(double refractionIndex) {
this(refractionIndex, new Color(1, 1, 1));
}
Expand All @@ -22,29 +23,29 @@ public Dielectric(double refractionIndex, Color albedo) {
@Override
public boolean scatter(Ray rayIn, HitRecord record, Color attenuation, Ray scattered) {
attenuation.set(albedo);
double refractionRatio = record.isFrontFace() ? (1.0/refractionIndex) : refractionIndex;
double refractionRatio = record.isFrontFace() ? (1.0 / refractionIndex) : refractionIndex;
Vector unitDirection = rayIn.getDirection().unit();

double cosTheta = Math.min(1d, Vectors.dot(unitDirection.negate(), record.getNormal()));
double sinTheta = Math.sqrt(1d - cosTheta*cosTheta);
double sinTheta = Math.sqrt(1d - cosTheta * cosTheta);
boolean cannotRefract = refractionRatio * sinTheta > 1.0;

Vector direction;
if(cannotRefract || reflectance(cosTheta, refractionRatio) > Math.random()){
if (cannotRefract || reflectance(cosTheta, refractionRatio) > Math.random()) {
direction = Vectors.reflect(unitDirection, record.getNormal());
}else{
} else {
direction = Vectors.refract(unitDirection, record.getNormal(), refractionRatio);
}
scattered.setOrigin(record.getPoint()).setDirection(direction).setTimeMoment(rayIn.getTimeMoment());
return true;
}

/**
*@see <a href="https://en.wikipedia.org/wiki/Schlick%27s_approximation">Schlick approximation</a>
* @see <a href="https://en.wikipedia.org/wiki/Schlick%27s_approximation">Schlick approximation</a>
*/
private double reflectance(double cos, double refRat){
double R0 = Math.pow((1d - refRat)/(1d + refRat), 2);
return R0 + (1 - R0)*Math.pow((1 - cos), 5);
private double reflectance(double cos, double refRat) {
double R0 = Math.pow((1d - refRat) / (1d + refRat), 2);
return R0 + (1 - R0) * Math.pow((1 - cos), 5);
}

}
2 changes: 1 addition & 1 deletion src/main/java/material/DiffuseLight.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import texture.SolidColorTexture;
import texture.Texture;

public class DiffuseLight implements Material{
public class DiffuseLight implements Material {
private final Texture texture;

public DiffuseLight(Color color) {
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/material/Lambertian.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
/**
* @see <a href="https://en.wikipedia.org/wiki/Lambertian_reflectance">Lambertian reflection</a>
*/
public class Lambertian implements Material{
public class Lambertian implements Material {
private final Texture albedo;

public Lambertian(Color albedo) {
this.albedo = new SolidColorTexture(albedo);
}
Expand All @@ -24,7 +25,7 @@ public Lambertian(Texture texture) {
@Override
public boolean scatter(Ray rayIn, HitRecord record, Color attenuation, Ray scattered) {
Vector scatteredDirection = record.getNormal().add(Vectors.randomUnitVector());
if(scatteredDirection.nearZero()){
if (scatteredDirection.nearZero()) {
scatteredDirection = record.getNormal();
}
scattered.setDirection(scatteredDirection).setOrigin(record.getPoint()).setTimeMoment(rayIn.getTimeMoment());
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/material/Material.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
public interface Material {
boolean scatter(Ray rayIn, HitRecord record, Color attenuation, Ray scattered);

default Color emitted(double u, double v, Point p){
default Color emitted(double u, double v, Point p) {
return new Color(0, 0, 0);
}
}
13 changes: 5 additions & 8 deletions src/main/java/material/Metal.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import texture.Texture;
import math.HitRecord;

public class Metal implements Material{
public class Metal implements Material {
private final Texture albedo;
//Для создания нечеткого отражения
private final double fuzz;
Expand All @@ -24,26 +24,23 @@ public Metal(Color albedo, double fuzz) {
this.fuzz = fuzz;
}

public Metal(Texture texture){
public Metal(Texture texture) {
this.albedo = texture;
this.fuzz = 0d;
}

public Metal(Texture texture, double fuzz){
public Metal(Texture texture, double fuzz) {
this.albedo = texture;
this.fuzz = fuzz;
}

@Override
public boolean scatter(Ray rayIn, HitRecord record, Color attenuation, Ray scattered) {
Vector reflected = Vectors.reflect(rayIn.getDirection().unit(), record.getNormal());
scattered.setOrigin(record.getPoint())
.setDirection(reflected.add(Vectors.randomInUnitSphere().multiply(fuzz)))
.setTimeMoment(rayIn.getTimeMoment());
scattered.setOrigin(record.getPoint()).setDirection(reflected.add(Vectors.randomInUnitSphere().multiply(fuzz))).setTimeMoment(rayIn.getTimeMoment());

boolean scatter = Vectors.dot(scattered.getDirection(), record.getNormal()) > 0;
if (scatter)
attenuation.set(albedo.value(record.getU(), record.getV(), record.getPoint()));
if (scatter) attenuation.set(albedo.value(record.getU(), record.getV(), record.getPoint()));
return scatter;
}
}
Loading

0 comments on commit 76cb3c5

Please sign in to comment.