Skip to content

Commit b335274

Browse files
committed
perspective correction can be disabled/enabled for points
1 parent 3f7ea95 commit b335274

File tree

5 files changed

+72
-33
lines changed

5 files changed

+72
-33
lines changed

core/src/processing/core/PConstants.java

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -510,40 +510,40 @@ public interface PConstants {
510510
// hints - hint values are positive for the alternate version,
511511
// negative of the same value returns to the normal/default state
512512

513-
static final int ENABLE_NATIVE_FONTS = 1;
514-
static final int DISABLE_NATIVE_FONTS = -1;
513+
static final int ENABLE_NATIVE_FONTS = 1;
514+
static final int DISABLE_NATIVE_FONTS = -1;
515515

516-
static final int DISABLE_DEPTH_TEST = 2;
517-
static final int ENABLE_DEPTH_TEST = -2;
516+
static final int DISABLE_DEPTH_TEST = 2;
517+
static final int ENABLE_DEPTH_TEST = -2;
518518

519-
static final int ENABLE_DEPTH_SORT = 3;
520-
static final int DISABLE_DEPTH_SORT = -3;
519+
static final int ENABLE_DEPTH_SORT = 3;
520+
static final int DISABLE_DEPTH_SORT = -3;
521521

522-
static final int DISABLE_OPENGL_ERROR_REPORT = 4;
523-
static final int ENABLE_OPENGL_ERROR_REPORT = -4;
522+
static final int DISABLE_OPENGL_ERROR_REPORT = 4;
523+
static final int ENABLE_OPENGL_ERROR_REPORT = -4;
524524

525-
static final int ENABLE_ACCURATE_TEXTURES = 5;
526-
static final int DISABLE_ACCURATE_TEXTURES = -5;
525+
static final int ENABLE_ACCURATE_TEXTURES = 5;
526+
static final int DISABLE_ACCURATE_TEXTURES = -5;
527527

528-
static final int DISABLE_DEPTH_MASK = 6;
529-
static final int ENABLE_DEPTH_MASK = -6;
528+
static final int DISABLE_DEPTH_MASK = 6;
529+
static final int ENABLE_DEPTH_MASK = -6;
530530

531-
static final int ENABLE_ACCURATE_2D = 7;
532-
static final int DISABLE_ACCURATE_2D = -7;
531+
static final int ENABLE_ACCURATE_2D = 7;
532+
static final int DISABLE_ACCURATE_2D = -7;
533533

534-
static final int DISABLE_TEXTURE_CACHE = 8;
535-
static final int ENABLE_TEXTURE_CACHE = -8;
534+
static final int DISABLE_TEXTURE_CACHE = 8;
535+
static final int ENABLE_TEXTURE_CACHE = -8;
536536

537-
static final int DISABLE_TRANSFORM_CACHE = 9;
538-
static final int ENABLE_TRANSFORM_CACHE = -9;
537+
static final int DISABLE_TRANSFORM_CACHE = 9;
538+
static final int ENABLE_TRANSFORM_CACHE = -9;
539539

540-
static final int ENABLE_PERSPECTIVE_CORRECTED_LINES = 10;
541-
static final int DISABLE_PERSPECTIVE_CORRECTED_LINES = -10;
540+
static final int ENABLE_PERSPECTIVE_CORRECTED_STROKE = 10;
541+
static final int DISABLE_PERSPECTIVE_CORRECTED_STROKE = -10;
542542

543-
static final int DISABLE_TEXTURE_MIPMAPS = 11;
544-
static final int ENABLE_TEXTURE_MIPMAPS = -11;
543+
static final int DISABLE_TEXTURE_MIPMAPS = 11;
544+
static final int ENABLE_TEXTURE_MIPMAPS = -11;
545545

546-
static final int HINT_COUNT = 12;
546+
static final int HINT_COUNT = 12;
547547

548548
// error messages
549549

core/src/processing/opengl/PGraphics2D.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class PGraphics2D extends PGraphicsOpenGL {
3636

3737
public PGraphics2D() {
3838
super();
39-
hints[ENABLE_PERSPECTIVE_CORRECTED_LINES] = false;
39+
hints[ENABLE_PERSPECTIVE_CORRECTED_STROKE] = false;
4040
}
4141

4242

@@ -61,7 +61,7 @@ public boolean is3D() {
6161

6262

6363
public void hint(int which) {
64-
if (which == ENABLE_PERSPECTIVE_CORRECTED_LINES) {
64+
if (which == ENABLE_PERSPECTIVE_CORRECTED_STROKE) {
6565
showWarning("2D lines cannot be perspective-corrected.");
6666
return;
6767
}

core/src/processing/opengl/PGraphics3D.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@
3434

3535
public class PGraphics3D extends PGraphicsOpenGL {
3636

37+
public PGraphics3D() {
38+
super();
39+
hints[ENABLE_PERSPECTIVE_CORRECTED_STROKE] = true;
40+
}
41+
3742
//////////////////////////////////////////////////////////////
3843

3944
// RENDERER SUPPORT QUERIES

core/src/processing/opengl/PGraphicsOpenGL.java

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2093,12 +2093,12 @@ public void hint(int which) {
20932093
setFlushMode(FLUSH_CONTINUOUSLY);
20942094
} else if (which == DISABLE_TEXTURE_CACHE) {
20952095
flush();
2096-
} else if (which == DISABLE_PERSPECTIVE_CORRECTED_LINES) {
2096+
} else if (which == DISABLE_PERSPECTIVE_CORRECTED_STROKE) {
20972097
if (0 < tessGeo.lineVertexCount && 0 < tessGeo.lineIndexCount) {
20982098
// We flush the geometry using the previous line setting.
20992099
flush();
21002100
}
2101-
} else if (which == ENABLE_PERSPECTIVE_CORRECTED_LINES) {
2101+
} else if (which == ENABLE_PERSPECTIVE_CORRECTED_STROKE) {
21022102
if (0 < tessGeo.lineVertexCount && 0 < tessGeo.lineIndexCount) {
21032103
// We flush the geometry using the previous line setting.
21042104
flush();
@@ -5447,9 +5447,8 @@ public void mask(PImage alpha) {
54475447
}
54485448

54495449
if (maskShader == null) {
5450-
maskShader = new PolyTexShader(parent,
5451-
defPolyTexShaderVertURL,
5452-
maskShaderFragURL);
5450+
maskShader = new PolyTexShader(parent, defPolyTexShaderVertURL,
5451+
maskShaderFragURL);
54535452
}
54545453
maskShader.set("maskSampler", alpha);
54555454
filter(maskShader);
@@ -6846,7 +6845,7 @@ public void bind() {
68466845
float h = pgCurrent.viewport[3];
68476846
setUniformValue(viewportLoc, x, y, w, h);
68486847

6849-
if (pgCurrent.hintEnabled(ENABLE_PERSPECTIVE_CORRECTED_LINES)) {
6848+
if (pgCurrent.hintEnabled(ENABLE_PERSPECTIVE_CORRECTED_STROKE)) {
68506849
setUniformValue(perspectiveLoc, 1);
68516850
} else {
68526851
setUniformValue(perspectiveLoc, 0);
@@ -6880,6 +6879,9 @@ protected class PointShader extends PShader {
68806879
protected int modelviewMatrixLoc;
68816880
protected int projectionMatrixLoc;
68826881

6882+
protected int viewportLoc;
6883+
protected int perspectiveLoc;
6884+
68836885
protected int inVertexLoc;
68846886
protected int inColorLoc;
68856887
protected int inPointLoc;
@@ -6907,6 +6909,9 @@ public void loadUniforms() {
69076909
projmodelviewMatrixLoc = getUniformLoc("projmodelviewMatrix");
69086910
modelviewMatrixLoc = getUniformLoc("modelviewMatrix");
69096911
projectionMatrixLoc = getUniformLoc("projectionMatrix");
6912+
6913+
viewportLoc = getUniformLoc("viewport");
6914+
perspectiveLoc = getUniformLoc("perspective");
69106915
}
69116916

69126917
public void setVertexAttribute(int vboId, int size, int type,
@@ -6950,6 +6955,18 @@ public void bind() {
69506955
pgCurrent.updateGLProjection();
69516956
setUniformMatrix(projectionMatrixLoc, pgCurrent.glProjection);
69526957
}
6958+
6959+
float x = pgCurrent.viewport[0];
6960+
float y = pgCurrent.viewport[1];
6961+
float w = pgCurrent.viewport[2];
6962+
float h = pgCurrent.viewport[3];
6963+
setUniformValue(viewportLoc, x, y, w, h);
6964+
6965+
if (pgCurrent.hintEnabled(ENABLE_PERSPECTIVE_CORRECTED_STROKE)) {
6966+
setUniformValue(perspectiveLoc, 1);
6967+
} else {
6968+
setUniformValue(perspectiveLoc, 0);
6969+
}
69536970
}
69546971

69556972
public void unbind() {

core/src/processing/opengl/PointShaderVert.glsl

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,33 @@
2121
uniform mat4 projectionMatrix;
2222
uniform mat4 modelviewMatrix;
2323

24+
uniform vec4 viewport;
25+
uniform int perspective;
26+
2427
attribute vec4 inVertex;
2528
attribute vec4 inColor;
2629
attribute vec2 inPoint;
2730

2831
varying vec4 vertColor;
2932

33+
vec4 windowToClipVector(vec2 window, vec4 viewport, float clip_w) {
34+
vec2 xypos = (window / viewport.zw) * 2.0;
35+
return vec4(xypos, 0.0, 0.0) * clip_w;
36+
}
37+
3038
void main() {
3139
vec4 pos = modelviewMatrix * inVertex;
32-
pos.xy += inPoint.xy;
33-
gl_Position = projectionMatrix * pos;
40+
vec4 clip = projectionMatrix * pos;
41+
42+
if (0 < perspective) {
43+
// Perspective correction (points will look thiner as they move away
44+
// from the view position).
45+
gl_Position = clip + projectionMatrix * vec4(inPoint.xy, 0, 0);
46+
} else {
47+
// No perspective correction.
48+
vec4 offset = windowToClipVector(inPoint.xy, viewport, clip.w);
49+
gl_Position = clip + offset;
50+
}
3451

3552
vertColor = inColor;
3653
}

0 commit comments

Comments
 (0)