public bool GetFirstIntersection(Vector3D origin, Vector3D dir, double maxLength, out IOpticalSceneObject obj,
                                         out Vector3D iPos, out double iDistance, out uint subIdx)
        {
            // intersect all bounds
            Vector3D endPt = origin + (dir * maxLength);
            double nDist = double.MaxValue;
            int nIdx = -1;
            Vector3D nPt = Vector3D.Empty;
            subIdx = 0;
            for (int i = 0; i < objects.Count; i++)
            {
                double dist = 0;
                if (objects[i].Radius == -1 ||
                    (SphereSceneObject.IntersectRaySphere(origin, dir, objects[i].Origin, objects[i].Radius * objects[i].Radius,
                    out dist) && dist < nDist))
                {
                    // try actual intesection
                    Vector3D pt;
                    if (objects[i].GetIntersect(origin, endPt, dir, out pt, out dist, out subIdx) && dist < nDist && dist > 0.01)
                    {
                        nDist = dist;
                        nIdx = i;
                        nPt = pt;
                    }
                    /*else
                    {
                        nDist = dist;
                        nIdx = i;
                        nPt = pt;
                        subIdx = 3;
                    }*/
                }
            }

            // pass back intersection if falls within rays remaining length
            if (nIdx != -1 && nDist < maxLength)
            {
                obj = objects[nIdx];
                iPos = nPt; // even needed?
                iDistance = nDist;
                return true;
            }
            obj = null;
            iPos = Vector3D.Empty;
            iDistance = double.NaN;
            return false;
        }
        public RGBA_D Shade(Ray ray, Vector3D hitPoint, uint subIdx, IOpticalSceneObject obj,
                            ISceneManager scene, out Ray reflection, out Ray refraction)
        {
            RGBA_D color = RGBA_D.Empty;

            // needed?
            // normal.Normalize();
            
            Vector3D normal = obj.GetNormal(hitPoint, subIdx);

            //color.R = normal.X * 255;
            //color.G = normal.Y * 255;
            //color.B = normal.Z * 255;
            //color.A = 255;
            //refraction = null;
            //reflection = null;
            //return color;

            /*double len = (ray.Origin - hitPoint).Length();
            len -= 2;
            color.R = color.G = color.B = len * 42.5;*/

            foreach (Light light in scene.Lights)
            {
                Vector3D lv = light.Position - hitPoint;
                lv.Normalize();

                // deal with light ray first (diffuse)
                if (true)//ray.TraceRayToLight(hitPoint, light.Position))
                {
                    // light pixel
                    double cost = Vector3D.GetCosAngle(lv, normal);
                    Vector3D vRefl = Vector3D.Reflect(-lv, normal);
                    vRefl.Normalize();

                    double cosf = Vector3D.GetCosAngle(ray.DirectionUV, vRefl);
                    double result1 = Math.Max(0, cost) * 255;
                    double result2 = Math.Pow(Math.Max(0, cosf), shininess) * 255;

                    double luminosity = light.LuminosityForPoint(hitPoint);

                    double r = ((clr.R * diffuse * light.Clr3D.X * result1) +
                                (light.Clr3D.X * result2)) * luminosity;
                    double g = ((clr.G * diffuse * light.Clr3D.Y * result1) +
                                (light.Clr3D.Y * result2)) * luminosity;
                    double b = ((clr.B * diffuse * light.Clr3D.Z * result1) +
                                (light.Clr3D.Z * result2)) * luminosity;

                    color.R += r;
                    color.G += g;
                    color.B += b;
                }
            }
            
            // add ambient
            double alpha = 1 - transmission;
            color.R += (diffuse * scene.Ambient.R + (clr.R * emmissive)) * 255;
            //color.R *= alpha;
            color.G += (diffuse * scene.Ambient.G + (clr.G * emmissive)) * 255;
            //color.G *= alpha;
            color.B += (diffuse * scene.Ambient.B + (clr.B * emmissive)) * 255;
            //color.B *= alpha;
            
            color.A = alpha * 255;

            // blend texture (if any)
            /*if (texture != null)
            {
                Vector2D tCoord = obj.GetTexCoord(hitPoint, subIdx);
                // clamp for now
                if (tCoord.X < 0)
                    tCoord.X = 0;
                if (tCoord.Y < 0)
                    tCoord.Y = 0;
                if (tCoord.X > 1)
                    tCoord.X = 1;
                if (tCoord.Y > 1)
                    tCoord.Y = 1;

                int tX = (int)(tCoord.X * (texture.Width - 1));
                int tY = (int)(tCoord.Y * (texture.Height - 1));

                Color tClr = ((Bitmap)texture).GetPixel(tX, tY);
                color.R = (color.R + tClr.R) / 2;
                color.G = (color.G + tClr.G) / 2;
                color.B = (color.B + tClr.B) / 2;
            }*/

            if (ray.Intensity > 0)
            {
                /*if (this.reflection > 0)
                {
                    Vector3D refl = Vector3D.Reflect(ray.DirectionUV, normal);
                    reflection = new Ray(hitPoint, refl, ray.Intensity * this.reflection, ray.Length, ray.MaxLength, ray.scene);
                }
                else*/
                    reflection = null;
                /*if (transmission > 0)
                    refraction = new Ray(hitPoint, Vector3D.Normalize(Vector3D.Refract(1, 1.33, -ray.DirectionUV, normal)), ray.Intensity * transmission, ray.Length, ray.MaxLength, ray.scene);
                else*/
                refraction = null;
            }
            else
                reflection = refraction = null;

            ray.Intensity = 0;

            return color;
        }
Exemple #3
0
 public void RemoveObject(IOpticalSceneObject obj)
 {
     objects.Remove(obj);
 }
Exemple #4
0
 public void AddObject(IOpticalSceneObject obj)
 {
     objects.Add(obj);
 }
Exemple #5
0
        public bool GetFirstIntersection(Vector3D origin, Vector3D dir, double maxLength, out IOpticalSceneObject obj,
                                         out Vector3D iPos, out double iDistance, out uint subIdx)
        {
            // intersect all bounds
            Vector3D endPt = origin + (dir * maxLength);
            double   nDist = double.MaxValue;
            int      nIdx  = -1;
            Vector3D nPt   = Vector3D.Empty;

            subIdx = 0;
            for (int i = 0; i < objects.Count; i++)
            {
                double dist = 0;
                if (objects[i].Radius == -1 ||
                    (SphereSceneObject.IntersectRaySphere(origin, dir, objects[i].Origin, objects[i].Radius * objects[i].Radius,
                                                          out dist) && dist < nDist))
                {
                    // try actual intesection
                    Vector3D pt;
                    if (objects[i].GetIntersect(origin, endPt, dir, out pt, out dist, out subIdx) && dist <nDist && dist> 0.01)
                    {
                        nDist = dist;
                        nIdx  = i;
                        nPt   = pt;
                    }

                    /*else
                     * {
                     *  nDist = dist;
                     *  nIdx = i;
                     *  nPt = pt;
                     *  subIdx = 3;
                     * }*/
                }
            }

            // pass back intersection if falls within rays remaining length
            if (nIdx != -1 && nDist < maxLength)
            {
                obj       = objects[nIdx];
                iPos      = nPt; // even needed?
                iDistance = nDist;
                return(true);
            }
            obj       = null;
            iPos      = Vector3D.Empty;
            iDistance = double.NaN;
            return(false);
        }
Exemple #6
0
        public RGBA_D Shade(Ray ray, Vector3D hitPoint, uint subIdx, IOpticalSceneObject obj,
                            ISceneManager scene, out Ray reflection, out Ray refraction)
        {
            RGBA_D color = RGBA_D.Empty;

            // needed?
            // normal.Normalize();

            Vector3D normal = obj.GetNormal(hitPoint, subIdx);

            //color.R = normal.X * 255;
            //color.G = normal.Y * 255;
            //color.B = normal.Z * 255;
            //color.A = 255;
            //refraction = null;
            //reflection = null;
            //return color;

            /*double len = (ray.Origin - hitPoint).Length();
             * len -= 2;
             * color.R = color.G = color.B = len * 42.5;*/

            foreach (Light light in scene.Lights)
            {
                Vector3D lv = light.Position - hitPoint;
                lv.Normalize();

                // deal with light ray first (diffuse)
                if (true)//ray.TraceRayToLight(hitPoint, light.Position))
                {
                    // light pixel
                    double   cost  = Vector3D.GetCosAngle(lv, normal);
                    Vector3D vRefl = Vector3D.Reflect(-lv, normal);
                    vRefl.Normalize();

                    double cosf    = Vector3D.GetCosAngle(ray.DirectionUV, vRefl);
                    double result1 = Math.Max(0, cost) * 255;
                    double result2 = Math.Pow(Math.Max(0, cosf), shininess) * 255;

                    double luminosity = light.LuminosityForPoint(hitPoint);

                    double r = ((clr.R * diffuse * light.Clr3D.X * result1) +
                                (light.Clr3D.X * result2)) * luminosity;
                    double g = ((clr.G * diffuse * light.Clr3D.Y * result1) +
                                (light.Clr3D.Y * result2)) * luminosity;
                    double b = ((clr.B * diffuse * light.Clr3D.Z * result1) +
                                (light.Clr3D.Z * result2)) * luminosity;

                    color.R += r;
                    color.G += g;
                    color.B += b;
                }
            }

            // add ambient
            double alpha = 1 - transmission;

            color.R += (diffuse * scene.Ambient.R + (clr.R * emmissive)) * 255;
            //color.R *= alpha;
            color.G += (diffuse * scene.Ambient.G + (clr.G * emmissive)) * 255;
            //color.G *= alpha;
            color.B += (diffuse * scene.Ambient.B + (clr.B * emmissive)) * 255;
            //color.B *= alpha;

            color.A = alpha * 255;

            // blend texture (if any)

            /*if (texture != null)
             * {
             *  Vector2D tCoord = obj.GetTexCoord(hitPoint, subIdx);
             *  // clamp for now
             *  if (tCoord.X < 0)
             *      tCoord.X = 0;
             *  if (tCoord.Y < 0)
             *      tCoord.Y = 0;
             *  if (tCoord.X > 1)
             *      tCoord.X = 1;
             *  if (tCoord.Y > 1)
             *      tCoord.Y = 1;
             *
             *  int tX = (int)(tCoord.X * (texture.Width - 1));
             *  int tY = (int)(tCoord.Y * (texture.Height - 1));
             *
             *  Color tClr = ((Bitmap)texture).GetPixel(tX, tY);
             *  color.R = (color.R + tClr.R) / 2;
             *  color.G = (color.G + tClr.G) / 2;
             *  color.B = (color.B + tClr.B) / 2;
             * }*/

            if (ray.Intensity > 0)
            {
                /*if (this.reflection > 0)
                 * {
                 *  Vector3D refl = Vector3D.Reflect(ray.DirectionUV, normal);
                 *  reflection = new Ray(hitPoint, refl, ray.Intensity * this.reflection, ray.Length, ray.MaxLength, ray.scene);
                 * }
                 * else*/
                reflection = null;

                /*if (transmission > 0)
                 *  refraction = new Ray(hitPoint, Vector3D.Normalize(Vector3D.Refract(1, 1.33, -ray.DirectionUV, normal)), ray.Intensity * transmission, ray.Length, ray.MaxLength, ray.scene);
                 * else*/
                refraction = null;
            }
            else
            {
                reflection = refraction = null;
            }

            ray.Intensity = 0;

            return(color);
        }
 public void RemoveObject(IOpticalSceneObject obj)
 {
     objects.Remove(obj);
 }
 public void AddObject(IOpticalSceneObject obj)
 {
     objects.Add(obj);
 }