Skip to content

Commit 4c777de

Browse files
committed
opt orthonormal matrix inversion
1 parent 7fb1d5d commit 4c777de

File tree

6 files changed

+20
-8
lines changed

6 files changed

+20
-8
lines changed

src/camera.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ struct Camera : ICamera {
627627
Core::mViewInv.scale(vec3(1.0f, -1.0f, 1.0f));
628628
}
629629

630-
Core::mView = Core::mViewInv.inverse();
630+
Core::mView = Core::mViewInv.inverseOrtho();
631631
if (shake > 0.0f)
632632
Core::mView.translate(vec3(0.0f, sinf(shake * PI * 7) * shake * 48.0f, 0.0f));
633633

src/controller.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,7 @@ struct Controller {
743743
}
744744

745745
bool collide(const Sphere &sphere) {
746-
return getBoundingBoxLocal().intersect(Sphere(getMatrix().inverse() * sphere.center, sphere.radius));
746+
return getBoundingBoxLocal().intersect(Sphere(getMatrix().inverseOrtho() * sphere.center, sphere.radius));
747747
}
748748

749749
vec3 trace(int fromRoom, const vec3 &from, const vec3 &to, int &room, bool isCamera) { // TODO: use Bresenham

src/frustum.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ struct Frustum {
4747
bool isVisible(const mat4 &matrix, const vec3 &min, const vec3 &max) {
4848
start = count;
4949
// transform clip planes (relative)
50-
mat4 m = matrix.inverse();
50+
mat4 m = matrix.inverseOrtho();
5151
for (int i = 0; i < count; i++) {
5252
vec4 &p = planes[i];
5353
vec4 o = m * vec4(p.xyz() * (-p.w), 1.0f);

src/inventory.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1329,7 +1329,7 @@ struct Inventory {
13291329

13301330
Core::mView.up() *= -1.0f;
13311331
Core::mView.dir() *= -1.0f;
1332-
Core::mViewInv = Core::mView.inverse();
1332+
Core::mViewInv = Core::mView.inverseOrtho();
13331333

13341334
Core::mProj = mat4(70.0f, aspect, 32.0f, 2048.0f);
13351335
Core::setViewProj(Core::mView, Core::mProj);

src/level.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1730,7 +1730,7 @@ struct Level : IGame {
17301730
}
17311731

17321732
Core::mViewInv = mat4(pos, pos + dir, up);
1733-
Core::mView = Core::mViewInv.inverse();
1733+
Core::mView = Core::mViewInv.inverseOrtho();
17341734
Core::mProj = mat4(90, 1.0f, camera->znear, camera->zfar);
17351735
Core::mViewProj = Core::mProj * Core::mView;
17361736
Core::viewPos = Core::mViewInv.offset().xyz();
@@ -1742,7 +1742,7 @@ struct Level : IGame {
17421742
vec3 pos = player->getBoundingBox().center();
17431743

17441744
Core::mViewInv = mat4(player->mainLightPos, pos, vec3(0, -1, 0));
1745-
Core::mView = Core::mViewInv.inverse();
1745+
Core::mView = Core::mViewInv.inverseOrtho();
17461746
Core::mProj = mat4(90.0f, 1.0f, camera->znear, player->mainLightColor.w * 1.5f);
17471747

17481748
mat4 bias;
@@ -1778,7 +1778,7 @@ struct Level : IGame {
17781778

17791779
#ifdef _DEBUG
17801780
void renderDebug() {
1781-
if (level.isTitle()) return;
1781+
if (level.isTitle() || inventory.titleTimer > 1.0f) return;
17821782

17831783
Core::setViewport(0, 0, Core::width, Core::height);
17841784
camera->setup(true);

src/utils.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,18 @@ struct mat4 {
737737
return r;
738738
}
739739

740+
mat4 inverseOrtho() const {
741+
mat4 r;
742+
r.e00 = e00; r.e10 = e01; r.e20 = e02; r.e30 = 0;
743+
r.e01 = e10; r.e11 = e11; r.e21 = e12; r.e31 = 0;
744+
r.e02 = e20; r.e12 = e21; r.e22 = e22; r.e32 = 0;
745+
r.e03 = -(e03 * e00 + e13 * e10 + e23 * e20); // -dot(pos, right)
746+
r.e13 = -(e03 * e01 + e13 * e11 + e23 * e21); // -dot(pos, up)
747+
r.e23 = -(e03 * e02 + e13 * e12 + e23 * e22); // -dot(pos, dir)
748+
r.e33 = 1;
749+
return r;
750+
}
751+
740752
mat4 transpose() const {
741753
mat4 r;
742754
r.e00 = e00; r.e10 = e01; r.e20 = e02; r.e30 = e03;
@@ -1126,7 +1138,7 @@ struct Box {
11261138
}
11271139

11281140
bool intersect(const mat4 &matrix, const vec3 &rayPos, const vec3 &rayDir, float &t) const {
1129-
mat4 mInv = matrix.inverse();
1141+
mat4 mInv = matrix.inverseOrtho();
11301142
return intersect(mInv * rayPos, (mInv * vec4(rayDir, 0)).xyz(), t);
11311143
}
11321144
};

0 commit comments

Comments
 (0)