159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta/* 259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * Copyright (c) 2009-2010 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; 3359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 3459b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.math.FastMath; 3559b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.math.Quaternion; 3659b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.scene.plugins.blender.exceptions.BlenderFileException; 3759b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.scene.plugins.blender.file.Pointer; 3859b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.scene.plugins.blender.file.Structure; 3959b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.scene.plugins.blender.objects.Properties; 4059b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.util.BufferUtils; 4159b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport java.nio.ByteBuffer; 4259b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport java.nio.FloatBuffer; 4359b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport java.util.List; 4459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 4559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta/** 4659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * A purpose of the helper class is to split calculation code into several classes. Each helper after use should be cleared because it can 4759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * hold the state of the calculations. 4859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * @author Marcin Roguski 4959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta */ 5059b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartapublic abstract class AbstractBlenderHelper { 5159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 5259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta /** The version of the blend file. */ 5359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta protected final int blenderVersion; 5459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta /** This variable indicates if the Y asxis is the UP axis or not. */ 5559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta protected boolean fixUpAxis; 5659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta /** Quaternion used to rotate data when Y is up axis. */ 5759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta protected Quaternion upAxisRotationQuaternion; 5859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 5959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta /** 6059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * This constructor parses the given blender version and stores the result. Some functionalities may differ in different blender 6159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * versions. 6259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * @param blenderVersion 6359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * the version read from the blend file 6459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * @param fixUpAxis 6559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * a variable that indicates if the Y asxis is the UP axis or not 6659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta */ 6759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta public AbstractBlenderHelper(String blenderVersion, boolean fixUpAxis) { 6859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta this.blenderVersion = Integer.parseInt(blenderVersion); 6959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta this.fixUpAxis = fixUpAxis; 7059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta if(fixUpAxis) { 7159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta upAxisRotationQuaternion = new Quaternion().fromAngles(-FastMath.HALF_PI, 0, 0); 7259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 7359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 7459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 7559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta /** 7659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * This method clears the state of the helper so that it can be used for different calculations of another feature. 7759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta */ 7859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta public void clearState() {} 7959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 8059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta /** 8159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * This method should be used to check if the text is blank. Avoid using text.trim().length()==0. This causes that more strings are 8259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * being created and stored in the memory. It can be unwise especially inside loops. 8359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * @param text 8459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * the text to be checked 8559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * @return <b>true</b> if the text is blank and <b>false</b> otherwise 8659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta */ 8759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta protected boolean isBlank(String text) { 8859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta if (text != null) { 8959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta for (int i = 0; i < text.length(); ++i) { 9059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta if (!Character.isWhitespace(text.charAt(i))) { 9159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta return false; 9259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 9359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 9459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 9559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta return true; 9659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 9759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 9859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta /** 9959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * Generate a new ByteBuffer using the given array of byte[4] objects. The ByteBuffer will be 4 * data.length 10059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * long and contain the vector data as data[0][0], data[0][1], data[0][2], data[0][3], data[1][0]... etc. 10159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * @param data 10259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * list of byte[4] objects to place into a new ByteBuffer 10359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta */ 10459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta protected ByteBuffer createByteBuffer(List<byte[]> data) { 10559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta if (data == null) { 10659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta return null; 10759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 10859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta ByteBuffer buff = BufferUtils.createByteBuffer(4 * data.size()); 10959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta for (byte[] v : data) { 11059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta if (v != null) { 11159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta buff.put(v[0]).put(v[1]).put(v[2]).put(v[3]); 11259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } else { 11359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta buff.put((byte)0).put((byte)0).put((byte)0).put((byte)0); 11459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 11559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 11659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta buff.flip(); 11759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta return buff; 11859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 11959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 12059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta /** 12159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * Generate a new FloatBuffer using the given array of float[4] objects. The FloatBuffer will be 4 * data.length 12259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * long and contain the vector data as data[0][0], data[0][1], data[0][2], data[0][3], data[1][0]... etc. 12359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * @param data 12459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * list of float[4] objects to place into a new FloatBuffer 12559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta */ 12659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta protected FloatBuffer createFloatBuffer(List<float[]> data) { 12759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta if (data == null) { 12859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta return null; 12959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 13059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta FloatBuffer buff = BufferUtils.createFloatBuffer(4 * data.size()); 13159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta for (float[] v : data) { 13259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta if (v != null) { 13359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta buff.put(v[0]).put(v[1]).put(v[2]).put(v[3]); 13459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } else { 13559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta buff.put(0).put(0).put(0).put(0); 13659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 13759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 13859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta buff.flip(); 13959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta return buff; 14059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 14159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 14259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta /** 14359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * This method loads the properties if they are available and defined for the structure. 14459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * @param structure 14559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * the structure we read the properties from 14659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * @param blenderContext 14759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * the blender context 14859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * @return loaded properties or null if they are not available 14959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * @throws BlenderFileException 15059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * an exception is thrown when the blend file is somehow corrupted 15159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta */ 15259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta protected Properties loadProperties(Structure structure, BlenderContext blenderContext) throws BlenderFileException { 15359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta Properties properties = null; 15459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta Structure id = (Structure) structure.getFieldValue("ID"); 15559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta if (id != null) { 15659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta Pointer pProperties = (Pointer) id.getFieldValue("properties"); 15759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta if (pProperties.isNotNull()) { 15859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta Structure propertiesStructure = pProperties.fetchData(blenderContext.getInputStream()).get(0); 15959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta properties = new Properties(); 16059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta properties.load(propertiesStructure, blenderContext); 16159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 16259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 16359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta return properties; 16459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 16559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 16659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta /** 16759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * This method analyzes the given structure and the data contained within 16859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * blender context and decides if the feature should be loaded. 16959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * @param structure 17059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * structure to be analyzed 17159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * @param blenderContext 17259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * the blender context 17359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * @return <b>true</b> if the feature should be loaded and false otherwise 17459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta */ 17559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta public abstract boolean shouldBeLoaded(Structure structure, BlenderContext blenderContext); 17659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta} 177