Search in sources :

Example 1 with EntityType

use of net.drewke.tdme.tools.shared.model.LevelEditorEntity.EntityType in project tdme by andreasdr.

the class ModelMetaDataFileImport method doImportFromJSON.

/**
	 * Imports a model meta data file from JSON object
	 * @param id or LevelEditorEntity.ID_NONE
	 * @param path name or null
	 * @param JSON entity root
	 */
public static LevelEditorEntity doImportFromJSON(int id, String pathName, JSONObject jEntityRoot) throws Exception {
    LevelEditorEntity levelEditorEntity;
    // check for version
    float version = Float.parseFloat(jEntityRoot.getString("version"));
    // pivot
    Vector3 pivot = new Vector3((float) jEntityRoot.getDouble("px"), (float) jEntityRoot.getDouble("py"), (float) jEntityRoot.getDouble("pz"));
    // String thumbnail = jRoot.getString("thumbnail");
    EntityType modelType = LevelEditorEntity.EntityType.valueOf(jEntityRoot.getString("type"));
    String modelFile = jEntityRoot.has("file") == true ? new File(jEntityRoot.getString("file")).getCanonicalPath() : null;
    String modelThumbnail = jEntityRoot.has("thumbnail") == true ? jEntityRoot.getString("thumbnail") : null;
    String name = jEntityRoot.getString("name");
    String description = jEntityRoot.getString("descr");
    // load model
    Model model = null;
    // having a model file?
    String gameRoot = Tools.getGameRootPath(pathName);
    String modelRelativeFileName = null;
    if (modelFile != null) {
        // yep, load it
        modelRelativeFileName = Tools.getRelativeResourcesFileName(gameRoot, modelFile);
        String modelPath = (gameRoot.length() > 0 ? gameRoot + "/" : "") + Tools.getPath(modelRelativeFileName);
        if (modelFile.toLowerCase().endsWith(".dae")) {
            model = DAEReader.read(modelPath, Tools.getFileName(modelRelativeFileName));
        } else if (modelFile.toLowerCase().endsWith(".tm")) {
            model = TMReader.read(modelPath, Tools.getFileName(modelRelativeFileName));
        } else {
            throw new Exception("Unsupported mode file: " + modelFile);
        }
    } else //	TODO: only load if required e.g. for LevelEditor
    if (modelType == EntityType.EMPTY) {
        model = DAEReader.read("resources/tools/leveleditor/models", "arrow.dae");
    }
    // load level editor model
    levelEditorEntity = new LevelEditorEntity(id, modelType, name, description, null, modelFile != null ? new File(gameRoot, modelRelativeFileName).getCanonicalPath() : null, modelThumbnail, model, pivot);
    // parse properties
    JSONArray jProperties = jEntityRoot.getJSONArray("properties");
    for (int i = 0; i < jProperties.length(); i++) {
        JSONObject jProperty = jProperties.getJSONObject(i);
        levelEditorEntity.addProperty(jProperty.getString("name"), jProperty.getString("value"));
    }
    // old: optional bounding volume
    if (jEntityRoot.has("bv") == true) {
        levelEditorEntity.addBoundingVolume(0, parseBoundingVolume(0, levelEditorEntity, jEntityRoot.getJSONObject("bv")));
    } else // new: optional bounding volumeS
    if (jEntityRoot.has("bvs") == true) {
        JSONArray jBoundingVolumes = jEntityRoot.getJSONArray("bvs");
        for (int i = 0; i < jBoundingVolumes.length(); i++) {
            JSONObject jBv = jBoundingVolumes.getJSONObject(i);
            levelEditorEntity.addBoundingVolume(i, parseBoundingVolume(i, levelEditorEntity, jBv));
        }
    }
    // parse particle system 
    if (modelType == EntityType.PARTICLESYSTEM) {
        LevelEditorEntityParticleSystem particleSystem = levelEditorEntity.getParticleSystem();
        JSONObject jParticleSystem = jEntityRoot.getJSONObject("ps");
        // type
        particleSystem.setType(Type.valueOf(jParticleSystem.getString("t")));
        switch(particleSystem.getType()) {
            case NONE:
                {
                    break;
                }
            case OBJECT_PARTICLE_SYSTEM:
                {
                    JSONObject jObjectParticleSystem = jParticleSystem.getJSONObject("ops");
                    ObjectParticleSystem objectParticleSystem = particleSystem.getObjectParticleSystem();
                    objectParticleSystem.setMaxCount(jObjectParticleSystem.getInt("mc"));
                    objectParticleSystem.getScale().setX((float) jObjectParticleSystem.getDouble("sx"));
                    objectParticleSystem.getScale().setY((float) jObjectParticleSystem.getDouble("sy"));
                    objectParticleSystem.getScale().setZ((float) jObjectParticleSystem.getDouble("sz"));
                    objectParticleSystem.setAutoEmit(jObjectParticleSystem.getBoolean("ae"));
                    try {
                        String particleModelFile = jObjectParticleSystem.getString("mf");
                        String particleModelRelativeFileName = Tools.getRelativeResourcesFileName(gameRoot, particleModelFile);
                        String particleModelPath = (gameRoot.length() > 0 ? gameRoot + "/" : "") + Tools.getPath(particleModelRelativeFileName);
                        objectParticleSystem.setModelFile(particleModelPath + "/" + Tools.getFileName(particleModelRelativeFileName));
                    } catch (Exception exception) {
                        exception.printStackTrace();
                        System.out.println("ModelMetaDataFileImport::doImport(): Failed to set model file: " + exception.getMessage());
                    }
                    break;
                }
            case POINT_PARTICLE_SYSTEM:
                {
                    JSONObject jPointParticleSystem = jParticleSystem.getJSONObject("pps");
                    particleSystem.getPointParticleSystem().setMaxPoints(jPointParticleSystem.getInt("mp"));
                    particleSystem.getPointParticleSystem().setAutoEmit(jPointParticleSystem.getBoolean("ae"));
                    break;
                }
            default:
                {
                    System.out.println("ModelMetaDataFileExport::export(): unknown particle system type '" + particleSystem.getType() + "'");
                    break;
                }
        }
        // emitter
        particleSystem.setEmitter(Emitter.valueOf(jParticleSystem.getString("e")));
        switch(particleSystem.getEmitter()) {
            case NONE:
                {
                    break;
                }
            case POINT_PARTICLE_EMITTER:
                {
                    JSONObject jPointParticleEmitter = jParticleSystem.getJSONObject("ppe");
                    PointParticleEmitter emitter = particleSystem.getPointParticleEmitter();
                    emitter.setCount(jPointParticleEmitter.getInt("c"));
                    emitter.setLifeTime(jPointParticleEmitter.getLong("lt"));
                    emitter.setLifeTimeRnd(jPointParticleEmitter.getLong("ltrnd"));
                    emitter.setMass((float) jPointParticleEmitter.getDouble("m"));
                    emitter.setMassRnd((float) jPointParticleEmitter.getDouble("mrnd"));
                    emitter.getPosition().setX((float) jPointParticleEmitter.getDouble("px"));
                    emitter.getPosition().setY((float) jPointParticleEmitter.getDouble("py"));
                    emitter.getPosition().setZ((float) jPointParticleEmitter.getDouble("pz"));
                    emitter.getVelocity().setX((float) jPointParticleEmitter.getDouble("vx"));
                    emitter.getVelocity().setY((float) jPointParticleEmitter.getDouble("vy"));
                    emitter.getVelocity().setZ((float) jPointParticleEmitter.getDouble("vz"));
                    emitter.getVelocityRnd().setX((float) jPointParticleEmitter.getDouble("vrndx"));
                    emitter.getVelocityRnd().setY((float) jPointParticleEmitter.getDouble("vrndy"));
                    emitter.getVelocityRnd().setZ((float) jPointParticleEmitter.getDouble("vrndz"));
                    emitter.getColorStart().setRed((float) jPointParticleEmitter.getDouble("csr"));
                    emitter.getColorStart().setGreen((float) jPointParticleEmitter.getDouble("csg"));
                    emitter.getColorStart().setBlue((float) jPointParticleEmitter.getDouble("csb"));
                    emitter.getColorStart().setAlpha((float) jPointParticleEmitter.getDouble("csa"));
                    emitter.getColorEnd().setRed((float) jPointParticleEmitter.getDouble("cer"));
                    emitter.getColorEnd().setGreen((float) jPointParticleEmitter.getDouble("ceg"));
                    emitter.getColorEnd().setBlue((float) jPointParticleEmitter.getDouble("ceb"));
                    emitter.getColorEnd().setAlpha((float) jPointParticleEmitter.getDouble("cea"));
                    break;
                }
            case BOUNDINGBOX_PARTICLE_EMITTER:
                {
                    JSONObject jBoundingBoxParticleEmitter = jParticleSystem.getJSONObject("bbpe");
                    BoundingBoxParticleEmitter emitter = particleSystem.getBoundingBoxParticleEmitters();
                    emitter.setCount(jBoundingBoxParticleEmitter.getInt("c"));
                    emitter.setLifeTime(jBoundingBoxParticleEmitter.getLong("lt"));
                    emitter.setLifeTimeRnd(jBoundingBoxParticleEmitter.getLong("ltrnd"));
                    emitter.setMass((float) jBoundingBoxParticleEmitter.getDouble("m"));
                    emitter.setMassRnd((float) jBoundingBoxParticleEmitter.getDouble("mrnd"));
                    emitter.getVelocity().setX((float) jBoundingBoxParticleEmitter.getDouble("vx"));
                    emitter.getVelocity().setY((float) jBoundingBoxParticleEmitter.getDouble("vy"));
                    emitter.getVelocity().setZ((float) jBoundingBoxParticleEmitter.getDouble("vz"));
                    emitter.getVelocityRnd().setX((float) jBoundingBoxParticleEmitter.getDouble("vrndx"));
                    emitter.getVelocityRnd().setY((float) jBoundingBoxParticleEmitter.getDouble("vrndy"));
                    emitter.getVelocityRnd().setZ((float) jBoundingBoxParticleEmitter.getDouble("vrndz"));
                    emitter.getColorStart().setRed((float) jBoundingBoxParticleEmitter.getDouble("csr"));
                    emitter.getColorStart().setGreen((float) jBoundingBoxParticleEmitter.getDouble("csg"));
                    emitter.getColorStart().setBlue((float) jBoundingBoxParticleEmitter.getDouble("csb"));
                    emitter.getColorStart().setAlpha((float) jBoundingBoxParticleEmitter.getDouble("csa"));
                    emitter.getColorEnd().setRed((float) jBoundingBoxParticleEmitter.getDouble("cer"));
                    emitter.getColorEnd().setGreen((float) jBoundingBoxParticleEmitter.getDouble("ceg"));
                    emitter.getColorEnd().setBlue((float) jBoundingBoxParticleEmitter.getDouble("ceb"));
                    emitter.getColorEnd().setAlpha((float) jBoundingBoxParticleEmitter.getDouble("cea"));
                    emitter.getObbCenter().setX((float) jBoundingBoxParticleEmitter.getDouble("ocx"));
                    emitter.getObbCenter().setY((float) jBoundingBoxParticleEmitter.getDouble("ocy"));
                    emitter.getObbCenter().setZ((float) jBoundingBoxParticleEmitter.getDouble("ocz"));
                    emitter.getObbHalfextension().setX((float) jBoundingBoxParticleEmitter.getDouble("ohex"));
                    emitter.getObbHalfextension().setY((float) jBoundingBoxParticleEmitter.getDouble("ohey"));
                    emitter.getObbHalfextension().setZ((float) jBoundingBoxParticleEmitter.getDouble("ohez"));
                    emitter.getObbAxis0().setX((float) jBoundingBoxParticleEmitter.getDouble("oa0x"));
                    emitter.getObbAxis0().setY((float) jBoundingBoxParticleEmitter.getDouble("oa0y"));
                    emitter.getObbAxis0().setZ((float) jBoundingBoxParticleEmitter.getDouble("oa0z"));
                    emitter.getObbAxis1().setX((float) jBoundingBoxParticleEmitter.getDouble("oa1x"));
                    emitter.getObbAxis1().setY((float) jBoundingBoxParticleEmitter.getDouble("oa1y"));
                    emitter.getObbAxis1().setZ((float) jBoundingBoxParticleEmitter.getDouble("oa1z"));
                    emitter.getObbAxis2().setX((float) jBoundingBoxParticleEmitter.getDouble("oa2x"));
                    emitter.getObbAxis2().setY((float) jBoundingBoxParticleEmitter.getDouble("oa2y"));
                    emitter.getObbAxis2().setZ((float) jBoundingBoxParticleEmitter.getDouble("oa2z"));
                    break;
                }
            case CIRCLE_PARTICLE_EMITTER:
                {
                    JSONObject jCircleParticleEmitter = jParticleSystem.getJSONObject("cpe");
                    CircleParticleEmitter emitter = particleSystem.getCircleParticleEmitter();
                    emitter.setCount(jCircleParticleEmitter.getInt("c"));
                    emitter.setLifeTime(jCircleParticleEmitter.getLong("lt"));
                    emitter.setLifeTimeRnd(jCircleParticleEmitter.getLong("ltrnd"));
                    emitter.setMass((float) jCircleParticleEmitter.getDouble("m"));
                    emitter.setMassRnd((float) jCircleParticleEmitter.getDouble("mrnd"));
                    emitter.getVelocity().setX((float) jCircleParticleEmitter.getDouble("vx"));
                    emitter.getVelocity().setY((float) jCircleParticleEmitter.getDouble("vy"));
                    emitter.getVelocity().setZ((float) jCircleParticleEmitter.getDouble("vz"));
                    emitter.getVelocityRnd().setX((float) jCircleParticleEmitter.getDouble("vrndx"));
                    emitter.getVelocityRnd().setY((float) jCircleParticleEmitter.getDouble("vrndy"));
                    emitter.getVelocityRnd().setZ((float) jCircleParticleEmitter.getDouble("vrndz"));
                    emitter.getColorStart().setRed((float) jCircleParticleEmitter.getDouble("csr"));
                    emitter.getColorStart().setGreen((float) jCircleParticleEmitter.getDouble("csg"));
                    emitter.getColorStart().setBlue((float) jCircleParticleEmitter.getDouble("csb"));
                    emitter.getColorStart().setAlpha((float) jCircleParticleEmitter.getDouble("csa"));
                    emitter.getColorEnd().setRed((float) jCircleParticleEmitter.getDouble("cer"));
                    emitter.getColorEnd().setGreen((float) jCircleParticleEmitter.getDouble("ceg"));
                    emitter.getColorEnd().setBlue((float) jCircleParticleEmitter.getDouble("ceb"));
                    emitter.getColorEnd().setAlpha((float) jCircleParticleEmitter.getDouble("cea"));
                    emitter.getCenter().setX((float) jCircleParticleEmitter.getDouble("cx"));
                    emitter.getCenter().setY((float) jCircleParticleEmitter.getDouble("cy"));
                    emitter.getCenter().setZ((float) jCircleParticleEmitter.getDouble("cz"));
                    emitter.setRadius((float) jCircleParticleEmitter.getDouble("r"));
                    emitter.getAxis0().setX((float) jCircleParticleEmitter.getDouble("a0x"));
                    emitter.getAxis0().setY((float) jCircleParticleEmitter.getDouble("a0y"));
                    emitter.getAxis0().setZ((float) jCircleParticleEmitter.getDouble("a0z"));
                    emitter.getAxis1().setX((float) jCircleParticleEmitter.getDouble("a1x"));
                    emitter.getAxis1().setY((float) jCircleParticleEmitter.getDouble("a1y"));
                    emitter.getAxis1().setZ((float) jCircleParticleEmitter.getDouble("a1z"));
                    break;
                }
            case CIRCLE_PARTICLE_EMITTER_PLANE_VELOCITY:
                {
                    JSONObject jCircleParticleEmitterPlaneVelocity = jParticleSystem.getJSONObject("cpeev");
                    CircleParticleEmitterPlaneVelocity emitter = particleSystem.getCircleParticleEmitterPlaneVelocity();
                    emitter.setCount(jCircleParticleEmitterPlaneVelocity.getInt("c"));
                    emitter.setLifeTime(jCircleParticleEmitterPlaneVelocity.getLong("lt"));
                    emitter.setLifeTimeRnd(jCircleParticleEmitterPlaneVelocity.getLong("ltrnd"));
                    emitter.setMass((float) jCircleParticleEmitterPlaneVelocity.getDouble("m"));
                    emitter.setMassRnd((float) jCircleParticleEmitterPlaneVelocity.getDouble("mrnd"));
                    emitter.setVelocity((float) jCircleParticleEmitterPlaneVelocity.getDouble("v"));
                    emitter.setVelocityRnd((float) jCircleParticleEmitterPlaneVelocity.getDouble("vrnd"));
                    emitter.getColorStart().setRed((float) jCircleParticleEmitterPlaneVelocity.getDouble("csr"));
                    emitter.getColorStart().setGreen((float) jCircleParticleEmitterPlaneVelocity.getDouble("csg"));
                    emitter.getColorStart().setBlue((float) jCircleParticleEmitterPlaneVelocity.getDouble("csb"));
                    emitter.getColorStart().setAlpha((float) jCircleParticleEmitterPlaneVelocity.getDouble("csa"));
                    emitter.getColorEnd().setRed((float) jCircleParticleEmitterPlaneVelocity.getDouble("cer"));
                    emitter.getColorEnd().setGreen((float) jCircleParticleEmitterPlaneVelocity.getDouble("ceg"));
                    emitter.getColorEnd().setBlue((float) jCircleParticleEmitterPlaneVelocity.getDouble("ceb"));
                    emitter.getColorEnd().setAlpha((float) jCircleParticleEmitterPlaneVelocity.getDouble("cea"));
                    emitter.getCenter().setX((float) jCircleParticleEmitterPlaneVelocity.getDouble("cx"));
                    emitter.getCenter().setY((float) jCircleParticleEmitterPlaneVelocity.getDouble("cy"));
                    emitter.getCenter().setZ((float) jCircleParticleEmitterPlaneVelocity.getDouble("cz"));
                    emitter.setRadius((float) jCircleParticleEmitterPlaneVelocity.getDouble("r"));
                    emitter.getAxis0().setX((float) jCircleParticleEmitterPlaneVelocity.getDouble("a0x"));
                    emitter.getAxis0().setY((float) jCircleParticleEmitterPlaneVelocity.getDouble("a0y"));
                    emitter.getAxis0().setZ((float) jCircleParticleEmitterPlaneVelocity.getDouble("a0z"));
                    emitter.getAxis1().setX((float) jCircleParticleEmitterPlaneVelocity.getDouble("a1x"));
                    emitter.getAxis1().setY((float) jCircleParticleEmitterPlaneVelocity.getDouble("a1y"));
                    emitter.getAxis1().setZ((float) jCircleParticleEmitterPlaneVelocity.getDouble("a1z"));
                    break;
                }
            case SPHERE_PARTICLE_EMITTER:
                {
                    JSONObject jSphereParticleEmitter = jParticleSystem.getJSONObject("spe");
                    SphereParticleEmitter emitter = particleSystem.getSphereParticleEmitter();
                    emitter.setCount(jSphereParticleEmitter.getInt("c"));
                    emitter.setLifeTime(jSphereParticleEmitter.getLong("lt"));
                    emitter.setLifeTimeRnd(jSphereParticleEmitter.getLong("ltrnd"));
                    emitter.setMass((float) jSphereParticleEmitter.getDouble("m"));
                    emitter.setMassRnd((float) jSphereParticleEmitter.getDouble("mrnd"));
                    emitter.getVelocity().setX((float) jSphereParticleEmitter.getDouble("vx"));
                    emitter.getVelocity().setY((float) jSphereParticleEmitter.getDouble("vy"));
                    emitter.getVelocity().setZ((float) jSphereParticleEmitter.getDouble("vz"));
                    emitter.getVelocityRnd().setX((float) jSphereParticleEmitter.getDouble("vrndx"));
                    emitter.getVelocityRnd().setY((float) jSphereParticleEmitter.getDouble("vrndy"));
                    emitter.getVelocityRnd().setZ((float) jSphereParticleEmitter.getDouble("vrndz"));
                    emitter.getColorStart().setRed((float) jSphereParticleEmitter.getDouble("csr"));
                    emitter.getColorStart().setGreen((float) jSphereParticleEmitter.getDouble("csg"));
                    emitter.getColorStart().setBlue((float) jSphereParticleEmitter.getDouble("csb"));
                    emitter.getColorStart().setAlpha((float) jSphereParticleEmitter.getDouble("csa"));
                    emitter.getColorEnd().setRed((float) jSphereParticleEmitter.getDouble("cer"));
                    emitter.getColorEnd().setGreen((float) jSphereParticleEmitter.getDouble("ceg"));
                    emitter.getColorEnd().setBlue((float) jSphereParticleEmitter.getDouble("ceb"));
                    emitter.getColorEnd().setAlpha((float) jSphereParticleEmitter.getDouble("cea"));
                    emitter.getCenter().setX((float) jSphereParticleEmitter.getDouble("cx"));
                    emitter.getCenter().setY((float) jSphereParticleEmitter.getDouble("cy"));
                    emitter.getCenter().setZ((float) jSphereParticleEmitter.getDouble("cz"));
                    emitter.setRadius((float) jSphereParticleEmitter.getDouble("r"));
                    break;
                }
            default:
                System.out.println("ModelMetaDataFileExport::export(): unknown particle system emitter '" + particleSystem.getEmitter() + "'");
        }
    }
    // done
    return levelEditorEntity;
}
Also used : CircleParticleEmitterPlaneVelocity(net.drewke.tdme.tools.shared.model.LevelEditorEntityParticleSystem.CircleParticleEmitterPlaneVelocity) JSONArray(org.json.JSONArray) CircleParticleEmitter(net.drewke.tdme.tools.shared.model.LevelEditorEntityParticleSystem.CircleParticleEmitter) Vector3(net.drewke.tdme.math.Vector3) JSONException(org.json.JSONException) IOException(java.io.IOException) LevelEditorEntityParticleSystem(net.drewke.tdme.tools.shared.model.LevelEditorEntityParticleSystem) EntityType(net.drewke.tdme.tools.shared.model.LevelEditorEntity.EntityType) JSONObject(org.json.JSONObject) Model(net.drewke.tdme.engine.model.Model) PrimitiveModel(net.drewke.tdme.engine.primitives.PrimitiveModel) BoundingBoxParticleEmitter(net.drewke.tdme.tools.shared.model.LevelEditorEntityParticleSystem.BoundingBoxParticleEmitter) ObjectParticleSystem(net.drewke.tdme.tools.shared.model.LevelEditorEntityParticleSystem.ObjectParticleSystem) LevelEditorEntity(net.drewke.tdme.tools.shared.model.LevelEditorEntity) File(java.io.File) SphereParticleEmitter(net.drewke.tdme.tools.shared.model.LevelEditorEntityParticleSystem.SphereParticleEmitter) PointParticleEmitter(net.drewke.tdme.tools.shared.model.LevelEditorEntityParticleSystem.PointParticleEmitter)

Example 2 with EntityType

use of net.drewke.tdme.tools.shared.model.LevelEditorEntity.EntityType in project tdme by andreasdr.

the class DAEReader method readLevel.

/**
	 * Reads Collada DAE file level
	 * @param path name
	 * @param file name
	 * @throws Exception
	 * @return Model instance
	 */
public static LevelEditorLevel readLevel(String pathName, String fileName) throws Exception {
    // (re)create tm files folder
    File tmFilesFolder = new File(pathName + "/" + fileName + "-models");
    if (tmFilesFolder.exists()) {
        tmFilesFolder.delete();
    }
    tmFilesFolder.mkdir();
    // create level
    LevelEditorLevel levelEditorLevel = new LevelEditorLevel();
    LevelPropertyPresets.getInstance().setDefaultLevelProperties(levelEditorLevel);
    // load dae xml document
    DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document document = builder.parse(FileSystem.getInstance().getInputStream(pathName, fileName));
    Element xmlRoot = document.getDocumentElement();
    // authoring tool
    AuthoringTool authoringTool = getAuthoringTool(xmlRoot);
    // up vector and rotation order
    UpVector upVector = getUpVector(xmlRoot);
    RotationOrder rotationOrder = null;
    switch(upVector) {
        case Y_UP:
            rotationOrder = RotationOrder.ZYX;
        case Z_UP:
            rotationOrder = RotationOrder.YZX;
    }
    levelEditorLevel.setRotationOrder(rotationOrder);
    // parse scene from xml
    String xmlSceneId = null;
    Element xmlScene = getChildrenByTagName(xmlRoot, "scene").get(0);
    for (Element xmlInstanceVisualscene : getChildrenByTagName(xmlScene, "instance_visual_scene")) {
        xmlSceneId = xmlInstanceVisualscene.getAttribute("url").substring(1);
    }
    // check for xml scene id
    if (xmlSceneId == null) {
        throw new ModelFileIOException("No scene id found");
    }
    // parse visual scenes
    Element xmlLibraryVisualScenes = getChildrenByTagName(xmlRoot, "library_visual_scenes").get(0);
    for (Element xmlLibraryVisualScene : getChildrenByTagName(xmlLibraryVisualScenes, "visual_scene")) {
        String xmlVisualSceneId = xmlLibraryVisualScene.getAttribute("id");
        if (xmlVisualSceneId.equals(xmlSceneId)) {
            // default FPS
            float fps = 30f;
            // parse frames per second
            List<Element> xmlExtraNodes = getChildrenByTagName(xmlLibraryVisualScene, "extra");
            if (xmlExtraNodes.isEmpty() == false) {
                Element xmlExtraNode = xmlExtraNodes.get(0);
                for (Element xmlTechnique : getChildrenByTagName(xmlExtraNode, "technique")) {
                    List<Element> xmlFrameRateNodes = getChildrenByTagName(xmlTechnique, "frame_rate");
                    if (xmlFrameRateNodes.isEmpty() == false) {
                        fps = Float.parseFloat(xmlFrameRateNodes.get(0).getTextContent());
                        break;
                    }
                }
            }
            // visual scene root nodes
            LevelEditorEntityLibrary entityLibrary = levelEditorLevel.getEntityLibrary();
            LevelEditorEntity emptyEntity = null;
            int nodeIdx = 0;
            for (Element xmlNode : getChildrenByTagName(xmlLibraryVisualScene, "node")) {
                // derive model name from node id
                String modelName = xmlNode.getAttribute("id");
                // replace blender _|-NUMBER, not sure if this is a good idea for all cases, we will see
                modelName = modelName.replaceAll("[\\-\\_]{1}+[0-9]+$", "");
                // replace number at the end still, not sure if this is a good idea for all cases, we will see
                modelName = modelName.replaceAll("[0-9]+$", "");
                // check if name is available, if not extend with numbers :DDD
                boolean haveName = entityLibrary.getEntityCount() == 0;
                if (haveName == false) {
                    for (int i = 0; i < 10000; i++) {
                        haveName = true;
                        String modelNameTry = modelName + (i == 0 ? "" : String.valueOf(i));
                        for (int entityIdx = 0; entityIdx < entityLibrary.getEntityCount(); entityIdx++) {
                            LevelEditorEntity entity = entityLibrary.getEntityAt(entityIdx);
                            if (entity.getName().equals(modelNameTry) == true) {
                                haveName = false;
                                break;
                            }
                        }
                        if (haveName == true) {
                            modelName = modelNameTry;
                            break;
                        }
                    }
                }
                // do we have a name now?
                if (haveName == false) {
                    // nope, cant imagine this will happen 
                    System.out.println("DAEReader::readLevel(): Skipping model '" + modelName + "' as no name could be created for it.");
                    continue;
                }
                // 	create model
                Model model = new Model(pathName + File.separator + fileName + '-' + modelName, fileName + '-' + modelName, upVector, rotationOrder, null);
                // import matrix
                setupModelImportRotationMatrix(xmlRoot, model);
                Matrix4x4 modelImportRotationMatrix = new Matrix4x4(model.getImportTransformationsMatrix());
                setupModelImportScaleMatrix(xmlRoot, model);
                // translation, scaling, rotation
                Vector3 translation = new Vector3();
                Vector3 scale = new Vector3();
                Vector3 rotation = new Vector3();
                Vector3 xAxis = new Vector3();
                Vector3 yAxis = new Vector3();
                Vector3 zAxis = new Vector3();
                // set up local transformations matrix
                Matrix4x4 nodeTransformationsMatrix = null;
                List<Element> xmlMatrixElements = getChildrenByTagName(xmlNode, "matrix");
                if (xmlMatrixElements.size() == 1) {
                    String xmlMatrix = xmlMatrixElements.get(0).getTextContent();
                    StringTokenizer t = new StringTokenizer(xmlMatrix, " \n\r");
                    // 
                    nodeTransformationsMatrix = new Matrix4x4(Float.parseFloat(t.nextToken()), Float.parseFloat(t.nextToken()), Float.parseFloat(t.nextToken()), Float.parseFloat(t.nextToken()), Float.parseFloat(t.nextToken()), Float.parseFloat(t.nextToken()), Float.parseFloat(t.nextToken()), Float.parseFloat(t.nextToken()), Float.parseFloat(t.nextToken()), Float.parseFloat(t.nextToken()), Float.parseFloat(t.nextToken()), Float.parseFloat(t.nextToken()), Float.parseFloat(t.nextToken()), Float.parseFloat(t.nextToken()), Float.parseFloat(t.nextToken()), Float.parseFloat(t.nextToken())).transpose();
                }
                // check if we have node transformations matrix
                if (nodeTransformationsMatrix == null) {
                    throw new ModelFileIOException("missing node transformations matrix for node " + xmlNode.getAttribute("id"));
                }
                // extract coordinate system axes
                nodeTransformationsMatrix.getAxes(xAxis, yAxis, zAxis);
                nodeTransformationsMatrix.getTranslation(translation);
                nodeTransformationsMatrix.getScale(scale);
                // normalize coordinate axes
                xAxis.normalize();
                yAxis.normalize();
                zAxis.normalize();
                // write back normalized x axis
                nodeTransformationsMatrix.setAxes(xAxis, yAxis, zAxis);
                // TODO: move me into Matrix4x4 decomposing code?
                if ((upVector == UpVector.Y_UP && Vector3.computeDotProduct(Vector3.computeCrossProduct(xAxis, yAxis), zAxis) < 0.0f) || (upVector == UpVector.Z_UP && Vector3.computeDotProduct(Vector3.computeCrossProduct(xAxis, zAxis), yAxis) < 0.0f)) {
                    // negate axes
                    xAxis.scale(-1f);
                    yAxis.scale(-1f);
                    zAxis.scale(-1f);
                    // and write back to node transformation matrices
                    nodeTransformationsMatrix.setAxes(xAxis, yAxis, zAxis);
                    // scale
                    scale.scale(-1f);
                }
                // determine rotation
                nodeTransformationsMatrix.computeEulerAngles(rotation);
                // apply model import matrix
                modelImportRotationMatrix.multiply(scale, scale);
                modelImportRotationMatrix.multiply(rotation, rotation);
                model.getImportTransformationsMatrix().multiply(translation, translation);
                // set up frames per seconds
                model.setFPS(fps);
                // read sub groups
                Group group = readVisualSceneNode(authoringTool, pathName, model, null, xmlRoot, xmlNode, fps);
                if (group != null) {
                    group.getTransformationsMatrix().identity();
                    model.getSubGroups().put(group.getId(), group);
                    model.getGroups().put(group.getId(), group);
                }
                // set up joints
                ModelHelper.setupJoints(model);
                // fix animation length
                ModelHelper.fixAnimationLength(model);
                // prepare for indexed rendering
                ModelHelper.prepareForIndexedRendering(model);
                // check if empty model
                EntityType entityType = EntityType.MODEL;
                ModelStatistics modelStatistics = ModelUtilities.computeModelStatistics(model);
                if (modelStatistics.getOpaqueFaceCount() == 0 && modelStatistics.getTransparentFaceCount() == 0) {
                    entityType = EntityType.EMPTY;
                }
                // level editor entity
                LevelEditorEntity levelEditorEntity = null;
                // model
                if (entityType == EntityType.MODEL) {
                    // check if we have that model already
                    for (int i = 0; i < levelEditorLevel.getEntityLibrary().getEntityCount(); i++) {
                        LevelEditorEntity levelEditorEntityCompare = levelEditorLevel.getEntityLibrary().getEntityAt(i);
                        if (levelEditorEntityCompare.getType() != EntityType.MODEL)
                            continue;
                        if (ModelUtilities.equals(model, levelEditorEntityCompare.getModel()) == true) {
                            levelEditorEntity = levelEditorEntityCompare;
                            break;
                        }
                    }
                    // create level editor model, if not yet exists
                    if (levelEditorEntity == null) {
                        // save model
                        TMWriter.write(model, pathName + "/" + fileName + "-models", modelName + ".tm");
                        // create level editor entity
                        levelEditorEntity = entityLibrary.addModel(nodeIdx++, modelName, modelName, pathName + "/" + fileName + "-models", modelName + ".tm", new Vector3());
                    }
                } else // empty
                if (entityType == EntityType.EMPTY) {
                    if (emptyEntity == null) {
                        emptyEntity = entityLibrary.addEmpty(nodeIdx++, "Default Empty", "");
                    }
                    levelEditorEntity = emptyEntity;
                } else {
                    System.out.println("DAEReader::readLevel(): unknown entity type. Skipping");
                    continue;
                }
                // level editor object transformations
                Transformations levelEditorObjectTransformations = new Transformations();
                levelEditorObjectTransformations.getTranslation().set(translation);
                levelEditorObjectTransformations.getRotations().add(new Rotation(rotation.getArray()[rotationOrder.getAxis0VectorIndex()], rotationOrder.getAxis0()));
                levelEditorObjectTransformations.getRotations().add(new Rotation(rotation.getArray()[rotationOrder.getAxis1VectorIndex()], rotationOrder.getAxis1()));
                levelEditorObjectTransformations.getRotations().add(new Rotation(rotation.getArray()[rotationOrder.getAxis2VectorIndex()], rotationOrder.getAxis2()));
                levelEditorObjectTransformations.getScale().set(scale);
                levelEditorObjectTransformations.update();
                // level editor object
                LevelEditorObject object = new LevelEditorObject(xmlNode.getAttribute("id"), xmlNode.getAttribute("id"), levelEditorObjectTransformations, levelEditorEntity);
                // add object to level
                levelEditorLevel.addObject(object);
            }
        }
    }
    // save level
    LevelFileExport.export(pathName + "/" + fileName + ".tl", levelEditorLevel);
    //
    return levelEditorLevel;
}
Also used : Group(net.drewke.tdme.engine.model.Group) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) ModelStatistics(net.drewke.tdme.engine.subsystems.object.ModelUtilitiesInternal.ModelStatistics) LevelEditorObject(net.drewke.tdme.tools.shared.model.LevelEditorObject) LevelEditorEntity(net.drewke.tdme.tools.shared.model.LevelEditorEntity) UpVector(net.drewke.tdme.engine.model.Model.UpVector) Vector3(net.drewke.tdme.math.Vector3) Rotation(net.drewke.tdme.engine.Rotation) Joint(net.drewke.tdme.engine.model.Joint) Matrix4x4(net.drewke.tdme.math.Matrix4x4) EntityType(net.drewke.tdme.tools.shared.model.LevelEditorEntity.EntityType) LevelEditorEntityLibrary(net.drewke.tdme.tools.shared.model.LevelEditorEntityLibrary) StringTokenizer(java.util.StringTokenizer) LevelEditorLevel(net.drewke.tdme.tools.shared.model.LevelEditorLevel) DocumentBuilder(javax.xml.parsers.DocumentBuilder) RotationOrder(net.drewke.tdme.engine.model.RotationOrder) Model(net.drewke.tdme.engine.model.Model) Transformations(net.drewke.tdme.engine.Transformations) File(java.io.File)

Aggregations

File (java.io.File)2 Model (net.drewke.tdme.engine.model.Model)2 Vector3 (net.drewke.tdme.math.Vector3)2 LevelEditorEntity (net.drewke.tdme.tools.shared.model.LevelEditorEntity)2 EntityType (net.drewke.tdme.tools.shared.model.LevelEditorEntity.EntityType)2 IOException (java.io.IOException)1 StringTokenizer (java.util.StringTokenizer)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 Rotation (net.drewke.tdme.engine.Rotation)1 Transformations (net.drewke.tdme.engine.Transformations)1 Group (net.drewke.tdme.engine.model.Group)1 Joint (net.drewke.tdme.engine.model.Joint)1 UpVector (net.drewke.tdme.engine.model.Model.UpVector)1 RotationOrder (net.drewke.tdme.engine.model.RotationOrder)1 PrimitiveModel (net.drewke.tdme.engine.primitives.PrimitiveModel)1 ModelStatistics (net.drewke.tdme.engine.subsystems.object.ModelUtilitiesInternal.ModelStatistics)1 Matrix4x4 (net.drewke.tdme.math.Matrix4x4)1 LevelEditorEntityLibrary (net.drewke.tdme.tools.shared.model.LevelEditorEntityLibrary)1 LevelEditorEntityParticleSystem (net.drewke.tdme.tools.shared.model.LevelEditorEntityParticleSystem)1 BoundingBoxParticleEmitter (net.drewke.tdme.tools.shared.model.LevelEditorEntityParticleSystem.BoundingBoxParticleEmitter)1