示例#1
0
        /// <summary>
        /// Initializes a render texture with a certain ARGB color (0xAARRGGBB) and a scale factor.
        /// </summary>
        public RenderTexture(float width, float height, uint argbFillColor = 0x0, float scale = 1.0f)
        {
            int legalWidth  = NumberUtil.NextPowerOfTwo(width * scale);
            int legalHeight = NumberUtil.NextPowerOfTwo(height * scale);

            TextureProperties properties = new TextureProperties
            {
                TextureFormat      = Sparrow.Textures.TextureFormat.Rgba8888,
                Scale              = scale,
                Width              = legalWidth,
                Height             = legalHeight,
                NumMipmaps         = 0,
                PremultipliedAlpha = true
            };

            Rectangle region    = new Rectangle(0, 0, width, height);
            GLTexture glTexture = new GLTexture(IntPtr.Zero, properties);

            Init(glTexture, region);
            _renderSupport = new RenderSupport();
            Clear(argbFillColor, ColorUtil.GetA(argbFillColor) / 255.0f);
        }
        /// <summary>
        /// Initializes a render texture with a certain ARGB color (0xAARRGGBB) and a scale factor.
        /// </summary>
        public RenderTexture(float width, float height, uint argbFillColor = 0x0, float scale = 1.0f)
        {
            int legalWidth = NumberUtil.NextPowerOfTwo(width * scale);
            int legalHeight = NumberUtil.NextPowerOfTwo(height * scale);

            TextureProperties properties = new TextureProperties
            {
                TextureFormat = Sparrow.Textures.TextureFormat.Rgba8888,
                Scale = scale,
                Width = legalWidth,
                Height = legalHeight,
                NumMipmaps = 0,
                PremultipliedAlpha = true
            };

            Rectangle region = new Rectangle(0, 0, width, height);
            GLTexture glTexture = new GLTexture(IntPtr.Zero, properties);

            Init(glTexture, region);
            _renderSupport = new RenderSupport();
            Clear(argbFillColor, ColorUtil.GetA(argbFillColor) / 255.0f);
        }
示例#3
0
        /// <summary>
        /// Initializes an uncompressed texture with with raw pixel data and a set of properties.
        /// Width and Height are expected pixel dimensions.
        /// </summary>
        public GLTexture(IntPtr imgData, TextureProperties properties)
        {
            uint glTexName;
            bool compressed = properties.TextureFormat.Compressed;
            GL.GenTextures(1, out glTexName);
            GL.BindTexture(TextureTarget.Texture2D, glTexName);

#if __WINDOWS__
            GL.TexStorage2D(TextureTarget2d.Texture2D,
#elif __ANDROID__
            GL.TexStorage2D(TextureTarget2D.Texture2D,
#endif
                properties.NumMipmaps + 1, // mipmap level, min 1
                properties.TextureFormat.InternalFormat,
                properties.Width,
                properties.Height);

            if (!compressed)
            {
                if (imgData != IntPtr.Zero)
                {
                    GL.TexSubImage2D(TextureTarget.Texture2D,
                        0, // level
                        0, // xOffset
                        0, // yOffset
                        properties.Width,
                        properties.Height,
                        properties.TextureFormat.PixelFormat,
                        properties.TextureFormat.PixelType,
                        imgData);
                }
            }
            else
            {
                // TODO this is not tested!
                if (imgData != IntPtr.Zero)
                {
                    int size = Math.Max(32, properties.Width * properties.Height * properties.TextureFormat.BitsPerPixel / 8);
                    GL.CompressedTexSubImage2D(TextureTarget.Texture2D,
                        0, // level
                        0, // xOffset
                        0, // yOffset
                        properties.Width,
                        properties.Height,
                        properties.TextureFormat.PixelFormat,
                        size,
                        imgData);
                }
            }

            if (properties.NumMipmaps > 0)
            {
#if __WINDOWS__
                GL.GenerateMipmap(GenerateMipmapTarget.Texture2D);
#elif __ANDROID__
                GL.GenerateMipmap(TextureTarget.Texture2D);
#endif
            }

            bool containsMipmaps = properties.NumMipmaps > 0;

            Init(glTexName, properties.Width, properties.Height, containsMipmaps, properties.Scale, properties.PremultipliedAlpha);
        }
        private Texture CreateTexture(int width, int height, float scale)
        {
            int legalWidth = NumberUtil.NextPowerOfTwo(width * scale);
            int legalHeight = NumberUtil.NextPowerOfTwo(height * scale);

            TextureProperties texProps = new TextureProperties
            {
                TextureFormat = TextureFormat.Rgba8888,
                Scale = scale,
                Width = legalWidth,
                Height = legalHeight,
                NumMipmaps = 0,
                PremultipliedAlpha = true
            };

            return new GLTexture(IntPtr.Zero, texProps);
        }
示例#5
0
        /// <summary>
        /// Initializes an uncompressed texture with with raw pixel data and a set of properties.
        /// Width and Height are expected pixel dimensions.
        /// </summary>
        public GLTexture(IntPtr imgData, TextureProperties properties)
        {
            uint glTexName;
            bool compressed = properties.TextureFormat.Compressed;

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

#if __WINDOWS__
            GL.TexStorage2D(TextureTarget2d.Texture2D,
#elif __ANDROID__
            GL.TexStorage2D(TextureTarget2D.Texture2D,
#endif
                            properties.NumMipmaps + 1, // mipmap level, min 1
                            properties.TextureFormat.InternalFormat,
                            properties.Width,
                            properties.Height);

            if (!compressed)
            {
                if (imgData != IntPtr.Zero)
                {
                    GL.TexSubImage2D(TextureTarget.Texture2D,
                                     0, // level
                                     0, // xOffset
                                     0, // yOffset
                                     properties.Width,
                                     properties.Height,
                                     properties.TextureFormat.PixelFormat,
                                     properties.TextureFormat.PixelType,
                                     imgData);
                }
            }
            else
            {
                // TODO this is not tested!
                if (imgData != IntPtr.Zero)
                {
                    int size = Math.Max(32, properties.Width * properties.Height * properties.TextureFormat.BitsPerPixel / 8);
                    GL.CompressedTexSubImage2D(TextureTarget.Texture2D,
                                               0, // level
                                               0, // xOffset
                                               0, // yOffset
                                               properties.Width,
                                               properties.Height,
                                               properties.TextureFormat.PixelFormat,
                                               size,
                                               imgData);
                }
            }

            if (properties.NumMipmaps > 0)
            {
#if __WINDOWS__
                GL.GenerateMipmap(GenerateMipmapTarget.Texture2D);
#elif __ANDROID__
                GL.GenerateMipmap(TextureTarget.Texture2D);
#endif
            }

            bool containsMipmaps = properties.NumMipmaps > 0;

            Init(glTexName, properties.Width, properties.Height, containsMipmaps, properties.Scale, properties.PremultipliedAlpha);
        }