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));
}
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));
}
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;
}
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;
}
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;
}
Aggregations