Exemple #1
0
        public void Gouraud(IBuffers buffer, Span span)
        {
            var spreadCount = span.XEnd - span.XStart;

            GouraudInterpolator.SetPoint(0, span.ZStart, span.ZEnd);
            GouraudInterpolator.SetPoint(1, span.ColorStart.Red, span.ColorEnd.Red);
            GouraudInterpolator.SetPoint(2, span.ColorStart.Green, span.ColorEnd.Green);
            GouraudInterpolator.SetPoint(3, span.ColorStart.Blue, span.ColorEnd.Blue);
            GouraudInterpolator.SetPoint(4, span.ColorStart.Alpha, span.ColorEnd.Alpha);

            var yOffset           = buffer.FrameBuffer.Stride * span.Y;
            var rOffset           = buffer.FrameBuffer.RedPosition;
            var gOffset           = buffer.FrameBuffer.GreenPosition;
            var bOffset           = buffer.FrameBuffer.BluePosition;
            var aOffset           = buffer.FrameBuffer.AlphaPosition;
            var bufferOffset      = yOffset + span.XStart;
            var depthBufferOffset = (buffer.Width * span.Y) + span.XStart;

            GouraudInterpolator.Interpolate(spreadCount);

            var xOffset = span.XStart;

            for (var index = 0; index < spreadCount; index++)
            {
                if (xOffset >= 0 && xOffset < buffer.Width)
                {
                    var z       = GouraudInterpolator.Points[0].InterpolatedValues[index];
                    var bufferZ = (UInt32)(z * (float)UInt32.MaxValue);


                    if (bufferZ < buffer.DepthBuffer[depthBufferOffset] &&
                        z >= 0f &&
                        z < 1f
                        )
                    {
                        buffer.DepthBuffer[depthBufferOffset] = bufferZ;

                        ColorAsVector.Red   = GouraudInterpolator.Points[1].InterpolatedValues[index];
                        ColorAsVector.Green = GouraudInterpolator.Points[2].InterpolatedValues[index];
                        ColorAsVector.Blue  = GouraudInterpolator.Points[3].InterpolatedValues[index];
                        ColorAsVector.Alpha = GouraudInterpolator.Points[4].InterpolatedValues[index];
                        var color = (int)ColorAsVector.ToColor().ToUInt32();
                        buffer.FrameBuffer.Pixels[bufferOffset] = color;
                    }
                }

                xOffset++;
                bufferOffset++;

                depthBufferOffset++;
            }
        }
Exemple #2
0
        public void Texture(IBuffers buffer, Span span, Image image, ImageContext texture)
        {
            var spreadCount = span.XEnd - span.XStart;

            TextureInterpolator.SetPoint(0, span.ZStart, span.ZEnd);
            TextureInterpolator.SetPoint(1, span.UStart, span.UEnd);
            TextureInterpolator.SetPoint(2, span.VStart, span.VEnd);
            var yOffset           = span.Y * buffer.FrameBuffer.Stride;
            var rOffset           = buffer.FrameBuffer.RedPosition;
            var gOffset           = buffer.FrameBuffer.GreenPosition;
            var bOffset           = buffer.FrameBuffer.BluePosition;
            var aOffset           = buffer.FrameBuffer.AlphaPosition;
            var bufferOffset      = yOffset + span.XStart;
            var depthBufferOffset = (buffer.Width * span.Y) + span.XStart;

            TextureInterpolator.Interpolate(spreadCount);

            var xOffset = span.XStart;

            for (var index = 0; index < spreadCount; index++)
            {
                if (xOffset >= 0 && xOffset < buffer.Width)
                {
                    var z       = TextureInterpolator.Points[0].InterpolatedValues[index];
                    var bufferZ = (UInt32)(z * (float)UInt32.MaxValue);


                    var u = TextureInterpolator.Points[1].InterpolatedValues[index];
                    var v = TextureInterpolator.Points[2].InterpolatedValues[index];

                    var intu = (int)(u * image.Width) & (image.Width - 1);
                    var intv = (int)(v * image.Height) & (image.Height - 1);

                    var texel = ((intv * image.Width) + intu);


                    if (bufferZ < buffer.DepthBuffer[depthBufferOffset] &&
                        z >= 0f &&
                        z < 1f
                        )
                    {
                        buffer.FrameBuffer.Pixels[bufferOffset] = texture.Pixels[texel];

                        buffer.DepthBuffer[depthBufferOffset] = bufferZ;
                    }
                }

                bufferOffset++;
                depthBufferOffset++;
                xOffset++;
            }
        }
Exemple #3
0
        private static void RenderScaled(IBuffers buffer, int positionOffset, Image image, Vector translatedPosition, int bufferSize, UInt32 bufferZ, float xScale, float yScale)
        {
            var rOffset      = buffer.FrameBuffer.RedPosition;
            var gOffset      = buffer.FrameBuffer.GreenPosition;
            var bOffset      = buffer.FrameBuffer.BluePosition;
            var aOffset      = buffer.FrameBuffer.AlphaPosition;
            var imageContext = image.ImageContext as ImageContext;

            var actualWidth  = (int)(((float)image.Width) * xScale);
            var actualHeight = (int)(((float)image.Height) * yScale);

            if (actualWidth <= 0 || actualHeight <= 0)
            {
                return;
            }

            var spriteOffset = 0;

            XScalingInterpolator.SetPoint(0, 0f, image.Width);
            XScalingInterpolator.Interpolate(actualWidth);

            YScalingInterpolator.SetPoint(0, 0f, image.Height);
            YScalingInterpolator.Interpolate(actualHeight);


            for (var y = 0; y < actualHeight; y++)
            {
                var offset            = y * buffer.FrameBuffer.Stride;
                var depthBufferOffset = (buffer.Width * ((int)translatedPosition.Y + y)) + (int)translatedPosition.X;

                var spriteY = (int)YScalingInterpolator.Points[0].InterpolatedValues[y];

                for (var x = 0; x < actualWidth; x++)
                {
                    var actualOffset = offset + positionOffset;

                    var spriteX = (int)XScalingInterpolator.Points[0].InterpolatedValues[x];
                    spriteOffset = (int)((spriteY * image.Width) + spriteX);

                    if (actualOffset >= 0 && actualOffset < bufferSize &&
                        bufferZ < buffer.DepthBuffer[depthBufferOffset])
                    {
                        buffer.FrameBuffer.Pixels[actualOffset] = imageContext.Pixels[spriteOffset];
                        buffer.DepthBuffer[depthBufferOffset]   = bufferZ;
                    }
                    offset++;

                    depthBufferOffset++;
                }
            }
        }
Exemple #4
0
        public void Flat(IBuffers buffer, Span span, Color color)
        {
            var spreadCount = span.Length;             //span.XEnd - span.XStart;

            DepthInterpolator.SetPoint(0, span.ZStart, span.ZEnd);
            var yOffset           = span.Y * buffer.FrameBuffer.Stride;
            var rOffset           = buffer.FrameBuffer.RedPosition;
            var gOffset           = buffer.FrameBuffer.GreenPosition;
            var bOffset           = buffer.FrameBuffer.BluePosition;
            var aOffset           = buffer.FrameBuffer.AlphaPosition;
            var bufferOffset      = yOffset + span.XStart;
            var depthBufferOffset = (buffer.Width * span.Y) + span.XStart;

            DepthInterpolator.Interpolate(spreadCount);

            var colorAsInt = (int)color.ToUInt32();

            var xOffset = span.XStart;

            for (var index = 0; index < spreadCount; index++)
            {
                if (xOffset >= 0 && xOffset < buffer.Width)
                {
                    var z       = DepthInterpolator.Points[0].InterpolatedValues[index];
                    var bufferZ = (UInt32)(z * (float)UInt32.MaxValue);

                    if (bufferZ < buffer.DepthBuffer[depthBufferOffset] &&
                        z >= 0f &&
                        z < 1f
                        )
                    {
                        buffer.FrameBuffer.Pixels[bufferOffset] = colorAsInt;
                        buffer.DepthBuffer[depthBufferOffset]   = bufferZ;
                    }
                    else
                    {
                        int i = 0;
                        i++;
                    }
                }

                xOffset++;
                bufferOffset++;
                depthBufferOffset++;
            }
        }