vendredi 18 décembre 2015

Reflect light in 3D space

I am trying to reflect light onto a triangle surface in 3D space. Everything worked fine until I moved the light source to the triangle's backside.

It appears that when the light face the triangle's backside the light get reflected as if the light source were on the other side of the triangle. The reflected light's theta (angle between incoming light and the reflected light) will also be half of that of the normal (green line) instead of double.

On the images provided, the yellow dot is the light source, white line is incoming light, green line is the triangle's normal and red line is the reflected light

 public Location Reflect(Location pivot, ref Line mid)
    {
        //measure the length of the white and green line
        double lNormal = mid.length;
        double lLight = Location.DistanceBetweenPoints(this, pivot);

        //calculates these line's x, y and z gradient
        double xN = (mid.Tail.X - mid.Head.X) / lNormal;
        double yN = (mid.Tail.Y - mid.Head.Y) / lNormal;
        double zN = (mid.Tail.Z - mid.Head.Z) / lNormal;
        double xLight = (x - pivot.X) / lLight;
        double yLight = (y - pivot.Y) / lLight;
        double zLight = (z - pivot.Z) / lLight;

        //reflect the white line's gradients around the green line.
        double xNew = xN - (xLight - xN);
        double yNew = yN - (yLight - yN);
        double zNew = zN - (zLight - zN);

        //multiply the gradients with the white line's length
        xNew = xNew * lLight;
        yNew = yNew * lLight;
        zNew = zNew * lLight;

        return new Location(xNew + mid.Head.X + pivot.X, yNew + mid.Head.Y + pivot.Y, zNew + mid.Head.Z + pivot.Z);
    }

Results:

Reflection when Normal and light source are on the same side Reflection when Normal and light source are on the same side

Reflection when Normal and light source are on opposite sides Reflection when Normal and light source are on opposite sides

I didn't use any additional 3D resources. Everything is coded in pure C#.

Any help to fix it will be appreciated. Thank you for your time.





Aucun commentaire:

Enregistrer un commentaire