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