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) { + } + } }