示例#1
0
        private void PostProcess()
        {
            float zBufferFlicker = ((float)MainClass.Random.NextDouble() - .5f) / 50f;

            Game.Screen.Fill((x, y) => {
                float r, g, b, a;
                MathUtils3D.GetFloatsFromColor(Game.Screen.Get(x, y), out r, out g, out b, out a);
                float zBuffer = 1f - Game.ZBuffer.Get(x, y);
                zBuffer      *= WallsDisappearAt;
                zBuffer      -= Flash;
                zBuffer      += zBufferFlicker;
                if (zBuffer > 1f)
                {
                    zBuffer = 1f;
                }
                zBuffer = 1f - zBuffer;

                return(MathUtils3D.GetColor(r * zBuffer, g * zBuffer, b * zBuffer, a));
            });
        }
示例#2
0
        public void DrawPatch(Vector2 corner0, Vector2 corner1, Vector2 corner2, Vector2 corner3, Func <float, float, Color> textureFunc, Func <float, float, float> zBufVal, object calledBy)
        {
            Vector2 topLeft;
            Vector2 topRight;
            Vector2 bottomLeft;
            Vector2 bottomRight;

            Vector2[]             all    = new [] { corner0, corner1, corner2, corner3 };
            IEnumerable <Vector2> sorted = all.OrderBy(x => x.X);

            Vector2[] left  = sorted.Take(2).ToArray();
            Vector2[] right = sorted.Skip(2).Take(2).ToArray();

            if (left [0].Y < left [1].Y)
            {
                topLeft    = left [0];
                bottomLeft = left [1];
            }
            else
            {
                bottomLeft = left [0];
                topLeft    = left [1];
            }

            if (right [0].Y < right [1].Y)
            {
                topRight    = right [0];
                bottomRight = right [1];
            }
            else
            {
                bottomRight = right [0];
                topRight    = right [1];
            }

            float dyDxTop    = (topRight.Y - topLeft.Y) / (topRight.X - topLeft.X);
            float dyDxBottom = (bottomRight.Y - bottomLeft.Y) / (bottomRight.X - bottomLeft.X);

            int startX = (int)Math.Round(topLeft.X);
            int endX   = (int)Math.Round(topRight.X);

            for (int x = startX; x < endX; ++x)
            {
                if (x >= 0 && x < MainClass.ScreenWidth)
                {
                    float xRelative = x - startX;
                    float s         = xRelative / (endX - startX);
                    float yTop      = topLeft.Y + xRelative * dyDxTop;
                    float yBottom   = bottomLeft.Y + xRelative * dyDxBottom;

                    float yDifference = yBottom - yTop;

                    int yTopInt    = (int)Math.Round(yTop);
                    int yBottomInt = (int)Math.Round(yBottom);

                    float z = zBufVal(yTopInt, yDifference);
                    if (z > .1f)
                    {
                        for (int y = yTopInt; y < yBottomInt; ++y)
                        {
                            if (y >= 0 && y < MainClass.ScreenHeight)
                            {
                                float zPrevious = Game.ZBuffer.Get(x, y);
                                if (z > zPrevious)
                                {
                                    float t = (float)(y - yTopInt) / (yBottomInt - yTopInt);
                                    Color color = textureFunc(s, t);
                                    float a, r, g, b;
                                    MathUtils3D.GetFloatsFromColor(color, out r, out g, out b, out a);
                                    if (a != 0)
                                    {
                                        Game.Screen.Set(x, y, color);
                                        Game.ZBuffer.Set(x, y, z);
                                        if (x == MainClass.ScreenWidth / 2 && y == MainClass.ScreenHeight / 2)
                                        {
                                            CurrentPlayer.AtCrossHair = new Tuple <object, float, float>(calledBy, s, t);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
示例#3
0
文件: Wall.cs 项目: antonijn/7dfps
        public void Draw(MainGameState game)
        {
            float x = X - game.CurrentPlayer.X;
            float z = Z - game.CurrentPlayer.Z;

            float zBackup = z;

            z = game.CurrentPlayer.CosWorldRotation * z - game.CurrentPlayer.SinWorldRotation * x;
            x = game.CurrentPlayer.CosWorldRotation * x + game.CurrentPlayer.SinWorldRotation * zBackup;
            float rotation = Rotation + game.CurrentPlayer.WorldRotation;

            float cosRot      = (float)Math.Cos(rotation);
            float sinRot      = (float)Math.Sin(rotation);
            float secondEdgeZ = cosRot;
            float secondEdgeX = sinRot;

            Tuple <float, float> yPoss0 = MathUtils3D.GetYForZ(z);
            Tuple <float, float> yPoss1 = MathUtils3D.GetYForZ(z + secondEdgeZ);

            float xPoss0 = MathUtils3D.GetXForX(x, yPoss0.Item1);
            float xPoss1 = MathUtils3D.GetXForX(secondEdgeX + x, yPoss1.Item1);

            {
                float yDifference    = yPoss0.Item1 - yPoss0.Item2;
                float altitudeOffset = Altitude * yDifference;
                yPoss0 = new Tuple <float, float>(yPoss0.Item1 - altitudeOffset, yPoss0.Item2 - altitudeOffset);
            }
            {
                float yDifference    = yPoss1.Item1 - yPoss1.Item2;
                float altitudeOffset = Altitude * yDifference;
                yPoss1 = new Tuple <float, float>(yPoss1.Item1 - altitudeOffset, yPoss1.Item2 - altitudeOffset);
            }

            if (z > 0f && z + secondEdgeZ > 0f && z < MainGameState.WallsDisappearAt)
            {
                game.DrawPatch(new Vector2(xPoss0, yPoss0.Item1), new Vector2(xPoss0, yPoss0.Item2), new Vector2(xPoss1, yPoss1.Item1), new Vector2(xPoss1, yPoss1.Item2),
                               (s, t) => {
                    Color texColor   = Texture.Get((int)(s * Texture.Width), (int)(t * Texture.Height));
                    Color decalColor = DecalTexture.Get((int)(s * DecalTexture.Width), (int)(t * DecalTexture.Height));
                    float dR, dG, dB, dA;
                    MathUtils3D.GetFloatsFromColor(decalColor, out dR, out dG, out dB, out dA);
                    float tR, tG, tB, tA;
                    MathUtils3D.GetFloatsFromColor(texColor, out tR, out tG, out tB, out tA);

                    float fR, fG, fB, fA;
                    fA = dA + tA * (1f - dA);
                    fR = (dR * dA + tR * tA * (1f - dA)) / fA;
                    fG = (dG * dA + tG * tA * (1f - dA)) / fA;
                    fB = (dB * dA + tB * tA * (1f - dA)) / fA;

                    return(MathUtils3D.GetColor32(fR, fG, fB, fA));
                }, (yTop, yDifference) => {
                    if (Altitude == 0f)
                    {
                        return(MathUtils3D.GetZForY(yTop));
                    }

                    // should be just a bit different... they probably won't notice because the doors rise so fast
                    return(MathUtils3D.GetZForY(yDifference * Altitude + yTop));
                }, this);
            }
        }