1
$\begingroup$

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 enter image description here

$\endgroup$
  • $\begingroup$ What steps have you taken so far to try to narrow down the cause of the problem? $\endgroup$ – Julien Guertault Feb 22 '18 at 12:20
  • $\begingroup$ 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. $\endgroup$ – Matthew Glynn Feb 22 '18 at 15:55
  • $\begingroup$ 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 $\endgroup$ – PaulHK Feb 23 '18 at 2:50
  • $\begingroup$ what Paul said, I don't see the code anywhere for shadows, its just for intersections. How do you compute your shadows? $\endgroup$ – gallickgunner Mar 5 '18 at 16:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.