-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModuleCamera.cpp
More file actions
136 lines (108 loc) · 4.43 KB
/
Copy pathModuleCamera.cpp
File metadata and controls
136 lines (108 loc) · 4.43 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include "Globals.h"
#include "ModuleCamera.h"
#include "Application.h"
#include "Mouse.h"
#include "Keyboard.h"
#include "ModuleD3D12.h"
#include "ModuleImGui.h"
bool ModuleCamera::init() {
position = Vector3(0.0f, 2.0f, 10.0f);
rotation = Quaternion::Identity;
// Inicializar parámetros como hace tu profesor
params.polar = 0.0f;
params.azimuthal = 0.0f;
params.translation = position;
// Inicializar proyección
projection = Matrix::CreatePerspectiveFieldOfView(fov, aspectRatio, nearPlane, farPlane);
// Inicializar vista como hace tu profesor
Quaternion invRot;
rotation.Inverse(invRot);
view = Matrix::CreateFromQuaternion(invRot);
view.Translation(-position);
return true;
}
void ModuleCamera::update() {
auto kb = DirectX::Keyboard::Get().GetState();
auto ms = DirectX::Mouse::Get().GetState();
float dt = app->getDeltaTime();
ModuleImGui* m_imgui = app->getImGui();
bool imguiWantsMouse = m_imgui ? m_imgui->wantsCaptureMouse() : false;
bool imguiWantsKeyboard = m_imgui ? m_imgui->wantsCaptureKeyboard() : false;
float currentSpeed = speed * (kb.LeftShift ? 3.0f : 1.0f);
//Movement - only if ImGui doesn't want keyboard
if (!imguiWantsKeyboard) {
Vector3 translate = Vector3::Zero;
if (kb.W) translate.z -= 1.0f;
if (kb.S) translate.z += 1.0f;
if (kb.D) translate.x += 1.0f;
if (kb.A) translate.x -= 1.0f;
if (kb.Q) translate.y += 1.0f; // Up
if (kb.E) translate.y -= 1.0f; // Down
if (translate != Vector3::Zero) {
translate.Normalize();
Vector3 localDir = Vector3::Transform(translate, rotation);
position += localDir * currentSpeed * dt;
params.translation = position;
}
}
// ZOOM with mouse wheel - only if ImGui doesn't want mouse
if (!imguiWantsMouse && ms.scrollWheelValue != lastScrollValue) {
int delta = ms.scrollWheelValue - lastScrollValue;
float zoomSpeed = 0.01f;
Vector3 forward = GetForward();
position += forward * (delta * zoomSpeed);
params.translation = position;
lastScrollValue = ms.scrollWheelValue;
}
// Rotation - only if ImGui doesn't want mouse
if (!imguiWantsMouse && ms.leftButton) {
float dx = (ms.x - dragPosX) * sensitivity * dt;
float dy = (ms.y - dragPosY) * sensitivity * dt;
params.polar -= dx; // Horizontal rotation (yaw)
params.azimuthal -= dy; // Vertical rotation (pitch)
// Limit pitch
params.azimuthal = std::max(-1.5f, std::min(1.5f, params.azimuthal));
}
dragPosX = ms.x;
dragPosY = ms.y;
//Update rotation
Quaternion rotation_polar = Quaternion::CreateFromAxisAngle(Vector3(0.0f, 1.0f, 0.0f), params.polar);
Quaternion rotation_azimuthal = Quaternion::CreateFromAxisAngle(Vector3(1.0f, 0.0f, 0.0f), params.azimuthal);
rotation = rotation_azimuthal * rotation_polar; // In this orden
position = params.translation; // Synchronize position
// Update view matrix
Quaternion invRot;
rotation.Inverse(invRot);
view = Matrix::CreateFromQuaternion(invRot);
view.Translation(Vector3::Transform(-position, invRot));
}
const Matrix& ModuleCamera::GetProjectionMatrix() const {
return projection;
}
void ModuleCamera::SetAspectRatio(float aspect) {
this->aspectRatio = aspect;
projection = Matrix::CreatePerspectiveFieldOfView(fov, aspectRatio, nearPlane, farPlane);
}
void ModuleCamera::SetFOV(float fovRadians) {
this->fov = fovRadians;
projection = Matrix::CreatePerspectiveFieldOfView(fov, aspectRatio, nearPlane, farPlane);
}
void ModuleCamera::SetPlaneDistances(float nearP, float farP) {
this->nearPlane = nearP;
this->farPlane = farP;
projection = Matrix::CreatePerspectiveFieldOfView(fov, aspectRatio, nearPlane, farPlane);
}
void ModuleCamera::LookAt(const Vector3& target) {
// Implementación más simple siguiendo el estilo de tu profesor
Vector3 forward = target - position;
forward.Normalize();
// Crear una vista LookAt
view = Matrix::CreateLookAt(position, target, Vector3(0, 1, 0));
// Extraer rotación de la matriz de vista
Matrix rotationMatrix = view;
rotationMatrix.Invert(); // La vista es la inversa de la matriz de la cámara
rotation = Quaternion::CreateFromRotationMatrix(rotationMatrix);
rotation.Normalize();
// Actualizar params
params.translation = position;
}