Example #1
0
        public static Color Lerp(Color c1, Color c2, float t)
        {
            int r = (int)MathUtility.Lerp(c1.R, c2.R, t);
            int g = (int)MathUtility.Lerp(c1.G, c2.G, t);
            int b = (int)MathUtility.Lerp(c1.B, c2.B, t);

            return(ColorUtility.ToColor(r, g, b));
        }
Example #2
0
        public static Vector Lerp(Vector a, Vector b, float t)
        {
            Vector vec = Vector.Zero;

            vec.x = MathUtility.Lerp(a.x, b.x, t);
            vec.y = MathUtility.Lerp(a.y, b.y, t);
            vec.z = MathUtility.Lerp(a.z, b.z, t);
            return(vec);
        }
Example #3
0
        public static Vertex LerpVertexInScreenSpace(Vertex v1, Vertex v2, float t)
        {
            Vertex vert = new Vertex();

            vert.pos     = MathUtility.Lerp(v1.pos, v2.pos, t);
            vert.color   = ColorUtility.Lerp(v1.color, v2.color, t);
            vert.u       = MathUtility.Lerp(v1.u, v2.u, t);
            vert.v       = MathUtility.Lerp(v1.v, v2.v, t);
            vert.onePerZ = MathUtility.Lerp(v1.onePerZ, v2.onePerZ, t);
            return(vert);
        }
        public void DrawScanLine(Vertex left, Vertex right)
        {
            Texture2D texture      = Application.Texture;
            int       length       = (int)(right.pos.x - left.pos.x);
            float     onePerLength = length == 0 ? 0 : 1f / length;
            int       y            = (int)left.pos.y;

            for (int i = (int)left.pos.x; i < right.pos.x; i++)
            {
                float t       = (i - left.pos.x) * onePerLength;
                float onePerZ = MathUtility.Lerp(left.onePerZ, right.onePerZ, t);
                if (false) //TODO: 深度测试
                {
                    continue;
                }
                //x'、y'与1/z是线性关系,u/z、v/z 与 x’、y’也是线性关系
                //所以可以对 1/z关于 x’、y'插值得到 1/z’
                //然后对u/z、v/z 关于 x’、y’插值得到 u'/z' 和 v'/z'
                // u'/z' 和 v'/z'除以 1/z’得出正确的u’、v'
                float tz     = 1 / onePerZ;
                float u      = MathUtility.Lerp(left.u, right.u, t) * tz * (texture.width - 1);
                float v      = MathUtility.Lerp(left.v, right.v, t) * tz * (texture.height - 1);
                int   uIndex = (int)Math.Round(u, MidpointRounding.AwayFromZero);
                int   vIndex = (int)Math.Round(v, MidpointRounding.AwayFromZero);

                Color color = Color.White;
                //if (uIndex > 128)
                //{
                //    color = Color.Yellow;
                //    if (vIndex > 128)
                //    {
                //        color = Color.YellowGreen;
                //    }
                //}
                //else if (vIndex > 128)
                //{
                //    color = Color.SlateBlue;
                //}

                color = texture.ReadTexture(uIndex, vIndex);
                DrawPoint(i, y, color);
            }
        }