From bae7fa5243287a92ee44f42a1b82c7e21b64531a Mon Sep 17 00:00:00 2001 From: benfry Date: Sun, 22 Jul 2012 00:19:48 +0000 Subject: [PATCH] more with method naming, add XML hasChildren() method --- core/src/processing/core/PApplet.java | 13 +- core/src/processing/core/PGraphics.java | 12 ++ core/src/processing/data/XML.java | 8 +- core/src/processing/opengl/PFontTexture.java | 201 +++++++++--------- .../processing/opengl/PGraphicsOpenGL.java | 8 +- core/todo.txt | 6 +- 6 files changed, 139 insertions(+), 109 deletions(-) diff --git a/core/src/processing/core/PApplet.java b/core/src/processing/core/PApplet.java index 6d712bef5..b391a8bce 100644 --- a/core/src/processing/core/PApplet.java +++ b/core/src/processing/core/PApplet.java @@ -24,7 +24,7 @@ package processing.core; import processing.data.*; -import processing.opengl.PShader; +import processing.opengl.*; import java.applet.*; import java.awt.*; @@ -9758,6 +9758,17 @@ public class PApplet extends Applet // public functions for processing.core + public PGL beginGL() { + return g.beginGL(); + } + + + public void endGL() { + if (recorder != null) recorder.endGL(); + g.endGL(); + } + + public void flush() { if (recorder != null) recorder.flush(); g.flush(); diff --git a/core/src/processing/core/PGraphics.java b/core/src/processing/core/PGraphics.java index 0fb3c52bf..e7974be06 100644 --- a/core/src/processing/core/PGraphics.java +++ b/core/src/processing/core/PGraphics.java @@ -27,6 +27,7 @@ package processing.core; import java.awt.*; import java.util.HashMap; +import processing.opengl.PGL; import processing.opengl.PShader; /** @@ -719,6 +720,17 @@ public class PGraphics extends PImage implements PConstants { } + public PGL beginGL() { + showMethodWarning("beginGL"); + return null; + } + + + public void endGL() { + showMethodWarning("endGL"); + } + + public void flush() { // no-op, mostly for P3D to write sorted stuff } diff --git a/core/src/processing/data/XML.java b/core/src/processing/data/XML.java index 60cabfe03..004874c2c 100644 --- a/core/src/processing/data/XML.java +++ b/core/src/processing/data/XML.java @@ -72,7 +72,7 @@ public class XML implements Serializable { this(PApplet.createReader(file)); } - + public XML(Reader reader) { try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); @@ -240,6 +240,12 @@ public class XML implements Serializable { } + public boolean hasChildren() { + checkChildren(); + return children.length > 0; + } + + /** * Put the names of all children into an array. Same as looping through * each child and calling getName() on each XMLElement. diff --git a/core/src/processing/opengl/PFontTexture.java b/core/src/processing/opengl/PFontTexture.java index 247fdec32..c39681513 100644 --- a/core/src/processing/opengl/PFontTexture.java +++ b/core/src/processing/opengl/PFontTexture.java @@ -31,26 +31,25 @@ import processing.core.PImage; import java.util.HashMap; /** - * All the infrastructure needed for optimized font rendering + * All the infrastructure needed for optimized font rendering * in OpenGL. Basically, this special class is needed because * fonts in Processing are handled by a separate PImage for each - * gyph. For performance reasons, all these glyphs should be - * stored in a single OpenGL texture (otherwise, rendering a - * string of text would involve binding and unbinding several - * textures. - * PFontTexture manages the correspondence between individual + * glyph. For performance reasons, all these glyphs should be + * stored in a single OpenGL texture (otherwise, rendering a + * string of text would involve binding and un-binding several + * textures. + * PFontTexture manages the correspondence between individual * glyphs and the large OpenGL texture containing them. Also, - * in the case that the font size is very large, one single + * in the case that the font size is very large, one single * OpenGL texture might not be enough to store all the glyphs, * so PFontTexture also takes care of spreading a single font * over several textures. - * By Andres Colubri - * + * @author Andres Colubri */ class PFontTexture implements PConstants { protected PApplet parent; protected PGraphicsOpenGL pg; - protected PGL pgl; + protected PGL pgl; protected PFont font; protected boolean is3D; @@ -62,53 +61,53 @@ class PFontTexture implements PConstants { protected Texture[] textures = null; protected PImage[] images = null; protected int currentTex; - protected int lastTex; - protected TextureInfo[] glyphTexinfos; - protected HashMap texinfoMap; - + protected int lastTex; + protected TextureInfo[] glyphTexinfos; + protected HashMap texinfoMap; + public PFontTexture(PApplet parent, PFont font, int maxw, int maxh, boolean is3D) { this.parent = parent; - this.font = font; + this.font = font; pg = (PGraphicsOpenGL)parent.g; pgl = pg.pgl; this.is3D = is3D; - + initTexture(maxw, maxh); - } - - - protected void allocate() { + } + + + protected void allocate() { // Nothing to do here: the font textures will allocate // themselves. } - - + + protected void initTexture(int w, int h) { maxTexWidth = w; maxTexHeight = h; - + currentTex = -1; lastTex = -1; - + addTexture(); - + offsetX = 0; offsetY = 0; lineHeight = 0; - + texinfoMap = new HashMap(); glyphTexinfos = new TextureInfo[font.getGlyphCount()]; addAllGlyphsToTexture(); } - - + + public boolean addTexture() { int w, h; boolean resize; - + w = maxTexWidth; if (-1 < currentTex && textures[currentTex].glHeight < maxTexHeight) { - // The height of the current texture is less than the maximum, this + // The height of the current texture is less than the maximum, this // means we can replace it with a larger texture. h = PApplet.min(2 * textures[currentTex].glHeight, maxTexHeight); resize = true; @@ -116,7 +115,7 @@ class PFontTexture implements PConstants { h = PApplet.min(PGraphicsOpenGL.maxTextureSize, PGL.MAX_FONT_TEX_SIZE / 2, maxTexHeight / 4); resize = false; } - + Texture tex; if (is3D) { // Bilinear sampling ensures that the texture doesn't look pixelated either @@ -132,9 +131,9 @@ class PFontTexture implements PConstants { if (textures == null) { textures = new Texture[1]; textures[0] = tex; - images = new PImage[1]; - images[0] = pg.wrapTexture(tex); - currentTex = 0; + images = new PImage[1]; + images[0] = pg.wrapTexture(tex); + currentTex = 0; } else if (resize) { // Replacing old smaller texture with larger one. // But first we must copy the contents of the older @@ -142,7 +141,7 @@ class PFontTexture implements PConstants { Texture tex0 = textures[currentTex]; tex.put(tex0); textures[currentTex] = tex; - + images[currentTex].setCache(pg, tex); images[currentTex].width = tex.width; images[currentTex].height = tex.height; @@ -153,46 +152,46 @@ class PFontTexture implements PConstants { PApplet.arrayCopy(tempTex, textures, tempTex.length); textures[tempTex.length] = tex; currentTex = textures.length - 1; - + PImage[] tempImg = images; images = new PImage[textures.length + 1]; - PApplet.arrayCopy(tempImg, images, tempImg.length); + PApplet.arrayCopy(tempImg, images, tempImg.length); images[tempImg.length] = pg.wrapTexture(tex); } lastTex = currentTex; - + // Make sure that the current texture is bound. //tex.bind(); - + return resize; } - - + + public void setFirstTexture() { setTexture(0); } - - + + public void setTexture(int idx) { - if (0 <= idx && idx < textures.length) { + if (0 <= idx && idx < textures.length) { currentTex = idx; } } - - + + public PImage getTexture(int idx) { - if (0 <= idx && idx < images.length) { + if (0 <= idx && idx < images.length) { return images[idx]; - } - return null; - } - - - public PImage getCurrentTexture() { - return getTexture(currentTex); + } + return null; } - + + public PImage getCurrentTexture() { + return getTexture(currentTex); + } + + // Add all the current glyphs to opengl texture. public void addAllGlyphsToTexture() { // loop over current glyphs. @@ -201,33 +200,33 @@ class PFontTexture implements PConstants { } } - + public void updateGlyphsTexCoords() { // loop over current glyphs. for (int i = 0; i < glyphTexinfos.length; i++) { TextureInfo tinfo = glyphTexinfos[i]; - if (tinfo != null && tinfo.texIndex == currentTex) { + if (tinfo != null && tinfo.texIndex == currentTex) { tinfo.updateUV(); - } + } } } - - + + public TextureInfo getTexInfo(PFont.Glyph glyph) { TextureInfo info = texinfoMap.get(glyph); return info; } - - + + public TextureInfo addToTexture(PFont.Glyph glyph) { int n = glyphTexinfos.length; if (n == 0) { glyphTexinfos = new TextureInfo[1]; - } - addToTexture(n, glyph); + } + addToTexture(n, glyph); return glyphTexinfos[n]; - } - + } + public boolean contextIsOutdated() { boolean outdated = false; @@ -243,13 +242,13 @@ class PFontTexture implements PConstants { } } return outdated; - } - + } + // Adds this glyph to the opengl texture in PFont. protected void addToTexture(int idx, PFont.Glyph glyph) { // We add one pixel to avoid issues when sampling the font texture at fractional - // screen positions. I.e.: the pixel on the screen only contains half of the - // font rectangle, so it would sample half of the color from the glyph + // screen positions. I.e.: the pixel on the screen only contains half of the + // font rectangle, so it would sample half of the color from the glyph // area in the texture, and the other half from the contiguous pixel. If the // later contains a portion of the neighbor glyph and the former doesn't, this // would result in a shaded pixel when the correct output is blank. @@ -257,14 +256,14 @@ class PFontTexture implements PConstants { // bilinear sampling. int w = 1 + glyph.width + 1; int h = 1 + glyph.height + 1; - - // Converting the pixels array from the PImage into a valid RGBA array for OpenGL. + + // Converting the pixels array from the PImage into a valid RGBA array for OpenGL. int[] rgba = new int[w * h]; int t = 0; int p = 0; - if (PGL.BIG_ENDIAN) { + if (PGL.BIG_ENDIAN) { java.util.Arrays.fill(rgba, 0, w, 0xFFFFFF00); // Set the first row to blank pixels. - t = w; + t = w; for (int y = 0; y < glyph.height; y++) { rgba[t++] = 0xFFFFFF00; // Set the leftmost pixel in this row as blank for (int x = 0; x < glyph.width; x++) { @@ -275,7 +274,7 @@ class PFontTexture implements PConstants { java.util.Arrays.fill(rgba, (h - 1) * w, h * w, 0xFFFFFF00); // Set the last row to blank pixels. } else { java.util.Arrays.fill(rgba, 0, w, 0x00FFFFFF); // Set the first row to blank pixels. - t = w; + t = w; for (int y = 0; y < glyph.height; y++) { rgba[t++] = 0x00FFFFFF; // Set the leftmost pixel in this row as blank for (int x = 0; x < glyph.width; x++) { @@ -285,7 +284,7 @@ class PFontTexture implements PConstants { } java.util.Arrays.fill(rgba, (h - 1) * w, h * w, 0x00FFFFFF); // Set the last row to blank pixels. } - + // Is there room for this glyph in the current line? if (offsetX + w > textures[currentTex].glWidth) { // No room, go to the next line: @@ -294,61 +293,61 @@ class PFontTexture implements PConstants { lineHeight = 0; } lineHeight = Math.max(lineHeight, h); - + boolean resized = false; - if (offsetY + lineHeight > textures[currentTex].glHeight) { + if (offsetY + lineHeight > textures[currentTex].glHeight) { // We run out of space in the current texture, so we add a new texture: resized = addTexture(); if (resized) { - // Because the current texture has been resized, we need to + // Because the current texture has been resized, we need to // update the UV coordinates of all the glyphs associated to it: updateGlyphsTexCoords(); } else { - // A new texture has been created. Reseting texture coordinates + // A new texture has been created. Reseting texture coordinates // and line. offsetX = 0; offsetY = 0; lineHeight = 0; } } - - if (lastTex == -1) { + + if (lastTex == -1) { lastTex = 0; } - + if (currentTex != lastTex || resized) { - currentTex = idx; - } - + currentTex = idx; + } + TextureInfo tinfo = new TextureInfo(currentTex, offsetX, offsetY, w, h, rgba); offsetX += w; - + if (idx == glyphTexinfos.length) { TextureInfo[] temp = new TextureInfo[glyphTexinfos.length + 1]; System.arraycopy(glyphTexinfos, 0, temp, 0, glyphTexinfos.length); glyphTexinfos = temp; } - - glyphTexinfos[idx] = tinfo; + + glyphTexinfos[idx] = tinfo; texinfoMap.put(glyph, tinfo); } - + public class TextureInfo { public int texIndex; public int width; - public int height; + public int height; public int[] crop; public float u0, u1; public float v0, v1; public int[] pixels; - + public TextureInfo(int tidx, int cropX, int cropY, int cropW, int cropH, int[] pix) { - texIndex = tidx; + texIndex = tidx; crop = new int[4]; - // The region of the texture corresponding to the glyph is surrounded by a + // The region of the texture corresponding to the glyph is surrounded by a // 1-pixel wide border to avoid artifacts due to bilinear sampling. This is - // why the additions and subtractions to the crop values. + // why the additions and subtractions to the crop values. crop[0] = cropX + 1; crop[1] = cropY + 1 + cropH - 2; crop[2] = cropW - 2; @@ -358,19 +357,19 @@ class PFontTexture implements PConstants { updateTex(); } - + void updateUV() { width = textures[texIndex].glWidth; - height = textures[texIndex].glHeight; + height = textures[texIndex].glHeight; u0 = (float)crop[0] / (float)width; u1 = u0 + (float)crop[2] / (float)width; v0 = (float)(crop[1] + crop[3]) / (float)height; - v1 = v0 - (float)crop[3] / (float)height; + v1 = v0 - (float)crop[3] / (float)height; } - - + + void updateTex() { - textures[texIndex].setNative(pixels, 0, crop[0] - 1, crop[1] + crop[3] - 1, crop[2] + 2, -crop[3] + 2); + textures[texIndex].setNative(pixels, 0, crop[0] - 1, crop[1] + crop[3] - 1, crop[2] + 2, -crop[3] + 2); } } } \ No newline at end of file diff --git a/core/src/processing/opengl/PGraphicsOpenGL.java b/core/src/processing/opengl/PGraphicsOpenGL.java index 0b3f383fa..3b748994c 100644 --- a/core/src/processing/opengl/PGraphicsOpenGL.java +++ b/core/src/processing/opengl/PGraphicsOpenGL.java @@ -1729,18 +1729,18 @@ public class PGraphicsOpenGL extends PGraphics { } - public PGL beginPGL() { + public PGL beginGL() { flush(); return pgl; } - public void endPGL() { + public void endGL() { restoreGL(); } - public void restartPGL() { + protected void restartPGL() { pgl.initialized = false; } @@ -5272,7 +5272,7 @@ public class PGraphicsOpenGL extends PGraphics { } - public void filter(Object shader) { + public void filter(PShader shader) { if (!(shader instanceof PolyTexShader)) { PGraphics.showWarning("Object is not a valid shader"); return; diff --git a/core/todo.txt b/core/todo.txt index 3d10433a6..00538addf 100644 --- a/core/todo.txt +++ b/core/todo.txt @@ -1,8 +1,12 @@ 0206 core (2.0a7) X change appletViewer back to 'online' +X begin/endGL added to PGraphics/PApplet +X add hasChildren() to XML library +X http://code.google.com/p/processing/issues/detail?id=1045 _ displayWidth/Height not being set properly before setup() _ http://code.google.com/p/processing/issues/detail?id=1120 +X can't reproduce.. might be Java update, or multi-display issue? constants/hints _ bring PConstants back in line w/ previous 1.5 (can't renumber) @@ -24,8 +28,6 @@ _ NEAREST, BILINEAR, BICUBIC, or 0, 2, 4? (need 8x too, so maybe numbers) _ final decision on pg.setQuality(sketchQuality()) _ should probably be setQuality(parent.sketchQuality()) -_ add hasChildren() to XML library? -_ http://code.google.com/p/processing/issues/detail?id=1045 X test what happens with NPE in OpenGL, does the error make sense? _ nope, it doesn't, need to clean this up