<p>It turns out, the problem was because the sphere was placed very far away and the radius is too big. This produces a normal with a high value z.</p>
<p>A solution for me was to place the sphere very close to the origin—about z=0.25, and set the radius of the sphere less than z— about r=0.125.</p>
<p>light_source (-1,0,0)
<img src="https://i.sstatic.net/TnOTu.png" alt="-1,0,0" />
light_source (0,0,-1)
<img src="https://i.sstatic.net/Guz4I.png" alt="0,0,-1" /></p>

CC BY-SA 4.0
2020-11-24T04:27:35.893Z
2020-11-24T04:27:35.893Z
Spade 000
<p>I think there is a problem with your ray-sphere intersection.</p>
<p>It should go like this</p>
<pre><code>sphere_normal = ray.origin - sphere_center;
b = dot(sphere_normal , ray.normalized_dir);
c = dot(sphere_normal , sphere_normal ) - sphere_radius*spheres_radius;
float disc = b * b - c;

float t = -1.0f;
if(disc &gt;= 0.0f)
t = -b +- sqrt(disc);

if(t &gt;= 0) // means ray intersects...
     
</code></pre>
<p>Check out scratchapixel to understand how I got this.
<a href="https://www.scratchapixel.com/lessons/3d-basic-rendering/minimal-ray-tracer-rendering-simple-shapes/ray-sphere-intersection" rel="nofollow noreferrer">https://www.scratchapixel.com/lessons/3d-basic-rendering/minimal-ray-tracer-rendering-simple-shapes/ray-sphere-intersection</a></p>

CC BY-SA 4.0
2020-11-23T07:52:45.430Z
2020-11-23T23:27:54.973Z
2020-11-23T23:27:54.973Z
gallickgunner
<p>I've sucessfully implemented a ray tracer from scratch in C and it's marvelous:
<a href="https://i.sstatic.net/QL1ix.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/QL1ix.png" alt="sphere1" /></a></p>
<p>The surface normal on this sphere is taken by computing the intersection point, A, and treating this as a euclidean vector with origin, B, the center of the sphere. To normalize it, I first subtracted A with B, yielding C:</p>
<p>C = (Ax-Bx,Ay-By)</p>
<p>From this, it's divided by the magnitude:</p>
<p>N = C/|C|</p>
<p>The illumination is calculated by taking the dot product of N and LIGHT_SOURCE and multiplying it with 255:</p>
<pre><code>if (dot_product&lt;0.0) dot_product = 0.0;
illumination = (unsigned char)(dot_product*255.0);
</code></pre>
<p>When I tried setting the light source to (0.0, 0.0, -1.0), it produce something like this:
<a href="https://i.sstatic.net/dhNtj.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/dhNtj.png" alt="sphere2" /></a></p>
<p>I expected the shade to be something like it's brighter on the middle and becoming more dimmer on the side.</p>
<p>Anyone know what's causing the problem?</p>
<p>Source code: <a href="https://gist.github.com/harieamjari/6297a35400460edbabbd8e39720ed0f5" rel="nofollow noreferrer">https://gist.github.com/harieamjari/6297a35400460edbabbd8e39720ed0f5</a></p>
<p>This can be compiled if you already have libpng16.</p>

<shading>
Lambertian shading illuminates all the front of a sphere
Spade 000
# Lambertian shading illuminates all the front of a sphere

(Asked by: username_0 on 2020-11-23)

I've sucessfully implemented a ray tracer from scratch in C and it's marvelous:
[![sphere1](https://i.sstatic.net/QL1ix.png)](https://i.sstatic.net/QL1ix.png)

The surface normal on this sphere is taken by computing the intersection point, A, and treating this as a euclidean vector with origin, B, the center of the sphere. To normalize it, I first subtracted A with B, yielding C:

C = (Ax-Bx,Ay-By)

From this, it's divided by the magnitude:

N = C/|C|

The illumination is calculated by taking the dot product of N and LIGHT\_SOURCE and multiplying it with 255:

```
if (dot_product<0.0) dot_product = 0.0;
illumination = (unsigned char)(dot_product*255.0);

```

When I tried setting the light source to (0.0, 0.0, -1.0), it produce something like this:
[![sphere2](https://i.sstatic.net/dhNtj.png)](https://i.sstatic.net/dhNtj.png)

I expected the shade to be something like it's brighter on the middle and becoming more dimmer on the side.

Anyone know what's causing the problem?

Source code: <https://gist.github.com/harieamjari/6297a35400460edbabbd8e39720ed0f5>

This can be compiled if you already have libpng16.

**Tags:** shading


---

## 2 answers

### Answer by username_0 on 2020-11-24. Score: 1

It turns out, the problem was because the sphere was placed very far away and the radius is too big. This produces a normal with a high value z.

A solution for me was to place the sphere very close to the origin—about z=0.25, and set the radius of the sphere less than z— about r=0.125.

light\_source (-1,0,0)
![-1,0,0](https://i.sstatic.net/TnOTu.png)
light\_source (0,0,-1)
![0,0,-1](https://i.sstatic.net/Guz4I.png)


---

### Answer by username_1 on 2020-11-23. Score: 0

I think there is a problem with your ray-sphere intersection.

It should go like this

```
sphere_normal = ray.origin - sphere_center;
b = dot(sphere_normal , ray.normalized_dir);
c = dot(sphere_normal , sphere_normal ) - sphere_radius*spheres_radius;
float disc = b * b - c;

float t = -1.0f;
if(disc >= 0.0f)
t = -b +- sqrt(disc);

if(t >= 0) // means ray intersects...
     

```

Check out scratchapixel to understand how I got this.
<https://www.scratchapixel.com/lessons/3d-basic-rendering/minimal-ray-tracer-rendering-simple-shapes/ray-sphere-intersection>


---

