Beispiel #1
0
 private Color GetNaturalColor(SceneObject thing, Vector pos, Vector norm, Vector rd, Scene scene)
 {
     Color ret = Color.Make(0, 0, 0);
     foreach (Light light in scene.Lights) {
         Vector ldis = Vector.Minus(light.Pos, pos);
         Vector livec = Vector.Norm(ldis);
         double neatIsect = TestRay(new Ray() { Start = pos, Dir = livec }, scene);
         bool isInShadow = !((neatIsect > Vector.Mag(ldis)) || (neatIsect == 0));
         if (!isInShadow) {
             double illum = Vector.Dot(livec, norm);
             Color lcolor = illum > 0 ? Color.Times(illum, light.Color) : Color.Make(0, 0, 0);
             double specular = Vector.Dot(livec, Vector.Norm(rd));
             Color scolor = specular > 0 ? Color.Times(Math.Pow(specular, thing.Surface.Roughness), light.Color) : Color.Make(0, 0, 0);
             ret = Color.Plus(ret, Color.Plus(Color.Times(thing.Surface.Diffuse(pos), lcolor),
                                              Color.Times(thing.Surface.Specular(pos), scolor)));
         }
     }
     return ret;
 }
Beispiel #2
0
 private Color GetReflectionColor(SceneObject thing, Vector pos, Vector norm, Vector rd, Scene scene, int depth)
 {
     return Color.Times(thing.Surface.Reflect(pos), TraceRay(new Ray() { Start = pos, Dir = rd }, scene, depth + 1));
 }
Beispiel #3
0
        private Color GetNaturalColor(SceneObject thing, Vector pos, Vector norm, Vector rd, Scene scene)
        {
            Color ret = new Color(Color.DefaultColor.R, Color.DefaultColor.G, Color.DefaultColor.B);
            Vector rdNormalized = Vector.Norm(rd);

            for (int i = 0; i < scene.Lights.Length; i++)
            {
                Light light = scene.Lights[i];

                float vx = light.Pos.X - pos.X;
                float vy = light.Pos.Y - pos.Y;
                float vz = light.Pos.Z - pos.Z;

                float sqrLength, invLength;

                sqrLength = vx * vx + vy * vy + vz * vz;
                invLength = SceneObject.InvSqrt(sqrLength);

                Vector livec = new Vector(vx * invLength, vy * invLength, vz * invLength);

                float neatIsect = TestRay(new Ray() { Start = pos, Dir = livec }, scene);

                bool isInShadow = !((neatIsect == 0) || (neatIsect > vx * vx + vy * vy + vz * vz));

                if (!isInShadow)
                {
                    float illum = livec.X * norm.X + livec.Y * norm.Y + livec.Z * norm.Z;
                    float specular = livec.X * rdNormalized.X + livec.Y * rdNormalized.Y + livec.Z * rdNormalized.Z;

                    Color lcolor = illum > 0 ? Color.Times(illum, light.Color) : Color.Background;

                    Color scolor = specular > 0 ? Color.Times((float)Math.Pow(specular, thing.Surface.Roughness), light.Color) : Color.Background;

                    var diffuseSurfaceColor = thing.Surface.Diffuse(pos);
                    var specularSurfaceColor = thing.Surface.Specular(pos);

                    ret.R += diffuseSurfaceColor.R * lcolor.R + specularSurfaceColor.R * scolor.R;
                    ret.G += diffuseSurfaceColor.G * lcolor.G + specularSurfaceColor.G * scolor.G;
                    ret.B += diffuseSurfaceColor.B * lcolor.B + specularSurfaceColor.B * scolor.B;

                }
            }
            return ret;
        }