Skip to content

Commit

Permalink
ported latest gl changes from java mode, fixes #199
Browse files Browse the repository at this point in the history
  • Loading branch information
codeanticode committed Apr 5, 2016
1 parent a432424 commit fa513f8
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 57 deletions.
81 changes: 33 additions & 48 deletions core/src/processing/opengl/FontTexture.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<PFont.Glyph, TextureInfo> texinfoMap;
Expand All @@ -86,7 +85,6 @@ protected void dispose() {


protected void initTexture(PGraphicsOpenGL pg, PFont font) {
currentTex = -1;
lastTex = -1;

int spow = PGL.nextPowerOfTwo(font.getSize());
Expand Down Expand Up @@ -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;
Expand All @@ -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();
Expand All @@ -188,7 +185,6 @@ public boolean addTexture(PGraphicsOpenGL pg) {


public void begin() {
setTexture(0);
}


Expand All @@ -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];
}


Expand All @@ -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();
}
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 5 additions & 1 deletion core/src/processing/opengl/PGL.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
10 changes: 4 additions & 6 deletions core/src/processing/opengl/PGraphicsOpenGL.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
22 changes: 20 additions & 2 deletions core/src/processing/opengl/PShapeOpenGL.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}


Expand Down

0 comments on commit fa513f8

Please sign in to comment.