Skip to content

Commit 4740f42

Browse files
committed
Added reflections and timing
1 parent 4e46886 commit 4740f42

File tree

5 files changed

+58
-34
lines changed

5 files changed

+58
-34
lines changed

src/v0_single_threaded/camera.h

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <mutex>
44
#include <thread>
55
#include <vector>
6+
#include <atomic>
67

78
#pragma once
89

@@ -19,15 +20,15 @@ class Camera
1920
const int image_height = -1;
2021
const int image_width = static_cast<int>(aspect_ratio * image_height);
2122

22-
double vfov = 45.0; // vertical field of view in degrees
23-
point3 lookfrom = point3(0, 1, 3); // Point camera is looking from
24-
point3 lookat = point3(0, 0, -1); // Point camera is looking at
23+
double vfov = 35.0; // vertical field of view in degrees
24+
point3 lookfrom = point3(2, 2.5, 3); // Point camera is looking from
25+
point3 lookat = point3(-1, 0, -1); // Point camera is looking at
2526
vec3 vup = vec3(0, 1, 0); // Camera-relative "up" direction
2627

2728
int samples_per_pixel = 1;
2829
const int max_depth = 64; // Maximum ray bounce depth
2930

30-
int n_rays = 0; // Number of rays traced so far with this cam
31+
std::atomic<int> n_rays{0}; // Number of rays traced so far with this cam (thread-safe)
3132

3233
Camera(const point3 &center, const int image_height, const int image_channels, int samples_per_pixel = 1)
3334
: image_height(image_height), samples_per_pixel(samples_per_pixel), image_channels(image_channels), camera_center(center)
@@ -81,6 +82,18 @@ class Camera
8182
showProgress(image_height - 1, image_height);
8283
cout << endl;
8384
}
85+
86+
void renderPixelsParallelWithTiming(const hittable &scene, vector<unsigned char> &image)
87+
{
88+
auto start_time = std::chrono::high_resolution_clock::now();
89+
90+
renderPixelsParallel(scene, image);
91+
92+
auto end_time = std::chrono::high_resolution_clock::now();
93+
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time);
94+
95+
cout << "Parallel rendering completed in " << duration.count() << " milliseconds" << endl;
96+
}
8497

8598
void renderPixelsParallel(const hittable &scene, vector<unsigned char> &image)
8699
{
@@ -188,16 +201,21 @@ class Camera
188201
if(depth <= 0)
189202
return color(0,0,0); // No more light is gathered
190203

191-
n_rays++;
204+
n_rays.fetch_add(1, std::memory_order_relaxed);
192205

193206
hit_record rec;
194207

195208
if (world.hit(r, interval(0.0001, inf), rec)){
196209
// Display only normal
197210
// return 0.5 * (rec.normal + color(1, 1, 1));
211+
212+
if (rec.isMirror){
213+
vec3 reflected = r.direction() - 2 * dot(r.direction(), rec.normal) * rec.normal;
214+
return color(.8, 0, 0) * 0.2 + 0.8 * ray_color(ray(rec.p, unit_vector(reflected)), world, depth - 1);
215+
}
198216

199217
auto new_ray = vec3::random_in_hemisphere(rec.normal);
200-
return 0.5 * ray_color(ray(rec.p, new_ray), world, depth - 1);
218+
return 0.7 * ray_color(ray(rec.p, new_ray), world, depth - 1);
201219
}
202220

203221
// Le vecteur unit_direction variera entre -1 et +1 en x et y
@@ -229,8 +247,7 @@ class Camera
229247
{
230248
const int barWidth = 70;
231249
static int frame = 0;
232-
const char *spinner = "|/-\\";
233-
// We add 1 to current to start at 1 instead of 0
250+
const char *spinner = "|/-\\";
234251
float progress = (float)(current + 1) / total;
235252
int pos = barWidth * progress;
236253

src/v0_single_threaded/hittable.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ class hit_record
1313
double t; // The ray distance at the hit point
1414
bool frontFacing; // True if the ray hits the front face of the object
1515

16+
bool isMirror = false;
17+
1618
void set_face_normal(const ray &r, const vec3 &outward_normal)
1719
{
1820
// Sets the hit record normal vector.

src/v0_single_threaded/main.cc

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,27 @@ scene many_spheres()
151151
{
152152
scene s;
153153

154-
s.add(make_shared<sphere>(point3(0, -500.5, -1), 500));
154+
s.add(make_shared<sphere>(point3(0, -1000.5, -1), 1000));
155155
s.add(make_shared<sphere>(point3(0, 0, 0), .5));
156-
s.add(make_shared<sphere>(point3(-1, 1, -1), .8));
157156

158-
for(int i = 0; i < 10; i++)
159-
s.add(make_shared<sphere>(point3(RndGen::random_double(-4, 4), 0, RndGen::random_double(0, -4)), .3));
157+
auto leftSphere = make_shared<sphere>(point3(-1, 1, -1), .8);
158+
leftSphere.get()->isMirror = true;
159+
s.add(leftSphere);
160+
161+
for(int i = 0; i < 180; i++){
162+
auto obj = make_shared<sphere>(
163+
point3(
164+
RndGen::random_normal(-2, 2),
165+
RndGen::random_normal(-0.5, 0.4),
166+
RndGen::random_normal(-0.5, -4)),
167+
.1 + RndGen::random_double(0, 0.2));
168+
169+
if(RndGen::random_double() < 0.3){
170+
obj.get()-> isMirror = true;
171+
}
172+
173+
s.add(obj);
174+
}
160175

161176
return s;
162177
}
@@ -165,7 +180,7 @@ scene single_cube()
165180
{
166181
scene s;
167182

168-
s.add(make_shared<sphere>(point3(0, -500.5, -1), 500));
183+
s.add(make_shared<sphere>(point3(0, -1000.5, -1), 1000));
169184
s.add(make_shared<sphere>(point3(-1, 1, -1), .5));
170185
auto rotatedCube = make_shared<cube>(point3(0, 0, -1), 1, vec3(0, 45, 0));
171186
s.add(rotatedCube);
@@ -174,37 +189,28 @@ scene single_cube()
174189

175190
int main()
176191
{
177-
vec3 v1(0.2, 0.2, 0.2);
178-
cout << v1 << ": " << v1.length_squared() <<endl;
179-
auto l = v1.length_squared();
180-
181-
// normalizing the vector
182-
v1 = unit_vector(v1);
192+
const int samples_per_pixel = 64;
183193

184-
cout << v1 / sqrt(l) << ": " << v1.length_squared() <<endl;
194+
Camera c(vec3(0, 0, 0), 1080, channels, samples_per_pixel);
185195

186-
const int samples_per_pixel = 32;
187-
Camera c(vec3(0, 0, 0), 720, channels, samples_per_pixel);
188196
image_width = c.image_width;
189197
image_height = c.image_height;
190198

191199
vector<unsigned char> image(c.image_width * c.image_height * channels);
192200

193-
// scene.add(make_shared<sphere>(point3(0, 0, -1), .5));
194-
195-
std::cout << "🖼️ resolution: " << c.image_width << " x " << c.image_height << " pixels" << std::endl;
201+
std::cout << "Rendering at resolution: " << c.image_width << " x " << c.image_height << " pixels" << std::endl;
196202

197203
// Create a new scene for this frame
198204
int i = 0;
199205

200-
RndGen::set_seed(12);
206+
RndGen::set_seed(123);
201207

202208
scene s = many_spheres();
203209
//scene s = single_cube();
204210

205211
// frameScene.add(rotatedCube);
206212
vector<unsigned char> localImage(image.size());
207-
c.renderPixelsParallel(s, localImage);
213+
c.renderPixelsParallelWithTiming(s, localImage);
208214
dumpImageToFile(localImage, "res/output" + to_string(i) + ".png");
209215

210216
// std::function<void(int)> renderFrame = [&](int i)

src/v0_single_threaded/rnd_gen.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,16 @@ class RndGen {
2323
return distribution(get_rng());
2424
}
2525

26-
static double random_double(double min, double max) {
27-
return random_double() * (max - min) + min;
28-
}
29-
3026
static double random_normal() {
3127
// Returns a random real from standard normal distribution (mean=0, stddev=1).
3228
static thread_local std::normal_distribution<double> dis(0.0, 1.0);
3329
return dis(get_rng());
3430
}
3531

32+
static double random_double(double min, double max) {
33+
return random_double() * (max - min) + min;
34+
}
35+
3636
static double random_normal(double mean, double stddev) {
3737
// Returns a random real from normal distribution with given mean and standard deviation.
3838
normal_distribution<double> dis(mean, stddev);

src/v0_single_threaded/sphere.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@
55

66
class sphere : public hittable
77
{
8-
public:
8+
public:
99
sphere(const point3 &center, double radius) : center(center), radius(std::fmax(0, radius)) {}
1010

11-
void setMirror(bool mirror) { isMirror = mirror; }
12-
1311
bool isMirror = false;
1412

1513
/**
@@ -61,6 +59,7 @@ class sphere : public hittable
6159
rec.t = root;
6260
rec.p = r.at(rec.t);
6361
rec.normal = (rec.p - center) / radius;
62+
rec.isMirror = isMirror;
6463

6564
return true;
6665
}

0 commit comments

Comments
 (0)