Search in sources :

Example 6 with Vector3

use of net.drewke.tdme.math.Vector3 in project tdme by andreasdr.

the class LevelFileImport method doImport.

/**
	 * Imports a level from a TDME level file to Level Editor
	 * @param path name
	 * @param file name
	 * @param level
	 * @param object id prefix
	 */
public static void doImport(String pathName, String fileName, LevelEditorLevel level, String objectIdPrefix) throws Exception {
    pathName = pathName.replace(File.separatorChar == '/' ? '\\' : '/', File.separatorChar);
    fileName = fileName.replace(File.separatorChar == '/' ? '\\' : '/', File.separatorChar);
    JSONObject jRoot = null;
    InputStream is = null;
    try {
        jRoot = new JSONObject(new JSONTokener(FileSystem.getInstance().getContent(pathName, fileName)));
    } catch (IOException ioe) {
        throw ioe;
    } finally {
        if (is != null)
            try {
                is.close();
            } catch (IOException ioei) {
            }
    }
    // game root path
    level.setGameRoot(Tools.getGameRootPath(pathName));
    // check for version
    float version = Float.parseFloat(jRoot.getString("version"));
    // new: rotation order
    level.setRotationOrder(jRoot.has("ro") == true ? RotationOrder.valueOf(jRoot.getString("ro")) : RotationOrder.XYZ);
    // map properties
    level.clearProperties();
    // parse properties
    JSONArray jMapProperties = jRoot.getJSONArray("properties");
    for (int i = 0; i < jMapProperties.length(); i++) {
        JSONObject jMapProperty = jMapProperties.getJSONObject(i);
        level.addProperty(jMapProperty.getString("name"), jMapProperty.getString("value"));
    }
    // lights
    if (jRoot.has("lights") == true) {
        JSONArray jLights = jRoot.getJSONArray("lights");
        for (int i = 0; i < jLights.length(); i++) {
            JSONObject jLight = jLights.getJSONObject(i);
            LevelEditorLight light = level.getLightAt(jLight.has("id") ? jLight.getInt("id") : i);
            // set up light in level
            light.getAmbient().set((float) jLight.getDouble("ar"), (float) jLight.getDouble("ag"), (float) jLight.getDouble("ab"), (float) jLight.getDouble("aa"));
            light.getDiffuse().set((float) jLight.getDouble("dr"), (float) jLight.getDouble("dg"), (float) jLight.getDouble("db"), (float) jLight.getDouble("da"));
            light.getSpecular().set((float) jLight.getDouble("sr"), (float) jLight.getDouble("sg"), (float) jLight.getDouble("sb"), (float) jLight.getDouble("sa"));
            light.getPosition().set((float) jLight.getDouble("px"), (float) jLight.getDouble("py"), (float) jLight.getDouble("pz"), (float) jLight.getDouble("pw"));
            light.setConstantAttenuation((float) jLight.getDouble("ca"));
            light.setLinearAttenuation((float) jLight.getDouble("la"));
            light.setQuadraticAttenuation((float) jLight.getDouble("qa"));
            light.getSpotTo().set((float) jLight.getDouble("stx"), (float) jLight.getDouble("sty"), (float) jLight.getDouble("stz"));
            light.getSpotDirection().set((float) jLight.getDouble("sdx"), (float) jLight.getDouble("sdy"), (float) jLight.getDouble("sdz"));
            light.setSpotExponent((float) jLight.getDouble("se"));
            light.setSpotCutOff((float) jLight.getDouble("sco"));
            light.setEnabled(jLight.getBoolean("e"));
        }
    }
    // entities
    level.getEntityLibrary().clear();
    JSONArray jModels = jRoot.getJSONArray("models");
    for (int i = 0; i < jModels.length(); i++) {
        JSONObject jModel = jModels.getJSONObject(i);
        // add model to library
        LevelEditorEntity levelEditorEntity = ModelMetaDataFileImport.doImportFromJSON(jModel.getInt("id"), new File(pathName).getCanonicalPath(), jModel.getJSONObject("entity"));
        // do we have a valid entity?
        if (levelEditorEntity == null) {
            throw new Exception("Invalid entity");
        }
        // add entity
        level.getEntityLibrary().addEntity(levelEditorEntity);
        // parse optional model properties
        if (jModel.has("properties")) {
            JSONArray jModelProperties = jModel.getJSONArray("properties");
            for (int j = 0; j < jModelProperties.length(); j++) {
                JSONObject jModelProperty = jModelProperties.getJSONObject(j);
                levelEditorEntity.addProperty(jModelProperty.getString("name"), jModelProperty.getString("value"));
            }
        }
    }
    // objects
    level.clearObjects();
    JSONArray jObjects = jRoot.getJSONArray("objects");
    for (int i = 0; i < jObjects.length(); i++) {
        JSONObject jObject = jObjects.getJSONObject(i);
        LevelEditorEntity model = level.getEntityLibrary().getEntity(jObject.getInt("mid"));
        Transformations transformations = new Transformations();
        transformations.getPivot().set(model.getPivot());
        transformations.getTranslation().set((float) jObject.getDouble("tx"), (float) jObject.getDouble("ty"), (float) jObject.getDouble("tz"));
        transformations.getScale().set((float) jObject.getDouble("sx"), (float) jObject.getDouble("sy"), (float) jObject.getDouble("sz"));
        Vector3 rotation = new Vector3((float) jObject.getDouble("rx"), (float) jObject.getDouble("ry"), (float) jObject.getDouble("rz"));
        transformations.getRotations().add(new Rotation(rotation.getArray()[level.getRotationOrder().getAxis0VectorIndex()], level.getRotationOrder().getAxis0()));
        transformations.getRotations().add(new Rotation(rotation.getArray()[level.getRotationOrder().getAxis1VectorIndex()], level.getRotationOrder().getAxis1()));
        transformations.getRotations().add(new Rotation(rotation.getArray()[level.getRotationOrder().getAxis2VectorIndex()], level.getRotationOrder().getAxis2()));
        transformations.update();
        LevelEditorObject levelEditorObject = new LevelEditorObject(objectIdPrefix != null ? objectIdPrefix + jObject.getString("id") : jObject.getString("id"), jObject.has("descr") ? jObject.getString("descr") : "", transformations, model);
        levelEditorObject.clearProperties();
        // parse optional object properties, new in LE 0.3a
        if (jObject.has("properties")) {
            JSONArray jObjectProperties = jObject.getJSONArray("properties");
            for (int j = 0; j < jObjectProperties.length(); j++) {
                JSONObject jObjectProperty = jObjectProperties.getJSONObject(j);
                levelEditorObject.addProperty(jObjectProperty.getString("name"), jObjectProperty.getString("value"));
            }
        }
        // check if entity already exists
        //	small workaround for a bug that would allow to place to objects at same place with same model
        /*
			boolean skipObject = false;
			for (int j = 0; j < level.getObjectCount(); j++) {
				LevelEditorObject _levelEditorObject = level.getObjectAt(j);
				if (_levelEditorObject.getModel() == levelEditorObject.getModel() &&
					_levelEditorObject.getTransformations().getTranslation().equals(levelEditorObject.getTransformations().getTranslation())) {
					System.out.println("Skipping '" + levelEditorObject + "' as we already have a object with this model and translation.");
					// we already have a object with selected model on this translation
					skipObject = true;
					break;
				}
			}

			// skip on object if requested
			if (skipObject == true) continue;
			*/
        // otherwise add
        level.addObject(levelEditorObject);
    }
    level.setObjectIdx(jRoot.getInt("objects_eidx"));
    level.setPathName(pathName);
    level.setFileName(fileName);
    level.computeDimension();
}
Also used : LevelEditorLight(net.drewke.tdme.tools.shared.model.LevelEditorLight) InputStream(java.io.InputStream) JSONArray(org.json.JSONArray) Vector3(net.drewke.tdme.math.Vector3) IOException(java.io.IOException) Rotation(net.drewke.tdme.engine.Rotation) IOException(java.io.IOException) JSONTokener(org.json.JSONTokener) JSONObject(org.json.JSONObject) Transformations(net.drewke.tdme.engine.Transformations) LevelEditorObject(net.drewke.tdme.tools.shared.model.LevelEditorObject) LevelEditorEntity(net.drewke.tdme.tools.shared.model.LevelEditorEntity) File(java.io.File)

Example 7 with Vector3

use of net.drewke.tdme.math.Vector3 in project tdme by andreasdr.

the class ModelMetaDataFileExport method exportToJSON.

/**
	 * Export model meta data file to JSON node
	 * @param entity
	 */
public static JSONObject exportToJSON(LevelEditorEntity entity) throws Exception {
    JSONObject jEntityRoot = new JSONObject();
    // re-convert to tm if having a model file
    if (entity.getType() == EntityType.MODEL && entity.getFileName() != null) {
        String modelPathName = Tools.getPath(entity.getFileName());
        String modelFileName = Tools.getFileName(entity.getFileName()) + (entity.getFileName().endsWith(".tm") == false ? ".tm" : "");
        TMWriter.write(entity.getModel(), modelPathName, modelFileName);
        jEntityRoot.put("file", modelPathName + "/" + modelFileName);
        // try to copy thumbnail
        try {
            String thumbnail = modelFileName + ".png";
            jEntityRoot.put("thumbnail", thumbnail);
            copyFile(new File("./tmp", entity.getThumbnail()), new File(Tools.getPath(entity.getFileName()), thumbnail));
        } catch (IOException ioe) {
            System.out.println("ModelMetaDataFileExport::export(): Could not copy thumbnail for '" + entity.getFileName() + "'");
        }
    }
    // general data
    jEntityRoot.put("version", "0.99");
    jEntityRoot.put("type", entity.getType());
    jEntityRoot.put("name", entity.getName());
    jEntityRoot.put("descr", entity.getDescription());
    jEntityRoot.put("px", entity.getPivot().getX());
    jEntityRoot.put("py", entity.getPivot().getY());
    jEntityRoot.put("pz", entity.getPivot().getZ());
    // export particle system
    if (entity.getType() == EntityType.PARTICLESYSTEM) {
        LevelEditorEntityParticleSystem particleSystem = entity.getParticleSystem();
        JSONObject jParticleSystem = new JSONObject();
        // type
        jParticleSystem.put("t", particleSystem.getType().toString());
        switch(particleSystem.getType()) {
            case NONE:
                {
                    break;
                }
            case OBJECT_PARTICLE_SYSTEM:
                {
                    JSONObject jObjectParticleSystem = new JSONObject();
                    // do we have a model file name?
                    if (particleSystem.getObjectParticleSystem().getModelFile() != null && particleSystem.getObjectParticleSystem().getModelFile().length() > 0) {
                        // yep, convert to .tm
                        String modelPathName = Tools.getPath(particleSystem.getObjectParticleSystem().getModelFile());
                        String modelFileName = Tools.getFileName(particleSystem.getObjectParticleSystem().getModelFile() + (particleSystem.getObjectParticleSystem().getModelFile().endsWith(".tm") == false ? ".tm" : ""));
                        TMWriter.write(particleSystem.getObjectParticleSystem().getModel(), modelPathName, modelFileName);
                        // and store to file
                        particleSystem.getObjectParticleSystem().setModelFile(modelPathName + "/" + modelFileName);
                    }
                    jObjectParticleSystem.put("mc", particleSystem.getObjectParticleSystem().getMaxCount());
                    jObjectParticleSystem.put("sx", particleSystem.getObjectParticleSystem().getScale().getX());
                    jObjectParticleSystem.put("sy", particleSystem.getObjectParticleSystem().getScale().getY());
                    jObjectParticleSystem.put("sz", particleSystem.getObjectParticleSystem().getScale().getZ());
                    jObjectParticleSystem.put("mf", particleSystem.getObjectParticleSystem().getModelFile());
                    jObjectParticleSystem.put("ae", particleSystem.getObjectParticleSystem().isAutoEmit());
                    jParticleSystem.put("ops", jObjectParticleSystem);
                    break;
                }
            case POINT_PARTICLE_SYSTEM:
                {
                    JSONObject jPointParticleSystem = new JSONObject();
                    jPointParticleSystem.put("mp", particleSystem.getPointParticleSystem().getMaxPoints());
                    jPointParticleSystem.put("ae", particleSystem.getPointParticleSystem().isAutoEmit());
                    jParticleSystem.put("pps", jPointParticleSystem);
                    break;
                }
            default:
                {
                    System.out.println("ModelMetaDataFileExport::export(): unknown particle system type '" + particleSystem.getType() + "'");
                    break;
                }
        }
        // emitter
        jParticleSystem.put("e", particleSystem.getEmitter().toString());
        switch(particleSystem.getEmitter()) {
            case NONE:
                {
                    break;
                }
            case POINT_PARTICLE_EMITTER:
                {
                    JSONObject jPointParticleEmitter = new JSONObject();
                    PointParticleEmitter emitter = particleSystem.getPointParticleEmitter();
                    jPointParticleEmitter.put("c", emitter.getCount());
                    jPointParticleEmitter.put("lt", emitter.getLifeTime());
                    jPointParticleEmitter.put("ltrnd", emitter.getLifeTimeRnd());
                    jPointParticleEmitter.put("m", emitter.getMass());
                    jPointParticleEmitter.put("mrnd", emitter.getMassRnd());
                    jPointParticleEmitter.put("px", emitter.getPosition().getX());
                    jPointParticleEmitter.put("py", emitter.getPosition().getY());
                    jPointParticleEmitter.put("pz", emitter.getPosition().getZ());
                    jPointParticleEmitter.put("vx", emitter.getVelocity().getX());
                    jPointParticleEmitter.put("vy", emitter.getVelocity().getY());
                    jPointParticleEmitter.put("vz", emitter.getVelocity().getZ());
                    jPointParticleEmitter.put("vrndx", emitter.getVelocityRnd().getX());
                    jPointParticleEmitter.put("vrndy", emitter.getVelocityRnd().getY());
                    jPointParticleEmitter.put("vrndz", emitter.getVelocityRnd().getZ());
                    jPointParticleEmitter.put("csr", emitter.getColorStart().getRed());
                    jPointParticleEmitter.put("csg", emitter.getColorStart().getGreen());
                    jPointParticleEmitter.put("csb", emitter.getColorStart().getBlue());
                    jPointParticleEmitter.put("csa", emitter.getColorStart().getAlpha());
                    jPointParticleEmitter.put("cer", emitter.getColorEnd().getRed());
                    jPointParticleEmitter.put("ceg", emitter.getColorEnd().getGreen());
                    jPointParticleEmitter.put("ceb", emitter.getColorEnd().getBlue());
                    jPointParticleEmitter.put("cea", emitter.getColorEnd().getAlpha());
                    jParticleSystem.put("ppe", jPointParticleEmitter);
                    break;
                }
            case BOUNDINGBOX_PARTICLE_EMITTER:
                {
                    JSONObject jBoundingBoxParticleEmitter = new JSONObject();
                    BoundingBoxParticleEmitter emitter = particleSystem.getBoundingBoxParticleEmitters();
                    jBoundingBoxParticleEmitter.put("c", emitter.getCount());
                    jBoundingBoxParticleEmitter.put("lt", emitter.getLifeTime());
                    jBoundingBoxParticleEmitter.put("ltrnd", emitter.getLifeTimeRnd());
                    jBoundingBoxParticleEmitter.put("m", emitter.getMass());
                    jBoundingBoxParticleEmitter.put("mrnd", emitter.getMassRnd());
                    jBoundingBoxParticleEmitter.put("vx", emitter.getVelocity().getX());
                    jBoundingBoxParticleEmitter.put("vy", emitter.getVelocity().getY());
                    jBoundingBoxParticleEmitter.put("vz", emitter.getVelocity().getZ());
                    jBoundingBoxParticleEmitter.put("vrndx", emitter.getVelocityRnd().getX());
                    jBoundingBoxParticleEmitter.put("vrndy", emitter.getVelocityRnd().getY());
                    jBoundingBoxParticleEmitter.put("vrndz", emitter.getVelocityRnd().getZ());
                    jBoundingBoxParticleEmitter.put("csr", emitter.getColorStart().getRed());
                    jBoundingBoxParticleEmitter.put("csg", emitter.getColorStart().getGreen());
                    jBoundingBoxParticleEmitter.put("csb", emitter.getColorStart().getBlue());
                    jBoundingBoxParticleEmitter.put("csa", emitter.getColorStart().getAlpha());
                    jBoundingBoxParticleEmitter.put("cer", emitter.getColorEnd().getRed());
                    jBoundingBoxParticleEmitter.put("ceg", emitter.getColorEnd().getGreen());
                    jBoundingBoxParticleEmitter.put("ceb", emitter.getColorEnd().getBlue());
                    jBoundingBoxParticleEmitter.put("cea", emitter.getColorEnd().getAlpha());
                    jBoundingBoxParticleEmitter.put("ocx", emitter.getObbCenter().getX());
                    jBoundingBoxParticleEmitter.put("ocy", emitter.getObbCenter().getY());
                    jBoundingBoxParticleEmitter.put("ocz", emitter.getObbCenter().getZ());
                    jBoundingBoxParticleEmitter.put("ohex", emitter.getObbHalfextension().getX());
                    jBoundingBoxParticleEmitter.put("ohey", emitter.getObbHalfextension().getY());
                    jBoundingBoxParticleEmitter.put("ohez", emitter.getObbHalfextension().getZ());
                    jBoundingBoxParticleEmitter.put("oa0x", emitter.getObbAxis0().getX());
                    jBoundingBoxParticleEmitter.put("oa0y", emitter.getObbAxis0().getY());
                    jBoundingBoxParticleEmitter.put("oa0z", emitter.getObbAxis0().getZ());
                    jBoundingBoxParticleEmitter.put("oa1x", emitter.getObbAxis1().getX());
                    jBoundingBoxParticleEmitter.put("oa1y", emitter.getObbAxis1().getY());
                    jBoundingBoxParticleEmitter.put("oa1z", emitter.getObbAxis1().getZ());
                    jBoundingBoxParticleEmitter.put("oa2x", emitter.getObbAxis2().getX());
                    jBoundingBoxParticleEmitter.put("oa2y", emitter.getObbAxis2().getY());
                    jBoundingBoxParticleEmitter.put("oa2z", emitter.getObbAxis2().getZ());
                    jParticleSystem.put("bbpe", jBoundingBoxParticleEmitter);
                    break;
                }
            case CIRCLE_PARTICLE_EMITTER:
                {
                    JSONObject jCircleParticleEmitter = new JSONObject();
                    CircleParticleEmitter emitter = particleSystem.getCircleParticleEmitter();
                    jCircleParticleEmitter.put("c", emitter.getCount());
                    jCircleParticleEmitter.put("lt", emitter.getLifeTime());
                    jCircleParticleEmitter.put("ltrnd", emitter.getLifeTimeRnd());
                    jCircleParticleEmitter.put("m", emitter.getMass());
                    jCircleParticleEmitter.put("mrnd", emitter.getMassRnd());
                    jCircleParticleEmitter.put("vx", emitter.getVelocity().getX());
                    jCircleParticleEmitter.put("vy", emitter.getVelocity().getY());
                    jCircleParticleEmitter.put("vz", emitter.getVelocity().getZ());
                    jCircleParticleEmitter.put("vrndx", emitter.getVelocityRnd().getX());
                    jCircleParticleEmitter.put("vrndy", emitter.getVelocityRnd().getY());
                    jCircleParticleEmitter.put("vrndz", emitter.getVelocityRnd().getZ());
                    jCircleParticleEmitter.put("csr", emitter.getColorStart().getRed());
                    jCircleParticleEmitter.put("csg", emitter.getColorStart().getGreen());
                    jCircleParticleEmitter.put("csb", emitter.getColorStart().getBlue());
                    jCircleParticleEmitter.put("csa", emitter.getColorStart().getAlpha());
                    jCircleParticleEmitter.put("cer", emitter.getColorEnd().getRed());
                    jCircleParticleEmitter.put("ceg", emitter.getColorEnd().getGreen());
                    jCircleParticleEmitter.put("ceb", emitter.getColorEnd().getBlue());
                    jCircleParticleEmitter.put("cea", emitter.getColorEnd().getAlpha());
                    jCircleParticleEmitter.put("cx", emitter.getCenter().getX());
                    jCircleParticleEmitter.put("cy", emitter.getCenter().getY());
                    jCircleParticleEmitter.put("cz", emitter.getCenter().getZ());
                    jCircleParticleEmitter.put("r", emitter.getRadius());
                    jCircleParticleEmitter.put("a0x", emitter.getAxis0().getX());
                    jCircleParticleEmitter.put("a0y", emitter.getAxis0().getY());
                    jCircleParticleEmitter.put("a0z", emitter.getAxis0().getZ());
                    jCircleParticleEmitter.put("a1x", emitter.getAxis1().getX());
                    jCircleParticleEmitter.put("a1y", emitter.getAxis1().getY());
                    jCircleParticleEmitter.put("a1z", emitter.getAxis1().getZ());
                    jParticleSystem.put("cpe", jCircleParticleEmitter);
                    break;
                }
            case CIRCLE_PARTICLE_EMITTER_PLANE_VELOCITY:
                {
                    JSONObject jCircleParticleEmitterPlaneVelocity = new JSONObject();
                    CircleParticleEmitterPlaneVelocity emitter = particleSystem.getCircleParticleEmitterPlaneVelocity();
                    jCircleParticleEmitterPlaneVelocity.put("c", emitter.getCount());
                    jCircleParticleEmitterPlaneVelocity.put("lt", emitter.getLifeTime());
                    jCircleParticleEmitterPlaneVelocity.put("ltrnd", emitter.getLifeTimeRnd());
                    jCircleParticleEmitterPlaneVelocity.put("m", emitter.getMass());
                    jCircleParticleEmitterPlaneVelocity.put("mrnd", emitter.getMassRnd());
                    jCircleParticleEmitterPlaneVelocity.put("v", emitter.getVelocity());
                    jCircleParticleEmitterPlaneVelocity.put("vrnd", emitter.getVelocityRnd());
                    jCircleParticleEmitterPlaneVelocity.put("csr", emitter.getColorStart().getRed());
                    jCircleParticleEmitterPlaneVelocity.put("csg", emitter.getColorStart().getGreen());
                    jCircleParticleEmitterPlaneVelocity.put("csb", emitter.getColorStart().getBlue());
                    jCircleParticleEmitterPlaneVelocity.put("csa", emitter.getColorStart().getAlpha());
                    jCircleParticleEmitterPlaneVelocity.put("cer", emitter.getColorEnd().getRed());
                    jCircleParticleEmitterPlaneVelocity.put("ceg", emitter.getColorEnd().getGreen());
                    jCircleParticleEmitterPlaneVelocity.put("ceb", emitter.getColorEnd().getBlue());
                    jCircleParticleEmitterPlaneVelocity.put("cea", emitter.getColorEnd().getAlpha());
                    jCircleParticleEmitterPlaneVelocity.put("cx", emitter.getCenter().getX());
                    jCircleParticleEmitterPlaneVelocity.put("cy", emitter.getCenter().getY());
                    jCircleParticleEmitterPlaneVelocity.put("cz", emitter.getCenter().getZ());
                    jCircleParticleEmitterPlaneVelocity.put("r", emitter.getRadius());
                    jCircleParticleEmitterPlaneVelocity.put("a0x", emitter.getAxis0().getX());
                    jCircleParticleEmitterPlaneVelocity.put("a0y", emitter.getAxis0().getY());
                    jCircleParticleEmitterPlaneVelocity.put("a0z", emitter.getAxis0().getZ());
                    jCircleParticleEmitterPlaneVelocity.put("a1x", emitter.getAxis1().getX());
                    jCircleParticleEmitterPlaneVelocity.put("a1y", emitter.getAxis1().getY());
                    jCircleParticleEmitterPlaneVelocity.put("a1z", emitter.getAxis1().getZ());
                    jParticleSystem.put("cpeev", jCircleParticleEmitterPlaneVelocity);
                    break;
                }
            case SPHERE_PARTICLE_EMITTER:
                {
                    JSONObject jSphereParticleEmitter = new JSONObject();
                    SphereParticleEmitter emitter = particleSystem.getSphereParticleEmitter();
                    jSphereParticleEmitter.put("c", emitter.getCount());
                    jSphereParticleEmitter.put("lt", emitter.getLifeTime());
                    jSphereParticleEmitter.put("ltrnd", emitter.getLifeTimeRnd());
                    jSphereParticleEmitter.put("m", emitter.getMass());
                    jSphereParticleEmitter.put("mrnd", emitter.getMassRnd());
                    jSphereParticleEmitter.put("vx", emitter.getVelocity().getX());
                    jSphereParticleEmitter.put("vy", emitter.getVelocity().getY());
                    jSphereParticleEmitter.put("vz", emitter.getVelocity().getZ());
                    jSphereParticleEmitter.put("vrndx", emitter.getVelocityRnd().getX());
                    jSphereParticleEmitter.put("vrndy", emitter.getVelocityRnd().getY());
                    jSphereParticleEmitter.put("vrndz", emitter.getVelocityRnd().getZ());
                    jSphereParticleEmitter.put("csr", emitter.getColorStart().getRed());
                    jSphereParticleEmitter.put("csg", emitter.getColorStart().getGreen());
                    jSphereParticleEmitter.put("csb", emitter.getColorStart().getBlue());
                    jSphereParticleEmitter.put("csa", emitter.getColorStart().getAlpha());
                    jSphereParticleEmitter.put("cer", emitter.getColorEnd().getRed());
                    jSphereParticleEmitter.put("ceg", emitter.getColorEnd().getGreen());
                    jSphereParticleEmitter.put("ceb", emitter.getColorEnd().getBlue());
                    jSphereParticleEmitter.put("cea", emitter.getColorEnd().getAlpha());
                    jSphereParticleEmitter.put("cx", emitter.getCenter().getX());
                    jSphereParticleEmitter.put("cy", emitter.getCenter().getY());
                    jSphereParticleEmitter.put("cz", emitter.getCenter().getZ());
                    jSphereParticleEmitter.put("r", emitter.getRadius());
                    jParticleSystem.put("spe", jSphereParticleEmitter);
                    break;
                }
            default:
                System.out.println("ModelMetaDataFileExport::export(): unknown particle system emitter '" + particleSystem.getEmitter() + "'");
        }
        // add to file
        jEntityRoot.put("ps", jParticleSystem);
    }
    // bounding volume
    JSONArray jBoundingVolumes = new JSONArray();
    for (int i = 0; i < entity.getBoundingVolumeCount(); i++) {
        LevelEditorEntityBoundingVolume entityBoundingVolume = entity.getBoundingVolumeAt(i);
        BoundingVolume bv = entityBoundingVolume.getBoundingVolume();
        if (bv == null)
            continue;
        JSONObject jBoundingVolume = new JSONObject();
        if (bv == null) {
            jBoundingVolume.put("type", "none");
            jBoundingVolumes.put(jBoundingVolume);
        } else if (bv instanceof Sphere) {
            Sphere sphere = (Sphere) bv;
            jBoundingVolume.put("type", "sphere");
            jBoundingVolume.put("cx", sphere.getCenter().getX());
            jBoundingVolume.put("cy", sphere.getCenter().getY());
            jBoundingVolume.put("cz", sphere.getCenter().getZ());
            jBoundingVolume.put("r", sphere.getRadius());
            jBoundingVolumes.put(jBoundingVolume);
        } else if (bv instanceof Capsule) {
            Capsule capsule = (Capsule) bv;
            jBoundingVolume.put("type", "capsule");
            jBoundingVolume.put("ax", capsule.getA().getX());
            jBoundingVolume.put("ay", capsule.getA().getY());
            jBoundingVolume.put("az", capsule.getA().getZ());
            jBoundingVolume.put("bx", capsule.getB().getX());
            jBoundingVolume.put("by", capsule.getB().getY());
            jBoundingVolume.put("bz", capsule.getB().getZ());
            jBoundingVolume.put("r", capsule.getRadius());
            jBoundingVolumes.put(jBoundingVolume);
        } else if (bv instanceof BoundingBox) {
            BoundingBox aabb = (BoundingBox) bv;
            jBoundingVolume.put("type", "aabb");
            jBoundingVolume.put("mix", aabb.getMin().getX());
            jBoundingVolume.put("miy", aabb.getMin().getY());
            jBoundingVolume.put("miz", aabb.getMin().getZ());
            jBoundingVolume.put("max", aabb.getMax().getX());
            jBoundingVolume.put("may", aabb.getMax().getY());
            jBoundingVolume.put("maz", aabb.getMax().getZ());
            jBoundingVolumes.put(jBoundingVolume);
        } else if (bv instanceof OrientedBoundingBox) {
            OrientedBoundingBox obb = (OrientedBoundingBox) bv;
            jBoundingVolume.put("type", "obb");
            jBoundingVolume.put("cx", obb.getCenter().getX());
            jBoundingVolume.put("cy", obb.getCenter().getY());
            jBoundingVolume.put("cz", obb.getCenter().getZ());
            jBoundingVolume.put("a0x", obb.getAxes()[0].getX());
            jBoundingVolume.put("a0y", obb.getAxes()[0].getY());
            jBoundingVolume.put("a0z", obb.getAxes()[0].getZ());
            jBoundingVolume.put("a1x", obb.getAxes()[1].getX());
            jBoundingVolume.put("a1y", obb.getAxes()[1].getY());
            jBoundingVolume.put("a1z", obb.getAxes()[1].getZ());
            jBoundingVolume.put("a2x", obb.getAxes()[2].getX());
            jBoundingVolume.put("a2y", obb.getAxes()[2].getY());
            jBoundingVolume.put("a2z", obb.getAxes()[2].getZ());
            jBoundingVolume.put("hex", obb.getHalfExtension().getX());
            jBoundingVolume.put("hey", obb.getHalfExtension().getY());
            jBoundingVolume.put("hez", obb.getHalfExtension().getZ());
            jBoundingVolumes.put(jBoundingVolume);
        } else if (bv instanceof ConvexMesh) {
            ConvexMesh mesh = (ConvexMesh) bv;
            jBoundingVolume.put("type", "convexmesh");
            jBoundingVolume.put("file", entityBoundingVolume.getModelMeshFile());
            JSONArray jMeshTriangles = new JSONArray();
            int triangleIdx = 0;
            for (Triangle triangle : mesh.getTriangles()) {
                JSONArray jMeshTriangleVertices = new JSONArray();
                int vertexIdx = 0;
                for (Vector3 vertex : triangle.getVertices()) {
                    JSONArray jMeshTriangleVertex = new JSONArray();
                    for (int vcIdx = 0; vcIdx < 3; vcIdx++) {
                        jMeshTriangleVertex.put(vcIdx, vertex.getArray()[vcIdx]);
                    }
                    jMeshTriangleVertices.put(vertexIdx++, jMeshTriangleVertex);
                }
                jMeshTriangles.put(triangleIdx++, jMeshTriangleVertices);
            }
            jBoundingVolume.put("t", jMeshTriangles);
            jBoundingVolumes.put(jBoundingVolume);
        }
    }
    jEntityRoot.put("bvs", jBoundingVolumes);
    // model properties
    JSONArray jModelProperties = new JSONArray();
    for (PropertyModelClass modelProperty : entity.getProperties()) {
        JSONObject jObjectProperty = new JSONObject();
        jObjectProperty.put("name", modelProperty.getName());
        jObjectProperty.put("value", modelProperty.getValue());
        jModelProperties.put(jObjectProperty);
    }
    jEntityRoot.put("properties", jModelProperties);
    //
    return jEntityRoot;
}
Also used : LevelEditorEntityBoundingVolume(net.drewke.tdme.tools.shared.model.LevelEditorEntityBoundingVolume) PropertyModelClass(net.drewke.tdme.tools.shared.model.PropertyModelClass) CircleParticleEmitterPlaneVelocity(net.drewke.tdme.tools.shared.model.LevelEditorEntityParticleSystem.CircleParticleEmitterPlaneVelocity) JSONArray(org.json.JSONArray) Triangle(net.drewke.tdme.engine.primitives.Triangle) CircleParticleEmitter(net.drewke.tdme.tools.shared.model.LevelEditorEntityParticleSystem.CircleParticleEmitter) Capsule(net.drewke.tdme.engine.primitives.Capsule) ConvexMesh(net.drewke.tdme.engine.primitives.ConvexMesh) Vector3(net.drewke.tdme.math.Vector3) IOException(java.io.IOException) LevelEditorEntityParticleSystem(net.drewke.tdme.tools.shared.model.LevelEditorEntityParticleSystem) Sphere(net.drewke.tdme.engine.primitives.Sphere) OrientedBoundingBox(net.drewke.tdme.engine.primitives.OrientedBoundingBox) JSONObject(org.json.JSONObject) BoundingBox(net.drewke.tdme.engine.primitives.BoundingBox) OrientedBoundingBox(net.drewke.tdme.engine.primitives.OrientedBoundingBox) LevelEditorEntityBoundingVolume(net.drewke.tdme.tools.shared.model.LevelEditorEntityBoundingVolume) BoundingVolume(net.drewke.tdme.engine.primitives.BoundingVolume) BoundingBoxParticleEmitter(net.drewke.tdme.tools.shared.model.LevelEditorEntityParticleSystem.BoundingBoxParticleEmitter) File(java.io.File) SphereParticleEmitter(net.drewke.tdme.tools.shared.model.LevelEditorEntityParticleSystem.SphereParticleEmitter) PointParticleEmitter(net.drewke.tdme.tools.shared.model.LevelEditorEntityParticleSystem.PointParticleEmitter)

Example 8 with Vector3

use of net.drewke.tdme.math.Vector3 in project tdme by andreasdr.

the class Tools method createGroundModel.

/**
	 * Creates a ground plate
	 * @param width
	 * @param depth
	 * @param float y
	 * @return ground model
	 */
public static Model createGroundModel(float width, float depth, float y) {
    // ground model
    Model ground = new Model("ground", "ground", UpVector.Y_UP, RotationOrder.XYZ, null);
    //	material
    Material groundMaterial = new Material("ground");
    groundMaterial.getSpecularColor().set(0f, 0f, 0f, 1f);
    ground.getMaterials().put("ground", groundMaterial);
    //	group
    Group groundGroup = new Group(ground, null, "ground", "ground");
    //	faces entity
    //		ground
    FacesEntity groupFacesEntityGround = new FacesEntity(groundGroup, "ground group faces entity ground");
    groupFacesEntityGround.setMaterial(groundMaterial);
    //	faces entity 
    ArrayList<FacesEntity> groupFacesEntities = new ArrayList<FacesEntity>();
    groupFacesEntities.add(groupFacesEntityGround);
    //	vertices
    ArrayList<Vector3> groundVertices = new ArrayList<Vector3>();
    // left, near, ground
    groundVertices.add(new Vector3(-width, y, -depth));
    // left, far, ground
    groundVertices.add(new Vector3(-width, y, +depth));
    // right far, ground
    groundVertices.add(new Vector3(+width, y, +depth));
    // right, near, ground
    groundVertices.add(new Vector3(+width, y, -depth));
    //	normals
    ArrayList<Vector3> groundNormals = new ArrayList<Vector3>();
    //		ground
    groundNormals.add(new Vector3(0f, 1f, 0f));
    // texture coordinates
    ArrayList<TextureCoordinate> groundTextureCoordinates = new ArrayList<TextureCoordinate>();
    groundTextureCoordinates.add(new TextureCoordinate(0f, 0f));
    groundTextureCoordinates.add(new TextureCoordinate(0f, 1f));
    groundTextureCoordinates.add(new TextureCoordinate(1f, 1f));
    groundTextureCoordinates.add(new TextureCoordinate(1f, 0f));
    //	faces ground
    ArrayList<Face> groundFacesGround = new ArrayList<Face>();
    groundFacesGround.add(new Face(groundGroup, 0, 1, 2, 0, 0, 0, 0, 1, 2));
    groundFacesGround.add(new Face(groundGroup, 2, 3, 0, 0, 0, 0, 2, 3, 0));
    // set up faces entity
    groupFacesEntityGround.setFaces(groundFacesGround);
    // setup ground group
    groundGroup.setVertices(groundVertices);
    groundGroup.setNormals(groundNormals);
    groundGroup.setTextureCoordinates(groundTextureCoordinates);
    groundGroup.setFacesEntities(groupFacesEntities);
    // register group
    ground.getGroups().put("ground", groundGroup);
    ground.getSubGroups().put("ground", groundGroup);
    // prepare for indexed rendering
    ModelHelper.prepareForIndexedRendering(ground);
    //
    return ground;
}
Also used : FacesEntity(net.drewke.tdme.engine.model.FacesEntity) Group(net.drewke.tdme.engine.model.Group) Model(net.drewke.tdme.engine.model.Model) ArrayList(java.util.ArrayList) Material(net.drewke.tdme.engine.model.Material) Vector3(net.drewke.tdme.math.Vector3) TextureCoordinate(net.drewke.tdme.engine.model.TextureCoordinate) Face(net.drewke.tdme.engine.model.Face)

Example 9 with Vector3

use of net.drewke.tdme.math.Vector3 in project tdme by andreasdr.

the class Tools method setDefaultLight.

/**
	 * Set up given engine light with default light
	 * @param light
	 */
public static void setDefaultLight(Light light) {
    light.getAmbient().set(1.0f, 1.0f, 1.0f, 1.0f);
    light.getDiffuse().set(0.5f, 0.5f, 0.5f, 1f);
    light.getSpecular().set(1f, 1f, 1f, 1f);
    light.getPosition().set(0f, 20000f, 0f, 1f);
    light.getSpotDirection().set(0f, 0f, 0f).sub(new Vector3(light.getPosition().getArray()));
    light.setConstantAttenuation(0.5f);
    light.setLinearAttenuation(0f);
    light.setQuadraticAttenuation(0f);
    light.setSpotExponent(0f);
    light.setSpotCutOff(180f);
    light.setEnabled(true);
}
Also used : Vector3(net.drewke.tdme.math.Vector3)

Example 10 with Vector3

use of net.drewke.tdme.math.Vector3 in project tdme by andreasdr.

the class CameraRotationInputHandler method handleInputEvents.

/*
	 * (non-Javadoc)
	 * @see net.drewke.tdme.gui.events.GUIInputEventHandler#handleInputEvents()
	 */
public void handleInputEvents() {
    // handle mouse events
    for (int i = 0; i < engine.getGUI().getMouseEvents().size(); i++) {
        GUIMouseEvent event = engine.getGUI().getMouseEvents().get(i);
        // skip on processed events
        if (event.isProcessed() == true)
            continue;
        // dragging
        if (mouseDragging == true) {
            if (event.getButton() == 1) {
                float xMoved = (event.getX() - mouseLastX) / 5f;
                float yMoved = (event.getY() - mouseLastY) / 5f;
                mouseLastX = event.getX();
                mouseLastY = event.getY();
                Rotation xRotation = lookFromRotations.getRotations().get(0);
                Rotation yRotation = lookFromRotations.getRotations().get(1);
                float xRotationAngle = xRotation.getAngle() + xMoved;
                float yRotationAngle = yRotation.getAngle() + yMoved;
                xRotation.setAngle(xRotationAngle);
                yRotation.setAngle(yRotationAngle);
                lookFromRotations.update();
            } else {
                mouseDragging = false;
            }
        } else {
            if (event.getButton() == 1) {
                mouseDragging = true;
                mouseLastX = event.getX();
                mouseLastY = event.getY();
            }
        }
        // process mouse wheel events
        float mouseWheel = event.getWheelY();
        if (mouseWheel != 0) {
            scale += mouseWheel * 0.05f;
            if (scale < 0.05f)
                scale = 0.05f;
        }
    }
    // handle keyboard events
    for (int i = 0; i < engine.getGUI().getKeyboardEvents().size(); i++) {
        GUIKeyboardEvent event = engine.getGUI().getKeyboardEvents().get(i);
        // skip on processed events
        if (event.isProcessed() == true)
            continue;
        //
        boolean isKeyDown = event.getType() == Type.KEY_PRESSED;
        if (event.getKeyCode() == GUIKeyboardEvent.KEYCODE_LEFT)
            keyLeft = isKeyDown;
        if (event.getKeyCode() == GUIKeyboardEvent.KEYCODE_RIGHT)
            keyRight = isKeyDown;
        if (event.getKeyCode() == GUIKeyboardEvent.KEYCODE_UP)
            keyUp = isKeyDown;
        if (event.getKeyCode() == GUIKeyboardEvent.KEYCODE_DOWN)
            keyDown = isKeyDown;
        if (Character.toLowerCase(event.getKeyChar()) == '.')
            keyPeriod = isKeyDown;
        if (Character.toLowerCase(event.getKeyChar()) == ',')
            keyComma = isKeyDown;
        if (Character.toLowerCase(event.getKeyChar()) == '+')
            keyPlus = isKeyDown;
        if (Character.toLowerCase(event.getKeyChar()) == '-')
            keyMinus = isKeyDown;
        if (Character.toLowerCase(event.getKeyChar()) == 'r')
            keyR = isKeyDown;
    }
    //
    // handle keyboard input
    //	get rotations
    Rotation rotationX = lookFromRotations.getRotations().get(0);
    Rotation rotationY = lookFromRotations.getRotations().get(1);
    Rotation rotationZ = lookFromRotations.getRotations().get(2);
    // 	transfer keyboard inputs to rotations
    if (keyLeft)
        rotationX.setAngle(rotationX.getAngle() - 1f);
    if (keyRight)
        rotationX.setAngle(rotationX.getAngle() + 1f);
    if (keyUp)
        rotationY.setAngle(rotationY.getAngle() + 1f);
    if (keyDown)
        rotationY.setAngle(rotationY.getAngle() - 1f);
    if (keyComma)
        rotationZ.setAngle(rotationZ.getAngle() - 1f);
    if (keyPeriod)
        rotationZ.setAngle(rotationZ.getAngle() + 1f);
    if (keyMinus)
        scale += 0.05f;
    if (keyPlus && scale > 0.05f)
        scale -= 0.05f;
    if (keyR == true || resetRequested == true) {
        rotationY.setAngle(-45f);
        rotationZ.setAngle(0f);
        scale = 1.0f;
    }
    // 	update transformations if key was pressed
    if (keyLeft || keyRight || keyUp || keyDown || keyComma || keyPeriod || keyR || resetRequested) {
        lookFromRotations.update();
        resetRequested = false;
    }
    // set up cam
    Camera cam = engine.getCamera();
    // we have a fixed look at
    Vector3 lookAt = cam.getLookAt();
    // look at -> look to vector
    Vector3 lookAtToFromVector = new Vector3(0f, 0f, +(maxAxisDimension * 1.2f));
    // apply look from rotations
    // apply look from rotations
    Vector3 lookAtToFromVectorTransformed = new Vector3();
    Vector3 lookAtToFromVectorScaled = new Vector3();
    Vector3 upVector = new Vector3();
    lookFromRotations.getTransformationsMatrix().multiply(lookAtToFromVector, lookAtToFromVectorTransformed);
    lookAtToFromVectorScaled.set(lookAtToFromVectorTransformed).scale(scale);
    lookFromRotations.getRotations().get(2).getQuaternion().multiply(new Vector3(0f, 1f, 0f), upVector);
    /*
		Vector3 forwardVector = lookAtToFromVectorTransformed.clone().scale(-1f);
		Vector3 sideVector = Vector3.computeCrossProduct(forwardVector, upVector);

		if (keyA) camLookAt.sub(sideVector.clone().scale(0.05f));
		if (keyD) camLookAt.add(sideVector.clone().scale(0.05f));
		if (keyW) camLookAt.add(upVector.clone().scale(0.05f * forwardVector.computeLength()));
		if (keyS) camLookAt.sub(upVector.clone().scale(0.05f * forwardVector.computeLength()));
		*/
    // look from with rotations
    Vector3 lookFrom = lookAt.clone().add(lookAtToFromVectorScaled);
    cam.getLookFrom().set(lookFrom);
    // up vector
    cam.getUpVector().set(upVector);
}
Also used : GUIKeyboardEvent(net.drewke.tdme.gui.events.GUIKeyboardEvent) Vector3(net.drewke.tdme.math.Vector3) Camera(net.drewke.tdme.engine.Camera) Rotation(net.drewke.tdme.engine.Rotation) GUIMouseEvent(net.drewke.tdme.gui.events.GUIMouseEvent)

Aggregations

Vector3 (net.drewke.tdme.math.Vector3)75 FacesEntity (net.drewke.tdme.engine.model.FacesEntity)20 Model (net.drewke.tdme.engine.model.Model)20 LevelEditorEntity (net.drewke.tdme.tools.shared.model.LevelEditorEntity)14 ArrayList (java.util.ArrayList)13 Rotation (net.drewke.tdme.engine.Rotation)12 Face (net.drewke.tdme.engine.model.Face)12 BoundingBox (net.drewke.tdme.engine.primitives.BoundingBox)11 Material (net.drewke.tdme.engine.model.Material)10 BoundingVolume (net.drewke.tdme.engine.primitives.BoundingVolume)10 Group (net.drewke.tdme.engine.model.Group)9 PrimitiveModel (net.drewke.tdme.engine.primitives.PrimitiveModel)9 Entity (net.drewke.tdme.engine.Entity)8 Object3D (net.drewke.tdme.engine.Object3D)8 LevelEditorObject (net.drewke.tdme.tools.shared.model.LevelEditorObject)8 File (java.io.File)7 IOException (java.io.IOException)7 Transformations (net.drewke.tdme.engine.Transformations)7 OrientedBoundingBox (net.drewke.tdme.engine.primitives.OrientedBoundingBox)7 Camera (net.drewke.tdme.engine.Camera)6