Search in sources :

Example 41 with Vector3

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

the class Object3DBase method getFaceTriangles.

/**
	 * Retrieves complete list of face triangles for all render groups 
	 * @return faces
	 */
public Triangle[] getFaceTriangles() {
    ArrayList<Triangle> triangles = new ArrayList<Triangle>();
    for (Object3DGroup object3DGroup : object3dGroups) {
        Vector3[] groupVerticesTransformed = object3DGroup.mesh.transformedVertices;
        for (FacesEntity facesEntity : object3DGroup.group.getFacesEntities()) for (Face face : facesEntity.getFaces()) {
            int[] faceVertexIndices = face.getVertexIndices();
            triangles.add(new Triangle(groupVerticesTransformed[faceVertexIndices[0]].clone(), groupVerticesTransformed[faceVertexIndices[1]].clone(), groupVerticesTransformed[faceVertexIndices[2]].clone()));
        }
    }
    Triangle[] triangleArray = new Triangle[triangles.size()];
    triangles.toArray(triangleArray);
    return triangleArray;
}
Also used : FacesEntity(net.drewke.tdme.engine.model.FacesEntity) ArrayList(java.util.ArrayList) Triangle(net.drewke.tdme.engine.primitives.Triangle) Vector3(net.drewke.tdme.math.Vector3) Face(net.drewke.tdme.engine.model.Face)

Example 42 with Vector3

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

the class PrimitiveModel method createOrientedBoundingBoxModel.

/**
	 * Creates a model from oriented bounding box
	 * @param bounding box
	 * @param id
	 * @return model
	 */
public static Model createOrientedBoundingBoxModel(OrientedBoundingBox orientedBoundingBox, String id) {
    // ground model
    Model model = new Model(id, id, UpVector.Y_UP, RotationOrder.XYZ, null);
    // material
    Material material = new Material("tdme.primitive.material");
    material.getAmbientColor().set(0.5f, 0.5f, 0.5f, 1.0f);
    material.getDiffuseColor().set(1.0f, 0.5f, 0.5f, 0.5f);
    material.getSpecularColor().set(0f, 0f, 0f, 1f);
    model.getMaterials().put(material.getId(), material);
    // group
    Group group = new Group(model, null, "group", "group");
    // faces entity
    FacesEntity groupFacesEntity = new FacesEntity(group, "faces entity");
    groupFacesEntity.setMaterial(material);
    // faces entity 
    ArrayList<FacesEntity> groupFacesEntities = new ArrayList<FacesEntity>();
    groupFacesEntities.add(groupFacesEntity);
    // triangle vertices indexes
    int[][] fvi = OrientedBoundingBox.facesVerticesIndexes;
    // vertices
    ArrayList<Vector3> vertices = new ArrayList<Vector3>();
    for (Vector3 vertex : orientedBoundingBox.vertices) {
        vertices.add(vertex.clone());
    }
    Vector3[] axes = orientedBoundingBox.axes;
    // normals
    ArrayList<Vector3> normals = new ArrayList<Vector3>();
    normals.add(axes[0].clone().scale(-1f));
    normals.add(axes[0].clone());
    normals.add(axes[1].clone().scale(-1f));
    normals.add(axes[1].clone());
    normals.add(axes[2].clone().scale(-1f));
    normals.add(axes[2].clone());
    // faces
    ArrayList<Face> faces = new ArrayList<Face>();
    //	left
    faces.add(new Face(group, fvi[0][0], fvi[0][1], fvi[0][2], 0, 0, 0));
    faces.add(new Face(group, fvi[1][0], fvi[1][1], fvi[1][2], 0, 0, 0));
    //	right
    faces.add(new Face(group, fvi[2][0], fvi[2][1], fvi[2][2], 1, 1, 1));
    faces.add(new Face(group, fvi[3][0], fvi[3][1], fvi[3][2], 1, 1, 1));
    //	top
    faces.add(new Face(group, fvi[4][0], fvi[4][1], fvi[4][2], 2, 2, 2));
    faces.add(new Face(group, fvi[5][0], fvi[5][1], fvi[5][2], 2, 2, 2));
    //	bottom
    faces.add(new Face(group, fvi[6][0], fvi[6][1], fvi[6][2], 3, 3, 3));
    faces.add(new Face(group, fvi[7][0], fvi[7][1], fvi[7][2], 3, 3, 3));
    //	near
    faces.add(new Face(group, fvi[8][0], fvi[8][1], fvi[8][2], 4, 4, 4));
    faces.add(new Face(group, fvi[9][0], fvi[9][1], fvi[9][2], 4, 4, 4));
    //	far
    faces.add(new Face(group, fvi[10][0], fvi[10][1], fvi[10][2], 5, 5, 5));
    faces.add(new Face(group, fvi[11][0], fvi[11][1], fvi[11][2], 5, 5, 5));
    // set up faces entity
    groupFacesEntity.setFaces(faces);
    // setup group vertex data
    group.setVertices(vertices);
    group.setNormals(normals);
    group.setFacesEntities(groupFacesEntities);
    // determine features
    group.determineFeatures();
    // register group
    model.getGroups().put("group", group);
    model.getSubGroups().put("group", group);
    // prepare for indexed rendering
    ModelHelper.prepareForIndexedRendering(model);
    //
    return model;
}
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) Face(net.drewke.tdme.engine.model.Face)

Example 43 with Vector3

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

the class PrimitiveModel method createCapsuleModel.

/**
	 * Creates a model from capsule
	 * @param sphere
	 * @param id
	 * @param number of x segments
	 * @param number of y segments
	 * @return model
	 */
public static Model createCapsuleModel(Capsule capsule, String id, int segmentsX, int segmentsY) {
    // capsule properties
    float radius = capsule.radius;
    Vector3 a = capsule.a;
    Vector3 b = capsule.b;
    // rotation quaternion
    Quaternion rotationQuaternion = new Quaternion();
    rotationQuaternion.identity();
    // angle between a and b
    Vector3 yAxis = new Vector3(0f, -1f, 0f);
    Vector3 abNormalized = a.clone().sub(b).normalize();
    float[] abNormalizedVectorXYZ = abNormalized.getArray();
    Vector3 rotationAxis;
    if (Math.abs(abNormalizedVectorXYZ[0]) < MathTools.EPSILON && Math.abs(abNormalizedVectorXYZ[2]) < MathTools.EPSILON) {
        rotationAxis = new Vector3(abNormalizedVectorXYZ[1], 0f, 0f);
    } else {
        rotationAxis = Vector3.computeCrossProduct(yAxis, abNormalized).normalize();
    }
    float angle = Vector3.computeAngle(yAxis, abNormalized, yAxis);
    rotationQuaternion.rotate(angle, rotationAxis);
    // ground model
    Model model = new Model(id, id, UpVector.Y_UP, RotationOrder.XYZ, null);
    // material
    Material material = new Material("tdme.primitive.material");
    material.getAmbientColor().set(0.5f, 0.5f, 0.5f, 1.0f);
    material.getDiffuseColor().set(1.0f, 0.5f, 0.5f, 0.5f);
    material.getSpecularColor().set(0f, 0f, 0f, 1f);
    model.getMaterials().put(material.getId(), material);
    // group
    Group group = new Group(model, null, "group", "group");
    // faces entity
    FacesEntity groupFacesEntity = new FacesEntity(group, "faces entity");
    groupFacesEntity.setMaterial(material);
    // faces entity 
    ArrayList<FacesEntity> groupFacesEntities = new ArrayList<FacesEntity>();
    groupFacesEntities.add(groupFacesEntity);
    // vertices
    ArrayList<Vector3> vertices = new ArrayList<Vector3>();
    for (int i = 0; i < (segmentsY + 2) * segmentsX; i++) vertices.add(null);
    //	top half sphere
    for (int ySegment = segmentsY / 2; ySegment <= segmentsY; ySegment++) for (int xSegment = 0; xSegment < segmentsX; xSegment++) {
        Vector3 vertex = new Vector3();
        rotationQuaternion.multiply(new Vector3((float) (Math.sin(Math.PI * ySegment / segmentsY) * Math.cos(Math.PI * 2 * xSegment / segmentsX)), (float) (Math.cos(Math.PI * ySegment / segmentsY)), (float) (Math.sin(Math.PI * ySegment / segmentsY) * Math.sin(Math.PI * 2 * xSegment / segmentsX))), vertex);
        vertex.scale(radius);
        vertex.add(a);
        vertices.set(ySegment * segmentsX + xSegment, vertex);
    }
    //	bottom half sphere
    for (int i = 0; i < (segmentsY + 1) * segmentsX; i++) vertices.add(null);
    for (int ySegment = 0; ySegment <= segmentsY / 2; ySegment++) for (int xSegment = 0; xSegment < segmentsX; xSegment++) {
        Vector3 vertex = new Vector3();
        rotationQuaternion.multiply(new Vector3((float) (Math.sin(Math.PI * ySegment / segmentsY) * Math.cos(Math.PI * 2 * xSegment / segmentsX)), (float) (Math.cos(Math.PI * ySegment / segmentsY)), (float) (Math.sin(Math.PI * ySegment / segmentsY) * Math.sin(Math.PI * 2 * xSegment / segmentsX))), vertex);
        vertex.scale(radius);
        vertex.add(b);
        vertices.set(ySegment * segmentsX + xSegment, vertex);
    }
    // normals
    ArrayList<Vector3> normals = new ArrayList<Vector3>();
    // faces
    ArrayList<Face> faces = new ArrayList<Face>();
    int vi0, vi1, vi2;
    int ni;
    for (int y = 0; y <= segmentsY + 1; y++) {
        for (int x = 0; x < segmentsX; x++) {
            vi0 = ((y + 0) % (segmentsY + 1)) * segmentsX + ((x + 0) % (segmentsX));
            vi1 = ((y + 1) % (segmentsY + 1)) * segmentsX + ((x + 1) % (segmentsX));
            vi2 = ((y + 1) % (segmentsY + 1)) * segmentsX + ((x + 0) % (segmentsX));
            ni = normals.size();
            for (Vector3 normal : ModelHelper.computeNormals(new Vector3[] { vertices.get(vi0), vertices.get(vi1), vertices.get(vi2) })) {
                normals.add(normal);
            }
            faces.add(new Face(group, vi0, vi1, vi2, ni + 0, ni + 1, ni + 2));
            vi0 = ((y + 0) % (segmentsY + 1)) * segmentsX + ((x + 0) % (segmentsX));
            vi1 = ((y + 0) % (segmentsY + 1)) * segmentsX + ((x + 1) % (segmentsX));
            vi2 = ((y + 1) % (segmentsY + 1)) * segmentsX + ((x + 1) % (segmentsX));
            ni = normals.size();
            for (Vector3 normal : ModelHelper.computeNormals(new Vector3[] { vertices.get(vi0), vertices.get(vi1), vertices.get(vi2) })) {
                normals.add(normal);
            }
            faces.add(new Face(group, vi0, vi1, vi2, ni + 0, ni + 1, ni + 2));
        }
    }
    // set up faces entity
    groupFacesEntity.setFaces(faces);
    // setup group vertex data
    group.setVertices(vertices);
    group.setNormals(normals);
    group.setFacesEntities(groupFacesEntities);
    // determine features
    group.determineFeatures();
    // register group
    model.getGroups().put("group", group);
    model.getSubGroups().put("group", group);
    // prepare for indexed rendering
    ModelHelper.prepareForIndexedRendering(model);
    //
    return model;
}
Also used : FacesEntity(net.drewke.tdme.engine.model.FacesEntity) Group(net.drewke.tdme.engine.model.Group) Quaternion(net.drewke.tdme.math.Quaternion) ArrayList(java.util.ArrayList) Vector3(net.drewke.tdme.math.Vector3) Material(net.drewke.tdme.engine.model.Material) Model(net.drewke.tdme.engine.model.Model) Face(net.drewke.tdme.engine.model.Face)

Example 44 with Vector3

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

the class PrimitiveModel method createSphereModel.

/**
	 * Creates a model from oriented bounding box
	 * @param sphere
	 * @param id
	 * @param number of x segments
	 * @param number of y segments
	 * @return model
	 */
public static Model createSphereModel(Sphere sphere, String id, int segmentsX, int segmentsY) {
    // sphere properties
    float radius = sphere.radius;
    Vector3 center = sphere.center;
    // ground model
    Model model = new Model(id, id, UpVector.Y_UP, RotationOrder.XYZ, null);
    // material
    Material material = new Material("tdme.primitive.material");
    material.getAmbientColor().set(0.5f, 0.5f, 0.5f, 1.0f);
    material.getDiffuseColor().set(1.0f, 0.5f, 0.5f, 0.5f);
    material.getSpecularColor().set(0f, 0f, 0f, 1f);
    model.getMaterials().put(material.getId(), material);
    // group
    Group group = new Group(model, null, "group", "group");
    // faces entity
    FacesEntity groupFacesEntity = new FacesEntity(group, "faces entity");
    groupFacesEntity.setMaterial(material);
    // faces entity 
    ArrayList<FacesEntity> groupFacesEntities = new ArrayList<FacesEntity>();
    groupFacesEntities.add(groupFacesEntity);
    // vertices
    ArrayList<Vector3> vertices = new ArrayList<Vector3>();
    for (int i = 0; i < (segmentsY + 1) * segmentsX; i++) vertices.add(null);
    for (int ySegment = 0; ySegment <= segmentsY; ySegment++) for (int xSegment = 0; xSegment < segmentsX; xSegment++) {
        Vector3 vertex = new Vector3((float) (Math.sin(Math.PI * ySegment / segmentsY) * Math.cos(Math.PI * 2 * xSegment / segmentsX)), (float) (Math.cos(Math.PI * ySegment / segmentsY)), (float) (Math.sin(Math.PI * ySegment / segmentsY) * Math.sin(Math.PI * 2 * xSegment / segmentsX))).scale(radius).add(center);
        vertices.set(ySegment * segmentsX + xSegment, vertex);
    }
    // normals
    ArrayList<Vector3> normals = new ArrayList<Vector3>();
    // faces
    ArrayList<Face> faces = new ArrayList<Face>();
    int vi0, vi1, vi2;
    int ni;
    for (int y = 0; y <= segmentsY; y++) {
        for (int x = 0; x < segmentsX; x++) {
            vi0 = ((y + 0) % (segmentsY + 1)) * segmentsX + ((x + 0) % (segmentsX));
            vi1 = ((y + 1) % (segmentsY + 1)) * segmentsX + ((x + 1) % (segmentsX));
            vi2 = ((y + 1) % (segmentsY + 1)) * segmentsX + ((x + 0) % (segmentsX));
            ni = normals.size();
            for (Vector3 normal : ModelHelper.computeNormals(new Vector3[] { vertices.get(vi0), vertices.get(vi1), vertices.get(vi2) })) {
                normals.add(normal);
            }
            faces.add(new Face(group, vi0, vi1, vi2, ni + 0, ni + 1, ni + 2));
            vi0 = ((y + 0) % (segmentsY + 1)) * segmentsX + ((x + 0) % (segmentsX));
            vi1 = ((y + 0) % (segmentsY + 1)) * segmentsX + ((x + 1) % (segmentsX));
            vi2 = ((y + 1) % (segmentsY + 1)) * segmentsX + ((x + 1) % (segmentsX));
            ni = normals.size();
            for (Vector3 normal : ModelHelper.computeNormals(new Vector3[] { vertices.get(vi0), vertices.get(vi1), vertices.get(vi2) })) {
                normals.add(normal);
            }
            faces.add(new Face(group, vi0, vi1, vi2, ni + 0, ni + 1, ni + 2));
        }
    }
    // set up faces entity
    groupFacesEntity.setFaces(faces);
    // setup group vertex data
    group.setVertices(vertices);
    group.setNormals(normals);
    group.setFacesEntities(groupFacesEntities);
    // determine features
    group.determineFeatures();
    // register group
    model.getGroups().put("group", group);
    model.getSubGroups().put("group", group);
    // prepare for indexed rendering
    ModelHelper.prepareForIndexedRendering(model);
    //
    return model;
}
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) Vector3(net.drewke.tdme.math.Vector3) Material(net.drewke.tdme.engine.model.Material) Face(net.drewke.tdme.engine.model.Face)

Example 45 with Vector3

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

the class PartitionQuadTree method addRigidBody.

/**
	 * Adds a object
	 * @param rigidBody
	 */
protected void addRigidBody(RigidBody rigidBody) {
    // update if already exists
    ArrayList<PartitionTreeNode> objectPartitionsVector = rigidBodyPartitionNodes.get(rigidBody.getId());
    if (objectPartitionsVector != null) {
        while (objectPartitionsVector.size() > 0) {
            int lastIdx = objectPartitionsVector.size() - 1;
            objectPartitionsVector.get(lastIdx).partitionRidigBodies.remove(rigidBody);
            objectPartitionsVector.remove(lastIdx);
        }
    }
    // determine max first level partition dimension
    // convert to aabb for fast collision tests
    BoundingVolume cbv = rigidBody.cbv;
    Vector3 center = cbv.getCenter();
    halfExtension.set(cbv.computeDimensionOnAxis(sideVector) + 0.2f, cbv.computeDimensionOnAxis(upVector) + 0.2f, cbv.computeDimensionOnAxis(forwardVector) + 0.2f).scale(0.5f);
    boundingBox.getMin().set(center);
    boundingBox.getMin().sub(halfExtension);
    boundingBox.getMax().set(center);
    boundingBox.getMax().add(halfExtension);
    boundingBox.update();
    // find, create root nodes if not exists
    int minXPartition = (int) Math.floor(boundingBox.getMin().getX() / PARTITION_SIZE_MAX);
    int minYPartition = (int) Math.floor(boundingBox.getMin().getY() / PARTITION_SIZE_MAX);
    int minZPartition = (int) Math.floor(boundingBox.getMin().getZ() / PARTITION_SIZE_MAX);
    int maxXPartition = (int) Math.floor(boundingBox.getMax().getX() / PARTITION_SIZE_MAX);
    int maxYPartition = (int) Math.floor(boundingBox.getMax().getY() / PARTITION_SIZE_MAX);
    int maxZPartition = (int) Math.floor(boundingBox.getMax().getZ() / PARTITION_SIZE_MAX);
    for (int yPartition = minYPartition; yPartition <= maxYPartition; yPartition++) for (int xPartition = minXPartition; xPartition <= maxXPartition; xPartition++) for (int zPartition = minZPartition; zPartition <= maxZPartition; zPartition++) {
        // try to find node by key
        key.reset();
        key.append(xPartition);
        key.append(",");
        key.append(yPartition);
        key.append(",");
        key.append(zPartition);
        PartitionTreeNode node = treeRoot.subNodesByCoordinate.get(key);
        if (node == null) {
            node = createPartition(treeRoot, xPartition, yPartition, zPartition, PARTITION_SIZE_MAX);
        }
    }
    // add rigid body to tree
    addToPartitionTree(rigidBody, boundingBox);
}
Also used : BoundingVolume(net.drewke.tdme.engine.primitives.BoundingVolume) Vector3(net.drewke.tdme.math.Vector3)

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