-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApplication.cpp
More file actions
245 lines (200 loc) · 8.4 KB
/
Application.cpp
File metadata and controls
245 lines (200 loc) · 8.4 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#include "Application.h"
#include <iostream>
#include <vector>
#include <glad/glad.h>
#include <glm/gtc/matrix_transform.hpp>
#include "constants.h"
#include "Cuboid.h"
Application::Application(const int w, const int h, const char *title, DeepCube deep_cube) :
window(nullptr), width(w), height(h), cube(*deep_cube.cube), ai(*deep_cube.ai), solver(*deep_cube.solver) {
if (initWindow(title)) renderer = new Renderer();
}
Application::~Application() {
delete renderer;
glfwTerminate();
}
bool Application::initWindow(const char *title) {glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
window = glfwCreateWindow(width, height, title, NULL, NULL);
if (!window) return false;
glfwMakeContextCurrent(window);
glfwSetWindowUserPointer(window, this);
glfwSetScrollCallback(window, scrollCallback);
glfwSetFramebufferSizeCallback(window, framebufferSizeCallback);
glfwSetKeyCallback(window, keyCallback);
gladLoadGLLoader((GLADloadproc)glfwGetProcAddress);
glEnable(GL_DEPTH_TEST);
return true;
}
void Application::processInput() {
// Close window on Escape
if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
glfwSetWindowShouldClose(window, true);
}
// Mouse Drag Logic
if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS) {
if (!isDragging) {
// We JUST clicked. Store the starting position.
isDragging = true;
glfwGetCursorPos(window, &lastMouseX, &lastMouseY);
} else {
// We are actively dragging. Calculate how far the mouse moved.
double currentX, currentY;
glfwGetCursorPos(window, ¤tX, ¤tY);
double deltaX = currentX - lastMouseX;
double deltaY = currentY - lastMouseY;
// Update camera angles (multiplied by a sensitivity factor)
cameraYaw += (float)deltaX * 0.5f;
cameraPitch += (float)deltaY * 0.5f;
// Clamp the pitch so we don't break our neck (Gimbal Lock)
if (cameraPitch > 89.0f) cameraPitch = 89.0f;
if (cameraPitch < -89.0f) cameraPitch = -89.0f;
// Update last positions for the next frame
lastMouseX = currentX;
lastMouseY = currentY;
}
} else {
// Mouse button was released
isDragging = false;
}
}
void Application::scrollCallback(GLFWwindow *window, double x_offset, double y_offset) {
Application* app = static_cast<Application*>(glfwGetWindowUserPointer(window));
if (app) {
// y_offset is usually +1 for scrolling up, and -1 for scrolling down
app->cameraDistance -= (float)y_offset * 0.5f; // The 0.5f is scroll sensitivity
// Clamp the distance so you can't zoom completely inside the cube or fly into the void
if (app->cameraDistance < 3.0f) app->cameraDistance = 3.0f;
}
}
void Application::framebufferSizeCallback(GLFWwindow *window, int width, int height) {
// 1. Tell OpenGL to adjust its drawing canvas to the new window size
glViewport(0, 0, width, height);
// 2. Update our Application's internal variables so the Camera matrix adapts
if (const auto app = static_cast<Application*>(glfwGetWindowUserPointer(window))) {
app->width = width;
app->height = height;
}
}
void Application::keyCallback(GLFWwindow *window, int key, int scancode, int action, int mods) {
if (action != GLFW_PRESS) return;
const auto app = static_cast<Application*>(glfwGetWindowUserPointer(window));
if (!app) return;
if (app->is_solving) return;
switch (key) {
case GLFW_KEY_SPACE: {
Move move = app->cube.getRandomMove();
std::cout << ">> Performing a random move: " << move.toString() << std::endl;
app->moveQueue.push(move);
break;
}
case GLFW_KEY_R: {
std::cout << ">> Scrambling the cube" << std::endl;
app->animationSpeed = SCRAMBLE_SPEED;
for (int i = 0; i < 20; ++i) {
app->moveQueue.push(app->cube.getRandomMove());
}
break;
}
case GLFW_KEY_S: {
std::cout << ">> Solving with A* search" << std::endl;
app->is_solving = true;
std::shared_ptr thread_safe_cube = app->cube.clone();
app->solution_future = std::async(std::launch::async, [thread_safe_cube, app]() {
return app->solver.solve(*thread_safe_cube, app->ai);
});
break;
}
default: break;
}
}
void Application::run() {
if (!window || !renderer) return;
while (!glfwWindowShouldClose(window)) {
processInput();
glClearColor(0.2f, 0.2f, 0.2f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// 1. Setup Camera (Orbit logic)
glm::mat4 view = glm::mat4(1.0f);
// Step B: Push the whole world away from the camera
view = glm::translate(view, glm::vec3(0.0f, 0.0f, -cameraDistance));
// Step A: Rotate the world based on our mouse angles
view = glm::rotate(view, glm::radians(cameraPitch), glm::vec3(1.0f, 0.0f, 0.0f));
view = glm::rotate(view, glm::radians(cameraYaw), glm::vec3(0.0f, 1.0f, 0.0f));
glm::mat4 projection = glm::perspective(glm::radians(45.0f), (float)width / (float)height, 0.1f, 100.0f);
// --- TIME TRACKING ---
float currentFrame = (float)glfwGetTime();
float deltaTime = currentFrame - lastFrameTime;
lastFrameTime = currentFrame;
// --- CHECK BACKGROUND SOLVER ---
if (is_solving && solution_future.valid()) {
if (solution_future.wait_for(std::chrono::seconds(0)) == std::future_status::ready) {
std::vector<int> solution_path = solution_future.get();
is_solving = false;
if (!solution_path.empty()) {
for (int move_id : solution_path) {
moveQueue.push(cube.getMove(move_id));
}
animationSpeed = SOLVE_SPEED;
}
}
}
// --- QUEUE PROCESSING ---
if (!isAnimating && !moveQueue.empty()) {
Move nextMove = moveQueue.front();
moveQueue.pop();
triggerMove(nextMove);
}
// --- ANIMATION UPDATE ---
if (isAnimating) {
float step = animationSpeed * deltaTime;
if (std::abs(targetAngle - currentAngle) <= step) {
currentAngle = targetAngle;
isAnimating = false;
cube.applyMove(currentMove);
if (moveQueue.empty())
animationSpeed = ANIMATION_SPEED;
} else {
currentAngle += (targetAngle > currentAngle) ? step : -step;
}
}
// --- FETCH & RENDER ---
std::vector<Cubelet> active_state = cube.getRenderState();
for (Cubelet& c : active_state) {
if (isAnimating) {
bool inActiveSlice = false;
glm::vec3 rotAxis;
if (currentMove.axis == X && c.x == currentMove.slice_index) {
inActiveSlice = true; rotAxis = glm::vec3(1.0f, 0.0f, 0.0f);
}
else if (currentMove.axis == Y && c.y == currentMove.slice_index) {
inActiveSlice = true; rotAxis = glm::vec3(0.0f, 1.0f, 0.0f);
}
else if (currentMove.axis == Z && c.z == currentMove.slice_index) {
inActiveSlice = true; rotAxis = glm::vec3(0.0f, 0.0f, 1.0f);
}
if (inActiveSlice) {
glm::mat4 rotMatrix = glm::rotate(glm::mat4(1.0f), glm::radians(currentAngle), rotAxis);
c.transform = rotMatrix * c.transform;
}
}
renderer->draw(c, view, projection);
}
glfwSwapBuffers(window);
glfwPollEvents();
}
}
void Application::triggerMove(const Move &move) {
if (isAnimating) return;
isAnimating = true;
currentMove = move;
currentAngle = 0.0f;
switch (move.rotation) {
case CW: targetAngle = 90.0f; break;
case CCW: targetAngle = -90.0f; break;
case Double: targetAngle = 180.0f; break;
default: break;
}
}