working on pshape and svg

This commit is contained in:
benfry
2008-09-22 02:44:45 +00:00
parent 594f756b92
commit 49f5fc3ffb
8 changed files with 182 additions and 114 deletions
+12
View File
@@ -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()
+24
View File
@@ -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);
+82
View File
@@ -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
+26 -16
View File
@@ -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<n;i++) {
j = (i + 1) % n;
k = (i + 2) % n;
z = (v[j][TX] - v[i][TX]) * (v[k][TY] - v[j][TY]);
z -= (v[j][TY] - v[i][TY]) * (v[k][TX] - v[j][TX]);
if (z < 0)
for (int i = 0; i < polygon.vertexCount; i++) {
float[] vi = polygon.vertices[i];
float[] vj = polygon.vertices[(i + 1) % polygon.vertexCount];
float[] vk = polygon.vertices[(i + 2) % polygon.vertexCount];
float z = ((vj[TX] - vi[TX]) * (vk[TY] - vj[TY]) -
(vj[TY] - vi[TY]) * (vk[TX] - vj[TX]));
if (z < 0) {
flag |= 1;
else if (z > 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 {
}
}
}
*/
+8 -6
View File
@@ -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;
+23 -90
View File
@@ -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<String,PShape> table;
protected HashMap<String,PShape> 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,PShape>();
String childName = who.getName();
if (childName != null) {
if (table == null) {
table = new HashMap<String,PShape>();
}
table.put(childName, who);
}
table.put(who.getName(), who);
}
@@ -541,6 +476,4 @@ abstract public class PShape implements PConstants {
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
}
+2
View File
@@ -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
+5 -2
View File
@@ -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