Skip to content

Commit 0124d64

Browse files
committed
Window Renderer fixed and Render options added
1 parent dbf6963 commit 0124d64

File tree

5 files changed

+87
-26
lines changed

5 files changed

+87
-26
lines changed

.idea/misc.xml

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/RayTracing/Main.java

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,66 @@
33
public class Main {
44
public static void main(String[] args) {
55
Scene chosen = Scene.finalScene();
6-
Renderer render = new Renderer(chosen.camera);
76

7+
// Using a MacBook Air M2 for comparison:
88
// imageRender(50spp, 1280x720) for final image = 206.791s
99
// imageRenderParallel(50spp, 1280x720) for final image = 46.492s
10-
render.imageRenderParallel(chosen.world, 50, "final_test.png");
10+
// imageRenderParallel(500spp, 1280x720) for final image = 546.934s
11+
// Only takes around 100MB of memory to render!
12+
13+
System.out.println("\nSelect a render mode:");
14+
System.out.println("1. Single-threaded with 50 samples per pixel");
15+
System.out.println("2. Multi-threaded with 50 samples per pixel");
16+
System.out.println("3. Multi-threaded with 500 samples per pixel");
17+
System.out.println("4. Windowed with 15 samples per pixel");
18+
System.out.println("5. Windowed with 50 samples per pixel");
19+
20+
21+
int choice = 0;
22+
while (choice < 1 || choice > 5) {
23+
choice = Utility.readInt();
24+
}
1125

12-
//render.windowRender(chosen.world, 50);
26+
System.out.println("\nSelect a Scene:");
27+
System.out.println("1. Initial Scene");
28+
System.out.println("2. Final Scene");
29+
30+
int sceneChoice = 0;
31+
while (sceneChoice < 1 || sceneChoice > 2) {
32+
sceneChoice = Utility.readInt();
33+
}
34+
35+
switch (sceneChoice) {
36+
case 1:
37+
chosen = Scene.initScene();
38+
break;
39+
case 2:
40+
chosen = Scene.finalScene();
41+
break;
42+
default:
43+
break;
44+
}
45+
46+
Renderer render = new Renderer(chosen.camera);
1347

48+
switch (choice) {
49+
case 1:
50+
render.imageRender(chosen.world, 50, "Output(50spp).png");
51+
break;
52+
case 2:
53+
render.imageRenderParallel(chosen.world, 50, "Output(50spp).png");
54+
break;
55+
case 3:
56+
render.imageRenderParallel(chosen.world, 500, "Output(500spp).png");
57+
break;
58+
case 4:
59+
render.windowRender(chosen.world, 15);
60+
break;
61+
case 5:
62+
render.windowRender(chosen.world, 50);
63+
break;
64+
default:
65+
break;
66+
}
1467
}
1568
}

src/RayTracing/Renderer.java

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -174,19 +174,27 @@ void imageRenderParallel(HitTable scene, int spp, String filename) {
174174

175175
void windowRender(HitTable scene, int spp) {
176176
Color[] pixelColors = new Color[image_height*image_width];
177+
177178
for (int i = 0; i<image_height*image_width; ++i)
178179
pixelColors[i] = new Color(0);
179180

180-
long init_time = System.currentTimeMillis(); // Time for Benchmark
181+
JFrame window = getjFrame(scene, spp, pixelColors);
182+
window.setVisible(true);
183+
window.setResizable(false);
184+
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
185+
}
181186

187+
private JFrame getjFrame(HitTable scene, int spp, Color[] pixelColors) {
182188
JFrame window = new JFrame("RayTracing") {
183189
@Override
184190
public void paint(Graphics g) {
185191
super.paint(g);
186192
System.out.println(image_width + "x" + image_height + ", " + spp + " samples per pixel");
193+
long init_time = System.currentTimeMillis(); // Time for Benchmark
194+
BufferedImage image = new BufferedImage(image_width, image_height, BufferedImage.TYPE_INT_RGB);
187195

188196
// For each sample per pixel, render the whole image
189-
for (long s=0; s < spp; ++s) {
197+
for (long s = 0; s < spp; ++s) {
190198
long start_time = System.currentTimeMillis(); // Time for Benchmark
191199
for (int y = 0; y < image_height; ++y) {
192200
for (int x = 0; x < image_width; ++x) {
@@ -198,31 +206,28 @@ public void paint(Graphics g) {
198206
pixelColors[y * image_width + x].equalAdd(rayColor(camera.get_ray(u, v), scene, depth));
199207
g.setColor(writeAwtColor(pixelColors[y * image_width + x], s + 1));
200208
g.drawRect(x, y, 1, 1);
209+
210+
// Save the pixel to the image
211+
if (s == spp - 1) {
212+
image.setRGB(x, y, writeAwtColor(pixelColors[y * image_width + x], s + 1).getRGB());
213+
}
201214
}
202215
}
203-
System.out.print("\r" + "Sample: " + (s+1) + " " + "in " + (double)(System.currentTimeMillis()-start_time)/1000+"s");
216+
System.out.println("Sample: " + (s+1) + " " + "in " + (double)(System.currentTimeMillis()-start_time)/1000+"s");
217+
}
218+
try {
219+
ImageIO.write(image, "png", new java.io.File("Output.png"));
220+
System.out.println("\nImage saved to Output.png");
221+
System.out.println("Rendering finished in " + (double)(System.currentTimeMillis()-init_time)/1000+"s");
222+
}
223+
catch (IOException e) {
224+
throw new RuntimeException(e);
204225
}
205226
}
206227
};
207228

208229

209230
window.setSize(image_width, image_height);
210-
window.setVisible(true);
211-
window.setResizable(false);
212-
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
213-
214-
// Save the image to a file
215-
try {
216-
BufferedImage image = new BufferedImage(image_width, image_height, BufferedImage.TYPE_INT_RGB);
217-
Graphics2D graphics = image.createGraphics();
218-
window.paint(graphics);
219-
ImageIO.write(image, "png", new java.io.File("Output.png"));
220-
System.out.println("\nImage saved to Output.png");
221-
System.out.println("Rendering finished in " + (double)(System.currentTimeMillis()-init_time)/1000+"s");
222-
223-
}
224-
catch (IOException e) {
225-
throw new RuntimeException(e);
226-
}
231+
return window;
227232
}
228233
}

src/RayTracing/Scene.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class Scene {
2929
}
3030

3131
public static Scene initScene(){
32-
Point lookFrom =new Point(0, 0, 0);
32+
Point lookFrom = new Point(0, 0, 0);
3333
Point lookAt = new Point(0, 0, -1);
3434
Vec3 vup = new Vec3(0, 1, 0);
3535
Vec3 lookLength = lookFrom.minus(lookAt);
@@ -59,7 +59,7 @@ public static Scene initScene(){
5959
}
6060

6161
public static Scene finalScene(){
62-
Point lookFrom =new Point(13, 2, 3);
62+
Point lookFrom = new Point(13, 2, 3);
6363
Point lookAt = new Point(0, 0, 0);
6464
Vec3 vup = new Vec3(0, 1, 0);
6565
Vec3 lookLength = lookFrom.minus(lookAt);

src/RayTracing/Utility.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,8 @@ public static double clamp(double x, double low, double high) {
2525
if(x<low) return low;
2626
else return Math.min(x, high);
2727
}
28+
29+
public static int readInt() {
30+
return Integer.parseInt(System.console().readLine());
31+
}
2832
}

0 commit comments

Comments
 (0)