<p>This line doesn't look quite right:</p>

<pre><code>float t = (-b - sqrt (det)) / 2.0 * a;
</code></pre>

<p>You're missing parentheses around <code>(2.0 * a)</code>. Remember that multiplication and division have equal operator precedence, so the expression as it's currently written means <code>((-b - sqrt (det)) / 2.0) * a</code>, putting the <code>a</code> in the numerator instead of the denominator.</p>

<p>(Oh and this is a nitpick, but the thing you named <code>det</code> is called a “discriminant”, not a determinant.)</p>

CC BY-SA 3.0
2018-02-20T08:16:54.957Z
lol this is what happens when you stop programming for a couple of years :-) thanks for idiot-checking me
spraff
CC BY-SA 3.0
2018-02-20T05:32:56.110Z
2018-02-20T06:10:25.333Z
2018-02-20T06:10:25.333Z
Nathan Reed
<p>I am learning GLSL and trying to raytrace a sphere.</p>

<p>Here is a fragment shader. It correctly discards fragments which are not on the sphere, but when I try to calculate the point of intersection (and hence, the normal), I get nonsense.</p>

<p>Here's the fragment shader.</p>

<pre><code>#version 300 es
precision mediump float;
out mediump vec4 out_colour;

in  mediump vec3 X;      // intersection of ray with polygon

uniform mediump vec3 C;  // camera position
uniform mediump vec3 CS; // sphere position - camera position
uniform         float r; // sphere radius

void main ()
{
    vec3 CX = X - C;

    // |t.CX-CS|=r, CX.CX.t.t - 2.CS.CX.t + CS.CS - r.r = 0

    float a = dot (CX, CX);
    float b = -2.0 * dot (CX, CS);
    float c = dot (CS, CS) - r * r;

    float det = b * b - 4.0 * a * c;

    if (det &lt; 0.0)
        discard;

    float t = (-b - sqrt (det)) / 2.0 * a;

    if (t &lt;= 0.0)
        discard;

    vec3 N = normalize (t * CX - CS);

    out_colour = vec4 (N, 1);
}
</code></pre>

<p>Here's an illustration of the variable names</p>

<p><a href="https://i.sstatic.net/m2gPL.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/m2gPL.png" alt="enter image description here"></a></p>

<p>I was expecting the normals to be illustrated with a wide-ranging rgb colour across the sphere, but as you can see...</p>

<p><a href="https://i.sstatic.net/XlwR9.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/XlwR9.png" alt="enter image description here"></a></p>

<p>What went wrong?</p>

<raytracing><glsl><geometry>
What's wrong with my computation of the intersection of a ray with a sphere
spraff
# What's wrong with my computation of the intersection of a ray with a sphere

(Asked by: username_0 on 2018-02-19)

I am learning GLSL and trying to raytrace a sphere.

Here is a fragment shader. It correctly discards fragments which are not on the sphere, but when I try to calculate the point of intersection (and hence, the normal), I get nonsense.

Here's the fragment shader.

```
#version 300 es
precision mediump float;
out mediump vec4 out_colour;

in  mediump vec3 X;      // intersection of ray with polygon

uniform mediump vec3 C;  // camera position
uniform mediump vec3 CS; // sphere position - camera position
uniform         float r; // sphere radius

void main ()
{
    vec3 CX = X - C;

    // |t.CX-CS|=r, CX.CX.t.t - 2.CS.CX.t + CS.CS - r.r = 0

    float a = dot (CX, CX);
    float b = -2.0 * dot (CX, CS);
    float c = dot (CS, CS) - r * r;

    float det = b * b - 4.0 * a * c;

    if (det < 0.0)
        discard;

    float t = (-b - sqrt (det)) / 2.0 * a;

    if (t <= 0.0)
        discard;

    vec3 N = normalize (t * CX - CS);

    out_colour = vec4 (N, 1);
}

```

Here's an illustration of the variable names

[![enter image description here](https://i.sstatic.net/m2gPL.png)](https://i.sstatic.net/m2gPL.png)

I was expecting the normals to be illustrated with a wide-ranging rgb colour across the sphere, but as you can see...

[![enter image description here](https://i.sstatic.net/XlwR9.png)](https://i.sstatic.net/XlwR9.png)

What went wrong?

**Tags:** raytracing, glsl, geometry


---

## 1 answers

### Answer by username_1 on 2018-02-20. Score: 2 | Accepted answer

This line doesn't look quite right:

```
float t = (-b - sqrt (det)) / 2.0 * a;

```

You're missing parentheses around `(2.0 * a)`. Remember that multiplication and division have equal operator precedence, so the expression as it's currently written means `((-b - sqrt (det)) / 2.0) * a`, putting the `a` in the numerator instead of the denominator.

(Oh and this is a nitpick, but the thing you named `det` is called a “discriminant”, not a determinant.)

> **Comments:**
> **username_0**: lol this is what happens when you stop programming for a couple of years :-) thanks for idiot-checking me  

---

