Ejemplo n.º 1
0
        /// <summary>
        /// </summary>
        /// <param name="arrayBuffer">
        /// </param>
        /// <returns>
        /// </returns>
        public static DDSInfo GetDDSInfo(byte[] arrayBuffer)
        {
            var header      = ArrayConvert.AsInt(arrayBuffer, 0, headerLengthInt);
            var mipmapCount = 1;

            if ((header[off_flags] & DDSD_MIPMAPCOUNT) > 0)
            {
                mipmapCount = Math.Max(1, header[off_mipmapCount]);
            }

            return(new DDSInfoDts
            {
                width = header[off_width],
                height = header[off_height],
                mipmapCount = mipmapCount,
                isFourCC = (header[off_pfFlags] & DDPF_FOURCC) == DDPF_FOURCC,
                isRGB = (header[off_pfFlags] & DDPF_RGB) == DDPF_RGB,
                isLuminance = (header[off_pfFlags] & DDPF_LUMINANCE) == DDPF_LUMINANCE,
                isCube = (header[off_caps2] & DDSCAPS2_CUBEMAP) == DDSCAPS2_CUBEMAP
            });
        }
Ejemplo n.º 2
0
        /// <summary>
        /// </summary>
        /// <param name="gl">
        /// </param>
        /// <param name="ext">
        /// </param>
        /// <param name="arrayBuffer">
        /// </param>
        /// <param name="info">
        /// </param>
        /// <param name="loadMipmaps">
        /// </param>
        /// <param name="faces">
        /// </param>
        public static void UploadDDSLevels(
            WebGLRenderingContext gl, WEBGL_compressed_texture_s3tc ext, byte[] arrayBuffer, DDSInfo info, bool loadMipmaps, int faces)
        {
            var header = ArrayConvert.AsInt(arrayBuffer, 0, headerLengthInt);
            int fourCC;
            var blockBytes     = 0;
            var internalFormat = 0;
            int width;
            int height;
            int dataLength;
            int dataOffset;

            byte[] byteArray;
            int    mipmapCount;
            int    i;

            if (header[off_magic] != DDS_MAGIC)
            {
                Tools.Error("Invalid magic number in DDS header");
                return;
            }

            if (!info.isFourCC && !info.isRGB && !info.isLuminance)
            {
                Tools.Error("Unsupported format, must contain a FourCC, RGB or LUMINANCE code");
                return;
            }

            if (info.isFourCC)
            {
                fourCC = header[off_pfFourCC];
                switch (fourCC)
                {
                case FOURCC_DXT1:
                    blockBytes     = 8;
                    internalFormat = ext.COMPRESSED_RGBA_S3TC_DXT1_EXT;
                    break;

                case FOURCC_DXT3:
                    blockBytes     = 16;
                    internalFormat = ext.COMPRESSED_RGBA_S3TC_DXT3_EXT;
                    break;

                case FOURCC_DXT5:
                    blockBytes     = 16;
                    internalFormat = ext.COMPRESSED_RGBA_S3TC_DXT5_EXT;
                    break;

                default:
                    Tools.Error(string.Format("Unsupported FourCC code: {0}", fourCC));
                    return;
                }
            }

            mipmapCount = 1;
            if ((header[off_flags] & DDSD_MIPMAPCOUNT) > 0 && loadMipmaps)
            {
                mipmapCount = Math.Max(1, header[off_mipmapCount]);
            }

            var bpp = header[off_RGBbpp];

            for (var face = 0; face < faces; face++)
            {
                var sampler = (faces == 1) ? Gl.TEXTURE_2D : (Gl.TEXTURE_CUBE_MAP_POSITIVE_X + face);
                width      = header[off_width];
                height     = header[off_height];
                dataOffset = header[off_size] + 4;
                for (i = 0; i < mipmapCount; ++i)
                {
                    if (info.isRGB)
                    {
                        if (bpp == 24)
                        {
                            dataLength = width * height * 3;
                            byteArray  = GetRGBArrayBuffer(width, height, dataOffset, dataLength, arrayBuffer);
                            gl.texImage2D(sampler, i, Gl.RGB, width, height, 0, Gl.RGB, Gl.UNSIGNED_BYTE, byteArray);
                        }
                        else
                        {
                            dataLength = width * height * 4;
                            byteArray  = GetRGBAArrayBuffer(width, height, dataOffset, dataLength, arrayBuffer);
                            gl.texImage2D(sampler, i, Gl.RGBA, width, height, 0, Gl.RGBA, Gl.UNSIGNED_BYTE, byteArray);
                        }
                    }
                    else if (info.isLuminance)
                    {
                        var unpackAlignment = (int)gl.getParameter(Gl.UNPACK_ALIGNMENT);
                        var unpaddedRowSize = width;
                        var paddedRowSize   = (int)Math.Floor((double)(width + unpackAlignment - 1) / unpackAlignment) * unpackAlignment;
                        dataLength = paddedRowSize * (height - 1) + unpaddedRowSize;
                        byteArray  = GetLuminanceArrayBuffer(width, height, dataOffset, dataLength, arrayBuffer);
                        gl.texImage2D(sampler, i, Gl.LUMINANCE, width, height, 0, Gl.LUMINANCE, Gl.UNSIGNED_BYTE, byteArray);
                    }
                    else
                    {
                        dataLength = Math.Max(4, width) / 4 * Math.Max(4, height) / 4 * blockBytes;
                        byteArray  = ArrayConvert.AsByte(arrayBuffer, dataOffset, dataLength);
                        gl.compressedTexImage2D(sampler, i, internalFormat, width, height, 0, byteArray);
                    }

                    dataOffset += dataLength;
                    width      /= 2;
                    height     /= 2;
                    width       = Math.Max(1, width);
                    height      = Math.Max(1, height);
                }
            }
        }