Ejemplo n.º 1
0
        public void Draw(IFramebuffer targetFramebuffer, IRenderbuffer depthStencil)
        {
            infra.GlContext.States.DepthStencil.StencilTestEnable.Set(true);
            infra.GlContext.States.Blend.BlendEnable.Set(false);
            infra.GlContext.States.DepthStencil.DepthMask.Set(false);
            infra.GlContext.States.DepthStencil.DepthTestEnable.Set(false);
            infra.GlContext.States.DepthStencil.Back.StencilWriteMask.Set(0xff);
            infra.GlContext.States.DepthStencil.Front.StencilWriteMask.Set(0xff);
            infra.GlContext.States.Rasterizer.CullFaceEnable.Set(false);

            var offScreen = offScreenContainer.Get(this, depthStencil, depthStencil.Width, depthStencil.Height, depthStencil.Samples, OffScreenTtl);

            drawSpotFramebuffer.AttachRenderbuffer(FramebufferAttachmentPoint.Color0, offScreen.ColorBuffer);
            drawSpotFramebuffer.AttachRenderbuffer(FramebufferAttachmentPoint.DepthStencil, depthStencil);
            drawSpotFramebuffer.ClearColor(0, new Color4(0, 0, 0, 0));
            infra.GlContext.Bindings.Framebuffers.Draw.Set(drawSpotFramebuffer);

            infra.GlContext.Bindings.Program.Set(program);
            infra.GlContext.Bindings.VertexArray.Set(vao);

            SetStencilFunc(StencilFunction.Equal);
            infra.GlContext.Actions.Draw.Arrays(BeginMode.TriangleStrip, 0, 4);
            offScreen.Resolve();
            infra.GlContext.Bindings.Framebuffers.Draw.Set(offScreen.Framebuffer);
            SetStencilFunc(StencilFunction.Always);

            bleedDrawer.Draw(offScreen.ResolvedTex, Common.Numericals.Colors.Color4.Orange);
            offScreen.Resolve();
            infra.GlContext.Bindings.Framebuffers.Draw.Set(targetFramebuffer);
            SetStencilFunc(StencilFunction.Notequal);
            quadDrawer.Draw(offScreen.ResolvedTex);
            //quadDrawer.Draw(Common.Numericals.Colors.Color4.Blue);
            SetStencilFunc(StencilFunction.Always);

            infra.GlContext.States.DepthStencil.StencilTestEnable.Set(false);
            infra.GlContext.States.DepthStencil.DepthMask.Set(true);
            infra.GlContext.States.DepthStencil.DepthTestEnable.Set(true);
        }
Ejemplo n.º 2
0
        public unsafe override void OnNewFrame(float totalSeconds, float elapsedSeconds)
        {
            float   angle = totalSeconds * 0.125f;
            Matrix4 world = Matrix4.CreateRotationX(angle) * Matrix4.CreateRotationY(2 * angle) * Matrix4.CreateRotationZ(3 * angle);

            world.Transpose();

            Vector3 cameraPosition = new Vector3(5, 3, 0);

            Matrix4 view    = Matrix4.LookAt(cameraPosition, Vector3.Zero, Vector3.UnitZ);
            Matrix4 proj    = Matrix4.CreatePerspectiveFieldOfView((float)Math.PI / 4f, 1f, 0.1f, 1000f);
            Matrix4 invertY = Matrix4.Identity;

            invertY.M22 = -1f;
            Matrix4 viewProjection = view * proj * invertY;

            viewProjection.Transpose();

            Vector3 lightPosition = new Vector3(10, -7, 2);

            transformBuffer.SetDataByMapping((IntPtr)(&world));
            cameraBuffer.SetDataByMapping((IntPtr)(&viewProjection));
            cameraExtraBuffer.SetDataByMapping((IntPtr)(&cameraPosition));
            lightBuffer.SetDataByMapping((IntPtr)(&lightPosition));

            framebuffer.ClearColor(0, new Color4(0.4f, 0.6f, 0.9f, 1.0f));
            framebuffer.ClearDepthStencil(DepthStencil.Both, 1f, 0);

            Context.Actions.ClearWindowColor(new Color4(0, 0, 0, 1));
            Context.Actions.ClearWindowDepthStencil(DepthStencil.Both, 1f, 0);

            Context.States.ScreenClipping.United.Viewport.Set(RenderTargetSize, RenderTargetSize);

            Context.States.Rasterizer.FrontFace.Set(FrontFaceDirection.Cw);
            Context.States.Rasterizer.CullFaceEnable.Set(true);
            Context.States.Rasterizer.CullFace.Set(CullFaceMode.Front);

            Context.States.DepthStencil.DepthTestEnable.Set(true);
            Context.States.DepthStencil.DepthMask.Set(true);

            Context.Bindings.Framebuffers.Draw.Set(framebuffer);

            Context.Bindings.Program.Set(program);
            Context.Bindings.VertexArray.Set(vertexArray);
            Context.Bindings.Buffers.UniformIndexed[0].Set(transformBuffer);
            Context.Bindings.Buffers.UniformIndexed[1].Set(cameraBuffer);
            Context.Bindings.Buffers.UniformIndexed[2].Set(lightBuffer);
            Context.Bindings.Textures.Units[0].Set(diffuseMap);
            Context.Bindings.Samplers[0].Set(sampler);

            // Inside cube

            Context.Actions.Draw.Elements(BeginMode.Triangles, 36, DrawElementsType.UnsignedShort, 0);
            renderTarget.GenerateMipmap();

            // Outside cube
            proj = Matrix4.CreatePerspectiveFieldOfView((float)Math.PI / 4f, (float)GameWindow.ClientSize.Width / GameWindow.ClientSize.Height, 0.1f, 1000f);

            viewProjection = view * proj;
            viewProjection.Transpose();

#if INTEL_WORKAROUND
            cameraOutsideBuffer.SetDataByMapping((IntPtr)(&viewProjection));
#else
            cameraBuffer.SetDataByMapping((IntPtr)(&viewProjection));
#endif
            Context.States.ScreenClipping.United.Viewport.Set(GameWindow.ClientSize.Width, GameWindow.ClientSize.Height);
            Context.States.Rasterizer.FrontFace.Set(FrontFaceDirection.Ccw);

            Context.Bindings.Framebuffers.Draw.Set(null);
#if INTEL_WORKAROUND
            Context.Pipeline.UniformBuffers[1] = cameraOutsideBuffer;
#endif
            Context.Bindings.Textures.Units[0].Set(renderTarget);
            Context.Bindings.Samplers[0].Set(sampler);

            Context.Actions.Draw.Elements(BeginMode.Triangles, 36, DrawElementsType.UnsignedShort, 0);
        }