static void GenerateCubemap(GLContext control, GLTextureCube texture,
                                    CubemapCamera camera, List <GenericRenderer> models, int numMips)
        {
            GL.BindTexture(TextureTarget.TextureCubeMap, 0);

            int size = CUBEMAP_UPSCALE_SIZE;

            Framebuffer frameBuffer = new Framebuffer(FramebufferTarget.Framebuffer, size, size, PixelInternalFormat.Rgba16f);

            frameBuffer.SetDrawBuffers(DrawBuffersEnum.ColorAttachment0);
            frameBuffer.Bind();

            //Render all 6 faces
            for (int mip = 0; mip < numMips; mip++)
            {
                int mipWidth  = (int)(size * Math.Pow(0.5, mip));
                int mipHeight = (int)(size * Math.Pow(0.5, mip));

                frameBuffer.Resize(mipWidth, mipHeight);
                GL.Viewport(0, 0, mipWidth, mipHeight);

                for (int i = 0; i < 6; i++)
                {
                    //First filter a normal texture 2d face
                    GL.FramebufferTexture2D(FramebufferTarget.Framebuffer,
                                            FramebufferAttachment.ColorAttachment0,
                                            TextureTarget.TextureCubeMapPositiveX + i, texture.ID, mip);

                    //point camera in the right direction
                    camera.SwitchToFace(i);

                    GL.ClearColor(0, 0, 0, 1);
                    GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

                    //render scene to fbo, and therefore to the current face of the cubemap
                    foreach (var model in models)
                    {
                        model.DrawCubeMapScene(control);
                    }
                }
            }

            var errorcheck = GL.CheckFramebufferStatus(FramebufferTarget.Framebuffer);

            if (errorcheck != FramebufferErrorCode.FramebufferComplete)
            {
                throw new Exception(errorcheck.ToString());
            }

            frameBuffer.Dispoe();
            frameBuffer.DisposeRenderBuffer();

            GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
            GL.UseProgram(0);
        }
        //Update all existing cubemap uint objects
        public static void GenerateCubemaps(List <GenericRenderer> targetModels, bool isWiiU)
        {
            var texture = isWiiU ? CubeMapTextureArray : CubeMapTexture;

            if (texture != null)
            {
                texture.Dispose();
            }

            if (isWiiU)
            {
                texture = GLTexture2DArray.CreateUncompressedTexture(CUBEMAP_SIZE, CUBEMAP_SIZE, MAX_LAYER_COUNT * 6, MAX_MIP_LEVEL,
                                                                     PixelInternalFormat.Rgba16f, PixelFormat.Rgba, PixelType.Float);
            }
            else
            {
                texture = GLTextureCubeArray.CreateEmptyCubemap(CUBEMAP_SIZE, MAX_LAYER_COUNT, MAX_MIP_LEVEL,
                                                                PixelInternalFormat.Rgba16f, PixelFormat.Rgba, PixelType.Float);
            }

            GLTextureCube cubemapTexture = GLTextureCube.CreateEmptyCubemap(
                CUBEMAP_UPSCALE_SIZE, PixelInternalFormat.Rgba16f, PixelFormat.Rgba, PixelType.Float, 9);

            //Get a list of cubemaps in the scene
            //The lighting engine has cube map objects with the object placement to draw
            var lightingEngine   = LightingEngine.LightSettings;
            var cubemapEnvParams = lightingEngine.Resources.CubeMapFiles.FirstOrDefault().Value;
            var cubeMapUints     = cubemapEnvParams.CubeMapObjects;
            int layer            = 0;

            foreach (var cubeMap in cubeMapUints)
            {
                var cUint = cubeMap.CubeMapUint;
                //Cubemap has no area assigned skip it
                if (cubeMap.CubeMapUint.Name == string.Empty)
                {
                    continue;
                }

                //Setup the camera to render the cube map faces
                CubemapCamera camera = new CubemapCamera(
                    new Vector3(cUint.Position.X, cUint.Position.Y, cUint.Position.Z)
                    * GLContext.PreviewScale, cUint.Near, cUint.Far);

                var context = new GLContext();
                context.Camera = camera;

                GenerateCubemap(context, cubemapTexture, camera, targetModels, MAX_MIP_LEVEL);

                cubemapTexture.Bind();
                cubemapTexture.GenerateMipmaps();
                cubemapTexture.Unbind();

                //HDR encode and output into the array
                CubemapHDREncodeRT.CreateCubemap(cubemapTexture, texture, layer, MAX_MIP_LEVEL, false, true);

                if (SAVE_TO_DISK)
                {
                    cubemapTexture.SaveDDS(cubeMap.Name + "default.dds");
                }

                layer++;
            }

            cubemapTexture.Dispose();

            //Just generate mips to keep things easier
            texture.Bind();
            texture.GenerateMipmaps();
            texture.Unbind();

            if (SAVE_TO_DISK)
            {
                texture.SaveDDS("Cubemap_Array_HDR.dds");
            }

            if (isWiiU)
            {
                CubeMapTextureArray = texture;
            }
            else
            {
                CubeMapTexture = texture;
            }
        }