예제 #1
0
파일: Color.cs 프로젝트: tzenes/SamTracer
        public static Color phong(ISect record, Scene scene)
        {
            if (record == null) return null;

            Color ambiant = new Color();
            Color diffuse = new Color();
            Color specular = new Color();
            Vec3 position = Ray.GetPosition(record.Ray, record.Dist);
            int i;

            foreach (Light light in scene.lights)
            {
                ambiant += record.Thing.mat.Ambiant * light.mat.Ambiant;
                var N = (position - record.Thing.Position).Normalize();
                var L = (light.Position - position).Normalize();
                var NDotL = N.Dot(L);

                if (NDotL < 0) continue;
                diffuse += record.Thing.mat.Diffuse * light.mat.Diffuse * NDotL;

                var R = N * 2 * NDotL - L;
                var V = (light.Position - position).Normalize();
                var RdotV = R.Dot(V);
                if (RdotV < 0) continue;
                specular += record.Thing.mat.Specular * light.mat.Specular * (float)Math.Pow(RdotV, record.Thing.mat.shinyness);
            }
            return Clamp(ambiant + diffuse + specular);
        }
예제 #2
0
        public override ISect intersect(Ray currentRay)
        {
            ISect record = null;
            float a = currentRay.Direction.Dot(currentRay.Direction);
            float b = (currentRay.Origin - Position).Dot(currentRay.Direction) * 2.0f;
            float c = (currentRay.Origin - Position).Dot(currentRay.Origin - Position) - (float)Math.Pow(radius,2.0);

            float det = (float)Math.Pow(b, 2) - 4 * a * c;
            if (det < 0)
                return null;

            float t1 = (-b + (float)Math.Sqrt(det)) / (2 * a);
            float t2 = (-b - (float)Math.Sqrt(det)) / (2 * a);

            if (t1 < 0 && t2 < 0)
                return record;

            record = new ISect();
            record.Thing = this;
            record.Ray = currentRay;

            if (t1 < 0)
                record.Dist = t2;
            else if (t2 < 0)
                record.Dist = t1;
            else
                record.Dist = Math.Min(t1, t2);

            return record;
        }