Skip to content

Commit

Permalink
Add readme, images, ppm2png convertor
Browse files Browse the repository at this point in the history
  • Loading branch information
VxDxK committed Sep 1, 2022
1 parent 8daddb5 commit 00c2a07
Show file tree
Hide file tree
Showing 11 changed files with 109 additions and 13 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Simple ray tracer
## Build and run
`mvn clean package`\
`java -jar ray-tracer.jar`

### 4K rendered scene
![4K rendered scene](pictures/image-4k.png)
Binary file added pictures/image-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pictures/image-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pictures/image-3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pictures/image-4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pictures/image-4k.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pictures/image-5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
Expand Down
86 changes: 86 additions & 0 deletions src/main/java/Convertor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class Convertor {
public static void main(String[] args) {

final String sourceFilename = "image.ppm";
final String destFilename = "image.png";

// try to get hold of the file.
try(BufferedReader reader = new BufferedReader(new FileReader(sourceFilename))) {
// validate file header
final String FileHeader = "P3";
String line = reader.readLine();
if (!FileHeader.equals(line)) {
System.err.printf("Failed to read %s, bad header\n", sourceFilename);
System.exit(1);
}

int width = 0;
int height = 0;

line = reader.readLine();
if (line != null) {
String[] values = line.split(" ");
width = Integer.parseInt(values[0]);
height = Integer.parseInt(values[1]);
} else {
System.err.printf("Failed to read %s, bad dimensions\n", sourceFilename);
System.exit(1);
}


// just validate the bpp value, even though it isn't being used
line = reader.readLine();
if (line != null) {
int bpp = Integer.parseInt(line);
if (bpp < 0 || bpp > 255) {
System.err.printf("Failed to read %s, bad colour size\n", sourceFilename);
System.exit(1);
}
} else {
System.err.printf("Failed to read %s, bad colour size\n", sourceFilename);
System.exit(1);
}

// prepare the data buffer
final int BPP = 3;
int imageSize = width * height;
System.out.println(width + "x" + height);
var imageData = new int[imageSize];

int row = 0;
while ((line = reader.readLine()) != null) {
String[] values = line.split(" ");
if (values.length != BPP) {
System.err.printf("Failed to read %s at line %d, bad colour size", sourceFilename, row);
System.exit(1);
}

int r = Integer.parseInt(values[0]);
int g = Integer.parseInt(values[1]);
int b = Integer.parseInt(values[2]);

int pixelValue = (r << 16) | (g << 8) | b;
imageData[row++] = pixelValue;
}

var bufferImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
bufferImg.setRGB(0, 0, width, height, imageData, 0, width);

var outputfile = new File(destFilename);
ImageIO.write(bufferImg, "png", outputfile);
} catch (IOException ex) {
System.out.println(ex.getMessage());
ex.printStackTrace();
System.exit(1);
}

System.out.printf("Processed %s into %s\n", sourceFilename, destFilename);
}
}
22 changes: 10 additions & 12 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,32 +36,31 @@ public static void main(String[] args) throws Exception{
public Main(BufferedWriter writer) throws IOException {
//Image
double aspectRatio = 16.0/9.0;
int width = 3840;
int width = 400;
int height = (int)(width / aspectRatio);
int samplesPerPixel = 500;
int samplesPerPixel = 100;
int depth = 50;
//World
HittableList world = new HittableArrayList();

Material ground = new Lambertian(new Color(0.5, 0.5, 0.5));
Material left = new Lambertian(new Color(0, 0, 1));
Material center = new Metal(new Color(0.8, 0.6, 0.2));
Material right = new Dielectric(2.4);
Material right = new Dielectric(1.5);

world.add(new Sphere(new Point(0, -1000, 0), 1000, ground));

world.add(new Sphere(new Point(-1.5, 1, 0), 1, left));
world.add(new Sphere(new Point(-2, 1, 0), 1, left));
world.add(new Sphere(new Point(0, 1, 0), 1, center));
world.add(new Sphere(new Point(1.5, 1, 0), 1, right));
Util.fillScene(world);
world.add(new Sphere(new Point(2, 1, 0), 1, right));
// Util.fillScene(world);
//Camera
Point lookFrom = new Point(13, 3, 13);
Point lookAt = new Point();
Point lookFrom = new Point(5, 5, 13);
Point lookAt = new Point(0, 1, 0);
Vector worldNormal = new Vector(0, 1, 0);
double fov = 20;
double aperture = 0.1;
double focusDist = new Vector(lookAt, lookAt).length();

double focusDist = new Vector(lookAt, lookFrom).length();

Camera camera = new ClearCamera(lookFrom, lookAt, worldNormal, fov, aspectRatio);
// Camera camera = new BlurCamera(lookFrom, lookAt, worldNormal, fov, aspectRatio, aperture, focusDist);
Expand All @@ -78,10 +77,10 @@ public Main(BufferedWriter writer) throws IOException {
double v = ((double) j + random.nextDouble())/(height - 1);
Ray ray = camera.getRay(u, v);
Color color = rayColor(ray, world, depth).scale(1d/samplesPerPixel);
// Color color = Util.normalColor(ray, world).scale(1d/samplesPerPixel);
pixelColor.setRed(pixelColor.getRed() + color.getRed())
.setGreen(pixelColor.getGreen() + color.getGreen())
.setBlue(pixelColor.getBlue() + color.getBlue());

}
writeColor(writer, pixelColor, Math::sqrt);
}
Expand Down Expand Up @@ -118,7 +117,6 @@ 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');
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/util/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static double clamp(double x, double min, double max){
/**
* Represents normal of hit point, as a color
*/
private Color normalColor(Ray r, Hittable world){
public static Color normalColor(Ray r, Hittable world){
HitRecord record = new HitRecord();

if(world.hit(r, 0, Double.POSITIVE_INFINITY, record)){
Expand Down

0 comments on commit 00c2a07

Please sign in to comment.