• 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 CAMERA_H
17 #define CAMERA_H
18 
19 #include "Algorithm.h"
20 #include "Matrix4x4.h"
21 #include "Vector3.h"
22 
23 // 定义相机移动的几个可能选项。用作抽象,以远离特定于窗口系统的输入方法
24 enum CameraMovement { FORWARD, BACKWARD, LEFT, RIGHT };
25 
26 // Default camera values
27 const float YAW = -90.0f;
28 const float PITCH = 0.0f;
29 const float SPEED = 2.5f;
30 const float SENSITIVITY = 0.1f;
31 const float ZOOM = 45.0f;
32 
33 // 一个抽象的相机类,用于处理输入并计算相应的欧拉角、矢量和矩阵,以便在OpenGL中使用
34 class Camera {
35 public:
36     // 相机属性值
37     Vector3 Position{0.0f, 0.0f, 0.0f};
38     Vector3 Front{0.0f, 0.0f, -1.0f};
39     Vector3 Up{0.0f, 1.0f, 0.0f};
40     Vector3 Right{0.0f, 0.0f, 0.0f};
41     Vector3 WorldUp{0.0f, 0.0f, 0.0f};
42     // 欧拉角度
43     float Yaw = YAW;
44     float Pitch = PITCH;
45     // 相机选项
46     float MovementSpeed = SPEED;
47     float MouseSensitivity = SENSITIVITY;
48     float Zoom = ZOOM;
49 
50     // 向量的构造函数
Camera(Vector3 position)51     Camera(Vector3 position)
52     {
53         Position = position;
54         WorldUp = Up;
55         updateCameraVectors();
56     }
57     // 返回使用“欧拉角”和“注视矩阵”计算的视图矩阵
GetViewMatrix()58     Matrix4x4 GetViewMatrix() { return Algorithm::LookAt(Position, Vector3::Add(Position, Front), Up); }
59 
60 private:
61     // 根据摄影机的(更新的)Euler角度计算前向量
updateCameraVectors()62     void updateCameraVectors()
63     {
64         // 计算新的Front矢量
65         Vector3 front = {0, 0, 0};
66         front.SetDataX(cos(Algorithm::Radians(Yaw)) * cos(Algorithm::Radians(Pitch)));
67         front.SetDataY(Algorithm::Radians(Pitch));
68         front.SetDataZ(Algorithm::Radians(Yaw) * cos(Algorithm::Radians(Pitch)));
69         Front = front.Normalize(front);
70         // 同时重新计算向右和向上矢量
71         // 对向量进行归一化,因为向上或向下看得越多,它们的长度就越接近0,这会导致移动速度变慢。
72         Right = Vector3::Normalize(Vector3::Cross(Front, WorldUp));
73         Up = Vector3::Normalize(Vector3::Cross(Right, Front));
74     }
75 };
76 #endif