Example #1
0
 public void SetPixelFormat(RPixelFormat format)
 {
     if (Id != 0)
     {
         pixelFormat = format;
     }
 }
Example #2
0
 public void SetData(RColor[] colors, RPixelFormat format, int x, int y, int width, int height)
 {
     Bind();
     GL.TexSubImage2D <RColor>(textureTarget, 0, x, y, width, height, (PixelFormat)format, PixelType.UnsignedByte, colors);
     REngine.CheckGLError();
     Unbind();
     REngine.CheckGLError();
 }
Example #3
0
 public void CreateDepth(int width, int height, RPixelFormat format, RDepthFormat depthFormat)
 {
     GL.GenTextures(1, out Id);
     textureTarget = TextureTarget.Texture2D;
     GL.BindTexture(textureTarget, Id);
     REngine.CheckGLError();
     GL.TexImage2D(textureTarget, 0, GetPixelInternalForDepth(depthFormat), width, height, 0, (PixelFormat)format, GetPixelTypeForDepth(depthFormat), IntPtr.Zero);
     REngine.CheckGLError();
     CreateProperties(textureTarget, false);
     REngine.CheckGLError();
 }
Example #4
0
 public void SetData <T>(T[] data, RPixelFormat format, int x, int y, int width, int height, bool packAlignment = true) where T : struct
 {
     if (!packAlignment)
     {
         GL.PixelStore(PixelStoreParameter.UnpackAlignment, 1);
     }
     Bind();
     GL.TexSubImage2D <T>(textureTarget, 0, x, y, width, height, (PixelFormat)format, PixelType.UnsignedByte, data);
     REngine.CheckGLError();
     Unbind();
     if (!packAlignment)
     {
         GL.PixelStore(PixelStoreParameter.PackAlignment, 1);
     }
 }
Example #5
0
        public void Create(RPixelFormat format, ref RTexture2D posX, ref RTexture2D posY, ref RTexture2D posZ, ref RTexture2D negX, ref RTexture2D negY, ref RTexture2D negZ)
        {
            PixelInternalFormat inf = PixelInternalFormat.Rgba;
            PixelFormat         pf  = PixelFormat.Rgba;

            if (format == RPixelFormat.Bgr)
            {
                inf = PixelInternalFormat.Rgb;
                pf  = PixelFormat.Bgr;
            }
            if (format == RPixelFormat.Rgb)
            {
                inf = PixelInternalFormat.Rgb;
                pf  = PixelFormat.Rgb;
            }
            if (format == RPixelFormat.Bgra)
            {
                inf = PixelInternalFormat.Rgba;
                pf  = PixelFormat.Bgra;
            }
            if (format == RPixelFormat.Rgba)
            {
                inf = PixelInternalFormat.Rgba;
                pf  = PixelFormat.Rgba;
            }
            GL.ActiveTexture(TextureUnit.Texture0);
            var posXcolors = posX.GetData <RColor>();
            var posYcolors = posY.GetData <RColor>();
            var posZcolors = posZ.GetData <RColor>();
            var negXcolors = negX.GetData <RColor>();
            var negYcolors = negY.GetData <RColor>();
            var negZcolors = negZ.GetData <RColor>();

            Bounds = posX.Bounds;
            GL.GenTextures(1, out Id);
            textureTarget = OpenTK.Graphics.OpenGL.TextureTarget.TextureCubeMap;
            GL.BindTexture(textureTarget, Id);
            SetTextureMagFilter(RTextureMagFilter.Nearest);
            SetTextureMinFilter(RTextureMinFilter.Nearest);
            SetTextureWrapMode(RTextureWrapMode.ClampToBorder, RTextureWrapMode.ClampToBorder);
            GL.TexImage2D <RColor>(TextureTarget.TextureCubeMapPositiveX, 0, inf, Bounds.Width, Bounds.Height, 0, pf, PixelType.UnsignedByte, posXcolors);
            GL.TexImage2D <RColor>(TextureTarget.TextureCubeMapPositiveY, 0, inf, Bounds.Width, Bounds.Height, 0, pf, PixelType.UnsignedByte, posYcolors);
            GL.TexImage2D <RColor>(TextureTarget.TextureCubeMapPositiveZ, 0, inf, Bounds.Width, Bounds.Height, 0, pf, PixelType.UnsignedByte, posZcolors);
            GL.TexImage2D <RColor>(TextureTarget.TextureCubeMapNegativeX, 0, inf, Bounds.Width, Bounds.Height, 0, pf, PixelType.UnsignedByte, negXcolors);
            GL.TexImage2D <RColor>(TextureTarget.TextureCubeMapNegativeY, 0, inf, Bounds.Width, Bounds.Height, 0, pf, PixelType.UnsignedByte, negYcolors);
            GL.TexImage2D <RColor>(TextureTarget.TextureCubeMapNegativeZ, 0, inf, Bounds.Width, Bounds.Height, 0, pf, PixelType.UnsignedByte, negZcolors);
        }
Example #6
0
 public void Create(int width, int height, RPixelFormat format, RSurfaceFormat surfaceFormat, bool multisample = false)
 {
     GL.GenTextures(1, out Id);
     if (multisample)
     {
         textureTarget = TextureTarget.Texture2DMultisample;
         GL.BindTexture(TextureTarget.Texture2DMultisample, Id);
         GL.TexImage2DMultisample(TextureTargetMultisample.Texture2DMultisample, 4, PixelInternalFormat.Rgba, width, height, true);
         REngine.CheckGLError();
         CreateProperties(TextureTarget.Texture2DMultisample, true);
         REngine.CheckGLError();
     }
     else
     {
         textureTarget = TextureTarget.Texture2D;
         GL.BindTexture(TextureTarget.Texture2D, Id);
         GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, width, height, 0, (PixelFormat)format, GetPixelTypeForSurface(surfaceFormat), IntPtr.Zero);
         REngine.CheckGLError();
         CreateProperties(TextureTarget.Texture2D, false);
         REngine.CheckGLError();
     }
 }
Example #7
0
        public static void LoadFromBitmap(ref Bitmap bitmap, out uint texturehandle, out TextureTarget dimension, out RPixelFormat format, out PixelType type)
        {
            dimension     = (TextureTarget)0;
            texturehandle = TextureLoaderParameters.OpenGLDefaultTexture;
            ErrorCode GLError = ErrorCode.NoError;

            Bitmap CurrentBitmap = null;

            try // Exceptions will be thrown if any Problem occurs while working on the file.
            {
                CurrentBitmap = bitmap;
                if (TextureLoaderParameters.FlipImages)
                {
                    CurrentBitmap.RotateFlip(RotateFlipType.RotateNoneFlipY);
                }

                if (CurrentBitmap.Height > 1)
                {
                    dimension = TextureTarget.Texture2D;
                }
                else
                {
                    dimension = TextureTarget.Texture1D;
                }

                GL.GenTextures(1, out texturehandle);
                GL.BindTexture(dimension, texturehandle);

                #region Load Texture
                OpenTK.Graphics.OpenGL.PixelInternalFormat pif;
                OpenTK.Graphics.OpenGL.PixelFormat         pf;
                OpenTK.Graphics.OpenGL.PixelType           pt;


                switch (CurrentBitmap.PixelFormat)
                {
                case System.Drawing.Imaging.PixelFormat.Format8bppIndexed:     // misses glColorTable setup
                    pif = OpenTK.Graphics.OpenGL.PixelInternalFormat.R8;
                    pf  = OpenTK.Graphics.OpenGL.PixelFormat.Red;
                    pt  = OpenTK.Graphics.OpenGL.PixelType.UnsignedByte;
                    break;

                case System.Drawing.Imaging.PixelFormat.Format16bppArgb1555:
                case System.Drawing.Imaging.PixelFormat.Format16bppRgb555:     // does not work
                    pif = OpenTK.Graphics.OpenGL.PixelInternalFormat.Rgb5A1;
                    pf  = OpenTK.Graphics.OpenGL.PixelFormat.Bgr;
                    pt  = OpenTK.Graphics.OpenGL.PixelType.UnsignedShort5551Ext;
                    break;

                /*  case System.Drawing.Imaging.PixelFormat.Format16bppRgb565:
                 * pif = Reactor.Graphics.OpenGL.PixelInternalFormat.R5G6B5IccSgix;
                 * pf = Reactor.Graphics.OpenGL.PixelFormat.R5G6B5IccSgix;
                 * pt = Reactor.Graphics.OpenGL.PixelType.UnsignedByte;
                 * break;
                 */
                case System.Drawing.Imaging.PixelFormat.Format24bppRgb:     // works
                    pif = OpenTK.Graphics.OpenGL.PixelInternalFormat.Rgb;
                    pf  = OpenTK.Graphics.OpenGL.PixelFormat.Bgr;
                    pt  = OpenTK.Graphics.OpenGL.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
                    pif = OpenTK.Graphics.OpenGL.PixelInternalFormat.Rgba;
                    pf  = OpenTK.Graphics.OpenGL.PixelFormat.Bgra;
                    pt  = OpenTK.Graphics.OpenGL.PixelType.UnsignedByte;
                    break;

                default:
                    throw new ArgumentException("ERROR: Unsupported Pixel Format " + CurrentBitmap.PixelFormat);
                }
                format = (RPixelFormat)pf;
                type   = pt;
                BitmapData Data = CurrentBitmap.LockBits(new System.Drawing.Rectangle(0, 0, CurrentBitmap.Width, CurrentBitmap.Height), ImageLockMode.ReadOnly, CurrentBitmap.PixelFormat);

                if (Data.Height > 1)
                { // image is 2D
                    if (TextureLoaderParameters.BuildMipmapsForUncompressed)
                    {
                        throw new Exception("Cannot build mipmaps, Glu is deprecated.");
                        //  Glu.Build2DMipmap(dimension, (int)pif, Data.Width, Data.Height, pf, pt, Data.Scan0);
                    }
                    else
                    {
                        GL.TexImage2D(dimension, 0, pif, Data.Width, Data.Height, TextureLoaderParameters.Border, pf, pt, Data.Scan0);
                    }
                }
                else
                { // image is 1D
                    if (TextureLoaderParameters.BuildMipmapsForUncompressed)
                    {
                        throw new Exception("Cannot build mipmaps, Glu is deprecated.");
                        //  Glu.Build1DMipmap(dimension, (int)pif, Data.Width, pf, pt, Data.Scan0);
                    }
                    else
                    {
                        GL.TexImage1D(dimension, 0, pif, Data.Width, TextureLoaderParameters.Border, pf, pt, Data.Scan0);
                    }
                }

                //GL.Finish( );
                REngine.CheckGLError();
                GLError = GL.GetError( );
                if (GLError != ErrorCode.NoError)
                {
                    throw new ArgumentException("Error building TexImage. GL Error: " + GLError);
                }

                CurrentBitmap.UnlockBits(Data);
                #endregion Load Texture
                Setup(dimension);


                return; // success
            } catch (Exception e)
            {
                dimension     = (TextureTarget)0;
                texturehandle = TextureLoaderParameters.OpenGLDefaultTexture;
                throw new ArgumentException("Texture Loading Error: Failed to read data.\n" + e);
                // return; // failure
            } finally
            {
                CurrentBitmap = null;
            }
        }
Example #8
0
        public static void LoadFromDisk(string filename, out uint texturehandle, out TextureTarget dimension, out RPixelFormat format, out PixelType type)
        {
            dimension     = (TextureTarget)0;
            texturehandle = TextureLoaderParameters.OpenGLDefaultTexture;
            Bitmap b = new Bitmap(filename);

            LoadFromBitmap(ref b, out texturehandle, out dimension, out format, out type);
        }
Example #9
0
        public static void LoadFromData(byte[] data, out uint texturehandle, out TextureTarget dimension, out RPixelFormat format, out PixelType type)
        {
            dimension     = (TextureTarget)0;
            texturehandle = TextureLoaderParameters.OpenGLDefaultTexture;
            MemoryStream stream = new MemoryStream(data);
            Bitmap       b      = new Bitmap(stream);

            LoadFromBitmap(ref b, out texturehandle, out dimension, out format, out type);
        }
Example #10
0
 private void Pack(RTextureSprite sprite, RPixelFormat format)
 {
     byte[] data = sprite.GetData <byte>();
     SetData <byte>(data, format, (int)sprite.Offset.X, (int)sprite.Offset.Y, sprite.ScaledBounds.Width, sprite.ScaledBounds.Height, false);
 }