Advertisement
gravityio

Application.cpp

Feb 9th, 2023
791
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.54 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4.  
  5. #include <glad/glad.h>
  6. #include <GLFW/glfw3.h>
  7.  
  8. #include "Macros.h"
  9. #include "Shader.h"
  10. #include "Model.h"
  11.  
  12. const unsigned int screenWidth = 800;
  13. const unsigned int screenHeight = 600;
  14.  
  15. const float modelData[] = {
  16.   // positions          // colors          // uv coords
  17.   -0.75f, -0.25f, 0.0f, 1.0f, 0.0f, 0.0f,  // Bottom Left
  18.   -0.75f,  0.25f, 0.0f, 0.0f, 1.0f, 0.0f,  // Top Left
  19.   -0.25f,  0.25f, 0.0f, 0.0f, 0.0f, 1.0f,  // Top Right
  20.   -0.25f, -0.25f, 0.0f, 0.5f, 0.5f, 0.5f,  // Bottom Right
  21. };
  22.  
  23. const unsigned int indices[] = {
  24.   0, 1, 2,
  25.   0, 2, 3
  26. };
  27.  
  28. void framebuffer_size_callback(GLFWwindow* window, int width, int height);
  29. void processInput(GLFWwindow* window);
  30.  
  31. int main(void)
  32. {
  33.   /* Initialize the library */
  34.   if (!glfwInit()) return -1;
  35.   glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  36.   glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  37.   glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  38.  
  39.   // GLFW has nothing to do with OpenGL, glfw just handles os specific window initialization I think
  40.   GLFWwindow* window = glfwCreateWindow(screenWidth, screenHeight, "LearnOpenGL", NULL, NULL);
  41.   if (window == NULL)
  42.   {
  43.       LOG("Failed to create GLFW window");
  44.       glfwTerminate();
  45.       return -1;
  46.   }
  47.   // Then you tell GLFW you want to be executing operations on this window object incase you want to execute operations on a different one I'd assume.
  48.   glfwMakeContextCurrent(window);
  49.   // Then you set a function pointer for when the window object gets resized
  50.   glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
  51.  
  52.   // Then we load the mappings for the actual OpenGL function pointers because our computers don't know what... actually idk what exactly this does
  53.   if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
  54.   {
  55.       LOG("Failed to initialize GLAD");
  56.       return -1;
  57.   }
  58.   glViewport(0, 0, screenWidth, screenHeight);
  59.  
  60.   Shader mainShader("D:/Harry/My Stuff/Code/CPP/OpenGLDemo/OpenGLDemo/src/shaders/main.vsh", "D:/Harry/My Stuff/Code/CPP/OpenGLDemo/OpenGLDemo/src/shaders/main.fsh");
  61.   Model model(modelData, indices);
  62.  
  63.   int nrAttributes;
  64.   glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &nrAttributes);
  65.   LOG("Maximum nr of vertex attributes supported: " << nrAttributes);
  66.  
  67.   LOG("RUNNING WHILE LOOP");
  68.   while (!glfwWindowShouldClose(window))
  69.   {
  70.       processInput(window);
  71.  
  72.       glClearColor(0.2f, 0.6f, 0.3f, 1.0f);
  73.       glClear(GL_COLOR_BUFFER_BIT);
  74.  
  75.       mainShader.use();
  76.      
  77.       model.render();
  78.  
  79.       // Basically applies all these draws to the actual screen from the gpus pixel buffer ig
  80.       glfwSwapBuffers(window);
  81.       // Polls for IO events like mouse and keyboard and shit
  82.       glfwPollEvents();
  83.   }
  84.  
  85.   // Deallocate gpu data if you want this happens in the decnostructor of ~Model
  86.  
  87.   // Will deallocate GLFW shit
  88.   glfwTerminate();
  89.   return 0;
  90. }
  91.  
  92.  
  93. // process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
  94. void processInput(GLFWwindow* window)
  95. {
  96.     if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
  97.         glfwSetWindowShouldClose(window, true);
  98. }
  99.  
  100. // glfw: whenever the window size changed (by OS or user resize) this callback function executes
  101. void framebuffer_size_callback(GLFWwindow* window, int width, int height)
  102. {
  103.     // make sure the viewport matches the new window dimensions; note that width and
  104.     // height will be significantly larger than specified on retina displays.
  105.     glViewport(0, 0, width, height);
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement