Example #1
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 #2
0
        public static int generate2DTexture(PixelInternalFormat fmt, int w, int h, PixelFormat pix_fmt, PixelType pix_type, int mipmap_count)
        {
            int tex_id = GL.GenTexture();

            GL.BindTexture(TextureTarget.Texture2D, tex_id);
            GL.TexImage2D(TextureTarget.Texture2D, 0, fmt, w, h, 0, pix_fmt, pix_type, IntPtr.Zero);
            return(tex_id);
        }
Example #3
0
 /// <summary>
 /// Retrieves the texture data.
 /// </summary>
 public static T[,] GetContent <T>(this Texture2D texture, PixelFormat pixelFormat, PixelType pixelType, int level = 0)
 where T : struct
 {
     var data = new T[texture.Width, texture.Height];
     texture.Bind();
     GL.GetTexImage(texture.TextureTarget, level, pixelFormat, pixelType, data);
     return(data);
 }
Example #4
0
        /// <summary>
        /// Define the texture's image contents for one level
        /// </summary>
        /// <param name="dimensions">Dimensions of the level</param>
        /// <param name="level">Which level</param>
        /// <param name="internalFormat">The internal image format</param>
        /// <param name="imageFormat">The source image pixel components</param>
        /// <param name="imageType">The source image pixel type</param>
        /// <param name="imageData">Pointer to the source image data, leave as zero for no source image data</param>
        /// <exception cref="ArgumentOutOfRangeException"></exception>
        public void Image2D(
            Vector2 <int> dimensions, int level             = 0, PixelInternalFormat internalFormat = PixelInternalFormat.Rgba,
            OpenTK.Graphics.OpenGL4.PixelFormat imageFormat = OpenTK.Graphics.OpenGL4.PixelFormat.Rgba,
            PixelType imageType = PixelType.UnsignedByte, IntPtr imageData = default
            )
        {
            this.Assert();

            if (level < 0 || level >= Levels)
            {
                throw new ArgumentOutOfRangeException(nameof(level));
            }
            Bind(TextureTarget.Texture2D);
            Dimensions[level] = dimensions;
            Formats[level]    = internalFormat;
            GL.TexImage2D(TextureTarget.Texture2D, level, internalFormat, dimensions.X, dimensions.Y, 0, imageFormat, imageType, imageData);
        }
Example #5
0
        internal void SetData <T>(T[] data, int level, OpenTK.Graphics.OpenGL4.PixelFormat format = OpenTK.Graphics.OpenGL4.PixelFormat.Bgra) where T : struct//ValueType
        {
            bool hwCompressed = format == (OpenTK.Graphics.OpenGL4.PixelFormat)engenious.Content.TextureContentFormat.DXT1 || format == (OpenTK.Graphics.OpenGL4.PixelFormat)engenious.Content.TextureContentFormat.DXT3 || format == (OpenTK.Graphics.OpenGL4.PixelFormat)engenious.Content.TextureContentFormat.DXT5;

            if (!hwCompressed && Marshal.SizeOf(typeof(T)) * data.Length < Width * Height)
            {
                throw new ArgumentException("Not enough pixel data");
            }

            unsafe
            {
                int width = Width, height = Height;
                for (int i = 0; i < level; i++)
                {
                    width  /= 2;
                    height /= 2;
                    if (width == 0 || height == 0)
                    {
                        return;
                    }
                }
                GCHandle handle = GCHandle.Alloc(data, GCHandleType.Pinned);
                ThreadingHelper.BlockOnUIThread(() =>
                {
                    Bind();
                    PixelType pxType = PixelType.UnsignedByte;
                    if (typeof(T) == typeof(Color))
                    {
                        pxType = PixelType.Float;
                    }
                    if (hwCompressed)
                    {
                        int blockSize = (format == (OpenTK.Graphics.OpenGL4.PixelFormat)engenious.Content.TextureContentFormat.DXT1) ? 8 : 16;
                        int mipSize   = ((width + 3) / 4) * ((height + 3) / 4) * blockSize;
                        GL.CompressedTexSubImage2D(TextureTarget.Texture2D, level, 0, 0, width, height, (OpenTK.Graphics.OpenGL4.PixelFormat)format, mipSize, handle.AddrOfPinnedObject());
                    }
                    else
                    {
                        GL.TexSubImage2D(TextureTarget.Texture2D, level, 0, 0, width, height, format, pxType, handle.AddrOfPinnedObject());
                    }
                });
                handle.Free();
            }
            //GL.TexSubImage2D<T> (TextureTarget.Texture2D, 0, 0, 0, Width, Height, OpenTK.Graphics.OpenGL4.PixelFormat.Bgra, getPixelType (typeof(T)), data);
        }
Example #6
0
        public TextureMaterial(Rhino.RhinoDoc doc, int width, int height, OpenTK.Graphics.OpenGL4.PixelFormat pixelFormat,
                               OpenTK.Graphics.OpenGL4.PixelType pixelType)
        {
            texWidth  = width;
            texHeight = height;

            mDoc    = doc;
            mShader = new GLShader();
            mShader.init("TextureMaterial", ShaderSource.TextureVertShader, ShaderSource.TextureFragShader);
            mShader.bind();

            m_iTexture = GL.GenTexture();
            GL.BindTexture(TextureTarget.Texture2D, m_iTexture);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
            GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgb, width, height, 0, pixelFormat, pixelType, IntPtr.Zero);

            GL.BindTexture(TextureTarget.Texture2D, 0);
        }
Example #7
0
 /// <summary>
 /// Instantiates a new Texture2D
 /// </summary>
 /// <param name="width">Width of the texture</param>
 /// <param name="height">Height of the texture</param>
 /// <param name="internalFormat">Internal Format</param>
 /// <param name="pixelFormat">Pixel Format</param>
 /// <param name="pixelType">Pixel Type</param>
 public Texture2D(int width, int height, PixelInternalFormat internalFormat, OpenTK.Graphics.OpenGL4.PixelFormat pixelFormat, PixelType pixelType)
     : this()
 {
     OpenGL.Invoke(() =>
     {
         this.Bind();
         GL.TexImage2D(
             this.Target,
             0,
             internalFormat,
             width, height,
             0,
             pixelFormat,
             pixelType,
             IntPtr.Zero);
     });
     this.Width  = width;
     this.Height = height;
 }
Example #8
0
        public Texture2D SetData <T> (T[] data, Rectangle?rect = null, GLPixelFormat pixelFormat = GLPixelFormat.Rgba, PixelType pixelType = PixelType.UnsignedByte) where T : struct
        {
            Rectangle r = rect ?? new Rectangle(0, 0, Width, Height);

            Bind(TextureUnit.Texture0);
            GL.TexSubImage2D(
                target: TextureTarget.Texture2D,
                level: 0,
                xoffset: r.X,
                yoffset: r.Y,
                width: r.Width,
                height: r.Height,
                format: pixelFormat,
                type: pixelType,
                pixels: data
                );
            Unbind(TextureUnit.Texture0);

            return(this);
        }
Example #9
0
        public Texture(T[] data, int width, int height, PixelInternalFormat pixelInternalFormat, PixelType pixelType, PixelFormat pixelFormat)
        {
            lock (Lock)
            {
                var textruesId = new int[1];
                GL.GenTextures(1, textruesId);

                Id = Count;
                Count++;
                GL.ActiveTexture(TextureUnit.Texture0 + Id);
                GL.BindTexture(TextureTarget.Texture2D, textruesId[0]);

                GL.TexImage2D(TextureTarget.Texture2D, 0, pixelInternalFormat, width, height, 0, pixelFormat, pixelType, data);
            }
        }
Example #10
0
        internal static int LoadTextureSkybox(string filename, bool isInAssembly = false)
        {
            Assembly a = Assembly.GetEntryAssembly();

            if (!filename.ToLower().EndsWith("jpg") && !filename.ToLower().EndsWith("jpeg") && !filename.ToLower().EndsWith("png"))
            {
                throw new Exception("Only JPG and PNG files are supported.");
            }

            if (!KWEngine.CustomTextures[KWEngine.CurrentWorld].ContainsKey(filename))
            {
                try
                {
                    using (Stream s = isInAssembly ? a.GetManifestResourceStream(a.GetName().Name + "." + filename) : File.Open(filename, FileMode.Open))
                    {
                        Bitmap image            = new Bitmap(s);
                        int    width            = image.Width;
                        int    height           = image.Height;
                        int    height_onethird  = height / 3;
                        int    width_onequarter = width / 4;

                        Bitmap image_front = new Bitmap(width_onequarter, height_onethird, image.PixelFormat);
                        Bitmap image_back  = new Bitmap(width_onequarter, height_onethird, image.PixelFormat);
                        Bitmap image_up    = new Bitmap(width_onequarter, height_onethird, image.PixelFormat);
                        Bitmap image_down  = new Bitmap(width_onequarter, height_onethird, image.PixelFormat);
                        Bitmap image_left  = new Bitmap(width_onequarter, height_onethird, image.PixelFormat);
                        Bitmap image_right = new Bitmap(width_onequarter, height_onethird, image.PixelFormat);

                        Graphics g = null;
                        //front
                        g = Graphics.FromImage(image_front);
                        g.DrawImage(image,
                                    new Rectangle(0, 0, width_onequarter, height_onethird),
                                    new Rectangle(2 * width_onequarter, height_onethird, width_onequarter, height_onethird),
                                    GraphicsUnit.Pixel
                                    );
                        g.Dispose();

                        //back
                        g = Graphics.FromImage(image_back);
                        g.DrawImage(image,
                                    new Rectangle(0, 0, width_onequarter, height_onethird),
                                    new Rectangle(0, height_onethird, width_onequarter, height_onethird),
                                    GraphicsUnit.Pixel
                                    );
                        g.Dispose();

                        //up
                        g = Graphics.FromImage(image_up);
                        g.DrawImage(image,
                                    new Rectangle(0, 0, width_onequarter, height_onethird),
                                    new Rectangle(width_onequarter, 0, width_onequarter, height_onethird),
                                    GraphicsUnit.Pixel
                                    );
                        g.Dispose();

                        //down
                        g = Graphics.FromImage(image_down);
                        g.DrawImage(image,
                                    new Rectangle(0, 0, width_onequarter, height_onethird),
                                    new Rectangle(width_onequarter, 2 * height_onethird, width_onequarter, height_onethird),
                                    GraphicsUnit.Pixel
                                    );
                        g.Dispose();

                        //left
                        g = Graphics.FromImage(image_left);
                        g.DrawImage(image,
                                    new Rectangle(0, 0, width_onequarter, height_onethird),
                                    new Rectangle(width_onequarter, height_onethird, width_onequarter, height_onethird),
                                    GraphicsUnit.Pixel
                                    );
                        g.Dispose();

                        //right
                        g = Graphics.FromImage(image_right);
                        g.DrawImage(image,
                                    new Rectangle(0, 0, width_onequarter, height_onethird),
                                    new Rectangle(3 * width_onequarter, height_onethird, width_onequarter, height_onethird),
                                    GraphicsUnit.Pixel
                                    );
                        g.Dispose();

                        int newTexture = GL.GenTexture();
                        GL.BindTexture(TextureTarget.TextureCubeMap, newTexture);
                        BitmapData data = null;

                        PixelInternalFormat iFormat = image.PixelFormat == System.Drawing.Imaging.PixelFormat.Format24bppRgb ? PixelInternalFormat.Rgb : PixelInternalFormat.Rgba;
                        OpenTK.Graphics.OpenGL4.PixelFormat pxFormat = image.PixelFormat == System.Drawing.Imaging.PixelFormat.Format24bppRgb ? OpenTK.Graphics.OpenGL4.PixelFormat.Bgr : OpenTK.Graphics.OpenGL4.PixelFormat.Bgra;

                        // front
                        data = image_front.LockBits(new Rectangle(0, 0, image_front.Width, image_front.Height), ImageLockMode.ReadOnly, image.PixelFormat);
                        GL.TexImage2D(TextureTarget.TextureCubeMapPositiveX, 0, iFormat, data.Width, data.Height, 0, pxFormat, PixelType.UnsignedByte, data.Scan0);
                        image_front.UnlockBits(data);

                        // back
                        data = image_back.LockBits(new Rectangle(0, 0, image_back.Width, image_back.Height), ImageLockMode.ReadOnly, image.PixelFormat);
                        GL.TexImage2D(TextureTarget.TextureCubeMapNegativeX, 0, iFormat, data.Width, data.Height, 0, pxFormat, PixelType.UnsignedByte, data.Scan0);
                        image_back.UnlockBits(data);

                        // up
                        data = image_up.LockBits(new Rectangle(0, 0, image_up.Width, image_up.Height), ImageLockMode.ReadOnly, image.PixelFormat);
                        GL.TexImage2D(TextureTarget.TextureCubeMapPositiveY, 0, iFormat, data.Width, data.Height, 0, pxFormat, PixelType.UnsignedByte, data.Scan0);
                        image_up.UnlockBits(data);

                        // down
                        data = image_down.LockBits(new Rectangle(0, 0, image_down.Width, image_down.Height), ImageLockMode.ReadOnly, image.PixelFormat);
                        GL.TexImage2D(TextureTarget.TextureCubeMapNegativeY, 0, iFormat, data.Width, data.Height, 0, pxFormat, PixelType.UnsignedByte, data.Scan0);
                        image_down.UnlockBits(data);

                        // left
                        data = image_left.LockBits(new Rectangle(0, 0, image_left.Width, image_left.Height), ImageLockMode.ReadOnly, image.PixelFormat);
                        GL.TexImage2D(TextureTarget.TextureCubeMapPositiveZ, 0, iFormat, data.Width, data.Height, 0, pxFormat, PixelType.UnsignedByte, data.Scan0);
                        image_left.UnlockBits(data);

                        // right
                        data = image_right.LockBits(new Rectangle(0, 0, image_right.Width, image_right.Height), ImageLockMode.ReadOnly, image.PixelFormat);
                        GL.TexImage2D(TextureTarget.TextureCubeMapNegativeZ, 0, iFormat, data.Width, data.Height, 0, pxFormat, PixelType.UnsignedByte, data.Scan0);
                        image_right.UnlockBits(data);

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

                        KWEngine.CustomTextures[KWEngine.CurrentWorld].Add(filename, newTexture);

                        image.Dispose();
                        image_front.Dispose();
                        image_back.Dispose();
                        image_up.Dispose();
                        image_down.Dispose();
                        image_left.Dispose();
                        image_right.Dispose();
                        GL.BindTexture(TextureTarget.TextureCubeMap, 0);

                        return(newTexture);
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("Error loading skybox texture: " + filename + " (" + ex.Message + ")");
                    return(-1);
                }
            }
            else
            {
                int id = -1;
                KWEngine.CustomTextures[KWEngine.CurrentWorld].TryGetValue(filename, out id);
                return(id);
            }
        }
Example #11
0
 public Texture2D(int width, int height, PixelInternalFormat internalformat, OpenTK.Graphics.OpenGL4.PixelFormat format) : this(width, height, internalformat, format, PixelType.UnsignedByte, IntPtr.Zero)
 {
 }
Example #12
0
 public Texture2D(int width, int height, PixelInternalFormat internalformat, OpenTK.Graphics.OpenGL4.PixelFormat format, PixelType type) : this(width, height, internalformat, format, type, IntPtr.Zero)
 {
     this.SetFiltering(TextureMinFilter.Nearest, TextureMagFilter.Nearest);
     this.SetWarpMode(TextureWrapMode.ClampToEdge);
 }
Example #13
0
 /// <summary>
 /// Allocate image memory and fill it with the specified data.
 /// </summary>
 /// <param name="target">texture target</param>
 /// <param name="width">texture width</param>
 /// <param name="height">texture height</param>
 /// <param name="depth">number of texture layers</param>
 /// <param name="length">number of texture layers</param>
 /// <param name="format">pixel format of the data to be uploaded</param>
 /// <param name="type">pixel type of the data to be uploaded</param>
 /// <param name="pixels">pixel data</param>
 private void TexImage(TexTarget target, int width, int height, int depth, int length,
     PixelFormat format, PixelType type, IntPtr pixels)
 {
     var colFormat = (GpuColorFormat)GpuFormat;
     switch (target)
     {
         case TexTarget.Texture1D:
             GL.TexImage1D(target, 0, colFormat, width, 0, format, type, pixels);
             break;
         case TexTarget.Texture1DArray:
             GL.TexImage2D(target, 0, colFormat, width, length, 0, format, type, pixels);
             break;
         case TexTarget.Texture2D:
             GL.TexImage2D(target, 0, colFormat, width, height, 0, format, type, pixels);
             break;
         case TexTarget.Texture2DArray:
             GL.TexImage3D(target, 0, colFormat, width, height, length, 0, format, type, pixels);
             break;
         case TexTarget.Texture3D:
             GL.TexImage3D(target, 0, colFormat, width, height, depth, 0, format, type, pixels);
             break;
     }
 }
Example #14
0
 /// <summary>
 /// Get the number of color channels from the specified pixel format.
 /// </summary>
 /// <param name="format"></param>
 /// <returns></returns>
 private static int ColorChannels(PixelFormat format)
 {
     switch (format)
     {
         case PixelFormat.Red:
         case PixelFormat.RedInteger:
         case PixelFormat.Green:
         case PixelFormat.GreenInteger:
         case PixelFormat.Blue:
         case PixelFormat.BlueInteger:
         case PixelFormat.Alpha:
         case PixelFormat.AlphaInteger:
         case PixelFormat.StencilIndex:
         case PixelFormat.UnsignedInt:
         case PixelFormat.UnsignedShort:
         case PixelFormat.DepthComponent:
         case PixelFormat.ColorIndex:
         case PixelFormat.Luminance:
             return 1;
         case PixelFormat.Rg:
         case PixelFormat.RgInteger:
         case PixelFormat.DepthStencil:
             return 2;
         case PixelFormat.Rgb:
         case PixelFormat.Bgr:
         case PixelFormat.BgrInteger:
             return 3;
         default:
             return 4;
     }
 }
Example #15
0
        /// <summary>
        /// Read GPU sub-image data.
        /// </summary>
        /// <param name="ID">OpenGL texture name</param>
        /// <param name="level">texture mipmap level</param>
        /// <param name="x">sub-image x offset</param>
        /// <param name="y">sub-image y offset</param>
        /// <param name="z">sub-image z offset (for texture arrays and 3D textures)</param>
        /// <param name="w">sub-image width</param>
        /// <param name="h">sub-image height</param>
        /// <param name="d">sub-image depth</param>
        /// <param name="format">pixel format (RGBA, BRGA, Red, Depth, ...)</param>
        /// <param name="type">pixel type (UnsignedByte, Short, UnsignedInt, ...)</param>
        /// <param name="size">returns the buffer size in bytes</param>
        /// <returns>Returns a buffer containing the specified sub-image region.</returns>
        private static IntPtr ReadSubImage(int ID, int level, int x, int y, int z, int w, int h, int d,
            PixelFormat format, PixelType type, out int size)
        {
            // compute size of the sub-image
            size = w * h * d * ColorChannels(format) * ColorBits(type) / 8;

            // read image data from GPU
            var data = Marshal.AllocHGlobal(size);
            GL.GetTextureSubImage(ID, level, x, y, z, w, h, d, format, type, size, data);
            return data;
        }
Example #16
0
        public Texture2D(TextureTarget target, PixelInternalFormat internalFormat, GLPixelFormat format, PixelType type, InterpolationMode mode, bool mipmap, int width, int height)
        {
            Width  = width;
            Height = height;

            // Get the texture id
            TextureId = GL.GenTexture();

            // Bind the texture
            Bind(TextureUnit.Texture0);

            // Choose which filters to use
            TextureMinFilter minfilter = TextureMinFilter.Linear;
            TextureMagFilter magfilter = TextureMagFilter.Linear;

            switch (mode)
            {
            case InterpolationMode.Linear:
                minfilter = mipmap
                                                ? TextureMinFilter.LinearMipmapLinear
                                                : TextureMinFilter.Linear;
                magfilter = TextureMagFilter.Linear;
                break;

            case InterpolationMode.Nearest:
                minfilter = mipmap
                                                ? TextureMinFilter.LinearMipmapNearest
                                                : TextureMinFilter.Nearest;
                magfilter = TextureMagFilter.Nearest;
                break;
            }

            // Set the min filter parameter
            GL.TexParameter(
                target: TextureTarget.Texture2D,
                pname: TextureParameterName.TextureMinFilter,
                param: (int)minfilter
                );

            // Set the mag filter parameter
            GL.TexParameter(
                target: TextureTarget.Texture2D,
                pname: TextureParameterName.TextureMagFilter,
                param: (int)magfilter
                );

            // Create the texture
            GL.TexImage2D(
                target: target,
                level: 0,
                internalformat: internalFormat,
                width: Width,
                height: Height,
                border: 0,
                format: format,
                type: type,
                pixels: IntPtr.Zero
                );

            // Create a mipmap if requested
            if (mipmap)
            {
                GL.GenerateMipmap(GenerateMipmapTarget.Texture2D);
            }

            Unbind(TextureUnit.Texture0);
        }