private MeshBuilder getBuilder (final VertexAttributes attributes) { for (final MeshBuilder mb : builders) if (mb.getAttributes().equals(attributes) && mb.lastIndex() < Short.MAX_VALUE / 2) return mb; final MeshBuilder result = new MeshBuilder(); result.begin(attributes); builders.add(result); return result; }
/** Generate mesh and renderable */ public void end () { if (!building) throw new GdxRuntimeException("Call begin() prior to calling end()"); building = false; builder.end(mesh); }
/** Begin building a mesh. * @param attributes bitwise mask of the {@link com.badlogic.gdx.graphics.VertexAttributes.Usage}, only Position, Color, Normal * and TextureCoordinates is supported. */ public void begin (final long attributes, int primitiveType) { begin(createAttributes(attributes), primitiveType); }
@Override public void line (VertexInfo p1, VertexInfo p2) { ensureVertices(2); line(vertex(p1), vertex(p2)); }
int offset = renderables.size; meshBuilder.begin(vertexAttributes); MeshPart part = meshBuilder.part("", primitiveType, meshPartPool.obtain()); renderables.add(obtainRenderable(material, primitiveType)); && renderable.meshPart.size + meshBuilder.getNumVertices() < Short.MAX_VALUE; // comparing indices and vertices... final boolean samePart = sameMesh && pt == primitiveType && mat.same(material, true); 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; 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;
Material solidMaterial = new Material(); MeshBuilder meshBuilder = new MeshBuilder(); meshBuilder.begin(Usage.Position | Usage.Normal | Usage.ColorPacked | Usage.TextureCoordinates, GL20.GL_TRIANGLES); meshBuilder.box(1f, 1f, 1f); Mesh mesh = new Mesh(true, meshBuilder.getNumVertices(), meshBuilder.getNumIndices(), meshBuilder.getAttributes()); mesh = meshBuilder.end(mesh);
@Override public void renderBottom(int atlasIndex, float x1, float z1, float x2, float z2, float y, float lightLevel, PerCornerLightData pcld, MeshBuilder builder) { // NEGATIVE Y float u = getU(atlasIndex); float v = getV(atlasIndex); builder.setUVRange(u, v, u, v); c00.setPos(x1, y, z1).setNor(0, -1, 0); c01.setPos(x1, y, z2).setNor(0, -1, 0); c10.setPos(x2, y, z1).setNor(0, -1, 0); c11.setPos(x2, y, z2).setNor(0, -1, 0); Color c = getColor((int) x1, (int) y, (int) z1); if(pcld == null) { builder.setColor(c.r*lightLevel, c.g*lightLevel, c.b*lightLevel, c.a); } else { c00.setCol(c.r*pcld.l00, c.g*pcld.l00, c.b*pcld.l00, c.a); c01.setCol(c.r*pcld.l01, c.g*pcld.l01, c.b*pcld.l01, c.a); c10.setCol(c.r*pcld.l10, c.g*pcld.l10, c.b*pcld.l10, c.a); c11.setCol(c.r*pcld.l11, c.g*pcld.l11, c.b*pcld.l11, c.a); } builder.rect(c00, c10, c11, c01); }
@Override public void addMesh (float[] vertices, short[] indices) { final short offset = (short)(lastIndex + 1); final int numVertices = vertices.length / stride; ensureVertices(numVertices); for (int v = 0; v < vertices.length; v += stride) addVertex(vertices, v); ensureIndices(indices.length); for (int i = 0; i < indices.length; ++i) index((short)(indices[i] + offset)); }
MeshBuilder meshBuilder = new MeshBuilder(); meshBuilder.begin(VertexAttributes.Usage.Position | VertexAttributes.Usage.Normal); meshBuilder.part("id1", GL20.GL_TRIANGLES); meshBuilder.box(1f, 1f, 1f); Mesh mesh1 = meshBuilder.end(); meshBuilder.begin(VertexAttributes.Usage.Position | VertexAttributes.Usage.Normal); meshBuilder.part("id2", GL20.GL_TRIANGLES); meshBuilder.cylinder(1f, 1f, 1f, 16); Mesh mesh2 = meshBuilder.end(); ModelBuilder modelBuilder = new ModelBuilder(); modelBuilder.begin(); modelBuilder.part("part1", mesh1, GL20.GL_TRIANGLES, new Material(ColorAttribute.createDiffuse(Color.RED))); modelBuilder.part("part2", mesh2, GL20.GL_TRIANGLES, new Material(ColorAttribute.createDiffuse(Color.RED))) .mesh.transform(new Matrix4().translate(1, 0, 0f));
MeshBuilder builder = new MeshBuilder(); builder.begin(VertexAttributes.Usage.Position | VertexAttributes.Usage.TextureCoordinates, GL20.GL_TRIANGLES); MeshPartBuilder.VertexInfo topLeftFront = new MeshPartBuilder.VertexInfo().setPos(x1, y2, z2).setUV(sideU2, sideV1); builder.rect(bottomLeftBack, bottomRightBack, topRightBack, topLeftBack); builder.rect(topLeftFront, topRightFront, bottomRightFront, bottomLeftFront); builder.rect(bottomLeftBack, topLeftBack, topLeftFront, bottomLeftFront); builder.rect(bottomRightFront, topRightFront, topRightBack, bottomRightBack); builder.rect(topLeftBack.setUV(topU1, topV1), topRightBack.setUV(topU2, topV1), topRightFront.setUV(topU2, topV2), topLeftFront.setUV(topU1, topV2)); builder.rect(bottomLeftFront.setUV(bottomU1, bottomV2), bottomRightFront.setUV(bottomU2, bottomV2), bottomRightBack.setUV(bottomU2, bottomV1), bottomLeftBack.setUV(bottomU1, bottomV1)); Mesh skybox = builder.end();
MeshBuilder meb = new MeshBuilder(); final long atr = Usage.Position | Usage.Color; //Add Usage.TextureCoordinates or similar here if you need it //Create mesh #1 meb.begin(atr); meb.cylinder(4f, 6f, 4f, 16); Mesh cyl1 = meb.end(); //Create mesh #2 meb.begin(atr); meb.cylinder(4f, 6f, 4f, 16); Mesh cyl2 = meb.end(); //Combine the two meshes into one model using ModelBuilder ModelBuilder mob = new ModelBuilder(); mob.begin(); mob.part("cylinder1", cyl1, Usage.Position | Usage.Normal | Usage.TextureCoordinates, new Material(ColorAttribute.createDiffuse(Color.RED), ColorAttribute.createSpecular(1, 1, 1, 1), FloatAttribute.createShininess(8f))); mob.part("cylinder2", cyl2, Usage.Position | Usage.Normal | Usage.TextureCoordinates, new Material(ColorAttribute.createDiffuse(Color.GREEN), ColorAttribute.createSpecular(1, 1, 1, 1), FloatAttribute.createShininess(8f))).mesh.transform(new Matrix4().translate(0, 0, -2f)); Model cyl = mob.end();
/** Initialize ShapeCache for mesh generation * @param primitiveType OpenGL primitive type */ public MeshPartBuilder begin (int primitiveType) { if (building) throw new GdxRuntimeException("Call end() after calling begin()"); building = true; builder.begin(mesh.getVertexAttributes()); builder.part(id, primitiveType, renderable.meshPart); return builder; }
public Mesh meshFaces(List<Face> faces, MeshBuilder builder) { builder.begin(VertexAttributes.Usage.Position | VertexAttributes.Usage.TextureCoordinates | VertexAttributes.Usage.ColorPacked | VertexAttributes.Usage.Normal, GL20.GL_TRIANGLES); builder.ensureVertices(faces.size() * 4); for (Face f : faces) { f.render(builder); } return builder.end(); }
/** Create a ModelCache using the specified {@link RenderableSorter} and {@link MeshPool} implementation. The * {@link RenderableSorter} implementation will be called with the camera specified in {@link #begin(Camera)}. By default this * will be null. The sorter is important for optimizing the cache. For the best result, make sure that renderables that can be * merged are next to each other. */ public ModelCache (RenderableSorter sorter, MeshPool meshPool) { this.sorter = sorter; this.meshPool = meshPool; meshBuilder = new MeshBuilder(); }
/** @deprecated use {@link #ensureVertices(int)} followed by {@link #ensureTriangleIndices(int)} instead. */ @Deprecated public void ensureTriangles (int numTriangles) { ensureVertices(3 * numTriangles); ensureTriangleIndices(numTriangles); }
/** @deprecated use {@link #ensureVertices(int)} followed by {@link #ensureRectangleIndices(int)} instead. */ public void ensureRectangles (int numRectangles) { ensureVertices(4 * numRectangles); ensureRectangleIndices(numRectangles); }
@Override public void triangle (VertexInfo p1, VertexInfo p2, VertexInfo p3) { ensureVertices(3); triangle(vertex(p1), vertex(p2), vertex(p3)); }
public HeightField (boolean isStatic, int width, int height, boolean smooth, int attributes) { this(isStatic, width, height, smooth, MeshBuilder.createAttributes(attributes)); }
@Override public void ensureCapacity (int numVertices, int numIndices) { ensureVertices(numVertices); ensureIndices(numIndices); }