コード例 #1
0
        public Color Trace(Ray renderRay, int lightIndex, int depth)
        {
            depth -= 1;

            objectRender = GetIntersectionObject(renderRay, out hitPosition, out hitNormal);
            if (objectRender == null)
            {
                pixelColor = Color.Black;
                return(pixelColor);
            }

            //if (depth > 0)
            //{
            //    Vector3 reflectDirection = ReflectRay(renderRay.Direction, hitNormal);
            //    renderRay.Set(hitPosition, reflectDirection);
            //    Trace(renderRay, lightIndex, depth);
            //}

            //objectRender = GetIntersectionObject(renderRay, out hitPosition, out hitNormal);
            //if (objectRender == null)
            //{
            //    pixelColor = Color.Black;
            //    return pixelColor;
            //}

            pixelColor = objectRender.Color;
            SetObjectRenderUVProperties();
            ProcessUVTexture();
            pixelColor = ComputeLight(pixelColor, hitPosition, hitNormal, objectRender, scene.GetPointLight(lightIndex));

            return(pixelColor);
        }
コード例 #2
0
        private PotatoObject GetIntersectionObject(Ray ray, out Vector3 hitPosition, out Vector3 hitNormal)
        {
            hitPosition = new Vector3();
            hitNormal   = new Vector3();

            PotatoObject obj = GetClosestIntersectObject(ray);

            if (obj == null)
            {
                return(obj);
            }

            CalcultateHitPositionHitNormal(ray, out hitPosition, out hitNormal, obj);
            return(obj);
        }
コード例 #3
0
        public bool IntersectObjectInTheSceneExcept(PotatoObject obj, Ray ray)
        {
            foreach (PotatoObject objs in scene.GetPotatoObjects())
            {
                if (objs == obj)
                {
                    continue;
                }

                if (objs.Intersect(ray).Intersect)
                {
                    return(true);
                }
            }

            return(false);
        }
コード例 #4
0
        private PotatoObject GetClosestIntersectObject(Ray ray)
        {
            IntersectResult intersectResult = new IntersectResult();
            PotatoObject    obj             = null;
            float           minD            = float.PositiveInfinity;

            for (int i = 0; i < scene.GetPotatoObjectsCount(); i++)
            {
                intersectResult = scene.GetPotatoObject(i).Intersect(ray);
                if (intersectResult.Intersect && intersectResult.Discriminent < minD)
                {
                    minD = intersectResult.Discriminent;
                    obj  = scene.GetPotatoObject(i);
                }
            }

            return(obj);
        }
コード例 #5
0
 private float GetIntersectionDiscriminent(PotatoObject objectToRender, Ray ray)
 {
     return(objectToRender.Intersect(ray.Origin, ray.Direction).Discriminent);
 }
コード例 #6
0
        private Color ComputeLight(Color finalColor, Vector3 hitPosition, Vector3 hitNormal, PotatoObject objectRender, PotatoPointLight light)
        {
            Vector3 directionToLight = light.GetDirection(hitPosition);
            Ray     shadowRay        = new Ray(hitPosition, directionToLight);

            if (light.InRange(hitPosition))
            {
                float normalAng = DiffuseAngle(hitPosition, hitNormal, light);

                if (normalAng > 0)
                {
                    finalColor = Color.FromArgb((int)Math.Round((light.Color.R + finalColor.R) * 0.5f * normalAng * light.Intensity),
                                                (int)Math.Round((light.Color.G + finalColor.G) * 0.5f * normalAng * light.Intensity),
                                                (int)Math.Round((light.Color.B + finalColor.B) * 0.5f * normalAng * light.Intensity));
                }
                else
                {
                    finalColor = Color.Black;
                }
            }

            return(finalColor);
        }
コード例 #7
0
        public bool IsIntersectObjectInTheScene(Ray ray)
        {
            PotatoObject obj = GetClosestIntersectObject(ray);

            return(obj != null);
        }
コード例 #8
0
        private void CalcultateHitPositionHitNormal(Ray ray, out Vector3 hitPosition, out Vector3 hitNornal, PotatoObject obj)
        {
            float discr = GetIntersectionDiscriminent(obj, ray);

            hitPosition = CalculateHitPosition(discr, ray);
            hitNornal   = CalculateHitNormal(obj, hitPosition);
        }
コード例 #9
0
 public bool IsIntersect(PotatoObject obj, Ray ray)
 {
     return(obj.Intersect(ray).Intersect);
 }
コード例 #10
0
 private Vector3 CalculateHitNormal(PotatoObject objectToRender, Vector3 hitPosition)
 {
     return(objectToRender.GetNormal(hitPosition));
 }