From 49f5fc3ffbc47551e9c670cd37ed340c5a2343ed Mon Sep 17 00:00:00 2001 From: benfry Date: Mon, 22 Sep 2008 02:44:45 +0000 Subject: [PATCH] working on pshape and svg --- core/api.txt | 12 +++ core/src/processing/core/PApplet.java | 24 +++++ core/src/processing/core/PGraphics.java | 82 ++++++++++++++++ core/src/processing/core/PGraphics2D.java | 42 +++++--- core/src/processing/core/PGraphics3D.java | 14 +-- core/src/processing/core/PShape.java | 113 +++++----------------- core/todo.txt | 2 + todo.txt | 7 +- 8 files changed, 182 insertions(+), 114 deletions(-) diff --git a/core/api.txt b/core/api.txt index ad12fa804..3cc01bb86 100644 --- a/core/api.txt +++ b/core/api.txt @@ -10,6 +10,13 @@ render_lines -> renderLines() depth_sort_lines -> sortLines() no longer any abstract methods in PGraphics itself +// + +shapeStyle(null) +noStyle() +s.noStyle? +style(PStyle s) + // setParent() @@ -104,6 +111,11 @@ no longer any abstract methods in PGraphics itself float a, float b, float c, float d, int u1, int v1, int u2, int v2) + public void shapeMode(int mode) + public void shape(PShape shape) + public void shape(PShape shape, float x, float y) + public void shape(PShape shape, float x, float y, float w, float h) + public void textAlign(int align) public void textAlign(int alignX, int alignY) public float textAscent() diff --git a/core/src/processing/core/PApplet.java b/core/src/processing/core/PApplet.java index 893f75baa..4dcae2b14 100644 --- a/core/src/processing/core/PApplet.java +++ b/core/src/processing/core/PApplet.java @@ -6962,6 +6962,30 @@ public class PApplet extends Applet } + public void shapeMode(int mode) { + if (recorder != null) recorder.shapeMode(mode); + g.shapeMode(mode); + } + + + public void shape(PShape shape) { + if (recorder != null) recorder.shape(shape); + g.shape(shape); + } + + + public void shape(PShape shape, float x, float y) { + if (recorder != null) recorder.shape(shape, x, y); + g.shape(shape, x, y); + } + + + public void shape(PShape shape, float x, float y, float c, float d) { + if (recorder != null) recorder.shape(shape, x, y, c, d); + g.shape(shape, x, y, c, d); + } + + public void textAlign(int align) { if (recorder != null) recorder.textAlign(align); g.textAlign(align); diff --git a/core/src/processing/core/PGraphics.java b/core/src/processing/core/PGraphics.java index de9a48d41..55b1d218b 100644 --- a/core/src/processing/core/PGraphics.java +++ b/core/src/processing/core/PGraphics.java @@ -398,6 +398,9 @@ public class PGraphics extends PImage implements PConstants { /** The current ellipse mode (read-only) */ public int ellipseMode; + + /** The current shape alignment mode (read-only) */ + public int shapeMode; /** The current text font (read-only) */ public PFont textFont; @@ -1964,6 +1967,85 @@ public class PGraphics extends PImage implements PConstants { + ////////////////////////////////////////////////////////////// + + // SHAPE OBJECTS + + + /** + * Set the orientation for the shape() command (like imageMode() or rectMode()). + * @param which Either CORNER, CORNERS, or CENTER. + */ + public void shapeMode(int mode) { + this.shapeMode = mode; + } + + + public void shape(PShape shape) { + if (shape.isVisible()) { // don't do expensive matrix ops if invisible + if (shapeMode == CENTER) { + pushMatrix(); + translate(-shape.getWidth()/2, -shape.getHeight()/2); + } + + shape.draw(this); // needs to handle recorder too + + if (shapeMode == CENTER) { + popMatrix(); + } + } + } + + + /** + * Convenience method to draw at a particular location. + */ + public void shape(PShape shape, float x, float y) { + if (shape.isVisible()) { // don't do expensive matrix ops if invisible + pushMatrix(); + + if (shapeMode == CENTER) { + translate(x - shape.getWidth()/2, y - shape.getHeight()/2); + + } else if ((shapeMode == CORNER) || (shapeMode == CORNERS)) { + translate(x, y); + } + shape.draw(this); + + popMatrix(); + } + } + + + public void shape(PShape shape, float x, float y, float c, float d) { + if (shape.isVisible()) { // don't do expensive matrix ops if invisible + pushMatrix(); + + if (shapeMode == CENTER) { + // x and y are center, c and d refer to a diameter + translate(x - c/2f, y - d/2f); + scale(c / shape.getWidth(), d / shape.getHeight()); + + } else if (shapeMode == CORNER) { + translate(x, y); + scale(c / shape.getWidth(), d / shape.getHeight()); + + } else if (shapeMode == CORNERS) { + // c and d are x2/y2, make them into width/height + c -= x; + d -= y; + // then same as above + translate(x, y); + scale(c / shape.getWidth(), d / shape.getHeight()); + } + shape.draw(this); + + popMatrix(); + } + } + + + ////////////////////////////////////////////////////////////// // TEXT/FONTS diff --git a/core/src/processing/core/PGraphics2D.java b/core/src/processing/core/PGraphics2D.java index 41bee4e1b..7b13326c6 100644 --- a/core/src/processing/core/PGraphics2D.java +++ b/core/src/processing/core/PGraphics2D.java @@ -41,10 +41,12 @@ public class PGraphics2D extends PGraphics { PPolygon spolygon; // stroke/line polygon float svertices[][]; // temp vertices used for stroking end of poly + /* // polygon that handles tesselation private PPolygon tpolygon; private int TPOLYGON_MAX_VERTICES = 512; private int tpolygon_vertex_order[]; // = new int[MAX_VERTICES]; + */ PLine line; @@ -436,42 +438,49 @@ public class PGraphics2D extends PGraphics { private boolean isConvex() { - float v[][] = polygon.vertices; - int n = polygon.vertexCount; - int j,k; - int flag = 0; - float z; + //float v[][] = polygon.vertices; + //int n = polygon.vertexCount; + //int j,k; //float tol = 0.001f; - if (n < 3) + if (polygon.vertexCount < 3) { // ERROR: this is a line or a point, render with CONVEX return true; + } + int flag = 0; // iterate along border doing dot product. // if the sign of the result changes, then is concave - for (int i=0;i 0) + } else if (z > 0) { flag |= 2; - if (flag == 3) + } + if (flag == 3) { return false; // CONCAVE + } } - if (flag != 0) + if (flag != 0) { return true; // CONVEX - else + } else { // ERROR: colinear points, self intersection // treat as CONVEX return true; + } } // triangulate the current polygon private void concaveRender() { + } + + /* // WARNING: code is not in optimum form // local initiations of some variables are made to // keep the code modular and easy to integrate @@ -671,6 +680,7 @@ public class PGraphics2D extends PGraphics { } } } + */ diff --git a/core/src/processing/core/PGraphics3D.java b/core/src/processing/core/PGraphics3D.java index f890a7644..3ce0d7ff9 100644 --- a/core/src/processing/core/PGraphics3D.java +++ b/core/src/processing/core/PGraphics3D.java @@ -989,12 +989,14 @@ public class PGraphics3D extends PGraphics { protected final void add_path() { if (pathCount == pathOffset.length) { - int temp1[] = new int[pathCount << 1]; - System.arraycopy(pathOffset, 0, temp1, 0, pathCount); - pathOffset = temp1; - int temp2[] = new int[pathCount << 1]; - System.arraycopy(pathLength, 0, temp2, 0, pathCount); - pathLength = temp2; +// int temp1[] = new int[pathCount << 1]; +// System.arraycopy(pathOffset, 0, temp1, 0, pathCount); +// pathOffset = temp1; +// int temp2[] = new int[pathCount << 1]; +// System.arraycopy(pathLength, 0, temp2, 0, pathCount); +// pathLength = temp2; + pathOffset = PApplet.expand(pathOffset); + pathLength = PApplet.expand(pathLength); } pathOffset[pathCount] = lineCount; pathLength[pathCount] = 0; diff --git a/core/src/processing/core/PShape.java b/core/src/processing/core/PShape.java index 0cf3856c0..c14b5e720 100644 --- a/core/src/processing/core/PShape.java +++ b/core/src/processing/core/PShape.java @@ -37,7 +37,7 @@ abstract public class PShape implements PConstants { protected String name; protected int kind; - protected int drawMode; + //protected int drawMode; protected PMatrix3D matrix; // setAxis -> .x and .y to move x and y coords of origin @@ -45,9 +45,9 @@ abstract public class PShape implements PConstants { protected float y; protected float width; protected float height; - + // set to false if the object is hidden in the layers palette - protected boolean visible; + protected boolean visible = true; protected boolean stroke; protected int strokeColor; @@ -58,7 +58,7 @@ abstract public class PShape implements PConstants { protected boolean fill; protected int fillColor; - protected boolean styles; + protected boolean styles = true; //public boolean hasTransform; //protected float[] transformation; @@ -75,7 +75,7 @@ abstract public class PShape implements PConstants { protected PShape parent; protected int childCount; protected PShape[] children; - protected HashMap table; + protected HashMap table2; // POINTS, LINES, xLINE_STRIP, xLINE_LOOP // TRIANGLES, TRIANGLE_STRIP, TRIANGLE_FAN @@ -166,15 +166,6 @@ abstract public class PShape implements PConstants { // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . - /** - * Set the orientation for drawn objects, similar to PImage.imageMode(). - * @param which Either CORNER, CORNERS, or CENTER. - */ - public void drawMode(int which) { - drawMode = which; - } - - boolean strokeSaved; int strokeColorSaved; float strokeWeightSaved; @@ -189,12 +180,15 @@ abstract public class PShape implements PConstants { protected void pre(PGraphics g) { if (matrix != null) { - boolean flat = g instanceof PGraphics3D; + matrix.print(); + boolean flat = g instanceof PGraphics2D; g.pushMatrix(); if (flat) { - g.applyMatrix(matrix.m00, matrix.m01, matrix.m02, - matrix.m10, matrix.m11, matrix.m12); + g.applyMatrix(matrix.m00, matrix.m01, matrix.m03, + matrix.m10, matrix.m11, matrix.m13); +// g.applyMatrix(matrix.m00, matrix.m01, matrix.m02, +// matrix.m10, matrix.m11, matrix.m12); } else { g.applyMatrix(matrix.m00, matrix.m01, matrix.m02, matrix.m03, matrix.m10, matrix.m11, matrix.m12, matrix.m13, @@ -264,88 +258,26 @@ abstract public class PShape implements PConstants { } } - + /** * Called by the following (the shape() command adds the g) * PShape s = loadShapes("blah.svg"); * shape(s); */ public void draw(PGraphics g) { - if (!visible) return; - - if (drawMode == PConstants.CENTER) { - g.pushMatrix(); - g.translate(-width/2, -height/2); - } - - pre(g); - drawImpl(g); - post(g); - - if (drawMode == PConstants.CENTER) { - g.popMatrix(); - } - } - - - /** - * Convenience method to draw at a particular location. - */ - public void draw(PGraphics g, float x, float y) { - if (!visible) return; - - g.pushMatrix(); - - if (drawMode == PConstants.CENTER) { - g.translate(x - width/2, y - height/2); - - } else if ((drawMode == PConstants.CORNER) || - (drawMode == PConstants.CORNERS)) { - g.translate(x, y); - } - pre(g); - drawImpl(g); - post(g); - - g.popMatrix(); - } - - - public void draw(PGraphics g, float x, float y, float c, float d) { - if (!visible) return; - - g.pushMatrix(); - - if (drawMode == PConstants.CENTER) { - // x and y are center, c and d refer to a diameter - g.translate(x - c/2f, y - d/2f); - g.scale(c / width, d / height); - - } else if (drawMode == PConstants.CORNER) { - g.translate(x, y); - g.scale(c / width, d / height); - - } else if (drawMode == PConstants.CORNERS) { - // c and d are x2/y2, make them into width/height - c -= x; - d -= y; - // then same as above - g.translate(x, y); - g.scale(c / width, d / height); - } + if (visible) { pre(g); drawImpl(g); post(g); - - g.popMatrix(); + } } - + /** * Draws the SVG document. */ abstract public void drawImpl(PGraphics g); - + // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . @@ -400,11 +332,14 @@ abstract public class PShape implements PConstants { } children[childCount++] = who; who.parent = this; - - if (table == null) { - table = new HashMap(); + + String childName = who.getName(); + if (childName != null) { + if (table == null) { + table = new HashMap(); + } + table.put(childName, who); } - table.put(who.getName(), who); } @@ -541,6 +476,4 @@ abstract public class PShape implements PConstants { // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . - - } \ No newline at end of file diff --git a/core/todo.txt b/core/todo.txt index 6665d29b3..a6a9c815b 100644 --- a/core/todo.txt +++ b/core/todo.txt @@ -460,6 +460,8 @@ _ http://dev.processing.org/bugs/show_bug.cgi?id=100 _ shows a blank canvas _ (was only happening once b/c was drawing first in perspective) _ seems to be mapping to 0, 0 - width/2, height/2 +_ fix 3D > OrthoVsPerspective example once ortho works properly +_ there's a depth problem in addition to the ortho weirdness _ improve hint(ENABLE_DEPTH_SORT) to use proper painter's algo _ http://dev.processing.org/bugs/show_bug.cgi?id=176 _ polygon z-order depth sorting with alpha in opengl diff --git a/todo.txt b/todo.txt index 1fe166503..e6dd39dd2 100644 --- a/todo.txt +++ b/todo.txt @@ -82,11 +82,14 @@ X also need to get frame location: X http://processing.org/discourse/yabb_beta/YaBB.cgi?board=Syntax;action=display;num=1221384557 shiffman -_ update match(), write new reference for matchAll() +X update match(), write new reference for matchAll() -_ get new version of examples and reference +_ working on pshape +_ add shape() methods to PGraphics/PApplet +_ test and fix svg examples _ revisions.txt for x/y/z/ tx/ty/tz.. other changes in api.txt _ PMatrix now PMatrix3D (maybe not yet?) +_ get new version of examples and reference major windows launcher problem