Guest User

drawing lines

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