コード例 #1
0
        public void OnEndRender()
        {
            if (_frameBuffer == null)
            {
                return;
            }
            _platform.LoadFrameBuffer(EnumFrameBuffer.Primary);
            GL.ClearBuffer(ClearBuffer.Color, 0, new [] { 0f, 0f, 0f, 1f });
            GL.ClearBuffer(ClearBuffer.Color, 1, new [] { 0f, 0f, 0f, 1f });

            var ambient    = _mod.CApi.Ambient;
            var uniforms   = _mod.CApi.Render.ShaderUniforms;
            var myUniforms = _mod.Uniforms;

            var fb        = _frameBuffer;
            var fbPrimary = _platform.FrameBuffers[(int)EnumFrameBuffer.Primary];

            _platform.GlDisableDepthTest();
            _platform.GlToggleBlend(false);
            GL.DrawBuffers(2, new [] { DrawBuffersEnum.ColorAttachment0, DrawBuffersEnum.ColorAttachment1 });

            var s = _shader;

            s.Use();

            s.BindTexture2D("gDepth", fbPrimary.DepthTextureId);
            s.BindTexture2D("gNormal", fbPrimary.ColorTextureIds[2]);
            s.BindTexture2D("inColor", fb.ColorTextureIds[0]);
            s.BindTexture2D("inGlow", fb.ColorTextureIds[1]);
            s.UniformMatrix("invProjectionMatrix", myUniforms.InvProjectionMatrix);
            s.UniformMatrix("invModelViewMatrix", myUniforms.InvModelViewMatrix);
            s.Uniform("dayLight", myUniforms.DayLight);
            s.Uniform("playerPos", uniforms.PlayerPos);
            s.Uniform("sunPosition", uniforms.SunPosition3D);


            if (ShaderProgramBase.shadowmapQuality > 0)
            {
                s.Uniform("shadowRangeFar", uniforms.ShadowRangeFar);
                s.Uniform("shadowRangeNear", uniforms.ShadowRangeNear);
                s.UniformMatrix("toShadowMapSpaceMatrixFar", uniforms.ToShadowMapSpaceMatrixFar);
                s.UniformMatrix("toShadowMapSpaceMatrixNear", uniforms.ToShadowMapSpaceMatrixNear);
            }

            s.Uniform("fogDensityIn", ambient.BlendedFogDensity);
            s.Uniform("fogMinIn", ambient.BlendedFogMin);
            s.Uniform("rgbaFog", ambient.BlendedFogColor);

            _platform.RenderFullscreenTriangle(_screenQuad);
            s.Stop();
            _platform.CheckGlError("Error while calculating deferred lighting");

            GL.DrawBuffers(4, new []
            {
                DrawBuffersEnum.ColorAttachment0, DrawBuffersEnum.ColorAttachment1,
                DrawBuffersEnum.ColorAttachment2, DrawBuffersEnum.ColorAttachment3
            });
            _platform.GlEnableDepthTest();
        }
コード例 #2
0
        private void OnRenderSsrChunks()
        {
            var ssrFB = _framebuffers[(int)EnumSSRFB.SSR];

            if (ssrFB == null)
            {
                return;
            }
            if (_shaders[(int)EnumSSRShaders.Liquid] == null)
            {
                return;
            }

            if (!(_textureIdsField.GetValue(_chunkRenderer) is int[] textureIds))
            {
                return;
            }

            var playerWaterDepth = _game.playerProperties.EyesInWaterDepth;
            var playerInWater    = playerWaterDepth >= 0.1f;
            var playerUnderwater = playerInWater ? 0f : 1f;

            // copy the depth buffer so we can work with it
            var primaryBuffer = _platform.FrameBuffers[(int)EnumFrameBuffer.Primary];

            GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer, primaryBuffer.FboId);
            GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, ssrFB.FboId);
            GL.Clear(ClearBufferMask.DepthBufferBit);
            GL.BlitFramebuffer(0, 0, primaryBuffer.Width, primaryBuffer.Height,
                               0, 0, _fbWidth, _fbHeight, ClearBufferMask.DepthBufferBit,
                               BlitFramebufferFilter.Nearest);

            // bind our framebuffer
            _platform.LoadFrameBuffer(ssrFB);
            GL.ClearBuffer(ClearBuffer.Color, 0, new[] { 0f, 0f, 0f, 1f });
            GL.ClearBuffer(ClearBuffer.Color, 1, new[] { 0f, 0f, 0f, playerUnderwater });
            GL.ClearBuffer(ClearBuffer.Color, 2, new[] { 0f, 0f, 0f, 1f });
            if (_refractionsEnabled)
            {
                GL.ClearBuffer(ClearBuffer.Color, 3, new [] { 0f, 0f, 0f, 1f });
            }

            _platform.GlEnableCullFace();
            _platform.GlDepthMask(true);
            _platform.GlEnableDepthTest();
            _platform.GlToggleBlend(false);

            var climateAt =
                _game.BlockAccessor.GetClimateAt(_game.EntityPlayer.Pos.AsBlockPos, EnumGetClimateMode.NowValues);
            var curRainFall = climateAt.Rainfall;

            var cameraPos = _game.EntityPlayer.CameraPos;

            // render stuff
            _game.GlPushMatrix();
            _game.GlLoadMatrix(_mod.CApi.Render.CameraMatrixOrigin);

            var shader = _shaders[(int)EnumSSRShaders.Opaque];

            shader.Use();
            shader.UniformMatrix("projectionMatrix", _mod.CApi.Render.CurrentProjectionMatrix);
            shader.UniformMatrix("modelViewMatrix", _mod.CApi.Render.CurrentModelviewMatrix);
            shader.Uniform("playerUnderwater", playerUnderwater);
            var pools = _chunkRenderer.poolsByRenderPass[(int)EnumChunkRenderPass.Opaque];

            for (var i = 0; i < textureIds.Length; ++i)
            {
                shader.BindTexture2D("terrainTex", textureIds[i], 0);
                shader.BindTexture2D("terrainTexLinear", textureIds[i], 0);
                pools[i].Render(cameraPos, "origin");
            }

            shader.Stop();

            if (_rainEnabled)
            {
                shader = _shaders[(int)EnumSSRShaders.Topsoil];
                shader.Use();
                shader.UniformMatrix("projectionMatrix", _mod.CApi.Render.CurrentProjectionMatrix);
                shader.UniformMatrix("modelViewMatrix", _mod.CApi.Render.CurrentModelviewMatrix);
                shader.Uniform("rainStrength", _currentRain);
                shader.Uniform("playerUnderwater", playerUnderwater);
                pools = _chunkRenderer.poolsByRenderPass[(int)EnumChunkRenderPass.TopSoil];
                for (var i = 0; i < textureIds.Length; ++i)
                {
                    shader.BindTexture2D("terrainTex", textureIds[i], 0);
                    pools[i].Render(cameraPos, "origin");
                }

                shader.Stop();
            }

            _platform.GlDisableCullFace();
            shader = _shaders[(int)EnumSSRShaders.Liquid];
            shader.Use();
            shader.UniformMatrix("projectionMatrix", _mod.CApi.Render.CurrentProjectionMatrix);
            shader.UniformMatrix("modelViewMatrix", _mod.CApi.Render.CurrentModelviewMatrix);
            shader.Uniform("dropletIntensity", curRainFall);
            shader.Uniform("waterFlowCounter", _platform.ShaderUniforms.WaterFlowCounter);
            shader.Uniform("windSpeed", _platform.ShaderUniforms.WindSpeed);
            shader.Uniform("playerUnderwater", playerUnderwater);
            shader.Uniform("cameraWorldPosition", _mod.Uniforms.CameraWorldPosition);
            pools = _chunkRenderer.poolsByRenderPass[(int)EnumChunkRenderPass.Liquid];
            for (var i = 0; i < textureIds.Length; ++i)
            {
                shader.BindTexture2D("terrainTex", textureIds[i], 0);
                pools[i].Render(cameraPos, "origin");
            }

            shader.Stop();
            _platform.GlEnableCullFace();

            shader = _shaders[(int)EnumSSRShaders.Transparent];
            shader.Use();
            shader.UniformMatrix("projectionMatrix", _mod.CApi.Render.CurrentProjectionMatrix);
            shader.UniformMatrix("modelViewMatrix", _mod.CApi.Render.CurrentModelviewMatrix);
            shader.Uniform("playerUnderwater", playerUnderwater);
            pools = _chunkRenderer.poolsByRenderPass[(int)EnumChunkRenderPass.Transparent];
            for (var i = 0; i < textureIds.Length; ++i)
            {
                shader.BindTexture2D("terrainTex", textureIds[i], 0);
                pools[i].Render(cameraPos, "origin");
            }

            shader.Stop();

            _game.GlPopMatrix();
            _platform.UnloadFrameBuffer(ssrFB);

            _platform.GlDepthMask(false);
            _platform.GlToggleBlend(true);

            _platform.CheckGlError("Error while rendering solid liquids");
        }