Skip to content

Commit fa513f8

Browse files
committed
ported latest gl changes from java mode, fixes #199
1 parent a432424 commit fa513f8

File tree

4 files changed

+62
-57
lines changed

4 files changed

+62
-57
lines changed

core/src/processing/opengl/FontTexture.java

Lines changed: 33 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ class FontTexture implements PConstants {
5959
protected int lineHeight;
6060
protected Texture[] textures = null;
6161
protected PImage[] images = null;
62-
protected int currentTex;
6362
protected int lastTex;
6463
protected TextureInfo[] glyphTexinfos;
6564
protected HashMap<PFont.Glyph, TextureInfo> texinfoMap;
@@ -86,7 +85,6 @@ protected void dispose() {
8685

8786

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

9290
int spow = PGL.nextPowerOfTwo(font.getSize());
@@ -117,10 +115,10 @@ public boolean addTexture(PGraphicsOpenGL pg) {
117115
boolean resize;
118116

119117
w = maxSize;
120-
if (-1 < currentTex && textures[currentTex].glHeight < maxSize) {
118+
if (-1 < lastTex && textures[lastTex].glHeight < maxSize) {
121119
// The height of the current texture is less than the maximum, this
122120
// means we can replace it with a larger texture.
123-
h = PApplet.min(2 * textures[currentTex].glHeight, maxSize);
121+
h = PApplet.min(2 * textures[lastTex].glHeight, maxSize);
124122
resize = true;
125123
} else {
126124
h = minSize;
@@ -147,38 +145,37 @@ public boolean addTexture(PGraphicsOpenGL pg) {
147145
textures[0] = tex;
148146
images = new PImage[1];
149147
images[0] = pg.wrapTexture(tex);
150-
currentTex = 0;
148+
lastTex = 0;
151149
} else if (resize) {
152150
// Replacing old smaller texture with larger one.
153151
// But first we must copy the contents of the older
154152
// texture into the new one. Setting blend mode to
155153
// REPLACE to preserve color of transparent pixels.
156-
Texture tex0 = textures[currentTex];
154+
Texture tex0 = textures[lastTex];
157155

158156
tex.pg.pushStyle();
159157
tex.pg.blendMode(REPLACE);
160158
tex.put(tex0);
161159
tex.pg.popStyle();
162160

163-
textures[currentTex] = tex;
161+
textures[lastTex] = tex;
164162

165-
pg.setCache(images[currentTex], tex);
166-
images[currentTex].width = tex.width;
167-
images[currentTex].height = tex.height;
163+
pg.setCache(images[lastTex], tex);
164+
images[lastTex].width = tex.width;
165+
images[lastTex].height = tex.height;
168166
} else {
169167
// Adding new texture to the list.
170-
Texture[] tempTex = textures;
171-
textures = new Texture[textures.length + 1];
172-
PApplet.arrayCopy(tempTex, textures, tempTex.length);
173-
textures[tempTex.length] = tex;
174-
currentTex = textures.length - 1;
175-
176-
PImage[] tempImg = images;
177-
images = new PImage[textures.length];
178-
PApplet.arrayCopy(tempImg, images, tempImg.length);
179-
images[tempImg.length] = pg.wrapTexture(tex);
168+
lastTex = textures.length;
169+
Texture[] tempTex = new Texture[lastTex + 1];
170+
PApplet.arrayCopy(textures, tempTex, textures.length);
171+
tempTex[lastTex] = tex;
172+
textures = tempTex;
173+
174+
PImage[] tempImg = new PImage[textures.length];
175+
PApplet.arrayCopy(images, tempImg, images.length);
176+
tempImg[lastTex] = pg.wrapTexture(tex);
177+
images = tempImg;
180178
}
181-
lastTex = currentTex;
182179

183180
// Make sure that the current texture is bound.
184181
tex.bind();
@@ -188,7 +185,6 @@ public boolean addTexture(PGraphicsOpenGL pg) {
188185

189186

190187
public void begin() {
191-
setTexture(0);
192188
}
193189

194190

@@ -199,23 +195,8 @@ public void end() {
199195
}
200196

201197

202-
public void setTexture(int idx) {
203-
if (0 <= idx && idx < textures.length) {
204-
currentTex = idx;
205-
}
206-
}
207-
208-
209-
public PImage getTexture(int idx) {
210-
if (0 <= idx && idx < images.length) {
211-
return images[idx];
212-
}
213-
return null;
214-
}
215-
216-
217-
public PImage getCurrentTexture() {
218-
return getTexture(currentTex);
198+
public PImage getTexture(TextureInfo info) {
199+
return images[info.texIndex];
219200
}
220201

221202

@@ -232,7 +213,7 @@ public void updateGlyphsTexCoords() {
232213
// loop over current glyphs.
233214
for (int i = 0; i < glyphTexinfos.length; i++) {
234215
TextureInfo tinfo = glyphTexinfos[i];
235-
if (tinfo != null && tinfo.texIndex == currentTex) {
216+
if (tinfo != null && tinfo.texIndex == lastTex) {
236217
tinfo.updateUV();
237218
}
238219
}
@@ -265,14 +246,19 @@ public boolean contextIsOutdated() {
265246
if (outdated) {
266247
for (int i = 0; i < textures.length; i++) {
267248
textures[i].dispose();
268-
// PGraphicsOpenGL.removeTextureObject(textures[i].glName,
269-
// textures[i].context);
270-
// textures[i].glName = 0;
271249
}
272250
}
273251
return outdated;
274252
}
275253

254+
// public void draw() {
255+
// Texture tex = textures[lastTex];
256+
// pgl.drawTexture(tex.glTarget, tex.glName,
257+
// tex.glWidth, tex.glHeight,
258+
// 0, 0, tex.glWidth, tex.glHeight);
259+
// }
260+
261+
276262
// Adds this glyph to the opengl texture in PFont.
277263
protected void addToTexture(PGraphicsOpenGL pg, int idx, PFont.Glyph glyph) {
278264
// 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) {
316302
}
317303

318304
// Is there room for this glyph in the current line?
319-
if (offsetX + w > textures[currentTex].glWidth) {
305+
if (offsetX + w > textures[lastTex].glWidth) {
320306
// No room, go to the next line:
321307
offsetX = 0;
322308
offsetY += lineHeight;
323-
lineHeight = 0;
324309
}
325310
lineHeight = Math.max(lineHeight, h);
326311

327312
boolean resized = false;
328-
if (offsetY + lineHeight > textures[currentTex].glHeight) {
313+
if (offsetY + lineHeight > textures[lastTex].glHeight) {
329314
// We run out of space in the current texture, so we add a new texture:
330315
resized = addTexture(pg);
331316
if (resized) {
@@ -341,8 +326,7 @@ protected void addToTexture(PGraphicsOpenGL pg, int idx, PFont.Glyph glyph) {
341326
}
342327
}
343328

344-
TextureInfo tinfo = new TextureInfo(currentTex, offsetX, offsetY,
345-
w, h, rgba);
329+
TextureInfo tinfo = new TextureInfo(lastTex, offsetX, offsetY, w, h, rgba);
346330
offsetX += w;
347331

348332
if (idx == glyphTexinfos.length) {
@@ -385,6 +369,7 @@ class TextureInfo {
385369
void updateUV() {
386370
width = textures[texIndex].glWidth;
387371
height = textures[texIndex].glHeight;
372+
388373
u0 = (float)crop[0] / (float)width;
389374
u1 = u0 + (float)crop[2] / (float)width;
390375
v0 = (float)(crop[1] + crop[3]) / (float)height;

core/src/processing/opengl/PGL.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2097,7 +2097,11 @@ protected boolean hasNpotTexSupport() {
20972097
int major = getGLVersion()[0];
20982098
if (major < 3) {
20992099
String ext = getString(EXTENSIONS);
2100-
return -1 < ext.indexOf("_texture_non_power_of_two");
2100+
if (isES()) {
2101+
return -1 < ext.indexOf("_texture_npot");
2102+
} else {
2103+
return -1 < ext.indexOf("_texture_non_power_of_two");
2104+
}
21012105
} else {
21022106
return true;
21032107
}

core/src/processing/opengl/PGraphicsOpenGL.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -683,13 +683,14 @@ public void requestDraw() {
683683
}
684684

685685

686-
/*
686+
/*
687687
@Override
688688
// Java only
689689
public PSurface createSurface() { // ignore
690690
return surface = new PSurfaceJOGL(this);
691691
}
692-
*/
692+
*/
693+
693694

694695
public boolean saveImpl(String filename) {
695696
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) {
36293630
protected void textCharModelImpl(FontTexture.TextureInfo info,
36303631
float x0, float y0,
36313632
float x1, float y1) {
3632-
if (textTex.currentTex != info.texIndex) {
3633-
textTex.setTexture(info.texIndex);
3634-
}
36353633
beginShape(QUADS);
3636-
texture(textTex.getCurrentTexture());
3634+
texture(textTex.getTexture(info));
36373635
vertex(x0, y0, info.u0, info.v0);
36383636
vertex(x1, y0, info.u1, info.v0);
36393637
vertex(x1, y1, info.u1, info.v1);

core/src/processing/opengl/PShapeOpenGL.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2013,13 +2013,31 @@ public void setStroke(boolean stroke) {
20132013
PShapeOpenGL child = (PShapeOpenGL) children[i];
20142014
child.setStroke(stroke);
20152015
}
2016-
} else if (this.stroke != stroke) {
2016+
this.stroke = stroke;
2017+
} else {
2018+
setStrokeImpl(stroke);
2019+
}
2020+
}
2021+
2022+
2023+
protected void setStrokeImpl(boolean stroke) {
2024+
if (this.stroke != stroke) {
2025+
if (stroke) {
2026+
// Before there was no stroke, now there is stroke, so current stroke
2027+
// color should be copied to the input geometry, and geometry should
2028+
// be marked as modified in case it needs to be re-tessellated.
2029+
int color = strokeColor;
2030+
strokeColor += 1; // Forces a color change
2031+
setStrokeImpl(color);
2032+
}
2033+
20172034
markForTessellation();
20182035
if (is2D() && parent != null) {
20192036
((PShapeOpenGL)parent).strokedTexture(stroke && image != null);
20202037
}
2038+
2039+
this.stroke = stroke;
20212040
}
2022-
this.stroke = stroke;
20232041
}
20242042

20252043

0 commit comments

Comments
 (0)