Beispiel #1
0
        public void DrawTriangle(Vector3 v1, Vector3 v2, Vector3 v3, Color frontFaceColor, Color backFaceColor, float[,] matrix)
        {
            v1 = v1.Transform(matrix);
            v2 = v2.Transform(matrix);
            v3 = v3.Transform(matrix);
            Vector3 normal = Vector3.Cross(v1 - v2, v1 - v3);
            normal.Normalize();

            //set color or return for culling
            Color color = frontFaceColor;
            if (normal.Z > 0)
                color = backFaceColor;
            if (color == Color.Transparent)
                return;
            if (EnableShading)
            {
                float shading = 0.5f + 0.5f * Math.Abs(normal.Z);
                color.R = (byte)(color.R * shading);
                color.G = (byte)(color.G * shading);
                color.B = (byte)(color.B * shading);
            }

            Vector2 min = new Vector2();
            Vector2 max = new Vector2();
            min.X = Math.Min(v1.X, Math.Min(v2.X, v3.X));
            min.Y = Math.Min(v1.Y, Math.Min(v2.Y, v3.Y));
            max.X = Math.Max(v1.X, Math.Max(v2.X, v3.X));
            max.Y = Math.Max(v1.Y, Math.Max(v2.Y, v3.Y));

            min = min.ClampVector2(Vector2.Zero, ScreenSize.ToVector2());
            max = max.ClampVector2(Vector2.Zero, ScreenSize.ToVector2());

            Vector3[] verts = new Vector3[3] { v1, v2, v3 };

            for (int yp = (int)min.Y; yp < (int)max.Y; yp++)
            {
                List<Vector3> intersections = new List<Vector3>();

                //voor alle lijnen van de triangle, vind alle intersecties (als het goed is altijd 2)
                for (int l = 0; l < 3; l++)
                {
                    Vector3 vert1, vert2;
                    vert1 = verts[l];
                    vert2 = verts[(l + 1) % 3];

                    float ymin, ymax;
                    ymin = Math.Min(vert1.Y, vert2.Y);
                    ymax = Math.Max(vert1.Y, vert2.Y);
                    if (yp > ymin && yp < ymax)//check of yp tussen de y-en van de vertices ligt, zo ja, intersectie
                    {
                        float xSlope = (vert2.X - vert1.X) / (vert2.Y - vert1.Y);
                        float xIntersect = vert1.X + xSlope * (yp - vert1.Y);

                        float zSlope = (vert2.Z - vert1.Z) / (vert2.Y - vert1.Y);
                        float zIntersect = vert1.Z + zSlope * (yp - vert1.Y);
                        intersections.Add(new Vector3((int)xIntersect, yp, zIntersect));
                    }
                }

                //vul scanline
                if (intersections.Count > 1)
                {
                    Vector3 pStart, pEnd;
                    if (intersections[0].X < intersections[1].X) { pStart = intersections[0]; pEnd = intersections[1]; }
                    else
                    { pStart = intersections[1]; pEnd = intersections[0]; }

                    //vul de rij met pixels
                    for (int xp = MathHelper.Clamp((int)pStart.X, 0, ScreenSize.X); xp < MathHelper.Clamp(pEnd.X, 0, ScreenSize.X); xp++)
                    {
                        float zSlope = (pEnd.Z - pStart.Z) / (pEnd.X - pStart.X);
                        float zCurrent = pStart.Z + zSlope * (xp - pStart.X);

                        //DrawPoint(new Vector3(xp, yp, zCurrent), color, Matrix.GetIdentity());
                        if (zCurrent < zBuffer[xp, yp] && zCurrent > ZNear)
                        {
                            screen[xp, yp] = color;
                            zBuffer[xp, yp] = zCurrent;
                        }
                    }
                }
            }
        }