コード例 #1
0
        private void DrawScanLine(PrimitiveContext ctx, int y, float xe, float xd)
        {
            int xmin = glm.Max((int)xe, 0);
            int xmax = glm.Min((int)xd, (int)m_CurrentViewport.width);

            int ib = ctx.baseIndex;

            float w0 = ctx.w[ctx.iV0];
            float w1 = ctx.w[ctx.iV1];
            float w2 = ctx.w[ctx.iV2];

            float dn01 = Linear2Homo(ctx.dfd01, w0, w1);
            float dn12 = Linear2Homo(ctx.dfd12, w1, w2);
            float dn02 = Linear2Homo(ctx.dfe02, w0, w2);
            float dn21 = Linear2Homo(ctx.dfe21, w2, w1);

            float dr01 = 1f - dn01;
            float dr12 = 1f - dn12;
            float dr02 = 1f - dn02;
            float dr21 = 1f - dn21;

            float bn012 = ctx.dfd012;
            float bn021 = ctx.dfe021;
            float br012 = 1f - bn012;
            float br021 = 1f - bn021;

            float R0  = dr01 * br012;
            float R1  = dr02 * br021;
            float R23 = dn01 * br012 + dr12 * bn012;
            float R4  = dn21 * bn021;
            float R5  = dn12 * bn012;
            float R67 = dn02 * br021 + dr21 * bn021;

            float wLeft  = w0 * R1 + w1 * R4 + w2 * R67;
            float wRight = w0 * R0 + w1 * R23 + w2 * R5;

            float zLeft  = ctx.v[ctx.iV0].z * R1 + ctx.v[ctx.iV1].z * R4 + ctx.v[ctx.iV2].z * R67;
            float zRight = ctx.v[ctx.iV0].z * R0 + ctx.v[ctx.iV1].z * R23 + ctx.v[ctx.iV2].z * R5;

            float deltaDx = (xd != xe) ? 1f / (float)(xd - xe) : 0f;

            for (int x = xmin; x <= xmax; x++)
            {
                float dx = (float)(x - xe) * deltaDx;

                dx = Linear2Homo(dx, wLeft, wRight);

                float z = zLeft + (zRight - zLeft) * dx;

                internal_FragCoord.x = x;
                internal_FragCoord.y = y;

                if (m_DepthBuffer.IsDepthTestEnabled())
                {
                    if (!m_DepthBuffer.TestDepth(internal_FragCoord, z))
                    {
                        continue;
                    }
                }
                m_DepthBuffer.WriteDepth(internal_FragCoord, z);

                ctx.RDX[ctx.iV0] = R1 + ((R0 - R1) * dx);
                ctx.RDX[ctx.iV1] = R4 + ((R23 - R4) * dx);
                ctx.RDX[ctx.iV2] = R67 + ((R5 - R67) * dx);

                gl_FragCoord.x = x;
                gl_FragCoord.y = y;
                gl_FragCoord.z = z;
                gl_FragCoord.w = 1;                 // TODO: gl_FragCoord.w = 1/w ?

                // RUN FRAGMENT SHADER
                // RunStage(StageType.FragmentShader);
                m_CompiledPipelineProgram.FragmentShader();
            }
        }