Skip to content

Commit

Permalink
feat(dependency): lookAt
Browse files Browse the repository at this point in the history
Add setLookAt to Matrix4
  • Loading branch information
Wang Ying committed Sep 22, 2020
1 parent 56268ab commit 5891d70
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/construct/domain_layer/library/structure/matrix/Matrix4.re
Original file line number Diff line number Diff line change
Expand Up @@ -600,3 +600,36 @@ let getEulerAngles = matTypeArr => {

(x^, y, z^)->Vector3.scale(Vector3.Float, Angle.getRadToDeg());
};

let setLookAt = (eye, center, up) => {
let (z1, z2, z3) as z =
Vector3.sub(Vector3.Float, eye, center) -> Vector3.normalize;

let y = up -> Vector3.normalize;

let (x1, x2, x3) as x =
Vector3.cross(y, z) -> Vector3.normalize;

let (y1, y2, y3) = Vector3.cross(z, x);

let (eyeX, eyeY, eyeZ) = eye;

Float32Array.make([|
x1,
x2,
x3,
0.,
y1,
y2,
y3,
0.,
z1,
z2,
z3,
0.,
eyeX,
eyeY,
eyeZ,
1.,
|]);
}
44 changes: 44 additions & 0 deletions test/construct/unit/library/structure/Matrix4_test.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
open Wonder_jest;
open Js.Typed_array;

let _ =
describe("Matrix4", () => {
open Expect;
open Expect.Operators;
open Sinon;

let sandbox = getSandboxDefaultVal();

beforeEach(() => {
sandbox := createSandbox();
TestTool.init(~sandbox, ());
});

afterEach(() => restoreSandbox(refJsObjToSandbox(sandbox^)));

describe("setLookAt", () => {
test("test", () => {
let matrix = Matrix4.setLookAt((0., 0., 20.), (0., 0., 0.), (0., 1., 0.));
let result = Float32Array.make([|
1.0,
0.0,
0.0,
0.0,
0.0,
1.0,
0.0,
0.0,
0.0,
0.0,
1.0,
0.0,
0.0,
0.0,
20.0,
1.0
|]);

matrix -> expect == result;
})
})
})

0 comments on commit 5891d70

Please sign in to comment.