Exemple #1
0
 public PixelFormatDescriptor(
     System.Drawing.Imaging.PixelFormat drawingFormat,
     PixelInternalFormat glInternalPixelFormat,
     PixelFormat glPixelFormat,
     PixelType glPixelType)
 {
     DrawingFormat = drawingFormat;
     GLPixelInternalFormat = glInternalPixelFormat;
     GLPixelFormat = glPixelFormat;
     GLPixelType = glPixelType;
 }
Exemple #2
0
            public void setPixelFormatFromNutFormat(int typet)
            {
                switch (typet)
                {
                case 0x0:
                    type = PixelInternalFormat.CompressedRgbaS3tcDxt1Ext;
                    break;

                case 0x1:
                    type = PixelInternalFormat.CompressedRgbaS3tcDxt3Ext;
                    break;

                case 0x2:
                    type = PixelInternalFormat.CompressedRgbaS3tcDxt5Ext;
                    break;

                case 14:
                    type  = PixelInternalFormat.Rgba;
                    utype = OpenTK.Graphics.OpenGL.PixelFormat.Rgba;
                    break;

                case 16:
                    type  = PixelInternalFormat.Rgba;
                    utype = OpenTK.Graphics.OpenGL.PixelFormat.AbgrExt;
                    break;

                case 17:
                    type  = PixelInternalFormat.Rgba;
                    utype = OpenTK.Graphics.OpenGL.PixelFormat.Bgra;
                    break;

                case 21:
                    type = PixelInternalFormat.CompressedRedRgtc1;
                    break;

                case 22:
                    type = PixelInternalFormat.CompressedRgRgtc2;
                    break;

                default:
                    throw new NotImplementedException($"Unknown nut format {typet}");
                }
            }
        public static Texture FromBitmap(Bitmap bitmap)
        {
            Texture texture = new Texture();

            texture.BeginUse();
            texture.FilterTrilinear();
            //todo: 16bit channels
            using (Bitmap bmp = new Bitmap(bitmap))
            {
                bmp.RotateFlip(RotateFlipType.RotateNoneFlipY);
                BitmapData          bmpData        = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, bmp.PixelFormat);
                PixelInternalFormat internalFormat = SelectInternalPixelFormat(bmp.PixelFormat);
                OpenTK.Graphics.OpenGL.PixelFormat inputPixelFormat = SelectInputPixelFormat(bmp.PixelFormat);
                texture.LoadPixels(bmpData.Scan0, bmpData.Width, bmpData.Height, internalFormat, inputPixelFormat, PixelType.UnsignedByte);
                bmp.UnlockBits(bmpData);
            }
            texture.EndUse();
            return(texture);
        }
Exemple #4
0
        public void FromBitmap(Bitmap image)
        {
            Width  = (uint)image.Width;
            Height = (uint)image.Height;

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

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

            int size = bData.Stride * bData.Height;

            byte[] data = new byte[size];

            Marshal.Copy(bData.Scan0, data, 0, size);

            Mipmaps.Add(data);

            image.UnlockBits(bData);
        }
Exemple #5
0
        private void CreateFromPtr(Point2 size, IntPtr dataPtr, OpenTK.Graphics.OpenGL.PixelFormat inputPixelFormat, PixelInternalFormat internalPixelFormat, PixelType pixelType)
        {
            ContextManager.ActivateDefaultIfNoCurrent();

            GL.GenTextures(1, out m_GLTextureID);
            GL.BindTexture(TextureTarget.Texture2D, m_GLTextureID);

            GL.TexImage2D(TextureTarget.Texture2D, 0, internalPixelFormat, size.X, size.Y, 0, inputPixelFormat, pixelType, dataPtr);

            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);

            ApplySize(size);

            m_pixelFormat         = inputPixelFormat;
            m_pixelType           = pixelType;
            m_pixelInternalFormat = internalPixelFormat;
        }
Exemple #6
0
        public GLTexture1D(string name, byte[] bytes, int width, OpenTK.Graphics.OpenGL.PixelFormat sourceformat, bool genmipmaps = false)
            : base(name, TextureTarget.Texture1D, width, 1)
        {
            this.bytes = bytes;
            this.sourceformat = sourceformat;
            tex = GL.GenTexture();
            GL.BindTexture(TextureTarget.Texture1D, tex);
            GL.TexImage1D<byte>(TextureTarget.Texture1D, 0, PixelInternalFormat.Rgba, width, 0, sourceformat, PixelType.UnsignedByte, bytes);

            if(genmipmaps)
            {
                GL.GenerateMipmap(GenerateMipmapTarget.Texture1D);
                GL.TexParameter(TextureTarget.Texture1D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.LinearMipmapNearest);
                GL.TexParameter(TextureTarget.Texture1D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Nearest);
            }
            else
            {
                GL.TexParameter(TextureTarget.Texture1D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest);
                GL.TexParameter(TextureTarget.Texture1D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Nearest);
            }
            GL.TexParameter(TextureTarget.Texture1D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Clamp);
        }
Exemple #7
0
        public GLTexture1D(string name, byte[] bytes, int width, OpenTK.Graphics.OpenGL.PixelFormat sourceformat, bool genmipmaps = false)
            : base(name, TextureTarget.Texture1D, width, 1)
        {
            this.bytes        = bytes;
            this.sourceformat = sourceformat;
            tex = GL.GenTexture();
            GL.BindTexture(TextureTarget.Texture1D, tex);
            GL.TexImage1D <byte>(TextureTarget.Texture1D, 0, PixelInternalFormat.Rgba, width, 0, sourceformat, PixelType.UnsignedByte, bytes);

            if (genmipmaps)
            {
                GL.GenerateMipmap(GenerateMipmapTarget.Texture1D);
                GL.TexParameter(TextureTarget.Texture1D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.LinearMipmapNearest);
                GL.TexParameter(TextureTarget.Texture1D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Nearest);
            }
            else
            {
                GL.TexParameter(TextureTarget.Texture1D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest);
                GL.TexParameter(TextureTarget.Texture1D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Nearest);
            }
            GL.TexParameter(TextureTarget.Texture1D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Clamp);
        }
        public OpenGLTexture2D(
            int mipLevels,
            int width,
            int height,
            PixelFormat veldridFormat,
            PixelInternalFormat internalFormat,
            OpenTK.Graphics.OpenGL.PixelFormat pixelFormat,
            PixelType pixelType)
            : base(TextureTarget.Texture2D, width, height)
        {
            MipLevels       = mipLevels;
            _veldridFormat  = veldridFormat;
            _internalFormat = internalFormat;
            _pixelFormat    = pixelFormat;
            _pixelType      = pixelType;

            Bind();

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

            for (int currentLevel = 0; currentLevel < mipLevels; currentLevel++)
            {
                // Set size, load empty data into texture
                GL.TexImage2D(
                    TextureTarget.Texture2D,
                    currentLevel,
                    internalFormat,
                    width, height,
                    0, // border
                    _pixelFormat,
                    _pixelType,
                    IntPtr.Zero);
                Utilities.CheckLastGLError();
                width  = Math.Max(1, width / 2);
                height = Math.Max(1, height / 2);
            }
        }
Exemple #9
0
        public void WriteData(IntPtr a_ptr, e_PixelType a_pixelType)
        {
            if (m_handle == -1)
            {
                m_handle = GL.GenTexture();
            }

            int width  = m_texture.Width;
            int height = m_texture.Height;

            OpenTK.Graphics.OpenGL.PixelFormat pixelFormat = GetOpenTKPixelFormat(m_texture.PixelFormat);
            PixelInternalFormat internalFormat             = GetOpenTKInternalPixelFormat(m_texture.InternalPixelFormat);
            PixelType           pixelType = GetOpenTKPixelType(a_pixelType);

            GL.BindTexture(TextureTarget.Texture2D, m_handle);

            GL.TexImage2D
            (
                TextureTarget.Texture2D,
                0,
                internalFormat,
                width, height,
                0,
                pixelFormat,
                pixelType,
                a_ptr
            );

            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)All.Linear);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)All.Linear);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapR, (int)All.ClampToEdge);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)All.ClampToEdge);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)All.ClampToEdge);

#if DEBUG_INFO
            Pipeline.GLError("Texture: Writing: ");
#endif
        }
Exemple #10
0
        public void LoadEmptyTexture(PixelInternalFormat pixelInternalFormat, OpenTK.Graphics.OpenGL.PixelFormat pixelFormat, bool isAnisotropic)
        {
            int myTextureHandle = 0;

            GL.GenTextures(1, out myTextureHandle);
            GL.BindTexture(TextureTarget.Texture2D, myTextureHandle);

            GL.TexImage2D(TextureTarget.Texture2D, 0, pixelInternalFormat, Width, Height, 0, pixelFormat, PixelType.UnsignedByte, IntPtr.Zero);

            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Nearest);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode);

            if (isAnisotropic)
            {
                GL.TexParameter(TextureTarget.Texture2D, (TextureParameterName)ExtTextureFilterAnisotropic.MaxTextureMaxAnisotropyExt, (float)16.0);
                GL.TexParameter(TextureTarget.Texture2D, (TextureParameterName)ExtTextureFilterAnisotropic.TextureMaxAnisotropyExt, (float)16.0);
            }

            MyTextureHandle = myTextureHandle;
            GL.BindTexture(TextureTarget.Texture2D, 0);
        }
Exemple #11
0
        public Bitmap loadBitmap(string filename, out OGL.PixelInternalFormat pif, out OGL.PixelFormat pf, out OGL.PixelType pt)
        {
            filename = Path.GetFullPath(filename);
            if (File.Exists(filename) == false)
            {
                throw new Exception("File " + filename + " does not exist");
            }

            Bitmap CurrentBitmap = null;

            try // Exceptions will be thrown if any Problem occurs while working on the file.
            {
                if (Path.GetExtension(filename) == ".pcx")
                {
                    CurrentBitmap = PCX.load(filename);
                }
                else
                {
                    CurrentBitmap = new Bitmap(filename);
                }

                evalBitmap(CurrentBitmap, out pif, out pf, out pt);

                //flip the image since it's backwards from what opengl expects
                if (myFlip == true)
                {
                    CurrentBitmap.RotateFlip(RotateFlipType.RotateNoneFlipY);
                }

                return(CurrentBitmap);
            }

            catch (Exception e)
            {
                throw new Exception("Texture Loading Error: Failed to read file " + filename + ": " + e.Message);
            }
        }
Exemple #12
0
        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 && format != PixelFormat.R32_Float)
                {
                    throw new NotImplementedException("The only supported depth texture formats are R16_UInt and R32_Float.");
                }

                pixelInternalFormat = PixelInternalFormat.DepthComponent16;
                if (format == PixelFormat.R16_UInt)
                {
                    pixelFormat = OpenTK.Graphics.OpenGL.PixelFormat.DepthComponent;
                }
                else
                {
                    pixelFormat = OpenTK.Graphics.OpenGL.PixelFormat.DepthComponent;
                }
            }

            return(new OpenGLTexture2D(
                       mipLevels,
                       width,
                       height,
                       format,
                       pixelInternalFormat,
                       pixelFormat,
                       OpenGLFormats.MapPixelType(format)));
        }
Exemple #13
0
        private void SetPixelFormats(TEX_FORMAT Format)
        {
            switch (Format)
            {
            case TEX_FORMAT.BC1_UNORM:
                pixelInternalFormat = PixelInternalFormat.CompressedRgbaS3tcDxt1Ext;
                break;

            case TEX_FORMAT.BC1_UNORM_SRGB:
                pixelInternalFormat = PixelInternalFormat.CompressedRgbaS3tcDxt1Ext;
                break;

            case TEX_FORMAT.BC2_UNORM:
                pixelInternalFormat = PixelInternalFormat.CompressedRgbaS3tcDxt3Ext;
                break;

            case TEX_FORMAT.BC2_UNORM_SRGB:
                pixelInternalFormat = PixelInternalFormat.CompressedRgbaS3tcDxt3Ext;
                break;

            case TEX_FORMAT.BC3_UNORM:
                pixelInternalFormat = PixelInternalFormat.CompressedRgbaS3tcDxt5Ext;
                break;

            case TEX_FORMAT.BC3_UNORM_SRGB:
                pixelInternalFormat = PixelInternalFormat.CompressedRgbaS3tcDxt5Ext;
                break;

            case TEX_FORMAT.BC4_UNORM:
            case TEX_FORMAT.BC4_SNORM:
                //Convert to rgb to prevent red output
                //While shaders could prevent this, converting is easier and works fine across all editors
                if (Runtime.UseDirectXTexDecoder)
                {
                    pixelInternalFormat = PixelInternalFormat.Rgba;
                    pixelFormat         = OpenTK.Graphics.OpenGL.PixelFormat.Rgba;
                }
                else
                {
                    pixelInternalFormat = PixelInternalFormat.CompressedRedRgtc1;
                    pixelInternalFormat = PixelInternalFormat.CompressedSignedRedRgtc1;
                }
                break;

            case TEX_FORMAT.BC5_SNORM:
                pixelInternalFormat = PixelInternalFormat.Rgba;
                pixelFormat         = OpenTK.Graphics.OpenGL.PixelFormat.Rgba;
                break;

            case TEX_FORMAT.BC5_UNORM:
                pixelInternalFormat = PixelInternalFormat.CompressedRgRgtc2;
                break;

            case TEX_FORMAT.BC6H_UF16:
                pixelInternalFormat = PixelInternalFormat.CompressedRgbBptcUnsignedFloat;
                break;

            case TEX_FORMAT.BC6H_SF16:
                pixelInternalFormat = PixelInternalFormat.CompressedRgbBptcSignedFloat;
                break;

            case TEX_FORMAT.BC7_UNORM:
                pixelInternalFormat = PixelInternalFormat.CompressedRgbaBptcUnorm;
                break;

            case TEX_FORMAT.BC7_UNORM_SRGB:
                pixelInternalFormat = PixelInternalFormat.CompressedSrgbAlphaBptcUnorm;
                break;

            case TEX_FORMAT.R8G8B8A8_UNORM:
                pixelInternalFormat = PixelInternalFormat.Rgba;
                pixelFormat         = OpenTK.Graphics.OpenGL.PixelFormat.Rgba;
                break;

            case TEX_FORMAT.R8G8B8A8_UNORM_SRGB:
                pixelInternalFormat = PixelInternalFormat.Rgba;
                pixelFormat         = OpenTK.Graphics.OpenGL.PixelFormat.Rgba;
                break;

            default:
                if (Runtime.UseDirectXTexDecoder)
                {
                    pixelInternalFormat = PixelInternalFormat.Rgba;
                    pixelFormat         = OpenTK.Graphics.OpenGL.PixelFormat.Rgba;
                }
                break;
            }
        }
Exemple #14
0
        public void LoadData(int W, int H, IntPtr Data, PixelInternalFormat InternalFormat = PixelInternalFormat.Rgba, PixelFormat PixFormat = PixelFormat.Rgba,
                             PixelType PixType = PixelType.UnsignedByte)
        {
            Width  = W;
            Height = H;

            GL.TexImage2D(Target, 0, UseSRGBA ? PixelInternalFormat.SrgbAlpha : InternalFormat, W, H, 0, PixFormat, PixType, Data);
        }
Exemple #15
0
 public Format(
     PixelInternalFormat PixelInternalFormat,
     OpenTK.Graphics.OpenGL.PixelFormat PixelFormat,
     PixelType PixelType)
 {
     this.PixelInternalFormat = PixelInternalFormat;
     this.PixelFormat = PixelFormat;
     this.PixelType = PixelType;
 }
Exemple #16
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);
        }
 public PixelFormatMap(Imaging.PixelFormat Format, OpenGL.PixelFormat GLFormat, OpenGL.PixelInternalFormat numbytes)
 {
     this.PixelFormat = Format;
     this.OpenGLPixelFormat = GLFormat;
     this.InternalFormat = numbytes;
 }
Exemple #18
0
        public void SetFlags(DXGI_FORMAT Format)
        {
            header.flags = (uint)(DDSD.CAPS | DDSD.HEIGHT | DDSD.WIDTH | DDSD.PIXELFORMAT | DDSD.MIPMAPCOUNT | DDSD.LINEARSIZE);
            header.caps  = (uint)DDSCAPS.TEXTURE;
            if (header.mipmapCount > 1)
            {
                header.caps |= (uint)(DDSCAPS.COMPLEX | DDSCAPS.MIPMAP);
            }

            switch (Format)
            {
            case DXGI_FORMAT.DXGI_FORMAT_R8G8B8A8_UNORM:
            case DXGI_FORMAT.DXGI_FORMAT_R8G8B8A8_UNORM_SRGB:
                header.ddspf.flags       = (uint)(DDPF.RGB | DDPF.ALPHAPIXELS);
                header.ddspf.RGBBitCount = 0x8 * 4;
                header.ddspf.RBitMask    = 0x000000FF;
                header.ddspf.GBitMask    = 0x0000FF00;
                header.ddspf.BBitMask    = 0x00FF0000;
                header.ddspf.ABitMask    = 0xFF000000;
                pixelInternalFormat      = PixelInternalFormat.SrgbAlpha;
                pixelFormat = OpenTK.Graphics.OpenGL.PixelFormat.Rgba;
                break;

            case DXGI_FORMAT.DXGI_FORMAT_BC1_UNORM_SRGB:
            case DXGI_FORMAT.DXGI_FORMAT_BC1_UNORM:
                header.ddspf.flags  = (uint)DDPF.FOURCC;
                header.ddspf.fourCC = FOURCC_DXT1;
                pixelInternalFormat = PixelInternalFormat.CompressedRgbaS3tcDxt1Ext;
                break;

            case DXGI_FORMAT.DXGI_FORMAT_BC2_UNORM_SRGB:
            case DXGI_FORMAT.DXGI_FORMAT_BC2_UNORM:
                header.ddspf.flags  = (uint)DDPF.FOURCC;
                header.ddspf.fourCC = FOURCC_DXT3;
                pixelInternalFormat = PixelInternalFormat.CompressedRgbaS3tcDxt3Ext;
                break;

            case DXGI_FORMAT.DXGI_FORMAT_BC3_UNORM_SRGB:
            case DXGI_FORMAT.DXGI_FORMAT_BC3_UNORM:
                header.ddspf.flags  = (uint)DDPF.FOURCC;
                header.ddspf.fourCC = FOURCC_DXT5;
                pixelInternalFormat = PixelInternalFormat.CompressedRgbaS3tcDxt5Ext;
                break;

            case DXGI_FORMAT.DXGI_FORMAT_BC4_UNORM:
            case DXGI_FORMAT.DXGI_FORMAT_BC4_SNORM:
                pixelInternalFormat = PixelInternalFormat.CompressedRedRgtc1;
                break;

            case DXGI_FORMAT.DXGI_FORMAT_BC5_UNORM:
            case DXGI_FORMAT.DXGI_FORMAT_BC5_SNORM:
            case DXGI_FORMAT.DXGI_FORMAT_BC6H_UF16:
            case DXGI_FORMAT.DXGI_FORMAT_BC6H_SF16:
            case DXGI_FORMAT.DXGI_FORMAT_BC7_UNORM:
            case DXGI_FORMAT.DXGI_FORMAT_BC7_UNORM_SRGB:
                header.ddspf.flags  = (uint)DDPF.FOURCC;
                header.ddspf.fourCC = FOURCC_DX10;
                if (DX10header == null)
                {
                    DX10header = new DX10Header();
                }

                IsDX10 = true;
                DX10header.DXGI_Format = Format;
                break;
            }
        }
Exemple #19
0
        public void LoadOpenGLTexture(STGenericTexture GenericTexture, int ArrayStartIndex = 0, bool LoadArrayLevels = false)
        {
            if (!Runtime.OpenTKInitialized || GLInitialized || Runtime.UseLegacyGL || IsFailedState)
            {
                return;
            }

            width  = (int)GenericTexture.Width;
            height = (int)GenericTexture.Height;

            if (Runtime.DisableLoadingGLHighResTextures)
            {
                if (width >= 3000 || height >= 3000)
                {
                    return;
                }
            }

            switch (GenericTexture.SurfaceType)
            {
            case STSurfaceType.Texture1D:
                TextureTarget = TextureTarget.Texture1D;
                break;

            case STSurfaceType.Texture2D:
                TextureTarget = TextureTarget.Texture2D;
                break;

            case STSurfaceType.Texture2D_Array:
                TextureTarget = TextureTarget.Texture2DArray;
                break;

            case STSurfaceType.Texture2D_Mulitsample:
                TextureTarget = TextureTarget.Texture2DMultisample;
                break;

            case STSurfaceType.Texture2D_Multisample_Array:
                TextureTarget = TextureTarget.Texture2DMultisampleArray;
                break;

            case STSurfaceType.Texture3D:
                TextureTarget = TextureTarget.Texture3D;
                break;

            case STSurfaceType.TextureCube:
                TextureTarget = TextureTarget.TextureCubeMap;
                break;

            case STSurfaceType.TextureCube_Array:
                TextureTarget = TextureTarget.TextureCubeMapArray;
                break;
            }

            if (GenericTexture.ArrayCount == 0)
            {
                GenericTexture.ArrayCount = 1;
            }

            List <STGenericTexture.Surface> Surfaces = new List <STGenericTexture.Surface>();

            try
            {
                if (UseMipmaps && GenericTexture.ArrayCount <= 1)
                {
                    //Load surfaces with mip maps
                    Surfaces = GenericTexture.GetSurfaces(ArrayStartIndex, false, 6);
                }
                else
                {
                    //Only load first mip level. Will be generated after
                    for (int i = 0; i < GenericTexture.ArrayCount; i++)
                    {
                        if (i >= ArrayStartIndex && i <= ArrayStartIndex + 6) //Only load up to 6 faces
                        {
                            Surfaces.Add(new STGenericTexture.Surface()
                            {
                                mipmaps = new List <byte[]>()
                                {
                                    GenericTexture.GetImageData(i, 0)
                                }
                            });
                        }
                    }
                }

                if (Surfaces.Count == 0 || Surfaces[0].mipmaps[0].Length == 0)
                {
                    return;
                }

                IsCubeMap = Surfaces.Count == 6;
                ImageSize = Surfaces[0].mipmaps[0].Length;

                if (IsCubeMap)
                {
                    TextureTarget = TextureTarget.TextureCubeMap;
                }

                //Force RGBA and use ST for decoding for weird width/heights
                //Open GL decoder has issues with certain width/heights

                Console.WriteLine($"width pow {width} {IsPow2(width)}");
                Console.WriteLine($"height pow {height} {IsPow2(height)}");

                if (!IsPow2(width) || !IsPow2(height))
                {
                    UseOpenGLDecoder = false;
                }

                pixelInternalFormat = PixelInternalFormat.Rgba;
                pixelFormat         = OpenTK.Graphics.OpenGL.PixelFormat.Rgba;

                if (GenericTexture.PlatformSwizzle == PlatformSwizzle.Platform_3DS ||
                    GenericTexture.PlatformSwizzle == PlatformSwizzle.Platform_Gamecube)
                {
                    UseOpenGLDecoder = false;
                    pixelFormat      = OpenTK.Graphics.OpenGL.PixelFormat.Bgra;
                }


                if (UseOpenGLDecoder)
                {
                    SetPixelFormats(GenericTexture.Format);
                }

                GLInitialized = true;
                for (int i = 0; i < Surfaces.Count; i++)
                {
                    for (int MipLevel = 0; MipLevel < Surfaces[i].mipmaps.Count; MipLevel++)
                    {
                        uint width  = Math.Max(1, GenericTexture.Width >> MipLevel);
                        uint height = Math.Max(1, GenericTexture.Height >> MipLevel);

                        Surfaces[i].mipmaps[MipLevel] = DecodeWithoutOpenGLDecoder(Surfaces[i].mipmaps[MipLevel], width, height, GenericTexture);
                    }
                }

                TexID = GenerateOpenGLTexture(this, Surfaces);

                if (IsCubeMap)
                {
                    TextureWrapS     = TextureWrapMode.Clamp;
                    TextureWrapT     = TextureWrapMode.Clamp;
                    TextureWrapR     = TextureWrapMode.Clamp;
                    TextureMinFilter = TextureMinFilter.LinearMipmapLinear;
                    TextureMagFilter = TextureMagFilter.Linear;
                }

                Surfaces.Clear();
            }
            catch
            {
                IsFailedState = true;
                GLInitialized = false;
                return;
            }
        }
        public void LoadOpenGLTexture(STGenericTexture GenericTexture, int ArrayStartIndex = 0, bool LoadArrayLevels = false)
        {
            if (!Runtime.OpenTKInitialized || GLInitialized || Runtime.UseLegacyGL)
            {
                return;
            }

            width  = (int)GenericTexture.Width;
            height = (int)GenericTexture.Height;

            if (Runtime.DisableLoadingGLHighResTextures)
            {
                if (width >= 3000 || height >= 3000)
                {
                    return;
                }
            }

            if (GenericTexture.ArrayCount == 0)
            {
                GenericTexture.ArrayCount = 1;
            }

            List <STGenericTexture.Surface> Surfaces = new List <STGenericTexture.Surface>();

            if (UseMipmaps && GenericTexture.ArrayCount <= 1)
            {
                //Load surfaces with mip maps
                Surfaces = GenericTexture.GetSurfaces(ArrayStartIndex, false, 6);
            }
            else
            {
                //Only load first mip level. Will be generated after
                for (int i = 0; i < GenericTexture.ArrayCount; i++)
                {
                    if (i >= ArrayStartIndex && i <= ArrayStartIndex + 6) //Only load up to 6 faces
                    {
                        Surfaces.Add(new STGenericTexture.Surface()
                        {
                            mipmaps = new List <byte[]>()
                            {
                                GenericTexture.GetImageData(i, 0)
                            }
                        });
                    }
                }
            }

            if (Surfaces.Count == 0 || Surfaces[0].mipmaps[0].Length == 0)
            {
                return;
            }

            IsCubeMap = Surfaces.Count == 6;
            ImageSize = Surfaces[0].mipmaps[0].Length;

            if (IsCubeMap)
            {
                TextureTarget    = TextureTarget.TextureCubeMap;
                TextureWrapS     = TextureWrapMode.Clamp;
                TextureWrapT     = TextureWrapMode.Clamp;
                TextureWrapR     = TextureWrapMode.Clamp;
                TextureMinFilter = TextureMinFilter.LinearMipmapLinear;
                TextureMagFilter = TextureMagFilter.Linear;
            }

            switch (GenericTexture.Format)
            {
            case TEX_FORMAT.BC1_UNORM:
                pixelInternalFormat = PixelInternalFormat.CompressedRgbaS3tcDxt1Ext;
                break;

            case TEX_FORMAT.BC1_UNORM_SRGB:
                pixelInternalFormat = PixelInternalFormat.CompressedRgbaS3tcDxt1Ext;
                break;

            case TEX_FORMAT.BC2_UNORM:
                pixelInternalFormat = PixelInternalFormat.CompressedRgbaS3tcDxt3Ext;
                break;

            case TEX_FORMAT.BC2_UNORM_SRGB:
                pixelInternalFormat = PixelInternalFormat.CompressedRgbaS3tcDxt3Ext;
                break;

            case TEX_FORMAT.BC3_UNORM:
                pixelInternalFormat = PixelInternalFormat.CompressedRgbaS3tcDxt5Ext;
                break;

            case TEX_FORMAT.BC3_UNORM_SRGB:
                pixelInternalFormat = PixelInternalFormat.CompressedRgbaS3tcDxt5Ext;
                break;

            case TEX_FORMAT.BC4_UNORM:
            case TEX_FORMAT.BC4_SNORM:
                //Convert to rgb to prevent red output
                //While shaders could prevent this, converting is easier and works fine across all editors
                if (Runtime.UseDirectXTexDecoder)
                {
                    pixelInternalFormat = PixelInternalFormat.Rgba;
                    pixelFormat         = OpenTK.Graphics.OpenGL.PixelFormat.Rgba;
                }
                else
                {
                    pixelInternalFormat = PixelInternalFormat.CompressedRedRgtc1;
                    pixelInternalFormat = PixelInternalFormat.CompressedSignedRedRgtc1;
                }
                break;

            case TEX_FORMAT.BC5_SNORM:
                pixelInternalFormat = PixelInternalFormat.Rgba;
                pixelFormat         = OpenTK.Graphics.OpenGL.PixelFormat.Rgba;
                break;

            case TEX_FORMAT.BC5_UNORM:
                pixelInternalFormat = PixelInternalFormat.CompressedRgRgtc2;
                break;

            case TEX_FORMAT.BC6H_UF16:
                pixelInternalFormat = PixelInternalFormat.CompressedRgbBptcUnsignedFloat;
                break;

            case TEX_FORMAT.BC6H_SF16:
                pixelInternalFormat = PixelInternalFormat.CompressedRgbBptcSignedFloat;
                break;

            case TEX_FORMAT.BC7_UNORM:
                pixelInternalFormat = PixelInternalFormat.CompressedRgbaBptcUnorm;
                break;

            case TEX_FORMAT.BC7_UNORM_SRGB:
                pixelInternalFormat = PixelInternalFormat.CompressedSrgbAlphaBptcUnorm;
                break;

            case TEX_FORMAT.R8G8B8A8_UNORM:
                pixelInternalFormat = PixelInternalFormat.Rgba;
                pixelFormat         = OpenTK.Graphics.OpenGL.PixelFormat.Rgba;
                break;

            case TEX_FORMAT.R8G8B8A8_UNORM_SRGB:
                pixelInternalFormat = PixelInternalFormat.Rgba;
                pixelFormat         = OpenTK.Graphics.OpenGL.PixelFormat.Rgba;
                break;

            default:
                if (Runtime.UseDirectXTexDecoder)
                {
                    pixelInternalFormat = PixelInternalFormat.Rgba;
                    pixelFormat         = OpenTK.Graphics.OpenGL.PixelFormat.Rgba;
                }
                break;
            }
            GLInitialized = true;
            for (int i = 0; i < Surfaces.Count; i++)
            {
                for (int MipLevel = 0; MipLevel < Surfaces[i].mipmaps.Count; MipLevel++)
                {
                    uint width  = Math.Max(1, GenericTexture.Width >> MipLevel);
                    uint height = Math.Max(1, GenericTexture.Height >> MipLevel);

                    Surfaces[i].mipmaps[MipLevel] = DecodeWithoutOpenGLDecoder(Surfaces[i].mipmaps[MipLevel], width, height, GenericTexture);
                }
            }

            TexID = GenerateOpenGLTexture(this, Surfaces);

            Surfaces.Clear();
        }
        public void LoadOpenGLTexture(STGenericTexture GenericTexture, int ArrayStartIndex = 0, bool LoadArrayLevels = false)
        {
            if (!Runtime.OpenTKInitialized || GLInitialized || IsFailedState)
            {
                return;
            }

            width     = (int)GenericTexture.Width;
            height    = (int)GenericTexture.Height;
            IsCubeMap = GenericTexture.ArrayCount == 6;

            ApplySurfaceInformation(GenericTexture.SurfaceType);

            if (GenericTexture.ArrayCount == 0)
            {
                GenericTexture.ArrayCount = 1;
            }

            List <STGenericTexture.Surface> Surfaces = new List <STGenericTexture.Surface>();

            try
            {
                if (UseMipmaps && GenericTexture.ArrayCount == 1)
                {
                    //Load surfaces with mip maps
                    Surfaces = GenericTexture.GetSurfaces(ArrayStartIndex, false, 6);
                }
                else
                {
                    //Only load first mip level. Will be generated after
                    for (int i = 0; i < GenericTexture.ArrayCount; i++)
                    {
                        if (i >= ArrayStartIndex && i <= ArrayStartIndex + 6) //Only load up to 6 faces
                        {
                            Surfaces.Add(new STGenericTexture.Surface()
                            {
                                mipmaps = new List <byte[]>()
                                {
                                    GenericTexture.GetSurface(i, 0)
                                }
                            });
                        }
                    }
                }

                if (Surfaces.Count == 0 || Surfaces[0].mipmaps[0].Length == 0)
                {
                    return;
                }

                IsCubeMap = Surfaces.Count == 6;
                ImageSize = Surfaces[0].mipmaps[0].Length;

                if (IsCubeMap)
                {
                    TextureTarget = TextureTarget.TextureCubeMap;
                }

                //Open GL decoder has issues with certain width/heights so use in tool converters if necessary
                if ((!IsPow2(width) || !IsPow2(height)) && GenericTexture.IsBCNCompressed() || GenericTexture.IsASTC() || GenericTexture.Platform.OutputFormat == TexFormat.BC5_SNORM)
                {
                    UseOpenGLDecoder = false;
                }

                pixelInternalFormat = PixelInternalFormat.Rgba;
                pixelFormat         = OpenTK.Graphics.OpenGL.PixelFormat.Rgba;

                if (GenericTexture.Platform is Imaging.CTRSwizzle ||
                    GenericTexture.Platform is Imaging.GamecubeSwizzle ||
                    GenericTexture.Platform is Imaging.NitroSwizzle || GenericTexture.IsASTC())
                {
                    pixelFormat      = OpenTK.Graphics.OpenGL.PixelFormat.Bgra;
                    UseOpenGLDecoder = false;
                }
                else if (UseOpenGLDecoder)
                {
                    SetPixelFormats(GenericTexture.Platform.OutputFormat);
                }


                GLInitialized = true;
                for (int i = 0; i < Surfaces.Count; i++)
                {
                    for (int MipLevel = 0; MipLevel < Surfaces[i].mipmaps.Count; MipLevel++)
                    {
                        uint width  = Math.Max(1, GenericTexture.Width >> MipLevel);
                        uint height = Math.Max(1, GenericTexture.Height >> MipLevel);

                        if (!UseOpenGLDecoder)
                        {
                            Surfaces[i].mipmaps[MipLevel] = TryDecodeSurface(Surfaces[i].mipmaps[MipLevel], width, height, GenericTexture);
                        }
                    }
                }

                TexID = GenerateOpenGLTexture(this, Surfaces);

                Surfaces.Clear();
            }
            catch
            {
                IsFailedState = true;
                GLInitialized = false;
                return;
            }
        }
Exemple #22
0
 private static void GetOpenGlFormat(System.Drawing.Imaging.PixelFormat drawingFormat,
     out PixelInternalFormat glInternalPixelFormat, out PixelFormat glPixelFormat, out PixelType glPixelType)
 {
     switch (drawingFormat)
     {
         case System.Drawing.Imaging.PixelFormat.Format8bppIndexed: // misses glColorTable setup
             glInternalPixelFormat = PixelInternalFormat.R8;
             glPixelFormat = PixelFormat.Red;
             glPixelType = PixelType.UnsignedByte;
             break;
         case System.Drawing.Imaging.PixelFormat.Format16bppArgb1555:
         case System.Drawing.Imaging.PixelFormat.Format16bppRgb555: // does not work
             glInternalPixelFormat = PixelInternalFormat.Rgb5A1;
             glPixelFormat = PixelFormat.Bgr;
             glPixelType = PixelType.UnsignedShort5551Ext;
             break;
         case System.Drawing.Imaging.PixelFormat.Format24bppRgb: // works
             glInternalPixelFormat = PixelInternalFormat.Rgb8;
             glPixelFormat = PixelFormat.Bgr;
             glPixelType = PixelType.UnsignedByte;
             break;
         case System.Drawing.Imaging.PixelFormat.Format32bppRgb: // has alpha too? wtf?
         case System.Drawing.Imaging.PixelFormat.Canonical:
         case System.Drawing.Imaging.PixelFormat.Format32bppArgb: // works
             glInternalPixelFormat = PixelInternalFormat.Rgba;
             glPixelFormat = PixelFormat.Bgra;
             glPixelType = PixelType.UnsignedByte;
             break;
         default:
             throw new NotSupportedException(drawingFormat.ToString());
     }
 }
Exemple #23
0
 public Texture(Point2 size, IntPtr dataPtr, OpenTK.Graphics.OpenGL.PixelFormat pixelFormat, PixelInternalFormat internalPixelFormat, PixelType pixelType)
 {
     CreateFromPtr(size, dataPtr, pixelFormat, internalPixelFormat, pixelType);
 }
Exemple #24
0
 public void Alloc(PixelInternalFormat internalFormat, PixelFormat format, PixelType type, IntPtr data)
 {
     GL.TexImage2D(textureTarget, 0, internalFormat, width, height, 0, format, type, data);
 }
Exemple #25
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);
        }
Exemple #26
0
        private static void GetOpenGlFormat(System.Drawing.Imaging.PixelFormat drawingFormat,
                                            out PixelInternalFormat glInternalPixelFormat, out PixelFormat glPixelFormat, out PixelType glPixelType)
        {
            switch (drawingFormat)
            {
            case System.Drawing.Imaging.PixelFormat.Format8bppIndexed:     // misses glColorTable setup
                glInternalPixelFormat = PixelInternalFormat.R8;
                glPixelFormat         = PixelFormat.Red;
                glPixelType           = PixelType.UnsignedByte;
                break;

            case System.Drawing.Imaging.PixelFormat.Format16bppArgb1555:
            case System.Drawing.Imaging.PixelFormat.Format16bppRgb555:     // does not work
                glInternalPixelFormat = PixelInternalFormat.Rgb5A1;
                glPixelFormat         = PixelFormat.Bgr;
                glPixelType           = PixelType.UnsignedShort5551Ext;
                break;

            case System.Drawing.Imaging.PixelFormat.Format24bppRgb:     // works
                glInternalPixelFormat = PixelInternalFormat.Rgb8;
                glPixelFormat         = PixelFormat.Bgr;
                glPixelType           = PixelType.UnsignedByte;
                break;

            case System.Drawing.Imaging.PixelFormat.Format32bppRgb:     // has alpha too? wtf?
            case System.Drawing.Imaging.PixelFormat.Canonical:
            case System.Drawing.Imaging.PixelFormat.Format32bppArgb:    // works
                glInternalPixelFormat = PixelInternalFormat.Rgba;
                glPixelFormat         = PixelFormat.Bgra;
                glPixelType           = PixelType.UnsignedByte;
                break;

            default:
                throw new NotSupportedException(drawingFormat.ToString());
            }
        }
Exemple #27
0
    /// <summary>
    /// buildTexture loads and build the OpenGL texture on the GPU. The loaded image
    /// data in the client memory is deleted again. The parameters min_filter and
    /// mag_filter set the minification and magnification. The wrapS and wrapT parameters
    /// set the texture wrapping mode. See the GL spec.
    /// </summary>
    /// <param name="textureFile"></param>
    /// <param name="min_filter"></param>
    /// <param name="mag_filter"></param>
    /// <param name="wrapS"></param>
    /// <param name="wrapT"></param>
    /// <returns></returns>
    public static int BuildTexture(string textureFile,
                                   TextureMinFilter min_filter,
                                   TextureMagFilter mag_filter,
                                   TextureWrapMode wrapS,
                                   TextureWrapMode wrapT)
    {
        // load texture image
        Bitmap img = null;

        try { img = (Bitmap)Image.FromFile(textureFile); }
        catch (Exception ex)
        { Console.WriteLine("BuildTexture: Loading failed: " + ex.ToString());
          Application.Exit(); }

        // Images on Windows are top left but OpenGL expects them bottom left
        img.RotateFlip(RotateFlipType.RotateNoneFlipY);

        // check max. size
        int maxSize = 0;

        gl.GetInteger(GetPName.MaxTextureSize, out maxSize);
        if (img.Width > maxSize || img.Height > maxSize)
        {
            Console.WriteLine("BuildTexture: Texture width or height is too big.");
            Application.Exit();
        }

        // generate texture name (= internal texture object)
        int textureHandle;

        gl.GenTextures(1, out textureHandle);

        // bind the texture as the active one
        gl.BindTexture(TextureTarget.Texture2D, textureHandle);

        // apply minification & magnification filter
        gl.TexParameter(TextureTarget.Texture2D,
                        TextureParameterName.TextureMinFilter,
                        (int)min_filter);
        gl.TexParameter(TextureTarget.Texture2D,
                        TextureParameterName.TextureMagFilter,
                        (int)mag_filter);

        // apply texture wrapping modes
        gl.TexParameter(TextureTarget.Texture2D,
                        TextureParameterName.TextureWrapS,
                        (int)wrapS);
        gl.TexParameter(TextureTarget.Texture2D,
                        TextureParameterName.TextureWrapT,
                        (int)wrapT);

        // Lock the image data memory to access the data
        BitmapData data = img.LockBits(new Rectangle(0, 0, img.Width, img.Height),
                                       ImageLockMode.ReadOnly,
                                       img.PixelFormat);

        PixelInternalFormat internFormat = PixelInternalFormat.Four;

        OpenTK.Graphics.OpenGL.PixelFormat pixelFormat = OpenTK.Graphics.OpenGL.PixelFormat.Bgra;
        switch (img.PixelFormat)
        {
        case System.Drawing.Imaging.PixelFormat.Alpha:
            internFormat = PixelInternalFormat.Alpha;
            pixelFormat  = OpenTK.Graphics.OpenGL.PixelFormat.Alpha;
            break;

        case System.Drawing.Imaging.PixelFormat.Format24bppRgb:
            internFormat = PixelInternalFormat.Rgb;
            pixelFormat  = OpenTK.Graphics.OpenGL.PixelFormat.Bgr;
            break;

        case System.Drawing.Imaging.PixelFormat.Format8bppIndexed:
            internFormat = PixelInternalFormat.Luminance;
            pixelFormat  = OpenTK.Graphics.OpenGL.PixelFormat.Luminance;
            break;
        }


        // Copy image data to the GPU. The image can be delete afterwards
        gl.TexImage2D(TextureTarget.Texture2D, // target texture type 1D, 2D or 3D
                      0,                       // Base level for mipmapped textures
                      internFormat,            // internal format: e.g. GL_RGBA, see spec.
                      img.Width,               // image width
                      img.Height,              // image height
                      0,                       // border pixels: must be 0
                      pixelFormat,             // pixel format
                      PixelType.UnsignedByte,  // data type
                      data.Scan0);             // image data pointer

        // generate the mipmap levels
        if (min_filter >= TextureMinFilter.NearestMipmapNearest)
        {
            gl.GenerateMipmap(GenerateMipmapTarget.Texture2D);
        }

        // Unlock the memory and dispose it
        img.UnlockBits(data);
        img.Dispose();

        GetGLError("BuildTexture", true);
        return(textureHandle);
    }
Exemple #28
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);
        }
Exemple #29
0
        public GLTexture2D(string name, 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, bool linearfilter = false)
            : 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, IntPtr.Zero);

            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);
        }
Exemple #30
0
 public Texture FromIntPtr(int width, int height, IntPtr data, PixelInternalFormat pixelInternalFormat = PixelInternalFormat.Rgba, PixelFormat format = PixelFormat.Bgra, PixelType pixelType = PixelType.UnsignedByte)
 {
     Handle = GL.GenTexture();
     GL.BindTexture(TextureTarget.Texture2D, Handle);
     GL.TexImage2D(TextureTarget.Texture2D, 0, pixelInternalFormat, 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.BindTexture(TextureTarget.Texture2D, 0);
     return(this);
 }
Exemple #31
0
        public void SetTexture(byte[] pixels, int width, int height, PixelFormat pixelFormat)
        {
            GL.BindTexture(TextureTarget.Texture2D, TextureId);

            PixelInternalFormat internalFormat = new PixelInternalFormat();
            OpenTK.Graphics.OpenGL.PixelFormat format = new OpenTK.Graphics.OpenGL.PixelFormat();
            if (pixelFormat == PixelFormat.Rgb)
            {
                internalFormat = PixelInternalFormat.Rgb;
                format = OpenTK.Graphics.OpenGL.PixelFormat.Rgb;
            }
            else if (pixelFormat == PixelFormat.Rgba)
            {
                internalFormat = PixelInternalFormat.Rgba;
                format = OpenTK.Graphics.OpenGL.PixelFormat.Rgba;
            }

            GL.TexImage2D(TextureTarget.Texture2D, 0, internalFormat, width, height, 0, format, PixelType.UnsignedByte, pixels);
            GL.BindTexture(TextureTarget.Texture2D, 0);

            _pixels = pixels;
            _width = width;
            _height = height;
            _pixelFormat = pixelFormat;
        }