From afebba3bef949d07accd6ce436f0ec398fa3640c Mon Sep 17 00:00:00 2001 From: codeanticode Date: Mon, 5 Dec 2011 23:18:16 +0000 Subject: [PATCH] Reworked texture binding in PFontTexture --- .../src/processing/opengl/PFontTexture.java | 28 ++++---- .../processing/opengl/PGraphicsOpenGL.java | 66 +++++++++++-------- .../opengl/src/processing/opengl/PShader.java | 4 +- .../src/processing/opengl/PShape3D.java | 10 ++- 4 files changed, 63 insertions(+), 45 deletions(-) diff --git a/java/libraries/opengl/src/processing/opengl/PFontTexture.java b/java/libraries/opengl/src/processing/opengl/PFontTexture.java index 6f906105c..0175cf07a 100644 --- a/java/libraries/opengl/src/processing/opengl/PFontTexture.java +++ b/java/libraries/opengl/src/processing/opengl/PFontTexture.java @@ -3,6 +3,7 @@ /* Part of the Processing project - http://processing.org + Copyright (c) 2011 Andres Colubri Copyright (c) 2004-08 Ben Fry and Casey Reas This library is free software; you can redistribute it and/or @@ -184,25 +185,23 @@ class PFontTexture implements PConstants { public void setFirstTexture() { setTexture(0); } - + public void setTexture(int idx) { if (0 <= idx && idx < textures.length) { currentTex = idx; - textures[currentTex].bind(); } } - + public PImage getTexture(int idx) { if (0 <= idx && idx < images.length) { - currentTex = idx; - return images[currentTex]; + return images[idx]; } - return null; - + return null; } + // Add all the current glyphs to opengl texture. public void addAllGlyphsToTexture() { // loop over current glyphs. @@ -310,13 +309,9 @@ class PFontTexture implements PConstants { lastTex = 0; } - // We assume GL_TEXTURE_2D is enabled at this point. - // We reset texture when it was resized because even the - // texture index didn't change, the texture is a new one - // in fact, so we need to rebind. if (currentTex != lastTex || resized) { - setTexture(lastTex); - } + currentTex = idx; + } TextureInfo tinfo = new TextureInfo(currentTex, offsetX, offsetY, w, h, rgba); offsetX += w; @@ -341,6 +336,7 @@ class PFontTexture implements PConstants { public float v0, v1; public int[] pixels; + public TextureInfo(int tidx, int cropX, int cropY, int cropW, int cropH, int[] pix) { texIndex = tidx; crop = new int[4]; @@ -356,6 +352,7 @@ class PFontTexture implements PConstants { updateTex(); } + void updateUV() { width = textures[texIndex].glWidth; height = textures[texIndex].glHeight; @@ -365,8 +362,11 @@ class PFontTexture implements PConstants { v1 = v0 - (float)crop[3] / (float)height; } + void updateTex() { - textures[texIndex].setTexels(offsetX, crop[0] - 1, crop[1] + crop[3] - 1, crop[2] + 2, -crop[3] + 2, pixels); + textures[texIndex].bind(); + textures[texIndex].setTexels(offsetX, crop[0] - 1, crop[1] + crop[3] - 1, crop[2] + 2, -crop[3] + 2, pixels); + textures[texIndex].unbind(); } } } \ No newline at end of file diff --git a/java/libraries/opengl/src/processing/opengl/PGraphicsOpenGL.java b/java/libraries/opengl/src/processing/opengl/PGraphicsOpenGL.java index aa4ff9a4f..3efbff900 100644 --- a/java/libraries/opengl/src/processing/opengl/PGraphicsOpenGL.java +++ b/java/libraries/opengl/src/processing/opengl/PGraphicsOpenGL.java @@ -313,7 +313,7 @@ public class PGraphicsOpenGL extends PGraphics { // Blending: - protected int screenBlendMode; + protected int blendMode; // ........................................................ @@ -1370,7 +1370,7 @@ public class PGraphicsOpenGL extends PGraphics { } // Restoring blending. - blendMode(screenBlendMode); + blendMode(blendMode); // Restoring fill if (fill) { @@ -1610,7 +1610,7 @@ public class PGraphicsOpenGL extends PGraphics { tessellator.setInGeometry(in); tessellator.setTessGeometry(tess); - tessellator.setFill(fill); + tessellator.setFill(fill || textureImage != null); tessellator.setStroke(stroke); tessellator.setStrokeWeight(strokeWeight); tessellator.setStrokeCap(strokeCap); @@ -1702,7 +1702,7 @@ public class PGraphicsOpenGL extends PGraphics { fG = fillG; fB = fillB; fA = fillA; - } else { + } else { if (tint) { fR = tintR; fG = tintG; @@ -1735,6 +1735,11 @@ public class PGraphicsOpenGL extends PGraphics { if (textured && textureMode == IMAGE) { u /= textureImage.width; v /= textureImage.height; + + PTexture tex = getTexture(textureImage); + if (tex.isFlippedY()) { + v = 1 - v; + } } drawing2D &= PApplet.abs(z) < FLOAT_EPS; @@ -1784,7 +1789,6 @@ public class PGraphicsOpenGL extends PGraphics { // buffers. renderFill(textureImage); } - PApplet.println("Flushing"); } @@ -2748,18 +2752,20 @@ public class PGraphicsOpenGL extends PGraphics { textTex = new PFontTexture(parent, textFont, maxTextureSize, maxTextureSize); textFont.setCache(this, textTex); } + textTex.setFirstTexture(); // Saving style parameters modified by text rendering. - int mode0 = textureMode; - boolean stroke0 = stroke; - float nX0 = normalX; - float nY0 = normalY; - float nZ0 = normalZ; - boolean tint0 = tint; - float tR0 = tintR; - float tG0 = tintG; - float tB0 = tintB; - float tA0 = tintA; + int savedTextureMode = textureMode; + boolean savedStroke = stroke; + float savedNormalX = normalX; + float savedNormalY = normalY; + float savedNormalZ = normalZ; + boolean savedTint = tint; + float savedTintR = tintR; + float savedTintG = tintG; + float savedTintB = tintB; + float savedTintA = tintA; + int savedBlendMode = blendMode; // Setting style used in text rendering. textureMode = NORMAL; @@ -2772,20 +2778,22 @@ public class PGraphicsOpenGL extends PGraphics { tintG = fillG; tintB = fillB; tintA = fillA; + blendMode(BLEND); super.textLineImpl(buffer, start, stop, x, y); // Restoring original style. - textureMode = mode0; - stroke = stroke0; - normalX = nX0; - normalY = nY0; - normalZ = nZ0; - tint = tint0; - tintR = tR0; - tintG = tG0; - tintB = tB0; - tintA = tA0; + textureMode = savedTextureMode; + stroke = savedStroke; + normalX = savedNormalX; + normalY = savedNormalY; + normalZ = savedNormalZ; + tint = savedTint; + tintR = savedTintR; + tintG = savedTintG; + tintB = savedTintB; + tintA = savedTintA; + blendMode(savedBlendMode); } protected void textCharImpl(char ch, float x, float y) { @@ -2820,6 +2828,10 @@ public class PGraphicsOpenGL extends PGraphics { protected void textCharModelImpl(PFontTexture.TextureInfo info, float x0, float y0, float x1, float y1) { +// if (textTex.currentTex != info.texIndex) { +// textTex.setTexture(info.texIndex); +// } + PImage tex = textTex.getTexture(info.texIndex); beginShape(QUADS); @@ -5234,7 +5246,7 @@ public class PGraphicsOpenGL extends PGraphics { * http://www.pegtop.net/delphi/articles/blendmodes/ */ public void blendMode(int mode) { - screenBlendMode = mode; + blendMode = mode; gl.glEnable(GL.GL_BLEND); if (mode == REPLACE) { @@ -5416,7 +5428,7 @@ public class PGraphicsOpenGL extends PGraphics { gl.glBindTexture(target, 0); gl.glDisable(target); - blendMode(screenBlendMode); + blendMode(blendMode); } protected void drawTexture(int w, int h, int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2) { diff --git a/java/libraries/opengl/src/processing/opengl/PShader.java b/java/libraries/opengl/src/processing/opengl/PShader.java index 8cf052268..7130da682 100644 --- a/java/libraries/opengl/src/processing/opengl/PShader.java +++ b/java/libraries/opengl/src/processing/opengl/PShader.java @@ -486,8 +486,8 @@ public class PShader { byte[] infoBytes = new byte[length]; infoLog.get(infoBytes); - System.err.println(title); - System.err.println(new String(infoBytes)); + System.out.println(title); + System.out.println(new String(infoBytes)); } protected void release() { diff --git a/java/libraries/opengl/src/processing/opengl/PShape3D.java b/java/libraries/opengl/src/processing/opengl/PShape3D.java index 02c37fb88..d6f4e24cf 100644 --- a/java/libraries/opengl/src/processing/opengl/PShape3D.java +++ b/java/libraries/opengl/src/processing/opengl/PShape3D.java @@ -304,9 +304,15 @@ public class PShape3D extends PShape { currentVertex[0] = x; currentVertex[1] = y; currentVertex[2] = z; - + + if (texture != null) { + PTexture tex = ogl.getTexture(texture); + if (tex.isFlippedY()) { + v = 1 - v; + } + } currentTexcoord[0] = u; - currentTexcoord[1] = v; + currentTexcoord[1] = v; if (breakShape) { code = BREAK;