private void ScanLine(int sx, int ex, int y, float sz, float ez)
        {
            for (int x = sx; x <= ex; x++)
            {
                float z = Mathf.Reciprocal(LerpZ(sx, ex, x, sz, ez));

                Vector4 pa = _a.position;
                Vector4 pb = _b.position;
                Vector4 pc = _c.position;
                //计算每个像素的重心坐标
                float t = ((pb.y - pc.y) * x + (pc.x - pb.x) * y + (pb.x * pc.y - pc.x * pb.y)) / ((pb.y - pc.y) * pa.x + (pc.x - pb.x) * pa.y + (pb.x * pc.y - pc.x * pb.y));
                float s = ((pa.y - pc.y) * x + (pc.x - pa.x) * y + (pa.x * pc.y - pc.x * pa.y)) / ((pa.y - pc.y) * pb.x + (pc.x - pa.x) * pb.y + (pa.x * pc.y - pc.x * pa.y));
                float w = 1 - t - s;
                //插值color和uv
                Color4   c  = _a.color * t + _b.color * s + _c.color * w;
                TexCoord uv = _a.uv * t + _b.uv * s + _c.uv * w;

                Fragment fragment = new Fragment(x, y, z, c, uv);
                fragments.Add(fragment);
            }
        }
예제 #2
0
        public Color4 Sample(TexCoord uv)
        {
            byte r = 255;
            byte g = 255;
            byte b = 255;
            byte a = 255;

            int x = (int)(uv.u * _bitmapData.Width);
            int y = (int)(uv.v * _bitmapData.Height) * _bitmapData.Stride;

            if (_bitmapData.PixelFormat == PixelFormat.Format24bppRgb)
            {
                unsafe
                {
                    int   p   = y + (x * 3);
                    byte *ptr = (byte *)_bitmapData.Scan0.ToPointer();
                    r = ptr[p + 2];
                    g = ptr[p + 1];
                    b = ptr[p + 0];
                }
            }
            else if (_bitmapData.PixelFormat == PixelFormat.Format32bppArgb)
            {
                unsafe
                {
                    int   p   = y + (x * 4);
                    byte *ptr = (byte *)_bitmapData.Scan0.ToPointer();
                    r = ptr[p * 4 + 3];
                    g = ptr[p * 4 + 2];
                    b = ptr[p * 4 + 1];
                    a = ptr[p * 4 + 0];
                }
            }

            return(new Color4(r, g, b, a));
        }