From 342bb2eaf083e1a775a366827f9f08db555221da Mon Sep 17 00:00:00 2001 From: codeanticode Date: Thu, 24 Jun 2010 04:01:48 +0000 Subject: [PATCH] Worked out some more texture handling issues in offscreen rendering in A3D --- android/core/src/processing/core/PApplet.java | 5 +- .../core/src/processing/core/PGraphics.java | 18 ++++ .../processing/core/PGraphicsAndroid3D.java | 88 ++++++++++--------- .../core/src/processing/core/PShape3D.java | 17 ++-- .../core/src/processing/core/PTexture.java | 8 +- 5 files changed, 81 insertions(+), 55 deletions(-) diff --git a/android/core/src/processing/core/PApplet.java b/android/core/src/processing/core/PApplet.java index d730a858c..706e6761b 100644 --- a/android/core/src/processing/core/PApplet.java +++ b/android/core/src/processing/core/PApplet.java @@ -7800,7 +7800,7 @@ public class PApplet extends Activity implements PConstants, Runnable { // A3D-only functions // TODO: Discuss proper integration into PApplet API. - synchronized public void clear() { + public void clear() { if (g instanceof PGraphicsAndroid3D) { ((PGraphicsAndroid3D)g).clear(); } else { @@ -7809,7 +7809,7 @@ public class PApplet extends Activity implements PConstants, Runnable { } - synchronized public void noClear() { + public void noClear() { if (g instanceof PGraphicsAndroid3D) { ((PGraphicsAndroid3D)g).noClear(); } else { @@ -7817,5 +7817,4 @@ public class PApplet extends Activity implements PConstants, Runnable { } } - } diff --git a/android/core/src/processing/core/PGraphics.java b/android/core/src/processing/core/PGraphics.java index 78b21641b..096bfdd4e 100644 --- a/android/core/src/processing/core/PGraphics.java +++ b/android/core/src/processing/core/PGraphics.java @@ -5059,5 +5059,23 @@ public class PGraphics extends PImage implements PConstants { public boolean is3D() { return false; } + ////////////////////////////////////////////////////////////// + + // A3D-only functions + + // TODO: Discuss proper integration into PGraphics API. + public void clear() { + if (!(this instanceof PGraphicsAndroid3D)) { + showMissingWarning("clear"); + } + } + + + public void noClear() { + if (!(this instanceof PGraphicsAndroid3D)) { + showMissingWarning("noClear"); + } + } + } diff --git a/android/core/src/processing/core/PGraphicsAndroid3D.java b/android/core/src/processing/core/PGraphicsAndroid3D.java index be6865eec..dddaa14f0 100644 --- a/android/core/src/processing/core/PGraphicsAndroid3D.java +++ b/android/core/src/processing/core/PGraphicsAndroid3D.java @@ -263,17 +263,6 @@ public class PGraphicsAndroid3D extends PGraphics { // ........................................................ - // Extensions support. - protected boolean npotTexSupported; - protected boolean mipmapSupported; - protected boolean matrixGetSupported; - protected boolean vboSupported; - protected boolean fboSupported; - protected int maxTextureSize; - protected float maxPointSize; - - // ........................................................ - // This array contains the recreateResource methods of all the GL objects // created in Processing. These methods are used to recreate the open GL // data when there is a context change or surface creation in Android. @@ -321,13 +310,24 @@ public class PGraphicsAndroid3D extends PGraphics { // ........................................................ boolean depthMask; + + // ........................................................ + + // Extensions support. + static protected boolean npotTexSupported; + static protected boolean mipmapSupported; + static protected boolean matrixGetSupported; + static protected boolean vboSupported; + static protected boolean fboSupported; + static protected int maxTextureSize; + static protected float maxPointSize; // ........................................................ - - public String OPENGL_VENDOR; - public String OPENGL_RENDERER; - public String OPENGL_VERSION; - + + // OpenGL strings + static public String OPENGL_VENDOR; + static public String OPENGL_RENDERER; + static public String OPENGL_VERSION; // //////////////////////////////////////////////////////////// @@ -693,12 +693,20 @@ public class PGraphicsAndroid3D extends PGraphics { drawTexCrop[2] = width; drawTexCrop[3] = height; - // Linear filtering is needed to keep decent image quality when rendering - // texture at a size different from its original resolution. This is expected - // to happen for offscreen rendering. drawImages = new PImage[2]; - drawImages[0] = parent.createImage(width, height, ARGB, LINEAR); - drawImages[1] = parent.createImage(width, height, ARGB, LINEAR); + if (primarySurface) { + // Nearest filtering is used for the primary surface, otherwise some + // artifacts appear (diagonal line when blending, for instance). This + // might deserve further examination. + drawImages[0] = parent.createImage(width, height, ARGB, NEAREST); + drawImages[1] = parent.createImage(width, height, ARGB, NEAREST); + } else { + // Linear filtering is needed to keep decent image quality when rendering + // texture at a size different from its original resolution. This is expected + // to happen for offscreen rendering. + drawImages[0] = parent.createImage(width, height, ARGB, LINEAR); + drawImages[1] = parent.createImage(width, height, ARGB, LINEAR); + } drawTextures = new PTexture[2]; drawTextures[0] = drawImages[0].getTexture(); @@ -745,10 +753,16 @@ public class PGraphicsAndroid3D extends PGraphics { TRIANGLECOUNT = 0; FACECOUNT = 0; - if (!primarySurface) { + if (!primarySurface) { PGraphicsAndroid3D a3d = (PGraphicsAndroid3D)parent.g; a3d.saveGLState(); + // Getting gl objects from primary surface: + gl = a3d.gl; + gl11 = a3d.gl11; + gl11x = a3d.gl11x; + gl11xp = a3d.gl11xp; + // Disabling all lights, so the offscreen renderer can set completely // new light configuration (otherwise some light config from the // primary renderer might stay). @@ -756,25 +770,12 @@ public class PGraphicsAndroid3D extends PGraphics { a3d.glLightDisable(i); } - // Getting gl objects from primary surface: - gl = a3d.gl; - gl11 = a3d.gl11; - gl11x = a3d.gl11x; - gl11xp = a3d.gl11xp; - - - OPENGL_VENDOR = a3d.OPENGL_VENDOR; - OPENGL_RENDERER = a3d.OPENGL_RENDERER; - OPENGL_VERSION = a3d.OPENGL_VERSION; - - npotTexSupported = a3d.npotTexSupported; - mipmapSupported = a3d.mipmapSupported; - matrixGetSupported = a3d.matrixGetSupported; - vboSupported = a3d.vboSupported; - fboSupported = a3d.fboSupported; - - maxTextureSize = a3d.maxTextureSize; - maxPointSize = a3d.maxPointSize; + if (a3d.lights) { + // The offscreen renderer starts with lights off by default. + // If the primary surface had lights on, the OpenGL state is + // changed accordingly. + noLights(); + } } if (!settingsInited) { @@ -862,7 +863,7 @@ public class PGraphicsAndroid3D extends PGraphics { pushFramebuffer(); setFramebuffer(drawFramebuffer); drawFramebuffer.addColorBuffer(drawTextures[drawIndex]); - + gl.glClearColor(0, 0, 0, 0); if (clear) { gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); @@ -894,7 +895,7 @@ public class PGraphicsAndroid3D extends PGraphics { if (fboSupported) { if (drawFramebuffer != null) { popFramebuffer(); - + if (primarySurface) { // Only the primary surface in clear mode will write the contents of the // ofscreen framebuffer to the screen. @@ -904,6 +905,7 @@ public class PGraphicsAndroid3D extends PGraphics { // Render current draw texture to screen. renderDrawTexture(drawIndex); } + swapDrawIndex(); } } else { diff --git a/android/core/src/processing/core/PShape3D.java b/android/core/src/processing/core/PShape3D.java index 6a6f7c954..9243baa95 100644 --- a/android/core/src/processing/core/PShape3D.java +++ b/android/core/src/processing/core/PShape3D.java @@ -113,7 +113,7 @@ public class PShape3D extends PShape implements PConstants { if (gl == null) { throw new RuntimeException("PShape3D: OpenGL ES 1.1 required"); } - if (!a3d.vboSupported) { + if (!PGraphicsAndroid3D.vboSupported) { throw new RuntimeException("PShape3D: Vertex Buffer Objects are not available"); } @@ -152,7 +152,7 @@ public class PShape3D extends PShape implements PConstants { if (gl == null) { throw new RuntimeException("PShape3D: OpenGL ES 1.1 required"); } - if (!a3d.vboSupported) { + if (!PGraphicsAndroid3D.vboSupported) { throw new RuntimeException("PShape3D: Vertex Buffer Objects are not available"); } @@ -1855,7 +1855,14 @@ public class PShape3D extends PShape implements PConstants { g.TRIANGLECOUNT = g.VERTEXCOUNT / 3; // Setting line width and point size from stroke value. - pointSize = PApplet.min(a3d.strokeWeight, a3d.maxPointSize); + // TODO: Here the stroke weight from the g renderer is used. Normally, no issue here, but + // in the case the shape is being rendered from an offscreen A3D surface, then this might + // lead to the possibilty of a stroke weight different from that of the main renderer. + // For strokeWeight it seems to make sense that the value of the offscreen renderer and not + // of the main renderer is used. But what about other properties such as textureMode or + // colorMode. Right now they are read from a3d, which refers to the main renderer. + // So what should be the normal behavior. + pointSize = PApplet.min(g.strokeWeight, PGraphicsAndroid3D.maxPointSize); gl.glPointSize(pointSize); if (!depthMaskEnabled) { @@ -1907,7 +1914,7 @@ public class PShape3D extends PShape implements PConstants { // the minimum and maximum point sizes. gl.glPointParameterf(GL11.GL_POINT_FADE_THRESHOLD_SIZE, 0.6f * pointSize); gl.glPointParameterf(GL11.GL_POINT_SIZE_MIN, 1.0f); - gl.glPointParameterf(GL11.GL_POINT_SIZE_MAX, a3d.maxPointSize); + gl.glPointParameterf(GL11.GL_POINT_SIZE_MAX, PGraphicsAndroid3D.maxPointSize); // Specify point sprite texture coordinate replacement mode for each // texture unit @@ -1932,7 +1939,7 @@ public class PShape3D extends PShape implements PConstants { if (0 < group.sw) { gl.glLineWidth(group.sw); } else { - gl.glLineWidth(a3d.strokeWeight); + gl.glLineWidth(g.strokeWeight); } if (0 < group.glMode && !pointSprites) { diff --git a/android/core/src/processing/core/PTexture.java b/android/core/src/processing/core/PTexture.java index 3626eae66..09ac940a7 100644 --- a/android/core/src/processing/core/PTexture.java +++ b/android/core/src/processing/core/PTexture.java @@ -285,7 +285,7 @@ public class PTexture implements PConstants { gl.glBindTexture(glTarget, glTextureID[0]); if (usingMipmaps) { - if (a3d.gl11 != null && a3d.mipmapSupported) { + if (a3d.gl11 != null && PGraphicsAndroid3D.mipmapSupported) { gl.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_GENERATE_MIPMAP, GL11.GL_TRUE); } else { // TODO: alternative mipmap generation. See the following link for more info: @@ -701,7 +701,7 @@ public class PTexture implements PConstants { protected void createTexture(int w, int h) { deleteTexture(); - if (a3d.npotTexSupported) { + if (PGraphicsAndroid3D.npotTexSupported) { glWidth = w; glHeight =h; } else { @@ -709,10 +709,10 @@ public class PTexture implements PConstants { glHeight = nextPowerOfTwo(h); } - if ((glWidth > a3d.maxTextureSize) || (glHeight > a3d.maxTextureSize)) { + if ((glWidth > PGraphicsAndroid3D.maxTextureSize) || (glHeight > PGraphicsAndroid3D.maxTextureSize)) { glWidth = glHeight = 0; throw new RuntimeException("Image width and height cannot be" + - " larger than " + a3d.maxTextureSize + + " larger than " + PGraphicsAndroid3D.maxTextureSize + " with this graphics card."); }