Beispiel #1
0
        public WebGL2Framebuffer(int width, int height, int colourTextures, TextureFormat textureColourFormat, TextureFormat?textureDepthFormat = null)
        {
            if (colourTextures < 1)
            {
                throw new CKGLException("Must have at least 1 colour texture.");
            }
            if (colourTextures > (int)GL.getParameter(MAX_COLOR_ATTACHMENTS_Static) || colourTextures > (int)GL.getParameter(MAX_DRAW_BUFFERS_Static))
            {
                throw new CKGLException("Too many colour textures.");
            }
            if (textureColourFormat.ToWebGL2PixelFormat() == DEPTH_COMPONENT || textureColourFormat.ToWebGL2PixelFormat() == DEPTH_STENCIL)
            {
                throw new CKGLException("textureColourFormat cannot be a depth(stencil) texture.");
            }
            if (textureDepthFormat.HasValue && !(textureDepthFormat.Value.ToWebGL2PixelFormat() == DEPTH_COMPONENT || textureDepthFormat.Value.ToWebGL2PixelFormat() == DEPTH_STENCIL))
            {
                throw new CKGLException("textureDepthFormat is not a depth(stencil) texture.");
            }

            this.width  = width;
            this.height = height;

            camera2D.Width  = width;
            camera2D.Height = height;

            id = GL.createFramebuffer();

            Bind();

            Textures = new Texture[colourTextures];
            double[] drawBuffers = new double[colourTextures];
            for (int i = 0; i < colourTextures; i++)
            {
                Textures[i]    = Texture.Create2D(Width, Height, textureColourFormat);
                drawBuffers[i] = COLOR_ATTACHMENT0 + i;
                GL.framebufferTexture2D(FRAMEBUFFER, COLOR_ATTACHMENT0 + i, (Textures[i] as WebGL2Texture).TextureTarget, (Textures[i] as WebGL2Texture).ID, 0);
                Textures[i].Unbind();
                CheckStatus();
            }
            GL.drawBuffers(drawBuffers);
            CheckStatus();

            if (textureDepthFormat.HasValue)
            {
                DepthStencilTexture = Texture.Create2D(Width, Height, textureDepthFormat.Value);
                GL.framebufferTexture2D(FRAMEBUFFER, textureDepthFormat.Value.ToWebGL2TextureAttachment(), (DepthStencilTexture as WebGL2Texture).TextureTarget, (DepthStencilTexture as WebGL2Texture).ID, 0);
                DepthStencilTexture.Unbind();
                CheckStatus();
            }
        }