Search in sources :

Example 36 with Pointer

use of com.jme3.scene.plugins.blender.file.Pointer in project jmonkeyengine by jMonkeyEngine.

the class Edge method loadAll.

/**
     * The method loads all edges from the given mesh structure that does not belong to any face.
     * @param meshStructure
     *            the mesh structure
     * @param temporalMesh
     *            the owner of the edges
     * @return all edges without faces
     * @throws BlenderFileException
     *             an exception is thrown when problems with file reading occur
     */
public static List<Edge> loadAll(Structure meshStructure, TemporalMesh temporalMesh) throws BlenderFileException {
    LOGGER.log(Level.FINE, "Loading all edges that do not belong to any face from mesh: {0}", meshStructure.getName());
    List<Edge> result = new ArrayList<Edge>();
    Pointer pMEdge = (Pointer) meshStructure.getFieldValue("medge");
    if (pMEdge.isNotNull()) {
        List<Structure> edges = pMEdge.fetchData();
        for (Structure edge : edges) {
            int flag = ((Number) edge.getFieldValue("flag")).intValue();
            int v1 = ((Number) edge.getFieldValue("v1")).intValue();
            int v2 = ((Number) edge.getFieldValue("v2")).intValue();
            // I do not know why, but blender stores (possibly only sometimes) crease as negative values and shows positive in the editor
            float crease = Math.abs(((Number) edge.getFieldValue("crease")).floatValue());
            boolean edgeInFace = (flag & Edge.FLAG_EDGE_NOT_IN_FACE) == 0;
            result.add(new Edge(v1, v2, crease, edgeInFace, temporalMesh));
        }
    }
    LOGGER.log(Level.FINE, "Loaded {0} edges.", result.size());
    return result;
}
Also used : ArrayList(java.util.ArrayList) Pointer(com.jme3.scene.plugins.blender.file.Pointer) Structure(com.jme3.scene.plugins.blender.file.Structure)

Example 37 with Pointer

use of com.jme3.scene.plugins.blender.file.Pointer 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 38 with Pointer

use of com.jme3.scene.plugins.blender.file.Pointer in project jmonkeyengine by jMonkeyEngine.

the class AnimationHelper method getTracks249.

/**
     * This method retuns the bone tracks for animation for blender version 2.49
     * (and probably several lower versions too).
     * 
     * @param actionStructure
     *            the structure containing the tracks
     * @param blenderContext
     *            the blender context
     * @return a list of tracks for the specified animation
     * @throws BlenderFileException
     *             an exception is thrown when there are problems with the blend
     *             file
     */
private BlenderAction getTracks249(Structure actionStructure, BlenderContext blenderContext) throws BlenderFileException {
    LOGGER.log(Level.FINE, "Getting tracks!");
    Structure chanbase = (Structure) actionStructure.getFieldValue("chanbase");
    // bActionChannel
    List<Structure> actionChannels = chanbase.evaluateListBase();
    BlenderAction blenderAction = new BlenderAction(actionStructure.getName(), blenderContext.getBlenderKey().getFps());
    int lastFrame = 1;
    for (Structure bActionChannel : actionChannels) {
        String animatedFeatureName = bActionChannel.getFieldValue("name").toString();
        Pointer p = (Pointer) bActionChannel.getFieldValue("ipo");
        if (!p.isNull()) {
            Structure ipoStructure = p.fetchData().get(0);
            Ipo ipo = this.fromIpoStructure(ipoStructure, blenderContext);
            if (ipo != null) {
                // this can happen when ipo with no curves appear in blender file
                lastFrame = Math.max(lastFrame, ipo.getLastFrame());
                blenderAction.featuresTracks.put(animatedFeatureName, ipo);
            }
        }
    }
    blenderAction.stopFrame = lastFrame;
    return blenderAction;
}
Also used : ConstIpo(com.jme3.scene.plugins.blender.animations.Ipo.ConstIpo) Pointer(com.jme3.scene.plugins.blender.file.Pointer) Structure(com.jme3.scene.plugins.blender.file.Structure)

Example 39 with Pointer

use of com.jme3.scene.plugins.blender.file.Pointer in project jmonkeyengine by jMonkeyEngine.

the class AnimationHelper method fromIpoStructure.

/**
     * This method creates an ipo object used for interpolation calculations.
     * 
     * @param ipoStructure
     *            the structure with ipo definition
     * @param blenderContext
     *            the blender context
     * @return the ipo object
     * @throws BlenderFileException
     *             this exception is thrown when the blender file is somehow
     *             corrupted
     */
public Ipo fromIpoStructure(Structure ipoStructure, BlenderContext blenderContext) throws BlenderFileException {
    Structure curvebase = (Structure) ipoStructure.getFieldValue("curve");
    // preparing bezier curves
    Ipo result = null;
    // IpoCurve
    List<Structure> curves = curvebase.evaluateListBase();
    if (curves.size() > 0) {
        BezierCurve[] bezierCurves = new BezierCurve[curves.size()];
        int frame = 0;
        for (Structure curve : curves) {
            Pointer pBezTriple = (Pointer) curve.getFieldValue("bezt");
            List<Structure> bezTriples = pBezTriple.fetchData();
            int type = ((Number) curve.getFieldValue("adrcode")).intValue();
            bezierCurves[frame++] = new BezierCurve(type, bezTriples, 2);
        }
        curves.clear();
        result = new Ipo(bezierCurves, fixUpAxis, blenderContext.getBlenderVersion());
        Long ipoOma = ipoStructure.getOldMemoryAddress();
        blenderContext.addLoadedFeatures(ipoOma, LoadedDataType.STRUCTURE, ipoStructure);
        blenderContext.addLoadedFeatures(ipoOma, LoadedDataType.FEATURE, result);
    }
    return result;
}
Also used : ConstIpo(com.jme3.scene.plugins.blender.animations.Ipo.ConstIpo) Pointer(com.jme3.scene.plugins.blender.file.Pointer) Structure(com.jme3.scene.plugins.blender.file.Structure) BezierCurve(com.jme3.scene.plugins.blender.curves.BezierCurve)

Example 40 with Pointer

use of com.jme3.scene.plugins.blender.file.Pointer in project jmonkeyengine by jMonkeyEngine.

the class AnimationHelper method getTracks250.

/**
     * This method retuns the bone tracks for animation for blender version 2.50
     * and higher.
     * 
     * @param actionStructure
     *            the structure containing the tracks
     * @param blenderContext
     *            the blender context
     * @return a list of tracks for the specified animation
     * @throws BlenderFileException
     *             an exception is thrown when there are problems with the blend
     *             file
     */
private BlenderAction getTracks250(Structure actionStructure, BlenderContext blenderContext) throws BlenderFileException {
    LOGGER.log(Level.FINE, "Getting tracks!");
    Structure groups = (Structure) actionStructure.getFieldValue("groups");
    // bActionGroup
    List<Structure> actionGroups = groups.evaluateListBase();
    BlenderAction blenderAction = new BlenderAction(actionStructure.getName(), blenderContext.getBlenderKey().getFps());
    int lastFrame = 1;
    for (Structure actionGroup : actionGroups) {
        String name = actionGroup.getFieldValue("name").toString();
        List<Structure> channels = ((Structure) actionGroup.getFieldValue("channels")).evaluateListBase();
        BezierCurve[] bezierCurves = new BezierCurve[channels.size()];
        int channelCounter = 0;
        for (Structure c : channels) {
            int type = this.getCurveType(c, blenderContext);
            Pointer pBezTriple = (Pointer) c.getFieldValue("bezt");
            List<Structure> bezTriples = pBezTriple.fetchData();
            bezierCurves[channelCounter++] = new BezierCurve(type, bezTriples, 2);
        }
        Ipo ipo = new Ipo(bezierCurves, fixUpAxis, blenderContext.getBlenderVersion());
        lastFrame = Math.max(lastFrame, ipo.getLastFrame());
        blenderAction.featuresTracks.put(name, ipo);
    }
    blenderAction.stopFrame = lastFrame;
    return blenderAction;
}
Also used : ConstIpo(com.jme3.scene.plugins.blender.animations.Ipo.ConstIpo) Pointer(com.jme3.scene.plugins.blender.file.Pointer) Structure(com.jme3.scene.plugins.blender.file.Structure) BezierCurve(com.jme3.scene.plugins.blender.curves.BezierCurve)

Aggregations

Pointer (com.jme3.scene.plugins.blender.file.Pointer)30 Structure (com.jme3.scene.plugins.blender.file.Structure)27 ArrayList (java.util.ArrayList)12 APIBuffer (com.jme3.lwjgl3.utils.APIBuffer)6 DynamicArray (com.jme3.scene.plugins.blender.file.DynamicArray)6 List (java.util.List)6 FileBlockHeader (com.jme3.scene.plugins.blender.file.FileBlockHeader)5 Vector2f (com.jme3.math.Vector2f)4 Vector3f (com.jme3.math.Vector3f)4 Map (java.util.Map)4 Spline (com.jme3.math.Spline)3 Spatial (com.jme3.scene.Spatial)3 ConstIpo (com.jme3.scene.plugins.blender.animations.Ipo.ConstIpo)3 BlenderFileException (com.jme3.scene.plugins.blender.file.BlenderFileException)3 BlenderInputStream (com.jme3.scene.plugins.blender.file.BlenderInputStream)3 TouchEvent (com.jme3.input.event.TouchEvent)2 Transform (com.jme3.math.Transform)2 CameraNode (com.jme3.scene.CameraNode)2 LightNode (com.jme3.scene.LightNode)2 Node (com.jme3.scene.Node)2