diff --git a/java/libraries/opengl2/src/processing/opengl2/PGraphicsOpenGL2.java b/java/libraries/opengl2/src/processing/opengl2/PGraphicsOpenGL2.java index cb7a4d9b4..d86c7a653 100644 --- a/java/libraries/opengl2/src/processing/opengl2/PGraphicsOpenGL2.java +++ b/java/libraries/opengl2/src/processing/opengl2/PGraphicsOpenGL2.java @@ -379,7 +379,7 @@ public class PGraphicsOpenGL2 extends PGraphics { // Testing geometry buffer for now (but it already rocks) ... public boolean USE_GBUFFER = false; - public boolean GBUFFER_MERGE_ALL = true; + public boolean GBUFFER_MERGE_ALL = false; public boolean GBUFFER_UPDATE_STACK = true; public GeometryBuffer gbuffer; @@ -651,10 +651,6 @@ public class PGraphicsOpenGL2 extends PGraphics { numTexBuffers = 1; - if (USE_GBUFFER) { - gbuffer = new GeometryBuffer(); - } - geometryAllocated = true; } @@ -730,6 +726,7 @@ public class PGraphicsOpenGL2 extends PGraphics { return id; } + protected void deleteGLResource(int id, int type) { if (type == GL_TEXTURE_OBJECT) { if (glTextureObjects.contains(id)) { @@ -923,9 +920,10 @@ public class PGraphicsOpenGL2 extends PGraphics { texCoordBuffer[t].rewind(); } - if (USE_GBUFFER && GBUFFER_MERGE_ALL) { - gbuffer.init(TRIANGLES); - } + if (USE_GBUFFER) { + if (gbuffer == null) gbuffer = new GeometryBuffer(); + if (GBUFFER_MERGE_ALL) gbuffer.init(TRIANGLES); + } // Each frame starts with textures disabled. noTexture(); @@ -999,10 +997,8 @@ public class PGraphicsOpenGL2 extends PGraphics { public void endDraw() { report("top endDraw()"); - - super.endDraw(); - if (USE_GBUFFER && GBUFFER_MERGE_ALL) { + if (USE_GBUFFER && GBUFFER_MERGE_ALL && (gbuffer != null)) { gl2f.glEnableClientState(GL2.GL_VERTEX_ARRAY); gl2f.glEnableClientState(GL2.GL_COLOR_ARRAY); gl2f.glEnableClientState(GL2.GL_NORMAL_ARRAY); @@ -1310,6 +1306,7 @@ public class PGraphicsOpenGL2 extends PGraphics { } } + @SuppressWarnings("unchecked") protected void beginShapeRecorderImpl() { recordingShape = true; @@ -2103,7 +2100,7 @@ public class PGraphicsOpenGL2 extends PGraphics { ////////////////////////////////////////////////////////////// - // POINTS (override from P3D) + // POINTS protected void renderPoints(int start, int stop) { gl2f.glEnableClientState(GL2.GL_VERTEX_ARRAY); @@ -2155,14 +2152,12 @@ public class PGraphicsOpenGL2 extends PGraphics { ////////////////////////////////////////////////////////////// - // LINES (override from P3D) - - // protected final void addLineBreak() // PGraphics3D + // LINES /** * Begin a new section of stroked geometry. */ - protected final void addLineBreak() { + protected void addLineBreak() { if (pathCount == pathOffset.length) { pathOffset = PApplet.expand(pathOffset); pathLength = PApplet.expand(pathLength); @@ -2347,8 +2342,7 @@ public class PGraphicsOpenGL2 extends PGraphics { int maxi = PApplet.max(a, b, c); PImage[] images; - images = vertexTex[a]; - + images = vertexTex[a]; boolean firstFace = triangleCount == 0; if (diffFromTextures0(images) || firstFace) { @@ -3196,6 +3190,31 @@ public class PGraphicsOpenGL2 extends PGraphics { ////////////////////////////////////////////////////////////// + // SMOOTH + + public void smooth() { + smooth = true; + if (hints[DISABLE_OPENGL_2X_SMOOTH]) { + gl2f.glEnable(GL2.GL_MULTISAMPLE); + gl2f.glEnable(GL2.GL_POINT_SMOOTH); + gl2f.glEnable(GL2.GL_LINE_SMOOTH); + gl2f.glEnable(GL2.GL_POLYGON_SMOOTH); + } + } + + + public void noSmooth() { + smooth = false; + if (hints[DISABLE_OPENGL_2X_SMOOTH]) { + gl2f.glDisable(GL2.GL_MULTISAMPLE); + gl2f.glDisable(GL2.GL_POINT_SMOOTH); + gl2f.glDisable(GL.GL_LINE_SMOOTH); + gl2f.glDisable(GL2.GL_POLYGON_SMOOTH); + } + } + + ////////////////////////////////////////////////////////////// + // SHAPE // public void shapeMode(int mode) @@ -3459,14 +3478,7 @@ public class PGraphicsOpenGL2 extends PGraphics { textTex.setTexture(info.texIndex); } - // There is no need to setup orthographic projection or any related matrix set/restore - // operations here because glDrawTexiOES operates on window coordinates: - // "glDrawTexiOES takes window coordinates and bypasses the transform pipeline - // (except for mapping Z to the depth range), so there is no need for any - // matrix setup/restore code." - // (from https://www.khronos.org/message_boards/viewtopic.php?f=4&t=948&p=2553). - //gl11.glTexParameteriv(GL10.GL_TEXTURE_2D, GL11Ext.GL_TEXTURE_CROP_RECT_OES, info.crop, 0); - //gl11x.glDrawTexiOES(xx, height - yy, 0, w0, h0); + drawTexture(info.crop, xx, height - yy, w0, h0); } protected void allocateTextModel() { @@ -3614,6 +3626,7 @@ public class PGraphicsOpenGL2 extends PGraphics { gbuffer.stack.rotate(angle, v0, v1, v2); return; } + gl2f.glRotatef(PApplet.degrees(angle), v0, v1, v2); if (usingGLMatrixStack) { if (matrixMode == PROJECTION) { @@ -4517,9 +4530,7 @@ public class PGraphicsOpenGL2 extends PGraphics { * Print the current projection matrix. */ public void printProjection() { - PMatrix3D temp = new PMatrix3D(); - copyGLArrayToPMatrix(glprojection, temp); - temp.print(); + projection.print(); } ////////////////////////////////////////////////////////////// @@ -4743,10 +4754,10 @@ public class PGraphicsOpenGL2 extends PGraphics { super.fillFromCalc(); calcColorBuffer(); - // A3D uses GL_COLOR_MATERIAL mode, so the ambient and diffuse components - // for all vertices are taken from the glColor/color buffer settings. - //gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_AMBIENT_AND_DIFFUSE, - // colorFloats, 0); + // OPENGL2 uses GL_COLOR_MATERIAL mode, so the ambient and diffuse components + // for all vertices are taken from the glColor/color buffer settings and we don't + // need this: + //gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_AMBIENT_AND_DIFFUSE, colorFloats, 0); } protected void setFillColor() { @@ -4783,7 +4794,7 @@ public class PGraphicsOpenGL2 extends PGraphics { super.ambientFromCalc(); calcColorBuffer(); - // A3D uses GL_COLOR_MATERIAL mode, so the ambient and diffuse components + // OPENGL uses GL_COLOR_MATERIAL mode, so the ambient and diffuse components // for all vertices are taken from the glColor/color buffer settings. gl2f.glMaterialfv(GL.GL_FRONT_AND_BACK, GL2.GL_AMBIENT, colorFloats, 0); } @@ -5354,6 +5365,23 @@ public class PGraphicsOpenGL2 extends PGraphics { // public boolean displayable() // public boolean dimensional() // from P3D + + + /** + * Return true if this renderer supports 2D drawing. Defaults to true. + */ + public boolean is2D() { + return true; + } + + + /** + * Return true if this renderer supports 2D drawing. Defaults to false. + */ + public boolean is3D() { + return true; + } + ////////////////////////////////////////////////////////////// @@ -5683,6 +5711,10 @@ public class PGraphicsOpenGL2 extends PGraphics { // Draws wherever it is in the screen texture right now to the screen. public void updateTexture() { + drawTexture(); + } + + protected void drawTexture() { drawTexture(texture, texCrop, 0, 0, width, height); } @@ -6273,15 +6305,15 @@ public class PGraphicsOpenGL2 extends PGraphics { * @param img the image to have a texture metadata associated to it */ protected PTexture addTexture(PImage img) { - PTexture.Parameters params = (PTexture.Parameters)img.getParams(this); + PTexture.Parameters params = (PTexture.Parameters)img.getParams(ogl); if (params == null) { params = PTexture.newParameters(); - img.setParams(this, params); + img.setParams(ogl, params); } PTexture tex = new PTexture(img.parent, img.width, img.height, params); img.loadPixels(); tex.set(img.pixels); - img.setCache(this, tex); + img.setCache(ogl, tex); return tex; } @@ -6358,6 +6390,47 @@ public class PGraphicsOpenGL2 extends PGraphics { gl2f.glPopMatrix(); } + /** + * Utility function to render currently bound using current blend mode. Equivalent to: + * glTexParameteriv(GL10.GL_TEXTURE_2D, GL11Ext.GL_TEXTURE_CROP_RECT_OES, crop, 0); + * glDrawTexiOES(x, y, 0, w, h); + * in OpenGL ES. + */ + protected void drawTexture(int[] crop, int x, int y, int w, int h) { + gl.glViewport(0, 0, w, h); + + gl2f.glMatrixMode(GL2.GL_PROJECTION); + gl2f.glPushMatrix(); + gl2f.glLoadIdentity(); + + gl2f.glOrthof(0, w, 0, h, -1, 1); + + gl2f.glMatrixMode(GL2.GL_MODELVIEW); + gl2f.glPushMatrix(); + gl2f.glLoadIdentity(); + + gl2f.glTranslatef(x, y, 0); + gl2f.glScalef(w, h, 1); + // Rendering the quad with the appropriate texture coordinates needed for the + // specified crop region + renderTexQuad(crop[0] / w, crop[1] / h, (crop[0] + crop[2]) / w, (crop[1] + crop[3]) / h); + + if (hints[DISABLE_DEPTH_MASK]) { + gl.glDepthMask(false); + } else { + gl.glDepthMask(true); + } + + // Restoring viewport. + gl.glViewport(0, 0, width, height); + + // Restoring matrices. + gl2f.glMatrixMode(GL2.GL_PROJECTION); + gl2f.glPopMatrix(); + gl2f.glMatrixMode(GL2.GL_MODELVIEW); + gl2f.glPopMatrix(); + } + protected void allocateTexQuad() { ByteBuffer vbb = ByteBuffer.allocateDirect(4 * 3 * SIZEOF_FLOAT); vbb.order(ByteOrder.nativeOrder()); @@ -6730,6 +6803,7 @@ public class PGraphicsOpenGL2 extends PGraphics { glparamsRead = true; } + ////////////////////////////////////////////////////////////// // UTILITY INNER CLASSES @@ -7143,7 +7217,8 @@ public class PGraphicsOpenGL2 extends PGraphics { public GLMatrixStack() { matrixStack = new Stack(); - current = new float[16]; + current = new float[16]; + setIdentity(); } public void setIdentity() { diff --git a/java/libraries/opengl2/src/processing/opengl2/PTexture.java b/java/libraries/opengl2/src/processing/opengl2/PTexture.java index 0fcfbfb10..b7ec36da9 100644 --- a/java/libraries/opengl2/src/processing/opengl2/PTexture.java +++ b/java/libraries/opengl2/src/processing/opengl2/PTexture.java @@ -40,7 +40,7 @@ public class PTexture implements PConstants { public int width, height; protected PApplet parent; - protected PGraphicsOpenGL2 pgl; + protected PGraphicsOpenGL2 ogl; protected GL gl; protected int glID; @@ -94,8 +94,8 @@ public class PTexture implements PConstants { this.width = width; this.height = height; - pgl = (PGraphicsOpenGL2)parent.g; - gl = pgl.gl; + ogl = (PGraphicsOpenGL2)parent.g; + gl = ogl.gl; glID = 0; @@ -123,8 +123,8 @@ public class PTexture implements PConstants { public PTexture(PApplet parent, String filename, Object params) { this.parent = parent; - pgl = (PGraphicsOpenGL2)parent.g; - gl = pgl.gl; + ogl = (PGraphicsOpenGL2)parent.g; + gl = ogl.gl; glID = 0; @@ -206,13 +206,13 @@ public class PTexture implements PConstants { public void set(PImage img) { - PTexture tex = (PTexture)img.getCache(pgl); + PTexture tex = (PTexture)img.getCache(ogl); set(tex); } public void set(PImage img, int x, int y, int w, int h) { - PTexture tex = (PTexture)img.getCache(pgl); + PTexture tex = (PTexture)img.getCache(ogl); set(tex, x, y, w, h); } @@ -306,18 +306,18 @@ public class PTexture implements PConstants { // Attaching the texture to the color buffer of a FBO, binding the FBO and reading the pixels // from the current draw buffer (which is the color buffer of the FBO). tempFbo.setColorBuffer(this); - pgl.pushFramebuffer(); - pgl.setFramebuffer(tempFbo); + ogl.pushFramebuffer(); + ogl.setFramebuffer(tempFbo); tempFbo.readPixels(); - pgl.popFramebuffer(); + ogl.popFramebuffer(); } else { // Here we don't have FBOs, so the method above is of no use. What we do instead is // to draw the texture to the screen framebuffer, and then grab the pixels from there. - pgl.pushFramebuffer(); - pgl.setFramebuffer(tempFbo); - pgl.drawTexture(this, 0, 0, glWidth, glHeight, 0, 0, glWidth, glHeight); + ogl.pushFramebuffer(); + ogl.setFramebuffer(tempFbo); + ogl.drawTexture(this, 0, 0, glWidth, glHeight, 0, 0, glWidth, glHeight); tempFbo.readPixels(); - pgl.popFramebuffer(); + ogl.popFramebuffer(); } if (tempPixels == null) { @@ -763,7 +763,7 @@ public class PTexture implements PConstants { usingMipmaps = glMinFilter == GL.GL_LINEAR_MIPMAP_LINEAR; gl.glEnable(glTarget); - glID = pgl.createGLResource(PGraphicsOpenGL2.GL_TEXTURE_OBJECT); + glID = ogl.createGLResource(PGraphicsOpenGL2.GL_TEXTURE_OBJECT); gl.glBindTexture(glTarget, glID); gl.glTexParameteri(glTarget, GL.GL_TEXTURE_MIN_FILTER, glMinFilter); gl.glTexParameteri(glTarget, GL.GL_TEXTURE_MAG_FILTER, glMagFilter); @@ -796,7 +796,7 @@ public class PTexture implements PConstants { */ protected void deleteTexture() { if (glID != 0) { - pgl.deleteGLResource(glID, PGraphicsOpenGL2.GL_TEXTURE_OBJECT); + ogl.deleteGLResource(glID, PGraphicsOpenGL2.GL_TEXTURE_OBJECT); glID = 0; } } @@ -816,19 +816,19 @@ public class PTexture implements PConstants { tempFbo.disableDepthTest(); // FBO copy: - pgl.pushFramebuffer(); - pgl.setFramebuffer(tempFbo); + ogl.pushFramebuffer(); + ogl.setFramebuffer(tempFbo); if (scale) { // Rendering tex into "this", and scaling the source rectangle // to cover the entire destination region. - pgl.drawTexture(tex, x, y, w, h, 0, 0, width, height); + ogl.drawTexture(tex, x, y, w, h, 0, 0, width, height); } else { // Rendering tex into "this" but without scaling so the contents // of the source texture fall in the corresponding texels of the // destination. - pgl.drawTexture(tex, x, y, w, h, x, y, w, h); + ogl.drawTexture(tex, x, y, w, h, x, y, w, h); } - pgl.popFramebuffer(); + ogl.popFramebuffer(); } protected void setTexels(int x, int y, int w, int h, int[] pix) { @@ -858,7 +858,7 @@ public class PTexture implements PConstants { height = src.height; parent = src.parent; - pgl = src.pgl; + ogl = src.ogl; gl = src.gl; glID = src.glID; @@ -931,7 +931,7 @@ public class PTexture implements PConstants { if (params.target == TEXTURE2D) { glTarget = GL.GL_TEXTURE_2D; } else { - throw new RuntimeException("GTexture: Unknown texture target"); + throw new RuntimeException("OPENGL2: Unknown texture target"); } if (params.format == RGB) { @@ -941,7 +941,7 @@ public class PTexture implements PConstants { } else if (params.format == ALPHA) { glFormat = GL.GL_ALPHA; } else { - throw new RuntimeException("GTexture: Unknown texture format"); + throw new RuntimeException("OPENGL2: Unknown texture format"); } if (params.sampling == POINT) { @@ -954,7 +954,7 @@ public class PTexture implements PConstants { glMagFilter = GL.GL_LINEAR; glMinFilter = GL.GL_LINEAR_MIPMAP_LINEAR; } else { - throw new RuntimeException("GTexture: Unknown texture filtering mode"); + throw new RuntimeException("OPENGL2: Unknown texture filtering mode"); } if (params.wrapU == CLAMP) { @@ -962,7 +962,7 @@ public class PTexture implements PConstants { } else if (params.wrapU == REPEAT) { glWrapS = GL.GL_REPEAT; } else { - throw new RuntimeException("GTexture: Unknown wrapping mode"); + throw new RuntimeException("OPENGL2: Unknown wrapping mode"); } if (params.wrapV == CLAMP) { @@ -970,7 +970,7 @@ public class PTexture implements PConstants { } else if (params.wrapV == REPEAT) { glWrapT = GL.GL_REPEAT; } else { - throw new RuntimeException("GTexture: Unknown wrapping mode"); + throw new RuntimeException("OPENGL2: Unknown wrapping mode"); } }