From 7e0f6da9915b6b63fec2835257eb9417d4e11d8a Mon Sep 17 00:00:00 2001 From: codeanticode Date: Sun, 29 Jan 2012 15:34:34 +0000 Subject: [PATCH] Replacing linear expansion by post-trimming --- android/core/src/processing/core/PGL.java | 83 ++++--- .../processing/core/PGraphicsAndroid3D.java | 230 ++++++++++++++---- .../core/src/processing/core/PShape3D.java | 10 +- 3 files changed, 240 insertions(+), 83 deletions(-) diff --git a/android/core/src/processing/core/PGL.java b/android/core/src/processing/core/PGL.java index bdd6033ca..72fea16f5 100644 --- a/android/core/src/processing/core/PGL.java +++ b/android/core/src/processing/core/PGL.java @@ -80,14 +80,6 @@ public class PGL { public static final int DEFAULT_TESS_VERTICES = 16; public static final int DEFAULT_TESS_INDICES = 32; - /** In and tess increments used in retained mode, where - * arrays are not doubled in size but increased linearly. - * */ - public static final int IN_VERTICES_INCREMENT = 8; - public static final int IN_EDGES_INCREMENT = 16; - public static final int TESS_VERTICES_INCREMENT = 8; - public static final int TESS_INDICES_INCREMENT = 16; - /** Initial sizes for vertex cache used in PShape3D. */ public static final int DEFAULT_VERTEX_CACHE_SIZE = 512; @@ -617,6 +609,7 @@ public class PGL { // Error public int getError() { + //return GLES20.glGetError(); return gl.glGetError(); } @@ -629,34 +622,42 @@ public class PGL { // Rendering options public void enableDepthTest() { + //GLES20.glEnable(GLES20.GL_DEPTH_TEST); gl.glEnable(GL10.GL_DEPTH_TEST); } public void disableDepthTest() { + //GLES20.glDisable(GLES20.GL_DEPTH_TEST); gl.glDisable(GL10.GL_DEPTH_TEST); } public void enableDepthMask() { + //GLES20.glDepthMask(true); gl.glDepthMask(true); } public void disableDepthMask() { + //GLES20.glDepthMask(false); gl.glDepthMask(false); } public void setDepthFunc(int func) { + //GLES20.glDepthFunc(func); gl.glDepthFunc(func); } public void setShadeModel(int model) { + // ?? gl.glShadeModel(model); } public void setFrontFace(int mode) { + //GLES20.glFrontFace(mode); gl.glFrontFace(mode); } public void enableMultisample() { + //GLES20.glEnable(??); gl.glEnable(GL10.GL_MULTISAMPLE); } @@ -665,6 +666,7 @@ public class PGL { } public void enablePointSmooth() { + //GLES20.glEnable(??); gl.glEnable(GL10.GL_POINT_SMOOTH); } @@ -730,6 +732,7 @@ public class PGL { // Textures public void genTexture(int[] id) { + //GLES20.glGenTextures(1, id, 0); gl.glGenTextures(1, id, 0); } @@ -742,6 +745,7 @@ public class PGL { } public void setActiveTexUnit(int tu) { + //GLES20.glActiveTexture(GLES20.GL_TEXTURE0 + tu); gl.glActiveTexture(GL10.GL_TEXTURE0 + tu); } @@ -758,6 +762,7 @@ public class PGL { } public void initTex(int target, int format, int w, int h) { + //GLES20.glTexImage2D(target, 0, format, w, h, 0, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, null); gl.glTexImage2D(target, 0, format, w, h, 0, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, null); } @@ -778,6 +783,7 @@ public class PGL { } public void enableTexMipmapGen(int target) { + //GLES20.glTexParameterf(target, ??, GL10.GL_TRUE); gl.glTexParameterf(target, GL11.GL_GENERATE_MIPMAP, GL10.GL_TRUE); } @@ -786,6 +792,7 @@ public class PGL { } public void setTexMinFilter(int target, int filter) { + //GLES20.glTexParameterf(target, GLES20.GL_TEXTURE_MIN_FILTER, filter); gl.glTexParameterf(target, GL10.GL_TEXTURE_MIN_FILTER, filter); } @@ -1044,73 +1051,73 @@ public class PGL { // Shaders public void genProgram(int[] id) { - //id[0] = gl2x.glCreateProgram(); + //id[0] = GLES20.glCreateProgram(); } public void delProgram(int[] id) { - //gl2x.glDeleteProgram(id[0]); + //GLES20.glDeleteProgram(id[0]); } public void genVertexShader(int[] id) { - //id[0] = gl2x.glCreateShader(GL2.GL_VERTEX_SHADER); + //id[0] = GLES20.glCreateShader(GLES20.GL_VERTEX_SHADER); } public void delVertexShader(int[] id) { - //gl2x.glDeleteShader(id[0]); + //GLES20.glDeleteShader(id[0]); } public void genFragmentShader(int[] id) { - //id[0] = gl2x.glCreateShader(GL2.GL_FRAGMENT_SHADER); + //id[0] = GLES20.glCreateShader(GLES20.GL_FRAGMENT_SHADER); } public void delFragmentShader(int[] id) { - //gl2x.glDeleteShader(id[0]); + //GLES20.glDeleteShader(id[0]); } public void linkProgram(int prog) { - //gl2.glLinkProgram(prog); + //GLES20.glLinkProgram(prog); } public void validateProgram(int prog) { - //gl2.glValidateProgram(prog); + //GLES20.glValidateProgram(prog); } public void startProgram(int prog) { - //gl2.glUseProgramObjectARB(prog); + //GLES20.glUseProgram(prog); } public void stopProgram() { - //gl2.glUseProgramObjectARB(0); + //GLES20.glUseProgram(0); } public int getAttribLocation(int prog, String name) { - //return gl2.glGetAttribLocation(prog, name); + //return GLES20.glGetAttribLocation(prog, name); return -1; } public int getUniformLocation(int prog, String name) { - //return gl2.glGetUniformLocation(prog, name); + //return GLES20.glGetUniformLocation(prog, name); return -1; } public void setIntUniform(int loc, int value) { - //gl2.glUniform1i(loc, value); + //GLES20.glUniform1i(loc, value); } public void setFloatUniform(int loc, float value) { - //gl2.glUniform1f(loc, value); + //GLES20.glUniform1f(loc, value); } public void setFloatUniform(int loc, float value0, float value1) { - //gl2.glUniform2f(loc, value0, value1); + //GLES20.glUniform2f(loc, value0, value1); } public void setFloatUniform(int loc, float value0, float value1, float value2) { - //gl2.glUniform3f(loc, value0, value1, value2); + //GLES20.glUniform3f(loc, value0, value1, value2); } public void setFloatUniform(int loc, float value0, float value1, float value2, float value3) { - //gl2.glUniform4f(loc, value0, value1, value2, value3); + //GLES20.glUniform4f(loc, value0, value1, value2, value3); } public void setMatUniform(int loc, float m00, float m01, @@ -1118,7 +1125,7 @@ public class PGL { float[] mat = new float[4]; mat[0] = m00; mat[4] = m01; mat[1] = m10; mat[5] = m11; - //gl2.glUniformMatrix2fv(loc, 1, false, mat, 0); + //GLES20.glUniformMatrix2fv(loc, 1, false, mat, 0); } public void setMatUniform(int loc, float m00, float m01, float m02, @@ -1128,7 +1135,7 @@ public class PGL { mat[0] = m00; mat[4] = m01; mat[ 8] = m02; mat[1] = m10; mat[5] = m11; mat[ 9] = m12; mat[2] = m20; mat[6] = m21; mat[10] = m22; - //gl2.glUniformMatrix3fv(loc, 1, false, mat, 0); + //GLES20.glUniformMatrix3fv(loc, 1, false, mat, 0); } public void setMatUniform(int loc, float m00, float m01, float m02, float m03, @@ -1140,41 +1147,41 @@ public class PGL { mat[1] = m10; mat[5] = m11; mat[ 9] = m12; mat[13] = m13; mat[2] = m20; mat[6] = m21; mat[10] = m22; mat[14] = m23; mat[3] = m30; mat[7] = m31; mat[11] = m32; mat[15] = m33; - //gl2.glUniformMatrix4fv(loc, 1, false, mat, 0); + //GLES20.glUniformMatrix4fv(loc, 1, false, mat, 0); } public void setFloatAttrib(int loc, float value) { - //gl2.glVertexAttrib1f(loc, value); + //GLES20.glVertexAttrib1f(loc, value); } public void setFloatAttrib(int loc, float value0, float value1) { - //gl2.glVertexAttrib2f(loc, value0, value1); + //GLES20.glVertexAttrib2f(loc, value0, value1); } public void setFloatAttrib(int loc, float value0, float value1, float value2) { - //gl2.glVertexAttrib3f(loc, value0, value1, value2); + //GLES20.glVertexAttrib3f(loc, value0, value1, value2); } public void setFloatAttrib(int loc, float value0, float value1, float value2, float value3) { - //gl2.glVertexAttrib4f(loc, value0, value1, value2, value3); + //GLES20.glVertexAttrib4f(loc, value0, value1, value2, value3); } public void setShaderSource(int id, String source) { - //gl2.glShaderSource(id, 1, new String[] { source }, (int[]) null, 0); + //GLES20.glShaderSource(id, 1, new String[] { source }, (int[]) null, 0); } public void compileShader(int id) { - //gl2.glCompileShader(id); + //GLES20.glCompileShader(id); } public void attachShader(int prog, int shader) { - //gl2.glAttachObjectARB(prog, shader); + //GLES20.glAttachShader(prog, shader); } public String getShaderLog(int id) { /* IntBuffer val = IntBuffer.allocate(1); - gl2.glGetObjectParameterivARB(id, GL2.GL_OBJECT_INFO_LOG_LENGTH_ARB, val); + GLES20.glGetObjectParameteriv(id, GLES20.GL_OBJECT_INFO_LOG_LENGTH, val); int length = val.get(); @@ -1186,7 +1193,7 @@ public class PGL { ByteBuffer infoLog = ByteBuffer.allocate(length); val.flip(); - gl2.glGetInfoLogARB(id, length, val, infoLog); + GLES20.glGetInfoLog(id, length, val, infoLog); byte[] infoBytes = new byte[length]; infoLog.get(infoBytes); @@ -1256,7 +1263,7 @@ public class PGL { gl.glLoadMatrixf(mat, 0); } - public void translate(float tx, float ty, float tz) { + public void translate(float tx, float ty, float tz) { gl.glTranslatef(tx, ty, tz); } diff --git a/android/core/src/processing/core/PGraphicsAndroid3D.java b/android/core/src/processing/core/PGraphicsAndroid3D.java index 31204b7dd..eae452da4 100644 --- a/android/core/src/processing/core/PGraphicsAndroid3D.java +++ b/android/core/src/processing/core/PGraphicsAndroid3D.java @@ -6225,6 +6225,16 @@ public class PGraphicsAndroid3D extends PGraphics { reset(); } + public void trim() { + if (vertexCount < vertices.length / 3) { + trimVertices(); + trimColors(); + trimNormals(); + trimTexcoords(); + trimEdges(); + } + } + public void dispose() { codes = null; vertices = null; @@ -6351,22 +6361,7 @@ public class PGraphicsAndroid3D extends PGraphics { public void vertexCheck() { if (vertexCount == vertices.length / 3) { - int newSize = vertexCount; - - // Increase of vertex arrays is different between - // immediate and retained modes: - // * in immediate mode, since we need to very quickly - // have larger arrays in order to accomodate the - // incoming geometry, doubling of size is used. - // * in retained mode, since the arrays are used - // to create arrays for individual shapes that - // don't change afterwards, we only need linear - // increase. - if (renderMode == IMMEDIATE) { - newSize <<= 1; - } else { - newSize += PGL.IN_VERTICES_INCREMENT; - } + int newSize = vertexCount << 1; expandCodes(newSize); expandVertices(newSize); @@ -6449,13 +6444,7 @@ public class PGraphicsAndroid3D extends PGraphics { public void edgeCheck() { if (edgeCount == edges.length) { - int newLen = edgeCount; - - if (renderMode == IMMEDIATE) { - newLen <<= 1; - } else { - newLen += PGL.IN_EDGES_INCREMENT; - } + int newLen = edgeCount << 1; int temp[][] = new int[newLen][3]; PApplet.arrayCopy(edges, 0, temp, 0, edgeCount); @@ -6475,7 +6464,7 @@ public class PGraphicsAndroid3D extends PGraphics { vertices = temp; } - protected void expandColors(int n){ + protected void expandColors(int n) { float temp[] = new float[4 * n]; PApplet.arrayCopy(colors, 0, temp, 0, 4 * vertexCount); colors = temp; @@ -6499,6 +6488,42 @@ public class PGraphicsAndroid3D extends PGraphics { strokes = temp; } + protected void trimVertices() { + float temp[] = new float[3 * vertexCount]; + PApplet.arrayCopy(vertices, 0, temp, 0, 3 * vertexCount); + vertices = temp; + } + + protected void trimColors() { + float temp[] = new float[4 * vertexCount]; + PApplet.arrayCopy(colors, 0, temp, 0, 4 * vertexCount); + colors = temp; + } + + protected void trimNormals() { + float temp[] = new float[3 * vertexCount]; + PApplet.arrayCopy(normals, 0, temp, 0, 3 * vertexCount); + normals = temp; + } + + protected void trimTexcoords() { + float temp[] = new float[2 * vertexCount]; + PApplet.arrayCopy(texcoords, 0, temp, 0, 2 * vertexCount); + texcoords = temp; + } + + protected void packStrokes() { + float temp[] = new float[5 * vertexCount]; + PApplet.arrayCopy(strokes, 0, temp, 0, 5 * vertexCount); + strokes = temp; + } + + protected void trimEdges() { + int temp[][] = new int[edgeCount][3]; + PApplet.arrayCopy(edges, 0, temp, 0, edgeCount); + edges = temp; + } + public int getNumLineVertices() { return 4 *(lastEdge - firstEdge + 1); } @@ -6858,6 +6883,131 @@ public class PGraphicsAndroid3D extends PGraphics { reset(); } + public void trim() { + if (fillVertexCount < fillVertices.length / 3) { + trimFillVertices(); + trimFillColors(); + trimFillNormals(); + trimFillTexcoords(); + } + + if (fillIndexCount < fillIndices.length) { + trimFillIndices(); + } + + if (lineVertexCount < lineVertices.length / 3) { + trimLineVertices(); + trimLineColors(); + trimLineNormals(); + trimLineAttributes(); + } + + if (lineIndexCount < lineIndices.length) { + trimLineIndices(); + } + + if (pointVertexCount < pointVertices.length / 3) { + trimPointVertices(); + trimPointColors(); + trimPointNormals(); + trimPointAttributes(); + } + + if (pointIndexCount < pointIndices.length) { + trimPointIndices(); + } + } + + protected void trimFillVertices() { + float temp[] = new float[3 * fillVertexCount]; + PApplet.arrayCopy(fillVertices, 0, temp, 0, 3 * fillVertexCount); + fillVertices = temp; + } + + protected void trimFillColors() { + float temp[] = new float[4 * fillVertexCount]; + PApplet.arrayCopy(fillColors, 0, temp, 0, 4 * fillVertexCount); + fillColors = temp; + } + + protected void trimFillNormals() { + float temp[] = new float[3 * fillVertexCount]; + PApplet.arrayCopy(fillNormals, 0, temp, 0, 3 * fillVertexCount); + fillNormals = temp; + } + + protected void trimFillTexcoords() { + float temp[] = new float[2 * fillVertexCount]; + PApplet.arrayCopy(fillTexcoords, 0, temp, 0, 2 * fillVertexCount); + fillTexcoords = temp; + } + + public void trimFillIndices() { + short temp[] = new short[fillIndexCount]; + PApplet.arrayCopy(fillIndices, 0, temp, 0, fillIndexCount); + fillIndices = temp; + } + + protected void trimLineVertices() { + float temp[] = new float[3 * lineVertexCount]; + PApplet.arrayCopy(lineVertices, 0, temp, 0, 3 * lineVertexCount); + lineVertices = temp; + } + + protected void trimLineColors() { + float temp[] = new float[4 * lineVertexCount]; + PApplet.arrayCopy(lineColors, 0, temp, 0, 4 * lineVertexCount); + lineColors = temp; + } + + protected void trimLineNormals() { + float temp[] = new float[3 * lineVertexCount]; + PApplet.arrayCopy(lineNormals, 0, temp, 0, 3 * lineVertexCount); + lineNormals = temp; + } + + protected void trimLineAttributes() { + float temp[] = new float[4 * lineVertexCount]; + PApplet.arrayCopy(lineAttributes, 0, temp, 0, 4 * lineVertexCount); + lineAttributes = temp; + } + + protected void trimLineIndices() { + short temp[] = new short[lineVertexCount]; + PApplet.arrayCopy(lineIndices, 0, temp, 0, lineIndexCount); + lineIndices = temp; + } + + protected void trimPointVertices() { + float temp[] = new float[3 * pointVertexCount]; + PApplet.arrayCopy(pointVertices, 0, temp, 0, 3 * pointVertexCount); + pointVertices = temp; + } + + protected void trimPointColors() { + float temp[] = new float[4 * pointVertexCount]; + PApplet.arrayCopy(pointColors, 0, temp, 0, 4 * pointVertexCount); + pointColors = temp; + } + + protected void trimPointNormals() { + float temp[] = new float[3 * pointVertexCount]; + PApplet.arrayCopy(pointNormals, 0, temp, 0, 3 * pointVertexCount); + pointNormals = temp; + } + + protected void trimPointAttributes() { + float temp[] = new float[2 * pointVertexCount]; + PApplet.arrayCopy(pointAttributes, 0, temp, 0, 2 * pointVertexCount); + pointAttributes = temp; + } + + protected void trimPointIndices() { + short temp[] = new short[pointIndexCount]; + PApplet.arrayCopy(pointIndices, 0, temp, 0, pointIndexCount); + pointIndices = temp; + } + public void dipose() { fillVertices = null; fillColors = null; @@ -7172,25 +7322,25 @@ public class PGraphicsAndroid3D extends PGraphics { lastLineVertex = lineVertexCount - 1; } - public void expandLineVertices(int n) { + protected void expandLineVertices(int n) { float temp[] = new float[3 * n]; PApplet.arrayCopy(lineVertices, 0, temp, 0, 3 * lineVertexCount); lineVertices = temp; } - public void expandLineColors(int n) { + protected void expandLineColors(int n) { float temp[] = new float[4 * n]; PApplet.arrayCopy(lineColors, 0, temp, 0, 4 * lineVertexCount); lineColors = temp; } - public void expandLineNormals(int n) { + protected void expandLineNormals(int n) { float temp[] = new float[3 * n]; PApplet.arrayCopy(lineNormals, 0, temp, 0, 3 * lineVertexCount); lineNormals = temp; } - public void expandLineAttributes(int n) { + protected void expandLineAttributes(int n) { float temp[] = new float[4 * n]; PApplet.arrayCopy(lineAttributes, 0, temp, 0, 4 * lineVertexCount); lineAttributes = temp; @@ -7209,7 +7359,7 @@ public class PGraphicsAndroid3D extends PGraphics { lastLineIndex = lineIndexCount - 1; } - public void expandLineIndices(int n) { + protected void expandLineIndices(int n) { short temp[] = new short[n]; PApplet.arrayCopy(lineIndices, 0, temp, 0, lineIndexCount); lineIndices = temp; @@ -7231,25 +7381,25 @@ public class PGraphicsAndroid3D extends PGraphics { lastPointVertex = pointVertexCount - 1; } - public void expandPointVertices(int n) { + protected void expandPointVertices(int n) { float temp[] = new float[3 * n]; PApplet.arrayCopy(pointVertices, 0, temp, 0, 3 * pointVertexCount); pointVertices = temp; } - public void expandPointColors(int n) { + protected void expandPointColors(int n) { float temp[] = new float[4 * n]; PApplet.arrayCopy(pointColors, 0, temp, 0, 4 * pointVertexCount); pointColors = temp; } - public void expandPointNormals(int n) { + protected void expandPointNormals(int n) { float temp[] = new float[3 * n]; PApplet.arrayCopy(pointNormals, 0, temp, 0, 3 * pointVertexCount); pointNormals = temp; } - public void expandPointAttributes(int n) { + protected void expandPointAttributes(int n) { float temp[] = new float[2 * n]; PApplet.arrayCopy(pointAttributes, 0, temp, 0, 2 * pointVertexCount); pointAttributes = temp; @@ -7268,7 +7418,7 @@ public class PGraphicsAndroid3D extends PGraphics { lastPointIndex = pointIndexCount - 1; } - public void expandPointIndices(int n) { + protected void expandPointIndices(int n) { short temp[] = new short[n]; PApplet.arrayCopy(pointIndices, 0, temp, 0, pointIndexCount); pointIndices = temp; @@ -7541,11 +7691,7 @@ public class PGraphicsAndroid3D extends PGraphics { public int expandVertSize(int currSize, int newMinSize) { int newSize = currSize; while (newSize < newMinSize) { - if (renderMode == IMMEDIATE) { - newSize <<= 1; - } else { - newSize += PGL.TESS_VERTICES_INCREMENT; - } + newSize <<= 1; } return newSize; } @@ -7553,11 +7699,7 @@ public class PGraphicsAndroid3D extends PGraphics { public int expandIndSize(int currSize, int newMinSize) { int newSize = currSize; while (newSize < newMinSize) { - if (renderMode == IMMEDIATE) { - newSize <<= 1; - } else { - newSize += PGL.TESS_INDICES_INCREMENT; - } + newSize <<= 1; } return newSize; } diff --git a/android/core/src/processing/core/PShape3D.java b/android/core/src/processing/core/PShape3D.java index 0909dee1d..3011c1d87 100644 --- a/android/core/src/processing/core/PShape3D.java +++ b/android/core/src/processing/core/PShape3D.java @@ -684,6 +684,10 @@ public class PShape3D extends PShape { return; } + // Input arrays are trimmed since they are expanded by doubling their old size, + // which might lead to arrays larger than the vertex counts. + in.trim(); + isClosed = mode == CLOSE; root.tessellated = false; tessellated = false; @@ -2269,9 +2273,13 @@ public class PShape3D extends PShape { // equivalent to use POLYGON with fill disabled. } + // Tessellated arrays are trimmed since they are expanded by doubling their old size, + // which might lead to arrays larger than the vertex counts. + tess.trim(); + if (texture != null && parent != null) { ((PShape3D)parent).addTexture(texture); - } + } } }