Example #1
0
        public static void vglLoadDDS(string filename, ref vglImageData image)
        {
            System.IO.FileStream   f  = new System.IO.FileStream(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read);
            System.IO.BinaryReader br = new System.IO.BinaryReader(f);

            //DDS_FILE_HEADER file_header = { 0, };
            DDS_FILE_HEADER file_header = new DDS_FILE_HEADER();

            //fread(&file_header, sizeof(file_header.magic) + sizeof(file_header.std_header), 1, f);
            file_header = br.ReadStruct <DDS_FILE_HEADER>();
            file_header.dxt10_header.format     = 0;
            file_header.dxt10_header.array_size = 0;
            f.Position = Marshal.SizeOf(file_header.magic) + Marshal.SizeOf(file_header.std_header);

            if (file_header.magic != DDSSignal.DDS_MAGIC)
            {
                goto done_close_file;
            }

            if (file_header.std_header.ddspf.dwFourCC == DDSSignal.DDS_FOURCC_DX10)
            {
                //fread(&file_header.dxt10_header, sizeof(file_header.dxt10_header), 1, f);
                f.Position = Marshal.SizeOf(file_header.magic) + Marshal.SizeOf(file_header.std_header);
                file_header.dxt10_header = br.ReadStruct <DDS_HEADER_DXT10>();
            }

            if (!vgl_DDSHeaderToImageDataHeader(ref file_header, ref image))
            {
                goto done_close_file;
            }

            image.target = vgl_GetTargetFromDDSHeader(ref file_header);

            if (image.target == GL.GL_NONE)
            {
                goto done_close_file;
            }

            //int current_pos = ftell(f);
            long current_pos = f.Position;
            long file_size   = f.Length;

            image.totalDataSize = (int)(file_size - current_pos);
            var data = new UnmanagedArray <byte>(image.totalDataSize);

            if (image.mip == null)
            {
                image.mip = new vglImageMipData[vermilion.MAX_TEXTURE_MIPS];
            }
            image.mip[0].data = data.Header;
            //image.mip[0].data = new byte[image.totalDataSize];

            //fread(image.mip[0].data, file_size - current_pos, 1, f);
            for (int i = 0; i < image.totalDataSize; i++)
            {
                data[i] = br.ReadByte();
            }

            int    level;
            IntPtr ptr = image.mip[0].data;

            uint width  = file_header.std_header.width;
            uint height = file_header.std_header.height;
            uint depth  = file_header.std_header.depth;

            image.sliceStride = 0;

            if (image.mipLevels == 0)
            {
                image.mipLevels = 1;
            }

            for (level = 0; level < image.mipLevels; ++level)
            {
                image.mip[level].data      = ptr;
                image.mip[level].width     = (int)width;
                image.mip[level].height    = (int)height;
                image.mip[level].depth     = (int)depth;
                image.mip[level].mipStride = (int)(vgl_GetDDSStride(ref file_header, (int)width) * height);
                image.sliceStride         += image.mip[level].mipStride;
                ptr      = new IntPtr(ptr.ToInt32() + image.mip[level].mipStride);
                width  >>= 1;
                height >>= 1;
                depth  >>= 1;
            }

done_close_file:
            f.Close();
        }
        public static uint vglLoadTexture(string filename, uint texture, ref vglImageData image)
        {
            //vglImageData local_image;
            int level;

            //if (image == 0)
            //image = &local_image;

            vglLoadImage(filename, ref image);

            GL gl = GL.Instance;

            if (texture == 0)
            {
                uint[] tmp = new uint[1];
                gl.GenTextures(1, tmp);
                texture = tmp[0];
            }

            gl.BindTexture(image.target, texture);

            if (image.mip == null)
            {
                image.mip = new vglImageMipData[vermilion.MAX_TEXTURE_MIPS];
            }
            IntPtr ptr = image.mip[0].data;

            switch (image.target)
            {
            case GL.GL_TEXTURE_1D:
                gl.TexStorage1D(image.target,
                                image.mipLevels,
                                image.internalFormat,
                                image.mip[0].width);
                for (level = 0; level < image.mipLevels; ++level)
                {
                    gl.TexSubImage1D(GL.GL_TEXTURE_1D,
                                     level,
                                     0,
                                     image.mip[level].width,
                                     image.format, image.type,
                                     image.mip[level].data);
                }
                break;

            case GL.GL_TEXTURE_1D_ARRAY:
                gl.TexStorage2D(image.target,
                                image.mipLevels,
                                image.internalFormat,
                                image.mip[0].width,
                                image.slices);
                for (level = 0; level < image.mipLevels; ++level)
                {
                    gl.TexSubImage2D(GL.GL_TEXTURE_1D,
                                     level,
                                     0, 0,
                                     image.mip[level].width, image.slices,
                                     image.format, image.type,
                                     image.mip[level].data);
                }
                break;

            case GL.GL_TEXTURE_2D:
                gl.TexStorage2D(image.target,
                                image.mipLevels,
                                image.internalFormat,
                                image.mip[0].width,
                                image.mip[0].height);
                for (level = 0; level < image.mipLevels; ++level)
                {
                    gl.TexSubImage2D(GL.GL_TEXTURE_2D,
                                     level,
                                     0, 0,
                                     image.mip[level].width, image.mip[level].height,
                                     image.format, image.type,
                                     image.mip[level].data);
                }
                break;

            case GL.GL_TEXTURE_CUBE_MAP:
                for (level = 0; level < image.mipLevels; ++level)
                {
                    ptr = image.mip[level].data;
                    for (int face = 0; face < 6; face++)
                    {
                        gl.TexImage2D((uint)(GL.GL_TEXTURE_CUBE_MAP_POSITIVE_X + face),
                                      level,
                                      image.internalFormat,
                                      image.mip[level].width, image.mip[level].height,
                                      0,
                                      image.format, image.type,
                                      new IntPtr(ptr.ToInt32() + image.sliceStride * face));
                    }
                }
                break;

            case GL.GL_TEXTURE_2D_ARRAY:
                gl.TexStorage3D(image.target,
                                image.mipLevels,
                                image.internalFormat,
                                image.mip[0].width,
                                image.mip[0].height,
                                image.slices);
                for (level = 0; level < image.mipLevels; ++level)
                {
                    gl.TexSubImage3D(GL.GL_TEXTURE_2D_ARRAY,
                                     level,
                                     0, 0, 0,
                                     image.mip[level].width, image.mip[level].height, image.slices,
                                     image.format, image.type,
                                     image.mip[level].data);
                }
                break;

            case GL.GL_TEXTURE_CUBE_MAP_ARRAY:
                gl.TexStorage3D(image.target,
                                image.mipLevels,
                                image.internalFormat,
                                image.mip[0].width,
                                image.mip[0].height,
                                image.slices);
                break;

            case GL.GL_TEXTURE_3D:
                gl.TexStorage3D(image.target,
                                image.mipLevels,
                                image.internalFormat,
                                image.mip[0].width,
                                image.mip[0].height,
                                image.mip[0].depth);
                for (level = 0; level < image.mipLevels; ++level)
                {
                    gl.TexSubImage3D(GL.GL_TEXTURE_3D,
                                     level,
                                     0, 0, 0,
                                     image.mip[level].width, image.mip[level].height, image.mip[level].depth,
                                     image.format, image.type,
                                     image.mip[level].data);
                }
                break;

            default:
                break;
            }

            int[] swizzle = new int[4];
            for (int i = 0; i < 4; i++)
            {
                swizzle[i] = (int)image.swizzle[i];
            }
            GL.TexParameteriv(image.target, GL.GL_TEXTURE_SWIZZLE_RGBA, swizzle);

            //if (image == &local_image)
            //{
            //vglUnloadImage(image);
            //}

            return(texture);
        }
 void vglUnloadImage(vglImageData image)
 {
 }
Example #4
0
 public static void vglLoadImage(string filename, ref vglImageData image)
 {
     vglLoadDDS(filename, ref image);
 }
 void vglLoadImage(string filename, vglImageData image)
 {
 }
 public static void vglUnloadImage(ref vglImageData image)
 {
     Marshal.FreeHGlobal(image.mip[0].data);
 }
        public static bool vgl_DDSHeaderToImageDataHeader(ref DDS_FILE_HEADER header, ref vglImageData image)
        {
            image.swizzle = new uint[4];

            if (header.std_header.ddspf.dwFlags == DDSSignal.DDS_DDPF_FOURCC &&
                header.std_header.ddspf.dwFourCC == DDSSignal.DDS_FOURCC_DX10)
            {
                if (header.dxt10_header.format < vermilion.NUM_DDS_FORMATS)
                {
                    DDS_FORMAT_GL_INFO format = InfoTable.gl_info_table[header.dxt10_header.format];
                    image.format         = format.format;
                    image.type           = format.type;
                    image.internalFormat = format.internalFormat;
                    image.swizzle[0]     = format.swizzle_r;
                    image.swizzle[1]     = format.swizzle_g;
                    image.swizzle[2]     = format.swizzle_b;
                    image.swizzle[3]     = format.swizzle_a;
                    image.mipLevels      = (int)header.std_header.mip_levels;
                    return(true);
                }
            }
            else if (header.std_header.ddspf.dwFlags == DDSSignal.DDS_DDPF_FOURCC)
            {
                image.swizzle[0] = GL.GL_RED;
                image.swizzle[1] = GL.GL_GREEN;
                image.swizzle[2] = GL.GL_BLUE;
                image.swizzle[3] = GL.GL_ALPHA;
                image.mipLevels  = (int)header.std_header.mip_levels;

                switch (header.std_header.ddspf.dwFourCC)
                {
                case 116:
                    image.format         = GL.GL_RGBA;
                    image.type           = GL.GL_FLOAT;
                    image.internalFormat = GL.GL_RGBA32F;

                    /*
                     * image.swizzle[0] = GL.GL_ALPHA;
                     * image.swizzle[1] = GL.GL_BLUE;
                     * image.swizzle[2] = GL.GL_GREEN;
                     * image.swizzle[3] = GL.GL_RED;
                     */
                    return(true);

                default:
                    break;
                }
            }
            else
            {
                image.swizzle[0] = GL.GL_RED;
                image.swizzle[1] = GL.GL_GREEN;
                image.swizzle[2] = GL.GL_BLUE;
                image.swizzle[3] = GL.GL_ALPHA;
                image.mipLevels  = (int)header.std_header.mip_levels;

                switch (header.std_header.ddspf.dwFlags)
                {
                case DDSSignal.DDS_DDPF_RGB:
                    image.format         = GL.GL_BGR;
                    image.type           = GL.GL_UNSIGNED_BYTE;
                    image.internalFormat = GL.GL_RGB8;
                    image.swizzle[3]     = GL.GL_ONE;
                    return(true);

                case (DDSSignal.DDS_DDPF_RGB | DDSSignal.DDS_DDPF_ALPHA):
                case (DDSSignal.DDS_DDPF_RGB | DDSSignal.DDS_DDPF_ALPHAPIXELS):
                    image.format         = GL.GL_BGRA;
                    image.type           = GL.GL_UNSIGNED_BYTE;
                    image.internalFormat = GL.GL_RGBA8;
                    return(true);

                case DDSSignal.DDS_DDPF_ALPHA:
                    image.format         = GL.GL_RED;
                    image.type           = GL.GL_UNSIGNED_BYTE;
                    image.internalFormat = GL.GL_R8;
                    image.swizzle[0]     = image.swizzle[1] = image.swizzle[2] = GL.GL_ZERO;
                    image.swizzle[3]     = GL.GL_RED;
                    return(true);

                case DDSSignal.DDS_DDPF_LUMINANCE:
                    image.format         = GL.GL_RED;
                    image.type           = GL.GL_UNSIGNED_BYTE;
                    image.internalFormat = GL.GL_R8;
                    image.swizzle[0]     = image.swizzle[1] = image.swizzle[2] = GL.GL_RED;
                    image.swizzle[3]     = GL.GL_ONE;
                    return(true);

                case (DDSSignal.DDS_DDPF_LUMINANCE | DDSSignal.DDS_DDPF_ALPHA):
                    image.format         = GL.GL_RG;
                    image.type           = GL.GL_UNSIGNED_BYTE;
                    image.internalFormat = GL.GL_RG8;
                    image.swizzle[0]     = image.swizzle[1] = image.swizzle[2] = GL.GL_RED;
                    image.swizzle[3]     = GL.GL_GREEN;
                    return(true);

                default:
                    break;
                }
            }

            image.format     = image.type = image.internalFormat = GL.GL_NONE;
            image.swizzle[0] = image.swizzle[1] = image.swizzle[2] = image.swizzle[3] = GL.GL_ZERO;

            return(false);
        }
Example #8
0
        public override void Apply()
        {
            vglImageData imageData = this.imageData;
            IntPtr       ptr       = imageData.mip[0].data;
            int          level;
            GL           gl = GL.Instance;

            switch (imageData.target)
            {
            case GL.GL_TEXTURE_1D:
                glTexStorage1D(imageData.target,
                               imageData.mipLevels,
                               imageData.internalFormat,
                               imageData.mip[0].width);
                for (level = 0; level < imageData.mipLevels; ++level)
                {
                    gl.TexSubImage1D(GL.GL_TEXTURE_1D,
                                     level,
                                     0,
                                     imageData.mip[level].width,
                                     imageData.format, imageData.type,
                                     imageData.mip[level].data);
                }
                break;

            case GL.GL_TEXTURE_1D_ARRAY:
                glTexStorage2D(imageData.target,
                               imageData.mipLevels,
                               imageData.internalFormat,
                               imageData.mip[0].width,
                               imageData.slices);
                for (level = 0; level < imageData.mipLevels; ++level)
                {
                    gl.TexSubImage2D(GL.GL_TEXTURE_1D,
                                     level,
                                     0, 0,
                                     imageData.mip[level].width, imageData.slices,
                                     imageData.format, imageData.type,
                                     imageData.mip[level].data);
                }
                break;

            case GL.GL_TEXTURE_2D:
                glTexStorage2D(imageData.target,
                               imageData.mipLevels,
                               imageData.internalFormat,
                               imageData.mip[0].width,
                               imageData.mip[0].height);
                for (level = 0; level < imageData.mipLevels; ++level)
                {
                    gl.TexSubImage2D(GL.GL_TEXTURE_2D,
                                     level,
                                     0, 0,
                                     imageData.mip[level].width, imageData.mip[level].height,
                                     imageData.format, imageData.type,
                                     imageData.mip[level].data);
                }
                break;

            case GL.GL_TEXTURE_CUBE_MAP:
                for (level = 0; level < imageData.mipLevels; ++level)
                {
                    ptr = imageData.mip[level].data;
                    for (int face = 0; face < 6; face++)
                    {
                        gl.TexImage2D((uint)(GL.GL_TEXTURE_CUBE_MAP_POSITIVE_X + face),
                                      level,
                                      imageData.internalFormat,
                                      imageData.mip[level].width, imageData.mip[level].height,
                                      0,
                                      imageData.format, imageData.type,
                                      new IntPtr(ptr.ToInt32() + imageData.sliceStride * face));
                    }
                }
                break;

            case GL.GL_TEXTURE_2D_ARRAY:
                glTexStorage3D(imageData.target,
                               imageData.mipLevels,
                               imageData.internalFormat,
                               imageData.mip[0].width,
                               imageData.mip[0].height,
                               imageData.slices);
                for (level = 0; level < imageData.mipLevels; ++level)
                {
                    glTexSubImage3D(GL.GL_TEXTURE_2D_ARRAY,
                                    level,
                                    0, 0, 0,
                                    imageData.mip[level].width, imageData.mip[level].height, imageData.slices,
                                    imageData.format, imageData.type,
                                    imageData.mip[level].data);
                }
                break;

            case GL.GL_TEXTURE_CUBE_MAP_ARRAY:
                glTexStorage3D(imageData.target,
                               imageData.mipLevels,
                               imageData.internalFormat,
                               imageData.mip[0].width,
                               imageData.mip[0].height,
                               imageData.slices);
                break;

            case GL.GL_TEXTURE_3D:
                glTexStorage3D(imageData.target,
                               imageData.mipLevels,
                               imageData.internalFormat,
                               imageData.mip[0].width,
                               imageData.mip[0].height,
                               imageData.mip[0].depth);
                for (level = 0; level < imageData.mipLevels; ++level)
                {
                    glTexSubImage3D(GL.GL_TEXTURE_3D,
                                    level,
                                    0, 0, 0,
                                    imageData.mip[level].width, imageData.mip[level].height, imageData.mip[level].depth,
                                    imageData.format, imageData.type,
                                    imageData.mip[level].data);
                }
                break;

            default:
                throw new Exception(string.Format("Not deal with {0}!", imageData.target));
            }
        }
Example #9
0
 public DDSStorage(vglImageData imageData)
     : base((TextureTarget)imageData.target, imageData.internalFormat, imageData.mipLevels, false)
 {
     this.imageData = imageData;
 }