Search in sources :

Example 11 with Object3D

use of net.drewke.tdme.engine.Object3D in project tdme by andreasdr.

the class ObjectParticleSystemEntityInternal method updateParticles.

/*
	 * (non-Javadoc)
	 * @see net.drewke.tdme.engine.ParticleSystemEntity#update()
	 */
public void updateParticles() {
    boolean first = true;
    float[] bbMinXYZ = boundingBoxTransformed.getMin().getArray();
    float[] bbMaxXYZ = boundingBoxTransformed.getMax().getArray();
    long timeDelta = engine.getTiming().getDeltaTime();
    for (int i = 0; i < particles.length; i++) {
        Particle particle = particles[i];
        if (particle.active == false)
            continue;
        //
        Object3D object = objects[i];
        // life time
        particle.lifeTimeCurrent += timeDelta;
        if (particle.lifeTimeCurrent >= particle.lifeTimeMax) {
            particle.active = false;
            object.setEnabled(false);
            enabledObjects.remove(object);
            continue;
        }
        // add gravity if our particle have a noticable mass
        if (particle.mass > MathTools.EPSILON)
            particle.velocity.subY(0.5f * MathTools.g * (float) timeDelta / 1000f);
        // TODO:
        //	maybe take air resistance into account like a huge paper needs more time to fall than a sphere of paper
        //	or heat for smoke or fire, whereas having no mass for those particles works around this problem for now
        // update up effect colors
        object.getEffectColorAdd().set(effectColorAdd);
        object.getEffectColorMul().set(effectColorMul);
        // translation
        object.getTranslation().add(velocityForTime.set(particle.velocity).scale((float) timeDelta / 1000f));
        object.update();
        if (first == true) {
            boundingBoxTransformed.getMin().set(object.getBoundingBoxTransformed().getMin());
            boundingBoxTransformed.getMax().set(object.getBoundingBoxTransformed().getMax());
            first = false;
        } else {
            float[] objBbMinXYZ = object.getBoundingBoxTransformed().getMin().getArray();
            float[] objBbMaxXYZ = object.getBoundingBoxTransformed().getMax().getArray();
            if (objBbMinXYZ[0] < bbMinXYZ[0])
                bbMinXYZ[0] = objBbMinXYZ[0];
            if (objBbMinXYZ[1] < bbMinXYZ[1])
                bbMinXYZ[1] = objBbMinXYZ[1];
            if (objBbMinXYZ[2] < bbMinXYZ[2])
                bbMinXYZ[2] = objBbMinXYZ[2];
            if (objBbMaxXYZ[0] > bbMaxXYZ[0])
                bbMaxXYZ[0] = objBbMaxXYZ[0];
            if (objBbMaxXYZ[1] > bbMaxXYZ[1])
                bbMaxXYZ[1] = objBbMaxXYZ[1];
            if (objBbMaxXYZ[2] > bbMaxXYZ[2])
                bbMaxXYZ[2] = objBbMaxXYZ[2];
        }
    }
    // compute bounding boxes
    boundingBoxTransformed.update();
    boundingBox.fromBoundingVolumeWithTransformations(boundingBoxTransformed, inverseTransformation);
}
Also used : Object3D(net.drewke.tdme.engine.Object3D)

Example 12 with Object3D

use of net.drewke.tdme.engine.Object3D in project tdme by andreasdr.

the class ObjectParticleSystemEntityInternal method emitParticles.

/*
	 * (non-Javadoc)
	 * @see net.drewke.tdme.engine.ParticleSystemEntity#emit(net.drewke.tdme.math.Vector3)
	 */
public int emitParticles() {
    // determine particles to spawn
    float particlesToSpawn = emitter.getCount() * engine.getTiming().getDeltaTime() / 1000f;
    // particles to spawn
    int particlesToSpawnInteger = 0;
    if (autoEmit == true) {
        particlesToSpawnInteger = (int) particlesToSpawn;
        particlesToSpawnRemainder += particlesToSpawn - particlesToSpawnInteger;
        if (particlesToSpawnRemainder > 1.0f) {
            particlesToSpawn += 1.0f;
            particlesToSpawnInteger++;
            particlesToSpawnRemainder -= 1f;
        }
    } else {
        particlesToSpawnInteger = emitter.getCount();
    }
    // skip if nothing to spawn
    if (particlesToSpawnInteger == 0)
        return 0;
    // spawn
    int particlesSpawned = 0;
    for (int i = 0; i < particles.length; i++) {
        Particle particle = particles[i];
        if (particle.active == true)
            continue;
        // emit particle
        emitter.emit(particle);
        // enable object
        Object3D object = objects[i];
        object.getTranslation().set(particle.position);
        object.update();
        object.setEnabled(true);
        object.getEffectColorAdd().set(effectColorAdd);
        object.getEffectColorMul().set(effectColorMul);
        enabledObjects.add(object);
        // all particles spawned?
        particlesSpawned++;
        if (particlesSpawned == particlesToSpawnInteger)
            break;
    }
    // done
    return particlesSpawned;
}
Also used : Object3D(net.drewke.tdme.engine.Object3D)

Example 13 with Object3D

use of net.drewke.tdme.engine.Object3D in project tdme by andreasdr.

the class LevelEditorView method objectDataApply.

/**
	 * On object data apply
	 * @param name
	 * @param description
	 */
public boolean objectDataApply(String name, String description) {
    if (selectedObjects.size() != 1)
        return false;
    // we only accept a single selection
    Entity selectedObject = selectedObjects.get(0);
    // skip if level editor internal object
    if (selectedObject.getId().startsWith("leveleditor."))
        return false;
    // check if not a internal leveleditor object
    LevelEditorObject levelEditorObject = level.getObjectById(selectedObject.getId());
    // we need a associated object in level 
    if (levelEditorObject == null)
        return false;
    // we always can safely set the description
    levelEditorObject.setDescription(description);
    // did id changed
    if (levelEditorObject.getId().equals(name) == false) {
        // yep, check if name is already in use
        if (engine.getEntity(name) != null) {
            return false;
        }
        // remove from engine, level and selection
        String oldId = levelEditorObject.getId();
        level.removeObject(levelEditorObject.getId());
        engine.removeEntity(levelEditorObject.getId());
        selectedObjectsById.clear();
        selectedObjects.clear();
        // set up id
        levelEditorObject.setId(name);
        // add to level, 3d engine
        level.addObject(levelEditorObject);
        Object3D object = new Object3D(levelEditorObject.getId(), levelEditorObject.getEntity().getModel());
        object.fromTransformations(levelEditorObject.getTransformations());
        object.setPickable(true);
        setStandardObjectColorEffect(object);
        setHighlightObjectColorEffect(object);
        engine.addEntity(object);
        // add to selected objects
        selectedObjects.add(object);
        selectedObjectsById.put(object.getId(), object);
        // update in objects listbox
        levelEditorScreenController.setObjectListbox(level.getObjectIdsIterator());
    }
    // set description
    levelEditorObject.setDescription(description);
    //
    return true;
}
Also used : Entity(net.drewke.tdme.engine.Entity) LevelEditorEntity(net.drewke.tdme.tools.shared.model.LevelEditorEntity) FacesEntity(net.drewke.tdme.engine.model.FacesEntity) LevelEditorObject(net.drewke.tdme.tools.shared.model.LevelEditorObject) Object3D(net.drewke.tdme.engine.Object3D)

Example 14 with Object3D

use of net.drewke.tdme.engine.Object3D 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 15 with Object3D

use of net.drewke.tdme.engine.Object3D in project tdme by andreasdr.

the class ShadowMap method render.

/**
	 * Renders given objects to shadow map
	 * @param gl
	 * @param objects
	 */
protected void render(Light light, ArrayList<Object3D> objects) {
    // clear visible objects
    visibleObjects.clear();
    // TODO: Spotlights are different!
    // viewers camera
    Camera camera = shadowMapping.engine.getCamera();
    float lightEyeDistance = lightDirection.set(camera.getLookAt()).sub(camera.getLookFrom()).computeLength() * shadowMapping.lightEyeDistanceScale;
    // compute camera from view of light 
    lightDirection.set(light.getSpotDirection()).normalize();
    lightLookAt.set(camera.getLookAt());
    lightLookFrom.set(lightLookAt).sub(lightDirection.scale(lightEyeDistance));
    // determine light camera z far
    float lightCameraZFar = lightEyeDistance * 2.0f;
    if (camera.getZFar() > lightCameraZFar)
        lightCameraZFar = camera.getZFar();
    // set up light camera from view of light
    lightCamera.setZNear(camera.getZNear());
    lightCamera.setZFar(lightCameraZFar);
    lightCamera.getLookFrom().set(lightLookFrom);
    lightCamera.getLookAt().set(lightLookAt);
    lightCamera.computeUpVector(lightCamera.getLookFrom(), lightCamera.getLookAt(), lightCamera.getUpVector());
    lightCamera.update(frameBuffer.getWidth(), frameBuffer.getHeight());
    // Bind frame buffer to shadow map fbo id
    frameBuffer.enableFrameBuffer();
    // clear depth buffer
    shadowMapping.renderer.clear(shadowMapping.renderer.CLEAR_DEPTH_BUFFER_BIT);
    // determine visible objects and objects that should generate a shadow
    for (Entity entity : shadowMapping.engine.getPartition().getVisibleEntities(lightCamera.getFrustum())) {
        if (entity instanceof Object3D) {
            Object3D object = (Object3D) entity;
            if (object.isDynamicShadowingEnabled() == false)
                continue;
            visibleObjects.add(object);
        } else if (entity instanceof ObjectParticleSystemEntity) {
            ObjectParticleSystemEntity opse = (ObjectParticleSystemEntity) entity;
            if (opse.isDynamicShadowingEnabled() == false)
                continue;
            visibleObjects.addAll(opse.getEnabledObjects());
        }
    }
    // generate shadow map texture matrix
    computeDepthBiasMVPMatrix();
    // only draw opaque face entities as shadows will not be produced from transparent objects
    shadowMapping.object3DVBORenderer.render(visibleObjects, true, Object3DVBORenderer.DepthBufferMode.FORCE, null);
}
Also used : ObjectParticleSystemEntity(net.drewke.tdme.engine.ObjectParticleSystemEntity) Entity(net.drewke.tdme.engine.Entity) ObjectParticleSystemEntity(net.drewke.tdme.engine.ObjectParticleSystemEntity) Camera(net.drewke.tdme.engine.Camera) Object3D(net.drewke.tdme.engine.Object3D)

Aggregations

Object3D (net.drewke.tdme.engine.Object3D)18 Entity (net.drewke.tdme.engine.Entity)8 Vector3 (net.drewke.tdme.math.Vector3)8 FacesEntity (net.drewke.tdme.engine.model.FacesEntity)7 Camera (net.drewke.tdme.engine.Camera)6 Rotation (net.drewke.tdme.engine.Rotation)6 Model (net.drewke.tdme.engine.model.Model)6 LevelEditorEntity (net.drewke.tdme.tools.shared.model.LevelEditorEntity)6 Light (net.drewke.tdme.engine.Light)5 BoundingVolume (net.drewke.tdme.engine.primitives.BoundingVolume)4 OrientedBoundingBox (net.drewke.tdme.engine.primitives.OrientedBoundingBox)4 PrimitiveModel (net.drewke.tdme.engine.primitives.PrimitiveModel)4 LevelEditorObject (net.drewke.tdme.tools.shared.model.LevelEditorObject)4 Object3DModel (net.drewke.tdme.engine.Object3DModel)3 Transformations (net.drewke.tdme.engine.Transformations)3 ConvexMesh (net.drewke.tdme.engine.primitives.ConvexMesh)3 Sphere (net.drewke.tdme.engine.primitives.Sphere)3 ObjectParticleSystemEntity (net.drewke.tdme.engine.ObjectParticleSystemEntity)2 PointsParticleSystemEntity (net.drewke.tdme.engine.PointsParticleSystemEntity)2 Color4 (net.drewke.tdme.engine.model.Color4)2