Exemple #1
0
        void DrawPlane_1(int vertexIndex1, int vertexIndex2, int vertexIndex3, int vertexIndex4)
        {
            vertex_t v1 = Def.mesh[vertexIndex1], v2 = Def.mesh[vertexIndex2], v3 = Def.mesh[vertexIndex3], v4 = Def.mesh[vertexIndex4];
            vector_t p1, p2, p3, p4, c1, c2, c3, c4;

            c1 = Tools.matrix_apply(v1.pos, mTransformer.transform);
            c2 = Tools.matrix_apply(v2.pos, mTransformer.transform);
            c3 = Tools.matrix_apply(v3.pos, mTransformer.transform);
            c4 = Tools.matrix_apply(v4.pos, mTransformer.transform);

            p1 = Tools.Homogenize(c1);
            p2 = Tools.Homogenize(c2);
            p3 = Tools.Homogenize(c3);
            p4 = Tools.Homogenize(c4);

            point_t _node1 = new point_t((int)p1.x, (int)p1.y);
            point_t _node2 = new point_t((int)p2.x, (int)p2.y);
            point_t _node3 = new point_t((int)p3.x, (int)p3.y);
            point_t _node4 = new point_t((int)p4.x, (int)p4.y);

            DrawLine(_node1, _node2, v1.color);
            DrawLine(_node2, _node3, v2.color);
            DrawLine(_node3, _node4, v3.color);
            DrawLine(_node4, _node1, v4.color);
        }
Exemple #2
0
        public static float Triangle_Acreage_2D(point_t p1, point_t p2, point_t p3)
        {
            float    _tmp    = 0f;
            vector_t _vec1_2 = new vector_t(p2.x - p1.x, p2.y - p1.y, 0, 0);
            vector_t _vec1_3 = new vector_t(p3.x - p1.x, p3.y - p1.y, 0, 0);

            _tmp = Tools.vector_length(Tools.vector_crossproduct(_vec1_2, _vec1_3)) / 2;
            return(Math.Abs(_tmp));
        }
Exemple #3
0
 public void DrawPoint_S(point_t _a, Color _color, int _radio)
 {
     for (int i = _a.x - _radio; i < _a.x + _radio; i++)
     {
         for (int j = _a.y - _radio; j < _a.y + _radio; j++)
         {
             DrawPoint(i, j, _color);
         }
     }
 }
Exemple #4
0
        void Rasterization_DrawLine_Horizontal(point_t _pS, point_t _pE, triangle_2D _triangle)
        {
            int _start = Math.Min(_pS.x, _pE.x);
            int _end   = Math.Max(_pS.x, _pE.x);

            for (int x = _start; x <= _end; x++)
            {
                point_t _cur = new point_t(x, _pS.y);
                Color   _col = Triangle_Interpolation_Color(_cur, _triangle);
                DrawPoint(_cur.x, _cur.y, _col);
            }
        }
Exemple #5
0
        void DrawTriangle(vertex_t v1, vertex_t v2, vertex_t v3)
        {
            vector_t p1, p2, p3, c1, c2, c3;

            // 按照 Transform 变化
            c1 = Tools.matrix_apply(v1.pos, mTransformer.transform);
            c2 = Tools.matrix_apply(v2.pos, mTransformer.transform);
            c3 = Tools.matrix_apply(v3.pos, mTransformer.transform);

            p1 = Tools.Homogenize(c1);
            p2 = Tools.Homogenize(c2);
            p3 = Tools.Homogenize(c3);

            point_t _point1 = new point_t((int)p1.x, (int)p1.y);
            point_t _point2 = new point_t((int)p2.x, (int)p2.y);
            point_t _point3 = new point_t((int)p3.x, (int)p3.y);


            //画线框
            //DrawLine(_point1, _point2, Color.Black);
            //DrawLine(_point2, _point3, Color.Black);
            //DrawLine(_point1, _point3, Color.Black);

            //DrawPoint_S(_point1, Color.Red, 3);
            //DrawPoint_S(_point2, Color.Yellow, 3);
            //DrawPoint_S(_point3, Color.Blue, 3);

            if (TriangleCullingJudge_A(c1, c2, c3))
            {
            }
            else
            {
                //光栅化填充
                triangle_2D _triangle = new triangle_2D();
                _triangle.mPoints[0] = _point1; _triangle.mPoints[1] = _point2; _triangle.mPoints[2] = _point3;
                _triangle.mColors[0] = v1.color; _triangle.mColors[1] = v2.color; _triangle.mColors[2] = v3.color;

                Rasterization_Triangle(_triangle);
            }
        }
Exemple #6
0
        Color Triangle_Interpolation_Color(point_t _c, triangle_2D _triangle)
        {
            Color _tmp = _triangle.mColors[0];

            float[] acreageList = new float[3];
            acreageList[0] = Tools.Triangle_Acreage_2D(_c, _triangle.mPoints[1], _triangle.mPoints[2]);
            acreageList[1] = Tools.Triangle_Acreage_2D(_c, _triangle.mPoints[0], _triangle.mPoints[2]);
            acreageList[2] = Tools.Triangle_Acreage_2D(_c, _triangle.mPoints[0], _triangle.mPoints[1]);
            float _sum = 0f, _r = 0f, _g = 0f, _b = 0f;

            for (int i = 0; i < 3; ++i)
            {
                _sum += acreageList[i];
                _r   += _triangle.mColors[i].R * acreageList[i];
                _g   += _triangle.mColors[i].G * acreageList[i];
                _b   += _triangle.mColors[i].B * acreageList[i];
            }
            _tmp = Color.FromArgb(Tools.CMID((int)(_r / _sum), 0, 255),
                                  Tools.CMID((int)(_g / _sum), 0, 255),
                                  Tools.CMID((int)(_b / _sum), 0, 255));
            return(_tmp);
        }
Exemple #7
0
        public void DrawLine(point_t _a, point_t _b, Color _color)
        {
            if (_a.x == _b.x && _a.y == _b.y)
            {
                DrawPoint(_a, _color);
            }
            else if (_a.x == _b.x)
            {
                int inc = (_a.y <= _b.y) ? 1 : -1;
                for (int y = _a.y; y != _b.y; y += inc)
                {
                    DrawPoint(_a.x, y, _color);
                }
                DrawPoint(_b, _color);
            }
            else if (_a.y == _b.y)
            {
                int inc = (_a.x <= _b.x) ? 1 : -1;
                for (int x = _a.x; x != _b.x; x += inc)
                {
                    DrawPoint(x, _b.y, _color);
                }
                DrawPoint(_b, _color);
            }
            else
            {
                int dx = (_a.x < _b.x) ? _b.x - _a.x : _a.x - _b.x;
                int dy = (_a.y < _b.y) ? _b.y - _a.y : _a.y - _b.y;

                if (dx >= dy)
                {
                    if (_b.x < _a.x)
                    {
                        int tmp1 = _a.x; int tmp2 = _a.y;
                        _a.x = _b.x; _a.y = _b.y;
                        _b.x = tmp1; _b.y = tmp2;
                    }
                    int inc = (_b.y >= _a.y) ? 1 : -1;
                    int rem = 0;
                    for (int x = _a.x, y = _a.y; x <= _b.x; x++)
                    {
                        DrawPoint(x, y, _color);
                        rem += dy;
                        if (rem >= dx)
                        {
                            rem -= dx;
                            y   += inc;
                            DrawPoint(x, y, _color);
                        }
                    }
                    DrawPoint(_b, _color);
                }
                else
                {
                    if (_b.y < _a.y)
                    {
                        int tmp1 = _a.x; int tmp2 = _a.y;
                        _a.x = _b.x; _a.y = _b.y;
                        _b.x = tmp1; _b.y = tmp2;
                    }
                    int inc = (_b.x >= _a.x) ? 1 : -1;
                    int rem = 0;
                    for (int x = _a.x, y = _a.y; y <= _b.y; y++)
                    {
                        DrawPoint(x, y, _color);
                        rem += dx;
                        if (rem >= dy)
                        {
                            rem -= dy;
                            x   += inc;
                            DrawPoint(x, y, _color);
                        }
                    }
                    DrawPoint(_b, _color);
                }
            }
        }
Exemple #8
0
 public void DrawPoint(point_t _a, Color _color)
 {
     DrawPoint(_a.x, _a.y, _color);
 }
Exemple #9
0
        //平底 A
        //平顶 V

        void Rasterization_Triangle(triangle_2D _triangle)
        {
            point_t p1 = _triangle.mPoints[0], p2 = _triangle.mPoints[1], p3 = _triangle.mPoints[2];
            int     x1 = p1.x, y1 = p1.y, x2 = p2.x, y2 = p2.y, x3 = p3.x, y3 = p3.y;

            if (y1 == y2)
            {
                if (y3 <= y1) // A
                {
                    Rasterization_Triangle_A(x3, y3, x1, y1, x2, y2, _triangle);
                }
                else // V
                {
                    Rasterization_Triangle_V(x1, y1, x2, y2, x3, y3, _triangle);
                }
            }
            else if (y1 == y3)
            {
                if (y2 <= y1) // A
                {
                    Rasterization_Triangle_A(x2, y2, x1, y1, x3, y3, _triangle);
                }
                else // V
                {
                    Rasterization_Triangle_V(x1, y1, x3, y3, x2, y2, _triangle);
                }
            }
            else if (y2 == y3)
            {
                if (y1 <= y2) // A
                {
                    Rasterization_Triangle_A(x1, y1, x2, y2, x3, y3, _triangle);
                }
                else // V
                {
                    Rasterization_Triangle_V(x2, y2, x3, y3, x1, y1, _triangle);
                }
            }
            else
            {
                int xtop = 0, ytop = 0, xmiddle = 0, ymiddle = 0, xbottom = 0, ybottom = 0;
                if (y1 < y2 && y2 < y3) // y1 y2 y3
                {
                    xtop    = x1;
                    ytop    = y1;
                    xmiddle = x2;
                    ymiddle = y2;
                    xbottom = x3;
                    ybottom = y3;
                }
                else if (y1 < y3 && y3 < y2) // y1 y3 y2
                {
                    xtop    = x1;
                    ytop    = y1;
                    xmiddle = x3;
                    ymiddle = y3;
                    xbottom = x2;
                    ybottom = y2;
                }
                else if (y2 < y1 && y1 < y3) // y2 y1 y3
                {
                    xtop    = x2;
                    ytop    = y2;
                    xmiddle = x1;
                    ymiddle = y1;
                    xbottom = x3;
                    ybottom = y3;
                }
                else if (y2 < y3 && y3 < y1) // y2 y3 y1
                {
                    xtop    = x2;
                    ytop    = y2;
                    xmiddle = x3;
                    ymiddle = y3;
                    xbottom = x1;
                    ybottom = y1;
                }
                else if (y3 < y1 && y1 < y2) // y3 y1 y2
                {
                    xtop    = x3;
                    ytop    = y3;
                    xmiddle = x1;
                    ymiddle = y1;
                    xbottom = x2;
                    ybottom = y2;
                }
                else if (y3 < y2 && y2 < y1) // y3 y2 y1
                {
                    xtop    = x3;
                    ytop    = y3;
                    xmiddle = x2;
                    ymiddle = y2;
                    xbottom = x1;
                    ybottom = y1;
                }
                int xl; // 长边在ymiddle时的x,来决定长边是在左边还是右边
                xl = (int)((ymiddle - ytop) * (xbottom - xtop) / (ybottom - ytop) + xtop + 0.5f);

                if (xl <= xmiddle) // 左三角形
                {
                    Rasterization_Triangle_A(xtop, ytop, xl, ymiddle, xmiddle, ymiddle, _triangle);
                    Rasterization_Triangle_V(xl, ymiddle, xmiddle, ymiddle, xbottom, ybottom, _triangle);
                }
                else // 右三角形
                {
                    Rasterization_Triangle_A(xtop, ytop, xmiddle, ymiddle, xl, ymiddle, _triangle);
                    Rasterization_Triangle_V(xmiddle, ymiddle, xl, ymiddle, xbottom, ybottom, _triangle);
                }
            }
        }