コード例 #1
0
ファイル: RenderPipeline.cs プロジェクト: xstos/idx3dsharp
        void enqueueTriangle(Triangle tri)
        {
            if (tri.getParent().material == null)
            {
                return;
            }
            if (tri.visible == false)
            {
                return;
            }
            if ((tri.getParent().material.transparency == 255) && (tri.getParent().material.reflectivity == 0))
            {
                return;
            }

            if (tri.getParent().material.transparency > 0)
            {
                transparentQueue.Add(tri);
            }
            else
            {
                opaqueQueue.Add(tri);
            }
        }
コード例 #2
0
ファイル: Rasterizer.cs プロジェクト: xstos/idx3dsharp
        public void Render(Triangle tri)
        {
            if (!ready)
            {
                return;
            }
            if (tri.getParent() == null)
            {
                return;
            }
            if ((mode & Wireframe) != 0)
            {
                drawWireframe(tri, color);
                if ((mode & Wireframe) == 0)
                {
                    return;
                }
            }

            p1 = tri.p1;
            p2 = tri.p2;
            p3 = tri.p3;

            if (p1.Y > p2.Y)
            {
                tempVertex = p1; p1 = p2; p2 = tempVertex;
            }
            if (p2.Y > p3.Y)
            {
                tempVertex = p2; p2 = p3; p3 = tempVertex;
            }
            if (p1.Y > p2.Y)
            {
                tempVertex = p1; p1 = p2; p2 = tempVertex;
            }

            if (p1.Y >= height)
            {
                return;
            }
            if (p3.Y < 0)
            {
                return;
            }
            if (p1.Y == p3.Y)
            {
                return;
            }

            if (mode == Flat)
            {
                lutID        = (int)(tri.n2.X * 127 + 127) + ((int)(tri.n2.Y * 127 + 127) << 8);
                c            = ColorUtility.multiply(color, diffuse[lutID]);
                s            = ColorUtility.scale(specular[lutID], reflectivity);
                currentColor = ColorUtility.add(c, s);
            }

            currentId = (tri.getParent().id << 16) | tri.id;

            x1 = p1.X << 8;
            x2 = p2.X << 8;
            x3 = p3.X << 8;
            y1 = p1.Y;
            y2 = p2.Y;
            y3 = p3.Y;

            x4   = x1 + (x3 - x1) * (y2 - y1) / (y3 - y1);
            x1 <<= 8; x2 <<= 8; x3 <<= 8; x4 <<= 8;

            z1  = p1.Z;
            z2  = p2.Z;
            z3  = p3.Z;
            nx1 = p1.nx << 16;
            nx2 = p2.nx << 16;
            nx3 = p3.nx << 16;
            ny1 = p1.ny << 16;
            ny2 = p2.ny << 16;
            ny3 = p3.ny << 16;
            tx1 = p1.tx << 16;
            tx2 = p2.tx << 16;
            tx3 = p3.tx << 16;
            ty1 = p1.ty << 16;
            ty2 = p2.ty << 16;
            ty3 = p3.ty << 16;

            dx = (x4 - x2) >> 16;
            if (dx == 0)
            {
                return;
            }

            temp = 256 * (y2 - y1) / (y3 - y1);

            z4  = z1 + ((z3 - z1) >> 8) * temp;
            nx4 = nx1 + ((nx3 - nx1) >> 8) * temp;
            ny4 = ny1 + ((ny3 - ny1) >> 8) * temp;
            tx4 = tx1 + ((tx3 - tx1) >> 8) * temp;
            ty4 = ty1 + ((ty3 - ty1) >> 8) * temp;

            dz  = (z4 - z2) / dx;
            dnx = (nx4 - nx2) / dx;
            dny = (ny4 - ny2) / dx;
            dtx = (tx4 - tx2) / dx;
            dty = (ty4 - ty2) / dx;


            if (dx < 0)
            {
                temp = x2; x2 = x4; x4 = temp;
                z2   = z4;
                tx2  = tx4;
                ty2  = ty4;
                nx2  = nx4;
                ny2  = ny4;
            }
            if (y2 >= 0)
            {
                dy = y2 - y1;
                if (dy != 0)
                {
                    dxL     = (x2 - x1) / dy;
                    dxR     = (x4 - x1) / dy;
                    dzBase  = (z2 - z1) / dy;
                    dnxBase = (nx2 - nx1) / dy;
                    dnyBase = (ny2 - ny1) / dy;
                    dtxBase = (tx2 - tx1) / dy;
                    dtyBase = (ty2 - ty1) / dy;
                }

                xBase  = x1;
                xMax   = x1;
                zBase  = z1;
                nxBase = nx1;
                nyBase = ny1;
                txBase = tx1;
                tyBase = ty1;

                if (y1 < 0)
                {
                    xBase  -= y1 * dxL;
                    xMax   -= y1 * dxR;
                    zBase  -= y1 * dzBase;
                    nxBase -= y1 * dnxBase;
                    nyBase -= y1 * dnyBase;
                    txBase -= y1 * dtxBase;
                    tyBase -= y1 * dtyBase;
                    y1      = 0;
                }

                y2     = (y2 < height) ? y2 : height;
                offset = y1 * width;
                for (y = y1; y < y2; y++)
                {
                    renderLine();
                }
            }

            if (y2 < height)
            {
                dy = y3 - y2;
                if (dy != 0)
                {
                    dxL     = (x3 - x2) / dy;
                    dxR     = (x3 - x4) / dy;
                    dzBase  = (z3 - z2) / dy;
                    dnxBase = (nx3 - nx2) / dy;
                    dnyBase = (ny3 - ny2) / dy;
                    dtxBase = (tx3 - tx2) / dy;
                    dtyBase = (ty3 - ty2) / dy;
                }

                xBase  = x2;
                xMax   = x4;
                zBase  = z2;
                nxBase = nx2;
                nyBase = ny2;
                txBase = tx2;
                tyBase = ty2;

                if (y2 < 0)
                {
                    xBase  -= y2 * dxL;
                    xMax   -= y2 * dxR;
                    zBase  -= y2 * dzBase;
                    nxBase -= y2 * dnxBase;
                    nyBase -= y2 * dnyBase;
                    txBase -= y2 * dtxBase;
                    tyBase -= y2 * dtyBase;
                    y2      = 0;
                }

                y3     = (y3 < height) ? y3 : height;
                offset = y2 * width;

                for (y = y2; y < y3; y++)
                {
                    renderLine();
                }
            }
        }