Skip to content

Commit b92dc04

Browse files
committed
some cleaning up, handle edges
1 parent 1505abe commit b92dc04

1 file changed

Lines changed: 59 additions & 79 deletions

File tree

core/src/processing/opengl/Texture.java

Lines changed: 59 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@
2424
import processing.core.PApplet;
2525
import processing.core.PConstants;
2626
import processing.core.PGraphics;
27+
2728
import java.lang.reflect.Method;
2829
import java.nio.ByteBuffer;
2930
import java.nio.IntBuffer;
31+
import java.util.Arrays;
3032
import java.util.LinkedList;
3133
import java.util.NoSuchElementException;
3234

@@ -100,6 +102,10 @@ public class Texture implements PConstants {
100102

101103
protected int[] rgbaPixels = null;
102104
protected IntBuffer pixelBuffer = null;
105+
106+
protected int[] edgePixels = null;
107+
protected IntBuffer edgeBuffer = null;
108+
103109
protected FrameBuffer tempFbo = null;
104110
protected int pixBufUpdateCount = 0;
105111
protected int rgbaPixUpdateCount = 0;
@@ -340,82 +346,20 @@ public void set(int[] pixels, int x, int y, int w, int h, int format) {
340346
}
341347
pgl.bindTexture(glTarget, glName);
342348

349+
loadPixels(w * h);
350+
convertToRGBA(pixels, format, w, h);
351+
updatePixelBuffer(rgbaPixels);
352+
pgl.texSubImage2D(glTarget, 0, x, y, w, h, PGL.RGBA, PGL.UNSIGNED_BYTE,
353+
pixelBuffer);
354+
fillEdges(x, y, w, h);
355+
343356
if (usingMipmaps) {
344357
if (PGraphicsOpenGL.autoMipmapGenSupported) {
345-
// Automatic mipmap generation.
346-
loadPixels(w * h);
347-
convertToRGBA(pixels, format, w, h);
348-
updatePixelBuffer(rgbaPixels);
349-
pgl.texSubImage2D(glTarget, 0, x, y, w, h, PGL.RGBA, PGL.UNSIGNED_BYTE,
350-
pixelBuffer);
351358
pgl.generateMipmap(glTarget);
352359
} else {
353-
// TODO: finish manual mipmap generation, replacing Bitmap with AWT's BufferedImage,
354-
// making it work in npot textures (embed npot tex into larger pot tex?), subregions,
355-
// and moving GLUtils.texImage2D (originally from Android SDK) into PGL.
356-
// Actually, this whole code should go into PGL, so the Android implementation can
357-
// use Bitmap, and desktop use BufferedImage.
358-
359-
/*
360-
if (w != width || h != height) {
361-
System.err.println("Sorry but I don't know how to generate mipmaps for a subregion.");
362-
return;
363-
}
364-
365-
// Code by Mike Miller obtained from here:
366-
// http://insanitydesign.com/wp/2009/08/01/android-opengl-es-mipmaps/
367-
int w0 = glWidth;
368-
int h0 = glHeight;
369-
int[] argbPixels = new int[w0 * h0];
370-
convertToARGB(pixels, argbPixels, format);
371-
int level = 0;
372-
int denom = 1;
373-
374-
// We create a Bitmap because then we use its built-in filtered downsampling
375-
// functionality.
376-
Bitmap bitmap = Bitmap.createBitmap(w0, h0, Config.ARGB_8888);
377-
bitmap.setPixels(argbPixels, 0, w0, 0, 0, w0, h0);
378-
379-
while (w0 >= 1 || h0 >= 1) {
380-
//First of all, generate the texture from our bitmap and set it to the according level
381-
GLUtils.texImage2D(glTarget, level, bitmap, 0);
382-
383-
// We are done.
384-
if (w0 == 1 && h0 == 1) {
385-
break;
386-
}
387-
388-
// Increase the mipmap level
389-
level++;
390-
denom *= 2;
391-
392-
// Downsampling bitmap. We must eventually arrive to the 1x1 level,
393-
// and if the width and height are different, there will be a few 1D
394-
// texture levels just before.
395-
// This update formula also allows for NPOT resolutions.
396-
w0 = PApplet.max(1, PApplet.floor((float)glWidth / denom));
397-
h0 = PApplet.max(1, PApplet.floor((float)glHeight / denom));
398-
// (see getScaledInstance in AWT Image)
399-
Bitmap bitmap2 = Bitmap.createScaledBitmap(bitmap, w0, h0, true);
400-
401-
// Clean up
402-
bitmap.recycle();
403-
bitmap = bitmap2;
404-
}
405-
*/
406-
407-
loadPixels(w * h);
408-
convertToRGBA(pixels, format, w, h);
409-
updatePixelBuffer(rgbaPixels);
410-
pgl.texSubImage2D(glTarget, 0, x, y, w, h, PGL.RGBA, PGL.UNSIGNED_BYTE,
411-
pixelBuffer);
360+
// TODO: finish manual mipmap generation,
361+
// https://github.com/processing/processing/issues/3335
412362
}
413-
} else {
414-
loadPixels(w * h);
415-
convertToRGBA(pixels, format, w, h);
416-
updatePixelBuffer(rgbaPixels);
417-
pgl.texSubImage2D(glTarget, 0, x, y, w, h, PGL.RGBA, PGL.UNSIGNED_BYTE,
418-
pixelBuffer);
419363
}
420364

421365
pgl.bindTexture(glTarget, 0);
@@ -472,20 +416,18 @@ public void setNative(IntBuffer pixBuf, int x, int y, int w, int h) {
472416
}
473417
pgl.bindTexture(glTarget, glName);
474418

419+
pgl.texSubImage2D(glTarget, 0, x, y, w, h, PGL.RGBA, PGL.UNSIGNED_BYTE,
420+
pixBuf);
421+
fillEdges(x, y, w, h);
422+
475423
if (usingMipmaps) {
476424
if (PGraphicsOpenGL.autoMipmapGenSupported) {
477-
pgl.texSubImage2D(glTarget, 0, x, y, w, h, PGL.RGBA, PGL.UNSIGNED_BYTE,
478-
pixBuf);
479425
pgl.generateMipmap(glTarget);
480426
} else {
481-
pgl.texSubImage2D(glTarget, 0, x, y, w, h, PGL.RGBA, PGL.UNSIGNED_BYTE,
482-
pixBuf);
427+
// TODO: finish manual mipmap generation,
428+
// https://github.com/processing/processing/issues/3335
483429
}
484-
} else {
485-
pgl.texSubImage2D(glTarget, 0, x, y, w, h, PGL.RGBA, PGL.UNSIGNED_BYTE,
486-
pixBuf);
487430
}
488-
489431
pgl.bindTexture(glTarget, 0);
490432
if (enabledTex) {
491433
pgl.disableTexturing(glTarget);
@@ -1504,6 +1446,44 @@ protected void setParameters(Parameters params) {
15041446
}
15051447

15061448

1449+
protected void fillEdges(int x, int y, int w, int h) {
1450+
if ((width < glWidth || height < glHeight) && (x + w == width || y + h == height)) {
1451+
if (x + w == width) {
1452+
int ew = glWidth - width;
1453+
edgePixels = new int[h * ew];
1454+
for (int i = 0; i < h; i++) {
1455+
int c = rgbaPixels[i * w + (w - 1)];
1456+
Arrays.fill(edgePixels, i * ew, (i + 1) * ew, c);
1457+
}
1458+
edgeBuffer = PGL.updateIntBuffer(edgeBuffer, edgePixels, true);
1459+
pgl.texSubImage2D(glTarget, 0, width, y, ew, h, PGL.RGBA,
1460+
PGL.UNSIGNED_BYTE, edgeBuffer);
1461+
}
1462+
1463+
if (y + h == height) {
1464+
int eh = glHeight - height;
1465+
edgePixels = new int[eh * w];
1466+
for (int i = 0; i < eh; i++) {
1467+
System.arraycopy(rgbaPixels, (h - 1) * w, edgePixels, i * w, w);
1468+
}
1469+
edgeBuffer = PGL.updateIntBuffer(edgeBuffer, edgePixels, true);
1470+
pgl.texSubImage2D(glTarget, 0, x, height, w, eh, PGL.RGBA,
1471+
PGL.UNSIGNED_BYTE, edgeBuffer);
1472+
}
1473+
1474+
if (x + w == width && y + h == height) {
1475+
int ew = glWidth - width;
1476+
int eh = glHeight - height;
1477+
int c = rgbaPixels[w * h - 1];
1478+
edgePixels = new int[eh * ew];
1479+
Arrays.fill(edgePixels, 0, eh * ew, c);
1480+
edgeBuffer = PGL.updateIntBuffer(edgeBuffer, edgePixels, true);
1481+
pgl.texSubImage2D(glTarget, 0, width, height, ew, eh, PGL.RGBA,
1482+
PGL.UNSIGNED_BYTE, edgeBuffer);
1483+
}
1484+
}
1485+
}
1486+
15071487
///////////////////////////////////////////////////////////////////////////
15081488

15091489
// Parameters object

0 commit comments

Comments
 (0)