From 2acd81befead34b7aca4bb73922c5fa401cb97dc Mon Sep 17 00:00:00 2001 From: codeanticode Date: Thu, 30 Aug 2012 21:32:57 +0000 Subject: [PATCH] perspective correction can be disabled/enabled for points --- core/src/processing/core/PConstants.java | 46 +++++++++---------- core/src/processing/opengl/PGraphics2D.java | 4 +- core/src/processing/opengl/PGraphics3D.java | 5 ++ .../processing/opengl/PGraphicsOpenGL.java | 29 +++++++++--- .../processing/opengl/PointShaderVert.glsl | 21 ++++++++- 5 files changed, 72 insertions(+), 33 deletions(-) diff --git a/core/src/processing/core/PConstants.java b/core/src/processing/core/PConstants.java index 6b8555670..5a52d9d46 100644 --- a/core/src/processing/core/PConstants.java +++ b/core/src/processing/core/PConstants.java @@ -510,40 +510,40 @@ public interface PConstants { // hints - hint values are positive for the alternate version, // negative of the same value returns to the normal/default state - static final int ENABLE_NATIVE_FONTS = 1; - static final int DISABLE_NATIVE_FONTS = -1; + static final int ENABLE_NATIVE_FONTS = 1; + static final int DISABLE_NATIVE_FONTS = -1; - static final int DISABLE_DEPTH_TEST = 2; - static final int ENABLE_DEPTH_TEST = -2; + static final int DISABLE_DEPTH_TEST = 2; + static final int ENABLE_DEPTH_TEST = -2; - static final int ENABLE_DEPTH_SORT = 3; - static final int DISABLE_DEPTH_SORT = -3; + static final int ENABLE_DEPTH_SORT = 3; + static final int DISABLE_DEPTH_SORT = -3; - static final int DISABLE_OPENGL_ERROR_REPORT = 4; - static final int ENABLE_OPENGL_ERROR_REPORT = -4; + static final int DISABLE_OPENGL_ERROR_REPORT = 4; + static final int ENABLE_OPENGL_ERROR_REPORT = -4; - static final int ENABLE_ACCURATE_TEXTURES = 5; - static final int DISABLE_ACCURATE_TEXTURES = -5; + static final int ENABLE_ACCURATE_TEXTURES = 5; + static final int DISABLE_ACCURATE_TEXTURES = -5; - static final int DISABLE_DEPTH_MASK = 6; - static final int ENABLE_DEPTH_MASK = -6; + static final int DISABLE_DEPTH_MASK = 6; + static final int ENABLE_DEPTH_MASK = -6; - static final int ENABLE_ACCURATE_2D = 7; - static final int DISABLE_ACCURATE_2D = -7; + static final int ENABLE_ACCURATE_2D = 7; + static final int DISABLE_ACCURATE_2D = -7; - static final int DISABLE_TEXTURE_CACHE = 8; - static final int ENABLE_TEXTURE_CACHE = -8; + static final int DISABLE_TEXTURE_CACHE = 8; + static final int ENABLE_TEXTURE_CACHE = -8; - static final int DISABLE_TRANSFORM_CACHE = 9; - static final int ENABLE_TRANSFORM_CACHE = -9; + static final int DISABLE_TRANSFORM_CACHE = 9; + static final int ENABLE_TRANSFORM_CACHE = -9; - static final int ENABLE_PERSPECTIVE_CORRECTED_LINES = 10; - static final int DISABLE_PERSPECTIVE_CORRECTED_LINES = -10; + static final int ENABLE_PERSPECTIVE_CORRECTED_STROKE = 10; + static final int DISABLE_PERSPECTIVE_CORRECTED_STROKE = -10; - static final int DISABLE_TEXTURE_MIPMAPS = 11; - static final int ENABLE_TEXTURE_MIPMAPS = -11; + static final int DISABLE_TEXTURE_MIPMAPS = 11; + static final int ENABLE_TEXTURE_MIPMAPS = -11; - static final int HINT_COUNT = 12; + static final int HINT_COUNT = 12; // error messages diff --git a/core/src/processing/opengl/PGraphics2D.java b/core/src/processing/opengl/PGraphics2D.java index eb05cfbac..7e1ab16ca 100644 --- a/core/src/processing/opengl/PGraphics2D.java +++ b/core/src/processing/opengl/PGraphics2D.java @@ -36,7 +36,7 @@ public class PGraphics2D extends PGraphicsOpenGL { public PGraphics2D() { super(); - hints[ENABLE_PERSPECTIVE_CORRECTED_LINES] = false; + hints[ENABLE_PERSPECTIVE_CORRECTED_STROKE] = false; } @@ -61,7 +61,7 @@ public class PGraphics2D extends PGraphicsOpenGL { public void hint(int which) { - if (which == ENABLE_PERSPECTIVE_CORRECTED_LINES) { + if (which == ENABLE_PERSPECTIVE_CORRECTED_STROKE) { showWarning("2D lines cannot be perspective-corrected."); return; } diff --git a/core/src/processing/opengl/PGraphics3D.java b/core/src/processing/opengl/PGraphics3D.java index e93fdc29b..514cfa8cb 100644 --- a/core/src/processing/opengl/PGraphics3D.java +++ b/core/src/processing/opengl/PGraphics3D.java @@ -34,6 +34,11 @@ import processing.core.PVector; public class PGraphics3D extends PGraphicsOpenGL { + public PGraphics3D() { + super(); + hints[ENABLE_PERSPECTIVE_CORRECTED_STROKE] = true; + } + ////////////////////////////////////////////////////////////// // RENDERER SUPPORT QUERIES diff --git a/core/src/processing/opengl/PGraphicsOpenGL.java b/core/src/processing/opengl/PGraphicsOpenGL.java index c9113ebd9..60ff4447a 100644 --- a/core/src/processing/opengl/PGraphicsOpenGL.java +++ b/core/src/processing/opengl/PGraphicsOpenGL.java @@ -2093,12 +2093,12 @@ public class PGraphicsOpenGL extends PGraphics { setFlushMode(FLUSH_CONTINUOUSLY); } else if (which == DISABLE_TEXTURE_CACHE) { flush(); - } else if (which == DISABLE_PERSPECTIVE_CORRECTED_LINES) { + } else if (which == DISABLE_PERSPECTIVE_CORRECTED_STROKE) { if (0 < tessGeo.lineVertexCount && 0 < tessGeo.lineIndexCount) { // We flush the geometry using the previous line setting. flush(); } - } else if (which == ENABLE_PERSPECTIVE_CORRECTED_LINES) { + } else if (which == ENABLE_PERSPECTIVE_CORRECTED_STROKE) { if (0 < tessGeo.lineVertexCount && 0 < tessGeo.lineIndexCount) { // We flush the geometry using the previous line setting. flush(); @@ -5447,9 +5447,8 @@ public class PGraphicsOpenGL extends PGraphics { } if (maskShader == null) { - maskShader = new PolyTexShader(parent, - defPolyTexShaderVertURL, - maskShaderFragURL); + maskShader = new PolyTexShader(parent, defPolyTexShaderVertURL, + maskShaderFragURL); } maskShader.set("maskSampler", alpha); filter(maskShader); @@ -6846,7 +6845,7 @@ public class PGraphicsOpenGL extends PGraphics { float h = pgCurrent.viewport[3]; setUniformValue(viewportLoc, x, y, w, h); - if (pgCurrent.hintEnabled(ENABLE_PERSPECTIVE_CORRECTED_LINES)) { + if (pgCurrent.hintEnabled(ENABLE_PERSPECTIVE_CORRECTED_STROKE)) { setUniformValue(perspectiveLoc, 1); } else { setUniformValue(perspectiveLoc, 0); @@ -6880,6 +6879,9 @@ public class PGraphicsOpenGL extends PGraphics { protected int modelviewMatrixLoc; protected int projectionMatrixLoc; + protected int viewportLoc; + protected int perspectiveLoc; + protected int inVertexLoc; protected int inColorLoc; protected int inPointLoc; @@ -6907,6 +6909,9 @@ public class PGraphicsOpenGL extends PGraphics { projmodelviewMatrixLoc = getUniformLoc("projmodelviewMatrix"); modelviewMatrixLoc = getUniformLoc("modelviewMatrix"); projectionMatrixLoc = getUniformLoc("projectionMatrix"); + + viewportLoc = getUniformLoc("viewport"); + perspectiveLoc = getUniformLoc("perspective"); } public void setVertexAttribute(int vboId, int size, int type, @@ -6950,6 +6955,18 @@ public class PGraphicsOpenGL extends PGraphics { pgCurrent.updateGLProjection(); setUniformMatrix(projectionMatrixLoc, pgCurrent.glProjection); } + + float x = pgCurrent.viewport[0]; + float y = pgCurrent.viewport[1]; + float w = pgCurrent.viewport[2]; + float h = pgCurrent.viewport[3]; + setUniformValue(viewportLoc, x, y, w, h); + + if (pgCurrent.hintEnabled(ENABLE_PERSPECTIVE_CORRECTED_STROKE)) { + setUniformValue(perspectiveLoc, 1); + } else { + setUniformValue(perspectiveLoc, 0); + } } public void unbind() { diff --git a/core/src/processing/opengl/PointShaderVert.glsl b/core/src/processing/opengl/PointShaderVert.glsl index bf393ed15..25b226b5b 100644 --- a/core/src/processing/opengl/PointShaderVert.glsl +++ b/core/src/processing/opengl/PointShaderVert.glsl @@ -21,16 +21,33 @@ uniform mat4 projectionMatrix; uniform mat4 modelviewMatrix; +uniform vec4 viewport; +uniform int perspective; + attribute vec4 inVertex; attribute vec4 inColor; attribute vec2 inPoint; varying vec4 vertColor; +vec4 windowToClipVector(vec2 window, vec4 viewport, float clip_w) { + vec2 xypos = (window / viewport.zw) * 2.0; + return vec4(xypos, 0.0, 0.0) * clip_w; +} + void main() { vec4 pos = modelviewMatrix * inVertex; - pos.xy += inPoint.xy; - gl_Position = projectionMatrix * pos; + vec4 clip = projectionMatrix * pos; + + if (0 < perspective) { + // Perspective correction (points will look thiner as they move away + // from the view position). + gl_Position = clip + projectionMatrix * vec4(inPoint.xy, 0, 0); + } else { + // No perspective correction. + vec4 offset = windowToClipVector(inPoint.xy, viewport, clip.w); + gl_Position = clip + offset; + } vertColor = inColor; } \ No newline at end of file