Skip to content

Commit 5965b34

Browse files
committed
port Java mode GL fixes/updates
1 parent 2a033e5 commit 5965b34

File tree

7 files changed

+131
-26
lines changed

7 files changed

+131
-26
lines changed

core/src/processing/core/PMatrix.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,19 @@ public void apply(float n00, float n01, float n02, float n03,
9494
float n20, float n21, float n22, float n23,
9595
float n30, float n31, float n32, float n33);
9696

97+
/**
98+
* Apply another matrix to the left of this one.
99+
*/
100+
public void preApply(PMatrix left);
101+
97102
/**
98103
* Apply another matrix to the left of this one.
99104
*/
100105
public void preApply(PMatrix2D left);
101106

107+
/**
108+
* Apply another matrix to the left of this one. 3D only.
109+
*/
102110
public void preApply(PMatrix3D left);
103111

104112
public void preApply(float n00, float n01, float n02,

core/src/processing/core/PMatrix2D.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,15 @@ public void apply(float n00, float n01, float n02, float n03,
247247
/**
248248
* Apply another matrix to the left of this one.
249249
*/
250+
public void preApply(PMatrix source) {
251+
if (source instanceof PMatrix2D) {
252+
preApply((PMatrix2D) source);
253+
} else if (source instanceof PMatrix3D) {
254+
preApply((PMatrix3D) source);
255+
}
256+
}
257+
258+
250259
public void preApply(PMatrix2D left) {
251260
preApply(left.m00, left.m01, left.m02,
252261
left.m10, left.m11, left.m12);

core/src/processing/core/PMatrix3D.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,18 @@ public void preApply(PMatrix2D left) {
367367
}
368368

369369

370+
/**
371+
* Apply another matrix to the left of this one.
372+
*/
373+
public void preApply(PMatrix source) {
374+
if (source instanceof PMatrix2D) {
375+
preApply((PMatrix2D) source);
376+
} else if (source instanceof PMatrix3D) {
377+
preApply((PMatrix3D) source);
378+
}
379+
}
380+
381+
370382
/**
371383
* Apply another matrix to the left of this one.
372384
*/

core/src/processing/opengl/PGL.java

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,12 @@ public abstract class PGL {
272272
protected boolean clearColor = false;
273273
protected boolean pclearColor;
274274

275+
protected boolean clearDepth = false;
276+
protected boolean pclearDepth;
277+
278+
protected boolean clearStencil = false;
279+
protected boolean pclearStencil;
280+
275281
// ........................................................
276282

277283
// Error messages
@@ -638,13 +644,49 @@ public boolean insideStopButton(float x, float y) {
638644
// Frame rendering
639645

640646

641-
protected void clearBackground(float r, float g, float b, float a, boolean depth) {
642-
if (depth) {
647+
protected void clearDepthStencil() {
648+
if (!pclearDepth && !pclearStencil) {
649+
depthMask(true);
643650
clearDepth(1);
644-
clear(PGL.DEPTH_BUFFER_BIT);
651+
clearStencil(0);
652+
clear(DEPTH_BUFFER_BIT | STENCIL_BUFFER_BIT);
653+
} else if (!pclearDepth) {
654+
depthMask(true);
655+
clearDepth(1);
656+
clear(DEPTH_BUFFER_BIT);
657+
} else if (!pclearStencil) {
658+
clearStencil(0);
659+
clear(STENCIL_BUFFER_BIT);
645660
}
661+
}
662+
663+
664+
protected void clearBackground(float r, float g, float b, float a,
665+
boolean depth, boolean stencil) {
646666
clearColor(r, g, b, a);
647-
clear(PGL.COLOR_BUFFER_BIT);
667+
if (depth && stencil) {
668+
clearDepth(1);
669+
clearStencil(0);
670+
clear(DEPTH_BUFFER_BIT | STENCIL_BUFFER_BIT | COLOR_BUFFER_BIT);
671+
if (0 < sketch.frameCount) {
672+
clearDepth = true;
673+
clearStencil = true;
674+
}
675+
} else if (depth) {
676+
clearDepth(1);
677+
clear(DEPTH_BUFFER_BIT | COLOR_BUFFER_BIT);
678+
if (0 < sketch.frameCount) {
679+
clearDepth = true;
680+
}
681+
} else if (stencil) {
682+
clearStencil(0);
683+
clear(STENCIL_BUFFER_BIT | COLOR_BUFFER_BIT);
684+
if (0 < sketch.frameCount) {
685+
clearStencil = true;
686+
}
687+
} else {
688+
clear(PGL.COLOR_BUFFER_BIT);
689+
}
648690
if (0 < sketch.frameCount) {
649691
clearColor = true;
650692
}

core/src/processing/opengl/PGraphicsOpenGL.java

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,12 @@ public class PGraphicsOpenGL extends PGraphics {
224224
/** Aspect ratio of camera's view. */
225225
public float cameraAspect;
226226

227+
/** Default camera properties. */
228+
public float defCameraFOV;
229+
public float defCameraX, defCameraY, defCameraZ;
230+
public float defCameraNear, defCameraFar;
231+
public float defCameraAspect;
232+
227233
/** Distance between camera eye and center. */
228234
protected float eyeDist;
229235

@@ -594,13 +600,21 @@ public void setSize(int iwidth, int iheight) {
594600
updatePixelSize();
595601

596602
// init perspective projection based on new dimensions
597-
cameraFOV = 60 * DEG_TO_RAD; // at least for now
598-
cameraX = width / 2.0f;
599-
cameraY = height / 2.0f;
600-
cameraZ = cameraY / ((float) Math.tan(cameraFOV / 2.0f));
601-
cameraNear = cameraZ / 10.0f;
602-
cameraFar = cameraZ * 10.0f;
603-
cameraAspect = (float) width / (float) height;
603+
defCameraFOV = 60 * DEG_TO_RAD; // at least for now
604+
defCameraX = width / 2.0f;
605+
defCameraY = height / 2.0f;
606+
defCameraZ = defCameraY / ((float) Math.tan(defCameraFOV / 2.0f));
607+
defCameraNear = defCameraZ / 10.0f;
608+
defCameraFar = defCameraZ * 10.0f;
609+
defCameraAspect = (float) width / (float) height;
610+
611+
cameraFOV = defCameraFOV;
612+
cameraX = defCameraX;
613+
cameraY = defCameraY;
614+
cameraZ = defCameraZ;
615+
cameraNear = defCameraNear;
616+
cameraFar = defCameraFar;
617+
cameraAspect = defCameraAspect;
604618

605619
sized = true;
606620
}
@@ -4334,7 +4348,8 @@ public void endCamera() {
43344348
*/
43354349
@Override
43364350
public void camera() {
4337-
camera(cameraX, cameraY, cameraZ, cameraX, cameraY, 0, 0, 1, 0);
4351+
camera(defCameraX, defCameraY, defCameraZ, defCameraX, defCameraY,
4352+
0, 0, 1, 0);
43384353
}
43394354

43404355

@@ -4398,6 +4413,10 @@ public void camera() {
43984413
public void camera(float eyeX, float eyeY, float eyeZ,
43994414
float centerX, float centerY, float centerZ,
44004415
float upX, float upY, float upZ) {
4416+
cameraX = eyeX;
4417+
cameraY = eyeY;
4418+
cameraZ = eyeZ;
4419+
44014420
// Calculating Z vector
44024421
float z0 = eyeX - centerX;
44034422
float z1 = eyeY - centerY;
@@ -4555,7 +4574,7 @@ public void ortho(float left, float right,
45554574
*/
45564575
@Override
45574576
public void perspective() {
4558-
perspective(cameraFOV, cameraAspect, cameraNear, cameraFar);
4577+
perspective(defCameraFOV, defCameraAspect, defCameraNear, defCameraFar);
45594578
}
45604579

45614580

@@ -4584,6 +4603,11 @@ public void frustum(float left, float right, float bottom, float top,
45844603
// Flushing geometry with a different perspective configuration.
45854604
flush();
45864605

4606+
cameraFOV = 2 * (float) Math.atan2(top, znear);
4607+
cameraAspect = left / bottom;
4608+
cameraNear = znear;
4609+
cameraFar = zfar;
4610+
45874611
float n2 = 2 * znear;
45884612
float w = right - left;
45894613
float h = top - bottom;
@@ -6819,6 +6843,8 @@ protected void setGLSettings() {
68196843
normalX = normalY = 0;
68206844
normalZ = 1;
68216845

6846+
pgl.clearDepthStencil();
6847+
68226848
// Clear depth and stencil buffers.
68236849
pgl.depthMask(true);
68246850
pgl.clearDepth(1);
@@ -8378,9 +8404,9 @@ void addTrianglesEdges() {
83788404
int i1 = 3 * i + 1;
83798405
int i2 = 3 * i + 2;
83808406

8381-
addEdge(i0, i1, true, false);
8407+
addEdge(i0, i1, true, false);
83828408
addEdge(i1, i2, false, false);
8383-
addEdge(i2, i0, false, false);
8409+
addEdge(i2, i0, false, false);
83848410
closeEdge(i2, i0);
83858411
}
83868412
}
@@ -8391,9 +8417,9 @@ void addTriangleFanEdges() {
83918417
int i1 = i;
83928418
int i2 = i + 1;
83938419

8394-
addEdge(i0, i1, true, false);
8420+
addEdge(i0, i1, true, false);
83958421
addEdge(i1, i2, false, false);
8396-
addEdge(i2, i0, false, false);
8422+
addEdge(i2, i0, false, false);
83978423
closeEdge(i2, i0);
83988424
}
83998425
}
@@ -8410,9 +8436,9 @@ void addTriangleStripEdges() {
84108436
i2 = i - 1;
84118437
}
84128438

8413-
addEdge(i0, i1, true, false);
8439+
addEdge(i0, i1, true, false);
84148440
addEdge(i1, i2, false, false);
8415-
addEdge(i2, i0, false, false);
8441+
addEdge(i2, i0, false, false);
84168442
closeEdge(i2, i0);
84178443
}
84188444
}
@@ -8424,10 +8450,10 @@ void addQuadsEdges() {
84248450
int i2 = 4 * i + 2;
84258451
int i3 = 4 * i + 3;
84268452

8427-
addEdge(i0, i1, true, false);
8453+
addEdge(i0, i1, true, false);
84288454
addEdge(i1, i2, false, false);
8429-
addEdge(i2, i3, false, false);
8430-
addEdge(i3, i0, false, false);
8455+
addEdge(i2, i3, false, false);
8456+
addEdge(i3, i0, false, false);
84318457
closeEdge(i3, i0);
84328458
}
84338459
}
@@ -8439,10 +8465,10 @@ void addQuadStripEdges() {
84398465
int i2 = 2 * qd + 1;
84408466
int i3 = 2 * qd;
84418467

8442-
addEdge(i0, i1, true, false);
8468+
addEdge(i0, i1, true, false);
84438469
addEdge(i1, i2, false, false);
8444-
addEdge(i2, i3, false, false);
8445-
addEdge(i3, i0, false, true);
8470+
addEdge(i2, i3, false, false);
8471+
addEdge(i3, i0, false, false);
84468472
closeEdge(i3, i0);
84478473
}
84488474
}

core/src/processing/opengl/PShader.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ public class PShader implements PConstants {
122122
protected int ppixelsLoc;
123123
protected int ppixelsUnit;
124124
protected int viewportLoc;
125+
protected int resolutionLoc;
125126

126127
// Uniforms only for lines and points
127128
protected int perspectiveLoc;
@@ -1148,6 +1149,7 @@ protected void loadUniforms() {
11481149
projectionMatLoc = getUniformLoc("projectionMatrix");
11491150

11501151
viewportLoc = getUniformLoc("viewport");
1152+
resolutionLoc = getUniformLoc("resolution");
11511153
ppixelsLoc = getUniformLoc("ppixels");
11521154

11531155
normalMatLoc = getUniformLoc("normalMatrix");
@@ -1199,6 +1201,12 @@ protected void setCommonUniforms() {
11991201
setUniformValue(viewportLoc, x, y, w, h);
12001202
}
12011203

1204+
if (-1 < resolutionLoc) {
1205+
float w = currentPG.viewport.get(2);
1206+
float h = currentPG.viewport.get(3);
1207+
setUniformValue(resolutionLoc, w, h);
1208+
}
1209+
12021210
if (-1 < ppixelsLoc) {
12031211
ppixelsUnit = getLastTexUnit() + 1;
12041212
setUniformValue(ppixelsLoc, ppixelsUnit);

core/src/processing/opengl/PShapeOpenGL.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1410,7 +1410,7 @@ protected void transform(int type, float... args) {
14101410
}
14111411
break;
14121412
}
1413-
matrix.apply(transform);
1413+
matrix.preApply(transform);
14141414
pushTransform();
14151415
if (tessellated) applyMatrixImpl(transform);
14161416
}

0 commit comments

Comments
 (0)