removed use of transform stack in PShapeOpenGL

This commit is contained in:
codeanticode
2019-01-10 06:49:40 -05:00
parent 7e59e4c578
commit a6a57e80f3
+40 -34
View File
@@ -43,7 +43,6 @@ import processing.opengl.PGraphicsOpenGL.VertexAttribute;
import java.nio.Buffer;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Stack;
/**
* This class holds a 3D model composed of vertices, normals, colors
@@ -171,7 +170,8 @@ public class PShapeOpenGL extends PShape {
// Geometric transformations.
protected PMatrix transform;
protected Stack<PMatrix> transformStack;
protected PMatrix transformInv;
protected PMatrix matrixInv;
// ........................................................
@@ -847,7 +847,7 @@ public class PShapeOpenGL extends PShape {
protected void addTexture(PImage tex) {
if (textures == null) {
textures = new HashSet<PImage>();
textures = new HashSet<>();
}
textures.add(tex);
if (parent != null) {
@@ -1334,39 +1334,34 @@ public class PShapeOpenGL extends PShape {
@Override
public void resetMatrix() {
if (shapeCreated && matrix != null && transformStack != null) {
if (shapeCreated && matrix != null && matrixInv != null) {
if (family == GROUP) {
updateTessellation();
}
if (tessellated) {
PMatrix mat = popTransform();
while (mat != null) {
boolean res = mat.invert();
if (res) {
applyMatrixImpl(mat);
} else {
PGraphics.showWarning("Transformation applied on the shape cannot be inverted");
}
mat = popTransform();
}
applyMatrixImpl(matrixInv);
}
matrix.reset();
transformStack.clear();
matrixInv.reset();
}
}
protected void transform(int type, float... args) {
int dimensions = is3D ? 3 : 2;
boolean invertible = true;
checkMatrix(dimensions);
if (transform == null) {
if (dimensions == 2) {
transform = new PMatrix2D();
transformInv = new PMatrix2D();
} else {
transform = new PMatrix3D();
transformInv = new PMatrix3D();
}
} else {
transform.reset();
transformInv.reset();
}
int ncoords = args.length;
@@ -1380,22 +1375,28 @@ public class PShapeOpenGL extends PShape {
case TRANSLATE:
if (ncoords == 3) {
transform.translate(args[0], args[1], args[2]);
PGraphicsOpenGL.invTranslate((PMatrix3D)transformInv, args[0], args[1], args[2]);
} else {
transform.translate(args[0], args[1]);
PGraphicsOpenGL.invTranslate((PMatrix2D)transformInv, args[0], args[1]);
}
break;
case ROTATE:
if (ncoords == 3) {
transform.rotate(args[0], args[1], args[2], args[3]);
PGraphicsOpenGL.invRotate((PMatrix3D)transformInv, args[0], args[1], args[2], args[3]);
} else {
transform.rotate(args[0]);
PGraphicsOpenGL.invRotate((PMatrix2D)transformInv, -args[0]);
}
break;
case SCALE:
if (ncoords == 3) {
transform.scale(args[0], args[1], args[2]);
PGraphicsOpenGL.invScale((PMatrix3D)transformInv, args[0], args[1], args[2]);
} else {
transform.scale(args[0], args[1]);
PGraphicsOpenGL.invScale((PMatrix2D)transformInv, args[0], args[1]);
}
break;
case MATRIX:
@@ -1408,32 +1409,20 @@ public class PShapeOpenGL extends PShape {
transform.set(args[0], args[1], args[2],
args[3], args[4], args[5]);
}
transformInv.set(transform);
invertible = transformInv.invert();
break;
}
matrix.preApply(transform);
pushTransform();
if (invertible) {
matrixInv.apply(transformInv);
} else {
PGraphics.showWarning("Transformation applied on the shape cannot be inverted");
}
if (tessellated) applyMatrixImpl(transform);
}
protected void pushTransform() {
if (transformStack == null) transformStack = new Stack<PMatrix>();
PMatrix mat;
if (transform instanceof PMatrix2D) {
mat = new PMatrix2D();
} else {
mat = new PMatrix3D();
}
mat.set(transform);
transformStack.push(mat);
}
protected PMatrix popTransform() {
if (transformStack == null || transformStack.size() == 0) return null;
return transformStack.pop();
}
protected void applyMatrixImpl(PMatrix matrix) {
if (hasPolys) {
tessGeo.applyMatrixOnPolyGeometry(matrix,
@@ -1465,6 +1454,23 @@ public class PShapeOpenGL extends PShape {
}
@Override
protected void checkMatrix(int dimensions) {
if (matrix == null) {
if (dimensions == 2) {
matrix = new PMatrix2D();
matrixInv = new PMatrix2D();
} else {
matrix = new PMatrix3D();
matrixInv = new PMatrix3D();
}
} else if (dimensions == 3 && (matrix instanceof PMatrix2D)) {
matrix = new PMatrix3D(matrix);
matrixInv = new PMatrix3D(matrixInv);
}
}
///////////////////////////////////////////////////////////
//