Esempio n. 1
0
        /// <summary>
        /// Draws all the physics bodies and UI elements.
        /// </summary>
        private unsafe void DrawFrame(TimeStep timeStep, FpsManager frameTimer)
        {
            _textDisplay.Clear();

            // check for global events
            _eventManager.CheckForGlobalEvents(this);

            RectangleD cameraBounds = _camera.Bounds;

            IGravitationalBody target = _gravitationalBodies[_targetIndex];
            var targetSpaceCraft      = target as SpaceCraftBase;

            // If openCL is supported render all cl bodies
            if (_renderingType == RenderingType.OpenCLHardware ||
                _renderingType == RenderingType.OpenCLSoftware)
            {
                _gpuClear.RenderCl(_clProxy);

                foreach (MassiveBodyBase renderable in _massiveBodies)
                {
                    if (renderable.Visibility(cameraBounds) > 0)
                    {
                        renderable.RenderCl(_clProxy, _camera, _sun);
                    }
                }

                int[] frameData = _clProxy.ReadIntBuffer("image", RenderUtils.ScreenArea);

                var rect = new Rectangle(0, 0, _imageBitmap.Width, _imageBitmap.Height);

                BitmapData bmpData = _imageBitmap.LockBits(rect, ImageLockMode.WriteOnly,
                                                           PixelFormat.Format32bppArgb);

                Marshal.Copy(frameData, 0, bmpData.Scan0, RenderUtils.ScreenArea);

                var ptr = (byte *)bmpData.Scan0;

                // Hack to force full alpha for now
                for (int i = 0; i < RenderUtils.ScreenArea; i++)
                {
                    ptr[i * 4 + 3] = 255;
                }

                _imageBitmap.UnlockBits(bmpData);
            }
            else
            {
                // Fall back to gdi for cl renderables
                using (var graphics = Graphics.FromImage(_imageBitmap))
                {
                    graphics.Clear(Color.Black);

                    _camera.ApplyScreenRotation(graphics);

                    foreach (MassiveBodyBase renderable in _massiveBodies)
                    {
                        if (renderable.Visibility(cameraBounds) > 0)
                        {
                            renderable.RenderGdiFallback(graphics, _camera, _sun);
                        }
                    }
                }
            }

            // Draw all orbit traces, spacecrafts, and GDI objects
            using (Graphics graphics = RenderUtils.GetContext(false, _imageBitmap))
            {
                _camera.ApplyScreenRotation(graphics);

                // Draw all massive body orbit traces
                foreach (MassiveBodyBase massiveBody in _massiveBodies)
                {
                    if (massiveBody is Sun)
                    {
                        continue;
                    }

                    massiveBody.RenderGdi(graphics, _camera);
                }

                graphics.ResetTransform();

                // Draw structures
                foreach (StructureBase structure in _structures)
                {
                    structure.RenderGdi(graphics, _camera);
                }

                _spaceCraftManager.Render(graphics, _camera);
            }

            // Draw all GUI elements (higher quality)
            using (Graphics graphics = RenderUtils.GetContext(true, _imageBitmap))
            {
                double throttle = 0;

                if (targetSpaceCraft != null)
                {
                    throttle = targetSpaceCraft.Throttle;

                    // TODO: render PIP
                    //int width = RenderUtils.ScreenWidth;
                    //int height = RenderUtils.ScreenHeight;
                    //Rectangle rectPip = new Rectangle(width - 220, 100, 200, height - 300);
                    //graphics.DrawRectangle(Pens.White, rectPip);

                    //_pipCam.ApplyScreenRotation(graphics);
                    //_spaceCraftManager.Render(graphics, _pipCam);
                }

                double pitch           = 0.0;
                double flightPathAngle = 0.0;
                foreach (IGauge gauge in _gauges)
                {
                    if (targetSpaceCraft != null)
                    {
                        if (_targetInOrbit && _rotateInOrbit)
                        {
                            pitch = _gravitationalBodies[_targetIndex].Pitch;
                        }
                        else
                        {
                            pitch = _gravitationalBodies[_targetIndex].GetRelativePitch();
                        }

                        flightPathAngle = pitch - targetSpaceCraft.GetAlpha();
                        gauge.Update(pitch, throttle / 100.0, flightPathAngle);
                    }

                    gauge.Render(graphics, cameraBounds);
                }

                _eventManager.Render(graphics);

                var elapsedTime = TimeSpan.FromSeconds(_totalElapsedSeconds - _clockDelay);

                int elapsedYears = elapsedTime.Days / 365;
                int elapsedDays  = elapsedTime.Days % 365;

                DateTime localTime = _originTime + elapsedTime;

                // Main timing display
                _textDisplay.AddTextBlock(StringAlignment.Near, new List <string>
                {
                    //$"Origin Time: {localTime.ToShortDateString()} {localTime.ToShortTimeString()}",
                    $"Origin Time: {string.Format("{0}-{1}-{2:D2}", localTime.Date.Year, localTime.Date.Month, localTime.Date.Day)} {localTime.ToShortTimeString()}",
                    //$"Elapsed Time: Y:{elapsedYears} D:{elapsedDays} H:{elapsedTime.Hours} M:{elapsedTime.Minutes} S:{Math.Round(elapsedTime.TotalSeconds % 60)}",
                    $"Elapsed Time: Y:{elapsedYears} D:{elapsedDays} H:{elapsedTime.Hours} M:{elapsedTime.Minutes} S:{elapsedTime.Seconds}",
                    $"Update Speed: {timeStep.Multiplier}"
                });

                // Target display
                _textDisplay.AddTextBlock(StringAlignment.Center, string.Format("Target: {0}", target));

                // FPS
                _textDisplay.AddTextBlock(StringAlignment.Far, "FPS: " + frameTimer.CurrentFps);

                double targetVelocity   = target.GetRelativeVelocity().Length();
                double inertialVelocity = target.GetInertialVelocity().Length();

                // Info for altitude
                // double altitude = target.GetRelativeAltitude();
                // double altitude = target.GetRelativeAltitude() + 82.5;    // Starship Mk1
                double altitude     = target.GetRelativeAltitude() - 111.4; // Starship + Super Heavy
                var    altitudeInfo = new List <string> {
                    "Altitude: " + UnitDisplay.Distance(altitude)
                };

                // Add downrange if spacecraft exists
                if (targetSpaceCraft != null)
                {
                    double downrangeDistance = targetSpaceCraft.GetDownrangeDistance(_structures[0].Position);

                    altitudeInfo.Add("Downrange: " + UnitDisplay.Distance(downrangeDistance));
                }

                _textDisplay.AddTextBlock(StringAlignment.Near, altitudeInfo);

                // Info for speed / acceleration
                var movementInfo = new List <string>
                {
                    "Inertial Velocity: " + UnitDisplay.Speed(targetVelocity, useKmh),
                    "Orbital Velocity: " + UnitDisplay.Speed(inertialVelocity, useKmh),
                    "Inertial Acceleration: " + UnitDisplay.Acceleration(target.GetRelativeAcceleration().Length()),
                    "Orbital Acceleration: " + UnitDisplay.Acceleration(target.GetInertialAcceleration().Length())
                };

                double lateralVelocity = target.GetLateralVelocity().Length();
                if (lateralVelocity > 0)
                {
                    double lateralPosition = target.GetLateralPosition().Length();
                    altitudeInfo.Add("Crossrange: " + UnitDisplay.Distance(lateralPosition));

                    movementInfo.Add("Lateral Velocity: " + UnitDisplay.Speed(lateralVelocity, useKmh));
                    movementInfo.Add("Lateral Acceleration: " + UnitDisplay.Acceleration(target.GetLateralAcceleration().Length()));
                }

                // Add angle of attack if it exists
                if (targetSpaceCraft != null)
                {
                    movementInfo.Add("Angle of Attack: " + UnitDisplay.AoA(targetSpaceCraft.GetAlpha()));

                    // calculate the current inclination
                    double G      = 6.67408E-11;
                    double M      = target.GravitationalParent.Mass;
                    double μ      = G * M;
                    double R      = target.GravitationalParent.SurfaceRadius;
                    double apogee = target.Apoapsis;
                    if (initialPerigee == 0)
                    {
                        initialPerigee = target.Periapsis;
                    }
                    double Ra = R + apogee;
                    double Rp = R + initialPerigee;
                    double e  = 1 - 2 / ((Ra / Rp) + 1);
                    double ω  = 0;
                    double f  = 0;

                    // orbital period
                    double a           = (initialPerigee + R * 2 + apogee) / 2;
                    double P           = 2 * Math.PI * Math.Pow(Math.Pow(a, 3) / μ, 0.5);
                    double n           = 2 * Math.PI / P;
                    double Δi          = 2 * Math.Asin(lateralVelocity * (1 + e * Math.Cos(f)) / (Math.Pow(1 - Math.Pow(e, 2), 0.5) * Math.Cos(ω + f) * n * a * 2));
                    double inclination = launchInclination * MathHelper.DegreesToRadians - Δi;
                    movementInfo.Add("Inclination: " + UnitDisplay.Degrees(inclination));

                    double speedOfSound = targetSpaceCraft.GravitationalParent.GetSpeedOfSound(target.GetRelativeAltitude());
                    if (speedOfSound > 0)
                    {
                        double machNumber = targetVelocity / speedOfSound;
                        movementInfo.Add("Mach number: " + UnitDisplay.Mach(machNumber));
                    }
                }

                _textDisplay.AddTextBlock(StringAlignment.Near, movementInfo);

                var forceInfo = new List <string> {
                    "Mass: " + UnitDisplay.Mass(target.Mass)
                };

                // Add additional forces
                if (targetSpaceCraft != null)
                {
                    DVector2 dragForce = targetSpaceCraft.AccelerationD * targetSpaceCraft.Mass;
                    DVector2 liftForce = targetSpaceCraft.AccelerationL * targetSpaceCraft.Mass;
                    if (Settings.Default.UseTheTurnForce)
                    {
                        liftForce *= Math.Cos(targetSpaceCraft.Roll);
                    }

                    forceInfo.Add("Thrust: " + UnitDisplay.Force(targetSpaceCraft.Thrust));
                    forceInfo.Add("Drag: " + UnitDisplay.Force(dragForce.Length()));
                    forceInfo.Add("Lift: " + UnitDisplay.Force(liftForce.Length()));
                }

                _textDisplay.AddTextBlock(StringAlignment.Near, forceInfo);

                // Don't show Apoapsis/Periapsis info for the sun
                if (!(target is Sun) && target.GravitationalParent != null)
                {
                    _textDisplay.AddTextBlock(StringAlignment.Near, new List <string>
                    {
                        $"{target.GravitationalParent.ApoapsisName}: {UnitDisplay.Distance(target.Apoapsis)}",
                        $"{target.GravitationalParent.PeriapsisName}: {UnitDisplay.Distance(target.Periapsis)}",
                    });
                }

                // Add atmospheric info if the spaceship is the target
                if (targetSpaceCraft != null && targetSpaceCraft.GravitationalParent != null)
                {
                    double density         = targetSpaceCraft.GravitationalParent.GetAtmosphericDensity(target.GetRelativeAltitude());
                    double dynamicPressure = 0.5 * density * targetVelocity * targetVelocity;

                    _textDisplay.AddTextBlock(StringAlignment.Near, new List <string>
                    {
                        "Air Density: " + UnitDisplay.Density(density),
                        "Dynamic Pressure: " + UnitDisplay.Pressure(dynamicPressure),
                        "Heat Flux: " + UnitDisplay.Heat(targetSpaceCraft.HeatingRate)
                    });
                }

                _textDisplay.Draw(graphics);
            }
        }