From fa513f807deffecee4e8370cc3c6e15f91c3549e Mon Sep 17 00:00:00 2001 From: codeanticode Date: Mon, 4 Apr 2016 23:53:20 -0400 Subject: [PATCH] ported latest gl changes from java mode, fixes #199 --- core/src/processing/opengl/FontTexture.java | 81 ++++++++----------- core/src/processing/opengl/PGL.java | 6 +- .../processing/opengl/PGraphicsOpenGL.java | 10 +-- core/src/processing/opengl/PShapeOpenGL.java | 22 ++++- 4 files changed, 62 insertions(+), 57 deletions(-) diff --git a/core/src/processing/opengl/FontTexture.java b/core/src/processing/opengl/FontTexture.java index 58fa64f83..c4c207b73 100644 --- a/core/src/processing/opengl/FontTexture.java +++ b/core/src/processing/opengl/FontTexture.java @@ -59,7 +59,6 @@ class FontTexture implements PConstants { protected int lineHeight; protected Texture[] textures = null; protected PImage[] images = null; - protected int currentTex; protected int lastTex; protected TextureInfo[] glyphTexinfos; protected HashMap texinfoMap; @@ -86,7 +85,6 @@ protected void dispose() { protected void initTexture(PGraphicsOpenGL pg, PFont font) { - currentTex = -1; lastTex = -1; int spow = PGL.nextPowerOfTwo(font.getSize()); @@ -117,10 +115,10 @@ public boolean addTexture(PGraphicsOpenGL pg) { boolean resize; w = maxSize; - if (-1 < currentTex && textures[currentTex].glHeight < maxSize) { + if (-1 < lastTex && textures[lastTex].glHeight < maxSize) { // 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, maxSize); + h = PApplet.min(2 * textures[lastTex].glHeight, maxSize); resize = true; } else { h = minSize; @@ -147,38 +145,37 @@ public boolean addTexture(PGraphicsOpenGL pg) { textures[0] = tex; images = new PImage[1]; images[0] = pg.wrapTexture(tex); - currentTex = 0; + lastTex = 0; } else if (resize) { // Replacing old smaller texture with larger one. // But first we must copy the contents of the older // texture into the new one. Setting blend mode to // REPLACE to preserve color of transparent pixels. - Texture tex0 = textures[currentTex]; + Texture tex0 = textures[lastTex]; tex.pg.pushStyle(); tex.pg.blendMode(REPLACE); tex.put(tex0); tex.pg.popStyle(); - textures[currentTex] = tex; + textures[lastTex] = tex; - pg.setCache(images[currentTex], tex); - images[currentTex].width = tex.width; - images[currentTex].height = tex.height; + pg.setCache(images[lastTex], tex); + images[lastTex].width = tex.width; + images[lastTex].height = tex.height; } else { // Adding new texture to the list. - Texture[] tempTex = textures; - textures = new Texture[textures.length + 1]; - PApplet.arrayCopy(tempTex, textures, tempTex.length); - textures[tempTex.length] = tex; - currentTex = textures.length - 1; - - PImage[] tempImg = images; - images = new PImage[textures.length]; - PApplet.arrayCopy(tempImg, images, tempImg.length); - images[tempImg.length] = pg.wrapTexture(tex); + lastTex = textures.length; + Texture[] tempTex = new Texture[lastTex + 1]; + PApplet.arrayCopy(textures, tempTex, textures.length); + tempTex[lastTex] = tex; + textures = tempTex; + + PImage[] tempImg = new PImage[textures.length]; + PApplet.arrayCopy(images, tempImg, images.length); + tempImg[lastTex] = pg.wrapTexture(tex); + images = tempImg; } - lastTex = currentTex; // Make sure that the current texture is bound. tex.bind(); @@ -188,7 +185,6 @@ public boolean addTexture(PGraphicsOpenGL pg) { public void begin() { - setTexture(0); } @@ -199,23 +195,8 @@ public void end() { } - public void setTexture(int idx) { - if (0 <= idx && idx < textures.length) { - currentTex = idx; - } - } - - - public PImage getTexture(int idx) { - if (0 <= idx && idx < images.length) { - return images[idx]; - } - return null; - } - - - public PImage getCurrentTexture() { - return getTexture(currentTex); + public PImage getTexture(TextureInfo info) { + return images[info.texIndex]; } @@ -232,7 +213,7 @@ 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 == lastTex) { tinfo.updateUV(); } } @@ -265,14 +246,19 @@ public boolean contextIsOutdated() { if (outdated) { for (int i = 0; i < textures.length; i++) { textures[i].dispose(); -// PGraphicsOpenGL.removeTextureObject(textures[i].glName, -// textures[i].context); -// textures[i].glName = 0; } } return outdated; } +// public void draw() { +// Texture tex = textures[lastTex]; +// pgl.drawTexture(tex.glTarget, tex.glName, +// tex.glWidth, tex.glHeight, +// 0, 0, tex.glWidth, tex.glHeight); +// } + + // Adds this glyph to the opengl texture in PFont. protected void addToTexture(PGraphicsOpenGL pg, int idx, PFont.Glyph glyph) { // We add one pixel to avoid issues when sampling the font texture at @@ -316,16 +302,15 @@ protected void addToTexture(PGraphicsOpenGL pg, int idx, PFont.Glyph glyph) { } // Is there room for this glyph in the current line? - if (offsetX + w > textures[currentTex].glWidth) { + if (offsetX + w > textures[lastTex].glWidth) { // No room, go to the next line: offsetX = 0; offsetY += lineHeight; - lineHeight = 0; } lineHeight = Math.max(lineHeight, h); boolean resized = false; - if (offsetY + lineHeight > textures[currentTex].glHeight) { + if (offsetY + lineHeight > textures[lastTex].glHeight) { // We run out of space in the current texture, so we add a new texture: resized = addTexture(pg); if (resized) { @@ -341,8 +326,7 @@ protected void addToTexture(PGraphicsOpenGL pg, int idx, PFont.Glyph glyph) { } } - TextureInfo tinfo = new TextureInfo(currentTex, offsetX, offsetY, - w, h, rgba); + TextureInfo tinfo = new TextureInfo(lastTex, offsetX, offsetY, w, h, rgba); offsetX += w; if (idx == glyphTexinfos.length) { @@ -385,6 +369,7 @@ class TextureInfo { void updateUV() { width = textures[texIndex].glWidth; 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; diff --git a/core/src/processing/opengl/PGL.java b/core/src/processing/opengl/PGL.java index 70fb2a7eb..8b471751c 100644 --- a/core/src/processing/opengl/PGL.java +++ b/core/src/processing/opengl/PGL.java @@ -2097,7 +2097,11 @@ protected boolean hasNpotTexSupport() { int major = getGLVersion()[0]; if (major < 3) { String ext = getString(EXTENSIONS); - return -1 < ext.indexOf("_texture_non_power_of_two"); + if (isES()) { + return -1 < ext.indexOf("_texture_npot"); + } else { + return -1 < ext.indexOf("_texture_non_power_of_two"); + } } else { return true; } diff --git a/core/src/processing/opengl/PGraphicsOpenGL.java b/core/src/processing/opengl/PGraphicsOpenGL.java index 9cadc1861..13784004f 100644 --- a/core/src/processing/opengl/PGraphicsOpenGL.java +++ b/core/src/processing/opengl/PGraphicsOpenGL.java @@ -683,13 +683,14 @@ public void requestDraw() { } -/* + /* @Override // Java only public PSurface createSurface() { // ignore return surface = new PSurfaceJOGL(this); } -*/ + */ + public boolean saveImpl(String filename) { return super.save(filename); // ASYNC save frame using PBOs not yet available on Android @@ -3629,11 +3630,8 @@ protected void textCharImpl(char ch, float x, float y) { protected void textCharModelImpl(FontTexture.TextureInfo info, float x0, float y0, float x1, float y1) { - if (textTex.currentTex != info.texIndex) { - textTex.setTexture(info.texIndex); - } beginShape(QUADS); - texture(textTex.getCurrentTexture()); + texture(textTex.getTexture(info)); vertex(x0, y0, info.u0, info.v0); vertex(x1, y0, info.u1, info.v0); vertex(x1, y1, info.u1, info.v1); diff --git a/core/src/processing/opengl/PShapeOpenGL.java b/core/src/processing/opengl/PShapeOpenGL.java index cb6197188..45811bbaf 100644 --- a/core/src/processing/opengl/PShapeOpenGL.java +++ b/core/src/processing/opengl/PShapeOpenGL.java @@ -2013,13 +2013,31 @@ public void setStroke(boolean stroke) { PShapeOpenGL child = (PShapeOpenGL) children[i]; child.setStroke(stroke); } - } else if (this.stroke != stroke) { + this.stroke = stroke; + } else { + setStrokeImpl(stroke); + } + } + + + protected void setStrokeImpl(boolean stroke) { + if (this.stroke != stroke) { + if (stroke) { + // Before there was no stroke, now there is stroke, so current stroke + // color should be copied to the input geometry, and geometry should + // be marked as modified in case it needs to be re-tessellated. + int color = strokeColor; + strokeColor += 1; // Forces a color change + setStrokeImpl(color); + } + markForTessellation(); if (is2D() && parent != null) { ((PShapeOpenGL)parent).strokedTexture(stroke && image != null); } + + this.stroke = stroke; } - this.stroke = stroke; }