Skip to content

Commit

Permalink
New scenes added
Browse files Browse the repository at this point in the history
  • Loading branch information
VxDxK committed Jan 20, 2023
1 parent d5bca5b commit 4c0eff2
Show file tree
Hide file tree
Showing 29 changed files with 644 additions and 168 deletions.
23 changes: 23 additions & 0 deletions src/main/java/App.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import image.Image;
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{
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());
long st = System.currentTimeMillis();
Image image = rayTracer.render();
System.out.printf("Processing time: %d seconds\n", TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis() - st));
writer.writeToFile(Paths.get("image.png"), image);
}
}
151 changes: 0 additions & 151 deletions src/main/java/Main.java

This file was deleted.

48 changes: 48 additions & 0 deletions src/main/java/image/ArrayImage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package image;

import math.Color;
import util.Pair;

import java.util.*;

public class ArrayImage implements Image{
private final int xLen;
private final int yLen;
private final Color[][] colors;

public ArrayImage(int xLen, int yLen) {
this.xLen = xLen;
this.yLen = yLen;
colors = new Color[xLen][yLen];
}

public ArrayImage(Color[][] colors) {
this.colors = colors;
this.xLen = colors.length;
this.yLen = colors[0].length;
}

@Override
public Pair<Integer, Integer> getSize() {
return new Pair<>(xLen, yLen);
}

@Override
public Color getColor(int x, int y) {
if(x >= xLen || y >= yLen){
throw new IllegalArgumentException("Coordinate out of image size");
}
return colors[x][y];
}

@Override
public Iterator<Pixel> iterator() {
List<Pixel> list = new ArrayList<>();
for (int i = 0; i < xLen; i++) {
for (int j = 0; j < yLen; j++) {
list.add(new Pixel(i, j, colors[i][j]));
}
}
return list.iterator();
}
}
45 changes: 45 additions & 0 deletions src/main/java/image/ColorImage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package image;

import math.Color;
import util.Pair;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ColorImage implements Image{
private final int x;
private final int y;
private final Color color;

public ColorImage(int x, int y, Color color) {
this.x = x;
this.y = y;
this.color = color;
}

public ColorImage(Color color) {
this(1, 1, color);
}

@Override
public Pair<Integer, Integer> getSize() {
return new Pair<>(x, y);
}

@Override
public Color getColor(int x, int y) {
return color;
}

@Override
public Iterator<Pixel> iterator() {
List<Pixel> pixels = new ArrayList<>();
for (int i = 0; i < x; i++){
for (int j = 0; j < y; j++) {
pixels.add(new Pixel(i, j, color));
}
}
return pixels.iterator();
}
}
2 changes: 2 additions & 0 deletions src/main/java/image/Image.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package image;

import math.Color;
import util.Pair;

public interface Image extends Iterable<Pixel>{
Pair<Integer, Integer> getSize();
Color getColor(int x, int y);
}
5 changes: 5 additions & 0 deletions src/main/java/image/ImageWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,9 @@ public void writeToFile(Path file, BufferedImage bufferedImage, OutputFormat for
public void writeToFile(Path file, BufferedImage bufferedImage) throws IOException {
writeToFile(file, bufferedImage, OutputFormat.PNG);
}

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

}
14 changes: 14 additions & 0 deletions src/main/java/image/ListImage.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package image;

import math.Color;
import util.Pair;

import java.util.ArrayList;
Expand All @@ -26,4 +27,17 @@ public Iterator<Pixel> iterator() {
public Pair<Integer, Integer> getSize() {
return new Pair<Integer, Integer>(xLen, yLen);
}

@Override
public Color getColor(int x, int y) {
if(x >= xLen || y >= yLen){
throw new IllegalArgumentException("Coordinate out of image size");
}
for (Pixel px: list) {
if(px.getX() == x && px.getY() == y){
return px.getColor();
}
}
throw new IllegalStateException("No color in this coordinate");
}
}
28 changes: 28 additions & 0 deletions src/main/java/material/DiffuseLight.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package material;

import math.*;
import texture.SolidColorTexture;
import texture.Texture;

public class DiffuseLight implements Material{
private final Texture texture;

public DiffuseLight(Color color) {
this.texture = new SolidColorTexture(color);
}

public DiffuseLight(Texture texture) {
this.texture = texture;
}

@Override
public boolean scatter(Ray rayIn, HitRecord record, Color attenuation, Ray scattered) {
scattered.setOrigin(new Point()).setDirection(new Vector());
return false;
}

@Override
public Color emitted(double u, double v, Point p) {
return texture.value(u, v, p);
}
}
5 changes: 5 additions & 0 deletions src/main/java/material/Material.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package material;

import math.Point;
import math.Ray;
import math.Color;
import math.HitRecord;

public interface Material {
boolean scatter(Ray rayIn, HitRecord record, Color attenuation, Ray scattered);

default Color emitted(double u, double v, Point p){
return new Color(0, 0, 0);
}
}
7 changes: 5 additions & 2 deletions src/main/java/material/Metal.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ public boolean scatter(Ray rayIn, HitRecord record, Color attenuation, Ray scatt
scattered.setOrigin(record.getPoint())
.setDirection(reflected.add(Vectors.randomInUnitSphere().multiply(fuzz)))
.setTimeMoment(rayIn.getTimeMoment());
attenuation.set(albedo.value(record.getU(), record.getV(), record.getPoint()));
return Vectors.dot(scattered.getDirection(), record.getNormal()) > 0;

boolean scatter = Vectors.dot(scattered.getDirection(), record.getNormal()) > 0;
if (scatter)
attenuation.set(albedo.value(record.getU(), record.getV(), record.getPoint()));
return scatter;
}
}
5 changes: 5 additions & 0 deletions src/main/java/math/Colors.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@ public class Colors {
public final static Color BLUE = new ImmutableColor(0, 0, 1);
public final static Color BLACK = new ImmutableColor(0, 0, 0);
public final static Color PURPLE = ImmutableColor.getByRGB(128,0,128);

public static Color add(Color a, Color b){
return new Color(a.getRed() + b.getRed(), a.getGreen() + b.getGreen(), a.getBlue() + b.getBlue());
}

public static Color multiply(Color a, Color b){
return new Color(a.getRed() * b.getRed(), a.getGreen() * b.getGreen(), a.getBlue() * b.getBlue());
}

public static class ImmutableColor extends Color{

public ImmutableColor() {
Expand Down
Loading

0 comments on commit 4c0eff2

Please sign in to comment.