private float CalculateBackfacePercentage(RGB[] buffer, int width, int height)
        {
            GL.ReadPixels<RGB>(0, 0, width, height, PixelFormat.Rgb, PixelType.UnsignedByte, buffer);

            int r_total = 0;
            int b_total = 0;

            Parallel.For(0, buffer.Length, () => new RedBlueTuple { R_Total = 0, B_Total = 0 }, (i, loop, partial) =>
            {
                partial.R_Total += buffer[i].r;
                partial.B_Total += buffer[i].b;
                return partial;
            },
            partial =>
            {
                Interlocked.Add(ref r_total, partial.R_Total);
                Interlocked.Add(ref b_total, partial.B_Total);
            });

            return r_total / (float)(r_total + b_total);
        }
Пример #2
0
        private void CreateCubemapBuffers()
        {
            CubemapFboHandle         = new uint[6];
            CubemapColorTexture      = new uint[6];
            CubemapDepthRenderbuffer = new uint[6];

            cubemapDirections    = new Vector3[6];
            cubemapDirections[0] = new Vector3(0.0f, 0.0f, 1.0f);
            cubemapDirections[1] = new Vector3(0.0f, 0.0f, -1.0f);
            cubemapDirections[2] = new Vector3(0.0f, 1.0f, 0.0f);
            cubemapDirections[3] = new Vector3(0.0f, -1.0f, 0.0f);
            cubemapDirections[4] = new Vector3(1.0f, 0.0f, 0.0f);
            cubemapDirections[5] = new Vector3(-1.0f, 0.0f, 0.0f);
            cubemapUps           = new Vector3[6];
            cubemapUps[0]        = Vector3.UnitY;
            cubemapUps[1]        = Vector3.UnitY;
            cubemapUps[2]        = Vector3.UnitZ;
            cubemapUps[3]        = Vector3.UnitZ;
            cubemapUps[4]        = Vector3.UnitY;
            cubemapUps[5]        = Vector3.UnitY;

            cubemapColorBuffers = new RGB[6][];
            for (int i = 0; i < 6; i++)
            {
                cubemapColorBuffers[i] = new RGB[CubemapWidth * CubemapHeight];
            }

            cubemapDepthBuffers = new float[6][];
            for (int i = 0; i < 6; i++)
            {
                cubemapDepthBuffers[i] = new float[CubemapWidth * CubemapHeight];
            }

            cubemapProjections = new Matrix4[6];

            // Create Color Texture
            GL.GenTextures(6, CubemapColorTexture);
            for (int i = 0; i < CubemapColorTexture.Length; i++)
            {
                GL.BindTexture(TextureTarget.Texture2D, CubemapColorTexture[i]);
                GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest);
                GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Nearest);
                GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Clamp);
                GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Clamp);
                GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba8, CubemapWidth, CubemapHeight, 0, PixelFormat.Rgba, PixelType.UnsignedByte, IntPtr.Zero);
            }

            // test for GL Error here (might be unsupported format)

            GL.BindTexture(TextureTarget.Texture2D, 0); // prevent feedback, reading and writing to the same image is a bad idea

            // Create Depth Renderbuffer
            GL.Ext.GenRenderbuffers(6, CubemapDepthRenderbuffer);
            for (int i = 0; i < CubemapDepthRenderbuffer.Length; i++)
            {
                GL.Ext.BindRenderbuffer(RenderbufferTarget.RenderbufferExt, CubemapDepthRenderbuffer[i]);
                GL.Ext.RenderbufferStorage(RenderbufferTarget.RenderbufferExt, (RenderbufferStorage)All.DepthComponent32, CubemapWidth, CubemapHeight);
            }

            // test for GL Error here (might be unsupported format)

            // Create a FBO and attach the textures
            GL.Ext.GenFramebuffers(6, CubemapFboHandle);
            for (int i = 0; i < CubemapFboHandle.Length; i++)
            {
                GL.Ext.BindFramebuffer(FramebufferTarget.FramebufferExt, CubemapFboHandle[i]);
                GL.Ext.FramebufferTexture2D(FramebufferTarget.FramebufferExt, FramebufferAttachment.ColorAttachment0Ext, TextureTarget.Texture2D, CubemapColorTexture[i], 0);
                GL.Ext.FramebufferRenderbuffer(FramebufferTarget.FramebufferExt, FramebufferAttachment.DepthAttachmentExt, RenderbufferTarget.RenderbufferExt, CubemapDepthRenderbuffer[i]);
            }

            GLEx.CheckFboStatus();

            GL.Ext.BindFramebuffer(FramebufferTarget.FramebufferExt, 0); // return to visible framebuffer
        }
        private void CreateCubemapBuffers()
        {
            CubemapFboHandle = new uint[6];
            CubemapColorTexture = new uint[6];
            CubemapDepthRenderbuffer = new uint[6];

            cubemapDirections = new Vector3[6];
            cubemapDirections[0] = new Vector3(0.0f, 0.0f, 1.0f);
            cubemapDirections[1] = new Vector3(0.0f, 0.0f, -1.0f);
            cubemapDirections[2] = new Vector3(0.0f, 1.0f, 0.0f);
            cubemapDirections[3] = new Vector3(0.0f, -1.0f, 0.0f);
            cubemapDirections[4] = new Vector3(1.0f, 0.0f, 0.0f);
            cubemapDirections[5] = new Vector3(-1.0f, 0.0f, 0.0f);
            cubemapUps = new Vector3[6];
            cubemapUps[0] = Vector3.UnitY;
            cubemapUps[1] = Vector3.UnitY;
            cubemapUps[2] = Vector3.UnitZ;
            cubemapUps[3] = Vector3.UnitZ;
            cubemapUps[4] = Vector3.UnitY;
            cubemapUps[5] = Vector3.UnitY;

            cubemapColorBuffers = new RGB[6][];
            for (int i = 0; i < 6; i++)
                cubemapColorBuffers[i] = new RGB[CubemapWidth * CubemapHeight];

            cubemapDepthBuffers = new float[6][];
            for (int i = 0; i < 6; i++)
                cubemapDepthBuffers[i] = new float[CubemapWidth * CubemapHeight];

            cubemapProjections = new Matrix4[6];

            // Create Color Texture
            GL.GenTextures(6, CubemapColorTexture);
            for (int i = 0; i < CubemapColorTexture.Length; i++)
            {
                GL.BindTexture(TextureTarget.Texture2D, CubemapColorTexture[i]);
                GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest);
                GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Nearest);
                GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Clamp);
                GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Clamp);
                GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba8, CubemapWidth, CubemapHeight, 0, PixelFormat.Rgba, PixelType.UnsignedByte, IntPtr.Zero);
            }

            // test for GL Error here (might be unsupported format)

            GL.BindTexture(TextureTarget.Texture2D, 0); // prevent feedback, reading and writing to the same image is a bad idea

            // Create Depth Renderbuffer
            GL.Ext.GenRenderbuffers(6, CubemapDepthRenderbuffer);
            for (int i = 0; i < CubemapDepthRenderbuffer.Length; i++)
            {
                GL.Ext.BindRenderbuffer(RenderbufferTarget.RenderbufferExt, CubemapDepthRenderbuffer[i]);
                GL.Ext.RenderbufferStorage(RenderbufferTarget.RenderbufferExt, (RenderbufferStorage)All.DepthComponent32, CubemapWidth, CubemapHeight);
            }

            // test for GL Error here (might be unsupported format)

            // Create a FBO and attach the textures
            GL.Ext.GenFramebuffers(6, CubemapFboHandle);
            for (int i = 0; i < CubemapFboHandle.Length; i++)
            {
                GL.Ext.BindFramebuffer(FramebufferTarget.FramebufferExt, CubemapFboHandle[i]);
                GL.Ext.FramebufferTexture2D(FramebufferTarget.FramebufferExt, FramebufferAttachment.ColorAttachment0Ext, TextureTarget.Texture2D, CubemapColorTexture[i], 0);
                GL.Ext.FramebufferRenderbuffer(FramebufferTarget.FramebufferExt, FramebufferAttachment.DepthAttachmentExt, RenderbufferTarget.RenderbufferExt, CubemapDepthRenderbuffer[i]);
            }

            GLEx.CheckFboStatus();

            GL.Ext.BindFramebuffer(FramebufferTarget.FramebufferExt, 0); // return to visible framebuffer
        }