227 lines
6.9 KiB
C++
227 lines
6.9 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>
|
|
|
|
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);
|
|
}
|
|
|
|
void processInput(GLFWwindow *window) {
|
|
if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
|
|
glfwSetWindowShouldClose(window, true);
|
|
}
|
|
}
|
|
|
|
|
|
int main () {
|
|
|
|
Coil c = Coil(2025, 1, 5, 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);
|
|
|
|
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));
|
|
|
|
|
|
c.initGLBuffers();
|
|
|
|
// float vertices[] = {
|
|
// // positions // texture coords
|
|
// 05.5f, 0.5f, 0.0f, 1.0f, 1.0f, // top right
|
|
// 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, // bottom right
|
|
// -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, // bottom left
|
|
// -0.5f, 0.5f, 0.0f, 0.0f, 1.0f // top left
|
|
// };
|
|
// unsigned int indices[] = {
|
|
// 0, 1, 3, // first triangle
|
|
// 1, 2, 3 // second triangle
|
|
// };
|
|
// unsigned int VBO, VAO, EBO;
|
|
// glGenVertexArrays(1, &VAO);
|
|
// glGenBuffers(1, &VBO);
|
|
// glGenBuffers(1, &EBO);
|
|
|
|
// glBindVertexArray(VAO);
|
|
|
|
// glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
|
// glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
|
|
|
// glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
|
|
// glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
|
|
|
|
// // position attribute
|
|
// glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
|
|
// glEnableVertexAttribArray(0);
|
|
// // texture coord attribute
|
|
// glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
|
|
// glEnableVertexAttribArray(1);
|
|
|
|
while(!glfwWindowShouldClose(window)) {
|
|
processInput(window);
|
|
|
|
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
|
|
glClear(GL_COLOR_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 = glm::translate(view, glm::vec3(0.0f, 0.0f, -89.0f));
|
|
|
|
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));
|
|
|
|
c.render();
|
|
// glBindVertexArray(VAO);
|
|
// glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
|
|
|
|
glfwSwapBuffers(window);
|
|
glfwPollEvents();
|
|
}
|
|
|
|
// CoilSegment cs = CoilSegment(10, 0, false);
|
|
// for (auto segment : c.getSegments()) {
|
|
// // segment.printVertices();
|
|
// }
|
|
// cs.printVertices();
|
|
gracefulExit(0);
|
|
// 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(800, 600, "TimeCoil", NULL, NULL);
|
|
// if (window == NULL) {
|
|
// std::cerr << "Failed to create GLFW window." << std::endl;
|
|
// gracefulExit(-1);
|
|
// }
|
|
|
|
// glfwMakeContextCurrent(window);
|
|
// glfwSetFramebufferSizeCallback(window, framebufferSizeCallback);
|
|
|
|
// if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
|
|
// std::cerr << "Failed to initialize GLAD." << std::endl;
|
|
// return -1;
|
|
// }
|
|
|
|
// unsigned int shaderProgram = prepareShaders();
|
|
|
|
// float *vertices = cs.getVertices();
|
|
// unsigned int amountOfVerticies = cs.getVerticesLength();
|
|
// std::cout << amountOfVerticies << std::endl;
|
|
// // float vertices[] = {
|
|
// // -1.0f, -1.0f, 0.0f, 1.0f, 0.0f, 0.0f, // upper left
|
|
// // -0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, // bottom left
|
|
// // 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, // upper right
|
|
// // 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f // bottom right
|
|
// // };
|
|
// // unsigned int indices[] = {
|
|
// // 1, 2, 3,
|
|
// // 1, 2, 0
|
|
// // };
|
|
// unsigned int VBO, VAO, EBO;
|
|
// glGenVertexArrays(1, &VAO);
|
|
// glGenBuffers(1, &VBO);
|
|
// // glGenBuffers(1, &EBO);
|
|
|
|
// glBindVertexArray(VAO);
|
|
|
|
// glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
|
// // glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
|
|
|
|
// glBufferData(GL_ARRAY_BUFFER, amountOfVerticies, vertices, GL_STATIC_DRAW);
|
|
// // glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
|
|
|
|
// glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
|
|
// glEnableVertexAttribArray(0);
|
|
|
|
// glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*) (3 * sizeof(float)));
|
|
// glEnableVertexAttribArray(1);
|
|
|
|
// glBindVertexArray(0);
|
|
|
|
// while(!glfwWindowShouldClose(window)) {
|
|
// processInput(window);
|
|
|
|
// glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
|
|
// glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
// glUseProgram(shaderProgram);
|
|
|
|
// // float time = glfwGetTime();
|
|
// // float greenValue = sin(time);
|
|
|
|
|
|
// // int vertexColorLocation = glGetUniformLocation(shaderProgram, "MyColor");
|
|
// // glUniform4f(vertexColorLocation, 0, greenValue, 0, 1);
|
|
// glBindVertexArray(VAO);
|
|
|
|
// // updateVertices(vertices);
|
|
// glBufferData(GL_ARRAY_BUFFER, amountOfVerticies, vertices, GL_DYNAMIC_DRAW);
|
|
// // glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
|
|
// glDrawArrays(GL_LINE_STRIP_ADJACENCY, -30, amountOfVerticies);
|
|
|
|
|
|
// glBindVertexArray(0);
|
|
// glfwSwapBuffers(window);
|
|
// glfwPollEvents();
|
|
// }
|
|
|
|
// glDeleteVertexArrays(1, &VAO);
|
|
// glDeleteBuffers(1, &VBO);
|
|
// glDeleteProgram(shaderProgram);
|
|
// gracefulExit(0);
|
|
}
|