/** Copies the indices from the Mesh to the short array. The short array must be large enough to hold destOffset + count * indices. * @param srcOffset the zero-based offset of the first index to fetch * @param count the total amount of indices to copy * @param indices the array to copy the indices to * @param destOffset the offset in the indices array to start copying */ public void getIndices (int srcOffset, int count, short[] indices, int destOffset) { int max = getNumIndices(); if (count < 0) count = max - srcOffset; if (srcOffset < 0 || srcOffset >= max || srcOffset + count > max) throw new IllegalArgumentException("Invalid range specified, offset: " + srcOffset + ", count: " + count + ", max: " + max); if ((indices.length - destOffset) < count) throw new IllegalArgumentException("not enough room in indices array, has " + indices.length + " shorts, needs " + count); int pos = getIndicesBuffer().position(); getIndicesBuffer().position(srcOffset); getIndicesBuffer().get(indices, destOffset, count); getIndicesBuffer().position(pos); }
/** Copies the indices from the Mesh to the short array. The short array must be large enough to hold destOffset + count * indices. * @param srcOffset the zero-based offset of the first index to fetch * @param count the total amount of indices to copy * @param indices the array to copy the indices to * @param destOffset the offset in the indices array to start copying */ public void getIndices (int srcOffset, int count, short[] indices, int destOffset) { int max = getNumIndices(); if (count < 0) count = max - srcOffset; if (srcOffset < 0 || srcOffset >= max || srcOffset + count > max) throw new IllegalArgumentException("Invalid range specified, offset: " + srcOffset + ", count: " + count + ", max: " + max); if ((indices.length - destOffset) < count) throw new IllegalArgumentException("not enough room in indices array, has " + indices.length + " shorts, needs " + count); int pos = getIndicesBuffer().position(); getIndicesBuffer().position(srcOffset); getIndicesBuffer().get(indices, destOffset, count); getIndicesBuffer().position(pos); }
protected void convertMesh (ModelMesh modelMesh) { int numIndices = 0; for (ModelMeshPart part : modelMesh.parts) { numIndices += part.indices.length; } VertexAttributes attributes = new VertexAttributes(modelMesh.attributes); int numVertices = modelMesh.vertices.length / (attributes.vertexSize / 4); Mesh mesh = new Mesh(true, numVertices, numIndices, attributes); meshes.add(mesh); disposables.add(mesh); BufferUtils.copy(modelMesh.vertices, mesh.getVerticesBuffer(), modelMesh.vertices.length, 0); int offset = 0; mesh.getIndicesBuffer().clear(); for (ModelMeshPart part : modelMesh.parts) { MeshPart meshPart = new MeshPart(); meshPart.id = part.id; meshPart.primitiveType = part.primitiveType; meshPart.offset = offset; meshPart.size = part.indices.length; meshPart.mesh = mesh; mesh.getIndicesBuffer().put(part.indices); offset += meshPart.size; meshParts.add(meshPart); } mesh.getIndicesBuffer().position(0); for (MeshPart part : meshParts) part.update(); }
protected void convertMesh (ModelMesh modelMesh) { int numIndices = 0; for (ModelMeshPart part : modelMesh.parts) { numIndices += part.indices.length; } VertexAttributes attributes = new VertexAttributes(modelMesh.attributes); int numVertices = modelMesh.vertices.length / (attributes.vertexSize / 4); Mesh mesh = new Mesh(true, numVertices, numIndices, attributes); meshes.add(mesh); disposables.add(mesh); BufferUtils.copy(modelMesh.vertices, mesh.getVerticesBuffer(), modelMesh.vertices.length, 0); int offset = 0; mesh.getIndicesBuffer().clear(); for (ModelMeshPart part : modelMesh.parts) { MeshPart meshPart = new MeshPart(); meshPart.id = part.id; meshPart.primitiveType = part.primitiveType; meshPart.offset = offset; meshPart.size = part.indices.length; meshPart.mesh = mesh; mesh.getIndicesBuffer().put(part.indices); offset += meshPart.size; meshParts.add(meshPart); } mesh.getIndicesBuffer().position(0); for (MeshPart part : meshParts) part.update(); }
@Override public void flush () { if (idx == 0) return; renderCalls++; totalRenderCalls++; int spritesInBatch = idx / 20; if (spritesInBatch > maxSpritesInBatch) maxSpritesInBatch = spritesInBatch; int count = spritesInBatch * 6; lastTexture.bind(); Mesh mesh = this.mesh; mesh.setVertices(vertices, 0, idx); mesh.getIndicesBuffer().position(0); mesh.getIndicesBuffer().limit(count); if (blendingDisabled) { Gdx.gl.glDisable(GL20.GL_BLEND); } else { Gdx.gl.glEnable(GL20.GL_BLEND); if (blendSrcFunc != -1) Gdx.gl.glBlendFuncSeparate(blendSrcFunc, blendDstFunc, blendSrcFuncAlpha, blendDstFuncAlpha); } mesh.render(customShader != null ? customShader : shader, GL20.GL_TRIANGLES, 0, count); idx = 0; }
@Override public void flush () { if (idx == 0) return; renderCalls++; totalRenderCalls++; int spritesInBatch = idx / 20; if (spritesInBatch > maxSpritesInBatch) maxSpritesInBatch = spritesInBatch; int count = spritesInBatch * 6; lastTexture.bind(); Mesh mesh = this.mesh; mesh.setVertices(vertices, 0, idx); mesh.getIndicesBuffer().position(0); mesh.getIndicesBuffer().limit(count); if (blendingDisabled) { Gdx.gl.glDisable(GL20.GL_BLEND); } else { Gdx.gl.glEnable(GL20.GL_BLEND); if (blendSrcFunc != -1) Gdx.gl.glBlendFuncSeparate(blendSrcFunc, blendDstFunc, blendSrcFuncAlpha, blendDstFuncAlpha); } mesh.render(customShader != null ? customShader : shader, GL20.GL_TRIANGLES, 0, count); idx = 0; }
/** Convenience method to set this btIndexedMesh to the specified {@link Mesh} * The specified mesh must be indexed and triangulated and must outlive this btIndexedMesh. * The buffers for the vertices and indices are shared amonst both. */ public void set(final Object tag, final Mesh mesh, int offset, int count) { if ((count <= 0) || ((count % 3) != 0)) throw new com.badlogic.gdx.utils.GdxRuntimeException("Mesh must be indexed and triangulated"); VertexAttribute posAttr = mesh.getVertexAttribute(Usage.Position); if (posAttr == null) throw new com.badlogic.gdx.utils.GdxRuntimeException("Mesh doesn't have a position attribute"); set(tag, mesh.getVerticesBuffer(), mesh.getVertexSize(), mesh.getNumVertices(), posAttr.offset, mesh.getIndicesBuffer(), offset, count); }
@Override public void render () { if (world.renderMeshes) { MeshPart meshPart = model.nodes.get(0).parts.get(0).meshPart; softBody.getVertices(meshPart.mesh.getVerticesBuffer(), meshPart.mesh.getVertexSize(), positionOffset, normalOffset, meshPart.mesh.getIndicesBuffer(), meshPart.offset, meshPart.size, indexMap, 0); softBody.getWorldTransform(entity.transform); } super.render(); }
/** Convenience method to set this btIndexedMesh to the specified {@link Mesh} * The specified mesh must be indexed and triangulated and must outlive this btIndexedMesh. * The buffers for the vertices and indices are shared amonst both. */ public void set(final Object tag, final Mesh mesh, int offset, int count) { if ((count <= 0) || ((count % 3) != 0)) throw new com.badlogic.gdx.utils.GdxRuntimeException("Mesh must be indexed and triangulated"); VertexAttribute posAttr = mesh.getVertexAttribute(Usage.Position); if (posAttr == null) throw new com.badlogic.gdx.utils.GdxRuntimeException("Mesh doesn't have a position attribute"); set(tag, mesh.getVerticesBuffer(), mesh.getVertexSize(), mesh.getNumVertices(), posAttr.offset, mesh.getIndicesBuffer(), offset, count); }
ShortBuffer indicesBuffer = mesh.getIndicesBuffer();
@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)); }
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);
/** Copies the indices from the Mesh to the short array. The short array must be large enough to hold destOffset + count * indices. * @param srcOffset the zero-based offset of the first index to fetch * @param count the total amount of indices to copy * @param indices the array to copy the indices to * @param destOffset the offset in the indices array to start copying */ public void getIndices (int srcOffset, int count, short[] indices, int destOffset) { int max = getNumIndices(); if (count < 0) count = max - srcOffset; if (srcOffset < 0 || srcOffset >= max || srcOffset + count > max) throw new IllegalArgumentException("Invalid range specified, offset: " + srcOffset + ", count: " + count + ", max: " + max); if ((indices.length - destOffset) < count) throw new IllegalArgumentException("not enough room in indices array, has " + indices.length + " shorts, needs " + count); int pos = getIndicesBuffer().position(); getIndicesBuffer().position(srcOffset); getIndicesBuffer().get(indices, destOffset, count); getIndicesBuffer().position(pos); }
protected void convertMesh (ModelMesh modelMesh) { int numIndices = 0; for (ModelMeshPart part : modelMesh.parts) { numIndices += part.indices.length; } VertexAttributes attributes = new VertexAttributes(modelMesh.attributes); int numVertices = modelMesh.vertices.length / (attributes.vertexSize / 4); Mesh mesh = new Mesh(true, numVertices, numIndices, attributes); meshes.add(mesh); disposables.add(mesh); BufferUtils.copy(modelMesh.vertices, mesh.getVerticesBuffer(), modelMesh.vertices.length, 0); int offset = 0; mesh.getIndicesBuffer().clear(); for (ModelMeshPart part : modelMesh.parts) { MeshPart meshPart = new MeshPart(); meshPart.id = part.id; meshPart.primitiveType = part.primitiveType; meshPart.offset = offset; meshPart.size = part.indices.length; meshPart.mesh = mesh; mesh.getIndicesBuffer().put(part.indices); offset += meshPart.size; meshParts.add(meshPart); } mesh.getIndicesBuffer().position(0); for (MeshPart part : meshParts) part.update(); }
@Override public void flush () { if (idx == 0) return; renderCalls++; totalRenderCalls++; int spritesInBatch = idx / 20; if (spritesInBatch > maxSpritesInBatch) maxSpritesInBatch = spritesInBatch; int count = spritesInBatch * 6; lastTexture.bind(); Mesh mesh = this.mesh; mesh.setVertices(vertices, 0, idx); mesh.getIndicesBuffer().position(0); mesh.getIndicesBuffer().limit(count); if (blendingDisabled) { Gdx.gl.glDisable(GL20.GL_BLEND); } else { Gdx.gl.glEnable(GL20.GL_BLEND); if (blendSrcFunc != -1) Gdx.gl.glBlendFuncSeparate(blendSrcFunc, blendDstFunc, blendSrcFuncAlpha, blendDstFuncAlpha); } mesh.render(customShader != null ? customShader : shader, GL20.GL_TRIANGLES, 0, count); idx = 0; }
/** Convenience method to set this btIndexedMesh to the specified {@link Mesh} * The specified mesh must be indexed and triangulated and must outlive this btIndexedMesh. * The buffers for the vertices and indices are shared amonst both. */ public void set(final Object tag, final Mesh mesh, int offset, int count) { if ((count <= 0) || ((count % 3) != 0)) throw new com.badlogic.gdx.utils.GdxRuntimeException("Mesh must be indexed and triangulated"); VertexAttribute posAttr = mesh.getVertexAttribute(Usage.Position); if (posAttr == null) throw new com.badlogic.gdx.utils.GdxRuntimeException("Mesh doesn't have a position attribute"); set(tag, mesh.getVerticesBuffer(), mesh.getVertexSize(), mesh.getNumVertices(), posAttr.offset, mesh.getIndicesBuffer(), offset, count); }
private void init(float w, float h) { // Init comparator comp = new DistToCameraComparator<IRenderable>(); // Init vertices float[] vertices = new float[20]; fillVertices(vertices, w, h); // We wont need indices if we use GL_TRIANGLE_FAN to draw our quad // TRIANGLE_FAN will draw the verts in this order: 0, 1, 2; 0, 2, 3 mesh = new Mesh(true, 4, 6, new VertexAttribute(Usage.Position, 2, ShaderProgram.POSITION_ATTRIBUTE), new VertexAttribute(Usage.ColorPacked, 4, ShaderProgram.COLOR_ATTRIBUTE), new VertexAttribute(Usage.TextureCoordinates, 2, ShaderProgram.TEXCOORD_ATTRIBUTE + "0")); mesh.setVertices(vertices, 0, vertices.length); mesh.getIndicesBuffer().position(0); mesh.getIndicesBuffer().limit(6); short[] indices = new short[] { 0, 1, 2, 0, 2, 3 }; mesh.setIndices(indices); quaternion = new Quaternion(); aux = new Vector3(); }
private void init(String tex0, float w, float h) { setTexture0(tex0); // Init comparator comp = new DistToCameraComparator<IRenderable>(); // Init vertices float[] vertices = new float[20]; fillVertices(vertices, w, h); // We wont need indices if we use GL_TRIANGLE_FAN to draw our quad // TRIANGLE_FAN will draw the verts in this order: 0, 1, 2; 0, 2, 3 mesh = new Mesh(true, 4, 6, new VertexAttribute(Usage.Position, 2, ShaderProgram.POSITION_ATTRIBUTE), new VertexAttribute(Usage.ColorPacked, 4, ShaderProgram.COLOR_ATTRIBUTE), new VertexAttribute(Usage.TextureCoordinates, 2, ShaderProgram.TEXCOORD_ATTRIBUTE + "0")); mesh.setVertices(vertices, 0, vertices.length); mesh.getIndicesBuffer().position(0); mesh.getIndicesBuffer().limit(6); short[] indices = new short[] { 0, 1, 2, 0, 2, 3 }; mesh.setIndices(indices); quaternion = new Quaternion(); aux = new Vector3(); }