Skip to content

Commit 83caafc

Browse files
committed
Improve numerical stability of projection
When the position vector has large values that are cancelled out by large values in the model matrix, it is more numerically stable to first multiply the position by the model matrix instead of multiplying the matrices together first.
1 parent 4d44bd9 commit 83caafc

File tree

3 files changed

+5
-5
lines changed

3 files changed

+5
-5
lines changed

lib/shaders/backgroundVert.glsl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ void main() {
1919
vec3 minRange = min(bounds[0], bounds[1]);
2020
vec3 maxRange = max(bounds[0], bounds[1]);
2121
vec3 nPosition = mix(minRange, maxRange, 0.5 * (position + 1.0));
22-
gl_Position = projection * view * model * vec4(nPosition, 1.0);
22+
gl_Position = projection * (view * (model * vec4(nPosition, 1.0)));
2323
} else {
2424
gl_Position = vec4(0,0,0,0);
2525
}
2626

2727
colorChannel = abs(realNormal);
28-
}
28+
}

lib/shaders/lineVert.glsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ uniform float lineWidth;
88
uniform vec2 screenShape;
99

1010
vec3 project(vec3 p) {
11-
vec4 pp = projection * view * model * vec4(p, 1.0);
11+
vec4 pp = projection * (view * (model * vec4(p, 1.0)));
1212
return pp.xyz / max(pp.w, 0.0001);
1313
}
1414

lib/shaders/textVert.glsl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ uniform float scale, angle, pixelScale;
88
uniform vec2 resolution;
99

1010
vec3 project(vec3 p) {
11-
vec4 pp = projection * view * model * vec4(p, 1.0);
11+
vec4 pp = projection * (view * (model * vec4(p, 1.0)));
1212
return pp.xyz / max(pp.w, 0.0001);
1313
}
1414

@@ -133,4 +133,4 @@ void main() {
133133

134134
//Done
135135
gl_Position = vec4(clipPosition, 1.0);
136-
}
136+
}

0 commit comments

Comments
 (0)