From 7fd145506194e266baa23b619bffb005b7bde285 Mon Sep 17 00:00:00 2001 From: codeanticode Date: Tue, 3 Sep 2013 14:40:34 -0400 Subject: [PATCH] fix #1990 --- core/src/processing/core/PShape.java | 2 + core/src/processing/opengl/PGraphics2D.java | 2 +- core/src/processing/opengl/PGraphics3D.java | 2 +- .../processing/opengl/PGraphicsOpenGL.java | 44 ++------ core/src/processing/opengl/PShapeOpenGL.java | 102 +++++++++++++++--- 5 files changed, 98 insertions(+), 54 deletions(-) diff --git a/core/src/processing/core/PShape.java b/core/src/processing/core/PShape.java index 40ec381f5..8420f72f6 100644 --- a/core/src/processing/core/PShape.java +++ b/core/src/processing/core/PShape.java @@ -167,6 +167,8 @@ public class PShape implements PConstants { protected float shininess; protected int sphereDetailU, sphereDetailV; + protected int rectMode; + protected int ellipseMode; /** Temporary toggle for whether styles should be honored. */ protected boolean style = true; diff --git a/core/src/processing/opengl/PGraphics2D.java b/core/src/processing/opengl/PGraphics2D.java index f1149ab65..d8adced31 100644 --- a/core/src/processing/opengl/PGraphics2D.java +++ b/core/src/processing/opengl/PGraphics2D.java @@ -353,7 +353,7 @@ public class PGraphics2D extends PGraphicsOpenGL { shape = new PShapeOpenGL(parent, PShape.PRIMITIVE); shape.setKind(RECT); } else if (kind == ELLIPSE) { - if (len != 4) { + if (len != 4 && len != 5) { showWarning("Wrong number of parameters"); return null; } diff --git a/core/src/processing/opengl/PGraphics3D.java b/core/src/processing/opengl/PGraphics3D.java index c2d78474a..318b32df2 100644 --- a/core/src/processing/opengl/PGraphics3D.java +++ b/core/src/processing/opengl/PGraphics3D.java @@ -224,7 +224,7 @@ public class PGraphics3D extends PGraphicsOpenGL { shape = new PShapeOpenGL(parent, PShape.PRIMITIVE); shape.setKind(RECT); } else if (kind == ELLIPSE) { - if (len != 4) { + if (len != 4 && len != 5) { showWarning("Wrong number of parameters"); return null; } diff --git a/core/src/processing/opengl/PGraphicsOpenGL.java b/core/src/processing/opengl/PGraphicsOpenGL.java index b3f6e2d28..318d33556 100644 --- a/core/src/processing/opengl/PGraphicsOpenGL.java +++ b/core/src/processing/opengl/PGraphicsOpenGL.java @@ -2922,8 +2922,7 @@ public class PGraphicsOpenGL extends PGraphics { inGeo.setMaterial(fillColor, strokeColor, strokeWeight, ambientColor, specularColor, emissiveColor, shininess); inGeo.setNormal(normalX, normalY, normalZ); - inGeo.addRect(a, b, c, d, - fill, stroke, rectMode); + inGeo.addRect(a, b, c, d, fill, stroke, rectMode); endShape(); } @@ -2937,8 +2936,7 @@ public class PGraphicsOpenGL extends PGraphics { inGeo.setMaterial(fillColor, strokeColor, strokeWeight, ambientColor, specularColor, emissiveColor, shininess); inGeo.setNormal(normalX, normalY, normalZ); - inGeo.addRect(a, b, c, d, - tl, tr, br, bl, + inGeo.addRect(a, b, c, d, tl, tr, br, bl, fill, stroke, bezierDetail, rectMode); endShape(CLOSE); } @@ -2953,14 +2951,14 @@ public class PGraphicsOpenGL extends PGraphics { @Override - public void ellipse(float a, float b, float c, float d) { + public void ellipseImpl(float a, float b, float c, float d) { beginShape(TRIANGLE_FAN); defaultEdges = false; normalMode = NORMAL_MODE_SHAPE; inGeo.setMaterial(fillColor, strokeColor, strokeWeight, ambientColor, specularColor, emissiveColor, shininess); inGeo.setNormal(normalX, normalY, normalZ); - inGeo.addEllipse(a, b, c, d, fill, stroke, ellipseMode); + inGeo.addEllipse(a, b, c, d, fill, stroke); endShape(); } @@ -8749,38 +8747,8 @@ public class PGraphicsOpenGL extends PGraphics { if (stroke) addPolygonEdges(true); } - void addEllipse(float a, float b, float c, float d, - boolean fill, boolean stroke, int ellipseMode) { - float x = a; - float y = b; - float w = c; - float h = d; - - if (ellipseMode == CORNERS) { - w = c - a; - h = d - b; - - } else if (ellipseMode == RADIUS) { - x = a - c; - y = b - d; - w = c * 2; - h = d * 2; - - } else if (ellipseMode == DIAMETER) { - x = a - c/2f; - y = b - d/2f; - } - - if (w < 0) { // undo negative width - x += w; - w = -w; - } - - if (h < 0) { // undo negative height - y += h; - h = -h; - } - + void addEllipse(float x, float y, float w, float h, + boolean fill, boolean stroke) { float radiusH = w / 2; float radiusV = h / 2; diff --git a/core/src/processing/opengl/PShapeOpenGL.java b/core/src/processing/opengl/PShapeOpenGL.java index d1dd3fe72..c218f4b3a 100644 --- a/core/src/processing/opengl/PShapeOpenGL.java +++ b/core/src/processing/opengl/PShapeOpenGL.java @@ -354,6 +354,9 @@ public class PShapeOpenGL extends PShape { sphereDetailU = pg.sphereDetailU; sphereDetailV = pg.sphereDetailV; + rectMode = pg.rectMode; + ellipseMode = pg.ellipseMode; + normalX = normalY = 0; normalZ = 1; @@ -2711,11 +2714,11 @@ public class PShapeOpenGL extends PShape { float tl = 0, tr = 0, br = 0, bl = 0; boolean rounded = false; if (params.length == 4) { - rounded = false; a = params[0]; b = params[1]; c = params[2]; d = params[3]; + rounded = false; } else if (params.length == 5) { a = params[0]; b = params[1]; @@ -2753,17 +2756,52 @@ public class PShapeOpenGL extends PShape { protected void tessellateEllipse() { float a = 0, b = 0, c = 0, d = 0; - if (params.length == 4) { + int mode = ellipseMode; + + if (4 <= params.length) { a = params[0]; b = params[1]; c = params[2]; d = params[3]; + if (params.length == 5) { + mode = (int)(params[4]); + } + } + + float x = a; + float y = b; + float w = c; + float h = d; + + if (mode == CORNERS) { + w = c - a; + h = d - b; + + } else if (mode == RADIUS) { + x = a - c; + y = b - d; + w = c * 2; + h = d * 2; + + } else if (mode == DIAMETER) { + x = a - c/2f; + y = b - d/2f; + } + + if (w < 0) { // undo negative width + x += w; + w = -w; + } + + if (h < 0) { // undo negative height + y += h; + h = -h; } inGeo.setMaterial(fillColor, strokeColor, strokeWeight, ambientColor, specularColor, emissiveColor, shininess); inGeo.setNormal(normalX, normalY, normalZ); - inGeo.addEllipse(a, b, c, d, fill, stroke, CORNER); + inGeo.addEllipse(x, y, w, h, fill, stroke); tessellator.tessellateTriangleFan(); } @@ -2771,25 +2809,61 @@ public class PShapeOpenGL extends PShape { protected void tessellateArc() { float a = 0, b = 0, c = 0, d = 0; float start = 0, stop = 0; -// int mode = 0; - if (params.length == 6 || params.length == 7) { + int mode = ellipseMode; + + if (6 <= params.length) { a = params[0]; b = params[1]; c = params[2]; d = params[3]; start = params[4]; stop = params[5]; - // Not using arc mode since PShape only uses CORNER -// if (params.length == 7) { -// mode = (int)(params[6]); -// } + if (params.length == 7) { + mode = (int)(params[6]); + } } - inGeo.setMaterial(fillColor, strokeColor, strokeWeight, - ambientColor, specularColor, emissiveColor, shininess); - inGeo.setNormal(normalX, normalY, normalZ); - inGeo.addArc(a, b, c, d, start, stop, fill, stroke, CORNER); - tessellator.tessellateTriangleFan(); + float x = a; + float y = b; + float w = c; + float h = d; + + if (mode == CORNERS) { + w = c - a; + h = d - b; + + } else if (mode == RADIUS) { + x = a - c; + y = b - d; + w = c * 2; + h = d * 2; + + } else if (mode == CENTER) { + x = a - c/2f; + y = b - d/2f; + } + + // make sure the loop will exit before starting while + if (!Float.isInfinite(start) && !Float.isInfinite(stop)) { + // ignore equal and degenerate cases + if (stop > start) { + // make sure that we're starting at a useful point + while (start < 0) { + start += TWO_PI; + stop += TWO_PI; + } + + if (stop - start > TWO_PI) { + start = 0; + stop = TWO_PI; + } + inGeo.setMaterial(fillColor, strokeColor, strokeWeight, + ambientColor, specularColor, emissiveColor, shininess); + inGeo.setNormal(normalX, normalY, normalZ); + inGeo.addArc(x, y, w, h, start, stop, fill, stroke, mode); + tessellator.tessellateTriangleFan(); + } + } }