Advertisement
anden3

Min GLFW

Mar 21st, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.08 KB | None | 0 0
  1. #include <GL/glew.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.  
  10. const float DEFAULT_FOV = 90.0f;
  11.  
  12. class Camera {
  13. public:
  14.     glm::vec3 Position = glm::vec3(0.0f);
  15.     glm::vec3 Front;
  16.     glm::vec3 Up;
  17.     glm::vec3 Right;
  18.     glm::vec3 FrontDirection;
  19.     glm::vec3 RightDirection;
  20.  
  21.     float Yaw = 270.0f;
  22.     float Pitch = 0.0f;
  23.     float Zoom = DEFAULT_FOV;
  24.  
  25.     Camera() {
  26.         UpdateCameraVectors();
  27.     }
  28.  
  29.     inline glm::mat4 GetViewMatrix() {
  30.         return glm::lookAt(Position, Position + Front, Up);
  31.     }
  32.    
  33.     void UpdateCameraVectors();
  34. };
  35.  
  36. void Camera::UpdateCameraVectors() {
  37.     glm::vec3 front;
  38.     front.x = glm::cos(glm::radians(Yaw)) * glm::cos(glm::radians(Pitch));
  39.     front.y = glm::sin(glm::radians(Pitch));
  40.     front.z = glm::sin(glm::radians(Yaw)) * glm::cos(glm::radians(Pitch));
  41.  
  42.     FrontDirection = glm::normalize(glm::vec3(front.x, 0, front.z));
  43.     RightDirection = glm::vec3(-FrontDirection.z, 0, FrontDirection.x);
  44.  
  45.     Front = glm::normalize(front);
  46.     Right = glm::normalize(glm::cross(Front, glm::vec3(0, 1, 0)));
  47.     Up    = glm::normalize(glm::cross(Right, Front));
  48. }
  49.  
  50. static bool WindowFocused   = true;
  51. static bool WindowMinimized = false;
  52.  
  53. double DeltaTime = 0.0;
  54. double LastFrame = 0.0;
  55.  
  56. // Defining shaders.
  57. Shader* shader = nullptr;
  58.  
  59. // Initializing objects.
  60. Camera Cam = Camera();
  61. UniformBuffer UBO = UniformBuffer();
  62.  
  63. // The main window which everything is rendered in.
  64. GLFWwindow* Window = nullptr;
  65.  
  66. bool FULLSCREEN            = true;
  67. bool VSYNC                 = true;
  68.  
  69. int  FOV                   = 90;
  70. int  MIPMAP_LEVEL          = 4;
  71. int  SCREEN_HEIGHT         = 1080;
  72. int  SCREEN_WIDTH          = 1920;
  73. int  ANISOTROPIC_FILTERING = 16;
  74.  
  75. void Key_Proxy(GLFWwindow* window, int key, int scancode, int action, int mods) {
  76.  
  77. }
  78.  
  79. void Mouse_Proxy(GLFWwindow* window, double posX, double posY) {
  80.  
  81. }
  82.  
  83. // Proxy for receiving Unicode codepoints, very useful for getting text input.
  84. void Text_Proxy(GLFWwindow* window, unsigned int codepoint) {
  85.  
  86. }
  87.  
  88. void Scroll_Proxy(GLFWwindow* window, double offsetX, double offsetY) {
  89.  
  90. }
  91.  
  92. void Click_Proxy(GLFWwindow* window, int button, int action, int mods) {
  93.  
  94. }
  95.  
  96. void Window_Focused(GLFWwindow* window, int focused) {
  97.     WindowFocused = focused > 0;
  98. }
  99.  
  100. void Window_Minimized(GLFWwindow* window, int iconified) {
  101.     WindowMinimized = iconified > 0;
  102. }
  103.  
  104. void Exit(void* caller) {
  105.     glfwSetWindowShouldClose(Window, true);
  106. }
  107.  
  108. void Init_GL() {
  109.     // Set the OpenGL version.
  110.     glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
  111.     glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
  112.     glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, true);
  113.     glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  114.     glfwWindowHint(GLFW_RESIZABLE, false);
  115.     glfwWindowHint(GLFW_AUTO_ICONIFY, false);
  116.  
  117.     GLFWmonitor* monitor = glfwGetPrimaryMonitor();
  118.  
  119.     // Get the video mode of the monitor.
  120.     const GLFWvidmode* videoMode = glfwGetVideoMode(monitor);
  121.  
  122.     int sWidth, sHeight;
  123.     glfwGetMonitorPhysicalSize(monitor, &sWidth, &sHeight);
  124.  
  125.     SCREEN_DPI = videoMode->width / (sWidth / 25.4);
  126.  
  127.     if (FULLSCREEN) {
  128.         // Stops the window from having any decoration, such as a title bar.
  129.         glfwWindowHint(GLFW_DECORATED, false);
  130.  
  131.         // Set SCREEN_WIDTH and SCREEN_HEIGHT to the resolution of the primary monitor.
  132.         SCREEN_WIDTH = videoMode->width;
  133.         SCREEN_HEIGHT = videoMode->height;
  134.         glfwWindowHint(GLFW_REFRESH_RATE, videoMode->refreshRate);
  135.  
  136.         // Create a fullscreen window.
  137.         Window = glfwCreateWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Craftmine", monitor, nullptr);
  138.     }
  139.     else {
  140.         // Set the window to be decorated, allowing users to close it.
  141.         glfwWindowHint(GLFW_DECORATED, true);
  142.  
  143.         // Create a windowed window.
  144.         Window = glfwCreateWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Craftmine", nullptr, nullptr);
  145.     }
  146.  
  147.     // Set the window's position to the upper left corner.
  148.     glfwSetWindowPos(Window, 0, 0);
  149.  
  150.     // Set the window to be used for future OpenGL calls.
  151.     glfwMakeContextCurrent(Window);
  152.  
  153.     // Set whether to use VSync based on the value in the config file.
  154.     glfwSwapInterval(VSYNC);
  155.  
  156.     // Set GLEW to experimental mode (doesn't work otherwise D:)
  157.     glewExperimental = GL_TRUE;
  158.  
  159.     // Initializes GLEW, which enables vendor-specific OpenGL extensions.
  160.     glewInit();
  161.  
  162.     // Set all the callback functions for events to the appropiate proxy functions.
  163.     glfwSetKeyCallback(Window, Key_Proxy);
  164.     glfwSetCharCallback(Window, Text_Proxy);
  165.     glfwSetScrollCallback(Window, Scroll_Proxy);
  166.     glfwSetCursorPosCallback(Window, Mouse_Proxy);
  167.     glfwSetMouseButtonCallback(Window, Click_Proxy);
  168.     glfwSetWindowIconifyCallback(Window, Window_Minimized);
  169.  
  170.     // Enable Blending, which makes transparency work.
  171.     glEnable(GL_BLEND);
  172.  
  173.     // Enable Fade Culling, which disables rendering of hidden faces.
  174.     glEnable(GL_CULL_FACE);
  175.  
  176.     // Enable Depth Testing,
  177.     // which chooses which elements to draw based on their depth, thus allowing 3D to work.
  178.     glEnable(GL_DEPTH_TEST);
  179.  
  180.     // Set the proper function for evaluating blending.
  181.     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  182.  
  183.     // Specify the size of the OpenGL view port.
  184.     glViewport(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
  185.  
  186.     // Set the color that the window gets filled with when the color buffer gets cleared.
  187.     glClearColor(CLEAR_COLOR.r, CLEAR_COLOR.g, CLEAR_COLOR.b, 1.0f);
  188. }
  189.  
  190. void Init_Textures() {
  191.     glActiveTexture(GL_TEXTURE0);
  192.  
  193.     // Load the texture atlas into a texture array, with mipmapping enabled,
  194.     // and store it in the active Texture Unit.
  195.     glBindTexture(
  196.         GL_TEXTURE_2D_ARRAY,
  197.         Load_Array_Texture(
  198.             "atlas.png", {16, 32},
  199.             MIPMAP_LEVEL, static_cast<float>(ANISOTROPIC_FILTERING)
  200.         )
  201.     );
  202. }
  203.  
  204. void Init_Shaders() {
  205.     // Load the shaders.
  206.     shader = new Shader("shader");
  207.  
  208.     // Create the frustrum projection matrix for the camera.
  209.     glm::mat4 projection = glm::perspective(
  210.         glm::radians(static_cast<float>(FOV)),
  211.         static_cast<float>(SCREEN_WIDTH) / SCREEN_HEIGHT,
  212.         Z_NEAR_LIMIT, Z_FAR_LIMIT
  213.     );
  214.  
  215.     // Create a matrix storage block in the shaders referenced in the last argument.
  216.     UBO.Create("Matrices", 0, 2 * sizeof(glm::mat4),
  217.         {shader}
  218.     );
  219.  
  220.     UBO.Upload(1, projection);
  221. }
  222.  
  223. void Init_Rendering() {
  224.     // Default identity matrix, does nothing.
  225.     glm::mat4 model;
  226.  
  227.     // Upload the empty matrix.
  228.     shader->Upload("model", model);
  229.  
  230.     // Upload the light level of an unlit block.
  231.     shader->Upload("ambient", AMBIENT_LIGHT);
  232.  
  233.     // Upload the max light level of a block.
  234.     shader->Upload("diffuse", DIFFUSE_LIGHT);
  235.  
  236.     // Upload the texture unit index of the main textures.
  237.     shader->Upload("diffTex", 0);
  238. }
  239.  
  240. void Render_Scene() {
  241.     UBO.Upload(0, Cam.GetViewMatrix());
  242.  
  243.     /*
  244.      * RENDER SHIT
  245.      */
  246. }
  247.  
  248. int main() {
  249.     // Initialize GLFW, the library responsible for windowing, events, etc...
  250.     glfwInit();
  251.  
  252.     Init_GL();
  253.     Init_Textures();
  254.     Init_Shaders();
  255.     Init_Rendering();
  256.  
  257.     // The main loop.
  258.     // Runs until window is closed.
  259.     while (!glfwWindowShouldClose(Window)) {
  260.         if (WindowMinimized || !WindowFocused) {
  261.             glfwWaitEvents();
  262.             continue;
  263.         }
  264.  
  265.         // Clear the screen buffer from the last frame.
  266.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  267.  
  268.         // Get the time difference between this frame and the last one.
  269.         double currentFrame = glfwGetTime();
  270.         DeltaTime = currentFrame - LastFrame;
  271.         LastFrame = currentFrame;
  272.  
  273.         // Polls and processes received events.
  274.         glfwPollEvents();
  275.  
  276.         Render_Scene();
  277.  
  278.         // Swap the newly rendered frame with the old one.
  279.         glfwSwapBuffers(Window);
  280.     }
  281.  
  282.     // Shut down the graphics library, and return.
  283.     glfwTerminate();
  284.  
  285.     return 0;
  286. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement