public Color Fragment(Vector3 bar)
        {
            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();

            Vector3 ref_l = Vector3.Normalize((Vector3.Multiply(n, Vector3.Dot(n, l) * 2) - l));  // reflected light
            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 = (int)Math.Min(2 + color.R * (diff + .49 * spec), 255); //5, 0.6
            int g = (int)Math.Min(2 + color.G * (diff + .49 * spec), 255);
            int b = (int)Math.Min(2 + color.B * (diff + .49 * spec), 255);

            color = Color.FromArgb(r, g, b);
            return(color);
        }
Beispiel #2
0
        public Color Fragment(Vector3 bar)
        {
            Vector2 uv    = VMatrix3x2.MultiplyMatrix3x2AndVec3(varying_uv, bar); // interpolate texture coordinates
            var     color = Model.Diffuse(uv);

            var v1 = varying_tri.Get_Col(0);
            var v2 = varying_tri.Get_Col(1);
            var v3 = varying_tri.Get_Col(2);

            Vector3 v31 = v3 - v1;
            Vector3 v21 = v2 - v1;

            Vector3 n = Vector3.Cross(v31, v21);

            n = n.Normalize();

            float intensity = Vector3.Dot(n, light_dir);

            //color = Color.White; // for no texture rendering

            int r = ToByteRange(color.R * intensity);
            int g = ToByteRange(color.G * intensity);
            int b = ToByteRange(color.B * intensity);

            color = Color.FromArgb(r, g, b);
            return(color);
        }
Beispiel #3
0
        public GouraudShader(Model Model, Matrix4x4 ViewPort, Matrix4x4 Projection, Matrix4x4 ModelView, Vector3 light_dir)
        {
            this.Model      = Model;
            this.ViewPort   = ViewPort;
            this.Projection = Projection;
            this.ModelView  = ModelView;
            this.light_dir  = light_dir;

            VPM        = ViewPort * Projection * ModelView;
            varying_uv = new VMatrix3x2();
        }
Beispiel #4
0
        public ShadowShader(Model Model, Matrix4x4 ViewPort, Matrix4x4 Projection,
                            Matrix4x4 ModelView,
                            Vector3 light_dir, float[] shadow_buffer, int width)
        {
            this.Model         = Model;
            this.ViewPort      = ViewPort;
            this.Projection    = Projection;
            this.ModelView     = ModelView;
            this.light_dir     = light_dir;
            this.shadow_buffer = shadow_buffer;
            this.width         = width;

            VPM = ViewPort * Projection * ModelView;

            varying_uv  = new VMatrix3x2();
            varying_tri = new VMatrix3x3();
        }
Beispiel #5
0
        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);
        }
Beispiel #6
0
        //public Color Fragment(Vec3F bar, out bool discard)
        //{
        //    discard = false;
        //    Vec2f uv = varying_uv * bar.VecToMat(); // interpolate texture coordinates
        //    var color = Model.Diffuse(uv);
        //    float intensity = varying_intensity * bar;   // interpolate intensity for the current pixel
        //    int r = (int)Math.Min(255, color.R * intensity);
        //    int g = (int)Math.Min(255, color.G * intensity);
        //    int b = (int)Math.Min(255, color.B * intensity);
        //    color = Color.FromArgb(r, g, b);
        //    return color;
        //}

        public Color Fragment(Vector3 bar)
        {
            Vector2 uv        = VMatrix3x2.MultiplyMatrix3x2AndVec3(varying_uv, bar); // interpolate texture coordinates
            var     color     = Model.Diffuse(uv);
            float   intensity = Vector3.Dot(varying_intensity, bar);                  // interpolate intensity for the current pixel

            if (intensity > .85)
            {
                intensity = 1;
            }
            else if (intensity > .60)
            {
                intensity = 0.80f;
            }
            else if (intensity > .45)
            {
                intensity = 0.60f;
            }
            else if (intensity > .30)
            {
                intensity = 0.45f;
            }
            else if (intensity > .15)
            {
                intensity = 0.30f;
            }
            else
            {
                intensity = 0;
            }

            int r = (int)Math.Min(255, color.R * intensity);
            int g = (int)Math.Min(0, color.G * intensity);
            int b = (int)Math.Min(200, color.B * intensity);

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