<p>So I'm working on an assignment where we have to design a simple ray tracer. The program has two objects that it can use, sphere and cube. It can render the cube and spheres, it also does shading on the objects and will cast a shadow of light for the spheres; however, the boxes are not casting shadows. Instead I think it is casting it upon the actual cube instead of "the ground". Any ideas on how to fix that?</p>

<p>Below is my code for the cube and the image it should be vs the image it produces. </p>

<pre><code>package ray.surface;

import ray.Intersections;
import ray.Ray
import ray.math.Point3;
import ray.math.Vector3;
public class Box extends Surface {    
/* The corner of the box with the smallest x, y, and z components. */
protected final Point3 minPt = new Point3();
public void setMinPt(Point3 minPt) { this.minPt.set(minPt); }

/* The corner of the box with the largest x, y, and z components. */
protected final Point3 maxPt = new Point3();
public void setMaxPt(Point3 maxPt) { this.maxPt.set(maxPt); }

/**
 * @param outRecord the output Intersections
 * @param ray the ray to intersect
 * @return true if the surface intersects the ray
 */
public boolean intersect(Intersections outRecord, Ray rayIn) {

    double tNear = 0;
    double tFar = Double.POSITIVE_INFINITY;

    double x,y,z;
    // Compute the t values 
    double txmin;
    double txmax;
    double tymin;
    double tymax;
    double tzmin;
    double tzmax;


        txmin = (minPt.x - rayIn.origin.x) / rayIn.direction.x;
        txmax = (maxPt.x - rayIn.origin.x) / rayIn.direction.x;
        x=1;
        if (txmin &gt; txmax) {
            double tmp1 = txmin;
            txmin = txmax;
            txmax = tmp1;
            x=-1;
        }
        if (txmin &gt; tNear)
            tNear = txmin;
        if (txmax &lt; tFar)
            tFar = txmax;
        if (tNear &gt; tFar)
            return false;
        if (tFar &lt; 0)
            return false;


    //ray y direction is parallel to plane

        tymin = (minPt.y - rayIn.origin.y) / rayIn.direction.y;
        tymax = (maxPt.y - rayIn.origin.y) / rayIn.direction.y;
        y=1;

        if (tymin &gt; tymax) {
            double tmp2 = tymin;
            tymin = tymax;
            tymax = tmp2;
            y=-1;
        }
        if (tymin &gt; tNear)
            tNear = tymin;
        if (tymax &lt; tFar)
            tFar = tymax;
        if (tNear &gt; tFar)
            return false;
        if (tFar &lt; 0)
            return false;


    //ray z direction is parallel to plane

        tzmin = (minPt.z - rayIn.origin.z) / rayIn.direction.z;
        tzmax = (maxPt.z - rayIn.origin.z) / rayIn.direction.z;
        z=1;

        if (tzmin &gt; tzmax) {
            double tmp3 = tzmin;
            tzmin = tzmax;
            tzmax = tmp3;
            z=-1;
        }
        if (tzmin &gt; tNear)
            tNear = tzmin;
        if (tzmax &lt; tFar)
            tFar = tzmax;
        if (tNear &gt; tFar)
            return false;
        if (tFar &lt; 0)
            return false;

    double t = Math.min(tNear, tFar);
    // Set the location point.

    rayIn.evaluate(outRecord.location, tNear);
    //rayIn.start=0;
    rayIn.end=t;

    outRecord.surface=this;
    outRecord.t=t;
    // Set the Normal Vector.
    if(tNear==txmin) {
        outRecord.normal.set(new Vector3(x,0,0));
    } else if (tNear==tymin) {
        outRecord.normal.set(new Vector3(0,y,0));
    } else if (tNear==tzmin) {
        outRecord.normal.set(new Vector3(0,0,z));
    }
    outRecord.normal.normalize();


    return true;
}

/**
 * @see Object#toString()
 */
public String toString() {
    return "Box " + minPt + " " + maxPt + " " + shader + " end";
}
</code></pre>

<p>}</p>

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

CC BY-SA 3.0
2018-02-22T12:20:57.837Z
What steps have you taken so far to try to narrow down the cause of the problem?
Julien Guertault
CC BY-SA 3.0
2018-02-22T15:55:54.573Z
I've reviewed over the way that you calculate the t value for a cube, over and over again, that seems to be correct. The way that the shadows are calculated seems to be correct because the circles clearly work. And I'm assigning the ray values and the outrecord values fairly identical to the sphere. I'm beginning to think its not in this class, but I've searched my other classes and they all seem to be correct.
Matthew Glynn
CC BY-SA 3.0
2018-02-23T02:50:01.423Z
Can we see the code which generates the ray towards the light source ? Maybe a quick debug check you can do is to output per-pixel object normals to your image buffer to make sure nothing weird is going on there
PaulHK
CC BY-SA 3.0
2018-03-05T16:58:05.927Z
what Paul said, I don't see the code anywhere for shadows, its just for intersections. How do you compute your shadows?
gallickgunner
<raytracing><shader><image-processing><computational-geometry>
Ray Tracer Shadowing
Matthew Glynn
# Ray Tracer Shadowing

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

So I'm working on an assignment where we have to design a simple ray tracer. The program has two objects that it can use, sphere and cube. It can render the cube and spheres, it also does shading on the objects and will cast a shadow of light for the spheres; however, the boxes are not casting shadows. Instead I think it is casting it upon the actual cube instead of "the ground". Any ideas on how to fix that?

Below is my code for the cube and the image it should be vs the image it produces.

```
package ray.surface;

import ray.Intersections;
import ray.Ray
import ray.math.Point3;
import ray.math.Vector3;
public class Box extends Surface {    
/* The corner of the box with the smallest x, y, and z components. */
protected final Point3 minPt = new Point3();
public void setMinPt(Point3 minPt) { this.minPt.set(minPt); }

/* The corner of the box with the largest x, y, and z components. */
protected final Point3 maxPt = new Point3();
public void setMaxPt(Point3 maxPt) { this.maxPt.set(maxPt); }

/**
 * @param outRecord the output Intersections
 * @param ray the ray to intersect
 * @return true if the surface intersects the ray
 */
public boolean intersect(Intersections outRecord, Ray rayIn) {

    double tNear = 0;
    double tFar = Double.POSITIVE_INFINITY;

    double x,y,z;
    // Compute the t values 
    double txmin;
    double txmax;
    double tymin;
    double tymax;
    double tzmin;
    double tzmax;

        txmin = (minPt.x - rayIn.origin.x) / rayIn.direction.x;
        txmax = (maxPt.x - rayIn.origin.x) / rayIn.direction.x;
        x=1;
        if (txmin > txmax) {
            double tmp1 = txmin;
            txmin = txmax;
            txmax = tmp1;
            x=-1;
        }
        if (txmin > tNear)
            tNear = txmin;
        if (txmax < tFar)
            tFar = txmax;
        if (tNear > tFar)
            return false;
        if (tFar < 0)
            return false;

    //ray y direction is parallel to plane

        tymin = (minPt.y - rayIn.origin.y) / rayIn.direction.y;
        tymax = (maxPt.y - rayIn.origin.y) / rayIn.direction.y;
        y=1;

        if (tymin > tymax) {
            double tmp2 = tymin;
            tymin = tymax;
            tymax = tmp2;
            y=-1;
        }
        if (tymin > tNear)
            tNear = tymin;
        if (tymax < tFar)
            tFar = tymax;
        if (tNear > tFar)
            return false;
        if (tFar < 0)
            return false;

    //ray z direction is parallel to plane

        tzmin = (minPt.z - rayIn.origin.z) / rayIn.direction.z;
        tzmax = (maxPt.z - rayIn.origin.z) / rayIn.direction.z;
        z=1;

        if (tzmin > tzmax) {
            double tmp3 = tzmin;
            tzmin = tzmax;
            tzmax = tmp3;
            z=-1;
        }
        if (tzmin > tNear)
            tNear = tzmin;
        if (tzmax < tFar)
            tFar = tzmax;
        if (tNear > tFar)
            return false;
        if (tFar < 0)
            return false;

    double t = Math.min(tNear, tFar);
    // Set the location point.

    rayIn.evaluate(outRecord.location, tNear);
    //rayIn.start=0;
    rayIn.end=t;

    outRecord.surface=this;
    outRecord.t=t;
    // Set the Normal Vector.
    if(tNear==txmin) {
        outRecord.normal.set(new Vector3(x,0,0));
    } else if (tNear==tymin) {
        outRecord.normal.set(new Vector3(0,y,0));
    } else if (tNear==tzmin) {
        outRecord.normal.set(new Vector3(0,0,z));
    }
    outRecord.normal.normalize();

    return true;
}

/**
 * @see Object#toString()
 */
public String toString() {
    return "Box " + minPt + " " + maxPt + " " + shader + " end";
}

```

}

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

**Tags:** raytracing, shader, image-processing, computational-geometry

> **Comments:**
> **username_1**: What steps have you taken so far to try to narrow down the cause of the problem?  
> **username_0**: I've reviewed over the way that you calculate the t value for a cube, over and over again, that seems to be correct. The way that the shadows are calculated seems to be correct because the circles clearly work. And I'm assigning the ray values and the outrecord values fairly identical to the sphere. I'm beginning to think its not in this class, but I've searched my other classes and they all seem to be correct.  
> **username_2**: Can we see the code which generates the ray towards the light source ? Maybe a quick debug check you can do is to output per-pixel object normals to your image buffer to make sure nothing weird is going on there  
> **username_3**: what Paul said, I don't see the code anywhere for shadows, its just for intersections. How do you compute your shadows?  

---

## 0 answers

