2024-09-27 16:25:26 +00:00
|
|
|
#include <GL/glew.h>
|
|
|
|
#include <GL/gl.h>
|
|
|
|
#include <GLFW/glfw3.h>
|
2024-10-01 17:03:43 +00:00
|
|
|
#include <glm/ext/matrix_transform.hpp>
|
|
|
|
#include <glm/ext/quaternion_trigonometric.hpp>
|
2024-09-27 21:01:23 +00:00
|
|
|
#include <glm/ext/vector_float3.hpp>
|
2024-10-01 17:03:43 +00:00
|
|
|
#include <glm/gtc/quaternion.hpp>
|
2025-01-17 08:24:18 +00:00
|
|
|
#include <memory>
|
2024-09-27 16:25:26 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2025-01-17 08:24:18 +00:00
|
|
|
#include "objects/part.h"
|
2024-09-27 20:48:57 +00:00
|
|
|
#include "rendering/renderer.h"
|
2024-09-28 13:55:27 +00:00
|
|
|
#include "physics/simulation.h"
|
2024-09-27 20:34:32 +00:00
|
|
|
#include "camera.h"
|
2024-09-27 16:31:14 +00:00
|
|
|
|
2024-11-25 20:51:17 +00:00
|
|
|
#include "common.h"
|
2024-09-27 16:25:26 +00:00
|
|
|
|
2024-11-25 20:51:17 +00:00
|
|
|
void errorCatcher(int id, const char* str);
|
2024-09-27 20:34:32 +00:00
|
|
|
|
2024-09-27 21:16:52 +00:00
|
|
|
int mode = 0;
|
|
|
|
|
2024-09-27 21:01:23 +00:00
|
|
|
void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods);
|
2024-09-27 20:34:32 +00:00
|
|
|
void processInput(GLFWwindow* window);
|
|
|
|
void mouseCallback(GLFWwindow* window, double xpos, double ypos);
|
|
|
|
// void scroll_callback(GLFWwindow* window, double xoffset, double yoffset);
|
|
|
|
void mouseButtonCallback(GLFWwindow* window, int button, int action, int mods);
|
|
|
|
|
2025-01-17 08:24:18 +00:00
|
|
|
std::shared_ptr<Part> lastPart;
|
|
|
|
|
2024-09-27 16:25:26 +00:00
|
|
|
int main() {
|
|
|
|
glfwSetErrorCallback(errorCatcher);
|
|
|
|
|
|
|
|
glfwInit();
|
2024-11-25 21:20:39 +00:00
|
|
|
GLFWwindow *window = glfwCreateWindow(1200, 900, "OpenBlocks Client ALPHA", NULL, NULL);
|
2024-09-27 21:01:23 +00:00
|
|
|
glfwSetKeyCallback(window, keyCallback);
|
2024-09-27 20:34:32 +00:00
|
|
|
glfwSetMouseButtonCallback(window, mouseButtonCallback);
|
|
|
|
glfwSetCursorPosCallback(window, mouseCallback);
|
2024-09-27 16:25:26 +00:00
|
|
|
|
|
|
|
glfwMakeContextCurrent(window);
|
2024-09-27 16:31:14 +00:00
|
|
|
glewInit();
|
|
|
|
|
2024-09-29 17:19:49 +00:00
|
|
|
simulationInit();
|
|
|
|
renderInit(window);
|
|
|
|
|
2024-09-29 17:22:13 +00:00
|
|
|
// Baseplate
|
2025-01-17 08:24:18 +00:00
|
|
|
workspace->AddChild(Part::New({
|
2024-09-29 17:22:13 +00:00
|
|
|
.position = glm::vec3(0, -5, 0),
|
|
|
|
.rotation = glm::vec3(0),
|
2024-11-17 12:11:53 +00:00
|
|
|
.scale = glm::vec3(512, 1.2, 512),
|
2024-09-29 17:22:13 +00:00
|
|
|
.material = Material {
|
2024-11-17 12:11:53 +00:00
|
|
|
.diffuse = glm::vec3(0.388235, 0.372549, 0.384314),
|
2024-09-29 17:22:13 +00:00
|
|
|
.specular = glm::vec3(0.5f, 0.5f, 0.5f),
|
|
|
|
.shininess = 32.0f,
|
|
|
|
},
|
|
|
|
.anchored = true,
|
2025-01-17 08:24:18 +00:00
|
|
|
}));
|
2024-09-29 17:22:13 +00:00
|
|
|
|
2025-01-17 08:24:18 +00:00
|
|
|
workspace->AddChild(lastPart = Part::New({
|
2024-09-27 21:01:23 +00:00
|
|
|
.position = glm::vec3(0),
|
|
|
|
.rotation = glm::vec3(0),
|
2024-11-17 12:11:53 +00:00
|
|
|
.scale = glm::vec3(4, 1.2, 2),
|
2024-09-27 21:01:23 +00:00
|
|
|
.material = Material {
|
2024-11-17 12:11:53 +00:00
|
|
|
.diffuse = glm::vec3(0.639216f, 0.635294f, 0.647059f),
|
2024-09-27 21:01:23 +00:00
|
|
|
.specular = glm::vec3(0.5f, 0.5f, 0.5f),
|
|
|
|
.shininess = 32.0f,
|
|
|
|
}
|
2025-01-17 08:24:18 +00:00
|
|
|
}));
|
|
|
|
|
|
|
|
for (InstanceRef inst : workspace->GetChildren()) {
|
|
|
|
if (inst->GetClass()->className != "Part") continue;
|
|
|
|
std::shared_ptr<Part> part = std::dynamic_pointer_cast<Part>(inst);
|
|
|
|
syncPartPhysics(part);
|
|
|
|
}
|
2024-09-27 16:31:14 +00:00
|
|
|
|
2024-09-28 13:55:27 +00:00
|
|
|
float lastTime = glfwGetTime();
|
2024-09-27 16:25:26 +00:00
|
|
|
do {
|
2024-09-28 13:55:27 +00:00
|
|
|
float deltaTime = glfwGetTime() - lastTime;
|
|
|
|
lastTime = glfwGetTime();
|
|
|
|
|
2024-09-27 20:34:32 +00:00
|
|
|
processInput(window);
|
2024-09-28 13:55:27 +00:00
|
|
|
physicsStep(deltaTime);
|
2024-09-27 16:31:14 +00:00
|
|
|
render(window);
|
|
|
|
|
2024-09-27 16:25:26 +00:00
|
|
|
glfwSwapBuffers(window);
|
|
|
|
glfwPollEvents();
|
|
|
|
} while(!glfwWindowShouldClose(window));
|
2024-09-27 20:34:32 +00:00
|
|
|
|
|
|
|
glfwTerminate();
|
|
|
|
return 0;
|
2024-09-27 16:25:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void errorCatcher(int id, const char* str) {
|
|
|
|
printf("Something *terrible* happened. Here's the briefing: [%d] %s\n", id, str);
|
2024-09-27 20:34:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
float lastTime;
|
|
|
|
void processInput(GLFWwindow* window) {
|
|
|
|
float deltaTime = glfwGetTime() - lastTime;
|
|
|
|
lastTime = glfwGetTime();
|
|
|
|
|
|
|
|
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
|
|
|
|
camera.processMovement(DIRECTION_FORWARD, deltaTime);
|
|
|
|
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
|
|
|
|
camera.processMovement(DIRECTION_BACKWARDS, deltaTime);
|
|
|
|
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
|
|
|
|
camera.processMovement(DIRECTION_LEFT, deltaTime);
|
|
|
|
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
|
|
|
|
camera.processMovement(DIRECTION_RIGHT, deltaTime);
|
2024-09-27 21:21:27 +00:00
|
|
|
if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS)
|
|
|
|
camera.processMovement(DIRECTION_UP, deltaTime);
|
|
|
|
if (glfwGetKey(window, GLFW_KEY_LEFT_CONTROL) == GLFW_PRESS)
|
|
|
|
camera.processMovement(DIRECTION_DOWN, deltaTime);
|
2024-09-27 21:16:52 +00:00
|
|
|
|
|
|
|
if (mode == 2) {
|
|
|
|
float shiftFactor = (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS) ? -0.5 : 0.5;
|
|
|
|
shiftFactor *= deltaTime;
|
|
|
|
if (glfwGetKey(window, GLFW_KEY_X) == GLFW_PRESS) {
|
2025-01-17 08:24:18 +00:00
|
|
|
lastPart->rotation *= glm::angleAxis(shiftFactor, glm::vec3(1, 0, 0));
|
|
|
|
syncPartPhysics(lastPart);
|
2024-09-27 21:16:52 +00:00
|
|
|
}
|
|
|
|
if (glfwGetKey(window, GLFW_KEY_Y) == GLFW_PRESS) {
|
2025-01-17 08:24:18 +00:00
|
|
|
lastPart->rotation *= glm::angleAxis(shiftFactor, glm::vec3(0, 1, 0));
|
|
|
|
syncPartPhysics(lastPart);
|
2024-09-27 21:16:52 +00:00
|
|
|
}
|
|
|
|
if (glfwGetKey(window, GLFW_KEY_Z) == GLFW_PRESS) {
|
2025-01-17 08:24:18 +00:00
|
|
|
lastPart->rotation *= glm::angleAxis(shiftFactor, glm::vec3(0, 0, 1));
|
|
|
|
syncPartPhysics(lastPart);
|
2024-09-27 21:16:52 +00:00
|
|
|
}
|
|
|
|
}
|
2024-09-27 20:34:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool mouseCapturing = false;
|
|
|
|
float lastX = 0, lastY = 0;
|
|
|
|
bool mouseFirst = true;
|
|
|
|
void mouseCallback(GLFWwindow* window, double xpos, double ypos) {
|
|
|
|
if (!mouseCapturing) return;
|
|
|
|
|
|
|
|
if (mouseFirst) {
|
|
|
|
lastX = xpos, lastY = ypos;
|
|
|
|
mouseFirst = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
float deltaX = xpos - lastX, deltaY = ypos - lastY;
|
|
|
|
lastX = xpos, lastY = ypos;
|
|
|
|
|
|
|
|
camera.processRotation(deltaX, deltaY);
|
|
|
|
}
|
|
|
|
|
|
|
|
void mouseButtonCallback(GLFWwindow* window, int button, int action, int mods) {
|
|
|
|
if (button == GLFW_MOUSE_BUTTON_2)
|
|
|
|
mouseCapturing = action == GLFW_PRESS;
|
|
|
|
if (mouseCapturing) {
|
|
|
|
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
|
|
|
|
} else {
|
|
|
|
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
|
|
|
|
mouseFirst = true;
|
|
|
|
}
|
|
|
|
}
|
2024-09-27 21:01:23 +00:00
|
|
|
|
|
|
|
void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods) {
|
|
|
|
if (key == GLFW_KEY_F && action == GLFW_PRESS) {
|
2025-01-17 08:24:18 +00:00
|
|
|
workspace->AddChild(lastPart = Part::New({
|
2024-09-27 21:01:23 +00:00
|
|
|
.position = camera.cameraPos + camera.cameraFront * glm::vec3(3),
|
|
|
|
.rotation = glm::vec3(0),
|
|
|
|
.scale = glm::vec3(1, 1, 1),
|
|
|
|
.material = Material {
|
|
|
|
.diffuse = glm::vec3(1.0f, 0.5f, 0.31f),
|
|
|
|
.specular = glm::vec3(0.5f, 0.5f, 0.5f),
|
|
|
|
.shininess = 32.0f,
|
|
|
|
}
|
2025-01-17 08:24:18 +00:00
|
|
|
}));
|
|
|
|
syncPartPhysics(lastPart);
|
2024-09-27 21:01:23 +00:00
|
|
|
}
|
2024-09-27 21:16:52 +00:00
|
|
|
|
|
|
|
float shiftFactor = (mods & GLFW_MOD_SHIFT) ? -0.2 : 0.2;
|
|
|
|
if (mode == 0) {
|
|
|
|
if (key == GLFW_KEY_X && action == GLFW_PRESS) {
|
2025-01-17 08:24:18 +00:00
|
|
|
lastPart->position.x += shiftFactor;
|
|
|
|
syncPartPhysics(lastPart);
|
2024-09-27 21:16:52 +00:00
|
|
|
}
|
|
|
|
if (key == GLFW_KEY_Y && action == GLFW_PRESS) {
|
2025-01-17 08:24:18 +00:00
|
|
|
lastPart->position.y += shiftFactor;
|
|
|
|
syncPartPhysics(lastPart);
|
2024-09-27 21:16:52 +00:00
|
|
|
}
|
|
|
|
if (key == GLFW_KEY_Z && action == GLFW_PRESS) {
|
2025-01-17 08:24:18 +00:00
|
|
|
lastPart->position.z += shiftFactor;
|
|
|
|
syncPartPhysics(lastPart);
|
2024-09-27 21:16:52 +00:00
|
|
|
}
|
|
|
|
} else if (mode == 1) {
|
|
|
|
if (key == GLFW_KEY_X && action == GLFW_PRESS) {
|
2025-01-17 08:24:18 +00:00
|
|
|
lastPart->scale.x += shiftFactor;
|
|
|
|
syncPartPhysics(lastPart);
|
2024-09-27 21:16:52 +00:00
|
|
|
}
|
|
|
|
if (key == GLFW_KEY_Y && action == GLFW_PRESS) {
|
2025-01-17 08:24:18 +00:00
|
|
|
lastPart->scale.y += shiftFactor;
|
|
|
|
syncPartPhysics(lastPart);
|
2024-09-27 21:16:52 +00:00
|
|
|
}
|
|
|
|
if (key == GLFW_KEY_Z && action == GLFW_PRESS) {
|
2025-01-17 08:24:18 +00:00
|
|
|
lastPart->scale.z += shiftFactor;
|
|
|
|
syncPartPhysics(lastPart);
|
2024-09-27 21:16:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (key == GLFW_KEY_M && action == GLFW_PRESS) mode = 0;
|
|
|
|
if (key == GLFW_KEY_E && action == GLFW_PRESS) mode = 1; // Enlarge
|
|
|
|
if (key == GLFW_KEY_R && action == GLFW_PRESS) mode = 2;
|
2024-09-27 21:01:23 +00:00
|
|
|
}
|