diff --git a/core/src/processing/opengl/FontTexture.java b/core/src/processing/opengl/FontTexture.java index 77e84d864..58fa64f83 100644 --- a/core/src/processing/opengl/FontTexture.java +++ b/core/src/processing/opengl/FontTexture.java @@ -155,13 +155,10 @@ class FontTexture implements PConstants { // REPLACE to preserve color of transparent pixels. Texture tex0 = textures[currentTex]; - PGraphicsOpenGL g = tex.pg.get(); - if (g != null) { - g.pushStyle(); - g.blendMode(REPLACE); - tex.put(tex0); - g.popStyle(); - } + tex.pg.pushStyle(); + tex.pg.blendMode(REPLACE); + tex.put(tex0); + tex.pg.popStyle(); textures[currentTex] = tex; diff --git a/core/src/processing/opengl/FrameBuffer.java b/core/src/processing/opengl/FrameBuffer.java index 8f8fda489..e591290a8 100644 --- a/core/src/processing/opengl/FrameBuffer.java +++ b/core/src/processing/opengl/FrameBuffer.java @@ -28,7 +28,6 @@ import processing.core.PApplet; import processing.core.PConstants; import processing.opengl.PGraphicsOpenGL.GLResourceFrameBuffer; -import java.lang.ref.WeakReference; import java.nio.IntBuffer; /** @@ -43,7 +42,7 @@ import java.nio.IntBuffer; */ public class FrameBuffer implements PConstants { - protected WeakReference pg; + protected PGraphicsOpenGL pg; protected PGL pgl; protected int context; // The context that created this framebuffer. @@ -73,7 +72,7 @@ public class FrameBuffer implements PConstants { FrameBuffer(PGraphicsOpenGL pg) { - this.pg = new WeakReference(pg); + this.pg = pg; pgl = pg.pgl; context = pgl.createEmptyContext(); } @@ -153,17 +152,15 @@ public class FrameBuffer implements PConstants { public void clear() { - PGraphicsOpenGL g = pg.get(); - if (g == null) return; - g.pushFramebuffer(); - g.setFramebuffer(this); + pg.pushFramebuffer(); + pg.setFramebuffer(this); pgl.clearDepth(1); pgl.clearStencil(0); pgl.clearColor(0, 0, 0, 0); pgl.clear(PGL.DEPTH_BUFFER_BIT | PGL.STENCIL_BUFFER_BIT | PGL.COLOR_BUFFER_BIT); - g.popFramebuffer(); + pg.popFramebuffer(); } public void copyColor(FrameBuffer dest) { @@ -179,14 +176,12 @@ public class FrameBuffer implements PConstants { } public void copy(FrameBuffer dest, int mask) { - PGraphicsOpenGL g = pg.get(); - if (g == null) return; pgl.bindFramebufferImpl(PGL.READ_FRAMEBUFFER, this.glFbo); pgl.bindFramebufferImpl(PGL.DRAW_FRAMEBUFFER, dest.glFbo); pgl.blitFramebuffer(0, 0, this.width, this.height, 0, 0, dest.width, dest.height, mask, PGL.NEAREST); - pgl.bindFramebufferImpl(PGL.READ_FRAMEBUFFER, g.getCurrentFB().glFbo); - pgl.bindFramebufferImpl(PGL.DRAW_FRAMEBUFFER, g.getCurrentFB().glFbo); + pgl.bindFramebufferImpl(PGL.READ_FRAMEBUFFER, pg.getCurrentFB().glFbo); + pgl.bindFramebufferImpl(PGL.DRAW_FRAMEBUFFER, pg.getCurrentFB().glFbo); } public void bind() { @@ -198,11 +193,9 @@ public class FrameBuffer implements PConstants { } public void finish() { - PGraphicsOpenGL g = pg.get(); - if (g == null) return; if (noDepth) { // No need to clear depth buffer because depth testing was disabled. - if (g.getHint(ENABLE_DEPTH_TEST)) { + if (pg.getHint(ENABLE_DEPTH_TEST)) { pgl.enable(PGL.DEPTH_TEST); } else { pgl.disable(PGL.DEPTH_TEST); @@ -258,8 +251,6 @@ public class FrameBuffer implements PConstants { public void setColorBuffers(Texture[] textures, int n) { - PGraphicsOpenGL g = pg.get(); - if (g == null) return; if (screenFb) return; if (numColorBuffers != PApplet.min(n, textures.length)) { @@ -271,8 +262,8 @@ public class FrameBuffer implements PConstants { colorBufferTex[i] = textures[i]; } - g.pushFramebuffer(); - g.setFramebuffer(this); + pg.pushFramebuffer(); + pg.setFramebuffer(this); // Making sure nothing is attached. for (int i = 0; i < numColorBuffers; i++) { @@ -288,14 +279,11 @@ public class FrameBuffer implements PConstants { pgl.validateFramebuffer(); - g.popFramebuffer(); + pg.popFramebuffer(); } public void swapColorBuffers() { - PGraphicsOpenGL g = pg.get(); - if (g == null) return; - for (int i = 0; i < numColorBuffers - 1; i++) { int i1 = (i + 1); Texture tmp = colorBufferTex[i]; @@ -303,8 +291,8 @@ public class FrameBuffer implements PConstants { colorBufferTex[i1] = tmp; } - g.pushFramebuffer(); - g.setFramebuffer(this); + pg.pushFramebuffer(); + pg.setFramebuffer(this); for (int i = 0; i < numColorBuffers; i++) { pgl.framebufferTexture2D(PGL.FRAMEBUFFER, PGL.COLOR_ATTACHMENT0 + i, colorBufferTex[i].glTarget, @@ -312,7 +300,7 @@ public class FrameBuffer implements PConstants { } pgl.validateFramebuffer(); - g.popFramebuffer(); + pg.popFramebuffer(); } @@ -433,12 +421,10 @@ public class FrameBuffer implements PConstants { protected void initColorBufferMultisample() { - PGraphicsOpenGL g = pg.get(); - if (g == null) return; if (screenFb) return; - g.pushFramebuffer(); - g.setFramebuffer(this); + pg.pushFramebuffer(); + pg.setFramebuffer(this); // glMultisample = PGraphicsOpenGL.createRenderBufferObject(context, pgl); pgl.bindRenderbuffer(PGL.RENDERBUFFER, glMultisample); @@ -447,21 +433,19 @@ public class FrameBuffer implements PConstants { pgl.framebufferRenderbuffer(PGL.FRAMEBUFFER, PGL.COLOR_ATTACHMENT0, PGL.RENDERBUFFER, glMultisample); - g.popFramebuffer(); + pg.popFramebuffer(); } protected void initPackedDepthStencilBuffer() { - PGraphicsOpenGL g = pg.get(); - if (g == null) return; if (screenFb) return; if (width == 0 || height == 0) { throw new RuntimeException("PFramebuffer: size undefined."); } - g.pushFramebuffer(); - g.setFramebuffer(this); + pg.pushFramebuffer(); + pg.setFramebuffer(this); // glDepthStencil = PGraphicsOpenGL.createRenderBufferObject(context, pgl); pgl.bindRenderbuffer(PGL.RENDERBUFFER, glDepthStencil); @@ -479,21 +463,19 @@ public class FrameBuffer implements PConstants { pgl.framebufferRenderbuffer(PGL.FRAMEBUFFER, PGL.STENCIL_ATTACHMENT, PGL.RENDERBUFFER, glDepthStencil); - g.popFramebuffer(); + pg.popFramebuffer(); } protected void initDepthBuffer() { - PGraphicsOpenGL g = pg.get(); - if (g == null) return; if (screenFb) return; if (width == 0 || height == 0) { throw new RuntimeException("PFramebuffer: size undefined."); } - g.pushFramebuffer(); - g.setFramebuffer(this); + pg.pushFramebuffer(); + pg.setFramebuffer(this); // glDepth = PGraphicsOpenGL.createRenderBufferObject(context, pgl); pgl.bindRenderbuffer(PGL.RENDERBUFFER, glDepth); @@ -517,21 +499,19 @@ public class FrameBuffer implements PConstants { pgl.framebufferRenderbuffer(PGL.FRAMEBUFFER, PGL.DEPTH_ATTACHMENT, PGL.RENDERBUFFER, glDepth); - g.popFramebuffer(); + pg.popFramebuffer(); } protected void initStencilBuffer() { - PGraphicsOpenGL g = pg.get(); - if (g == null) return; if (screenFb) return; if (width == 0 || height == 0) { throw new RuntimeException("PFramebuffer: size undefined."); } - g.pushFramebuffer(); - g.setFramebuffer(this); + pg.pushFramebuffer(); + pg.setFramebuffer(this); // glStencil = PGraphicsOpenGL.createRenderBufferObject(context, pgl); pgl.bindRenderbuffer(PGL.RENDERBUFFER, glStencil); @@ -554,7 +534,7 @@ public class FrameBuffer implements PConstants { pgl.framebufferRenderbuffer(PGL.FRAMEBUFFER, PGL.STENCIL_ATTACHMENT, PGL.RENDERBUFFER, glStencil); - g.popFramebuffer(); + pg.popFramebuffer(); } diff --git a/core/src/processing/opengl/PGraphicsOpenGL.java b/core/src/processing/opengl/PGraphicsOpenGL.java index ae3d05456..3d06ca5a5 100644 --- a/core/src/processing/opengl/PGraphicsOpenGL.java +++ b/core/src/processing/opengl/PGraphicsOpenGL.java @@ -788,12 +788,9 @@ public class PGraphicsOpenGL extends PGraphics { drainRefQueueBounded(); - PGraphicsOpenGL g = tex.pg.get(); - if (g != null) { - pgl = g.getPrimaryPGL(); - pgl.genTextures(1, intBuffer); - tex.glName = intBuffer.get(0); - } + pgl = tex.pg.getPrimaryPGL(); + pgl.genTextures(1, intBuffer); + tex.glName = intBuffer.get(0); this.glName = tex.glName; this.context = tex.context; @@ -866,12 +863,9 @@ public class PGraphicsOpenGL extends PGraphics { drainRefQueueBounded(); - PGraphicsOpenGL g = vbo.pgl.graphics; - if (g != null) { - pgl = g.getPrimaryPGL(); - pgl.genBuffers(1, intBuffer); - vbo.glId = intBuffer.get(0); - } + pgl = vbo.pgl.graphics.getPrimaryPGL(); + pgl.genBuffers(1, intBuffer); + vbo.glId = intBuffer.get(0); this.glId = vbo.glId; this.context = vbo.context; @@ -946,13 +940,10 @@ public class PGraphicsOpenGL extends PGraphics { drainRefQueueBounded(); - PGraphicsOpenGL g = sh.pgl.graphics; - if (g != null) { - this.pgl = g.getPrimaryPGL(); - sh.glProgram = pgl.createProgram(); - sh.glVertex = pgl.createShader(PGL.VERTEX_SHADER); - sh.glFragment = pgl.createShader(PGL.FRAGMENT_SHADER); - } + this.pgl = sh.pgl.graphics.getPrimaryPGL(); + sh.glProgram = pgl.createProgram(); + sh.glVertex = pgl.createShader(PGL.VERTEX_SHADER); + sh.glFragment = pgl.createShader(PGL.FRAGMENT_SHADER); this.glProgram = sh.glProgram; this.glVertex = sh.glVertex; @@ -1043,38 +1034,35 @@ public class PGraphicsOpenGL extends PGraphics { drainRefQueueBounded(); - PGraphicsOpenGL g = fb.pgl.graphics; - if (g != null) { - pgl = g.getPrimaryPGL(); - if (!fb.screenFb) { - pgl.genFramebuffers(1, intBuffer); - fb.glFbo = intBuffer.get(0); + pgl = fb.pg.getPrimaryPGL(); + if (!fb.screenFb) { + pgl.genFramebuffers(1, intBuffer); + fb.glFbo = intBuffer.get(0); - if (fb.multisample) { - pgl.genRenderbuffers(1, intBuffer); - fb.glMultisample = intBuffer.get(0); - } - - if (fb.packedDepthStencil) { - pgl.genRenderbuffers(1, intBuffer); - fb.glDepthStencil = intBuffer.get(0); - } else { - if (0 < fb.depthBits) { - pgl.genRenderbuffers(1, intBuffer); - fb.glDepth = intBuffer.get(0); - } - if (0 < fb.stencilBits) { - pgl.genRenderbuffers(1, intBuffer); - fb.glStencil = intBuffer.get(0); - } - } - - this.glFbo = fb.glFbo; - this.glDepth = fb.glDepth; - this.glStencil = fb.glStencil; - this.glDepthStencil = fb.glDepthStencil; - this.glMultisample = fb.glMultisample; + if (fb.multisample) { + pgl.genRenderbuffers(1, intBuffer); + fb.glMultisample = intBuffer.get(0); } + + if (fb.packedDepthStencil) { + pgl.genRenderbuffers(1, intBuffer); + fb.glDepthStencil = intBuffer.get(0); + } else { + if (0 < fb.depthBits) { + pgl.genRenderbuffers(1, intBuffer); + fb.glDepth = intBuffer.get(0); + } + if (0 < fb.stencilBits) { + pgl.genRenderbuffers(1, intBuffer); + fb.glStencil = intBuffer.get(0); + } + } + + this.glFbo = fb.glFbo; + this.glDepth = fb.glDepth; + this.glStencil = fb.glStencil; + this.glDepthStencil = fb.glDepthStencil; + this.glMultisample = fb.glMultisample; } this.context = fb.context; @@ -7383,9 +7371,9 @@ public class PGraphicsOpenGL extends PGraphics { // Holds an array of textures and the range of vertex // indices each texture applies to. static protected class TexCache { + PGraphicsOpenGL pg; int size; - WeakReference pg; - WeakReference[] textures; + PImage[] textures; int[] firstIndex; int[] lastIndex; int[] firstCache; @@ -7393,13 +7381,12 @@ public class PGraphicsOpenGL extends PGraphics { boolean hasTextures; TexCache(PGraphicsOpenGL pg) { - this.pg = new WeakReference(pg); + this.pg = pg; allocate(); } - @SuppressWarnings("unchecked") void allocate() { - textures = new WeakReference[PGL.DEFAULT_IN_TEXTURES]; + textures = new PImage[PGL.DEFAULT_IN_TEXTURES]; firstIndex = new int[PGL.DEFAULT_IN_TEXTURES]; lastIndex = new int[PGL.DEFAULT_IN_TEXTURES]; firstCache = new int[PGL.DEFAULT_IN_TEXTURES]; @@ -7416,22 +7403,21 @@ public class PGraphicsOpenGL extends PGraphics { boolean containsTexture(PImage img) { for (int i = 0; i < size; i++) { - if (textures[i].get() == img) return true; + if (textures[i] == img) return true; } return false; } PImage getTextureImage(int i) { - return textures[i].get(); + return textures[i]; } Texture getTexture(int i) { - PGraphicsOpenGL g = pg.get(); - PImage img = textures[i].get(); + PImage img = textures[i]; Texture tex = null; - if (img != null && g != null) { - tex = g.getTexture(img); + if (img != null) { + tex = pg.getTexture(img); } return tex; @@ -7440,7 +7426,7 @@ public class PGraphicsOpenGL extends PGraphics { void addTexture(PImage img, int firsti, int firstb, int lasti, int lastb) { arrayCheck(); - textures[size] = new WeakReference(img); + textures[size] = img; firstIndex[size] = firsti; lastIndex[size] = lasti; firstCache[size] = firstb; @@ -7469,9 +7455,8 @@ public class PGraphicsOpenGL extends PGraphics { } } - @SuppressWarnings("unchecked") void expandTextures(int n) { - WeakReference[] temp = new WeakReference[n]; + PImage[] temp = new PImage[n]; PApplet.arrayCopy(textures, 0, temp, 0, size); textures = temp; } @@ -7611,7 +7596,7 @@ public class PGraphicsOpenGL extends PGraphics { // Holds the input vertices: xyz coordinates, fill/tint color, // normal, texture coordinates and stroke color and weight. static protected class InGeometry { - WeakReference pg; + PGraphicsOpenGL pg; int renderMode; AttributeMap attribs; @@ -7654,7 +7639,7 @@ public class PGraphicsOpenGL extends PGraphics { float normalX, normalY, normalZ; InGeometry(PGraphicsOpenGL pg, AttributeMap attr, int mode) { - this.pg = new WeakReference(pg); + this.pg = pg; this.attribs = attr; renderMode = mode; allocate(); @@ -8710,9 +8695,6 @@ public class PGraphicsOpenGL extends PGraphics { void addEllipse(float x, float y, float w, float h, boolean fill, boolean stroke) { - PGraphicsOpenGL g = pg.get(); - if (g == null) return; - float radiusH = w / 2; float radiusV = h / 2; @@ -8720,10 +8702,10 @@ public class PGraphicsOpenGL extends PGraphics { float centerY = y + radiusV; // should call screenX/Y using current renderer. - float sx1 = g.screenX(x, y); - float sy1 = g.screenY(x, y); - float sx2 = g.screenX(x + w, y + h); - float sy2 = g.screenY(x + w, y + h); + float sx1 = pg.screenX(x, y); + float sy1 = pg.screenY(x, y); + float sx2 = pg.screenX(x + w, y + h); + float sy2 = pg.screenY(x + w, y + h); int accuracy = PApplet.min(MAX_POINT_ACCURACY, PApplet.max(MIN_POINT_ACCURACY, @@ -8765,9 +8747,6 @@ public class PGraphicsOpenGL extends PGraphics { void addArc(float x, float y, float w, float h, float start, float stop, boolean fill, boolean stroke, int arcMode) { - PGraphicsOpenGL g = pg.get(); - if (g == null) return; - float hr = w / 2f; float vr = h / 2f; @@ -8810,10 +8789,10 @@ public class PGraphicsOpenGL extends PGraphics { int inc; { // initializes inc the same way ellipse does - float sx1 = g.screenX(x, y); - float sy1 = g.screenY(x, y); - float sx2 = g.screenX(x + w, y + h); - float sy2 = g.screenY(x + w, y + h); + float sx1 = pg.screenX(x, y); + float sy1 = pg.screenY(x, y); + float sx2 = pg.screenX(x + w, y + h); + float sy2 = pg.screenY(x + w, y + h); int accuracy = PApplet.min(MAX_POINT_ACCURACY, PApplet.max(MIN_POINT_ACCURACY, @@ -8976,9 +8955,6 @@ public class PGraphicsOpenGL extends PGraphics { // any vertex or edge. int[] addSphere(float r, int detailU, int detailV, boolean fill, boolean stroke) { - PGraphicsOpenGL g = pg.get(); - if (g == null) return null; - int nind = 3 * detailU + (6 * detailU + 3) * (detailV - 2) + 3 * detailU; int[] indices = new int[nind]; @@ -9005,15 +8981,15 @@ public class PGraphicsOpenGL extends PGraphics { vert0 = vertCount; u = 1; v -= dv; for (int i = 0; i < detailU; i++) { - setNormal(g.sphereX[i], g.sphereY[i], g.sphereZ[i]); - addVertex(r*g.sphereX[i], r*g.sphereY[i], r*g.sphereZ[i], u , v, + setNormal(pg.sphereX[i], pg.sphereY[i], pg.sphereZ[i]); + addVertex(r*pg.sphereX[i], r*pg.sphereY[i], r*pg.sphereZ[i], u , v, VERTEX, false); u -= du; } vertCount += detailU; vert1 = vertCount; - setNormal(g.sphereX[0], g.sphereY[0], g.sphereZ[0]); - addVertex(r*g.sphereX[0], r*g.sphereY[0], r*g.sphereZ[0], u, v, + setNormal(pg.sphereX[0], pg.sphereY[0], pg.sphereZ[0]); + addVertex(r*pg.sphereX[0], r*pg.sphereY[0], r*pg.sphereZ[0], u, v, VERTEX, false); vertCount++; @@ -9039,15 +9015,15 @@ public class PGraphicsOpenGL extends PGraphics { u = 1; v -= dv; for (int i = 0; i < detailU; i++) { int ioff = offset + i; - setNormal(g.sphereX[ioff], g.sphereY[ioff], g.sphereZ[ioff]); - addVertex(r*g.sphereX[ioff], r*g.sphereY[ioff], r*g.sphereZ[ioff], + setNormal(pg.sphereX[ioff], pg.sphereY[ioff], pg.sphereZ[ioff]); + addVertex(r*pg.sphereX[ioff], r*pg.sphereY[ioff], r*pg.sphereZ[ioff], u , v, VERTEX, false); u -= du; } vertCount += detailU; vert1 = vertCount; - setNormal(g.sphereX[offset], g.sphereY[offset], g.sphereZ[offset]); - addVertex(r*g.sphereX[offset], r*g.sphereY[offset], r*g.sphereZ[offset], + setNormal(pg.sphereX[offset], pg.sphereY[offset], pg.sphereZ[offset]); + addVertex(r*pg.sphereX[offset], r*pg.sphereY[offset], r*pg.sphereZ[offset], u, v, VERTEX, false); vertCount++; @@ -9108,7 +9084,7 @@ public class PGraphicsOpenGL extends PGraphics { // Holds tessellated data for polygon, line and point geometry. static protected class TessGeometry { int renderMode; - WeakReference pg; + PGraphicsOpenGL pg; AttributeMap polyAttribs; // Tessellated polygon data @@ -9188,7 +9164,7 @@ public class PGraphicsOpenGL extends PGraphics { HashMap bpolyAttribs = new HashMap(); TessGeometry(PGraphicsOpenGL pg, AttributeMap attr, int mode) { - this.pg = new WeakReference(pg); + this.pg = pg; this.polyAttribs = attr; renderMode = mode; allocate(); @@ -10102,9 +10078,6 @@ public class PGraphicsOpenGL extends PGraphics { // Sets point vertex with index tessIdx using the data from input vertex // inIdx. void setPointVertex(int tessIdx, InGeometry in, int inIdx) { - PGraphicsOpenGL g = pg.get(); - if (g == null) return; - int index; index = 3 * inIdx; @@ -10112,8 +10085,8 @@ public class PGraphicsOpenGL extends PGraphics { float y = in.vertices[index++]; float z = in.vertices[index ]; - if (renderMode == IMMEDIATE && g.flushMode == FLUSH_WHEN_FULL) { - PMatrix3D mm = g.modelview; + if (renderMode == IMMEDIATE && pg.flushMode == FLUSH_WHEN_FULL) { + PMatrix3D mm = pg.modelview; index = 4 * tessIdx; pointVertices[index++] = x*mm.m00 + y*mm.m01 + z*mm.m02 + mm.m03; @@ -10136,9 +10109,6 @@ public class PGraphicsOpenGL extends PGraphics { // Add line geometry void setLineVertex(int tessIdx, float[] vertices, int inIdx0, int rgba) { - PGraphicsOpenGL g = pg.get(); - if (g == null) return; - int index; index = 3 * inIdx0; @@ -10146,8 +10116,8 @@ public class PGraphicsOpenGL extends PGraphics { float y0 = vertices[index++]; float z0 = vertices[index ]; - if (renderMode == IMMEDIATE && g.flushMode == FLUSH_WHEN_FULL) { - PMatrix3D mm = g.modelview; + if (renderMode == IMMEDIATE && pg.flushMode == FLUSH_WHEN_FULL) { + PMatrix3D mm = pg.modelview; index = 4 * tessIdx; lineVertices[index++] = x0*mm.m00 + y0*mm.m01 + z0*mm.m02 + mm.m03; @@ -10174,9 +10144,6 @@ public class PGraphicsOpenGL extends PGraphics { // inIdx0 and inIdx1. void setLineVertex(int tessIdx, float[] vertices, int inIdx0, int inIdx1, int rgba, float weight) { - PGraphicsOpenGL g = pg.get(); - if (g == null) return; - int index; index = 3 * inIdx0; @@ -10193,8 +10160,8 @@ public class PGraphicsOpenGL extends PGraphics { float dy = y1 - y0; float dz = z1 - z0; - if (renderMode == IMMEDIATE && g.flushMode == FLUSH_WHEN_FULL) { - PMatrix3D mm = g.modelview; + if (renderMode == IMMEDIATE && pg.flushMode == FLUSH_WHEN_FULL) { + PMatrix3D mm = pg.modelview; index = 4 * tessIdx; lineVertices[index++] = x0*mm.m00 + y0*mm.m01 + z0*mm.m02 + mm.m03; @@ -10228,9 +10195,6 @@ public class PGraphicsOpenGL extends PGraphics { // Add poly geometry void addPolyVertex(double[] d, boolean clampXY) { - PGraphicsOpenGL g = pg.get(); - if (g == null) return; - int fcolor = (int)d[ 3] << 24 | (int)d[ 4] << 16 | (int)d[ 5] << 8 | (int)d[ 6]; int acolor = @@ -10250,8 +10214,8 @@ public class PGraphicsOpenGL extends PGraphics { if (25 < d.length) { // Add the values of the custom attributes... - PMatrix3D mm = g.modelview; - PMatrix3D nm = g.modelviewInv; + PMatrix3D mm = pg.modelview; + PMatrix3D nm = pg.modelviewInv; int tessIdx = polyVertexCount - 1; int index; int pos = 25; @@ -10271,7 +10235,7 @@ public class PGraphicsOpenGL extends PGraphics { float x = (float)d[pos++]; float y = (float)d[pos++]; float z = (float)d[pos++]; - if (renderMode == IMMEDIATE && g.flushMode == FLUSH_WHEN_FULL) { + if (renderMode == IMMEDIATE && pg.flushMode == FLUSH_WHEN_FULL) { if (clampXY) { // ceil emulates the behavior of JAVA2D farray[index++] = @@ -10295,7 +10259,7 @@ public class PGraphicsOpenGL extends PGraphics { float x = (float)d[pos + 0]; float y = (float)d[pos + 1]; float z = (float)d[pos + 2]; - if (renderMode == IMMEDIATE && g.flushMode == FLUSH_WHEN_FULL) { + if (renderMode == IMMEDIATE && pg.flushMode == FLUSH_WHEN_FULL) { farray[index++] = x*nm.m00 + y*nm.m10 + z*nm.m20; farray[index++] = x*nm.m01 + y*nm.m11 + z*nm.m21; farray[index ] = x*nm.m02 + y*nm.m12 + z*nm.m22; @@ -10358,14 +10322,11 @@ public class PGraphicsOpenGL extends PGraphics { float u, float v, int am, int sp, int em, float shine, boolean clampXY) { - PGraphicsOpenGL g = pg.get(); - if (g == null) return; - int index; - if (renderMode == IMMEDIATE && g.flushMode == FLUSH_WHEN_FULL) { - PMatrix3D mm = g.modelview; - PMatrix3D nm = g.modelviewInv; + if (renderMode == IMMEDIATE && pg.flushMode == FLUSH_WHEN_FULL) { + PMatrix3D mm = pg.modelview; + PMatrix3D nm = pg.modelviewInv; index = 4 * tessIdx; if (clampXY) { @@ -10419,17 +10380,14 @@ public class PGraphicsOpenGL extends PGraphics { } void addPolyVertices(InGeometry in, int i0, int i1, boolean clampXY) { - PGraphicsOpenGL g = pg.get(); - if (g == null) return; - int index; int nvert = i1 - i0 + 1; polyVertexCheck(nvert); - if (renderMode == IMMEDIATE && g.flushMode == FLUSH_WHEN_FULL) { - PMatrix3D mm = g.modelview; - PMatrix3D nm = g.modelviewInv; + if (renderMode == IMMEDIATE && pg.flushMode == FLUSH_WHEN_FULL) { + PMatrix3D mm = pg.modelview; + PMatrix3D nm = pg.modelviewInv; for (int i = 0; i < nvert; i++) { int inIdx = i0 + i; @@ -10889,8 +10847,6 @@ public class PGraphicsOpenGL extends PGraphics { // Generates tessellated geometry given a batch of input vertices. static protected class Tessellator { - WeakReference pg; - InGeometry in; TessGeometry tess; TexCache texCache; @@ -10913,6 +10869,7 @@ public class PGraphicsOpenGL extends PGraphics { PMatrix transform; float transformScale; boolean is2D, is3D; + protected PGraphicsOpenGL pg; int[] rawIndices; int rawSize; @@ -10950,10 +10907,9 @@ public class PGraphicsOpenGL extends PGraphics { } void initGluTess() { - PGraphicsOpenGL g = pg.get(); - if (g != null && gluTess == null) { + if (gluTess == null) { callback = new TessellatorCallback(tess.polyAttribs); - gluTess = g.pgl.createTessellator(callback); + gluTess = pg.pgl.createTessellator(callback); } } @@ -11006,7 +10962,7 @@ public class PGraphicsOpenGL extends PGraphics { } protected void setRenderer(PGraphicsOpenGL pg) { - this.pg = new WeakReference(pg); + this.pg = pg; } void set3D(boolean value) { @@ -11025,10 +10981,7 @@ public class PGraphicsOpenGL extends PGraphics { } void resetCurveVertexCount() { - PGraphicsOpenGL g = pg.get(); - if (g != null) { - g.curveVertexCount = 0; - } + pg.curveVertexCount = 0; } // ----------------------------------------------------------------- @@ -11308,9 +11261,8 @@ public class PGraphicsOpenGL extends PGraphics { } boolean clamp2D() { - PGraphicsOpenGL g = pg.get(); - return is2D && tess.renderMode == IMMEDIATE && g != null && - zero(g.modelview.m01) && zero(g.modelview.m10); + return is2D && tess.renderMode == IMMEDIATE && + zero(pg.modelview.m01) && zero(pg.modelview.m10); } boolean clampSquarePoints2D() { @@ -11985,13 +11937,10 @@ public class PGraphicsOpenGL extends PGraphics { } void unclampLine2D(int tessIdx, float x, float y) { - PGraphicsOpenGL g = pg.get(); - if (g != null) { - PMatrix3D mm = g.modelview; - int index = 4 * tessIdx; - tess.polyVertices[index++] = x*mm.m00 + y*mm.m01 + mm.m03; - tess.polyVertices[index++] = x*mm.m10 + y*mm.m11 + mm.m13; - } + PMatrix3D mm = pg.modelview; + int index = 4 * tessIdx; + tess.polyVertices[index++] = x*mm.m00 + y*mm.m01 + mm.m03; + tess.polyVertices[index++] = x*mm.m10 + y*mm.m11 + mm.m13; } boolean noCapsJoins(int nInVert) { @@ -12614,14 +12563,11 @@ public class PGraphicsOpenGL extends PGraphics { } void addBezierVertex(int i) { - PGraphicsOpenGL g = pg.get(); - if (g == null) return; + pg.curveVertexCount = 0; + pg.bezierInitCheck(); + pg.bezierVertexCheck(POLYGON, i); - g.curveVertexCount = 0; - g.bezierInitCheck(); - g.bezierVertexCheck(POLYGON, i); - - PMatrix3D draw = g.bezierDrawMatrix; + PMatrix3D draw = pg.bezierDrawMatrix; int i1 = i - 1; float x1 = in.vertices[3*i1 + 0]; @@ -12659,7 +12605,7 @@ public class PGraphicsOpenGL extends PGraphics { float zplot2 = draw.m20*z1 + draw.m21*z2 + draw.m22*z3 + draw.m23*z4; float zplot3 = draw.m30*z1 + draw.m31*z2 + draw.m32*z3 + draw.m33*z4; - for (int j = 0; j < g.bezierDetail; j++) { + for (int j = 0; j < pg.bezierDetail; j++) { x1 += xplot1; xplot1 += xplot2; xplot2 += xplot3; y1 += yplot1; yplot1 += yplot2; yplot2 += yplot3; z1 += zplot1; zplot1 += zplot2; zplot2 += zplot3; @@ -12675,14 +12621,11 @@ public class PGraphicsOpenGL extends PGraphics { } void addQuadraticVertex(int i) { - PGraphicsOpenGL g = pg.get(); - if (g == null) return; + pg.curveVertexCount = 0; + pg.bezierInitCheck(); + pg.bezierVertexCheck(POLYGON, i); - g.curveVertexCount = 0; - g.bezierInitCheck(); - g.bezierVertexCheck(POLYGON, i); - - PMatrix3D draw = g.bezierDrawMatrix; + PMatrix3D draw = pg.bezierDrawMatrix; int i1 = i - 1; float x1 = in.vertices[3*i1 + 0]; @@ -12727,7 +12670,7 @@ public class PGraphicsOpenGL extends PGraphics { float zplot2 = draw.m20*z1 + draw.m21*z2 + draw.m22*z3 + draw.m23*z4; float zplot3 = draw.m30*z1 + draw.m31*z2 + draw.m32*z3 + draw.m33*z4; - for (int j = 0; j < g.bezierDetail; j++) { + for (int j = 0; j < pg.bezierDetail; j++) { x1 += xplot1; xplot1 += xplot2; xplot2 += xplot3; y1 += yplot1; yplot1 += yplot2; yplot2 += yplot3; z1 += zplot1; zplot1 += zplot2; zplot2 += zplot3; @@ -12743,27 +12686,24 @@ public class PGraphicsOpenGL extends PGraphics { } void addCurveVertex(int i) { - PGraphicsOpenGL g = pg.get(); - if (g == null) return; + pg.curveVertexCheck(POLYGON); - g.curveVertexCheck(POLYGON); - - float[] vertex = g.curveVertices[g.curveVertexCount]; + float[] vertex = pg.curveVertices[pg.curveVertexCount]; vertex[X] = in.vertices[3*i + 0]; vertex[Y] = in.vertices[3*i + 1]; vertex[Z] = in.vertices[3*i + 2]; - g.curveVertexCount++; + pg.curveVertexCount++; // draw a segment if there are enough points - if (g.curveVertexCount == 3) { - float[] v = g.curveVertices[g.curveVertexCount - 2]; + if (pg.curveVertexCount == 3) { + float[] v = pg.curveVertices[pg.curveVertexCount - 2]; addCurveInitialVertex(i, v[X], v[Y], v[Z]); } - if (g.curveVertexCount > 3) { - float[] v1 = g.curveVertices[g.curveVertexCount - 4]; - float[] v2 = g.curveVertices[g.curveVertexCount - 3]; - float[] v3 = g.curveVertices[g.curveVertexCount - 2]; - float[] v4 = g.curveVertices[g.curveVertexCount - 1]; + if (pg.curveVertexCount > 3) { + float[] v1 = pg.curveVertices[pg.curveVertexCount - 4]; + float[] v2 = pg.curveVertices[pg.curveVertexCount - 3]; + float[] v3 = pg.curveVertices[pg.curveVertexCount - 2]; + float[] v4 = pg.curveVertices[pg.curveVertexCount - 1]; addCurveVertexSegment(i, v1[X], v1[Y], v1[Z], v2[X], v2[Y], v2[Z], v3[X], v3[Y], v3[Z], @@ -12788,9 +12728,6 @@ public class PGraphicsOpenGL extends PGraphics { float x2, float y2, float z2, float x3, float y3, float z3, float x4, float y4, float z4) { - PGraphicsOpenGL g = pg.get(); - if (g == null) return; - int strokeColor = 0; float strokeWeight = 0; if (stroke) { @@ -12804,7 +12741,7 @@ public class PGraphicsOpenGL extends PGraphics { float y = y2; float z = z2; - PMatrix3D draw = g.curveDrawMatrix; + PMatrix3D draw = pg.curveDrawMatrix; float xplot1 = draw.m10*x1 + draw.m11*x2 + draw.m12*x3 + draw.m13*x4; float xplot2 = draw.m20*x1 + draw.m21*x2 + draw.m22*x3 + draw.m23*x4; @@ -12818,7 +12755,7 @@ public class PGraphicsOpenGL extends PGraphics { float zplot2 = draw.m20*z1 + draw.m21*z2 + draw.m22*z3 + draw.m23*z4; float zplot3 = draw.m30*z1 + draw.m31*z2 + draw.m32*z3 + draw.m33*z4; - for (int j = 0; j < g.curveDetail; j++) { + for (int j = 0; j < pg.curveDetail; j++) { x += xplot1; xplot1 += xplot2; xplot2 += xplot3; y += yplot1; yplot1 += yplot2; yplot2 += yplot3; z += zplot1; zplot1 += zplot2; zplot2 += zplot3; @@ -12834,10 +12771,7 @@ public class PGraphicsOpenGL extends PGraphics { } void addVertex(int i) { - PGraphicsOpenGL g = pg.get(); - if (g == null) return; - - g.curveVertexCount = 0; + pg.curveVertexCount = 0; float x = in.vertices[3*i + 0]; float y = in.vertices[3*i + 1]; @@ -13212,11 +13146,8 @@ public class PGraphicsOpenGL extends PGraphics { } public void error(int errnum) { - PGraphicsOpenGL g = pg.get(); - if (g != null) { - String estring = g.pgl.tessError(errnum); - PGraphics.showWarning(TESSELLATION_ERROR, estring); - } + String estring = pg.pgl.tessError(errnum); + PGraphics.showWarning(TESSELLATION_ERROR, estring); } /** diff --git a/core/src/processing/opengl/PShader.java b/core/src/processing/opengl/PShader.java index 310840779..d24d1ae0a 100644 --- a/core/src/processing/opengl/PShader.java +++ b/core/src/processing/opengl/PShader.java @@ -27,7 +27,6 @@ package processing.opengl; import processing.core.*; import processing.opengl.PGraphicsOpenGL.GLResourceShader; -import java.lang.ref.WeakReference; import java.net.URL; import java.nio.FloatBuffer; import java.nio.IntBuffer; @@ -80,8 +79,8 @@ public class PShader implements PConstants { // be called by different renderers within a single application // (the one corresponding to the main surface, or other offscreen // renderers). - protected WeakReference primaryPG; - protected WeakReference currentPG; + protected PGraphicsOpenGL primaryPG; + protected PGraphicsOpenGL currentPG; protected PGL pgl; protected int context; // The context that created this shader. @@ -185,9 +184,8 @@ public class PShader implements PConstants { public PShader(PApplet parent) { this(); this.parent = parent; - PGraphicsOpenGL g = (PGraphicsOpenGL)parent.g; - primaryPG = new WeakReference(g); - pgl = g.pgl; + primaryPG = (PGraphicsOpenGL)parent.g; + pgl = primaryPG.pgl; context = pgl.createEmptyContext(); } @@ -202,9 +200,8 @@ public class PShader implements PConstants { */ public PShader(PApplet parent, String vertFilename, String fragFilename) { this.parent = parent; - PGraphicsOpenGL g = (PGraphicsOpenGL)parent.g; - primaryPG = new WeakReference(g); - pgl = g.pgl; + primaryPG = (PGraphicsOpenGL)parent.g; + pgl = primaryPG.pgl; this.vertexURL = null; this.fragmentURL = null; @@ -242,9 +239,8 @@ public class PShader implements PConstants { */ public PShader(PApplet parent, URL vertURL, URL fragURL) { this.parent = parent; - PGraphicsOpenGL g = (PGraphicsOpenGL)parent.g; - primaryPG = new WeakReference(g); - pgl = g.pgl; + primaryPG = (PGraphicsOpenGL)parent.g; + pgl = primaryPG.pgl; this.vertexURL = vertURL; this.fragmentURL = fragURL; @@ -277,9 +273,8 @@ public class PShader implements PConstants { public PShader(PApplet parent, String[] vertSource, String[] fragSource) { this.parent = parent; - PGraphicsOpenGL g = (PGraphicsOpenGL)parent.g; - primaryPG = new WeakReference(g); - pgl = g.pgl; + primaryPG = (PGraphicsOpenGL)parent.g; + pgl = primaryPG.pgl; this.vertexURL = null; this.fragmentURL = null; @@ -824,9 +819,7 @@ public class PShader implements PConstants { pgl.uniformMatrix4fv(loc, 1, false, floatBuffer); } else if (val.type == UniformValue.SAMPLER2D) { PImage img = (PImage)val.value; - PGraphicsOpenGL g = currentPG.get(); - if (g == null) return; - Texture tex = g.getTexture(img); + Texture tex = currentPG.getTexture(img); if (textures == null) textures = new HashMap(); textures.put(loc, tex); @@ -1133,7 +1126,7 @@ public class PShader implements PConstants { protected void setRenderer(PGraphicsOpenGL pg) { - this.currentPG = new WeakReference(pg); + this.currentPG = pg; } @@ -1206,29 +1199,26 @@ public class PShader implements PConstants { protected void setCommonUniforms() { - PGraphicsOpenGL g = currentPG.get(); - if (g == null) return; - if (-1 < transformMatLoc) { - g.updateGLProjmodelview(); - setUniformMatrix(transformMatLoc, g.glProjmodelview); + currentPG.updateGLProjmodelview(); + setUniformMatrix(transformMatLoc, currentPG.glProjmodelview); } if (-1 < modelviewMatLoc) { - g.updateGLModelview(); - setUniformMatrix(modelviewMatLoc, g.glModelview); + currentPG.updateGLModelview(); + setUniformMatrix(modelviewMatLoc, currentPG.glModelview); } if (-1 < projectionMatLoc) { - g.updateGLProjection(); - setUniformMatrix(projectionMatLoc, g.glProjection); + currentPG.updateGLProjection(); + setUniformMatrix(projectionMatLoc, currentPG.glProjection); } if (-1 < viewportLoc) { - float x = g.viewport.get(0); - float y = g.viewport.get(1); - float w = g.viewport.get(2); - float h = g.viewport.get(3); + float x = currentPG.viewport.get(0); + float y = currentPG.viewport.get(1); + float w = currentPG.viewport.get(2); + float h = currentPG.viewport.get(3); setUniformValue(viewportLoc, x, y, w, h); } @@ -1236,7 +1226,7 @@ public class PShader implements PConstants { ppixelsUnit = getLastTexUnit() + 1; setUniformValue(ppixelsLoc, ppixelsUnit); pgl.activeTexture(PGL.TEXTURE0 + ppixelsUnit); - g.bindFrontTexture(); + currentPG.bindFrontTexture(); } else { ppixelsUnit = -1; } @@ -1244,15 +1234,12 @@ public class PShader implements PConstants { protected void bindTyped() { - PGraphicsOpenGL pg = primaryPG.get(); if (currentPG == null) { - setRenderer(pg.getCurrentPG()); + setRenderer(primaryPG.getCurrentPG()); loadAttributes(); loadUniforms(); } setCommonUniforms(); - PGraphicsOpenGL g = currentPG.get(); - if (g == null) return; if (-1 < vertexLoc) pgl.enableVertexAttribArray(vertexLoc); if (-1 < colorLoc) pgl.enableVertexAttribArray(colorLoc); @@ -1260,8 +1247,8 @@ public class PShader implements PConstants { if (-1 < normalLoc) pgl.enableVertexAttribArray(normalLoc); if (-1 < normalMatLoc) { - g.updateGLNormal(); - setUniformMatrix(normalMatLoc, g.glNormal); + currentPG.updateGLNormal(); + setUniformMatrix(normalMatLoc, currentPG.glNormal); } if (-1 < ambientLoc) pgl.enableVertexAttribArray(ambientLoc); @@ -1269,17 +1256,17 @@ public class PShader implements PConstants { if (-1 < emissiveLoc) pgl.enableVertexAttribArray(emissiveLoc); if (-1 < shininessLoc) pgl.enableVertexAttribArray(shininessLoc); - int count = g.lightCount; + int count = currentPG.lightCount; setUniformValue(lightCountLoc, count); if (0 < count) { - setUniformVector(lightPositionLoc, g.lightPosition, 4, count); - setUniformVector(lightNormalLoc, g.lightNormal, 3, count); - setUniformVector(lightAmbientLoc, g.lightAmbient, 3, count); - setUniformVector(lightDiffuseLoc, g.lightDiffuse, 3, count); - setUniformVector(lightSpecularLoc, g.lightSpecular, 3, count); - setUniformVector(lightFalloffLoc, g.lightFalloffCoefficients, + setUniformVector(lightPositionLoc, currentPG.lightPosition, 4, count); + setUniformVector(lightNormalLoc, currentPG.lightNormal, 3, count); + setUniformVector(lightAmbientLoc, currentPG.lightAmbient, 3, count); + setUniformVector(lightDiffuseLoc, currentPG.lightDiffuse, 3, count); + setUniformVector(lightSpecularLoc, currentPG.lightSpecular, 3, count); + setUniformVector(lightFalloffLoc, currentPG.lightFalloffCoefficients, 3, count); - setUniformVector(lightSpotLoc, g.lightSpotParameters, 2, count); + setUniformVector(lightSpotLoc, currentPG.lightSpotParameters, 2, count); } if (-1 < directionLoc) pgl.enableVertexAttribArray(directionLoc); @@ -1287,8 +1274,8 @@ public class PShader implements PConstants { if (-1 < offsetLoc) pgl.enableVertexAttribArray(offsetLoc); if (-1 < perspectiveLoc) { - if (g.getHint(ENABLE_STROKE_PERSPECTIVE) && - g.nonOrthoProjection()) { + if (currentPG.getHint(ENABLE_STROKE_PERSPECTIVE) && + currentPG.nonOrthoProjection()) { setUniformValue(perspectiveLoc, 1); } else { setUniformValue(perspectiveLoc, 0); @@ -1296,11 +1283,11 @@ public class PShader implements PConstants { } if (-1 < scaleLoc) { - if (g.getHint(DISABLE_OPTIMIZED_STROKE)) { + if (currentPG.getHint(DISABLE_OPTIMIZED_STROKE)) { setUniformValue(scaleLoc, 1.0f, 1.0f, 1.0f); } else { float f = PGL.STROKE_DISPLACEMENT; - if (g.orthoProjection()) { + if (currentPG.orthoProjection()) { setUniformValue(scaleLoc, 1, 1, f); } else { setUniformValue(scaleLoc, f, f, f); @@ -1310,9 +1297,6 @@ public class PShader implements PConstants { } protected void unbindTyped() { - PGraphicsOpenGL g = currentPG.get(); - if (g == null) return; - if (-1 < offsetLoc) pgl.disableVertexAttribArray(offsetLoc); if (-1 < directionLoc) pgl.disableVertexAttribArray(directionLoc); @@ -1337,7 +1321,7 @@ public class PShader implements PConstants { if (-1 < ppixelsLoc) { pgl.enableFBOLayer(); pgl.activeTexture(PGL.TEXTURE0 + ppixelsUnit); - g.unbindFrontTexture(); + currentPG.unbindFrontTexture(); pgl.activeTexture(PGL.TEXTURE0); } diff --git a/core/src/processing/opengl/PShapeOpenGL.java b/core/src/processing/opengl/PShapeOpenGL.java index a9defa8d5..2c9492ec2 100644 --- a/core/src/processing/opengl/PShapeOpenGL.java +++ b/core/src/processing/opengl/PShapeOpenGL.java @@ -40,7 +40,6 @@ import processing.opengl.PGraphicsOpenGL.TessGeometry; import processing.opengl.PGraphicsOpenGL.Tessellator; import processing.opengl.PGraphicsOpenGL.VertexAttribute; -import java.lang.ref.WeakReference; import java.nio.Buffer; import java.util.Arrays; import java.util.HashSet; @@ -76,7 +75,7 @@ public class PShapeOpenGL extends PShape { static protected final int SCALE = 2; static protected final int MATRIX = 3; - protected WeakReference pg; + protected PGraphicsOpenGL pg; protected PGL pgl; protected int context; // The context that created this shape. @@ -326,7 +325,7 @@ public class PShapeOpenGL extends PShape { public PShapeOpenGL(PGraphicsOpenGL pg, int family) { - this.pg = new WeakReference(pg); + this.pg = pg; this.family = family; pgl = pg.pgl; @@ -1128,16 +1127,13 @@ public class PShapeOpenGL extends PShape { protected VertexAttribute attribImpl(String name, int kind, int type, int size) { - PGraphicsOpenGL g = pg.get(); - if (g == null) return null; - if (4 < size) { PGraphics.showWarning("Vertex attributes cannot have more than 4 values"); return null; } VertexAttribute attrib = polyAttribs.get(name); if (attrib == null) { - attrib = new VertexAttribute(g, name, kind, type, size); + attrib = new VertexAttribute(pg, name, kind, type, size); polyAttribs.put(name, attrib); inGeo.initAttrib(attrib); } @@ -2489,9 +2485,6 @@ public class PShapeOpenGL extends PShape { @Override public PShape getTessellation() { - PGraphicsOpenGL g = pg.get(); - if (g == null) return null; - updateTessellation(); float[] vertices = tessGeo.polyVertices; @@ -2511,7 +2504,7 @@ public class PShapeOpenGL extends PShape { // PGraphics.showWarning("This shape is not either 2D or 3D!"); // return null; // } - tess = g.createShapeFamily(PShape.GEOMETRY); + tess = pg.createShapeFamily(PShape.GEOMETRY); tess.set3D(is3D); // if this is a 3D shape, make the new shape 3D as well tess.beginShape(TRIANGLES); tess.noStroke(); @@ -2761,9 +2754,6 @@ public class PShapeOpenGL extends PShape { protected void tessellate() { - PGraphicsOpenGL g = pg.get(); - if (g == null) return; - if (root == this && parent == null) { // Root shape if (polyAttribs == null) { polyAttribs = PGraphicsOpenGL.newAttributeMap(); @@ -2771,7 +2761,7 @@ public class PShapeOpenGL extends PShape { } if (tessGeo == null) { - tessGeo = PGraphicsOpenGL.newTessGeometry(g, polyAttribs, PGraphicsOpenGL.RETAINED); + tessGeo = PGraphicsOpenGL.newTessGeometry(pg, polyAttribs, PGraphicsOpenGL.RETAINED); } tessGeo.clear(); for (int i = 0; i < polyAttribs.size(); i++) { @@ -2814,9 +2804,6 @@ public class PShapeOpenGL extends PShape { } protected void tessellateImpl() { - PGraphicsOpenGL g = pg.get(); - if (g == null) return; - tessGeo = root.tessGeo; firstPolyIndexCache = -1; @@ -2848,7 +2835,7 @@ public class PShapeOpenGL extends PShape { tessellator.setStrokeWeight(strokeWeight); tessellator.setStrokeCap(strokeCap); tessellator.setStrokeJoin(strokeJoin); - tessellator.setRenderer(g); + tessellator.setRenderer(pg); tessellator.setTransform(matrix); tessellator.set3D(is3D()); @@ -3258,9 +3245,6 @@ public class PShapeOpenGL extends PShape { protected void tessellateSphere() { - PGraphicsOpenGL g = pg.get(); - if (g == null) return; - float r = 0; int nu = sphereDetailU; int nv = sphereDetailV; @@ -3277,10 +3261,10 @@ public class PShapeOpenGL extends PShape { if (nu < 3 || nv < 2) { nu = nv = 30; } - int savedDetailU = g.sphereDetailU; - int savedDetailV = g.sphereDetailV; - if (g.sphereDetailU != nu || g.sphereDetailV != nv) { - g.sphereDetail(nu, nv); + int savedDetailU = pg.sphereDetailU; + int savedDetailV = pg.sphereDetailV; + if (pg.sphereDetailU != nu || pg.sphereDetailV != nv) { + pg.sphereDetail(nu, nv); } inGeo.setMaterial(fillColor, strokeColor, strokeWeight, @@ -3289,7 +3273,7 @@ public class PShapeOpenGL extends PShape { tessellator.tessellateTriangles(indices); if (savedDetailU != nu || savedDetailV != nv) { - g.sphereDetail(savedDetailU, savedDetailV); + pg.sphereDetail(savedDetailU, savedDetailV); } } @@ -3421,47 +3405,35 @@ public class PShapeOpenGL extends PShape { } protected void saveBezierVertexSettings() { - PGraphicsOpenGL g = pg.get(); - if (g == null) return; - - savedBezierDetail = g.bezierDetail; - if (g.bezierDetail != bezierDetail) { - g.bezierDetail(bezierDetail); + savedBezierDetail = pg.bezierDetail; + if (pg.bezierDetail != bezierDetail) { + pg.bezierDetail(bezierDetail); } } protected void restoreBezierVertexSettings() { - PGraphicsOpenGL g = pg.get(); - if (g == null) return; - if (savedBezierDetail != bezierDetail) { - g.bezierDetail(savedBezierDetail); + pg.bezierDetail(savedBezierDetail); } } protected void saveCurveVertexSettings() { - PGraphicsOpenGL g = pg.get(); - if (g == null) return; - - savedCurveDetail = g.curveDetail; - savedCurveTightness = g.curveTightness; - if (g.curveDetail != curveDetail) { - g.curveDetail(curveDetail); + savedCurveDetail = pg.curveDetail; + savedCurveTightness = pg.curveTightness; + if (pg.curveDetail != curveDetail) { + pg.curveDetail(curveDetail); } - if (g.curveTightness != curveTightness) { - g.curveTightness(curveTightness); + if (pg.curveTightness != curveTightness) { + pg.curveTightness(curveTightness); } } protected void restoreCurveVertexSettings() { - PGraphicsOpenGL g = pg.get(); - if (g == null) return; - if (savedCurveDetail != curveDetail) { - g.curveDetail(savedCurveDetail); + pg.curveDetail(savedCurveDetail); } if (savedCurveTightness != curveTightness) { - g.curveTightness(savedCurveTightness); + pg.curveTightness(savedCurveTightness); } } @@ -3858,9 +3830,6 @@ public class PShapeOpenGL extends PShape { protected void initPolyBuffers() { - PGraphicsOpenGL g = pg.get(); - if (g == null) return; - int size = tessGeo.polyVertexCount; int sizef = size * PGL.SIZEOF_FLOAT; int sizei = size * PGL.SIZEOF_INT; @@ -3868,7 +3837,7 @@ public class PShapeOpenGL extends PShape { tessGeo.updatePolyVerticesBuffer(); if (bufPolyVertex == null) // glPolyVertex = PGraphicsOpenGL.createVertexBufferObject(context, pgl); - bufPolyVertex = new VertexBuffer(g, PGL.ARRAY_BUFFER, 4, PGL.SIZEOF_FLOAT); + bufPolyVertex = new VertexBuffer(pg, PGL.ARRAY_BUFFER, 4, PGL.SIZEOF_FLOAT); pgl.bindBuffer(PGL.ARRAY_BUFFER, bufPolyVertex.glId); pgl.bufferData(PGL.ARRAY_BUFFER, 4 * sizef, tessGeo.polyVerticesBuffer, glUsage); @@ -3876,7 +3845,7 @@ public class PShapeOpenGL extends PShape { tessGeo.updatePolyColorsBuffer(); if (bufPolyColor == null) // glPolyColor = PGraphicsOpenGL.createVertexBufferObject(context, pgl); - bufPolyColor = new VertexBuffer(g, PGL.ARRAY_BUFFER, 1, PGL.SIZEOF_INT); + bufPolyColor = new VertexBuffer(pg, PGL.ARRAY_BUFFER, 1, PGL.SIZEOF_INT); pgl.bindBuffer(PGL.ARRAY_BUFFER, bufPolyColor.glId); pgl.bufferData(PGL.ARRAY_BUFFER, sizei, tessGeo.polyColorsBuffer, glUsage); @@ -3884,7 +3853,7 @@ public class PShapeOpenGL extends PShape { tessGeo.updatePolyNormalsBuffer(); if (bufPolyNormal == null) // glPolyNormal = PGraphicsOpenGL.createVertexBufferObject(context, pgl); - bufPolyNormal = new VertexBuffer(g, PGL.ARRAY_BUFFER, 3, PGL.SIZEOF_FLOAT); + bufPolyNormal = new VertexBuffer(pg, PGL.ARRAY_BUFFER, 3, PGL.SIZEOF_FLOAT); pgl.bindBuffer(PGL.ARRAY_BUFFER, bufPolyNormal.glId); pgl.bufferData(PGL.ARRAY_BUFFER, 3 * sizef, tessGeo.polyNormalsBuffer, glUsage); @@ -3892,7 +3861,7 @@ public class PShapeOpenGL extends PShape { tessGeo.updatePolyTexCoordsBuffer(); if (bufPolyTexcoord == null) // glPolyTexcoord = PGraphicsOpenGL.createVertexBufferObject(context, pgl); - bufPolyTexcoord = new VertexBuffer(g, PGL.ARRAY_BUFFER, 2, PGL.SIZEOF_FLOAT); + bufPolyTexcoord = new VertexBuffer(pg, PGL.ARRAY_BUFFER, 2, PGL.SIZEOF_FLOAT); pgl.bindBuffer(PGL.ARRAY_BUFFER, bufPolyTexcoord.glId); pgl.bufferData(PGL.ARRAY_BUFFER, 2 * sizef, tessGeo.polyTexCoordsBuffer, glUsage); @@ -3900,7 +3869,7 @@ public class PShapeOpenGL extends PShape { tessGeo.updatePolyAmbientBuffer(); if (bufPolyAmbient == null) // glPolyAmbient = PGraphicsOpenGL.createVertexBufferObject(context, pgl); - bufPolyAmbient = new VertexBuffer(g, PGL.ARRAY_BUFFER, 1, PGL.SIZEOF_INT); + bufPolyAmbient = new VertexBuffer(pg, PGL.ARRAY_BUFFER, 1, PGL.SIZEOF_INT); pgl.bindBuffer(PGL.ARRAY_BUFFER, bufPolyAmbient.glId); pgl.bufferData(PGL.ARRAY_BUFFER, sizei, tessGeo.polyAmbientBuffer, glUsage); @@ -3908,7 +3877,7 @@ public class PShapeOpenGL extends PShape { tessGeo.updatePolySpecularBuffer(); if (bufPolySpecular == null) // glPolySpecular = PGraphicsOpenGL.createVertexBufferObject(context, pgl); - bufPolySpecular = new VertexBuffer(g, PGL.ARRAY_BUFFER, 1, PGL.SIZEOF_INT); + bufPolySpecular = new VertexBuffer(pg, PGL.ARRAY_BUFFER, 1, PGL.SIZEOF_INT); pgl.bindBuffer(PGL.ARRAY_BUFFER, bufPolySpecular.glId); pgl.bufferData(PGL.ARRAY_BUFFER, sizei, tessGeo.polySpecularBuffer, glUsage); @@ -3916,7 +3885,7 @@ public class PShapeOpenGL extends PShape { tessGeo.updatePolyEmissiveBuffer(); if (bufPolyEmissive == null) // glPolyEmissive = PGraphicsOpenGL.createVertexBufferObject(context, pgl); - bufPolyEmissive = new VertexBuffer(g, PGL.ARRAY_BUFFER, 1, PGL.SIZEOF_INT); + bufPolyEmissive = new VertexBuffer(pg, PGL.ARRAY_BUFFER, 1, PGL.SIZEOF_INT); pgl.bindBuffer(PGL.ARRAY_BUFFER, bufPolyEmissive.glId); pgl.bufferData(PGL.ARRAY_BUFFER, sizei, tessGeo.polyEmissiveBuffer, glUsage); @@ -3924,7 +3893,7 @@ public class PShapeOpenGL extends PShape { tessGeo.updatePolyShininessBuffer(); if (bufPolyShininess == null) // glPolyShininess = PGraphicsOpenGL.createVertexBufferObject(context, pgl); - bufPolyShininess = new VertexBuffer(g, PGL.ARRAY_BUFFER, 1, PGL.SIZEOF_FLOAT); + bufPolyShininess = new VertexBuffer(pg, PGL.ARRAY_BUFFER, 1, PGL.SIZEOF_FLOAT); pgl.bindBuffer(PGL.ARRAY_BUFFER, bufPolyShininess.glId); pgl.bufferData(PGL.ARRAY_BUFFER, sizef, tessGeo.polyShininessBuffer, glUsage); @@ -3943,7 +3912,7 @@ public class PShapeOpenGL extends PShape { tessGeo.updatePolyIndicesBuffer(); if (bufPolyIndex == null) // glPolyIndex = PGraphicsOpenGL.createVertexBufferObject(context, pgl); - bufPolyIndex = new VertexBuffer(g, PGL.ELEMENT_ARRAY_BUFFER, 1, PGL.SIZEOF_INDEX, true); + bufPolyIndex = new VertexBuffer(pg, PGL.ELEMENT_ARRAY_BUFFER, 1, PGL.SIZEOF_INDEX, true); pgl.bindBuffer(PGL.ELEMENT_ARRAY_BUFFER, bufPolyIndex.glId); pgl.bufferData(PGL.ELEMENT_ARRAY_BUFFER, tessGeo.polyIndexCount * PGL.SIZEOF_INDEX, @@ -3954,9 +3923,6 @@ public class PShapeOpenGL extends PShape { protected void initLineBuffers() { - PGraphicsOpenGL g = pg.get(); - if (g == null) return; - int size = tessGeo.lineVertexCount; int sizef = size * PGL.SIZEOF_FLOAT; int sizei = size * PGL.SIZEOF_INT; @@ -3964,7 +3930,7 @@ public class PShapeOpenGL extends PShape { tessGeo.updateLineVerticesBuffer(); if (bufLineVertex == null) // glLineVertex = PGraphicsOpenGL.createVertexBufferObject(context, pgl); - bufLineVertex = new VertexBuffer(g, PGL.ARRAY_BUFFER, 4, PGL.SIZEOF_FLOAT); + bufLineVertex = new VertexBuffer(pg, PGL.ARRAY_BUFFER, 4, PGL.SIZEOF_FLOAT); pgl.bindBuffer(PGL.ARRAY_BUFFER, bufLineVertex.glId); pgl.bufferData(PGL.ARRAY_BUFFER, 4 * sizef, tessGeo.lineVerticesBuffer, glUsage); @@ -3972,7 +3938,7 @@ public class PShapeOpenGL extends PShape { tessGeo.updateLineColorsBuffer(); if (bufLineColor == null) // glLineColor = PGraphicsOpenGL.createVertexBufferObject(context, pgl); - bufLineColor = new VertexBuffer(g, PGL.ARRAY_BUFFER, 1, PGL.SIZEOF_INT); + bufLineColor = new VertexBuffer(pg, PGL.ARRAY_BUFFER, 1, PGL.SIZEOF_INT); pgl.bindBuffer(PGL.ARRAY_BUFFER, bufLineColor.glId); pgl.bufferData(PGL.ARRAY_BUFFER, sizei, tessGeo.lineColorsBuffer, glUsage); @@ -3980,7 +3946,7 @@ public class PShapeOpenGL extends PShape { tessGeo.updateLineDirectionsBuffer(); if (bufLineAttrib == null) // glLineAttrib = PGraphicsOpenGL.createVertexBufferObject(context, pgl); - bufLineAttrib = new VertexBuffer(g, PGL.ARRAY_BUFFER, 4, PGL.SIZEOF_FLOAT); + bufLineAttrib = new VertexBuffer(pg, PGL.ARRAY_BUFFER, 4, PGL.SIZEOF_FLOAT); pgl.bindBuffer(PGL.ARRAY_BUFFER, bufLineAttrib.glId); pgl.bufferData(PGL.ARRAY_BUFFER, 4 * sizef, tessGeo.lineDirectionsBuffer, glUsage); @@ -3990,7 +3956,7 @@ public class PShapeOpenGL extends PShape { tessGeo.updateLineIndicesBuffer(); if (bufLineIndex == null) // glLineIndex = PGraphicsOpenGL.createVertexBufferObject(context, pgl); - bufLineIndex = new VertexBuffer(g, PGL.ELEMENT_ARRAY_BUFFER, 1, PGL.SIZEOF_INDEX, true); + bufLineIndex = new VertexBuffer(pg, PGL.ELEMENT_ARRAY_BUFFER, 1, PGL.SIZEOF_INDEX, true); pgl.bindBuffer(PGL.ELEMENT_ARRAY_BUFFER, bufLineIndex.glId); pgl.bufferData(PGL.ELEMENT_ARRAY_BUFFER, tessGeo.lineIndexCount * PGL.SIZEOF_INDEX, @@ -4001,9 +3967,6 @@ public class PShapeOpenGL extends PShape { protected void initPointBuffers() { - PGraphicsOpenGL g = pg.get(); - if (g == null) return; - int size = tessGeo.pointVertexCount; int sizef = size * PGL.SIZEOF_FLOAT; int sizei = size * PGL.SIZEOF_INT; @@ -4011,7 +3974,7 @@ public class PShapeOpenGL extends PShape { tessGeo.updatePointVerticesBuffer(); if (bufPointVertex == null) // glPointVertex = PGraphicsOpenGL.createVertexBufferObject(context, pgl); - bufPointVertex = new VertexBuffer(g, PGL.ARRAY_BUFFER, 4, PGL.SIZEOF_FLOAT); + bufPointVertex = new VertexBuffer(pg, PGL.ARRAY_BUFFER, 4, PGL.SIZEOF_FLOAT); pgl.bindBuffer(PGL.ARRAY_BUFFER, bufPointVertex.glId); pgl.bufferData(PGL.ARRAY_BUFFER, 4 * sizef, tessGeo.pointVerticesBuffer, glUsage); @@ -4019,7 +3982,7 @@ public class PShapeOpenGL extends PShape { tessGeo.updatePointColorsBuffer(); if (bufPointColor == null) // glPointColor = PGraphicsOpenGL.createVertexBufferObject(context, pgl); - bufPointColor = new VertexBuffer(g, PGL.ARRAY_BUFFER, 1, PGL.SIZEOF_INT); + bufPointColor = new VertexBuffer(pg, PGL.ARRAY_BUFFER, 1, PGL.SIZEOF_INT); pgl.bindBuffer(PGL.ARRAY_BUFFER, bufPointColor.glId); pgl.bufferData(PGL.ARRAY_BUFFER, sizei, tessGeo.pointColorsBuffer, glUsage); @@ -4027,7 +3990,7 @@ public class PShapeOpenGL extends PShape { tessGeo.updatePointOffsetsBuffer(); if (bufPointAttrib == null) // glPointAttrib = PGraphicsOpenGL.createVertexBufferObject(context, pgl); - bufPointAttrib = new VertexBuffer(g, PGL.ARRAY_BUFFER, 2, PGL.SIZEOF_FLOAT); + bufPointAttrib = new VertexBuffer(pg, PGL.ARRAY_BUFFER, 2, PGL.SIZEOF_FLOAT); pgl.bindBuffer(PGL.ARRAY_BUFFER, bufPointAttrib.glId); pgl.bufferData(PGL.ARRAY_BUFFER, 2 * sizef, tessGeo.pointOffsetsBuffer, glUsage); @@ -4037,7 +4000,7 @@ public class PShapeOpenGL extends PShape { tessGeo.updatePointIndicesBuffer(); if (bufPointIndex == null) // glPointIndex = PGraphicsOpenGL.createVertexBufferObject(context, pgl); - bufPointIndex = new VertexBuffer(g, PGL.ELEMENT_ARRAY_BUFFER, 1, PGL.SIZEOF_INDEX, true); + bufPointIndex = new VertexBuffer(pg, PGL.ELEMENT_ARRAY_BUFFER, 1, PGL.SIZEOF_INDEX, true); pgl.bindBuffer(PGL.ELEMENT_ARRAY_BUFFER, bufPointIndex.glId); pgl.bufferData(PGL.ELEMENT_ARRAY_BUFFER, tessGeo.pointIndexCount * PGL.SIZEOF_INDEX, diff --git a/core/src/processing/opengl/Texture.java b/core/src/processing/opengl/Texture.java index aecf87689..4c5b85641 100644 --- a/core/src/processing/opengl/Texture.java +++ b/core/src/processing/opengl/Texture.java @@ -29,7 +29,6 @@ import processing.core.PConstants; import processing.core.PGraphics; import processing.opengl.PGraphicsOpenGL.GLResourceTexture; -import java.lang.ref.WeakReference; import java.lang.reflect.Method; import java.nio.ByteBuffer; import java.nio.IntBuffer; @@ -91,7 +90,7 @@ public class Texture implements PConstants { public int glHeight; private GLResourceTexture glres; - protected WeakReference pg; + protected PGraphicsOpenGL pg; protected PGL pgl; // The interface between Processing and OpenGL. protected int context; // The context that created this texture. protected boolean colorBuffer; // true if it is the color attachment of @@ -132,7 +131,7 @@ public class Texture implements PConstants { public Texture(PGraphicsOpenGL pg) { - this.pg = new WeakReference(pg); + this.pg = pg; pgl = pg.pgl; context = pgl.createEmptyContext(); @@ -161,7 +160,7 @@ public class Texture implements PConstants { * @param params Parameters */ public Texture(PGraphicsOpenGL pg, int width, int height, Object params) { - this.pg = new WeakReference(pg); + this.pg = pg; pgl = pg.pgl; context = pgl.createEmptyContext(); @@ -247,15 +246,12 @@ public class Texture implements PConstants { public void resize(int wide, int high) { - PGraphicsOpenGL g = pg.get(); - if (g == null) return; - // Marking the texture object as finalized so it is deleted // when creating the new texture. dispose(); // Creating new texture with the appropriate size. - Texture tex = new Texture(g, wide, high, getParameters()); + Texture tex = new Texture(pg, wide, high, getParameters()); // Copying the contents of this texture into tex. tex.set(this); @@ -441,9 +437,6 @@ public class Texture implements PConstants { * Copy texture to pixels. Involves video memory to main memory transfer (slow). */ public void get(int[] pixels) { - PGraphicsOpenGL g = pg.get(); - if (g == null) return; - if (pixels == null) { throw new RuntimeException("Trying to copy texture to null pixels array"); } @@ -453,17 +446,17 @@ public class Texture implements PConstants { } if (tempFbo == null) { - tempFbo = new FrameBuffer(g, glWidth, glHeight); + tempFbo = new FrameBuffer(pg, glWidth, glHeight); } // Attaching the texture to the color buffer of a FBO, binding the FBO and // reading the pixels from the current draw buffer (which is the color // buffer of the FBO). tempFbo.setColorBuffer(this); - g.pushFramebuffer(); - g.setFramebuffer(tempFbo); + pg.pushFramebuffer(); + pg.setFramebuffer(tempFbo); tempFbo.readPixels(); - g.popFramebuffer(); + pg.popFramebuffer(); tempFbo.getPixels(pixels); convertToARGB(pixels); @@ -1221,15 +1214,12 @@ public class Texture implements PConstants { // Copies source texture tex into this. protected void copyTexture(Texture tex, int x, int y, int w, int h, boolean scale) { - PGraphicsOpenGL g = pg.get(); - if (g == null) return; - if (tex == null) { throw new RuntimeException("Source texture is null"); } if (tempFbo == null) { - tempFbo = new FrameBuffer(g, glWidth, glHeight); + tempFbo = new FrameBuffer(pg, glWidth, glHeight); } // This texture is the color (destination) buffer of the FBO. @@ -1237,8 +1227,8 @@ public class Texture implements PConstants { tempFbo.disableDepthTest(); // FBO copy: - g.pushFramebuffer(); - g.setFramebuffer(tempFbo); + pg.pushFramebuffer(); + pg.setFramebuffer(tempFbo); // Clear the color buffer to make sure that the alpha channel is set to // full transparency pgl.clearColor(0, 0, 0, 0); @@ -1258,7 +1248,7 @@ public class Texture implements PConstants { 0, 0, tempFbo.width, tempFbo.height, 1, x, y, x + w, y + h, x, y, x + w, y + h); } - g.popFramebuffer(); + pg.popFramebuffer(); updateTexels(x, y, w, h); } @@ -1268,11 +1258,8 @@ public class Texture implements PConstants { protected void copyTexture(int texTarget, int texName, int texWidth, int texHeight, int x, int y, int w, int h, boolean scale) { - PGraphicsOpenGL g = pg.get(); - if (g == null) return; - if (tempFbo == null) { - tempFbo = new FrameBuffer(g, glWidth, glHeight); + tempFbo = new FrameBuffer(pg, glWidth, glHeight); } // This texture is the color (destination) buffer of the FBO. @@ -1280,8 +1267,8 @@ public class Texture implements PConstants { tempFbo.disableDepthTest(); // FBO copy: - g.pushFramebuffer(); - g.setFramebuffer(tempFbo); + pg.pushFramebuffer(); + pg.setFramebuffer(tempFbo); if (scale) { // Rendering tex into "this", and scaling the source rectangle // to cover the entire destination region. @@ -1297,7 +1284,7 @@ public class Texture implements PConstants { 0, 0, tempFbo.width, tempFbo.height, x, y, w, h, x, y, w, h); } - g.popFramebuffer(); + pg.popFramebuffer(); updateTexels(x, y, w, h); }