public static GLTexture2D CreateWhiteTexture(int width, int height)
        {
            GLTexture2D texture = new GLTexture2D();

            texture.PixelInternalFormat = PixelInternalFormat.Rgba8;
            texture.PixelFormat         = PixelFormat.Rgba;
            texture.PixelType           = PixelType.UnsignedByte;
            texture.Width  = width; texture.Height = height;
            texture.Target = TextureTarget.Texture2D;
            texture.Bind();

            byte[] buffer = new byte[width * height * 4];
            for (int i = 0; i < width * height * 4; i++)
            {
                buffer[i] = 255;
            }

            GL.TexImage2D(TextureTarget.Texture2D, 0, texture.PixelInternalFormat,
                          texture.Width, texture.Height,
                          0, texture.PixelFormat, texture.PixelType, buffer);

            texture.UpdateParameters();
            texture.Unbind();
            return(texture);
        }
        private void GenerateRender(GLContext control, EventHandler thumbnailUpdate)
        {
            Framebuffer frameBuffer = new Framebuffer(FramebufferTarget.Framebuffer, Width, Height);

            frameBuffer.Bind();

            GL.ClearColor(0, 0, 0, 0);
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
            GL.Viewport(0, 0, control.Width, control.Height);

            var camMtx  = Camera.ViewMatrix;
            var projMtx = Camera.ProjectionMatrix;

            //Draw all the models into the current camera screen
            //     scene.Draw(control, Pass.OPAQUE);
            //     scene.Draw(control, Pass.TRANSPARENT);

            //Disable shader and textures
            GL.UseProgram(0);
            GL.BindTexture(TextureTarget.Texture2D, 0);

            RenderedScreen = (GLTexture2D)frameBuffer.Attachments[1];
            var thumbnail = frameBuffer.ReadImagePixels(true);

            //Dispose frame buffer
            frameBuffer.Dispoe();
            frameBuffer.DisposeRenderBuffer();

            this.Thumbnail = thumbnail;
            thumbnailUpdate?.Invoke(this, EventArgs.Empty);
        }
        public static GLTexture2D FromBitmap(Bitmap image)
        {
            GLTexture2D texture = new GLTexture2D();

            texture.Target = TextureTarget.Texture2D;
            texture.Width  = image.Width; texture.Height = image.Height;
            texture.LoadImage(image);
            return(texture);
        }
        private GLTexture2D CreateColorAttachment(int width, int height, int numSamples)
        {
            GLTexture2D texture = GLTexture2DMultiSampled.CreateUncompressedTexture(width, height,
                                                                                    PixelInternalFormat, PixelFormat.Rgba, PixelType.Float);

            // Don't use mipmaps for color attachments.
            texture.MinFilter = TextureMinFilter.Linear;
            texture.MagFilter = TextureMagFilter.Linear;
            texture.UpdateParameters();
            return(texture);
        }
        public static GLTexture2D FromBitmap(byte[] imageFile)
        {
            Bitmap image = (Bitmap)Bitmap.FromStream(new System.IO.MemoryStream(imageFile));

            GLTexture2D texture = new GLTexture2D();

            texture.Target = TextureTarget.Texture2D;
            texture.Width  = image.Width; texture.Height = image.Height;
            texture.LoadImage(image);
            return(texture);
        }
        public static void Init()
        {
            if (defaultTex != null)
            {
                return;
            }

            defaultTex          = GLTexture2D.FromBitmap(Properties.Resources.DefaultTexture);
            uvTestPattern       = GLTexture2D.FromBitmap(Properties.Resources.UVPattern);
            boneWeightGradient  = GLTexture2D.FromBitmap(Properties.Resources.boneWeightGradient);
            boneWeightGradient2 = GLTexture2D.FromBitmap(Properties.Resources.boneWeightGradient2);
        }
        public static GLTexture2D FromGeneric(STGenericTexture texture, ImageParameters parameters = null)
        {
            if (parameters == null)
            {
                parameters = new ImageParameters();
            }

            GLTexture2D glTexture = new GLTexture2D();

            glTexture.Target = TextureTarget.Texture2D;
            glTexture.Width  = (int)texture.Width;
            glTexture.Height = (int)texture.Height;
            glTexture.LoadImage(texture, parameters);
            return(glTexture);
        }
        public static GLTexture2D CreateFloat32Texture(int width, int height, float[] buffer)
        {
            GLTexture2D texture = new GLTexture2D();

            texture.PixelInternalFormat = PixelInternalFormat.R32f;
            texture.PixelFormat         = PixelFormat.Red;
            texture.PixelType           = PixelType.Float;
            texture.Width  = width; texture.Height = height;
            texture.Target = TextureTarget.Texture2D;
            texture.Bind();

            GL.TexImage2D(TextureTarget.Texture2D, 0, texture.PixelInternalFormat,
                          texture.Width, texture.Height,
                          0, texture.PixelFormat, texture.PixelType, buffer);

            texture.UpdateParameters();
            texture.Unbind();
            return(texture);
        }
        private List <IFramebufferAttachment> CreateColorAttachments(int width, int height, int colorAttachmentsCount, int numSamples)
        {
            var colorAttachments = new List <IFramebufferAttachment>();

            List <DrawBuffersEnum> attachmentEnums = new List <DrawBuffersEnum>();

            for (int i = 0; i < colorAttachmentsCount; i++)
            {
                DrawBuffersEnum attachmentPoint = DrawBuffersEnum.ColorAttachment0 + i;
                attachmentEnums.Add(attachmentPoint);

                GLTexture2D texture = CreateColorAttachment(width, height, numSamples);
                colorAttachments.Add(texture);
                AddAttachment((FramebufferAttachment)attachmentPoint, texture);
            }

            SetDrawBuffers(attachmentEnums.ToArray());

            return(colorAttachments);
        }
        public static GLTexture2D CreateUncompressedTexture(int width, int height,
                                                            PixelInternalFormat format = PixelInternalFormat.Rgba8,
                                                            PixelFormat pixelFormat    = PixelFormat.Rgba,
                                                            PixelType pixelType        = PixelType.UnsignedByte)
        {
            GLTexture2D texture = new GLTexture2D();

            texture.PixelInternalFormat = format;
            texture.PixelFormat         = pixelFormat;
            texture.PixelType           = pixelType;
            texture.Width  = width; texture.Height = height;
            texture.Target = TextureTarget.Texture2D;
            texture.Bind();

            GL.TexImage2D(TextureTarget.Texture2D, 0, format,
                          texture.Width, texture.Height,
                          0, pixelFormat, pixelType, IntPtr.Zero);

            texture.UpdateParameters();
            texture.Unbind();
            return(texture);
        }
        public static GLTexture FromGenericTexture(STGenericTexture texture, ImageParameters parameters = null)
        {
            if (parameters == null)
            {
                parameters = new ImageParameters();
            }

            switch (texture.SurfaceType)
            {
            case STSurfaceType.Texture2D_Array:
                return(GLTexture2DArray.FromGeneric(texture, parameters));

            case STSurfaceType.Texture3D:
                return(GLTexture3D.FromGeneric(texture, parameters));

            case STSurfaceType.TextureCube:
                return(GLTextureCube.FromGeneric(texture, parameters));

            default:
                return(GLTexture2D.FromGeneric(texture, parameters));
            }
        }
        public static GLTexture2D CreateTextureRender(GLTexture texture, int arrayLevel, int mipLevel, bool force = false)
        {
            if (!force && cubemapCache.ContainsKey(texture.ID.ToString()))
            {
                return(cubemapCache[texture.ID.ToString()]);
            }
            else
            {
                if (cubemapCache.ContainsKey(texture.ID.ToString()))
                {
                    cubemapCache[texture.ID.ToString()]?.Dispose();
                }
            }

            int width  = texture.Width * 4;
            int height = texture.Height * 3;

            width  = 512;
            height = 256;

            var shader        = GlobalShaders.GetShader("EQUIRECTANGULAR");
            var textureOutput = GLTexture2D.CreateUncompressedTexture(width, height, PixelInternalFormat.Rgba32f);

            textureOutput.MipCount = texture.MipCount;

            texture.Bind();

            textureOutput.Bind();
            textureOutput.GenerateMipmaps();
            textureOutput.Unbind();

            Framebuffer frameBuffer = new Framebuffer(FramebufferTarget.Framebuffer, width, height);

            frameBuffer.Bind();

            GL.Disable(EnableCap.Blend);

            shader.Enable();
            shader.SetBoolToInt("is_array", texture is GLTextureCubeArray);

            if (texture is GLTextureCubeArray)
            {
                GL.ActiveTexture(TextureUnit.Texture1);
                texture.Bind();
                shader.SetInt("dynamic_texture_array", 1);
            }
            else
            {
                GL.ActiveTexture(TextureUnit.Texture1);
                texture.Bind();
                shader.SetInt("dynamic_texture", 1);
            }

            for (int i = 0; i < textureOutput.MipCount; i++)
            {
                int mipWidth  = (int)(width * Math.Pow(0.5, i));
                int mipHeight = (int)(height * Math.Pow(0.5, i));
                frameBuffer.Resize(mipWidth, mipHeight);

                GL.FramebufferTexture(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0,
                                      textureOutput.ID, i);

                shader.SetInt("arrayLevel", arrayLevel);
                shader.SetInt("mipLevel", mipLevel);

                GL.ClearColor(0, 0, 0, 0);
                GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
                GL.Viewport(0, 0, mipWidth, mipHeight);

                //Draw the texture onto the framebuffer
                ScreenQuadRender.Draw();

                break;
            }

            //Disable shader and textures
            GL.UseProgram(0);
            GL.BindTexture(TextureTarget.Texture2D, 0);

            if (cubemapCache.ContainsKey(texture.ID.ToString()))
            {
                cubemapCache[texture.ID.ToString()] = textureOutput;
            }
            else
            {
                cubemapCache.Add(texture.ID.ToString(), textureOutput);
            }
            return(textureOutput);
        }