Search in sources :

Example 6 with MeshPart

use of com.badlogic.gdx.graphics.g3d.model.MeshPart in project libgdx by libgdx.

the class SoftBodyTest method create.

@Override
public void create() {
    super.create();
    world.add("ground", 0f, 0f, 0f).setColor(0.25f + 0.5f * (float) Math.random(), 0.25f + 0.5f * (float) Math.random(), 0.25f + 0.5f * (float) Math.random(), 1f);
    float x0 = -2f, y0 = 6f, z0 = -2f;
    float x1 = 8f, y1 = 6f, z1 = 8f;
    Vector3 patch00 = new Vector3(x0, y0, z0);
    Vector3 patch10 = new Vector3(x1, y1, z0);
    Vector3 patch01 = new Vector3(x0, y0, z1);
    Vector3 patch11 = new Vector3(x1, y1, z1);
    softBody = btSoftBodyHelpers.CreatePatch(worldInfo, patch00, patch10, patch01, patch11, 15, 15, 15, false);
    softBody.takeOwnership();
    softBody.setTotalMass(100f);
    ((btSoftRigidDynamicsWorld) (world.collisionWorld)).addSoftBody(softBody);
    final int vertCount = softBody.getNodeCount();
    final int faceCount = softBody.getFaceCount();
    mesh = new Mesh(false, vertCount, faceCount * 3, new VertexAttribute(Usage.Position, 3, ShaderProgram.POSITION_ATTRIBUTE), new VertexAttribute(Usage.Normal, 3, ShaderProgram.NORMAL_ATTRIBUTE), new VertexAttribute(Usage.TextureCoordinates, 2, ShaderProgram.TEXCOORD_ATTRIBUTE + "0"));
    final int vertSize = mesh.getVertexSize() / 4;
    mesh.getVerticesBuffer().position(0);
    mesh.getVerticesBuffer().limit(vertCount * vertSize);
    mesh.getIndicesBuffer().position(0);
    mesh.getIndicesBuffer().limit(faceCount * 3);
    softBody.getVertices(mesh.getVerticesBuffer(), vertCount, mesh.getVertexSize(), 0);
    softBody.getIndices(mesh.getIndicesBuffer(), faceCount);
    final float[] verts = new float[vertCount * vertSize];
    final int uvOffset = mesh.getVertexAttribute(Usage.TextureCoordinates).offset / 4;
    final int normalOffset = mesh.getVertexAttribute(Usage.Normal).offset / 4;
    mesh.getVertices(verts);
    for (int i = 0; i < vertCount; i++) {
        verts[i * vertSize + normalOffset] = 0f;
        verts[i * vertSize + normalOffset + 1] = 1f;
        verts[i * vertSize + normalOffset + 2] = 0f;
        verts[i * vertSize + uvOffset] = (verts[i * vertSize] - x0) / (x1 - x0);
        verts[i * vertSize + uvOffset + 1] = (verts[i * vertSize + 2] - z0) / (z1 - z0);
    }
    mesh.setVertices(verts);
    texture = new Texture(Gdx.files.internal("data/badlogic.jpg"));
    ModelBuilder builder = new ModelBuilder();
    builder.begin();
    builder.part(new MeshPart("", mesh, 0, mesh.getNumIndices(), GL20.GL_TRIANGLES), new Material(TextureAttribute.createDiffuse(texture), ColorAttribute.createSpecular(Color.WHITE), FloatAttribute.createShininess(64f), IntAttribute.createCullFace(0)));
    model = builder.end();
    instance = new ModelInstance(model);
    world.add(new BulletEntity(instance, null));
}
Also used : com.badlogic.gdx.physics.bullet.softbody.btSoftRigidDynamicsWorld(com.badlogic.gdx.physics.bullet.softbody.btSoftRigidDynamicsWorld) Mesh(com.badlogic.gdx.graphics.Mesh) Vector3(com.badlogic.gdx.math.Vector3) Material(com.badlogic.gdx.graphics.g3d.Material) MeshPart(com.badlogic.gdx.graphics.g3d.model.MeshPart) Texture(com.badlogic.gdx.graphics.Texture) ModelInstance(com.badlogic.gdx.graphics.g3d.ModelInstance) ModelBuilder(com.badlogic.gdx.graphics.g3d.utils.ModelBuilder) VertexAttribute(com.badlogic.gdx.graphics.VertexAttribute)

Example 7 with MeshPart

use of com.badlogic.gdx.graphics.g3d.model.MeshPart in project libgdx by libgdx.

the class SoftMeshTest method create.

@Override
public void create() {
    super.create();
    world.maxSubSteps = 20;
    world.add("ground", 0f, 0f, 0f).setColor(0.25f + 0.5f * (float) Math.random(), 0.25f + 0.5f * (float) Math.random(), 0.25f + 0.5f * (float) Math.random(), 1f);
    // Note: not every model is suitable for a one on one translation with a soft body, a better model might be added later.
    model = objLoader.loadModel(Gdx.files.internal("data/wheel.obj"));
    MeshPart meshPart = model.nodes.get(0).parts.get(0).meshPart;
    meshPart.mesh.scale(6, 6, 6);
    indexMap = BufferUtils.newShortBuffer(meshPart.size);
    positionOffset = meshPart.mesh.getVertexAttribute(Usage.Position).offset;
    normalOffset = meshPart.mesh.getVertexAttribute(Usage.Normal).offset;
    softBody = new btSoftBody(worldInfo, meshPart.mesh.getVerticesBuffer(), meshPart.mesh.getVertexSize(), positionOffset, normalOffset, meshPart.mesh.getIndicesBuffer(), meshPart.offset, meshPart.size, indexMap, 0);
    // Set mass of the first vertex to zero so its unmovable, comment out this line to make it a fully dynamic body.
    softBody.setMass(0, 0);
    com.badlogic.gdx.physics.bullet.softbody.btSoftBody.Material pm = softBody.appendMaterial();
    pm.setKLST(0.2f);
    pm.setFlags(0);
    softBody.generateBendingConstraints(2, pm);
    // Be careful increasing iterations, it decreases performance (but increases accuracy).
    softBody.setConfig_piterations(7);
    softBody.setConfig_kDF(0.2f);
    softBody.randomizeConstraints();
    softBody.setTotalMass(1);
    softBody.translate(tmpV.set(1, 5, 1));
    ((btSoftRigidDynamicsWorld) (world.collisionWorld)).addSoftBody(softBody);
    world.add(entity = new BulletEntity(model, (btCollisionObject) null, 1, 5, 1));
}
Also used : com.badlogic.gdx.physics.bullet.softbody.btSoftRigidDynamicsWorld(com.badlogic.gdx.physics.bullet.softbody.btSoftRigidDynamicsWorld) com.badlogic.gdx.physics.bullet.softbody.btSoftBody(com.badlogic.gdx.physics.bullet.softbody.btSoftBody) MeshPart(com.badlogic.gdx.graphics.g3d.model.MeshPart)

Example 8 with MeshPart

use of com.badlogic.gdx.graphics.g3d.model.MeshPart in project libgdx by libgdx.

the class Model method loadNode.

protected Node loadNode(ModelNode modelNode) {
    Node node = new Node();
    node.id = modelNode.id;
    if (modelNode.translation != null)
        node.translation.set(modelNode.translation);
    if (modelNode.rotation != null)
        node.rotation.set(modelNode.rotation);
    if (modelNode.scale != null)
        node.scale.set(modelNode.scale);
    // FIXME create temporary maps for faster lookup?
    if (modelNode.parts != null) {
        for (ModelNodePart modelNodePart : modelNode.parts) {
            MeshPart meshPart = null;
            Material meshMaterial = null;
            if (modelNodePart.meshPartId != null) {
                for (MeshPart part : meshParts) {
                    if (modelNodePart.meshPartId.equals(part.id)) {
                        meshPart = part;
                        break;
                    }
                }
            }
            if (modelNodePart.materialId != null) {
                for (Material material : materials) {
                    if (modelNodePart.materialId.equals(material.id)) {
                        meshMaterial = material;
                        break;
                    }
                }
            }
            if (meshPart == null || meshMaterial == null)
                throw new GdxRuntimeException("Invalid node: " + node.id);
            if (meshPart != null && meshMaterial != null) {
                NodePart nodePart = new NodePart();
                nodePart.meshPart = meshPart;
                nodePart.material = meshMaterial;
                node.parts.add(nodePart);
                if (modelNodePart.bones != null)
                    nodePartBones.put(nodePart, modelNodePart.bones);
            }
        }
    }
    if (modelNode.children != null) {
        for (ModelNode child : modelNode.children) {
            node.addChild(loadNode(child));
        }
    }
    return node;
}
Also used : GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) ModelNode(com.badlogic.gdx.graphics.g3d.model.data.ModelNode) Node(com.badlogic.gdx.graphics.g3d.model.Node) ModelNodePart(com.badlogic.gdx.graphics.g3d.model.data.ModelNodePart) ModelMaterial(com.badlogic.gdx.graphics.g3d.model.data.ModelMaterial) ModelMeshPart(com.badlogic.gdx.graphics.g3d.model.data.ModelMeshPart) MeshPart(com.badlogic.gdx.graphics.g3d.model.MeshPart) NodePart(com.badlogic.gdx.graphics.g3d.model.NodePart) ModelNodePart(com.badlogic.gdx.graphics.g3d.model.data.ModelNodePart) ModelNode(com.badlogic.gdx.graphics.g3d.model.data.ModelNode)

Example 9 with MeshPart

use of com.badlogic.gdx.graphics.g3d.model.MeshPart in project libgdx by libgdx.

the class ModelCache method end.

/** Finishes creating the cache, must be called after a call to {@link #begin()}, only after this call the cache will be valid
	 * (until the next call to {@link #begin()}). Calling this method will process all renderables added using one of the add(...)
	 * methods and will combine them if possible. */
public void end() {
    if (!building)
        throw new GdxRuntimeException("Call begin() prior to calling end()");
    building = false;
    if (items.size == 0)
        return;
    sorter.sort(camera, items);
    int itemCount = items.size;
    int initCount = renderables.size;
    final Renderable first = items.get(0);
    VertexAttributes vertexAttributes = first.meshPart.mesh.getVertexAttributes();
    Material material = first.material;
    int primitiveType = first.meshPart.primitiveType;
    int offset = renderables.size;
    meshBuilder.begin(vertexAttributes);
    MeshPart part = meshBuilder.part("", primitiveType, meshPartPool.obtain());
    renderables.add(obtainRenderable(material, primitiveType));
    for (int i = 0, n = items.size; i < n; ++i) {
        final Renderable renderable = items.get(i);
        final VertexAttributes va = renderable.meshPart.mesh.getVertexAttributes();
        final Material mat = renderable.material;
        final int pt = renderable.meshPart.primitiveType;
        final boolean sameMesh = va.equals(vertexAttributes) && // comparing indices and vertices...
        renderable.meshPart.size + meshBuilder.getNumVertices() < Short.MAX_VALUE;
        final boolean samePart = sameMesh && pt == primitiveType && mat.same(material, true);
        if (!samePart) {
            if (!sameMesh) {
                final Mesh mesh = meshBuilder.end(meshPool.obtain(vertexAttributes, meshBuilder.getNumVertices(), meshBuilder.getNumIndices()));
                while (offset < renderables.size) renderables.get(offset++).meshPart.mesh = mesh;
                meshBuilder.begin(vertexAttributes = va);
            }
            final MeshPart newPart = meshBuilder.part("", pt, meshPartPool.obtain());
            final Renderable previous = renderables.get(renderables.size - 1);
            previous.meshPart.offset = part.offset;
            previous.meshPart.size = part.size;
            part = newPart;
            renderables.add(obtainRenderable(material = mat, primitiveType = pt));
        }
        meshBuilder.setVertexTransform(renderable.worldTransform);
        meshBuilder.addMesh(renderable.meshPart.mesh, renderable.meshPart.offset, renderable.meshPart.size);
    }
    final Mesh mesh = meshBuilder.end(meshPool.obtain(vertexAttributes, meshBuilder.getNumVertices(), meshBuilder.getNumIndices()));
    while (offset < renderables.size) renderables.get(offset++).meshPart.mesh = mesh;
    final Renderable previous = renderables.get(renderables.size - 1);
    previous.meshPart.offset = part.offset;
    previous.meshPart.size = part.size;
}
Also used : GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) VertexAttributes(com.badlogic.gdx.graphics.VertexAttributes) Mesh(com.badlogic.gdx.graphics.Mesh) MeshPart(com.badlogic.gdx.graphics.g3d.model.MeshPart)

Example 10 with MeshPart

use of com.badlogic.gdx.graphics.g3d.model.MeshPart in project libgdx by libgdx.

the class MeshBuilder method end.

/** End building the mesh and returns the mesh
	 * @param mesh The mesh to receive the built vertices and indices, must have the same attributes and must be big enough to hold
	 *           the data, any existing data will be overwritten. */
public Mesh end(Mesh mesh) {
    endpart();
    if (attributes == null)
        throw new GdxRuntimeException("Call begin() first");
    if (!attributes.equals(mesh.getVertexAttributes()))
        throw new GdxRuntimeException("Mesh attributes don't match");
    if ((mesh.getMaxVertices() * stride) < vertices.size)
        throw new GdxRuntimeException("Mesh can't hold enough vertices: " + mesh.getMaxVertices() + " * " + stride + " < " + vertices.size);
    if (mesh.getMaxIndices() < indices.size)
        throw new GdxRuntimeException("Mesh can't hold enough indices: " + mesh.getMaxIndices() + " < " + indices.size);
    mesh.setVertices(vertices.items, 0, vertices.size);
    mesh.setIndices(indices.items, 0, indices.size);
    for (MeshPart p : parts) p.mesh = mesh;
    parts.clear();
    attributes = null;
    vertices.clear();
    indices.clear();
    return mesh;
}
Also used : GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) MeshPart(com.badlogic.gdx.graphics.g3d.model.MeshPart)

Aggregations

MeshPart (com.badlogic.gdx.graphics.g3d.model.MeshPart)10 Mesh (com.badlogic.gdx.graphics.Mesh)3 GdxRuntimeException (com.badlogic.gdx.utils.GdxRuntimeException)3 VertexAttributes (com.badlogic.gdx.graphics.VertexAttributes)2 Node (com.badlogic.gdx.graphics.g3d.model.Node)2 NodePart (com.badlogic.gdx.graphics.g3d.model.NodePart)2 ModelMeshPart (com.badlogic.gdx.graphics.g3d.model.data.ModelMeshPart)2 ModelBuilder (com.badlogic.gdx.graphics.g3d.utils.ModelBuilder)2 Vector3 (com.badlogic.gdx.math.Vector3)2 com.badlogic.gdx.physics.bullet.softbody.btSoftRigidDynamicsWorld (com.badlogic.gdx.physics.bullet.softbody.btSoftRigidDynamicsWorld)2 Matrix3f (javax.vecmath.Matrix3f)2 Texture (com.badlogic.gdx.graphics.Texture)1 VertexAttribute (com.badlogic.gdx.graphics.VertexAttribute)1 Material (com.badlogic.gdx.graphics.g3d.Material)1 Model (com.badlogic.gdx.graphics.g3d.Model)1 ModelInstance (com.badlogic.gdx.graphics.g3d.ModelInstance)1 ModelMaterial (com.badlogic.gdx.graphics.g3d.model.data.ModelMaterial)1 ModelMesh (com.badlogic.gdx.graphics.g3d.model.data.ModelMesh)1 ModelNode (com.badlogic.gdx.graphics.g3d.model.data.ModelNode)1 ModelNodePart (com.badlogic.gdx.graphics.g3d.model.data.ModelNodePart)1