From 02093a8aa03dc2941fe9e562a56cacce5d5a54c1 Mon Sep 17 00:00:00 2001 From: codeanticode Date: Tue, 19 Jun 2012 17:28:41 +0000 Subject: [PATCH] z-displacement in line shader disabled in accurate 2d mode --- .../src/processing/opengl/LineShaderVert.glsl | 7 ++- .../processing/opengl/PGraphicsOpenGL.java | 56 +++++++++---------- .../src/processing/opengl/LineShaderVert.glsl | 7 ++- .../processing/opengl/PGraphicsOpenGL.java | 56 +++++++++---------- 4 files changed, 58 insertions(+), 68 deletions(-) diff --git a/android/core/src/processing/opengl/LineShaderVert.glsl b/android/core/src/processing/opengl/LineShaderVert.glsl index ca7d07293..e7d411346 100644 --- a/android/core/src/processing/opengl/LineShaderVert.glsl +++ b/android/core/src/processing/opengl/LineShaderVert.glsl @@ -23,6 +23,7 @@ uniform mat4 projectionMatrix; uniform vec4 viewport; uniform int perspective; +uniform float zfactor; attribute vec4 inVertex; attribute vec4 inColor; @@ -44,18 +45,18 @@ vec4 windowToClipVector(vec2 window, vec4 viewport, float clip_w) { void main() { vec4 pos_p = inVertex; vec4 v_p = modelviewMatrix * pos_p; - // Moving vertices slightly toward the camera + // Moving vertices slightly toward the camera (if zfactor < 1) // to avoid depth-fighting with the fill triangles. // Discussed here: // http://www.opengl.org/discussion_boards/ubbthreads.php?ubb=showflat&Number=252848 - v_p.xyz = v_p.xyz * 0.99; + v_p.xyz = v_p.xyz * zfactor; vec4 clip_p = projectionMatrix * v_p; float thickness = inLine.w; if (thickness != 0.0) { vec4 pos_q = vec4(inLine.xyz, 1); vec4 v_q = modelviewMatrix * pos_q; - v_q.xyz = v_q.xyz * 0.99; + v_q.xyz = v_q.xyz * zfactor; vec4 clip_q = projectionMatrix * v_q; vec3 window_p = clipToWindow(clip_p, viewport); diff --git a/android/core/src/processing/opengl/PGraphicsOpenGL.java b/android/core/src/processing/opengl/PGraphicsOpenGL.java index fb4b6470c..1e44720e8 100644 --- a/android/core/src/processing/opengl/PGraphicsOpenGL.java +++ b/android/core/src/processing/opengl/PGraphicsOpenGL.java @@ -385,8 +385,6 @@ public class PGraphicsOpenGL extends PGraphics { static protected final int EDGE_START = 1; static protected final int EDGE_STOP = 2; static protected final int EDGE_SINGLE = 3; - - protected boolean perspectiveCorrectedLines = false; /** Used in round point and ellipse tessellation. The * number of subdivisions per round point or ellipse is @@ -1555,8 +1553,6 @@ public class PGraphicsOpenGL extends PGraphics { // The current normal vector is set to be parallel to the Z axis. normalX = normalY = normalZ = 0; - perspectiveCorrectedLines = hints[ENABLE_PERSPECTIVE_CORRECTED_LINES]; - // Clear depth and stencil buffers. pgl.glDepthMask(true); pgl.glClearColor(0, 0, 0, 0); @@ -1970,13 +1966,11 @@ public class PGraphicsOpenGL extends PGraphics { // We flush the geometry using the previous line setting. flush(); } - perspectiveCorrectedLines = false; } else if (which == ENABLE_PERSPECTIVE_CORRECTED_LINES) { if (0 < tessGeo.lineVertexCount && 0 < tessGeo.lineIndexCount) { // We flush the geometry using the previous line setting. flush(); } - perspectiveCorrectedLines = true; } } @@ -5993,13 +5987,12 @@ public class PGraphicsOpenGL extends PGraphics { protected class LineShader extends PShader { - protected PGraphicsOpenGL renderer; - protected int projectionMatrixLoc; protected int modelviewMatrixLoc; protected int viewportLoc; protected int perspectiveLoc; + protected int zfactorLoc; protected int inVertexLoc; protected int inColorLoc; @@ -6017,10 +6010,6 @@ public class PGraphicsOpenGL extends PGraphics { super(parent, vertURL, fragURL); } - public void setRenderer(PGraphicsOpenGL pg) { - this.renderer = pg; - } - public void loadAttributes() { inVertexLoc = getAttribLocation("inVertex"); inColorLoc = getAttribLocation("inColor"); @@ -6033,6 +6022,7 @@ public class PGraphicsOpenGL extends PGraphics { viewportLoc = getUniformLocation("viewport"); perspectiveLoc = getUniformLocation("perspective"); + zfactorLoc = getUniformLocation("zfactor"); } public void setAttribute(int loc, int vboId, int size, int type, boolean normalized, int stride, int offset) { @@ -6059,17 +6049,27 @@ public class PGraphicsOpenGL extends PGraphics { if (-1 < inColorLoc) pgl.glEnableVertexAttribArray(inColorLoc); if (-1 < inAttribLoc) pgl.glEnableVertexAttribArray(inAttribLoc); - if (renderer != null) { - renderer.updateGLProjection(); - set4x4MatUniform(projectionMatrixLoc, renderer.glProjection); + if (pgCurrent != null) { + pgCurrent.updateGLProjection(); + set4x4MatUniform(projectionMatrixLoc, pgCurrent.glProjection); - renderer.updateGLModelview(); - set4x4MatUniform(modelviewMatrixLoc, renderer.glModelview); + pgCurrent.updateGLModelview(); + set4x4MatUniform(modelviewMatrixLoc, pgCurrent.glModelview); - set4FloatUniform(viewportLoc, renderer.viewport[0], renderer.viewport[1], renderer.viewport[2], renderer.viewport[3]); + set4FloatUniform(viewportLoc, pgCurrent.viewport[0], pgCurrent.viewport[1], pgCurrent.viewport[2], pgCurrent.viewport[3]); + + if (pgCurrent.hintEnabled(ENABLE_PERSPECTIVE_CORRECTED_LINES)) { + setIntUniform(perspectiveLoc, 1); + } else { + setIntUniform(perspectiveLoc, 0); + } + + if (pgCurrent.hintEnabled(ENABLE_ACCURATE_2D)) { + set1FloatUniform(zfactorLoc, 1); + } else { + set1FloatUniform(zfactorLoc, 0.99f); + } } - - setIntUniform(perspectiveLoc, perspectiveCorrectedLines ? 1 : 0); } public void stop() { @@ -6085,8 +6085,6 @@ public class PGraphicsOpenGL extends PGraphics { protected class PointShader extends PShader { - protected PGraphicsOpenGL renderer; - protected int projectionMatrixLoc; protected int modelviewMatrixLoc; @@ -6106,10 +6104,6 @@ public class PGraphicsOpenGL extends PGraphics { super(parent, vertURL, fragURL); } - public void setRenderer(PGraphicsOpenGL pg) { - this.renderer = pg; - } - public void loadAttributes() { inVertexLoc = getAttribLocation("inVertex"); inColorLoc = getAttribLocation("inColor"); @@ -6145,12 +6139,12 @@ public class PGraphicsOpenGL extends PGraphics { if (-1 < inColorLoc) pgl.glEnableVertexAttribArray(inColorLoc); if (-1 < inPointLoc) pgl.glEnableVertexAttribArray(inPointLoc); - if (renderer != null) { - renderer.updateGLProjection(); - set4x4MatUniform(projectionMatrixLoc, renderer.glProjection); + if (pgCurrent != null) { + pgCurrent.updateGLProjection(); + set4x4MatUniform(projectionMatrixLoc, pgCurrent.glProjection); - renderer.updateGLModelview(); - set4x4MatUniform(modelviewMatrixLoc, renderer.glModelview); + pgCurrent.updateGLModelview(); + set4x4MatUniform(modelviewMatrixLoc, pgCurrent.glModelview); } } diff --git a/java/libraries/opengl/src/processing/opengl/LineShaderVert.glsl b/java/libraries/opengl/src/processing/opengl/LineShaderVert.glsl index ca7d07293..e7d411346 100644 --- a/java/libraries/opengl/src/processing/opengl/LineShaderVert.glsl +++ b/java/libraries/opengl/src/processing/opengl/LineShaderVert.glsl @@ -23,6 +23,7 @@ uniform mat4 projectionMatrix; uniform vec4 viewport; uniform int perspective; +uniform float zfactor; attribute vec4 inVertex; attribute vec4 inColor; @@ -44,18 +45,18 @@ vec4 windowToClipVector(vec2 window, vec4 viewport, float clip_w) { void main() { vec4 pos_p = inVertex; vec4 v_p = modelviewMatrix * pos_p; - // Moving vertices slightly toward the camera + // Moving vertices slightly toward the camera (if zfactor < 1) // to avoid depth-fighting with the fill triangles. // Discussed here: // http://www.opengl.org/discussion_boards/ubbthreads.php?ubb=showflat&Number=252848 - v_p.xyz = v_p.xyz * 0.99; + v_p.xyz = v_p.xyz * zfactor; vec4 clip_p = projectionMatrix * v_p; float thickness = inLine.w; if (thickness != 0.0) { vec4 pos_q = vec4(inLine.xyz, 1); vec4 v_q = modelviewMatrix * pos_q; - v_q.xyz = v_q.xyz * 0.99; + v_q.xyz = v_q.xyz * zfactor; vec4 clip_q = projectionMatrix * v_q; vec3 window_p = clipToWindow(clip_p, viewport); diff --git a/java/libraries/opengl/src/processing/opengl/PGraphicsOpenGL.java b/java/libraries/opengl/src/processing/opengl/PGraphicsOpenGL.java index fb4b6470c..1e44720e8 100644 --- a/java/libraries/opengl/src/processing/opengl/PGraphicsOpenGL.java +++ b/java/libraries/opengl/src/processing/opengl/PGraphicsOpenGL.java @@ -385,8 +385,6 @@ public class PGraphicsOpenGL extends PGraphics { static protected final int EDGE_START = 1; static protected final int EDGE_STOP = 2; static protected final int EDGE_SINGLE = 3; - - protected boolean perspectiveCorrectedLines = false; /** Used in round point and ellipse tessellation. The * number of subdivisions per round point or ellipse is @@ -1555,8 +1553,6 @@ public class PGraphicsOpenGL extends PGraphics { // The current normal vector is set to be parallel to the Z axis. normalX = normalY = normalZ = 0; - perspectiveCorrectedLines = hints[ENABLE_PERSPECTIVE_CORRECTED_LINES]; - // Clear depth and stencil buffers. pgl.glDepthMask(true); pgl.glClearColor(0, 0, 0, 0); @@ -1970,13 +1966,11 @@ public class PGraphicsOpenGL extends PGraphics { // We flush the geometry using the previous line setting. flush(); } - perspectiveCorrectedLines = false; } else if (which == ENABLE_PERSPECTIVE_CORRECTED_LINES) { if (0 < tessGeo.lineVertexCount && 0 < tessGeo.lineIndexCount) { // We flush the geometry using the previous line setting. flush(); } - perspectiveCorrectedLines = true; } } @@ -5993,13 +5987,12 @@ public class PGraphicsOpenGL extends PGraphics { protected class LineShader extends PShader { - protected PGraphicsOpenGL renderer; - protected int projectionMatrixLoc; protected int modelviewMatrixLoc; protected int viewportLoc; protected int perspectiveLoc; + protected int zfactorLoc; protected int inVertexLoc; protected int inColorLoc; @@ -6017,10 +6010,6 @@ public class PGraphicsOpenGL extends PGraphics { super(parent, vertURL, fragURL); } - public void setRenderer(PGraphicsOpenGL pg) { - this.renderer = pg; - } - public void loadAttributes() { inVertexLoc = getAttribLocation("inVertex"); inColorLoc = getAttribLocation("inColor"); @@ -6033,6 +6022,7 @@ public class PGraphicsOpenGL extends PGraphics { viewportLoc = getUniformLocation("viewport"); perspectiveLoc = getUniformLocation("perspective"); + zfactorLoc = getUniformLocation("zfactor"); } public void setAttribute(int loc, int vboId, int size, int type, boolean normalized, int stride, int offset) { @@ -6059,17 +6049,27 @@ public class PGraphicsOpenGL extends PGraphics { if (-1 < inColorLoc) pgl.glEnableVertexAttribArray(inColorLoc); if (-1 < inAttribLoc) pgl.glEnableVertexAttribArray(inAttribLoc); - if (renderer != null) { - renderer.updateGLProjection(); - set4x4MatUniform(projectionMatrixLoc, renderer.glProjection); + if (pgCurrent != null) { + pgCurrent.updateGLProjection(); + set4x4MatUniform(projectionMatrixLoc, pgCurrent.glProjection); - renderer.updateGLModelview(); - set4x4MatUniform(modelviewMatrixLoc, renderer.glModelview); + pgCurrent.updateGLModelview(); + set4x4MatUniform(modelviewMatrixLoc, pgCurrent.glModelview); - set4FloatUniform(viewportLoc, renderer.viewport[0], renderer.viewport[1], renderer.viewport[2], renderer.viewport[3]); + set4FloatUniform(viewportLoc, pgCurrent.viewport[0], pgCurrent.viewport[1], pgCurrent.viewport[2], pgCurrent.viewport[3]); + + if (pgCurrent.hintEnabled(ENABLE_PERSPECTIVE_CORRECTED_LINES)) { + setIntUniform(perspectiveLoc, 1); + } else { + setIntUniform(perspectiveLoc, 0); + } + + if (pgCurrent.hintEnabled(ENABLE_ACCURATE_2D)) { + set1FloatUniform(zfactorLoc, 1); + } else { + set1FloatUniform(zfactorLoc, 0.99f); + } } - - setIntUniform(perspectiveLoc, perspectiveCorrectedLines ? 1 : 0); } public void stop() { @@ -6085,8 +6085,6 @@ public class PGraphicsOpenGL extends PGraphics { protected class PointShader extends PShader { - protected PGraphicsOpenGL renderer; - protected int projectionMatrixLoc; protected int modelviewMatrixLoc; @@ -6106,10 +6104,6 @@ public class PGraphicsOpenGL extends PGraphics { super(parent, vertURL, fragURL); } - public void setRenderer(PGraphicsOpenGL pg) { - this.renderer = pg; - } - public void loadAttributes() { inVertexLoc = getAttribLocation("inVertex"); inColorLoc = getAttribLocation("inColor"); @@ -6145,12 +6139,12 @@ public class PGraphicsOpenGL extends PGraphics { if (-1 < inColorLoc) pgl.glEnableVertexAttribArray(inColorLoc); if (-1 < inPointLoc) pgl.glEnableVertexAttribArray(inPointLoc); - if (renderer != null) { - renderer.updateGLProjection(); - set4x4MatUniform(projectionMatrixLoc, renderer.glProjection); + if (pgCurrent != null) { + pgCurrent.updateGLProjection(); + set4x4MatUniform(projectionMatrixLoc, pgCurrent.glProjection); - renderer.updateGLModelview(); - set4x4MatUniform(modelviewMatrixLoc, renderer.glModelview); + pgCurrent.updateGLModelview(); + set4x4MatUniform(modelviewMatrixLoc, pgCurrent.glModelview); } }