Beispiel #1
0
        public void Draw(Renderer renderer)
        {
            if (renderer.DrawWireframe)
            {
                return;
            }
            Profiler.Begin("Skybox Draw");

            Shaders.Skybox.Set(renderer);

            Atmosphere a = StarSystem.ActiveSystem.GetCurrentAtmosphere(renderer.ActiveCamera.Position);

            if (a != null && a.Planet.WasDrawnLastFrame)
            {
                a.SetConstantBuffer(renderer);
            }
            else
            {
                renderer.Context.PixelShader.SetConstantBuffer(3, null);
            }

            renderer.Context.Rasterizer.State = renderer.rasterizerStateSolidNoCull;
            renderer.Context.OutputMerger.SetDepthStencilState(renderer.depthStencilStateNoDepth);

            renderer.Context.InputAssembler.SetVertexBuffers(0, new D3D11.VertexBufferBinding(Resources.QuadVertexBuffer, Utilities.SizeOf <float>() * 5, 0));
            renderer.Context.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;
            renderer.Context.InputAssembler.SetIndexBuffer(Resources.QuadIndexBuffer, SharpDX.DXGI.Format.R16_UInt, 0);

            Vector3 lightDir = Vector3d.Normalize(renderer.ActiveCamera.Position - StarSystem.ActiveSystem.GetStar().Position);

            for (int i = 0; i < 6; i++)
            {
                Matrix  m      = sides[i];
                float[] buffer = new float[] {
                    m.M11, m.M12, m.M13, m.M14,
                    m.M21, m.M22, m.M23, m.M24,
                    m.M31, m.M32, m.M33, m.M34,
                    m.M41, m.M42, m.M43, m.M44,
                    lightDir.X, lightDir.Y, lightDir.Z, 0
                };
                renderer.Context.PixelShader.SetShaderResource(1, TextureViews[i]);
                renderer.Context.UpdateSubresource(buffer, constBuffer);
                renderer.Context.VertexShader.SetConstantBuffer(1, constBuffer);
                renderer.Context.DrawIndexed(6, 0, 0);
            }

            renderer.Context.Rasterizer.State = renderer.DrawWireframe ? renderer.rasterizerStateWireframeCullBack : renderer.rasterizerStateSolidCullBack;
            renderer.Context.OutputMerger.SetDepthStencilState(renderer.depthStencilStateDefault);

            Profiler.End();
        }
Beispiel #2
0
        public Planet(string name, Vector3d pos, double radius, double mass, double terrainHeight, Atmosphere atmosphere = null) : base(pos, radius, mass)
        {
            Name          = name;
            Radius        = radius;
            TerrainHeight = terrainHeight;
            Atmosphere    = atmosphere;

            BoundingRadius = radius + terrainHeight;

            if (atmosphere != null)
            {
                atmosphere.Planet = this;
            }

            OceanHeight = .5;
            OceanColor  = new Color(35, 90, 200);
        }
Beispiel #3
0
        public virtual void UpdateForces(double deltaTime)
        {
            if (DisablePhysics)
            {
                return;
            }

            // gravity
            foreach (PhysicsBody cb in PhysicsSystem.bodies)
            {
                if (cb is CelestialBody && this != cb && !cb.DisablePhysics)
                {
                    AddForce(Physics.Gravity(this, cb, deltaTime), Vector3.Zero);
                }
            }

            // drag
            if (!(this is CelestialBody) && Drag > 0)
            {
                Atmosphere a = StarSystem.ActiveSystem.GetCurrentAtmosphere(Position);
                if (a != null)
                {
                    Vector3d dir = (Position - a.Planet.Position);
                    double   h   = dir.Length();
                    dir /= h;
                    if (h < a.Radius)
                    {
                        Vector3d airspeed = (Velocity - a.Planet.Velocity) - a.Planet.VelocityOnPoint(Position - a.Planet.Position);
                        double   v        = airspeed.LengthSquared();
                        if (v > .1)
                        {
                            double temp;
                            double pressure;
                            double density;
                            double c;
                            a.MeasureProperties(dir, h, out pressure, out density, out temp, out c);

                            double drag = .5 * density * v * Drag;
                            AddForce(Vector3d.Normalize(-airspeed) * drag, Vector3.Zero);
                        }
                    }
                }
            }
        }
Beispiel #4
0
        public void DrawHUD(Renderer renderer)
        {
            // time warp rect
            renderer.D2DContext.FillRectangle(
                new RawRectangleF(0, 0, 100, 40),
                renderer.Brushes["White"]);
            // time warp
            renderer.D2DContext.DrawText("x" + TimeWarp.ToString("N0"), renderer.SegoeUI24,
                                         new RawRectangleF(10, 0, 100, 40),
                                         renderer.Brushes["Black"]);

            // center rect
            float xmid = renderer.ResolutionX * .5f;

            renderer.D2DContext.FillRectangle(
                new RawRectangleF(xmid - 150, 0, xmid + 150, 50),
                renderer.Brushes["White"]);

            renderer.SegoeUI24.TextAlignment = SharpDX.DirectWrite.TextAlignment.Leading;
            renderer.SegoeUI14.TextAlignment = SharpDX.DirectWrite.TextAlignment.Leading;

            // get nearest body/star
            CelestialBody cb  = StarSystem.ActiveSystem.GetCurrentSOI(player.Position);
            Vector3d      dir = cb.Position - player.Position;
            double        h   = dir.Length();

            dir /= h;
            if (h > cb.SOI)
            {
                cb = StarSystem.ActiveSystem.GetStar();
            }

            Vector3d relative = cb.Velocity;
            string   vlbl     = "Orbit";

            double r             = cb.Radius;
            double radarAltitude = h - cb.GetHeight(dir);

            if (radarAltitude < 10000)
            {
                vlbl     = "Surface";
                relative = cb.VelocityOnPoint(player.Position - cb.Position) + cb.Velocity;
            }

            if (cb is Planet)
            {
                Planet p = cb as Planet;
                r = p.Radius + p.OceanHeight * p.TerrainHeight;

                #region surface info
                double temp  = p.GetTemperature(dir);
                double humid = p.GetHumidity(dir) * 100;

                renderer.D2DContext.FillRectangle(
                    new RawRectangleF(xmid + 155, 0, xmid + 260, 80),
                    renderer.Brushes["White"]);

                renderer.D2DContext.DrawText("Surface: ", renderer.SegoeUI14,
                                             new RawRectangleF(xmid + 155, 3, xmid + 240, 10),
                                             renderer.Brushes["Black"]);

                renderer.D2DContext.DrawText(temp.ToString("F1") + "°C", renderer.SegoeUI14,
                                             new RawRectangleF(xmid + 165, 15, xmid + 240, 30),
                                             renderer.Brushes["Black"]);

                renderer.D2DContext.DrawText(humid.ToString("F1") + "%", renderer.SegoeUI14,
                                             new RawRectangleF(xmid + 165, 30, xmid + 240, 45),
                                             renderer.Brushes["Black"]);
                #endregion
                #region atmosphere info
                Atmosphere a = p.Atmosphere;
                if (a != null && h < a.Radius * 1.5)
                {
                    double atemp;
                    double pressure;
                    double density;
                    double c;
                    a.MeasureProperties(dir, h, out pressure, out density, out atemp, out c);
                    if (pressure > .1)
                    {
                        renderer.D2DContext.FillRectangle(
                            new RawRectangleF(xmid - 260, 0, xmid - 155, 80),
                            renderer.Brushes["White"]);

                        renderer.D2DContext.DrawText("Atmosphere: ", renderer.SegoeUI14,
                                                     new RawRectangleF(xmid - 250, 3, xmid - 155, 10),
                                                     renderer.Brushes["Black"]);

                        renderer.D2DContext.DrawText(atemp.ToString("F1") + "°C", renderer.SegoeUI14,
                                                     new RawRectangleF(xmid - 240, 15, xmid - 155, 30),
                                                     renderer.Brushes["Black"]);

                        renderer.D2DContext.DrawText(pressure.ToString("F1") + " kPa", renderer.SegoeUI14,
                                                     new RawRectangleF(xmid - 240, 30, xmid - 155, 45),
                                                     renderer.Brushes["Black"]);

                        renderer.D2DContext.DrawText(density.ToString("F1") + " kg/m^3", renderer.SegoeUI14,
                                                     new RawRectangleF(xmid - 240, 45, xmid - 155, 60),
                                                     renderer.Brushes["Black"]);

                        renderer.D2DContext.DrawText("Mach " + (player.Velocity.Length() / c).ToString("F2"), renderer.SegoeUI14,
                                                     new RawRectangleF(xmid - 240, 60, xmid - 155, 75),
                                                     renderer.Brushes["Black"]);
                    }
                }
                #endregion
            }

            // altitude
            renderer.D2DContext.DrawText(Physics.FormatDistance(h - r), renderer.SegoeUI24,
                                         new RawRectangleF(xmid, 0, xmid + 150, 40),
                                         renderer.Brushes["Black"]);
            // body label
            renderer.D2DContext.DrawText(cb.Name, renderer.SegoeUI14,
                                         new RawRectangleF(xmid, 30, xmid + 150, 50),
                                         renderer.Brushes["Black"]);

            Orbit o = new Orbit(player.Position - cb.Position, player.Velocity - cb.Velocity, cb);

            renderer.SegoeUI14.ParagraphAlignment = SharpDX.DirectWrite.ParagraphAlignment.Far;
            renderer.SegoeUI14.TextAlignment      = SharpDX.DirectWrite.TextAlignment.Leading;
            renderer.D2DContext.DrawText(o.ToString(), renderer.SegoeUI14,
                                         new RawRectangleF(renderer.ResolutionX * .5f, renderer.ResolutionY, renderer.ResolutionX * .5f, renderer.ResolutionY),
                                         renderer.Brushes["White"]);

            // TODO: fix keplerian orbits
            //List<Vector3d> pts = new List<Vector3d>();
            //Vector3d op, ov;
            //for (double t = 0; t < o.T; t += o.T * .05) {
            //    o.ToCartesian(cb, t, out op, out ov);
            //    pts.Add(cb.Position + op);
            //}
            //o.ToCartesian(cb, o.T, out op, out ov);
            //pts.Add(cb.Position + op);
            //Debug.DrawLine(Color.CornflowerBlue, pts.ToArray());
            //
            //foreach (Vector3d p in pts) {
            //    Vector3d pos;
            //    double scale;
            //    renderer.Camera.GetScaledSpace(p + cb.Position, out pos, out scale);
            //    Debug.DrawBox(Color.Green, new OrientedBoundingBox(pos - (Vector3d)Vector3.One * 10000 * scale, pos + (Vector3d)Vector3.One * 10000 * scale));
            //}

            // velocity
            renderer.D2DContext.DrawText(Physics.FormatSpeed((player.Velocity - relative).Length()), renderer.SegoeUI24,
                                         new RawRectangleF(xmid - 130, 0, xmid, 40),
                                         renderer.Brushes["Black"]);
            renderer.D2DContext.DrawText(vlbl, renderer.SegoeUI14,
                                         new RawRectangleF(xmid - 130, 35, xmid, 50),
                                         renderer.Brushes["Black"]);
        }