Search in sources :

Example 16 with Pointer

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

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

the class ConstraintHelper method loadConstraints.

/**
     * This method reads constraints for for the given structure. The
     * constraints are loaded only once for object/bone.
     * 
     * @param objectStructure
     *            the structure we read constraint's for
     * @param blenderContext
     *            the blender context
     * @throws BlenderFileException
     */
public void loadConstraints(Structure objectStructure, BlenderContext blenderContext) throws BlenderFileException {
    LOGGER.fine("Loading constraints.");
    // reading influence ipos for the constraints
    AnimationHelper animationHelper = blenderContext.getHelper(AnimationHelper.class);
    Map<String, Map<String, Ipo>> constraintsIpos = new HashMap<String, Map<String, Ipo>>();
    Pointer pActions = (Pointer) objectStructure.getFieldValue("action");
    if (pActions.isNotNull()) {
        List<Structure> actions = pActions.fetchData();
        for (Structure action : actions) {
            Structure chanbase = (Structure) action.getFieldValue("chanbase");
            List<Structure> actionChannels = chanbase.evaluateListBase();
            for (Structure actionChannel : actionChannels) {
                Map<String, Ipo> ipos = new HashMap<String, Ipo>();
                Structure constChannels = (Structure) actionChannel.getFieldValue("constraintChannels");
                List<Structure> constraintChannels = constChannels.evaluateListBase();
                for (Structure constraintChannel : constraintChannels) {
                    Pointer pIpo = (Pointer) constraintChannel.getFieldValue("ipo");
                    if (pIpo.isNotNull()) {
                        String constraintName = constraintChannel.getFieldValue("name").toString();
                        Ipo ipo = animationHelper.fromIpoStructure(pIpo.fetchData().get(0), blenderContext);
                        ipos.put(constraintName, ipo);
                    }
                }
                String actionName = actionChannel.getFieldValue("name").toString();
                constraintsIpos.put(actionName, ipos);
            }
        }
    }
    // loading constraints connected with the object's bones
    Pointer pPose = (Pointer) objectStructure.getFieldValue("pose");
    if (pPose.isNotNull()) {
        List<Structure> poseChannels = ((Structure) pPose.fetchData().get(0).getFieldValue("chanbase")).evaluateListBase();
        for (Structure poseChannel : poseChannels) {
            List<Constraint> constraintsList = new ArrayList<Constraint>();
            Long boneOMA = Long.valueOf(((Pointer) poseChannel.getFieldValue("bone")).getOldMemoryAddress());
            // the name is read directly from structure because bone might
            // not yet be loaded
            String name = blenderContext.getFileBlock(boneOMA).getStructure(blenderContext).getFieldValue("name").toString();
            List<Structure> constraints = ((Structure) poseChannel.getFieldValue("constraints")).evaluateListBase();
            for (Structure constraint : constraints) {
                String constraintName = constraint.getFieldValue("name").toString();
                Map<String, Ipo> ipoMap = constraintsIpos.get(name);
                Ipo ipo = ipoMap == null ? null : ipoMap.get(constraintName);
                if (ipo == null) {
                    float enforce = ((Number) constraint.getFieldValue("enforce")).floatValue();
                    ipo = animationHelper.fromValue(enforce);
                }
                constraintsList.add(new BoneConstraint(constraint, boneOMA, ipo, blenderContext));
            }
            blenderContext.addConstraints(boneOMA, constraintsList);
        }
    }
    // loading constraints connected with the object itself
    List<Structure> constraints = ((Structure) objectStructure.getFieldValue("constraints")).evaluateListBase();
    if (constraints != null && constraints.size() > 0) {
        Pointer pData = (Pointer) objectStructure.getFieldValue("data");
        String dataType = pData.isNotNull() ? pData.fetchData().get(0).getType() : null;
        List<Constraint> constraintsList = new ArrayList<Constraint>(constraints.size());
        for (Structure constraint : constraints) {
            String constraintName = constraint.getFieldValue("name").toString();
            String objectName = objectStructure.getName();
            Map<String, Ipo> objectConstraintsIpos = constraintsIpos.get(objectName);
            Ipo ipo = objectConstraintsIpos != null ? objectConstraintsIpos.get(constraintName) : null;
            if (ipo == null) {
                float enforce = ((Number) constraint.getFieldValue("enforce")).floatValue();
                ipo = animationHelper.fromValue(enforce);
            }
            constraintsList.add(this.createConstraint(dataType, constraint, objectStructure.getOldMemoryAddress(), ipo, blenderContext));
        }
        blenderContext.addConstraints(objectStructure.getOldMemoryAddress(), constraintsList);
    }
}
Also used : AnimationHelper(com.jme3.scene.plugins.blender.animations.AnimationHelper) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Ipo(com.jme3.scene.plugins.blender.animations.Ipo) Pointer(com.jme3.scene.plugins.blender.file.Pointer) Structure(com.jme3.scene.plugins.blender.file.Structure) HashMap(java.util.HashMap) Map(java.util.Map)

Example 18 with Pointer

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

the class AbstractBlenderHelper method loadProperties.

/**
     * This method loads the properties if they are available and defined for the structure.
     * @param structure
     *            the structure we read the properties from
     * @param blenderContext
     *            the blender context
     * @return loaded properties or null if they are not available
     * @throws BlenderFileException
     *             an exception is thrown when the blend file is somehow corrupted
     */
protected Properties loadProperties(Structure structure, BlenderContext blenderContext) throws BlenderFileException {
    Properties properties = null;
    Structure id = (Structure) structure.getFieldValue("ID");
    if (id != null) {
        Pointer pProperties = (Pointer) id.getFieldValue("properties");
        if (pProperties.isNotNull()) {
            Structure propertiesStructure = pProperties.fetchData().get(0);
            properties = new Properties();
            properties.load(propertiesStructure, blenderContext);
        }
    }
    return properties;
}
Also used : Pointer(com.jme3.scene.plugins.blender.file.Pointer) Properties(com.jme3.scene.plugins.blender.objects.Properties) Structure(com.jme3.scene.plugins.blender.file.Structure)

Example 19 with Pointer

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

the class AbstractBlenderHelper method loadLibrary.

/**
     * The method loads library of a given ID from linked blender file.
     * @param id
     *            the ID of the linked feature (it contains its name and blender path)
     * @return loaded feature or null if none was found
     * @throws BlenderFileException
     *             and exception is throw when problems with reading a blend file occur
     */
protected Object loadLibrary(Structure id) throws BlenderFileException {
    Pointer pLib = (Pointer) id.getFieldValue("lib");
    if (pLib.isNotNull()) {
        // we need full name with the prefix
        String fullName = id.getFieldValue("name").toString();
        String nameOfFeatureToLoad = id.getName();
        Structure library = pLib.fetchData().get(0);
        String path = library.getFieldValue("filepath").toString();
        if (!blenderContext.getLinkedFeatures().keySet().contains(path)) {
            Spatial loadedAsset = null;
            BlenderKey blenderKey = new BlenderKey(path);
            blenderKey.setLoadUnlinkedAssets(true);
            try {
                loadedAsset = blenderContext.getAssetManager().loadAsset(blenderKey);
            } catch (AssetNotFoundException e) {
                LOGGER.log(Level.FINEST, "Cannot locate linked resource at path: {0}.", path);
            }
            if (loadedAsset != null) {
                Map<String, Map<String, Object>> linkedData = loadedAsset.getUserData("linkedData");
                for (Entry<String, Map<String, Object>> entry : linkedData.entrySet()) {
                    String linkedDataFilePath = "this".equals(entry.getKey()) ? path : entry.getKey();
                    blenderContext.getLinkedFeatures().put(linkedDataFilePath, entry.getValue());
                }
            } else {
                LOGGER.log(Level.WARNING, "No features loaded from path: {0}.", path);
            }
        }
        Object result = blenderContext.getLinkedFeature(path, fullName);
        if (result == null) {
            LOGGER.log(Level.WARNING, "Could NOT find asset named {0} in the library of path: {1}.", new Object[] { nameOfFeatureToLoad, path });
        } else {
            blenderContext.addLoadedFeatures(id.getOldMemoryAddress(), LoadedDataType.STRUCTURE, id);
            blenderContext.addLoadedFeatures(id.getOldMemoryAddress(), LoadedDataType.FEATURE, result);
        }
        return result;
    } else {
        LOGGER.warning("Library link points to nothing!");
    }
    return null;
}
Also used : Spatial(com.jme3.scene.Spatial) Pointer(com.jme3.scene.plugins.blender.file.Pointer) BlenderKey(com.jme3.asset.BlenderKey) AssetNotFoundException(com.jme3.asset.AssetNotFoundException) Structure(com.jme3.scene.plugins.blender.file.Structure) Map(java.util.Map)

Example 20 with Pointer

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

the class AndroidTouchInput method generateMouseEvent.

// TODO: Ring Buffer for mouse events?
public InputEvent generateMouseEvent(TouchEvent event) {
    InputEvent inputEvent = null;
    int newX;
    int newY;
    int newDX;
    int newDY;
    // MouseEvents do not support multi-touch, so only evaluate 1 finger pointer events
    if (!isSimulateMouse() || numPointers > 1) {
        return null;
    }
    if (isMouseEventsInvertX()) {
        newX = (int) (invertX(event.getX()));
        newDX = (int) event.getDeltaX() * -1;
    } else {
        newX = (int) event.getX();
        newDX = (int) event.getDeltaX();
    }
    if (isMouseEventsInvertY()) {
        newY = (int) (invertY(event.getY()));
        newDY = (int) event.getDeltaY() * -1;
    } else {
        newY = (int) event.getY();
        newDY = (int) event.getDeltaY();
    }
    switch(event.getType()) {
        case DOWN:
            // Handle mouse down event
            inputEvent = new MouseButtonEvent(0, true, newX, newY);
            inputEvent.setTime(event.getTime());
            break;
        case UP:
            // Handle mouse up event
            inputEvent = new MouseButtonEvent(0, false, newX, newY);
            inputEvent.setTime(event.getTime());
            break;
        case HOVER_MOVE:
        case MOVE:
            inputEvent = new MouseMotionEvent(newX, newY, newDX, newDY, (int) event.getScaleSpan(), (int) event.getDeltaScaleSpan());
            inputEvent.setTime(event.getTime());
            break;
    }
    return inputEvent;
}
Also used : MouseMotionEvent(com.jme3.input.event.MouseMotionEvent) InputEvent(com.jme3.input.event.InputEvent) KeyInputEvent(com.jme3.input.event.KeyInputEvent) MouseButtonEvent(com.jme3.input.event.MouseButtonEvent)

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