From 5dd9ce289f6fdb3e41729f52588fc6574209325a Mon Sep 17 00:00:00 2001 From: codeanticode Date: Wed, 30 Nov 2011 23:41:35 +0000 Subject: [PATCH] Fixed tess data allocation, added library API to get tess data in PShape3D --- .../processing/opengl/PGraphicsOpenGL.java | 32 +++-- .../src/processing/opengl/PShape3D.java | 116 +++++++++++++++--- 2 files changed, 119 insertions(+), 29 deletions(-) diff --git a/java/libraries/opengl/src/processing/opengl/PGraphicsOpenGL.java b/java/libraries/opengl/src/processing/opengl/PGraphicsOpenGL.java index 06fb80408..0297b4a7d 100644 --- a/java/libraries/opengl/src/processing/opengl/PGraphicsOpenGL.java +++ b/java/libraries/opengl/src/processing/opengl/PGraphicsOpenGL.java @@ -431,7 +431,7 @@ public class PGraphicsOpenGL extends PGraphics { public static final int MAX_TESS_VERTICES = 1000000; public static final int DEFAULT_IN_VERTICES = 512; - public static final int DEFAULT_TESS_VERTICES = 512; + public static final int DEFAULT_TESS_VERTICES = 64; public static final int DEFAULT_TESS_INDICES = 1024; protected Tessellator tessellator; @@ -6370,8 +6370,9 @@ public class PGraphicsOpenGL extends PGraphics { } public void addFillVertices(int count) { - if (fillVertexCount + count >= fillVertices.length / 3) { - int newSize = expandSize(fillVertexCount, fillVertexCount + count); + int oldSize = fillVertices.length / 3; + if (fillVertexCount + count >= oldSize) { + int newSize = expandSize(oldSize, fillVertexCount + count); expandFillVertices(newSize); expandFillColors(newSize); @@ -6385,8 +6386,9 @@ public class PGraphicsOpenGL extends PGraphics { } public void addFillIndices(int count) { - if (fillIndexCount + count >= fillIndices.length) { - int newSize = expandSize(fillIndexCount, fillIndexCount + count); + int oldSize = fillIndices.length; + if (fillIndexCount + count >= oldSize) { + int newSize = expandSize(oldSize, fillIndexCount + count); expandFillIndices(newSize); } @@ -6421,8 +6423,9 @@ public class PGraphicsOpenGL extends PGraphics { } public void addLineVertices(int count) { - if (lineVertexCount + count >= lineVertices.length / 3) { - int newSize = expandSize(lineVertexCount, lineVertexCount + count); + int oldSize = lineVertices.length / 3; + if (lineVertexCount + count >= oldSize) { + int newSize = expandSize(oldSize, lineVertexCount + count); expandLineVertices(newSize); expandLineColors(newSize); @@ -6460,8 +6463,9 @@ public class PGraphicsOpenGL extends PGraphics { } public void addLineIndices(int count) { - if (lineIndexCount + count >= lineIndices.length) { - int newSize = expandSize(lineIndexCount, lineIndexCount + count); + int oldSize = lineIndices.length; + if (lineIndexCount + count >= oldSize) { + int newSize = expandSize(oldSize, lineIndexCount + count); expandLineIndices(newSize); } @@ -6478,8 +6482,9 @@ public class PGraphicsOpenGL extends PGraphics { } public void addPointVertices(int count) { - if (pointVertexCount + count >= pointVertices.length / 3) { - int newSize = expandSize(pointVertexCount, pointVertexCount + count); + int oldSize = pointVertices.length / 3; + if (pointVertexCount + count >= oldSize) { + int newSize = expandSize(oldSize, pointVertexCount + count); expandPointVertices(newSize); expandPointColors(newSize); @@ -6517,8 +6522,9 @@ public class PGraphicsOpenGL extends PGraphics { } public void addPointIndices(int count) { - if (pointIndexCount + count >= pointIndices.length) { - int newSize = expandSize(pointIndexCount, pointIndexCount + count); + int oldSize = pointIndices.length; + if (pointIndexCount + count >= oldSize) { + int newSize = expandSize(oldSize, pointIndexCount + count); expandPointIndices(newSize); } diff --git a/java/libraries/opengl/src/processing/opengl/PShape3D.java b/java/libraries/opengl/src/processing/opengl/PShape3D.java index d857cf666..dae394564 100644 --- a/java/libraries/opengl/src/processing/opengl/PShape3D.java +++ b/java/libraries/opengl/src/processing/opengl/PShape3D.java @@ -36,6 +36,7 @@ import processing.opengl.PGraphicsOpenGL.InGeometry; import processing.opengl.PGraphicsOpenGL.TessGeometry; import processing.opengl.PGraphicsOpenGL.Tessellator; +import java.nio.ByteBuffer; import java.nio.FloatBuffer; import java.nio.IntBuffer; import java.util.HashSet; @@ -63,6 +64,7 @@ public class PShape3D extends PShape { protected Tessellator tessellator; protected PShape3D root; protected PImage texture; + protected int glMode; public int glFillVertexBufferID; public int glFillColorBufferID; @@ -159,6 +161,8 @@ public class PShape3D extends PShape { public PShape3D(PApplet parent, int family) { ogl = (PGraphicsOpenGL)parent.g; + glMode = GL.GL_STATIC_DRAW; + glFillVertexBufferID = 0; glFillColorBufferID = 0; glFillNormalBufferID = 0; @@ -205,6 +209,15 @@ public class PShape3D extends PShape { this.kind = kind; } + public void setMode(int mode) { + if (mode == STATIC) { + glMode = GL.GL_STATIC_DRAW; + } else if (mode == DYNAMIC) { + glMode = GL.GL_DYNAMIC_DRAW; + } else if (mode == STREAM) { + glMode = GL2.GL_STREAM_COPY; + } + } protected void finalize() throws Throwable { try { @@ -582,9 +595,80 @@ public class PShape3D extends PShape { // - // Methods to access tessellation data... + // Methods to access tessellated data. Intended to use by libraries. + public int firstFillVertex() { + return tess.firstFillVertex; + } + + public int lastFillVertex() { + return tess.lastFillVertex; + } + + public int firstLineVertex() { + return tess.firstLineVertex; + } + + public int lastLineVertex() { + return tess.lastLineVertex; + } + + + public FloatBuffer mapFillVertices() { + ByteBuffer bb; + + getGl().glBindBuffer(GL.GL_ARRAY_BUFFER, root.glFillVertexBufferID); + if (root == this) { + bb = getGl().glMapBuffer(GL.GL_ARRAY_BUFFER, GL2.GL_READ_WRITE); + } else { + bb = ogl.gl2x.glMapBufferRange(GL.GL_ARRAY_BUFFER, 3 * tess.firstFillVertex, 3 * tess.fillVertexCount, GL2.GL_READ_WRITE); + } + return bb.asFloatBuffer(); + } + + public void unmapFillVertices() { + getGl().glUnmapBuffer(GL.GL_ARRAY_BUFFER); + getGl().glBindBuffer(GL.GL_ARRAY_BUFFER, 0); + } + + + + public FloatBuffer mapLineVertices() { + ByteBuffer bb; + + getGl().glBindBuffer(GL.GL_ARRAY_BUFFER, root.glLineVertexBufferID); + if (root == this) { + bb = getGl().glMapBuffer(GL.GL_ARRAY_BUFFER, GL2.GL_READ_WRITE); + } else { + bb = ogl.gl2x.glMapBufferRange(GL.GL_ARRAY_BUFFER, 3 * tess.firstLineVertex, 3 * tess.lineVertexCount, GL2.GL_READ_WRITE); + } + return bb.asFloatBuffer(); + } + + public void unmapLineVertices() { + getGl().glUnmapBuffer(GL.GL_ARRAY_BUFFER); + getGl().glBindBuffer(GL.GL_ARRAY_BUFFER, 0); + } + + + + public FloatBuffer mapLineAttributes() { + ByteBuffer bb; + + getGl().glBindBuffer(GL.GL_ARRAY_BUFFER, root.glLineAttribBufferID); + if (root == this) { + bb = getGl().glMapBuffer(GL.GL_ARRAY_BUFFER, GL2.GL_READ_WRITE); + } else { + bb = ogl.gl2x.glMapBufferRange(GL.GL_ARRAY_BUFFER, 3 * tess.firstLineVertex, 3 * tess.lineVertexCount, GL2.GL_READ_WRITE); + } + return bb.asFloatBuffer(); + } + + public void unmapLineAttributes() { + getGl().glUnmapBuffer(GL.GL_ARRAY_BUFFER); + getGl().glBindBuffer(GL.GL_ARRAY_BUFFER, 0); + } /////////////////////////////////////////////////////////// @@ -765,27 +849,27 @@ public class PShape3D extends PShape { protected void initFillBuffers(int nvert, int nind) { glFillVertexBufferID = ogl.createVertexBufferObject(); getGl().glBindBuffer(GL.GL_ARRAY_BUFFER, glFillVertexBufferID); - getGl().glBufferData(GL.GL_ARRAY_BUFFER, nvert * 3 * PGraphicsOpenGL.SIZEOF_FLOAT, null, GL.GL_STATIC_DRAW); + getGl().glBufferData(GL.GL_ARRAY_BUFFER, nvert * 3 * PGraphicsOpenGL.SIZEOF_FLOAT, null, glMode); getGl().glBindBuffer(GL.GL_ARRAY_BUFFER, 0); glFillColorBufferID = ogl.createVertexBufferObject(); getGl().glBindBuffer(GL.GL_ARRAY_BUFFER, glFillColorBufferID); - getGl().glBufferData(GL.GL_ARRAY_BUFFER, nvert * 4 * PGraphicsOpenGL.SIZEOF_FLOAT, null, GL.GL_STATIC_DRAW); + getGl().glBufferData(GL.GL_ARRAY_BUFFER, nvert * 4 * PGraphicsOpenGL.SIZEOF_FLOAT, null, glMode); getGl().glBindBuffer(GL.GL_ARRAY_BUFFER, 0); glFillNormalBufferID = ogl.createVertexBufferObject(); getGl().glBindBuffer(GL.GL_ARRAY_BUFFER, glFillNormalBufferID); - getGl().glBufferData(GL.GL_ARRAY_BUFFER, nvert * 3 * PGraphicsOpenGL.SIZEOF_FLOAT, null, GL.GL_STATIC_DRAW); + getGl().glBufferData(GL.GL_ARRAY_BUFFER, nvert * 3 * PGraphicsOpenGL.SIZEOF_FLOAT, null, glMode); getGl().glBindBuffer(GL.GL_ARRAY_BUFFER, 0); glFillTexCoordBufferID = ogl.createVertexBufferObject(); getGl().glBindBuffer(GL.GL_ARRAY_BUFFER, glFillTexCoordBufferID); - getGl().glBufferData(GL.GL_ARRAY_BUFFER, nvert * 2 * PGraphicsOpenGL.SIZEOF_FLOAT, null, GL.GL_STATIC_DRAW); + getGl().glBufferData(GL.GL_ARRAY_BUFFER, nvert * 2 * PGraphicsOpenGL.SIZEOF_FLOAT, null, glMode); getGl().glBindBuffer(GL.GL_ARRAY_BUFFER, 0); glFillIndexBufferID = ogl.createVertexBufferObject(); getGl().glBindBuffer(GL.GL_ARRAY_BUFFER, glFillIndexBufferID); - getGl().glBufferData(GL.GL_ARRAY_BUFFER, nind * PGraphicsOpenGL.SIZEOF_INT, null, GL.GL_STATIC_DRAW); + getGl().glBufferData(GL.GL_ARRAY_BUFFER, nind * PGraphicsOpenGL.SIZEOF_INT, null, glMode); getGl().glBindBuffer(GL.GL_ARRAY_BUFFER, 0); } @@ -846,27 +930,27 @@ public class PShape3D extends PShape { protected void initLineBuffers(int nvert, int nind) { glLineVertexBufferID = ogl.createVertexBufferObject(); getGl().glBindBuffer(GL.GL_ARRAY_BUFFER, glLineVertexBufferID); - getGl().glBufferData(GL.GL_ARRAY_BUFFER, nvert * 3 * PGraphicsOpenGL.SIZEOF_FLOAT, null, GL.GL_STATIC_DRAW); + getGl().glBufferData(GL.GL_ARRAY_BUFFER, nvert * 3 * PGraphicsOpenGL.SIZEOF_FLOAT, null, glMode); getGl().glBindBuffer(GL.GL_ARRAY_BUFFER, 0); glLineColorBufferID = ogl.createVertexBufferObject(); getGl().glBindBuffer(GL.GL_ARRAY_BUFFER, glLineColorBufferID); - getGl().glBufferData(GL.GL_ARRAY_BUFFER, nvert * 4 * PGraphicsOpenGL.SIZEOF_FLOAT, null, GL.GL_STATIC_DRAW); + getGl().glBufferData(GL.GL_ARRAY_BUFFER, nvert * 4 * PGraphicsOpenGL.SIZEOF_FLOAT, null, glMode); getGl().glBindBuffer(GL.GL_ARRAY_BUFFER, 0); glLineNormalBufferID = ogl.createVertexBufferObject(); getGl().glBindBuffer(GL.GL_ARRAY_BUFFER, glLineNormalBufferID); - getGl().glBufferData(GL.GL_ARRAY_BUFFER, nvert * 3 * PGraphicsOpenGL.SIZEOF_FLOAT, null, GL.GL_STATIC_DRAW); + getGl().glBufferData(GL.GL_ARRAY_BUFFER, nvert * 3 * PGraphicsOpenGL.SIZEOF_FLOAT, null, glMode); getGl().glBindBuffer(GL.GL_ARRAY_BUFFER, 0); glLineAttribBufferID = ogl.createVertexBufferObject(); getGl().glBindBuffer(GL.GL_ARRAY_BUFFER, glLineAttribBufferID); - getGl().glBufferData(GL.GL_ARRAY_BUFFER, nvert * 4 * PGraphicsOpenGL.SIZEOF_FLOAT, null, GL.GL_STATIC_DRAW); + getGl().glBufferData(GL.GL_ARRAY_BUFFER, nvert * 4 * PGraphicsOpenGL.SIZEOF_FLOAT, null, glMode); getGl().glBindBuffer(GL.GL_ARRAY_BUFFER, 0); glLineIndexBufferID = ogl.createVertexBufferObject(); getGl().glBindBuffer(GL.GL_ARRAY_BUFFER, glLineIndexBufferID); - getGl().glBufferData(GL.GL_ARRAY_BUFFER, nind * PGraphicsOpenGL.SIZEOF_INT, null, GL.GL_STATIC_DRAW); + getGl().glBufferData(GL.GL_ARRAY_BUFFER, nind * PGraphicsOpenGL.SIZEOF_INT, null, glMode); getGl().glBindBuffer(GL.GL_ARRAY_BUFFER, 0); } @@ -925,27 +1009,27 @@ public class PShape3D extends PShape { protected void initPointBuffers(int nvert, int nind) { glPointVertexBufferID = ogl.createVertexBufferObject(); getGl().glBindBuffer(GL.GL_ARRAY_BUFFER, glPointVertexBufferID); - getGl().glBufferData(GL.GL_ARRAY_BUFFER, nvert * 3 * PGraphicsOpenGL.SIZEOF_FLOAT, null, GL.GL_STATIC_DRAW); + getGl().glBufferData(GL.GL_ARRAY_BUFFER, nvert * 3 * PGraphicsOpenGL.SIZEOF_FLOAT, null, glMode); getGl().glBindBuffer(GL.GL_ARRAY_BUFFER, 0); glPointColorBufferID = ogl.createVertexBufferObject(); getGl().glBindBuffer(GL.GL_ARRAY_BUFFER, glPointColorBufferID); - getGl().glBufferData(GL.GL_ARRAY_BUFFER, nvert * 4 * PGraphicsOpenGL.SIZEOF_FLOAT, null, GL.GL_STATIC_DRAW); + getGl().glBufferData(GL.GL_ARRAY_BUFFER, nvert * 4 * PGraphicsOpenGL.SIZEOF_FLOAT, null, glMode); getGl().glBindBuffer(GL.GL_ARRAY_BUFFER, 0); glPointNormalBufferID = ogl.createVertexBufferObject(); getGl().glBindBuffer(GL.GL_ARRAY_BUFFER, glPointNormalBufferID); - getGl().glBufferData(GL.GL_ARRAY_BUFFER, nvert * 3 * PGraphicsOpenGL.SIZEOF_FLOAT, null, GL.GL_STATIC_DRAW); + getGl().glBufferData(GL.GL_ARRAY_BUFFER, nvert * 3 * PGraphicsOpenGL.SIZEOF_FLOAT, null, glMode); getGl().glBindBuffer(GL.GL_ARRAY_BUFFER, 0); glPointAttribBufferID = ogl.createVertexBufferObject(); getGl().glBindBuffer(GL.GL_ARRAY_BUFFER, glPointAttribBufferID); - getGl().glBufferData(GL.GL_ARRAY_BUFFER, nvert * 4 * PGraphicsOpenGL.SIZEOF_FLOAT, null, GL.GL_STATIC_DRAW); + getGl().glBufferData(GL.GL_ARRAY_BUFFER, nvert * 4 * PGraphicsOpenGL.SIZEOF_FLOAT, null, glMode); getGl().glBindBuffer(GL.GL_ARRAY_BUFFER, 0); glPointIndexBufferID = ogl.createVertexBufferObject(); getGl().glBindBuffer(GL.GL_ARRAY_BUFFER, glPointIndexBufferID); - getGl().glBufferData(GL.GL_ARRAY_BUFFER, nind * PGraphicsOpenGL.SIZEOF_INT, null, GL.GL_STATIC_DRAW); + getGl().glBufferData(GL.GL_ARRAY_BUFFER, nind * PGraphicsOpenGL.SIZEOF_INT, null, glMode); getGl().glBindBuffer(GL.GL_ARRAY_BUFFER, 0); }