コード例 #1
0
ファイル: GContext1.cs プロジェクト: bazoocaze/CsFun
        private void RasterizeBox(vec4[] vertices)
        {
            CounterTriangules++;

            ShaderProgram.VaryingIn = VaryingIn;

            float minX = float.PositiveInfinity;
            float minY = float.PositiveInfinity;
            float maxX = float.NegativeInfinity;
            float maxY = float.NegativeInfinity;

            for (int v = 0; v < 3; v++)
            {
                minX = Util.Min(minX, vertices[v].x);
                minY = Util.Min(minY, vertices[v].y);
                maxX = Util.Max(maxX, vertices[v].x);
                maxY = Util.Max(maxY, vertices[v].y);
            }

            minX = Util.Clamp(minX, 0, Viewport.Width - 1);
            minY = Util.Clamp(minY, 0, Viewport.Height - 1);
            maxX = Util.Clamp(maxX, 0, Viewport.Width - 1);
            maxY = Util.Clamp(maxY, 0, Viewport.Height - 1);

            for (int y = (int)minY; y <= maxY; y++)
            {
                for (int x = (int)minX; x <= maxX; x++)
                {
                    float dpz = vertices[0].z;
                    if (dpz < DepthBuffer.GetPoint(x, y))
                    {
                        VaryingIn[0].x = ((float)x - minX) / (maxX - minX);
                        VaryingIn[0].y = ((float)y - minY) / (maxY - minY);

                        if (ShaderProgram.PixelShader())
                        {
                            CounterFragments++;
                            BackBuffer.SetPixel(x, y, ShaderProgram.gl_FragColor);
                            DepthBuffer.SetPoint(x, y, dpz);
                        }
                    }
                }
            }
        }
コード例 #2
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, Viewport.Width);

            int ib = ctx.baseIndex;

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

            vec4 C0 = ctx.bcolors[ib + ctx.iV0];
            vec4 C1 = ctx.bcolors[ib + ctx.iV1];
            vec4 C2 = ctx.bcolors[ib + ctx.iV2];

            vec2 T0 = ctx.btc[ib + ctx.iV0];
            vec2 T1 = ctx.btc[ib + ctx.iV1];
            vec2 T2 = ctx.btc[ib + 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;

            vec4 CE = C0 * R1 + C1 * R4 + C2 * R67;
            vec4 CD = C0 * R0 + C1 * R23 + C2 * R5;
            vec2 TE = T0 * R1 + T1 * R4 + T2 * R67;
            vec2 TD = T0 * R0 + T1 * R23 + T2 * 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;

            vec4 CDE = CD - CE;
            vec2 TDE = TD - TE;

            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;
                if (z >= DepthBuffer.GetPoint(x, y))
                {
                    continue;
                }

                DepthBuffer.SetPoint(x, y, z);

                vec4 vertexColor  = CE + (CDE * dx);
                vec2 textureCoord = TE + (TDE * dx);

                vec4 textureColor = CurrentTexture.GetTexel(textureCoord.x, textureCoord.y);

                vec4 color = vec4.Mix(vertexColor, textureColor, TextureBlendFactor);

                DrawPoint(x, y, Util.ToARGB(color));
            }
        }