Advertisement
pgiovanni

Untitled

Jul 9th, 2021
831
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.43 KB | None | 0 0
  1. #include <glad.h>
  2. #include <GLFW/glfw3.h>
  3.  
  4. #include <glm/glm.hpp>
  5. #include <glm/gtc/matrix_transform.hpp>
  6. #include <glm/gtc/type_ptr.hpp>
  7.  
  8. #include <learnopengl/filesystem.h>
  9. #include <learnopengl/shader_m.h>
  10. #include <learnopengl/camera.h>
  11. #include <learnopengl/model.h>
  12.  
  13. #include <iostream>
  14.  
  15. void framebuffer_size_callback(GLFWwindow* window, int width, int height);
  16. void mouse_callback(GLFWwindow* window, double xpos, double ypos);
  17. void scroll_callback(GLFWwindow* window, double xoffset, double yoffset);
  18. void processInput(GLFWwindow *window);
  19.  
  20. // settings
  21. const unsigned int SCR_WIDTH = 800;
  22. const unsigned int SCR_HEIGHT = 600;
  23.  
  24. // camera
  25. Camera camera(glm::vec3(0.0f, 0.0f, 3.0f));
  26. float lastX = SCR_WIDTH / 2.0f;
  27. float lastY = SCR_HEIGHT / 2.0f;
  28. bool firstMouse = true;
  29.  
  30. // timing
  31. float deltaTime = 0.0f;
  32. float lastFrame = 0.0f;
  33.  
  34. int main()
  35. {
  36.     // glfw: initialize and configure
  37.     // ------------------------------
  38.     glfwInit();
  39.     glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  40.     glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  41.     glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  42.  
  43. #ifdef __APPLE__
  44.     glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
  45. #endif
  46.  
  47.     // glfw window creation
  48.     // --------------------
  49.     GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
  50.     if (window == NULL)
  51.     {
  52.         std::cout << "Failed to create GLFW window" << std::endl;
  53.         glfwTerminate();
  54.         return -1;
  55.     }
  56.     glfwMakeContextCurrent(window);
  57.     glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
  58.     glfwSetCursorPosCallback(window, mouse_callback);
  59.     glfwSetScrollCallback(window, scroll_callback);
  60.  
  61.     // tell GLFW to capture our mouse
  62.     glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
  63.  
  64.     // glad: load all OpenGL function pointers
  65.     // ---------------------------------------
  66.     if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
  67.     {
  68.         std::cout << "Failed to initialize GLAD" << std::endl;
  69.         return -1;
  70.     }
  71.  
  72.     // tell stb_image.h to flip loaded texture's on the y-axis (before loading model).
  73.     stbi_set_flip_vertically_on_load(true);
  74.  
  75.     // configure global opengl state
  76.     // -----------------------------
  77.     glEnable(GL_DEPTH_TEST);
  78.  
  79.     // build and compile shaders
  80.     // -------------------------
  81.     Shader ourShader("1.model_loading.vs", "1.model_loading.fs");
  82.  
  83.     // load models
  84.     // -----------
  85.     Model ourModel(FileSystem::getPath("resources/objects/backpack/backpack.obj"));
  86.  
  87.    
  88.     // draw in wireframe
  89.     //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  90.  
  91.     // render loop
  92.     // -----------
  93.     while (!glfwWindowShouldClose(window))
  94.     {
  95.         // per-frame time logic
  96.         // --------------------
  97.         float currentFrame = glfwGetTime();
  98.         deltaTime = currentFrame - lastFrame;
  99.         lastFrame = currentFrame;
  100.  
  101.         // input
  102.         // -----
  103.         processInput(window);
  104.  
  105.         // render
  106.         // ------
  107.         glClearColor(0.05f, 0.05f, 0.05f, 1.0f);
  108.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  109.  
  110.         // don't forget to enable shader before setting uniforms
  111.         ourShader.use();
  112.  
  113.         // view/projection transformations
  114.         glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
  115.         glm::mat4 view = camera.GetViewMatrix();
  116.         ourShader.setMat4("projection", projection);
  117.         ourShader.setMat4("view", view);
  118.  
  119.         // render the loaded model
  120.         glm::mat4 model = glm::mat4(1.0f);
  121.         model = glm::translate(model, glm::vec3(0.0f, 0.0f, 0.0f)); // translate it down so it's at the center of the scene
  122.         model = glm::scale(model, glm::vec3(1.0f, 1.0f, 1.0f)); // it's a bit too big for our scene, so scale it down
  123.         ourShader.setMat4("model", model);
  124.         ourModel.Draw(ourShader);
  125.  
  126.  
  127.         // glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
  128.         // -------------------------------------------------------------------------------
  129.         glfwSwapBuffers(window);
  130.         glfwPollEvents();
  131.     }
  132.  
  133.     // glfw: terminate, clearing all previously allocated GLFW resources.
  134.     // ------------------------------------------------------------------
  135.     glfwTerminate();
  136.     return 0;
  137. }
  138.  
  139. // process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
  140. // ---------------------------------------------------------------------------------------------------------
  141. void processInput(GLFWwindow *window)
  142. {
  143.     if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
  144.         glfwSetWindowShouldClose(window, true);
  145.  
  146.     if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
  147.         camera.ProcessKeyboard(FORWARD, deltaTime);
  148.     if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
  149.         camera.ProcessKeyboard(BACKWARD, deltaTime);
  150.     if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
  151.         camera.ProcessKeyboard(LEFT, deltaTime);
  152.     if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
  153.         camera.ProcessKeyboard(RIGHT, deltaTime);
  154. }
  155.  
  156. // glfw: whenever the window size changed (by OS or user resize) this callback function executes
  157. // ---------------------------------------------------------------------------------------------
  158. void framebuffer_size_callback(GLFWwindow* window, int width, int height)
  159. {
  160.     // make sure the viewport matches the new window dimensions; note that width and
  161.     // height will be significantly larger than specified on retina displays.
  162.     glViewport(0, 0, width, height);
  163. }
  164.  
  165. // glfw: whenever the mouse moves, this callback is called
  166. // -------------------------------------------------------
  167. void mouse_callback(GLFWwindow* window, double xpos, double ypos)
  168. {
  169.     if (firstMouse)
  170.     {
  171.         lastX = xpos;
  172.         lastY = ypos;
  173.         firstMouse = false;
  174.     }
  175.  
  176.     float xoffset = xpos - lastX;
  177.     float yoffset = lastY - ypos; // reversed since y-coordinates go from bottom to top
  178.  
  179.     lastX = xpos;
  180.     lastY = ypos;
  181.  
  182.     camera.ProcessMouseMovement(xoffset, yoffset);
  183. }
  184.  
  185. // glfw: whenever the mouse scroll wheel scrolls, this callback is called
  186. // ----------------------------------------------------------------------
  187. void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
  188. {
  189.     camera.ProcessMouseScroll(yoffset);
  190. }
  191.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement