예제 #1
0
        public static Reaction <Camera> Renderer(SceneGraph scene,
                                                 int mapSize, ShadowMapType type, bool cascaded)
        {
            var depthFramebuffer = new Framebuffer(FramebufferTarget.Framebuffer);

            _shadowShader = cascaded ?
                            new GLProgram(
                VertexShaderCascaded(),
                GeometryShaderCascaded(),
                DepthFragmentShader()) :
                            new GLProgram(
                VertexShader(),
                type == ShadowMapType.Depth ? DepthFragmentShader() : VarianceFragmentShader());

            _instance = new Shadows(_shadowShader, cascaded);

            Texture depthTexture;
            var     render = React.By <Camera> (_instance.Render);

            if (type == ShadowMapType.Depth || cascaded)
            {
                depthTexture = cascaded ?
                               new Texture(TextureTarget.Texture2DArray, PixelInternalFormat.DepthComponent16,
                                           mapSize, mapSize, CascadedShadowUniforms.MapCount, PixelFormat.DepthComponent,
                                           PixelType.Float, IntPtr.Zero) :
                               new Texture(TextureTarget.Texture2D, PixelInternalFormat.DepthComponent16,
                                           mapSize, mapSize, PixelFormat.DepthComponent, PixelType.Float, IntPtr.Zero);
                depthFramebuffer.AddTexture(FramebufferAttachment.DepthAttachment, depthTexture);
            }
            else
            {
                depthTexture = new Texture(TextureTarget.Texture2D, PixelInternalFormat.Rg32f,
                                           mapSize, mapSize, PixelFormat.Rg, PixelType.Float, IntPtr.Zero);
                depthFramebuffer.AddTexture(FramebufferAttachment.ColorAttachment0, depthTexture);
                depthFramebuffer.AddRenderbuffer(FramebufferAttachment.DepthAttachment,
                                                 RenderbufferStorage.DepthComponent16, mapSize, mapSize);
                var gaussTexture = new Texture(TextureTarget.Texture2D, PixelInternalFormat.Rg32f,
                                               mapSize / 2, mapSize / 2, PixelFormat.Rg, PixelType.Float, IntPtr.Zero);
                render = render.And(GaussianFilter.Both().MapInput((Camera cam) =>
                                                                   Tuple.Create(depthTexture, gaussTexture)));
            }
            scene.GlobalLighting.ShadowMap = depthTexture;

            return(render
                   .DrawBuffer(type == ShadowMapType.Depth ? DrawBufferMode.None : DrawBufferMode.Front)
                   .DepthTest()
                   .Culling()
                   .Viewport(new Vec2i(mapSize, mapSize))
                   .Program(_shadowShader)
                   .Texture(depthTexture)
                   .Framebuffer(depthFramebuffer));
        }
예제 #2
0
        public static Reaction<Camera> Renderer(SceneGraph scene,
			int mapSize, ShadowMapType type, bool cascaded)
        {
            var depthFramebuffer = new Framebuffer (FramebufferTarget.Framebuffer);
            _shadowShader = cascaded ?
                new GLProgram (
                    VertexShaderCascaded (),
                    GeometryShaderCascaded (),
                    DepthFragmentShader ()) :
                new GLProgram (
                    VertexShader (),
                    type == ShadowMapType.Depth ? DepthFragmentShader () : VarianceFragmentShader ());

            _instance = new Shadows (_shadowShader, cascaded);

            Texture depthTexture;
            var render =React.By<Camera> (_instance.Render);
            if (type == ShadowMapType.Depth || cascaded)
            {
                depthTexture = cascaded ?
                    new Texture (TextureTarget.Texture2DArray, PixelInternalFormat.DepthComponent16,
                        mapSize, mapSize, CascadedShadowUniforms.MapCount, PixelFormat.DepthComponent,
                        PixelType.Float, IntPtr.Zero) :
                    new Texture (TextureTarget.Texture2D, PixelInternalFormat.DepthComponent16,
                        mapSize, mapSize, PixelFormat.DepthComponent, PixelType.Float, IntPtr.Zero);
                depthFramebuffer.AddTexture (FramebufferAttachment.DepthAttachment, depthTexture);
            }
            else
            {
                depthTexture = new Texture (TextureTarget.Texture2D, PixelInternalFormat.Rg32f,
                    mapSize, mapSize, PixelFormat.Rg, PixelType.Float, IntPtr.Zero);
                depthFramebuffer.AddTexture (FramebufferAttachment.ColorAttachment0, depthTexture);
                depthFramebuffer.AddRenderbuffer (FramebufferAttachment.DepthAttachment,
                    RenderbufferStorage.DepthComponent16, mapSize, mapSize);
                var gaussTexture = new Texture (TextureTarget.Texture2D, PixelInternalFormat.Rg32f,
                    mapSize / 2, mapSize / 2, PixelFormat.Rg, PixelType.Float, IntPtr.Zero);
                render = render.And (GaussianFilter.Both ().MapInput ((Camera cam) =>
                    Tuple.Create (depthTexture, gaussTexture)));
            }
            scene.GlobalLighting.ShadowMap = depthTexture;

            return render
                .DrawBuffer (type == ShadowMapType.Depth ? DrawBufferMode.None : DrawBufferMode.Front)
                .DepthTest ()
                .Culling ()
                .Viewport (new Vec2i (mapSize, mapSize))
                .Program (_shadowShader)
                .Texture (depthTexture)
                .Framebuffer (depthFramebuffer);
        }