Skip to content

Commit b6af112

Browse files
committed
A sphere !!
With a pseudo-shading from depth
1 parent c7cc1f5 commit b6af112

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

output.png

12.6 KB
Loading

ray_tracer_cpp.code-workspace

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,10 @@
33
{
44
"path": "."
55
}
6-
]
6+
],
7+
"settings": {
8+
"files.associations": {
9+
"iostream": "cpp"
10+
}
11+
}
712
}

src/v0_single_threaded/main.cc

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,39 @@ inline void setPixel(vector<unsigned char> &viewPort, int x, int y, unsigned cha
6464
viewPort[index + 2] = b;
6565
}
6666

67+
double hit_sphere(const point3& center, double radius, const ray& r){
68+
// The points on the sphere are those who satisfy : (C−P)⋅(C−P)=r^2
69+
70+
// a=d⋅d
71+
// b=−2*d⋅(C−Q)
72+
// c=(C−Q)⋅(C−Q)−r2
73+
74+
auto a = r.direction().length_squared(); // Which is like r.dir · r.dir = ||r.dir||^2
75+
auto b = -2.0 * dot(r.direction(), r.origin() - center);
76+
auto c = (center - r.origin()).length_squared() - radius * radius;
77+
auto discriminant = b * b - 4 * a * c;
78+
79+
if(discriminant < 0){
80+
return -1.0;
81+
} else {
82+
return (-b + sqrt(discriminant)) / (2.0 * a);
83+
}
84+
}
85+
6786
color ray_color(const ray& r)
6887
{
6988
vec3 unit_direction = unit_vector(r.direction());
70-
// cout << "r: " << r.direction() << ", unit: " << unit_direction << endl;
7189

72-
// Le vecteur unit_direction variera entre -1 et +1 en x et y
90+
auto t = hit_sphere(point3(0,0,-1), 0.5, r);
7391

92+
if(t > -1){
93+
return color(t,0,0);
94+
}
95+
96+
// Le vecteur unit_direction variera entre -1 et +1 en x et y
7497
// A blue to white gradient background
75-
double t = 0.5 * (unit_direction.y() + 1.0);
76-
return (1.0 - t) * color(1.0, 1.0, 1.0) + t * color(0.5, 0.7, 1.0);
98+
double q = 0.5 * (unit_direction.y() + 1.0);
99+
return (1.0 - q) * color(1.0, 1.0, 1.0) + q * color(0.5, 0.7, 1.0);
77100
}
78101

79102
void renderPixels(std::vector<unsigned char> &image)

0 commit comments

Comments
 (0)