Advertisement
Guest User

drawing lines

a guest
Nov 23rd, 2021
3,309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.24 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <math.h>
  4.  
  5. #include <glad/glad.h>
  6. #include <GLFW/glfw3.h>
  7. #define GLM_ENABLE_EXPERIMENTAL
  8. #include <glm/glm.hpp>
  9. #include <glm/gtc/matrix_transform.hpp>
  10.  
  11. using glm::mat4;
  12. using glm::vec3;
  13. using glm::radians;
  14. using glm::lookAt;
  15. using std::vector;
  16.  
  17. void processInput(GLFWwindow *window);
  18.  
  19. // settings
  20. const unsigned int SCR_WIDTH = 800;
  21. const unsigned int SCR_HEIGHT = 600;
  22.  
  23. float lastX = SCR_WIDTH / 2.0f;
  24. float lastY = SCR_HEIGHT / 2.0f;
  25. bool firstMouse = true;
  26.  
  27. // timing
  28. float deltaTime = 0.0f; // time between current frame and last frame
  29. float lastFrame = 0.0f;
  30.  
  31. class Camera {
  32. public:
  33.     vec3 position;
  34.     Camera() {
  35.         position = vec3(0,0,0);
  36.     }
  37. };
  38. Camera camera;
  39.  
  40. class Line {
  41.     int shaderProgram;
  42.     unsigned int VBO, VAO;
  43.     vector<float> vertices;
  44.     vec3 startPoint;
  45.     vec3 endPoint;
  46.     mat4 MVP = mat4(1.0);
  47.     vec3 lineColor;
  48. public:
  49.     Line(vec3 start, vec3 end) {
  50.  
  51.         startPoint = start;
  52.         endPoint = end;
  53.         lineColor = vec3(1,1,1);
  54.  
  55.         const char *vertexShaderSource = "#version 330 core\n"
  56.             "layout (location = 0) in vec3 aPos;\n"
  57.             "uniform mat4 MVP;\n"
  58.             "void main()\n"
  59.             "{\n"
  60.             "   gl_Position = MVP * vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
  61.             "}\0";
  62.         const char *fragmentShaderSource = "#version 330 core\n"
  63.             "out vec4 FragColor;\n"
  64.             "uniform vec3 color;\n"
  65.             "void main()\n"
  66.             "{\n"
  67.             "   FragColor = vec4(color, 1.0f);\n"
  68.             "}\n\0";
  69.  
  70.         // vertex shader
  71.         int vertexShader = glCreateShader(GL_VERTEX_SHADER);
  72.         glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
  73.         glCompileShader(vertexShader);
  74.         // check for shader compile errors
  75.  
  76.         // fragment shader
  77.         int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
  78.         glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
  79.         glCompileShader(fragmentShader);
  80.         // check for shader compile errors
  81.  
  82.         // link shaders
  83.         shaderProgram = glCreateProgram();
  84.         glAttachShader(shaderProgram, vertexShader);
  85.         glAttachShader(shaderProgram, fragmentShader);
  86.         glLinkProgram(shaderProgram);
  87.         // check for linking errors
  88.  
  89.         glDeleteShader(vertexShader);
  90.         glDeleteShader(fragmentShader);
  91.  
  92.         vertices = {
  93.              start.x, start.y, start.z,
  94.              end.x, end.y, end.z,
  95.  
  96.         };
  97.        
  98.         glGenVertexArrays(1, &VAO);
  99.         glGenBuffers(1, &VBO);
  100.         glBindVertexArray(VAO);
  101.  
  102.         glBindBuffer(GL_ARRAY_BUFFER, VBO);
  103.         glBufferData(GL_ARRAY_BUFFER, sizeof(vertices)*vertices.size(), vertices.data(), GL_STATIC_DRAW);
  104.  
  105.         glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
  106.         glEnableVertexAttribArray(0);
  107.  
  108.         glBindBuffer(GL_ARRAY_BUFFER, 0);
  109.         glBindVertexArray(0);
  110.  
  111.     }
  112.  
  113.     int setMVP(mat4 mvp) {
  114.         MVP = mvp;
  115.         return 1;
  116.     }
  117.  
  118.     int setColor(vec3 color) {
  119.         lineColor = color;
  120.         return 1;
  121.     }
  122.  
  123.     int draw() {
  124.         glUseProgram(shaderProgram);
  125.  
  126.         glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "MVP"), 1, GL_FALSE, &MVP[0][0]);
  127.         glUniform3fv(glGetUniformLocation(shaderProgram, "color"), 1, &lineColor[0]);
  128.  
  129.         glBindVertexArray(VAO);
  130.         glDrawArrays(GL_LINES, 0, 2);
  131.         return 1;
  132.     }
  133.  
  134.     ~Line() {
  135.         glDeleteVertexArrays(1, &VAO);
  136.         glDeleteBuffers(1, &VBO);
  137.         glDeleteProgram(shaderProgram);
  138.     }
  139. };
  140.  
  141. int main()
  142. {
  143.     // glfw: initialize and configure
  144.     // ------------------------------
  145.     glfwInit();
  146.     glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  147.     glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  148.     glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  149.  
  150. #ifdef __APPLE__
  151.     glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
  152. #endif
  153.  
  154.     // glfw window creation
  155.     // --------------------
  156.     GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "drawing lines", NULL, NULL);
  157.     if (window == NULL)
  158.     {
  159.         std::cout << "Failed to create GLFW window" << std::endl;
  160.         glfwTerminate();
  161.         return -1;
  162.     }
  163.     glfwMakeContextCurrent(window);
  164.  
  165.     // glad: load all OpenGL function pointers
  166.     // ---------------------------------------
  167.     if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
  168.     {
  169.         std::cout << "Failed to initialize GLAD" << std::endl;
  170.         return -1;
  171.     }
  172.     camera.position = vec3(3,3,3);
  173.  
  174.     // 3d lines example
  175.     Line line1(vec3(0,0,0), vec3(1,0,0));
  176.     line1.setColor(vec3(1,0,0));
  177.     Line line2(vec3(0,0,0), vec3(0,1,0));
  178.     line2.setColor(vec3(0,1,0));
  179.     Line line3(vec3(0,0,0), vec3(0,0,1));
  180.     line3.setColor(vec3(0,0,1));
  181.     glm::mat4 projection = glm::perspective(glm::radians(45.0f), (float) SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
  182.  
  183.     float angle = 0.0f;
  184.     float rotationSpeed = 50.0f;
  185.  
  186.     // 2d line example
  187.     vec3 start = vec3(10,10,0);
  188.     vec3 end = vec3(20,20,0);
  189.  
  190.     float x1 = start.x;
  191.     float y1 = start.y;
  192.     float x2 = end.x;
  193.     float y2 = end.y;
  194.     float w = SCR_WIDTH;
  195.     float h = SCR_HEIGHT;
  196.  
  197.     // convert 3d world space position 2d screen space position
  198.     x1 = 2*x1 / w - 1;
  199.     y1 = 2*y1 / h - 1;
  200.  
  201.     x2 = 2*x2 / w - 1;
  202.     y2 = 2*y2 / h - 1;
  203.  
  204.     start.x = x1;
  205.     start.y = y1;
  206.     end.x = x2;
  207.     end.y = y2;
  208.  
  209.     Line line4(start, end);
  210.  
  211.  
  212.     // render loop
  213.     // -----------
  214.     while (!glfwWindowShouldClose(window))
  215.     {
  216.         float currentFrame = glfwGetTime();
  217.         deltaTime = currentFrame - lastFrame;
  218.         // input
  219.         // -----
  220.         processInput(window);
  221.  
  222.         // render
  223.         // ------
  224.         glClearColor(0.0, 0.0, 0.0, 1.0);
  225.         glClear(GL_COLOR_BUFFER_BIT);
  226.  
  227.         angle = deltaTime * rotationSpeed;
  228.  
  229.         // update camera position (rotating)
  230.         camera.position = vec3(3*cos(radians(angle)), 3, 3*sin(radians(angle)));
  231.         mat4 view = lookAt(camera.position, vec3(0,0,0), vec3(0,1,0));
  232.  
  233.  
  234.         line1.setMVP(projection * view);
  235.         line2.setMVP(projection * view);
  236.         line3.setMVP(projection * view);
  237.  
  238.         line1.draw();
  239.         line2.draw();
  240.         line3.draw();
  241.  
  242.  
  243.         line4.draw();
  244.  
  245.         // glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
  246.         // -------------------------------------------------------------------------------
  247.         glfwSwapBuffers(window);
  248.         glfwPollEvents();
  249.     }
  250.  
  251.  
  252.     // glfw: terminate, clearing all previously allocated GLFW resources.
  253.     // ------------------------------------------------------------------
  254.     glfwTerminate();
  255.     return 0;
  256. }
  257.  
  258. // process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
  259. // ---------------------------------------------------------------------------------------------------------
  260. void processInput(GLFWwindow *window)
  261. {
  262.     if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
  263.         glfwSetWindowShouldClose(window, true);
  264. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement