123 lines
3.5 KiB
C++
123 lines
3.5 KiB
C++
#include <glad/glad.h>
|
|
#include <GLFW/glfw3.h>
|
|
#include <cstring>
|
|
#include <cmath>
|
|
#include <cstdlib>
|
|
#include <ctime>
|
|
#include <iostream>
|
|
#include <glm/glm.hpp>
|
|
#include <glm/gtc/matrix_transform.hpp>
|
|
#include <glm/gtc/type_ptr.hpp>
|
|
|
|
|
|
|
|
#include <coil/coil.hpp>
|
|
#include <coil/segment.hpp>
|
|
#include <utils/utils.hpp>
|
|
#include <camera/camera.hpp>
|
|
|
|
|
|
|
|
using std::cerr;
|
|
using std::cout;
|
|
using std::endl;
|
|
|
|
#define RAND_0_1 ((float)rand()) / RAND_MAX
|
|
#define DISPLAY_WIDTH 800
|
|
#define DISPLAY_HEIGHT 600
|
|
|
|
void framebufferSizeCallback(GLFWwindow *window, int width, int height) {
|
|
glViewport(10, 10, width-10, height-10);
|
|
}
|
|
|
|
Camera cam = Camera(glm::vec3(0., 0., 0), glm::vec3(0., 1.,0.),-89, 0, 20, 0.1, CameraType::FREE);
|
|
|
|
|
|
void processInput(GLFWwindow *window) {
|
|
if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
|
|
glfwSetWindowShouldClose(window, true);
|
|
return;
|
|
}
|
|
|
|
CameraMovement dir;
|
|
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
|
|
dir = CameraMovement::FORWARD;
|
|
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
|
|
dir = CameraMovement::BACKWARD;
|
|
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
|
|
dir = CameraMovement::LEFT;
|
|
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
|
|
dir = CameraMovement::RIGHT;
|
|
|
|
cam.processKeyboardInput(dir);
|
|
}
|
|
|
|
void processMousePosCallback(GLFWwindow *window, double deltaX, double deltaY) {
|
|
cam.processMouseInput(window, deltaX, deltaY);
|
|
}
|
|
|
|
int main () {
|
|
Coil coil = Coil(2025, 3, 10, 2, 50, 10);
|
|
|
|
srand(time(0));
|
|
glfwInit();
|
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
|
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
|
#ifdef __APPLE__
|
|
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
|
|
#endif
|
|
|
|
GLFWwindow *window = glfwCreateWindow(DISPLAY_WIDTH, DISPLAY_HEIGHT, "TimeCoil", NULL, NULL);
|
|
if (window == NULL) {
|
|
cerr << "Failed to create GLFW window." << endl;
|
|
gracefulExit(-1);
|
|
}
|
|
|
|
glfwMakeContextCurrent(window);
|
|
glfwSetFramebufferSizeCallback(window, framebufferSizeCallback);
|
|
glfwSetCursorPosCallback(window, processMousePosCallback);
|
|
// glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_CAPTURED);
|
|
|
|
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
|
|
cerr << "Failed to initialize GLAD." << endl;
|
|
gracefulExit(-1);
|
|
}
|
|
|
|
uint64_t shaderProgram = prepareShaders();
|
|
glUseProgram(shaderProgram);
|
|
|
|
glm::mat4 projection = glm::mat4(1.0f);
|
|
projection = glm::perspective(glm::radians(50.0f), (float)DISPLAY_WIDTH / (float)DISPLAY_HEIGHT, 0.001f, 2000.0f);
|
|
|
|
unsigned int projectionLoc = glGetUniformLocation(shaderProgram, "projection");
|
|
glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection));
|
|
|
|
// glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
|
coil.initGLBuffers();
|
|
glEnable(GL_DEPTH_TEST);
|
|
while(!glfwWindowShouldClose(window)) {
|
|
processInput(window);
|
|
|
|
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
|
|
glm::mat4 model = glm::mat4(1.0f);
|
|
glm::mat4 view = glm::mat4(1.0f);
|
|
model = glm::rotate(model, glm::radians(5.0f), glm::vec3(1.0f, 0.0f, 0.0f));
|
|
view = cam.getViewMatrix();
|
|
unsigned int modelLoc = glGetUniformLocation(shaderProgram, "model");
|
|
unsigned int viewLoc = glGetUniformLocation(shaderProgram, "view");
|
|
|
|
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
|
|
glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
|
|
|
|
coil.render();
|
|
|
|
glfwSwapBuffers(window);
|
|
glfwPollEvents();
|
|
}
|
|
|
|
gracefulExit(0);
|
|
}
|