@Override public Mesh obtain (VertexAttributes vertexAttributes, int vertexCount, int indexCount) { for (int i = 0, n = freeMeshes.size; i < n; ++i) { final Mesh mesh = freeMeshes.get(i); if (mesh.getVertexAttributes().equals(vertexAttributes) && mesh.getMaxVertices() == vertexCount && mesh.getMaxIndices() == indexCount) { freeMeshes.removeIndex(i); usedMeshes.add(mesh); return mesh; } } Mesh result = new Mesh(true, vertexCount, indexCount, vertexAttributes); usedMeshes.add(result); return result; }
public void flush () { if (numVertices == 0) return; shader.begin(); shader.setUniformMatrix("u_projModelView", projModelView); for (int i = 0; i < numTexCoords; i++) shader.setUniformi(shaderUniformNames[i], i); mesh.setVertices(vertices, 0, vertexIdx); mesh.render(shader, primitiveType); shader.end(); numSetTexCoords = 0; vertexIdx = 0; numVertices = 0; }
protected void transformUV (final Matrix3 matrix, final int start, final int count) { final VertexAttribute posAttr = getVertexAttribute(Usage.TextureCoordinates); final int offset = posAttr.offset / 4; final int vertexSize = getVertexSize() / 4; final int numVertices = getNumVertices(); final float[] vertices = new float[numVertices * vertexSize]; // TODO: getVertices(vertices, start * vertexSize, count * vertexSize); getVertices(0, vertices.length, vertices); transformUV(matrix, vertices, vertexSize, offset, start, count); setVertices(vertices, 0, vertices.length); // TODO: setVertices(start * vertexSize, vertices, 0, vertices.length); }
/** Method to transform the positions in the mesh. Normals will be kept as is. This is a potentially slow operation, use with * care. It will also create a temporary float[] which will be garbage collected. * * @param matrix the transformation matrix */ public void transform (final Matrix4 matrix) { transform(matrix, 0, getNumVertices()); }
/** Adds the specified vertices to the cache. Each vertex should have 5 elements, one for each of the attributes: x, y, color, * u, and v. If indexed geometry is used, each image should be specified as 4 vertices, otherwise each image should be * specified as 6 vertices. */ public void add (Texture texture, float[] vertices, int offset, int length) { if (currentCache == null) throw new IllegalStateException("beginCache must be called before add."); int verticesPerImage = mesh.getNumIndices() > 0 ? 4 : 6; int count = length / (verticesPerImage * VERTEX_SIZE) * 6; int lastIndex = textures.size - 1; if (lastIndex < 0 || textures.get(lastIndex) != texture) { textures.add(texture); counts.add(count); } else counts.incr(lastIndex, count); mesh.getVerticesBuffer().put(vertices, offset, length); }
if (currentCache == null) throw new IllegalStateException("beginCache must be called before endCache."); Cache cache = currentCache; int cacheCount = mesh.getVerticesBuffer().position() - cache.offset; if (cache.textures == null) { cache.textures = textures.toArray(Texture.class); cache.counts = new int[cache.textureCount]; for (int i = 0, n = counts.size; i < n; i++) cache.counts[i] = counts.get(i); mesh.getVerticesBuffer().flip(); } else { throw new GdxRuntimeException( "If a cache is not the last created, it cannot be redefined with more entries than when it was first created: " + cacheCount + " (" + cache.maxCount + " max)"); cache.textures[i] = textures.get(i); cache.counts[i] = counts.get(i); FloatBuffer vertices = mesh.getVerticesBuffer(); vertices.position(0); Cache lastCache = caches.get(caches.size - 1); vertices.limit(lastCache.offset + lastCache.maxCount);
/** Copies the specified vertices from the Mesh to the float array. The float array must be large enough to hold * destOffset+count vertices. * @param srcOffset the offset (in number of floats) of the vertices in the mesh to copy * @param count the amount of floats to copy * @param vertices the array to copy the vertices to * @param destOffset the offset (in floats) in the vertices array to start copying */ public float[] getVertices (int srcOffset, int count, float[] vertices, int destOffset) { // TODO: Perhaps this method should be vertexSize aware?? final int max = getNumVertices() * getVertexSize() / 4; if (count == -1) { count = max - srcOffset; if (count > vertices.length - destOffset) count = vertices.length - destOffset; } if (srcOffset < 0 || count <= 0 || (srcOffset + count) > max || destOffset < 0 || destOffset >= vertices.length) throw new IndexOutOfBoundsException(); if ((vertices.length - destOffset) < count) throw new IllegalArgumentException("not enough room in vertices array, has " + vertices.length + " floats, needs " + count); int pos = getVerticesBuffer().position(); getVerticesBuffer().position(srcOffset); getVerticesBuffer().get(vertices, destOffset, count); getVerticesBuffer().position(pos); return vertices; }
/** Starts the redefinition of an existing cache, allowing the add and {@link #endCache()} methods to be called. If this is not * the last cache created, it cannot have more entries added to it than when it was first created. To do that, use * {@link #clear()} and then {@link #begin()}. */ public void beginCache (int cacheID) { if (drawing) throw new IllegalStateException("end must be called before beginCache"); if (currentCache != null) throw new IllegalStateException("endCache must be called before begin."); if (cacheID == caches.size - 1) { Cache oldCache = caches.removeIndex(cacheID); mesh.getVerticesBuffer().limit(oldCache.offset); beginCache(); return; } currentCache = caches.get(cacheID); mesh.getVerticesBuffer().position(currentCache.offset); }
final int numVertices = getNumVertices(); if (numVertices == 0) throw new GdxRuntimeException("No vertices defined"); final VertexAttribute posAttrib = getVertexAttribute(Usage.Position); final int offset = posAttrib.offset / 4; final int vertexSize = vertices.getAttributes().vertexSize / 4; case 1: for (int i = 0; i < numVertices; i++) { bbox.ext(verts.get(idx), 0, 0); idx += vertexSize; case 2: for (int i = 0; i < numVertices; i++) { bbox.ext(verts.get(idx), verts.get(idx + 1), 0); idx += vertexSize;
/** Starts the definition of a new cache, allowing the add and {@link #endCache()} methods to be called. */ public void beginCache () { if (drawing) throw new IllegalStateException("end must be called before beginCache"); if (currentCache != null) throw new IllegalStateException("endCache must be called before begin."); int verticesPerImage = mesh.getNumIndices() > 0 ? 4 : 6; currentCache = new Cache(caches.size, mesh.getVerticesBuffer().limit()); caches.add(currentCache); mesh.getVerticesBuffer().compact(); }
/** Invalidates all cache IDs and resets the SpriteCache so new caches can be added. */ public void clear () { caches.clear(); mesh.getVerticesBuffer().clear().flip(); }
/** 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; }
@Override public void create () { Preferences pref = Gdx.app.getPreferences("test"); boolean resultb = pref.getBoolean("test"); int resulti = pref.getInteger("test"); shader = new ShaderProgram(Gdx.files.internal("data/shaders/shader-vs.glsl"), Gdx.files.internal("data/shaders/shader-fs.glsl")); if (!shader.isCompiled()) throw new GdxRuntimeException(shader.getLog()); mesh = new Mesh(VertexDataType.VertexBufferObject, true, 6, 0, VertexAttribute.Position(), VertexAttribute.TexCoords(0)); mesh.setVertices(new float[] {-0.5f, -0.5f, 0, 0, 1, 0.5f, -0.5f, 0, 1, 1, 0.5f, 0.5f, 0, 1, 0, 0.5f, 0.5f, 0, 1, 0, -0.5f, 0.5f, 0, 0, 0, -0.5f, -0.5f, 0, 0, 1}); texture = new Texture(new Pixmap(Gdx.files.internal("data/badlogic.jpg")), true); texture.setFilter(TextureFilter.MipMap, TextureFilter.Linear); String params = Gdx.files.internal("data/gwttestparams.txt").readString(); numSprites = Integer.parseInt(params); batch = new SpriteBatch(); positions = new ArrayList<Vector2>(); for (int i = 0; i < numSprites; i++) { positions.add(new Vector2(MathUtils.random() * Gdx.graphics.getWidth(), MathUtils.random() * Gdx.graphics.getHeight())); } sprite = new Sprite(texture); sprite.setSize(64, 64); sprite.setOrigin(32, 32); font = new BitmapFont(Gdx.files.internal("data/arial-15.fnt"), false); cache = font.newFontCache(); cache.setColor(Color.RED); cache.setText("This is a Test", 0, 0); atlas = new TextureAtlas(Gdx.files.internal("data/pack")); }
@Override public void create () { camera = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); camera.position.set(0, 1.5f, 1.5f); camera.lookAt(0, 0, 0); camera.update(); controller = new PerspectiveCamController(camera); mesh = new Mesh(true, 4, 4, new VertexAttribute(Usage.Position, 3, ShaderProgram.POSITION_ATTRIBUTE), new VertexAttribute( Usage.TextureCoordinates, 2, ShaderProgram.TEXCOORD_ATTRIBUTE)); mesh.setVertices(new float[] {-1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, -1, 1, 0, -1, 0, -1, 0, 0,}); mesh.setIndices(new short[] {0, 1, 2, 3}); shader = new ShaderProgram(Gdx.files.internal("data/shaders/flattex-vert.glsl").readString(), Gdx.files.internal( "data/shaders/flattex-frag.glsl").readString()); if (!shader.isCompiled()) throw new GdxRuntimeException("shader error: " + shader.getLog()); textureHW = new Texture(Gdx.files.internal("data/badlogic.jpg"), Format.RGB565, true); MipMapGenerator.setUseHardwareMipMap(false); textureSW = new Texture(Gdx.files.internal("data/badlogic.jpg"), Format.RGB565, true); currTexture = textureHW; createUI(); multiplexer = new InputMultiplexer(); Gdx.input.setInputProcessor(multiplexer); multiplexer.addProcessor(ui); multiplexer.addProcessor(controller); }
public void render () { Gdx.gl20.glViewport(0, 0, Gdx.graphics.getBackBufferWidth(), Gdx.graphics.getBackBufferHeight()); Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT); Gdx.gl20.glActiveTexture(GL20.GL_TEXTURE0); texture.bind(); Gdx.gl20.glActiveTexture(GL20.GL_TEXTURE1); texture2.bind(); shader.begin(); shader.setUniformi("s_texture", 0); shader.setUniformi("s_texture2", 1); mesh.render(shader, GL20.GL_TRIANGLES); shader.end(); }
public static btConvexHullShape createConvexHullShape (final Model model, boolean optimize) { final Mesh mesh = model.meshes.get(0); final btConvexHullShape shape = new btConvexHullShape(mesh.getVerticesBuffer(), mesh.getNumVertices(), mesh.getVertexSize()); if (!optimize) return shape; // now optimize the shape final btShapeHull hull = new btShapeHull(shape); hull.buildHull(shape.getMargin()); final btConvexHullShape result = new btConvexHullShape(hull); // delete the temporary shape shape.dispose(); hull.dispose(); return result; }
/** 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(); }