From 5b65f762f2dae8ca8d5e46dbe8c7eb083c9ced92 Mon Sep 17 00:00:00 2001 From: benfry Date: Sun, 27 Feb 2005 20:19:53 +0000 Subject: [PATCH] finished cleaning PGraphics3 --- processing/core/PApplet.java | 8 +- processing/core/PGraphics.java | 14 +- processing/core/PGraphics2.java | 4 +- processing/core/PGraphics3.java | 372 +++++++++++++++++++++----------- processing/core/PMethods.java | 10 +- processing/core/todo.txt | 2 + 6 files changed, 266 insertions(+), 144 deletions(-) diff --git a/processing/core/PApplet.java b/processing/core/PApplet.java index 15c6167be..531dc1ce6 100644 --- a/processing/core/PApplet.java +++ b/processing/core/PApplet.java @@ -262,6 +262,8 @@ public class PApplet extends Applet public void depth() { + // OPT if PGraphics already exists, pass in its pixels[] + // buffer so as not to re-allocate all that memory again if (g.width != 0) { g = new PGraphics3(g.width, g.height); } else { @@ -4670,9 +4672,9 @@ v PApplet.this.stop(); } - public void arc(float start, float stop, - float a, float b, float c, float d) { - g.arc(start, stop, a, b, c, d); + public void arc(float a, float b, float c, float d, + float start, float stop) { + g.arc(a, b, c, d, start, stop); } diff --git a/processing/core/PGraphics.java b/processing/core/PGraphics.java index 67d1ac3b8..c5d1fc889 100644 --- a/processing/core/PGraphics.java +++ b/processing/core/PGraphics.java @@ -306,7 +306,7 @@ public class PGraphics extends PImage implements PConstants { allocate(); // clear the screen with the old background color - background(backgroundColor); + //background(backgroundColor); } @@ -318,7 +318,9 @@ public class PGraphics extends PImage implements PConstants { // because of a java 1.1 bug, pixels must be registered as // opaque before their first run, the memimgsrc will flicker // and run very slowly. - for (int i = 0; i < pixelCount; i++) pixels[i] = 0xffffffff; + backgroundColor |= 0xff000000; // just for good measure + for (int i = 0; i < pixelCount; i++) pixels[i] = backgroundColor; + //for (int i = 0; i < pixelCount; i++) pixels[i] = 0xffffffff; cm = new DirectColorModel(32, 0x00ff0000, 0x0000ff00, 0x000000ff);; mis = new MemoryImageSource(width, height, pixels, 0, width); @@ -865,8 +867,8 @@ public class PGraphics extends PImage implements PConstants { * angleMode() sets DEGREES or RADIANS for the start & stop * ellipseMode() sets the placement. */ - public void arc(float start, float stop, - float a, float b, float c, float d) { + public void arc(float a, float b, float c, float d, + float start, float stop) { float x = a; float y = b; float w = c; @@ -901,8 +903,8 @@ public class PGraphics extends PImage implements PConstants { } - protected void arcImpl(float start, float stop, - float x, float y, float w, float h) { + protected void arcImpl(float x, float y, float w, float h, + float start, float stop) { } diff --git a/processing/core/PGraphics2.java b/processing/core/PGraphics2.java index e33548ed8..6dce69ddc 100644 --- a/processing/core/PGraphics2.java +++ b/processing/core/PGraphics2.java @@ -463,8 +463,8 @@ public class PGraphics2 extends PGraphics { } - public void arcImpl(float start, float stop, - float x, float y, float w, float h) { + public void arcImpl(float x, float y, float w, float h, + float start, float stop) { arc.setArc(x, y, w, h, start, stop-start, Arc2D.PIE); draw_shape(arc); } diff --git a/processing/core/PGraphics3.java b/processing/core/PGraphics3.java index 42b878887..05f3e775a 100644 --- a/processing/core/PGraphics3.java +++ b/processing/core/PGraphics3.java @@ -262,25 +262,6 @@ public class PGraphics3 extends PGraphics { } - public void defaults() { - super.defaults(); - - cameraMode(PERSPECTIVE); - - //System.out.println("PGraphics3.defaults()"); - // easiest for beginners - textureMode(IMAGE_SPACE); - - // better to leave this turned off by default - noLights(); - - lightEnable(0); - lightAmbient(0, 0, 0, 0); - - light(1, cameraX, cameraY, cameraZ, 255, 255, 255); - } - - public void beginFrame() { super.beginFrame(); @@ -323,6 +304,28 @@ public class PGraphics3 extends PGraphics { } + public void defaults() { + super.defaults(); + + cameraMode(PERSPECTIVE); + + //System.out.println("PGraphics3.defaults()"); + // easiest for beginners + textureMode(IMAGE_SPACE); + + // better to leave this turned off by default + noLights(); + + lightEnable(0); + lightAmbient(0, 0, 0, 0); + + light(1, cameraX, cameraY, cameraZ, 255, 255, 255); + } + + + ////////////////////////////////////////////////////////////// + + public void beginShape(int kind) { shape = kind; @@ -353,6 +356,62 @@ public class PGraphics3 extends PGraphics { } + /** + * Sets the current normal. Mostly will apply to vertices + * inside a beginShape/endShape block. + */ + public void normal(float nx, float ny, float nz) { + // if drawing a shape and the normal hasn't changed yet, + // then need to set all the normal for each vertex so far + /* + if ((shape != 0) && !normalChanged) { + for (int i = vertex_start; i < vertex_end; i++) { + vertices[i][NX] = normalX; + vertices[i][NY] = normalY; + vertices[i][NZ] = normalZ; + } + normalChanged = true; + } + */ + normalX = nx; + normalY = ny; + normalZ = nz; + } + + + /** + * set texture mode to either IMAGE_SPACE (more intuitive + * for new users) or NORMAL_SPACE (better for advanced chaps) + */ + public void textureMode(int mode) { + this.textureMode = mode; + } + + + /** + * set texture image for current shape + * needs to be called between @see beginShape and @see endShape + * + * @param image reference to a PImage object + */ + public void texture(PImage image) { + textureImage = image; + + if (texture_index == textures.length - 1) { + PImage temp[] = new PImage[texture_index<<1]; + System.arraycopy(textures, 0, temp, 0, texture_index); + textures = temp; + //message(CHATTER, "allocating more textures " + textures.length); + } + + if (textures[0] != null) { // wHY? + texture_index++; + } + + textures[texture_index] = image; + } + + public void vertex(float x, float y) { setup_vertex(x, y, 0); } @@ -567,62 +626,6 @@ public class PGraphics3 extends PGraphics { } - /** - * Sets the current normal. Mostly will apply to vertices - * inside a beginShape/endShape block. - */ - public void normal(float nx, float ny, float nz) { - // if drawing a shape and the normal hasn't changed yet, - // then need to set all the normal for each vertex so far - /* - if ((shape != 0) && !normalChanged) { - for (int i = vertex_start; i < vertex_end; i++) { - vertices[i][NX] = normalX; - vertices[i][NY] = normalY; - vertices[i][NZ] = normalZ; - } - normalChanged = true; - } - */ - normalX = nx; - normalY = ny; - normalZ = nz; - } - - - /** - * set texture mode to either IMAGE_SPACE (more intuitive - * for new users) or NORMAL_SPACE (better for advanced chaps) - */ - public void textureMode(int mode) { - this.textureMode = mode; - } - - - /** - * set texture image for current shape - * needs to be called between @see beginShape and @see endShape - * - * @param image reference to a PImage object - */ - public void texture(PImage image) { - textureImage = image; - - if (texture_index == textures.length - 1) { - PImage temp[] = new PImage[texture_index<<1]; - System.arraycopy(textures, 0, temp, 0, texture_index); - textures = temp; - //message(CHATTER, "allocating more textures " + textures.length); - } - - if (textures[0] != null) { // wHY? - texture_index++; - } - - textures[texture_index] = image; - } - - public void endShape() { vertex_end = vertexCount; @@ -1082,12 +1085,6 @@ public class PGraphics3 extends PGraphics { } - - ////////////////////////////////////////////////////////////// - - // LIGHTS AND COLOR - - /** * This method handles the transformation, lighting, and clipping * operations for the shapes. Broken out as a separate function @@ -1480,12 +1477,12 @@ public class PGraphics3 extends PGraphics { // PLACED SHAPES - public void draw_rect(float x1, float y1, float x2, float y2) { + protected void rectImpl(float x1, float y1, float x2, float y2) { quad(x1, y1, x2, y1, x2, y2, x1, y2); } - protected void draw_ellipse(float x, float y, float hradius, float vradius) { + protected void ellipseImpl(float x, float y, float hradius, float vradius) { // adapt accuracy to radii used w/ a minimum of 4 segments [toxi] // now uses current scale factors to determine "real" transformed radius @@ -1520,8 +1517,8 @@ public class PGraphics3 extends PGraphics { * This is so that an arc can be drawn that crosses zero mark, * and the user will still collect $200. */ - protected void draw_arc(float start, float stop, - float x, float y, float w, float h) { + protected void arcImpl(float x, float y, float w, float h, + float start, float stop) { float hr = w / 2f; float vr = h / 2f; @@ -1577,25 +1574,6 @@ public class PGraphics3 extends PGraphics { - ////////////////////////////////////////////////////////////// - - // CURVES - - - public void bezier(float x1, float y1, float z1, - float x2, float y2, float z2, - float x3, float y3, float z3, - float x4, float y4, float z4) { - beginShape(LINE_STRIP); - bezierVertex(x1, y1, z1); - bezierVertex(x2, y2, z2); - bezierVertex(x3, y3, z3); - bezierVertex(x4, y4, z4); - endShape(); - } - - - ////////////////////////////////////////////////////////////// // 3D BOX @@ -1792,6 +1770,114 @@ public class PGraphics3 extends PGraphics { + ////////////////////////////////////////////////////////////// + + // CURVES + + + public void bezier(float x1, float y1, + float x2, float y2, + float x3, float y3, + float x4, float y4) { + bezier(x1, y1, 0, + x2, y2, 0, + x3, y3, 0, + x4, y4, 0); + } + + + public void bezier(float x1, float y1, float z1, + float x2, float y2, float z2, + float x3, float y3, float z3, + float x4, float y4, float z4) { + beginShape(LINE_STRIP); + bezierVertex(x1, y1, z1); + bezierVertex(x2, y2, z2); + bezierVertex(x3, y3, z3); + bezierVertex(x4, y4, z4); + endShape(); + } + + + public void curve(float x1, float y1, + float x2, float y2, + float x3, float y3, + float x4, float y4) { + curve(x1, y1, 0, + x2, y2, 0, + x3, y3, 0, + x4, y4, 0); + } + + + public void curve(float x1, float y1, float z1, + float x2, float y2, float z2, + float x3, float y3, float z3, + float x4, float y4, float z4) { + beginShape(LINE_STRIP); + curveVertex(x1, y1, z1); + curveVertex(x2, y2, z2); + curveVertex(x3, y3, z3); + curveVertex(x4, y4, z4); + endShape(); + } + + + ////////////////////////////////////////////////////////////// + + + protected void imageImpl(PImage image, + float x1, float y1, float w, float h, + int u1, int v1, int u2, int v2) { + + float x2 = x1 + w; + float y2 = y1 + h; + + boolean savedStroke = stroke; + boolean savedFill = fill; + int savedTextureMode = textureMode; + + stroke = false; + fill = true; + textureMode = IMAGE_SPACE; + + float savedFillR = fillR; + float savedFillG = fillG; + float savedFillB = fillB; + float savedFillA = fillA; + + if (tint) { + fillR = tintR; + fillG = tintG; + fillB = tintB; + fillA = tintA; + + } else { + fillR = 1; + fillG = 1; + fillB = 1; + fillA = 1; + } + + beginShape(QUADS); + texture(image); + vertex(x1, y1, u1, v1); + vertex(x1, y2, u1, v2); + vertex(x2, y2, u2, v2); + vertex(x2, y1, u2, v1); + endShape(); + + stroke = savedStroke; + fill = savedFill; + textureMode = savedTextureMode; + + fillR = savedFillR; + fillG = savedFillG; + fillB = savedFillB; + fillA = savedFillA; + } + + ////////////////////////////////////////////////////////////// // MATRIX TRANSFORMATIONS @@ -1810,25 +1896,6 @@ public class PGraphics3 extends PGraphics { } - // OPT could save several multiplies for the 0s and 1s by just - // putting the multMatrix code here and removing uneccessary terms - - public void rotateX(float angle) { - //dimensions = 3; - float c = cos(angle); - float s = sin(angle); - applyMatrix(1, 0, 0, 0, 0, c, -s, 0, 0, s, c, 0, 0, 0, 0, 1); - } - - - public void rotateY(float angle) { - //dimensions = 3; - float c = cos(angle); - float s = sin(angle); - applyMatrix(c, 0, s, 0, 0, 1, 0, 0, -s, 0, c, 0, 0, 0, 0, 1); - } - - /** * Two dimensional rotation. Same as rotateZ (this is identical * to a 3D rotation along the z-axis) but included for clarity -- @@ -1840,6 +1907,23 @@ public class PGraphics3 extends PGraphics { } + // OPT could save several multiplies for the 0s and 1s by just + // putting the multMatrix code here and removing uneccessary terms + + public void rotateX(float angle) { + float c = cos(angle); + float s = sin(angle); + applyMatrix(1, 0, 0, 0, 0, c, -s, 0, 0, s, c, 0, 0, 0, 0, 1); + } + + + public void rotateY(float angle) { + float c = cos(angle); + float s = sin(angle); + applyMatrix(c, 0, s, 0, 0, 1, 0, 0, -s, 0, c, 0, 0, 0, 0, 1); + } + + /** * Rotate in the XY plane by an angle. * @@ -1848,8 +1932,6 @@ public class PGraphics3 extends PGraphics { * 2D rotate in the XY plane. */ public void rotateZ(float angle) { - //rotate(angle, 0, 0, 1); - //if (dimensions == 0) dimensions = 2; // otherwise already 2 or higher float c = cos(angle); float s = sin(angle); applyMatrix(c, -s, 0, 0, s, c, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1); @@ -1882,19 +1964,26 @@ public class PGraphics3 extends PGraphics { /** - * This will scale in all three dimensions, but not set the - * dimensions higher than two, in case this is still 2D mode. + * Same as scale(s, s, s); */ public void scale(float s) { applyMatrix(s, 0, 0, 0, 0, s, 0, 0, 0, 0, s, 0, 0, 0, 0, 1); } + /** + * Not recommended for use in 3D, because the z-dimension is just + * scaled by 1, since there's no way to know what else to scale it by. + * Equivalent to scale(sx, sy, 1); + */ public void scale(float sx, float sy) { applyMatrix(sx, 0, 0, 0, 0, sy, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1); } + /** + * Scale in three dimensions. + */ public void scale(float x, float y, float z) { applyMatrix(x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1); } @@ -2235,6 +2324,12 @@ public class PGraphics3 extends PGraphics { } + + ////////////////////////////////////////////////////////////// + + // SCREEN AND OBJECT COORDINATES + + public float screenX(float x, float y) { return screenX(x, y, 0); } @@ -2425,7 +2520,28 @@ public class PGraphics3 extends PGraphics { ////////////////////////////////////////////////////////////// - // MATH + // SMOOTH (not available, throws error) + + // although should this bother throwing an error? + // could be a pain in the ass when trying to debug with opengl + + + public void smooth() { + String msg = "smooth() not available when used with depth()"; + throw new RuntimeException(msg); + } + + + public void noSmooth() { + String msg = "noSmooth() not available when used with depth()"; + throw new RuntimeException(msg); + } + + + + ////////////////////////////////////////////////////////////// + + // MATH (internal use only) private final float mag(float a, float b) { diff --git a/processing/core/PMethods.java b/processing/core/PMethods.java index 1c6c71ef8..159c6b812 100755 --- a/processing/core/PMethods.java +++ b/processing/core/PMethods.java @@ -87,8 +87,8 @@ public interface PMethods { // REMOVED public void arc(float start, float stop, // float x, float y, float radius); - public void arc(float start, float stop, - float x, float y, float hr, float vr); + public void arc(float x, float y, float hr, float vr, + float start, float stop); //protected void arcImpl(float start, float stop, // float x, float y, float hr, float vr); @@ -155,9 +155,9 @@ public interface PMethods { float x1, float y1, float x2, float y2, int u1, int v1, int u2, int v2); - //public void cache(PImage image); - - //public void cache(PImage images[]); + //protected void imageImpl(PImage image, + // float x, float y, float w, float h, + // int u1, int v1, int u2, int v2); // diff --git a/processing/core/todo.txt b/processing/core/todo.txt index fbd38dadc..fc7b8cdcb 100644 --- a/processing/core/todo.txt +++ b/processing/core/todo.txt @@ -48,6 +48,8 @@ X imageMode(CORNER) and CORNERS are the only usable versions _ alpha(PImage) is now called mask() instead? _ already have an alpha() function that gets alpha bits _ sphere x,y,z,r or box w,h,d.. need to make them consistent +_ should we switch to curveVertex(1,2,3) (ala curveto) +_ because some confusion with mixing types of curves _ the updatePixels() in PGraphics has to be overridden (from PImage) _ to make itself actually update on-screen