Search in sources :

Example 61 with Vector3

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

the class Tools method setupEntity.

/**
	 * Set up entity in given engine with look from rotations and scale
	 * @param entity
	 * @param engine
	 * @param look from rotations
	 * @param scale
	 */
public static void setupEntity(LevelEditorEntity entity, Engine engine, Transformations lookFromRotations, float scale) {
    if (entity == null)
        return;
    // entity bounding box
    BoundingBox entityBoundingBox = null;
    // add model to engine
    if (entity.getType() == EntityType.PARTICLESYSTEM) {
        entityBoundingBox = new BoundingBox(new Vector3(-0.5f, 0f, -0.5f), new Vector3(0.5f, 3f, 0.5f));
        Entity particleSystemObject = Level.createParticleSystem(entity.getParticleSystem(), "model", true);
        if (particleSystemObject != null) {
            engine.addEntity(particleSystemObject);
        }
    } else {
        entityBoundingBox = entity.getModel().getBoundingBox();
        Object3D modelObject = new Object3D("model", entity.getModel());
        modelObject.setDynamicShadowingEnabled(true);
        engine.addEntity(modelObject);
    }
    // create ground object
    Model ground = createGroundModel((entityBoundingBox.getMax().getX() - entityBoundingBox.getMin().getX()) * 1f, (entityBoundingBox.getMax().getZ() - entityBoundingBox.getMin().getZ()) * 1f, entityBoundingBox.getMin().getY() - MathTools.EPSILON);
    // add ground to engine
    Object3D groundObject = new Object3D("ground", ground);
    groundObject.setEnabled(false);
    engine.addEntity(groundObject);
    // add bounding volume if we have any
    for (int i = 0; i < entity.getBoundingVolumeCount(); i++) {
        LevelEditorEntityBoundingVolume boundingVolume = entity.getBoundingVolumeAt(i);
        if (boundingVolume.getModel() == null)
            continue;
        Object3D modelBoundingVolumeObject = new Object3D("model_bv." + i, boundingVolume.getModel());
        modelBoundingVolumeObject.setEnabled(false);
        engine.addEntity(modelBoundingVolumeObject);
    }
    // set up lights
    for (Light light : engine.getLights()) light.setEnabled(false);
    Light light0 = engine.getLightAt(0);
    light0.getAmbient().set(1.0f, 1.0f, 1.0f, 1.0f);
    light0.getDiffuse().set(0.5f, 0.5f, 0.5f, 1f);
    light0.getSpecular().set(1f, 1f, 1f, 1f);
    light0.getPosition().set(entityBoundingBox.getMin().getX() + ((entityBoundingBox.getMax().getX() - entityBoundingBox.getMin().getX()) / 2f), //modelBoundingBox.getMax().getY(),
    entityBoundingBox.getMin().getY() + ((entityBoundingBox.getMax().getY() - entityBoundingBox.getMin().getY()) / 2f), -entityBoundingBox.getMin().getZ() * 4f, 1f);
    light0.getSpotDirection().set(0f, 0f, 0f).sub(new Vector3(light0.getPosition().getArray()));
    light0.setConstantAttenuation(0.5f);
    light0.setLinearAttenuation(0f);
    light0.setQuadraticAttenuation(0f);
    light0.setSpotExponent(0f);
    light0.setSpotCutOff(180f);
    light0.setEnabled(true);
    // model dimension
    Vector3 dimension = entityBoundingBox.getMax().clone().sub(entityBoundingBox.getMin());
    // determine max dimension on each axis
    float maxAxisDimension = computeMaxAxisDimension(entityBoundingBox);
    // set up cam
    Camera cam = engine.getCamera();
    cam.setZNear(maxAxisDimension / 5000f);
    cam.setZFar(maxAxisDimension);
    // look at
    Vector3 lookAt = entityBoundingBox.getMin().clone().add(dimension.clone().scale(0.5f));
    cam.getLookAt().set(lookAt);
    // look at -> look to vector
    Vector3 lookAtToFromVector = new Vector3(0f, 0f, +(maxAxisDimension * 1.2f));
    // 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);
    // look from with rotations
    Vector3 lookFrom = lookAt.clone().add(lookAtToFromVectorScaled);
    cam.getLookFrom().set(lookFrom);
    // up vector
    cam.getUpVector().set(upVector);
}
Also used : LevelEditorEntityBoundingVolume(net.drewke.tdme.tools.shared.model.LevelEditorEntityBoundingVolume) Entity(net.drewke.tdme.engine.Entity) LevelEditorEntity(net.drewke.tdme.tools.shared.model.LevelEditorEntity) FacesEntity(net.drewke.tdme.engine.model.FacesEntity) Light(net.drewke.tdme.engine.Light) BoundingBox(net.drewke.tdme.engine.primitives.BoundingBox) Model(net.drewke.tdme.engine.model.Model) Vector3(net.drewke.tdme.math.Vector3) Camera(net.drewke.tdme.engine.Camera) Object3D(net.drewke.tdme.engine.Object3D)

Example 62 with Vector3

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

the class LevelEditorView method objectTranslationApply.

/**
	 * Apply object translation
	 * @param x
	 * @param y
	 * @param z
	 */
public void objectTranslationApply(float x, float y, float z) {
    if (selectedObjects.size() == 0)
        return;
    // handle single object
    if (selectedObjects.size() == 1) {
        Entity selectedObject = selectedObjects.get(0);
        LevelEditorObject currentEntity = level.getObjectById(selectedObject.getId());
        if (currentEntity == null)
            return;
        currentEntity.getTransformations().getTranslation().set(x, y, z);
        currentEntity.getTransformations().update();
        selectedObject.fromTransformations(currentEntity.getTransformations());
    } else if (selectedObjects.size() > 1) {
        // multiple objects
        for (Entity selectedObject : selectedObjects) {
            LevelEditorObject currentEntity = level.getObjectById(selectedObject.getId());
            if (currentEntity == null)
                continue;
            currentEntity.getTransformations().getTranslation().add(new Vector3(x, y, z));
            currentEntity.getTransformations().update();
            selectedObject.fromTransformations(currentEntity.getTransformations());
        }
        // reset controller object properties
        levelEditorScreenController.setObject(new Vector3(0f, 0f, 0f), new Vector3(1f, 1f, 1f), 0f, 0f, 0f);
    }
    level.computeDimension();
    updateGUIElements();
}
Also used : Entity(net.drewke.tdme.engine.Entity) LevelEditorEntity(net.drewke.tdme.tools.shared.model.LevelEditorEntity) FacesEntity(net.drewke.tdme.engine.model.FacesEntity) Vector3(net.drewke.tdme.math.Vector3) LevelEditorObject(net.drewke.tdme.tools.shared.model.LevelEditorObject)

Example 63 with Vector3

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

the class Tools method oseInit.

/**
	 * Init off screen engine for making thumbails
	 * @param drawable
	 */
public static void oseInit(GLAutoDrawable drawable) {
    osEngine = Engine.createOffScreenInstance(drawable, 128, 128);
    osEngine.setPartition(new PartitionNone());
    setDefaultLight(osEngine.getLightAt(0));
    oseScale = 0.75f;
    oseLookFromRotations = new Transformations();
    oseLookFromRotations.getRotations().add(new Rotation(-45f, new Vector3(0f, 1f, 0f)));
    oseLookFromRotations.getRotations().add(new Rotation(-45f, new Vector3(1f, 0f, 0f)));
    oseLookFromRotations.getRotations().add(new Rotation(0f, new Vector3(0f, 0f, 1f)));
    oseLookFromRotations.update();
}
Also used : PartitionNone(net.drewke.tdme.engine.PartitionNone) Transformations(net.drewke.tdme.engine.Transformations) Vector3(net.drewke.tdme.math.Vector3) Rotation(net.drewke.tdme.engine.Rotation)

Example 64 with Vector3

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

the class EntityBoundingVolumeView method resetBoundingVolume.

/**
	 * Reset bounding volume
	 * @param entity
	 * @param idx
	 */
public void resetBoundingVolume(LevelEditorEntity entity, int idx) {
    // determine AABB
    BoundingBox aabb = null;
    // if we have a model we also have a AABB
    if (entity.getModel() != null) {
        aabb = entity.getModel().getBoundingBox();
    } else {
        // otherwise just create one for now
        // this applies currently for particle systems
        // TODO: check if particle system
        aabb = new BoundingBox(new Vector3(-0.5f, 0f, -0.5f), new Vector3(0.5f, 3f, 0.5f));
    }
    // set up oriented bounding box
    OrientedBoundingBox obb = new OrientedBoundingBox(aabb);
    // set up sphere
    modelViewerScreenController.setupSphere(idx, obb.getCenter(), obb.getHalfExtension().computeLength());
    // set up capsule
    {
        Vector3 a = new Vector3();
        Vector3 b = new Vector3();
        float radius = 0.0f;
        float[] halfExtensionXYZ = obb.getHalfExtension().getArray();
        // determine a, b
        if (halfExtensionXYZ[0] > halfExtensionXYZ[1] && halfExtensionXYZ[0] > halfExtensionXYZ[2]) {
            radius = (float) Math.sqrt(halfExtensionXYZ[1] * halfExtensionXYZ[1] + halfExtensionXYZ[2] * halfExtensionXYZ[2]);
            a.set(obb.getAxes()[0]);
            a.scale(-(halfExtensionXYZ[0] - radius));
            a.add(obb.getCenter());
            b.set(obb.getAxes()[0]);
            b.scale(+(halfExtensionXYZ[0] - radius));
            b.add(obb.getCenter());
        } else if (halfExtensionXYZ[1] > halfExtensionXYZ[0] && halfExtensionXYZ[1] > halfExtensionXYZ[2]) {
            radius = (float) Math.sqrt(halfExtensionXYZ[0] * halfExtensionXYZ[0] + halfExtensionXYZ[2] * halfExtensionXYZ[2]);
            a.set(obb.getAxes()[1]);
            a.scale(-(halfExtensionXYZ[1] - radius));
            a.add(obb.getCenter());
            b.set(obb.getAxes()[1]);
            b.scale(+(halfExtensionXYZ[1] - radius));
            b.add(obb.getCenter());
        } else {
            radius = (float) Math.sqrt(halfExtensionXYZ[0] * halfExtensionXYZ[0] + halfExtensionXYZ[1] * halfExtensionXYZ[1]);
            a.set(obb.getAxes()[2]);
            a.scale(-(halfExtensionXYZ[2] - radius));
            a.add(obb.getCenter());
            b.set(obb.getAxes()[2]);
            b.scale(+(halfExtensionXYZ[2] - radius));
            b.add(obb.getCenter());
        }
        // setup capsule
        modelViewerScreenController.setupCapsule(idx, a, b, radius);
    }
    // set up AABB bounding box
    modelViewerScreenController.setupBoundingBox(idx, aabb.getMin(), aabb.getMax());
    // set up oriented bounding box
    modelViewerScreenController.setupOrientedBoundingBox(idx, obb.getCenter(), obb.getAxes()[0], obb.getAxes()[1], obb.getAxes()[2], obb.getHalfExtension());
    //
    modelViewerScreenController.selectBoundingVolume(idx, EntityBoundingVolumeSubScreenController.BoundingVolumeType.NONE);
}
Also used : OrientedBoundingBox(net.drewke.tdme.engine.primitives.OrientedBoundingBox) OrientedBoundingBox(net.drewke.tdme.engine.primitives.OrientedBoundingBox) BoundingBox(net.drewke.tdme.engine.primitives.BoundingBox) Vector3(net.drewke.tdme.math.Vector3)

Example 65 with Vector3

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

the class ModelViewerView method loadModel.

/**
	 * Load a model
	 */
private void loadModel() {
    //
    System.out.println("Model file: " + modelFile);
    // scene
    try {
        LevelEditorEntity oldModel = entity;
        // add entity to library
        entity = loadModel(modelFile.getName(), "", modelFile.getParentFile().getAbsolutePath(), modelFile.getName(), new Vector3());
        onLoadModel(oldModel, entity);
    } catch (Exception exception) {
        popUps.getInfoDialogScreenController().show("Warning", exception.getMessage());
    }
}
Also used : Vector3(net.drewke.tdme.math.Vector3) LevelEditorEntity(net.drewke.tdme.tools.shared.model.LevelEditorEntity) IOException(java.io.IOException)

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