From 1428da87b82606b057079ac6f4a95669cda249b4 Mon Sep 17 00:00:00 2001 From: benfry Date: Sun, 28 Sep 2008 22:05:50 +0000 Subject: [PATCH] working on pgraphics/pshape cleanup --- core/api.txt | 10 +- core/src/processing/core/PGraphics.java | 106 +++++++----- core/src/processing/core/PGraphics2D.java | 2 +- core/src/processing/core/PGraphics3D.java | 20 +-- core/src/processing/core/PGraphicsJava2D.java | 163 ++---------------- core/src/processing/core/PTriangle.java | 2 +- .../processing/opengl/PGraphicsOpenGL.java | 4 +- todo.txt | 1 + 8 files changed, 104 insertions(+), 204 deletions(-) diff --git a/core/api.txt b/core/api.txt index e9f6bf430..b0fbd125b 100644 --- a/core/api.txt +++ b/core/api.txt @@ -51,6 +51,7 @@ render_lines -> renderLines() depth_sort_lines -> sortLines() no longer any abstract methods in PGraphics itself removed support for specular alpha (and its 'SPA' constant) +clear() -> backgroundImpl() // @@ -109,10 +110,12 @@ style(PStyle s) float x3, float y3, float x4, float y4) public void rectMode(int mode) public void rect(float x1, float y1, float x2, float y2) + public void ellipseMode(int mode) public void ellipse(float a, float b, float c, float d) public void arc(float a, float b, float c, float d, float start, float stop) + public void box(float size) public void box(float w, float h, float d) public void sphereDetail(int res) @@ -194,8 +197,6 @@ style(PStyle s) public void scale(float sx, float sy) public void scale(float x, float y, float z) - public void pushMatrix() - public void popMatrix() public void resetMatrix() public void applyMatrix(float n00, float n01, float n02, float n10, float n11, float n12) @@ -204,6 +205,9 @@ style(PStyle s) float n20, float n21, float n22, float n23, float n30, float n31, float n32, float n33) + public void pushMatrix() + public void popMatrix() + public void loadMatrix() public void printMatrix() @@ -214,7 +218,6 @@ style(PStyle s) float centerX, float centerY, float centerZ, float upX, float upY, float upZ) public void printCamera() - public void ortho() public void ortho(float left, float right, float bottom, float top, @@ -306,6 +309,7 @@ style(PStyle s) public void background(float x, float y, float z) public void background(float x, float y, float z, float a) public void background(PImage image) + protected void backgroundImpl() public final float alpha(int what) public final float red(int what) diff --git a/core/src/processing/core/PGraphics.java b/core/src/processing/core/PGraphics.java index 507ee53b2..d9737235f 100644 --- a/core/src/processing/core/PGraphics.java +++ b/core/src/processing/core/PGraphics.java @@ -303,13 +303,13 @@ public class PGraphics extends PImage implements PConstants { public PFont textFont; /** The current text align (read-only) */ - public int textAlign; + public int textAlign = LEFT; /** The current vertical text alignment (read-only) */ - public int textAlignY; + public int textAlignY = BASELINE; /** The current text mode (read-only) */ - public int textMode; + public int textMode = MODEL; /** The current text size (read-only) */ public float textSize; @@ -489,7 +489,7 @@ public class PGraphics extends PImage implements PConstants { public PMatrix3D modelview; /** Inverse modelview matrix, used for lighting. */ - public PMatrix3D modelviewInv; + protected PMatrix3D modelviewInv; /** * The camera matrix, the modelview will be set to this on beginDraw. @@ -497,34 +497,33 @@ public class PGraphics extends PImage implements PConstants { public PMatrix3D camera; /** Inverse camera matrix */ - public PMatrix3D cameraInv; + protected PMatrix3D cameraInv; // ........................................................ - /** Camera field of view (in radians, as of rev 86) */ + /** Camera field of view. */ public float cameraFOV; - /** Position of the camera */ + /** Position of the camera. */ public float cameraX, cameraY, cameraZ; - public float cameraNear, cameraFar; + /** Aspect ratio of camera's view. */ public float cameraAspect; - // projection matrix + /** Current projection matrix. */ public PMatrix3D projection; // ........................................................ - /// the stencil buffer - public int stencil[]; - - /// depth buffer - public float zbuffer[]; + /** The depth buffer. */ + public float[] zbuffer; // ........................................................ - /** Maximum lights by default is 8, which is arbitrary, - but is the minimum defined by OpenGL */ + /** + * Maximum lights by default is 8, which is arbitrary for this renderer, + * but is the minimum defined by OpenGL + */ public static final int MAX_LIGHTS = 8; public int lightCount = 0; @@ -592,9 +591,7 @@ public class PGraphics extends PImage implements PConstants { // ........................................................ - /** - * Normals - */ + /** Current normal vector. */ public float normalX, normalY, normalZ; // ........................................................ @@ -797,15 +794,18 @@ public class PGraphics extends PImage implements PConstants { } if (stroke) { stroke(strokeColor); - if (strokeWeight != DEFAULT_STROKE_WEIGHT) { - strokeWeight(strokeWeight); - } - if (strokeCap != DEFAULT_STROKE_CAP) { - strokeCap(strokeCap); - } - if (strokeJoin != DEFAULT_STROKE_JOIN) { - strokeJoin(strokeJoin); - } + + // The if() statements should be handled inside the functions, + // otherwise an actual reset/revert won't work properly. + //if (strokeWeight != DEFAULT_STROKE_WEIGHT) { + strokeWeight(strokeWeight); + //} +// if (strokeCap != DEFAULT_STROKE_CAP) { + strokeCap(strokeCap); +// } +// if (strokeJoin != DEFAULT_STROKE_JOIN) { + strokeJoin(strokeJoin); +// } } else { noStroke(); } @@ -827,6 +827,8 @@ public class PGraphics extends PImage implements PConstants { textFont(textFont, textSize); textLeading(saveLeading); } + textMode(textMode); + textAlign(textAlign, textAlignY); background(backgroundColor); //reapplySettings = false; @@ -3546,26 +3548,42 @@ public class PGraphics extends PImage implements PConstants { calcG = (float)calcGi / 255.0f; calcB = (float)calcBi / 255.0f; calcAlpha = (calcAi != 255); - } ////////////////////////////////////////////////////////////// - public void strokeWeight(float weight) { strokeWeight = weight; + //if (strokeWeight != DEFAULT_STROKE_WEIGHT) { + strokeWeightImpl(); } + /** Renderer-specific handling after the strokeJoin has been set. */ + protected void strokeWeightImpl() { + } + + public void strokeJoin(int join) { strokeJoin = join; + strokeJoinImpl(); + } + + + /** Renderer-specific handling after the strokeJoin has been set. */ + protected void strokeJoinImpl() { } public void strokeCap(int cap) { strokeCap = cap; } + + + /** Renderer-specific handling after the strokeCap has been set. */ + protected void strokeCapImpl() { + } public void noStroke() { @@ -3922,7 +3940,7 @@ public class PGraphics extends PImage implements PConstants { } colorCalcARGB(rgb, colorModeA); backgroundFromCalc(); - clear(); + backgroundImpl(); } } @@ -3941,7 +3959,7 @@ public class PGraphics extends PImage implements PConstants { } else { colorCalcARGB(rgb, alpha); backgroundFromCalc(); - clear(); + backgroundImpl(); } } } @@ -3954,7 +3972,7 @@ public class PGraphics extends PImage implements PConstants { public void background(float gray) { colorCalc(gray); backgroundFromCalc(); - clear(); + backgroundImpl(); } @@ -3968,7 +3986,7 @@ public class PGraphics extends PImage implements PConstants { } else { colorCalc(gray, alpha); backgroundFromCalc(); - clear(); + backgroundImpl(); } } @@ -3980,7 +3998,7 @@ public class PGraphics extends PImage implements PConstants { public void background(float x, float y, float z) { colorCalc(x, y, z); backgroundFromCalc(); - clear(); + backgroundImpl(); } @@ -4002,7 +4020,7 @@ public class PGraphics extends PImage implements PConstants { } else { colorCalc(x, y, z, a); backgroundFromCalc(); - clear(); + backgroundImpl(); } } @@ -4053,7 +4071,7 @@ public class PGraphics extends PImage implements PConstants { /** * Clear the pixel buffer. */ - protected void clear() { + protected void backgroundImpl() { } @@ -4066,6 +4084,10 @@ public class PGraphics extends PImage implements PConstants { HashMap errors; + /** + * Show a renderer error, and keep track of it so that it's only shown once. + * @param msg the error message (which will be stored for later comparison) + */ protected void showError(String msg) { if (errors == null) { errors = new HashMap(); @@ -4084,8 +4106,9 @@ public class PGraphics extends PImage implements PConstants { protected void depthErrorXYZ(String method) { - showError(method + "(x, y, z) can only be used with " + - "OPENGL or P3D, use " + method + "(x, y) instead."); + showError(method + "(x, y, z) can only be used with a renderer that " + + "supports 3D, such as P3D or OPENGL. " + + "Use " + method + "(x, y) instead."); } @@ -4094,6 +4117,11 @@ public class PGraphics extends PImage implements PConstants { } + /** + * Error that a particular variation of a method is unavailable (even though + * other variations are). For instance, if vertex(x, y, u, v) is unavailable, + * but vertex(x, y) is just fine, it doesn't make sense to use methodError(). + */ protected void variationError(String str) { showError(str + " is not available with this renderer."); } diff --git a/core/src/processing/core/PGraphics2D.java b/core/src/processing/core/PGraphics2D.java index 7b13326c6..b29b5b1a5 100644 --- a/core/src/processing/core/PGraphics2D.java +++ b/core/src/processing/core/PGraphics2D.java @@ -1774,7 +1774,7 @@ public class PGraphics2D extends PGraphics { /** * Clear the pixel buffer. */ - protected void clear() { + protected void backgroundImpl() { Arrays.fill(pixels, backgroundColor); } diff --git a/core/src/processing/core/PGraphics3D.java b/core/src/processing/core/PGraphics3D.java index e38dfc900..d24aa0dc7 100644 --- a/core/src/processing/core/PGraphics3D.java +++ b/core/src/processing/core/PGraphics3D.java @@ -28,8 +28,6 @@ import java.awt.Toolkit; import java.awt.image.*; import java.util.*; -import processing.core.PGraphics.Style; - /** * Subclass of PGraphics that handles 3D rendering. @@ -284,7 +282,7 @@ public class PGraphics3D extends PGraphics { Arrays.fill(zbuffer, Float.MAX_VALUE); } - stencil = new int[pixelCount]; +// stencil = new int[pixelCount]; line = new PLine(this); triangle = new PTriangle(this); @@ -769,14 +767,14 @@ public class PGraphics3D extends PGraphics { add_path(); for (int i = vertex_start; i < stop; i++) { //counter = i - vertex_start; - add_line(i,i+1); + add_line(i, i+1); } // then draw from vertex (n) to (n+2) stop = vertex_end-2; for (int i = vertex_start; i < stop; i++) { add_path(); - add_line(i,i+2); + add_line(i, i+2); } } break; @@ -945,7 +943,6 @@ public class PGraphics3D extends PGraphics { } - // ------------------------------------------------------------------ // POINTS FROM CAMERA SPACE (VX, VY, VZ) TO SCREEN SPACE (X, Y, Z) // this appears to be wasted time with the opengl renderer @@ -1016,6 +1013,7 @@ public class PGraphics3D extends PGraphics { ////////////////////////////////////////////////////////////// + // begin a new section of stroked geometry protected final void add_path() { if (pathCount == pathOffset.length) { // int temp1[] = new int[pathCount << 1]; @@ -1179,6 +1177,7 @@ public class PGraphics3D extends PGraphics { } } + private final int interpolate_clip_vertex(int a, int b) { float[] va; float[] vb; @@ -2205,7 +2204,6 @@ public class PGraphics3D extends PGraphics { protected void handle_lighting() { - // If the lighting does not depend on vertex position and there is a single // normal specified for this shape, go ahead and apply the same lighting // contribution to every vertex in this shape (one lighting calc!) @@ -2663,7 +2661,7 @@ public class PGraphics3D extends PGraphics { /** - * Same as scale(s, s, s); + * Same as scale(s, s, s). */ public void scale(float s) { scale(s, s, s); @@ -2671,6 +2669,7 @@ public class PGraphics3D extends PGraphics { /** + * Same as scale(sx, sy, 1). */ public void scale(float sx, float sy) { scale(sx, sy, 1); @@ -3847,13 +3846,12 @@ public class PGraphics3D extends PGraphics { /** * Clear pixel buffer. With P3D and OPENGL, this also clears the zbuffer. - * Stencil buffer should also be cleared, but for now is ignored in P3D. */ - protected void clear() { + protected void clear3() { Arrays.fill(pixels, backgroundColor); Arrays.fill(zbuffer, Float.MAX_VALUE); clearRaw(); - } + } /** diff --git a/core/src/processing/core/PGraphicsJava2D.java b/core/src/processing/core/PGraphicsJava2D.java index b559d5e2f..474ba6aa7 100644 --- a/core/src/processing/core/PGraphicsJava2D.java +++ b/core/src/processing/core/PGraphicsJava2D.java @@ -756,120 +756,6 @@ public class PGraphicsJava2D extends PGraphics2D { } } - /* - // for rev 0124, passing the tintColor in here. the problem is that - // the 'parent' PGraphics object of this inner class may not be - // the same one that's used when drawing. for instance, if this - // is a font used by the main drawing surface, then it's later - // used in an offscreen PGraphics, the tintColor value from the - // original PGraphics will be used. - public void update(boolean tint, int tintColor) { - if (tintedPixels == null) { - //System.out.println("tinted pixels null"); - tintedPixels = new int[source.width * source.height]; - } - - if ((source.format == ARGB) || (source.format == RGB)) { - if (tint) { - // create tintedPixels[] if necessary - //if (tintedPixels == null) { - // tintedPixels = new int[source.width * source.height]; - //} - - int a2 = (tintColor >> 24) & 0xff; - int r2 = (tintColor >> 16) & 0xff; - int g2 = (tintColor >> 8) & 0xff; - int b2 = (tintColor) & 0xff; - - // multiply each of the color components into tintedPixels - // if straight RGB image, don't bother multiplying - // (also avoids problems if high bits not set) - if (source.format == RGB) { - int alpha = a2 << 24; - - for (int i = 0; i < tintedPixels.length; i++) { - int argb1 = source.pixels[i]; - int r1 = (argb1 >> 16) & 0xff; - int g1 = (argb1 >> 8) & 0xff; - int b1 = (argb1) & 0xff; - - tintedPixels[i] = alpha | - (((r2 * r1) & 0xff00) << 8) | - ((g2 * g1) & 0xff00) | - (((b2 * b1) & 0xff00) >> 8); - } - - } else { - for (int i = 0; i < tintedPixels.length; i++) { - int argb1 = source.pixels[i]; - int a1 = (argb1 >> 24) & 0xff; - int r1 = (argb1 >> 16) & 0xff; - int g1 = (argb1 >> 8) & 0xff; - int b1 = (argb1) & 0xff; - - tintedPixels[i] = - (((a2 * a1) & 0xff00) << 16) | - (((r2 * r1) & 0xff00) << 8) | - ((g2 * g1) & 0xff00) | - (((b2 * b1) & 0xff00) >> 8); - } - } - - tinted = true; - tintedColor = tintColor; - - // finally, do a setRGB based on tintedPixels - //image.setRGB(0, 0, source.width, source.height, - // tintedPixels, 0, source.width); - WritableRaster raster = ((BufferedImage) image).getRaster(); - raster.setDataElements(0, 0, source.width, source.height, - tintedPixels); - - } else { // no tint - // just do a setRGB like before - // (and we'll just hope that the high bits are set) - //image.setRGB(0, 0, source.width, source.height, - // source.pixels, 0, source.width); - WritableRaster raster = ((BufferedImage) image).getRaster(); - raster.setDataElements(0, 0, source.width, source.height, - source.pixels); - } - - } else if (source.format == ALPHA) { - int lowbits = tintColor & 0x00ffffff; - if (((tintColor >> 24) & 0xff) >= 254) { - //PApplet.println(" no alfa " + PApplet.hex(tintColor)); - // no actual alpha to the tint, set the image's alpha - // as the high 8 bits, and use the color as the low 24 bits - for (int i = 0; i < tintedPixels.length; i++) { - // don't bother with the math if value is zero - tintedPixels[i] = (source.pixels[i] == 0) ? - 0 : (source.pixels[i] << 24) | lowbits; - } - - } else { - //PApplet.println(" yes alfa " + PApplet.hex(tintColor)); - // multiply each image alpha by the tint alpha - int alphabits = (tintColor >> 24) & 0xff; - for (int i = 0; i < tintedPixels.length; i++) { - tintedPixels[i] = (source.pixels[i] == 0) ? - 0 : (((alphabits * source.pixels[i]) & 0xFF00) << 16) | lowbits; - } - } - - // mark the pixels for next time - tinted = true; - tintedColor = tintColor; - - // finally, do a setRGB based on tintedPixels - //image.setRGB(0, 0, source.width, source.height, - // tintedPixels, 0, source.width); - WritableRaster raster = ((BufferedImage) image).getRaster(); - raster.setDataElements(0, 0, source.width, source.height, tintedPixels); - } - } - */ - ////////////////////////////////////////////////////////////// @@ -1144,7 +1030,7 @@ public class PGraphicsJava2D extends PGraphics2D { public void background(PImage image) { if ((image.width != width) || (image.height != height)) { throw new RuntimeException("background image must be " + - "the same size as your application"); + "the same size as the sketch."); } if ((image.format != RGB) && (image.format != ARGB)) { throw new RuntimeException("background images should be RGB or ARGB"); @@ -1156,21 +1042,14 @@ public class PGraphicsJava2D extends PGraphics2D { int[] clearPixels; - public void clear() { - // the only way to properly clear the screen is to re-allocate + public void backgroundImpl() { if (backgroundAlpha) { - // clearRect() doesn't work because it just makes everything black. - // instead, just wipe out the canvas to its transparent original - //allocate(); - - // allocate also won't work, because all the settings - // (like smooth) will be completely reset. - // Instead, create a small array that can be used to set the pixels - // several times. Using a single-pixel line of length 'width' is a - // tradeoff between speed (setting each pixel individually is too slow) - // and memory (an array for width*height would waste lots of memory - // if it stayed resident, and would terrify the gc if it were - // re-created on each trip to background(). + // Create a small array that can be used to set the pixels several times. + // Using a single-pixel line of length 'width' is a tradeoff between + // speed (setting each pixel individually is too slow) and memory + // (an array for width*height would waste lots of memory if it stayed + // resident, and would terrify the gc if it were re-created on each trip + // to background(). WritableRaster raster = ((BufferedImage) image).getRaster(); if ((clearPixels == null) || (clearPixels.length < width)) { clearPixels = new int[width]; @@ -1221,7 +1100,7 @@ public class PGraphicsJava2D extends PGraphics2D { public void beginRaw(PGraphics recorderRaw) { - throw new RuntimeException("beginRaw() not available with this renderer"); + methodError("beginRaw"); } @@ -1262,22 +1141,12 @@ public class PGraphicsJava2D extends PGraphics2D { * update happens, in PGraphicsJava2D, this will happen immediately. */ public void updatePixels(int x, int y, int c, int d) { - if ((x == 0) && (y == 0) && (c == width) && (d == height)) { - updatePixels(); - } else { - throw new RuntimeException("updatePixels(x, y, c, d) not implemented"); + //if ((x == 0) && (y == 0) && (c == width) && (d == height)) { + if ((x != 0) || (y != 0) || (c != width) || (d != height)) { + // Show a warning message, but continue anyway. + variationError("updatePixels(x, y, w, h)"); } - /* - ((BufferedImage) image).setRGB(x, y, - (imageMode == CORNER) ? c : (c - x), - (imageMode == CORNER) ? d : (d - y), - pixels, 0, width); - WritableRaster raster = ((BufferedImage) image).getRaster(); - raster.setDataElements(x, y, - (imageMode == CORNER) ? c : (c - x), - (imageMode == CORNER) ? d : (d - y), - pixels); - */ + updatePixels(); } @@ -1380,12 +1249,12 @@ public class PGraphicsJava2D extends PGraphics2D { public void mask(int alpha[]) { - throw new RuntimeException("mask() cannot be used with JAVA2D"); + methodError("mask"); } public void mask(PImage alpha) { - throw new RuntimeException("mask() cannot be used with JAVA2D"); + methodError("mask"); } diff --git a/core/src/processing/core/PTriangle.java b/core/src/processing/core/PTriangle.java index 673a97324..0eefcf3d4 100644 --- a/core/src/processing/core/PTriangle.java +++ b/core/src/processing/core/PTriangle.java @@ -258,7 +258,7 @@ public class PTriangle implements PConstants //SCREEN_HEIGHT1 = SCREEN_HEIGHT-1; m_pixels = parent.pixels; - m_stencil = parent.stencil; +// m_stencil = parent.stencil; m_zbuffer = parent.zbuffer; noDepthTest = parent.hints[DISABLE_DEPTH_TEST]; diff --git a/opengl/src/processing/opengl/PGraphicsOpenGL.java b/opengl/src/processing/opengl/PGraphicsOpenGL.java index 3eafe8411..6712998da 100644 --- a/opengl/src/processing/opengl/PGraphicsOpenGL.java +++ b/opengl/src/processing/opengl/PGraphicsOpenGL.java @@ -1869,12 +1869,12 @@ public class PGraphicsOpenGL extends PGraphics3D { public void background(PImage bgimage) { - clear(); + backgroundImpl(); set(0, 0, bgimage); } - public void clear() { + public void backgroundImpl() { gl.glClearColor(backgroundR, backgroundG, backgroundB, 1); gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); diff --git a/todo.txt b/todo.txt index 0d0975ad3..d0993d441 100644 --- a/todo.txt +++ b/todo.txt @@ -90,6 +90,7 @@ X video capture problems with opengl X http://dev.processing.org/bugs/show_bug.cgi?id=882 + _ sketch must be saved to use a constructor _ http://dev.processing.org/bugs/show_bug.cgi?id=929