示例#1
0
        public void DrawTopTriangle(Vertex v1, Vertex v2, Vertex v3, Scene scene, VertexTriangle vt, VertexTriangle oriVt)
        {
            Vector4 p1 = v1.ScreenSpacePosition;
            Vector4 p2 = v2.ScreenSpacePosition;
            Vector4 p3 = v3.ScreenSpacePosition;

            Color4 color1 = v1.Color;
            Color4 color2 = v2.Color;
            Color4 color3 = v3.Color;

            float ax = p1.X;
            float bx = p2.X;
            float cx = p3.X;
            float ay = p1.Y;
            float by = p2.Y;
            float cy = p3.Y;

            float az = p1.Z;
            float bz = p2.Z;
            float cz = p3.Z;

            Vector4 normal1 = v1.nowNormal;
            Vector4 normal2 = v2.nowNormal;
            Vector4 normal3 = v3.nowNormal;

            oriVt.PreCalWeight();

            if (bx == cx)
            {
                for (int y = (int)ay; y <= (int)cy; y++)
                {
                    float y1 = (float)(y - ay) / (float)(cy - ay);
                    float y2 = (float)(y - by) / (float)(cy - by);

                    y1 = MathUtil.Clamp01(y1);
                    y2 = MathUtil.Clamp01(y2);

                    int x0 = (int)MathUtil.Interp(ax, cx, y1);
                    int x1 = (int)bx;

                    float z1 = MathUtil.Interp(az, cz, y1);
                    float z2 = bz;

                    Color4 c1 = new Color4();
                    Color4 c2 = new Color4();

                    if (scene.renderState == Scene.RenderState.GouraduShading)
                    {
                        c1 = MathUtil.ColorInterp(color1, color3, y1);
                        c2 = MathUtil.ColorInterp(color2, color3, y2);
                    }

                    Vector4 n1 = new Vector4(0, 0, 0, 0);
                    Vector4 n2 = new Vector4(0, 0, 0, 0);

                    if (DirectionLight.IsEnable)
                    {
                        n1 = MathUtil.Vector4Interp(normal1, normal3, y1);
                        n2 = MathUtil.Vector4Interp(normal2, normal3, y2);
                    }

                    for (int x = x0; x <= x1; x++)
                    {
                        float r3 = (float)(x - x0) / (float)(x1 - x0);

                        r3 = MathUtil.Clamp01(r3);

                        float z = MathUtil.Interp(z1, z2, r3);

                        Vector4 pos = new Vector4(x, y, z, 0);

                        switch (scene.renderState)
                        {
                        case Scene.RenderState.WireFrame:
                        { }
                        break;

                        case Scene.RenderState.GouraduShading:
                        {
                            oriVt.CalWeight(pos);
                            Color4 c3 = MathUtil.ColorInterp(c1, c2, r3);

                            if (DirectionLight.IsEnable)
                            {
                                Vector4 n3 = MathUtil.Vector4Interp(n1, n2, r3);
                                n3    = oriVt.GetNormal();
                                final = DirectionLight.GetFinalLightColor(n3, c3);
                            }
                            else
                            {
                                final = c3;
                            }
                        }
                        break;

                        case Scene.RenderState.TextureMapping:
                        {
                            oriVt.CalWeight(pos);

                            Vector4 n3 = MathUtil.Vector4Interp(n1, n2, r3);

                            Vector4 uv = oriVt.GetInterUV();

                            if (DirectionLight.IsEnable)
                            {
                                Color4 c = device.Tex2D(uv.X, uv.Y, scene.mesh.texture);
                                n3    = oriVt.GetNormal();
                                final = DirectionLight.GetFinalLightColor(n3, c);
                            }
                            else
                            {
                                final = device.Tex2D(uv.X, uv.Y, scene.mesh.texture);
                            }
                        }
                        break;
                        }
                        device.DrawPoint(pos, final);
                    }
                }
            }
            else if (ax == cx)
            {
                for (int y = (int)ay; y <= (int)cy; y++)
                {
                    float y1 = (float)(y - ay) / (float)(cy - ay);
                    float y2 = (float)(y - by) / (float)(cy - by);
                    y1 = MathUtil.Clamp01(y1);
                    y2 = MathUtil.Clamp01(y2);

                    int x0 = (int)ax;
                    int x1 = (int)MathUtil.Interp(bx, cx, y2);

                    float z1 = MathUtil.Interp(az, cz, y1);
                    float z2 = MathUtil.Interp(bz, cz, y2);

                    Color4 c1 = new Color4();
                    Color4 c2 = new Color4();

                    if (scene.renderState == Scene.RenderState.GouraduShading)
                    {
                        c1 = MathUtil.ColorInterp(color1, color3, y1);
                        c2 = MathUtil.ColorInterp(color2, color3, y2);
                    }

                    Vector4 n1 = new Vector4(0, 0, 0, 0);
                    Vector4 n2 = new Vector4(0, 0, 0, 0);

                    if (DirectionLight.IsEnable)
                    {
                        n1 = MathUtil.Vector4Interp(normal1, normal3, y1);
                        n2 = MathUtil.Vector4Interp(normal2, normal3, y2);
                    }

                    for (int x = x0; x < x1; x++)
                    {
                        float r3 = (float)(x - x0) / (float)(x1 - x0);

                        r3 = MathUtil.Clamp01(r3);

                        float z = MathUtil.Interp(z1, z2, r3);

                        Vector4 pos = new Vector4(x, y, z, 0);

                        switch (scene.renderState)
                        {
                        case Scene.RenderState.WireFrame:
                        { }
                        break;

                        case Scene.RenderState.GouraduShading:
                        {
                            oriVt.CalWeight(pos);
                            Color4 c3 = MathUtil.ColorInterp(c1, c2, r3);

                            if (DirectionLight.IsEnable)
                            {
                                Vector4 n3 = MathUtil.Vector4Interp(n1, n2, r3);
                                n3    = oriVt.GetNormal();
                                final = DirectionLight.GetFinalLightColor(n3, c3);
                            }
                            else
                            {
                                final = c3;
                            }
                        }
                        break;

                        case Scene.RenderState.TextureMapping:
                        {
                            oriVt.CalWeight(pos);

                            Vector4 n3 = MathUtil.Vector4Interp(n1, n2, r3);

                            Vector4 uv = oriVt.GetInterUV();

                            if (DirectionLight.IsEnable)
                            {
                                Color4 c = device.Tex2D(uv.X, uv.Y, scene.mesh.texture);
                                n3    = oriVt.GetNormal();
                                final = DirectionLight.GetFinalLightColor(n3, c);
                            }
                            else
                            {
                                final = device.Tex2D(uv.X, uv.Y, scene.mesh.texture);
                            }
                        }
                        break;
                        }
                        device.DrawPoint(pos, final);
                    }
                }
            }
            else
            {
                for (int y = (int)ay; y <= cy; y++)
                {
                    float y1 = (float)(y - ay) / (float)(cy - ay);
                    y1 = MathUtil.Clamp01(y1);

                    float y2 = (y - by) / (cy - by);
                    y2 = MathUtil.Clamp01(y2);

                    float x0 = (int)MathUtil.Interp(ax, cx, y1);
                    float x1 = (int)MathUtil.Interp(bx, cx, y2);

                    float z1 = MathUtil.Interp(az, cz, y1);
                    float z2 = MathUtil.Interp(bz, cz, y2);

                    Color4 c1 = new Color4();
                    Color4 c2 = new Color4();

                    if (scene.renderState == Scene.RenderState.GouraduShading)
                    {
                        c1 = MathUtil.ColorInterp(color1, color3, y1);
                        c2 = MathUtil.ColorInterp(color2, color3, y2);
                    }
                    Vector4 n1 = new Vector4(0, 0, 0, 0);
                    Vector4 n2 = new Vector4(0, 0, 0, 0);

                    if (DirectionLight.IsEnable)
                    {
                        n1 = MathUtil.Vector4Interp(normal1, normal3, y1);
                        n2 = MathUtil.Vector4Interp(normal2, normal3, y2);
                    }

                    for (float x = x0; x < x1; x++)
                    {
                        float r3 = (float)(x - x0) / (float)(x1 - x0);

                        r3 = MathUtil.Clamp01(r3);

                        float z = MathUtil.Interp(z1, z2, r3);

                        //Color4 c3 = MathUtil.ColorInterp(c1, c2, r3);

                        Vector4 pos = new Vector4(x, y, z, 0);

                        switch (scene.renderState)
                        {
                        case Scene.RenderState.WireFrame:
                        { }
                        break;

                        case Scene.RenderState.GouraduShading:
                        {
                            Color4 c3 = MathUtil.ColorInterp(c1, c2, r3);
                            oriVt.CalWeight(pos);
                            if (DirectionLight.IsEnable)
                            {
                                Vector4 n3 = MathUtil.Vector4Interp(n1, n2, r3);
                                n3    = oriVt.GetNormal();
                                final = DirectionLight.GetFinalLightColor(n3, c3);
                            }
                            else
                            {
                                final = c3;
                            }
                        }
                        break;

                        case Scene.RenderState.TextureMapping:
                        {
                            oriVt.CalWeight(pos);

                            Vector4 n3 = MathUtil.Vector4Interp(n1, n2, r3);

                            Vector4 uv = oriVt.GetInterUV();

                            if (DirectionLight.IsEnable)
                            {
                                Color4 c = device.Tex2D(uv.X, uv.Y, scene.mesh.texture);
                                n3    = oriVt.GetNormal();
                                final = DirectionLight.GetFinalLightColor(n3, c);
                            }
                            else
                            {
                                final = device.Tex2D(uv.X, uv.Y, scene.mesh.texture);
                            }
                        }
                        break;
                        }
                        device.DrawPoint(pos, final);
                    }
                }
            }
        }