List of usage examples for com.badlogic.gdx.graphics Mesh getVerticesBuffer
public FloatBuffer getVerticesBuffer()
From source file:com.badlogic.gdx.physics.bullet.demo.simulationobjects.MeshSimulationObject.java
public static btBvhTriangleMeshShape createTriangleMeshShape(Mesh mesh, AtomicReference<btTriangleMesh> triangleMesh) { final btTriangleMesh m = new btTriangleMesh(); final ShortBuffer indices = mesh.getIndicesBuffer(); indices.rewind();//from w w w .j a va 2 s . c o m final FloatBuffer vertices = mesh.getVerticesBuffer(); vertices.rewind(); // Some meshes have vertices but no indices declared final boolean hasIndices = mesh.getNumIndices() != 0; final int vertexStride = mesh.getVertexSize() / 4; final Vector3 v0 = Pools.VECTOR3.obtain(); final Vector3 v1 = Pools.VECTOR3.obtain(); final Vector3 v2 = Pools.VECTOR3.obtain(); // Set up a small array to keep the loop code simpler final Vector3[] vectors = new Vector3[] { v0, v1, v2 }; short vectorIndex = 0; int i = -1; int verticesRead = 0; while (verticesRead < mesh.getNumVertices()) { if (hasIndices) { i = indices.get(); } else { i++; } vectors[vectorIndex++].set(vertices.get(i * vertexStride), vertices.get(i * vertexStride + 1), vertices.get(i * vertexStride + 2)); if (vectorIndex == vectors.length) { m.addTriangle(v0, v1, v2, true); vectorIndex = 0; } verticesRead++; } Pools.VECTOR3.free(v0); Pools.VECTOR3.free(v1); Pools.VECTOR3.free(v2); triangleMesh.set(m); return new btBvhTriangleMeshShape(m, true); }
From source file:com.mygdx.game.pathfinding.NavMeshGraph.java
License:Apache License
/** * Get an array of the vertex indices from the mesh. Any vertices which share the same position will be counted * as a single vertex and share the same index. That is, position duplicates will be filtered out. * * @param mesh//ww w .ja va 2 s . c o m * @return */ private static short[] getUniquePositionVertexIndices(Mesh mesh) { FloatBuffer verticesBuffer = mesh.getVerticesBuffer(); int positionOffset = mesh.getVertexAttributes().findByUsage(VertexAttributes.Usage.Position).offset / 4; // Number of array elements which make up a vertex int vertexSize = mesh.getVertexSize() / 4; // The indices tell us which vertices are part of a triangle. short[] indices = new short[mesh.getNumIndices()]; mesh.getIndices(indices); // Marks true if an index has already been compared to avoid unnecessary comparisons Bits handledIndices = new Bits(mesh.getNumIndices()); for (int i = 0; i < indices.length; i++) { short indexI = indices[i]; if (handledIndices.get(indexI)) { // Index handled in an earlier iteration continue; } int vBufIndexI = indexI * vertexSize + positionOffset; float xi = verticesBuffer.get(vBufIndexI++); float yi = verticesBuffer.get(vBufIndexI++); float zi = verticesBuffer.get(vBufIndexI++); for (int j = i + 1; j < indices.length; j++) { short indexJ = indices[j]; int vBufIndexJ = indexJ * vertexSize + positionOffset; float xj = verticesBuffer.get(vBufIndexJ++); float yj = verticesBuffer.get(vBufIndexJ++); float zj = verticesBuffer.get(vBufIndexJ++); if (xi == xj && yi == yj && zi == zj) { indices[j] = indexI; } } handledIndices.set(indexI); } return indices; }
From source file:com.mygdx.game.pathfinding.NavMeshGraph.java
License:Apache License
/** * Creates Vector3 objects from the vertices of the mesh. The resulting array follows the ordering of the provided * index array./*from w w w. j a v a2s . c om*/ * * @param mesh * @param indices * @return */ private static Vector3[] createVertexVectors(Mesh mesh, short[] indices) { FloatBuffer verticesBuffer = mesh.getVerticesBuffer(); int positionOffset = mesh.getVertexAttributes().findByUsage(VertexAttributes.Usage.Position).offset / 4; int vertexSize = mesh.getVertexSize() / 4; Vector3[] vertexVectors = new Vector3[mesh.getNumIndices()]; for (int i = 0; i < indices.length; i++) { short index = indices[i]; int a = index * vertexSize + positionOffset; float x = verticesBuffer.get(a++); float y = verticesBuffer.get(a++); float z = verticesBuffer.get(a); vertexVectors[index] = new Vector3(x, y, z); } return vertexVectors; }
From source file:com.mygdx.game.scene.GameObjectBlueprint.java
License:Apache License
private void setRigidBody(BlenderEmpty blenderEmpty, Model model, Vector3 scale) { this.shapeType = blenderEmpty.custom_properties.get(blenderCollisionShapeField); this.mass = Float.parseFloat(blenderEmpty.custom_properties.get(blenderMassField)); tmpScale.set(blenderEmpty.scale.x * scale.x, blenderEmpty.scale.y * scale.y, blenderEmpty.scale.z * scale.z); if (shapeType.equals("capsule")) { float radius = Math.max(tmpScale.x, tmpScale.z); shape = new btCapsuleShape(radius, scale.y); } else if (shapeType.equals("sphere")) { float radius = Math.max(Math.max(tmpScale.x, tmpScale.y), tmpScale.z); shape = new btSphereShape(radius); } else if (shapeType.equals("box")) { shape = new btBoxShape(tmpScale); } else if (shapeType.equals("convex_hull")) { // We need a model instance with the correct scale ModelInstance modelInstance = new ModelInstance(model); GameModel.applyTransform(position, rotation, scale, modelInstance); // Copy the vertices to a work buffer, where we apply the model global transform to them Matrix4 transform = new Matrix4(modelInstance.nodes.get(0).globalTransform); Mesh mesh = modelInstance.model.meshes.get(0); FloatBuffer workBuffer = BufferUtils.newFloatBuffer(mesh.getVerticesBuffer().capacity()); BufferUtils.copy(mesh.getVerticesBuffer(), workBuffer, mesh.getNumVertices() * mesh.getVertexSize() / 4); BufferUtils.transform(workBuffer, 3, mesh.getVertexSize(), mesh.getNumVertices(), transform); // First create a shape using all the vertices, then use the built in tool to reduce // the number of vertices to a manageable amount. btConvexShape convexShape = new btConvexHullShape(workBuffer, mesh.getNumVertices(), mesh.getVertexSize());/*from ww w . j av a 2 s.c o m*/ btShapeHull hull = new btShapeHull(convexShape); hull.buildHull(convexShape.getMargin()); shape = new btConvexHullShape(hull); convexShape.dispose(); hull.dispose(); } else if (shapeType.equals("none")) { shape = null; } else { throw new GdxRuntimeException("Cannot load collision shape data for " + blenderEmpty.name + " from '" + blenderEmpty.name + "'"); } setCollisionFlags(this.mass); }
From source file:mobi.shad.s3lib.gfx.g3d.simpleobject.DebugObject.java
License:Apache License
public static void debugMesh(Mesh mesh) { int numVertices = mesh.getNumVertices(); int numIndices = mesh.getNumIndices(); ShortBuffer indicesBuffer = mesh.getIndicesBuffer(); FloatBuffer verticesBuffer = mesh.getVerticesBuffer(); S3Log.log("debugMesh", "NumVertices: " + numVertices); S3Log.log("debugMesh", "NumIndices: " + numIndices); S3Log.log("debugMesh", "vertexSize (bytes): " + mesh.getVertexSize()); S3Log.log("debugMesh", "Indices Buffer:", 1); StringBuilder buffer = new StringBuilder(32); buffer.append('['); buffer.append(indicesBuffer.get(0)); for (int i = 1; i < numIndices; i++) { buffer.append(", "); buffer.append(indicesBuffer.get(i)); }/*from w w w . j a v a 2 s .c o m*/ buffer.append(']'); S3Log.log("debugMesh", buffer.toString()); S3Log.log("debugMesh", "Vertices Buffer:", 1); StringBuilder buffer2 = new StringBuilder(32); buffer2.append('['); buffer2.append(verticesBuffer.get(0)); for (int i = 1; i < numIndices; i++) { buffer2.append(", "); buffer2.append(verticesBuffer.get(i)); } buffer.append(']'); S3Log.log("debugMesh", buffer2.toString()); }
From source file:org.interreg.docexplore.reader.book.BookCover.java
License:Open Source License
void setVertex(int vertex, float x, float y, float z) { for (int i = 0; i < meshes.length; i++) if (indices[i][vertex] > -1) { Mesh mesh = meshes[i]; mesh.getVerticesBuffer().put(indices[i][vertex], x); mesh.getVerticesBuffer().put(indices[i][vertex] + 1, y); mesh.getVerticesBuffer().put(indices[i][vertex] + 2, z); }/*from w w w .j a v a 2 s . c o m*/ }
From source file:org.interreg.docexplore.reader.book.BookCover.java
License:Open Source License
void setNormal(int meshIndex, int vertex, float x, float y, float z) { Mesh mesh = meshes[meshIndex]; mesh.getVerticesBuffer().put(indices[meshIndex][vertex] + 3, x); mesh.getVerticesBuffer().put(indices[meshIndex][vertex] + 4, y); mesh.getVerticesBuffer().put(indices[meshIndex][vertex] + 5, z); }
From source file:org.interreg.docexplore.reader.gfx.GfxUtils.java
License:Open Source License
public static Mesh buildQuad(float x1, float y1, float s1, float t1, float x2, float y2, float s2, float t2) { Mesh mesh = new Mesh(false, 4, 6, new VertexAttribute(VertexAttributes.Usage.Position, 3, "p"), new VertexAttribute(VertexAttributes.Usage.TextureCoordinates, 2, "tc")); FloatBuffer vertexBuffer = mesh.getVerticesBuffer(); vertexBuffer.limit(vertexBuffer.capacity()); vertexBuffer.put(x1).put(y1).put(0).put(s1).put(t1).put(x2).put(y1).put(0).put(s2).put(t1).put(x2).put(y2) .put(0).put(s2).put(t2).put(x1).put(y2).put(0).put(s1).put(t2).flip(); ShortBuffer indexBuffer = mesh.getIndicesBuffer(); indexBuffer.limit(indexBuffer.capacity()); indexBuffer.put(new short[] { 0, 1, 2, 0, 2, 3 }).flip(); return mesh;//from w w w . ja v a2 s. c o m }