Advertisement
Nightdra

Main Code

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