Example #1
0
        public static unsafe void TestRasterize()
        {
            Rasterizer.StartRasterize(CRenderSettings.ResolutionF);
            RenderBuffer <float>     tex0    = new RenderBuffer <float>(CRenderSettings.Resolution, 3);
            CharRenderBuffer <float> texChar = new CharRenderBuffer <float>(tex0);
            Vector2 *points = stackalloc Vector2[4];

            for (float i = 0; i < JMath.PI_TWO; i += .02f)
            {
                Vector2 dir      = new Vector2(MathF.Cos(i) * .3f, MathF.Sin(i) * .3f);
                Vector2 orthoDir = new Vector2(-dir.Y, dir.X);
                points[0] = new Vector2(.5f, .5f) + dir;
                points[1] = new Vector2(.5f, .5f) - dir;
                points[2] = new Vector2(.5f, .5f) + orthoDir;
                points[3] = new Vector2(.5f, .5f) - orthoDir;
                LinePrimitive *line = stackalloc LinePrimitive[1] {
                    new LinePrimitive(points, null, 0)
                };
                Rasterizer.Rasterize <Line, LinePrimitive>(line);
                tex0.WritePixel(Rasterizer.ContriveResult(), new GenericVector <float>(3)
                {
                    1f, 1f, 1f
                });
                CRenderer.Render(texChar);
                tex0.Clear();
                Thread.Sleep(16);
            }

            Rasterizer.EndRasterize();
        }
Example #2
0
        public static unsafe void TestRasterizerLine()
        {
            Vector2 *coords = stackalloc Vector2[2] {
                Vector2.Zero, Vector2.One
            };

            Rasterizer.StartRasterize(new Vector2(100, 100));
            LinePrimitive *line = stackalloc LinePrimitive[1] {
                new LinePrimitive(coords, null, 0)
            };

            Rasterizer.Rasterize <Line, LinePrimitive>(line);
            var resultArr = Rasterizer.ContriveResult();

            Assert.AreEqual(resultArr.Length, 100, 1);
            Rasterizer.EndRasterize();
        }
    }
Example #3
0
        public void Rasterize(LinePrimitive *linePtr)
        {
            Vector2 *verticesPtr = linePtr->CoordsPtr;
            float ** verticesDataPtr = linePtr->VerticesDataPtr;
            int      verticesDataCount = linePtr->VerticesDataCount;
            Vector2  from = verticesPtr[0] * Resolution, to = verticesPtr[1] * Resolution;
            float    xSub = to.X - from.X,
                     ySub = to.Y - from.Y;

            //0: X-major 1:Y-major
            int   dir;
            int   dirStep, otherDirStep;
            float slopeAbs;

            if (Math.Abs(xSub) >= Math.Abs(ySub))
            {
                dir = 0;
            }
            else
            {
                dir = 1;
                float temp = xSub;
                xSub = ySub;
                ySub = temp;
            }
            dirStep      = Math.Sign(xSub);
            otherDirStep = Math.Sign(ySub);
            slopeAbs     = Math.Abs(ySub / xSub);

            bool ifInterpolate = verticesDataCount > 0;

            if (ifInterpolate)
            {
                LineInterpolator.SetInterpolation(verticesDataPtr[0], verticesDataPtr[1], verticesDataCount, Math.Abs(xSub));
            }

            Vector2Int resultPoint = new Vector2Int(JMath.RoundToInt(from.X), JMath.RoundToInt(from.Y));

            if (resultPoint.X == Resolution.X)
            {
                resultPoint.X--;
            }
            if (resultPoint.Y == Resolution.Y)
            {
                resultPoint.Y--;
            }

            //End coordinate in Int
            int end = JMath.RoundToInt(to[dir]);

            for (float otherDirFrac = slopeAbs; resultPoint[dir] != end; otherDirFrac += slopeAbs, resultPoint[dir] += dirStep)
            {
                if (ifInterpolate)
                {
                    RasterizerEntry.OutputRasterization(resultPoint, LineInterpolator.InterpolatedValues);
                    LineInterpolator.IncrementStep();
                }
                else
                {
                    RasterizerEntry.OutputRasterization(resultPoint, LineInterpolator.InterpolatedValues);
                }
                if (otherDirFrac >= 1f)
                {
                    resultPoint[1 - dir] += otherDirStep;
                    otherDirFrac--;
                }
            }
        }