Example #1
0
        public OpenGLTexture2D(
            int width,
            int height,
            PixelFormat veldridFormat,
            PixelInternalFormat internalFormat,
            OpenTK.Graphics.OpenGL.PixelFormat pixelFormat,
            PixelType pixelType,
            IntPtr pixelData)
            : base(TextureTarget.Texture2D, width, height)
        {
            _veldridFormat  = veldridFormat;
            _internalFormat = internalFormat;
            _pixelFormat    = pixelFormat;
            _pixelType      = pixelType;

            Bind();

            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Nearest);

            // Set size, load empty data into texture
            GL.TexImage2D(
                TextureTarget.Texture2D,
                0, // level
                internalFormat,
                width, height,
                0, // border
                _pixelFormat,
                _pixelType,
                pixelData);
        }
Example #2
0
        public RenderBuffer(IOpenGL30 gl, int width, int height, PixelInternalFormat internalFormat, int samples)
        {
            if(gl == null)
                throw new ArgumentNullException("gl");

            if(width <= 0)
                throw new ArgumentException("Width must be greater than 0", "width");

            if(height <= 0)
                throw new ArgumentException("Height must be greater than 0.", "height");

            uint handle = gl.GenRenderBuffer();
            if(handle == 0)
                throw new NoHandleCreatedException();

            Handle = handle;
            Width = width;
            Height = height;
            InternalFormat = internalFormat;
            _gl = gl;

            gl.BindRenderbuffer(Constants.Renderbuffer, Handle);
            gl.RenderbufferStorage(Constants.Renderbuffer, (uint)InternalFormat, Width, Height);
            gl.BindRenderbuffer(Constants.Renderbuffer, 0);
        }
Example #3
0
        private static int RawLoadImage(int width, int height, PixelFormat pixelFormat, PixelType pixeltype, IntPtr ptr, bool loadAlpha)
        {
            int texID = GL.GenTexture();

            GL.BindTexture(TextureTarget.Texture2D, texID);

            //Anisotropic filtering
            float maxAniso;

            GL.GetFloat((GetPName)ExtTextureFilterAnisotropic.MaxTextureMaxAnisotropyExt, out maxAniso);
            GL.TexParameter(TextureTarget.Texture2D, (TextureParameterName)ExtTextureFilterAnisotropic.TextureMaxAnisotropyExt, maxAniso);

            PixelInternalFormat pxIntFormat = loadAlpha ? PixelInternalFormat.SrgbAlpha : PixelInternalFormat.Srgb;

            GL.TexImage2D(TextureTarget.Texture2D, 0, pxIntFormat, width, height, 0, pixelFormat, pixeltype, ptr);

            //GL.GenerateMipmap(GenerateMipmapTarget.Texture2D);

            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);

            GL.BindTexture(TextureTarget.Texture2D, 0);

            return(texID);
        }
Example #4
0
        public static Texture CreateTexture(int width, int height, PixelInternalFormat pixelInternalFormat, OpenTK.Graphics.OpenGL.PixelFormat pixelFormat, IntPtr data)
        {
            //Bitmap image = new Bitmap(fileName);
            Texture texture = new Texture(width, height, pixelInternalFormat, pixelFormat, data);

            return(texture);
        }
Example #5
0
        private void Create(int width, int height, PixelInternalFormat PIF, bool depthbuffer)
        {
            textureID = GL.GenTexture();
            GL.BindTexture(TextureTarget.Texture2D, textureID);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (float)TextureMinFilter.Linear);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (float)TextureMagFilter.Linear);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (float)TextureWrapMode.ClampToBorder);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (float)TextureWrapMode.ClampToBorder);
            GL.TexImage2D(TextureTarget.Texture2D, 0, PIF, width, height, 0, PixelFormat.Rgba, PixelType.UnsignedByte, IntPtr.Zero);

            GL.BindTexture(TextureTarget.Texture2D, 0);

            depthID = GL.GenTexture();
            GL.BindTexture(TextureTarget.Texture2D, depthID);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (float)TextureMinFilter.Nearest);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (float)TextureMagFilter.Nearest);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (float)TextureWrapMode.ClampToEdge);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (float)TextureWrapMode.ClampToEdge);
            GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.DepthComponent, width, height, 0, PixelFormat.DepthComponent, PixelType.UnsignedByte, IntPtr.Zero);

            GL.BindTexture(TextureTarget.Texture2D, 0);

            framebufferID = GL.GenFramebuffer();
            GL.BindFramebuffer(FramebufferTarget.Framebuffer, framebufferID);
            GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, TextureTarget.Texture2D, textureID, 0);
            GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.DepthAttachment, TextureTarget.Texture2D, depthID, 0);

            RobustEngine.CheckGLErrors();

            GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
        }
Example #6
0
        // Similiar realisation exists in Material scene object maybe need to be merged in future
        int LoadCumbemapImage(Dictionary <int, Bitmap> loadedImages, PixelInternalFormat textureColorspace)
        {
            int texID = GL.GenTexture();

            GL.BindTexture(TextureTarget.TextureCubeMap, texID);
            foreach (var image in loadedImages)
            {
                BitmapData data = image.Value.LockBits(new Rectangle(0, 0, image.Value.Width, image.Value.Height),
                                                       ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

                GL.TexImage2D(TextureTarget.TextureCubeMapPositiveX + image.Key, 0, textureColorspace, data.Width, data.Height, 0,
                              OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0);

                image.Value.UnlockBits(data);
            }

            GL.TexParameter(TextureTarget.TextureCubeMap, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
            GL.TexParameter(TextureTarget.TextureCubeMap, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
            GL.TexParameter(TextureTarget.TextureCubeMap, TextureParameterName.TextureWrapS, (int)TextureWrapMode.ClampToEdge);
            GL.TexParameter(TextureTarget.TextureCubeMap, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToEdge);
            GL.TexParameter(TextureTarget.TextureCubeMap, TextureParameterName.TextureWrapR, (int)TextureWrapMode.ClampToEdge);

            GL.GenerateMipmap(GenerateMipmapTarget.TextureCubeMap);

            return(texID);
        }
Example #7
0
        public void SetData(ITextureSource src)
        {
            GPUStateMachine.BindTexture(0, src.GetTextureTarget(), id);
            switch (src.GetDimensions())
            {
            case 1:
                GL.TexImage1D(src.GetTextureTarget(), src.GetLevels(), src.GetInternalFormat(), src.GetWidth(), 0, src.GetFormat(), src.GetType(), src.GetPixelData());
                break;

            case 2:
                GL.TexImage2D(src.GetTextureTarget(), src.GetLevels(), src.GetInternalFormat(), src.GetWidth(), src.GetHeight(), 0, src.GetFormat(), src.GetType(), src.GetPixelData());
                break;

            case 3:
                GL.TexImage3D(src.GetTextureTarget(), src.GetLevels(), src.GetInternalFormat(), src.GetWidth(), src.GetHeight(), src.GetDepth(), 0, src.GetFormat(), src.GetType(), src.GetPixelData());
                break;
            }

            GL.GenerateMipmap((GenerateMipmapTarget)src.GetTextureTarget());
            GPUStateMachine.UnbindTexture(0, src.GetTextureTarget());

            this.Width      = src.GetWidth();
            this.Height     = src.GetHeight();
            this.Depth      = src.GetDepth();
            this.LevelCount = src.GetLevels();

            this.format         = src.GetFormat();
            this.internalformat = src.GetInternalFormat();
            this.texTarget      = src.GetTextureTarget();
        }
        /// <summary>
        /// Information about implementation-dependent support for internal formats can be queried with the command GetInternalformativ
        /// No more than 1 int will be returned. If more data are available, they will be ignored and no error will be generated.
        /// </summary>
        /// <param name="target">target indicates the usage of the internalformat, and must be one the targets listed in enum.</param>
        /// <param name="internalFormat">internalformat can be any value</param>
        /// <param name="pname">The INTERNALFORMAT_SUPPORTED pname can be used to determine if the internal format is supported, and the other pnames are defined in terms of whether or not the format is supported</param>
        /// <returns>No more than 1 int will be returned. If more data are available, they will be ignored and no error will be generated.</returns>
        public static int GetInternalformativ(GetInternalformatTargets target, PixelInternalFormat internalFormat, GetInternalformatParameters pname)
        {
            int tmp = 0;

            Delegates.glGetInternalformativ(target, internalFormat, pname, 1, ref tmp);
            return(tmp);
        }
        /// <summary>
        /// Create or update texture using this pixel format - special. Used for depth or stencil buffers
        /// See <href>https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glTexImage2D.xhtml</href>
        /// </summary>
        /// <param name="width">Width of texture</param>
        /// <param name="height">Height of texture</param>
        /// <param name="pixelinternalformat">Pixel internal format</param>
        /// <param name="pixelformat">Pixel format</param>
        /// <param name="pixeltype">Pixel type</param>
        public void CreateOrUpdateTexturePixelFormat(int width, int height, PixelInternalFormat pixelinternalformat, PixelFormat pixelformat, PixelType pixeltype) // make with a pixel format..
        {
            if (Id < 0 || Width != width || Height != height)                                                                                                      // if not there, or changed, we can't just replace it, size is fixed. Delete it
            {
                if (Id >= 0)
                {
                    Dispose();
                }

                InternalFormat = 0;         // PixelInternalFormat does not fit within this, so zero it
                Width          = width;
                Height         = height;
                MipMapLevels   = 1;

                GL.CreateTextures(TextureTarget.Texture2D, 1, out int id);
                GLStatics.RegisterAllocation(typeof(GLTexture2D));
                GLStatics.Check();
                Id = id;

                GL.BindTexture(TextureTarget.Texture2D, Id);

                GL.TexImage2D(TextureTarget.Texture2D, 0, pixelinternalformat, width, height, 0, pixelformat, pixeltype, (IntPtr)0);     // we don't actually load data in, so its a null ptr.

                GLStatics.Check();
            }
        }
Example #10
0
        public static FrameBuffer Create(GraphicsDevice device, int numColorBuffers, PixelInternalFormat textureFormat = PixelInternalFormat.Rgba8, float scale = 1.0f, bool depthTexture = true)
        {
            int width  = (int)(device.Width * scale);
            int height = (int)(device.Height * scale);

            return(Create(device, numColorBuffers, textureFormat, width, height, depthTexture));
        }
Example #11
0
        public static Texture Create(int width, int height, byte components = 4, bool floatingPoint = false)
        {
            Debug.Assert(components < 5);
            PixelInternalFormat internalFormat   = PixelInternalFormat.Rgba8;
            PixelFormat         inputPixelFormat = PixelFormat.Rgba;
            PixelType           type             = PixelType.UnsignedByte;

            if (floatingPoint)
            {
                type = PixelType.Float;
                switch (components)
                {
                case 1: internalFormat = PixelInternalFormat.R32f; inputPixelFormat = PixelFormat.Red; break;

                case 2: internalFormat = PixelInternalFormat.Rg32f; inputPixelFormat = PixelFormat.Rg; break;

                case 3: internalFormat = PixelInternalFormat.Rgb32f; inputPixelFormat = PixelFormat.Rgb; break;
                }
            }
            else
            {
                switch (components)
                {
                case 1: internalFormat = PixelInternalFormat.R8; inputPixelFormat = PixelFormat.Red; break;

                case 2: internalFormat = PixelInternalFormat.Rg8; inputPixelFormat = PixelFormat.Rg; break;

                case 3: internalFormat = PixelInternalFormat.Rgb8; inputPixelFormat = PixelFormat.Rgb; break;
                }
            }
            return(Texture.Create(width, height, internalFormat, inputPixelFormat, type));
        }
Example #12
0
        public static Cubemap FromArrays <T>(
            string name,
            int width, int height,
            T[] right, T[] left, T[] top, T[] bottom, T[] back, T[] front,
            PixelInternalFormat internalFormat = PixelInternalFormat.Rgba,
            PixelFormat format  = PixelFormat.Rgba,
            PixelType pixelType = PixelType.UnsignedByte) where T : struct
        {
            Cubemap cubemap = new Cubemap(name);

            cubemap._handle = GL.GenTexture();
            GL.BindTexture(TextureTarget.TextureCubeMap, cubemap._handle);

            //upload
            GL.TexImage2D(TextureTarget.TextureCubeMapPositiveX, 0, internalFormat, width, height, 0, format, pixelType, right);
            GL.TexImage2D(TextureTarget.TextureCubeMapNegativeX, 0, internalFormat, width, height, 0, format, pixelType, left);
            GL.TexImage2D(TextureTarget.TextureCubeMapPositiveY, 0, internalFormat, width, height, 0, format, pixelType, top);
            GL.TexImage2D(TextureTarget.TextureCubeMapNegativeY, 0, internalFormat, width, height, 0, format, pixelType, bottom);
            GL.TexImage2D(TextureTarget.TextureCubeMapPositiveZ, 0, internalFormat, width, height, 0, format, pixelType, back);
            GL.TexImage2D(TextureTarget.TextureCubeMapNegativeZ, 0, internalFormat, width, height, 0, format, pixelType, front);

            //params
            GL.TexParameter(TextureTarget.TextureCubeMap, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
            GL.TexParameter(TextureTarget.TextureCubeMap, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
            GL.TexParameter(TextureTarget.TextureCubeMap, TextureParameterName.TextureWrapS, (int)TextureWrapMode.ClampToEdge);
            GL.TexParameter(TextureTarget.TextureCubeMap, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToEdge);
            GL.TexParameter(TextureTarget.TextureCubeMap, TextureParameterName.TextureWrapR, (int)TextureWrapMode.ClampToEdge);

            GL.ObjectLabel(ObjectLabelIdentifier.Texture, cubemap._handle, name.Length, name);

            GL.BindTexture(TextureTarget.TextureCubeMap, 0);

            return(cubemap);
        }
Example #13
0
 public Texture2D(int width, int height, PixelInternalFormat internalformat, OpenTK.Graphics.OpenGL4.PixelFormat format, PixelType type, IntPtr data) : base(TextureTarget.Texture2D)
 {
     GL.TexImage2D(TextureTarget.Texture2D, 0, internalformat, width, height, 0, format, PixelType.UnsignedByte, data);
     this.format         = format;
     this.internalformat = internalformat;
     this.pixeltype      = type;
 }
Example #14
0
        public void Set(int Index, GalTexture Tex)
        {
            GL.ActiveTexture(TextureUnit.Texture0 + Index);

            int Handle = EnsureTextureInitialized(Index);

            GL.BindTexture(TextureTarget.Texture2D, Handle);

            int W = Tex.Width;
            int H = Tex.Height;

            byte[] Data = Tex.Data;

            int Length = Data.Length;

            if (IsCompressedTextureFormat(Tex.Format))
            {
                PixelInternalFormat Pif = OGLEnumConverter.GetCompressedTextureFormat(Tex.Format);

                GL.CompressedTexImage2D(TextureTarget.Texture2D, 0, Pif, W, H, 0, Length, Data);
            }
            else
            {
                //TODO: Get those from Texture format.
                const PixelInternalFormat Pif = PixelInternalFormat.Rgba;

                const PixelFormat Pf = PixelFormat.Rgba;

                const PixelType Pt = PixelType.UnsignedByte;

                GL.TexImage2D(TextureTarget.Texture2D, 0, Pif, W, H, 0, Pf, Pt, Data);
            }
        }
Example #15
0
        public static Texture FromPointer(string name, int width, int height, IntPtr data,
                                          PixelInternalFormat internalFormat = PixelInternalFormat.Rgba,
                                          PixelFormat format  = PixelFormat.Rgba,
                                          PixelType pixelType = PixelType.UnsignedByte)
        {
            Texture texture = new Texture(name);

            //copy pixel array
            texture._pixelInternalFormat = internalFormat;
            texture._pixelFormat         = format;
            texture._pixelType           = pixelType;

            texture._handle = GL.GenTexture();
            GL.BindTexture(TextureTarget.Texture2D, texture._handle);
            GL.TexImage2D(TextureTarget.Texture2D, 0, internalFormat, width, height, 0, format, pixelType, data);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.ClampToEdge);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToEdge);

            GL.BindTexture(TextureTarget.Texture2D, 0);

            GL.ObjectLabel(ObjectLabelIdentifier.Texture, texture._handle, name.Length, name);

            texture._size = new Vector2i(width, height);

            return(texture);
        }
Example #16
0
 private void resize(int width, int height, PixelInternalFormat pixelFormat)
 {
     GL.TexImage2D(TextureTarget.Texture2D, 0, pixelFormat, width, height, 0,
                   PixelFormat.Rgba, PixelType.UnsignedByte, IntPtr.Zero);
     Width  = width;
     Height = height;
 }
Example #17
0
        public Texture2D(TextureNativeSectionData tex)
            : base(TextureTarget.Texture2D)
        {
            Width  = tex.Width;
            Height = tex.Height;

            WrapModeU = FindWrapMode(tex.WrapU);
            WrapModeV = FindWrapMode(tex.WrapV);

            InternalFormat = FindInternalFormat(tex.Compression, tex.Format);
            ExternalFormat = FindExternalFormat(tex.Compression, tex.Format);

            Compressed = tex.Compression != TextureNativeSectionData.CompressionMode.None;
            Alpha      = tex.Alpha;

            MinFilter = FindMinFilter(tex.FilterFlags);

            MipMapCount     = 1;    // tex.MipMapCount;
            ContainsMipMaps = (tex.Format & TextureNativeSectionData.RasterFormat.ExtMipMap) != 0;
            GenerateMipMaps = true; // ( tex.Format & TextureNativeSectionData.RasterFormat.ExtAutoMipMap ) != 0;

            ImageLevelSizes = new int[MipMapCount];
            for (int i = 0; i < MipMapCount; ++i)
            {
                ImageLevelSizes[i] = FindImageDataSize(Width >> i, Height >> i, tex.Compression, tex.Format);
            }

            ImageLevelData = new byte[MipMapCount][];
            for (int i = 0; i < MipMapCount; ++i)
            {
                ImageLevelData[i] = tex.ImageLevelData[i];
            }
        }
Example #18
0
        public static void TexImage2D(TextureTarget target, int level,
                                      PixelInternalFormat internalFormat,
                                      int width, int height, int border,
                                      PixelFormat format,
                                      PixelType type,
                                      Byte[] pixels)
        {
#if USE_OPENGL
            if (HardwareAvailable)
            {
                OpenTK.Graphics.OpenGL.GL.TexImage2D(
                    (OpenTK.Graphics.OpenGL.TextureTarget)target, level,
                    (OpenTK.Graphics.OpenGL.PixelInternalFormat)internalFormat,
                    width, height, border,
                    (OpenTK.Graphics.OpenGL.PixelFormat)format,
                    (OpenTK.Graphics.OpenGL.PixelType)type, pixels);
            }
#else
            OpenTK.Graphics.ES11.GL.TexImage2D(
                (OpenTK.Graphics.ES11.All)target, level,
                (int)internalFormat,
                width, height, border,
                (OpenTK.Graphics.ES11.All)format,
                (OpenTK.Graphics.ES11.All)type, pixels);
#endif
        }
Example #19
0
 public static Texture2D FromFile(string path, PixelInternalFormat format)
 {
     using (var stream = new FileStream(path, FileMode.Open))
     {
         return(FromStream(stream));
     }
 }
Example #20
0
        internal static RenderbufferStorage TexFormatToRboFormat(PixelInternalFormat format)
        {
            switch (format)
            {
            case PixelInternalFormat.Alpha:
            case PixelInternalFormat.Alpha8:
                return(RenderbufferStorage.Alpha8);

            case PixelInternalFormat.R8:
            case PixelInternalFormat.Luminance:
                return(RenderbufferStorage.R8);

            case PixelInternalFormat.Rg8:
            case PixelInternalFormat.LuminanceAlpha:
                return(RenderbufferStorage.Rg8);

            case PixelInternalFormat.Rgb:
            case PixelInternalFormat.Rgb8:
                return(RenderbufferStorage.Rgb8);

            default:
            case PixelInternalFormat.Rgba:
            case PixelInternalFormat.Rgba8:
                return(RenderbufferStorage.Rgba8);

            case PixelInternalFormat.Rgba16f:
                return(RenderbufferStorage.Rgba16f);

            case PixelInternalFormat.Rgba16:
                return(RenderbufferStorage.Rgba16);
            }
        }
Example #21
0
 public static Texture2D FromStream(Stream stream, PixelInternalFormat format)
 {
     using (var bitmap = new System.Drawing.Bitmap(stream))
     {
         return(FromBitmap(bitmap));
     }
 }
Example #22
0
        private void SetupTexture(int Handle, int Width, int Height)
        {
            GL.BindTexture(TextureTarget.Texture2D, Handle);

            const int MinFilter = (int)TextureMinFilter.Linear;
            const int MagFilter = (int)TextureMagFilter.Linear;

            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, MinFilter);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, MagFilter);

            (PixelFormat Format, PixelType Type) = OGLEnumConverter.GetTextureFormat(GalTextureFormat.A8B8G8R8);

            const PixelInternalFormat InternalFmt = PixelInternalFormat.Rgba;

            const int Level  = 0;
            const int Border = 0;

            GL.TexImage2D(
                TextureTarget.Texture2D,
                Level,
                InternalFmt,
                Width,
                Height,
                Border,
                Format,
                Type,
                IntPtr.Zero);
        }
Example #23
0
        private MeshTexture TextureFromFile(string filename, string directory, string typeName)
        {
            string resPath = directory + @"\" + filename;

            MeshTexture texture = new MeshTexture
            {
                Type = typeName,
                Path = filename
            };

            // load texture file with StbImage
            var io      = new System.IO.FileStream(resPath, System.IO.FileMode.Open);
            var resLoad = ImageResult.FromStream(io);

            int             width = resLoad.Width, height = resLoad.Height;
            ColorComponents nrComponents = resLoad.SourceComp;

            if (resLoad != null)
            {
                PixelInternalFormat format = 0;
                if (nrComponents == ColorComponents.Grey)
                {
                    format = PixelInternalFormat.CompressedRed;
                }
                else if (nrComponents == ColorComponents.RedGreenBlue)
                {
                    format = PixelInternalFormat.Rgb;
                }
                else if (nrComponents == ColorComponents.RedGreenBlueAlpha)
                {
                    format = PixelInternalFormat.Rgba;
                }

                // send necessary actions to dispatcher
                Dispatcher.ActionsQueue.Enqueue(() =>
                {
                    // load and generate the texture
                    GL.GenTextures(1, out texture.ID);

                    GL.BindTexture(TextureTarget.Texture2D, texture.ID);
                    GL.TexImage2D(TextureTarget.Texture2D, 0, format, width, height, 0, (PixelFormat)format, PixelType.UnsignedByte, resLoad.Data);
                    GL.GenerateMipmap(GenerateMipmapTarget.Texture2D);

                    // set the texture wrapping/filtering options (on the currently bound texture object)
                    GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)OpenTK.Graphics.OpenGL4.TextureWrapMode.Repeat);
                    GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)OpenTK.Graphics.OpenGL4.TextureWrapMode.Repeat);
                    GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.LinearMipmapLinear);
                    GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
                });
            }
            else
            {
                // TODO: throw new exception, although not necessarily - it's already implemented in ImageResult.FromResult
            }

            // explicitly destroy I/O stream object
            io.Dispose();

            return(texture);
        }
Example #24
0
        public Texture2D( TextureNativeSectionData tex )
            : base(TextureTarget.Texture2D)
        {
            Width = tex.Width;
            Height = tex.Height;

            WrapModeU = FindWrapMode( tex.WrapU );
            WrapModeV = FindWrapMode( tex.WrapV );

            InternalFormat = FindInternalFormat( tex.Compression, tex.Format );
            ExternalFormat = FindExternalFormat( tex.Compression, tex.Format );

            Compressed = tex.Compression != TextureNativeSectionData.CompressionMode.None;
            Alpha = tex.Alpha;

            MinFilter = FindMinFilter( tex.FilterFlags );

            MipMapCount = 1; // tex.MipMapCount;
            ContainsMipMaps = ( tex.Format & TextureNativeSectionData.RasterFormat.ExtMipMap ) != 0;
            GenerateMipMaps = true; // ( tex.Format & TextureNativeSectionData.RasterFormat.ExtAutoMipMap ) != 0;

            ImageLevelSizes = new int[ MipMapCount ];
            for ( int i = 0; i < MipMapCount; ++i )
                ImageLevelSizes[ i ] = FindImageDataSize( Width >> i, Height >> i, tex.Compression, tex.Format );

            ImageLevelData = new byte[ MipMapCount ][];
            for ( int i = 0; i < MipMapCount; ++i )
                ImageLevelData[ i ] = tex.ImageLevelData[ i ];
        }
        public override DeviceTexture2D CreateTexture(
            int mipLevels,
            int width,
            int height,
            PixelFormat format,
            DeviceTextureCreateOptions createOptions)
        {
            OpenTK.Graphics.OpenGL.PixelFormat pixelFormat = OpenGLFormats.MapPixelFormat(format);
            PixelInternalFormat pixelInternalFormat        = OpenGLFormats.MapPixelInternalFormat(format);

            if (createOptions == DeviceTextureCreateOptions.DepthStencil)
            {
                if (format != PixelFormat.R16_UInt)
                {
                    throw new NotImplementedException("R16_UInt is the only supported depth texture format.");
                }

                pixelFormat         = OpenTK.Graphics.OpenGL.PixelFormat.DepthComponent;
                pixelInternalFormat = PixelInternalFormat.DepthComponent16;
            }

            return(new OpenGLTexture2D(
                       mipLevels,
                       width,
                       height,
                       format,
                       pixelInternalFormat,
                       pixelFormat,
                       OpenGLFormats.MapPixelType(format)));
        }
Example #26
0
        /// <summary>
        /// Looks up most suitable internal and clientside format specified by the parameters
        /// </summary>
        /// <param name="components">The number of components in the texture (1 2 3 or 4)</param>
        /// <param name="type">The datatype that is used on the client side</param>
        /// <param name="internalFormat">The looked up GL data format for the server side</param>
        /// <param name="format">The looked up GL data format for the client side</param>
        public static void GetFormat(int components, Type type, out PixelInternalFormat internalFormat,
                                     out PixelFormat format)
        {
            if (components < 1 || components > 4)
            {
                throw new ArgumentOutOfRangeException("components", components, "Components must be between 1 and 4.");
            }

            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            var index = components - 1;

            format = _formats[index];

            PixelInternalFormat[] internalTable;
            if (!_internalFormats.TryGetValue(type, out internalTable))
            {
                throw new ArgumentException("Unsupported texture data type.", "type");
            }

            internalFormat = internalTable[index];
        }
Example #27
0
        public static Texture FromPointer(string name, int width, int height, IntPtr data,
                                          PixelInternalFormat internalFormat = PixelInternalFormat.Rgba,
                                          PixelFormat format  = PixelFormat.Rgba,
                                          PixelType pixelType = PixelType.UnsignedByte,
                                          string id           = null)
        {
            Texture texture = new Texture(name, id);

            //copy pixel array
            int singlePixelSize = TextureHelper.GetSinglePixelSize(internalFormat, pixelType);

            texture.rawData = new byte[width * height * singlePixelSize];
            Marshal.Copy(data, texture.rawData, 0, width * height * singlePixelSize);

            texture.pixelInternalFormat = internalFormat;
            texture.pixelFormat         = format;
            texture.pixelType           = pixelType;

            texture.handle = GL.GenTexture();
            GL.BindTexture(TextureTarget.Texture2D, texture.handle);
            GL.TexImage2D(TextureTarget.Texture2D, 0, internalFormat, width, height, 0, format, pixelType, data);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.ClampToEdge);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToEdge);

            GL.ObjectLabel(ObjectLabelIdentifier.Texture, texture.handle, name.Length, name);

            GL.BindTexture(TextureTarget.Texture2D, 0);

            texture.Size = new Vector2i(width, height);

            return(texture);
        }
Example #28
0
 public unsafe static void TexImage3D <T>(TextureTarget target, int level, PixelInternalFormat internalFormat, int width, int height, int depth, int border, PixelFormat format, PixelType type, [In] T[] pixels) where T : unmanaged
 {
     fixed(T *ptr = &(pixels != null && pixels.Length != 0 ? ref pixels[0] : ref *(T *)null))
     {
         TexImage3D(target, level, internalFormat, width, height, depth, border, format, type, (IntPtr)ptr);
     }
 }
Example #29
0
        public Texture2D(System.Drawing.Bitmap bitmap, bool genMipmap = true, PixelInternalFormat internalFormat = PixelInternalFormat.Rgba) : this(bitmap.Width, bitmap.Height)
        {
            internal_format = internalFormat;

            /*
             * for (int x = 0; x < Width; x++) {
             *  for (int y = 0; y < Height; y++) {
             *      Pixels[x, y] = bitmap.GetPixel(x, y);
             *  }
             * }
             */


            var data  = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, bitmap.PixelFormat);
            var bytes = new byte[data.Stride * bitmap.Height];

            System.Runtime.InteropServices.Marshal.Copy(data.Scan0, bytes, 0, bytes.Length);

            var bpp = data.Stride / data.Width;

            for (int i = 0; i < bytes.Length; i += bpp)
            {
                var pixelindex = (i / bpp);
                pixels[pixelindex % bitmap.Width, pixelindex / bitmap.Width] = new color32(bytes[i + 2], bytes[i + 1], bytes[i], bpp == 4 ? bytes[i + 3] : (byte)255);
            }
            bitmap.UnlockBits(data);

            apply(genMipmap);
        }
Example #30
0
        public void SetPixelFormatFromG1TextureCompressionType(byte comp)
        {
            switch (comp)
            {
            case 0x1:
                pixelInternalFormat = PixelInternalFormat.Rgba8;
                pixelFormat         = PixelFormat.Bgra;
                pixelType           = PixelType.UnsignedByte;
                break;

            case 0x2:
                pixelInternalFormat = PixelInternalFormat.Rgba8;
                pixelFormat         = PixelFormat.Rgba;
                pixelType           = PixelType.UnsignedByte;
                break;

            case 0x6:
            case 0x59:
                pixelInternalFormat = PixelInternalFormat.CompressedRgbS3tcDxt1Ext;
                break;

            case 0x8:
            case 0x5b:
                pixelInternalFormat = PixelInternalFormat.CompressedRgbaS3tcDxt5Ext;
                break;

            default:
                throw new NotImplementedException($"Unknown pixel format: 0x{comp:X}");
            }
        }
        public DepthTexture(int width, int height, PixelInternalFormat pixelInternalFormat)
            : base()
        {
            Target = TextureTarget.Texture2D;
            PixelInternalFormat = pixelInternalFormat;
            PixelFormat         = PixelFormat.DepthComponent;
            PixelType           = PixelType.Float;
            Width  = width;
            Height = height;

            // Set texture settings.
            Bind();

            GL.TexImage2D(Target, 0, PixelInternalFormat, Width, Height, 0, PixelFormat, PixelType, IntPtr.Zero);
            MagFilter = TextureMagFilter.Nearest;
            MinFilter = TextureMinFilter.Nearest;

            // Use white for values outside the depth map's border.
            WrapS = TextureWrapMode.ClampToBorder;
            WrapT = TextureWrapMode.ClampToBorder;
            UpdateParameters();

            GL.TexParameter(Target, TextureParameterName.TextureCompareMode, (int)TextureCompareMode.None);
            GL.TexParameter(Target, TextureParameterName.TextureBorderColor, new float[] { 1, 1, 1, 1 });

            Unbind();
        }
Example #32
0
        private Texture(int width, int height, PixelInternalFormat pixelInternalFormat, OpenTK.Graphics.OpenGL.PixelFormat pixelFormat, IntPtr data)
        {
            ID = GL.GenTexture();
            GL.BindTexture(TextureTarget.Texture2D, ID);

            //BitmapData data = image.LockBits(
            //    new System.Drawing.Rectangle(0, 0, image.Width, image.Height),
            //    ImageLockMode.ReadOnly,
            //    System.Drawing.Imaging.PixelFormat.Format32bppArgb
            //);

            GL.TexImage2D(
                TextureTarget.Texture2D,
                0,
                pixelInternalFormat,
                width,
                height,
                0,
                pixelFormat,
                PixelType.UnsignedByte,
                data
                );

            //image.UnlockBits(data);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureParameterName.ClampToEdge);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureParameterName.ClampToEdge);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
            //GL.GenerateMipmap(GenerateMipmapTarget.Texture2D);
        }
Example #33
0
 public Texture(int id, int width, int height, TextureTarget target, PixelInternalFormat format)
 {
     Width = width;
     Height = height;
     ID = id;
     TextureTarget = target;
     PixelInternalFormat = format;
 }
Example #34
0
 public Attachment(AttachmentPoint attachmentPoint, PixelFormat pixelFormat, PixelInternalFormat pixelInternalFormat, PixelType pixelType, int index = 0, bool mipmaps = false)
 {
     AttachmentPoint = attachmentPoint;
     PixelFormat = pixelFormat;
     PixelInternalFormat = pixelInternalFormat;
     PixelType = pixelType;
     Index = index;
     MipMaps = mipmaps;
 }
Example #35
0
 public Framebuffer AttachDepth(PixelInternalFormat internalFormat, PixelFormat format, PixelType type, InterpolationMode interpolation)
 {
     this.BufferTextures[FboAttachment.DepthAttachment] = new Texture2D (TextureTarget.Texture2D, internalFormat, format, type, interpolation, false, this.Width, this.Height);
     this.Bind ();
     this.BufferTextures[FboAttachment.DepthAttachment].Bind ();
     GL.FramebufferTexture (this.Target, FramebufferAttachment.DepthAttachment, this.BufferTextures[FboAttachment.DepthAttachment].TextureId, 0);
     this.Unbind ();
     return this;
 }
 protected RenderTargetTexture(Vector2I size, PixelFormat pixelFormat, PixelInternalFormat internalFormat)
     : base(size.X, size.Y)
 {
     Init2D(pixelFormat, internalFormat);
     SetParameters(
            TextureMinFilter.Nearest,
            TextureMagFilter.Nearest,
            TextureWrapMode.ClampToEdge);
 }
Example #37
0
			public TextureAttributes(int Width, int Height, int Depth, PixelInternalFormat InternalFormat, OpenTK.Graphics.OpenGL.PixelFormat PixelFormat, PixelType PixelType)
			{
				this.Width = Width;
				this.Height = Height;
				this.Depth = Depth;
				this.InternalFormat = InternalFormat;
				this.PixelFormat = PixelFormat;
				this.PixelType = PixelType;
			}
Example #38
0
 public Framebuffer AttachTexture(FboAttachment attachment, DrawBuffersEnum mode, PixelInternalFormat internalFormat, PixelFormat format, PixelType type, InterpolationMode interpolation)
 {
     this.Attachments.Add (mode);
     this.BufferTextures[attachment] = new Texture2D (TextureTarget.Texture2D, internalFormat, format, type, interpolation, false, this.Width, this.Height);
     this.Bind ();
     this.BufferTextures[attachment].Bind ();
     GL.FramebufferTexture (this.Target, (FramebufferAttachment) mode, this.BufferTextures[attachment].TextureId, 0);
     this.Unbind ();
     return this;
 }
Example #39
0
        protected Texture(TextureTarget type, PixelInternalFormat format, OpenTK.Graphics.OpenGL.PixelFormat format2, int num) {
            ID = GL.GenTexture();

            Type = type;
            Format = format;
            Number = num;

            Activate();

        }
Example #40
0
        internal static void GetOpenGLTextureFormat(SurfaceFormat format, out PixelInternalFormat glInternalFormat, out PixelFormat glFormat, out PixelType glType)
        {
            glInternalFormat = PixelInternalFormat.Rgba;
            glFormat = PixelFormat.Rgba;
            glType = PixelType.UnsignedByte;

            switch (format)
            {
                case SurfaceFormat.Color:
                    glInternalFormat = PixelInternalFormat.Rgba;
                    glFormat = PixelFormat.Rgba;
                    glType = PixelType.UnsignedByte;
                    break;
                case SurfaceFormat.Bgr565:
                    glInternalFormat = PixelInternalFormat.Rgb;
                    glFormat = PixelFormat.Rgb;
                    glType = PixelType.UnsignedShort565;
                    break;
                case SurfaceFormat.Bgra4444:
                    glInternalFormat = PixelInternalFormat.Rgba4;
                    glFormat = PixelFormat.Rgba;
                    glType = PixelType.UnsignedShort4444;
                    break;
                case SurfaceFormat.Bgra5551:
                    glInternalFormat = PixelInternalFormat.Rgba;
                    glFormat = PixelFormat.Rgba;
                    glType = PixelType.UnsignedShort5551;
                    break;
                case SurfaceFormat.Alpha8:
                    glInternalFormat = PixelInternalFormat.Luminance;
                    glFormat = PixelFormat.Luminance;
                    glType = PixelType.UnsignedByte;
                    break;
                case SurfaceFormat.Dxt1:
                    glInternalFormat = PixelInternalFormat.CompressedRgbaS3tcDxt1Ext;
                    glFormat = (PixelFormat)All.CompressedTextureFormats;
                    break;
                case SurfaceFormat.Dxt3:
                    glInternalFormat = PixelInternalFormat.CompressedRgbaS3tcDxt3Ext;
                    glFormat = (PixelFormat)All.CompressedTextureFormats;
                    break;
                case SurfaceFormat.Dxt5:
                    glInternalFormat = PixelInternalFormat.CompressedRgbaS3tcDxt5Ext;
                    glFormat = (PixelFormat)All.CompressedTextureFormats;
                    break;

                case SurfaceFormat.Single:
                    glInternalFormat = PixelInternalFormat.R32f;
                    glFormat = PixelFormat.Red;
                    glType = PixelType.Float;
                    break;
                default:
                    throw new NotSupportedException();
            }
        }
Example #41
0
 public PixelFormatDescriptor(
     System.Drawing.Imaging.PixelFormat drawingFormat,
     PixelInternalFormat glInternalPixelFormat,
     PixelFormat glPixelFormat,
     PixelType glPixelType)
 {
     DrawingFormat = drawingFormat;
     GLPixelInternalFormat = glInternalPixelFormat;
     GLPixelFormat = glPixelFormat;
     GLPixelType = glPixelType;
 }
Example #42
0
 public static Texture Create(int width, int height, PixelInternalFormat internalFormat = PixelInternalFormat.Rgba8
     , PixelFormat inputPixelFormat = PixelFormat.Rgba, PixelType type = PixelType.UnsignedByte)
 {
     var texture = new Texture();
     //create empty texture of given size
     texture.LoadPixels(IntPtr.Zero, width, height, internalFormat, inputPixelFormat, type);
     //set default parameters for filtering and clamping
     texture.FilterBilinear();
     texture.WrapMode(TextureWrapMode.Repeat);
     return texture;
 }
Example #43
0
        public Texture(int width, int height, TextureTarget type, PixelInternalFormat format, OpenTK.Graphics.OpenGL.PixelFormat format2, int num)
        {
            ID = GL.GenTexture();

            Type = type;
            Format = format;
            Number = num;

            Activate();

            GL.TexImage2D(Type, 0, Format, width, height, 0, format2, PixelType.UnsignedByte, IntPtr.Zero);
        }
Example #44
0
        /// <summary>
        /// Creates a new managed 2D Texture with an explicitly specified format
        /// </summary>
        /// <param name="width">The width in Pixels of the Texture</param>
        /// <param name="height">The height in Pixels of the Texture</param>
        /// <param name="format">The format to use</param>
        /// <param name="target">The target this Texture will be bound to</param>
        public Texture2D(int width, int height, PixelInternalFormat format,  TextureTarget target = TextureTarget.Texture2D)
            : base(target)
        {
            _internalFormat = format;
            Activate();
            GL.TexParameter(Target, TextureParameterName.TextureWrapS, (int)TextureWrapMode.ClampToEdge); // GL_REPEAT
            GL.TexParameter(Target, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToEdge); // GL_REPEAT
            GL.TexParameter(Target, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
            GL.TexParameter(Target, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);

            Resize(width, height);
        }
Example #45
0
 public void TexImage2D(
     TextureTarget target,
     int level,
     PixelInternalFormat internalFormat,
     int width,
     int height,
     int border,
     PixelFormat format,
     PixelType type,
     IntPtr pixels)
 {
     GraphicsContext.Assert();
     GL.TexImage2D(target, level, internalFormat, width, height, border, format, type, pixels);
     OpenGlErrorHelper.CheckGlError();
 }
Example #46
0
        public GLTexture2D(string name, IBitmap bmp, bool genmipmaps = false, bool linearfilter = false, OpenTK.Graphics.OpenGL.PixelFormat sourceformat = OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelInternalFormat destformat = PixelInternalFormat.Rgba, PixelType sourcetype = PixelType.UnsignedByte)
            : base(name, TextureTarget.Texture2D, bmp.Width, bmp.Height)
        {
            tex = GL.GenTexture();
            GL.BindTexture(TextureTarget.Texture2D, tex);

            reattempt_load:
            try {
                /*BitmapData bmpdata = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                GL.TexImage2D(TextureTarget.Texture2D, 0, destformat, bmpdata.Width, bmpdata.Height, 0, sourceformat, sourcetype, bmpdata.Scan0);
                bmp.UnlockBits(bmpdata);*/
                bmp.TexImage2D(destformat);
            }
            catch(OutOfMemoryException) {
                GC.WaitForPendingFinalizers();
                goto reattempt_load;
            }

            if(genmipmaps)
            {
                GL.GenerateMipmap(GenerateMipmapTarget.Texture2D);
                if(linearfilter)
                {
                    GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.LinearMipmapLinear);
                    GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
                }
                else
                {
                    GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.NearestMipmapNearest);
                    GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Nearest);
                }
            }
            else
            {
                if(linearfilter)
                {
                    GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
                    GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
                }
                else
                {
                    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);
        }
Example #47
0
        public Texture(Image img, TextureTarget type, PixelInternalFormat format, int num)
        {
            ID = GL.GenTexture();

            Type = type;
            Format = format;
            Number = num;

            Activate();

            using(Bitmap bmp = new Bitmap(img)) {
                BitmapData data = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                GL.TexImage2D(Type, 0, Format, bmp.Width, bmp.Height, 0, OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0);
                bmp.UnlockBits(data);
            }
        }
Example #48
0
        /// <summary>
        /// Creates a framebuffer object and its associated resources (depth and pbuffers).
        /// </summary>
        /// <param name="Size">Specifies the size (in pixels) of the framebuffer and it's associated buffers.</param>
        /// <param name="Attachments">Specifies the attachments to use for the pbuffers.</param>
        /// <param name="Format">Specifies the internal pixel format for the pbuffers.</param>
        public FBO(Size Size, FramebufferAttachment[] Attachments, PixelInternalFormat Format, bool Mipmaps)
        {
            this.Size = Size;
            this.Attachments = Attachments;
            this.Format = Format;
            this.mipmaps = Mipmaps;

            // First create the framebuffer
            BufferID = Gl.GenFramebuffer();
            Gl.BindFramebuffer(FramebufferTarget.Framebuffer, BufferID);

            // Create and attach a 24-bit depth buffer to the framebuffer
            DepthID = Gl.GenRenderbuffer();
            Gl.BindRenderbuffer(RenderbufferTarget.Renderbuffer, DepthID);
            Gl.RenderbufferStorage(RenderbufferTarget.Renderbuffer, RenderbufferStorage.DepthComponent24, Size.Width, Size.Height);

            // Create n texture buffers (known by the number of attachments)
            TextureID = new uint[Attachments.Length];
            Gl.GenTextures(Attachments.Length, TextureID);

            // Bind the n texture buffers to the framebuffer
            for (int i = 0; i < Attachments.Length; i++)
            {
                Gl.BindTexture(TextureTarget.Texture2D, TextureID[i]);
                Gl.TexImage2D(TextureTarget.Texture2D, 0, Format, Size.Width, Size.Height, 0, PixelFormat.Rgba, PixelType.UnsignedByte, IntPtr.Zero);
                if (Mipmaps)
                {
                    Gl.TexParameteri(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, 9729); // public const int GL_LINEAR = 9729;
                    Gl.TexParameteri(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, 9987); // public const int GL_LINEAR_MIPMAP_LINEAR = 9987;
                    Gl.GenerateMipmap(GenerateMipmapTarget.Texture2D);
                }
                Gl.FramebufferTexture(FramebufferTarget.Framebuffer, Attachments[i], TextureID[i], 0);
            }

            // Build the framebuffer and check for errors
            Gl.FramebufferRenderbuffer(FramebufferTarget.Framebuffer, FramebufferAttachment.DepthAttachment, RenderbufferTarget.Renderbuffer, DepthID);

            FramebufferErrorCode status = Gl.CheckFramebufferStatus(FramebufferTarget.Framebuffer);
            if (status != FramebufferErrorCode.FramebufferComplete)
            {
                Console.WriteLine("Frame buffer did not compile correctly.  Returned {0}, glError: {1}", status.ToString(), Gl.GetError().ToString());
            }

            Gl.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
        }
Example #49
0
        public Texture()
        {
            int id;
            // Create handle
            GL.GenTextures(1, out id);
            ID = id;
            GL.BindTexture(TextureTarget.Texture2D, ID);

            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)DefaultMinFilter);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)DefaultMagFilter);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)DefaultHorizontalWrapFilter);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)DefaultVerticalWrapFilter);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.GenerateMipmap, 0);

            PixelInternalFormat = PixelInternalFormat.Rgba;
            this.PixelFormat = OpenTK.Graphics.OpenGL.PixelFormat.Bgra;

            BorderColor = DefaultBorderColor;
        }
Example #50
0
		private GLTextureObject(Size? size, Bitmap bitmap, int numMipMapLevels, PixelInternalFormat internalFormat)
		{
			if (size != null && bitmap != null) throw new ArgumentException("Can not pass size and bitmap!");
			if (size != null)
			{
				bitmap = makeBitmap(size.Value);
			}
			this.Size = bitmap.Size;
			_id = GL.GenTexture();
			this.TextureUnit = TextureUnit.Texture0;
			GL.ActiveTexture(this.TextureUnit);
			GL.BindTexture(TextureTarget.Texture2D, _id);
			GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureBaseLevel, 0);
			GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMaxLevel, numMipMapLevels - 1);
			Size currentSize = new Size(bitmap.Width, bitmap.Height);
			Bitmap currentBitmap = new Bitmap(bitmap);
			currentBitmap.RotateFlip(RotateFlipType.RotateNoneFlipY); //bitmaps coordinate system does not match opengl's coordinate system... the y coord is flipped
			for (int i = 0; i < numMipMapLevels; i++)
			{
				//Load currentBitmap
				BitmapData currentData = currentBitmap.LockBits(new System.Drawing.Rectangle(0, 0, currentBitmap.Width, currentBitmap.Height),
					ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
				GL.TexImage2D(TextureTarget.Texture2D, i, internalFormat, currentSize.Width, currentSize.Height, 0,
					OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, currentData.Scan0);

				currentBitmap.UnlockBits(currentData);
				//Prepare for next iteration
				currentSize = new Size(currentSize.Width / 2, currentSize.Height / 2);
				Bitmap tempBitmap = scaleBitmap(currentBitmap, currentSize);
				currentBitmap.Dispose();
				currentBitmap = tempBitmap;
			}
			currentBitmap.Dispose();

			GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.NearestMipmapLinear);
			GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);

			if (size != null)
			{
				bitmap.Dispose();
			}
		}
Example #51
0
        public void AddTarget(PixelInternalFormat format, PixelType type)
        {
            Texture target = new Texture();

            // Bind the texture
            target.Bind();

            // Give an empty image to OpenGL
            GL.TexImage2D(TextureTarget.Texture2D, 0, format, gameWindow.Width, gameWindow.Height, 0, PixelFormat.Rgba, type, IntPtr.Zero);

            // Poor filtering. Needed !
            GL.TexParameter(TextureTarget.Texture2D,
                            TextureParameterName.TextureMinFilter,
                            (int)TextureMagFilter.Nearest);
            GL.TexParameter(TextureTarget.Texture2D,
                            TextureParameterName.TextureMagFilter,
                            (int)TextureMagFilter.Nearest);

            targets.Add(target);
        }
Example #52
0
        public GLTexture2D(string name, byte[] bytes, int width, int height, bool genmipmaps = false, OpenTK.Graphics.OpenGL.PixelFormat sourceformat = OpenTK.Graphics.OpenGL.PixelFormat.Rgb, PixelInternalFormat destformat = PixelInternalFormat.Rgba, PixelType sourcetype = PixelType.UnsignedByte)
            : base(name, TextureTarget.Texture2D, width, height)
        {
            tex = GL.GenTexture();
            GL.BindTexture(TextureTarget.Texture2D, tex);
            GL.TexImage2D(TextureTarget.Texture2D, 0, destformat, width, height, 0, sourceformat, sourcetype, bytes);

            if(genmipmaps)
            {
                GL.GenerateMipmap(GenerateMipmapTarget.Texture2D);
                GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.LinearMipmapNearest);
                GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Nearest);
            }
            else
            {
                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);
        }
Example #53
0
		/// <summary>
		/// Creates a new empty Texture with the specified size.
		/// </summary>
		/// <param name="width">The Textures width.</param>
		/// <param name="height">The Textures height</param>
		/// <param name="sizeMode">Specifies behaviour in case the specified size has non-power-of-two dimensions.</param>
		/// <param name="filterMag">The OpenGL filter mode for drawing the Texture bigger than it is.</param>
		/// <param name="filterMin">The OpenGL fitler mode for drawing the Texture smaller than it is.</param>
		/// <param name="wrapX">The OpenGL wrap mode on the texel x axis.</param>
		/// <param name="wrapY">The OpenGL wrap mode on the texel y axis.</param>
		/// <param name="format">The format in which OpenGL stores the pixel data.</param>
		public Texture(int width, int height, 
			SizeMode sizeMode			= SizeMode.Default, 
			TextureMagFilter filterMag	= TextureMagFilter.Linear, 
			TextureMinFilter filterMin	= TextureMinFilter.LinearMipmapLinear,
			TextureWrapMode wrapX		= TextureWrapMode.ClampToEdge,
			TextureWrapMode wrapY		= TextureWrapMode.ClampToEdge,
			PixelInternalFormat format	= PixelInternalFormat.Rgba)
		{
			this.filterMag = filterMag;
			this.filterMin = filterMin;
			this.wrapX = wrapX;
			this.wrapY = wrapY;
			this.pixelformat = format;
			this.texSizeMode = sizeMode;
			this.AdjustSize(width, height);
			this.SetupOpenGLRes();
		}
Example #54
0
		/// <summary>
		/// Creates a new Texture based on a <see cref="Duality.Resources.Pixmap"/>.
		/// </summary>
		/// <param name="basePixmap">The <see cref="Duality.Resources.Pixmap"/> to use as source for pixel data.</param>
		/// <param name="sizeMode">Specifies behaviour in case the source data has non-power-of-two dimensions.</param>
		/// <param name="filterMag">The OpenGL filter mode for drawing the Texture bigger than it is.</param>
		/// <param name="filterMin">The OpenGL fitler mode for drawing the Texture smaller than it is.</param>
		/// <param name="wrapX">The OpenGL wrap mode on the texel x axis.</param>
		/// <param name="wrapY">The OpenGL wrap mode on the texel y axis.</param>
		/// <param name="format">The format in which OpenGL stores the pixel data.</param>
		public Texture(ContentRef<Pixmap> basePixmap, 
			SizeMode sizeMode			= SizeMode.Default, 
			TextureMagFilter filterMag	= TextureMagFilter.Linear, 
			TextureMinFilter filterMin	= TextureMinFilter.LinearMipmapLinear,
			TextureWrapMode wrapX		= TextureWrapMode.ClampToEdge,
			TextureWrapMode wrapY		= TextureWrapMode.ClampToEdge,
			PixelInternalFormat format	= PixelInternalFormat.Rgba)
		{
			this.filterMag = filterMag;
			this.filterMin = filterMin;
			this.wrapX = wrapX;
			this.wrapY = wrapY;
			this.pixelformat = format;
			this.LoadData(basePixmap, sizeMode);
		}
 // ARB_texture_storage
 /// <summary>
 /// TexStorageXX allocates images with the given size (width​, height​, and depth​, where appropriate), with the number of mipmaps given by levels​. The storage is created here, but the contents of that storage is undefined.
 /// </summary>
 /// <param name="textureId"></param>
 /// <param name="target">Valid target​: GL_TEXTURE_1D</param>
 /// <param name="levels">Number of mipmaps to alloc.</param>
 /// <param name="piformat">Any Sized Internal format.</param>
 /// <param name="Width">Width of base mipmap.</param>
 /// <remarks>
 ///  immutable storage allocates all of the images for the texture all at once. Every mipmap level, array layer, and cube map face is all allocated with a single call, giving all of these images a specific Image Format. It is called "immutable" because once the storage is allocated, the storage cannot be changed. The texture can be deleted as normal, but the storage cannot be altered. A 256x256 2D texture with 5 mipmap layers that uses the GL_RGBA8​ image format will *always* be a 256x256 2D texture with 5 mipmap layers that uses the GL_RGBA8​ image format.
 ///  Note that what immutable storage refers to is the allocation of the memory, not the contents of that memory. You can upload different pixel data to immutable storage all you want. With mutable storage, you can re-vamp the storage of a texture object entirely, changing a 256x256 texture into a 1024x1024 texture.
 /// </remarks>
 public static void TextureStorage1DEXT(uint textureId, TextureTarget target, int levels, PixelInternalFormat piformat, int Width)
 {
     Delegates.glTextureStorage1DEXT(textureId, target, levels, piformat, Width);
 }
Example #56
0
		public static void TexImage2D(TextureTarget target, int level,
			PixelInternalFormat internalFormat,
			int width, int height, int border,
			PixelFormat format,
			PixelType type,
			Byte[] pixels)
		{
#if USE_OPENGL
			if (openGlHardwareAvailable)
			{
				OpenTK.Graphics.OpenGL.GL.TexImage2D(
					(OpenTK.Graphics.OpenGL.TextureTarget)target, level,
					(OpenTK.Graphics.OpenGL.PixelInternalFormat)internalFormat,
					width, height, border,
					(OpenTK.Graphics.OpenGL.PixelFormat)format,
					(OpenTK.Graphics.OpenGL.PixelType)type, pixels);
			}
#else
			OpenTK.Graphics.ES11.GL.TexImage2D(
				(OpenTK.Graphics.ES11.All)target, level,
				(int)internalFormat,
				width, height, border,
				(OpenTK.Graphics.ES11.All)format,
				(OpenTK.Graphics.ES11.All)type, pixels);
#endif
		}
 public TexFormat(PixelInternalFormat _pif, PixelFormat _pf, PixelType _pt)
 {
     pif = _pif;
     pf = _pf;
     pt = _pt;
 }
Example #58
0
        public Texture2D( int width, int height, byte[] data )
            : base(TextureTarget.Texture2D)
        {
            Width = width;
            Height = height;

            WrapModeU = TextureWrapMode.Repeat;
            WrapModeV = TextureWrapMode.Repeat;

            InternalFormat = PixelInternalFormat.Rgba;
            ExternalFormat = PixelFormat.Bgra;

            Compressed = false;
            Alpha = true;

            MinFilter = TextureMinFilter.Linear;

            MipMapCount = 1;
            ContainsMipMaps = false;
            GenerateMipMaps = false;

            ImageLevelSizes = new int[] { ( width * height ) << 2 };
            ImageLevelData = new byte[][] { data };
        }
Example #59
0
 /// <summary>
 /// Creates a framebuffer object and its associated resources (depth and pbuffers).
 /// </summary>
 /// <param name="Size">Specifies the size (in pixels) of the framebuffer and it's associated buffers.</param>
 /// <param name="Attachments">Specifies the attachment to use for the pbuffer.</param>
 /// <param name="Format">Specifies the internal pixel format for the pbuffer.</param>
 public FBO(Size Size, FramebufferAttachment Attachment = FramebufferAttachment.ColorAttachment0, PixelInternalFormat Format = PixelInternalFormat.Rgba8, bool Mipmaps = true)
     : this(Size, new FramebufferAttachment[] { Attachment }, Format, Mipmaps)
 {
 }
Example #60
0
        /// <summary>
        /// Creates a framebuffer object and its associated resources (depth and pbuffers).
        /// </summary>
        /// <param name="Size">Specifies the size (in pixels) of the framebuffer and it's associated buffers.</param>
        /// <param name="Attachments">Specifies the attachments to use for the frame buffer.</param>
        /// <param name="Format">Specifies the internal pixel format for the frame buffer.</param>
        /// <param name="Mipmaps">Specified whether to build mipmaps after the frame buffer is unbound.</param>
        public FBO(Size Size, FramebufferAttachment[] Attachments, PixelInternalFormat Format, bool Mipmaps, TextureParameter filterType = TextureParameter.Linear)
        {
            this.Size = Size;
            this.Attachments = Attachments;
            this.Format = Format;
            this.mipmaps = Mipmaps;

            // First create the framebuffer
            BufferID = Gl.GenFramebuffer();
            Gl.BindFramebuffer(FramebufferTarget.Framebuffer, BufferID);

            if (Attachments.Length == 1 && Attachments[0] == FramebufferAttachment.DepthAttachment)
            {
                // if this is a depth attachment only
                TextureID = new uint[] { Gl.GenTexture() };
                Gl.BindTexture(TextureTarget.Texture2D, TextureID[0]);

                Gl.TexImage2D(TextureTarget.Texture2D, 0, Format, Size.Width, Size.Height, 0, PixelFormat.DepthComponent, PixelType.Float, IntPtr.Zero);
                Gl.TexParameteri(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, TextureParameter.Nearest);
                Gl.TexParameteri(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, TextureParameter.Nearest);
                Gl.TexParameteri(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, TextureParameter.ClampToEdge);
                Gl.TexParameteri(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, TextureParameter.ClampToEdge);

                Gl.FramebufferTexture(FramebufferTarget.Framebuffer, FramebufferAttachment.DepthAttachment, TextureID[0], 0);
                Gl.DrawBuffer(DrawBufferMode.None);
                Gl.ReadBuffer(ReadBufferMode.None);
            }
            else
            {
                // Create n texture buffers (known by the number of attachments)
                TextureID = new uint[Attachments.Length];
                Gl.GenTextures(Attachments.Length, TextureID);

                // Bind the n texture buffers to the framebuffer
                for (int i = 0; i < Attachments.Length; i++)
                {
                    Gl.BindTexture(TextureTarget.Texture2D, TextureID[i]);
                    Gl.TexImage2D(TextureTarget.Texture2D, 0, Format, Size.Width, Size.Height, 0, PixelFormat.Rgba, PixelType.UnsignedByte, IntPtr.Zero);
                    if (Mipmaps)
                    {
                        Gl.TexParameteri(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, TextureParameter.Linear);
                        Gl.TexParameteri(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, TextureParameter.LinearMipMapLinear);
                        Gl.GenerateMipmap(GenerateMipmapTarget.Texture2D);
                    }
                    else
                    {
                        Gl.TexParameteri(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, filterType);
                        Gl.TexParameteri(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, filterType);
                    }
                    Gl.FramebufferTexture(FramebufferTarget.Framebuffer, Attachments[i], TextureID[i], 0);
                }

                // Create and attach a 24-bit depth buffer to the framebuffer
                DepthID = Gl.GenTexture();
                Gl.BindTexture(TextureTarget.Texture2D, DepthID);

                Gl.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Depth24Stencil8, Size.Width, Size.Height, 0, PixelFormat.DepthStencil, PixelType.UnsignedInt248, IntPtr.Zero);
                Gl.TexParameteri(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, TextureParameter.Nearest);
                Gl.TexParameteri(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, TextureParameter.Nearest);
                Gl.TexParameteri(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, TextureParameter.ClampToEdge);
                Gl.TexParameteri(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, TextureParameter.ClampToEdge);

                Gl.FramebufferTexture(FramebufferTarget.Framebuffer, FramebufferAttachment.DepthAttachment, DepthID, 0);
                Gl.FramebufferTexture(FramebufferTarget.Framebuffer, FramebufferAttachment.StencilAttachment, DepthID, 0);
            }

            // Build the framebuffer and check for errors
            FramebufferErrorCode status = Gl.CheckFramebufferStatus(FramebufferTarget.Framebuffer);
            if (status != FramebufferErrorCode.FramebufferComplete)
            {
                Console.WriteLine("Frame buffer did not compile correctly.  Returned {0}, glError: {1}", status.ToString(), Gl.GetError().ToString());
            }

            Gl.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
        }