Skip to content

Commit 081eaf6

Browse files
committed
port Java mode GL fixes/updates
1 parent 71e67c7 commit 081eaf6

File tree

4 files changed

+109
-32
lines changed

4 files changed

+109
-32
lines changed

core/src/processing/opengl/PGL.java

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,13 @@ 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+
281+
275282
// ........................................................
276283

277284
// Error messages
@@ -410,7 +417,7 @@ public PGL(PGraphicsOpenGL pg) {
410417
public void dispose() {
411418
destroyFBOLayer();
412419
graphics = null;
413-
sketch = null;
420+
sketch = null;
414421
}
415422

416423

@@ -647,13 +654,49 @@ public boolean insideStopButton(float x, float y) {
647654
// Frame rendering
648655

649656

650-
protected void clearBackground(float r, float g, float b, float a, boolean depth) {
651-
if (depth) {
657+
protected void clearDepthStencil() {
658+
if (!pclearDepth && !pclearStencil) {
659+
depthMask(true);
652660
clearDepth(1);
653-
clear(PGL.DEPTH_BUFFER_BIT);
661+
clearStencil(0);
662+
clear(DEPTH_BUFFER_BIT | STENCIL_BUFFER_BIT);
663+
} else if (!pclearDepth) {
664+
depthMask(true);
665+
clearDepth(1);
666+
clear(DEPTH_BUFFER_BIT);
667+
} else if (!pclearStencil) {
668+
clearStencil(0);
669+
clear(STENCIL_BUFFER_BIT);
654670
}
671+
}
672+
673+
674+
protected void clearBackground(float r, float g, float b, float a,
675+
boolean depth, boolean stencil) {
655676
clearColor(r, g, b, a);
656-
clear(PGL.COLOR_BUFFER_BIT);
677+
if (depth && stencil) {
678+
clearDepth(1);
679+
clearStencil(0);
680+
clear(DEPTH_BUFFER_BIT | STENCIL_BUFFER_BIT | COLOR_BUFFER_BIT);
681+
if (0 < sketch.frameCount) {
682+
clearDepth = true;
683+
clearStencil = true;
684+
}
685+
} else if (depth) {
686+
clearDepth(1);
687+
clear(DEPTH_BUFFER_BIT | COLOR_BUFFER_BIT);
688+
if (0 < sketch.frameCount) {
689+
clearDepth = true;
690+
}
691+
} else if (stencil) {
692+
clearStencil(0);
693+
clear(STENCIL_BUFFER_BIT | COLOR_BUFFER_BIT);
694+
if (0 < sketch.frameCount) {
695+
clearStencil = true;
696+
}
697+
} else {
698+
clear(PGL.COLOR_BUFFER_BIT);
699+
}
657700
if (0 < sketch.frameCount) {
658701
clearColor = true;
659702
}
@@ -671,6 +714,12 @@ protected void beginRender() {
671714
pclearColor = clearColor;
672715
clearColor = false;
673716

717+
pclearDepth = clearDepth;
718+
clearColor = false;
719+
720+
pclearStencil = clearStencil;
721+
clearStencil = false;
722+
674723
if (SINGLE_BUFFERED && sketch.frameCount == 1) {
675724
restoreFirstFrame();
676725
}

core/src/processing/opengl/PGraphicsOpenGL.java

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

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

@@ -595,13 +601,21 @@ public void setSize(int iwidth, int iheight) {
595601
updatePixelSize();
596602

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

606620
sized = true;
607621
}
@@ -4341,7 +4355,8 @@ public void endCamera() {
43414355
*/
43424356
@Override
43434357
public void camera() {
4344-
camera(cameraX, cameraY, cameraZ, cameraX, cameraY, 0, 0, 1, 0);
4358+
camera(defCameraX, defCameraY, defCameraZ, defCameraX, defCameraY,
4359+
0, 0, 1, 0);
43454360
}
43464361

43474362

@@ -4405,6 +4420,10 @@ public void camera() {
44054420
public void camera(float eyeX, float eyeY, float eyeZ,
44064421
float centerX, float centerY, float centerZ,
44074422
float upX, float upY, float upZ) {
4423+
cameraX = eyeX;
4424+
cameraY = eyeY;
4425+
cameraZ = eyeZ;
4426+
44084427
// Calculating Z vector
44094428
float z0 = eyeX - centerX;
44104429
float z1 = eyeY - centerY;
@@ -4562,7 +4581,7 @@ public void ortho(float left, float right,
45624581
*/
45634582
@Override
45644583
public void perspective() {
4565-
perspective(cameraFOV, cameraAspect, cameraNear, cameraFar);
4584+
perspective(defCameraFOV, defCameraAspect, defCameraNear, defCameraFar);
45664585
}
45674586

45684587

@@ -4591,6 +4610,11 @@ public void frustum(float left, float right, float bottom, float top,
45914610
// Flushing geometry with a different perspective configuration.
45924611
flush();
45934612

4613+
cameraFOV = 2 * (float) Math.atan2(top, znear);
4614+
cameraAspect = left / bottom;
4615+
cameraNear = znear;
4616+
cameraFar = zfar;
4617+
45944618
float n2 = 2 * znear;
45954619
float w = right - left;
45964620
float h = top - bottom;
@@ -6827,11 +6851,7 @@ protected void setGLSettings() {
68276851
normalX = normalY = 0;
68286852
normalZ = 1;
68296853

6830-
// Clear depth and stencil buffers.
6831-
pgl.depthMask(true);
6832-
pgl.clearDepth(1);
6833-
pgl.clearStencil(0);
6834-
pgl.clear(PGL.DEPTH_BUFFER_BIT | PGL.STENCIL_BUFFER_BIT);
6854+
pgl.clearDepthStencil();
68356855

68366856
if (hints[DISABLE_DEPTH_MASK]) {
68376857
pgl.depthMask(false);
@@ -8386,9 +8406,9 @@ void addTrianglesEdges() {
83868406
int i1 = 3 * i + 1;
83878407
int i2 = 3 * i + 2;
83888408

8389-
addEdge(i0, i1, true, false);
8409+
addEdge(i0, i1, true, false);
83908410
addEdge(i1, i2, false, false);
8391-
addEdge(i2, i0, false, false);
8411+
addEdge(i2, i0, false, false);
83928412
closeEdge(i2, i0);
83938413
}
83948414
}
@@ -8399,9 +8419,9 @@ void addTriangleFanEdges() {
83998419
int i1 = i;
84008420
int i2 = i + 1;
84018421

8402-
addEdge(i0, i1, true, false);
8422+
addEdge(i0, i1, true, false);
84038423
addEdge(i1, i2, false, false);
8404-
addEdge(i2, i0, false, false);
8424+
addEdge(i2, i0, false, false);
84058425
closeEdge(i2, i0);
84068426
}
84078427
}
@@ -8418,9 +8438,9 @@ void addTriangleStripEdges() {
84188438
i2 = i - 1;
84198439
}
84208440

8421-
addEdge(i0, i1, true, false);
8441+
addEdge(i0, i1, true, false);
84228442
addEdge(i1, i2, false, false);
8423-
addEdge(i2, i0, false, false);
8443+
addEdge(i2, i0, false, false);
84248444
closeEdge(i2, i0);
84258445
}
84268446
}
@@ -8432,10 +8452,10 @@ void addQuadsEdges() {
84328452
int i2 = 4 * i + 2;
84338453
int i3 = 4 * i + 3;
84348454

8435-
addEdge(i0, i1, true, false);
8455+
addEdge(i0, i1, true, false);
84368456
addEdge(i1, i2, false, false);
8437-
addEdge(i2, i3, false, false);
8438-
addEdge(i3, i0, false, false);
8457+
addEdge(i2, i3, false, false);
8458+
addEdge(i3, i0, false, false);
84398459
closeEdge(i3, i0);
84408460
}
84418461
}
@@ -8447,10 +8467,10 @@ void addQuadStripEdges() {
84478467
int i2 = 2 * qd + 1;
84488468
int i3 = 2 * qd;
84498469

8450-
addEdge(i0, i1, true, false);
8470+
addEdge(i0, i1, true, false);
84518471
addEdge(i1, i2, false, false);
8452-
addEdge(i2, i3, false, false);
8453-
addEdge(i3, i0, false, true);
8472+
addEdge(i2, i3, false, false);
8473+
addEdge(i3, i0, false, false);
84548474
closeEdge(i3, i0);
84558475
}
84568476
}

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)