Search in sources :

Example 16 with FacesEntity

use of net.drewke.tdme.engine.model.FacesEntity 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 17 with FacesEntity

use of net.drewke.tdme.engine.model.FacesEntity 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 18 with FacesEntity

use of net.drewke.tdme.engine.model.FacesEntity 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 19 with FacesEntity

use of net.drewke.tdme.engine.model.FacesEntity 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 20 with FacesEntity

use of net.drewke.tdme.engine.model.FacesEntity in project tdme by andreasdr.

the class TMReader method readFacesEntities.

/**
	 * Read faces entities from input stream
	 * @param input stream
	 * @param group
	 * @throws IOException
	 * @throws ModelIOException
	 */
private static void readFacesEntities(InputStream is, Group g) throws IOException, ModelFileIOException {
    FacesEntity[] facesEntities = new FacesEntity[readInt(is)];
    for (int i = 0; i < facesEntities.length; i++) {
        facesEntities[i] = new FacesEntity(g, readString(is));
        if (readBoolean(is) == true) {
            facesEntities[i].setMaterial(g.getModel().getMaterials().get(readString(is)));
        }
        Face[] faces = new Face[readInt(is)];
        for (int j = 0; j < faces.length; j++) {
            int[] vertexIndices = readIndices(is);
            int[] normalIndices = readIndices(is);
            faces[j] = new Face(g, vertexIndices[0], vertexIndices[1], vertexIndices[2], normalIndices[0], normalIndices[1], normalIndices[2]);
            int[] textureCoordinateIndices = readIndices(is);
            if (textureCoordinateIndices != null && textureCoordinateIndices.length > 0) {
                faces[j].setTextureCoordinateIndices(textureCoordinateIndices[0], textureCoordinateIndices[1], textureCoordinateIndices[2]);
            }
            int[] tangentIndices = readIndices(is);
            int[] bitangentIndices = readIndices(is);
            if (tangentIndices != null && tangentIndices.length > 0 && bitangentIndices != null && bitangentIndices.length > 0) {
                faces[j].setTangentIndices(tangentIndices[0], tangentIndices[1], tangentIndices[2]);
                faces[j].setBitangentIndices(bitangentIndices[0], bitangentIndices[1], bitangentIndices[2]);
            }
        }
        facesEntities[i].setFaces(faces);
    }
    g.setFacesEntities(facesEntities);
}
Also used : FacesEntity(net.drewke.tdme.engine.model.FacesEntity) Face(net.drewke.tdme.engine.model.Face) Joint(net.drewke.tdme.engine.model.Joint)

Aggregations

FacesEntity (net.drewke.tdme.engine.model.FacesEntity)22 Face (net.drewke.tdme.engine.model.Face)15 Material (net.drewke.tdme.engine.model.Material)15 Vector3 (net.drewke.tdme.math.Vector3)12 ArrayList (java.util.ArrayList)11 Model (net.drewke.tdme.engine.model.Model)9 Group (net.drewke.tdme.engine.model.Group)8 TextureCoordinate (net.drewke.tdme.engine.model.TextureCoordinate)6 Joint (net.drewke.tdme.engine.model.Joint)5 StringTokenizer (java.util.StringTokenizer)2 Object3D (net.drewke.tdme.engine.Object3D)2 BufferedReader (java.io.BufferedReader)1 DataInputStream (java.io.DataInputStream)1 InputStreamReader (java.io.InputStreamReader)1 HashSet (java.util.HashSet)1 Engine (net.drewke.tdme.engine.Engine)1 Object3DModel (net.drewke.tdme.engine.Object3DModel)1 Color4 (net.drewke.tdme.engine.model.Color4)1 JointWeight (net.drewke.tdme.engine.model.JointWeight)1 Skinning (net.drewke.tdme.engine.model.Skinning)1