diff options
author | Mike Reed <reed@google.com> | 2018-05-30 16:02:48 -0400 |
---|---|---|
committer | Skia Commit-Bot <skia-commit-bot@chromium.org> | 2018-05-30 20:36:08 +0000 |
commit | 27714a026d9178ff7834b87677cbd10bf5150dc7 (patch) | |
tree | 424afe8d98ea19b18d6a3833950bcae9c80fa34c /src/utils | |
parent | a15cedbc26fee1ba23465aaa0dc46dd0e69996cc (diff) |
3d
Bug: skia:
Change-Id: I10e56f08a2463fcac2b7813d43d0aae35268ac27
Reviewed-on: https://skia-review.googlesource.com/130842
Commit-Queue: Brian Salomon <bsalomon@google.com>
Auto-Submit: Mike Reed <reed@google.com>
Reviewed-by: Brian Salomon <bsalomon@google.com>
Diffstat (limited to 'src/utils')
-rw-r--r-- | src/utils/Sk3D.cpp | 64 | ||||
-rw-r--r-- | src/utils/Sk3D.h | 19 |
2 files changed, 83 insertions, 0 deletions
diff --git a/src/utils/Sk3D.cpp b/src/utils/Sk3D.cpp new file mode 100644 index 0000000000..b1b5d6c0ea --- /dev/null +++ b/src/utils/Sk3D.cpp @@ -0,0 +1,64 @@ +/* + * 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] }; + } +} + diff --git a/src/utils/Sk3D.h b/src/utils/Sk3D.h new file mode 100644 index 0000000000..46bb5cce80 --- /dev/null +++ b/src/utils/Sk3D.h @@ -0,0 +1,19 @@ +/* + * Copyright 2018 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#ifndef Sk3D_DEFINED +#define Sk3D_DEFINED + +#include "SkPoint3.h" +#include "SkMatrix44.h" + +void Sk3LookAt(SkMatrix44* dst, const SkPoint3& eye, const SkPoint3& center, const SkPoint3& up); +bool Sk3Perspective(SkMatrix44* dst, float near, float far, float angle); +void Sk3MapPts(SkPoint dst[], const SkMatrix44& m4, const SkPoint3 src[], int count); + +#endif + |