• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef MY_X_COMPONENT_ALGORITHM_H
17 #define MY_X_COMPONENT_ALGORITHM_H
18 #include <cstddef>
19 #include "Matrix4x4.h"
20 #include "Vector3.h"
21 #include "cmath"
22 #include "util/types.h"
23 
24 class Algorithm {
25 public:
Radians(float deg)26     static float Radians(float deg) { return deg * static_cast<float>(kNumber); }
27 
Perspective(float fovy,float aspect,float zNear,float zFar)28     static Matrix4x4 Perspective(float fovy, float aspect, float zNear, float zFar)
29     {
30         const float tanHalfFovy = tan(fovy / 2.0f);
31         Matrix4x4 result;
32         result.SetValue(0 * kFour + 0, 1.0f / (aspect * tanHalfFovy));
33         result.SetValue(1 * kFour + 1, 1.0f / (tanHalfFovy));
34         result.SetValue(kTwo * kFour + kTwo, -(zFar + zNear) / (zFar - zNear));
35         result.SetValue(kTwo * kFour + kThree, -1);
36         result.SetValue(kThree * kFour + kTwo, -(2.0f * zFar * zNear) / (zFar - zNear));
37         return result;
38     }
LookAt(Vector3 eye,Vector3 center,Vector3 const & up)39     static Matrix4x4 LookAt(Vector3 eye, Vector3 center, Vector3 const &up)
40     {
41         Vector3 f = Vector3::Normalize(Vector3::Subtract(center, eye));
42         Vector3 s = Vector3::Normalize(Vector3::Cross(f, up));
43         Vector3 u = Vector3::Normalize(Vector3::Cross(s, f));
44 
45         Matrix4x4 result = Matrix4x4::Identity();
46         result.SetValue(0 * kFour + 0, s.GetDataX());
47         result.SetValue(1 * kFour + 0, s.GetDataY());
48         result.SetValue(kTwo * kFour + 0, s.GetDataZ());
49 
50         result.SetValue(0 * kFour + 1, u.GetDataX());
51         result.SetValue(1 * kFour + 1, u.GetDataY());
52         result.SetValue(kTwo * kFour + 1, u.GetDataZ());
53 
54         result.SetValue(0 * kFour + kTwo, -f.GetDataX());
55         result.SetValue(1 * kFour + kTwo, -f.GetDataY());
56         result.SetValue(kTwo * kFour + kTwo, -f.GetDataZ());
57 
58         result.SetValue(kThree * kFour + 0, -Vector3::Dot(s, eye));
59         result.SetValue(kThree * kFour + 1, -Vector3::Dot(u, eye));
60         result.SetValue(kThree * kFour + kTwo, Vector3::Dot(f, eye));
61         return result;
62     };
63 };
64 
65 #endif // MY_X_COMPONENT_ALGORITHM_H
66