All pastes #243526 Raw Edit

rast

public text v1 · immutable
·published 2006-11-08 16:25 UTC
rendered paste body
            Generic.cPipelineElement3X<sVert, sVert>.ElementProcessor3X rasterizerProcessor = delegate(sVert[] element)
            {
                sVert v1 = element[0];
                sVert v2 = element[1];
                sVert v3 = element[2];

                sVert tmp;

                if (v1.Position.Y > v2.Position.Y)
                {
                    // SWAP
                    tmp = v1;
                    v1 = v2;
                    v2 = tmp;
                }
                if (v2.Position.Y > v3.Position.Y)
                {
                    // SWAP
                    tmp = v2;
                    v2 = v3;
                    v3 = tmp;
                }
                if (v1.Position.Y > v2.Position.Y)
                {
                    // SWAP
                    tmp = v1;
                    v1 = v2;
                    v2 = tmp;
                }

                v1.Position = v1.Position * 200 + new Vector3(100, 100, 0.0f);
                v2.Position = v2.Position * 200 + new Vector3(100, 100, 0.0f);
                v3.Position = v3.Position * 200 + new Vector3(100, 100, 0.0f);

                int currY = (int)Math.Round(v1.Position.Y);
                int goalY = (int)Math.Round(v2.Position.Y);
                int maxY = (int)Math.Round(v3.Position.Y);

                float factor1 = (v2.Position.X - v1.Position.X) / (v2.Position.Y - v1.Position.Y);
                float factor2 = (v3.Position.X - v1.Position.X) / (v3.Position.Y - v1.Position.Y);
                float factor3 = (v3.Position.X - v2.Position.X) / (v3.Position.Y - v2.Position.Y);

                float zFactor1 = (v2.Position.Z - v1.Position.Z) / (v2.Position.Y - v1.Position.Y);
                float zFactor2 = (v3.Position.Z - v1.Position.Z) / (v3.Position.Y - v1.Position.Y);
                float zFactor3 = (v3.Position.Z - v2.Position.Z) / (v3.Position.Y - v2.Position.Y);

                Vector4 cFactor1 = (v2.Color - v1.Color) * (1.0f / (v2.Position.Y - v1.Position.Y));
                Vector4 cFactor2 = (v3.Color - v1.Color) * (1.0f / (v3.Position.Y - v1.Position.Y));
                Vector4 cFactor3 = (v3.Color - v2.Color) * (1.0f / (v3.Position.Y - v2.Position.Y));

                Vector4 leftColor = v1.Color;
                Vector4 rightColor = v1.Color;

                float leftZ = v1.Position.Z;
                float rightZ = v1.Position.Z;

                while (currY < goalY)
                {
                    int start = (int)(v1.Position.X + ((float)currY - v1.Position.Y) * factor1);
                    leftColor += cFactor1;
                    rightColor += cFactor2;

                    leftZ += zFactor1;
                    rightZ += zFactor2;
                    //Rasterizer.Output(new Vector3(start, currY, 0.0f));
                    int end = (int)(v1.Position.X + ((float)currY - v1.Position.Y) * factor2);
                    int count = start;
                    if (start < end)
                        while (count++ < end)
                        {
                            float lerpVal = (count - start) / (float)(end - start);
                            Rasterizer.Output(new sVert(new Vector3(count, currY, leftZ + (leftZ - rightZ) * lerpVal), Vector4.Lerp(leftColor, rightColor, lerpVal)));
                        }
                    else if (end < start)
                        while (count-- > end)
                        {
                            float lerpVal = (count - start) / (float)(end - start);
                            Rasterizer.Output(new sVert(new Vector3(count, currY, leftZ + (leftZ - rightZ) * lerpVal), Vector4.Lerp(leftColor, rightColor, lerpVal)));
                        }
                    currY++;
                }
                while (currY < maxY)
                {
                    int start = (int)(v2.Position.X + ((float)currY - v2.Position.Y) * factor3);
                    leftColor += cFactor3;
                    rightColor += cFactor2;
                    leftZ += zFactor3;
                    rightZ += zFactor2;
                    //Rasterizer.Output(new Vector3(start, currY, 0.0f));
                    int end = (int)(v1.Position.X + ((float)currY - v1.Position.Y) * factor2);
                    int count = start;
                    if (start < end)
                        while (count++ < end)
                        {
                            float lerpVal = (count - start) / (float)(end - start);
                            Rasterizer.Output(new sVert(new Vector3(count, currY, leftZ + (leftZ - rightZ) * lerpVal), Vector4.Lerp(leftColor, rightColor, lerpVal)));
                        }
                    else if (end < start)
                        while (count-- > end)
                        {
                            float lerpVal = (count - start) / (float)(end - start);
                            Rasterizer.Output(new sVert(new Vector3(count, currY, leftZ + (leftZ - rightZ) * lerpVal), Vector4.Lerp(leftColor, rightColor, lerpVal)));
                        }

                    currY++;
                }