protected QuadBatch createSepiaBatch() { return new TriangleBatch(game.graphics.gl, new TriangleBatch.Source() { @Override protected String textureTint () { return super.textureTint() + " float grey = dot(textureColor.rgb, vec3(0.299, 0.587, 0.114));\n" + " textureColor = vec4(grey * vec3(1.2, 1.0, 0.8), textureColor.a);\n"; } }); }
/** * Adds a collection of textured triangles to the current render operation. See * {@link #addTris(Texture,int,AffineTransform,float[],int,int,float,float,int[],int,int,int)} * for parameter documentation. * * @param sxys a list of sx/sy texture coordinates as: {@code [sx1, sy1, sx2, sy2, ...]}. This * must be of the same length as {@code xys}. */ public void addTris (Texture tex, int tint, AffineTransform xf, float[] xys, float[] sxys, int xysOffset, int xysLen, int[] indices, int indicesOffset, int indicesLen, int indexBase) { setTexture(tex); prepare(tint, xf); addTris(xys, sxys, xysOffset, xysLen, indices, indicesOffset, indicesLen, indexBase); }
/** * Adds triangle primitives to a prepared batch. This must be preceded by calls to * {@link #setTexture} and {@link #prepare} to configure the texture and stable attributes. */ public void addTris (float[] xys, int xysOffset, int xysLen, float tw, float th, int[] indices, int indicesOffset, int indicesLen, int indexBase) { int vertIdx = beginPrimitive(xysLen/2, indicesLen), offset = vertPos; float[] verts = vertices, stables = stableAttrs; for (int ii = xysOffset, ll = ii+xysLen; ii < ll; ii += 2) { float x = xys[ii], y = xys[ii+1]; offset = add(verts, add(verts, offset, stables), x, y, x/tw, y/th); } vertPos = offset; addElems(vertIdx, indices, indicesOffset, indicesLen, indexBase); }
protected int beginPrimitive (int vertexCount, int elemCount) { // check whether we have enough room to hold this primitive int vertIdx = vertPos / vertexSize(); int verts = vertIdx + vertexCount, elems = elemPos + elemCount; int availVerts = vertices.length / vertexSize(), availElems = elements.length; if (verts <= availVerts && elems <= availElems) return vertIdx; // otherwise, flush and expand our buffers if needed flush(); if (verts > availVerts) expandVerts(verts); if (elems > availElems) expandElems(elems); return 0; }
@Override public void addQuad (int tint, float m00, float m01, float m10, float m11, float tx, float ty, float x1, float y1, float sx1, float sy1, float x2, float y2, float sx2, float sy2, float x3, float y3, float sx3, float sy3, float x4, float y4, float sx4, float sy4) { prepare(tint, m00, m01, m10, m11, tx, ty); int vertIdx = beginPrimitive(4, 6); int offset = vertPos; float[] verts = vertices, stables = stableAttrs; offset = add(verts, add(verts, offset, stables), x1, y1, sx1, sy1); offset = add(verts, add(verts, offset, stables), x2, y2, sx2, sy2); offset = add(verts, add(verts, offset, stables), x3, y3, sx3, sy3); offset = add(verts, add(verts, offset, stables), x4, y4, sx4, sy4); vertPos = offset; addElems(vertIdx, QUAD_INDICES, 0, QUAD_INDICES.length, 0); }
@Override public void begin (float fbufWidth, float fbufHeight, boolean flip) { super.begin(fbufWidth, fbufHeight, flip); gl.glUniform1f(uAngle, elapsed * FloatMath.PI); gl.glUniform2f(uEye, eyeX, eyeY); } }
@Override public void flush () { super.flush(); if (vertPos > 0) { bindTexture(); if (delayedBinding) { bindAttribsBufs(); // see comments in activate() gl.checkError("TriangleBatch.flush bind"); } gl.bufs.setFloatBuffer(vertices, 0, vertPos); gl.glBufferData(GL_ARRAY_BUFFER, vertPos*4, gl.bufs.floatBuffer, GL_STREAM_DRAW); gl.bufs.setShortBuffer(elements, 0, elemPos); gl.glBufferData(GL_ELEMENT_ARRAY_BUFFER, elemPos*2, gl.bufs.shortBuffer, GL_STREAM_DRAW); gl.checkError("TriangleBatch.flush BufferData"); gl.glDrawElements(GL_TRIANGLES, elemPos, GL_UNSIGNED_SHORT, 0); gl.checkError("TriangleBatch.flush DrawElements"); vertPos = 0; elemPos = 0; } }
protected void paintImpl (Surface surf) { // fill some shapes with patterns surf.setFillPattern(ttex).fillRect(10, 0, 100, 100); // render a sliding window of half of our triangles to test the slice rendering triangleBatch.addTris(ttex, Tint.NOOP_TINT, af, verts, offset*4, (hsamples+1)*4, ttex.width(), ttex.height(), indices, offset*6, hsamples*6, offset*2); offset += doff; if (offset == 0) doff = 1; else if (offset == hsamples) doff = -1; } private int offset = 0, doff = 1;
@Override public void begin (float fbufWidth, float fbufHeight, boolean flip) { super.begin(fbufWidth, fbufHeight, flip); program.activate(); gl.glUniform2f(uHScreenSize, fbufWidth/2f, fbufHeight/2f); gl.glUniform1f(uFlip, flip ? -1 : 1); // certain graphics cards (I'm looking at you, Intel) exhibit broken behavior if we bind our // attributes once during activation, so for those cards we bind every time in flush() if (!delayedBinding) bindAttribsBufs(); gl.checkError("TriangleBatch begin"); }
/** * See {@link #prepare(int,AffineTransform)}. */ public void prepare (int tint, float m00, float m01, float m10, float m11, float tx, float ty) { float[] stables = stableAttrs; stables[0] = m00; stables[1] = m01; stables[2] = m10; stables[3] = m11; stables[4] = tx; stables[5] = ty; stables[6] = (tint >> 16) & 0xFFFF; // ar stables[7] = (tint >> 0) & 0xFFFF; // gb addExtraStableAttrs(stables, 8); }
protected int beginPrimitive (int vertexCount, int elemCount) { // check whether we have enough room to hold this primitive int vertIdx = vertPos / vertexSize(); int verts = vertIdx + vertexCount, elems = elemPos + elemCount; int availVerts = vertices.length / vertexSize(), availElems = elements.length; if (verts <= availVerts && elems <= availElems) return vertIdx; // otherwise, flush and expand our buffers if needed flush(); if (verts > availVerts) expandVerts(verts); if (elems > availElems) expandElems(elems); return 0; }
@Override public void addQuad (int tint, float m00, float m01, float m10, float m11, float tx, float ty, float x1, float y1, float sx1, float sy1, float x2, float y2, float sx2, float sy2, float x3, float y3, float sx3, float sy3, float x4, float y4, float sx4, float sy4) { prepare(tint, m00, m01, m10, m11, tx, ty); int vertIdx = beginPrimitive(4, 6); int offset = vertPos; float[] verts = vertices, stables = stableAttrs; offset = add(verts, add(verts, offset, stables), x1, y1, sx1, sy1); offset = add(verts, add(verts, offset, stables), x2, y2, sx2, sy2); offset = add(verts, add(verts, offset, stables), x3, y3, sx3, sy3); offset = add(verts, add(verts, offset, stables), x4, y4, sx4, sy4); vertPos = offset; addElems(vertIdx, QUAD_INDICES, 0, QUAD_INDICES.length, 0); }
@Override public void begin (float fbufWidth, float fbufHeight, boolean flip) { super.begin(fbufWidth, fbufHeight, flip); program.activate(); gl.glUniform1f(uAngle, angle); }
@Override public void flush () { super.flush(); if (vertPos > 0) { bindTexture(); if (delayedBinding) { bindAttribsBufs(); // see comments in activate() gl.checkError("TriangleBatch.flush bind"); } gl.bufs.setFloatBuffer(vertices, 0, vertPos); gl.glBufferData(GL_ARRAY_BUFFER, vertPos*4, gl.bufs.floatBuffer, GL_STREAM_DRAW); gl.bufs.setShortBuffer(elements, 0, elemPos); gl.glBufferData(GL_ELEMENT_ARRAY_BUFFER, elemPos*2, gl.bufs.shortBuffer, GL_STREAM_DRAW); gl.checkError("TriangleBatch.flush BufferData"); gl.glDrawElements(GL_TRIANGLES, elemPos, GL_UNSIGNED_SHORT, 0); gl.checkError("TriangleBatch.flush DrawElements"); vertPos = 0; elemPos = 0; } }
@Override public void begin (float fbufWidth, float fbufHeight, boolean flip) { super.begin(fbufWidth, fbufHeight, flip); program.activate(); gl.glUniform2f(uHScreenSize, fbufWidth/2f, fbufHeight/2f); gl.glUniform1f(uFlip, flip ? -1 : 1); // certain graphics cards (I'm looking at you, Intel) exhibit broken behavior if we bind our // attributes once during activation, so for those cards we bind every time in flush() if (!delayedBinding) bindAttribsBufs(); gl.checkError("TriangleBatch begin"); }
/** * See {@link #prepare(int,AffineTransform)}. */ public void prepare (int tint, float m00, float m01, float m10, float m11, float tx, float ty) { float[] stables = stableAttrs; stables[0] = m00; stables[1] = m01; stables[2] = m10; stables[3] = m11; stables[4] = tx; stables[5] = ty; stables[6] = (tint >> 16) & 0xFFFF; // ar stables[7] = (tint >> 0) & 0xFFFF; // gb addExtraStableAttrs(stables, 8); }
/** * Adds a collection of textured triangles to the current render operation. See * {@link #addTris(Texture,int,AffineTransform,float[],int,int,float,float,int[],int,int,int)} * for parameter documentation. * * @param sxys a list of sx/sy texture coordinates as: {@code [sx1, sy1, sx2, sy2, ...]}. This * must be of the same length as {@code xys}. */ public void addTris (Texture tex, int tint, AffineTransform xf, float[] xys, float[] sxys, int xysOffset, int xysLen, int[] indices, int indicesOffset, int indicesLen, int indexBase) { setTexture(tex); prepare(tint, xf); addTris(xys, sxys, xysOffset, xysLen, indices, indicesOffset, indicesLen, indexBase); }
/** * Adds triangle primitives to a prepared batch. This must be preceded by calls to * {@link #setTexture} and {@link #prepare} to configure the texture and stable attributes. */ public void addTris (float[] xys, float[] sxys, int xysOffset, int xysLen, int[] indices, int indicesOffset, int indicesLen, int indexBase) { int vertIdx = beginPrimitive(xysLen/2, indicesLen), offset = vertPos; float[] verts = vertices, stables = stableAttrs; for (int ii = xysOffset, ll = ii+xysLen; ii < ll; ii += 2) { offset = add(verts, add(verts, offset, stables), xys[ii], xys[ii+1], sxys[ii], sxys[ii+1]); } vertPos = offset; addElems(vertIdx, indices, indicesOffset, indicesLen, indexBase); }
/** Creates the {@link QuadBatch} used as the default top-level batch when rendering the scene * graph. This uses {@link UniformQuadBatch} if possible, {@link TriangleBatch} otherwise. */ protected QuadBatch createDefaultBatch (GL20 gl) { try { if (UniformQuadBatch.isLikelyToPerform(gl)) return new UniformQuadBatch(gl); } catch (Exception e) { // oops, fall through and use a TriangleBatch } return new TriangleBatch(gl); } }
float[] xys, int xysOffset, int xysLen, float tw, float th, int[] indices, int indicesOffset, int indicesLen, int indexBase) { setTexture(tex); prepare(tint, xf); addTris(xys, xysOffset, xysLen, tw, th, indices, indicesOffset, indicesLen, indexBase);