From 2cf37e49114c4bd271362a8af03fad2e754f60c5 Mon Sep 17 00:00:00 2001 From: Manindra Moharana Date: Wed, 30 Oct 2013 23:48:59 +0530 Subject: [PATCH 01/51] Fixes #2082, Find Replace inf loop --- app/src/processing/app/FindReplace.java | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/app/src/processing/app/FindReplace.java b/app/src/processing/app/FindReplace.java index 922bcc9ba..57c851dd7 100644 --- a/app/src/processing/app/FindReplace.java +++ b/app/src/processing/app/FindReplace.java @@ -454,9 +454,22 @@ public class FindReplace extends JFrame { editor.setSelection(0, 0); boolean foundAtLeastOne = false; - while (true) { + int startTab = -1, startIndex = -1, c = 50000; + // you couldn't seriously be replacing 50K times o_O + while (--c > 0) { if (find(false, false)) { - foundAtLeastOne = true; + if(editor.getSketch().getCurrentCodeIndex() == startTab + && editor.getSelectionStart() == startIndex){ + // we've reached where we started, so stop the replace + Toolkit.beep(); + editor.statusNotice("Reached beginning of search!"); + break; + } + if(!foundAtLeastOne){ + foundAtLeastOne = true; + startTab = editor.getSketch().getCurrentCodeIndex(); + startIndex = editor.getSelectionStart(); + } replace(); } else { break; From 24921b631b5467e3fc2bf7b8803eb982eb8ab481 Mon Sep 17 00:00:00 2001 From: boubpopsyteam Date: Mon, 25 Nov 2013 02:14:07 +0100 Subject: [PATCH 02/51] Update PApplet.java Handle alpha in color(float,float) --- core/src/processing/core/PApplet.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/processing/core/PApplet.java b/core/src/processing/core/PApplet.java index 5b0107c2c..f038f0fdf 100755 --- a/core/src/processing/core/PApplet.java +++ b/core/src/processing/core/PApplet.java @@ -10115,7 +10115,7 @@ public class PApplet extends Applet int alpha = (int) falpha; if (gray > 255) gray = 255; else if (gray < 0) gray = 0; if (alpha > 255) alpha = 255; else if (alpha < 0) alpha = 0; - return 0xff000000 | (gray << 16) | (gray << 8) | gray; + return (alpha << 24) | (gray << 16) | (gray << 8) | gray; } return g.color(fgray, falpha); } From 7d9e51e9ae6e5be5338e225457b1fdd9f7d62dd7 Mon Sep 17 00:00:00 2001 From: gohai Date: Tue, 17 Dec 2013 00:02:07 -0800 Subject: [PATCH 03/51] Appbundler: Add Contents/Java to java.library.path for loadLibrary to find .jnilib files Upon including processing.serial.*, the java.library.path of the sketch - when compiled normally - gets expanded to include the location of the specific library subfolder. For exported sketches on MacOS, java.library.path points just to Contents/MacOS inside the application, which causes loadLibrary inside custom libraries to fail. Since those library files (*.jnilib) get copied into Contents/Java, change the java.library.path to include this location as well. Somewhat confident that this shouldn't break things, but this is touching core infrastructure. --- build/macosx/appbundler/native/main.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build/macosx/appbundler/native/main.m b/build/macosx/appbundler/native/main.m index 4a365e304..95a4485c4 100644 --- a/build/macosx/appbundler/native/main.m +++ b/build/macosx/appbundler/native/main.m @@ -185,7 +185,7 @@ int launch(char *commandName) { */ // Set the library path - NSString *libraryPath = [NSString stringWithFormat:@"-Djava.library.path=%@/Contents/MacOS", mainBundlePath]; + NSString *libraryPath = [NSString stringWithFormat:@"-Djava.library.path=:%@/Contents/Java:%@/Contents/MacOS", mainBundlePath, mainBundlePath]; // Get the VM options NSArray *options = [infoDictionary objectForKey:@JVM_OPTIONS_KEY]; From 8fcc868767b3a571a35ddf44bd274bda21c54101 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Pr=C3=A9vot?= Date: Thu, 9 Jan 2014 18:10:48 -0400 Subject: [PATCH 04/51] dxf: Fix the clean target --- java/libraries/dxf/build.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/java/libraries/dxf/build.xml b/java/libraries/dxf/build.xml index 5f4719dac..4e77c39aa 100755 --- a/java/libraries/dxf/build.xml +++ b/java/libraries/dxf/build.xml @@ -3,7 +3,7 @@ - + From 616c3b980a4ed941a3423332e8e710093cfc7cd4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Pr=C3=A9vot?= Date: Thu, 9 Jan 2014 18:13:19 -0400 Subject: [PATCH 05/51] pdf: Fix the clean target --- java/libraries/pdf/build.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/java/libraries/pdf/build.xml b/java/libraries/pdf/build.xml index c122c2876..e10313534 100755 --- a/java/libraries/pdf/build.xml +++ b/java/libraries/pdf/build.xml @@ -3,7 +3,7 @@ - + From be28e4d2201b35e6364ee02347fd894328e278ca Mon Sep 17 00:00:00 2001 From: George Bateman Date: Fri, 10 Jan 2014 17:48:20 +0000 Subject: [PATCH 06/51] Update Brackets.java Fixed bug first spotted at http://code.google.com/p/processing/issues/detail?id=1032 where brackets aren't seen directly after a slash. An example that failed: min(1, x/(1.0)); min(1, x /(1.0)); --- app/src/processing/app/syntax/Brackets.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/processing/app/syntax/Brackets.java b/app/src/processing/app/syntax/Brackets.java index c35e9c359..1fd46ec27 100644 --- a/app/src/processing/app/syntax/Brackets.java +++ b/app/src/processing/app/syntax/Brackets.java @@ -90,7 +90,7 @@ public class Brackets { readComment(text); } else if (d == '*') { readMLComment(text); - } + } else pos--; // Go back because there isn't a comment. } else if (c == '"' || c == '\'') { readString(text, c); } else if (c == '{' || c == '[' || c == '(' || c == '}' || c == ']' From b57c1c489fc741dc3df9f24cc8963224f1475d95 Mon Sep 17 00:00:00 2001 From: codeanticode Date: Thu, 16 Jan 2014 12:11:28 -0500 Subject: [PATCH 09/51] adjust stroke weight in HsvSpace example --- java/libraries/video/examples/Capture/HsvSpace/HsvSpace.pde | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/java/libraries/video/examples/Capture/HsvSpace/HsvSpace.pde b/java/libraries/video/examples/Capture/HsvSpace/HsvSpace.pde index 9f72fe96f..c77208410 100644 --- a/java/libraries/video/examples/Capture/HsvSpace/HsvSpace.pde +++ b/java/libraries/video/examples/Capture/HsvSpace/HsvSpace.pde @@ -80,8 +80,9 @@ void draw() { rotateY(radians(36 + leftRightAngle)); //, 0, 1, 0); rotateX(radians(-228 + upDownAngle)); //, 1, 0, 0); - if (blobby) { - stroke(0.35, 0.35, 0.25, 0.15); + strokeWeight(0.1); + if (blobby) { + stroke(0.35, 0.35, 0.25, 0.15); wireCone(MAX_RADIUS, MAX_RADIUS * CONE_HEIGHT, 18, 18); } else { From 5be1f111459d45e38cede2eb5ca3c6ac5fe941f4 Mon Sep 17 00:00:00 2001 From: codeanticode Date: Thu, 16 Jan 2014 16:21:10 -0500 Subject: [PATCH 10/51] call setLoaded() in PImage.loadPixels() --- core/src/processing/core/PImage.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/processing/core/PImage.java b/core/src/processing/core/PImage.java index e3994c91e..430d67228 100644 --- a/core/src/processing/core/PImage.java +++ b/core/src/processing/core/PImage.java @@ -477,7 +477,7 @@ public class PImage implements PConstants, Cloneable { if (pixels == null || pixels.length != width*height) { pixels = new int[width*height]; } - isLoaded(); + setLoaded(); } From cf8e03ff702a922e3b762827ae09621ac0132342 Mon Sep 17 00:00:00 2001 From: codeanticode Date: Thu, 16 Jan 2014 18:04:15 -0500 Subject: [PATCH 11/51] fix #2202 --- core/src/processing/opengl/Texture.java | 2 +- .../video/src/processing/video/Capture.java | 24 +++++++++++++++++ .../video/src/processing/video/Movie.java | 26 ++++++++++++++++++- 3 files changed, 50 insertions(+), 2 deletions(-) diff --git a/core/src/processing/opengl/Texture.java b/core/src/processing/opengl/Texture.java index a8ea6662c..555ae5986 100644 --- a/core/src/processing/opengl/Texture.java +++ b/core/src/processing/opengl/Texture.java @@ -881,7 +881,7 @@ public class Texture implements PConstants { public void getBufferPixels(int[] pixels) { BufferData data = null; if (usedBuffers != null && 0 < usedBuffers.size()) { - // the last used buffer is the one currently stored in the opengl the + // the last used buffer is the one currently stored in the opengl // texture data = usedBuffers.getLast(); } else if (bufferCache != null && 0 < bufferCache.size()) { diff --git a/java/libraries/video/src/processing/video/Capture.java b/java/libraries/video/src/processing/video/Capture.java index 7cad36876..8ff7de94e 100644 --- a/java/libraries/video/src/processing/video/Capture.java +++ b/java/libraries/video/src/processing/video/Capture.java @@ -107,6 +107,7 @@ public class Capture extends PImage implements PConstants { protected int reqHeight; protected boolean useBufferSink = false; + protected boolean outdatedPixels = true; protected Object bufferSink; protected Method sinkCopyMethod; protected Method sinkSetMethod; @@ -375,6 +376,7 @@ public class Capture extends PImage implements PConstants { } if (useBufferSink) { // The native buffer from gstreamer is copied to the buffer sink. + outdatedPixels = true; if (natBuffer == null) { return; } @@ -442,10 +444,32 @@ public class Capture extends PImage implements PConstants { e.printStackTrace(); } } + + // super.loadPixels() sets loaded to true, but in the useBufferSink mode, + // the contents of the pixels array is overriden by the buffers coming + // from gstreamer, so we don't want PGraphicsOpenGL replacing the OpenGL + // texture with the pixels. + setLoaded(false); + outdatedPixels = false; } } + public int get(int x, int y) { + if (outdatedPixels) loadPixels(); + return super.get(x, y); + } + + + protected void getImpl(int sourceX, int sourceY, + int sourceWidth, int sourceHeight, + PImage target, int targetX, int targetY) { + if (outdatedPixels) loadPixels(); + super.getImpl(sourceX, sourceY, sourceWidth, sourceHeight, + target, targetX, targetY); + } + + //////////////////////////////////////////////////////////// // List methods. diff --git a/java/libraries/video/src/processing/video/Movie.java b/java/libraries/video/src/processing/video/Movie.java index fcb0056e6..694f92f27 100644 --- a/java/libraries/video/src/processing/video/Movie.java +++ b/java/libraries/video/src/processing/video/Movie.java @@ -79,6 +79,7 @@ public class Movie extends PImage implements PConstants { protected boolean seeking = false; protected boolean useBufferSink = false; + protected boolean outdatedPixels = true; protected Object bufferSink; protected Method sinkCopyMethod; protected Method sinkSetMethod; @@ -487,6 +488,7 @@ public class Movie extends PImage implements PConstants { } if (useBufferSink) { // The native buffer from gstreamer is copied to the buffer sink. + outdatedPixels = true; if (natBuffer == null) { return; } @@ -565,11 +567,33 @@ public class Movie extends PImage implements PConstants { } catch (Exception e) { e.printStackTrace(); } - } + } + + // super.loadPixels() sets loaded to true, but in the useBufferSink mode, + // the contents of the pixels array is overriden by the buffers coming + // from gstreamer, so we don't want PGraphicsOpenGL replacing the OpenGL + // texture with the pixels. + setLoaded(false); + outdatedPixels = false; } } + public int get(int x, int y) { + if (outdatedPixels) loadPixels(); + return super.get(x, y); + } + + + protected void getImpl(int sourceX, int sourceY, + int sourceWidth, int sourceHeight, + PImage target, int targetX, int targetY) { + if (outdatedPixels) loadPixels(); + super.getImpl(sourceX, sourceY, sourceWidth, sourceHeight, + target, targetX, targetY); + } + + //////////////////////////////////////////////////////////// // Initialization methods. From 376bbb1938b8e77370fb8c273a2c00fecc575625 Mon Sep 17 00:00:00 2001 From: codeanticode Date: Thu, 16 Jan 2014 18:17:40 -0500 Subject: [PATCH 12/51] removed commented out method --- core/src/processing/opengl/PJOGL.java | 5 ----- 1 file changed, 5 deletions(-) diff --git a/core/src/processing/opengl/PJOGL.java b/core/src/processing/opengl/PJOGL.java index 4ebb60f6e..8dc1fa95e 100644 --- a/core/src/processing/opengl/PJOGL.java +++ b/core/src/processing/opengl/PJOGL.java @@ -219,11 +219,6 @@ public class PJOGL extends PGL { } -// public Object getCanvas() { -// return canvas; -// } - - @Override protected void setFps(float fps) { if (!setFps || targetFps != fps) { From a8a15d112e1c4af17eb5ac1b4cf8da8174157463 Mon Sep 17 00:00:00 2001 From: codeanticode Date: Sun, 19 Jan 2014 01:56:55 -0500 Subject: [PATCH 13/51] making pgl non-static member of PGraphicsOpenGL --- core/src/processing/opengl/FontTexture.java | 6 +- core/src/processing/opengl/FrameBuffer.java | 18 +- .../processing/opengl/PGraphicsOpenGL.java | 185 +++++++++--------- core/src/processing/opengl/PShader.java | 20 +- core/src/processing/opengl/PShapeOpenGL.java | 70 +++---- core/src/processing/opengl/Texture.java | 6 +- 6 files changed, 154 insertions(+), 151 deletions(-) diff --git a/core/src/processing/opengl/FontTexture.java b/core/src/processing/opengl/FontTexture.java index 2a9fbadeb..e32d4f440 100644 --- a/core/src/processing/opengl/FontTexture.java +++ b/core/src/processing/opengl/FontTexture.java @@ -63,11 +63,11 @@ class FontTexture implements PConstants { protected TextureInfo[] glyphTexinfos; protected HashMap texinfoMap; - public FontTexture(PGraphicsOpenGL pg, PFont font, boolean is3D) { - pgl = PGraphicsOpenGL.pgl; + public FontTexture(PFont font, boolean is3D) { + pgl = PGraphicsOpenGL.pgPrimary.pgl; this.is3D = is3D; - initTexture(pg, font); + initTexture(PGraphicsOpenGL.pgPrimary, font); } diff --git a/core/src/processing/opengl/FrameBuffer.java b/core/src/processing/opengl/FrameBuffer.java index 8821861c9..f7b8f56bf 100644 --- a/core/src/processing/opengl/FrameBuffer.java +++ b/core/src/processing/opengl/FrameBuffer.java @@ -68,14 +68,14 @@ public class FrameBuffer implements PConstants { FrameBuffer() { - pgl = PGraphicsOpenGL.pgl; + pgl = PGraphicsOpenGL.pgPrimary.pgl; context = pgl.createEmptyContext(); } FrameBuffer(int w, int h, int samples, int colorBuffers, - int depthBits, int stencilBits, boolean packedDepthStencil, - boolean screen) { + int depthBits, int stencilBits, boolean packedDepthStencil, + boolean screen) { this(); glFbo = 0; @@ -141,7 +141,7 @@ public class FrameBuffer implements PConstants { } - FrameBuffer(int w, int h, boolean screen) { + FrameBuffer(PGraphicsOpenGL pg, int w, int h, boolean screen) { this(w, h, 1, 1, 0, 0, false, screen); } @@ -345,7 +345,7 @@ public class FrameBuffer implements PConstants { glFbo = 0; } else { //create the FBO object... - glFbo = PGraphicsOpenGL.createFrameBufferObject(context); + glFbo = PGraphicsOpenGL.createFrameBufferObject(context, pgl); // ... and then create the rest of the stuff. if (multisample) { @@ -423,7 +423,7 @@ public class FrameBuffer implements PConstants { PGraphicsOpenGL.pushFramebuffer(); PGraphicsOpenGL.setFramebuffer(this); - glMultisample = PGraphicsOpenGL.createRenderBufferObject(context); + glMultisample = PGraphicsOpenGL.createRenderBufferObject(context, pgl); pgl.bindRenderbuffer(PGL.RENDERBUFFER, glMultisample); pgl.renderbufferStorageMultisample(PGL.RENDERBUFFER, nsamples, PGL.RGBA8, width, height); @@ -444,7 +444,7 @@ public class FrameBuffer implements PConstants { PGraphicsOpenGL.pushFramebuffer(); PGraphicsOpenGL.setFramebuffer(this); - glDepthStencil = PGraphicsOpenGL.createRenderBufferObject(context); + glDepthStencil = PGraphicsOpenGL.createRenderBufferObject(context, pgl); pgl.bindRenderbuffer(PGL.RENDERBUFFER, glDepthStencil); if (multisample) { @@ -474,7 +474,7 @@ public class FrameBuffer implements PConstants { PGraphicsOpenGL.pushFramebuffer(); PGraphicsOpenGL.setFramebuffer(this); - glDepth = PGraphicsOpenGL.createRenderBufferObject(context); + glDepth = PGraphicsOpenGL.createRenderBufferObject(context, pgl); pgl.bindRenderbuffer(PGL.RENDERBUFFER, glDepth); int glConst = PGL.DEPTH_COMPONENT16; @@ -510,7 +510,7 @@ public class FrameBuffer implements PConstants { PGraphicsOpenGL.pushFramebuffer(); PGraphicsOpenGL.setFramebuffer(this); - glStencil = PGraphicsOpenGL.createRenderBufferObject(context); + glStencil = PGraphicsOpenGL.createRenderBufferObject(context, pgl); pgl.bindRenderbuffer(PGL.RENDERBUFFER, glStencil); int glConst = PGL.STENCIL_INDEX1; diff --git a/core/src/processing/opengl/PGraphicsOpenGL.java b/core/src/processing/opengl/PGraphicsOpenGL.java index 480b067f0..9596caf9a 100644 --- a/core/src/processing/opengl/PGraphicsOpenGL.java +++ b/core/src/processing/opengl/PGraphicsOpenGL.java @@ -34,7 +34,7 @@ import java.util.*; */ public class PGraphicsOpenGL extends PGraphics { /** Interface between Processing and OpenGL */ - public static PGL pgl; + public PGL pgl; /** The main PApplet renderer. */ protected static PGraphicsOpenGL pgPrimary = null; @@ -520,9 +520,7 @@ public class PGraphicsOpenGL extends PGraphics { public PGraphicsOpenGL() { - if (pgl == null) { - pgl = createPGL(this); - } + pgl = createPGL(this); if (tessellator == null) { tessellator = new Tessellator(); @@ -647,7 +645,7 @@ public class PGraphicsOpenGL extends PGraphics { } } - deleteFinalizedGLResources(); + deleteFinalizedGLResources(pgl); if (primarySurface) { pgl.deleteSurface(); @@ -656,7 +654,7 @@ public class PGraphicsOpenGL extends PGraphics { // This is important in the context of, say, a unit test suite, which // runs more than one OpenGL sketch within the same classloader // (as in the case of processing.py). Please don't remove it! - pgl = null; + //pgl = null; } } @@ -738,8 +736,8 @@ public class PGraphicsOpenGL extends PGraphics { // Texture Objects ----------------------------------------------------------- - protected static int createTextureObject(int context) { - deleteFinalizedTextureObjects(); + protected static int createTextureObject(int context, PGL pgl) { + deleteFinalizedTextureObjects(pgl); pgl.genTextures(1, intBuffer); int id = intBuffer.get(0); @@ -752,7 +750,7 @@ public class PGraphicsOpenGL extends PGraphics { return id; } - protected static void deleteTextureObject(int id, int context) { + protected static void deleteTextureObject(int id, int context, PGL pgl) { GLResource res = new GLResource(id, context); if (glTextureObjects.containsKey(res)) { intBuffer.put(0, id); @@ -761,7 +759,7 @@ public class PGraphicsOpenGL extends PGraphics { } } - protected static void deleteAllTextureObjects() { + protected static void deleteAllTextureObjects(PGL pgl) { for (GLResource res : glTextureObjects.keySet()) { intBuffer.put(0, res.id); if (pgl.threadIsCurrent()) pgl.deleteTextures(1, intBuffer); @@ -777,7 +775,7 @@ public class PGraphicsOpenGL extends PGraphics { } } - protected static void deleteFinalizedTextureObjects() { + protected static void deleteFinalizedTextureObjects(PGL pgl) { Set finalized = new HashSet(); for (GLResource res : glTextureObjects.keySet()) { @@ -802,8 +800,8 @@ public class PGraphicsOpenGL extends PGraphics { // Vertex Buffer Objects ----------------------------------------------------- - protected static int createVertexBufferObject(int context) { - deleteFinalizedVertexBufferObjects(); + protected static int createVertexBufferObject(int context, PGL pgl) { + deleteFinalizedVertexBufferObjects(pgl); pgl.genBuffers(1, intBuffer); int id = intBuffer.get(0); @@ -816,7 +814,7 @@ public class PGraphicsOpenGL extends PGraphics { return id; } - protected static void deleteVertexBufferObject(int id, int context) { + protected static void deleteVertexBufferObject(int id, int context, PGL pgl) { GLResource res = new GLResource(id, context); if (glVertexBuffers.containsKey(res)) { intBuffer.put(0, id); @@ -825,7 +823,7 @@ public class PGraphicsOpenGL extends PGraphics { } } - protected static void deleteAllVertexBufferObjects() { + protected static void deleteAllVertexBufferObjects(PGL pgl) { for (GLResource res : glVertexBuffers.keySet()) { intBuffer.put(0, res.id); if (pgl.threadIsCurrent()) pgl.deleteBuffers(1, intBuffer); @@ -841,7 +839,7 @@ public class PGraphicsOpenGL extends PGraphics { } } - protected static void deleteFinalizedVertexBufferObjects() { + protected static void deleteFinalizedVertexBufferObjects(PGL pgl) { Set finalized = new HashSet(); for (GLResource res : glVertexBuffers.keySet()) { @@ -866,8 +864,8 @@ public class PGraphicsOpenGL extends PGraphics { // FrameBuffer Objects ------------------------------------------------------- - protected static int createFrameBufferObject(int context) { - deleteFinalizedFrameBufferObjects(); + protected static int createFrameBufferObject(int context, PGL pgl) { + deleteFinalizedFrameBufferObjects(pgl); pgl.genFramebuffers(1, intBuffer); int id = intBuffer.get(0); @@ -880,7 +878,7 @@ public class PGraphicsOpenGL extends PGraphics { return id; } - protected static void deleteFrameBufferObject(int id, int context) { + protected static void deleteFrameBufferObject(int id, int context, PGL pgl) { GLResource res = new GLResource(id, context); if (glFrameBuffers.containsKey(res)) { intBuffer.put(0, id); @@ -889,7 +887,7 @@ public class PGraphicsOpenGL extends PGraphics { } } - protected static void deleteAllFrameBufferObjects() { + protected static void deleteAllFrameBufferObjects(PGL pgl) { for (GLResource res : glFrameBuffers.keySet()) { intBuffer.put(0, res.id); if (pgl.threadIsCurrent()) pgl.deleteFramebuffers(1, intBuffer); @@ -905,7 +903,7 @@ public class PGraphicsOpenGL extends PGraphics { } } - protected static void deleteFinalizedFrameBufferObjects() { + protected static void deleteFinalizedFrameBufferObjects(PGL pgl) { Set finalized = new HashSet(); for (GLResource res : glFrameBuffers.keySet()) { @@ -932,8 +930,8 @@ public class PGraphicsOpenGL extends PGraphics { // RenderBuffer Objects ------------------------------------------------------ - protected static int createRenderBufferObject(int context) { - deleteFinalizedRenderBufferObjects(); + protected static int createRenderBufferObject(int context, PGL pgl) { + deleteFinalizedRenderBufferObjects(pgl); pgl.genRenderbuffers(1, intBuffer); int id = intBuffer.get(0); @@ -946,7 +944,7 @@ public class PGraphicsOpenGL extends PGraphics { return id; } - protected static void deleteRenderBufferObject(int id, int context) { + protected static void deleteRenderBufferObject(int id, int context, PGL pgl) { GLResource res = new GLResource(id, context); if (glRenderBuffers.containsKey(res)) { intBuffer.put(0, id); @@ -955,7 +953,7 @@ public class PGraphicsOpenGL extends PGraphics { } } - protected static void deleteAllRenderBufferObjects() { + protected static void deleteAllRenderBufferObjects(PGL pgl) { for (GLResource res : glRenderBuffers.keySet()) { intBuffer.put(0, res.id); if (pgl.threadIsCurrent()) pgl.deleteRenderbuffers(1, intBuffer); @@ -971,7 +969,7 @@ public class PGraphicsOpenGL extends PGraphics { } } - protected static void deleteFinalizedRenderBufferObjects() { + protected static void deleteFinalizedRenderBufferObjects(PGL pgl) { Set finalized = new HashSet(); for (GLResource res : glRenderBuffers.keySet()) { @@ -996,8 +994,8 @@ public class PGraphicsOpenGL extends PGraphics { // GLSL Program Objects ------------------------------------------------------ - protected static int createGLSLProgramObject(int context) { - deleteFinalizedGLSLProgramObjects(); + protected static int createGLSLProgramObject(int context, PGL pgl) { + deleteFinalizedGLSLProgramObjects(pgl); int id = pgl.createProgram(); @@ -1009,7 +1007,7 @@ public class PGraphicsOpenGL extends PGraphics { return id; } - protected static void deleteGLSLProgramObject(int id, int context) { + protected static void deleteGLSLProgramObject(int id, int context, PGL pgl) { GLResource res = new GLResource(id, context); if (glslPrograms.containsKey(res)) { if (pgl.threadIsCurrent()) pgl.deleteProgram(res.id); @@ -1017,7 +1015,7 @@ public class PGraphicsOpenGL extends PGraphics { } } - protected static void deleteAllGLSLProgramObjects() { + protected static void deleteAllGLSLProgramObjects(PGL pgl) { for (GLResource res : glslPrograms.keySet()) { if (pgl.threadIsCurrent()) pgl.deleteProgram(res.id); } @@ -1032,7 +1030,7 @@ public class PGraphicsOpenGL extends PGraphics { } } - protected static void deleteFinalizedGLSLProgramObjects() { + protected static void deleteFinalizedGLSLProgramObjects(PGL pgl) { Set finalized = new HashSet(); for (GLResource res : glslPrograms.keySet()) { @@ -1056,8 +1054,8 @@ public class PGraphicsOpenGL extends PGraphics { // GLSL Vertex Shader Objects ------------------------------------------------ - protected static int createGLSLVertShaderObject(int context) { - deleteFinalizedGLSLVertShaderObjects(); + protected static int createGLSLVertShaderObject(int context, PGL pgl) { + deleteFinalizedGLSLVertShaderObjects(pgl); int id = pgl.createShader(PGL.VERTEX_SHADER); @@ -1069,7 +1067,7 @@ public class PGraphicsOpenGL extends PGraphics { return id; } - protected static void deleteGLSLVertShaderObject(int id, int context) { + protected static void deleteGLSLVertShaderObject(int id, int context, PGL pgl) { GLResource res = new GLResource(id, context); if (glslVertexShaders.containsKey(res)) { if (pgl.threadIsCurrent()) pgl.deleteShader(res.id); @@ -1077,7 +1075,7 @@ public class PGraphicsOpenGL extends PGraphics { } } - protected static void deleteAllGLSLVertShaderObjects() { + protected static void deleteAllGLSLVertShaderObjects(PGL pgl) { for (GLResource res : glslVertexShaders.keySet()) { if (pgl.threadIsCurrent()) pgl.deleteShader(res.id); } @@ -1093,7 +1091,7 @@ public class PGraphicsOpenGL extends PGraphics { } } - protected static void deleteFinalizedGLSLVertShaderObjects() { + protected static void deleteFinalizedGLSLVertShaderObjects(PGL pgl) { Set finalized = new HashSet(); for (GLResource res : glslVertexShaders.keySet()) { @@ -1117,8 +1115,8 @@ public class PGraphicsOpenGL extends PGraphics { // GLSL Fragment Shader Objects ---------------------------------------------- - protected static int createGLSLFragShaderObject(int context) { - deleteFinalizedGLSLFragShaderObjects(); + protected static int createGLSLFragShaderObject(int context, PGL pgl) { + deleteFinalizedGLSLFragShaderObjects(pgl); int id = pgl.createShader(PGL.FRAGMENT_SHADER); @@ -1130,7 +1128,7 @@ public class PGraphicsOpenGL extends PGraphics { return id; } - protected static void deleteGLSLFragShaderObject(int id, int context) { + protected static void deleteGLSLFragShaderObject(int id, int context, PGL pgl) { GLResource res = new GLResource(id, context); if (glslFragmentShaders.containsKey(res)) { if (pgl.threadIsCurrent()) pgl.deleteShader(res.id); @@ -1138,7 +1136,7 @@ public class PGraphicsOpenGL extends PGraphics { } } - protected static void deleteAllGLSLFragShaderObjects() { + protected static void deleteAllGLSLFragShaderObjects(PGL pgl) { for (GLResource res : glslFragmentShaders.keySet()) { if (pgl.threadIsCurrent()) pgl.deleteShader(res.id); } @@ -1154,7 +1152,7 @@ public class PGraphicsOpenGL extends PGraphics { } } - protected static void deleteFinalizedGLSLFragShaderObjects() { + protected static void deleteFinalizedGLSLFragShaderObjects(PGL pgl) { Set finalized = new HashSet(); for (GLResource res : glslFragmentShaders.keySet()) { @@ -1178,14 +1176,14 @@ public class PGraphicsOpenGL extends PGraphics { // All OpenGL resources ------------------------------------------------------ - protected static void deleteFinalizedGLResources() { - deleteFinalizedTextureObjects(); - deleteFinalizedVertexBufferObjects(); - deleteFinalizedFrameBufferObjects(); - deleteFinalizedRenderBufferObjects(); - deleteFinalizedGLSLProgramObjects(); - deleteFinalizedGLSLVertShaderObjects(); - deleteFinalizedGLSLFragShaderObjects(); + protected static void deleteFinalizedGLResources(PGL pgl) { + deleteFinalizedTextureObjects(pgl); + deleteFinalizedVertexBufferObjects(pgl); + deleteFinalizedFrameBufferObjects(pgl); + deleteFinalizedRenderBufferObjects(pgl); + deleteFinalizedGLSLProgramObjects(pgl); + deleteFinalizedGLSLVertShaderObjects(pgl); + deleteFinalizedGLSLFragShaderObjects(pgl); } @@ -1238,41 +1236,41 @@ public class PGraphicsOpenGL extends PGraphics { int sizei = INIT_VERTEX_BUFFER_SIZE * PGL.SIZEOF_INT; int sizex = INIT_INDEX_BUFFER_SIZE * PGL.SIZEOF_INDEX; - glPolyVertex = createVertexBufferObject(polyBuffersContext); + glPolyVertex = createVertexBufferObject(polyBuffersContext, pgl); pgl.bindBuffer(PGL.ARRAY_BUFFER, glPolyVertex); pgl.bufferData(PGL.ARRAY_BUFFER, 3 * sizef, null, PGL.STATIC_DRAW); - glPolyColor = createVertexBufferObject(polyBuffersContext); + glPolyColor = createVertexBufferObject(polyBuffersContext, pgl); pgl.bindBuffer(PGL.ARRAY_BUFFER, glPolyColor); pgl.bufferData(PGL.ARRAY_BUFFER, sizei, null, PGL.STATIC_DRAW); - glPolyNormal = createVertexBufferObject(polyBuffersContext); + glPolyNormal = createVertexBufferObject(polyBuffersContext, pgl); pgl.bindBuffer(PGL.ARRAY_BUFFER, glPolyNormal); pgl.bufferData(PGL.ARRAY_BUFFER, 3 * sizef, null, PGL.STATIC_DRAW); - glPolyTexcoord = createVertexBufferObject(polyBuffersContext); + glPolyTexcoord = createVertexBufferObject(polyBuffersContext, pgl); pgl.bindBuffer(PGL.ARRAY_BUFFER, glPolyTexcoord); pgl.bufferData(PGL.ARRAY_BUFFER, 2 * sizef, null, PGL.STATIC_DRAW); - glPolyAmbient = createVertexBufferObject(polyBuffersContext); + glPolyAmbient = createVertexBufferObject(polyBuffersContext, pgl); pgl.bindBuffer(PGL.ARRAY_BUFFER, glPolyAmbient); pgl.bufferData(PGL.ARRAY_BUFFER, sizei, null, PGL.STATIC_DRAW); - glPolySpecular = createVertexBufferObject(polyBuffersContext); + glPolySpecular = createVertexBufferObject(polyBuffersContext, pgl); pgl.bindBuffer(PGL.ARRAY_BUFFER, glPolySpecular); pgl.bufferData(PGL.ARRAY_BUFFER, sizei, null, PGL.STATIC_DRAW); - glPolyEmissive = createVertexBufferObject(polyBuffersContext); + glPolyEmissive = createVertexBufferObject(polyBuffersContext, pgl); pgl.bindBuffer(PGL.ARRAY_BUFFER, glPolyEmissive); pgl.bufferData(PGL.ARRAY_BUFFER, sizei, null, PGL.STATIC_DRAW); - glPolyShininess = createVertexBufferObject(polyBuffersContext); + glPolyShininess = createVertexBufferObject(polyBuffersContext, pgl); pgl.bindBuffer(PGL.ARRAY_BUFFER, glPolyShininess); pgl.bufferData(PGL.ARRAY_BUFFER, sizef, null, PGL.STATIC_DRAW); pgl.bindBuffer(PGL.ARRAY_BUFFER, 0); - glPolyIndex = createVertexBufferObject(polyBuffersContext); + glPolyIndex = createVertexBufferObject(polyBuffersContext, pgl); pgl.bindBuffer(PGL.ELEMENT_ARRAY_BUFFER, glPolyIndex); pgl.bufferData(PGL.ELEMENT_ARRAY_BUFFER, sizex, null, PGL.STATIC_DRAW); @@ -1357,31 +1355,31 @@ public class PGraphicsOpenGL extends PGraphics { protected void deletePolyBuffers() { if (polyBuffersCreated) { - deleteVertexBufferObject(glPolyVertex, polyBuffersContext); + deleteVertexBufferObject(glPolyVertex, polyBuffersContext, pgl); glPolyVertex = 0; - deleteVertexBufferObject(glPolyColor, polyBuffersContext); + deleteVertexBufferObject(glPolyColor, polyBuffersContext, pgl); glPolyColor = 0; - deleteVertexBufferObject(glPolyNormal, polyBuffersContext); + deleteVertexBufferObject(glPolyNormal, polyBuffersContext, pgl); glPolyNormal = 0; - deleteVertexBufferObject(glPolyTexcoord, polyBuffersContext); + deleteVertexBufferObject(glPolyTexcoord, polyBuffersContext, pgl); glPolyTexcoord = 0; - deleteVertexBufferObject(glPolyAmbient, polyBuffersContext); + deleteVertexBufferObject(glPolyAmbient, polyBuffersContext, pgl); glPolyAmbient = 0; - deleteVertexBufferObject(glPolySpecular, polyBuffersContext); + deleteVertexBufferObject(glPolySpecular, polyBuffersContext, pgl); glPolySpecular = 0; - deleteVertexBufferObject(glPolyEmissive, polyBuffersContext); + deleteVertexBufferObject(glPolyEmissive, polyBuffersContext, pgl); glPolyEmissive = 0; - deleteVertexBufferObject(glPolyShininess, polyBuffersContext); + deleteVertexBufferObject(glPolyShininess, polyBuffersContext, pgl); glPolyShininess = 0; - deleteVertexBufferObject(glPolyIndex, polyBuffersContext); + deleteVertexBufferObject(glPolyIndex, polyBuffersContext, pgl); glPolyIndex = 0; polyBuffersCreated = false; @@ -1397,22 +1395,22 @@ public class PGraphicsOpenGL extends PGraphics { int sizei = INIT_VERTEX_BUFFER_SIZE * PGL.SIZEOF_INT; int sizex = INIT_INDEX_BUFFER_SIZE * PGL.SIZEOF_INDEX; - glLineVertex = createVertexBufferObject(lineBuffersContext); + glLineVertex = createVertexBufferObject(lineBuffersContext, pgl); pgl.bindBuffer(PGL.ARRAY_BUFFER, glLineVertex); pgl.bufferData(PGL.ARRAY_BUFFER, 3 * sizef, null, PGL.STATIC_DRAW); - glLineColor = createVertexBufferObject(lineBuffersContext); + glLineColor = createVertexBufferObject(lineBuffersContext, pgl); pgl.bindBuffer(PGL.ARRAY_BUFFER, glLineColor); pgl.bufferData(PGL.ARRAY_BUFFER, sizei, null, PGL.STATIC_DRAW); - glLineAttrib = createVertexBufferObject(lineBuffersContext); + glLineAttrib = createVertexBufferObject(lineBuffersContext, pgl); pgl.bindBuffer(PGL.ARRAY_BUFFER, glLineAttrib); pgl.bufferData(PGL.ARRAY_BUFFER, 4 * sizef, null, PGL.STATIC_DRAW); pgl.bindBuffer(PGL.ARRAY_BUFFER, 0); - glLineIndex = createVertexBufferObject(lineBuffersContext); + glLineIndex = createVertexBufferObject(lineBuffersContext, pgl); pgl.bindBuffer(PGL.ELEMENT_ARRAY_BUFFER, glLineIndex); pgl.bufferData(PGL.ELEMENT_ARRAY_BUFFER, sizex, null, PGL.STATIC_DRAW); @@ -1466,16 +1464,16 @@ public class PGraphicsOpenGL extends PGraphics { protected void deleteLineBuffers() { if (lineBuffersCreated) { - deleteVertexBufferObject(glLineVertex, lineBuffersContext); + deleteVertexBufferObject(glLineVertex, lineBuffersContext, pgl); glLineVertex = 0; - deleteVertexBufferObject(glLineColor, lineBuffersContext); + deleteVertexBufferObject(glLineColor, lineBuffersContext, pgl); glLineColor = 0; - deleteVertexBufferObject(glLineAttrib, lineBuffersContext); + deleteVertexBufferObject(glLineAttrib, lineBuffersContext, pgl); glLineAttrib = 0; - deleteVertexBufferObject(glLineIndex, lineBuffersContext); + deleteVertexBufferObject(glLineIndex, lineBuffersContext, pgl); glLineIndex = 0; lineBuffersCreated = false; @@ -1491,21 +1489,21 @@ public class PGraphicsOpenGL extends PGraphics { int sizei = INIT_VERTEX_BUFFER_SIZE * PGL.SIZEOF_INT; int sizex = INIT_INDEX_BUFFER_SIZE * PGL.SIZEOF_INDEX; - glPointVertex = createVertexBufferObject(pointBuffersContext); + glPointVertex = createVertexBufferObject(pointBuffersContext, pgl); pgl.bindBuffer(PGL.ARRAY_BUFFER, glPointVertex); pgl.bufferData(PGL.ARRAY_BUFFER, 3 * sizef, null, PGL.STATIC_DRAW); - glPointColor = createVertexBufferObject(pointBuffersContext); + glPointColor = createVertexBufferObject(pointBuffersContext, pgl); pgl.bindBuffer(PGL.ARRAY_BUFFER, glPointColor); pgl.bufferData(PGL.ARRAY_BUFFER, sizei, null, PGL.STATIC_DRAW); - glPointAttrib = createVertexBufferObject(pointBuffersContext); + glPointAttrib = createVertexBufferObject(pointBuffersContext, pgl); pgl.bindBuffer(PGL.ARRAY_BUFFER, glPointAttrib); pgl.bufferData(PGL.ARRAY_BUFFER, 2 * sizef, null, PGL.STATIC_DRAW); pgl.bindBuffer(PGL.ARRAY_BUFFER, 0); - glPointIndex = createVertexBufferObject(pointBuffersContext); + glPointIndex = createVertexBufferObject(pointBuffersContext, pgl); pgl.bindBuffer(PGL.ELEMENT_ARRAY_BUFFER, glPointIndex); pgl.bufferData(PGL.ELEMENT_ARRAY_BUFFER, sizex, null, PGL.STATIC_DRAW); @@ -1559,16 +1557,16 @@ public class PGraphicsOpenGL extends PGraphics { protected void deletePointBuffers() { if (pointBuffersCreated) { - deleteVertexBufferObject(glPointVertex, pointBuffersContext); + deleteVertexBufferObject(glPointVertex, pointBuffersContext, pgl); glPointVertex = 0; - deleteVertexBufferObject(glPointColor, pointBuffersContext); + deleteVertexBufferObject(glPointColor, pointBuffersContext, pgl); glPointColor = 0; - deleteVertexBufferObject(glPointAttrib, pointBuffersContext); + deleteVertexBufferObject(glPointAttrib, pointBuffersContext, pgl); glPointAttrib = 0; - deleteVertexBufferObject(glPointIndex, pointBuffersContext); + deleteVertexBufferObject(glPointIndex, pointBuffersContext, pgl); glPointIndex = 0; pointBuffersCreated = false; @@ -3510,7 +3508,7 @@ public class PGraphicsOpenGL extends PGraphics { textTex = pgPrimary.getFontTexture(textFont); if (textTex == null || textTex.contextIsOutdated()) { - textTex = new FontTexture(pgPrimary, textFont, is3D()); + textTex = new FontTexture(textFont, is3D()); pgPrimary.setFontTexture(textFont, textTex); } @@ -6208,11 +6206,11 @@ public class PGraphicsOpenGL extends PGraphics { pgl.beginDraw(clearColorBuffer); if (drawFramebuffer == null) { - drawFramebuffer = new FrameBuffer(width, height, true); + drawFramebuffer = new FrameBuffer(this, width, height, true); } drawFramebuffer.setFBO(pgl.getDrawFramebuffer()); if (readFramebuffer == null) { - readFramebuffer = new FrameBuffer(width, height, true); + readFramebuffer = new FrameBuffer(this, width, height, true); } readFramebuffer.setFBO(pgl.getReadFramebuffer()); if (currentFramebuffer == null) { @@ -9659,8 +9657,6 @@ public class PGraphicsOpenGL extends PGraphics { int beginPath; public Tessellator() { - callback = new TessellatorCallback(); - gluTess = pgl.createTessellator(callback); rawIndices = new int[512]; accurate2DStrokes = true; transform = null; @@ -9668,6 +9664,13 @@ public class PGraphicsOpenGL extends PGraphics { is3D = true; } + void initGluTess() { + if (gluTess == null) { + callback = new TessellatorCallback(); + gluTess = pg.pgl.createTessellator(callback); + } + } + void setInGeometry(InGeometry in) { this.in = in; @@ -9689,7 +9692,6 @@ public class PGraphicsOpenGL extends PGraphics { void setTexCache(TexCache texCache, PImage newTexImage) { this.texCache = texCache; - //this.prevTexImage = prevTexImage; this.newTexImage = newTexImage; } @@ -11198,6 +11200,7 @@ public class PGraphicsOpenGL extends PGraphics { if (3 <= nInVert) { firstPolyIndexCache = -1; + initGluTess(); boolean clamp = clampPolygon(); callback.init(in.renderMode == RETAINED, false, calcNormals, clamp); @@ -11727,8 +11730,8 @@ public class PGraphicsOpenGL extends PGraphics { // Based on the opengl stroke hack described here: // http://wiki.processing.org/w/Stroke_attributes_in_OpenGL public void tessellateLinePath(LinePath path) { + initGluTess(); boolean clamp = clampLinePath(); - callback.init(in.renderMode == RETAINED, true, false, clamp); int cap = strokeCap == ROUND ? LinePath.CAP_ROUND : @@ -11960,7 +11963,7 @@ public class PGraphicsOpenGL extends PGraphics { } public void error(int errnum) { - String estring = pgl.tessError(errnum); + 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 c1030b219..d903a5670 100644 --- a/core/src/processing/opengl/PShader.java +++ b/core/src/processing/opengl/PShader.java @@ -180,7 +180,7 @@ public class PShader implements PConstants { public PShader(PApplet parent) { this(); this.parent = parent; - pgl = PGraphicsOpenGL.pgl; + pgl = PGraphicsOpenGL.pgCurrent.pgl; context = pgl.createEmptyContext(); } @@ -195,7 +195,7 @@ public class PShader implements PConstants { */ public PShader(PApplet parent, String vertFilename, String fragFilename) { this.parent = parent; - pgl = PGraphicsOpenGL.pgl; + pgl = PGraphicsOpenGL.pgCurrent.pgl; this.vertexURL = null; this.fragmentURL = null; @@ -233,7 +233,7 @@ public class PShader implements PConstants { */ public PShader(PApplet parent, URL vertURL, URL fragURL) { this.parent = parent; - pgl = PGraphicsOpenGL.pgl; + pgl = PGraphicsOpenGL.pgCurrent.pgl; this.vertexURL = vertURL; this.fragmentURL = fragURL; @@ -266,7 +266,7 @@ public class PShader implements PConstants { public PShader(PApplet parent, String[] vertSource, String[] fragSource) { this.parent = parent; - pgl = PGraphicsOpenGL.pgl; + pgl = PGraphicsOpenGL.pgCurrent.pgl; this.vertexURL = null; this.fragmentURL = null; @@ -894,7 +894,7 @@ public class PShader implements PConstants { protected void init() { if (glProgram == 0 || contextIsOutdated()) { context = pgl.getCurrentContext(); - glProgram = PGraphicsOpenGL.createGLSLProgramObject(context); + glProgram = PGraphicsOpenGL.createGLSLProgramObject(context, pgl); boolean vertRes = true; if (hasVertexShader()) { @@ -964,7 +964,7 @@ public class PShader implements PConstants { * @param shaderSource a string containing the shader's code */ protected boolean compileVertexShader() { - glVertex = PGraphicsOpenGL.createGLSLVertShaderObject(context); + glVertex = PGraphicsOpenGL.createGLSLVertShaderObject(context, pgl); pgl.shaderSource(glVertex, PApplet.join(vertexShaderSource, "\n")); pgl.compileShader(glVertex); @@ -985,7 +985,7 @@ public class PShader implements PConstants { * @param shaderSource a string containing the shader's code */ protected boolean compileFragmentShader() { - glFragment = PGraphicsOpenGL.createGLSLFragShaderObject(context); + glFragment = PGraphicsOpenGL.createGLSLFragShaderObject(context, pgl); pgl.shaderSource(glFragment, PApplet.join(fragmentShaderSource, "\n")); pgl.compileShader(glFragment); @@ -1004,15 +1004,15 @@ public class PShader implements PConstants { protected void dispose() { if (glVertex != 0) { - PGraphicsOpenGL.deleteGLSLVertShaderObject(glVertex, context); + PGraphicsOpenGL.deleteGLSLVertShaderObject(glVertex, context, pgl); glVertex = 0; } if (glFragment != 0) { - PGraphicsOpenGL.deleteGLSLFragShaderObject(glFragment, context); + PGraphicsOpenGL.deleteGLSLFragShaderObject(glFragment, context, pgl); glFragment = 0; } if (glProgram != 0) { - PGraphicsOpenGL.deleteGLSLProgramObject(glProgram, context); + PGraphicsOpenGL.deleteGLSLProgramObject(glProgram, context, pgl); glProgram = 0; } } diff --git a/core/src/processing/opengl/PShapeOpenGL.java b/core/src/processing/opengl/PShapeOpenGL.java index d11841b9e..eff4ad51e 100644 --- a/core/src/processing/opengl/PShapeOpenGL.java +++ b/core/src/processing/opengl/PShapeOpenGL.java @@ -292,7 +292,7 @@ public class PShapeOpenGL extends PShape { public PShapeOpenGL(PApplet parent, int family) { pg = PGraphicsOpenGL.pgCurrent; - pgl = PGraphicsOpenGL.pgl; + pgl = pg.pgl; context = pgl.createEmptyContext(); glPolyVertex = 0; @@ -3651,56 +3651,56 @@ public class PShapeOpenGL extends PShape { tessGeo.updatePolyVerticesBuffer(); if (glPolyVertex == 0) - glPolyVertex = PGraphicsOpenGL.createVertexBufferObject(context); + glPolyVertex = PGraphicsOpenGL.createVertexBufferObject(context, pgl); pgl.bindBuffer(PGL.ARRAY_BUFFER, glPolyVertex); pgl.bufferData(PGL.ARRAY_BUFFER, 4 * sizef, tessGeo.polyVerticesBuffer, PGL.STATIC_DRAW); tessGeo.updatePolyColorsBuffer(); if (glPolyColor == 0) - glPolyColor = PGraphicsOpenGL.createVertexBufferObject(context); + glPolyColor = PGraphicsOpenGL.createVertexBufferObject(context, pgl); pgl.bindBuffer(PGL.ARRAY_BUFFER, glPolyColor); pgl.bufferData(PGL.ARRAY_BUFFER, sizei, tessGeo.polyColorsBuffer, PGL.STATIC_DRAW); tessGeo.updatePolyNormalsBuffer(); if (glPolyNormal == 0) - glPolyNormal = PGraphicsOpenGL.createVertexBufferObject(context); + glPolyNormal = PGraphicsOpenGL.createVertexBufferObject(context, pgl); pgl.bindBuffer(PGL.ARRAY_BUFFER, glPolyNormal); pgl.bufferData(PGL.ARRAY_BUFFER, 3 * sizef, tessGeo.polyNormalsBuffer, PGL.STATIC_DRAW); tessGeo.updatePolyTexCoordsBuffer(); if (glPolyTexcoord == 0) - glPolyTexcoord = PGraphicsOpenGL.createVertexBufferObject(context); + glPolyTexcoord = PGraphicsOpenGL.createVertexBufferObject(context, pgl); pgl.bindBuffer(PGL.ARRAY_BUFFER, glPolyTexcoord); pgl.bufferData(PGL.ARRAY_BUFFER, 2 * sizef, tessGeo.polyTexCoordsBuffer, PGL.STATIC_DRAW); tessGeo.updatePolyAmbientBuffer(); if (glPolyAmbient == 0) - glPolyAmbient = PGraphicsOpenGL.createVertexBufferObject(context); + glPolyAmbient = PGraphicsOpenGL.createVertexBufferObject(context, pgl); pgl.bindBuffer(PGL.ARRAY_BUFFER, glPolyAmbient); pgl.bufferData(PGL.ARRAY_BUFFER, sizei, tessGeo.polyAmbientBuffer, PGL.STATIC_DRAW); tessGeo.updatePolySpecularBuffer(); if (glPolySpecular == 0) - glPolySpecular = PGraphicsOpenGL.createVertexBufferObject(context); + glPolySpecular = PGraphicsOpenGL.createVertexBufferObject(context, pgl); pgl.bindBuffer(PGL.ARRAY_BUFFER, glPolySpecular); pgl.bufferData(PGL.ARRAY_BUFFER, sizei, tessGeo.polySpecularBuffer, PGL.STATIC_DRAW); tessGeo.updatePolyEmissiveBuffer(); if (glPolyEmissive == 0) - glPolyEmissive = PGraphicsOpenGL.createVertexBufferObject(context); + glPolyEmissive = PGraphicsOpenGL.createVertexBufferObject(context, pgl); pgl.bindBuffer(PGL.ARRAY_BUFFER, glPolyEmissive); pgl.bufferData(PGL.ARRAY_BUFFER, sizei, tessGeo.polyEmissiveBuffer, PGL.STATIC_DRAW); tessGeo.updatePolyShininessBuffer(); if (glPolyShininess == 0) - glPolyShininess = PGraphicsOpenGL.createVertexBufferObject(context); + glPolyShininess = PGraphicsOpenGL.createVertexBufferObject(context, pgl); pgl.bindBuffer(PGL.ARRAY_BUFFER, glPolyShininess); pgl.bufferData(PGL.ARRAY_BUFFER, sizef, tessGeo.polyShininessBuffer, PGL.STATIC_DRAW); @@ -3709,7 +3709,7 @@ public class PShapeOpenGL extends PShape { tessGeo.updatePolyIndicesBuffer(); if (glPolyIndex == 0) - glPolyIndex = PGraphicsOpenGL.createVertexBufferObject(context); + glPolyIndex = PGraphicsOpenGL.createVertexBufferObject(context, pgl); pgl.bindBuffer(PGL.ELEMENT_ARRAY_BUFFER, glPolyIndex); pgl.bufferData(PGL.ELEMENT_ARRAY_BUFFER, tessGeo.polyIndexCount * PGL.SIZEOF_INDEX, @@ -3726,21 +3726,21 @@ public class PShapeOpenGL extends PShape { tessGeo.updateLineVerticesBuffer(); if (glLineVertex == 0) - glLineVertex = PGraphicsOpenGL.createVertexBufferObject(context); + glLineVertex = PGraphicsOpenGL.createVertexBufferObject(context, pgl); pgl.bindBuffer(PGL.ARRAY_BUFFER, glLineVertex); pgl.bufferData(PGL.ARRAY_BUFFER, 4 * sizef, tessGeo.lineVerticesBuffer, PGL.STATIC_DRAW); tessGeo.updateLineColorsBuffer(); if (glLineColor == 0) - glLineColor = PGraphicsOpenGL.createVertexBufferObject(context); + glLineColor = PGraphicsOpenGL.createVertexBufferObject(context, pgl); pgl.bindBuffer(PGL.ARRAY_BUFFER, glLineColor); pgl.bufferData(PGL.ARRAY_BUFFER, sizei, tessGeo.lineColorsBuffer, PGL.STATIC_DRAW); tessGeo.updateLineDirectionsBuffer(); if (glLineAttrib == 0) - glLineAttrib = PGraphicsOpenGL.createVertexBufferObject(context); + glLineAttrib = PGraphicsOpenGL.createVertexBufferObject(context, pgl); pgl.bindBuffer(PGL.ARRAY_BUFFER, glLineAttrib); pgl.bufferData(PGL.ARRAY_BUFFER, 4 * sizef, tessGeo.lineDirectionsBuffer, PGL.STATIC_DRAW); @@ -3749,7 +3749,7 @@ public class PShapeOpenGL extends PShape { tessGeo.updateLineIndicesBuffer(); if (glLineIndex == 0) - glLineIndex = PGraphicsOpenGL.createVertexBufferObject(context); + glLineIndex = PGraphicsOpenGL.createVertexBufferObject(context, pgl); pgl.bindBuffer(PGL.ELEMENT_ARRAY_BUFFER, glLineIndex); pgl.bufferData(PGL.ELEMENT_ARRAY_BUFFER, tessGeo.lineIndexCount * PGL.SIZEOF_INDEX, @@ -3766,21 +3766,21 @@ public class PShapeOpenGL extends PShape { tessGeo.updatePointVerticesBuffer(); if (glPointVertex == 0) - glPointVertex = PGraphicsOpenGL.createVertexBufferObject(context); + glPointVertex = PGraphicsOpenGL.createVertexBufferObject(context, pgl); pgl.bindBuffer(PGL.ARRAY_BUFFER, glPointVertex); pgl.bufferData(PGL.ARRAY_BUFFER, 4 * sizef, tessGeo.pointVerticesBuffer, PGL.STATIC_DRAW); tessGeo.updatePointColorsBuffer(); if (glPointColor == 0) - glPointColor = PGraphicsOpenGL.createVertexBufferObject(context); + glPointColor = PGraphicsOpenGL.createVertexBufferObject(context, pgl); pgl.bindBuffer(PGL.ARRAY_BUFFER, glPointColor); pgl.bufferData(PGL.ARRAY_BUFFER, sizei, tessGeo.pointColorsBuffer, PGL.STATIC_DRAW); tessGeo.updatePointOffsetsBuffer(); if (glPointAttrib == 0) - glPointAttrib = PGraphicsOpenGL.createVertexBufferObject(context); + glPointAttrib = PGraphicsOpenGL.createVertexBufferObject(context, pgl); pgl.bindBuffer(PGL.ARRAY_BUFFER, glPointAttrib); pgl.bufferData(PGL.ARRAY_BUFFER, 2 * sizef, tessGeo.pointOffsetsBuffer, PGL.STATIC_DRAW); @@ -3789,7 +3789,7 @@ public class PShapeOpenGL extends PShape { tessGeo.updatePointIndicesBuffer(); if (glPointIndex == 0) - glPointIndex = PGraphicsOpenGL.createVertexBufferObject(context); + glPointIndex = PGraphicsOpenGL.createVertexBufferObject(context, pgl); pgl.bindBuffer(PGL.ELEMENT_ARRAY_BUFFER, glPointIndex); pgl.bufferData(PGL.ELEMENT_ARRAY_BUFFER, tessGeo.pointIndexCount * PGL.SIZEOF_INDEX, @@ -3870,47 +3870,47 @@ public class PShapeOpenGL extends PShape { protected void deletePolyBuffers() { if (glPolyVertex != 0) { - PGraphicsOpenGL.deleteVertexBufferObject(glPolyVertex, context); + PGraphicsOpenGL.deleteVertexBufferObject(glPolyVertex, context, pgl); glPolyVertex = 0; } if (glPolyColor != 0) { - PGraphicsOpenGL.deleteVertexBufferObject(glPolyColor, context); + PGraphicsOpenGL.deleteVertexBufferObject(glPolyColor, context, pgl); glPolyColor = 0; } if (glPolyNormal != 0) { - PGraphicsOpenGL.deleteVertexBufferObject(glPolyNormal, context); + PGraphicsOpenGL.deleteVertexBufferObject(glPolyNormal, context, pgl); glPolyNormal = 0; } if (glPolyTexcoord != 0) { - PGraphicsOpenGL.deleteVertexBufferObject(glPolyTexcoord, context); + PGraphicsOpenGL.deleteVertexBufferObject(glPolyTexcoord, context, pgl); glPolyTexcoord = 0; } if (glPolyAmbient != 0) { - PGraphicsOpenGL.deleteVertexBufferObject(glPolyAmbient, context); + PGraphicsOpenGL.deleteVertexBufferObject(glPolyAmbient, context, pgl); glPolyAmbient = 0; } if (glPolySpecular != 0) { - PGraphicsOpenGL.deleteVertexBufferObject(glPolySpecular, context); + PGraphicsOpenGL.deleteVertexBufferObject(glPolySpecular, context, pgl); glPolySpecular = 0; } if (glPolyEmissive != 0) { - PGraphicsOpenGL.deleteVertexBufferObject(glPolyEmissive, context); + PGraphicsOpenGL.deleteVertexBufferObject(glPolyEmissive, context, pgl); glPolyEmissive = 0; } if (glPolyShininess != 0) { - PGraphicsOpenGL.deleteVertexBufferObject(glPolyShininess, context); + PGraphicsOpenGL.deleteVertexBufferObject(glPolyShininess, context, pgl); glPolyShininess = 0; } if (glPolyIndex != 0) { - PGraphicsOpenGL.deleteVertexBufferObject(glPolyIndex, context); + PGraphicsOpenGL.deleteVertexBufferObject(glPolyIndex, context, pgl); glPolyIndex = 0; } } @@ -3918,22 +3918,22 @@ public class PShapeOpenGL extends PShape { protected void deleteLineBuffers() { if (glLineVertex != 0) { - PGraphicsOpenGL.deleteVertexBufferObject(glLineVertex, context); + PGraphicsOpenGL.deleteVertexBufferObject(glLineVertex, context, pgl); glLineVertex = 0; } if (glLineColor != 0) { - PGraphicsOpenGL.deleteVertexBufferObject(glLineColor, context); + PGraphicsOpenGL.deleteVertexBufferObject(glLineColor, context, pgl); glLineColor = 0; } if (glLineAttrib != 0) { - PGraphicsOpenGL.deleteVertexBufferObject(glLineAttrib, context); + PGraphicsOpenGL.deleteVertexBufferObject(glLineAttrib, context, pgl); glLineAttrib = 0; } if (glLineIndex != 0) { - PGraphicsOpenGL.deleteVertexBufferObject(glLineIndex, context); + PGraphicsOpenGL.deleteVertexBufferObject(glLineIndex, context, pgl); glLineIndex = 0; } } @@ -3941,22 +3941,22 @@ public class PShapeOpenGL extends PShape { protected void deletePointBuffers() { if (glPointVertex != 0) { - PGraphicsOpenGL.deleteVertexBufferObject(glPointVertex, context); + PGraphicsOpenGL.deleteVertexBufferObject(glPointVertex, context, pgl); glPointVertex = 0; } if (glPointColor != 0) { - PGraphicsOpenGL.deleteVertexBufferObject(glPointColor, context); + PGraphicsOpenGL.deleteVertexBufferObject(glPointColor, context, pgl); glPointColor = 0; } if (glPointAttrib != 0) { - PGraphicsOpenGL.deleteVertexBufferObject(glPointAttrib, context); + PGraphicsOpenGL.deleteVertexBufferObject(glPointAttrib, context, pgl); glPointAttrib = 0; } if (glPointIndex != 0) { - PGraphicsOpenGL.deleteVertexBufferObject(glPointIndex, context); + PGraphicsOpenGL.deleteVertexBufferObject(glPointIndex, context, pgl); glPointIndex = 0; } } diff --git a/core/src/processing/opengl/Texture.java b/core/src/processing/opengl/Texture.java index 555ae5986..c75b6bcc2 100644 --- a/core/src/processing/opengl/Texture.java +++ b/core/src/processing/opengl/Texture.java @@ -119,7 +119,7 @@ public class Texture implements PConstants { public Texture() { - pgl = PGraphicsOpenGL.pgl; + pgl = PGraphicsOpenGL.pgPrimary.pgl; context = pgl.createEmptyContext(); colorBuffer = false; @@ -147,7 +147,7 @@ public class Texture implements PConstants { * @param params Parameters */ public Texture(int width, int height, Object params) { - pgl = PGraphicsOpenGL.pgl; + pgl = PGraphicsOpenGL.pgPrimary.pgl; context = pgl.createEmptyContext(); colorBuffer = false; @@ -1157,7 +1157,7 @@ public class Texture implements PConstants { } context = pgl.getCurrentContext(); - glName = PGraphicsOpenGL.createTextureObject(context); + glName = PGraphicsOpenGL.createTextureObject(context, pgl); pgl.bindTexture(glTarget, glName); pgl.texParameteri(glTarget, PGL.TEXTURE_MIN_FILTER, glMinFilter); From f2c730b58ff31bd6aae61eb054b22ca95b654123 Mon Sep 17 00:00:00 2001 From: codeanticode Date: Mon, 20 Jan 2014 12:14:35 -0500 Subject: [PATCH 14/51] continues the removal of static references --- core/src/processing/opengl/FontTexture.java | 15 +- core/src/processing/opengl/FrameBuffer.java | 18 +- core/src/processing/opengl/PGL.java | 14 +- core/src/processing/opengl/PGraphics2D.java | 32 ++-- core/src/processing/opengl/PGraphics3D.java | 36 ++-- .../processing/opengl/PGraphicsOpenGL.java | 162 +++++++++++------- core/src/processing/opengl/PJOGL.java | 47 +++-- core/src/processing/opengl/PShader.java | 75 ++++---- core/src/processing/opengl/PShapeOpenGL.java | 42 +++-- core/src/processing/opengl/Texture.java | 23 +-- 10 files changed, 262 insertions(+), 202 deletions(-) diff --git a/core/src/processing/opengl/FontTexture.java b/core/src/processing/opengl/FontTexture.java index e32d4f440..8c1c66f5d 100644 --- a/core/src/processing/opengl/FontTexture.java +++ b/core/src/processing/opengl/FontTexture.java @@ -63,11 +63,12 @@ class FontTexture implements PConstants { protected TextureInfo[] glyphTexinfos; protected HashMap texinfoMap; - public FontTexture(PFont font, boolean is3D) { - pgl = PGraphicsOpenGL.pgPrimary.pgl; + + public FontTexture(PGraphicsOpenGL pg, PFont font, boolean is3D) { + pgl = pg.pgl; this.is3D = is3D; - initTexture(PGraphicsOpenGL.pgPrimary, font); + initTexture(pg, font); } @@ -130,15 +131,15 @@ class FontTexture implements PConstants { if (is3D) { // Bilinear sampling ensures that the texture doesn't look pixelated // either when it is magnified or minified... - tex = new Texture(w, h, new Texture.Parameters(ARGB, Texture.BILINEAR, - false)); + tex = new Texture(pg, w, h, + new Texture.Parameters(ARGB, Texture.BILINEAR, false)); } else { // ...however, the effect of bilinear sampling is to add some blurriness // to the text in its original size. In 2D, we assume that text will be // shown at its original size, so linear sampling is chosen instead (which // only affects minimized text). - tex = new Texture(w, h, new Texture.Parameters(ARGB, Texture.LINEAR, - false)); + tex = new Texture(pg, w, h, + new Texture.Parameters(ARGB, Texture.LINEAR, false)); } if (textures == null) { diff --git a/core/src/processing/opengl/FrameBuffer.java b/core/src/processing/opengl/FrameBuffer.java index f7b8f56bf..1e5ef72a6 100644 --- a/core/src/processing/opengl/FrameBuffer.java +++ b/core/src/processing/opengl/FrameBuffer.java @@ -40,6 +40,7 @@ import java.nio.IntBuffer; */ public class FrameBuffer implements PConstants { + protected PGraphicsOpenGL pg; protected PGL pgl; protected int context; // The context that created this framebuffer. @@ -67,16 +68,17 @@ public class FrameBuffer implements PConstants { protected IntBuffer pixelBuffer; - FrameBuffer() { - pgl = PGraphicsOpenGL.pgPrimary.pgl; + FrameBuffer(PGraphicsOpenGL pg) { + this.pg = pg; + pgl = pg.pgl; context = pgl.createEmptyContext(); } - FrameBuffer(int w, int h, int samples, int colorBuffers, + FrameBuffer(PGraphicsOpenGL pg, int w, int h, int samples, int colorBuffers, int depthBits, int stencilBits, boolean packedDepthStencil, boolean screen) { - this(); + this(pg); glFbo = 0; glDepth = 0; @@ -136,13 +138,13 @@ public class FrameBuffer implements PConstants { } - FrameBuffer(int w, int h) { - this(w, h, 1, 1, 0, 0, false, false); + FrameBuffer(PGraphicsOpenGL pg, int w, int h) { + this(pg, w, h, 1, 1, 0, 0, false, false); } FrameBuffer(PGraphicsOpenGL pg, int w, int h, boolean screen) { - this(w, h, 1, 1, 0, 0, false, screen); + this(pg, w, h, 1, 1, 0, 0, false, screen); } @@ -201,7 +203,7 @@ public class FrameBuffer implements PConstants { noDepth = true; } - public void finish(PGraphicsOpenGL pg) { + public void finish() { if (noDepth) { // No need to clear depth buffer because depth testing was disabled. if (pg.getHint(ENABLE_DEPTH_TEST)) { diff --git a/core/src/processing/opengl/PGL.java b/core/src/processing/opengl/PGL.java index 0a302c1e4..bd4066021 100644 --- a/core/src/processing/opengl/PGL.java +++ b/core/src/processing/opengl/PGL.java @@ -52,10 +52,10 @@ public abstract class PGL { protected PGraphicsOpenGL pg; /** OpenGL thread */ - protected static Thread glThread; + protected Thread glThread; /** ID of the GL context associated to the surface **/ - protected static int glContext; + protected int glContext; // ........................................................ @@ -437,7 +437,7 @@ public abstract class PGL { protected Texture wrapBackTexture(Texture texture) { if (texture == null) { - texture = new Texture(); + texture = new Texture(pg); texture.init(pg.width, pg.height, glColorTex.get(backTex), TEXTURE_2D, RGBA, fboWidth, fboHeight, NEAREST, NEAREST, @@ -454,7 +454,7 @@ public abstract class PGL { protected Texture wrapFrontTexture(Texture texture) { if (texture == null) { - texture = new Texture(); + texture = new Texture(pg); texture.init(pg.width, pg.height, glColorTex.get(frontTex), TEXTURE_2D, RGBA, fboWidth, fboHeight, NEAREST, NEAREST, @@ -737,6 +737,9 @@ public abstract class PGL { } + protected abstract void getGL(PGL pgl); + + protected abstract boolean canDraw(); @@ -903,7 +906,8 @@ public abstract class PGL { protected void initTex2DShader() { - if (!loadedTex2DShader || tex2DShaderContext != glContext) { + if (!loadedTex2DShader/* || tex2DShaderContext != glContext*/) { + System.out.println("initializing texture shader"); String vertSource = PApplet.join(texVertShaderSource, "\n"); String fragSource = PApplet.join(tex2DFragShaderSource, "\n"); tex2DVertShader = createShader(VERTEX_SHADER, vertSource); diff --git a/core/src/processing/opengl/PGraphics2D.java b/core/src/processing/opengl/PGraphics2D.java index 18df3b2a6..7229e561d 100644 --- a/core/src/processing/opengl/PGraphics2D.java +++ b/core/src/processing/opengl/PGraphics2D.java @@ -265,7 +265,7 @@ public class PGraphics2D extends PGraphicsOpenGL { } if (svg != null) { - PShapeOpenGL p2d = PShapeOpenGL.createShape2D(pg.parent, svg); + PShapeOpenGL p2d = PShapeOpenGL.createShape2D((PGraphicsOpenGL)pg, svg); return p2d; } else { return null; @@ -280,7 +280,7 @@ public class PGraphics2D extends PGraphicsOpenGL { @Override public PShape createShape(PShape source) { - return PShapeOpenGL.createShape2D(parent, source); + return PShapeOpenGL.createShape2D(this, source); } @@ -292,31 +292,31 @@ public class PGraphics2D extends PGraphicsOpenGL { @Override public PShape createShape(int type) { - return createShapeImpl(parent, type); + return createShapeImpl(this, type); } @Override public PShape createShape(int kind, float... p) { - return createShapeImpl(parent, kind, p); + return createShapeImpl(this, kind, p); } - static protected PShapeOpenGL createShapeImpl(PApplet parent, int type) { + static protected PShapeOpenGL createShapeImpl(PGraphicsOpenGL pg, int type) { PShapeOpenGL shape = null; if (type == PConstants.GROUP) { - shape = new PShapeOpenGL(parent, PConstants.GROUP); + shape = new PShapeOpenGL(pg, PConstants.GROUP); } else if (type == PShape.PATH) { - shape = new PShapeOpenGL(parent, PShape.PATH); + shape = new PShapeOpenGL(pg, PShape.PATH); } else if (type == PShape.GEOMETRY) { - shape = new PShapeOpenGL(parent, PShape.GEOMETRY); + shape = new PShapeOpenGL(pg, PShape.GEOMETRY); } shape.is3D(false); return shape; } - static protected PShapeOpenGL createShapeImpl(PApplet parent, + static protected PShapeOpenGL createShapeImpl(PGraphicsOpenGL pg, int kind, float... p) { PShapeOpenGL shape = null; int len = p.length; @@ -326,49 +326,49 @@ public class PGraphics2D extends PGraphicsOpenGL { showWarning("Wrong number of parameters"); return null; } - shape = new PShapeOpenGL(parent, PShape.PRIMITIVE); + shape = new PShapeOpenGL(pg, PShape.PRIMITIVE); shape.setKind(POINT); } else if (kind == LINE) { if (len != 4) { showWarning("Wrong number of parameters"); return null; } - shape = new PShapeOpenGL(parent, PShape.PRIMITIVE); + shape = new PShapeOpenGL(pg, PShape.PRIMITIVE); shape.setKind(LINE); } else if (kind == TRIANGLE) { if (len != 6) { showWarning("Wrong number of parameters"); return null; } - shape = new PShapeOpenGL(parent, PShape.PRIMITIVE); + shape = new PShapeOpenGL(pg, PShape.PRIMITIVE); shape.setKind(TRIANGLE); } else if (kind == QUAD) { if (len != 8) { showWarning("Wrong number of parameters"); return null; } - shape = new PShapeOpenGL(parent, PShape.PRIMITIVE); + shape = new PShapeOpenGL(pg, PShape.PRIMITIVE); shape.setKind(QUAD); } else if (kind == RECT) { if (len != 4 && len != 5 && len != 8 && len != 9) { showWarning("Wrong number of parameters"); return null; } - shape = new PShapeOpenGL(parent, PShape.PRIMITIVE); + shape = new PShapeOpenGL(pg, PShape.PRIMITIVE); shape.setKind(RECT); } else if (kind == ELLIPSE) { if (len != 4 && len != 5) { showWarning("Wrong number of parameters"); return null; } - shape = new PShapeOpenGL(parent, PShape.PRIMITIVE); + shape = new PShapeOpenGL(pg, PShape.PRIMITIVE); shape.setKind(ELLIPSE); } else if (kind == ARC) { if (len != 6 && len != 7) { showWarning("Wrong number of parameters"); return null; } - shape = new PShapeOpenGL(parent, PShape.PRIMITIVE); + shape = new PShapeOpenGL(pg, PShape.PRIMITIVE); shape.setKind(ARC); } else if (kind == BOX) { showWarning("Primitive not supported in 2D"); diff --git a/core/src/processing/opengl/PGraphics3D.java b/core/src/processing/opengl/PGraphics3D.java index 38b3e775d..77edcc9f0 100644 --- a/core/src/processing/opengl/PGraphics3D.java +++ b/core/src/processing/opengl/PGraphics3D.java @@ -144,7 +144,7 @@ public class PGraphics3D extends PGraphicsOpenGL { if (obj != null) { int prevTextureMode = pg.textureMode; pg.textureMode = NORMAL; - PShapeOpenGL p3d = PShapeOpenGL.createShape3D(pg.parent, obj); + PShapeOpenGL p3d = PShapeOpenGL.createShape3D((PGraphicsOpenGL)pg, obj); pg.textureMode = prevTextureMode; return p3d; } else { @@ -160,7 +160,7 @@ public class PGraphics3D extends PGraphicsOpenGL { @Override public PShape createShape(PShape source) { - return PShapeOpenGL.createShape3D(parent, source); + return PShapeOpenGL.createShape3D(this, source); } @@ -172,31 +172,31 @@ public class PGraphics3D extends PGraphicsOpenGL { @Override public PShape createShape(int type) { - return createShapeImpl(parent, type); + return createShapeImpl(this, type); } @Override public PShape createShape(int kind, float... p) { - return createShapeImpl(parent, kind, p); + return createShapeImpl(this, kind, p); } - static protected PShapeOpenGL createShapeImpl(PApplet parent, int type) { + static protected PShapeOpenGL createShapeImpl(PGraphicsOpenGL pg, int type) { PShapeOpenGL shape = null; if (type == PConstants.GROUP) { - shape = new PShapeOpenGL(parent, PConstants.GROUP); + shape = new PShapeOpenGL(pg, PConstants.GROUP); } else if (type == PShape.PATH) { - shape = new PShapeOpenGL(parent, PShape.PATH); + shape = new PShapeOpenGL(pg, PShape.PATH); } else if (type == PShape.GEOMETRY) { - shape = new PShapeOpenGL(parent, PShape.GEOMETRY); + shape = new PShapeOpenGL(pg, PShape.GEOMETRY); } shape.is3D(true); return shape; } - static protected PShapeOpenGL createShapeImpl(PApplet parent, + static protected PShapeOpenGL createShapeImpl(PGraphicsOpenGL pg, int kind, float... p) { PShapeOpenGL shape = null; int len = p.length; @@ -206,63 +206,63 @@ public class PGraphics3D extends PGraphicsOpenGL { showWarning("Wrong number of parameters"); return null; } - shape = new PShapeOpenGL(parent, PShape.PRIMITIVE); + shape = new PShapeOpenGL(pg, PShape.PRIMITIVE); shape.setKind(POINT); } else if (kind == LINE) { if (len != 4 && len != 6) { showWarning("Wrong number of parameters"); return null; } - shape = new PShapeOpenGL(parent, PShape.PRIMITIVE); + shape = new PShapeOpenGL(pg, PShape.PRIMITIVE); shape.setKind(LINE); } else if (kind == TRIANGLE) { if (len != 6) { showWarning("Wrong number of parameters"); return null; } - shape = new PShapeOpenGL(parent, PShape.PRIMITIVE); + shape = new PShapeOpenGL(pg, PShape.PRIMITIVE); shape.setKind(TRIANGLE); } else if (kind == QUAD) { if (len != 8) { showWarning("Wrong number of parameters"); return null; } - shape = new PShapeOpenGL(parent, PShape.PRIMITIVE); + shape = new PShapeOpenGL(pg, PShape.PRIMITIVE); shape.setKind(QUAD); } else if (kind == RECT) { if (len != 4 && len != 5 && len != 8 && len != 9) { showWarning("Wrong number of parameters"); return null; } - shape = new PShapeOpenGL(parent, PShape.PRIMITIVE); + shape = new PShapeOpenGL(pg, PShape.PRIMITIVE); shape.setKind(RECT); } else if (kind == ELLIPSE) { if (len != 4 && len != 5) { showWarning("Wrong number of parameters"); return null; } - shape = new PShapeOpenGL(parent, PShape.PRIMITIVE); + shape = new PShapeOpenGL(pg, PShape.PRIMITIVE); shape.setKind(ELLIPSE); } else if (kind == ARC) { if (len != 6 && len != 7) { showWarning("Wrong number of parameters"); return null; } - shape = new PShapeOpenGL(parent, PShape.PRIMITIVE); + shape = new PShapeOpenGL(pg, PShape.PRIMITIVE); shape.setKind(ARC); } else if (kind == BOX) { if (len != 1 && len != 3) { showWarning("Wrong number of parameters"); return null; } - shape = new PShapeOpenGL(parent, PShape.PRIMITIVE); + shape = new PShapeOpenGL(pg, PShape.PRIMITIVE); shape.setKind(BOX); } else if (kind == SPHERE) { if (len < 1 || 3 < len) { showWarning("Wrong number of parameters"); return null; } - shape = new PShapeOpenGL(parent, PShape.PRIMITIVE); + shape = new PShapeOpenGL(pg, PShape.PRIMITIVE); shape.setKind(SPHERE); } else { showWarning("Unrecognized primitive type"); diff --git a/core/src/processing/opengl/PGraphicsOpenGL.java b/core/src/processing/opengl/PGraphicsOpenGL.java index 9596caf9a..adfe00cbe 100644 --- a/core/src/processing/opengl/PGraphicsOpenGL.java +++ b/core/src/processing/opengl/PGraphicsOpenGL.java @@ -36,11 +36,8 @@ public class PGraphicsOpenGL extends PGraphics { /** Interface between Processing and OpenGL */ public PGL pgl; - /** The main PApplet renderer. */ - protected static PGraphicsOpenGL pgPrimary = null; - /** The renderer currently in use. */ - protected static PGraphicsOpenGL pgCurrent = null; + protected PGraphicsOpenGL currentPG; /** Font cache for texture objects. */ protected WeakHashMap fontMap = @@ -56,8 +53,6 @@ public class PGraphicsOpenGL extends PGraphics { "blendMode(%1$s) is not supported by this hardware (or driver)"; static final String BLEND_RENDERER_ERROR = "blendMode(%1$s) is not supported by this renderer"; - static final String NESTED_DRAW_ERROR = - "Already called drawing on another PGraphicsOpenGL object"; static final String ALREADY_BEGAN_CONTOUR_ERROR = "Already called beginContour()"; static final String NO_BEGIN_CONTOUR_ERROR = @@ -424,7 +419,7 @@ public class PGraphicsOpenGL extends PGraphics { // Screen surface: /** Texture containing the current frame */ - protected Texture texture; + public Texture texture; /** Texture containing the previous frame */ protected Texture ptexture; @@ -535,7 +530,7 @@ public class PGraphicsOpenGL extends PGraphics { inGeo = newInGeometry(this, IMMEDIATE); tessGeo = newTessGeometry(this, IMMEDIATE); - texCache = newTexCache(); + texCache = newTexCache(this); initialized = false; } @@ -686,21 +681,44 @@ public class PGraphicsOpenGL extends PGraphics { } + ////////////////////////////////////////////////////////////// + + // IMAGE METADATA FOR THIS RENDERER + + + @Override + public void setCache(PImage image, Object storage) { + getPrimaryPG().cacheMap.put(image, storage); + } + + + @Override + public Object getCache(PImage image) { + return getPrimaryPG().cacheMap.get(image); + } + + + @Override + public void removeCache(PImage image) { + getPrimaryPG().cacheMap.remove(image); + } + + ////////////////////////////////////////////////////////////// protected void setFontTexture(PFont font, FontTexture fontTexture) { - fontMap.put(font, fontTexture); + getPrimaryPG().fontMap.put(font, fontTexture); } protected FontTexture getFontTexture(PFont font) { - return fontMap.get(font); + return getPrimaryPG().fontMap.get(font); } protected void removeFontTexture(PFont font) { - fontMap.remove(font); + getPrimaryPG().fontMap.remove(font); } @@ -1216,7 +1234,7 @@ public class PGraphicsOpenGL extends PGraphics { fbStackDepth--; FrameBuffer fbo = fbStack[fbStackDepth]; if (currentFramebuffer != fbo) { - currentFramebuffer.finish(pgPrimary); + currentFramebuffer.finish(); currentFramebuffer = fbo; currentFramebuffer.bind(); } @@ -1605,6 +1623,13 @@ public class PGraphicsOpenGL extends PGraphics { @Override public void beginDraw() { + if (primarySurface) { + setCurrentPG(this); + } else { + pgl.getGL(getPrimaryPGL()); + getPrimaryPG().setCurrentPG(this); + } + report("top beginDraw()"); if (!checkGLThread()) { @@ -1615,19 +1640,11 @@ public class PGraphicsOpenGL extends PGraphics { return; } - if (pgCurrent != null && !pgCurrent.primarySurface && - !this.primarySurface) { - // It seems that the user is trying to start another beginDraw()/endDraw() - // block for an offscreen surface, still drawing on another one. - PGraphics.showWarning(NESTED_DRAW_ERROR); - return; - } - - if (!primarySurface && pgPrimary.texCache.containsTexture(this)) { + if (!primarySurface && getPrimaryPG().texCache.containsTexture(this)) { // This offscreen surface is being used as a texture earlier in draw, - // so we should update the rendering up to this point since it will + // so we should update the rendering up to this point since it will be // modified. - pgPrimary.flush(); + getPrimaryPG().flush(); } if (!glParamsRead) { @@ -1642,7 +1659,6 @@ public class PGraphicsOpenGL extends PGraphics { } setDrawDefaults(); // TODO: look at using checkSettings() instead... - pgCurrent = this; drawing = true; report("bot beginDraw()"); @@ -1661,7 +1677,7 @@ public class PGraphicsOpenGL extends PGraphics { flush(); if (PGL.SAVE_SURFACE_TO_PIXELS_HACK && - (!pgPrimary.initialized || parent.frameCount == 0)) { + (!getPrimaryPG().initialized || parent.frameCount == 0)) { // Smooth was disabled/enabled at some point during drawing. We save // the current contents of the back buffer (because the buffers haven't // been swapped yet) to the pixels array. The frameCount == 0 condition @@ -1677,12 +1693,10 @@ public class PGraphicsOpenGL extends PGraphics { endOffscreenDraw(); } - if (pgCurrent == pgPrimary) { - // Done with the main surface - pgCurrent = null; + if (primarySurface) { + setCurrentPG(null); } else { - // Done with an offscreen surface, going back to onscreen drawing. - pgCurrent = pgPrimary; + getPrimaryPG().setCurrentPG(getPrimaryPG()); } drawing = false; @@ -1696,6 +1710,31 @@ public class PGraphicsOpenGL extends PGraphics { } + protected PGraphicsOpenGL getPrimaryPG() { + if (primarySurface) { + return this; + } else { + return (PGraphicsOpenGL)parent.g; + } + } + + protected void setCurrentPG(PGraphicsOpenGL pg) { + currentPG = pg; + } + + protected PGraphicsOpenGL getCurrentPG() { + return currentPG; + } + + protected PGL getPrimaryPGL() { + if (primarySurface) { + return pgl; + } else { + return ((PGraphicsOpenGL)parent.g).pgl; + } + } + + @Override public PGL beginPGL() { flush(); @@ -1764,11 +1803,11 @@ public class PGraphicsOpenGL extends PGraphics { } public void beginReadPixels() { - pgCurrent.beginPixelsOp(OP_READ); + beginPixelsOp(OP_READ); } public void endReadPixels() { - pgCurrent.endPixelsOp(); + endPixelsOp(); } protected void beginPixelsOp(int op) { @@ -2229,7 +2268,7 @@ public class PGraphicsOpenGL extends PGraphics { tessellator.setStrokeWeight(strokeWeight); tessellator.setStrokeCap(strokeCap); tessellator.setStrokeJoin(strokeJoin); - tessellator.setRenderer(pgCurrent); + tessellator.setRenderer(this); tessellator.setTransform(modelview); tessellator.set3D(is3D()); @@ -3505,11 +3544,11 @@ public class PGraphicsOpenGL extends PGraphics { protected void textLineImpl(char buffer[], int start, int stop, float x, float y) { if (textMode == MODEL) { - textTex = pgPrimary.getFontTexture(textFont); + textTex = getFontTexture(textFont); if (textTex == null || textTex.contextIsOutdated()) { - textTex = new FontTexture(textFont, is3D()); - pgPrimary.setFontTexture(textFont, textTex); + textTex = new FontTexture(this, textFont, is3D()); + setFontTexture(textFont, textTex); } textTex.begin(); @@ -3568,7 +3607,7 @@ public class PGraphicsOpenGL extends PGraphics { if (tinfo == null) { // Adding new glyph to the font texture. - tinfo = textTex.addToTexture(pgPrimary, glyph); + tinfo = textTex.addToTexture(this, glyph); } float high = glyph.height / (float) textFont.getSize(); @@ -5327,16 +5366,6 @@ public class PGraphicsOpenGL extends PGraphics { } - - ////////////////////////////////////////////////////////////// - - // PIMAGE METHODS - - // getImage - // setCache, getCache, removeCache - // isModified, setModified - - ////////////////////////////////////////////////////////////// // LOAD/UPDATE PIXELS @@ -5617,16 +5646,16 @@ public class PGraphicsOpenGL extends PGraphics { if (texture == null || texture.contextIsOutdated()) { Texture.Parameters params = new Texture.Parameters(ARGB, sampling, mipmap); - texture = new Texture(width, height, params); + texture = new Texture(this, width, height, params); texture.invertedY(true); texture.colorBuffer(true); - pgPrimary.setCache(this, texture); + setCache(this, texture); } } protected void createPTexture() { - ptexture = new Texture(width, height, texture.getParameters()); + ptexture = new Texture(this, width, height, texture.getParameters()); ptexture.invertedY(true); ptexture.colorBuffer(true); } @@ -5755,7 +5784,7 @@ public class PGraphicsOpenGL extends PGraphics { loadTexture(); if (filterTexture == null || filterTexture.contextIsOutdated()) { - filterTexture = new Texture(texture.width, texture.height, + filterTexture = new Texture(this, texture.width, texture.height, texture.getParameters()); filterTexture.invertedY(true); filterImage = wrapTexture(filterTexture); @@ -5826,7 +5855,7 @@ public class PGraphicsOpenGL extends PGraphics { if (primarySurface) pgl.requestFBOLayer(); loadTexture(); if (filterTexture == null || filterTexture.contextIsOutdated()) { - filterTexture = new Texture(texture.width, texture.height, + filterTexture = new Texture(this, texture.width, texture.height, texture.getParameters()); filterTexture.invertedY(true); filterImage = wrapTexture(filterTexture); @@ -6044,7 +6073,7 @@ public class PGraphicsOpenGL extends PGraphics { return null; } - Texture tex = (Texture)pgPrimary.getCache(img); + Texture tex = (Texture)getCache(img); if (tex == null || tex.contextIsOutdated()) { tex = addTexture(img); if (tex != null) { @@ -6097,8 +6126,8 @@ public class PGraphicsOpenGL extends PGraphics { if (img.parent == null) { img.parent = parent; } - Texture tex = new Texture(img.width, img.height, params); - pgPrimary.setCache(img, tex); + Texture tex = new Texture(this, img.width, img.height, params); + setCache(img, tex); return tex; } @@ -6132,7 +6161,7 @@ public class PGraphicsOpenGL extends PGraphics { img.width = tex.width; img.height = tex.height; img.format = ARGB; - pgPrimary.setCache(img, tex); + setCache(img, tex); return img; } @@ -6194,10 +6223,9 @@ public class PGraphicsOpenGL extends PGraphics { protected void initPrimary() { pgl.initSurface(quality); if (texture != null) { - pgPrimary.removeCache(this); + removeCache(this); texture = ptexture = null; } - pgPrimary = this; initialized = true; } @@ -6247,7 +6275,7 @@ public class PGraphicsOpenGL extends PGraphics { packedDepthStencilSupported; if (PGraphicsOpenGL.fboMultisampleSupported && 1 < quality) { multisampleFramebuffer = - new FrameBuffer(texture.glWidth, texture.glHeight, quality, 0, + new FrameBuffer(this, texture.glWidth, texture.glHeight, quality, 0, depthBits, stencilBits, packed, false); multisampleFramebuffer.clear(); @@ -6257,13 +6285,13 @@ public class PGraphicsOpenGL extends PGraphics { // to doesn't need depth and stencil buffers since they are part of the // multisampled framebuffer. offscreenFramebuffer = - new FrameBuffer(texture.glWidth, texture.glHeight, 1, 1, 0, 0, + new FrameBuffer(this, texture.glWidth, texture.glHeight, 1, 1, 0, 0, false, false); } else { quality = 0; offscreenFramebuffer = - new FrameBuffer(texture.glWidth, texture.glHeight, 1, 1, + new FrameBuffer(this, texture.glWidth, texture.glHeight, 1, 1, depthBits, stencilBits, packed, false); offscreenMultisample = false; } @@ -6331,7 +6359,7 @@ public class PGraphicsOpenGL extends PGraphics { popFramebuffer(); texture.updateTexels(); // Mark all texels in screen texture as modified. - pgPrimary.restoreGL(); + getPrimaryPG().restoreGL(); } @@ -6740,14 +6768,15 @@ public class PGraphicsOpenGL extends PGraphics { } - static protected TexCache newTexCache() { - return new TexCache(); + static protected TexCache newTexCache(PGraphicsOpenGL pg) { + return new TexCache(pg); } // Holds an array of textures and the range of vertex // indices each texture applies to. static protected class TexCache { + PGraphicsOpenGL pg; int size; PImage[] textures; int[] firstIndex; @@ -6756,7 +6785,8 @@ public class PGraphicsOpenGL extends PGraphics { int[] lastCache; boolean hasTextures; - TexCache() { + TexCache(PGraphicsOpenGL pg) { + this.pg = pg; allocate(); } @@ -6792,7 +6822,7 @@ public class PGraphicsOpenGL extends PGraphics { Texture tex = null; if (img != null) { - tex = pgPrimary.getTexture(img); + tex = pg.getTexture(img); } return tex; diff --git a/core/src/processing/opengl/PJOGL.java b/core/src/processing/opengl/PJOGL.java index 8dc1fa95e..6979d4df4 100644 --- a/core/src/processing/opengl/PJOGL.java +++ b/core/src/processing/opengl/PJOGL.java @@ -66,16 +66,16 @@ public class PJOGL extends PGL { // Public members to access the underlying GL objects and context /** Basic GL functionality, common to all profiles */ - public static GL gl; + public GL gl; /** GLU interface **/ - public static GLU glu; + public GLU glu; /** The rendering context (holds rendering state info) */ - public static GLContext context; + public GLContext context; /** The canvas where OpenGL rendering takes place */ - public static Canvas canvas; + public Canvas canvas; /** Selected GL profile */ public static GLProfile profile; @@ -132,32 +132,32 @@ public class PJOGL extends PGL { // Protected JOGL-specific objects needed to access the GL profiles /** The capabilities of the OpenGL rendering surface */ - protected static GLCapabilitiesImmutable capabilities; + protected GLCapabilitiesImmutable capabilities; /** The rendering surface */ - protected static GLDrawable drawable; + protected GLDrawable drawable; /** GLES2 functionality (shaders, etc) */ - protected static GL2ES2 gl2; + protected GL2ES2 gl2; /** GL3 interface */ - protected static GL2GL3 gl3; + protected GL2GL3 gl3; /** GL2 desktop functionality (blit framebuffer, map buffer range, * multisampled renerbuffers) */ - protected static GL2 gl2x; + protected GL2 gl2x; /** The AWT-OpenGL canvas */ - protected static GLCanvas canvasAWT; + protected GLCanvas canvasAWT; /** The NEWT-OpenGL canvas */ - protected static NewtCanvasAWT canvasNEWT; + protected NewtCanvasAWT canvasNEWT; /** The NEWT window */ - protected static GLWindow window; + protected GLWindow window; /** The listener that fires the frame rendering in Processing */ - protected static PGLListener listener; + protected PGLListener listener; /** This countdown latch is used to maintain the synchronization between * Processing's drawing thread and JOGL's rendering thread */ @@ -490,7 +490,7 @@ public class PJOGL extends PGL { protected Texture wrapBackTexture(Texture texture) { if (texture == null || changedBackTex) { if (USE_JOGL_FBOLAYER) { - texture = new Texture(); + texture = new Texture(pg); texture.init(pg.width, pg.height, backTexAttach.getName(), TEXTURE_2D, RGBA, backTexAttach.getWidth(), backTexAttach.getHeight(), @@ -517,7 +517,7 @@ public class PJOGL extends PGL { protected Texture wrapFrontTexture(Texture texture) { if (texture == null || changedFrontTex) { if (USE_JOGL_FBOLAYER) { - texture = new Texture(); + texture = new Texture(pg); texture.init(pg.width, pg.height, backTexAttach.getName(), TEXTURE_2D, RGBA, frontTexAttach.getWidth(), frontTexAttach.getHeight(), @@ -612,6 +612,23 @@ public class PJOGL extends PGL { } + @Override + protected void getGL(PGL pgl) { + PJOGL pjogl = (PJOGL)pgl; + + this.drawable = pjogl.drawable; + this.context = pjogl.context; + this.glContext = pjogl.glContext; + this.glThread = pjogl.glThread; + + this.gl = pjogl.gl; + this.gl2 = pjogl.gl2; + this.gl2x = pjogl.gl2x; + this.gl3 = pjogl.gl3; + } + + + @Override protected boolean canDraw() { return pg.initialized && pg.parent.isDisplayable(); diff --git a/core/src/processing/opengl/PShader.java b/core/src/processing/opengl/PShader.java index d903a5670..97dbbc891 100644 --- a/core/src/processing/opengl/PShader.java +++ b/core/src/processing/opengl/PShader.java @@ -77,7 +77,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 PGraphicsOpenGL pg; + protected PGraphicsOpenGL primaryPG; + protected PGraphicsOpenGL currentPG; protected PGL pgl; protected int context; // The context that created this shader. @@ -180,7 +181,8 @@ public class PShader implements PConstants { public PShader(PApplet parent) { this(); this.parent = parent; - pgl = PGraphicsOpenGL.pgCurrent.pgl; + primaryPG = (PGraphicsOpenGL)parent.g; + pgl = primaryPG.pgl; context = pgl.createEmptyContext(); } @@ -195,7 +197,8 @@ public class PShader implements PConstants { */ public PShader(PApplet parent, String vertFilename, String fragFilename) { this.parent = parent; - pgl = PGraphicsOpenGL.pgCurrent.pgl; + primaryPG = (PGraphicsOpenGL)parent.g; + pgl = primaryPG.pgl; this.vertexURL = null; this.fragmentURL = null; @@ -233,7 +236,8 @@ public class PShader implements PConstants { */ public PShader(PApplet parent, URL vertURL, URL fragURL) { this.parent = parent; - pgl = PGraphicsOpenGL.pgCurrent.pgl; + primaryPG = (PGraphicsOpenGL)parent.g; + pgl = primaryPG.pgl; this.vertexURL = vertURL; this.fragmentURL = fragURL; @@ -266,7 +270,8 @@ public class PShader implements PConstants { public PShader(PApplet parent, String[] vertSource, String[] fragSource) { this.parent = parent; - pgl = PGraphicsOpenGL.pgCurrent.pgl; + primaryPG = (PGraphicsOpenGL)parent.g; + pgl = primaryPG.pgl; this.vertexURL = null; this.fragmentURL = null; @@ -828,7 +833,7 @@ public class PShader implements PConstants { pgl.uniformMatrix4fv(loc, 1, false, floatBuffer); } else if (val.type == UniformValue.SAMPLER2D) { PImage img = (PImage)val.value; - Texture tex = PGraphicsOpenGL.pgPrimary.getTexture(img); + Texture tex = currentPG.getTexture(img); if (textures == null) textures = new HashMap(); textures.put(loc, tex); @@ -1108,7 +1113,7 @@ public class PShader implements PConstants { protected void setRenderer(PGraphicsOpenGL pg) { - this.pg = pg; + this.currentPG = pg; } @@ -1182,25 +1187,25 @@ public class PShader implements PConstants { protected void setCommonUniforms() { if (-1 < transformMatLoc) { - pg.updateGLProjmodelview(); - setUniformMatrix(transformMatLoc, pg.glProjmodelview); + currentPG.updateGLProjmodelview(); + setUniformMatrix(transformMatLoc, currentPG.glProjmodelview); } if (-1 < modelviewMatLoc) { - pg.updateGLModelview(); - setUniformMatrix(modelviewMatLoc, pg.glModelview); + currentPG.updateGLModelview(); + setUniformMatrix(modelviewMatLoc, currentPG.glModelview); } if (-1 < projectionMatLoc) { - pg.updateGLProjection(); - setUniformMatrix(projectionMatLoc, pg.glProjection); + currentPG.updateGLProjection(); + setUniformMatrix(projectionMatLoc, currentPG.glProjection); } if (-1 < viewportLoc) { - float x = pg.viewport.get(0); - float y = pg.viewport.get(1); - float w = pg.viewport.get(2); - float h = pg.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); } @@ -1208,15 +1213,15 @@ public class PShader implements PConstants { bufferUnit = getLastTexUnit() + 1; setUniformValue(bufferLoc, bufferUnit); pgl.activeTexture(PGL.TEXTURE0 + bufferUnit); - pg.bindFrontTexture(); + currentPG.bindFrontTexture(); } else { bufferUnit = -1; } } protected void bindTyped() { - if (pg == null) { - setRenderer(PGraphicsOpenGL.pgCurrent); + if (currentPG == null) { + setRenderer(primaryPG.getCurrentPG()); loadAttributes(); loadUniforms(); } @@ -1228,8 +1233,8 @@ public class PShader implements PConstants { if (-1 < normalLoc) pgl.enableVertexAttribArray(normalLoc); if (-1 < normalMatLoc) { - pg.updateGLNormal(); - setUniformMatrix(normalMatLoc, pg.glNormal); + currentPG.updateGLNormal(); + setUniformMatrix(normalMatLoc, currentPG.glNormal); } if (-1 < ambientLoc) pgl.enableVertexAttribArray(ambientLoc); @@ -1237,17 +1242,17 @@ public class PShader implements PConstants { if (-1 < emissiveLoc) pgl.enableVertexAttribArray(emissiveLoc); if (-1 < shininessLoc) pgl.enableVertexAttribArray(shininessLoc); - int count = pg.lightCount; + int count = currentPG.lightCount; setUniformValue(lightCountLoc, count); if (0 < count) { - setUniformVector(lightPositionLoc, pg.lightPosition, 4, count); - setUniformVector(lightNormalLoc, pg.lightNormal, 3, count); - setUniformVector(lightAmbientLoc, pg.lightAmbient, 3, count); - setUniformVector(lightDiffuseLoc, pg.lightDiffuse, 3, count); - setUniformVector(lightSpecularLoc, pg.lightSpecular, 3, count); - setUniformVector(lightFalloffLoc, pg.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, pg.lightSpotParameters, 2, count); + setUniformVector(lightSpotLoc, currentPG.lightSpotParameters, 2, count); } if (-1 < directionLoc) pgl.enableVertexAttribArray(directionLoc); @@ -1255,8 +1260,8 @@ public class PShader implements PConstants { if (-1 < offsetLoc) pgl.enableVertexAttribArray(offsetLoc); if (-1 < perspectiveLoc) { - if (pg.getHint(ENABLE_STROKE_PERSPECTIVE) && - pg.nonOrthoProjection()) { + if (currentPG.getHint(ENABLE_STROKE_PERSPECTIVE) && + currentPG.nonOrthoProjection()) { setUniformValue(perspectiveLoc, 1); } else { setUniformValue(perspectiveLoc, 0); @@ -1264,11 +1269,11 @@ public class PShader implements PConstants { } if (-1 < scaleLoc) { - if (pg.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 (pg.orthoProjection()) { + if (currentPG.orthoProjection()) { setUniformValue(scaleLoc, 1, 1, f); } else { setUniformValue(scaleLoc, f, f, f); @@ -1302,7 +1307,7 @@ public class PShader implements PConstants { if (-1 < bufferLoc) { pgl.requestFBOLayer(); pgl.activeTexture(PGL.TEXTURE0 + bufferUnit); - pg.unbindFrontTexture(); + currentPG.unbindFrontTexture(); pgl.activeTexture(PGL.TEXTURE0); } diff --git a/core/src/processing/opengl/PShapeOpenGL.java b/core/src/processing/opengl/PShapeOpenGL.java index eff4ad51e..6db7fddca 100644 --- a/core/src/processing/opengl/PShapeOpenGL.java +++ b/core/src/processing/opengl/PShapeOpenGL.java @@ -290,8 +290,8 @@ public class PShapeOpenGL extends PShape { } - public PShapeOpenGL(PApplet parent, int family) { - pg = PGraphicsOpenGL.pgCurrent; + public PShapeOpenGL(PGraphicsOpenGL pg, int family) { + this.pg = pg; pgl = pg.pgl; context = pgl.createEmptyContext(); @@ -568,20 +568,19 @@ public class PShapeOpenGL extends PShape { // Shape creation (temporary hack) - public static PShapeOpenGL createShape3D(PApplet parent, PShape src) { + public static PShapeOpenGL createShape3D(PGraphicsOpenGL pg, PShape src) { PShapeOpenGL dest = null; if (src.getFamily() == GROUP) { - dest = PGraphics3D.createShapeImpl(parent, GROUP); - copyGroup3D(parent, src, dest); + dest = PGraphics3D.createShapeImpl(pg, GROUP); + copyGroup3D(pg, src, dest); } else if (src.getFamily() == PRIMITIVE) { - dest = PGraphics3D.createShapeImpl(parent, src.getKind(), - src.getParams()); + dest = PGraphics3D.createShapeImpl(pg, src.getKind(), src.getParams()); PShape.copyPrimitive(src, dest); } else if (src.getFamily() == GEOMETRY) { - dest = PGraphics3D.createShapeImpl(parent, PShape.GEOMETRY); + dest = PGraphics3D.createShapeImpl(pg, PShape.GEOMETRY); PShape.copyGeometry(src, dest); } else if (src.getFamily() == PATH) { - dest = PGraphics3D.createShapeImpl(parent, PATH); + dest = PGraphics3D.createShapeImpl(pg, PATH); PShape.copyPath(src, dest); } dest.setName(src.getName()); @@ -592,20 +591,19 @@ public class PShapeOpenGL extends PShape { } - static public PShapeOpenGL createShape2D(PApplet parent, PShape src) { + static public PShapeOpenGL createShape2D(PGraphicsOpenGL pg, PShape src) { PShapeOpenGL dest = null; if (src.getFamily() == GROUP) { - dest = PGraphics2D.createShapeImpl(parent, GROUP); - copyGroup2D(parent, src, dest); + dest = PGraphics2D.createShapeImpl(pg, GROUP); + copyGroup2D(pg, src, dest); } else if (src.getFamily() == PRIMITIVE) { - dest = PGraphics2D.createShapeImpl(parent, src.getKind(), - src.getParams()); + dest = PGraphics2D.createShapeImpl(pg, src.getKind(), src.getParams()); PShape.copyPrimitive(src, dest); } else if (src.getFamily() == GEOMETRY) { - dest = PGraphics2D.createShapeImpl(parent, PShape.GEOMETRY); + dest = PGraphics2D.createShapeImpl(pg, PShape.GEOMETRY); PShape.copyGeometry(src, dest); } else if (src.getFamily() == PATH) { - dest = PGraphics2D.createShapeImpl(parent, PATH); + dest = PGraphics2D.createShapeImpl(pg, PATH); PShape.copyPath(src, dest); } dest.setName(src.getName()); @@ -615,25 +613,25 @@ public class PShapeOpenGL extends PShape { } - static public void copyGroup3D(PApplet parent, PShape src, PShape dest) { + static public void copyGroup3D(PGraphicsOpenGL pg, PShape src, PShape dest) { copyMatrix(src, dest); copyStyles(src, dest); copyImage(src, dest); for (int i = 0; i < src.getChildCount(); i++) { - PShape c = createShape3D(parent, src.getChild(i)); + PShape c = createShape3D(pg, src.getChild(i)); dest.addChild(c); } } - static public void copyGroup2D(PApplet parent, PShape src, PShape dest) { + static public void copyGroup2D(PGraphicsOpenGL pg, PShape src, PShape dest) { copyMatrix(src, dest); copyStyles(src, dest); copyImage(src, dest); for (int i = 0; i < src.getChildCount(); i++) { - PShape c = createShape2D(parent, src.getChild(i)); + PShape c = createShape2D(pg, src.getChild(i)); dest.addChild(c); } } @@ -2413,9 +2411,9 @@ public class PShapeOpenGL extends PShape { PShape tess; if (is3D()) { - tess = PGraphics3D.createShapeImpl(pg.parent, PShape.GEOMETRY); + tess = PGraphics3D.createShapeImpl(pg, PShape.GEOMETRY); } else if (is2D()) { - tess = PGraphics2D.createShapeImpl(pg.parent, PShape.GEOMETRY); + tess = PGraphics2D.createShapeImpl(pg, PShape.GEOMETRY); } else { PGraphics.showWarning("This shape is not either 2D or 3D!"); return null; diff --git a/core/src/processing/opengl/Texture.java b/core/src/processing/opengl/Texture.java index c75b6bcc2..cd9ab6052 100644 --- a/core/src/processing/opengl/Texture.java +++ b/core/src/processing/opengl/Texture.java @@ -83,6 +83,7 @@ public class Texture implements PConstants { public int glWidth; public int glHeight; + 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 @@ -118,8 +119,9 @@ public class Texture implements PConstants { // Constructors. - public Texture() { - pgl = PGraphicsOpenGL.pgPrimary.pgl; + public Texture(PGraphicsOpenGL pg) { + this.pg = pg; + pgl = pg.pgl; context = pgl.createEmptyContext(); colorBuffer = false; @@ -134,8 +136,8 @@ public class Texture implements PConstants { * @param width int * @param height int */ - public Texture(int width, int height) { - this(width, height, new Parameters()); + public Texture(PGraphicsOpenGL pg, int width, int height) { + this(pg, width, height, new Parameters()); } @@ -146,8 +148,9 @@ public class Texture implements PConstants { * @param height int * @param params Parameters */ - public Texture(int width, int height, Object params) { - pgl = PGraphicsOpenGL.pgPrimary.pgl; + public Texture(PGraphicsOpenGL pg, int width, int height, Object params) { + this.pg = pg; + pgl = pg.pgl; context = pgl.createEmptyContext(); colorBuffer = false; @@ -249,7 +252,7 @@ public class Texture implements PConstants { dispose(); // Creating new texture with the appropriate size. - Texture tex = new Texture(wide, high, getParameters()); + Texture tex = new Texture(pg, wide, high, getParameters()); // Copying the contents of this texture into tex. tex.set(this); @@ -511,7 +514,7 @@ public class Texture implements PConstants { } if (tempFbo == null) { - tempFbo = new FrameBuffer(glWidth, glHeight); + tempFbo = new FrameBuffer(pg, glWidth, glHeight); } // Attaching the texture to the color buffer of a FBO, binding the FBO and @@ -1237,7 +1240,7 @@ public class Texture implements PConstants { } if (tempFbo == null) { - tempFbo = new FrameBuffer(glWidth, glHeight); + tempFbo = new FrameBuffer(pg, glWidth, glHeight); } // This texture is the color (destination) buffer of the FBO. @@ -1277,7 +1280,7 @@ public class Texture implements PConstants { int texWidth, int texHeight, int x, int y, int w, int h, boolean scale) { if (tempFbo == null) { - tempFbo = new FrameBuffer(glWidth, glHeight); + tempFbo = new FrameBuffer(pg, glWidth, glHeight); } // This texture is the color (destination) buffer of the FBO. From 9aef0a1426b61d40fe348b599259f39a63aee9b9 Mon Sep 17 00:00:00 2001 From: codeanticode Date: Mon, 20 Jan 2014 12:40:45 -0500 Subject: [PATCH 15/51] default shaders are not static, but only initialized in the primary renderer --- .../processing/opengl/PGraphicsOpenGL.java | 69 ++++++++++--------- 1 file changed, 36 insertions(+), 33 deletions(-) diff --git a/core/src/processing/opengl/PGraphicsOpenGL.java b/core/src/processing/opengl/PGraphicsOpenGL.java index adfe00cbe..732e2a1f1 100644 --- a/core/src/processing/opengl/PGraphicsOpenGL.java +++ b/core/src/processing/opengl/PGraphicsOpenGL.java @@ -186,7 +186,7 @@ public class PGraphicsOpenGL extends PGraphics { // ........................................................ - // GL objects: + // GL resources: static protected HashMap glTextureObjects = new HashMap(); @@ -227,17 +227,16 @@ public class PGraphicsOpenGL extends PGraphics { PGraphicsOpenGL.class.getResource("PointVert.glsl"); static protected URL defPointShaderFragURL = PGraphicsOpenGL.class.getResource("PointFrag.glsl"); - - static protected PShader defColorShader; - static protected PShader defTextureShader; - static protected PShader defLightShader; - static protected PShader defTexlightShader; - static protected PShader defLineShader; - static protected PShader defPointShader; - static protected URL maskShaderFragURL = PGraphicsOpenGL.class.getResource("MaskFrag.glsl"); - static protected PShader maskShader; + + protected PShader defColorShader; + protected PShader defTextureShader; + protected PShader defLightShader; + protected PShader defTexlightShader; + protected PShader defLineShader; + protected PShader defPointShader; + protected PShader maskShader; protected PShader polyShader; protected PShader lineShader; @@ -5727,12 +5726,13 @@ public class PGraphicsOpenGL extends PGraphics { "the same size as the applet."); } - if (maskShader == null) { - maskShader = new PShader(parent, defTextureShaderVertURL, - maskShaderFragURL); + PGraphicsOpenGL ppg = getPrimaryPG(); + if (ppg.maskShader == null) { + ppg.maskShader = new PShader(parent, defTextureShaderVertURL, + maskShaderFragURL); } - maskShader.set("mask", alpha); - filter(maskShader); + ppg.maskShader.set("mask", alpha); + filter(ppg.maskShader); } @@ -6634,6 +6634,7 @@ public class PGraphicsOpenGL extends PGraphics { protected PShader getPolyShader(boolean lit, boolean tex) { PShader shader; + PGraphicsOpenGL ppg = getPrimaryPG(); boolean useDefault = polyShader == null; if (polyShader != null) { polyShader.setRenderer(this); @@ -6643,23 +6644,23 @@ public class PGraphicsOpenGL extends PGraphics { if (lit) { if (tex) { if (useDefault || !polyShader.checkPolyType(PShader.TEXLIGHT)) { - if (defTexlightShader == null) { + if (ppg.defTexlightShader == null) { String[] vertSource = pgl.loadVertexShader(defTexlightShaderVertURL, 120); String[] fragSource = pgl.loadFragmentShader(defTextureShaderFragURL, 120); - defTexlightShader = new PShader(parent, vertSource, fragSource); + ppg.defTexlightShader = new PShader(parent, vertSource, fragSource); } - shader = defTexlightShader; + shader = ppg.defTexlightShader; } else { shader = polyShader; } } else { if (useDefault || !polyShader.checkPolyType(PShader.LIGHT)) { - if (defLightShader == null) { + if (ppg.defLightShader == null) { String[] vertSource = pgl.loadVertexShader(defLightShaderVertURL, 120); String[] fragSource = pgl.loadFragmentShader(defColorShaderFragURL, 120); - defLightShader = new PShader(parent, vertSource, fragSource); + ppg.defLightShader = new PShader(parent, vertSource, fragSource); } - shader = defLightShader; + shader = ppg.defLightShader; } else { shader = polyShader; } @@ -6672,23 +6673,23 @@ public class PGraphicsOpenGL extends PGraphics { if (tex) { if (useDefault || !polyShader.checkPolyType(PShader.TEXTURE)) { - if (defTextureShader == null) { + if (ppg.defTextureShader == null) { String[] vertSource = pgl.loadVertexShader(defTextureShaderVertURL, 120); String[] fragSource = pgl.loadFragmentShader(defTextureShaderFragURL, 120); - defTextureShader = new PShader(parent, vertSource, fragSource); + ppg.defTextureShader = new PShader(parent, vertSource, fragSource); } - shader = defTextureShader; + shader = ppg.defTextureShader; } else { shader = polyShader; } } else { if (useDefault || !polyShader.checkPolyType(PShader.COLOR)) { - if (defColorShader == null) { + if (ppg.defColorShader == null) { String[] vertSource = pgl.loadVertexShader(defColorShaderVertURL, 120); String[] fragSource = pgl.loadFragmentShader(defColorShaderFragURL, 120); - defColorShader = new PShader(parent, vertSource, fragSource); + ppg.defColorShader = new PShader(parent, vertSource, fragSource); } - shader = defColorShader; + shader = ppg.defColorShader; } else { shader = polyShader; } @@ -6705,13 +6706,14 @@ public class PGraphicsOpenGL extends PGraphics { protected PShader getLineShader() { PShader shader; + PGraphicsOpenGL ppg = getPrimaryPG(); if (lineShader == null) { - if (defLineShader == null) { + if (ppg.defLineShader == null) { String[] vertSource = pgl.loadVertexShader(defLineShaderVertURL, 120); String[] fragSource = pgl.loadFragmentShader(defLineShaderFragURL, 120); - defLineShader = new PShader(parent, vertSource, fragSource); + ppg.defLineShader = new PShader(parent, vertSource, fragSource); } - shader = defLineShader; + shader = ppg.defLineShader; } else { shader = lineShader; } @@ -6724,13 +6726,14 @@ public class PGraphicsOpenGL extends PGraphics { protected PShader getPointShader() { PShader shader; + PGraphicsOpenGL ppg = getPrimaryPG(); if (pointShader == null) { - if (defPointShader == null) { + if (ppg.defPointShader == null) { String[] vertSource = pgl.loadVertexShader(defPointShaderVertURL, 120); String[] fragSource = pgl.loadFragmentShader(defPointShaderFragURL, 120); - defPointShader = new PShader(parent, vertSource, fragSource); + ppg.defPointShader = new PShader(parent, vertSource, fragSource); } - shader = defPointShader; + shader = ppg.defPointShader; } else { shader = pointShader; } From 9110cebd0dc1914f00ef78a8aca1dd6957442a7e Mon Sep 17 00:00:00 2001 From: codeanticode Date: Mon, 20 Jan 2014 16:48:19 -0500 Subject: [PATCH 16/51] more refactoring/testing with non-static fields in PGL --- core/src/processing/opengl/FrameBuffer.java | 42 +- core/src/processing/opengl/PGL.java | 395 ++++++++++-------- .../processing/opengl/PGraphicsOpenGL.java | 209 ++++----- core/src/processing/opengl/PJOGL.java | 134 +++--- core/src/processing/opengl/Texture.java | 18 +- 5 files changed, 431 insertions(+), 367 deletions(-) diff --git a/core/src/processing/opengl/FrameBuffer.java b/core/src/processing/opengl/FrameBuffer.java index 1e5ef72a6..e70ac1173 100644 --- a/core/src/processing/opengl/FrameBuffer.java +++ b/core/src/processing/opengl/FrameBuffer.java @@ -174,15 +174,15 @@ public class FrameBuffer implements PConstants { } public void clear() { - PGraphicsOpenGL.pushFramebuffer(); - PGraphicsOpenGL.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); - PGraphicsOpenGL.popFramebuffer(); + pg.popFramebuffer(); } public void copy(FrameBuffer dest, FrameBuffer current) { @@ -273,8 +273,8 @@ public class FrameBuffer implements PConstants { colorBufferTex[i] = textures[i]; } - PGraphicsOpenGL.pushFramebuffer(); - PGraphicsOpenGL.setFramebuffer(this); + pg.pushFramebuffer(); + pg.setFramebuffer(this); // Making sure nothing is attached. for (int i = 0; i < numColorBuffers; i++) { @@ -290,7 +290,7 @@ public class FrameBuffer implements PConstants { pgl.validateFramebuffer(); - PGraphicsOpenGL.popFramebuffer(); + pg.popFramebuffer(); } @@ -302,8 +302,8 @@ public class FrameBuffer implements PConstants { colorBufferTex[i1] = tmp; } - PGraphicsOpenGL.pushFramebuffer(); - PGraphicsOpenGL.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, @@ -311,7 +311,7 @@ public class FrameBuffer implements PConstants { } pgl.validateFramebuffer(); - PGraphicsOpenGL.popFramebuffer(); + pg.popFramebuffer(); } @@ -422,8 +422,8 @@ public class FrameBuffer implements PConstants { protected void createColorBufferMultisample() { if (screenFb) return; - PGraphicsOpenGL.pushFramebuffer(); - PGraphicsOpenGL.setFramebuffer(this); + pg.pushFramebuffer(); + pg.setFramebuffer(this); glMultisample = PGraphicsOpenGL.createRenderBufferObject(context, pgl); pgl.bindRenderbuffer(PGL.RENDERBUFFER, glMultisample); @@ -432,7 +432,7 @@ public class FrameBuffer implements PConstants { pgl.framebufferRenderbuffer(PGL.FRAMEBUFFER, PGL.COLOR_ATTACHMENT0, PGL.RENDERBUFFER, glMultisample); - PGraphicsOpenGL.popFramebuffer(); + pg.popFramebuffer(); } @@ -443,8 +443,8 @@ public class FrameBuffer implements PConstants { throw new RuntimeException("PFramebuffer: size undefined."); } - PGraphicsOpenGL.pushFramebuffer(); - PGraphicsOpenGL.setFramebuffer(this); + pg.pushFramebuffer(); + pg.setFramebuffer(this); glDepthStencil = PGraphicsOpenGL.createRenderBufferObject(context, pgl); pgl.bindRenderbuffer(PGL.RENDERBUFFER, glDepthStencil); @@ -462,7 +462,7 @@ public class FrameBuffer implements PConstants { pgl.framebufferRenderbuffer(PGL.FRAMEBUFFER, PGL.STENCIL_ATTACHMENT, PGL.RENDERBUFFER, glDepthStencil); - PGraphicsOpenGL.popFramebuffer(); + pg.popFramebuffer(); } @@ -473,8 +473,8 @@ public class FrameBuffer implements PConstants { throw new RuntimeException("PFramebuffer: size undefined."); } - PGraphicsOpenGL.pushFramebuffer(); - PGraphicsOpenGL.setFramebuffer(this); + pg.pushFramebuffer(); + pg.setFramebuffer(this); glDepth = PGraphicsOpenGL.createRenderBufferObject(context, pgl); pgl.bindRenderbuffer(PGL.RENDERBUFFER, glDepth); @@ -498,7 +498,7 @@ public class FrameBuffer implements PConstants { pgl.framebufferRenderbuffer(PGL.FRAMEBUFFER, PGL.DEPTH_ATTACHMENT, PGL.RENDERBUFFER, glDepth); - PGraphicsOpenGL.popFramebuffer(); + pg.popFramebuffer(); } @@ -509,8 +509,8 @@ public class FrameBuffer implements PConstants { throw new RuntimeException("PFramebuffer: size undefined."); } - PGraphicsOpenGL.pushFramebuffer(); - PGraphicsOpenGL.setFramebuffer(this); + pg.pushFramebuffer(); + pg.setFramebuffer(this); glStencil = PGraphicsOpenGL.createRenderBufferObject(context, pgl); pgl.bindRenderbuffer(PGL.RENDERBUFFER, glStencil); @@ -533,7 +533,7 @@ public class FrameBuffer implements PConstants { pgl.framebufferRenderbuffer(PGL.FRAMEBUFFER, PGL.STENCIL_ATTACHMENT, PGL.RENDERBUFFER, glStencil); - PGraphicsOpenGL.popFramebuffer(); + pg.popFramebuffer(); } diff --git a/core/src/processing/opengl/PGL.java b/core/src/processing/opengl/PGL.java index bd4066021..629388ed8 100644 --- a/core/src/processing/opengl/PGL.java +++ b/core/src/processing/opengl/PGL.java @@ -57,6 +57,9 @@ public abstract class PGL { /** ID of the GL context associated to the surface **/ protected int glContext; + /** true if this is the GL interface for a primary surface PGraphics */ + protected boolean primaryPGL; + // ........................................................ // Parameters @@ -131,21 +134,21 @@ public abstract class PGL { // FBO layer - protected static boolean fboLayerRequested = false; - protected static boolean fboLayerCreated = false; - protected static boolean fboLayerInUse = false; - protected static boolean firstFrame = true; - protected static int reqNumSamples; - protected static int numSamples; - protected static IntBuffer glColorFbo; - protected static IntBuffer glMultiFbo; - protected static IntBuffer glColorBuf; - protected static IntBuffer glColorTex; - protected static IntBuffer glDepthStencil; - protected static IntBuffer glDepth; - protected static IntBuffer glStencil; - protected static int fboWidth, fboHeight; - protected static int backTex, frontTex; + protected boolean fboLayerRequested = false; + protected boolean fboLayerCreated = false; + protected boolean fboLayerInUse = false; + protected boolean firstFrame = true; + protected int reqNumSamples; + protected int numSamples; + protected IntBuffer glColorFbo; + protected IntBuffer glMultiFbo; + protected IntBuffer glColorBuf; + protected IntBuffer glColorTex; + protected IntBuffer glDepthStencil; + protected IntBuffer glDepth; + protected IntBuffer glStencil; + protected int fboWidth, fboHeight; + protected int backTex, frontTex; /** Flags used to handle the creation of a separate front texture */ protected boolean usingFrontTex = false; @@ -155,33 +158,33 @@ public abstract class PGL { // Texture rendering - protected static boolean loadedTex2DShader = false; - protected static int tex2DShaderProgram; - protected static int tex2DVertShader; - protected static int tex2DFragShader; - protected static int tex2DShaderContext; - protected static int texGeoVBO; - protected static int tex2DVertLoc; - protected static int tex2DTCoordLoc; - protected static int tex2DSamplerLoc; + protected boolean loadedTex2DShader = false; + protected int tex2DShaderProgram; + protected int tex2DVertShader; + protected int tex2DFragShader; + protected int tex2DShaderContext; + protected int texGeoVBO; + protected int tex2DVertLoc; + protected int tex2DTCoordLoc; + protected int tex2DSamplerLoc; - protected static boolean loadedTexRectShader = false; - protected static int texRectShaderProgram; - protected static int texRectVertShader; - protected static int texRectFragShader; - protected static int texRectShaderContext; - protected static int texRectVertLoc; - protected static int texRectTCoordLoc; - protected static int texRectSamplerLoc; + protected boolean loadedTexRectShader = false; + protected int texRectShaderProgram; + protected int texRectVertShader; + protected int texRectFragShader; + protected int texRectShaderContext; + protected int texRectVertLoc; + protected int texRectTCoordLoc; + protected int texRectSamplerLoc; - protected static float[] texCoords = { + protected float[] texCoords = { // X, Y, U, V -1.0f, -1.0f, 0.0f, 0.0f, +1.0f, -1.0f, 1.0f, 0.0f, -1.0f, +1.0f, 0.0f, 1.0f, +1.0f, +1.0f, 1.0f, 1.0f }; - protected static FloatBuffer texData; + protected FloatBuffer texData; protected static final String SHADER_PREPROCESSOR_DIRECTIVE = "#ifdef GL_ES\n" + @@ -218,12 +221,12 @@ public abstract class PGL { }; /** Which texturing targets are enabled */ - protected static boolean[] texturingTargets = { false, false }; + protected boolean[] texturingTargets = { false, false }; /** Used to keep track of which textures are bound to each target */ - protected static int maxTexUnits; - protected static int activeTexUnit = 0; - protected static int[][] boundTextures; + protected int maxTexUnits; + protected int activeTexUnit = 0; + protected int[][] boundTextures; // ........................................................ @@ -270,6 +273,13 @@ public abstract class PGL { protected static final String TEXUNIT_ERROR = "Number of texture units not supported by this hardware (or driver)" + WIKI; + protected static final String NONPRIMARY_ERROR = + "The renderer is trying to call a PGL function that can only be called on a primary PGL. " + + "This is most likely due to a bug in the renderer's code, please report it with an " + + "issue on Processing's github page https://github.com/processing/processing/issues?state=open " + + "if using any of the built-in OpenGL renderers. If you are using a contributed " + + "library, contact the library's developers."; + // ........................................................ // Constants @@ -334,6 +344,17 @@ public abstract class PGL { } + public void setPrimary(boolean primary) { + primaryPGL = primary; + } + + + protected void checkPrimary() { + if (!primaryPGL) { + throw new RuntimeException(NONPRIMARY_ERROR); + } + } + /** * Return the native canvas the OpenGL context associated to this PGL object * is rendering to (if any). @@ -354,6 +375,8 @@ public abstract class PGL { protected void deleteSurface() { + checkPrimary(); + if (threadIsCurrent() && fboLayerCreated) { deleteTextures(2, glColorTex); deleteFramebuffers(1, glColorFbo); @@ -371,11 +394,13 @@ public abstract class PGL { protected int getReadFramebuffer() { + checkPrimary(); return fboLayerInUse ? glColorFbo.get(0) : 0; } protected int getDrawFramebuffer() { + checkPrimary(); if (fboLayerInUse) return 1 < numSamples ? glMultiFbo.get(0) : glColorFbo.get(0); else return 0; @@ -383,26 +408,31 @@ public abstract class PGL { protected int getDefaultDrawBuffer() { + checkPrimary(); return fboLayerInUse ? COLOR_ATTACHMENT0 : FRONT; } protected int getDefaultReadBuffer() { + checkPrimary(); return fboLayerInUse ? COLOR_ATTACHMENT0 : FRONT; } protected boolean isFBOBacked() { + checkPrimary(); return fboLayerInUse; } protected void requestFBOLayer() { + checkPrimary(); fboLayerRequested = true; } protected boolean isMultisampled() { + checkPrimary(); return 1 < numSamples; } @@ -436,6 +466,7 @@ public abstract class PGL { protected Texture wrapBackTexture(Texture texture) { + checkPrimary(); if (texture == null) { texture = new Texture(pg); texture.init(pg.width, pg.height, @@ -453,6 +484,7 @@ public abstract class PGL { protected Texture wrapFrontTexture(Texture texture) { + checkPrimary(); if (texture == null) { texture = new Texture(pg); texture.init(pg.width, pg.height, @@ -469,6 +501,7 @@ public abstract class PGL { protected void bindFrontTexture() { + checkPrimary(); usingFrontTex = true; if (!texturingIsEnabled(TEXTURE_2D)) { enableTexturing(TEXTURE_2D); @@ -478,6 +511,7 @@ public abstract class PGL { protected void unbindFrontTexture() { + checkPrimary(); if (textureIsBound(TEXTURE_2D, glColorTex.get(frontTex))) { // We don't want to unbind another texture // that might be bound instead of this one. @@ -493,6 +527,7 @@ public abstract class PGL { protected void syncBackTexture() { + checkPrimary(); if (usingFrontTex) needSepFrontTex = true; if (1 < numSamples) { bindFramebuffer(READ_FRAMEBUFFER, glMultiFbo.get(0)); @@ -504,18 +539,122 @@ public abstract class PGL { } - protected int qualityToSamples(int quality) { - if (quality <= 1) { - return 1; + /////////////////////////////////////////////////////////// + + // Frame rendering + + + protected void beginDraw(boolean clear0) { + checkPrimary(); + if (needFBOLayer(clear0)) { + if (!fboLayerCreated) createFBOLayer(); + + bindFramebuffer(FRAMEBUFFER, glColorFbo.get(0)); + framebufferTexture2D(FRAMEBUFFER, COLOR_ATTACHMENT0, + TEXTURE_2D, glColorTex.get(backTex), 0); + + if (1 < numSamples) { + bindFramebuffer(FRAMEBUFFER, glMultiFbo.get(0)); + } + + if (firstFrame) { + // No need to draw back color buffer because we are in the first frame. + int argb = pg.backgroundColor; + float a = ((argb >> 24) & 0xff) / 255.0f; + float r = ((argb >> 16) & 0xff) / 255.0f; + float g = ((argb >> 8) & 0xff) / 255.0f; + float b = ((argb) & 0xff) / 255.0f; + clearColor(r, g, b, a); + clear(COLOR_BUFFER_BIT); + } else if (!clear0) { + // Render previous back texture (now is the front) as background, + // because no background() is being used ("incremental drawing") + drawTexture(TEXTURE_2D, glColorTex.get(frontTex), + fboWidth, fboHeight, pg.width, pg.height, + 0, 0, pg.width, pg.height, + 0, 0, pg.width, pg.height); + } + + fboLayerInUse = true; } else { - // Number of samples is always an even number: - int n = 2 * (quality / 2); - return n; + fboLayerInUse = false; + } + + if (firstFrame) { + firstFrame = false; + } + + if (!USE_FBOLAYER_BY_DEFAULT) { + // The result of this assignment is the following: if the user requested + // at some point the use of the FBO layer, but subsequently didn't + // request it again, then the rendering won't render to the FBO layer if + // not needed by the condif, since it is slower than simple onscreen + // rendering. + fboLayerRequested = false; } } - protected void createFBOLayer() { + protected void endDraw(boolean clear0) { + checkPrimary(); + if (fboLayerInUse) { + syncBackTexture(); + + // Draw the contents of the back texture to the screen framebuffer. + bindFramebuffer(FRAMEBUFFER, 0); + + clearDepth(1); + clearColor(0, 0, 0, 0); + clear(COLOR_BUFFER_BIT | DEPTH_BUFFER_BIT); + + // Render current back texture to screen, without blending. + disable(BLEND); + drawTexture(TEXTURE_2D, glColorTex.get(backTex), + fboWidth, fboHeight, pg.width, pg.height, + 0, 0, pg.width, pg.height, + 0, 0, pg.width, pg.height); + + // Swapping front and back textures. + int temp = frontTex; + frontTex = backTex; + backTex = temp; + } + } + + + protected abstract void getGL(PGL pgl); + + + protected abstract boolean canDraw(); + + + protected abstract void requestFocus(); + + + protected abstract void requestDraw(); + + + protected abstract void swapBuffers(); + + + protected boolean threadIsCurrent() { + return Thread.currentThread() == glThread; + } + + + protected void beginGL() { } + + + protected void endGL() { } + + + private boolean needFBOLayer(boolean clear0) { + return !clear0 || fboLayerRequested || 1 < numSamples; + } + + + private void createFBOLayer() { + System.out.println("Creating FBO layer"); String ext = getString(EXTENSIONS); if (-1 < ext.indexOf("texture_non_power_of_two")) { fboWidth = pg.width; @@ -656,118 +795,6 @@ public abstract class PGL { } - /////////////////////////////////////////////////////////// - - // Frame rendering - - - protected void beginDraw(boolean clear0) { - if (needFBOLayer(clear0)) { - if (!fboLayerCreated) createFBOLayer(); - - bindFramebuffer(FRAMEBUFFER, glColorFbo.get(0)); - framebufferTexture2D(FRAMEBUFFER, COLOR_ATTACHMENT0, - TEXTURE_2D, glColorTex.get(backTex), 0); - - if (1 < numSamples) { - bindFramebuffer(FRAMEBUFFER, glMultiFbo.get(0)); - } - - if (firstFrame) { - // No need to draw back color buffer because we are in the first frame. - int argb = pg.backgroundColor; - float a = ((argb >> 24) & 0xff) / 255.0f; - float r = ((argb >> 16) & 0xff) / 255.0f; - float g = ((argb >> 8) & 0xff) / 255.0f; - float b = ((argb) & 0xff) / 255.0f; - clearColor(r, g, b, a); - clear(COLOR_BUFFER_BIT); - } else if (!clear0) { - // Render previous back texture (now is the front) as background, - // because no background() is being used ("incremental drawing") - drawTexture(TEXTURE_2D, glColorTex.get(frontTex), - fboWidth, fboHeight, pg.width, pg.height, - 0, 0, pg.width, pg.height, - 0, 0, pg.width, pg.height); - } - - fboLayerInUse = true; - } else { - fboLayerInUse = false; - } - - if (firstFrame) { - firstFrame = false; - } - - if (!USE_FBOLAYER_BY_DEFAULT) { - // The result of this assignment is the following: if the user requested - // at some point the use of the FBO layer, but subsequently didn't - // request it again, then the rendering won't render to the FBO layer if - // not needed by the condif, since it is slower than simple onscreen - // rendering. - fboLayerRequested = false; - } - } - - - protected void endDraw(boolean clear0) { - if (fboLayerInUse) { - syncBackTexture(); - - // Draw the contents of the back texture to the screen framebuffer. - bindFramebuffer(FRAMEBUFFER, 0); - - clearDepth(1); - clearColor(0, 0, 0, 0); - clear(COLOR_BUFFER_BIT | DEPTH_BUFFER_BIT); - - // Render current back texture to screen, without blending. - disable(BLEND); - drawTexture(TEXTURE_2D, glColorTex.get(backTex), - fboWidth, fboHeight, pg.width, pg.height, - 0, 0, pg.width, pg.height, - 0, 0, pg.width, pg.height); - - // Swapping front and back textures. - int temp = frontTex; - frontTex = backTex; - backTex = temp; - } - } - - - protected abstract void getGL(PGL pgl); - - - protected abstract boolean canDraw(); - - - protected abstract void requestFocus(); - - - protected abstract void requestDraw(); - - - protected abstract void swapBuffers(); - - - protected boolean threadIsCurrent() { - return Thread.currentThread() == glThread; - } - - - protected boolean needFBOLayer(boolean clear0) { - return !clear0 || fboLayerRequested || 1 < numSamples; - } - - - protected void beginGL() { } - - - protected void endGL() { } - - /////////////////////////////////////////////////////////// // Context interface @@ -906,8 +933,8 @@ public abstract class PGL { protected void initTex2DShader() { - if (!loadedTex2DShader/* || tex2DShaderContext != glContext*/) { - System.out.println("initializing texture shader"); + if (!loadedTex2DShader || tex2DShaderContext != glContext) { + System.out.println("Initializing PGL texture shader"); String vertSource = PApplet.join(texVertShaderSource, "\n"); String fragSource = PApplet.join(tex2DFragShaderSource, "\n"); tex2DVertShader = createShader(VERTEX_SHADER, vertSource); @@ -927,6 +954,8 @@ public abstract class PGL { texGeoVBO = intBuffer.get(0); bindBuffer(ARRAY_BUFFER, texGeoVBO); bufferData(ARRAY_BUFFER, 16 * SIZEOF_FLOAT, null, STATIC_DRAW); + + System.out.println("Done!"); } if (texData == null) { @@ -1436,6 +1465,17 @@ public abstract class PGL { } + protected static int qualityToSamples(int quality) { + if (quality <= 1) { + return 1; + } else { + // Number of samples is always an even number: + int n = 2 * (quality / 2); + return n; + } + } + + protected String[] loadVertexShader(String filename) { return pg.parent.loadStrings(filename); } @@ -1486,18 +1526,45 @@ public abstract class PGL { } - protected String[] convertFragmentSource(String[] fragSrc0, - int version0, int version1) { + protected static String[] convertFragmentSource(String[] fragSrc0, + int version0, int version1) { + if (version0 == 120 && version1 == 150) { + String[] fragSrc = new String[fragSrc0.length + 2]; + fragSrc[0] = "#version 150"; + fragSrc[1] = "out vec4 fragColor;"; + for (int i = 0; i < fragSrc0.length; i++) { + String line = fragSrc0[i]; + line = line.replace("varying", "in"); + line = line.replace("attribute", "in"); + line = line.replace("gl_FragColor", "fragColor"); + line = line.replace("texture", "texMap"); + line = line.replace("texMap2D(", "texture("); + line = line.replace("texMap2DRect(", "texture("); + fragSrc[i + 2] = line; + } + return fragSrc; + } return fragSrc0; } - protected String[] convertVertexSource(String[] vertSrc0, - int version0, int version1) { + + protected static String[] convertVertexSource(String[] vertSrc0, + int version0, int version1) { + if (version0 == 120 && version1 == 150) { + String[] vertSrc = new String[vertSrc0.length + 1]; + vertSrc[0] = "#version 150"; + for (int i = 0; i < vertSrc0.length; i++) { + String line = vertSrc0[i]; + line = line.replace("attribute", "in"); + line = line.replace("varying", "out"); + vertSrc[i + 1] = line; + } + return vertSrc; + } return vertSrc0; } - protected int createShader(int shaderType, String source) { int shader = createShader(shaderType); if (shader != 0) { diff --git a/core/src/processing/opengl/PGraphicsOpenGL.java b/core/src/processing/opengl/PGraphicsOpenGL.java index 732e2a1f1..b86ece976 100644 --- a/core/src/processing/opengl/PGraphicsOpenGL.java +++ b/core/src/processing/opengl/PGraphicsOpenGL.java @@ -40,66 +40,7 @@ public class PGraphicsOpenGL extends PGraphics { protected PGraphicsOpenGL currentPG; /** Font cache for texture objects. */ - protected WeakHashMap fontMap = - new WeakHashMap(); - - // ........................................................ - - static final String OPENGL_THREAD_ERROR = - "Cannot run the OpenGL renderer outside the main thread, change your code" + - "\nso the drawing calls are all inside the main thread, " + - "\nor use the default renderer instead."; - static final String BLEND_DRIVER_ERROR = - "blendMode(%1$s) is not supported by this hardware (or driver)"; - static final String BLEND_RENDERER_ERROR = - "blendMode(%1$s) is not supported by this renderer"; - static final String ALREADY_BEGAN_CONTOUR_ERROR = - "Already called beginContour()"; - static final String NO_BEGIN_CONTOUR_ERROR = - "Need to call beginContour() first"; - static final String UNSUPPORTED_SMOOTH_LEVEL_ERROR = - "Smooth level %1$s is not available. Using %2$s instead"; - static final String UNSUPPORTED_SMOOTH_ERROR = - "Smooth is not supported by this hardware (or driver)"; - static final String TOO_MANY_SMOOTH_CALLS_ERROR = - "The smooth/noSmooth functions are being called too often.\n" + - "This results in screen flickering, so they will be disabled\n" + - "for the rest of the sketch's execution"; - static final String UNSUPPORTED_SHAPE_FORMAT_ERROR = - "Unsupported shape format"; - static final String MISSING_UV_TEXCOORDS_ERROR = - "No uv texture coordinates supplied with vertex() call"; - static final String INVALID_FILTER_SHADER_ERROR = - "Your shader cannot be used as a filter because is of type POINT or LINES"; - static final String INCONSISTENT_SHADER_TYPES = - "The vertex and fragment shaders have different types"; - static final String WRONG_SHADER_TYPE_ERROR = - "shader() called with a wrong shader"; - static final String SHADER_NEED_LIGHT_ATTRIBS = - "The provided shader needs light attributes (ambient, diffuse, etc.), but " + - "the current scene is unlit, so the default shader will be used instead"; - static final String MISSING_FRAGMENT_SHADER = - "The fragment shader is missing, cannot create shader object"; - static final String MISSING_VERTEX_SHADER = - "The vertex shader is missing, cannot create shader object"; - static final String UNKNOWN_SHADER_KIND_ERROR = - "Unknown shader kind"; - static final String NO_TEXLIGHT_SHADER_ERROR = - "Your shader needs to be of TEXLIGHT type " + - "to render this geometry properly, using default shader instead."; - static final String NO_LIGHT_SHADER_ERROR = - "Your shader needs to be of LIGHT type " + - "to render this geometry properly, using default shader instead."; - static final String NO_TEXTURE_SHADER_ERROR = - "Your shader needs to be of TEXTURE type " + - "to render this geometry properly, using default shader instead."; - static final String NO_COLOR_SHADER_ERROR = - "Your shader needs to be of COLOR type " + - "to render this geometry properly, using default shader instead."; - static final String TOO_LONG_STROKE_PATH_ERROR = - "Stroke path is too long, some bevel triangles won't be added"; - static final String TESSELLATION_ERROR = - "Tessellation Error: %1$s"; + protected WeakHashMap fontMap; // ........................................................ @@ -153,8 +94,8 @@ public class PGraphicsOpenGL extends PGraphics { protected boolean pointBuffersCreated = false; protected int pointBuffersContext; - protected static final int INIT_VERTEX_BUFFER_SIZE = 256; - protected static final int INIT_INDEX_BUFFER_SIZE = 512; + static protected final int INIT_VERTEX_BUFFER_SIZE = 256; + static protected final int INIT_INDEX_BUFFER_SIZE = 512; // ........................................................ @@ -248,8 +189,8 @@ public class PGraphicsOpenGL extends PGraphics { protected InGeometry inGeo; protected TessGeometry tessGeo; - static protected Tessellator tessellator; protected TexCache texCache; + static protected Tessellator tessellator; // ........................................................ @@ -397,11 +338,11 @@ public class PGraphicsOpenGL extends PGraphics { static protected final int FB_STACK_DEPTH = 16; - static protected int fbStackDepth; - static protected FrameBuffer[] fbStack = new FrameBuffer[FB_STACK_DEPTH]; - static protected FrameBuffer drawFramebuffer; - static protected FrameBuffer readFramebuffer; - static protected FrameBuffer currentFramebuffer; + protected int fbStackDepth; + protected FrameBuffer[] fbStack; + protected FrameBuffer drawFramebuffer; + protected FrameBuffer readFramebuffer; + protected FrameBuffer currentFramebuffer; // ....................................................... @@ -508,6 +449,67 @@ public class PGraphicsOpenGL extends PGraphics { static protected IntBuffer intBuffer; static protected FloatBuffer floatBuffer; + // ........................................................ + + // Error strings: + + static final String OPENGL_THREAD_ERROR = + "Cannot run the OpenGL renderer outside the main thread, change your code" + + "\nso the drawing calls are all inside the main thread, " + + "\nor use the default renderer instead."; + static final String BLEND_DRIVER_ERROR = + "blendMode(%1$s) is not supported by this hardware (or driver)"; + static final String BLEND_RENDERER_ERROR = + "blendMode(%1$s) is not supported by this renderer"; + static final String ALREADY_BEGAN_CONTOUR_ERROR = + "Already called beginContour()"; + static final String NO_BEGIN_CONTOUR_ERROR = + "Need to call beginContour() first"; + static final String UNSUPPORTED_SMOOTH_LEVEL_ERROR = + "Smooth level %1$s is not available. Using %2$s instead"; + static final String UNSUPPORTED_SMOOTH_ERROR = + "Smooth is not supported by this hardware (or driver)"; + static final String TOO_MANY_SMOOTH_CALLS_ERROR = + "The smooth/noSmooth functions are being called too often.\n" + + "This results in screen flickering, so they will be disabled\n" + + "for the rest of the sketch's execution"; + static final String UNSUPPORTED_SHAPE_FORMAT_ERROR = + "Unsupported shape format"; + static final String MISSING_UV_TEXCOORDS_ERROR = + "No uv texture coordinates supplied with vertex() call"; + static final String INVALID_FILTER_SHADER_ERROR = + "Your shader cannot be used as a filter because is of type POINT or LINES"; + static final String INCONSISTENT_SHADER_TYPES = + "The vertex and fragment shaders have different types"; + static final String WRONG_SHADER_TYPE_ERROR = + "shader() called with a wrong shader"; + static final String SHADER_NEED_LIGHT_ATTRIBS = + "The provided shader needs light attributes (ambient, diffuse, etc.), but " + + "the current scene is unlit, so the default shader will be used instead"; + static final String MISSING_FRAGMENT_SHADER = + "The fragment shader is missing, cannot create shader object"; + static final String MISSING_VERTEX_SHADER = + "The vertex shader is missing, cannot create shader object"; + static final String UNKNOWN_SHADER_KIND_ERROR = + "Unknown shader kind"; + static final String NO_TEXLIGHT_SHADER_ERROR = + "Your shader needs to be of TEXLIGHT type " + + "to render this geometry properly, using default shader instead."; + static final String NO_LIGHT_SHADER_ERROR = + "Your shader needs to be of LIGHT type " + + "to render this geometry properly, using default shader instead."; + static final String NO_TEXTURE_SHADER_ERROR = + "Your shader needs to be of TEXTURE type " + + "to render this geometry properly, using default shader instead."; + static final String NO_COLOR_SHADER_ERROR = + "Your shader needs to be of COLOR type " + + "to render this geometry properly, using default shader instead."; + static final String TOO_LONG_STROKE_PATH_ERROR = + "Stroke path is too long, some bevel triangles won't be added"; + static final String TESSELLATION_ERROR = + "Tessellation Error: %1$s"; + + ////////////////////////////////////////////////////////////// // INIT/ALLOCATE/FINISH @@ -538,7 +540,12 @@ public class PGraphicsOpenGL extends PGraphics { @Override public void setPrimary(boolean primary) { super.setPrimary(primary); + pgl.setPrimary(primary); format = ARGB; + if (primary) { + fbStack = new FrameBuffer[FB_STACK_DEPTH]; + fontMap = new WeakHashMap(); + } } @@ -643,12 +650,6 @@ public class PGraphicsOpenGL extends PGraphics { if (primarySurface) { pgl.deleteSurface(); - - // This next line is critical to release many static allocations. - // This is important in the context of, say, a unit test suite, which - // runs more than one OpenGL sketch within the same classloader - // (as in the case of processing.py). Please don't remove it! - //pgl = null; } } @@ -1209,37 +1210,45 @@ public class PGraphicsOpenGL extends PGraphics { // FRAMEBUFFERS - protected static void pushFramebuffer() { - if (fbStackDepth == FB_STACK_DEPTH) { + protected void pushFramebuffer() { + PGraphicsOpenGL ppg = getPrimaryPG(); + if (ppg.fbStackDepth == FB_STACK_DEPTH) { throw new RuntimeException("Too many pushFramebuffer calls"); } - fbStack[fbStackDepth] = currentFramebuffer; - fbStackDepth++; + ppg.fbStack[ppg.fbStackDepth] = ppg.currentFramebuffer; + ppg.fbStackDepth++; } - protected static void setFramebuffer(FrameBuffer fbo) { - if (currentFramebuffer != fbo) { - currentFramebuffer = fbo; - currentFramebuffer.bind(); + protected void setFramebuffer(FrameBuffer fbo) { + PGraphicsOpenGL ppg = getPrimaryPG(); + if (ppg.currentFramebuffer != fbo) { + ppg.currentFramebuffer = fbo; + ppg.currentFramebuffer.bind(); } } - protected static void popFramebuffer() { - if (fbStackDepth == 0) { + protected void popFramebuffer() { + PGraphicsOpenGL ppg = getPrimaryPG(); + if (ppg.fbStackDepth == 0) { throw new RuntimeException("popFramebuffer call is unbalanced."); } - fbStackDepth--; - FrameBuffer fbo = fbStack[fbStackDepth]; - if (currentFramebuffer != fbo) { - currentFramebuffer.finish(); - currentFramebuffer = fbo; - currentFramebuffer.bind(); + ppg.fbStackDepth--; + FrameBuffer fbo = ppg.fbStack[ppg.fbStackDepth]; + if (ppg.currentFramebuffer != fbo) { + ppg.currentFramebuffer.finish(); + ppg.currentFramebuffer = fbo; + ppg.currentFramebuffer.bind(); } } + protected FrameBuffer getCurrentFB() { + return getPrimaryPG().currentFramebuffer; + } + + ////////////////////////////////////////////////////////////// // FRAME RENDERING @@ -1797,8 +1806,8 @@ public class PGraphicsOpenGL extends PGraphics { pgl.depthMask(true); } - currentFramebuffer.bind(); - pgl.drawBuffer(currentFramebuffer.getDefaultDrawBuffer()); + getCurrentFB().bind(); + pgl.drawBuffer(getCurrentFB().getDefaultDrawBuffer()); } public void beginReadPixels() { @@ -1831,7 +1840,7 @@ public class PGraphicsOpenGL extends PGraphics { if (op == OP_READ) { if (offscreenMultisample) { // Making sure the offscreen FBO is up-to-date - multisampleFramebuffer.copy(offscreenFramebuffer, currentFramebuffer); + multisampleFramebuffer.copy(offscreenFramebuffer, getCurrentFB()); } // We always read the screen pixels from the color FBO. pixfb = offscreenFramebuffer; @@ -1844,7 +1853,7 @@ public class PGraphicsOpenGL extends PGraphics { } // Set the framebuffer where the pixel operation shall be carried out. - if (pixfb != currentFramebuffer) { + if (pixfb != getCurrentFB()) { pushFramebuffer(); setFramebuffer(pixfb); pixOpChangedFB = true; @@ -1852,9 +1861,9 @@ public class PGraphicsOpenGL extends PGraphics { // We read from/write to the draw buffer. if (op == OP_READ) { - pgl.readBuffer(currentFramebuffer.getDefaultDrawBuffer()); + pgl.readBuffer(getCurrentFB().getDefaultDrawBuffer()); } else if (op == OP_WRITE) { - pgl.drawBuffer(currentFramebuffer.getDefaultDrawBuffer()); + pgl.drawBuffer(getCurrentFB().getDefaultDrawBuffer()); } pixelsOp = op; @@ -1869,8 +1878,8 @@ public class PGraphicsOpenGL extends PGraphics { } // Restoring default read/draw buffer configuration. - pgl.readBuffer(currentFramebuffer.getDefaultReadBuffer()); - pgl.drawBuffer(currentFramebuffer.getDefaultDrawBuffer()); + pgl.readBuffer(getCurrentFB().getDefaultReadBuffer()); + pgl.drawBuffer(getCurrentFB().getDefaultDrawBuffer()); pixelsOp = OP_NONE; } @@ -5609,7 +5618,7 @@ public class PGraphicsOpenGL extends PGraphics { } else if (offscreenMultisample) { // We need to copy the contents of the multisampled buffer to the color // buffer, so the later is up-to-date with the last drawing. - multisampleFramebuffer.copy(offscreenFramebuffer, currentFramebuffer); + multisampleFramebuffer.copy(offscreenFramebuffer, getCurrentFB()); } if (needEndDraw) { @@ -6353,7 +6362,7 @@ public class PGraphicsOpenGL extends PGraphics { // pgl.colorMask(true, true, true, true); if (offscreenMultisample) { - multisampleFramebuffer.copy(offscreenFramebuffer, currentFramebuffer); + multisampleFramebuffer.copy(offscreenFramebuffer, getCurrentFB()); } popFramebuffer(); diff --git a/core/src/processing/opengl/PJOGL.java b/core/src/processing/opengl/PJOGL.java index 6979d4df4..4fbd88e34 100644 --- a/core/src/processing/opengl/PJOGL.java +++ b/core/src/processing/opengl/PJOGL.java @@ -79,6 +79,41 @@ public class PJOGL extends PGL { /** Selected GL profile */ public static GLProfile profile; + static { + System.out.println("Animation thread: " + Thread.currentThread()); + if (PROFILE == 2) { + try { + profile = GLProfile.getGL2ES1(); + } catch (GLException ex) { + profile = GLProfile.getMaxFixedFunc(true); + } + } else if (PROFILE == 3) { + try { + profile = GLProfile.getGL2GL3(); + } catch (GLException ex) { + profile = GLProfile.getMaxProgrammable(true); + } + if (!profile.isGL3()) { + PGraphics.showWarning("Requested profile GL3 but is not available, got: " + profile); + } + } else if (PROFILE == 4) { + try { + profile = GLProfile.getGL4ES3(); + } catch (GLException ex) { + profile = GLProfile.getMaxProgrammable(true); + } + if (!profile.isGL4()) { + PGraphics.showWarning("Requested profile GL4 but is not available, got: " + profile); + } + } else throw new RuntimeException(UNSUPPORTED_GLPROF_ERROR); + System.out.println("Done, got this profile: " + profile); + + if (2 < PROFILE) { + texVertShaderSource = convertVertexSource(texVertShaderSource, 120, 150); + tex2DFragShaderSource = convertFragmentSource(tex2DFragShaderSource, 120, 150); + texRectFragShaderSource = convertFragmentSource(texRectFragShaderSource, 120, 150); + } + } // ........................................................ @@ -173,13 +208,13 @@ public class PJOGL extends PGL { // JOGL's FBO-layer /** Back (== draw, current frame) buffer */ - protected static FBObject backFBO; + protected FBObject backFBO; /** Sink buffer, used in the multisampled case */ - protected static FBObject sinkFBO; + protected FBObject sinkFBO; /** Front (== read, previous frame) buffer */ - protected static FBObject frontFBO; - protected static FBObject.TextureAttachment backTexAttach; - protected static FBObject.TextureAttachment frontTexAttach; + protected FBObject frontFBO; + protected FBObject.TextureAttachment backTexAttach; + protected FBObject.TextureAttachment frontTexAttach; protected boolean changedFrontTex = false; protected boolean changedBackTex = false; @@ -221,6 +256,7 @@ public class PJOGL extends PGL { @Override protected void setFps(float fps) { + checkPrimary(); if (!setFps || targetFps != fps) { if (60 < fps) { // Disables v-sync @@ -238,39 +274,11 @@ public class PJOGL extends PGL { @Override protected void initSurface(int antialias) { - if (profile == null) { - if (PROFILE == 2) { - try { - profile = GLProfile.getGL2ES1(); - } catch (GLException ex) { - profile = GLProfile.getMaxFixedFunc(true); - } - } else if (PROFILE == 3) { - try { - profile = GLProfile.getGL2GL3(); - } catch (GLException ex) { - profile = GLProfile.getMaxProgrammable(true); - } - if (!profile.isGL3()) { - PGraphics.showWarning("Requested profile GL3 but is not available, got: " + profile); - } - } else if (PROFILE == 4) { - try { - profile = GLProfile.getGL4ES3(); - } catch (GLException ex) { - profile = GLProfile.getMaxProgrammable(true); - } - if (!profile.isGL4()) { - PGraphics.showWarning("Requested profile GL4 but is not available, got: " + profile); - } - } else throw new RuntimeException(UNSUPPORTED_GLPROF_ERROR); + checkPrimary(); - if (2 < PROFILE) { - texVertShaderSource = convertVertexSource(texVertShaderSource, 120, 150); - tex2DFragShaderSource = convertFragmentSource(tex2DFragShaderSource, 120, 150); - texRectFragShaderSource = convertFragmentSource(texRectFragShaderSource, 120, 150); - } - } else { + System.out.println("initializing surface"); + + if (canvasAWT != null && canvasNEWT != null) { // Restarting... if (canvasAWT != null) { canvasAWT.removeGLEventListener(listener); @@ -355,11 +363,14 @@ public class PJOGL extends PGL { fboLayerInUse = false; firstFrame = true; setFps = false; + + System.out.println("done"); } @Override protected void reinitSurface() { + checkPrimary(); sinkFBO = backFBO = frontFBO = null; fboLayerCreated = false; fboLayerInUse = false; @@ -409,11 +420,13 @@ public class PJOGL extends PGL { window.removeGLEventListener(listener); } GLProfile.shutdown(); + System.out.println("bye bye"); } @Override protected int getReadFramebuffer() { + checkPrimary(); if (fboLayerInUse) { return glColorFbo.get(0); } else if (capabilities.isFBO()) { @@ -426,6 +439,7 @@ public class PJOGL extends PGL { @Override protected int getDrawFramebuffer() { + checkPrimary(); if (fboLayerInUse) { if (1 < numSamples) { return glMultiFbo.get(0); @@ -442,6 +456,7 @@ public class PJOGL extends PGL { @Override protected int getDefaultDrawBuffer() { + checkPrimary(); if (fboLayerInUse) { return COLOR_ATTACHMENT0; } else if (capabilities.isFBO()) { @@ -456,6 +471,7 @@ public class PJOGL extends PGL { @Override protected int getDefaultReadBuffer() { + checkPrimary(); if (fboLayerInUse) { return COLOR_ATTACHMENT0; } else if (capabilities.isFBO()) { @@ -470,6 +486,7 @@ public class PJOGL extends PGL { @Override protected boolean isFBOBacked() { + checkPrimary(); return super.isFBOBacked() || capabilities.isFBO(); } @@ -488,6 +505,7 @@ public class PJOGL extends PGL { @Override protected Texture wrapBackTexture(Texture texture) { + checkPrimary(); if (texture == null || changedBackTex) { if (USE_JOGL_FBOLAYER) { texture = new Texture(pg); @@ -515,6 +533,7 @@ public class PJOGL extends PGL { @Override protected Texture wrapFrontTexture(Texture texture) { + checkPrimary(); if (texture == null || changedFrontTex) { if (USE_JOGL_FBOLAYER) { texture = new Texture(pg); @@ -542,6 +561,7 @@ public class PJOGL extends PGL { @Override protected void bindFrontTexture() { if (USE_JOGL_FBOLAYER) { + checkPrimary(); usingFrontTex = true; if (!texturingIsEnabled(TEXTURE_2D)) { enableTexturing(TEXTURE_2D); @@ -554,6 +574,7 @@ public class PJOGL extends PGL { @Override protected void unbindFrontTexture() { if (USE_JOGL_FBOLAYER) { + checkPrimary(); if (textureIsBound(TEXTURE_2D, frontTexAttach.getName())) { // We don't want to unbind another texture // that might be bound instead of this one. @@ -572,6 +593,7 @@ public class PJOGL extends PGL { @Override protected void syncBackTexture() { if (USE_JOGL_FBOLAYER) { + checkPrimary(); if (usingFrontTex) needSepFrontTex = true; if (1 < numSamples) { backFBO.syncSamplingSink(gl); @@ -628,7 +650,6 @@ public class PJOGL extends PGL { } - @Override protected boolean canDraw() { return pg.initialized && pg.parent.isDisplayable(); @@ -641,6 +662,7 @@ public class PJOGL extends PGL { @Override protected void requestDraw() { + checkPrimary(); boolean canDraw = pg.parent.canDraw(); if (pg.initialized && (canDraw || prevCanDraw)) { try { @@ -673,6 +695,7 @@ public class PJOGL extends PGL { @Override protected void swapBuffers() { + checkPrimary(); if (WINDOW_TOOLKIT == AWT) { canvasAWT.swapBuffers(); } else if (WINDOW_TOOLKIT == NEWT) { @@ -1129,41 +1152,6 @@ public class PJOGL extends PGL { } - @Override - protected String[] convertFragmentSource(String[] fragSrc0, - int version0, int version1) { - String[] fragSrc = new String[fragSrc0.length + 2]; - fragSrc[0] = "#version 150"; - fragSrc[1] = "out vec4 fragColor;"; - for (int i = 0; i < fragSrc0.length; i++) { - String line = fragSrc0[i]; - line = line.replace("varying", "in"); - line = line.replace("attribute", "in"); - line = line.replace("gl_FragColor", "fragColor"); - line = line.replace("texture", "texMap"); - line = line.replace("texMap2D(", "texture("); - line = line.replace("texMap2DRect(", "texture("); - fragSrc[i + 2] = line; - } - return fragSrc; - } - - - @Override - protected String[] convertVertexSource(String[] vertSrc0, - int version0, int version1) { - String[] vertSrc = new String[vertSrc0.length + 1]; - vertSrc[0] = "#version 150"; - for (int i = 0; i < vertSrc0.length; i++) { - String line = vertSrc0[i]; - line = line.replace("attribute", "in"); - line = line.replace("varying", "out"); - vertSrc[i + 1] = line; - } - return vertSrc; - } - - /////////////////////////////////////////////////////////// // Tessellator diff --git a/core/src/processing/opengl/Texture.java b/core/src/processing/opengl/Texture.java index cd9ab6052..990d584e3 100644 --- a/core/src/processing/opengl/Texture.java +++ b/core/src/processing/opengl/Texture.java @@ -521,10 +521,10 @@ public class Texture implements PConstants { // reading the pixels from the current draw buffer (which is the color // buffer of the FBO). tempFbo.setColorBuffer(this); - PGraphicsOpenGL.pushFramebuffer(); - PGraphicsOpenGL.setFramebuffer(tempFbo); + pg.pushFramebuffer(); + pg.setFramebuffer(tempFbo); tempFbo.readPixels(); - PGraphicsOpenGL.popFramebuffer(); + pg.popFramebuffer(); tempFbo.getPixels(pixels); convertToARGB(pixels); @@ -1248,8 +1248,8 @@ public class Texture implements PConstants { tempFbo.disableDepthTest(); // FBO copy: - PGraphicsOpenGL.pushFramebuffer(); - PGraphicsOpenGL.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); @@ -1269,7 +1269,7 @@ public class Texture implements PConstants { tex.glWidth, tex.glHeight, tempFbo.width, tempFbo.height, x, y, w, h, x, y, w, h); } - PGraphicsOpenGL.popFramebuffer(); + pg.popFramebuffer(); updateTexels(x, y, w, h); } @@ -1288,8 +1288,8 @@ public class Texture implements PConstants { tempFbo.disableDepthTest(); // FBO copy: - PGraphicsOpenGL.pushFramebuffer(); - PGraphicsOpenGL.setFramebuffer(tempFbo); + pg.pushFramebuffer(); + pg.setFramebuffer(tempFbo); if (scale) { // Rendering tex into "this", and scaling the source rectangle // to cover the entire destination region. @@ -1305,7 +1305,7 @@ public class Texture implements PConstants { texWidth, texHeight, tempFbo.width, tempFbo.height, x, y, w, h, x, y, w, h); } - PGraphicsOpenGL.popFramebuffer(); + pg.popFramebuffer(); updateTexels(x, y, w, h); } From 5d713058b88196c00ef88f5100ab89c39dcea072 Mon Sep 17 00:00:00 2001 From: codeanticode Date: Mon, 20 Jan 2014 17:10:18 -0500 Subject: [PATCH 17/51] texture shaders in PGL are initialized only in the primary interface. --- core/src/processing/opengl/PGL.java | 131 ++++++++++++++------------ core/src/processing/opengl/PJOGL.java | 6 -- 2 files changed, 71 insertions(+), 66 deletions(-) diff --git a/core/src/processing/opengl/PGL.java b/core/src/processing/opengl/PGL.java index 629388ed8..dde84424c 100644 --- a/core/src/processing/opengl/PGL.java +++ b/core/src/processing/opengl/PGL.java @@ -163,7 +163,6 @@ public abstract class PGL { protected int tex2DVertShader; protected int tex2DFragShader; protected int tex2DShaderContext; - protected int texGeoVBO; protected int tex2DVertLoc; protected int tex2DTCoordLoc; protected int tex2DSamplerLoc; @@ -177,6 +176,8 @@ public abstract class PGL { protected int texRectTCoordLoc; protected int texRectSamplerLoc; + protected int texGeoVBO; + protected float[] texCoords = { // X, Y, U, V -1.0f, -1.0f, 0.0f, 0.0f, @@ -932,44 +933,48 @@ public abstract class PGL { } - protected void initTex2DShader() { - if (!loadedTex2DShader || tex2DShaderContext != glContext) { + protected PGL initTex2DShader() { + PGL ppgl = primaryPGL ? this : pg.getPrimaryPGL(); + + if (!ppgl.loadedTex2DShader || ppgl.tex2DShaderContext != ppgl.glContext) { System.out.println("Initializing PGL texture shader"); String vertSource = PApplet.join(texVertShaderSource, "\n"); String fragSource = PApplet.join(tex2DFragShaderSource, "\n"); - tex2DVertShader = createShader(VERTEX_SHADER, vertSource); - tex2DFragShader = createShader(FRAGMENT_SHADER, fragSource); - if (0 < tex2DVertShader && 0 < tex2DFragShader) { - tex2DShaderProgram = createProgram(tex2DVertShader, tex2DFragShader); + ppgl.tex2DVertShader = createShader(VERTEX_SHADER, vertSource); + ppgl.tex2DFragShader = createShader(FRAGMENT_SHADER, fragSource); + if (0 < ppgl.tex2DVertShader && 0 < ppgl.tex2DFragShader) { + ppgl.tex2DShaderProgram = createProgram(ppgl.tex2DVertShader, ppgl.tex2DFragShader); } - if (0 < tex2DShaderProgram) { - tex2DVertLoc = getAttribLocation(tex2DShaderProgram, "position"); - tex2DTCoordLoc = getAttribLocation(tex2DShaderProgram, "texCoord"); - tex2DSamplerLoc = getUniformLocation(tex2DShaderProgram, "texMap"); + if (0 < ppgl.tex2DShaderProgram) { + ppgl.tex2DVertLoc = getAttribLocation(ppgl.tex2DShaderProgram, "position"); + ppgl.tex2DTCoordLoc = getAttribLocation(ppgl.tex2DShaderProgram, "texCoord"); + ppgl.tex2DSamplerLoc = getUniformLocation(ppgl.tex2DShaderProgram, "texMap"); } - loadedTex2DShader = true; - tex2DShaderContext = glContext; + ppgl.loadedTex2DShader = true; + ppgl.tex2DShaderContext = ppgl.glContext; - genBuffers(1, intBuffer); - texGeoVBO = intBuffer.get(0); - bindBuffer(ARRAY_BUFFER, texGeoVBO); - bufferData(ARRAY_BUFFER, 16 * SIZEOF_FLOAT, null, STATIC_DRAW); - - System.out.println("Done!"); + if (ppgl.texGeoVBO == 0) { + genBuffers(1, intBuffer); + ppgl.texGeoVBO = intBuffer.get(0); + bindBuffer(ARRAY_BUFFER, ppgl.texGeoVBO); + bufferData(ARRAY_BUFFER, 16 * SIZEOF_FLOAT, null, STATIC_DRAW); + } } if (texData == null) { texData = allocateDirectFloatBuffer(texCoords.length); } + + return ppgl; } protected void drawTexture2D(int id, int texW, int texH, int scrW, int scrH, int texX0, int texY0, int texX1, int texY1, int scrX0, int scrY0, int scrX1, int scrY1) { - initTex2DShader(); + PGL ppgl = initTex2DShader(); - if (0 < tex2DShaderProgram) { + if (0 < ppgl.tex2DShaderProgram) { // The texture overwrites anything drawn earlier. boolean depthTest = getDepthTest(); disable(DEPTH_TEST); @@ -986,10 +991,10 @@ public abstract class PGL { getIntegerv(VIEWPORT, viewBuffer); viewport(0, 0, scrW, scrH); - useProgram(tex2DShaderProgram); + useProgram(ppgl.tex2DShaderProgram); - enableVertexAttribArray(tex2DVertLoc); - enableVertexAttribArray(tex2DTCoordLoc); + enableVertexAttribArray(ppgl.tex2DVertLoc); + enableVertexAttribArray(ppgl.tex2DTCoordLoc); // Vertex coordinates of the textured quad are specified // in normalized screen space (-1, 1): @@ -1024,14 +1029,14 @@ public abstract class PGL { enabledTex = true; } bindTexture(TEXTURE_2D, id); - uniform1i(tex2DSamplerLoc, 0); + uniform1i(ppgl.tex2DSamplerLoc, 0); texData.position(0); - bindBuffer(ARRAY_BUFFER, texGeoVBO); + bindBuffer(ARRAY_BUFFER, ppgl.texGeoVBO); bufferData(ARRAY_BUFFER, 16 * SIZEOF_FLOAT, texData, STATIC_DRAW); - vertexAttribPointer(tex2DVertLoc, 2, FLOAT, false, 4 * SIZEOF_FLOAT, 0); - vertexAttribPointer(tex2DTCoordLoc, 2, FLOAT, false, 4 * SIZEOF_FLOAT, 2 * SIZEOF_FLOAT); + vertexAttribPointer(ppgl.tex2DVertLoc, 2, FLOAT, false, 4 * SIZEOF_FLOAT, 0); + vertexAttribPointer(ppgl.tex2DTCoordLoc, 2, FLOAT, false, 4 * SIZEOF_FLOAT, 2 * SIZEOF_FLOAT); drawArrays(TRIANGLE_STRIP, 0, 4); @@ -1042,8 +1047,8 @@ public abstract class PGL { disableTexturing(TEXTURE_2D); } - disableVertexAttribArray(tex2DVertLoc); - disableVertexAttribArray(tex2DTCoordLoc); + disableVertexAttribArray(ppgl.tex2DVertLoc); + disableVertexAttribArray(ppgl.tex2DTCoordLoc); useProgram(0); @@ -1055,47 +1060,53 @@ public abstract class PGL { depthMask(depthMask); viewport(viewBuffer.get(0), viewBuffer.get(1), - viewBuffer.get(2),viewBuffer.get(3)); + viewBuffer.get(2), viewBuffer.get(3)); } } - protected void initTexRectShader() { - if (!loadedTexRectShader || texRectShaderContext != glContext) { + protected PGL initTexRectShader() { + PGL ppgl = primaryPGL ? this : pg.getPrimaryPGL(); + + if (!ppgl.loadedTexRectShader || ppgl.texRectShaderContext != ppgl.glContext) { String vertSource = PApplet.join(texVertShaderSource, "\n"); String fragSource = PApplet.join(texRectFragShaderSource, "\n"); - texRectVertShader = createShader(VERTEX_SHADER, vertSource); - texRectFragShader = createShader(FRAGMENT_SHADER, fragSource); - if (0 < texRectVertShader && 0 < texRectFragShader) { - texRectShaderProgram = createProgram(texRectVertShader, - texRectFragShader); + ppgl.texRectVertShader = createShader(VERTEX_SHADER, vertSource); + ppgl.texRectFragShader = createShader(FRAGMENT_SHADER, fragSource); + if (0 < ppgl.texRectVertShader && 0 < ppgl.texRectFragShader) { + ppgl.texRectShaderProgram = createProgram(ppgl.texRectVertShader, + ppgl.texRectFragShader); } - if (0 < texRectShaderProgram) { - texRectVertLoc = getAttribLocation(texRectShaderProgram, "position"); - texRectTCoordLoc = getAttribLocation(texRectShaderProgram, "texCoord"); - texRectSamplerLoc = getUniformLocation(texRectShaderProgram, "texMap"); + if (0 < ppgl.texRectShaderProgram) { + ppgl.texRectVertLoc = getAttribLocation(ppgl.texRectShaderProgram, "position"); + ppgl.texRectTCoordLoc = getAttribLocation(ppgl.texRectShaderProgram, "texCoord"); + ppgl.texRectSamplerLoc = getUniformLocation(ppgl.texRectShaderProgram, "texMap"); } - loadedTexRectShader = true; - texRectShaderContext = glContext; + ppgl.loadedTexRectShader = true; + ppgl.texRectShaderContext = ppgl.glContext; - genBuffers(1, intBuffer); - texGeoVBO = intBuffer.get(0); - bindBuffer(ARRAY_BUFFER, texGeoVBO); - bufferData(ARRAY_BUFFER, 16 * SIZEOF_FLOAT, null, STATIC_DRAW); + if (ppgl.texGeoVBO == 0) { + genBuffers(1, intBuffer); + ppgl.texGeoVBO = intBuffer.get(0); + bindBuffer(ARRAY_BUFFER, ppgl.texGeoVBO); + bufferData(ARRAY_BUFFER, 16 * SIZEOF_FLOAT, null, STATIC_DRAW); + } } + + return ppgl; } protected void drawTextureRect(int id, int texW, int texH, int scrW, int scrH, int texX0, int texY0, int texX1, int texY1, int scrX0, int scrY0, int scrX1, int scrY1) { - initTexRectShader(); + PGL ppgl = initTexRectShader(); if (texData == null) { texData = allocateDirectFloatBuffer(texCoords.length); } - if (0 < texRectShaderProgram) { + if (0 < ppgl.texRectShaderProgram) { // The texture overwrites anything drawn earlier. boolean depthTest = getDepthTest(); disable(DEPTH_TEST); @@ -1112,10 +1123,10 @@ public abstract class PGL { getIntegerv(VIEWPORT, viewBuffer); viewport(0, 0, scrW, scrH); - useProgram(texRectShaderProgram); + useProgram(ppgl.texRectShaderProgram); - enableVertexAttribArray(texRectVertLoc); - enableVertexAttribArray(texRectTCoordLoc); + enableVertexAttribArray(ppgl.texRectVertLoc); + enableVertexAttribArray(ppgl.texRectTCoordLoc); // Vertex coordinates of the textured quad are specified // in normalized screen space (-1, 1): @@ -1150,14 +1161,14 @@ public abstract class PGL { enabledTex = true; } bindTexture(TEXTURE_RECTANGLE, id); - uniform1i(texRectSamplerLoc, 0); + uniform1i(ppgl.texRectSamplerLoc, 0); texData.position(0); - bindBuffer(ARRAY_BUFFER, texGeoVBO); + bindBuffer(ARRAY_BUFFER, ppgl.texGeoVBO); bufferData(ARRAY_BUFFER, 16 * SIZEOF_FLOAT, texData, STATIC_DRAW); - vertexAttribPointer(texRectVertLoc, 2, FLOAT, false, 4 * SIZEOF_FLOAT, 0); - vertexAttribPointer(texRectTCoordLoc, 2, FLOAT, false, 4 * SIZEOF_FLOAT, 2 * SIZEOF_FLOAT); + vertexAttribPointer(ppgl.texRectVertLoc, 2, FLOAT, false, 4 * SIZEOF_FLOAT, 0); + vertexAttribPointer(ppgl.texRectTCoordLoc, 2, FLOAT, false, 4 * SIZEOF_FLOAT, 2 * SIZEOF_FLOAT); drawArrays(TRIANGLE_STRIP, 0, 4); @@ -1168,8 +1179,8 @@ public abstract class PGL { disableTexturing(TEXTURE_RECTANGLE); } - disableVertexAttribArray(texRectVertLoc); - disableVertexAttribArray(texRectTCoordLoc); + disableVertexAttribArray(ppgl.texRectVertLoc); + disableVertexAttribArray(ppgl.texRectTCoordLoc); useProgram(0); @@ -1181,7 +1192,7 @@ public abstract class PGL { depthMask(depthMask); viewport(viewBuffer.get(0), viewBuffer.get(1), - viewBuffer.get(2),viewBuffer.get(3)); + viewBuffer.get(2), viewBuffer.get(3)); } } diff --git a/core/src/processing/opengl/PJOGL.java b/core/src/processing/opengl/PJOGL.java index 4fbd88e34..fb618f50c 100644 --- a/core/src/processing/opengl/PJOGL.java +++ b/core/src/processing/opengl/PJOGL.java @@ -80,7 +80,6 @@ public class PJOGL extends PGL { /** Selected GL profile */ public static GLProfile profile; static { - System.out.println("Animation thread: " + Thread.currentThread()); if (PROFILE == 2) { try { profile = GLProfile.getGL2ES1(); @@ -106,7 +105,6 @@ public class PJOGL extends PGL { PGraphics.showWarning("Requested profile GL4 but is not available, got: " + profile); } } else throw new RuntimeException(UNSUPPORTED_GLPROF_ERROR); - System.out.println("Done, got this profile: " + profile); if (2 < PROFILE) { texVertShaderSource = convertVertexSource(texVertShaderSource, 120, 150); @@ -276,8 +274,6 @@ public class PJOGL extends PGL { protected void initSurface(int antialias) { checkPrimary(); - System.out.println("initializing surface"); - if (canvasAWT != null && canvasNEWT != null) { // Restarting... if (canvasAWT != null) { @@ -363,8 +359,6 @@ public class PJOGL extends PGL { fboLayerInUse = false; firstFrame = true; setFps = false; - - System.out.println("done"); } From 759f58bf825e76a31f438d3b13670bfb4f356942 Mon Sep 17 00:00:00 2001 From: Ben Fry Date: Mon, 20 Jan 2014 22:58:06 -0500 Subject: [PATCH 18/51] not needed anymore --- core/make.sh | 7 ------- 1 file changed, 7 deletions(-) delete mode 100755 core/make.sh diff --git a/core/make.sh b/core/make.sh deleted file mode 100755 index 78d91b33a..000000000 --- a/core/make.sh +++ /dev/null @@ -1,7 +0,0 @@ -#!/bin/sh - -#javadoc -public -d doc *.java -#javadoc -private -d doc *.java -chmod +x preproc.pl -./preproc.pl -jikes -d . +D *.java From a0731dd3d7a6cdfd07af2bc73b3a3ca5b30c3f9e Mon Sep 17 00:00:00 2001 From: codeanticode Date: Tue, 21 Jan 2014 01:02:08 -0500 Subject: [PATCH 19/51] testing/debugging, context sharing --- core/src/processing/opengl/PGL.java | 25 +--- .../processing/opengl/PGraphicsOpenGL.java | 15 ++- core/src/processing/opengl/PJOGL.java | 126 +++++++++--------- 3 files changed, 76 insertions(+), 90 deletions(-) diff --git a/core/src/processing/opengl/PGL.java b/core/src/processing/opengl/PGL.java index dde84424c..849b528db 100644 --- a/core/src/processing/opengl/PGL.java +++ b/core/src/processing/opengl/PGL.java @@ -350,12 +350,6 @@ public abstract class PGL { } - protected void checkPrimary() { - if (!primaryPGL) { - throw new RuntimeException(NONPRIMARY_ERROR); - } - } - /** * Return the native canvas the OpenGL context associated to this PGL object * is rendering to (if any). @@ -376,8 +370,6 @@ public abstract class PGL { protected void deleteSurface() { - checkPrimary(); - if (threadIsCurrent() && fboLayerCreated) { deleteTextures(2, glColorTex); deleteFramebuffers(1, glColorFbo); @@ -395,13 +387,11 @@ public abstract class PGL { protected int getReadFramebuffer() { - checkPrimary(); return fboLayerInUse ? glColorFbo.get(0) : 0; } protected int getDrawFramebuffer() { - checkPrimary(); if (fboLayerInUse) return 1 < numSamples ? glMultiFbo.get(0) : glColorFbo.get(0); else return 0; @@ -409,31 +399,26 @@ public abstract class PGL { protected int getDefaultDrawBuffer() { - checkPrimary(); return fboLayerInUse ? COLOR_ATTACHMENT0 : FRONT; } protected int getDefaultReadBuffer() { - checkPrimary(); return fboLayerInUse ? COLOR_ATTACHMENT0 : FRONT; } - protected boolean isFBOBacked() { - checkPrimary(); + protected boolean isFBOBacked() {; return fboLayerInUse; } protected void requestFBOLayer() { - checkPrimary(); fboLayerRequested = true; } protected boolean isMultisampled() { - checkPrimary(); return 1 < numSamples; } @@ -467,7 +452,6 @@ public abstract class PGL { protected Texture wrapBackTexture(Texture texture) { - checkPrimary(); if (texture == null) { texture = new Texture(pg); texture.init(pg.width, pg.height, @@ -485,7 +469,6 @@ public abstract class PGL { protected Texture wrapFrontTexture(Texture texture) { - checkPrimary(); if (texture == null) { texture = new Texture(pg); texture.init(pg.width, pg.height, @@ -502,7 +485,6 @@ public abstract class PGL { protected void bindFrontTexture() { - checkPrimary(); usingFrontTex = true; if (!texturingIsEnabled(TEXTURE_2D)) { enableTexturing(TEXTURE_2D); @@ -512,7 +494,6 @@ public abstract class PGL { protected void unbindFrontTexture() { - checkPrimary(); if (textureIsBound(TEXTURE_2D, glColorTex.get(frontTex))) { // We don't want to unbind another texture // that might be bound instead of this one. @@ -528,7 +509,6 @@ public abstract class PGL { protected void syncBackTexture() { - checkPrimary(); if (usingFrontTex) needSepFrontTex = true; if (1 < numSamples) { bindFramebuffer(READ_FRAMEBUFFER, glMultiFbo.get(0)); @@ -546,7 +526,6 @@ public abstract class PGL { protected void beginDraw(boolean clear0) { - checkPrimary(); if (needFBOLayer(clear0)) { if (!fboLayerCreated) createFBOLayer(); @@ -597,7 +576,6 @@ public abstract class PGL { protected void endDraw(boolean clear0) { - checkPrimary(); if (fboLayerInUse) { syncBackTexture(); @@ -655,7 +633,6 @@ public abstract class PGL { private void createFBOLayer() { - System.out.println("Creating FBO layer"); String ext = getString(EXTENSIONS); if (-1 < ext.indexOf("texture_non_power_of_two")) { fboWidth = pg.width; diff --git a/core/src/processing/opengl/PGraphicsOpenGL.java b/core/src/processing/opengl/PGraphicsOpenGL.java index b86ece976..1dd4640bf 100644 --- a/core/src/processing/opengl/PGraphicsOpenGL.java +++ b/core/src/processing/opengl/PGraphicsOpenGL.java @@ -508,7 +508,9 @@ public class PGraphicsOpenGL extends PGraphics { "Stroke path is too long, some bevel triangles won't be added"; static final String TESSELLATION_ERROR = "Tessellation Error: %1$s"; - + static final String TEXTURE_SIZE_ERROR = + "The backing texture for the primary surface has a width/height different " + + "from the one given in size()."; ////////////////////////////////////////////////////////////// @@ -5348,7 +5350,7 @@ public class PGraphicsOpenGL extends PGraphics { * Don't use this inside glBegin/glEnd otherwise it'll * throw an GL_INVALID_OPERATION error. */ - public void report(String where) { + protected void report(String where) { if (!hints[DISABLE_OPENGL_ERRORS]) { int err = pgl.getError(); if (err != 0) { @@ -5499,11 +5501,12 @@ public class PGraphicsOpenGL extends PGraphics { // non-multisampled FBO, texture is actually the color buffer used by the // color FBO, so with the copy operation we should be done updating the // (off)screen buffer. - // First, copy the pixels to the texture. We don't need to invert the // pixel copy because the texture will be drawn inverted. + int tw = PApplet.min(texture.glWidth - x, w); + int th = PApplet.min(texture.glHeight - y, h); pgl.copyToTexture(texture.glTarget, texture.glFormat, texture.glName, - x, y, w, h, nativePixelBuffer); + x, y, tw, th, nativePixelBuffer); beginPixelsOp(OP_WRITE); drawTexture(x, y, w, h); endPixelsOp(); @@ -6257,6 +6260,10 @@ public class PGraphicsOpenGL extends PGraphics { if (pgl.isFBOBacked()) { texture = pgl.wrapBackTexture(texture); ptexture = pgl.wrapFrontTexture(ptexture); + + if (texture.glWidth != width || texture.glHeight != height) { + PGraphics.showWarning(TEXTURE_SIZE_ERROR); + } } } diff --git a/core/src/processing/opengl/PJOGL.java b/core/src/processing/opengl/PJOGL.java index fb618f50c..2725879b0 100644 --- a/core/src/processing/opengl/PJOGL.java +++ b/core/src/processing/opengl/PJOGL.java @@ -79,39 +79,6 @@ public class PJOGL extends PGL { /** Selected GL profile */ public static GLProfile profile; - static { - if (PROFILE == 2) { - try { - profile = GLProfile.getGL2ES1(); - } catch (GLException ex) { - profile = GLProfile.getMaxFixedFunc(true); - } - } else if (PROFILE == 3) { - try { - profile = GLProfile.getGL2GL3(); - } catch (GLException ex) { - profile = GLProfile.getMaxProgrammable(true); - } - if (!profile.isGL3()) { - PGraphics.showWarning("Requested profile GL3 but is not available, got: " + profile); - } - } else if (PROFILE == 4) { - try { - profile = GLProfile.getGL4ES3(); - } catch (GLException ex) { - profile = GLProfile.getMaxProgrammable(true); - } - if (!profile.isGL4()) { - PGraphics.showWarning("Requested profile GL4 but is not available, got: " + profile); - } - } else throw new RuntimeException(UNSUPPORTED_GLPROF_ERROR); - - if (2 < PROFILE) { - texVertShaderSource = convertVertexSource(texVertShaderSource, 120, 150); - tex2DFragShaderSource = convertFragmentSource(tex2DFragShaderSource, 120, 150); - texRectFragShaderSource = convertFragmentSource(texRectFragShaderSource, 120, 150); - } - } // ........................................................ @@ -183,11 +150,17 @@ public class PJOGL extends PGL { /** The AWT-OpenGL canvas */ protected GLCanvas canvasAWT; - /** The NEWT-OpenGL canvas */ - protected NewtCanvasAWT canvasNEWT; + /** The shared AWT-OpenGL canvas */ + protected static GLCanvas sharedCanvasAWT; /** The NEWT window */ - protected GLWindow window; + protected GLWindow windowNEWT; + + /** The shared NEWT window */ + protected static GLWindow sharedWindowNEWT; + + /** The NEWT-OpenGL canvas */ + protected NewtCanvasAWT canvasNEWT; /** The listener that fires the frame rendering in Processing */ protected PGLListener listener; @@ -254,7 +227,6 @@ public class PJOGL extends PGL { @Override protected void setFps(float fps) { - checkPrimary(); if (!setFps || targetFps != fps) { if (60 < fps) { // Disables v-sync @@ -272,16 +244,50 @@ public class PJOGL extends PGL { @Override protected void initSurface(int antialias) { - checkPrimary(); + if (profile == null) { + if (PROFILE == 2) { + try { + profile = GLProfile.getGL2ES1(); + } catch (GLException ex) { + profile = GLProfile.getMaxFixedFunc(true); + } + } else if (PROFILE == 3) { + try { + profile = GLProfile.getGL2GL3(); + } catch (GLException ex) { + profile = GLProfile.getMaxProgrammable(true); + } + if (!profile.isGL3()) { + PGraphics.showWarning("Requested profile GL3 but is not available, got: " + profile); + } + } else if (PROFILE == 4) { + try { + profile = GLProfile.getGL4ES3(); + } catch (GLException ex) { + profile = GLProfile.getMaxProgrammable(true); + } + if (!profile.isGL4()) { + PGraphics.showWarning("Requested profile GL4 but is not available, got: " + profile); + } + } else throw new RuntimeException(UNSUPPORTED_GLPROF_ERROR); + + if (2 < PROFILE) { + texVertShaderSource = convertVertexSource(texVertShaderSource, 120, 150); + tex2DFragShaderSource = convertFragmentSource(tex2DFragShaderSource, 120, 150); + texRectFragShaderSource = convertFragmentSource(texRectFragShaderSource, 120, 150); + } + } if (canvasAWT != null && canvasNEWT != null) { // Restarting... if (canvasAWT != null) { + sharedCanvasAWT = null; canvasAWT.removeGLEventListener(listener); pg.parent.removeListeners(canvasAWT); pg.parent.remove(canvasAWT); } else if (canvasNEWT != null) { - window.removeGLEventListener(listener); + sharedWindowNEWT = null; + windowNEWT.removeGLEventListener(listener); pg.parent.remove(canvasNEWT); } sinkFBO = backFBO = frontFBO = null; @@ -328,6 +334,11 @@ public class PJOGL extends PGL { if (WINDOW_TOOLKIT == AWT) { canvasAWT = new GLCanvas(caps); + if (sharedCanvasAWT == null) { + sharedCanvasAWT = canvasAWT; + } else { + canvasAWT.setSharedAutoDrawable(sharedCanvasAWT); + } canvasAWT.setBounds(0, 0, pg.width, pg.height); canvasAWT.setBackground(new Color(pg.backgroundColor, true)); canvasAWT.setFocusable(true); @@ -339,8 +350,13 @@ public class PJOGL extends PGL { canvas = canvasAWT; canvasNEWT = null; } else if (WINDOW_TOOLKIT == NEWT) { - window = GLWindow.create(caps); - canvasNEWT = new NewtCanvasAWT(window); + windowNEWT = GLWindow.create(caps); + if (sharedWindowNEWT == null) { + sharedWindowNEWT = windowNEWT; + } else { + windowNEWT.setSharedAutoDrawable(sharedWindowNEWT); + } + canvasNEWT = new NewtCanvasAWT(windowNEWT); canvasNEWT.setBounds(0, 0, pg.width, pg.height); canvasNEWT.setBackground(new Color(pg.backgroundColor, true)); canvasNEWT.setFocusable(true); @@ -364,7 +380,6 @@ public class PJOGL extends PGL { @Override protected void reinitSurface() { - checkPrimary(); sinkFBO = backFBO = frontFBO = null; fboLayerCreated = false; fboLayerInUse = false; @@ -383,18 +398,18 @@ public class PJOGL extends PGL { } else if (WINDOW_TOOLKIT == NEWT) { if (EVENTS_TOOLKIT == NEWT) { NEWTMouseListener mouseListener = new NEWTMouseListener(); - window.addMouseListener(mouseListener); + windowNEWT.addMouseListener(mouseListener); NEWTKeyListener keyListener = new NEWTKeyListener(); - window.addKeyListener(keyListener); + windowNEWT.addKeyListener(keyListener); NEWTWindowListener winListener = new NEWTWindowListener(); - window.addWindowListener(winListener); + windowNEWT.addWindowListener(winListener); } else if (EVENTS_TOOLKIT == AWT) { pg.parent.removeListeners(canvasNEWT); pg.parent.addListeners(canvasNEWT); } listener = new PGLListener(); - window.addGLEventListener(listener); + windowNEWT.addGLEventListener(listener); } if (canvas != null) { @@ -411,16 +426,14 @@ public class PJOGL extends PGL { canvasAWT.removeGLEventListener(listener); pg.parent.removeListeners(canvasAWT); } else if (canvasNEWT != null) { - window.removeGLEventListener(listener); + windowNEWT.removeGLEventListener(listener); } GLProfile.shutdown(); - System.out.println("bye bye"); } @Override protected int getReadFramebuffer() { - checkPrimary(); if (fboLayerInUse) { return glColorFbo.get(0); } else if (capabilities.isFBO()) { @@ -433,7 +446,6 @@ public class PJOGL extends PGL { @Override protected int getDrawFramebuffer() { - checkPrimary(); if (fboLayerInUse) { if (1 < numSamples) { return glMultiFbo.get(0); @@ -450,7 +462,6 @@ public class PJOGL extends PGL { @Override protected int getDefaultDrawBuffer() { - checkPrimary(); if (fboLayerInUse) { return COLOR_ATTACHMENT0; } else if (capabilities.isFBO()) { @@ -465,7 +476,6 @@ public class PJOGL extends PGL { @Override protected int getDefaultReadBuffer() { - checkPrimary(); if (fboLayerInUse) { return COLOR_ATTACHMENT0; } else if (capabilities.isFBO()) { @@ -480,7 +490,6 @@ public class PJOGL extends PGL { @Override protected boolean isFBOBacked() { - checkPrimary(); return super.isFBOBacked() || capabilities.isFBO(); } @@ -499,7 +508,6 @@ public class PJOGL extends PGL { @Override protected Texture wrapBackTexture(Texture texture) { - checkPrimary(); if (texture == null || changedBackTex) { if (USE_JOGL_FBOLAYER) { texture = new Texture(pg); @@ -527,7 +535,6 @@ public class PJOGL extends PGL { @Override protected Texture wrapFrontTexture(Texture texture) { - checkPrimary(); if (texture == null || changedFrontTex) { if (USE_JOGL_FBOLAYER) { texture = new Texture(pg); @@ -555,7 +562,6 @@ public class PJOGL extends PGL { @Override protected void bindFrontTexture() { if (USE_JOGL_FBOLAYER) { - checkPrimary(); usingFrontTex = true; if (!texturingIsEnabled(TEXTURE_2D)) { enableTexturing(TEXTURE_2D); @@ -568,7 +574,6 @@ public class PJOGL extends PGL { @Override protected void unbindFrontTexture() { if (USE_JOGL_FBOLAYER) { - checkPrimary(); if (textureIsBound(TEXTURE_2D, frontTexAttach.getName())) { // We don't want to unbind another texture // that might be bound instead of this one. @@ -587,7 +592,6 @@ public class PJOGL extends PGL { @Override protected void syncBackTexture() { if (USE_JOGL_FBOLAYER) { - checkPrimary(); if (usingFrontTex) needSepFrontTex = true; if (1 < numSamples) { backFBO.syncSamplingSink(gl); @@ -656,7 +660,6 @@ public class PJOGL extends PGL { @Override protected void requestDraw() { - checkPrimary(); boolean canDraw = pg.parent.canDraw(); if (pg.initialized && (canDraw || prevCanDraw)) { try { @@ -664,7 +667,7 @@ public class PJOGL extends PGL { if (WINDOW_TOOLKIT == AWT) { canvasAWT.display(); } else if (WINDOW_TOOLKIT == NEWT) { - window.display(); + windowNEWT.display(); } try { drawLatch.await(DRAW_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS); @@ -689,11 +692,10 @@ public class PJOGL extends PGL { @Override protected void swapBuffers() { - checkPrimary(); if (WINDOW_TOOLKIT == AWT) { canvasAWT.swapBuffers(); } else if (WINDOW_TOOLKIT == NEWT) { - window.swapBuffers(); + windowNEWT.swapBuffers(); } } From adb0f431a86be956f2203d59babaeb396879b9a7 Mon Sep 17 00:00:00 2001 From: codeanticode Date: Tue, 21 Jan 2014 11:23:03 -0500 Subject: [PATCH 20/51] making sure that backing textures are reset when surface is resized --- core/src/processing/opengl/PGraphicsOpenGL.java | 9 +-------- core/src/processing/opengl/PJOGL.java | 5 +++-- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/core/src/processing/opengl/PGraphicsOpenGL.java b/core/src/processing/opengl/PGraphicsOpenGL.java index 1dd4640bf..9d7420a82 100644 --- a/core/src/processing/opengl/PGraphicsOpenGL.java +++ b/core/src/processing/opengl/PGraphicsOpenGL.java @@ -508,9 +508,6 @@ public class PGraphicsOpenGL extends PGraphics { "Stroke path is too long, some bevel triangles won't be added"; static final String TESSELLATION_ERROR = "Tessellation Error: %1$s"; - static final String TEXTURE_SIZE_ERROR = - "The backing texture for the primary surface has a width/height different " + - "from the one given in size()."; ////////////////////////////////////////////////////////////// @@ -6260,10 +6257,6 @@ public class PGraphicsOpenGL extends PGraphics { if (pgl.isFBOBacked()) { texture = pgl.wrapBackTexture(texture); ptexture = pgl.wrapFrontTexture(ptexture); - - if (texture.glWidth != width || texture.glHeight != height) { - PGraphics.showWarning(TEXTURE_SIZE_ERROR); - } } } @@ -6428,7 +6421,7 @@ public class PGraphicsOpenGL extends PGraphics { pgl.disable(PGL.POLYGON_SMOOTH); if (sized) { - //reapplySettings(); +// reapplySettings(); // To avoid having garbage in the screen after a resize, // in the case background is not called in draw(). diff --git a/core/src/processing/opengl/PJOGL.java b/core/src/processing/opengl/PJOGL.java index 2725879b0..0d8bb626b 100644 --- a/core/src/processing/opengl/PJOGL.java +++ b/core/src/processing/opengl/PJOGL.java @@ -113,7 +113,7 @@ public class PJOGL extends PGL { WINDOW_TOOLKIT = AWT; EVENTS_TOOLKIT = AWT; USE_FBOLAYER_BY_DEFAULT = true; - USE_JOGL_FBOLAYER = true; + USE_JOGL_FBOLAYER = false; } else if (PApplet.platform == PConstants.LINUX) { WINDOW_TOOLKIT = AWT; EVENTS_TOOLKIT = AWT; @@ -820,6 +820,8 @@ public class PJOGL extends PGL { frontTexAttach = (FBObject.TextureAttachment)frontFBO. getColorbuffer(0); } else { + changedFrontTex = changedBackTex = sinkFBO == null; + // Default setting (to save resources): the front and back // textures are the same. sinkFBO = backFBO.getSamplingSinkFBO(); @@ -827,7 +829,6 @@ public class PJOGL extends PGL { getColorbuffer(0); frontTexAttach = backTexAttach; } - } else { // w/out multisampling, rendering is done on the back buffer. frontFBO = fboDrawable.getFBObject(GL.GL_FRONT); From 71325def84f81abcff66ba8fb666f10a8fdc5eb6 Mon Sep 17 00:00:00 2001 From: codeanticode Date: Tue, 21 Jan 2014 12:12:59 -0500 Subject: [PATCH 22/51] added EmbedFrameTest example in Demos/Test --- .../Tests/EmbedFrameTest/EmbedFrameTest.pde | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 java/examples/Demos/Tests/EmbedFrameTest/EmbedFrameTest.pde diff --git a/java/examples/Demos/Tests/EmbedFrameTest/EmbedFrameTest.pde b/java/examples/Demos/Tests/EmbedFrameTest/EmbedFrameTest.pde new file mode 100644 index 000000000..9a29eb58b --- /dev/null +++ b/java/examples/Demos/Tests/EmbedFrameTest/EmbedFrameTest.pde @@ -0,0 +1,88 @@ +// Based on code by GeneKao (https://github.com/GeneKao) + +import peasy.*; + +import javax.swing.JFrame; +import java.awt.BorderLayout; +import java.awt.Insets; +EmbeddedSketch eSketch; +ChildApplet child = new ChildApplet(); +boolean mousePressedOnParent = false; +PeasyCam cam, cam2; + +void setup() { + size(320, 240, P3D); + cam = new PeasyCam(this, 300); + eSketch = new EmbeddedSketch(child); + smooth(); +} + +void draw() { + background(250); + if (mousePressed) { + fill(0); + text("Mouse pressed on parent.", 10, 10); + fill(0, 240, 0); + ellipse(mouseX, mouseY, 60, 60); + mousePressedOnParent = true; + } else { + fill(20); + ellipse(width/2, height/2, 60, 60); + mousePressedOnParent = false; + } + box(100); + if (eSketch.sketch.mousePressed) { + text("Mouse pressed on child.", 10, 30); + } +} + +//The JFrame which will contain the child applet +class EmbeddedSketch extends JFrame { + PApplet sketch; + EmbeddedSketch(PApplet p) { + int w = 400; + int h = 400; + sketch = p; + setVisible(true); + + setLayout(new BorderLayout()); + add(p, BorderLayout.CENTER); + p.init(); + + Insets insets = getInsets(); + setSize(insets.left + w, insets.top + h); + p.setBounds(insets.left, insets.top, w, h); + + setLocation(500, 200); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + } +} + +class ChildApplet extends PApplet { + void setup() { + size(400, 400, P3D); + smooth(); + cam2 = new PeasyCam(this, 300); + cam2.reset(); + } + + void draw() { + background(0); + if (mousePressed) { + fill(240, 0, 0); + ellipse(mouseX, mouseY, 20, 20); + fill(255); + text("Mouse pressed on child.", 10, 30); + } else { + fill(255); + ellipse(width/2, height/2, 20, 20); + } + + box(100, 200, 100); + if (mousePressedOnParent) { + fill(255); + text("Mouse pressed on parent", 20, 20); + } + } +} + From 864281466e238db597c5f0a8168aebf037451c0d Mon Sep 17 00:00:00 2001 From: codeanticode Date: Tue, 21 Jan 2014 12:14:15 -0500 Subject: [PATCH 23/51] Implemented getGL() method in PLWJGL, but LWJGL not tested after latest changes (removal of most static fields in PGraphicsOpenGL, PGL) --- java/libraries/lwjgl/src/processing/lwjgl/PLWJGL.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/java/libraries/lwjgl/src/processing/lwjgl/PLWJGL.java b/java/libraries/lwjgl/src/processing/lwjgl/PLWJGL.java index b842a276b..7be2db1e0 100644 --- a/java/libraries/lwjgl/src/processing/lwjgl/PLWJGL.java +++ b/java/libraries/lwjgl/src/processing/lwjgl/PLWJGL.java @@ -1996,4 +1996,9 @@ public class PLWJGL extends PGL { public void drawBuffer(int buf) { GL11.glDrawBuffer(buf); } + + + @Override + protected void getGL(PGL pgl) { + } } From c27d1bf72c24e2a2433025d562cd6c6364f1eeaa Mon Sep 17 00:00:00 2001 From: codeanticode Date: Tue, 21 Jan 2014 15:36:12 -0500 Subject: [PATCH 24/51] added JOGL.getGL() utility function, restore original USE_JOGL_FBOLAYER value (true for OSX) --- core/src/processing/opengl/PGL.java | 3 +- core/src/processing/opengl/PJOGL.java | 60 ++++++++++++------- .../LargeStage.pde} | 0 3 files changed, 41 insertions(+), 22 deletions(-) rename java/libraries/glw/examples/{BigWindow/BigWindow.pde => LargeStage/LargeStage.pde} (100%) diff --git a/core/src/processing/opengl/PGL.java b/core/src/processing/opengl/PGL.java index 849b528db..96c4fcb7d 100644 --- a/core/src/processing/opengl/PGL.java +++ b/core/src/processing/opengl/PGL.java @@ -58,7 +58,7 @@ public abstract class PGL { protected int glContext; /** true if this is the GL interface for a primary surface PGraphics */ - protected boolean primaryPGL; + public boolean primaryPGL; // ........................................................ @@ -914,7 +914,6 @@ public abstract class PGL { PGL ppgl = primaryPGL ? this : pg.getPrimaryPGL(); if (!ppgl.loadedTex2DShader || ppgl.tex2DShaderContext != ppgl.glContext) { - System.out.println("Initializing PGL texture shader"); String vertSource = PApplet.join(texVertShaderSource, "\n"); String fragSource = PApplet.join(tex2DFragShaderSource, "\n"); ppgl.tex2DVertShader = createShader(VERTEX_SHADER, vertSource); diff --git a/core/src/processing/opengl/PJOGL.java b/core/src/processing/opengl/PJOGL.java index 0d8bb626b..2d293684d 100644 --- a/core/src/processing/opengl/PJOGL.java +++ b/core/src/processing/opengl/PJOGL.java @@ -113,7 +113,7 @@ public class PJOGL extends PGL { WINDOW_TOOLKIT = AWT; EVENTS_TOOLKIT = AWT; USE_FBOLAYER_BY_DEFAULT = true; - USE_JOGL_FBOLAYER = false; + USE_JOGL_FBOLAYER = true; } else if (PApplet.platform == PConstants.LINUX) { WINDOW_TOOLKIT = AWT; EVENTS_TOOLKIT = AWT; @@ -648,6 +648,26 @@ public class PJOGL extends PGL { } + protected void getGL(GLAutoDrawable glDrawable) { + context = glDrawable.getContext(); + glContext = context.hashCode(); + glThread = Thread.currentThread(); + + gl = context.getGL(); + gl2 = gl.getGL2ES2(); + try { + gl2x = gl.getGL2(); + } catch (javax.media.opengl.GLException e) { + gl2x = null; + } + try { + gl3 = gl.getGL2GL3(); + } catch (javax.media.opengl.GLException e) { + gl3 = null; + } + } + + @Override protected boolean canDraw() { return pg.initialized && pg.parent.isDisplayable(); @@ -869,25 +889,25 @@ public class PJOGL extends PGL { //getGL(glDrawable); } - private void getGL(GLAutoDrawable glDrawable) { - drawable = glDrawable; - context = glDrawable.getContext(); - glContext = context.hashCode(); - glThread = Thread.currentThread(); - - gl = context.getGL(); - gl2 = gl.getGL2ES2(); - try { - gl2x = gl.getGL2(); - } catch (javax.media.opengl.GLException e) { - gl2x = null; - } - try { - gl3 = gl.getGL2GL3(); - } catch (javax.media.opengl.GLException e) { - gl3 = null; - } - } +// private void getGL(GLAutoDrawable glDrawable) { +// drawable = glDrawable; +// context = glDrawable.getContext(); +// glContext = context.hashCode(); +// glThread = Thread.currentThread(); +// +// gl = context.getGL(); +// gl2 = gl.getGL2ES2(); +// try { +// gl2x = gl.getGL2(); +// } catch (javax.media.opengl.GLException e) { +// gl2x = null; +// } +// try { +// gl3 = gl.getGL2GL3(); +// } catch (javax.media.opengl.GLException e) { +// gl3 = null; +// } +// } } protected void nativeMouseEvent(com.jogamp.newt.event.MouseEvent nativeEvent, diff --git a/java/libraries/glw/examples/BigWindow/BigWindow.pde b/java/libraries/glw/examples/LargeStage/LargeStage.pde similarity index 100% rename from java/libraries/glw/examples/BigWindow/BigWindow.pde rename to java/libraries/glw/examples/LargeStage/LargeStage.pde From 5628631f951c004e4b06a5884b458d242a5c1a68 Mon Sep 17 00:00:00 2001 From: codeanticode Date: Tue, 21 Jan 2014 15:46:10 -0500 Subject: [PATCH 25/51] added public constructors to the NEWT event listeners in PJOGL --- core/src/processing/opengl/PJOGL.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/core/src/processing/opengl/PJOGL.java b/core/src/processing/opengl/PJOGL.java index 2d293684d..994ff21d1 100644 --- a/core/src/processing/opengl/PJOGL.java +++ b/core/src/processing/opengl/PJOGL.java @@ -976,6 +976,9 @@ public class PJOGL extends PGL { } protected class NEWTWindowListener implements com.jogamp.newt.event.WindowListener { + public NEWTWindowListener() { + super(); + } @Override public void windowGainedFocus(com.jogamp.newt.event.WindowEvent arg0) { pg.parent.focusGained(null); @@ -1008,6 +1011,9 @@ public class PJOGL extends PGL { // NEWT mouse listener protected class NEWTMouseListener extends com.jogamp.newt.event.MouseAdapter { + public NEWTMouseListener() { + super(); + } @Override public void mousePressed(com.jogamp.newt.event.MouseEvent e) { nativeMouseEvent(e, MouseEvent.PRESS); @@ -1044,6 +1050,9 @@ public class PJOGL extends PGL { // NEWT key listener protected class NEWTKeyListener extends com.jogamp.newt.event.KeyAdapter { + public NEWTKeyListener() { + super(); + } @Override public void keyPressed(com.jogamp.newt.event.KeyEvent e) { nativeKeyEvent(e, KeyEvent.PRESS); From ac617f7cb406a3aee8a31db80f76f455561c7fa4 Mon Sep 17 00:00:00 2001 From: codeanticode Date: Tue, 21 Jan 2014 15:46:31 -0500 Subject: [PATCH 26/51] making progress with GLW library --- .../libraries/glw/src/processing/glw/GLW.java | 70 ++++++- .../glw/src/processing/glw/PGraphics2D.java | 12 ++ .../glw/src/processing/glw/PGraphics3D.java | 12 ++ .../glw/src/processing/glw/PGraphicsGLW.java | 12 ++ .../glw/src/processing/glw/PNEWT.java | 196 +++++++++++++++--- 5 files changed, 269 insertions(+), 33 deletions(-) diff --git a/java/libraries/glw/src/processing/glw/GLW.java b/java/libraries/glw/src/processing/glw/GLW.java index c45805cb6..290204dde 100644 --- a/java/libraries/glw/src/processing/glw/GLW.java +++ b/java/libraries/glw/src/processing/glw/GLW.java @@ -1,6 +1,70 @@ package processing.glw; -public interface GLW { - static final String P2D = "processing.glw.PGraphics2D"; - static final String P3D = "processing.glw.PGraphics3D"; +import java.util.HashMap; + +import com.jogamp.newt.opengl.GLWindow; + +import processing.core.PGraphics; +import processing.opengl.PGraphicsOpenGL; + +public class GLW { + static public final String DUMMY = "processing.glw.PGraphicsGLW"; + static public final String OPENGL = "processing.glw.PGraphicsGLW"; + static public final String P2D = "processing.glw.PGraphics2D"; + static public final String P3D = "processing.glw.PGraphics3D"; + + static protected HashMap windows = new HashMap(); +// static protected HashMap canvases; + + public GLW() { + + } + + static public void createWindow(PGraphics pg) { + if (pg instanceof PGraphicsGLW || pg instanceof PGraphics2D || pg instanceof PGraphics3D) { + //PGraphicsOpenGL pgopengl = (PGraphicsOpenGL)pg; + //PNEWT pgl = (PNEWT)pgopengl.pgl; + //GLWindow win = pgl.createWindow(pg.width, pg.height, /*PNEWT.getWindow().getContext(), */pgopengl); + + +// windows.put(pg, win); + windows.put(pg, null); + //canvases.put(win, pg); + //win.setTitle("NEWT window " + windows.size()); + +// if (pg instanceof PGraphicsGLW) { +// PGraphicsGLW pgw = (PGraphicsGLW)pg; +// pgw.windowed = true; +// } else if (pg instanceof PGraphics2D) { +// PGraphics2D pgw = (PGraphics2D)pg; +// pgw.windowed = true; +// } else if (pg instanceof PGraphics3D) { +// PGraphics3D pgw = (PGraphics3D)pg; +// pgw.windowed = true; +// } + } + } + + static public GLWindow getWindow(PGraphics pg) { + return windows.get(pg); + } + + static public boolean isFocused(PGraphics pg) { + GLWindow win = windows.get(pg); + return win != null && win.hasFocus(); + } + + static public PGraphics getFocusedGraphics() { + for (PGraphics pg: windows.keySet()) { + if (isFocused(pg)) return pg; + } + return null; + } + + static public GLWindow getFocusedWindow() { + for (PGraphics pg: windows.keySet()) { + if (isFocused(pg)) return windows.get(pg); + } + return null; + } } diff --git a/java/libraries/glw/src/processing/glw/PGraphics2D.java b/java/libraries/glw/src/processing/glw/PGraphics2D.java index b28f93f31..a7b9d3055 100644 --- a/java/libraries/glw/src/processing/glw/PGraphics2D.java +++ b/java/libraries/glw/src/processing/glw/PGraphics2D.java @@ -4,7 +4,19 @@ import processing.opengl.PGL; import processing.opengl.PGraphicsOpenGL; public class PGraphics2D extends processing.opengl.PGraphics2D { +// protected boolean windowed = false; protected PGL createPGL(PGraphicsOpenGL pg) { return new PNEWT(pg); } + +// public void requestDraw() { +// System.out.println("requesting draw"); +// if (primarySurface || windowed) { +// if (initialized) { +// ((PNEWT)pgl).update(sized); +// } else { +// initPrimary(); +// } +// } +// } } diff --git a/java/libraries/glw/src/processing/glw/PGraphics3D.java b/java/libraries/glw/src/processing/glw/PGraphics3D.java index c4e011a98..3d3063e79 100644 --- a/java/libraries/glw/src/processing/glw/PGraphics3D.java +++ b/java/libraries/glw/src/processing/glw/PGraphics3D.java @@ -4,7 +4,19 @@ import processing.opengl.PGL; import processing.opengl.PGraphicsOpenGL; public class PGraphics3D extends processing.opengl.PGraphics3D { +// protected boolean windowed = false; + protected PGL createPGL(PGraphicsOpenGL pg) { return new PNEWT(pg); } + +// public void requestDraw() { +// if (primarySurface || windowed) { +// if (initialized) { +// ((PNEWT)pgl).update(sized); +// } else { +// initPrimary(); +// } +// } +// } } diff --git a/java/libraries/glw/src/processing/glw/PGraphicsGLW.java b/java/libraries/glw/src/processing/glw/PGraphicsGLW.java index 97572a1a0..0c96c6564 100644 --- a/java/libraries/glw/src/processing/glw/PGraphicsGLW.java +++ b/java/libraries/glw/src/processing/glw/PGraphicsGLW.java @@ -30,7 +30,19 @@ import processing.opengl.PGraphicsOpenGL; * */ public class PGraphicsGLW extends PGraphicsOpenGL { +// protected boolean windowed = false; + protected PGL createPGL(PGraphicsOpenGL pg) { return new PNEWT(pg); } + +// public void requestDraw() { +// if (primarySurface || windowed) { +// if (initialized) { +// ((PNEWT)pgl).update(sized); +// } else { +// initPrimary(); +// } +// } +// } } diff --git a/java/libraries/glw/src/processing/glw/PNEWT.java b/java/libraries/glw/src/processing/glw/PNEWT.java index 8eead55cc..020a0eab9 100644 --- a/java/libraries/glw/src/processing/glw/PNEWT.java +++ b/java/libraries/glw/src/processing/glw/PNEWT.java @@ -24,17 +24,31 @@ package processing.glw; +import javax.media.opengl.GL; +import javax.media.opengl.GLAutoDrawable; import javax.media.opengl.GLCapabilities; +import javax.media.opengl.GLContext; +import javax.media.opengl.GLDrawableFactory; +import javax.media.opengl.GLEventListener; import javax.media.opengl.GLException; +import javax.media.opengl.GLFBODrawable; import javax.media.opengl.GLProfile; +import javax.media.opengl.awt.GLCanvas; import com.jogamp.newt.opengl.GLWindow; import com.jogamp.newt.event.WindowAdapter; import com.jogamp.newt.event.WindowEvent; +import com.jogamp.opengl.FBObject; +import processing.core.PApplet; import processing.core.PGraphics; +import processing.event.KeyEvent; +import processing.event.MouseEvent; +import processing.opengl.PGL; import processing.opengl.PGraphicsOpenGL; import processing.opengl.PJOGL; +import processing.opengl.Texture; + public class PNEWT extends PJOGL { @@ -45,15 +59,14 @@ public class PNEWT extends PJOGL { USE_JOGL_FBOLAYER = false; } + protected static GLCapabilities sharedCaps; + protected static GLAutoDrawable sharedDrawable; + public PNEWT(PGraphicsOpenGL pg) { super(pg); } - - - static public GLWindow getWindow() { - return window; - } + protected void initSurface(int antialias) { if (profile == null) { @@ -88,25 +101,26 @@ public class PNEWT extends PJOGL { tex2DFragShaderSource = convertFragmentSource(tex2DFragShaderSource, 120, 150); texRectFragShaderSource = convertFragmentSource(texRectFragShaderSource, 120, 150); } - } else { - window.removeGLEventListener(listener); - pg.parent.remove(canvasNEWT); } // Setting up the desired capabilities; - GLCapabilities caps = new GLCapabilities(profile); - caps.setAlphaBits(REQUESTED_ALPHA_BITS); - caps.setDepthBits(REQUESTED_DEPTH_BITS); - caps.setStencilBits(REQUESTED_STENCIL_BITS); + sharedCaps = new GLCapabilities(profile); + sharedCaps.setAlphaBits(REQUESTED_ALPHA_BITS); + sharedCaps.setDepthBits(REQUESTED_DEPTH_BITS); + sharedCaps.setStencilBits(REQUESTED_STENCIL_BITS); + + sharedCaps.setPBuffer(false); + sharedCaps.setFBO(false); + sharedCaps.setSampleBuffers(false); - if (1 < antialias) { - caps.setSampleBuffers(true); - caps.setNumSamples(antialias); - } else { - caps.setSampleBuffers(false); - } - fboLayerRequested = false; + fboLayerRequested = false; + sharedDrawable = GLDrawableFactory.getFactory(profile).createDummyAutoDrawable(null, true, sharedCaps, null); + sharedDrawable.display(); // triggers GLContext object creation and native realization. + DummyListener listener = new DummyListener(); + sharedDrawable.addGLEventListener(listener); + + /* window = GLWindow.create(caps); window.setSize(pg.width, pg.height); window.setVisible(true); @@ -125,19 +139,141 @@ public class PNEWT extends PJOGL { }); registerListeners(); + */ + + pg.parent.frame.setVisible(false); } - protected int getCurrentWidth() { - if (window == null) return 0; - else return window.getWidth(); - } - - protected int getCurrentHeight() { - if (window == null) return 0; - else return window.getHeight(); - } - + protected boolean displayable() { return false; - } + } + + protected void requestDraw() { + // Creating windows + for (PGraphics pg: GLW.windows.keySet()) { + GLWindow win = GLW.windows.get(pg); + if (win == null) { + System.out.println("creating window"); + //win = GLWindow.create(sharedCaps); + win = GLWindow.create(sharedCaps); + win.setSharedAutoDrawable(sharedDrawable); + win.setSize(pg.width, pg.height); + win.setTitle("TEST"); + win.setVisible(true); + GLW.windows.put(pg, win); + + + NEWTListener listener = new NEWTListener(pg); + win.addGLEventListener(listener); + + NEWTMouseListener mouseListener = new NEWTMouseListener(); + win.addMouseListener(mouseListener); + NEWTKeyListener keyListener = new NEWTKeyListener(); + win.addKeyListener(keyListener); + NEWTWindowListener winListener = new NEWTWindowListener(); + win.addWindowListener(winListener); + + + win.addWindowListener(new WindowAdapter() { + @Override + public void windowDestroyNotify(final WindowEvent e) { + System.out.println("window destroyed"); +// System.exit(0); + } + }); + } + } + + sharedDrawable.display(); + + // Display windows + int totalCount = 0; + int realizedCount = 0; + for (GLWindow win: GLW.windows.values()) { + if (win != null) { + totalCount++; + if (win.isRealized()) realizedCount++; + win.display(); + } + } + + if (0 < totalCount && realizedCount == 0) { + System.out.println("SHOULD QUIT NOW"); + sharedDrawable.destroy(); + System.exit(0); + } + } + + protected class DummyListener implements GLEventListener { + public DummyListener() { + } + + @Override + public void display(GLAutoDrawable glDrawable) { + getGL(glDrawable); + pg.parent.handleDraw(); + } + + @Override + public void dispose(GLAutoDrawable adrawable) { + } + + @Override + public void init(GLAutoDrawable glDrawable) { + getGL(glDrawable); + + capabilities = glDrawable.getChosenGLCapabilities(); + if (!hasFBOs()) { + throw new RuntimeException(MISSING_FBO_ERROR); + } + if (!hasShaders()) { + throw new RuntimeException(MISSING_GLSL_ERROR); + } + if (USE_JOGL_FBOLAYER && capabilities.isFBO()) { + int maxs = maxSamples(); + numSamples = PApplet.min(capabilities.getNumSamples(), maxs); + } + } + + @Override + public void reshape(GLAutoDrawable glDrawable, int x, int y, int w, int h) { + } + } + + + protected class NEWTListener implements GLEventListener { + PGraphicsOpenGL pg; + PNEWT pgl; + + public NEWTListener(PGraphics pg) { + this.pg = (PGraphicsOpenGL)pg; + pgl = (PNEWT)this.pg.pgl; + } + + @Override + public void display(GLAutoDrawable glDrawable) { + pgl.getGL(glDrawable); + Texture tex = pg.texture; + if (tex != null) { + pgl.disable(PGL.BLEND); + pgl.drawTexture(tex.glTarget, tex.glName, + tex.glWidth, tex.glHeight, + 0, 0, pg.width, pg.height); + pgl.enable(PGL.BLEND); + } + } + + @Override + public void dispose(GLAutoDrawable adrawable) { + } + + @Override + public void init(GLAutoDrawable glDrawable) { + } + + @Override + public void reshape(GLAutoDrawable glDrawable, int x, int y, int w, int h) { + } + } } From 66118b56ef0f155b865e133ff4ea1b595200efd9 Mon Sep 17 00:00:00 2001 From: codeanticode Date: Tue, 21 Jan 2014 16:29:37 -0500 Subject: [PATCH 27/51] added checks for null framebuffers (useful to allow for "dummy" renderers such as GLW) --- .../processing/opengl/PGraphicsOpenGL.java | 23 ++++++++++++++----- 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/core/src/processing/opengl/PGraphicsOpenGL.java b/core/src/processing/opengl/PGraphicsOpenGL.java index 9d7420a82..b3a0450fb 100644 --- a/core/src/processing/opengl/PGraphicsOpenGL.java +++ b/core/src/processing/opengl/PGraphicsOpenGL.java @@ -359,7 +359,7 @@ public class PGraphicsOpenGL extends PGraphics { // Screen surface: /** Texture containing the current frame */ - public Texture texture; + protected Texture texture; /** Texture containing the previous frame */ protected Texture ptexture; @@ -1223,7 +1223,7 @@ public class PGraphicsOpenGL extends PGraphics { PGraphicsOpenGL ppg = getPrimaryPG(); if (ppg.currentFramebuffer != fbo) { ppg.currentFramebuffer = fbo; - ppg.currentFramebuffer.bind(); + if (ppg.currentFramebuffer != null) ppg.currentFramebuffer.bind(); } } @@ -1238,7 +1238,7 @@ public class PGraphicsOpenGL extends PGraphics { if (ppg.currentFramebuffer != fbo) { ppg.currentFramebuffer.finish(); ppg.currentFramebuffer = fbo; - ppg.currentFramebuffer.bind(); + if (ppg.currentFramebuffer != null) ppg.currentFramebuffer.bind(); } } @@ -1805,8 +1805,11 @@ public class PGraphicsOpenGL extends PGraphics { pgl.depthMask(true); } - getCurrentFB().bind(); - pgl.drawBuffer(getCurrentFB().getDefaultDrawBuffer()); + FrameBuffer fb = getCurrentFB(); + if (fb != null) { + getCurrentFB().bind(); + pgl.drawBuffer(getCurrentFB().getDefaultDrawBuffer()); + } } public void beginReadPixels() { @@ -6044,7 +6047,15 @@ public class PGraphicsOpenGL extends PGraphics { * off the screen (or offscreen drawing surface). */ public Texture getTexture() { - loadTexture(); + return getTexture(true); + } + + + /** + * Not an approved function either, don't use it. + */ + public Texture getTexture(boolean load) { + if (load) loadTexture(); return texture; } From 9714f24648a7a01981f5426fb3ae88484257784e Mon Sep 17 00:00:00 2001 From: codeanticode Date: Tue, 21 Jan 2014 16:43:11 -0500 Subject: [PATCH 28/51] more work on GLW --- .../MultipleWindows/MultipleWindows.pde | 0 .../libraries/glw/src/processing/glw/GLW.java | 44 ++++------- .../glw/src/processing/glw/PGraphics2D.java | 14 +--- .../glw/src/processing/glw/PGraphics3D.java | 12 --- .../glw/src/processing/glw/PGraphicsGLW.java | 63 +++++++++++---- .../glw/src/processing/glw/PNEWT.java | 78 ++++++++----------- 6 files changed, 97 insertions(+), 114 deletions(-) create mode 100644 java/libraries/glw/examples/MultipleWindows/MultipleWindows.pde diff --git a/java/libraries/glw/examples/MultipleWindows/MultipleWindows.pde b/java/libraries/glw/examples/MultipleWindows/MultipleWindows.pde new file mode 100644 index 000000000..e69de29bb diff --git a/java/libraries/glw/src/processing/glw/GLW.java b/java/libraries/glw/src/processing/glw/GLW.java index 290204dde..5968461e7 100644 --- a/java/libraries/glw/src/processing/glw/GLW.java +++ b/java/libraries/glw/src/processing/glw/GLW.java @@ -1,47 +1,29 @@ package processing.glw; -import java.util.HashMap; +import processing.core.PGraphics; import com.jogamp.newt.opengl.GLWindow; -import processing.core.PGraphics; -import processing.opengl.PGraphicsOpenGL; +import java.util.HashMap; public class GLW { - static public final String DUMMY = "processing.glw.PGraphicsGLW"; - static public final String OPENGL = "processing.glw.PGraphicsGLW"; - static public final String P2D = "processing.glw.PGraphics2D"; - static public final String P3D = "processing.glw.PGraphics3D"; + static public final String RENDERER = "processing.glw.PGraphicsGLW"; + static public final String OPENGL = "processing.glw.PGraphicsGLW"; - static protected HashMap windows = new HashMap(); -// static protected HashMap canvases; + static public final String P2D = "processing.glw.PGraphics2D"; + static public final String P3D = "processing.glw.PGraphics3D"; - public GLW() { - + static protected HashMap windows = + new HashMap(); + + public GLW() { } static public void createWindow(PGraphics pg) { - if (pg instanceof PGraphicsGLW || pg instanceof PGraphics2D || pg instanceof PGraphics3D) { - //PGraphicsOpenGL pgopengl = (PGraphicsOpenGL)pg; - //PNEWT pgl = (PNEWT)pgopengl.pgl; - //GLWindow win = pgl.createWindow(pg.width, pg.height, /*PNEWT.getWindow().getContext(), */pgopengl); - - -// windows.put(pg, win); + if (pg instanceof PGraphics2D || pg instanceof PGraphics3D) { windows.put(pg, null); - //canvases.put(win, pg); - //win.setTitle("NEWT window " + windows.size()); - -// if (pg instanceof PGraphicsGLW) { -// PGraphicsGLW pgw = (PGraphicsGLW)pg; -// pgw.windowed = true; -// } else if (pg instanceof PGraphics2D) { -// PGraphics2D pgw = (PGraphics2D)pg; -// pgw.windowed = true; -// } else if (pg instanceof PGraphics3D) { -// PGraphics3D pgw = (PGraphics3D)pg; -// pgw.windowed = true; -// } + } else { + throw new RuntimeException("Only GLW.P2D or GLW.P3D surfaces can be attached to a window"); } } diff --git a/java/libraries/glw/src/processing/glw/PGraphics2D.java b/java/libraries/glw/src/processing/glw/PGraphics2D.java index a7b9d3055..90b52899c 100644 --- a/java/libraries/glw/src/processing/glw/PGraphics2D.java +++ b/java/libraries/glw/src/processing/glw/PGraphics2D.java @@ -4,19 +4,7 @@ import processing.opengl.PGL; import processing.opengl.PGraphicsOpenGL; public class PGraphics2D extends processing.opengl.PGraphics2D { -// protected boolean windowed = false; protected PGL createPGL(PGraphicsOpenGL pg) { return new PNEWT(pg); - } - -// public void requestDraw() { -// System.out.println("requesting draw"); -// if (primarySurface || windowed) { -// if (initialized) { -// ((PNEWT)pgl).update(sized); -// } else { -// initPrimary(); -// } -// } -// } + } } diff --git a/java/libraries/glw/src/processing/glw/PGraphics3D.java b/java/libraries/glw/src/processing/glw/PGraphics3D.java index 3d3063e79..c4e011a98 100644 --- a/java/libraries/glw/src/processing/glw/PGraphics3D.java +++ b/java/libraries/glw/src/processing/glw/PGraphics3D.java @@ -4,19 +4,7 @@ import processing.opengl.PGL; import processing.opengl.PGraphicsOpenGL; public class PGraphics3D extends processing.opengl.PGraphics3D { -// protected boolean windowed = false; - protected PGL createPGL(PGraphicsOpenGL pg) { return new PNEWT(pg); } - -// public void requestDraw() { -// if (primarySurface || windowed) { -// if (initialized) { -// ((PNEWT)pgl).update(sized); -// } else { -// initPrimary(); -// } -// } -// } } diff --git a/java/libraries/glw/src/processing/glw/PGraphicsGLW.java b/java/libraries/glw/src/processing/glw/PGraphicsGLW.java index 0c96c6564..34c334fce 100644 --- a/java/libraries/glw/src/processing/glw/PGraphicsGLW.java +++ b/java/libraries/glw/src/processing/glw/PGraphicsGLW.java @@ -26,23 +26,60 @@ import processing.opengl.PGL; import processing.opengl.PGraphicsOpenGL; /** - * GLW renderer. + * GLW renderer. It's only role is to drive the main animation loop by calling + * requestDraw() and so allowing the offscreen canvases to be drawn inside the + * draw() method of the sketch. Currently, it cannot be used to draw into. * */ public class PGraphicsGLW extends PGraphicsOpenGL { -// protected boolean windowed = false; - protected PGL createPGL(PGraphicsOpenGL pg) { return new PNEWT(pg); - } + } -// public void requestDraw() { -// if (primarySurface || windowed) { -// if (initialized) { -// ((PNEWT)pgl).update(sized); -// } else { -// initPrimary(); -// } -// } -// } + public void beginDraw() { + if (primarySurface) { + setCurrentPG(this); + } else { + throw new RuntimeException("GLW renderer cannot be used as an offscreen surface"); + } + + report("top beginDraw()"); + + if (!checkGLThread()) { + return; + } + + if (drawing) { + return; + } + + if (!glParamsRead) { + getGLParameters(); + } + + drawing = true; + + report("bot beginDraw()"); + } + + public void endDraw() { + report("top endDraw()"); + + if (!drawing) { + return; + } + + if (primarySurface) { + setCurrentPG(null); + } else { + throw new RuntimeException("GLW renderer cannot be used as an offscreen surface."); + } + drawing = false; + + report("bot endDraw()"); + } + + protected void vertexImpl(float x, float y, float z, float u, float v) { + throw new RuntimeException("The main GLW renderer cannot be used to draw to."); + } } diff --git a/java/libraries/glw/src/processing/glw/PNEWT.java b/java/libraries/glw/src/processing/glw/PNEWT.java index 020a0eab9..08d3de581 100644 --- a/java/libraries/glw/src/processing/glw/PNEWT.java +++ b/java/libraries/glw/src/processing/glw/PNEWT.java @@ -24,26 +24,19 @@ package processing.glw; -import javax.media.opengl.GL; import javax.media.opengl.GLAutoDrawable; import javax.media.opengl.GLCapabilities; -import javax.media.opengl.GLContext; import javax.media.opengl.GLDrawableFactory; import javax.media.opengl.GLEventListener; import javax.media.opengl.GLException; -import javax.media.opengl.GLFBODrawable; import javax.media.opengl.GLProfile; -import javax.media.opengl.awt.GLCanvas; import com.jogamp.newt.opengl.GLWindow; import com.jogamp.newt.event.WindowAdapter; import com.jogamp.newt.event.WindowEvent; -import com.jogamp.opengl.FBObject; import processing.core.PApplet; import processing.core.PGraphics; -import processing.event.KeyEvent; -import processing.event.MouseEvent; import processing.opengl.PGL; import processing.opengl.PGraphicsOpenGL; import processing.opengl.PJOGL; @@ -60,7 +53,7 @@ public class PNEWT extends PJOGL { } protected static GLCapabilities sharedCaps; - protected static GLAutoDrawable sharedDrawable; + protected static GLAutoDrawable sharedDrawable; public PNEWT(PGraphicsOpenGL pg) { @@ -69,6 +62,10 @@ public class PNEWT extends PJOGL { protected void initSurface(int antialias) { + if (!(pg instanceof PGraphicsGLW)) { + throw new RuntimeException("GLW.RENDERER is the only option in size() when using the GLW library."); + } + if (profile == null) { if (PROFILE == 2) { try { @@ -118,28 +115,6 @@ public class PNEWT extends PJOGL { sharedDrawable.display(); // triggers GLContext object creation and native realization. DummyListener listener = new DummyListener(); sharedDrawable.addGLEventListener(listener); - - - /* - window = GLWindow.create(caps); - window.setSize(pg.width, pg.height); - window.setVisible(true); - window.setTitle(pg.parent.frame.getTitle()); - //window.setUndecorated(true); - pg.parent.frame.setVisible(false); - - canvas = canvasNEWT; - canvasAWT = null; - - window.addWindowListener(new WindowAdapter() { - @Override - public void windowDestroyNotify(final WindowEvent e) { - System.exit(0); - } - }); - - registerListeners(); - */ pg.parent.frame.setVisible(false); } @@ -149,13 +124,29 @@ public class PNEWT extends PJOGL { return false; } + + protected void beginDraw(boolean clear0) { + } + + + protected void endDraw(boolean clear0) { + } + + protected void requestDraw() { - // Creating windows + createWindows(); + + // Calling display() so the main draw() method is triggered, where the + // offscreen GLW canvases can be updated. + sharedDrawable.display(); + + displayWindows(); + } + + private void createWindows() { for (PGraphics pg: GLW.windows.keySet()) { GLWindow win = GLW.windows.get(pg); if (win == null) { - System.out.println("creating window"); - //win = GLWindow.create(sharedCaps); win = GLWindow.create(sharedCaps); win.setSharedAutoDrawable(sharedDrawable); win.setSize(pg.width, pg.height); @@ -163,7 +154,6 @@ public class PNEWT extends PJOGL { win.setVisible(true); GLW.windows.put(pg, win); - NEWTListener listener = new NEWTListener(pg); win.addGLEventListener(listener); @@ -173,21 +163,18 @@ public class PNEWT extends PJOGL { win.addKeyListener(keyListener); NEWTWindowListener winListener = new NEWTWindowListener(); win.addWindowListener(winListener); - win.addWindowListener(new WindowAdapter() { @Override public void windowDestroyNotify(final WindowEvent e) { - System.out.println("window destroyed"); -// System.exit(0); } }); } } - - sharedDrawable.display(); - - // Display windows + } + + + private void displayWindows() { int totalCount = 0; int realizedCount = 0; for (GLWindow win: GLW.windows.values()) { @@ -199,11 +186,12 @@ public class PNEWT extends PJOGL { } if (0 < totalCount && realizedCount == 0) { - System.out.println("SHOULD QUIT NOW"); + // All windows where closed, exit the application sharedDrawable.destroy(); System.exit(0); - } - } + } + } + protected class DummyListener implements GLEventListener { public DummyListener() { @@ -254,7 +242,7 @@ public class PNEWT extends PJOGL { @Override public void display(GLAutoDrawable glDrawable) { pgl.getGL(glDrawable); - Texture tex = pg.texture; + Texture tex = pg.getTexture(false); if (tex != null) { pgl.disable(PGL.BLEND); pgl.drawTexture(tex.glTarget, tex.glName, From 7412bcbd889648e5f81359db052f9cd754d0d201 Mon Sep 17 00:00:00 2001 From: codeanticode Date: Tue, 21 Jan 2014 17:13:11 -0500 Subject: [PATCH 30/51] cleaning up the GLW examples a bit --- .../glw/examples/LargeStage/LargeStage.pde | 24 ++++++++++++---- .../MultipleWindows/MultipleWindows.pde | 28 +++++++++++++++++++ 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/java/libraries/glw/examples/LargeStage/LargeStage.pde b/java/libraries/glw/examples/LargeStage/LargeStage.pde index 3149b6996..c40f9b0bc 100644 --- a/java/libraries/glw/examples/LargeStage/LargeStage.pde +++ b/java/libraries/glw/examples/LargeStage/LargeStage.pde @@ -1,13 +1,25 @@ import processing.glw.*; +PGraphics stage; + void setup() { - size(2560, 1440, GLW.P2D); + // The main window will be hidden, only GLW.RENDERER + // can be used in size() + size(100, 100, GLW.RENDERER); + + stage = createGraphics(2560, 1440, GLW.P2D); + GLW.createWindow(stage); frameRate(180); } void draw() { - background(255, 0, 0); - - fill(255); - text("FPS: " + frameRate, mouseX, mouseY); -} + // The draw() method is used to update the offscreen surfaces, + // but not to draw directly to the screen. + stage.beginDraw(); + stage.background(200); + stage.fill(255); + stage.ellipse(mouseX, mouseY, 50, 50); + stage.fill(0); + stage.text(frameRate, 100, 100); + stage.endDraw(); +} \ No newline at end of file diff --git a/java/libraries/glw/examples/MultipleWindows/MultipleWindows.pde b/java/libraries/glw/examples/MultipleWindows/MultipleWindows.pde index e69de29bb..902fdbcd4 100644 --- a/java/libraries/glw/examples/MultipleWindows/MultipleWindows.pde +++ b/java/libraries/glw/examples/MultipleWindows/MultipleWindows.pde @@ -0,0 +1,28 @@ +import processing.glw.*; + +PGraphics canvas1; +PGraphics canvas2; + +void setup() { + size(100, 100, GLW.RENDERER); + canvas1 = createGraphics(320, 240, GLW.P2D); + canvas2 = createGraphics(320, 240, GLW.P2D); + GLW.createWindow(canvas1); + GLW.createWindow(canvas2); +} + +void draw() { + canvas1.beginDraw(); + canvas1.background(200); + canvas1.ellipse(mouseX, mouseY, 100, 100); + canvas1.endDraw(); + + canvas2.beginDraw(); + canvas2.background(170); + canvas2.ellipse(mouseX, mouseY, 50, 50); + canvas2.endDraw(); +} + +void keyPressed() { + GLW.getFocusedWindow().setVisible(false); +} \ No newline at end of file From 69a3beaedee7cf0ca20cad77844bc43176178907 Mon Sep 17 00:00:00 2001 From: Ben Fry Date: Tue, 21 Jan 2014 18:19:28 -0500 Subject: [PATCH 31/51] fix compile warnings/errors --- .../gui/datatransfer/CompositeTransferable.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/build/shared/tools/MovieMaker/src/ch/randelshofer/gui/datatransfer/CompositeTransferable.java b/build/shared/tools/MovieMaker/src/ch/randelshofer/gui/datatransfer/CompositeTransferable.java index 22275f310..72d12198e 100644 --- a/build/shared/tools/MovieMaker/src/ch/randelshofer/gui/datatransfer/CompositeTransferable.java +++ b/build/shared/tools/MovieMaker/src/ch/randelshofer/gui/datatransfer/CompositeTransferable.java @@ -20,8 +20,8 @@ import java.io.*; * @author Werner Randelshofer */ public class CompositeTransferable implements java.awt.datatransfer.Transferable { - private HashMap transferables = new HashMap(); - private LinkedList flavors = new LinkedList(); + private HashMap transferables = new HashMap(); + private LinkedList flavors = new LinkedList(); /** Creates a new instance of CompositeTransferable */ public CompositeTransferable() { @@ -48,7 +48,7 @@ public class CompositeTransferable implements java.awt.datatransfer.Transferable * not supported. */ public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException { - Transferable t = (Transferable) transferables.get(flavor); + Transferable t = transferables.get(flavor); if (t == null) throw new UnsupportedFlavorException(flavor); return t.getTransferData(flavor); } @@ -60,7 +60,7 @@ public class CompositeTransferable implements java.awt.datatransfer.Transferable * @return an array of data flavors in which this data can be transferred */ public DataFlavor[] getTransferDataFlavors() { - return (DataFlavor[]) flavors.toArray(new DataFlavor[transferables.size()]); + return flavors.toArray(new DataFlavor[transferables.size()]); } /** From 9ca4000356e50e8dec0321714fbce7d0d5737e6a Mon Sep 17 00:00:00 2001 From: Ben Fry Date: Tue, 21 Jan 2014 18:19:53 -0500 Subject: [PATCH 32/51] remove unnecessary cast --- java/libraries/pdf/src/processing/pdf/PGraphicsPDF.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/java/libraries/pdf/src/processing/pdf/PGraphicsPDF.java b/java/libraries/pdf/src/processing/pdf/PGraphicsPDF.java index b49895706..69e02d1f8 100644 --- a/java/libraries/pdf/src/processing/pdf/PGraphicsPDF.java +++ b/java/libraries/pdf/src/processing/pdf/PGraphicsPDF.java @@ -408,8 +408,8 @@ public class PGraphicsPDF extends PGraphicsJava2D { translate(x1, y1); int imageWidth = image.width; int imageHeight = image.height; - scale((x2 - x1) / (float)imageWidth, - (y2 - y1) / (float)imageHeight); + scale((x2 - x1) / imageWidth, + (y2 - y1) / imageHeight); if (u2-u1 == imageWidth && v2-v1 == imageHeight) { g2.drawImage((Image) image.getNative(), 0, 0, null); } else { From cfb40240b356ef6476e362faee9ba7274b4ca09f Mon Sep 17 00:00:00 2001 From: Ben Fry Date: Tue, 21 Jan 2014 18:20:16 -0500 Subject: [PATCH 33/51] fix compile warning --- .../tools/MovieMaker/src/processing/app/tools/MovieMaker.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build/shared/tools/MovieMaker/src/processing/app/tools/MovieMaker.java b/build/shared/tools/MovieMaker/src/processing/app/tools/MovieMaker.java index eba834ee3..ec01f3762 100644 --- a/build/shared/tools/MovieMaker/src/processing/app/tools/MovieMaker.java +++ b/build/shared/tools/MovieMaker/src/processing/app/tools/MovieMaker.java @@ -1347,7 +1347,7 @@ public class MovieMaker extends JFrame implements Tool { private JLabel aboutLabel; private JButton chooseImageFolderButton; private JButton chooseSoundFileButton; - private JComboBox compressionBox; + private JComboBox compressionBox; private JLabel compressionLabel; // private JRadioButton fastStartCompressedRadio; // private JRadioButton fastStartRadio; From f1fa2f3a03fd8712504cfbba300428424dfc5a06 Mon Sep 17 00:00:00 2001 From: Ben Fry Date: Tue, 21 Jan 2014 18:21:46 -0500 Subject: [PATCH 34/51] add special case for null in println(), remove println() from dataPath() --- core/src/processing/core/PApplet.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/core/src/processing/core/PApplet.java b/core/src/processing/core/PApplet.java index bbde1fcef..945b91b92 100755 --- a/core/src/processing/core/PApplet.java +++ b/core/src/processing/core/PApplet.java @@ -4512,7 +4512,9 @@ public class PApplet extends Applet * be reliably bound by the compiler. */ static public void println(Object what) { - if (what != null && what.getClass().isArray()) { + if (what == null) { + System.out.println("null"); + } else if (what.getClass().isArray()) { printArray(what); } else { System.out.println(what.toString()); @@ -7876,7 +7878,6 @@ public class PApplet extends Applet // http://code.google.com/p/processing/issues/detail?id=1073 File containingFolder = new File(urlDecode(jarPath)).getParentFile(); File dataFolder = new File(containingFolder, "data"); - System.out.println(dataFolder); return new File(dataFolder, where); } // Windows, Linux, or when not using a Mac OS X .app file @@ -10894,7 +10895,7 @@ public class PApplet extends Applet final String[] argsWithSketchName = new String[args.length + 1]; System.arraycopy(args, 0, argsWithSketchName, 0, args.length); final String className = this.getClass().getSimpleName(); - final String cleanedClass = + final String cleanedClass = className.replaceAll("__[^_]+__\\$", "").replaceAll("\\$\\d+", ""); argsWithSketchName[args.length] = cleanedClass; runSketch(argsWithSketchName, this); From 2c02301f9ad6be6d9863e3d66dd29f706202fff8 Mon Sep 17 00:00:00 2001 From: Ben Fry Date: Tue, 21 Jan 2014 18:25:52 -0500 Subject: [PATCH 35/51] attempt to fix text issues, other notes --- core/src/processing/core/PGraphicsJava2D.java | 41 +++++++++++++------ 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/core/src/processing/core/PGraphicsJava2D.java b/core/src/processing/core/PGraphicsJava2D.java index 62c58a659..cb47ee7e1 100644 --- a/core/src/processing/core/PGraphicsJava2D.java +++ b/core/src/processing/core/PGraphicsJava2D.java @@ -3,7 +3,8 @@ /* Part of the Processing project - http://processing.org - Copyright (c) 2005-11 Ben Fry and Casey Reas + Copyright (c) 2013-14 The Processing Foundation + Copyright (c) 2005-13 Ben Fry and Casey Reas This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -35,22 +36,15 @@ import processing.data.XML; /** * Subclass for PGraphics that implements the graphics API using Java2D. - * - *

Pixel operations too slow? As of release 0085 (the first beta), - * the default renderer uses Java2D. It's more accurate than the renderer - * used in alpha releases of Processing (it handles stroke caps and joins, - * and has better polygon tessellation), but it's super slow for handling - * pixels. At least until we get a chance to get the old 2D renderer - * (now called P2D) working in a similar fashion, you can use - * size(w, h, P3D) instead of size(w, h) which will - * be faster for general pixel flipping madness.

- * - *

To get access to the Java 2D "Graphics2D" object for the default + *

+ * To get access to the Java 2D "Graphics2D" object for the default * renderer, use: *

Graphics2D g2 = ((PGraphicsJava2D)g).g2;
* This will let you do Java 2D stuff directly, but is not supported in * any way shape or form. Which just means "have fun, but don't complain - * if it breaks."

+ * if it breaks." + *

+ * Advanced debugging notes for Java2D. */ public class PGraphicsJava2D extends PGraphics { BufferStrategy strategy; @@ -425,6 +419,14 @@ public class PGraphicsJava2D extends PGraphics { @Override protected void defaultSettings() { if (!useCanvas) { + // Papered over another threading issue... + // See if this comes back now that the other issue is fixed. +// while (g2 == null) { +// try { +// System.out.println("sleeping until g2 is available"); +// Thread.sleep(5); +// } catch (InterruptedException e) { } +// } defaultComposite = g2.getComposite(); } super.defaultSettings(); @@ -1191,6 +1193,17 @@ public class PGraphicsJava2D extends PGraphics { quality == 4 ? RenderingHints.VALUE_INTERPOLATION_BICUBIC : RenderingHints.VALUE_INTERPOLATION_BILINEAR); + + // http://docs.oracle.com/javase/tutorial/2d/text/renderinghints.html + // Oracle Java text anti-aliasing on OS X looks like s*t compared to the + // text rendering with Apple's old Java 6. Below, failed attempts to fix: + g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, + RenderingHints.VALUE_TEXT_ANTIALIAS_ON); +// g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, +// RenderingHints.VALUE_TEXT_ANTIALIAS_GASP); +// g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, +// RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB); + // g2.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, // RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY); } @@ -1214,6 +1227,8 @@ public class PGraphicsJava2D extends PGraphics { RenderingHints.VALUE_ANTIALIAS_OFF); g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR); + g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, + RenderingHints.VALUE_TEXT_ANTIALIAS_OFF); } From fd57fac8b68fd94145bcd2a570c146a6b63b6700 Mon Sep 17 00:00:00 2001 From: Ben Fry Date: Tue, 21 Jan 2014 18:26:28 -0500 Subject: [PATCH 36/51] added print() method --- core/src/processing/data/IntList.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/core/src/processing/data/IntList.java b/core/src/processing/data/IntList.java index 1b8860fcc..78775be3f 100644 --- a/core/src/processing/data/IntList.java +++ b/core/src/processing/data/IntList.java @@ -771,6 +771,13 @@ public class IntList implements Iterable { } + public void print() { + for (int i = 0; i < size(); i++) { + System.out.format("[%d] %d%n", i, data[i]); + } + } + + @Override public String toString() { StringBuilder sb = new StringBuilder(); From 0339fae08c7e67f4a9db217fa8b1bb98bbf1a2a9 Mon Sep 17 00:00:00 2001 From: Ben Fry Date: Tue, 21 Jan 2014 18:29:31 -0500 Subject: [PATCH 37/51] additional notes and updates --- .../processing/app/syntax/TextAreaPainter.java | 10 ++++++++-- core/todo.txt | 12 ++++++++++++ todo.txt | 17 ++++++++++++++++- 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/app/src/processing/app/syntax/TextAreaPainter.java b/app/src/processing/app/syntax/TextAreaPainter.java index d1679ddaa..8bafb1d4e 100644 --- a/app/src/processing/app/syntax/TextAreaPainter.java +++ b/app/src/processing/app/syntax/TextAreaPainter.java @@ -564,7 +564,7 @@ public class TextAreaPainter extends JComponent implements TabExpander { /** Returns next tab stop after a specified point. */ -// TabExpander tabExpander = new TabExpander() { +// TabExpander tabExpander = new TabExpander() { @Override public float nextTabStop(float x, int tabOffset) { int offset = textArea.getHorizontalOffset(); @@ -572,7 +572,13 @@ public class TextAreaPainter extends JComponent implements TabExpander { return (ntabs + 1) * tabSize + offset; } // }; - + + + // do we go here? do will kill tabs? +// public float nextTabStop(float x, int tabOffset) { +// return x; +// } + public Dimension getPreferredSize() { return new Dimension(fm.charWidth('w') * defaults.cols, diff --git a/core/todo.txt b/core/todo.txt index c6d6d570b..dd6ba5ec5 100644 --- a/core/todo.txt +++ b/core/todo.txt @@ -1,6 +1,14 @@ 0224 core X PImage resize() causes PImage not to be rendered in JAVA2D X https://github.com/processing/processing/issues/2179 +X remove make.sh from core.. ancient +X remove println() from dataPath() +X add special case for 'null' to println() +X added print() method to IntList +X do for the others as well + +_ text looks lousy compared to the Apple JVM +_ mess with rendering hints? (notes in PGraphicsJava2D) fixed in 2.1 X draw() called again before finishing on OS X (retina issue) @@ -150,6 +158,10 @@ _ OpenGL offscreen requires primary surface to be OpenGL _ explain the new PGL interface _ can't really change the smoothing/options on offscreen _ is this still true? +_ how to name the retina pixel stuff +_ hint(ENABLE_RETINA_PIXELS) or hint(ENABLE_HIDPI_PIXELS) +_ hint(ENABLE_2X_PIXELS)? +_ hidpi is Apple's name as well diff --git a/todo.txt b/todo.txt index a24414ca3..7431403e1 100644 --- a/todo.txt +++ b/todo.txt @@ -1,6 +1,10 @@ 0224 pde X readStringUntil() missing from new serial library X https://github.com/processing/processing/issues/2174 +_ noJavaArg set to --export +_ https://github.com/processing/processing/issues/2182 +_ decision to be made on nextTabStop() inside TextAreaPainter + fixed in 2.1 X init() not called on tools until later @@ -10,10 +14,16 @@ X https://github.com/processing/processing/issues/1840 high -_ adding characters doesn't happen on position of cursor +_ sketchPath() returns user.home in exported apps on OSX +_ https://github.com/processing/processing/issues/2181 +_ tab characters not recognized/drawn in the editor (2.1) _ https://github.com/processing/processing/issues/2180 +_ https://github.com/processing/processing/issues/2183 +_ udp library has tabs in the text _ Chinese text is overlapped in Processing 2.1 editor _ https://github.com/processing/processing/issues/2173 +_ check on why 2x core.jar inside the Java folder +_ maybe OS X Java can't look in subfolders? (just auto-adds things) medium @@ -607,6 +617,9 @@ _ make available the background colors for present mode, stop button color _ isolate color chooser into a simpler/smaller class outside tools _ then can also use from inside processing applications as well _ http://code.google.com/p/processing/issues/detail?id=30 +_ maybe user prefs should only cover things that've changed? +_ how to balance colors/etc being stored elsewhere +_ ton of work to maintain this... PDE / Runner @@ -779,6 +792,8 @@ _ http://code.google.com/p/processing/issues/detail?id=632 DIST / Mac OS X +_ possible better option for doing retina? +_ g.getFontRenderContext().getTransform().equals(AffineTransform.getScaleInstance(2.0, 2.0)) _ appbundler improvements _ don't re-copy JRE into work folder if already exists _ implement a splash screen From 1a24a3e8e9fa7aaf1699aafc83c4e8e351e7c447 Mon Sep 17 00:00:00 2001 From: Ben Fry Date: Tue, 21 Jan 2014 19:37:01 -0500 Subject: [PATCH 38/51] Update README.md --- README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index 55aaa792a..c01b621bd 100644 --- a/README.md +++ b/README.md @@ -19,8 +19,7 @@ The [processing-web](https://github.com/processing/processing-web/) repository contains reference, examples, and the site. (Please use that link to file issues regarding the web site, the examples, or the reference.) -The instructions for building the source [are here](https://github.com/processing/processing/wiki/Build-Instructions), -although they [need an update](https://github.com/processing/processing/issues/1629). +The instructions for building the source [are here](https://github.com/processing/processing/wiki/Build-Instructions). Someday we'll also write code style guidelines, fix all these bugs, throw together hundreds of unit tests, and solve the Israeli-Palestinian conflict. From 0e469143cbb79802b1bde109c8118a013f8cc6d9 Mon Sep 17 00:00:00 2001 From: Ben Fry Date: Tue, 21 Jan 2014 21:33:01 -0500 Subject: [PATCH 39/51] fix app name on OS X, update JVM requirement, misc todo items --- build/build.xml | 5 +++-- core/todo.txt | 38 +++++++++++++++++++++++++++++++++++++- todo.txt | 47 ++++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 84 insertions(+), 6 deletions(-) diff --git a/build/build.xml b/build/build.xml index 736f95f26..60f2540b3 100755 --- a/build/build.xml +++ b/build/build.xml @@ -152,7 +152,7 @@ - + @@ -542,10 +542,11 @@