Example #1
0
        protected void RenderStuff()
        {
            Camera camera = Camera.Main;

            if (camera == null)
            {
                return;
            }

            foreach (Entity entity in DataManager.CurrentScene.SceneEntities)
            {
                if (entity.Parent == null)
                {
                    entity.Transform.CalculateModelMatrixRecursively();
                }
            }

            foreach (Entity entity in DataManager.UnlistedEntities)
            {
                if (entity.Parent == null)
                {
                    entity.Transform.CalculateModelMatrixRecursively();
                }
            }

            //shadow mapping
            Matrix4 lightView       = Matrix4.Identity;
            Matrix4 lightProjection = Matrix4.Identity;

            if (DirectionalLight.Main == null)
            {
                goto RenderOpaque;
            }

            DirectionalLight.Main.CalculateViewProjection(out lightView, out lightProjection, shadowMapSize, camera.BaseTransform.Position);

            LightData lightData = new LightData
            {
                View       = lightView,
                Projection = lightProjection
            };

            foreach (Entity entity in DataManager.CurrentScene.SceneEntities)
            {
                entity.RenderShadowMap(in lightData);
            }

            foreach (Entity entity in DataManager.UnlistedEntities)
            {
                entity.RenderShadowMap(in lightData);
            }

            GL.Viewport(0, 0, shadowMapRes, shadowMapRes);

            GL.BindFramebuffer(FramebufferTarget.Framebuffer, shadowMapFBO);
            GL.Clear(ClearBufferMask.DepthBufferBit);

            GL.Enable(EnableCap.DepthTest);

            GL.Enable(EnableCap.CullFace);
            GL.CullFace(CullFaceMode.Back);
            GL.FrontFace(FrontFaceDirection.Ccw);

            foreach (var buffer in shadowMapBuffers)
            {
                ExecuteCommandBuffer(buffer);
            }

            GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);

            //render opaque
RenderOpaque:
            camera.CalculateViewProjection(out var view, out var projection);

            //call all render funcs
            RenderData renderData = new RenderData
            {
                View             = view,
                Projection       = projection,
                LightView        = lightView,
                LightProjection  = lightProjection,
                ShadowMapTexture = shadowMap
            };

            foreach (var entity in DataManager.CurrentScene.SceneEntities)
            {
                entity.Render(renderData);
            }

            foreach (var entity in DataManager.UnlistedEntities)
            {
                entity.Render(renderData);
            }

            GL.Viewport(0, 0, ViewportSize.X, ViewportSize.Y);

            postProcessor.Begin();
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

            GL.Enable(EnableCap.DepthTest);

            GL.Enable(EnableCap.CullFace);
            GL.CullFace(CullFaceMode.Back);
            GL.FrontFace(FrontFaceDirection.Ccw);

            foreach (var buffer in opaqueBuffers)
            {
                ExecuteCommandBuffer(buffer);
            }

            //render transparent
            GL.DepthMask(false);

            GL.Disable(EnableCap.CullFace);

            GL.Enable(EnableCap.Blend);
            GL.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha);

            foreach (var buffer in transparentBuffers)
            {
                ExecuteCommandBuffer(buffer);
            }

            postProcessor.End();

            //clean up
            shadowMapBuffers.Clear();
            opaqueBuffers.Clear();
            transparentBuffers.Clear();
            guiBuffers.Clear();

            CommandProcessor.Reset();
        }
Example #2
0
        public override void Render()
        {
            if (Camera.Main == null)
            {
                return;
            }

            DataManager.CurrentScene.SceneRootEntities.ForEach(en => en.Transform.CalculateModelMatrixRecursively());
            DataManager.EngineReservedEntities.ForEach(en => en.Transform.CalculateModelMatrixRecursively());

            //shadow mapping
            if (DirectionalLight.Main == null)
            {
                goto RenderOpaque;
            }

            Matrix4 lView;
            Matrix4 lProjection;

            DirectionalLight.Main.CalculateViewProjection(out lView, out lProjection, shadowMapSize, Camera.Main.BaseTransform.Position);
            LightingGlobals.LightView       = lView;
            LightingGlobals.LightProjection = lProjection;

            DataManager.CurrentScene.SceneRootEntities.ForEach(en => en.RecursivelyRenderShadowMap(LightingGlobals.LightView, LightingGlobals.LightProjection));
            DataManager.EngineReservedEntities.ForEach(en => en.RecursivelyRenderShadowMap(LightingGlobals.LightView, LightingGlobals.LightProjection));

            GL.Viewport(0, 0, shadowMapRes, shadowMapRes);

            shadowMapFramebuffer.Begin();
            GL.Clear(ClearBufferMask.DepthBufferBit);

            GL.Enable(EnableCap.DepthTest);

            GL.Enable(EnableCap.CullFace);
            GL.CullFace(CullFaceMode.Back);
            GL.FrontFace(FrontFaceDirection.Ccw);

            DrawDrawList(drawLists[DrawType.ShadowMap]);

            shadowMapFramebuffer.End();

            LightingGlobals.ShadowMap = shadowMapFramebuffer.GetTexture(FramebufferAttachment.DepthAttachment);

            //render opaque
RenderOpaque:
            Camera.Main.CalculateViewProjection(out Matrix4 view, out Matrix4 projection);

            //call all render funcs
            DataManager.CurrentScene.SceneRootEntities.ForEach(en => en.RecursivelyRender(view, projection));
            DataManager.EngineReservedEntities.ForEach(en => en.RecursivelyRender(view, projection));

            GL.Viewport(0, 0, ViewportSize.X, ViewportSize.Y);

            postProcessor.Begin();
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

            GL.Enable(EnableCap.DepthTest);

            GL.Enable(EnableCap.CullFace);
            GL.CullFace(CullFaceMode.Back);
            GL.FrontFace(FrontFaceDirection.Ccw);

            DrawDrawList(drawLists[DrawType.Opaque]);

            //render transparent
            GL.DepthMask(false);

            GL.Disable(EnableCap.CullFace);

            GL.Enable(EnableCap.Blend);
            GL.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha);

            DrawDrawList(drawLists[DrawType.Transparent]);
            postProcessor.End();

            //reset states
            GL.DepthMask(true);
            GL.Disable(EnableCap.DepthTest);
            GL.Disable(EnableCap.CullFace);

            postProcessor.Render();

            ClearDrawList();
        }