-
Notifications
You must be signed in to change notification settings - Fork 0
/
main3d.cpp
431 lines (360 loc) · 13.6 KB
/
main3d.cpp
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
#include <iostream>
#include <filesystem>
#include <string>
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include "shader.cpp"
#include "rubicscube.cpp"
// Global variables that hold the state of the game
RubicsCube game;
RotationManager rotation_manager(&game);
// Camera state
glm::vec3 cameraPos = glm::vec3(0.0f, 0.0f, 5.0f);
glm::vec3 cameraFront = glm::vec3(0.0f, 0.0f, -1.0f);
glm::vec3 cameraUp = glm::vec3(0.0f, 1.0f, 0.0f);
// Key states for the game
bool keyMajPressed = false;
bool keyFPressed = false;
bool keyRPressed = false;
bool keyUPressed = false;
bool key1_pressed = false;
bool key2_pressed = false;
bool key3_pressed = false;
bool key4_pressed = false;
bool key5_pressed = false;
bool key6_pressed = false;
unsigned int yellow, red, white, blue, orange, green, none;
unsigned int color_to_code(Color c) {
switch (c)
{
case Color::YELLOW: return yellow;
case Color::GREEN: return green;
case Color::RED: return red;
case Color::BLUE: return blue;
case Color::ORANGE: return orange;
case Color::WHITE: return white;
case Color::NONE: return none;
}
return 0;
}
/**
* Process all inputs for the rubicscube solver
*/
void processInput(GLFWwindow *window)
{
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
const float cameraSpeed = 0.15f; // adjust accordingly
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
cameraPos -= 0.1f * cameraSpeed * cameraPos;
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
cameraPos += 0.1f * cameraSpeed * cameraPos;
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
cameraPos += glm::normalize(glm::cross(cameraPos, cameraUp)) * cameraSpeed;
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
cameraPos -= glm::normalize(glm::cross(cameraPos, cameraUp)) * cameraSpeed;
// ups and downs
if (glfwGetKey(window, GLFW_KEY_K) == GLFW_PRESS)
cameraPos += cameraUp * cameraSpeed;
if (glfwGetKey(window, GLFW_KEY_J) == GLFW_PRESS)
cameraPos -= cameraUp * cameraSpeed;
// Detect if maj is pressed (to do backward motion)
if (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS) {
if (!keyMajPressed) keyMajPressed = true;
} else if (keyMajPressed) {
keyMajPressed = false;
}
// Game actions: F, R, U
if (glfwGetKey(window, GLFW_KEY_F) == GLFW_PRESS) {
if (!keyFPressed) {
keyFPressed = true;
if (rotation_manager.is_free()) {
rotation_manager.start_motion(Motion::F, !keyMajPressed);
}
}
} else if (keyFPressed) {
keyFPressed = false;
}
if (glfwGetKey(window, GLFW_KEY_R) == GLFW_PRESS) {
if (!keyRPressed) {
keyRPressed = true;
if (rotation_manager.is_free()) {
rotation_manager.start_motion(Motion::R, !keyMajPressed);
}
}
} else if (keyRPressed) {
keyRPressed = false;
}
if (glfwGetKey(window, GLFW_KEY_U) == GLFW_PRESS) {
if (!keyUPressed) {
keyUPressed = true;
if (rotation_manager.is_free()) {
rotation_manager.start_motion(Motion::U, !keyMajPressed);
}
}
} else if (keyUPressed) {
keyUPressed = false;
}
// Game actions: 1,2,3,4,5,6 (to change colors)
if (glfwGetKey(window, GLFW_KEY_1) == GLFW_PRESS) {
if (!key1_pressed) {
key1_pressed = true;
game.set_main_color(Color::WHITE);
}
} else if (key1_pressed) {
key1_pressed = false;
}
if (glfwGetKey(window, GLFW_KEY_2) == GLFW_PRESS) {
if (!key2_pressed) {
key2_pressed = true;
game.set_main_color(Color::BLUE);
}
} else if (key2_pressed) {
key2_pressed = false;
}
if (glfwGetKey(window, GLFW_KEY_3) == GLFW_PRESS) {
if (!key3_pressed) {
key3_pressed = true;
game.set_main_color(Color::YELLOW);
}
} else if (key3_pressed) {
key3_pressed = false;
}
if (glfwGetKey(window, GLFW_KEY_4) == GLFW_PRESS) {
if (!key4_pressed) {
key4_pressed = true;
game.set_main_color(Color::GREEN);
}
} else if (key4_pressed) {
key4_pressed = false;
}
if (glfwGetKey(window, GLFW_KEY_5) == GLFW_PRESS) {
if (!key5_pressed) {
key5_pressed = true;
game.set_main_color(Color::RED);
}
} else if (key5_pressed) {
key5_pressed = false;
}
if (glfwGetKey(window, GLFW_KEY_6) == GLFW_PRESS) {
if (!key6_pressed) {
key6_pressed = true;
game.set_main_color(Color::ORANGE);
}
} else if (key6_pressed) {
key6_pressed = false;
}
}
// glfw: whenever the window size changed (by OS or user resize) this callback function executes
void framebuffer_size_callback(GLFWwindow *window, int width, int height)
{
// make sure the viewport matches the new window dimensions; note that width and
// height will be significantly larger than specified on retina displays.
glViewport(0, 0, width, height);
}
/**
* A function that loads a texture from a given path
*/
void load_gl_texture(unsigned int& id, const char* path, bool rgba = false) {
glGenTextures(1, &id);
glBindTexture(GL_TEXTURE_2D, id);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// load image, create texture and generate mipmaps
int width, height, nrChannels;
stbi_set_flip_vertically_on_load(true); // tell stb_image.h to flip loaded texture's on the y-axis.
unsigned char *data = stbi_load(path, &width, &height, &nrChannels, 0);
if (data)
{
if (rgba)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
else
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
}
else
{
std::cout << "Failed to load texture" << std::endl;
}
stbi_image_free(data);
}
// settings
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;
using std::cout;
using std::endl;
int main()
{
// glfw: initialize and configure
// ------------------------------
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// glfw window creation
// --------------------
GLFWwindow *window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
// build and compile our shader zprogram
// ------------------------------------
// Shader ourShader("6.1.coordinate_systems.vs", "6.1.coordinate_systems.fs");
Shader ourShader("/home/arthur/dev/cpp/tuto1/shader_vert.glsl", "/home/arthur/dev/cpp/tuto1/shader_frag.glsl");
// set up vertex data (and buffer(s)) and configure vertex attributes
// ------------------------------------------------------------------
/*
* Faces are each assigned to a number as defined below
Back = 0
Front = 1
Left = 2
Right = 3
Bottom = 4
Top = 5
*/
float vertices[] = {
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, 0.0f,
0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 1.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 1.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
-0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 2.0f,
-0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 2.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f, 2.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f, 2.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 2.0f,
-0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 2.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 3.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 3.0f,
0.5f, -0.5f, -0.5f, 0.0f, 1.0f, 3.0f,
0.5f, -0.5f, -0.5f, 0.0f, 1.0f, 3.0f,
0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 3.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 3.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f, 4.0f,
0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 4.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 4.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 4.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 4.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f, 4.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 5.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 5.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 5.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 5.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 5.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 5.0f
};
// Setup VBO, VAO and EBO
unsigned int VBO, VAO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
// position attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void *)0);
glEnableVertexAttribArray(0);
// texture coord attribute
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void *)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
// load and create a texture
// -------------------------
load_gl_texture(yellow, "/home/arthur/dev/cpp/tuto1/resources/yellow.png");
load_gl_texture(red, "/home/arthur/dev/cpp/tuto1/resources/red.png");
load_gl_texture(white, "/home/arthur/dev/cpp/tuto1/resources/white.png");
load_gl_texture(blue, "/home/arthur/dev/cpp/tuto1/resources/blue.png");
load_gl_texture(orange, "/home/arthur/dev/cpp/tuto1/resources/orange.png");
load_gl_texture(green, "/home/arthur/dev/cpp/tuto1/resources/green.png");
load_gl_texture(none, "/home/arthur/dev/cpp/tuto1/resources/selected.png", true);
ourShader.use();
ourShader.setInt("texture0", 0);
ourShader.setInt("texture1", 1);
ourShader.setInt("texture2", 2);
ourShader.setInt("texture3", 3);
ourShader.setInt("texture4", 4);
ourShader.setInt("texture5", 5);
ourShader.setInt("textureNone", 6);
// Activate depth buffer
glEnable(GL_DEPTH_TEST);
game.set_main_color(Color::BLUE);
// Wireframe mode ?
// glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
// render loop
Color colors[3] = {Color::NONE, Color::NONE, Color::NONE};
while (!glfwWindowShouldClose(window))
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
processInput(window);
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// Makes every rotation
rotation_manager.step();
// activate shader
ourShader.use();
// create transformations
glm::mat4 model = glm::mat4(1.0f);
glm::mat4 view = glm::mat4(1.0f);
glm::mat4 projection = glm::mat4(1.0f);
// Setup the camera
view = glm::lookAt(cameraPos, vec3(0., 0., 0.), cameraUp);
projection = glm::perspective(glm::radians(70.0f), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
ourShader.setMat4("model", model);
ourShader.setMat4("view", view);
ourShader.setMat4("projection", projection);
glActiveTexture(GL_TEXTURE6);
glBindTexture(GL_TEXTURE_2D, none);
glBindVertexArray(VAO);
for (const auto& cube: game.cubes) {
// Get the colors of the cube
cube.fillColors(colors);
// FRONT
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, color_to_code(colors[0]));
// RIGHT
glActiveTexture(GL_TEXTURE3);
glBindTexture(GL_TEXTURE_2D, color_to_code(colors[1]));
// TOP
glActiveTexture(GL_TEXTURE5);
glBindTexture(GL_TEXTURE_2D, color_to_code(colors[2]));
// also set the BOTTOM color to allow for swapping axis
glActiveTexture(GL_TEXTURE4);
glBindTexture(GL_TEXTURE_2D, color_to_code(colors[2]));
// Is this cube on the main face ?
ourShader.setBool("onCurrentFace", game.is_cube_on_selected_face(cube));
// Set the model matrix to the transform of the cube and then render
ourShader.setMat4("model", cube.transform);
glDrawArrays(GL_TRIANGLES, 0, 36);
}
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
glfwSwapBuffers(window);
glfwPollEvents();
}
// glfw: terminate, clearing all previously allocated GLFW resources.
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
glfwTerminate();
return 0;
}