Search in sources :

Example 1 with BlenderInputStream

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

the class AnimationHelper method getCurveType.

/**
     * This method returns the type of the ipo curve.
     * 
     * @param structure
     *            the structure must contain the 'rna_path' field and
     *            'array_index' field (the type is not important here)
     * @param blenderContext
     *            the blender context
     * @return the type of the curve
     */
public int getCurveType(Structure structure, BlenderContext blenderContext) {
    // reading rna path first
    BlenderInputStream bis = blenderContext.getInputStream();
    int currentPosition = bis.getPosition();
    Pointer pRnaPath = (Pointer) structure.getFieldValue("rna_path");
    FileBlockHeader dataFileBlock = blenderContext.getFileBlock(pRnaPath.getOldMemoryAddress());
    bis.setPosition(dataFileBlock.getBlockPosition());
    String rnaPath = bis.readString();
    bis.setPosition(currentPosition);
    int arrayIndex = ((Number) structure.getFieldValue("array_index")).intValue();
    // determining the curve type
    if (rnaPath.endsWith("location")) {
        return Ipo.AC_LOC_X + arrayIndex;
    }
    if (rnaPath.endsWith("rotation_quaternion")) {
        return Ipo.AC_QUAT_W + arrayIndex;
    }
    if (rnaPath.endsWith("scale")) {
        return Ipo.AC_SIZE_X + arrayIndex;
    }
    if (rnaPath.endsWith("rotation") || rnaPath.endsWith("rotation_euler")) {
        return Ipo.OB_ROT_X + arrayIndex;
    }
    LOGGER.log(Level.WARNING, "Unknown curve rna path: {0}", rnaPath);
    return -1;
}
Also used : FileBlockHeader(com.jme3.scene.plugins.blender.file.FileBlockHeader) BlenderInputStream(com.jme3.scene.plugins.blender.file.BlenderInputStream) Pointer(com.jme3.scene.plugins.blender.file.Pointer)

Example 2 with BlenderInputStream

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

the class CurvesTemporalMesh method loadNurbSurface.

/**
     * This method loads the NURBS curve or surface.
     * @param nurb
     *            the NURBS data structure
     * @throws BlenderFileException
     *             an exception is thrown when problems with reading occur
     */
@SuppressWarnings("unchecked")
private void loadNurbSurface(Structure nurb, int materialIndex) throws BlenderFileException {
    // loading the knots
    List<Float>[] knots = new List[2];
    Pointer[] pKnots = new Pointer[] { (Pointer) nurb.getFieldValue("knotsu"), (Pointer) nurb.getFieldValue("knotsv") };
    for (int i = 0; i < knots.length; ++i) {
        if (pKnots[i].isNotNull()) {
            FileBlockHeader fileBlockHeader = blenderContext.getFileBlock(pKnots[i].getOldMemoryAddress());
            BlenderInputStream blenderInputStream = blenderContext.getInputStream();
            blenderInputStream.setPosition(fileBlockHeader.getBlockPosition());
            int knotsAmount = fileBlockHeader.getCount() * fileBlockHeader.getSize() / 4;
            knots[i] = new ArrayList<Float>(knotsAmount);
            for (int j = 0; j < knotsAmount; ++j) {
                knots[i].add(Float.valueOf(blenderInputStream.readFloat()));
            }
        }
    }
    // loading the flags and orders (basis functions degrees)
    int flag = ((Number) nurb.getFieldValue("flag")).intValue();
    boolean smooth = (flag & FLAG_SMOOTH) != 0;
    int flagU = ((Number) nurb.getFieldValue("flagu")).intValue();
    int flagV = ((Number) nurb.getFieldValue("flagv")).intValue();
    int orderU = ((Number) nurb.getFieldValue("orderu")).intValue();
    int orderV = ((Number) nurb.getFieldValue("orderv")).intValue();
    // loading control points and their weights
    int pntsU = ((Number) nurb.getFieldValue("pntsu")).intValue();
    int pntsV = ((Number) nurb.getFieldValue("pntsv")).intValue();
    List<Structure> bPoints = ((Pointer) nurb.getFieldValue("bp")).fetchData();
    List<List<Vector4f>> controlPoints = new ArrayList<List<Vector4f>>(pntsV);
    for (int i = 0; i < pntsV; ++i) {
        List<Vector4f> uControlPoints = new ArrayList<Vector4f>(pntsU);
        for (int j = 0; j < pntsU; ++j) {
            DynamicArray<Float> vec = (DynamicArray<Float>) bPoints.get(j + i * pntsU).getFieldValue("vec");
            if (blenderContext.getBlenderKey().isFixUpAxis()) {
                uControlPoints.add(new Vector4f(vec.get(0).floatValue(), vec.get(2).floatValue(), -vec.get(1).floatValue(), vec.get(3).floatValue()));
            } else {
                uControlPoints.add(new Vector4f(vec.get(0).floatValue(), vec.get(1).floatValue(), vec.get(2).floatValue(), vec.get(3).floatValue()));
            }
        }
        if ((flagU & 0x01) != 0) {
            for (int k = 0; k < orderU - 1; ++k) {
                uControlPoints.add(uControlPoints.get(k));
            }
        }
        controlPoints.add(uControlPoints);
    }
    if ((flagV & 0x01) != 0) {
        for (int k = 0; k < orderV - 1; ++k) {
            controlPoints.add(controlPoints.get(k));
        }
    }
    int originalVerticesAmount = vertices.size();
    int resolu = ((Number) nurb.getFieldValue("resolu")).intValue();
    if (knots[1] == null) {
        // creating the NURB curve
        Curve curve = new Curve(new Spline(controlPoints.get(0), knots[0]), resolu);
        FloatBuffer vertsBuffer = (FloatBuffer) curve.getBuffer(Type.Position).getData();
        beziers.add(new BezierLine(BufferUtils.getVector3Array(vertsBuffer), materialIndex, smooth, false));
    } else {
        // creating the NURB surface
        int resolv = ((Number) nurb.getFieldValue("resolv")).intValue();
        int uSegments = resolu * controlPoints.get(0).size() - 1;
        int vSegments = resolv * controlPoints.size() - 1;
        Surface nurbSurface = Surface.createNurbsSurface(controlPoints, knots, uSegments, vSegments, orderU, orderV, smooth);
        FloatBuffer vertsBuffer = (FloatBuffer) nurbSurface.getBuffer(Type.Position).getData();
        vertices.addAll(Arrays.asList(BufferUtils.getVector3Array(vertsBuffer)));
        FloatBuffer normalsBuffer = (FloatBuffer) nurbSurface.getBuffer(Type.Normal).getData();
        normals.addAll(Arrays.asList(BufferUtils.getVector3Array(normalsBuffer)));
        IndexBuffer indexBuffer = nurbSurface.getIndexBuffer();
        for (int i = 0; i < indexBuffer.size(); i += 3) {
            int index1 = indexBuffer.get(i) + originalVerticesAmount;
            int index2 = indexBuffer.get(i + 1) + originalVerticesAmount;
            int index3 = indexBuffer.get(i + 2) + originalVerticesAmount;
            faces.add(new Face(new Integer[] { index1, index2, index3 }, smooth, materialIndex, null, null, this));
        }
    }
}
Also used : ArrayList(java.util.ArrayList) Pointer(com.jme3.scene.plugins.blender.file.Pointer) FloatBuffer(java.nio.FloatBuffer) Spline(com.jme3.math.Spline) Surface(com.jme3.scene.shape.Surface) IndexBuffer(com.jme3.scene.mesh.IndexBuffer) Vector4f(com.jme3.math.Vector4f) ArrayList(java.util.ArrayList) List(java.util.List) BlenderInputStream(com.jme3.scene.plugins.blender.file.BlenderInputStream) Structure(com.jme3.scene.plugins.blender.file.Structure) Face(com.jme3.scene.plugins.blender.meshes.Face) FileBlockHeader(com.jme3.scene.plugins.blender.file.FileBlockHeader) Curve(com.jme3.scene.shape.Curve) DynamicArray(com.jme3.scene.plugins.blender.file.DynamicArray)

Example 3 with BlenderInputStream

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

the class Field method fill.

/**
     * This method fills the field wth data read from the input stream.
     * @param blenderInputStream
     *            the stream we read data from
     * @throws BlenderFileException
     *             an exception is thrown when the blend file is somehow invalid or corrupted
     */
public void fill(BlenderInputStream blenderInputStream) throws BlenderFileException {
    int dataToRead = 1;
    if (tableSizes != null && tableSizes.length > 0) {
        for (int size : tableSizes) {
            if (size <= 0) {
                throw new BlenderFileException("The field " + name + " has invalid table size: " + size);
            }
            dataToRead *= size;
        }
    }
    DataType dataType = pointerLevel == 0 ? DataType.getDataType(type, blenderContext) : DataType.POINTER;
    switch(dataType) {
        case POINTER:
            if (dataToRead == 1) {
                Pointer pointer = new Pointer(pointerLevel, function, blenderContext);
                pointer.fill(blenderInputStream);
                value = pointer;
            } else {
                Pointer[] data = new Pointer[dataToRead];
                for (int i = 0; i < dataToRead; ++i) {
                    Pointer pointer = new Pointer(pointerLevel, function, blenderContext);
                    pointer.fill(blenderInputStream);
                    data[i] = pointer;
                }
                value = new DynamicArray<Pointer>(tableSizes, data);
            }
            break;
        case CHARACTER:
            // and characters are very often used as byte number stores instead of real chars
            if (dataToRead == 1) {
                value = Byte.valueOf((byte) blenderInputStream.readByte());
            } else {
                Character[] data = new Character[dataToRead];
                for (int i = 0; i < dataToRead; ++i) {
                    data[i] = Character.valueOf((char) blenderInputStream.readByte());
                }
                value = new DynamicArray<Character>(tableSizes, data);
            }
            break;
        case SHORT:
            if (dataToRead == 1) {
                value = Integer.valueOf(blenderInputStream.readShort());
            } else {
                Number[] data = new Number[dataToRead];
                for (int i = 0; i < dataToRead; ++i) {
                    data[i] = Integer.valueOf(blenderInputStream.readShort());
                }
                value = new DynamicArray<Number>(tableSizes, data);
            }
            break;
        case INTEGER:
            if (dataToRead == 1) {
                value = Integer.valueOf(blenderInputStream.readInt());
            } else {
                Number[] data = new Number[dataToRead];
                for (int i = 0; i < dataToRead; ++i) {
                    data[i] = Integer.valueOf(blenderInputStream.readInt());
                }
                value = new DynamicArray<Number>(tableSizes, data);
            }
            break;
        case LONG:
            if (dataToRead == 1) {
                value = Long.valueOf(blenderInputStream.readLong());
            } else {
                Number[] data = new Number[dataToRead];
                for (int i = 0; i < dataToRead; ++i) {
                    data[i] = Long.valueOf(blenderInputStream.readLong());
                }
                value = new DynamicArray<Number>(tableSizes, data);
            }
            break;
        case FLOAT:
            if (dataToRead == 1) {
                value = Float.valueOf(blenderInputStream.readFloat());
            } else {
                Number[] data = new Number[dataToRead];
                for (int i = 0; i < dataToRead; ++i) {
                    data[i] = Float.valueOf(blenderInputStream.readFloat());
                }
                value = new DynamicArray<Number>(tableSizes, data);
            }
            break;
        case DOUBLE:
            if (dataToRead == 1) {
                value = Double.valueOf(blenderInputStream.readDouble());
            } else {
                Number[] data = new Number[dataToRead];
                for (int i = 0; i < dataToRead; ++i) {
                    data[i] = Double.valueOf(blenderInputStream.readDouble());
                }
                value = new DynamicArray<Number>(tableSizes, data);
            }
            break;
        case VOID:
            break;
        case STRUCTURE:
            if (dataToRead == 1) {
                Structure structure = blenderContext.getDnaBlockData().getStructure(type);
                structure.fill(blenderContext.getInputStream());
                value = structure;
            } else {
                Structure[] data = new Structure[dataToRead];
                for (int i = 0; i < dataToRead; ++i) {
                    Structure structure = blenderContext.getDnaBlockData().getStructure(type);
                    structure.fill(blenderContext.getInputStream());
                    data[i] = structure;
                }
                value = new DynamicArray<Structure>(tableSizes, data);
            }
            break;
        default:
            throw new IllegalStateException("Unimplemented filling of type: " + type);
    }
}
Also used : DataType(com.jme3.scene.plugins.blender.file.Structure.DataType)

Example 4 with BlenderInputStream

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

the class Properties method load.

/**
     * This method loads the property from the belnder file.
     * @param idPropertyStructure
     *            the ID structure constining the property
     * @param blenderContext
     *            the blender context
     * @throws BlenderFileException
     *             an exception is thrown when the belnder file is somehow invalid
     */
public void load(Structure idPropertyStructure, BlenderContext blenderContext) throws BlenderFileException {
    name = idPropertyStructure.getFieldValue("name").toString();
    if (name == null || name.length() == 0) {
        name = DEFAULT_NAME;
    }
    subType = ((Number) idPropertyStructure.getFieldValue("subtype")).intValue();
    type = ((Number) idPropertyStructure.getFieldValue("type")).intValue();
    // reading the data
    Structure data = (Structure) idPropertyStructure.getFieldValue("data");
    int len = ((Number) idPropertyStructure.getFieldValue("len")).intValue();
    switch(type) {
        case IDP_STRING:
            {
                Pointer pointer = (Pointer) data.getFieldValue("pointer");
                BlenderInputStream bis = blenderContext.getInputStream();
                FileBlockHeader dataFileBlock = blenderContext.getFileBlock(pointer.getOldMemoryAddress());
                bis.setPosition(dataFileBlock.getBlockPosition());
                value = bis.readString();
                break;
            }
        case IDP_INT:
            int intValue = ((Number) data.getFieldValue("val")).intValue();
            value = Integer.valueOf(intValue);
            break;
        case IDP_FLOAT:
            int floatValue = ((Number) data.getFieldValue("val")).intValue();
            value = Float.valueOf(Float.intBitsToFloat(floatValue));
            break;
        case IDP_ARRAY:
            {
                Pointer pointer = (Pointer) data.getFieldValue("pointer");
                BlenderInputStream bis = blenderContext.getInputStream();
                FileBlockHeader dataFileBlock = blenderContext.getFileBlock(pointer.getOldMemoryAddress());
                bis.setPosition(dataFileBlock.getBlockPosition());
                int elementAmount = dataFileBlock.getSize();
                switch(subType) {
                    case IDP_INT:
                        elementAmount /= 4;
                        int[] intList = new int[elementAmount];
                        for (int i = 0; i < elementAmount; ++i) {
                            intList[i] = bis.readInt();
                        }
                        value = intList;
                        break;
                    case IDP_FLOAT:
                        elementAmount /= 4;
                        float[] floatList = new float[elementAmount];
                        for (int i = 0; i < elementAmount; ++i) {
                            floatList[i] = bis.readFloat();
                        }
                        value = floatList;
                        break;
                    case IDP_DOUBLE:
                        elementAmount /= 8;
                        double[] doubleList = new double[elementAmount];
                        for (int i = 0; i < elementAmount; ++i) {
                            doubleList[i] = bis.readDouble();
                        }
                        value = doubleList;
                        break;
                    default:
                        throw new IllegalStateException("Invalid array subtype: " + subType);
                }
            }
        case IDP_GROUP:
            Structure group = (Structure) data.getFieldValue("group");
            List<Structure> dataList = group.evaluateListBase();
            List<Properties> subProperties = new ArrayList<Properties>(len);
            for (Structure d : dataList) {
                Properties properties = new Properties();
                properties.load(d, blenderContext);
                subProperties.add(properties);
            }
            value = subProperties;
            break;
        case IDP_DOUBLE:
            int doublePart1 = ((Number) data.getFieldValue("val")).intValue();
            int doublePart2 = ((Number) data.getFieldValue("val2")).intValue();
            long doubleVal = (long) doublePart2 << 32 | doublePart1;
            value = Double.valueOf(Double.longBitsToDouble(doubleVal));
            break;
        case IDP_IDPARRAY:
            {
                Pointer pointer = (Pointer) data.getFieldValue("pointer");
                List<Structure> arrays = pointer.fetchData();
                List<Object> result = new ArrayList<Object>(arrays.size());
                Properties temp = new Properties();
                for (Structure array : arrays) {
                    temp.load(array, blenderContext);
                    result.add(temp.value);
                }
                value = result;
                break;
            }
        case IDP_NUMTYPES:
            throw new UnsupportedOperationException();
        // return null;
        default:
            throw new IllegalStateException("Unknown custom property type: " + type);
    }
    this.completeLoading();
}
Also used : FileBlockHeader(com.jme3.scene.plugins.blender.file.FileBlockHeader) ArrayList(java.util.ArrayList) Pointer(com.jme3.scene.plugins.blender.file.Pointer) BlenderInputStream(com.jme3.scene.plugins.blender.file.BlenderInputStream) List(java.util.List) ArrayList(java.util.ArrayList) Structure(com.jme3.scene.plugins.blender.file.Structure)

Example 5 with BlenderInputStream

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

the class ImageLoader method loadImage.

/**
     * This method loads the image from the blender file itself. It tries each loader to load the image.
     * 
     * @param inputStream
     *            blender input stream
     * @param startPosition
     *            position in the stream where the image data starts
     * @param flipY
     *            if the image should be flipped (does not work with DirectX image)
     * @return loaded image or null if it could not be loaded
     */
public Image loadImage(BlenderInputStream inputStream, int startPosition, boolean flipY) {
    // loading using AWT loader
    inputStream.setPosition(startPosition);
    Image result = this.loadImage(inputStream, ImageType.AWT, flipY);
    // loading using TGA loader
    if (result == null) {
        inputStream.setPosition(startPosition);
        result = this.loadImage(inputStream, ImageType.TGA, flipY);
    }
    // loading using DDS loader
    if (result == null) {
        inputStream.setPosition(startPosition);
        result = this.loadImage(inputStream, ImageType.DDS, flipY);
    }
    if (result == null) {
        LOGGER.warning("Image could not be loaded by none of available loaders!");
    }
    return result;
}
Also used : Image(com.jme3.texture.Image)

Aggregations

BlenderInputStream (com.jme3.scene.plugins.blender.file.BlenderInputStream)4 FileBlockHeader (com.jme3.scene.plugins.blender.file.FileBlockHeader)4 Pointer (com.jme3.scene.plugins.blender.file.Pointer)3 ArrayList (java.util.ArrayList)3 Structure (com.jme3.scene.plugins.blender.file.Structure)2 List (java.util.List)2 BlenderKey (com.jme3.asset.BlenderKey)1 ModelKey (com.jme3.asset.ModelKey)1 Spline (com.jme3.math.Spline)1 Vector4f (com.jme3.math.Vector4f)1 IndexBuffer (com.jme3.scene.mesh.IndexBuffer)1 AnimationHelper (com.jme3.scene.plugins.blender.animations.AnimationHelper)1 CameraHelper (com.jme3.scene.plugins.blender.cameras.CameraHelper)1 ConstraintHelper (com.jme3.scene.plugins.blender.constraints.ConstraintHelper)1 CurvesHelper (com.jme3.scene.plugins.blender.curves.CurvesHelper)1 DynamicArray (com.jme3.scene.plugins.blender.file.DynamicArray)1 DataType (com.jme3.scene.plugins.blender.file.Structure.DataType)1 LandscapeHelper (com.jme3.scene.plugins.blender.landscape.LandscapeHelper)1 LightHelper (com.jme3.scene.plugins.blender.lights.LightHelper)1 MaterialHelper (com.jme3.scene.plugins.blender.materials.MaterialHelper)1