Search in sources :

Example 6 with FileBlockHeader

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

the class TextureHelper method loadImageAsTexture.

/**
     * This class returns a texture read from the file or from packed blender
     * data.
     * 
     * @param imageStructure
     *            image structure filled with data
     * @param imaflag
     *            the image flag
     * @param blenderContext
     *            the blender context
     * @return the texture that can be used by JME engine
     * @throws BlenderFileException
     *             this exception is thrown when the blend file structure is
     *             somehow invalid or corrupted
     */
public Texture loadImageAsTexture(Structure imageStructure, int imaflag, BlenderContext blenderContext) throws BlenderFileException {
    LOGGER.log(Level.FINE, "Fetching texture with OMA = {0}", imageStructure.getOldMemoryAddress());
    Texture result = null;
    Image im = (Image) blenderContext.getLoadedFeature(imageStructure.getOldMemoryAddress(), LoadedDataType.FEATURE);
    // if (im == null) {  HACK force reaload always, as constructor in else case is destroying the TextureKeys!
    if ("ID".equals(imageStructure.getType())) {
        LOGGER.fine("Loading texture from external blend file.");
        result = (Texture) this.loadLibrary(imageStructure);
    } else {
        String texturePath = imageStructure.getFieldValue("name").toString();
        Pointer pPackedFile = (Pointer) imageStructure.getFieldValue("packedfile");
        if (pPackedFile.isNull()) {
            LOGGER.log(Level.FINE, "Reading texture from file: {0}", texturePath);
            result = this.loadImageFromFile(texturePath, imaflag, blenderContext);
        } else {
            LOGGER.fine("Packed texture. Reading directly from the blend file!");
            Structure packedFile = pPackedFile.fetchData().get(0);
            Pointer pData = (Pointer) packedFile.getFieldValue("data");
            FileBlockHeader dataFileBlock = blenderContext.getFileBlock(pData.getOldMemoryAddress());
            blenderContext.getInputStream().setPosition(dataFileBlock.getBlockPosition());
            // Should the texture be flipped? It works for sinbad ..
            result = new Texture2D(new ImageLoader().loadImage(blenderContext.getInputStream(), dataFileBlock.getBlockPosition(), true));
        }
    }
    if (result != null) {
        // render result is not being loaded
        blenderContext.addLoadedFeatures(imageStructure.getOldMemoryAddress(), LoadedDataType.STRUCTURE, imageStructure);
        blenderContext.addLoadedFeatures(imageStructure.getOldMemoryAddress(), LoadedDataType.FEATURE, result.getImage());
        result.setName(imageStructure.getName());
    }
    return result;
}
Also used : Texture2D(com.jme3.texture.Texture2D) FileBlockHeader(com.jme3.scene.plugins.blender.file.FileBlockHeader) Pointer(com.jme3.scene.plugins.blender.file.Pointer) Image(com.jme3.texture.Image) Structure(com.jme3.scene.plugins.blender.file.Structure) Texture(com.jme3.texture.Texture)

Example 7 with FileBlockHeader

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

the class BlenderLoader method setup.

/**
     * This method sets up the loader.
     * @param assetInfo
     *            the asset info
     * @throws BlenderFileException
     *             an exception is throw when something wrong happens with blender file
     */
protected BlenderContext setup(AssetInfo assetInfo) throws BlenderFileException {
    // registering loaders
    ModelKey modelKey = (ModelKey) assetInfo.getKey();
    BlenderKey blenderKey;
    if (modelKey instanceof BlenderKey) {
        blenderKey = (BlenderKey) modelKey;
    } else {
        blenderKey = new BlenderKey(modelKey.getName());
    }
    // opening stream
    BlenderInputStream inputStream = new BlenderInputStream(assetInfo.openStream());
    // reading blocks
    List<FileBlockHeader> blocks = new ArrayList<FileBlockHeader>();
    FileBlockHeader fileBlock;
    BlenderContext blenderContext = new BlenderContext();
    blenderContext.setBlenderVersion(inputStream.getVersionNumber());
    blenderContext.setAssetManager(assetInfo.getManager());
    blenderContext.setInputStream(inputStream);
    blenderContext.setBlenderKey(blenderKey);
    // creating helpers
    blenderContext.putHelper(AnimationHelper.class, new AnimationHelper(inputStream.getVersionNumber(), blenderContext));
    blenderContext.putHelper(TextureHelper.class, new TextureHelper(inputStream.getVersionNumber(), blenderContext));
    blenderContext.putHelper(MeshHelper.class, new MeshHelper(inputStream.getVersionNumber(), blenderContext));
    blenderContext.putHelper(ObjectHelper.class, new ObjectHelper(inputStream.getVersionNumber(), blenderContext));
    blenderContext.putHelper(CurvesHelper.class, new CurvesHelper(inputStream.getVersionNumber(), blenderContext));
    blenderContext.putHelper(LightHelper.class, new LightHelper(inputStream.getVersionNumber(), blenderContext));
    blenderContext.putHelper(CameraHelper.class, new CameraHelper(inputStream.getVersionNumber(), blenderContext));
    blenderContext.putHelper(ModifierHelper.class, new ModifierHelper(inputStream.getVersionNumber(), blenderContext));
    blenderContext.putHelper(MaterialHelper.class, new MaterialHelper(inputStream.getVersionNumber(), blenderContext));
    blenderContext.putHelper(ConstraintHelper.class, new ConstraintHelper(inputStream.getVersionNumber(), blenderContext));
    blenderContext.putHelper(ParticlesHelper.class, new ParticlesHelper(inputStream.getVersionNumber(), blenderContext));
    blenderContext.putHelper(LandscapeHelper.class, new LandscapeHelper(inputStream.getVersionNumber(), blenderContext));
    // reading the blocks (dna block is automatically saved in the blender context when found)
    FileBlockHeader sceneFileBlock = null;
    do {
        fileBlock = new FileBlockHeader(inputStream, blenderContext);
        if (!fileBlock.isDnaBlock()) {
            blocks.add(fileBlock);
            // save the scene's file block
            if (fileBlock.getCode() == BlockCode.BLOCK_SC00) {
                sceneFileBlock = fileBlock;
            }
        }
    } while (!fileBlock.isLastBlock());
    if (sceneFileBlock != null) {
        blenderContext.setSceneStructure(sceneFileBlock.getStructure(blenderContext));
    }
    // adding locator for linked content
    assetInfo.getManager().registerLocator(assetInfo.getKey().getName(), LinkedContentLocator.class);
    return blenderContext;
}
Also used : ObjectHelper(com.jme3.scene.plugins.blender.objects.ObjectHelper) AnimationHelper(com.jme3.scene.plugins.blender.animations.AnimationHelper) ModelKey(com.jme3.asset.ModelKey) FileBlockHeader(com.jme3.scene.plugins.blender.file.FileBlockHeader) ArrayList(java.util.ArrayList) ConstraintHelper(com.jme3.scene.plugins.blender.constraints.ConstraintHelper) ModifierHelper(com.jme3.scene.plugins.blender.modifiers.ModifierHelper) LandscapeHelper(com.jme3.scene.plugins.blender.landscape.LandscapeHelper) CurvesHelper(com.jme3.scene.plugins.blender.curves.CurvesHelper) TextureHelper(com.jme3.scene.plugins.blender.textures.TextureHelper) MaterialHelper(com.jme3.scene.plugins.blender.materials.MaterialHelper) BlenderKey(com.jme3.asset.BlenderKey) BlenderInputStream(com.jme3.scene.plugins.blender.file.BlenderInputStream) ParticlesHelper(com.jme3.scene.plugins.blender.particles.ParticlesHelper) LightHelper(com.jme3.scene.plugins.blender.lights.LightHelper) MeshHelper(com.jme3.scene.plugins.blender.meshes.MeshHelper) CameraHelper(com.jme3.scene.plugins.blender.cameras.CameraHelper)

Example 8 with FileBlockHeader

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

the class AnimationHelper method loadAnimations.

/**
     * Loads all animations that are stored in the blender file. The animations are not yet applied to the scene features.
     * This should be called before objects are loaded.
     * @throws BlenderFileException
     *             an exception is thrown when problems with blender file reading occur
     */
public void loadAnimations() throws BlenderFileException {
    LOGGER.info("Loading animations that will be later applied to scene features.");
    List<FileBlockHeader> actionHeaders = blenderContext.getFileBlocks(BlockCode.BLOCK_AC00);
    if (actionHeaders != null) {
        for (FileBlockHeader header : actionHeaders) {
            Structure actionStructure = header.getStructure(blenderContext);
            LOGGER.log(Level.INFO, "Found animation: {0}.", actionStructure.getName());
            blenderContext.addAction(this.getTracks(actionStructure, blenderContext));
        }
    }
}
Also used : FileBlockHeader(com.jme3.scene.plugins.blender.file.FileBlockHeader) Structure(com.jme3.scene.plugins.blender.file.Structure)

Aggregations

FileBlockHeader (com.jme3.scene.plugins.blender.file.FileBlockHeader)8 Structure (com.jme3.scene.plugins.blender.file.Structure)6 Pointer (com.jme3.scene.plugins.blender.file.Pointer)5 BlenderInputStream (com.jme3.scene.plugins.blender.file.BlenderInputStream)4 ArrayList (java.util.ArrayList)4 MeshHelper (com.jme3.scene.plugins.blender.meshes.MeshHelper)3 ObjectHelper (com.jme3.scene.plugins.blender.objects.ObjectHelper)3 BlenderKey (com.jme3.asset.BlenderKey)2 AnimationHelper (com.jme3.scene.plugins.blender.animations.AnimationHelper)2 ConstraintHelper (com.jme3.scene.plugins.blender.constraints.ConstraintHelper)2 BlenderFileException (com.jme3.scene.plugins.blender.file.BlenderFileException)2 LandscapeHelper (com.jme3.scene.plugins.blender.landscape.LandscapeHelper)2 MaterialHelper (com.jme3.scene.plugins.blender.materials.MaterialHelper)2 TemporalMesh (com.jme3.scene.plugins.blender.meshes.TemporalMesh)2 TextureHelper (com.jme3.scene.plugins.blender.textures.TextureHelper)2 Texture (com.jme3.texture.Texture)2 List (java.util.List)2 ModelKey (com.jme3.asset.ModelKey)1 BoundingBox (com.jme3.bounding.BoundingBox)1 BoundingSphere (com.jme3.bounding.BoundingSphere)1