Skip to content

Commit a01538d

Browse files
committed
rotate() check for zero vectors, fixed naming error in PGL.Context class
1 parent 2c8faf6 commit a01538d

File tree

3 files changed

+35
-18
lines changed

3 files changed

+35
-18
lines changed

core/src/processing/core/PMatrix3D.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,19 @@ public void rotateZ(float angle) {
229229

230230

231231
public void rotate(float angle, float v0, float v1, float v2) {
232-
// TODO should make sure this vector is normalized
232+
float norm2 = v0 * v0 + v1 * v1 + v2 * v2;
233+
if (norm2 < PConstants.EPSILON) {
234+
// The vector is zero, cannot apply rotation.
235+
return;
236+
}
237+
238+
if (Math.abs(norm2 - 1) > PConstants.EPSILON) {
239+
// The rotation vector is not normalized.
240+
float norm = PApplet.sqrt(norm2);
241+
v0 /= norm;
242+
v1 /= norm;
243+
v2 /= norm;
244+
}
233245

234246
float c = cos(angle);
235247
float s = sin(angle);

java/libraries/opengl/src/processing/opengl/PGL.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,35 +1135,35 @@ public Context getCurrentContext() {
11351135

11361136

11371137
public class Context {
1138-
protected GLContext context;
1138+
protected GLContext glContext;
11391139

11401140
Context() {
1141-
context = null;
1141+
glContext = null;
11421142
}
11431143

11441144
Context(GLContext context) {
1145-
this.context = context;
1145+
glContext = context;
11461146
}
11471147

11481148
boolean current() {
11491149
return equal(context);
11501150
}
11511151

11521152
boolean equal(GLContext context) {
1153-
if (this.context == null || context == null) {
1153+
if (glContext == null || context == null) {
11541154
// A null context means a still non-created resource,
11551155
// so it is considered equal to the argument.
11561156
return true;
11571157
} else {
1158-
return this.context.hashCode() == context.hashCode();
1158+
return glContext.hashCode() == context.hashCode();
11591159
}
11601160
}
11611161

11621162
int code() {
1163-
if (context == null) {
1163+
if (glContext == null) {
11641164
return -1;
11651165
} else {
1166-
return context.hashCode();
1166+
return glContext.hashCode();
11671167
}
11681168
}
11691169
}

java/libraries/opengl/src/processing/opengl/PGraphicsOpenGL.java

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1429,8 +1429,8 @@ public void beginDraw() {
14291429
} else {
14301430
setFramebuffer(offscreenFramebuffer);
14311431
}
1432-
pgl.glDrawBuffer(PGL.GL_COLOR_ATTACHMENT0);
14331432
pgl.updateOffscreen(pg.pgl);
1433+
pgl.glDrawBuffer(PGL.GL_COLOR_ATTACHMENT0);
14341434
}
14351435

14361436
// We are ready to go!
@@ -3159,14 +3159,12 @@ public void rotate(float angle, float v0, float v1, float v2) {
31593159
flush();
31603160
}
31613161

3162-
modelview.rotate(angle, v0, v1, v2);
3163-
invRotate(modelviewInv, angle, v0, v1, v2);
3164-
calcProjmodelview(); // Possibly cheaper than doing projmodelview.rotate()
3165-
}
3166-
3167-
3168-
static private void invRotate(PMatrix3D matrix, float angle, float v0, float v1, float v2) {
31693162
float norm2 = v0 * v0 + v1 * v1 + v2 * v2;
3163+
if (norm2 < EPSILON) {
3164+
// The vector is zero, cannot apply rotation.
3165+
return;
3166+
}
3167+
31703168
if (Math.abs(norm2 - 1) > EPSILON) {
31713169
// The rotation vector is not normalized.
31723170
float norm = PApplet.sqrt(norm2);
@@ -3175,6 +3173,13 @@ static private void invRotate(PMatrix3D matrix, float angle, float v0, float v1,
31753173
v2 /= norm;
31763174
}
31773175

3176+
modelview.rotate(angle, v0, v1, v2);
3177+
invRotate(modelviewInv, angle, v0, v1, v2);
3178+
calcProjmodelview(); // Possibly cheaper than doing projmodelview.rotate()
3179+
}
3180+
3181+
3182+
static private void invRotate(PMatrix3D matrix, float angle, float v0, float v1, float v2) {
31783183
float c = PApplet.cos(-angle);
31793184
float s = PApplet.sin(-angle);
31803185
float t = 1.0f - c;
@@ -5010,10 +5015,10 @@ protected void initOffscreen() {
50105015
// Getting the context and capabilities from the main renderer.
50115016
pg = (PGraphicsOpenGL)parent.g;
50125017
pgl.initOffscreenSurface(pg.pgl);
5013-
50145018
pgl.updateOffscreen(pg.pgl);
5019+
50155020
loadTextureImpl(BILINEAR);
5016-
5021+
50175022
// In case of reinitialization (for example, when the smooth level
50185023
// is changed), we make sure that all the OpenGL resources associated
50195024
// to the surface are released by calling delete().

0 commit comments

Comments
 (0)