Exemple #1
0
        void DrawGL(int renderWidth, int renderHeight)
        {
            var       cam = new LookAtCamera();
            Matrix4x4 rot = Matrix4x4.CreateRotationX(aleViewport.CameraRotation.Y) *
                            Matrix4x4.CreateRotationY(aleViewport.CameraRotation.X);
            var dir = Vector3.Transform(-Vector3.UnitZ, rot);
            var to  = aleViewport.CameraOffset + (dir * 10);

            if (aleViewport.Mode == CameraModes.Arcball)
            {
                to = Vector3.Zero;
            }
            cam.Update(renderWidth, renderHeight, aleViewport.CameraOffset, to, rot);
            buffer.StartFrame(rstate);
            polyline.SetCamera(cam);
            debug.StartFrame(cam, rstate);
            instance.Draw(transform, sparam);
            pool.Draw(cam, polyline, res, debug);
            polyline.FrameEnd();
            buffer.DrawOpaque(rstate);
            rstate.DepthWrite = false;
            buffer.DrawTransparent(rstate);
            rstate.DepthWrite = true;
            debug.Render();
        }
        void DrawGL(int renderWidth, int renderHeight)
        {
            var     cam = new LookAtCamera();
            Matrix4 rot = Matrix4.CreateRotationX(aleViewport.CameraRotation.Y) *
                          Matrix4.CreateRotationY(aleViewport.CameraRotation.X);
            var dir = rot.Transform(Vector3.Forward);
            var to  = aleViewport.CameraOffset + (dir * 10);

            cam.Update(renderWidth, renderHeight, aleViewport.CameraOffset, to, rot);
            buffer.StartFrame(rstate);
            polyline.SetCamera(cam);
            billboards.Begin(cam, buffer);
            debug.StartFrame(cam, rstate);
            instance.Draw(polyline, billboards, debug, transform, sparam);
            pool.Draw(polyline, billboards, debug);
            polyline.FrameEnd();
            billboards.End();
            buffer.DrawOpaque(rstate);
            rstate.DepthWrite = false;
            buffer.DrawTransparent(rstate);
            rstate.DepthWrite = true;
            debug.Render();
        }
Exemple #3
0
        public override void Draw()
        {
            ImGui.Columns(2);
            ImGui.BeginChild("##munitions");
            foreach (var m in projectileList)
            {
                if (ImGui.Selectable(m.Nickname, currentMunition == m))
                {
                    currentMunition = m;
                    constEffect     = effects.FindEffect(m.ConstEffect);
                    bolt            = effects.BeamBolts.FirstOrDefault(x =>
                                                                       x.Nickname.Equals(constEffect.VisBeam, StringComparison.OrdinalIgnoreCase));
                    beam = effects.BeamSpears.FirstOrDefault(x =>
                                                             x.Nickname.Equals(constEffect.VisBeam, StringComparison.OrdinalIgnoreCase));
                    viewport.ResetControls();
                }
            }
            ImGui.EndChild();
            ImGui.NextColumn();
            ImGui.BeginChild("##rendering");
            ViewerControls.DropdownButton("Camera Mode", ref cameraMode, camModes);
            viewport.Mode = (CameraModes)camModes[cameraMode].Tag;
            ImGui.SameLine();
            if (ImGui.Button("Reset Camera (Ctrl+R)"))
            {
                viewport.ResetControls();
            }
            viewport.Begin();
            Matrix4x4 rot = Matrix4x4.CreateRotationX(viewport.CameraRotation.Y) *
                            Matrix4x4.CreateRotationY(viewport.CameraRotation.X);
            var dirRot = Matrix4x4.CreateRotationX(viewport.ModelRotation.Y) * Matrix4x4.CreateRotationY(viewport.ModelRotation.X);
            var norm   = Vector3.TransformNormal(-Vector3.UnitZ, dirRot);
            var dir    = Vector3.Transform(-Vector3.UnitZ, rot);
            var to     = viewport.CameraOffset + (dir * 10);

            if (viewport.Mode == CameraModes.Arcball)
            {
                to = Vector3.Zero;
            }
            camera.Update(viewport.RenderWidth, viewport.RenderHeight, viewport.CameraOffset, to, rot);
            mw.Commands.StartFrame(mw.RenderState);
            beams.Begin(mw.Commands, mw.Resources, camera);
            var position = Vector3.Zero;

            if (beam != null)
            {
                beams.AddBeamSpear(position, norm, beam);
            }
            else if (bolt != null)
            {
                Vector2 tl, tr, bl, br;
                //CoordsFromTexture(bolt.HeadTexture, out tl, out tr, out bl, out br);
            }
            beams.End();
            fxPool.Draw(camera, null, mw.Resources, null);
            mw.Commands.DrawOpaque(mw.RenderState);
            mw.RenderState.DepthWrite = false;
            mw.Commands.DrawTransparent(mw.RenderState);
            mw.RenderState.DepthWrite = true;
            if (constEffect != null)
            {
                mw.Renderer2D.Start(viewport.RenderWidth, viewport.RenderHeight);
                var debugText = new StringBuilder();
                debugText.AppendLine($"ConstEffect: {constEffect.Nickname}");
                if (bolt != null)
                {
                    debugText.AppendLine($"Bolt: {bolt.Nickname}");
                }
                if (beam != null)
                {
                    debugText.AppendLine($"Beam: {beam.Nickname}");
                }
                mw.Renderer2D.DrawString("Arial", 10, debugText.ToString(), Vector2.One, Color4.White);
                mw.Renderer2D.Finish();
            }
            viewport.End();
            ImGui.EndChild();
        }
        public unsafe void Draw()
        {
            if (gconfig.MSAASamples > 0)
            {
                if (_mwidth != Game.Width || _mheight != Game.Height)
                {
                    _mwidth  = Game.Width;
                    _mheight = Game.Height;
                    if (msaa != null)
                    {
                        msaa.Dispose();
                    }
                    msaa = new MultisampleTarget(Game.Width, Game.Height, gconfig.MSAASamples);
                }
                msaa.Bind();
            }
            NebulaRenderer nr = CheckNebulae();             //are we in a nebula?

            bool transitioned = false;

            if (nr != null)
            {
                transitioned = nr.FogTransitioned();
            }
            rstate.DepthEnabled = true;
            //Add Nebula light
            if (GLExtensions.Features430 && ExtraLights)
            {
                //TODO: Re-add [LightSource] to the compute shader, it shouldn't regress.
                PointLight p2;
                if (nr != null && nr.DoLightning(out p2))
                {
                    pointLights.Add(p2);
                }
            }
            //Async calcs
            objects = new List <ObjectRenderer>(250);
            for (int i = 0; i < World.Objects.Count; i += 16)
            {
                JThreads.Instance.AddTask((o) =>
                {
                    var offset = (int)o;
                    for (int j = 0; j < 16 && ((j + offset) < World.Objects.Count); j++)
                    {
                        World.Objects[j + offset].PrepareRender(camera, nr, this);
                    }
                }, i);
            }
            JThreads.Instance.BeginExecute();
            if (transitioned)
            {
                //Fully in fog. Skip Starsphere
                rstate.ClearColor = nr.Nebula.FogColor;
                rstate.ClearAll();
            }
            else
            {
                rstate.DepthEnabled = false;
                if (starSystem == null)
                {
                    rstate.ClearColor = NullColor;
                }
                else
                {
                    rstate.ClearColor = starSystem.BackgroundColor;
                }
                rstate.ClearAll();
                //Starsphere
                for (int i = 0; i < StarSphereModels.Length; i++)
                {
                    Matrix4 ssworld = Matrix4.CreateTranslation(camera.Position);

                    if (StarSphereWorlds != null)
                    {
                        ssworld = StarSphereWorlds[i] * ssworld;
                    }
                    else if (StarSphereModels[i] is CmpFile)
                    {
                        var cmp = (CmpFile)StarSphereModels[i];
                        ssworld = Matrix4.CreateTranslation(cmp.GetRootPart().Model.Levels[0].Center) * ssworld;
                    }
                    else if (StarSphereModels[i] is ModelFile)
                    {
                        var mdl = (ModelFile)StarSphereModels[i];
                        ssworld = Matrix4.CreateTranslation(mdl.Levels[0].Center) * ssworld;
                    }
                    StarSphereModels[i].Draw(rstate, ssworld, Lighting.Empty);
                }
                //Render fog transition: if any
                if (nr != null)
                {
                    rstate.DepthEnabled = false;
                    nr.RenderFogTransition();
                    rstate.DepthEnabled = true;
                }
            }
            DebugRenderer.StartFrame(camera, rstate);
            Polyline.SetCamera(camera);
            commands.StartFrame(rstate);
            rstate.DepthEnabled = true;
            //Optimisation for dictionary lookups
            LightEquipRenderer.FrameStart();
            //Clear depth buffer for game objects
            rstate.ClearDepth();
            billboards.Begin(camera, commands);
            JThreads.Instance.FinishExecute();             //Make sure visibility calculations are complete
            if (GLExtensions.Features430 && ExtraLights)
            {
                //Forward+ heck yeah!
                //(WORKED AROUND) Lights being culled too aggressively - Pittsburgh planet light, intro_planet_chunks
                //Z test doesn't seem to be working (commented out in shader)
                //May need optimisation
                int plc = pointLights.Count;
                using (var h = pointLightBuffer.Map()) {
                    var ptr = (PointLight *)h.Handle;
                    for (int i = 0; i < pointLights.Count; i++)
                    {
                        ptr[i] = pointLights[i];
                    }
                }
                pointLights.Clear();
                //Setup Visible Buffers
                var tilesW = (Game.Width + (Game.Width % 16)) / 16;
                var tilesH = (Game.Height + (Game.Height % 16)) / 16;
                SystemLighting.NumberOfTilesX = tilesW;
                if (_twidth != tilesW || _theight != tilesH)
                {
                    _twidth  = tilesW;
                    _theight = tilesH;
                    //if (opaqueLightBuffer != null) opaqueLightBuffer.Dispose();
                    if (transparentLightBuffer != null)
                    {
                        transparentLightBuffer.Dispose();
                    }
                    //opaqueLightBuffer = new ShaderStorageBuffer((tilesW * tilesH) * 512 * sizeof(int));
                    transparentLightBuffer = new ShaderStorageBuffer((tilesW * tilesH) * 512 * sizeof(int));
                }
                //Depth
                if (_dwidth != Game.Width || _dheight != Game.Height)
                {
                    _dwidth  = Game.Width;
                    _dheight = Game.Height;
                    if (depthMap != null)
                    {
                        depthMap.Dispose();
                    }
                    depthMap = new DepthMap(Game.Width, game.Height);
                }
                depthMap.BindFramebuffer();
                rstate.ClearDepth();
                rstate.DepthFunction = DepthFunction.Less;
                foreach (var obj in objects)
                {
                    obj.DepthPrepass(camera, rstate);
                }
                rstate.DepthFunction = DepthFunction.LessEqual;
                RenderTarget2D.ClearBinding();
                if (gconfig.MSAASamples > 0)
                {
                    msaa.Bind();
                }
                //Run compute shader
                pointLightBuffer.BindIndex(0);
                transparentLightBuffer.BindIndex(1);
                //opaqueLightBuffer.BindIndex(2);
                pointLightCull.Uniform1i("depthTexture", 7);
                depthMap.BindTo(7);
                pointLightCull.Uniform1i("numLights", plc);
                pointLightCull.Uniform1i("windowWidth", Game.Width);
                pointLightCull.Uniform1i("windowHeight", Game.Height);
                var v = camera.View;
                var p = camera.Projection;
                p.Invert();
                pointLightCull.UniformMatrix4fv("viewMatrix", ref v);
                pointLightCull.UniformMatrix4fv("invProjection", ref p);
                GL.MemoryBarrier(GL.GL_SHADER_STORAGE_BARRIER_BIT);                 //I don't think these need to be here - confirm then remove?
                pointLightCull.Dispatch((uint)tilesW, (uint)tilesH, 1);
                GL.MemoryBarrier(GL.GL_SHADER_STORAGE_BARRIER_BIT);
            }
            else
            {
                SystemLighting.NumberOfTilesX = -1;
                //Simple depth pre-pass
                rstate.ColorWrite    = false;
                rstate.DepthFunction = DepthFunction.Less;
                foreach (var obj in objects)
                {
                    obj.DepthPrepass(camera, rstate);
                }
                rstate.DepthFunction = DepthFunction.LessEqual;
                rstate.ColorWrite    = true;
            }

            //Actual Drawing
            foreach (var obj in objects)
            {
                obj.Draw(camera, commands, SystemLighting, nr);
            }
            FxPool.Draw(camera, Polyline, resman, DebugRenderer);
            for (int i = 0; i < AsteroidFields.Count; i++)
            {
                AsteroidFields[i].Draw(cache, SystemLighting, commands, nr);
            }
            nebulae.NewFrame();
            if (nr == null)
            {
                for (int i = 0; i < Nebulae.Count; i++)
                {
                    Nebulae[i].Draw(commands);
                }
            }
            else
            {
                nr.Draw(commands);
            }
            nebulae.SetData();
            billboards.End();
            Polyline.FrameEnd();
            //Opaque Pass
            rstate.DepthEnabled = true;
            commands.DrawOpaque(rstate);
            //Transparent Pass
            rstate.DepthWrite = false;
            commands.DrawTransparent(rstate);
            rstate.DepthWrite = true;
            PhysicsHook?.Invoke();
            DebugRenderer.Render();
            if (gconfig.MSAASamples > 0)
            {
                msaa.BlitToScreen();
            }
            rstate.DepthEnabled = true;
        }