private Color TraceTriangle(int lightIndex, ClosestTriangle result)
        {
            pixelColor = result.Mesh.Color;
            pixelColor = ComputeLight(pixelColor, result.HitPosition, result.HitNormal, sceneData.Lights[lightIndex]);

            return(pixelColor);
        }
Example #2
0
        public ClosestEntityIntersection GetClosestEntity(Ray ray)
        {
            /* Tree intersection
             * HashSet<BoundingBoxNode> nodesToExplore = new HashSet<BoundingBoxNode>();
             * double distance = 0.0;
             * for(int i = 0; i < scene.BoundingBoxTree.Root.Childs.Count; i++)
             * {
             *  if(BoxIntersection.Intersect(ray, scene.BoundingBoxTree.Root.Childs[i].Box, ref distance))
             *  {
             *      System.Console.WriteLine("Root to explore");
             *      nodesToExplore.Add(scene.BoundingBoxTree.Root.Childs[i]);
             *  }
             * }
             *
             * while(nodesToExplore.Count > 1)
             * {
             *  foreach (BoundingBoxNode node in nodesToExplore)
             *  {
             *      for (int i = 0; i < node.Childs.Count; i++)
             *      {
             *          if (BoxIntersection.Intersect(ray, node.Childs[i].Box, ref distance))
             *          {
             *              nodesToExplore.Add(node.Childs[i]);
             *          }
             *      }
             *
             *      nodesToExplore.Remove(node);
             *  }
             * }
             *
             * bool intersect = false;
             * double distance = 0.0;
             * double realDistance = double.PositiveInfinity;
             * BoundingBoxNode aabbNode = null;
             * List<BoundingBoxNode> closestIntersectedBox = new List<BoundingBoxNode>();
             * for(int i = 0; i < scene.aabbNodes.Count; i++)
             * {
             *  if(BoxIntersection.Intersect(ray, scene.aabbNodes[i].Box, ref distance))
             *  {
             *      if (distance < realDistance)
             *      {
             *          intersect = true;
             *          aabbNode = scene.aabbNodes[i];
             *          realDistance = distance;
             *
             *          closestIntersectedBox.Add(scene.aabbNodes[i]);
             *      }
             *  }
             * }
             *
             * for (int i = closestIntersectedBox.Count - 1; i >= 0; i--)
             * {
             *  if (closestIntersectedBox[i].IsMesh)
             *  {
             *      ClosestTriangle st = GetClosestTriangleIntersectionInList(ray, new List<PotatoMesh>() { closestIntersectedBox[i].Entity as PotatoMesh });
             *      if (st.IsIntersect) return st;
             *  }
             *  else
             *  {
             *      ClosestSphere sr = GetClosestSphereIntersectionInList(ray, new List<PotatoSphere>() { closestIntersectedBox[i].Entity as PotatoSphere });
             *      if (sr.IsIntersect) return sr;
             *  }
             * }
             *
             * return new ClosestEntityIntersection(new Vector3(), new Vector3(), 0.0, false, true);
             */


            ClosestTriangle triangleResult = GetClosestTriangleIntersectionInScene(ray);
            ClosestSphere   sphereResult   = GetClosestSphereIntersectionInScene(ray);

            if (!triangleResult.IsIntersect && sphereResult.IsIntersect)
            {
                return(sphereResult);
            }
            else if (!sphereResult.IsIntersect && triangleResult.IsIntersect)
            {
                return(triangleResult);
            }
            else if (triangleResult.IsIntersect && sphereResult.IsIntersect)
            {
                if (sphereResult.Distance > triangleResult.Distance)
                {
                    return(triangleResult);
                }
                else
                {
                    return(sphereResult);
                }
            }

            return(new ClosestEntityIntersection(new Vector3(), new Vector3(), 0.0, false, true));
        }