diff --git a/core/src/processing/opengl/PGL.java b/core/src/processing/opengl/PGL.java index 09d4ee806..19bd6e5c6 100644 --- a/core/src/processing/opengl/PGL.java +++ b/core/src/processing/opengl/PGL.java @@ -32,7 +32,7 @@ import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.FloatBuffer; import java.nio.IntBuffer; -import java.util.Arrays; +import java.nio.ShortBuffer; import java.util.Timer; import java.util.TimerTask; @@ -455,6 +455,8 @@ public class PGL { +1.0f, +1.0f, 1.0f, 1.0f }; protected static FloatBuffer texData; + protected static ByteBuffer byteBuffer; + protected static IntBuffer intBuffer; protected static String texVertShaderSource = "attribute vec2 inVertex;" + @@ -837,29 +839,45 @@ public class PGL { } - public void getIntegerv(int name, int[] values, int offset) { - gl.glGetIntegerv(name, values, offset); +// public void getIntegerv(int name, int[] values, int offset) { +// gl.glGetIntegerv(name, values, offset); +// } + + + public void getIntegerv(int name, IntBuffer values) { + gl.glGetIntegerv(name, values); } - public void getFloatv(int name, float[] values, int offset) { - gl.glGetFloatv(name, values, offset); +// public void getFloatv(int name, float[] values, int offset) { +// gl.glGetFloatv(name, values, offset); +// } + + + public void getFloatv(int name, FloatBuffer values) { + gl.glGetFloatv(name, values); } - public void getBooleanv(int name, boolean[] values, int offset) { - if (-1 < name) { - byte[] bvalues = new byte[values.length]; - gl.glGetBooleanv(name, bvalues, offset); - for (int i = 0; i < values.length; i++) { - values[i] = bvalues[i] != 0; - } - } else { - Arrays.fill(values, false); - } +// public void getBooleanv(int name, boolean[] values, int offset) { +// if (-1 < name) { +// byte[] bvalues = new byte[values.length]; +// gl.glGetBooleanv(name, bvalues, offset); +// for (int i = 0; i < values.length; i++) { +// values[i] = bvalues[i] != 0; +// } +// } else { +// Arrays.fill(values, false); +// } +// } + + + public void getBooleanv(int name, ByteBuffer values) { + gl.glGetBooleanv(name, values); } + /////////////////////////////////////////////////////////// // Enable/disable caps @@ -939,13 +957,23 @@ public class PGL { // Textures - public void genTextures(int n, int[] ids, int offset) { - gl.glGenTextures(n, ids, offset); +// public void genTextures(int n, int[] ids, int offset) { +// gl.glGenTextures(n, ids, offset); +// } + + + public void genTextures(int n, IntBuffer ids) { + gl.glGenTextures(n, ids); } - public void deleteTextures(int n, int[] ids, int offset) { - gl.glDeleteTextures(n, ids, offset); +// public void deleteTextures(int n, int[] ids, int offset) { +// gl.glDeleteTextures(n, ids, offset); +// } + + + public void deleteTextures(int n, IntBuffer ids) { + gl.glDeleteTextures(n, ids); } @@ -990,9 +1018,14 @@ public class PGL { } - public void getTexParameteriv(int target, int param, int[] values, - int offset) { - gl.glGetTexParameteriv(target, param, values, offset); +// public void getTexParameteriv(int target, int param, int[] values, +// int offset) { +// gl.glGetTexParameteriv(target, param, values, offset); +// } + + + public void getTexParameteriv(int target, int param, IntBuffer values) { + gl.glGetTexParameteriv(target, param, values); } @@ -1006,13 +1039,23 @@ public class PGL { // Vertex Buffers - public void genBuffers(int n, int[] ids, int offset) { - gl.glGenBuffers(n, ids, offset); +// public void genBuffers(int n, int[] ids, int offset) { +// gl.glGenBuffers(n, ids, offset); +// } + + + public void genBuffers(int n, IntBuffer ids) { + gl.glGenBuffers(n, ids); } - public void deleteBuffers(int n, int[] ids, int offset) { - gl.glDeleteBuffers(n, ids, offset); +// public void deleteBuffers(int n, int[] ids, int offset) { +// gl.glDeleteBuffers(n, ids, offset); +// } + + + public void deleteBuffers(int n, IntBuffer ids) { + gl.glDeleteBuffers(n, ids); } @@ -1088,23 +1131,43 @@ public class PGL { // Framebuffers, renderbuffers - public void genFramebuffers(int n, int[] ids, int offset) { - gl.glGenFramebuffers(n, ids, offset); +// public void genFramebuffers(int n, int[] ids, int offset) { +// gl.glGenFramebuffers(n, ids, offset); +// } + + + public void genFramebuffers(int n, IntBuffer ids) { + gl.glGenFramebuffers(n, ids); } - public void deleteFramebuffers(int n, int[] ids, int offset) { - gl.glDeleteFramebuffers(n, ids, offset); +// public void deleteFramebuffers(int n, int[] ids, int offset) { +// gl.glDeleteFramebuffers(n, ids, offset); +// } + + + public void deleteFramebuffers(int n, IntBuffer ids) { + gl.glDeleteFramebuffers(n, ids); } - public void genRenderbuffers(int n, int[] ids, int offset) { - gl.glGenRenderbuffers(n, ids, offset); +// public void genRenderbuffers(int n, int[] ids, int offset) { +// gl.glGenRenderbuffers(n, ids, offset); +// } + + + public void genRenderbuffers(int n, IntBuffer ids) { + gl.glGenRenderbuffers(n, ids); } - public void deleteRenderbuffers(int n, int[] ids, int offset) { - gl.glDeleteRenderbuffers(n, ids, offset); +// public void deleteRenderbuffers(int n, int[] ids, int offset) { +// gl.glDeleteRenderbuffers(n, ids, offset); +// } + + + public void deleteRenderbuffers(int n, IntBuffer ids) { + gl.glDeleteRenderbuffers(n, ids); } @@ -1252,61 +1315,119 @@ public class PGL { } - public void uniform1iv(int loc, int count, int[] v, int offset) { - gl2.glUniform1iv(loc, count, v, offset); +// public void uniform1iv(int loc, int count, int[] v, int offset) { +// gl2.glUniform1iv(loc, count, v, offset); +// } + + + public void uniform1iv(int loc, int count, IntBuffer v) { + gl2.glUniform1iv(loc, count, v); } - public void uniform2iv(int loc, int count, int[] v, int offset) { - gl2.glUniform2iv(loc, count, v, offset); +// public void uniform2iv(int loc, int count, int[] v, int offset) { +// gl2.glUniform2iv(loc, count, v, offset); +// } + + + public void uniform2iv(int loc, int count, IntBuffer v) { + gl2.glUniform2iv(loc, count, v); } - public void uniform3iv(int loc, int count, int[] v, int offset) { - gl2.glUniform3iv(loc, count, v, offset); +// public void uniform3iv(int loc, int count, int[] v, int offset) { +// gl2.glUniform3iv(loc, count, v, offset); +// } + + + public void uniform3iv(int loc, int count, IntBuffer v) { + gl2.glUniform3iv(loc, count, v); } - public void uniform4iv(int loc, int count, int[] v, int offset) { - gl2.glUniform4iv(loc, count, v, offset); +// public void uniform4iv(int loc, int count, int[] v, int offset) { +// gl2.glUniform4iv(loc, count, v, offset); +// } + + + public void uniform4iv(int loc, int count, IntBuffer v) { + gl2.glUniform4iv(loc, count, v); } - public void uniform1fv(int loc, int count, float[] v, int offset) { - gl2.glUniform1fv(loc, count, v, offset); +// public void uniform1fv(int loc, int count, float[] v, int offset) { +// gl2.glUniform1fv(loc, count, v, offset); +// } + + + public void uniform1fv(int loc, int count, FloatBuffer v) { + gl2.glUniform1fv(loc, count, v); } - public void uniform2fv(int loc, int count, float[] v, int offset) { - gl2.glUniform2fv(loc, count, v, offset); +// public void uniform2fv(int loc, int count, float[] v, int offset) { +// gl2.glUniform2fv(loc, count, v, offset); +// } + + + public void uniform2fv(int loc, int count, FloatBuffer v) { + gl2.glUniform2fv(loc, count, v); } - public void uniform3fv(int loc, int count, float[] v, int offset) { - gl2.glUniform3fv(loc, count, v, offset); +// public void uniform3fv(int loc, int count, float[] v, int offset) { +// gl2.glUniform3fv(loc, count, v, offset); +// } + + + public void uniform3fv(int loc, int count, FloatBuffer v) { + gl2.glUniform3fv(loc, count, v); } - public void uniform4fv(int loc, int count, float[] v, int offset) { - gl2.glUniform4fv(loc, count, v, offset); +// public void uniform4fv(int loc, int count, float[] v, int offset) { +// gl2.glUniform4fv(loc, count, v, offset); +// } + + + public void uniform4fv(int loc, int count, FloatBuffer v) { + gl2.glUniform4fv(loc, count, v); } +// public void uniformMatrix2fv(int loc, int count, boolean transpose, +// float[] mat, int offset) { +// gl2.glUniformMatrix2fv(loc, count, transpose, mat, offset); +// } + + public void uniformMatrix2fv(int loc, int count, boolean transpose, - float[] mat, int offset) { - gl2.glUniformMatrix2fv(loc, count, transpose, mat, offset); + FloatBuffer mat) { + gl2.glUniformMatrix2fv(loc, count, transpose, mat); } +// public void uniformMatrix3fv(int loc, int count, boolean transpose, +// float[] mat, int offset) { +// gl2.glUniformMatrix3fv(loc, count, transpose, mat, offset); +// } + + public void uniformMatrix3fv(int loc, int count, boolean transpose, - float[] mat, int offset) { - gl2.glUniformMatrix3fv(loc, count, transpose, mat, offset); + FloatBuffer mat) { + gl2.glUniformMatrix3fv(loc, count, transpose, mat); } +// public void uniformMatrix4fv(int loc, int count, boolean transpose, +// float[] mat, int offset) { +// gl2.glUniformMatrix4fv(loc, count, transpose, mat, offset); +// } + + public void uniformMatrix4fv(int loc, int count, boolean transpose, - float[] mat, int offset) { - gl2.glUniformMatrix4fv(loc, count, transpose, mat, offset); + FloatBuffer mat) { + gl2.glUniformMatrix4fv(loc, count, transpose, mat); } @@ -1331,23 +1452,43 @@ public class PGL { } - public void vertexAttrib1fv(int loc, float[] v, int offset) { - gl2.glVertexAttrib1fv(loc, v, offset); +// public void vertexAttrib1fv(int loc, float[] v, int offset) { +// gl2.glVertexAttrib1fv(loc, v, offset); +// } + + + public void vertexAttrib1fv(int loc, FloatBuffer v) { + gl2.glVertexAttrib1fv(loc, v); } - public void vertexAttrib2fv(int loc, float[] v, int offset) { - gl2.glVertexAttrib2fv(loc, v, offset); +// public void vertexAttrib2fv(int loc, float[] v, int offset) { +// gl2.glVertexAttrib2fv(loc, v, offset); +// } + + + public void vertexAttrib2fv(int loc, FloatBuffer v) { + gl2.glVertexAttrib2fv(loc, v); } - public void vertexAttrib3fv(int loc, float[] v, int offset) { - gl2.glVertexAttrib3fv(loc, v, offset); +// public void vertexAttrib3fv(int loc, float[] v, int offset) { +// gl2.glVertexAttrib3fv(loc, v, offset); +// } + + + public void vertexAttrib3fv(int loc, FloatBuffer v) { + gl2.glVertexAttrib3fv(loc, v); } - public void vertexAttrib4fv(int loc, float[] v, int offset) { - gl2.glVertexAttrib4fv(loc, v, offset); +// public void vertexAttrib4fv(int loc, float[] v, int offset) { +// gl2.glVertexAttrib4fv(loc, v, offset); +// } + + + public void vertexAttri4fv(int loc, FloatBuffer v) { + gl2.glVertexAttrib4fv(loc, v); } @@ -1366,8 +1507,13 @@ public class PGL { } - public void getShaderiv(int shader, int pname, int[] params, int offset) { - gl2.glGetShaderiv(shader, pname, params, offset); +// public void getShaderiv(int shader, int pname, int[] params, int offset) { +// gl2.glGetShaderiv(shader, pname, params, offset); +// } + + + public void getShaderiv(int shader, int pname, IntBuffer params) { + gl2.glGetShaderiv(shader, pname, params); } @@ -1382,8 +1528,13 @@ public class PGL { } - public void getProgramiv(int prog, int pname, int[] params, int offset) { - gl2.glGetProgramiv(prog, pname, params, offset); +// public void getProgramiv(int prog, int pname, int[] params, int offset) { +// gl2.glGetProgramiv(prog, pname, params, offset); +// } + + + public void getProgramiv(int prog, int pname, IntBuffer params) { + gl2.glGetProgramiv(prog, pname, params); } @@ -1750,18 +1901,22 @@ public class PGL { texData = allocateDirectFloatBuffer(texCoords.length); } + if (byteBuffer == null) { + byteBuffer = allocateDirectByteBuffer(1); + } + if (0 < tex2DShaderProgram) { // The texture overwrites anything drawn earlier. - boolean[] depthTest = new boolean[1]; - getBooleanv(DEPTH_TEST, depthTest, 0); + getBooleanv(DEPTH_TEST, byteBuffer); + boolean depthTest = byteBuffer.get(0) == 0 ? false : true; disable(DEPTH_TEST); // When drawing the texture we don't write to the // depth mask, so the texture remains in the background // and can be occluded by anything drawn later, even if // if it is behind it. - boolean[] depthMask = new boolean[1]; - getBooleanv(DEPTH_WRITEMASK, depthMask, 0); + getBooleanv(DEPTH_WRITEMASK, byteBuffer); + boolean depthMask = byteBuffer.get(0) == 0 ? false : true; depthMask(false); useProgram(tex2DShaderProgram); @@ -1824,12 +1979,12 @@ public class PGL { useProgram(0); - if (depthTest[0]) { + if (depthTest) { enable(DEPTH_TEST); } else { disable(DEPTH_TEST); } - depthMask(depthMask[0]); + depthMask(depthMask); } } @@ -1857,18 +2012,22 @@ public class PGL { texData = allocateDirectFloatBuffer(texCoords.length); } + if (byteBuffer == null) { + byteBuffer = allocateDirectByteBuffer(1); + } + if (0 < texRectShaderProgram) { // The texture overwrites anything drawn earlier. - boolean[] depthTest = new boolean[1]; - getBooleanv(DEPTH_TEST, depthTest, 0); + getBooleanv(DEPTH_TEST, byteBuffer); + boolean depthTest = byteBuffer.get(0) == 0 ? false : true; disable(DEPTH_TEST); // When drawing the texture we don't write to the // depth mask, so the texture remains in the background // and can be occluded by anything drawn later, even if // if it is behind it. - boolean[] depthMask = new boolean[1]; - getBooleanv(DEPTH_WRITEMASK, depthMask, 0); + getBooleanv(DEPTH_WRITEMASK, byteBuffer); + boolean depthMask = byteBuffer.get(0) == 0 ? false : true; depthMask(false); useProgram(texRectShaderProgram); @@ -1931,12 +2090,12 @@ public class PGL { useProgram(0); - if (depthTest[0]) { + if (depthTest) { enable(DEPTH_TEST); } else { disable(DEPTH_TEST); } - depthMask(depthMask[0]); + depthMask(depthMask); } } @@ -2283,11 +2442,14 @@ public class PGL { protected int createShader(int shaderType, String source) { int shader = createShader(shaderType); if (shader != 0) { + if (intBuffer == null) { + intBuffer = allocateDirectIntBuffer(1); + } shaderSource(shader, source); compileShader(shader); - int[] compiled = new int[1]; - getShaderiv(shader, COMPILE_STATUS, compiled, 0); - if (compiled[0] == FALSE) { + getShaderiv(shader, COMPILE_STATUS, intBuffer); + boolean compiled = intBuffer.get(0) == 0 ? false : true; + if (!compiled) { System.err.println("Could not compile shader " + shaderType + ":"); System.err.println(getShaderInfoLog(shader)); deleteShader(shader); @@ -2301,12 +2463,15 @@ public class PGL { protected int createProgram(int vertexShader, int fragmentShader) { int program = createProgram(); if (program != 0) { + if (intBuffer == null) { + intBuffer = allocateDirectIntBuffer(1); + } attachShader(program, vertexShader); attachShader(program, fragmentShader); linkProgram(program); - int[] linked = new int[1]; - getProgramiv(program, LINK_STATUS, linked, 0); - if (linked[0] == FALSE) { + getProgramiv(program, LINK_STATUS, intBuffer); + boolean linked = intBuffer.get(0) == 0 ? false : true; + if (!linked) { System.err.println("Could not link program: "); System.err.println(getProgramInfoLog(program)); deleteProgram(program); @@ -2345,24 +2510,6 @@ public class PGL { } - protected static ByteBuffer allocateDirectByteBuffer(int size) { - return ByteBuffer.allocateDirect(size * SIZEOF_BYTE). - order(ByteOrder.nativeOrder()); - } - - - protected static IntBuffer allocateDirectIntBuffer(int size) { - return ByteBuffer.allocateDirect(size * SIZEOF_INT). - order(ByteOrder.nativeOrder()).asIntBuffer(); - } - - - protected static FloatBuffer allocateDirectFloatBuffer(int size) { - return ByteBuffer.allocateDirect(size * SIZEOF_FLOAT). - order(ByteOrder.nativeOrder()).asFloatBuffer(); - } - - protected int[] getGLVersion() { String version = gl.glGetString(GL.GL_VERSION).trim(); int[] res = {0, 0, 0}; @@ -2390,6 +2537,30 @@ public class PGL { } + protected static ByteBuffer allocateDirectByteBuffer(int size) { + return ByteBuffer.allocateDirect(size * SIZEOF_BYTE). + order(ByteOrder.nativeOrder()); + } + + + protected static ShortBuffer allocateDirectShortBuffer(int size) { + return ByteBuffer.allocateDirect(size * SIZEOF_SHORT). + order(ByteOrder.nativeOrder()).asShortBuffer(); + } + + + protected static IntBuffer allocateDirectIntBuffer(int size) { + return ByteBuffer.allocateDirect(size * SIZEOF_INT). + order(ByteOrder.nativeOrder()).asIntBuffer(); + } + + + protected static FloatBuffer allocateDirectFloatBuffer(int size) { + return ByteBuffer.allocateDirect(size * SIZEOF_FLOAT). + order(ByteOrder.nativeOrder()).asFloatBuffer(); + } + + /////////////////////////////////////////////////////////// // Java specific stuff diff --git a/core/src/processing/opengl/PGraphicsOpenGL.java b/core/src/processing/opengl/PGraphicsOpenGL.java index 8fab7b367..27730515f 100644 --- a/core/src/processing/opengl/PGraphicsOpenGL.java +++ b/core/src/processing/opengl/PGraphicsOpenGL.java @@ -475,7 +475,7 @@ public class PGraphicsOpenGL extends PGraphics { protected int pixelsOp = OP_NONE; /** Viewport dimensions. */ - protected int[] viewport = {0, 0, 0, 0}; + protected IntBuffer viewport; /** Used to register calls to glClear. */ protected boolean clearColorBuffer; @@ -509,6 +509,9 @@ public class PGraphicsOpenGL extends PGraphics { final protected float[][] QUAD_POINT_SIGNS = { {-1, +1}, {-1, -1}, {+1, -1}, {+1, +1} }; + /** To get data from OpenGL. */ + protected IntBuffer intBuffer; + protected FloatBuffer floatBuffer; ////////////////////////////////////////////////////////////// @@ -522,6 +525,12 @@ public class PGraphicsOpenGL extends PGraphics { tessellator = new Tessellator(); } + + intBuffer = PGL.allocateDirectIntBuffer(2); + floatBuffer = PGL.allocateDirectFloatBuffer(2); + + viewport = PGL.allocateDirectIntBuffer(4); + inGeo = newInGeometry(IMMEDIATE); tessGeo = newTessGeometry(IMMEDIATE); texCache = newTexCache(); @@ -703,9 +712,8 @@ public class PGraphicsOpenGL extends PGraphics { protected int createTextureObject(int context) { deleteFinalizedTextureObjects(); - int[] temp = new int[1]; - pgl.genTextures(1, temp, 0); - int id = temp[0]; + pgl.genTextures(1, intBuffer); + int id = intBuffer.get(0); GLResource res = new GLResource(id, context); @@ -721,16 +729,16 @@ public class PGraphicsOpenGL extends PGraphics { protected void deleteTextureObject(int id, int context) { GLResource res = new GLResource(id, context); if (glTextureObjects.containsKey(res)) { - int[] temp = { id }; - pgl.deleteTextures(1, temp, 0); + intBuffer.put(0, id); + pgl.deleteTextures(1, intBuffer); glTextureObjects.remove(res); } } protected void deleteAllTextureObjects() { for (GLResource res : glTextureObjects.keySet()) { - int[] temp = { res.id }; - pgl.deleteTextures(1, temp, 0); + intBuffer.put(0, res.id); + pgl.deleteTextures(1, intBuffer); } glTextureObjects.clear(); } @@ -749,8 +757,8 @@ public class PGraphicsOpenGL extends PGraphics { for (GLResource res : glTextureObjects.keySet()) { if (glTextureObjects.get(res)) { finalized.add(res); - int[] temp = { res.id }; - pgl.deleteTextures(1, temp, 0); + intBuffer.put(0, res.id); + pgl.deleteTextures(1, intBuffer); } } @@ -771,9 +779,8 @@ public class PGraphicsOpenGL extends PGraphics { protected int createVertexBufferObject(int context) { deleteFinalizedVertexBufferObjects(); - int[] temp = new int[1]; - pgl.genBuffers(1, temp, 0); - int id = temp[0]; + pgl.genBuffers(1, intBuffer); + int id = intBuffer.get(0); GLResource res = new GLResource(id, context); @@ -789,16 +796,16 @@ public class PGraphicsOpenGL extends PGraphics { protected void deleteVertexBufferObject(int id, int context) { GLResource res = new GLResource(id, context); if (glVertexBuffers.containsKey(res)) { - int[] temp = { id }; - pgl.deleteBuffers(1, temp, 0); + intBuffer.put(0, id); + pgl.deleteBuffers(1, intBuffer); glVertexBuffers.remove(res); } } protected void deleteAllVertexBufferObjects() { for (GLResource res : glVertexBuffers.keySet()) { - int[] temp = { res.id }; - pgl.deleteBuffers(1, temp, 0); + intBuffer.put(0, res.id); + pgl.deleteBuffers(1, intBuffer); } glVertexBuffers.clear(); } @@ -817,8 +824,8 @@ public class PGraphicsOpenGL extends PGraphics { for (GLResource res : glVertexBuffers.keySet()) { if (glVertexBuffers.get(res)) { finalized.add(res); - int[] temp = { res.id }; - pgl.deleteBuffers(1, temp, 0); + intBuffer.put(0, res.id); + pgl.deleteBuffers(1, intBuffer); } } @@ -839,9 +846,8 @@ public class PGraphicsOpenGL extends PGraphics { protected int createFrameBufferObject(int context) { deleteFinalizedFrameBufferObjects(); - int[] temp = new int[1]; - pgl.genFramebuffers(1, temp, 0); - int id = temp[0]; + pgl.genFramebuffers(1, intBuffer); + int id = intBuffer.get(0); GLResource res = new GLResource(id, context); @@ -857,16 +863,16 @@ public class PGraphicsOpenGL extends PGraphics { protected void deleteFrameBufferObject(int id, int context) { GLResource res = new GLResource(id, context); if (glFrameBuffers.containsKey(res)) { - int[] temp = { id }; - pgl.deleteFramebuffers(1, temp, 0); + intBuffer.put(0, id); + pgl.deleteFramebuffers(1, intBuffer); glFrameBuffers.remove(res); } } protected void deleteAllFrameBufferObjects() { for (GLResource res : glFrameBuffers.keySet()) { - int[] temp = { res.id }; - pgl.deleteFramebuffers(1, temp, 0); + intBuffer.put(0, res.id); + pgl.deleteFramebuffers(1, intBuffer); } glFrameBuffers.clear(); } @@ -885,8 +891,8 @@ public class PGraphicsOpenGL extends PGraphics { for (GLResource res : glFrameBuffers.keySet()) { if (glFrameBuffers.get(res)) { finalized.add(res); - int[] temp = { res.id }; - pgl.deleteFramebuffers(1, temp, 0); + intBuffer.put(0, res.id); + pgl.deleteFramebuffers(1, intBuffer); } } @@ -907,9 +913,8 @@ public class PGraphicsOpenGL extends PGraphics { protected int createRenderBufferObject(int context) { deleteFinalizedRenderBufferObjects(); - int[] temp = new int[1]; - pgl.genRenderbuffers(1, temp, 0); - int id = temp[0]; + pgl.genRenderbuffers(1, intBuffer); + int id = intBuffer.get(0); GLResource res = new GLResource(id, context); @@ -925,16 +930,16 @@ public class PGraphicsOpenGL extends PGraphics { protected void deleteRenderBufferObject(int id, int context) { GLResource res = new GLResource(id, context); if (glRenderBuffers.containsKey(res)) { - int[] temp = { id }; - pgl.deleteRenderbuffers(1, temp, 0); + intBuffer.put(0, id); + pgl.deleteRenderbuffers(1, intBuffer); glRenderBuffers.remove(res); } } protected void deleteAllRenderBufferObjects() { for (GLResource res : glRenderBuffers.keySet()) { - int[] temp = { res.id }; - pgl.deleteRenderbuffers(1, temp, 0); + intBuffer.put(0, res.id); + pgl.deleteRenderbuffers(1, intBuffer); } glRenderBuffers.clear(); } @@ -953,8 +958,8 @@ public class PGraphicsOpenGL extends PGraphics { for (GLResource res : glRenderBuffers.keySet()) { if (glRenderBuffers.get(res)) { finalized.add(res); - int[] temp = { res.id }; - pgl.deleteRenderbuffers(1, temp, 0); + intBuffer.put(0, res.id); + pgl.deleteRenderbuffers(1, intBuffer); } } @@ -1279,9 +1284,10 @@ public class PGraphicsOpenGL extends PGraphics { int sizef = size * PGL.SIZEOF_FLOAT; int sizei = size * PGL.SIZEOF_INT; + tessGeo.readyPolyVertices(); pgl.bindBuffer(PGL.ARRAY_BUFFER, glPolyVertex); - pgl.bufferData(PGL.ARRAY_BUFFER, 4 * sizef, - FloatBuffer.wrap(tessGeo.polyVertices, 0, 4 * size), PGL.STATIC_DRAW); + pgl.bufferData(PGL.ARRAY_BUFFER, 4 * sizef, tessGeo.polyVertices, + PGL.STATIC_DRAW); pgl.bindBuffer(PGL.ARRAY_BUFFER, glPolyColor); pgl.bufferData(PGL.ARRAY_BUFFER, sizei, @@ -1315,10 +1321,10 @@ public class PGraphicsOpenGL extends PGraphics { FloatBuffer.wrap(tessGeo.polyTexcoords, 0, 2 * size), PGL.STATIC_DRAW); } + tessGeo.readyPolyIndices(); pgl.bindBuffer(PGL.ELEMENT_ARRAY_BUFFER, glPolyIndex); pgl.bufferData(PGL.ELEMENT_ARRAY_BUFFER, - tessGeo.polyIndexCount * PGL.SIZEOF_INDEX, - ShortBuffer.wrap(tessGeo.polyIndices, 0, tessGeo.polyIndexCount), + tessGeo.polyIndexCount * PGL.SIZEOF_INDEX, tessGeo.polyIndices, PGL.STATIC_DRAW); } @@ -1687,7 +1693,8 @@ public class PGraphicsOpenGL extends PGraphics { pgl.disable(PGL.POLYGON_SMOOTH); } - pgl.viewport(viewport[0], viewport[1], viewport[2], viewport[3]); + pgl.viewport(viewport.get(0), viewport.get(1), + viewport.get(2), viewport.get(3)); if (clip) { pgl.enable(PGL.SCISSOR_TEST); pgl.scissor(clipRect[0], clipRect[1], clipRect[2], clipRect[3]); @@ -2382,10 +2389,10 @@ public class PGraphicsOpenGL extends PGraphics { raw.noStroke(); raw.beginShape(TRIANGLES); - float[] vertices = tessGeo.polyVertices; + FloatBuffer vertices = tessGeo.polyVertices; int[] color = tessGeo.polyColors; float[] uv = tessGeo.polyTexcoords; - short[] indices = tessGeo.polyIndices; + ShortBuffer indices = tessGeo.polyIndices; for (int i = 0; i < texCache.size; i++) { PImage textureImage = texCache.getTextureImage(i); @@ -2402,9 +2409,9 @@ public class PGraphicsOpenGL extends PGraphics { int voffset = cache.vertexOffset[n]; for (int tr = ioffset / 3; tr < (ioffset + icount) / 3; tr++) { - int i0 = voffset + indices[3 * tr + 0]; - int i1 = voffset + indices[3 * tr + 1]; - int i2 = voffset + indices[3 * tr + 2]; + int i0 = voffset + indices.get(3 * tr + 0); + int i1 = voffset + indices.get(3 * tr + 1); + int i2 = voffset + indices.get(3 * tr + 2); float[] pt0 = {0, 0, 0, 0}; float[] pt1 = {0, 0, 0, 0}; @@ -2417,16 +2424,16 @@ public class PGraphicsOpenGL extends PGraphics { float[] src0 = {0, 0, 0, 0}; float[] src1 = {0, 0, 0, 0}; float[] src2 = {0, 0, 0, 0}; - PApplet.arrayCopy(vertices, 4 * i0, src0, 0, 4); - PApplet.arrayCopy(vertices, 4 * i1, src1, 0, 4); - PApplet.arrayCopy(vertices, 4 * i2, src2, 0, 4); + vertices.position(4 * i0); vertices.get(src0, 0, 4); + vertices.position(4 * i1); vertices.get(src1, 0, 4); + vertices.position(4 * i2); vertices.get(src2, 0, 4); modelview.mult(src0, pt0); modelview.mult(src1, pt1); modelview.mult(src2, pt2); } else { - PApplet.arrayCopy(vertices, 4 * i0, pt0, 0, 4); - PApplet.arrayCopy(vertices, 4 * i1, pt1, 0, 4); - PApplet.arrayCopy(vertices, 4 * i2, pt2, 0, 4); + vertices.position(4 * i0); vertices.get(pt0, 0, 4); + vertices.position(4 * i1); vertices.get(pt1, 0, 4); + vertices.position(4 * i2); vertices.get(pt2, 0, 4); } if (textureImage != null) { @@ -5917,10 +5924,10 @@ public class PGraphicsOpenGL extends PGraphics { } if (primarySurface) { - int[] temp = new int[1]; - pgl.getIntegerv(PGL.SAMPLES, temp, 0); - if (quality != temp[0] && 1 < temp[0] && 1 < quality) { - quality = temp[0]; + pgl.getIntegerv(PGL.SAMPLES, intBuffer); + int temp = intBuffer.get(0); + if (quality != temp && 1 < temp && 1 < quality) { + quality = temp; } } if (quality < 2) { @@ -5933,8 +5940,10 @@ public class PGraphicsOpenGL extends PGraphics { pgl.disable(PGL.POLYGON_SMOOTH); // setup opengl viewport. - viewport[0] = 0; viewport[1] = 0; viewport[2] = width; viewport[3] = height; - pgl.viewport(viewport[0], viewport[1], viewport[2], viewport[3]); + viewport.put(0, 0); viewport.put(1, 0); + viewport.put(2, width); viewport.put(3, height); + pgl.viewport(viewport.get(0), viewport.get(1), + viewport.get(2), viewport.get(3)); if (sized) { // To avoid having garbage in the screen after a resize, @@ -6044,27 +6053,24 @@ public class PGraphicsOpenGL extends PGraphics { blendEqSupported = false; } - int temp[] = new int[2]; - depthBits = pgl.getDepthBits(); stencilBits = pgl.getStencilBits(); - pgl.getIntegerv(PGL.MAX_TEXTURE_SIZE, temp, 0); - maxTextureSize = temp[0]; + pgl.getIntegerv(PGL.MAX_TEXTURE_SIZE, intBuffer); + maxTextureSize = intBuffer.get(0); - pgl.getIntegerv(PGL.MAX_SAMPLES, temp, 0); - maxSamples = temp[0]; + pgl.getIntegerv(PGL.MAX_SAMPLES, intBuffer); + maxSamples = intBuffer.get(0); - pgl.getIntegerv(PGL.ALIASED_LINE_WIDTH_RANGE, temp, 0); - maxLineWidth = temp[1]; + pgl.getIntegerv(PGL.ALIASED_LINE_WIDTH_RANGE, intBuffer); + maxLineWidth = intBuffer.get(0); - pgl.getIntegerv(PGL.ALIASED_POINT_SIZE_RANGE, temp, 0); - maxPointSize = temp[1]; + pgl.getIntegerv(PGL.ALIASED_POINT_SIZE_RANGE, intBuffer); + maxPointSize = intBuffer.get(0); if (anisoSamplingSupported) { - float ftemp[] = new float[1]; - pgl.getFloatv(PGL.MAX_TEXTURE_MAX_ANISOTROPY, ftemp, 0); - maxAnisoAmount = ftemp[0]; + pgl.getFloatv(PGL.MAX_TEXTURE_MAX_ANISOTROPY, floatBuffer); + maxAnisoAmount = floatBuffer.get(0); } glParamsRead = true; @@ -6481,10 +6487,10 @@ public class PGraphicsOpenGL extends PGraphics { } if (-1 < viewportLoc) { - float x = pgCurrent.viewport[0]; - float y = pgCurrent.viewport[1]; - float w = pgCurrent.viewport[2]; - float h = pgCurrent.viewport[3]; + float x = pgCurrent.viewport.get(0); + float y = pgCurrent.viewport.get(1); + float w = pgCurrent.viewport.get(2); + float h = pgCurrent.viewport.get(3); setUniformValue(viewportLoc, x, y, w, h); } @@ -8876,7 +8882,7 @@ public class PGraphicsOpenGL extends PGraphics { int polyVertexCount; int firstPolyVertex; int lastPolyVertex; - float[] polyVertices; + FloatBuffer polyVertices; int[] polyColors; float[] polyNormals; float[] polyTexcoords; @@ -8891,7 +8897,7 @@ public class PGraphicsOpenGL extends PGraphics { int polyIndexCount; int firstPolyIndex; int lastPolyIndex; - short[] polyIndices; + ShortBuffer polyIndices; IndexCache polyIndexCache = new IndexCache(); // Tessellated line data @@ -8932,7 +8938,7 @@ public class PGraphicsOpenGL extends PGraphics { // Allocate/dispose void allocate() { - polyVertices = new float[4 * PGL.DEFAULT_TESS_VERTICES]; + polyVertices = PGL.allocateDirectFloatBuffer(4 * PGL.DEFAULT_TESS_VERTICES); polyColors = new int[PGL.DEFAULT_TESS_VERTICES]; polyNormals = new float[3 * PGL.DEFAULT_TESS_VERTICES]; polyTexcoords = new float[2 * PGL.DEFAULT_TESS_VERTICES]; @@ -8940,7 +8946,7 @@ public class PGraphicsOpenGL extends PGraphics { polySpecular = new int[PGL.DEFAULT_TESS_VERTICES]; polyEmissive = new int[PGL.DEFAULT_TESS_VERTICES]; polyShininess = new float[PGL.DEFAULT_TESS_VERTICES]; - polyIndices = new short[PGL.DEFAULT_TESS_VERTICES]; + polyIndices = PGL.allocateDirectShortBuffer(PGL.DEFAULT_TESS_VERTICES); lineVertices = new float[4 * PGL.DEFAULT_TESS_VERTICES]; lineColors = new int[PGL.DEFAULT_TESS_VERTICES]; @@ -8993,9 +8999,8 @@ public class PGraphicsOpenGL extends PGraphics { } void polyVertexCheck() { - if (polyVertexCount == polyVertices.length / 4) { + if (polyVertexCount == polyVertices.capacity() / 4) { int newSize = polyVertexCount << 1; - expandPolyVertices(newSize); expandPolyColors(newSize); expandPolyNormals(newSize); @@ -9012,10 +9017,9 @@ public class PGraphicsOpenGL extends PGraphics { } void polyVertexCheck(int count) { - int oldSize = polyVertices.length / 4; + int oldSize = polyVertices.capacity() / 4; if (polyVertexCount + count > oldSize) { int newSize = expandArraySize(oldSize, polyVertexCount + count); - expandPolyVertices(newSize); expandPolyColors(newSize); expandPolyNormals(newSize); @@ -9032,10 +9036,9 @@ public class PGraphicsOpenGL extends PGraphics { } void polyIndexCheck(int count) { - int oldSize = polyIndices.length; + int oldSize = polyIndices.capacity(); if (polyIndexCount + count > oldSize) { int newSize = expandArraySize(oldSize, polyIndexCount + count); - expandPolyIndices(newSize); } @@ -9045,7 +9048,7 @@ public class PGraphicsOpenGL extends PGraphics { } void polyIndexCheck() { - if (polyIndexCount == polyIndices.length) { + if (polyIndexCount == polyIndices.capacity()) { int newSize = polyIndexCount << 1; expandPolyIndices(newSize); } @@ -9124,9 +9127,9 @@ public class PGraphicsOpenGL extends PGraphics { void getPolyVertexMin(PVector v, int first, int last) { for (int i = first; i <= last; i++) { int index = 4 * i; - v.x = PApplet.min(v.x, polyVertices[index++]); - v.y = PApplet.min(v.y, polyVertices[index++]); - v.z = PApplet.min(v.z, polyVertices[index ]); + v.x = PApplet.min(v.x, polyVertices.get(index++)); + v.y = PApplet.min(v.y, polyVertices.get(index++)); + v.z = PApplet.min(v.z, polyVertices.get(index )); } } @@ -9151,9 +9154,9 @@ public class PGraphicsOpenGL extends PGraphics { void getPolyVertexMax(PVector v, int first, int last) { for (int i = first; i <= last; i++) { int index = 4 * i; - v.x = PApplet.max(v.x, polyVertices[index++]); - v.y = PApplet.max(v.y, polyVertices[index++]); - v.z = PApplet.max(v.z, polyVertices[index ]); + v.x = PApplet.max(v.x, polyVertices.get(index++)); + v.y = PApplet.max(v.y, polyVertices.get(index++)); + v.z = PApplet.max(v.z, polyVertices.get(index )); } } @@ -9178,9 +9181,9 @@ public class PGraphicsOpenGL extends PGraphics { int getPolyVertexSum(PVector v, int first, int last) { for (int i = first; i <= last; i++) { int index = 4 * i; - v.x += polyVertices[index++]; - v.y += polyVertices[index++]; - v.z += polyVertices[index ]; + v.x += polyVertices.get(index++); + v.y += polyVertices.get(index++); + v.z += polyVertices.get(index ); } return last - first + 1; } @@ -9210,8 +9213,9 @@ public class PGraphicsOpenGL extends PGraphics { // Expand arrays void expandPolyVertices(int n) { - float temp[] = new float[4 * n]; - PApplet.arrayCopy(polyVertices, 0, temp, 0, 4 * polyVertexCount); + readyPolyVertices(); + FloatBuffer temp = PGL.allocateDirectFloatBuffer(4 * n); + temp.put(polyVertices); polyVertices = temp; } @@ -9258,8 +9262,9 @@ public class PGraphicsOpenGL extends PGraphics { } void expandPolyIndices(int n) { - short temp[] = new short[n]; - PApplet.arrayCopy(polyIndices, 0, temp, 0, polyIndexCount); + readyPolyIndices(); + ShortBuffer temp = PGL.allocateDirectShortBuffer(n); + temp.put(polyIndices); polyIndices = temp; } @@ -9311,12 +9316,40 @@ public class PGraphicsOpenGL extends PGraphics { pointIndices = temp; } + // ----------------------------------------------------------------- + // + // Copy handling + + + protected void readyPolyVertices() { + polyVertices.position(0); + polyVertices.limit(polyVertices.capacity()); + } + + protected void readyPolyVertices(int count) { + polyVertices.position(0); + polyVertices.limit(count); + } + + + protected void readyPolyIndices() { + polyIndices.position(0); + polyIndices.limit(polyIndices.capacity()); + } + + + protected void readyPolyIndices(int count) { + polyIndices.position(0); + polyIndices.limit(count); + } + + // ----------------------------------------------------------------- // // Trim arrays void trim() { - if (0 < polyVertexCount && polyVertexCount < polyVertices.length / 4) { + if (0 < polyVertexCount && polyVertexCount < polyVertices.capacity() / 4) { trimPolyVertices(); trimPolyColors(); trimPolyNormals(); @@ -9327,7 +9360,7 @@ public class PGraphicsOpenGL extends PGraphics { trimPolyShininess(); } - if (0 < polyIndexCount && polyIndexCount < polyIndices.length) { + if (0 < polyIndexCount && polyIndexCount < polyIndices.capacity()) { trimPolyIndices(); } @@ -9353,8 +9386,9 @@ public class PGraphicsOpenGL extends PGraphics { } void trimPolyVertices() { - float temp[] = new float[4 * polyVertexCount]; - PApplet.arrayCopy(polyVertices, 0, temp, 0, 4 * polyVertexCount); + readyPolyVertices(4 * polyVertexCount); + FloatBuffer temp = PGL.allocateDirectFloatBuffer(4 * polyVertexCount); + temp.put(polyVertices); polyVertices = temp; } @@ -9401,8 +9435,9 @@ public class PGraphicsOpenGL extends PGraphics { } void trimPolyIndices() { - short temp[] = new short[polyIndexCount]; - PApplet.arrayCopy(polyIndices, 0, temp, 0, polyIndexCount); + readyPolyIndices(polyIndexCount); + ShortBuffer temp = PGL.allocateDirectShortBuffer(polyIndexCount); + temp.put(polyIndices); polyIndices = temp; } @@ -9460,7 +9495,7 @@ public class PGraphicsOpenGL extends PGraphics { void incPolyIndices(int first, int last, int inc) { for (int i = first; i <= last; i++) { - polyIndices[i] += inc; + polyIndices.put(i, (short)(polyIndices.get(i) + inc)); } } @@ -9484,19 +9519,19 @@ public class PGraphicsOpenGL extends PGraphics { int index; index = 4 * i0; - float x0 = polyVertices[index++]; - float y0 = polyVertices[index++]; - float z0 = polyVertices[index ]; + float x0 = polyVertices.get(index++); + float y0 = polyVertices.get(index++); + float z0 = polyVertices.get(index ); index = 4 * i1; - float x1 = polyVertices[index++]; - float y1 = polyVertices[index++]; - float z1 = polyVertices[index ]; + float x1 = polyVertices.get(index++); + float y1 = polyVertices.get(index++); + float z1 = polyVertices.get(index ); index = 4 * i2; - float x2 = polyVertices[index++]; - float y2 = polyVertices[index++]; - float z2 = polyVertices[index ]; + float x2 = polyVertices.get(index++); + float y2 = polyVertices.get(index++); + float z2 = polyVertices.get(index ); float v12x = x2 - x1; float v12y = y2 - y1; @@ -9669,10 +9704,10 @@ public class PGraphicsOpenGL extends PGraphics { PMatrix3D nm = modelviewInv; index = 4 * tessIdx; - polyVertices[index++] = x*mm.m00 + y*mm.m01 + z*mm.m02 + mm.m03; - polyVertices[index++] = x*mm.m10 + y*mm.m11 + z*mm.m12 + mm.m13; - polyVertices[index++] = x*mm.m20 + y*mm.m21 + z*mm.m22 + mm.m23; - polyVertices[index ] = x*mm.m30 + y*mm.m31 + z*mm.m32 + mm.m33; + polyVertices.put(index++, x*mm.m00 + y*mm.m01 + z*mm.m02 + mm.m03); + polyVertices.put(index++, x*mm.m10 + y*mm.m11 + z*mm.m12 + mm.m13); + polyVertices.put(index++, x*mm.m20 + y*mm.m21 + z*mm.m22 + mm.m23); + polyVertices.put(index , x*mm.m30 + y*mm.m31 + z*mm.m32 + mm.m33); index = 3 * tessIdx; polyNormals[index++] = nx*nm.m00 + ny*nm.m10 + nz*nm.m20; @@ -9680,10 +9715,10 @@ public class PGraphicsOpenGL extends PGraphics { polyNormals[index ] = nx*nm.m02 + ny*nm.m12 + nz*nm.m22; } else { index = 4 * tessIdx; - polyVertices[index++] = x; - polyVertices[index++] = y; - polyVertices[index++] = z; - polyVertices[index ] = 1; + polyVertices.put(index++, x); + polyVertices.put(index++, y); + polyVertices.put(index++, z); + polyVertices.put(index , 1); index = 3 * tessIdx; polyNormals[index++] = nx; @@ -9717,10 +9752,10 @@ public class PGraphicsOpenGL extends PGraphics { PMatrix3D nm = modelviewInv; index = 4 * count; - polyVertices[index++] = x*mm.m00 + y*mm.m01 + z*mm.m02 + mm.m03; - polyVertices[index++] = x*mm.m10 + y*mm.m11 + z*mm.m12 + mm.m13; - polyVertices[index++] = x*mm.m20 + y*mm.m21 + z*mm.m22 + mm.m23; - polyVertices[index ] = x*mm.m30 + y*mm.m31 + z*mm.m32 + mm.m33; + polyVertices.put(index++, x*mm.m00 + y*mm.m01 + z*mm.m02 + mm.m03); + polyVertices.put(index++, x*mm.m10 + y*mm.m11 + z*mm.m12 + mm.m13); + polyVertices.put(index++, x*mm.m20 + y*mm.m21 + z*mm.m22 + mm.m23); + polyVertices.put(index , x*mm.m30 + y*mm.m31 + z*mm.m32 + mm.m33); index = 3 * count; polyNormals[index++] = nx*nm.m00 + ny*nm.m10 + nz*nm.m20; @@ -9728,10 +9763,10 @@ public class PGraphicsOpenGL extends PGraphics { polyNormals[index ] = nx*nm.m02 + ny*nm.m12 + nz*nm.m22; } else { index = 4 * count; - polyVertices[index++] = x; - polyVertices[index++] = y; - polyVertices[index++] = z; - polyVertices[index ] = 1; + polyVertices.put(index++, x); + polyVertices.put(index++, y); + polyVertices.put(index++, z); + polyVertices.put(index , 1); index = 3 * count; polyNormals[index++] = nx; @@ -9784,10 +9819,10 @@ public class PGraphicsOpenGL extends PGraphics { float nz = in.normals[index ]; index = 4 * tessIdx; - polyVertices[index++] = x*mm.m00 + y*mm.m01 + z*mm.m02 + mm.m03; - polyVertices[index++] = x*mm.m10 + y*mm.m11 + z*mm.m12 + mm.m13; - polyVertices[index++] = x*mm.m20 + y*mm.m21 + z*mm.m22 + mm.m23; - polyVertices[index ] = x*mm.m30 + y*mm.m31 + z*mm.m32 + mm.m33; + polyVertices.put(index++, x*mm.m00 + y*mm.m01 + z*mm.m02 + mm.m03); + polyVertices.put(index++, x*mm.m10 + y*mm.m11 + z*mm.m12 + mm.m13); + polyVertices.put(index++, x*mm.m20 + y*mm.m21 + z*mm.m22 + mm.m23); + polyVertices.put(index , x*mm.m30 + y*mm.m31 + z*mm.m32 + mm.m33); index = 3 * tessIdx; polyNormals[index++] = nx*nm.m00 + ny*nm.m10 + nz*nm.m20; @@ -9813,10 +9848,10 @@ public class PGraphicsOpenGL extends PGraphics { float nz = in.normals[index ]; index = 4 * tessIdx; - polyVertices[index++] = x; - polyVertices[index++] = y; - polyVertices[index++] = z; - polyVertices[index ] = 1; + polyVertices.put(index++, x); + polyVertices.put(index++, y); + polyVertices.put(index++, z); + polyVertices.put(index , 1); index = 3 * tessIdx; polyNormals[index++] = nx; @@ -9827,9 +9862,9 @@ public class PGraphicsOpenGL extends PGraphics { for (int i = 0; i < nvert; i++) { int inIdx = i0 + i; int tessIdx = firstPolyVertex + i; - PApplet.arrayCopy(in.vertices, 3 * inIdx, - polyVertices, 4 * tessIdx, 3); - polyVertices[4 * tessIdx + 3] = 1; + polyVertices.position(4 * tessIdx); + polyVertices.put(in.vertices, 3 * inIdx, 3); + polyVertices.put(4 * tessIdx + 3, 1); } PApplet.arrayCopy(in.normals, 3 * i0, polyNormals, 3 * firstPolyVertex, 3 * nvert); @@ -9906,16 +9941,16 @@ public class PGraphicsOpenGL extends PGraphics { for (int i = first; i <= last; i++) { index = 4 * i; - float x = polyVertices[index++]; - float y = polyVertices[index ]; + float x = polyVertices.get(index++); + float y = polyVertices.get(index ); index = 3 * i; float nx = polyNormals[index++]; float ny = polyNormals[index ]; index = 4 * i; - polyVertices[index++] = x*tr.m00 + y*tr.m01 + tr.m02; - polyVertices[index ] = x*tr.m10 + y*tr.m11 + tr.m12; + polyVertices.put(index++, x*tr.m00 + y*tr.m01 + tr.m02); + polyVertices.put(index , x*tr.m10 + y*tr.m11 + tr.m12); index = 3 * i; polyNormals[index++] = nx*tr.m00 + ny*tr.m01; @@ -9970,10 +10005,10 @@ public class PGraphicsOpenGL extends PGraphics { for (int i = first; i <= last; i++) { index = 4 * i; - float x = polyVertices[index++]; - float y = polyVertices[index++]; - float z = polyVertices[index++]; - float w = polyVertices[index ]; + float x = polyVertices.get(index++); + float y = polyVertices.get(index++); + float z = polyVertices.get(index++); + float w = polyVertices.get(index ); index = 3 * i; float nx = polyNormals[index++]; @@ -9981,10 +10016,10 @@ public class PGraphicsOpenGL extends PGraphics { float nz = polyNormals[index ]; index = 4 * i; - polyVertices[index++] = x*tr.m00 + y*tr.m01 + z*tr.m02 + w*tr.m03; - polyVertices[index++] = x*tr.m10 + y*tr.m11 + z*tr.m12 + w*tr.m13; - polyVertices[index++] = x*tr.m20 + y*tr.m21 + z*tr.m22 + w*tr.m23; - polyVertices[index ] = x*tr.m30 + y*tr.m31 + z*tr.m32 + w*tr.m33; + polyVertices.put(index++, x*tr.m00 + y*tr.m01 + z*tr.m02 + w*tr.m03); + polyVertices.put(index++, x*tr.m10 + y*tr.m11 + z*tr.m12 + w*tr.m13); + polyVertices.put(index++, x*tr.m20 + y*tr.m21 + z*tr.m22 + w*tr.m23); + polyVertices.put(index , x*tr.m30 + y*tr.m31 + z*tr.m32 + w*tr.m33); index = 3 * i; polyNormals[index++] = nx*tr.m00 + ny*tr.m01 + nz*tr.m02; @@ -10291,14 +10326,14 @@ public class PGraphicsOpenGL extends PGraphics { // Adding vert0 to take into account the triangles of all // the preceding points. for (int k = 1; k < nPtVert - 1; k++) { - tess.polyIndices[indIdx++] = (short) (count + 0); - tess.polyIndices[indIdx++] = (short) (count + k); - tess.polyIndices[indIdx++] = (short) (count + k + 1); + tess.polyIndices.put(indIdx++, (short) (count + 0)); + tess.polyIndices.put(indIdx++, (short) (count + k)); + tess.polyIndices.put(indIdx++, (short) (count + k + 1)); } // Final triangle between the last and first point: - tess.polyIndices[indIdx++] = (short) (count + 0); - tess.polyIndices[indIdx++] = (short) (count + 1); - tess.polyIndices[indIdx++] = (short) (count + nPtVert - 1); + tess.polyIndices.put(indIdx++, (short) (count + 0)); + tess.polyIndices.put(indIdx++, (short) (count + 1)); + tess.polyIndices.put(indIdx++, (short) (count + nPtVert - 1)); cache.incCounts(index, 3 * (nPtVert - 1), nPtVert); } @@ -10414,14 +10449,14 @@ public class PGraphicsOpenGL extends PGraphics { } for (int k = 1; k < nvert - 1; k++) { - tess.polyIndices[indIdx++] = (short) (count + 0); - tess.polyIndices[indIdx++] = (short) (count + k); - tess.polyIndices[indIdx++] = (short) (count + k + 1); + tess.polyIndices.put(indIdx++, (short) (count + 0)); + tess.polyIndices.put(indIdx++, (short) (count + k)); + tess.polyIndices.put(indIdx++, (short) (count + k + 1)); } // Final triangle between the last and first point: - tess.polyIndices[indIdx++] = (short) (count + 0); - tess.polyIndices[indIdx++] = (short) (count + 1); - tess.polyIndices[indIdx++] = (short) (count + nvert - 1); + tess.polyIndices.put(indIdx++, (short) (count + 0)); + tess.polyIndices.put(indIdx++, (short) (count + 1)); + tess.polyIndices.put(indIdx++, (short) (count + nvert - 1)); cache.incCounts(index, 12, 5); } @@ -10848,11 +10883,11 @@ public class PGraphicsOpenGL extends PGraphics { tess.setPolyVertex(vidx++, x0 + normx * weight/2, y0 + normy * weight/2, 0, color); - tess.polyIndices[iidx++] = (short) (count + 0); + tess.polyIndices.put(iidx++, (short) (count + 0)); tess.setPolyVertex(vidx++, x0 - normx * weight/2, y0 - normy * weight/2, 0, color); - tess.polyIndices[iidx++] = (short) (count + 1); + tess.polyIndices.put(iidx++, (short) (count + 1)); if (!constStroke) { color = in.strokeColors[i1]; @@ -10861,15 +10896,15 @@ public class PGraphicsOpenGL extends PGraphics { tess.setPolyVertex(vidx++, x1 - normx * weight/2, y1 - normy * weight/2, 0, color); - tess.polyIndices[iidx++] = (short) (count + 2); + tess.polyIndices.put(iidx++, (short) (count + 2)); // Starting a new triangle re-using prev vertices. - tess.polyIndices[iidx++] = (short) (count + 2); - tess.polyIndices[iidx++] = (short) (count + 0); + tess.polyIndices.put(iidx++, (short) (count + 2)); + tess.polyIndices.put(iidx++, (short) (count + 0)); tess.setPolyVertex(vidx++, x1 + normx * weight/2, y1 + normy * weight/2, 0, color); - tess.polyIndices[iidx++] = (short) (count + 3); + tess.polyIndices.put(iidx++, (short) (count + 3)); cache.incCounts(index, 6, 4); return index; @@ -11108,9 +11143,9 @@ public class PGraphicsOpenGL extends PGraphics { ri2 = ii2; } else ri2 = count + ii2; - tess.polyIndices[offset + 3 * tr + 0] = (short) ri0; - tess.polyIndices[offset + 3 * tr + 1] = (short) ri1; - tess.polyIndices[offset + 3 * tr + 2] = (short) ri2; + tess.polyIndices.put(offset + 3 * tr + 0, (short) ri0); + tess.polyIndices.put(offset + 3 * tr + 1, (short) ri1); + tess.polyIndices.put(offset + 3 * tr + 2, (short) ri2); inInd1 = 3 * tr + 2; inMaxVert1 = PApplet.max(inMaxVert1, PApplet.max(i0, i1, i2)); @@ -11130,10 +11165,10 @@ public class PGraphicsOpenGL extends PGraphics { // Adjusting the negative indices so they correspond to vertices // added at the end of the block. for (int i = inInd0; i <= inInd1; i++) { - int ri = tess.polyIndices[offset + i]; + int ri = tess.polyIndices.get(offset + i); if (ri < 0) { - tess.polyIndices[offset + i] = - (short) (inMaxVertRel + 1 + dupIndexPos(ri)); + tess.polyIndices.put(offset + i, + (short) (inMaxVertRel + 1 + dupIndexPos(ri))); } } @@ -11538,8 +11573,8 @@ public class PGraphicsOpenGL extends PGraphics { protected void addIndex(int tessIdx) { tess.polyIndexCheck(); - tess.polyIndices[tess.polyIndexCount - 1] = - (short) (vertFirst + tessIdx); + tess.polyIndices.put(tess.polyIndexCount - 1, + (short) (vertFirst + tessIdx)); } protected void calcTriNormal(int tessIdx0, int tessIdx1, int tessIdx2) { diff --git a/core/src/processing/opengl/PShader.java b/core/src/processing/opengl/PShader.java index 15c263cfb..acba492c4 100644 --- a/core/src/processing/opengl/PShader.java +++ b/core/src/processing/opengl/PShader.java @@ -27,6 +27,8 @@ import processing.core.*; import java.io.IOException; import java.net.URL; +import java.nio.FloatBuffer; +import java.nio.IntBuffer; import java.util.HashMap; /** @@ -79,6 +81,9 @@ public class PShader { protected int firstTexUnit; protected int lastTexUnit; + // Direct buffers to pass shader dat to GL + protected IntBuffer intBuffer; + protected FloatBuffer floatBuffer; public PShader() { parent = null; @@ -97,6 +102,9 @@ public class PShader { firstTexUnit = 0; + intBuffer = PGL.allocateDirectIntBuffer(1); + floatBuffer = PGL.allocateDirectFloatBuffer(1); + bound = false; } @@ -132,6 +140,9 @@ public class PShader { glProgram = 0; glVertex = 0; glFragment = 0; + + intBuffer = PGL.allocateDirectIntBuffer(1); + floatBuffer = PGL.allocateDirectFloatBuffer(1); } /** @@ -151,6 +162,9 @@ public class PShader { glProgram = 0; glVertex = 0; glFragment = 0; + + intBuffer = PGL.allocateDirectIntBuffer(1); + floatBuffer = PGL.allocateDirectFloatBuffer(1); } @@ -460,14 +474,15 @@ public class PShader { protected void setUniformVector(int loc, int[] vec, int ncoords, int length) { if (-1 < loc) { + copyToIntBuffer(vec); if (ncoords == 1) { - pgl.uniform1iv(loc, length, vec, 0); + pgl.uniform1iv(loc, length, intBuffer); } else if (ncoords == 2) { - pgl.uniform2iv(loc, length, vec, 0); + pgl.uniform2iv(loc, length, intBuffer); } else if (ncoords == 3) { - pgl.uniform3iv(loc, length, vec, 0); + pgl.uniform3iv(loc, length, intBuffer); } else if (ncoords == 4) { - pgl.uniform3iv(loc, length, vec, 0); + pgl.uniform3iv(loc, length, intBuffer); } } } @@ -476,14 +491,15 @@ public class PShader { protected void setUniformVector(int loc, float[] vec, int ncoords, int length) { if (-1 < loc) { + copyToFloatBuffer(vec); if (ncoords == 1) { - pgl.uniform1fv(loc, length, vec, 0); + pgl.uniform1fv(loc, length, floatBuffer); } else if (ncoords == 2) { - pgl.uniform2fv(loc, length, vec, 0); + pgl.uniform2fv(loc, length, floatBuffer); } else if (ncoords == 3) { - pgl.uniform3fv(loc, length, vec, 0); + pgl.uniform3fv(loc, length, floatBuffer); } else if (ncoords == 4) { - pgl.uniform4fv(loc, length, vec, 0); + pgl.uniform4fv(loc, length, floatBuffer); } } } @@ -491,12 +507,13 @@ public class PShader { protected void setUniformMatrix(int loc, float[] mat) { if (-1 < loc) { + copyToFloatBuffer(mat); if (mat.length == 4) { - pgl.uniformMatrix2fv(loc, 1, false, mat, 0); + pgl.uniformMatrix2fv(loc, 1, false, floatBuffer); } else if (mat.length == 9) { - pgl.uniformMatrix3fv(loc, 1, false, mat, 0); + pgl.uniformMatrix3fv(loc, 1, false, floatBuffer); } else if (mat.length == 16) { - pgl.uniformMatrix4fv(loc, 1, false, mat, 0); + pgl.uniformMatrix4fv(loc, 1, false, floatBuffer); } } } @@ -587,37 +604,48 @@ public class PShader { pgl.uniform4f(loc, v[0], v[1], v[2], v[3]); } else if (val.type == UniformValue.INT1VEC) { int[] v = ((int[])val.value); - pgl.uniform1iv(loc, v.length, v, 0); + copyToIntBuffer(v); + pgl.uniform1iv(loc, v.length, intBuffer); } else if (val.type == UniformValue.INT2VEC) { int[] v = ((int[])val.value); - pgl.uniform2iv(loc, v.length / 2, v, 0); + copyToIntBuffer(v); + pgl.uniform2iv(loc, v.length / 2, intBuffer); } else if (val.type == UniformValue.INT3VEC) { int[] v = ((int[])val.value); - pgl.uniform3iv(loc, v.length / 3, v, 0); + copyToIntBuffer(v); + pgl.uniform3iv(loc, v.length / 3, intBuffer); } else if (val.type == UniformValue.INT4VEC) { int[] v = ((int[])val.value); - pgl.uniform4iv(loc, v.length / 4, v, 0); + copyToIntBuffer(v); + pgl.uniform4iv(loc, v.length / 4, intBuffer); } else if (val.type == UniformValue.FLOAT1VEC) { float[] v = ((float[])val.value); - pgl.uniform1fv(loc, v.length, v, 0); + copyToFloatBuffer(v); + pgl.uniform1fv(loc, v.length, floatBuffer); } else if (val.type == UniformValue.FLOAT2VEC) { float[] v = ((float[])val.value); - pgl.uniform2fv(loc, v.length / 2, v, 0); + copyToFloatBuffer(v); + pgl.uniform2fv(loc, v.length / 2, floatBuffer); } else if (val.type == UniformValue.FLOAT3VEC) { float[] v = ((float[])val.value); - pgl.uniform3fv(loc, v.length / 3, v, 0); + copyToFloatBuffer(v); + pgl.uniform3fv(loc, v.length / 3, floatBuffer); } else if (val.type == UniformValue.FLOAT4VEC) { float[] v = ((float[])val.value); - pgl.uniform4fv(loc, v.length / 4, v, 0); + copyToFloatBuffer(v); + pgl.uniform4fv(loc, v.length / 4, floatBuffer); } else if (val.type == UniformValue.MAT2) { float[] v = ((float[])val.value); - pgl.uniformMatrix2fv(loc, 1, false, v, 0); + copyToFloatBuffer(v); + pgl.uniformMatrix2fv(loc, 1, false, floatBuffer); } else if (val.type == UniformValue.MAT3) { float[] v = ((float[])val.value); - pgl.uniformMatrix3fv(loc, 1, false, v, 0); + copyToFloatBuffer(v); + pgl.uniformMatrix3fv(loc, 1, false, floatBuffer); } else if (val.type == UniformValue.MAT4) { float[] v = ((float[])val.value); - pgl.uniformMatrix4fv(loc, 1, false, v, 0); + copyToFloatBuffer(v); + pgl.uniformMatrix4fv(loc, 1, false, floatBuffer); } else if (val.type == UniformValue.SAMPLER2D) { PImage img = (PImage)val.value; Texture tex = pgMain.getTexture(img); @@ -634,6 +662,26 @@ public class PShader { } + protected void copyToIntBuffer(int[] vec) { + if (intBuffer.capacity() < vec.length) { + intBuffer = PGL.allocateDirectIntBuffer(vec.length); + } + intBuffer.rewind(); + intBuffer.put(vec, 0, vec.length); + intBuffer.rewind(); + } + + + protected void copyToFloatBuffer(float[] vec) { + if (floatBuffer.capacity() < vec.length) { + floatBuffer = PGL.allocateDirectFloatBuffer(vec.length); + } + floatBuffer.rewind(); + floatBuffer.put(vec, 0, vec.length); + floatBuffer.rewind(); + } + + protected void bindTextures() { if (textures != null) { for (int unit: textures.keySet()) { @@ -701,18 +749,17 @@ public class PShader { } pgl.linkProgram(glProgram); - int[] linked = new int[1]; - pgl.getProgramiv(glProgram, PGL.LINK_STATUS, linked, 0); - if (linked[0] == PGL.FALSE) { + pgl.getProgramiv(glProgram, PGL.LINK_STATUS, intBuffer); + boolean linked = intBuffer.get(0) == 0 ? false : true; + if (!linked) { PGraphics.showException("Cannot link shader program:\n" + pgl.getProgramInfoLog(glProgram)); } pgl.validateProgram(glProgram); - - int[] validated = new int[1]; - pgl.getProgramiv(glProgram, PGL.VALIDATE_STATUS, validated, 0); - if (validated[0] == PGL.FALSE) { + pgl.getProgramiv(glProgram, PGL.VALIDATE_STATUS, intBuffer); + boolean validated = intBuffer.get(0) == 0 ? false : true; + if (!validated) { PGraphics.showException("Cannot validate shader program:\n" + pgl.getProgramInfoLog(glProgram)); } @@ -801,9 +848,9 @@ public class PShader { pgl.shaderSource(glVertex, vertexShaderSource); pgl.compileShader(glVertex); - int[] compiled = new int[1]; - pgl.getShaderiv(glVertex, PGL.COMPILE_STATUS, compiled, 0); - if (compiled[0] == PGL.FALSE) { + pgl.getShaderiv(glVertex, PGL.COMPILE_STATUS, intBuffer); + boolean compiled = intBuffer.get(0) == 0 ? false : true; + if (!compiled) { PGraphics.showException("Cannot compile vertex shader:\n" + pgl.getShaderInfoLog(glVertex)); return false; @@ -822,9 +869,9 @@ public class PShader { pgl.shaderSource(glFragment, fragmentShaderSource); pgl.compileShader(glFragment); - int[] compiled = new int[1]; - pgl.getShaderiv(glFragment, PGL.COMPILE_STATUS, compiled, 0); - if (compiled[0] == PGL.FALSE) { + pgl.getShaderiv(glFragment, PGL.COMPILE_STATUS, intBuffer); + boolean compiled = intBuffer.get(0) == 0 ? false : true; + if (!compiled) { PGraphics.showException("Cannot compile fragment shader:\n" + pgl.getShaderInfoLog(glFragment)); return false; diff --git a/core/src/processing/opengl/PShapeOpenGL.java b/core/src/processing/opengl/PShapeOpenGL.java index 8f2149cb4..6416a905f 100644 --- a/core/src/processing/opengl/PShapeOpenGL.java +++ b/core/src/processing/opengl/PShapeOpenGL.java @@ -2417,11 +2417,11 @@ public class PShapeOpenGL extends PShape { public PShape getTessellation() { updateTessellation(); - float[] vertices = tessGeo.polyVertices; + FloatBuffer vertices = tessGeo.polyVertices; float[] normals = tessGeo.polyNormals; int[] color = tessGeo.polyColors; float[] uv = tessGeo.polyTexcoords; - short[] indices = tessGeo.polyIndices; + ShortBuffer indices = tessGeo.polyIndices; PShape tess; if (is3D()) { @@ -2441,20 +2441,20 @@ public class PShapeOpenGL extends PShape { int voffset = cache.vertexOffset[n]; for (int tr = ioffset / 3; tr < (ioffset + icount) / 3; tr++) { - int i0 = voffset + indices[3 * tr + 0]; - int i1 = voffset + indices[3 * tr + 1]; - int i2 = voffset + indices[3 * tr + 2]; + int i0 = voffset + indices.get(3 * tr + 0); + int i1 = voffset + indices.get(3 * tr + 1); + int i2 = voffset + indices.get(3 * tr + 2); if (is3D()) { - float x0 = vertices[4 * i0 + 0]; - float y0 = vertices[4 * i0 + 1]; - float z0 = vertices[4 * i0 + 2]; - float x1 = vertices[4 * i1 + 0]; - float y1 = vertices[4 * i1 + 1]; - float z1 = vertices[4 * i1 + 2]; - float x2 = vertices[4 * i2 + 0]; - float y2 = vertices[4 * i2 + 1]; - float z2 = vertices[4 * i2 + 2]; + float x0 = vertices.get(4 * i0 + 0); + float y0 = vertices.get(4 * i0 + 1); + float z0 = vertices.get(4 * i0 + 2); + float x1 = vertices.get(4 * i1 + 0); + float y1 = vertices.get(4 * i1 + 1); + float z1 = vertices.get(4 * i1 + 2); + float x2 = vertices.get(4 * i2 + 0); + float y2 = vertices.get(4 * i2 + 1); + float z2 = vertices.get(4 * i2 + 2); float nx0 = normals[3 * i0 + 0]; float ny0 = normals[3 * i0 + 1]; @@ -2482,9 +2482,9 @@ public class PShapeOpenGL extends PShape { tess.normal(nx2, ny2, nz2); tess.vertex(x2, y2, z2, uv[2 * i2 + 0], uv[2 * i2 + 1]); } else if (is2D()) { - float x0 = vertices[4 * i0 + 0], y0 = vertices[4 * i0 + 1]; - float x1 = vertices[4 * i1 + 0], y1 = vertices[4 * i1 + 1]; - float x2 = vertices[4 * i2 + 0], y2 = vertices[4 * i2 + 1]; + float x0 = vertices.get(4 * i0 + 0), y0 = vertices.get(4 * i0 + 1); + float x1 = vertices.get(4 * i1 + 0), y1 = vertices.get(4 * i1 + 1); + float x2 = vertices.get(4 * i2 + 0), y2 = vertices.get(4 * i2 + 1); int argb0 = PGL.nativeToJavaARGB(color[i0]); int argb1 = PGL.nativeToJavaARGB(color[i1]); @@ -3450,10 +3450,11 @@ public class PShapeOpenGL extends PShape { int sizef = size * PGL.SIZEOF_FLOAT; int sizei = size * PGL.SIZEOF_INT; + tessGeo.readyPolyVertices(); glPolyVertex = pg.createVertexBufferObject(context.id()); pgl.bindBuffer(PGL.ARRAY_BUFFER, glPolyVertex); pgl.bufferData(PGL.ARRAY_BUFFER, 4 * sizef, - FloatBuffer.wrap(tessGeo.polyVertices, 0, 4 * size), + tessGeo.polyVertices, PGL.STATIC_DRAW); glPolyColor = pg.createVertexBufferObject(context.id()); @@ -3500,12 +3501,12 @@ public class PShapeOpenGL extends PShape { pgl.bindBuffer(PGL.ARRAY_BUFFER, 0); + tessGeo.readyPolyIndices(); glPolyIndex = pg.createVertexBufferObject(context.id()); pgl.bindBuffer(PGL.ELEMENT_ARRAY_BUFFER, glPolyIndex); pgl.bufferData(PGL.ELEMENT_ARRAY_BUFFER, tessGeo.polyIndexCount * PGL.SIZEOF_INDEX, - ShortBuffer.wrap(tessGeo.polyIndices, 0, - tessGeo.polyIndexCount), PGL.STATIC_DRAW); + tessGeo.polyIndices, PGL.STATIC_DRAW); pgl.bindBuffer(PGL.ELEMENT_ARRAY_BUFFER, 0); } @@ -3882,11 +3883,10 @@ public class PShapeOpenGL extends PShape { protected void copyPolyVertices(int offset, int size) { + tessGeo.readyPolyVertices(); pgl.bindBuffer(PGL.ARRAY_BUFFER, glPolyVertex); pgl.bufferSubData(PGL.ARRAY_BUFFER, 4 * offset * PGL.SIZEOF_FLOAT, - 4 * size * PGL.SIZEOF_FLOAT, - FloatBuffer.wrap(tessGeo.polyVertices, - 4 * offset, 4 * size)); + 4 * size * PGL.SIZEOF_FLOAT, tessGeo.polyVertices); pgl.bindBuffer(PGL.ARRAY_BUFFER, 0); } @@ -4396,10 +4396,10 @@ public class PShapeOpenGL extends PShape { raw.noStroke(); raw.beginShape(TRIANGLES); - float[] vertices = tessGeo.polyVertices; + FloatBuffer vertices = tessGeo.polyVertices; int[] color = tessGeo.polyColors; float[] uv = tessGeo.polyTexcoords; - short[] indices = tessGeo.polyIndices; + ShortBuffer indices = tessGeo.polyIndices; IndexCache cache = tessGeo.polyIndexCache; for (int n = firstPolyIndexCache; n <= lastPolyIndexCache; n++) { @@ -4408,9 +4408,9 @@ public class PShapeOpenGL extends PShape { int voffset = cache.vertexOffset[n]; for (int tr = ioffset / 3; tr < (ioffset + icount) / 3; tr++) { - int i0 = voffset + indices[3 * tr + 0]; - int i1 = voffset + indices[3 * tr + 1]; - int i2 = voffset + indices[3 * tr + 2]; + int i0 = voffset + indices.get(3 * tr + 0); + int i1 = voffset + indices.get(3 * tr + 1); + int i2 = voffset + indices.get(3 * tr + 2); float[] src0 = {0, 0, 0, 0}; float[] src1 = {0, 0, 0, 0}; @@ -4422,9 +4422,9 @@ public class PShapeOpenGL extends PShape { int argb1 = PGL.nativeToJavaARGB(color[i1]); int argb2 = PGL.nativeToJavaARGB(color[i2]); - PApplet.arrayCopy(vertices, 4 * i0, src0, 0, 4); - PApplet.arrayCopy(vertices, 4 * i1, src1, 0, 4); - PApplet.arrayCopy(vertices, 4 * i2, src2, 0, 4); + vertices.position(4 * i0); vertices.get(src0, 0, 4); + vertices.position(4 * i1); vertices.get(src1, 0, 4); + vertices.position(4 * i2); vertices.get(src2, 0, 4); // Applying any transformation is currently stored in the // modelview matrix of the renderer. g.modelview.mult(src0, pt0);