Advertisement
Guest User

Untitled

a guest
Nov 14th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 25.10 KB | None | 0 0
  1. // dear imgui: standalone example application for GLFW + OpenGL 3, using programmable pipeline
  2. // If you are new to dear imgui, see examples/README.txt and documentation at the top of imgui.cpp.
  3. // (GLFW is a cross-platform general purpose library for handling windows, inputs, OpenGL/Vulkan graphics context creation, etc.)
  4.  
  5. #include "imgui.h"
  6. #include "imgui_impl_glfw.h"
  7. #include "imgui_impl_opengl3.h"
  8. #include <glm/gtc/matrix_transform.hpp>
  9. #include <glfw/glfw3.h>
  10. #include <glm/gtc/quaternion.hpp>
  11. #include "glm/gtc/matrix_transform.hpp"
  12. #include <vector>
  13. //#include <glm/gtx/quaternion.hpp>
  14. #include <stdio.h>
  15.  
  16. // About OpenGL function loaders: modern OpenGL doesn't have a standard header file and requires individual function pointers to be loaded manually.
  17. // Helper libraries are often used for this purpose! Here we are supporting a few common ones: gl3w, glew, glad.
  18. // You may use another loader/header of your choice (glext, glLoadGen, etc.), or chose to manually implement your own.
  19. #if defined(IMGUI_IMPL_OPENGL_LOADER_GL3W)
  20. #include <GL/gl3w.h>    // Initialize with gl3wInit()
  21. #elif defined(IMGUI_IMPL_OPENGL_LOADER_GLEW)
  22. #include <GL/glew.h>    // Initialize with glewInit()
  23. #elif defined(IMGUI_IMPL_OPENGL_LOADER_GLAD)
  24. #include <glad/glad.h>  // Initialize with gladLoadGL()
  25. #else
  26. #include IMGUI_IMPL_OPENGL_LOADER_CUSTOM
  27. #endif
  28.  
  29. #include <GLFW/glfw3.h> // Include glfw3.h after our OpenGL definitions
  30. #include <iostream>
  31.  
  32. static void glfw_error_callback(int error, const char* description)
  33. {
  34.     fprintf(stderr, "Glfw Error %d: %s\n", error, description);
  35. }
  36.  
  37. float Distance(float x1, float x2, float y1, float y2)
  38. {
  39.     return sqrtf((x2 - x1)*(x2 - x1)+(y2 - y1)*(y2 - y1));
  40. }
  41. ImVec4 BGcolor = ImVec4(1.0f, 1.0f, 1.0f, 1.0f);
  42.  
  43. #pragma region Dywan
  44. //static void Carpet(int step, GLfloat maxX, GLfloat maxY, GLfloat minX, GLfloat minY, GLuint &VBO, GLuint &VAO, GLuint &program)
  45. //{
  46. //  if (step == 0)
  47. //      return;
  48. //
  49. //  GLfloat white[] = {
  50. //      minX, maxY, 0.0f,
  51. //      maxX, maxY, 0.0f,
  52. //      maxX, minY, 0.0f,
  53. //      minX, maxY, 0.0f,
  54. //      minX, minY, 0.0f,
  55. //      maxX, minY, 0.0f
  56. //  };
  57. //  /*GLfloat black[] = {
  58. //      minX + (maxX - minX)*(1.0f / 3.0f), minY + (maxY - minY)*(2.0f / 3.0f), 0.0f,
  59. //      minX + (maxX - minX)*(2.0f / 3.0f), minY + (maxY - minY)*(2.0f / 3.0f), 0.0f,
  60. //      minX + (maxX - minX)*(2.0f / 3.0f), minY + (maxY - minY)*(1.0f / 3.0f), 0.0f,
  61. //      minX + (maxX - minX)*(1.0f / 3.0f), minY + (maxY - minY)*(2.0f / 3.0f), 0.0f,
  62. //      minX + (maxX - minX)*(1.0f / 3.0f), minY + (maxY - minY)*(1.0f / 3.0f), 0.0f,
  63. //      minX + (maxX - minX)*(2.0f / 3.0f), minY + (maxY - minY)*(1.0f / 3.0f), 0.0f
  64. //  };*/
  65. //  GLfloat black[] = {
  66. //      minX, minY + (maxY - minY)*(2.0f / 3.0f), 0.0f,
  67. //      minX + (maxX - minX)*(2.0f / 3.0f), minY + (maxY - minY)*(2.0f / 3.0f), 0.0f,
  68. //      minX + (maxX - minX)*(2.0f / 3.0f), minY + (maxY - minY)*(1.0f / 3.0f), 0.0f,
  69. //      minX, minY + (maxY - minY)*(2.0f / 3.0f), 0.0f,
  70. //      minX, minY + (maxY - minY)*(1.0f / 3.0f), 0.0f,
  71. //      minX + (maxX - minX)*(2.0f / 3.0f), minY + (maxY - minY)*(1.0f / 3.0f), 0.0f
  72. //  };
  73. //
  74. //  glBindBuffer(GL_ARRAY_BUFFER, VBO);
  75. //
  76. //  glBufferData(GL_ARRAY_BUFFER, sizeof(white), white, GL_DYNAMIC_DRAW);
  77. //  glUseProgram(program);
  78. //  int vertexColorLocation = glGetUniformLocation(program, "ourColor");
  79. //  glUniform4f(vertexColorLocation, BGcolor.x, BGcolor.y, BGcolor.z, 1.0f);
  80. //  glBindVertexArray(VAO);
  81. //  glDrawArrays(GL_TRIANGLES, 0, 6);
  82. //
  83. //  glBufferData(GL_ARRAY_BUFFER, sizeof(black), black, GL_DYNAMIC_DRAW);
  84. //  glUseProgram(program);
  85. //  glUniform4f(vertexColorLocation, 0.0f, 0.0f, 0.0f, 1.0f);
  86. //  glBindVertexArray(VAO);
  87. //  glDrawArrays(GL_TRIANGLES, 0, 6);
  88. //
  89. //  Carpet(step-1, minX + (maxX - minX)*(1.0f / 3.0f), maxY, minX, minY + (maxY - minY)*(2.0f / 3.0f), VBO, VAO, program);                                                              //TOP LEFT
  90. //  Carpet(step-1, minX + (maxX - minX)*(2.0f / 3.0f), maxY, minX + (maxX - minX)*(1.0f / 3.0f), minY + (maxY - minY)*(2.0f / 3.0f), VBO, VAO, program);                                //TOP CENTER
  91. //  Carpet(step-1, maxX, maxY, minX + (maxX - minX)*(2.0f / 3.0f), minY + (maxY - minY)*(2.0f / 3.0f), VBO, VAO, program);                                                              //TOP RIGHT
  92. //  //Carpet(step-1, minX + (maxX - minX)*(1.0f / 3.0f), minY + (maxY - minY)*(2.0f / 3.0f), minX, minY + (maxY - minY)*(1.0f / 3.0f), VBO, VAO, program);                              //CENTER LEFT
  93. //  Carpet(step-1, maxX, minY + (maxY - minY)*(2.0f / 3.0f), minX + (maxX - minX)*(2.0f / 3.0f), minY + (maxY - minY)*(1.0f / 3.0f), VBO, VAO, program);                                //CENTER RIGHT
  94. //  Carpet(step-1, minX + (maxX - minX)*(1.0f / 3.0f), minY + (maxY - minY)*(1.0f / 3.0f), minX, minY, VBO, VAO, program);                                                              //BOTTOM LEFT
  95. //  Carpet(step-1, minX + (maxX - minX)*(2.0f / 3.0f), minY + (maxY - minY)*(1.0f / 3.0f), minX + (maxX - minX)*(1.0f / 3.0f), minY, VBO, VAO, program);                                //BOTTOM CENTER
  96. //  Carpet(step-1, maxX, minY + (maxY - minY)*(1.0f / 3.0f), minX + (maxX - minX)*(2.0f / 3.0f), minY, VBO, VAO, program);                                                              //BOTTOM RIGHT
  97. //}
  98. #pragma endregion
  99.  
  100. #pragma region MengerCube
  101. void Square(glm::vec3 max, glm::vec3 min, std::vector<GLfloat>* vertices, int* triangleCount, std::vector<GLfloat>* UV)
  102. {
  103.     vertices->push_back(max[0]); //TOP FACE
  104.     vertices->push_back(max[1]);
  105.     vertices->push_back(max[2]);
  106.     vertices->push_back(min[0]);
  107.     vertices->push_back(max[1]);
  108.     vertices->push_back(max[2]);
  109.     vertices->push_back(min[0]);
  110.     vertices->push_back(max[1]);
  111.     vertices->push_back(min[2]);
  112.     UV->push_back(0.000059f);
  113.     UV->push_back(1.0f - 0.000004f);
  114.     UV->push_back(0.000103f);
  115.     UV->push_back(1.0f - 0.336048f);
  116.     UV->push_back(0.335973f);
  117.     UV->push_back(1.0f - 0.335903f);
  118.     (*triangleCount)++;
  119.     vertices->push_back(min[0]);
  120.     vertices->push_back(max[1]);
  121.     vertices->push_back(min[2]);
  122.     vertices->push_back(max[0]);
  123.     vertices->push_back(max[1]);
  124.     vertices->push_back(min[2]);
  125.     vertices->push_back(max[0]);
  126.     vertices->push_back(max[1]);
  127.     vertices->push_back(max[2]);
  128.     UV->push_back(1.000023f);
  129.     UV->push_back(1.0f - 0.000013f);
  130.     UV->push_back(0.667979f);
  131.     UV->push_back(1.0f - 0.335851f);
  132.     UV->push_back(0.999958f);
  133.     UV->push_back(1.0f - 0.336064f);
  134.     (*triangleCount)++;
  135.     vertices->push_back(max[0]); //WEST FACE
  136.     vertices->push_back(max[1]);
  137.     vertices->push_back(max[2]);
  138.     vertices->push_back(max[0]);
  139.     vertices->push_back(max[1]);
  140.     vertices->push_back(min[2]);
  141.     vertices->push_back(max[0]);
  142.     vertices->push_back(min[1]);
  143.     vertices->push_back(min[2]);
  144.     UV->push_back(0.667979f);
  145.     UV->push_back(1.0f - 0.335851f);
  146.     UV->push_back(0.336024f);
  147.     UV->push_back(1.0f - 0.671877f);
  148.     UV->push_back(0.667969f);
  149.     UV->push_back(1.0f - 0.671889f);
  150.     (*triangleCount)++;
  151.     vertices->push_back(max[0]);
  152.     vertices->push_back(min[1]);
  153.     vertices->push_back(min[2]);
  154.     vertices->push_back(max[0]);
  155.     vertices->push_back(min[1]);
  156.     vertices->push_back(max[2]);
  157.     vertices->push_back(max[0]);
  158.     vertices->push_back(max[1]);
  159.     vertices->push_back(max[2]);
  160.     UV->push_back(1.000023f);
  161.     UV->push_back(1.0f - 0.000013f);
  162.     UV->push_back(0.668104f);
  163.     UV->push_back(1.0f - 0.000013f);
  164.     UV->push_back(0.667979f);
  165.     UV->push_back(1.0f - 0.335851f);
  166.     (*triangleCount)++;
  167.     vertices->push_back(max[0]); //SOUTH FACE
  168.     vertices->push_back(max[1]);
  169.     vertices->push_back(min[2]);
  170.     vertices->push_back(max[0]);
  171.     vertices->push_back(min[1]);
  172.     vertices->push_back(min[2]);
  173.     vertices->push_back(min[0]);
  174.     vertices->push_back(min[1]);
  175.     vertices->push_back(min[2]);
  176.     UV->push_back(0.000059f);
  177.     UV->push_back(1.0f - 0.000004f);
  178.     UV->push_back(0.335973f);
  179.     UV->push_back(1.0f - 0.335903f);
  180.     UV->push_back(0.336098f);
  181.     UV->push_back(1.0f - 0.000071f);
  182.     (*triangleCount)++;
  183.     vertices->push_back(min[0]);
  184.     vertices->push_back(min[1]);
  185.     vertices->push_back(min[2]);
  186.     vertices->push_back(min[0]);
  187.     vertices->push_back(max[1]);
  188.     vertices->push_back(min[2]);
  189.     vertices->push_back(max[0]);
  190.     vertices->push_back(max[1]);
  191.     vertices->push_back(min[2]);
  192.     UV->push_back(0.667979f);
  193.     UV->push_back(1.0f - 0.335851f);
  194.     UV->push_back(0.335973f);
  195.     UV->push_back(1.0f - 0.335903f);
  196.     UV->push_back(0.336024f);
  197.     UV->push_back(1.0f - 0.671877f);
  198.     (*triangleCount)++;
  199.     vertices->push_back(min[0]); //EAST FACE
  200.     vertices->push_back(max[1]);
  201.     vertices->push_back(min[2]);
  202.     vertices->push_back(min[0]);
  203.     vertices->push_back(min[1]);
  204.     vertices->push_back(min[2]);
  205.     vertices->push_back(min[0]);
  206.     vertices->push_back(min[1]);
  207.     vertices->push_back(max[2]);
  208.     UV->push_back(1.000004f);
  209.     UV->push_back(1.0f - 0.671847f);
  210.     UV->push_back(0.999958f);
  211.     UV->push_back(1.0f - 0.336064f);
  212.     UV->push_back(0.667979f);
  213.     UV->push_back(1.0f - 0.335851f);
  214.     (*triangleCount)++;
  215.     vertices->push_back(min[0]);
  216.     vertices->push_back(min[1]);
  217.     vertices->push_back(max[2]);
  218.     vertices->push_back(min[0]);
  219.     vertices->push_back(max[1]);
  220.     vertices->push_back(max[2]);
  221.     vertices->push_back(min[0]);
  222.     vertices->push_back(max[1]);
  223.     vertices->push_back(min[2]);
  224.     UV->push_back(0.668104f);
  225.     UV->push_back(1.0f - 0.000013f);
  226.     UV->push_back(0.335973f);
  227.     UV->push_back(1.0f - 0.335903f);
  228.     UV->push_back(0.667979f);
  229.     UV->push_back(1.0f - 0.335851f);
  230.     (*triangleCount)++;
  231.     vertices->push_back(min[0]); //NORTH FACE
  232.     vertices->push_back(max[1]);
  233.     vertices->push_back(max[2]);
  234.     vertices->push_back(min[0]);
  235.     vertices->push_back(min[1]);
  236.     vertices->push_back(max[2]);
  237.     vertices->push_back(max[0]);
  238.     vertices->push_back(min[1]);
  239.     vertices->push_back(max[2]);
  240.     UV->push_back(0.335973f);
  241.     UV->push_back(1.0f - 0.335903f);
  242.     UV->push_back(0.668104f);
  243.     UV->push_back(1.0f - 0.000013f);
  244.     UV->push_back(0.336098f);
  245.     UV->push_back(1.0f - 0.000071f);
  246.     (*triangleCount)++;
  247.     vertices->push_back(max[0]);
  248.     vertices->push_back(min[1]);
  249.     vertices->push_back(max[2]);
  250.     vertices->push_back(max[0]);
  251.     vertices->push_back(max[1]);
  252.     vertices->push_back(max[2]);
  253.     vertices->push_back(min[0]);
  254.     vertices->push_back(max[1]);
  255.     vertices->push_back(max[2]);
  256.     UV->push_back(0.000103f);
  257.     UV->push_back(1.0f - 0.336048f);
  258.     UV->push_back(0.000004f);
  259.     UV->push_back(1.0f - 0.671870f);
  260.     UV->push_back(0.336024f);
  261.     UV->push_back(1.0f - 0.671877f);
  262.     (*triangleCount)++;
  263.     vertices->push_back(max[0]); //DOWN FACE
  264.     vertices->push_back(min[1]);
  265.     vertices->push_back(max[2]);
  266.     vertices->push_back(max[0]);
  267.     vertices->push_back(min[1]);
  268.     vertices->push_back(min[2]);
  269.     vertices->push_back(min[0]);
  270.     vertices->push_back(min[1]);
  271.     vertices->push_back(min[2]);
  272.     UV->push_back(0.000103f);
  273.     UV->push_back(1.0f - 0.336048f);
  274.     UV->push_back(0.336024f);
  275.     UV->push_back(1.0f - 0.671877f);
  276.     UV->push_back(0.335973f);
  277.     UV->push_back(1.0f - 0.335903f);
  278.     (*triangleCount)++;
  279.     vertices->push_back(min[0]);
  280.     vertices->push_back(min[1]);
  281.     vertices->push_back(min[2]);
  282.     vertices->push_back(min[0]);
  283.     vertices->push_back(min[1]);
  284.     vertices->push_back(max[2]);
  285.     vertices->push_back(max[0]);
  286.     vertices->push_back(min[1]);
  287.     vertices->push_back(max[2]);
  288.     UV->push_back(0.667969f);
  289.     UV->push_back(1.0f - 0.671889f);
  290.     UV->push_back(1.000004f);
  291.     UV->push_back(1.0f - 0.671847f);
  292.     UV->push_back(0.667979f);
  293.     UV->push_back(1.0f - 0.335851f);
  294.     (*triangleCount)++;
  295. }
  296. void Sponge(glm::vec3 max, glm::vec3 min, int step, std::vector<GLfloat>* vertices, int* triangleCount, std::vector<GLfloat>* UV)
  297. {
  298.     float xStep = (max[0] - min[0])*(1.0f / 3.0f);
  299.     float yStep = (max[1] - min[1])*(1.0f / 3.0f);
  300.     float zStep = (max[2] - min[2])*(1.0f / 3.0f);
  301.  
  302.     if (step == 1)
  303.         Square(max, min, vertices, triangleCount, UV);
  304.     else
  305.     {
  306.         Sponge(max, glm::vec3(xStep*2,yStep*2,zStep*2), step - 1, vertices, triangleCount, UV); //TOP LAYER TOP LEFT CORNER
  307.         Sponge(glm::vec3(xStep*2,max[1],max[2]), glm::vec3(xStep,yStep*2,zStep*2), step - 1, vertices, triangleCount, UV); //TOP LAYER TOP CENTER
  308.         Sponge(glm::vec3(xStep,max[1],max[2]), glm::vec3(min[0],yStep*2,zStep*2), step - 1, vertices, triangleCount, UV); //TOP LAYER TOP RIGHT CORNER
  309.         Sponge(glm::vec3(max[0],max[1],zStep*2), glm::vec3(xStep*2,yStep*2,zStep), step - 1, vertices, triangleCount, UV); //TOP LAYER LEFT CENTER
  310.         Sponge(glm::vec3(xStep,max[1],zStep*2), glm::vec3(min[0],yStep*2,zStep), step - 1, vertices, triangleCount, UV); //TOP LAYER RIGHT CENTER
  311.         Sponge(glm::vec3(max[0],max[1],zStep), glm::vec3(xStep*2,yStep*2,min[2]), step - 1, vertices, triangleCount, UV); //TOP LAYER BOTTOM LEFT CORNER
  312.         Sponge(glm::vec3(xStep*2,max[1],zStep), glm::vec3(xStep,yStep*2,min[2]), step - 1, vertices, triangleCount, UV); //TOP LAYER BOTTOM CENTER
  313.         Sponge(glm::vec3(xStep,max[1],zStep), glm::vec3(min[0],yStep*2,min[2]), step - 1, vertices, triangleCount, UV); //TOP LAYER BOTTOM RIGHT CORNER
  314.         Sponge(glm::vec3(max[0],yStep*2,max[2]), glm::vec3(xStep*2,yStep,zStep*2), step - 1, vertices, triangleCount, UV); //MID LAYER TOP LEFT CORNER
  315.         Sponge(glm::vec3(xStep,yStep*2,max[2]), glm::vec3(min[0],yStep,zStep*2), step - 1, vertices, triangleCount, UV); //MID LAYER TOP RIGHT CORNER
  316.         Sponge(glm::vec3(max[0],yStep*2,zStep), glm::vec3(xStep*2,yStep,min[2]), step - 1, vertices, triangleCount, UV); //MID LAYER BOTTOM LEFT CORNER
  317.         Sponge(glm::vec3(xStep,yStep*2,zStep), glm::vec3(min[0],yStep,min[2]), step - 1, vertices, triangleCount, UV); //MID LAYER BOTTOM RIGHT CORNER
  318.         Sponge(glm::vec3(max[0],yStep,max[2]), glm::vec3(xStep*2,min[1],zStep*2), step - 1, vertices, triangleCount, UV); //BOTTOM LAYER TOP LEFT CORNER
  319.         Sponge(glm::vec3(xStep * 2, yStep, max[2]), glm::vec3(min[0], min[1], zStep * 2), step - 1, vertices, triangleCount, UV); //BOTTOM LAYER TOP CENTER
  320.         Sponge(glm::vec3(xStep, yStep, max[2]), glm::vec3(min[0], min[1], zStep * 2), step - 1, vertices, triangleCount, UV); //BOTTOM LAYER TOP RIGHT CORNER
  321.         Sponge(glm::vec3(max[0], yStep, zStep * 2), glm::vec3(xStep * 2, min[1], zStep), step - 1, vertices, triangleCount, UV); //BOTTOM LAYER LEFT CENTER
  322.         Sponge(glm::vec3(xStep, yStep, zStep * 2), glm::vec3(min[0], min[1], zStep), step - 1, vertices, triangleCount, UV); //BOTTOM LAYER RIGHT CENTER
  323.         Sponge(glm::vec3(max[0], yStep, zStep), glm::vec3(xStep * 2, min[1], min[2]), step - 1, vertices, triangleCount, UV); //BOTTOM LAYER BOTTOM LEFT CORNER
  324.         Sponge(glm::vec3(xStep * 2, yStep, zStep), glm::vec3(xStep, min[1], min[2]), step - 1, vertices, triangleCount, UV); //BOTTOM LAYER BOTTOM CENTER
  325.         Sponge(glm::vec3(xStep, yStep, zStep), min, step - 1, vertices, triangleCount, UV); //BOTTOM LAYER BOTTOM RIGHT CORNER
  326.     }
  327. }
  328. #pragma endregion
  329.  
  330. // position
  331. glm::vec3 position = glm::vec3(0, 0, 5);
  332. // horizontal angle : toward -Z
  333. float horizontalAngle = 3.14f;
  334. // vertical angle : 0, look at the horizon
  335. float verticalAngle = 0.0f;
  336. // Initial Field of View
  337. float initialFoV = 45.0f;
  338.  
  339. float speed = 3.0f; // 3 units / second
  340. float mouseSpeed = 0.025f;
  341. double lastTime = 0;
  342.  
  343. GLuint loadBMP_custom(const char * imagepath)
  344. {
  345.     // Data read from the header of the BMP file
  346.     unsigned char header[54]; // Each BMP file begins by a 54-bytes header
  347.     unsigned int dataPos;     // Position in the file where the actual data begins
  348.     unsigned int width, height;
  349.     unsigned int imageSize;   // = width*height*3
  350.                               // Actual RGB data
  351.     unsigned char * data;
  352.  
  353.     // Open the file
  354.     FILE * file = fopen(imagepath, "rb");
  355.     if (!file) { printf("Image could not be opened\n"); return 0; }
  356.  
  357.     if (fread(header, 1, 54, file) != 54) { // If not 54 bytes read : problem
  358.         printf("Not a correct BMP file\n");
  359.         return false;
  360.     }
  361.     if (header[0] != 'B' || header[1] != 'M') {
  362.         printf("Not a correct BMP file\n");
  363.         return 0;
  364.     }
  365.     // Read ints from the byte array
  366.     dataPos = *(int*)&(header[0x0A]);
  367.     imageSize = *(int*)&(header[0x22]);
  368.     width = *(int*)&(header[0x12]);
  369.     height = *(int*)&(header[0x16]);
  370.     // Some BMP files are misformatted, guess missing information
  371.     if (imageSize == 0)    imageSize = width * height * 3; // 3 : one byte for each Red, Green and Blue component
  372.     if (dataPos == 0)      dataPos = 54; // The BMP header is done that way
  373.                                          // Create a buffer
  374.     data = new unsigned char[imageSize];
  375.  
  376.     // Read the actual data from the file into the buffer
  377.     fread(data, 1, imageSize, file);
  378.  
  379.     //Everything is in memory now, the file can be closed
  380.     fclose(file);
  381.  
  382.     // Create one OpenGL texture
  383.     GLuint textureID;
  384.     glGenTextures(1, &textureID);
  385.  
  386.     // "Bind" the newly created texture : all future texture functions will modify this texture
  387.     glBindTexture(GL_TEXTURE_2D, textureID);
  388.  
  389.     // Give the image to OpenGL
  390.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_BGR, GL_UNSIGNED_BYTE, data);
  391.  
  392.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  393.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  394. }
  395.  
  396. int main(int, char**)
  397. {
  398.     // Setup window
  399.     glfwSetErrorCallback(glfw_error_callback);
  400.     if (!glfwInit())
  401.         return 1;
  402.  
  403.     // Decide GL+GLSL versions
  404. #if __APPLE__
  405.     // GL 3.2 + GLSL 150
  406.     const char* glsl_version = "#version 150";
  407.     glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  408.     glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
  409.     glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);  // 3.2+ only
  410.     glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);            // Required on Mac
  411. #else
  412.     // GL 4.3 + GLSL 430
  413.     const char* glsl_version = "#version 430";
  414.     glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
  415.     glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  416.     glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);  // 3.2+ only
  417.     glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);            // 3.0+ only
  418. #endif
  419.  
  420.     // Create window with graphics context
  421.     GLFWwindow* window = glfwCreateWindow(1280, 720, "Menger Sponge", NULL, NULL);
  422.     if (window == NULL)
  423.         return 1;
  424.     glfwMakeContextCurrent(window);
  425.     glfwSwapInterval(1); // Enable vsync
  426.  
  427.     // Initialize OpenGL loader
  428. #if defined(IMGUI_IMPL_OPENGL_LOADER_GL3W)
  429.     bool err = gl3wInit() != 0;
  430. #elif defined(IMGUI_IMPL_OPENGL_LOADER_GLEW)
  431.     bool err = glewInit() != GLEW_OK;
  432. #elif defined(IMGUI_IMPL_OPENGL_LOADER_GLAD)
  433.     bool err = !gladLoadGLLoader((GLADloadproc)glfwGetProcAddress);
  434. #endif
  435.     if (err)
  436.     {
  437.         fprintf(stderr, "Failed to initialize OpenGL loader!\n");
  438.         return 1;
  439.     }
  440.  
  441.     // Setup Dear ImGui binding
  442.     IMGUI_CHECKVERSION();
  443.     ImGui::CreateContext();
  444.     ImGuiIO& io = ImGui::GetIO(); (void)io;
  445.     io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;  // Enable Keyboard Controls
  446.     io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad;   // Enable Gamepad Controls
  447.  
  448.     ImGui_ImplGlfw_InitForOpenGL(window, true);
  449.     ImGui_ImplOpenGL3_Init(glsl_version);
  450.  
  451.     // Projection matrix : 45° Field of View, 4:3 ratio, display range : 0.1 unit <-> 100 units
  452.     glm::mat4 Projection = glm::perspective(glm::radians(45.0f), 1280.0f / 720.0f, 0.1f, 100.0f);
  453.  
  454.     // Or, for an ortho camera :
  455.     //glm::mat4 Projection = glm::ortho(-10.0f,10.0f,-10.0f,10.0f,0.0f,100.0f); // In world coordinates
  456.  
  457.     // Camera matrix
  458.     // Camera matrix
  459.     glm::mat4 View = glm::lookAt(
  460.         glm::vec3(-10, 3, -10), // Camera is at (4,3,3), in World Space
  461.         glm::vec3(0, 0, 0), // and looks at the origin
  462.         glm::vec3(0, 1, 0)  // Head is up (set to 0,-1,0 to look upside-down)
  463.     );
  464.  
  465.     // Model matrix : an identity matrix (model will be at the origin)
  466.     glm::mat4 Model = glm::mat4(1.0f);
  467.     // Our ModelViewProjection : multiplication of our 3 matrices
  468.  
  469.     // Setup style
  470.     ImGui::StyleColorsClassic();
  471.     const char *vertexShaderSource = "#version 330 core\n"
  472.         "layout (location = 0) in vec3 aPos;\n"
  473.         "layout (location = 1) in vec2 vertexUV;\n"
  474.         "uniform mat4 MVP;\n"
  475.         "out vec2 UV;\n"
  476.         "void main()\n"
  477.         "{\n"
  478.         "   gl_Position = MVP * vec4(aPos, 1);\n"
  479.         "   UV = vertexUV;\n"
  480.         "}\0";
  481.     const char *fragmentShaderSource = "#version 330 core\n"
  482.         "in vec2 UV;\n"
  483.         "out vec4 FragColor;\n"
  484.         "uniform sampler2D myTextureSampler;\n"
  485.         "uniform vec4 bgcolor;\n"
  486.         "void main()\n"
  487.         "{\n"
  488.         "   FragColor = texture(myTextureSampler, UV) * vec4(bgcolor);\n"
  489.         "}\n\0";
  490.  
  491.     GLuint Texture = loadBMP_custom("tex.bmp");
  492.  
  493.     GLuint vertexShader, fragmentShader;
  494.     vertexShader = glCreateShader(GL_VERTEX_SHADER);
  495.     fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
  496.     glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
  497.     glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
  498.     glCompileShader(vertexShader);
  499.     GLint success;
  500.     GLchar infoLog[512];
  501.     glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
  502.     if (!success)
  503.     {
  504.         return 1;
  505.         glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
  506.         //std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::end;
  507.     }
  508.     glCompileShader(fragmentShader);
  509.     glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
  510.     if (!success)
  511.     {
  512.         return 2;
  513.         glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
  514.         //std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::end;
  515.     }
  516.  
  517.     GLuint image;
  518.  
  519.     int triangleCount = 0;
  520.  
  521.     std::vector<GLfloat> vertices;
  522.     std::vector<GLfloat> UV;
  523.  
  524.     GLuint shaderProgram;
  525.     shaderProgram = glCreateProgram();
  526.     glAttachShader(shaderProgram, vertexShader);
  527.     glAttachShader(shaderProgram, fragmentShader);
  528.     glLinkProgram(shaderProgram);
  529.  
  530.     glDeleteShader(vertexShader);
  531.     glDeleteShader(fragmentShader);
  532.  
  533.     GLuint VBO, VAO, colorbuffer;
  534.     glGenVertexArrays(1, &VAO);
  535.     glBindVertexArray(VAO);
  536.  
  537.     glGenBuffers(1, &VBO); 
  538.     glBindBuffer(GL_ARRAY_BUFFER, VBO);
  539.     glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(GLfloat), vertices.data(), GL_STATIC_DRAW);
  540.     glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
  541.     glEnableVertexAttribArray(0);
  542.  
  543.     glGenBuffers(1, &colorbuffer);
  544.     glBindBuffer(GL_ARRAY_BUFFER, colorbuffer);
  545.     glBufferData(GL_ARRAY_BUFFER, UV.size()*sizeof(GLfloat), UV.data(), GL_DYNAMIC_DRAW);
  546.     glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
  547.     glEnableVertexAttribArray(1);
  548.  
  549.     glBindBuffer(GL_ARRAY_BUFFER, 0);
  550.     glBindVertexArray(0);
  551.  
  552.     // Get a handle for our "MVP" uniform
  553.     // Only during the initialisation
  554.     GLuint MatrixID = glGetUniformLocation(shaderProgram, "MVP");
  555.     int vertexColorLocation = glGetUniformLocation(shaderProgram, "bgcolor");
  556.     // Main loop
  557.     while (!glfwWindowShouldClose(window))
  558.     {
  559.         bool rotated = false;
  560.         //glUniform4f(vertexColorLocation, 0.1f, 0.1f, 0.2f, 1.0f);
  561.         //glBindBuffer(GL_ARRAY_BUFFER, VBO);
  562.         //glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_DYNAMIC_DRAW);
  563.         glfwPollEvents();
  564.         ImGui_ImplOpenGL3_NewFrame();
  565.         ImGui_ImplGlfw_NewFrame();
  566.         ImGui::NewFrame();
  567.  
  568.         //glm::mat4 Model = glm::mat4(1.0f);
  569.  
  570.         static float x = 5;
  571.         static float y = 1;
  572.         static int f = 3;
  573.         glm::mat4 RotationFull = glm::mat4(1.0f);
  574.  
  575.         ImGui::Begin("Options");                                          // Create a window called "Options" and append into it.
  576.         ImGui::Text("Menger Sponge");                // Display some text (you can use a format strings too)
  577.         if(ImGui::SliderFloat("RotationY", &y, 1, 1000))
  578.         {
  579.             Model = glm::mat4(1.0f);
  580.             rotated = true;
  581.         }
  582.         glm::mat4 RotationY = glm::rotate(Model, glm::radians(y), glm::vec3(1.0f, 0.0f, 0.0f));
  583.         if (ImGui::SliderFloat("RotationX", &x, 1, 1000))
  584.         {
  585.             Model = glm::mat4(1.0f);
  586.             rotated = true;
  587.         }
  588.         glm::mat4 RotationX = glm::rotate(Model, glm::radians(x), glm::vec3(0.0f, 1.0f, 0.0f));
  589.         RotationFull = RotationY * RotationX;
  590.         ImGui::SliderInt("Depth", &f, 1, 5);
  591.         ImGui::ColorEdit3("TextureColor", (float*)&BGcolor);
  592.         ImGui::End();
  593.         //Model = glm::mat4(1.0f);
  594.         if(rotated)
  595.             Model *= RotationFull;
  596.  
  597.         glm::mat4 mvp = Projection * View * Model;
  598.  
  599.         std::vector<GLfloat> vertices;
  600.         std::vector<GLfloat> UV;
  601.  
  602.         // Rendering
  603.         ImGui::Render();
  604.         int display_w, display_h;
  605.        
  606.         glfwMakeContextCurrent(window);
  607.         glfwGetFramebufferSize(window, &display_w, &display_h);
  608.         glViewport(0, 0, display_w, display_h);
  609.         // Send our transformation to the currently bound shader, in the "MVP" uniform
  610.         // This is done in the main loop since each model will have a different MVP matrix (At least for the M part)
  611.         glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &mvp[0][0]);
  612.         glUniform4f(vertexColorLocation, BGcolor.x, BGcolor.y, BGcolor.z, 1.0f);
  613.         glClearColor(.5f, .5f, .5f, 1);
  614.         glEnable(GL_DEPTH_TEST);
  615.         glDepthFunc(GL_LESS);
  616.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  617.  
  618.         Sponge(glm::vec3(1, 1, 1), glm::vec3(-1, -1, -1), f, &vertices, &triangleCount, &UV);
  619.  
  620.         glBindVertexArray(VAO);
  621.  
  622.         glGenBuffers(1, &VBO);
  623.         glBindBuffer(GL_ARRAY_BUFFER, VBO);
  624.         glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(GLfloat), vertices.data(), GL_STATIC_DRAW);
  625.         glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
  626.         glEnableVertexAttribArray(0);
  627.  
  628.         glGenBuffers(1, &colorbuffer);
  629.         glBindBuffer(GL_ARRAY_BUFFER, colorbuffer);
  630.         glBufferData(GL_ARRAY_BUFFER, UV.size() * sizeof(GLfloat), UV.data(), GL_DYNAMIC_DRAW);
  631.         glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
  632.         glEnableVertexAttribArray(1);
  633.  
  634.         //Carpet(f, 1.0f, 1.0f, -1.0f, -1.0f, VBO, VAO, shaderProgram);
  635.         glUseProgram(shaderProgram);
  636.         glBindVertexArray(VAO);
  637.         glDrawArrays(GL_TRIANGLES, 0, vertices.size());
  638.  
  639.         ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
  640.  
  641.         glfwMakeContextCurrent(window);
  642.         glfwSwapBuffers(window);
  643.     }
  644.  
  645.     // Cleanup
  646.     ImGui_ImplOpenGL3_Shutdown();
  647.     ImGui_ImplGlfw_Shutdown();
  648.     ImGui::DestroyContext();
  649.  
  650.     glfwDestroyWindow(window);
  651.     glfwTerminate();
  652.  
  653.     return 0;
  654. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement