Search in sources :

Example 41 with Format

use of com.jme3.scene.VertexBuffer.Format in project jmonkeyengine by jMonkeyEngine.

the class TGALoader method load.

/**
     * <code>loadImage</code> is a manual image loader which is entirely
     * independent of AWT. OUT: RGB888 or RGBA8888 Image object
     * 
     * 
    
     * @param in
     *            InputStream of an uncompressed 24b RGB or 32b RGBA TGA
     * @param flip
     *            Flip the image vertically
     * @return <code>Image</code> object that contains the
     *         image, either as a RGB888 or RGBA8888
     * @throws java.io.IOException
     */
public static Image load(InputStream in, boolean flip) throws IOException {
    boolean flipH = false;
    // open a stream to the file
    DataInputStream dis = new DataInputStream(new BufferedInputStream(in));
    // ---------- Start Reading the TGA header ---------- //
    // length of the image id (1 byte)
    int idLength = dis.readUnsignedByte();
    // Type of color map (if any) included with the image
    // 0 - no color map data is included
    // 1 - a color map is included
    int colorMapType = dis.readUnsignedByte();
    // Type of image being read:
    int imageType = dis.readUnsignedByte();
    // Read Color Map Specification (5 bytes)
    // Index of first color map entry (if we want to use it, uncomment and remove extra read.)
    //        short cMapStart = flipEndian(dis.readShort());
    dis.readShort();
    // number of entries in the color map
    short cMapLength = flipEndian(dis.readShort());
    // number of bits per color map entry
    int cMapDepth = dis.readUnsignedByte();
    // Read Image Specification (10 bytes)
    // horizontal coordinate of lower left corner of image. (if we want to use it, uncomment and remove extra read.)
    //        int xOffset = flipEndian(dis.readShort());
    dis.readShort();
    // vertical coordinate of lower left corner of image. (if we want to use it, uncomment and remove extra read.)
    //        int yOffset = flipEndian(dis.readShort());
    dis.readShort();
    // width of image - in pixels
    int width = flipEndian(dis.readShort());
    // height of image - in pixels
    int height = flipEndian(dis.readShort());
    // bits per pixel in image.
    int pixelDepth = dis.readUnsignedByte();
    int imageDescriptor = dis.readUnsignedByte();
    if (// bit 5 : if 1, flip top/bottom ordering
    (imageDescriptor & 32) != 0) {
        flip = !flip;
    }
    if (// bit 4 : if 1, flip left/right ordering
    (imageDescriptor & 16) != 0) {
        flipH = !flipH;
    }
    // Skip image ID
    if (idLength > 0) {
        dis.skip(idLength);
    }
    ColorMapEntry[] cMapEntries = null;
    if (colorMapType != 0) {
        // read the color map.
        int bytesInColorMap = (cMapDepth * cMapLength) >> 3;
        int bitsPerColor = Math.min(cMapDepth / 3, 8);
        byte[] cMapData = new byte[bytesInColorMap];
        dis.read(cMapData);
        // table if this is declared a color mapped image.
        if (imageType == TYPE_COLORMAPPED || imageType == TYPE_COLORMAPPED_RLE) {
            cMapEntries = new ColorMapEntry[cMapLength];
            int alphaSize = cMapDepth - (3 * bitsPerColor);
            float scalar = 255f / (FastMath.pow(2, bitsPerColor) - 1);
            float alphaScalar = 255f / (FastMath.pow(2, alphaSize) - 1);
            for (int i = 0; i < cMapLength; i++) {
                ColorMapEntry entry = new ColorMapEntry();
                int offset = cMapDepth * i;
                entry.red = (byte) (int) (getBitsAsByte(cMapData, offset, bitsPerColor) * scalar);
                entry.green = (byte) (int) (getBitsAsByte(cMapData, offset + bitsPerColor, bitsPerColor) * scalar);
                entry.blue = (byte) (int) (getBitsAsByte(cMapData, offset + (2 * bitsPerColor), bitsPerColor) * scalar);
                if (alphaSize <= 0) {
                    entry.alpha = (byte) 255;
                } else {
                    entry.alpha = (byte) (int) (getBitsAsByte(cMapData, offset + (3 * bitsPerColor), alphaSize) * alphaScalar);
                }
                cMapEntries[i] = entry;
            }
        }
    }
    // Allocate image data array
    Format format;
    byte[] rawData = null;
    int dl;
    if (pixelDepth == 32) {
        rawData = new byte[width * height * 4];
        dl = 4;
    } else {
        rawData = new byte[width * height * 3];
        dl = 3;
    }
    int rawDataIndex = 0;
    if (imageType == TYPE_TRUECOLOR) {
        byte red = 0;
        byte green = 0;
        byte blue = 0;
        byte alpha = 0;
        // just make a seperate loop for each.
        if (pixelDepth == 16) {
            byte[] data = new byte[2];
            float scalar = 255f / 31f;
            for (int i = 0; i <= (height - 1); i++) {
                if (!flip) {
                    rawDataIndex = (height - 1 - i) * width * dl;
                }
                for (int j = 0; j < width; j++) {
                    data[1] = dis.readByte();
                    data[0] = dis.readByte();
                    rawData[rawDataIndex++] = (byte) (int) (getBitsAsByte(data, 1, 5) * scalar);
                    rawData[rawDataIndex++] = (byte) (int) (getBitsAsByte(data, 6, 5) * scalar);
                    rawData[rawDataIndex++] = (byte) (int) (getBitsAsByte(data, 11, 5) * scalar);
                    if (dl == 4) {
                        // create an alpha channel
                        alpha = getBitsAsByte(data, 0, 1);
                        if (alpha == 1) {
                            alpha = (byte) 255;
                        }
                        rawData[rawDataIndex++] = alpha;
                    }
                }
            }
            format = dl == 4 ? Format.RGBA8 : Format.RGB8;
        } else if (pixelDepth == 24) {
            for (int y = 0; y < height; y++) {
                if (!flip) {
                    rawDataIndex = (height - 1 - y) * width * dl;
                } else {
                    rawDataIndex = y * width * dl;
                }
                dis.readFully(rawData, rawDataIndex, width * dl);
            //                    for (int x = 0; x < width; x++) {
            //read scanline
            //                        blue = dis.readByte();
            //                        green = dis.readByte();
            //                        red = dis.readByte();
            //                        rawData[rawDataIndex++] = red;
            //                        rawData[rawDataIndex++] = green;
            //                        rawData[rawDataIndex++] = blue;
            //                    }
            }
            format = Format.BGR8;
        } else if (pixelDepth == 32) {
            for (int i = 0; i <= (height - 1); i++) {
                if (!flip) {
                    rawDataIndex = (height - 1 - i) * width * dl;
                }
                for (int j = 0; j < width; j++) {
                    blue = dis.readByte();
                    green = dis.readByte();
                    red = dis.readByte();
                    alpha = dis.readByte();
                    rawData[rawDataIndex++] = red;
                    rawData[rawDataIndex++] = green;
                    rawData[rawDataIndex++] = blue;
                    rawData[rawDataIndex++] = alpha;
                }
            }
            format = Format.RGBA8;
        } else {
            throw new IOException("Unsupported TGA true color depth: " + pixelDepth);
        }
    } else if (imageType == TYPE_TRUECOLOR_RLE) {
        byte red = 0;
        byte green = 0;
        byte blue = 0;
        byte alpha = 0;
        // just make a seperate loop for each.
        if (pixelDepth == 32) {
            for (int i = 0; i <= (height - 1); ++i) {
                if (!flip) {
                    rawDataIndex = (height - 1 - i) * width * dl;
                }
                for (int j = 0; j < width; ++j) {
                    // Get the number of pixels the next chunk covers (either packed or unpacked)
                    int count = dis.readByte();
                    if ((count & 0x80) != 0) {
                        // Its an RLE packed block - use the following 1 pixel for the next <count> pixels
                        count &= 0x07f;
                        j += count;
                        blue = dis.readByte();
                        green = dis.readByte();
                        red = dis.readByte();
                        alpha = dis.readByte();
                        while (count-- >= 0) {
                            rawData[rawDataIndex++] = red;
                            rawData[rawDataIndex++] = green;
                            rawData[rawDataIndex++] = blue;
                            rawData[rawDataIndex++] = alpha;
                        }
                    } else {
                        // Its not RLE packed, but the next <count> pixels are raw.
                        j += count;
                        while (count-- >= 0) {
                            blue = dis.readByte();
                            green = dis.readByte();
                            red = dis.readByte();
                            alpha = dis.readByte();
                            rawData[rawDataIndex++] = red;
                            rawData[rawDataIndex++] = green;
                            rawData[rawDataIndex++] = blue;
                            rawData[rawDataIndex++] = alpha;
                        }
                    }
                }
            }
            format = Format.RGBA8;
        } else if (pixelDepth == 24) {
            for (int i = 0; i <= (height - 1); i++) {
                if (!flip) {
                    rawDataIndex = (height - 1 - i) * width * dl;
                }
                for (int j = 0; j < width; ++j) {
                    // Get the number of pixels the next chunk covers (either packed or unpacked)
                    int count = dis.readByte();
                    if ((count & 0x80) != 0) {
                        // Its an RLE packed block - use the following 1 pixel for the next <count> pixels
                        count &= 0x07f;
                        j += count;
                        blue = dis.readByte();
                        green = dis.readByte();
                        red = dis.readByte();
                        while (count-- >= 0) {
                            rawData[rawDataIndex++] = red;
                            rawData[rawDataIndex++] = green;
                            rawData[rawDataIndex++] = blue;
                        }
                    } else {
                        // Its not RLE packed, but the next <count> pixels are raw.
                        j += count;
                        while (count-- >= 0) {
                            blue = dis.readByte();
                            green = dis.readByte();
                            red = dis.readByte();
                            rawData[rawDataIndex++] = red;
                            rawData[rawDataIndex++] = green;
                            rawData[rawDataIndex++] = blue;
                        }
                    }
                }
            }
            format = Format.RGB8;
        } else if (pixelDepth == 16) {
            byte[] data = new byte[2];
            float scalar = 255f / 31f;
            for (int i = 0; i <= (height - 1); i++) {
                if (!flip) {
                    rawDataIndex = (height - 1 - i) * width * dl;
                }
                for (int j = 0; j < width; j++) {
                    // Get the number of pixels the next chunk covers (either packed or unpacked)
                    int count = dis.readByte();
                    if ((count & 0x80) != 0) {
                        // Its an RLE packed block - use the following 1 pixel for the next <count> pixels
                        count &= 0x07f;
                        j += count;
                        data[1] = dis.readByte();
                        data[0] = dis.readByte();
                        blue = (byte) (int) (getBitsAsByte(data, 1, 5) * scalar);
                        green = (byte) (int) (getBitsAsByte(data, 6, 5) * scalar);
                        red = (byte) (int) (getBitsAsByte(data, 11, 5) * scalar);
                        while (count-- >= 0) {
                            rawData[rawDataIndex++] = red;
                            rawData[rawDataIndex++] = green;
                            rawData[rawDataIndex++] = blue;
                        }
                    } else {
                        // Its not RLE packed, but the next <count> pixels are raw.
                        j += count;
                        while (count-- >= 0) {
                            data[1] = dis.readByte();
                            data[0] = dis.readByte();
                            blue = (byte) (int) (getBitsAsByte(data, 1, 5) * scalar);
                            green = (byte) (int) (getBitsAsByte(data, 6, 5) * scalar);
                            red = (byte) (int) (getBitsAsByte(data, 11, 5) * scalar);
                            rawData[rawDataIndex++] = red;
                            rawData[rawDataIndex++] = green;
                            rawData[rawDataIndex++] = blue;
                        }
                    }
                }
            }
            format = Format.RGB8;
        } else {
            throw new IOException("Unsupported TGA true color depth: " + pixelDepth);
        }
    } else if (imageType == TYPE_COLORMAPPED) {
        int bytesPerIndex = pixelDepth / 8;
        if (bytesPerIndex == 1) {
            for (int i = 0; i <= (height - 1); i++) {
                if (!flip) {
                    rawDataIndex = (height - 1 - i) * width * dl;
                }
                for (int j = 0; j < width; j++) {
                    int index = dis.readUnsignedByte();
                    if (index >= cMapEntries.length || index < 0) {
                        throw new IOException("TGA: Invalid color map entry referenced: " + index);
                    }
                    ColorMapEntry entry = cMapEntries[index];
                    rawData[rawDataIndex++] = entry.blue;
                    rawData[rawDataIndex++] = entry.green;
                    rawData[rawDataIndex++] = entry.red;
                    if (dl == 4) {
                        rawData[rawDataIndex++] = entry.alpha;
                    }
                }
            }
        } else if (bytesPerIndex == 2) {
            for (int i = 0; i <= (height - 1); i++) {
                if (!flip) {
                    rawDataIndex = (height - 1 - i) * width * dl;
                }
                for (int j = 0; j < width; j++) {
                    int index = flipEndian(dis.readShort());
                    if (index >= cMapEntries.length || index < 0) {
                        throw new IOException("TGA: Invalid color map entry referenced: " + index);
                    }
                    ColorMapEntry entry = cMapEntries[index];
                    rawData[rawDataIndex++] = entry.blue;
                    rawData[rawDataIndex++] = entry.green;
                    rawData[rawDataIndex++] = entry.red;
                    if (dl == 4) {
                        rawData[rawDataIndex++] = entry.alpha;
                    }
                }
            }
        } else {
            throw new IOException("TGA: unknown colormap indexing size used: " + bytesPerIndex);
        }
        format = dl == 4 ? Format.RGBA8 : Format.RGB8;
    } else {
        throw new IOException("Monochrome and RLE colormapped images are not supported");
    }
    in.close();
    // Get a pointer to the image memory
    ByteBuffer scratch = BufferUtils.createByteBuffer(rawData.length);
    scratch.clear();
    scratch.put(rawData);
    scratch.rewind();
    // Create the Image object
    Image textureImage = new Image();
    textureImage.setFormat(format);
    textureImage.setWidth(width);
    textureImage.setHeight(height);
    textureImage.setData(scratch);
    return textureImage;
}
Also used : IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) Image(com.jme3.texture.Image) ByteBuffer(java.nio.ByteBuffer) Format(com.jme3.texture.Image.Format) BufferedInputStream(java.io.BufferedInputStream)

Example 42 with Format

use of com.jme3.scene.VertexBuffer.Format in project jmonkeyengine by jMonkeyEngine.

the class KTXLoader method load.

private Image load(InputStream stream) {
    byte[] fileId = new byte[12];
    DataInput in = new DataInputStream(stream);
    try {
        stream.read(fileId, 0, 12);
        if (!checkFileIdentifier(fileId)) {
            throw new IllegalArgumentException("Unrecognized ktx file identifier : " + new String(fileId) + " should be " + new String(fileIdentifier));
        }
        int endianness = in.readInt();
        //opposite endianness
        if (endianness == 0x01020304) {
            in = new LittleEndien(stream);
        }
        int glType = in.readInt();
        int glTypeSize = in.readInt();
        int glFormat = in.readInt();
        int glInternalFormat = in.readInt();
        int glBaseInternalFormat = in.readInt();
        int pixelWidth = in.readInt();
        int pixelHeight = in.readInt();
        int pixelDepth = in.readInt();
        int numberOfArrayElements = in.readInt();
        int numberOfFaces = in.readInt();
        int numberOfMipmapLevels = in.readInt();
        int bytesOfKeyValueData = in.readInt();
        log.log(Level.FINE, "glType = {0}", glType);
        log.log(Level.FINE, "glTypeSize = {0}", glTypeSize);
        log.log(Level.FINE, "glFormat = {0}", glFormat);
        log.log(Level.FINE, "glInternalFormat = {0}", glInternalFormat);
        log.log(Level.FINE, "glBaseInternalFormat = {0}", glBaseInternalFormat);
        log.log(Level.FINE, "pixelWidth = {0}", pixelWidth);
        log.log(Level.FINE, "pixelHeight = {0}", pixelHeight);
        log.log(Level.FINE, "pixelDepth = {0}", pixelDepth);
        log.log(Level.FINE, "numberOfArrayElements = {0}", numberOfArrayElements);
        log.log(Level.FINE, "numberOfFaces = {0}", numberOfFaces);
        log.log(Level.FINE, "numberOfMipmapLevels = {0}", numberOfMipmapLevels);
        log.log(Level.FINE, "bytesOfKeyValueData = {0}", bytesOfKeyValueData);
        if ((numberOfFaces > 1 && pixelDepth > 1) || (numberOfFaces > 1 && numberOfArrayElements > 1) || (pixelDepth > 1 && numberOfArrayElements > 1)) {
            throw new UnsupportedOperationException("jME doesn't support cube maps of 3D textures or arrays of 3D texture or arrays of cube map of 3d textures");
        }
        PixelReader pixelReader = parseMetaData(bytesOfKeyValueData, in);
        if (pixelReader == null) {
            pixelReader = new SrTuRoPixelReader();
        }
        //some of the values may be 0 we need them at least to be 1
        pixelDepth = Math.max(1, pixelDepth);
        numberOfArrayElements = Math.max(1, numberOfArrayElements);
        numberOfFaces = Math.max(1, numberOfFaces);
        numberOfMipmapLevels = Math.max(1, numberOfMipmapLevels);
        int nbSlices = Math.max(numberOfFaces, numberOfArrayElements);
        Image.Format imgFormat = getImageFormat(glFormat, glInternalFormat, glType);
        log.log(Level.FINE, "img format {0}", imgFormat.toString());
        int bytePerPixel = imgFormat.getBitsPerPixel() / 8;
        int byteBuffersSize = computeBuffersSize(numberOfMipmapLevels, pixelWidth, pixelHeight, bytePerPixel, pixelDepth);
        log.log(Level.FINE, "data size {0}", byteBuffersSize);
        int[] mipMapSizes = new int[numberOfMipmapLevels];
        Image image = createImage(nbSlices, byteBuffersSize, imgFormat, pixelWidth, pixelHeight, pixelDepth);
        byte[] pixelData = new byte[bytePerPixel];
        int offset = 0;
        //iterate over data
        for (int mipLevel = 0; mipLevel < numberOfMipmapLevels; mipLevel++) {
            //size of the image in byte.
            //this value is bogus in many example, when using mipmaps.
            //instead we compute the theorical size and display a warning when it does not match.
            int fileImageSize = in.readInt();
            int width = Math.max(1, pixelWidth >> mipLevel);
            int height = Math.max(1, pixelHeight >> mipLevel);
            int imageSize = width * height * bytePerPixel;
            mipMapSizes[mipLevel] = imageSize;
            log.log(Level.FINE, "current mip size {0}", imageSize);
            if (fileImageSize != imageSize) {
                log.log(Level.WARNING, "Mip map size is wrong in the file for mip level {0} size is {1} should be {2}", new Object[] { mipLevel, fileImageSize, imageSize });
            }
            for (int arrayElem = 0; arrayElem < numberOfArrayElements; arrayElem++) {
                for (int face = 0; face < numberOfFaces; face++) {
                    int nbPixelRead = 0;
                    for (int depth = 0; depth < pixelDepth; depth++) {
                        ByteBuffer byteBuffer = image.getData(getSlice(face, arrayElem));
                        log.log(Level.FINE, "position {0}", byteBuffer.position());
                        byteBuffer.position(offset);
                        nbPixelRead = pixelReader.readPixels(width, height, pixelData, byteBuffer, in);
                    }
                    //cube padding
                    if (numberOfFaces == 6 && numberOfArrayElements == 0) {
                        in.skipBytes(3 - ((nbPixelRead + 3) % 4));
                    }
                }
            }
            //mip padding
            log.log(Level.FINE, "skipping {0}", (3 - ((imageSize + 3) % 4)));
            in.skipBytes(3 - ((imageSize + 3) % 4));
            offset += imageSize;
        }
        //there are loaded mip maps we set the sizes
        if (numberOfMipmapLevels > 1) {
            image.setMipMapSizes(mipMapSizes);
        }
        //if 3D texture and slices' orientation is inside, we reverse the data array.
        if (pixelDepth > 1 && slicesInside) {
            Collections.reverse(image.getData());
        }
        return image;
    } catch (IOException ex) {
        Logger.getLogger(KTXLoader.class.getName()).log(Level.SEVERE, null, ex);
    }
    return null;
}
Also used : IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) Image(com.jme3.texture.Image) ByteBuffer(java.nio.ByteBuffer) LittleEndien(com.jme3.util.LittleEndien) DataInput(java.io.DataInput)

Example 43 with Format

use of com.jme3.scene.VertexBuffer.Format in project jmonkeyengine by jMonkeyEngine.

the class KTXWriter method write.

/**
     * Writes an image with the given params
     * 
     * textureType, allows one to write textureArrays, Texture3D, and TextureCubeMaps.
     * Texture2D will write a 2D image.
     * Note that the fileName should contain the extension (.ktx sounds like a wise choice)
     * @param image the image to write
     * @param textureType the texture type
     * @param fileName the name of the file to write
     */
public void write(Image image, Class<? extends Texture> textureType, String fileName) {
    FileOutputStream outs = null;
    try {
        File file = new File(filePath + "/" + fileName);
        outs = new FileOutputStream(file);
        DataOutput out = new DataOutputStream(outs);
        //fileID
        out.write(fileIdentifier);
        //endianness
        out.writeInt(0x04030201);
        GLImageFormat format = getGlFormat(image.getFormat());
        //glType
        out.writeInt(format.dataType);
        //glTypeSize
        out.writeInt(1);
        //glFormat
        out.writeInt(format.format);
        //glInernalFormat
        out.writeInt(format.internalFormat);
        //glBaseInternalFormat
        out.writeInt(format.format);
        //pixelWidth
        out.writeInt(image.getWidth());
        //pixelHeight
        out.writeInt(image.getHeight());
        int pixelDepth = 1;
        int numberOfArrayElements = 1;
        int numberOfFaces = 1;
        if (image.getDepth() > 1) {
            //pixelDepth
            if (textureType == Texture3D.class) {
                pixelDepth = image.getDepth();
            }
        }
        if (image.getData().size() > 1) {
            //numberOfArrayElements
            if (textureType == TextureArray.class) {
                numberOfArrayElements = image.getData().size();
            }
            //numberOfFaces                
            if (textureType == TextureCubeMap.class) {
                numberOfFaces = image.getData().size();
            }
        }
        out.writeInt(pixelDepth);
        out.writeInt(numberOfArrayElements);
        out.writeInt(numberOfFaces);
        int numberOfMipmapLevels = 1;
        //numberOfMipmapLevels
        if (image.hasMipmaps()) {
            numberOfMipmapLevels = image.getMipMapSizes().length;
        }
        out.writeInt(numberOfMipmapLevels);
        //bytesOfKeyValueData
        String keyValues = "KTXorientation\0S=r,T=u\0";
        int bytesOfKeyValueData = keyValues.length() + 4;
        int padding = 3 - ((bytesOfKeyValueData + 3) % 4);
        bytesOfKeyValueData += padding;
        out.writeInt(bytesOfKeyValueData);
        //keyAndValueByteSize
        out.writeInt(bytesOfKeyValueData - 4 - padding);
        //values
        out.writeBytes(keyValues);
        pad(padding, out);
        int offset = 0;
        //iterate over data
        for (int mipLevel = 0; mipLevel < numberOfMipmapLevels; mipLevel++) {
            int width = Math.max(1, image.getWidth() >> mipLevel);
            int height = Math.max(1, image.getHeight() >> mipLevel);
            int imageSize;
            if (image.hasMipmaps()) {
                imageSize = image.getMipMapSizes()[mipLevel];
            } else {
                imageSize = width * height * image.getFormat().getBitsPerPixel() / 8;
            }
            out.writeInt(imageSize);
            for (int arrayElem = 0; arrayElem < numberOfArrayElements; arrayElem++) {
                for (int face = 0; face < numberOfFaces; face++) {
                    int nbPixelWritten = 0;
                    for (int depth = 0; depth < pixelDepth; depth++) {
                        ByteBuffer byteBuffer = image.getData(getSlice(face, arrayElem));
                        // BufferUtils.ensureLargeEnough(byteBuffer, imageSize);
                        log.log(Level.FINE, "position {0}", byteBuffer.position());
                        byteBuffer.position(offset);
                        byte[] b = getByteBufferArray(byteBuffer, imageSize);
                        out.write(b);
                        nbPixelWritten = b.length;
                    }
                    //cube padding
                    if (numberOfFaces == 6 && numberOfArrayElements == 0) {
                        padding = 3 - ((nbPixelWritten + 3) % 4);
                        pad(padding, out);
                    }
                }
            }
            //mip padding
            log.log(Level.FINE, "skipping {0}", (3 - ((imageSize + 3) % 4)));
            padding = 3 - ((imageSize + 3) % 4);
            pad(padding, out);
            offset += imageSize;
        }
    } catch (FileNotFoundException ex) {
        Logger.getLogger(KTXWriter.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(KTXWriter.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        try {
            if (outs != null) {
                outs.close();
            }
        } catch (IOException ex) {
            Logger.getLogger(KTXWriter.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
Also used : DataOutput(java.io.DataOutput) DataOutputStream(java.io.DataOutputStream) FileOutputStream(java.io.FileOutputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) GLImageFormat(com.jme3.renderer.opengl.GLImageFormat) File(java.io.File) ByteBuffer(java.nio.ByteBuffer)

Example 44 with Format

use of com.jme3.scene.VertexBuffer.Format in project jmonkeyengine by jMonkeyEngine.

the class ShaderNodeLoaderDelegate method readInputMapping.

/**
     * reads an input mapping
     *
     * @param statement1 the statement being read
     * @return the mapping
     * @throws IOException
     */
public VariableMapping readInputMapping(Statement statement1) throws IOException {
    VariableMapping mapping = null;
    try {
        mapping = parseMapping(statement1, new boolean[] { false, true });
    } catch (Exception e) {
        throw new MatParseException("Unexpected mapping format", statement1, e);
    }
    ShaderNodeVariable left = mapping.getLeftVariable();
    ShaderNodeVariable right = mapping.getRightVariable();
    if (!updateVariableFromList(left, shaderNode.getDefinition().getInputs())) {
        throw new MatParseException(left.getName() + " is not an input variable of " + shaderNode.getDefinition().getName(), statement1);
    }
    if (left.getType().startsWith("sampler") && !right.getNameSpace().equals("MatParam")) {
        throw new MatParseException("Samplers can only be assigned to MatParams", statement1);
    }
    if (right.getNameSpace().equals("Global")) {
        //Globals are all vec4 for now (maybe forever...)
        right.setType("vec4");
        //        updateCondition(right, mapping);
        storeGlobal(right, statement1);
    } else if (right.getNameSpace().equals("Attr")) {
        if (shaderNode.getDefinition().getType() == Shader.ShaderType.Fragment) {
            throw new MatParseException("Cannot have an attribute as input in a fragment shader" + right.getName(), statement1);
        }
        updateVarFromAttributes(mapping.getRightVariable(), mapping);
        //          updateCondition(mapping.getRightVariable(), mapping);
        storeAttribute(mapping.getRightVariable());
    } else if (right.getNameSpace().equals("MatParam")) {
        MatParam param = findMatParam(right.getName());
        if (param == null) {
            throw new MatParseException("Could not find a Material Parameter named " + right.getName(), statement1);
        }
        if (shaderNode.getDefinition().getType() == Shader.ShaderType.Vertex) {
            if (updateRightFromUniforms(param, mapping, vertexDeclaredUniforms, statement1)) {
                storeVertexUniform(mapping.getRightVariable());
            }
        } else {
            if (updateRightFromUniforms(param, mapping, fragmentDeclaredUniforms, statement1)) {
                if (mapping.getRightVariable().getType().contains("|")) {
                    String type = fixSamplerType(left.getType(), mapping.getRightVariable().getType());
                    if (type != null) {
                        mapping.getRightVariable().setType(type);
                    } else {
                        throw new MatParseException(param.getVarType().toString() + " can only be matched to one of " + param.getVarType().getGlslType().replaceAll("\\|", ",") + " found " + left.getType(), statement1);
                    }
                }
                storeFragmentUniform(mapping.getRightVariable());
            }
        }
    } else if (right.getNameSpace().equals("WorldParam")) {
        UniformBinding worldParam = findWorldParam(right.getName());
        if (worldParam == null) {
            throw new MatParseException("Could not find a World Parameter named " + right.getName(), statement1);
        }
        if (shaderNode.getDefinition().getType() == Shader.ShaderType.Vertex) {
            if (updateRightFromUniforms(worldParam, mapping, vertexDeclaredUniforms)) {
                storeVertexUniform(mapping.getRightVariable());
            }
        } else {
            if (updateRightFromUniforms(worldParam, mapping, fragmentDeclaredUniforms)) {
                storeFragmentUniform(mapping.getRightVariable());
            }
        }
    } else {
        ShaderNode node = nodes.get(right.getNameSpace());
        if (node == null) {
            throw new MatParseException("Undeclared node" + right.getNameSpace() + ". Make sure this node is declared before the current node", statement1);
        }
        ShaderNodeVariable var = findNodeOutput(node.getDefinition().getOutputs(), right.getName());
        if (var == null) {
            throw new MatParseException("Cannot find output variable" + right.getName() + " form ShaderNode " + node.getName(), statement1);
        }
        right.setNameSpace(node.getName());
        right.setType(var.getType());
        right.setMultiplicity(var.getMultiplicity());
        mapping.setRightVariable(right);
        storeVaryings(node, mapping.getRightVariable());
    }
    checkTypes(mapping, statement1);
    return mapping;
}
Also used : UniformBinding(com.jme3.shader.UniformBinding) MatParam(com.jme3.material.MatParam) ShaderNode(com.jme3.shader.ShaderNode) VariableMapping(com.jme3.shader.VariableMapping) ShaderNodeVariable(com.jme3.shader.ShaderNodeVariable) IOException(java.io.IOException) AssetNotFoundException(com.jme3.asset.AssetNotFoundException)

Example 45 with Format

use of com.jme3.scene.VertexBuffer.Format in project jmonkeyengine by jMonkeyEngine.

the class ShaderNodeLoaderDelegate method readOutputMapping.

/**
     * reads an output mapping
     *
     * @param statement1 the statement being read
     * @return the mapping
     * @throws IOException
     */
public VariableMapping readOutputMapping(Statement statement1) throws IOException {
    VariableMapping mapping = null;
    try {
        mapping = parseMapping(statement1, new boolean[] { true, false });
    } catch (Exception e) {
        throw new MatParseException("Unexpected mapping format", statement1, e);
    }
    ShaderNodeVariable left = mapping.getLeftVariable();
    ShaderNodeVariable right = mapping.getRightVariable();
    if (left.getType().startsWith("sampler") || right.getType().startsWith("sampler")) {
        throw new MatParseException("Samplers can only be inputs", statement1);
    }
    if (left.getNameSpace().equals("Global")) {
        //Globals are all vec4 for now (maybe forever...)
        left.setType("vec4");
        storeGlobal(left, statement1);
    } else {
        throw new MatParseException("Only Global nameSpace is allowed for outputMapping, got" + left.getNameSpace(), statement1);
    }
    if (!updateVariableFromList(right, shaderNode.getDefinition().getOutputs())) {
        throw new MatParseException(right.getName() + " is not an output variable of " + shaderNode.getDefinition().getName(), statement1);
    }
    checkTypes(mapping, statement1);
    return mapping;
}
Also used : VariableMapping(com.jme3.shader.VariableMapping) ShaderNodeVariable(com.jme3.shader.ShaderNodeVariable) IOException(java.io.IOException) AssetNotFoundException(com.jme3.asset.AssetNotFoundException)

Aggregations

Image (com.jme3.texture.Image)20 ByteBuffer (java.nio.ByteBuffer)19 Format (com.jme3.texture.Image.Format)13 IOException (java.io.IOException)9 ArrayList (java.util.ArrayList)8 ColorRGBA (com.jme3.math.ColorRGBA)7 BufferedImage (java.awt.image.BufferedImage)7 TextureCubeMap (com.jme3.texture.TextureCubeMap)6 Vector3f (com.jme3.math.Vector3f)4 TexturePixel (com.jme3.scene.plugins.blender.textures.TexturePixel)4 PixelInputOutput (com.jme3.scene.plugins.blender.textures.io.PixelInputOutput)4 Texture2D (com.jme3.texture.Texture2D)4 DataInputStream (java.io.DataInputStream)4 AssetNotFoundException (com.jme3.asset.AssetNotFoundException)3 LittleEndien (com.jme3.util.LittleEndien)3 InputStream (java.io.InputStream)3 TextureKey (com.jme3.asset.TextureKey)2 VRAPI (com.jme3.input.vr.VRAPI)2 MatParam (com.jme3.material.MatParam)2 Material (com.jme3.material.Material)2