Search in sources :

Example 21 with Model

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

the class DAEReader method read.

/**
	 * Reads Collada DAE file
	 * @param path name
	 * @param file name
	 * @throws Exception
	 * @return Model instance
	 */
public static Model read(String pathName, String fileName) throws Exception {
    // load dae xml document
    DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document document = builder.parse(FileSystem.getInstance().getInputStream(pathName, fileName));
    Element xmlRoot = document.getDocumentElement();
    // authoring tool
    AuthoringTool authoringTool = getAuthoringTool(xmlRoot);
    // up vector and rotation order
    UpVector upVector = getUpVector(xmlRoot);
    RotationOrder rotationOrder = null;
    switch(upVector) {
        case Y_UP:
            rotationOrder = RotationOrder.ZYX;
        case Z_UP:
            rotationOrder = RotationOrder.YZX;
    }
    // 	create model
    Model model = new Model(pathName + File.separator + fileName, fileName, upVector, rotationOrder, null);
    // import matrix
    setupModelImportRotationMatrix(xmlRoot, model);
    setupModelImportScaleMatrix(xmlRoot, model);
    // parse scene from xml
    String xmlSceneId = null;
    Element xmlScene = getChildrenByTagName(xmlRoot, "scene").get(0);
    for (Element xmlInstanceVisualscene : getChildrenByTagName(xmlScene, "instance_visual_scene")) {
        xmlSceneId = xmlInstanceVisualscene.getAttribute("url").substring(1);
    }
    // check for xml scene id
    if (xmlSceneId == null) {
        throw new ModelFileIOException("No scene id found");
    }
    // parse visual scenes
    Element xmlLibraryVisualScenes = getChildrenByTagName(xmlRoot, "library_visual_scenes").get(0);
    for (Element xmlLibraryVisualScene : getChildrenByTagName(xmlLibraryVisualScenes, "visual_scene")) {
        String xmlVisualSceneId = xmlLibraryVisualScene.getAttribute("id");
        if (xmlVisualSceneId.equals(xmlSceneId)) {
            // default FPS
            float fps = 30f;
            // parse frames per second
            List<Element> xmlExtraNodes = getChildrenByTagName(xmlLibraryVisualScene, "extra");
            if (xmlExtraNodes.isEmpty() == false) {
                Element xmlExtraNode = xmlExtraNodes.get(0);
                for (Element xmlTechnique : getChildrenByTagName(xmlExtraNode, "technique")) {
                    List<Element> xmlFrameRateNodes = getChildrenByTagName(xmlTechnique, "frame_rate");
                    if (xmlFrameRateNodes.isEmpty() == false) {
                        fps = Float.parseFloat(xmlFrameRateNodes.get(0).getTextContent());
                        break;
                    }
                }
            }
            // set up frames per seconds
            model.setFPS(fps);
            // visual scene root nodes
            for (Element xmlNode : getChildrenByTagName(xmlLibraryVisualScene, "node")) {
                Group group = readVisualSceneNode(authoringTool, pathName, model, null, xmlRoot, xmlNode, fps);
                if (group != null) {
                    model.getSubGroups().put(group.getId(), group);
                    model.getGroups().put(group.getId(), group);
                }
            }
        }
    }
    // set up joints
    ModelHelper.setupJoints(model);
    // fix animation length
    ModelHelper.fixAnimationLength(model);
    // prepare for indexed rendering
    ModelHelper.prepareForIndexedRendering(model);
    //
    return model;
}
Also used : Group(net.drewke.tdme.engine.model.Group) UpVector(net.drewke.tdme.engine.model.Model.UpVector) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Element(org.w3c.dom.Element) RotationOrder(net.drewke.tdme.engine.model.RotationOrder) Model(net.drewke.tdme.engine.model.Model) Document(org.w3c.dom.Document)

Example 22 with Model

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

the class WFObjReader method read.

/**
	 * Reads a wave front object file
	 * @param path name
	 * @param file name
	 * @return model
	 * @throws IOException
	 * @throws ModelIOException
	 */
public static Model read(String pathName, String fileName) throws IOException, ModelFileIOException {
    // create object
    Model model = new Model(pathName + File.separator + fileName, fileName, UpVector.Y_UP, RotationOrder.XYZ, null);
    ArrayList<Vector3> vertices = new ArrayList<Vector3>();
    ArrayList<TextureCoordinate> textureCoordinates = new ArrayList<TextureCoordinate>();
    HashMap<String, Material> materials = model.getMaterials();
    HashMap<String, Group> subGroups = model.getSubGroups();
    HashMap<String, Group> groups = model.getGroups();
    // current group
    Group group = null;
    // model vertices -> group vertices mapping
    HashMap<Integer, Integer> modelGroupVerticesMapping = null;
    // model texture coordinates -> group texture coordinates mapping
    HashMap<Integer, Integer> modelGroupTextureCoordinatesMapping = null;
    // current group data
    ArrayList<Face> groupFacesEntityFaces = null;
    ArrayList<Vector3> groupVertices = null;
    ArrayList<Vector3> groupNormals = null;
    ArrayList<TextureCoordinate> groupTextureCoordinates = null;
    // current group's faces entity
    ArrayList<FacesEntity> groupFacesEntities = null;
    FacesEntity groupFacesEntity = null;
    //
    DataInputStream inputStream = new DataInputStream(FileSystem.getInstance().getInputStream(pathName, fileName));
    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
    //
    try {
        //
        String line;
        while ((line = reader.readLine()) != null) {
            line = line.trim();
            // skip on comments
            if (line.startsWith("#")) {
                continue;
            }
            // determine index of first ' ' which will separate command from arguments
            int commandEndIdx = line.indexOf(' ');
            if (commandEndIdx == -1)
                commandEndIdx = line.length();
            // determine command
            String command = line.substring(0, commandEndIdx).trim().toLowerCase();
            // determine arguments if any exist
            String arguments = command.length() + 1 > line.length() ? "" : line.substring(command.length() + 1);
            // parse
            if (command.equals("mtllib")) {
                String materialFileName = arguments;
                materials = WFObjReader.readMaterials(pathName, materialFileName);
            } else if (command.equals("v")) {
                StringTokenizer t = new StringTokenizer(arguments, " ");
                float x = Float.parseFloat(t.nextToken());
                float y = Float.parseFloat(t.nextToken());
                float z = Float.parseFloat(t.nextToken());
                // add vertex
                vertices.add(new Vector3(x, y, z));
            } else if (command.equals("vt")) {
                StringTokenizer t = new StringTokenizer(arguments, " ");
                float u = Float.parseFloat(t.nextToken());
                float v = Float.parseFloat(t.nextToken());
                textureCoordinates.add(new TextureCoordinate(u, v));
            } else if (command.equals("f")) {
                StringTokenizer t2;
                StringTokenizer t = new StringTokenizer(arguments, " ");
                int v0 = -1;
                int v1 = -1;
                int v2 = -1;
                int vt0 = -1;
                int vt1 = -1;
                int vt2 = -1;
                // parse vertex index 0, vertex texture index 0
                t2 = new StringTokenizer(t.nextToken(), "/");
                v0 = Integer.parseInt(t2.nextToken()) - 1;
                if (t2.hasMoreTokens()) {
                    vt0 = Integer.parseInt(t2.nextToken()) - 1;
                }
                // parse vertex index 1, vertex texture index 1
                t2 = new StringTokenizer(t.nextToken(), "/");
                v1 = Integer.parseInt(t2.nextToken()) - 1;
                if (t2.hasMoreTokens()) {
                    vt1 = Integer.parseInt(t2.nextToken()) - 1;
                }
                // parse vertex index 2, vertex texture index 2
                t2 = new StringTokenizer(t.nextToken(), "/");
                v2 = Integer.parseInt(t2.nextToken()) - 1;
                if (t2.hasMoreTokens()) {
                    vt2 = Integer.parseInt(t2.nextToken()) - 1;
                }
                // check if triangulated
                if (t.hasMoreTokens()) {
                    throw new ModelFileIOException("We only support triangulated meshes");
                }
                Integer mappedVertex = null;
                // map v0 to group
                mappedVertex = modelGroupVerticesMapping.get(v0);
                if (mappedVertex == null) {
                    groupVertices.add(vertices.get(v0).clone());
                    v0 = groupVertices.size() - 1;
                } else {
                    v0 = mappedVertex.intValue();
                }
                // map v1 to group
                mappedVertex = modelGroupVerticesMapping.get(v1);
                if (mappedVertex == null) {
                    groupVertices.add(vertices.get(v1).clone());
                    v1 = groupVertices.size() - 1;
                } else {
                    v1 = mappedVertex.intValue();
                }
                // map v2 to group
                mappedVertex = modelGroupVerticesMapping.get(v2);
                if (mappedVertex == null) {
                    groupVertices.add(vertices.get(v2).clone());
                    v2 = groupVertices.size() - 1;
                } else {
                    v2 = mappedVertex.intValue();
                }
                //
                Integer mappedTextureCoordinate = null;
                // map vt0 to group
                mappedTextureCoordinate = modelGroupTextureCoordinatesMapping.get(vt0);
                if (mappedTextureCoordinate == null) {
                    groupTextureCoordinates.add(textureCoordinates.get(vt0).clone());
                    vt0 = groupTextureCoordinates.size() - 1;
                } else {
                    vt0 = mappedTextureCoordinate.intValue();
                }
                // map vt1 to group
                mappedTextureCoordinate = modelGroupTextureCoordinatesMapping.get(vt1);
                if (mappedTextureCoordinate == null) {
                    groupTextureCoordinates.add(textureCoordinates.get(vt1).clone());
                    vt1 = groupTextureCoordinates.size() - 1;
                } else {
                    vt1 = mappedTextureCoordinate.intValue();
                }
                // map vt2 to group
                mappedTextureCoordinate = modelGroupTextureCoordinatesMapping.get(vt2);
                if (mappedTextureCoordinate == null) {
                    groupTextureCoordinates.add(textureCoordinates.get(vt2).clone());
                    vt2 = groupTextureCoordinates.size() - 1;
                } else {
                    vt2 = mappedTextureCoordinate.intValue();
                }
                // compute vertex normal
                Vector3[] faceVertexNormals = ModelHelper.computeNormals(new Vector3[] { groupVertices.get(v0), groupVertices.get(v1), groupVertices.get(v2) });
                // store group normals
                int n0 = groupNormals.size();
                groupNormals.add(faceVertexNormals[0]);
                int n1 = groupNormals.size();
                groupNormals.add(faceVertexNormals[1]);
                int n2 = groupNormals.size();
                groupNormals.add(faceVertexNormals[2]);
                // create face with vertex indices
                //	we only support triangulated faces
                Face face = new Face(group, v0, v1, v2, n0, n1, n2);
                if (vt0 != -1 && vt1 != -1 && vt2 != -1) {
                    // set optional texture coordinate index
                    face.setTextureCoordinateIndices(vt0, vt1, vt2);
                }
                groupFacesEntityFaces.add(face);
            } else if (command.equals("g")) {
                if (group != null) {
                    // current faces entity
                    if (groupFacesEntityFaces.isEmpty() == false) {
                        groupFacesEntity.setFaces(groupFacesEntityFaces);
                        groupFacesEntities.add(groupFacesEntity);
                    }
                    // group
                    group.setVertices(groupVertices);
                    group.setNormals(groupNormals);
                    group.setTextureCoordinates(groupTextureCoordinates);
                    group.setFacesEntities(groupFacesEntities);
                    group.determineFeatures();
                }
                StringTokenizer t = new StringTokenizer(arguments, " ");
                String name = t.nextToken();
                groupVertices = new ArrayList<Vector3>();
                groupNormals = new ArrayList<Vector3>();
                groupTextureCoordinates = new ArrayList<TextureCoordinate>();
                groupFacesEntityFaces = new ArrayList<Face>();
                group = new Group(model, null, name, name);
                groupFacesEntity = new FacesEntity(group, name);
                groupFacesEntities = new ArrayList<FacesEntity>();
                modelGroupVerticesMapping = new HashMap<Integer, Integer>();
                modelGroupTextureCoordinatesMapping = new HashMap<Integer, Integer>();
                subGroups.put(name, group);
                groups.put(name, group);
            } else if (command.equals("usemtl")) {
                if (group != null) {
                    // current faces entity
                    if (groupFacesEntityFaces.isEmpty() == false) {
                        groupFacesEntity.setFaces(groupFacesEntityFaces);
                        groupFacesEntities.add(groupFacesEntity);
                    }
                    // set up new one
                    groupFacesEntity = new FacesEntity(group, "#" + groupFacesEntities.size());
                    groupFacesEntityFaces = new ArrayList<Face>();
                }
                groupFacesEntity.setMaterial(materials.get(arguments));
            } else {
            // not supported
            }
        }
        // finish last group
        if (group != null) {
            // current faces entity
            if (groupFacesEntityFaces.isEmpty() == false) {
                groupFacesEntity.setFaces(groupFacesEntityFaces);
                groupFacesEntities.add(groupFacesEntity);
            }
            // group
            group.setVertices(groupVertices);
            group.setNormals(groupNormals);
            group.setTextureCoordinates(groupTextureCoordinates);
            group.setFacesEntities(groupFacesEntities);
            group.determineFeatures();
        }
    } finally {
        // close resouces
        reader.close();
        inputStream.close();
    }
    // 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) InputStreamReader(java.io.InputStreamReader) ArrayList(java.util.ArrayList) Vector3(net.drewke.tdme.math.Vector3) Material(net.drewke.tdme.engine.model.Material) DataInputStream(java.io.DataInputStream) StringTokenizer(java.util.StringTokenizer) Model(net.drewke.tdme.engine.model.Model) BufferedReader(java.io.BufferedReader) TextureCoordinate(net.drewke.tdme.engine.model.TextureCoordinate) Face(net.drewke.tdme.engine.model.Face)

Example 23 with Model

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

the class PhysicsTest2 method init.

/*
	 * (non-Javadoc)
	 * @see com.jogamp.opengl.GLEventListener#init(com.jogamp.opengl.GLAutoDrawable)
	 */
public void init(GLAutoDrawable drawable) {
    drawable.getGL().setSwapInterval(0);
    engine.init(drawable);
    Object3D entity;
    // cam
    Camera cam = engine.getCamera();
    // Test scenario serg
    cam.setZNear(0.1f);
    cam.setZFar(100.00f);
    cam.getLookFrom().set(0f, 30f, 30f);
    cam.getLookAt().set(0f, 0f, 0f);
    // lights
    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(0f, 20000f, 0f, 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);
    // ground
    OrientedBoundingBox ground = new OrientedBoundingBox(new Vector3(0f, 0f, 0f), OrientedBoundingBox.AABB_AXIS_X.clone(), OrientedBoundingBox.AABB_AXIS_Y.clone(), OrientedBoundingBox.AABB_AXIS_Z.clone(), new Vector3(30f, 1f, 30f));
    Model groundModel = PrimitiveModel.createModel(ground, "ground_model");
    groundModel.getMaterials().get("tdme.primitive.material").getAmbientColor().set(0.8f, 0.8f, 0.8f, 1f);
    groundModel.getMaterials().get("tdme.primitive.material").getDiffuseColor().set(1f, 1f, 1f, 1f);
    entity = new Object3D("ground", groundModel);
    entity.getTranslation().setY(-1f);
    entity.update();
    engine.addEntity(entity);
    world.addStaticRigidBody("ground", true, RIGID_TYPEID_STANDARD, entity, ground, 0.5f);
    OrientedBoundingBox box = new OrientedBoundingBox(new Vector3(0f, 0f, 0f), OrientedBoundingBox.AABB_AXIS_X.clone(), OrientedBoundingBox.AABB_AXIS_Y.clone(), OrientedBoundingBox.AABB_AXIS_Z.clone(), new Vector3(1f, 1f, 1f));
    Model boxModel = PrimitiveModel.createModel(box, "box_model");
    boxModel.getMaterials().get("tdme.primitive.material").getAmbientColor().set(0.8f, 0.5f, 0.5f, 1f);
    boxModel.getMaterials().get("tdme.primitive.material").getDiffuseColor().set(1f, 0f, 0f, 1f);
    // boxes
    for (int i = 0; i < BOX_COUNT; i++) {
        entity = new Object3D("box" + i, boxModel);
        entity.setDynamicShadowingEnabled(true);
        entity.getTranslation().addY(i * 2f + 1f);
        //entity.getTranslation().addX(i * 50f);
        entity.update();
        engine.addEntity(entity);
        world.addRigidBody("box" + i, true, RIGID_TYPEID_STANDARD, entity, box, 0f, 0.8f, 100f, RigidBody.computeInertiaMatrix(box, 100f, 1f, 1f, 1f));
    }
}
Also used : OrientedBoundingBox(net.drewke.tdme.engine.primitives.OrientedBoundingBox) Light(net.drewke.tdme.engine.Light) Model(net.drewke.tdme.engine.model.Model) PrimitiveModel(net.drewke.tdme.engine.primitives.PrimitiveModel) Vector3(net.drewke.tdme.math.Vector3) Camera(net.drewke.tdme.engine.Camera) Object3D(net.drewke.tdme.engine.Object3D)

Example 24 with Model

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

the class PhysicsTest3 method init.

/*
	 * (non-Javadoc)
	 * @see com.jogamp.opengl.GLEventListener#init(com.jogamp.opengl.GLAutoDrawable)
	 */
public void init(GLAutoDrawable drawable) {
    drawable.getGL().setSwapInterval(0);
    engine.init(drawable);
    Object3D entity;
    // cam
    Camera cam = engine.getCamera();
    cam.setZNear(0.10f);
    cam.setZFar(50.00f);
    cam.getLookFrom().set(0f, 4f * 2.5f, -6f * 2.5f);
    cam.getLookAt().set(0f, 0.0f, 0f);
    cam.computeUpVector(cam.getLookFrom(), cam.getLookAt(), cam.getUpVector());
    // lights
    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(0f, 20000f, 0f, 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);
    // side
    OrientedBoundingBox side = new OrientedBoundingBox(new Vector3(0f, 0f, 0f), OrientedBoundingBox.AABB_AXIS_X.clone(), OrientedBoundingBox.AABB_AXIS_Y.clone(), OrientedBoundingBox.AABB_AXIS_Z.clone(), new Vector3(1f, 16f, 8f));
    Model sideModel = PrimitiveModel.createModel(side, "side_model");
    sideModel.getMaterials().get("tdme.primitive.material").getAmbientColor().set(0.8f, 0.8f, 0.8f, 1f);
    sideModel.getMaterials().get("tdme.primitive.material").getDiffuseColor().set(1f, 1f, 1f, 1f);
    // far
    OrientedBoundingBox nearFar = new OrientedBoundingBox(new Vector3(0f, 0f, 0f), OrientedBoundingBox.AABB_AXIS_X.clone(), OrientedBoundingBox.AABB_AXIS_Y.clone(), OrientedBoundingBox.AABB_AXIS_Z.clone(), new Vector3(8f, 16f, 1f));
    Model nearFarModel = PrimitiveModel.createModel(nearFar, "far_model");
    nearFarModel.getMaterials().get("tdme.primitive.material").getAmbientColor().set(0.8f, 0.8f, 0.8f, 1f);
    nearFarModel.getMaterials().get("tdme.primitive.material").getDiffuseColor().set(1f, 1f, 1f, 1f);
    // far
    entity = new Object3D("far", nearFarModel);
    entity.getTranslation().addZ(+9f);
    entity.update();
    engine.addEntity(entity);
    world.addStaticRigidBody("far", true, RIGID_TYPEID_STANDARD, entity, nearFar, 0.5f);
    // near
    entity = new Object3D("near", nearFarModel);
    entity.getTranslation().addZ(-9f);
    entity.getEffectColorMul().set(1f, 1f, 1f, 0f);
    entity.update();
    engine.addEntity(entity);
    world.addStaticRigidBody("near", true, RIGID_TYPEID_STANDARD, entity, nearFar, 0.5f);
    // side left
    entity = new Object3D("sideright", sideModel);
    entity.getTranslation().addX(-9f);
    entity.update();
    engine.addEntity(entity);
    world.addStaticRigidBody("sideright", true, RIGID_TYPEID_STANDARD, entity, side, 0.5f);
    // side right
    entity = new Object3D("sideleft", sideModel);
    entity.getTranslation().addX(9f);
    entity.update();
    engine.addEntity(entity);
    world.addStaticRigidBody("sideleft", true, RIGID_TYPEID_STANDARD, entity, side, 0.5f);
    // box
    OrientedBoundingBox box = new OrientedBoundingBox(new Vector3(0f, 0f, 0f), OrientedBoundingBox.AABB_AXIS_X.clone(), OrientedBoundingBox.AABB_AXIS_Y.clone(), OrientedBoundingBox.AABB_AXIS_Z.clone(), new Vector3(0.6f, 0.6f, 0.6f));
    Model boxModel = PrimitiveModel.createModel(box, "box_model");
    boxModel.getMaterials().get("tdme.primitive.material").getAmbientColor().set(0.8f, 0.5f, 0.5f, 1f);
    boxModel.getMaterials().get("tdme.primitive.material").getDiffuseColor().set(1f, 0f, 0f, 1f);
    // boxes
    for (int i = 0; i < BOX_COUNT; i++) {
        entity = new Object3D("box" + i, boxModel);
        entity.setDynamicShadowingEnabled(true);
        entity.getTranslation().addY(10f + i * 3.0f);
        entity.getTranslation().addX(-2f + i * 0.1f);
        entity.update();
        engine.addEntity(entity);
        world.addRigidBody("box" + i, true, RIGID_TYPEID_STANDARD, entity, box, 0f, 1f, 100f, RigidBody.computeInertiaMatrix(box, 100f, 1f, 1f, 1f));
    }
    // stack
    for (int i = 0; i < BOXSTACK_COUNT; i++) {
        entity = new Object3D("box" + (BOX_COUNT + i), boxModel);
        entity.setDynamicShadowingEnabled(true);
        entity.getTranslation().addY(1.6f + (i * 1.2f));
        entity.getTranslation().addX(+3f);
        entity.getTranslation().addZ(-5f);
        entity.update();
        engine.addEntity(entity);
        world.addRigidBody("box" + (BOX_COUNT + i), true, RIGID_TYPEID_STANDARD, entity, box, 0f, 1f, 100f, RigidBody.computeInertiaMatrix(box, 100f, 1f, 1f, 1f));
    }
    // sphere
    Sphere sphere = new Sphere(new Vector3(0f, 0f, 0f), 0.4f);
    Model sphereModel = PrimitiveModel.createModel(sphere, "sphere_model");
    sphereModel.getMaterials().get("tdme.primitive.material").getAmbientColor().set(0.5f, 0.8f, 0.8f, 1f);
    sphereModel.getMaterials().get("tdme.primitive.material").getDiffuseColor().set(0f, 1f, 1f, 1f);
    // spheres
    for (int i = 0; i < SPHERE_COUNT; i++) {
        entity = new Object3D("sphere" + i, sphereModel);
        entity.setDynamicShadowingEnabled(true);
        entity.getTranslation().addY(12f + (i * 1f));
        entity.getTranslation().addX(0.45f * i - 3f);
        entity.getTranslation().addZ(0.1f * i - 3f);
        entity.update();
        engine.addEntity(entity);
        world.addRigidBody("sphere" + i, true, RIGID_TYPEID_STANDARD, entity, sphere, 0.75f, 0.4f, 10f, RigidBody.computeInertiaMatrix(sphere, 10f, 1f, 1f, 1f));
    }
    // sphere
    Capsule capsule = new Capsule(new Vector3(0f, 0.5f, 0f), new Vector3(0f, -0.5f, 0f), 0.25f);
    Model capsuleModel = PrimitiveModel.createModel(capsule, "capsule_model");
    capsuleModel.getMaterials().get("tdme.primitive.material").getAmbientColor().set(0.8f, 0.0f, 0.8f, 1f);
    capsuleModel.getMaterials().get("tdme.primitive.material").getDiffuseColor().set(1f, 0f, 1f, 1f);
    //
    for (int i = 0; i < CAPSULE_COUNT; i++) {
        entity = new Object3D("capsule" + i, capsuleModel);
        entity.setDynamicShadowingEnabled(true);
        entity.getTranslation().addY(14f + (i * 2f));
        entity.getTranslation().addX((i * 0.5f));
        // entity.getPivot().set(capsule.getCenter());
        entity.update();
        engine.addEntity(entity);
        world.addRigidBody("capsule" + i, true, RIGID_TYPEID_STANDARD, entity, capsule, 0.0f, 0.4f, 3f, RigidBody.computeInertiaMatrix(capsule, 3f, 1f, 1f, 1f));
    }
    // sphere
    //Capsule capsuleBig = new Capsule(
    //	new Vector3(0f,+1f,0f),
    //	new Vector3(0f,-1f,0f),
    //	0.5f
    //);
    OrientedBoundingBox capsuleBig = new OrientedBoundingBox(new Vector3(0f, 0f, 0f), OrientedBoundingBox.AABB_AXIS_X.clone(), OrientedBoundingBox.AABB_AXIS_Y.clone(), OrientedBoundingBox.AABB_AXIS_Z.clone(), new Vector3(0.5f, 1f, 0.5f));
    Model capsuleBigModel = PrimitiveModel.createModel(capsuleBig, "capsulebig_model");
    capsuleBigModel.getMaterials().get("tdme.primitive.material").getAmbientColor().set(1f, 0.8f, 0.8f, 1f);
    capsuleBigModel.getMaterials().get("tdme.primitive.material").getDiffuseColor().set(1f, 0f, 0f, 1f);
    System.out.println(capsuleBig.getCenter());
    //
    entity = new Object3D("capsulebig1", capsuleBigModel);
    entity.setDynamicShadowingEnabled(true);
    entity.getTranslation().addY(5f);
    entity.getTranslation().addX(-2f);
    entity.update();
    engine.addEntity(entity);
    world.addRigidBody("capsulebig1", true, RIGID_TYPEID_STANDARD, entity, capsuleBig, 0f, 1f, 80f, RigidBody.getNoRotationInertiaMatrix());
    //
    entity = new Object3D("capsulebig2", capsuleBigModel);
    entity.setDynamicShadowingEnabled(true);
    entity.getTranslation().addY(5f);
    entity.getTranslation().addX(+2f);
    entity.update();
    engine.addEntity(entity);
    world.addRigidBody("capsulebig2", true, RIGID_TYPEID_STANDARD, entity, capsuleBig, 0f, 1f, 100f, RigidBody.getNoRotationInertiaMatrix());
    //
    try {
        Model _terrainModel = DAEReader.read("resources/tests/environment/terrain_test", "terrain_test.dae");
        _terrainModel.getImportTransformationsMatrix().scale(1.5f);
        entity = new Object3D("terrain", _terrainModel);
        entity.getTranslation().setY(-4f);
        entity.update();
        engine.addEntity(entity);
        ArrayList<ConvexMesh> groundConvexMeshes = new ArrayList<ConvexMesh>();
        ConvexMesh.createTerrainConvexMeshes(new Object3DModel(_terrainModel), groundConvexMeshes);
        for (int i = 0; i < groundConvexMeshes.size(); i++) {
            world.addStaticRigidBody("ground" + i, true, RIGID_TYPEID_STANDARD, entity, groundConvexMeshes.get(i), 0.5f);
        }
        // load barrel, set up bounding volume
        Model _barrel = DAEReader.read("resources/tests/models/barrel", "barrel.dae");
        // _barrel.getImportTransformationsMatrix().scale(2f);
        ConvexMesh barrelBoundingVolume = new ConvexMesh(new Object3DModel(_barrel));
        // set up barrel 1 in 3d engine
        entity = new Object3D("barrel1", _barrel);
        entity.setDynamicShadowingEnabled(true);
        entity.getTranslation().addY(5f);
        entity.getTranslation().addX(+4f);
        entity.getScale().set(2f, 2f, 2f);
        entity.update();
        engine.addEntity(entity);
        world.addRigidBody("barrel1", true, RIGID_TYPEID_STANDARD, entity, barrelBoundingVolume, 0f, 1f, 100f, RigidBody.computeInertiaMatrix(barrelBoundingVolume, 100f, 1f, 1f, 1f));
        // set up barrel 2 in 3d engine
        entity = new Object3D("barrel2", _barrel);
        entity.setDynamicShadowingEnabled(true);
        entity.getTranslation().addY(5f);
        entity.getTranslation().addX(+6f);
        entity.getScale().set(2f, 2f, 2f);
        entity.update();
        engine.addEntity(entity);
        world.addRigidBody("barrel2", true, RIGID_TYPEID_STANDARD, entity, barrelBoundingVolume, 0f, 1f, 100f, RigidBody.computeInertiaMatrix(barrelBoundingVolume, 100f, 1f, 1f, 1f));
        // load cone, set up bounding volume
        Model _cone = DAEReader.read("resources/tests/models/cone", "cone.dae");
        // _barrel.getImportTransformationsMatrix().scale(2f);
        ConvexMesh coneBoundingVolume = new ConvexMesh(new Object3DModel(_cone));
        // set up cone 1 in 3d engine
        entity = new Object3D("cone1", _cone);
        entity.setDynamicShadowingEnabled(true);
        entity.getTranslation().addY(5f);
        entity.getTranslation().addX(-4f);
        entity.getScale().set(3f, 3f, 3f);
        entity.update();
        engine.addEntity(entity);
        world.addRigidBody("cone1", true, RIGID_TYPEID_STANDARD, entity, coneBoundingVolume, 0f, 1f, 100f, RigidBody.computeInertiaMatrix(coneBoundingVolume, 100f, 1f, 1f, 1f));
        // set up cone 1 in 3d engine
        entity = new Object3D("cone2", _cone);
        entity.setDynamicShadowingEnabled(true);
        entity.getTranslation().addY(5f);
        entity.getTranslation().addX(-5f);
        entity.getScale().set(3f, 3f, 3f);
        entity.update();
        engine.addEntity(entity);
        world.addRigidBody("cone2", true, RIGID_TYPEID_STANDARD, entity, coneBoundingVolume, 0f, 1f, 100f, RigidBody.computeInertiaMatrix(coneBoundingVolume, 100f, 1f, 1f, 1f));
        // load cone, set up bounding volume
        Model _tire = DAEReader.read("resources/tests/models/tire", "tire.dae");
        // _barrel.getImportTransformationsMatrix().scale(2f);
        ConvexMesh tireBoundingVolume = new ConvexMesh(new Object3DModel(_tire));
        // set up tire 1 in 3d engine
        entity = new Object3D("tire1", _tire);
        entity.setDynamicShadowingEnabled(true);
        entity.getRotations().add(new Rotation(90f, new Vector3(1f, 0f, 0f)));
        entity.getTranslation().addY(5f);
        entity.getTranslation().addX(-4f);
        entity.getTranslation().addZ(-2f);
        entity.getScale().set(2f, 2f, 2f);
        entity.update();
        engine.addEntity(entity);
        world.addRigidBody("tire1", true, RIGID_TYPEID_STANDARD, entity, tireBoundingVolume, 0f, 1f, 100f, RigidBody.computeInertiaMatrix(tireBoundingVolume, 100f, 1f, 1f, 1f));
        // set up tire 1 in 3d engine
        entity = new Object3D("tire2", _tire);
        entity.setDynamicShadowingEnabled(true);
        entity.getRotations().add(new Rotation(90f, new Vector3(1f, 0f, 0f)));
        entity.getTranslation().addY(5f);
        entity.getTranslation().addX(-6f);
        entity.getTranslation().addZ(-2f);
        entity.getScale().set(2f, 2f, 2f);
        entity.update();
        engine.addEntity(entity);
        world.addRigidBody("tire2", true, RIGID_TYPEID_STANDARD, entity, tireBoundingVolume, 0f, 1f, 100f, RigidBody.computeInertiaMatrix(tireBoundingVolume, 100f, 1f, 1f, 1f));
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : ArrayList(java.util.ArrayList) Vector3(net.drewke.tdme.math.Vector3) Capsule(net.drewke.tdme.engine.primitives.Capsule) ConvexMesh(net.drewke.tdme.engine.primitives.ConvexMesh) Object3DModel(net.drewke.tdme.engine.Object3DModel) Rotation(net.drewke.tdme.engine.Rotation) Object3D(net.drewke.tdme.engine.Object3D) Sphere(net.drewke.tdme.engine.primitives.Sphere) OrientedBoundingBox(net.drewke.tdme.engine.primitives.OrientedBoundingBox) Light(net.drewke.tdme.engine.Light) Model(net.drewke.tdme.engine.model.Model) PrimitiveModel(net.drewke.tdme.engine.primitives.PrimitiveModel) Object3DModel(net.drewke.tdme.engine.Object3DModel) Camera(net.drewke.tdme.engine.Camera)

Example 25 with Model

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

the class EngineTest method createWallModel.

/**
	 * Create wall model
	 * @return
	 */
private Model createWallModel() {
    // wall model
    Model wall = new Model("wall", "wall", UpVector.Y_UP, RotationOrder.XYZ, null);
    // wall material
    Material wallMaterial = new Material("wall");
    wall.getMaterials().put("wall", wallMaterial);
    //	group
    Group wallGroup = new Group(wall, null, "wall", "wall");
    //	faces entity
    //		far plane
    FacesEntity groupFacesEntityFarPlane = new FacesEntity(wallGroup, "wall");
    wallMaterial.getAmbientColor().set(1f, 1f, 1f, 1f);
    wallMaterial.getDiffuseColor().set(1f, 1f, 1f, 1f);
    groupFacesEntityFarPlane.setMaterial(wallMaterial);
    //	faces entity 
    ArrayList<FacesEntity> groupFacesEntities = new ArrayList<FacesEntity>();
    groupFacesEntities.add(groupFacesEntityFarPlane);
    //	vertices
    ArrayList<Vector3> vertices = new ArrayList<Vector3>();
    // left, near, far plane
    vertices.add(new Vector3(-4f, 0f, +4f));
    // left, far, far plane
    vertices.add(new Vector3(-4f, +4f, +4f));
    // right far, far plane
    vertices.add(new Vector3(+4f, +4f, +4f));
    // right, near, far plane
    vertices.add(new Vector3(+4f, 0f, +4f));
    //	normals
    ArrayList<Vector3> normals = new ArrayList<Vector3>();
    //		far plane
    normals.add(new Vector3(0f, 0f, -1f));
    // texture coordinates
    ArrayList<TextureCoordinate> textureCoordinates = new ArrayList<TextureCoordinate>();
    textureCoordinates.add(new TextureCoordinate(0f, 0f));
    textureCoordinates.add(new TextureCoordinate(0f, 1f));
    textureCoordinates.add(new TextureCoordinate(1f, 1f));
    textureCoordinates.add(new TextureCoordinate(1f, 0f));
    //	faces ground far plane
    ArrayList<Face> facesFarPlane = new ArrayList<Face>();
    facesFarPlane.add(new Face(wallGroup, 0, 1, 2, 0, 0, 0, 0, 1, 2));
    facesFarPlane.add(new Face(wallGroup, 2, 3, 0, 0, 0, 0, 2, 3, 0));
    // set up faces entity
    groupFacesEntityFarPlane.setFaces(facesFarPlane);
    // setup ground group
    wallGroup.setVertices(vertices);
    wallGroup.setNormals(normals);
    wallGroup.setTextureCoordinates(textureCoordinates);
    wallGroup.setFacesEntities(groupFacesEntities);
    // determine features
    wallGroup.determineFeatures();
    // register group
    wall.getGroups().put("wall", wallGroup);
    wall.getSubGroups().put("wall", wallGroup);
    // prepare for indexed rendering
    ModelHelper.prepareForIndexedRendering(wall);
    //
    return wall;
}
Also used : FacesEntity(net.drewke.tdme.engine.model.FacesEntity) Group(net.drewke.tdme.engine.model.Group) Model(net.drewke.tdme.engine.model.Model) Object3DModel(net.drewke.tdme.engine.Object3DModel) PrimitiveModel(net.drewke.tdme.engine.primitives.PrimitiveModel) ArrayList(java.util.ArrayList) Material(net.drewke.tdme.engine.model.Material) Vector3(net.drewke.tdme.math.Vector3) TextureCoordinate(net.drewke.tdme.engine.model.TextureCoordinate) Face(net.drewke.tdme.engine.model.Face)

Aggregations

Model (net.drewke.tdme.engine.model.Model)25 Vector3 (net.drewke.tdme.math.Vector3)20 ArrayList (java.util.ArrayList)10 FacesEntity (net.drewke.tdme.engine.model.FacesEntity)10 Group (net.drewke.tdme.engine.model.Group)10 Material (net.drewke.tdme.engine.model.Material)10 PrimitiveModel (net.drewke.tdme.engine.primitives.PrimitiveModel)10 Face (net.drewke.tdme.engine.model.Face)8 Object3D (net.drewke.tdme.engine.Object3D)6 Camera (net.drewke.tdme.engine.Camera)5 Light (net.drewke.tdme.engine.Light)5 Object3DModel (net.drewke.tdme.engine.Object3DModel)5 File (java.io.File)4 Rotation (net.drewke.tdme.engine.Rotation)4 TextureCoordinate (net.drewke.tdme.engine.model.TextureCoordinate)4 BoundingBox (net.drewke.tdme.engine.primitives.BoundingBox)4 ConvexMesh (net.drewke.tdme.engine.primitives.ConvexMesh)4 OrientedBoundingBox (net.drewke.tdme.engine.primitives.OrientedBoundingBox)4 LevelEditorEntity (net.drewke.tdme.tools.shared.model.LevelEditorEntity)4 UpVector (net.drewke.tdme.engine.model.Model.UpVector)3