Exemple #1
0
        public Color Fragment(Vector3 bar)
        {
            var m = VMatrix3x3.MultiplyMatrix3x3AndVec3f(varying_tri, bar);

            int r = (int)Math.Min(255 * (m.Z / 2000), 255);
            int g = (int)Math.Min(255 * (m.Z / 2000), 255);
            int b = (int)Math.Min(255 * (m.Z / 2000), 255);

            var color = Color.FromArgb(r, g, b);

            return(color);
        }
        public Color Fragment(Vector3 bar)
        {
            Vector3 m  = VMatrix3x3.MultiplyMatrix3x3AndVec3f(varying_tri, bar);
            Vector3 sb = uniform_m_shadow.MultiplyByVector3_V3(m);

            int idx = (int)(sb.X) + (int)(sb.Y) * width;

            var closestDepth = shadow_buffer[idx];
            var currentDepth = sb.Z;

            var shadow = 0.3f + 0.7f * (closestDepth < currentDepth + 43.34f ? 1.2f : 0.45f);  //1.2, 0.45

            // interpolate texture coordinates

            Vector2 uv    = VMatrix3x2.MultiplyMatrix3x2AndVec3(varying_uv, bar);
            var     color = Model.Diffuse(uv);

            var norm = Model.Normal(uv);
            var n    = uniform_mit.MultiplyByVector3_V3(norm);

            n = n.Normalize();

            var l = uniform_m.MultiplyByVector3_V3(light_dir);

            l = l.Normalize();

            // reflected light value

            var ref_l = Vector3.Normalize((Vector3.Multiply(n, Vector3.Dot(n, l) * 2) - l));

            float spec = (float)Math.Pow(Math.Max(ref_l.Z, 0.0f), Model.Specular(uv));
            float diff = Math.Max(0, Vector3.Dot(n, l));

            int r = ToByteRange(2 + color.R * shadow * (diff + .49 * spec));
            int g = ToByteRange(2 + color.G * shadow * (diff + .49 * spec));
            int b = ToByteRange(2 + color.B * shadow * (diff + .49 * spec));

            color = Color.FromArgb(r, g, b);
            return(color);
        }