@@ -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+
6786color 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
79102void renderPixels (std::vector<unsigned char > &image)
0 commit comments