159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta/*
259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * Copyright (c) 2009-2012 jMonkeyEngine
359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * All rights reserved.
459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *
559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * Redistribution and use in source and binary forms, with or without
659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * modification, are permitted provided that the following conditions are
759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * met:
859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *
959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * * Redistributions of source code must retain the above copyright
1059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *   notice, this list of conditions and the following disclaimer.
1159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *
1259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * * Redistributions in binary form must reproduce the above copyright
1359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *   notice, this list of conditions and the following disclaimer in the
1459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *   documentation and/or other materials provided with the distribution.
1559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *
1659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
1759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *   may be used to endorse or promote products derived from this software
1859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *   without specific prior written permission.
1959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *
2059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
2459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
2559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
2659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
2759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
2859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
2959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
3059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta */
3259b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartapackage com.jme3.scene.plugins.blender.modifiers;
3359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
3459b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport java.util.ArrayList;
3559b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport java.util.Collection;
3659b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport java.util.List;
3759b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport java.util.logging.Level;
3859b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport java.util.logging.Logger;
3959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
4059b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.scene.plugins.blender.AbstractBlenderHelper;
4159b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.scene.plugins.blender.BlenderContext;
4259b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.scene.plugins.blender.animations.Ipo;
4359b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.scene.plugins.blender.animations.IpoHelper;
4459b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.scene.plugins.blender.exceptions.BlenderFileException;
4559b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.scene.plugins.blender.file.Pointer;
4659b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.scene.plugins.blender.file.Structure;
4759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
4859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta/**
4959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * A class that is used in modifiers calculations.
5059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *
5159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * @author Marcin Roguski
5259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta */
5359b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartapublic class ModifierHelper extends AbstractBlenderHelper {
5459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
5559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta	private static final Logger	LOGGER	= Logger.getLogger(ModifierHelper.class.getName());
5659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
5759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta	/**
5859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta	 * This constructor parses the given blender version and stores the result.
5959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta	 * Some functionalities may differ in different blender versions.
6059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta	 *
6159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta	 * @param blenderVersion
6259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta	 *            the version read from the blend file
6359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta	 * @param fixUpAxis
6459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta	 *            a variable that indicates if the Y asxis is the UP axis or not
6559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta	 */
6659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta	public ModifierHelper(String blenderVersion, boolean fixUpAxis) {
6759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta		super(blenderVersion, fixUpAxis);
6859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta	}
6959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
7059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta	/**
7159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta	 * This method reads the given object's modifiers.
7259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta	 *
7359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta	 * @param objectStructure
7459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta	 *            the object structure
7559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta	 * @param blenderContext
7659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta	 *            the blender context
7759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta	 * @throws BlenderFileException
7859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta	 *             this exception is thrown when the blender file is somehow
7959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta	 *             corrupted
8059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta	 */
8159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta	public Collection<Modifier> readModifiers(Structure objectStructure, BlenderContext blenderContext) throws BlenderFileException {
8259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta		Collection<Modifier> result = new ArrayList<Modifier>();
8359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta		Structure modifiersListBase = (Structure) objectStructure.getFieldValue("modifiers");
8459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta		List<Structure> modifiers = modifiersListBase.evaluateListBase(blenderContext);
8559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta		for (Structure modifierStructure : modifiers) {
8659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta			Modifier modifier = null;
8759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta			if (Modifier.ARRAY_MODIFIER_DATA.equals(modifierStructure.getType())) {
8859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta				modifier = new ArrayModifier(modifierStructure, blenderContext);
8959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta			} else if (Modifier.MIRROR_MODIFIER_DATA.equals(modifierStructure.getType())) {
9059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta				modifier = new MirrorModifier(modifierStructure, blenderContext);
9159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta			} else if (Modifier.ARMATURE_MODIFIER_DATA.equals(modifierStructure.getType())) {
9259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta				modifier = new ArmatureModifier(objectStructure, modifierStructure, blenderContext);
9359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta			} else if (Modifier.PARTICLE_MODIFIER_DATA.equals(modifierStructure.getType())) {
9459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta				modifier = new ParticlesModifier(modifierStructure, blenderContext);
9559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta			}
9659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
9759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta			if (modifier != null) {
9859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta				result.add(modifier);
9959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta				blenderContext.addModifier(objectStructure.getOldMemoryAddress(), modifier);
10059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta			} else {
10159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta				LOGGER.log(Level.WARNING, "Unsupported modifier type: {0}", modifierStructure.getType());
10259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta			}
10359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta		}
10459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
10559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta		// at the end read object's animation modifier (object animation is
10659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta		// either described by action or by ipo of the object)
10759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta		Modifier modifier;
10859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta		if (blenderVersion <= 249) {
10959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta			modifier = this.readAnimationModifier249(objectStructure, blenderContext);
11059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta		} else {
11159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta			modifier = this.readAnimationModifier250(objectStructure, blenderContext);
11259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta		}
11359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta		if (modifier != null) {
11459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta			result.add(modifier);
11559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta		}
11659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta		return result;
11759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta	}
11859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
11959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta	@Override
12059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta	public boolean shouldBeLoaded(Structure structure, BlenderContext blenderContext) {
12159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta		return true;
12259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta	}
12359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
12459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta	/**
12559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta	 * This method reads the object's animation modifier for blender version
12659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta	 * 2.49 and lower.
12759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta	 *
12859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta	 * @param objectStructure
12959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta	 *            the object's structure
13059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta	 * @param blenderContext
13159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta	 *            the blender context
13259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta	 * @return loaded modifier
13359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta	 * @throws BlenderFileException
13459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta	 *             this exception is thrown when the blender file is somehow
13559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta	 *             corrupted
13659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta	 */
13759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta	private Modifier readAnimationModifier249(Structure objectStructure, BlenderContext blenderContext) throws BlenderFileException {
13859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta		Modifier result = null;
13959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta		Pointer pAction = (Pointer) objectStructure.getFieldValue("action");
14059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta		IpoHelper ipoHelper = blenderContext.getHelper(IpoHelper.class);
14159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta		if (pAction.isNotNull()) {
14259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta			Structure action = pAction.fetchData(blenderContext.getInputStream()).get(0);
14359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta			List<Structure> actionChannels = ((Structure) action.getFieldValue("chanbase")).evaluateListBase(blenderContext);
14459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta			if (actionChannels.size() == 1) {// object's animtion action has
14559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta												// only one channel
14659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta				Pointer pChannelIpo = (Pointer) actionChannels.get(0).getFieldValue("ipo");
14759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta				Structure ipoStructure = pChannelIpo.fetchData(blenderContext.getInputStream()).get(0);
14859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta				Ipo ipo = ipoHelper.fromIpoStructure(ipoStructure, blenderContext);
14959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta				result = new ObjectAnimationModifier(ipo, action.getName(), objectStructure.getOldMemoryAddress(), blenderContext);
15059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta				blenderContext.addModifier(objectStructure.getOldMemoryAddress(), result);
15159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta			} else {
15259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta				throw new IllegalStateException("Object's action cannot have more than one channel!");
15359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta			}
15459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta		} else {
15559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta			Pointer pIpo = (Pointer) objectStructure.getFieldValue("ipo");
15659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta			if (pIpo.isNotNull()) {
15759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta				Structure ipoStructure = pIpo.fetchData(blenderContext.getInputStream()).get(0);
15859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta				Ipo ipo = ipoHelper.fromIpoStructure(ipoStructure, blenderContext);
15959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta				result = new ObjectAnimationModifier(ipo, objectStructure.getName(), objectStructure.getOldMemoryAddress(), blenderContext);
16059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta				blenderContext.addModifier(objectStructure.getOldMemoryAddress(), result);
16159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta			}
16259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta		}
16359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta		return result;
16459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta	}
16559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
16659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta	/**
16759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta	 * This method reads the object's animation modifier for blender version
16859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta	 * 2.50 and higher.
16959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta	 *
17059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta	 * @param objectStructure
17159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta	 *            the object's structure
17259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta	 * @param blenderContext
17359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta	 *            the blender context
17459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta	 * @return loaded modifier
17559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta	 * @throws BlenderFileException
17659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta	 *             this exception is thrown when the blender file is somehow
17759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta	 *             corrupted
17859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta	 */
17959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta	private Modifier readAnimationModifier250(Structure objectStructure, BlenderContext blenderContext) throws BlenderFileException {
18059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta		Modifier result = null;
18159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta		Pointer pAnimData = (Pointer) objectStructure.getFieldValue("adt");
18259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta		if (pAnimData.isNotNull()) {
18359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta			Structure animData = pAnimData.fetchData(blenderContext.getInputStream()).get(0);
18459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta			Pointer pAction = (Pointer) animData.getFieldValue("action");
18559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta			if (pAction.isNotNull()) {
18659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta				Structure actionStructure = pAction.fetchData(blenderContext.getInputStream()).get(0);
18759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta				IpoHelper ipoHelper = blenderContext.getHelper(IpoHelper.class);
18859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta				Ipo ipo = ipoHelper.fromAction(actionStructure, blenderContext);
18959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta				result = new ObjectAnimationModifier(ipo, actionStructure.getName(), objectStructure.getOldMemoryAddress(), blenderContext);
19059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta				blenderContext.addModifier(objectStructure.getOldMemoryAddress(), result);
19159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta			}
19259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta		}
19359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta		return result;
19459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta	}
19559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta}
196