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);
}
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();
}
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();
}
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);
}
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());
}
}
Aggregations