From 67c0d5e9e4e338d6816ff584402ff98f3c14ab82 Mon Sep 17 00:00:00 2001 From: codeanticode Date: Sat, 31 Mar 2012 15:16:27 +0000 Subject: [PATCH] Using short indices on android and desktop --- android/core/src/processing/opengl/PGL.java | 18 ++++++++--- .../processing/opengl/PGraphicsOpenGL.java | 1 - .../opengl/src/processing/opengl/PGL.java | 32 +++++++++++++++---- .../processing/opengl/PGraphicsOpenGL.java | 32 +++++++++---------- .../src/processing/opengl/PShape3D.java | 13 ++++---- 5 files changed, 62 insertions(+), 34 deletions(-) diff --git a/android/core/src/processing/opengl/PGL.java b/android/core/src/processing/opengl/PGL.java index ec7dc7b34..271b933f5 100644 --- a/android/core/src/processing/opengl/PGL.java +++ b/android/core/src/processing/opengl/PGL.java @@ -31,6 +31,7 @@ import java.nio.IntBuffer; import java.util.Arrays; import processing.core.PApplet; +import processing.core.PGraphics; import processing.opengl.tess.PGLU; import processing.opengl.tess.PGLUtessellator; import processing.opengl.tess.PGLUtessellatorCallbackAdapter; @@ -81,9 +82,10 @@ public class PGL { public static final int MAX_LIGHTS = 8; /** Maximum number of tessellated vertices. GLES restricts the vertex indices - * to be of type short, so 2^16 = 65536 is the maximum possible number of - * vertices that can be referred to within a single VBO. */ - public static final int MAX_TESS_VERTICES = 65536; + * to be of type unsigned short. Since Java only supports native shorts as + * primitive type we have 2^15 = 32768 as the maximum number of vertices + * that can be referred to within a single VBO. */ + public static final int MAX_TESS_VERTICES = 32768; /** Maximum number of indices. 2 times the max number of * vertices to have good room for vertex reuse. */ @@ -1184,12 +1186,20 @@ public class PGL { static public short makeIndex(int intIdx) { + // The old hack to have unsigned shorts as indices using Java's + // signed shorts: // When the index value is greater than 32767, subtracting 65536 // will make it (as a short) to wrap around to the negative range, which // is all we need to later pass these numbers to opengl (which will // interpret them as unsigned shorts). See discussion here: // http://stackoverflow.com/questions/4331021/java-opengl-gldrawelements-with-32767-vertices - return 32767 < intIdx ? (short)(intIdx - 65536) : (short)intIdx; + //return 32767 < intIdx ? (short)(intIdx - 65536) : (short)intIdx; + if (32767 < intIdx) { + PGraphics.showWarning("P3D: Vertex index is greater than 32767"); + return 32767; + } else { + return (short)intIdx; + } } diff --git a/android/core/src/processing/opengl/PGraphicsOpenGL.java b/android/core/src/processing/opengl/PGraphicsOpenGL.java index e5faea60b..44c5eed02 100644 --- a/android/core/src/processing/opengl/PGraphicsOpenGL.java +++ b/android/core/src/processing/opengl/PGraphicsOpenGL.java @@ -26,7 +26,6 @@ import java.nio.*; import java.util.*; import processing.core.*; -import processing.opengl.PGraphicsOpenGL.Tessellator.TessellatorCallback; // drawPixels is missing...calls to glDrawPixels are commented out diff --git a/java/libraries/opengl/src/processing/opengl/PGL.java b/java/libraries/opengl/src/processing/opengl/PGL.java index 8fd4b78c2..08c2f1073 100644 --- a/java/libraries/opengl/src/processing/opengl/PGL.java +++ b/java/libraries/opengl/src/processing/opengl/PGL.java @@ -50,6 +50,8 @@ import javax.media.opengl.glu.GLU; import javax.media.opengl.glu.GLUtessellator; import javax.media.opengl.glu.GLUtessellatorCallbackAdapter; +import processing.core.PGraphics; + import com.jogamp.newt.awt.NewtCanvasAWT; import com.jogamp.newt.opengl.GLWindow; import com.jogamp.opengl.util.AnimatorBase; @@ -75,10 +77,10 @@ public class PGL { static final int SIZEOF_FLOAT = Float.SIZE / 8; /** Size of a vertex index. */ - static final int SIZEOF_INDEX = SIZEOF_INT; + static final int SIZEOF_INDEX = SIZEOF_SHORT; /** Type of a vertex index. */ - static final int INDEX_TYPE = GL.GL_UNSIGNED_INT; + static final int INDEX_TYPE = GL.GL_UNSIGNED_SHORT; /** Initial sizes for arrays of input and tessellated data. */ public static final int DEFAULT_IN_VERTICES = 64; @@ -93,8 +95,11 @@ public class PGL { /** Maximum lights by default is 8, the minimum defined by OpenGL. */ public static final int MAX_LIGHTS = 8; - /** Maximum number of tessellated vertices, using 2^19 for Mac/PC. */ - public static final int MAX_TESS_VERTICES = 524288; + /** Maximum number of tessellated vertices. GLES restricts the vertex indices + * to be of type unsigned short. Since Java only supports native shorts as + * primitive type we have 2^15 = 32768 as the maximum number of vertices + * that can be referred to within a single VBO. */ + public static final int MAX_TESS_VERTICES = 32768; /** Maximum number of indices. 2 times the max number of * vertices to have good room for vertex reuse. */ @@ -1221,9 +1226,22 @@ public class PGL { } - static public int makeIndex(int intIdx) { - return intIdx; - } + static public short makeIndex(int intIdx) { + // The old hack to have unsigned shorts as indices using Java's + // signed shorts: + // When the index value is greater than 32767, subtracting 65536 + // will make it (as a short) to wrap around to the negative range, which + // is all we need to later pass these numbers to opengl (which will + // interpret them as unsigned shorts). See discussion here: + // http://stackoverflow.com/questions/4331021/java-opengl-gldrawelements-with-32767-vertices + //return 32767 < intIdx ? (short)(intIdx - 65536) : (short)intIdx; + if (32767 < intIdx) { + PGraphics.showWarning("P3D: Vertex index is greater than 32767"); + return 32767; + } else { + return (short)intIdx; + } + } public void enableTexturing(int target) { diff --git a/java/libraries/opengl/src/processing/opengl/PGraphicsOpenGL.java b/java/libraries/opengl/src/processing/opengl/PGraphicsOpenGL.java index ed9f28adc..4829f4785 100644 --- a/java/libraries/opengl/src/processing/opengl/PGraphicsOpenGL.java +++ b/java/libraries/opengl/src/processing/opengl/PGraphicsOpenGL.java @@ -1152,7 +1152,7 @@ public class PGraphicsOpenGL extends PGraphics { pgl.glBindBuffer(PGL.GL_ELEMENT_ARRAY_BUFFER, glFillIndexBufferID); pgl.glBufferData(PGL.GL_ELEMENT_ARRAY_BUFFER, tessGeo.fillIndexCount * PGL.SIZEOF_INDEX, - IntBuffer.wrap(tessGeo.fillIndices, 0, tessGeo.fillIndexCount), vboMode); + ShortBuffer.wrap(tessGeo.fillIndices, 0, tessGeo.fillIndexCount), vboMode); } @@ -1236,7 +1236,7 @@ public class PGraphicsOpenGL extends PGraphics { pgl.glBindBuffer(PGL.GL_ELEMENT_ARRAY_BUFFER, glLineIndexBufferID); pgl.glBufferData(PGL.GL_ELEMENT_ARRAY_BUFFER, tessGeo.lineIndexCount * PGL.SIZEOF_INDEX, - IntBuffer.wrap(tessGeo.lineIndices, 0, tessGeo.lineIndexCount), vboMode); + ShortBuffer.wrap(tessGeo.lineIndices, 0, tessGeo.lineIndexCount), vboMode); } @@ -1304,7 +1304,7 @@ public class PGraphicsOpenGL extends PGraphics { pgl.glBindBuffer(PGL.GL_ELEMENT_ARRAY_BUFFER, glPointIndexBufferID); pgl.glBufferData(PGL.GL_ELEMENT_ARRAY_BUFFER, tessGeo.pointIndexCount * PGL.SIZEOF_INDEX, - IntBuffer.wrap(tessGeo.pointIndices, 0, tessGeo.pointIndexCount), vboMode); + ShortBuffer.wrap(tessGeo.pointIndices, 0, tessGeo.pointIndexCount), vboMode); } @@ -2477,7 +2477,7 @@ public class PGraphicsOpenGL extends PGraphics { int size = tessGeo.fillIndexCount; int sizex = size * PGL.SIZEOF_INDEX; pgl.glBindBuffer(PGL.GL_ELEMENT_ARRAY_BUFFER, glFillIndexBufferID); - pgl.glBufferData(PGL.GL_ELEMENT_ARRAY_BUFFER, sizex, IntBuffer.wrap(tessGeo.fillIndices, 0, size), vboMode); + pgl.glBufferData(PGL.GL_ELEMENT_ARRAY_BUFFER, sizex, ShortBuffer.wrap(tessGeo.fillIndices, 0, size), vboMode); pgl.glDrawElements(PGL.GL_TRIANGLES, size, PGL.INDEX_TYPE, 0); pgl.glBindBuffer(PGL.GL_ELEMENT_ARRAY_BUFFER, 0); @@ -6823,7 +6823,7 @@ public class PGraphicsOpenGL extends PGraphics { public int fillIndexCount; public int firstFillIndex; public int lastFillIndex; - public int[] fillIndices; + public short[] fillIndices; // Tessellated line data public int lineVertexCount; @@ -6836,7 +6836,7 @@ public class PGraphicsOpenGL extends PGraphics { public int lineIndexCount; public int firstLineIndex; public int lastLineIndex; - public int[] lineIndices; + public short[] lineIndices; // Tessellated point data public int pointVertexCount; @@ -6849,7 +6849,7 @@ public class PGraphicsOpenGL extends PGraphics { public int pointIndexCount; public int firstPointIndex; public int lastPointIndex; - public int[] pointIndices; + public short[] pointIndices; public boolean isStroked; @@ -6880,17 +6880,17 @@ public class PGraphicsOpenGL extends PGraphics { fillSpecular = new int[PGL.DEFAULT_TESS_VERTICES]; fillEmissive = new int[PGL.DEFAULT_TESS_VERTICES]; fillShininess = new float[PGL.DEFAULT_TESS_VERTICES]; - fillIndices = new int[PGL.DEFAULT_TESS_VERTICES]; + fillIndices = new short[PGL.DEFAULT_TESS_VERTICES]; lineVertices = new float[3 * PGL.DEFAULT_TESS_VERTICES]; lineColors = new int[PGL.DEFAULT_TESS_VERTICES]; lineDirWidths = new float[4 * PGL.DEFAULT_TESS_VERTICES]; - lineIndices = new int[PGL.DEFAULT_TESS_VERTICES]; + lineIndices = new short[PGL.DEFAULT_TESS_VERTICES]; pointVertices = new float[3 * PGL.DEFAULT_TESS_VERTICES]; pointColors = new int[PGL.DEFAULT_TESS_VERTICES]; pointSizes = new float[2 * PGL.DEFAULT_TESS_VERTICES]; - pointIndices = new int[PGL.DEFAULT_TESS_VERTICES]; + pointIndices = new short[PGL.DEFAULT_TESS_VERTICES]; clear(); } @@ -6981,7 +6981,7 @@ public class PGraphicsOpenGL extends PGraphics { } public void trimFillIndices() { - int temp[] = new int[fillIndexCount]; + short temp[] = new short[fillIndexCount]; PApplet.arrayCopy(fillIndices, 0, temp, 0, fillIndexCount); fillIndices = temp; } @@ -7005,7 +7005,7 @@ public class PGraphicsOpenGL extends PGraphics { } protected void trimLineIndices() { - int temp[] = new int[lineIndexCount]; + short temp[] = new short[lineIndexCount]; PApplet.arrayCopy(lineIndices, 0, temp, 0, lineIndexCount); lineIndices = temp; } @@ -7029,7 +7029,7 @@ public class PGraphicsOpenGL extends PGraphics { } protected void trimPointIndices() { - int temp[] = new int[pointIndexCount]; + short temp[] = new short[pointIndexCount]; PApplet.arrayCopy(pointIndices, 0, temp, 0, pointIndexCount); pointIndices = temp; } @@ -7207,7 +7207,7 @@ public class PGraphicsOpenGL extends PGraphics { } public void expandFillIndices(int n) { - int temp[] = new int[n]; + short temp[] = new short[n]; PApplet.arrayCopy(fillIndices, 0, temp, 0, fillIndexCount); fillIndices = temp; } @@ -7413,7 +7413,7 @@ public class PGraphicsOpenGL extends PGraphics { } protected void expandLineIndices(int n) { - int temp[] = new int[n]; + short temp[] = new short[n]; PApplet.arrayCopy(lineIndices, 0, temp, 0, lineIndexCount); lineIndices = temp; } @@ -7465,7 +7465,7 @@ public class PGraphicsOpenGL extends PGraphics { } protected void expandPointIndices(int n) { - int temp[] = new int[n]; + short temp[] = new short[n]; PApplet.arrayCopy(pointIndices, 0, temp, 0, pointIndexCount); pointIndices = temp; } diff --git a/java/libraries/opengl/src/processing/opengl/PShape3D.java b/java/libraries/opengl/src/processing/opengl/PShape3D.java index 23e4a2a4a..00fc873b9 100644 --- a/java/libraries/opengl/src/processing/opengl/PShape3D.java +++ b/java/libraries/opengl/src/processing/opengl/PShape3D.java @@ -41,6 +41,7 @@ import java.io.BufferedReader; import java.nio.ByteBuffer; import java.nio.FloatBuffer; import java.nio.IntBuffer; +import java.nio.ShortBuffer; import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; @@ -3414,9 +3415,9 @@ public class PShape3D extends PShape { } - protected void copyFillIndices(int offset, int size, int[] indices) { + protected void copyFillIndices(int offset, int size, short[] indices) { pgl.glBindBuffer(PGL.GL_ELEMENT_ARRAY_BUFFER, glFillIndexBufferID); - pgl.glBufferSubData(PGL.GL_ELEMENT_ARRAY_BUFFER, offset * PGL.SIZEOF_INDEX, size * PGL.SIZEOF_INDEX, IntBuffer.wrap(indices, 0, size)); + pgl.glBufferSubData(PGL.GL_ELEMENT_ARRAY_BUFFER, offset * PGL.SIZEOF_INDEX, size * PGL.SIZEOF_INDEX, ShortBuffer.wrap(indices, 0, size)); pgl.glBindBuffer(PGL.GL_ELEMENT_ARRAY_BUFFER, 0); } @@ -3508,9 +3509,9 @@ public class PShape3D extends PShape { } - protected void copyLineIndices(int offset, int size, int[] indices) { + protected void copyLineIndices(int offset, int size, short[] indices) { pgl.glBindBuffer(PGL.GL_ELEMENT_ARRAY_BUFFER, glLineIndexBufferID); - pgl.glBufferSubData(PGL.GL_ELEMENT_ARRAY_BUFFER, offset * PGL.SIZEOF_INDEX, size * PGL.SIZEOF_INDEX, IntBuffer.wrap(indices, 0, size)); + pgl.glBufferSubData(PGL.GL_ELEMENT_ARRAY_BUFFER, offset * PGL.SIZEOF_INDEX, size * PGL.SIZEOF_INDEX, ShortBuffer.wrap(indices, 0, size)); pgl.glBindBuffer(PGL.GL_ELEMENT_ARRAY_BUFFER, 0); } @@ -3602,9 +3603,9 @@ public class PShape3D extends PShape { } - protected void copyPointIndices(int offset, int size, int[] indices) { + protected void copyPointIndices(int offset, int size, short[] indices) { pgl.glBindBuffer(PGL.GL_ELEMENT_ARRAY_BUFFER, glPointIndexBufferID); - pgl.glBufferSubData(PGL.GL_ELEMENT_ARRAY_BUFFER, offset * PGL.SIZEOF_INDEX, size * PGL.SIZEOF_INDEX, IntBuffer.wrap(indices, 0, size)); + pgl.glBufferSubData(PGL.GL_ELEMENT_ARRAY_BUFFER, offset * PGL.SIZEOF_INDEX, size * PGL.SIZEOF_INDEX, ShortBuffer.wrap(indices, 0, size)); pgl.glBindBuffer(PGL.GL_ELEMENT_ARRAY_BUFFER, 0); }