Guest User

Untitled

a guest
Sep 5th, 2024
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.58 KB | None | 0 0
  1. #include <algorithm>
  2. #include <array>
  3. #include <cstring>
  4. #include <glad/glad.h>
  5. #include <GLFW/glfw3.h>
  6. #include <glm/glm.hpp>
  7. #include <iostream>
  8. #include <glm/glm.hpp>
  9. #include <iterator>
  10. #include <vector>
  11. #include "camera.hpp"
  12. #include "glm/ext/matrix_float4x4.hpp"
  13. #include "glm/ext/matrix_transform.hpp"
  14. #include "glm/ext/vector_float3.hpp"
  15. #include "glm/trigonometric.hpp"
  16. #include "glm/ext/matrix_clip_space.hpp"
  17.  
  18. #include "shader.hpp"
  19. #include "cubes.hpp"
  20. #include "triangles.hpp"
  21.  
  22. void framebuffer_change_callback(GLFWwindow* window, int width, int height);
  23. void cursor_position_callback(GLFWwindow* window, double xpos, double ypos);
  24.  
  25. Camera camera = Camera(glm::vec3(0.0f,0.0f,1.0f), glm::vec3(0.0f,0.0f,0.0f), 80.0f);
  26.  
  27. float mouse_xpos = 0.0f;
  28. float mouse_ypos = 0.0f;
  29.  
  30. int main(){
  31.  
  32.  
  33.   glfwInit();
  34.   glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
  35.   glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
  36.   glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  37.  
  38.   GLFWwindow* window = glfwCreateWindow(600, 400, "Test", nullptr, nullptr);
  39.  
  40.   glfwMakeContextCurrent(window);
  41.  
  42.   glfwSetFramebufferSizeCallback(window, framebuffer_change_callback);
  43.   glfwSetCursorPosCallback(window, cursor_position_callback);
  44.  
  45.   if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)){
  46.  
  47.     std::cout << "Error starting Opengl" << std::endl;
  48.     return -1;
  49.  
  50.   }
  51.  
  52.   float vertices[] = {
  53.     0.0f, -0.5f, -0.5,
  54.     0.5f, -0.5f, 0.0f,
  55.     0.0f, -0.5f, 0.5f,
  56.     -0.5f, -0.5f, 0.0f,
  57.  
  58.     0.0f, 0.5f, -0.5f,
  59.     0.5f, 0.5f, 0.0f,
  60.     0.0f, 0.5f, 0.5f,
  61.     -0.5f, 0.5f, 0.0f,
  62.  
  63.     -0.5f, 0.0f, -0.5f,
  64.     0.5f, 0.0f, -0.5f,
  65.     0.5f, 0.0f, 0.5f,
  66.     -0.5f, 0.0f, 0.5f,
  67.  
  68. };
  69.  
  70.   unsigned int VBO, VAO, EBO;
  71.   glGenVertexArrays(1, &VAO);
  72.   glGenBuffers(1, &VBO);
  73.   glGenBuffers(1, &EBO);
  74.  
  75.   glBindVertexArray(VAO);
  76.   glBindBuffer(GL_ARRAY_BUFFER, VBO);
  77.  
  78.   glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
  79.   glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
  80.   glEnableVertexAttribArray(0);
  81.  
  82.   Cube cube = Cube(glm::vec3(0.0,0.0,0.0));
  83.  
  84.   cube.values[0] = 1.0;
  85.   cube.values[1] = 1.0;
  86.  
  87.   int edgeIndex = edgeTable[cube.getTriangleIndex(0.5)];
  88.  
  89.  
  90.   // need to put indices in here
  91.  
  92.   int indices[16];
  93.  
  94.   //segfault
  95.  
  96.   memcpy(indices, triTable[edgeIndex], 16);
  97.  
  98.   // load ebo here
  99.   glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
  100.   glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
  101.  
  102.   Shader shader = Shader("res/shaders/shader.vs", "res/shaders/shader.fs");
  103.  
  104.   glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
  105.  
  106.   //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  107.   glEnable(GL_DEPTH_TEST);
  108.  
  109.   //glEnable(GL_CULL_FACE);
  110.   //glCullFace(GL_BACK);
  111.  
  112.   while(!glfwWindowShouldClose(window)){
  113.    
  114.     float camera_speed = 0.03f;
  115.  
  116.     if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS){
  117.       camera.position -= camera.front * camera_speed;
  118.       camera.target -= camera.front * camera_speed;
  119.     }
  120.     if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS){
  121.       camera.position += camera.front * camera_speed;
  122.       camera.target += camera.front * camera_speed;
  123.     }
  124.     if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS){
  125.       camera.position -= camera.right * camera_speed;
  126.       camera.target -= camera.right * camera_speed;
  127.     }
  128.     if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS){
  129.       camera.position += camera.right * camera_speed;
  130.       camera.target += camera.right * camera_speed;
  131.     }
  132.  
  133.     glClearColor(0.0f,0.0f,0.0f,1.0f);
  134.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  135.  
  136.     int width, height;
  137.     glfwGetWindowSize(window, &width, &height);
  138.  
  139.     glm::mat4 projection = glm::perspective(glm::radians(camera.fov), (float)width / (float)height, 0.1f, 100.0f);
  140.  
  141.     shader.setMat4("projection", projection);
  142.     shader.setMat4("view", camera.view);
  143.     shader.setMat4("model", glm::mat4(1.0f));
  144.  
  145.     glBindVertexArray(VAO);
  146.  
  147.     glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, 0);
  148.  
  149.     shader.use();
  150.  
  151.     camera.update_camera_transform();
  152.     glfwSwapBuffers(window);
  153.     glfwPollEvents();
  154.   }
  155.  
  156.  
  157.  
  158.  
  159.   return 0;
  160. }
  161.  
  162. void framebuffer_change_callback(GLFWwindow* window, int width, int height){
  163.   glViewport(0,0,width,height);
  164. }
  165.  
  166. void cursor_position_callback(GLFWwindow* window, double xpos, double ypos){
  167.   float x_delta = mouse_xpos - xpos;
  168.   float y_delta = mouse_ypos - ypos;
  169.  
  170.   mouse_ypos = ypos;
  171.   mouse_xpos = xpos;
  172.  
  173.   camera.target -= (camera.right * x_delta * 0.01f);
  174.   camera.target += (camera.up * y_delta * 0.01f);
  175. }
  176.  
Advertisement
Add Comment
Please, Sign In to add comment