@Override protected Core createTextureCore() { return new ITCore(vertexShader(), textureFragmentShader()); }
@Override public void addQuad(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) { // write our stable vertex attributes into a buffer, then copy that in four times stableAttrs[0] = m00; stableAttrs[1] = m01; stableAttrs[2] = m10; stableAttrs[3] = m11; stableAttrs[4] = tx; stableAttrs[5] = ty; addExtraStableAttrs(stableAttrs, 6); int vertIdx = beginPrimitive(4, 6), offset = vertices.position(); float[] vertData = vertices.array(); offset = addVert(vertData, offset, stableAttrs, x1, y1, sx1, sy1); offset = addVert(vertData, offset, stableAttrs, x2, y2, sx2, sy2); offset = addVert(vertData, offset, stableAttrs, x3, y3, sx3, sy3); offset = addVert(vertData, offset, stableAttrs, x4, y4, sx4, sy4); vertices.skip(offset - vertices.position()); addElems(vertIdx, QUAD_INDICES, 0, QUAD_INDICES.length, 0); }
@Override public void flush() { super.flush(); if (vertices.position() == 0) return; ctx.checkGLError("Shader.flush"); if (delayedBinding) { // see comments in activate() bindAttribsBufs(); ctx.checkGLError("Shader.flush bind"); } vertices.send(GL20.GL_ARRAY_BUFFER, GL20.GL_STREAM_DRAW); int elems = elements.send(GL20.GL_ELEMENT_ARRAY_BUFFER, GL20.GL_STREAM_DRAW); ctx.checkGLError("Shader.flush BufferData"); elements.drawElements(GL20.GL_TRIANGLES, elems); ctx.checkGLError("Shader.flush DrawElements"); }
@Override public void addTriangles(float m00, float m01, float m10, float m11, float tx, float ty, float[] xys, float[] sxys, int xysOffset, int xysLen, int[] indices, int indicesOffset, int indicesLen, int indexBase) { stableAttrs[0] = m00; stableAttrs[1] = m01; stableAttrs[2] = m10; stableAttrs[3] = m11; stableAttrs[4] = tx; stableAttrs[5] = ty; addExtraStableAttrs(stableAttrs, 6); int vertIdx = beginPrimitive(xysLen/2, indicesLen); int offset = vertices.position(); float[] vertData = vertices.array(); for (int ii = xysOffset, ll = ii+xysLen; ii < ll; ii += 2) { offset = addVert(vertData, offset, stableAttrs, xys[ii], xys[ii+1], sxys[ii], sxys[ii+1]); } vertices.skip(offset - vertices.position()); addElems(vertIdx, indices, indicesOffset, indicesLen, indexBase); }
@Override public void addTriangles(float m00, float m01, float m10, float m11, float tx, float ty, float[] xys, int xysOffset, int xysLen, float tw, float th, int[] indices, int indicesOffset, int indicesLen, int indexBase) { stableAttrs[0] = m00; stableAttrs[1] = m01; stableAttrs[2] = m10; stableAttrs[3] = m11; stableAttrs[4] = tx; stableAttrs[5] = ty; addExtraStableAttrs(stableAttrs, 6); int vertIdx = beginPrimitive(xysLen/2, indicesLen); int offset = vertices.position(); float[] vertData = vertices.array(); for (int ii = xysOffset, ll = ii+xysLen; ii < ll; ii += 2) { float x = xys[ii], y = xys[ii+1]; offset = addVert(vertData, offset, stableAttrs, x, y, x/tw, y/th); } vertices.skip(offset - vertices.position()); addElems(vertIdx, indices, indicesOffset, indicesLen, indexBase); }
protected int vertexStride() { return vertexSize() * FLOAT_SIZE_BYTES; }
public ITCore(String vertShader, String fragShader) { super(vertShader, fragShader); // determine our various shader program locations uScreenSize = prog.getUniform2f("u_ScreenSize"); aMatrix = prog.getAttrib("a_Matrix", 4, GL20.GL_FLOAT); aTranslation = prog.getAttrib("a_Translation", 2, GL20.GL_FLOAT); aColor = prog.getAttrib("a_Color", 2, GL20.GL_FLOAT); aPosition = prog.getAttrib("a_Position", 2, GL20.GL_FLOAT); aTexCoord = prog.getAttrib("a_TexCoord", 2, GL20.GL_FLOAT); // create our vertex and index buffers stableAttrs = new float[stableAttrsSize()]; vertices = ctx.createFloatBuffer(START_VERTS*vertexSize()); elements = ctx.createShortBuffer(START_ELEMS); }
protected int beginPrimitive(int vertexCount, int elemCount) { int vertIdx = vertices.position() / vertexSize(); int verts = vertIdx + vertexCount, elems = elements.position() + elemCount; int availVerts = vertices.capacity() / vertexSize(), availElems = elements.capacity(); if ((verts > availVerts) || (elems > availElems)) { IndexedTrisShader.this.flush(); if (verts > availVerts) expandVerts(verts); if (elems > availElems) expandElems(elems); return 0; } return vertIdx; }
protected int vertexSize() { return stableAttrsSize() + 4; }
private void expandVerts(int vertCount) { int newVerts = vertices.capacity() / vertexSize(); while (newVerts < vertCount) newVerts += EXPAND_VERTS; vertices.expand(newVerts*vertexSize()); }
@Override public void activate(int fbufWidth, int fbufHeight) { prog.bind(); uScreenSize.bind(fbufWidth, fbufHeight); // 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(); ctx.checkGLError("Shader.activate bind"); }
private void bindAttribsBufs() { vertices.bind(GL20.GL_ARRAY_BUFFER); // bind our stable attributes int stride = vertexStride(); aMatrix.bind(stride, 0); aTranslation.bind(stride, 16); aColor.bind(stride, 24); // bind our changing attributes int offset = stableAttrsSize()*FLOAT_SIZE_BYTES; aPosition.bind(stride, offset); if (aTexCoord != null) aTexCoord.bind(stride, offset+8); elements.bind(GL20.GL_ELEMENT_ARRAY_BUFFER); }