From 602c7b373edd111c93f09baeb3aae548c4660154 Mon Sep 17 00:00:00 2001 From: codeanticode Date: Thu, 8 Apr 2010 06:40:45 +0000 Subject: [PATCH] PShapeSVG to GLModel tesselation is working --- .../core/src/processing/core/GLConstants.java | 3 + android/core/src/processing/core/GLModel.java | 57 +++++++++----- android/core/src/processing/core/PApplet.java | 24 +++++- .../core/src/processing/core/PConstants.java | 6 +- .../processing/core/PGraphicsAndroid3D.java | 75 +++++++++---------- 5 files changed, 100 insertions(+), 65 deletions(-) diff --git a/android/core/src/processing/core/GLConstants.java b/android/core/src/processing/core/GLConstants.java index 89cb05b50..d08ffa82d 100644 --- a/android/core/src/processing/core/GLConstants.java +++ b/android/core/src/processing/core/GLConstants.java @@ -64,6 +64,9 @@ public interface GLConstants { public static final int DYNAMIC = 1; + /** + * Used in GLModels.. + */ public static final int VERTICES = 0; public static final int NORMALS = 1; public static final int COLORS = 2; diff --git a/android/core/src/processing/core/GLModel.java b/android/core/src/processing/core/GLModel.java index 2238c1139..30922400d 100644 --- a/android/core/src/processing/core/GLModel.java +++ b/android/core/src/processing/core/GLModel.java @@ -76,7 +76,10 @@ public class GLModel extends PShape implements GLConstants, PConstants { protected int recreateResourceIdx; public float depth; - + protected float xmin, xmax; + protected float ymin, ymax; + protected float zmin, zmax; + protected static final int SIZEOF_FLOAT = Float.SIZE / 8; @@ -126,6 +129,10 @@ public class GLModel extends PShape implements GLConstants, PConstants { createModel(numVert); initGroups(); updateElement = -1; + + width = height = depth = 0; + xmin = ymin = zmin = 10000; + xmax = ymax = zmax = -10000; try { Method meth = this.getClass().getMethod("recreateResource", new Class[] { PGraphicsAndroid3D.class }); @@ -362,9 +369,10 @@ public class GLModel extends PShape implements GLConstants, PConstants { grIdx1 = idx; } + updateBounds(x, y, z); vertexArray[3 * idx + 0] = x; vertexArray[3 * idx + 1] = y; - vertexArray[3 * idx + 2] = z; + vertexArray[3 * idx + 2] = z; } @@ -381,6 +389,10 @@ public class GLModel extends PShape implements GLConstants, PConstants { grIdx1 = numVertices - 1; } + for (int i = 0; i < numVertices; i++) { + updateBounds(data[3 * i + 0], data[3 * i + 1], data[3 * i + 2]); + } + PApplet.arrayCopy(data, vertexArray); } @@ -404,9 +416,25 @@ public class GLModel extends PShape implements GLConstants, PConstants { vertexArray[3 * i + 0] = vec.x; vertexArray[3 * i + 1] = vec.y; vertexArray[3 * i + 2] = vec.z; + updateBounds(vec.x, vec.y, vec.z); } } + + protected void updateBounds(float x, float y, float z) { + xmin = PApplet.min(xmin, x); + xmax = PApplet.max(xmax, x); + + ymin = PApplet.min(ymin, y); + ymax = PApplet.max(ymax, y); + + zmin = PApplet.min(zmin, z); + zmax = PApplet.max(zmax, z); + + width = xmax - xmin; + height = ymax - ymin; + depth = zmax - zmin; + } //////////////////////////////////////////////////////////// @@ -1296,42 +1324,31 @@ public class GLModel extends PShape implements GLConstants, PConstants { // Methods inherited from PShape. - public float getWidth() { - return width; - } - - public float getHeight() { - return height; - } - public float getDepth() { return depth; } public boolean isVisible() { - return true; + return visible; } - public void draw(PGraphics g) { - render(); - } /////////////////////////////////////////////////////////// - // Rendering methods + // Drawing methods - public void render() { - render(0, groups.size() - 1); + public void draw(PGraphics g) { + draw(g, 0, groups.size() - 1); } - public void render(int gr) { - render(gr, gr); + public void draw(PGraphics g, int gr) { + draw(g, gr, gr); } - public void render(int gr0, int gr1) { + public void draw(PGraphics g, int gr0, int gr1) { int texTarget = GL11.GL_TEXTURE_2D; GLTexture tex; float pointSize; diff --git a/android/core/src/processing/core/PApplet.java b/android/core/src/processing/core/PApplet.java index 06589f478..b8686032e 100644 --- a/android/core/src/processing/core/PApplet.java +++ b/android/core/src/processing/core/PApplet.java @@ -3012,7 +3012,6 @@ public class PApplet extends Activity implements PConstants, Runnable { return new PShapeSVG(this, filename); } else if (filename.toLowerCase().endsWith(".obj")) { if (g instanceof PGraphicsAndroid3D) { - // TODO: implement obj loading for GLModels return new GLModel(this, filename); } else { throw new RuntimeException("OBJ files can be loaded only when using the A3D renderer."); @@ -3022,10 +3021,14 @@ public class PApplet extends Activity implements PConstants, Runnable { } - public PShape createShape(int nvert) { + public PShape createShape(int nvert, int kind) { + return this.createShape(nvert, kind, GLConstants.STATIC); + } + + + public PShape createShape(int nvert, int kind, int mode) { if (g instanceof PGraphicsAndroid3D) { - // TODO: how to handle custom parameters? - GLModel.Parameters params = GLModel.newParameters(TRIANGLES, GLConstants.STATIC); + GLModel.Parameters params = GLModel.newParameters(kind, GLConstants.STATIC); GLModel model = new GLModel(this, nvert, params); return model; } else { @@ -3033,6 +3036,19 @@ public class PApplet extends Activity implements PConstants, Runnable { } } + + public PShape createShape(PShape shape) { + if (g instanceof PGraphicsAndroid3D) { + PGraphicsAndroid3D a3d = (PGraphicsAndroid3D)g; + a3d.beginShapeRecorderImpl(); + shape(shape, 0, 0, 1, 1); + GLModel model = a3d.endShapeRecorderImpl(); + return model; + } else { + throw new RuntimeException("3D PShapes can only be created when using the A3D renderer."); + } + } + ////////////////////////////////////////////////////////////// // FONT I/O diff --git a/android/core/src/processing/core/PConstants.java b/android/core/src/processing/core/PConstants.java index 2ab6811f5..c463f1f2c 100644 --- a/android/core/src/processing/core/PConstants.java +++ b/android/core/src/processing/core/PConstants.java @@ -290,9 +290,9 @@ public interface PConstants { static final int SPHERE = 40; static final int BOX = 41; - static public final int LINE_STRIP = 50; - static public final int LINE_LOOP = 51; - static public final int POINT_SPRITES = 52; + static public final int LINE_STRIP = 50; + static public final int LINE_LOOP = 51; + static public final int POINT_SPRITES = 52; // shape closing modes diff --git a/android/core/src/processing/core/PGraphicsAndroid3D.java b/android/core/src/processing/core/PGraphicsAndroid3D.java index 12a216d05..935febe16 100644 --- a/android/core/src/processing/core/PGraphicsAndroid3D.java +++ b/android/core/src/processing/core/PGraphicsAndroid3D.java @@ -630,21 +630,26 @@ public class PGraphicsAndroid3D extends PGraphics { //public void beginShape() - + public void beginShapeRecorder() { beginShapeRecorder(POLYGON); } public void beginShapeRecorder(int kind) { + beginShapeRecorderImpl(); + beginShape(kind); + } + + + protected void beginShapeRecorderImpl() { recordingModel = true; recordedVertices = new ArrayList(vertexBuffer.capacity() / 3); recordedColors = new ArrayList(colorBuffer.capacity() / 4); recordedNormals = new ArrayList(normalBuffer.capacity() / 4); recordedTexCoords = new ArrayList(texCoordBuffer.capacity() / 2); - recordedGroups = new ArrayList(); - beginShape(kind); + recordedGroups = new ArrayList(); } @@ -959,31 +964,38 @@ public class PGraphicsAndroid3D extends PGraphics { public GLModel endShapeRecorder(int mode) { endShape(mode); - - recordingModel = false; - GLModel model = new GLModel(parent, recordedVertices.size()); - - model.beginUpdate(GLConstants.VERTICES); - model.setVertex(recordedVertices); - model.endUpdate(); - - model.beginUpdate(GLConstants.COLORS); - model.setColor(recordedColors); - model.endUpdate(); - - model.beginUpdate(GLConstants.NORMALS); - model.setNormal(recordedNormals); - model.endUpdate(); - - model.beginUpdate(GLConstants.TEXTURES); - model.setTexCoord(recordedTexCoords); - model.endUpdate(); - - model.setGroups(recordedGroups); - return model; + return endShapeRecorderImpl(); } + protected GLModel endShapeRecorderImpl() { + recordingModel = false; + if (0 < recordedVertices.size()) { + GLModel model = new GLModel(parent, recordedVertices.size()); + + model.beginUpdate(GLConstants.VERTICES); + model.setVertex(recordedVertices); + model.endUpdate(); + + model.beginUpdate(GLConstants.COLORS); + model.setColor(recordedColors); + model.endUpdate(); + + model.beginUpdate(GLConstants.NORMALS); + model.setNormal(recordedNormals); + model.endUpdate(); + + model.beginUpdate(GLConstants.TEXTURES); + model.setTexCoord(recordedTexCoords); + model.endUpdate(); + + model.setGroups(recordedGroups); + return model; + } + return null; + } + + ////////////////////////////////////////////////////////////// // BEZIER CURVE VERTICES @@ -1704,19 +1716,6 @@ public class PGraphicsAndroid3D extends PGraphics { //protected void sort() - - - ////////////////////////////////////////////////////////////// - - // MODEL, SHAPE - - - public void model(GLModel model, float x, float y, float z) { - gl.glPushMatrix(); - gl.glTranslatef(x, y, z); - model.render(); - gl.glPopMatrix(); - } //////////////////////////////////////////////////////////////