1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
| /*
| * Copyright 2018 Google Inc.
| *
| * Use of this source code is governed by a BSD-style license that can be
| * found in the LICENSE file.
| */
|
| #include "Sk3D.h"
|
| static void set_col(SkMatrix44* m, int col, const SkPoint3& v) {
| m->set(0, col, v.fX);
| m->set(1, col, v.fY);
| m->set(2, col, v.fZ);
| }
|
| static SkPoint3 cross(const SkPoint3& a, const SkPoint3& b) {
| return {
| a.fY * b.fZ - a.fZ * b.fY,
| a.fZ * b.fX - a.fX * b.fZ,
| a.fX * b.fY - a.fY * b.fX,
| };
| }
|
| void Sk3LookAt(SkMatrix44* dst, const SkPoint3& eye, const SkPoint3& center, const SkPoint3& up) {
| SkPoint3 f = center - eye;
| f.normalize();
| SkPoint3 u = up;
| u.normalize();
| SkPoint3 s = cross(f, u);
| s.normalize();
| u = cross(s, f);
|
| dst->setIdentity();
| set_col(dst, 0, s);
| set_col(dst, 1, u);
| set_col(dst, 2, -f);
| set_col(dst, 3, eye);
| dst->invert(dst);
| }
|
| bool Sk3Perspective(SkMatrix44* dst, float near, float far, float angle) {
| SkASSERT(far > near);
|
| float denomInv = sk_ieee_float_divide(1, far - near);
| float halfAngle = angle * 0.5f;
| float cot = sk_float_cos(halfAngle) / sk_float_sin(halfAngle);
|
| dst->setIdentity();
| dst->set(0, 0, cot);
| dst->set(1, 1, cot);
| dst->set(2, 2, (far + near) * denomInv);
| dst->set(2, 3, 2 * far * near * denomInv);
| dst->set(3, 2, -1);
| return true;
| }
|
| void Sk3MapPts(SkPoint dst[], const SkMatrix44& m4, const SkPoint3 src[], int count) {
| for (int i = 0; i < count; ++i) {
| SkVector4 v = m4 * SkVector4{ src[i].fX, src[i].fY, src[i].fZ, 1 };
| // clip v;
| dst[i] = { v.fData[0] / v.fData[3], v.fData[1] / v.fData[3] };
| }
| }
|
|