Search in sources :

Example 11 with Vector3

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

the class LevelEditorEntityLibrary method addEmpty.

/**
	 * Add a empty
	 * @param name
	 * @param description
	 * @return level editor entity
	 * @throws Exception
	 */
public LevelEditorEntity addEmpty(int id, String name, String description) throws Exception {
    String cacheId = "leveleditor.empty";
    LevelEditorEntity levelEditorEntity = null;
    // create entity
    Model model = DAEReader.read("resources/tools/leveleditor/models", "arrow.dae");
    levelEditorEntity = new LevelEditorEntity(id == ID_ALLOCATE ? allocateEntityId() : id, EntityType.EMPTY, name, description, null, cacheId, model.getId().replace("\\", "_").replace("/", "_").replace(":", "_") + ".png", model, new Vector3());
    // add empty
    addEntity(levelEditorEntity);
    //
    return levelEditorEntity;
}
Also used : Model(net.drewke.tdme.engine.model.Model) PrimitiveModel(net.drewke.tdme.engine.primitives.PrimitiveModel) Vector3(net.drewke.tdme.math.Vector3)

Example 12 with Vector3

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

the class LevelEditorLevel method computeBoundingBox.

/**
	 * Computes level bounding box
	 */
public void computeBoundingBox() {
    boolean haveDimension = false;
    float left = 0.0f;
    float right = 0.0f;
    float near = 0.0f;
    float far = 0.0f;
    float top = 0.0f;
    float bottom = 0.0f;
    Vector3 bbDimension = new Vector3();
    Vector3 bbMin = new Vector3();
    Vector3 bbMax = new Vector3();
    for (LevelEditorObject levelEditorObject : objects) {
        if (levelEditorObject.getEntity().getType() != EntityType.MODEL)
            continue;
        BoundingBox bv = levelEditorObject.getEntity().getModel().getBoundingBox();
        BoundingVolume cbv = bv.clone();
        cbv.fromBoundingVolumeWithTransformations(bv, levelEditorObject.getTransformations());
        bbDimension.set(cbv.computeDimensionOnAxis(new Vector3(1f, 0f, 0f)), cbv.computeDimensionOnAxis(new Vector3(0f, 1f, 0f)), cbv.computeDimensionOnAxis(new Vector3(0f, 0f, 1f)));
        bbDimension.scale(0.5f);
        bbMin.set(cbv.getCenter());
        bbMin.sub(bbDimension);
        bbMax.set(cbv.getCenter());
        bbMax.add(bbDimension);
        float objectLeft = bbMin.getX();
        float objectRight = bbMax.getX();
        float objectNear = bbMin.getZ();
        float objectFar = bbMax.getZ();
        float objectBottom = bbMin.getY();
        float objectTop = bbMax.getY();
        if (haveDimension == false) {
            left = objectLeft;
            right = objectRight;
            near = objectNear;
            far = objectFar;
            top = objectTop;
            bottom = objectBottom;
            haveDimension = true;
        } else {
            if (objectLeft < left)
                left = objectLeft;
            if (objectRight > right)
                right = objectRight;
            if (objectNear < near)
                near = objectNear;
            if (objectFar > far)
                far = objectFar;
            if (objectTop > top)
                top = objectTop;
            if (objectBottom < bottom)
                bottom = objectBottom;
        }
    }
    boundingBox.getMin().set(left, bottom, near);
    boundingBox.getMax().set(right, top, far);
    boundingBox.update();
    dimension.setX(right - left);
    dimension.setZ(far - near);
    dimension.setY(top - bottom);
}
Also used : BoundingBox(net.drewke.tdme.engine.primitives.BoundingBox) BoundingVolume(net.drewke.tdme.engine.primitives.BoundingVolume) Vector3(net.drewke.tdme.math.Vector3)

Example 13 with Vector3

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

the class DAEReader method readGeometry.

/**
	 * Reads a geometry
	 * @param authoring tools
	 * @param path name
	 * @param model
	 * @param group
	 * @param xml root
	 * @param xml node id
	 * @param material symbols
	 * @throws Exception
	 */
public static void readGeometry(AuthoringTool authoringTool, String pathName, Model model, Group group, Element xmlRoot, String xmlNodeId, HashMap<String, String> materialSymbols) throws Exception {
    StringTokenizer t;
    //
    FacesEntity facesEntity = null;
    ArrayList<FacesEntity> facesEntities = new ArrayList<FacesEntity>(Arrays.asList(group.getFacesEntities()));
    int verticesOffset = group.getVertices().length;
    ArrayList<Vector3> vertices = new ArrayList<Vector3>(Arrays.asList(group.getVertices()));
    int normalsOffset = group.getNormals().length;
    ArrayList<Vector3> normals = new ArrayList<Vector3>(Arrays.asList(group.getNormals()));
    int textureCoordinatesOffset = group.getTextureCoordinates() != null ? group.getTextureCoordinates().length : 0;
    ArrayList<TextureCoordinate> textureCoordinates = group.getTextureCoordinates() != null ? new ArrayList<TextureCoordinate>(Arrays.asList(group.getTextureCoordinates())) : new ArrayList<TextureCoordinate>();
    Element xmlLibraryGeometries = getChildrenByTagName(xmlRoot, "library_geometries").get(0);
    for (Element xmlGeometry : getChildrenByTagName(xmlLibraryGeometries, "geometry")) {
        if (xmlGeometry.getAttribute("id").equals(xmlNodeId)) {
            Element xmlMesh = getChildrenByTagName(xmlGeometry, "mesh").get(0);
            ArrayList<Element> xmlPolygonsList = new ArrayList<Element>();
            // try to read from triangles
            for (Element xmlTriangesElement : getChildrenByTagName(xmlMesh, "triangles")) {
                xmlPolygonsList.add(xmlTriangesElement);
            }
            // try to read from polylist
            for (Element xmlPolyListElement : getChildrenByTagName(xmlMesh, "polylist")) {
                xmlPolygonsList.add(xmlPolyListElement);
            }
            // try to read from polygons
            for (Element xmlPolygonsElement : getChildrenByTagName(xmlMesh, "polygons")) {
                xmlPolygonsList.add(xmlPolygonsElement);
            }
            // parse from xml polygons elements
            for (Element xmlPolygons : xmlPolygonsList) {
                ArrayList<Face> faces = new ArrayList<Face>();
                facesEntity = new FacesEntity(group, xmlNodeId);
                if (xmlPolygons.getNodeName().toLowerCase().equals("polylist")) {
                    t = new StringTokenizer(getChildrenByTagName(xmlPolygons, "vcount").get(0).getTextContent());
                    while (t.hasMoreTokens()) {
                        int vertexCount = Integer.parseInt(t.nextToken());
                        if (vertexCount != 3) {
                            throw new ModelFileIOException("we only support triangles in " + xmlNodeId);
                        }
                    }
                }
                // parse triangles
                int xmlInputs = -1;
                int xmlVerticesOffset = -1;
                String xmlVerticesSource = null;
                int xmlNormalsOffset = -1;
                String xmlNormalsSource = null;
                int xmlTexCoordOffset = -1;
                String xmlTexCoordSource = null;
                int xmlColorOffset = -1;
                String xmlColorSource = null;
                // material
                String xmlMaterialId = xmlPolygons.getAttribute("material");
                String materialSymbol = materialSymbols.get(xmlMaterialId);
                if (materialSymbol != null)
                    xmlMaterialId = materialSymbol.substring(1);
                if (xmlMaterialId != null && xmlMaterialId.length() > 0) {
                    Material material = model.getMaterials().get(xmlMaterialId);
                    if (material == null) {
                        // parse material as we do not have it yet
                        material = readMaterial(authoringTool, pathName, model, xmlRoot, xmlMaterialId);
                    }
                    // set it up
                    facesEntity.setMaterial(material);
                }
                // parse input sources
                HashSet<Integer> xmlInputSet = new HashSet<Integer>();
                for (Element xmlTrianglesInput : getChildrenByTagName(xmlPolygons, "input")) {
                    // check for vertices sources
                    if (xmlTrianglesInput.getAttribute("semantic").equals("VERTEX")) {
                        xmlVerticesOffset = Integer.parseInt(xmlTrianglesInput.getAttribute("offset"));
                        xmlVerticesSource = xmlTrianglesInput.getAttribute("source").substring(1);
                        xmlInputSet.add(xmlVerticesOffset);
                    } else // check for normals sources
                    if (xmlTrianglesInput.getAttribute("semantic").equals("NORMAL")) {
                        xmlNormalsOffset = Integer.parseInt(xmlTrianglesInput.getAttribute("offset"));
                        xmlNormalsSource = xmlTrianglesInput.getAttribute("source").substring(1);
                        xmlInputSet.add(xmlNormalsOffset);
                    }
                    // check for texture coordinate sources
                    if (xmlTrianglesInput.getAttribute("semantic").equals("TEXCOORD")) {
                        xmlTexCoordOffset = Integer.parseInt(xmlTrianglesInput.getAttribute("offset"));
                        xmlTexCoordSource = xmlTrianglesInput.getAttribute("source").substring(1);
                        xmlInputSet.add(xmlTexCoordOffset);
                    }
                    // check for color coordinate sources
                    if (xmlTrianglesInput.getAttribute("semantic").equals("COLOR")) {
                        xmlColorOffset = Integer.parseInt(xmlTrianglesInput.getAttribute("offset"));
                        xmlColorSource = xmlTrianglesInput.getAttribute("source").substring(1);
                        xmlInputSet.add(xmlColorOffset);
                    }
                }
                xmlInputs = xmlInputSet.size();
                // get vertices source
                for (Element xmlVertices : getChildrenByTagName(xmlMesh, "vertices")) {
                    if (xmlVertices.getAttribute("id").equals(xmlVerticesSource)) {
                        for (Element xmlVerticesInput : getChildrenByTagName(xmlVertices, "input")) {
                            if (xmlVerticesInput.getAttribute("semantic").equalsIgnoreCase("position")) {
                                xmlVerticesSource = xmlVerticesInput.getAttribute("source").substring(1);
                            } else if (xmlVerticesInput.getAttribute("semantic").equalsIgnoreCase("normal")) {
                                xmlNormalsSource = xmlVerticesInput.getAttribute("source").substring(1);
                            }
                        }
                    }
                }
                // check for triangles vertices sources
                if (xmlVerticesSource == null) {
                    throw new ModelFileIOException("Could not determine triangles vertices source for '" + xmlNodeId + "'");
                }
                // check for triangles normals sources
                if (xmlNormalsSource == null) {
                    throw new ModelFileIOException("Could not determine triangles normal source for '" + xmlNodeId + "'");
                }
                // load vertices, normals, texture coordinates
                for (Element xmlMeshSource : getChildrenByTagName(xmlMesh, "source")) {
                    // vertices
                    if (xmlMeshSource.getAttribute("id").equals(xmlVerticesSource)) {
                        Element xmlFloatArray = getChildrenByTagName(xmlMeshSource, "float_array").get(0);
                        String valueString = xmlFloatArray.getTextContent();
                        t = new StringTokenizer(valueString, " \n\r");
                        while (t.hasMoreTokens()) {
                            Vector3 v = new Vector3(Float.parseFloat(t.nextToken()), Float.parseFloat(t.nextToken()), Float.parseFloat(t.nextToken()));
                            vertices.add(v);
                        }
                    } else // normals
                    if (xmlMeshSource.getAttribute("id").equals(xmlNormalsSource)) {
                        Element xmlFloatArray = getChildrenByTagName(xmlMeshSource, "float_array").get(0);
                        String valueString = xmlFloatArray.getTextContent();
                        t = new StringTokenizer(valueString, " \n\r");
                        while (t.hasMoreTokens()) {
                            Vector3 v = new Vector3(Float.parseFloat(t.nextToken()), Float.parseFloat(t.nextToken()), Float.parseFloat(t.nextToken()));
                            normals.add(v);
                        }
                    }
                    // texture coordinates
                    if (xmlTexCoordSource != null) {
                        if (xmlMeshSource.getAttribute("id").equals(xmlTexCoordSource)) {
                            Element xmlFloatArray = getChildrenByTagName(xmlMeshSource, "float_array").get(0);
                            String valueString = xmlFloatArray.getTextContent();
                            t = new StringTokenizer(valueString, " \n\r");
                            while (t.hasMoreTokens()) {
                                TextureCoordinate tc = new TextureCoordinate(Float.parseFloat(t.nextToken()), Float.parseFloat(t.nextToken()));
                                textureCoordinates.add(tc);
                            }
                        }
                    }
                }
                // load faces
                for (Element xmlPolygon : getChildrenByTagName(xmlPolygons, "p")) {
                    String valueString = xmlPolygon.getTextContent();
                    t = new StringTokenizer(valueString, " \n\r");
                    int[] vi = new int[3];
                    int viIdx = 0;
                    int[] ni = new int[3];
                    int niIdx = 0;
                    int[] ti = xmlTexCoordSource == null ? null : new int[3];
                    int tiIdx = 0;
                    int valueIdx = 0;
                    boolean valid = true;
                    while (t.hasMoreTokens()) {
                        int value = Integer.parseInt(t.nextToken());
                        if (valueIdx % xmlInputs == xmlVerticesOffset) {
                            vi[viIdx++] = value;
                            // validate
                            if (value < 0 || value >= vertices.size() - verticesOffset) {
                                valid = false;
                            }
                            // fix for some strange models
                            if (xmlNormalsSource != null && xmlNormalsOffset == -1) {
                                ni[niIdx++] = value;
                                // validate
                                if (value < 0 || value >= normals.size() - normalsOffset) {
                                    valid = false;
                                }
                            }
                        }
                        if (xmlNormalsOffset != -1 && valueIdx % xmlInputs == xmlNormalsOffset) {
                            ni[niIdx++] = value;
                            // validate
                            if (value < 0 || value >= normals.size() - normalsOffset) {
                                valid = false;
                            }
                        }
                        if (xmlTexCoordOffset != -1 && valueIdx % xmlInputs == xmlTexCoordOffset) {
                            ti[tiIdx++] = value;
                            // validate
                            if (value < 0 || value >= textureCoordinates.size() - textureCoordinatesOffset) {
                                valid = false;
                            }
                        }
                        if (viIdx == 3 && niIdx == 3 && (ti == null || tiIdx == 3)) {
                            // only add valid faces
                            if (valid) {
                                // add face
                                Face f = new Face(group, vi[0] + verticesOffset, vi[1] + verticesOffset, vi[2] + verticesOffset, ni[0] + normalsOffset, ni[1] + normalsOffset, ni[2] + normalsOffset);
                                if (ti != null) {
                                    f.setTextureCoordinateIndices(ti[0] + textureCoordinatesOffset, ti[1] + textureCoordinatesOffset, ti[2] + textureCoordinatesOffset);
                                }
                                faces.add(f);
                            }
                            viIdx = 0;
                            niIdx = 0;
                            tiIdx = 0;
                            valid = true;
                        }
                        valueIdx++;
                    }
                }
                // add faces entities if we have any
                if (faces.isEmpty() == false) {
                    facesEntity.setFaces(faces);
                    facesEntities.add(facesEntity);
                }
            }
        }
    }
    // set up group
    group.setVertices(vertices);
    group.setNormals(normals);
    if (textureCoordinates.size() > 0)
        group.setTextureCoordinates(textureCoordinates);
    group.setFacesEntities(facesEntities);
    // create normal tangents and bitangents
    ModelHelper.createNormalTangentsAndBitangents(group);
    // determine features
    group.determineFeatures();
}
Also used : FacesEntity(net.drewke.tdme.engine.model.FacesEntity) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) Vector3(net.drewke.tdme.math.Vector3) Material(net.drewke.tdme.engine.model.Material) Joint(net.drewke.tdme.engine.model.Joint) StringTokenizer(java.util.StringTokenizer) TextureCoordinate(net.drewke.tdme.engine.model.TextureCoordinate) Face(net.drewke.tdme.engine.model.Face) HashSet(java.util.HashSet)

Example 14 with Vector3

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

the class ConstraintsSolver method checkChainSuccessor.

/**
	 * Finds rigid body successors in a direction for given rigid body src 
	 * @param rigid body src
	 * @param normal last
	 * @param rigid bodies current chain
	 * @param rigid bodies current chain result
	 * @param calls
	 */
private void checkChainSuccessor(RigidBody rigidBodySrc, Vector3 normalLast, ArrayList<RigidBody> rigidBodiesCurrentChain) {
    rigidBodiesCurrentChain.add(rigidBodySrc);
    for (int i = 0; i < constraintsEntityCount; i++) {
        ConstraintsEntity constraintEntity = constraintsEntities[i];
        // rigid body to check
        RigidBody rigidBodyCheck = null;
        // check if rigid body is another rigid body velocity change rigid
        if (constraintEntity.rb1 == rigidBodySrc) {
            rigidBodyCheck = constraintEntity.rb2;
        } else if (constraintEntity.rb2 == rigidBodySrc) {
            rigidBodyCheck = constraintEntity.rb1;
        } else {
            continue;
        }
        // do not check static rigids
        if (rigidBodyCheck.isStatic == true) {
            continue;
        }
        // skip on disabled rigid bodies
        if (rigidBodyCheck.enabled == false)
            continue;
        // check if we checked this node already
        boolean haveRigidBodyCheck = false;
        for (int j = 0; j < rigidBodiesCurrentChain.size(); j++) {
            if (rigidBodiesCurrentChain.get(j) == rigidBodyCheck) {
                haveRigidBodyCheck = true;
                break;
            }
        }
        if (haveRigidBodyCheck == true) {
            continue;
        }
        // check if normal have same direction
        Vector3 normalCurrent = constraintEntity.collision.getNormal();
        if (normalLast != null) {
            if (Math.abs(Vector3.computeDotProduct(normalLast, normalCurrent)) < 0.75f) {
                continue;
            }
        }
        // check next
        checkChainSuccessor(rigidBodyCheck, normalCurrent, rigidBodiesCurrentChain);
    }
}
Also used : Vector3(net.drewke.tdme.math.Vector3)

Example 15 with Vector3

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

the class PartitionQuadTree method getObjectsNearTo.

/**
	 * Get objects near to
	 * @param cbv
	 * @return objects near to cbv
	 */
public ArrayListIteratorMultiple<RigidBody> getObjectsNearTo(BoundingVolume 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();
    rigidBodyIterator.clear();
    int lookUps = 0;
    for (int i = 0; i < treeRoot.subNodes.size(); i++) {
        lookUps += doPartitionTreeLookUpNearEntities(treeRoot.subNodes.get(i), boundingBox, rigidBodyIterator);
    }
    return rigidBodyIterator;
}
Also used : 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