Advertisement
Guest User

Untitled

a guest
Apr 3rd, 2022
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.39 KB | None | 0 0
  1. //========================================================================
  2. // Simple multi-window example
  3. // Copyright (c) Camilla Löwy <[email protected]>
  4. //
  5. // This software is provided 'as-is', without any express or implied
  6. // warranty. In no event will the authors be held liable for any damages
  7. // arising from the use of this software.
  8. //
  9. // Permission is granted to anyone to use this software for any purpose,
  10. // including commercial applications, and to alter it and redistribute it
  11. // freely, subject to the following restrictions:
  12. //
  13. // 1. The origin of this software must not be misrepresented; you must not
  14. //    claim that you wrote the original software. If you use this software
  15. //    in a product, an acknowledgment in the product documentation would
  16. //    be appreciated but is not required.
  17. //
  18. // 2. Altered source versions must be plainly marked as such, and must not
  19. //    be misrepresented as being the original software.
  20. //
  21. // 3. This notice may not be removed or altered from any source
  22. //    distribution.
  23. //
  24. //========================================================================
  25.  
  26. /* MODIFIED GLFW EXAMPLE */
  27.  
  28. #include <glad/glad.h>
  29.  
  30. #define GLFW_INCLUDE_NONE
  31. #include <GLFW/glfw3.h>
  32.  
  33. using namespace std;
  34. #include <iostream>
  35. #include <vector>
  36. #include <string>
  37.  
  38. #include <glm/glm.hpp>
  39. #include <glm/ext.hpp>
  40.  
  41. #define STB_IMAGE_IMPLEMENTATION
  42. #include "stb_image.h"
  43.  
  44. typedef unsigned int ID;
  45.  
  46. static const unsigned int SCR_WIDTH = 800;
  47. const unsigned int SCR_HEIGHT = 380;
  48. static const float TILE_WIDTH = 64.0f; // = 0.6f;
  49. static const float TILE_HEIGHT = 32.0f; // = -0.16f;
  50.  
  51. class GraphicEngine {
  52. public:
  53.     GraphicEngine();
  54.     ~GraphicEngine();
  55.     void initShaders(const char* vertexShaderSource, const char* fragmentShaderSource);
  56.     void initRenderData(float vertices[], unsigned int size);
  57.     std::vector<ID> initTextures(std::vector<std::string>& paths);
  58.     void drawTextures(std::vector<ID> testuresIds, float wndRatio);
  59.  
  60. private:
  61.     GraphicEngine(GraphicEngine&) = delete;
  62.     GraphicEngine(GraphicEngine&&) = delete;
  63.     GraphicEngine& operator=(const GraphicEngine& other) = delete;
  64. private:
  65.     unsigned int VBO = 0;
  66.     unsigned int VAO = 0;
  67.     unsigned int EBO = 0;
  68.     unsigned int shaderProgram;
  69. };
  70.  
  71. GraphicEngine::GraphicEngine() {
  72. }
  73.  
  74. GraphicEngine::~GraphicEngine() {
  75.     glDeleteVertexArrays(1, &VAO);
  76.     glDeleteBuffers(1, &VBO);
  77.     glDeleteBuffers(1, &EBO);
  78. }
  79.  
  80. void GraphicEngine::initShaders(const char* vertexShaderSource, const char* fragmentShaderSource) {
  81. #if 1
  82.     glEnable(GL_BLEND);
  83.     glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
  84.     //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  85. #endif
  86. #if 0
  87.     glEnable(GL_BLEND);
  88.     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  89.     glBlendEquation(GL_FUNC_ADD);
  90. #endif
  91. #if 0
  92.     glEnable(GL_BLEND);
  93.     glBlendFuncSeparate(GL_ONE, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ZERO);
  94.     glBlendEquationSeparate(GL_FUNC_ADD, GL_FUNC_ADD);
  95. #endif
  96.  
  97. #if 0
  98.     glHint(GL_POINT_SMOOTH, GL_NICEST);
  99.     glHint(GL_LINE_SMOOTH, GL_NICEST);
  100.     glHint(GL_POLYGON_SMOOTH, GL_NICEST);
  101.     glEnable(GL_POINT_SMOOTH);
  102.     glEnable(GL_LINE_SMOOTH);
  103.     glEnable(GL_POLYGON_SMOOTH);
  104. #endif
  105.     unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER);
  106.     unsigned int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
  107.     shaderProgram = glCreateProgram();
  108.     glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
  109.     glCompileShader(vertexShader);
  110.     glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
  111.     glCompileShader(fragmentShader);
  112.     glAttachShader(shaderProgram, vertexShader);
  113.     glAttachShader(shaderProgram, fragmentShader);
  114.     glLinkProgram(shaderProgram);
  115. }
  116.  
  117. void GraphicEngine::initRenderData(float vertices[], unsigned int size) {
  118.     unsigned int indices[] = {
  119.         0, 1, 3,
  120.         1, 2, 3
  121.     };
  122.     glGenVertexArrays(1, &VAO);
  123.     glGenBuffers(1, &VBO);
  124.     glGenBuffers(1, &EBO);
  125.  
  126.     glBindVertexArray(VAO);
  127.     glBindBuffer(GL_ARRAY_BUFFER, VBO);
  128.     glBufferData(GL_ARRAY_BUFFER, size, vertices, GL_STATIC_DRAW);
  129.  
  130.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
  131.     glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
  132.  
  133.     glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
  134.     glEnableVertexAttribArray(0);
  135.     glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
  136.     glEnableVertexAttribArray(1);
  137. }
  138.  
  139. std::vector<ID> GraphicEngine::initTextures(std::vector<std::string>& paths) {
  140.     std::vector<ID> ids(paths.size());
  141.     stbi_set_flip_vertically_on_load(true);
  142.     for (int i = 0; i < paths.size(); i++) {
  143.         glGenTextures(1, &ids[i]);
  144.         glBindTexture(GL_TEXTURE_2D, ids[i]);
  145.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  146.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  147.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); // GL_LINEAR); // GL_NEAREST);
  148.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); // GL_LINEAR); // GL_NEAREST);
  149.         int width, height, nrChannels;
  150.         unsigned char* data = stbi_load(paths[i].c_str(), &width, &height, &nrChannels, STBI_rgb_alpha);
  151.         if (data) {
  152.             glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
  153.             glGenerateMipmap(GL_TEXTURE_2D);
  154.         }
  155.         stbi_image_free(data);
  156.     }
  157.     return ids;
  158. }
  159.  
  160. void GraphicEngine::drawTextures(std::vector<ID> testuresIds, float wndRatio) {
  161.     const int countx = 3, county = 3; /* number of tiles */
  162.     const float scale = 100.0f; /* zoom */
  163.     const glm::mat4 mvp = glm::ortho(-wndRatio * scale, wndRatio * scale, -scale, scale, 2.0f, -2.0f);
  164.     const float offx = -((countx * TILE_WIDTH * 2.0f) * 0.5f - TILE_WIDTH);
  165.     const float offy = -TILE_WIDTH * 0.5f;
  166.     for (auto testureId : testuresIds) {
  167.         for (int y = 0; y < county; y++) {
  168.             for (int x = 0; x < countx - (y & 1 ? 1 : 0); x++) {
  169.                 const glm::mat4 transform = mvp * glm::translate(glm::mat4(1.0f), glm::vec3(
  170.                     offx + x * TILE_WIDTH * 2.0f + (y & 1 ? TILE_WIDTH : 0.0f),
  171.                     offy + y * TILE_HEIGHT, 0.0f));
  172.                 glBindTexture(GL_TEXTURE_2D, testureId);
  173.                 const GLint transformLoc = glGetUniformLocation(shaderProgram, "transform");
  174.                 glUniformMatrix4fv(transformLoc, 1, GL_FALSE, glm::value_ptr(transform));
  175.                 glUseProgram(shaderProgram);
  176.                 glBindVertexArray(VAO);
  177.                 glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
  178.             }
  179.         }
  180.     }
  181. }
  182.  
  183. class Window {
  184. protected:
  185.     GLFWwindow* window;
  186.     GraphicEngine graphicEngine;
  187. public:
  188.     Window::Window() :
  189.         window(nullptr)
  190.     {}
  191.  
  192.     Window::~Window() {
  193.         glfwTerminate();
  194.     }
  195.  
  196.     bool Window::initWindowResources() {
  197.         bool result = false;
  198.         if (glfwInit() == GLFW_TRUE) {
  199.             glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  200.             glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  201.             glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  202.             window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
  203.             if (window != nullptr) {
  204.                 glfwMakeContextCurrent(window);
  205.                 if (glfwSetFramebufferSizeCallback(window, [](GLFWwindow* window, int width, int height) {
  206.                     glViewport(0, 0, width, height); }) == NULL) {
  207.                     if (gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
  208.                         result = true;
  209.                     }
  210.                 }
  211.             }
  212.         }
  213.         return result;
  214.     }
  215.  
  216.     void Window::mainWindowLoop() {
  217.         const char* vertexShaderSource =
  218.             "#version 330 core\n"
  219.             "layout(location = 0) in vec3 aPos;\n"
  220.             "layout(location = 1) in vec2 aTexCoord;\n"
  221.             "out vec2 TexCoord;\n"
  222.             "uniform mat4 transform;\n"
  223.             "void main()\n"
  224.             "{\n"
  225.             "    gl_Position = transform * vec4(aPos, 1.0);\n"
  226.             "    TexCoord = vec2(aTexCoord.x, aTexCoord.y);\n"
  227.             "}\n\0";
  228.  
  229.         const char* fragmentShaderSource =
  230.             "#version 330 core\n"
  231.             "out vec4 FragColor;\n"
  232.             "in vec3 ourColor;\n"
  233.             "in vec2 TexCoord;\n"
  234.             "uniform sampler2D texture1;\n"
  235.             "void main()\n"
  236.             "{\n"
  237.             "    vec4 c = texture(texture1, TexCoord);\n"
  238.             "    //if (c.a > 0.8f)\n"
  239.             "       FragColor = c; \n"
  240.             "    //else discard; \n"
  241.             "}\n\0";
  242.  
  243.         graphicEngine.initShaders(vertexShaderSource, fragmentShaderSource);
  244.         std::vector<std::string> pathsTextures = {
  245.             //"IlbtL.png" /* original tile image, premultiplied alpha */
  246.             //"IlbtLmod0.png" /* enlarge 64x32 tile image, premultiplied alpha */
  247.             //"testb.png" /* modified tile image, not masked */
  248.             "sea2.png" /* modified tile image, 64x32, with marked edges */
  249.             //"sea3.png" /* modified tile image, 64x32, w/o marked edges */
  250.             //"sea4.png" /* modified tile image, 64x32, w/o marked edges, darker */
  251.         };
  252.         float vertices[] = {
  253.             // positions               // texture coords
  254.             -TILE_WIDTH,  TILE_HEIGHT, 0.0f,  1.0f, 1.0f, // top right
  255.             -TILE_WIDTH, -TILE_HEIGHT, 0.0f,  1.0f, 0.0f, // bottom right
  256.              TILE_WIDTH, -TILE_HEIGHT, 0.0f,  0.0f, 0.0f, // bottom left
  257.              TILE_WIDTH,  TILE_HEIGHT, 0.0f,  0.0f, 1.0f  // top left
  258.         };
  259.  
  260.         graphicEngine.initRenderData(vertices, sizeof(vertices));
  261.         std::vector<ID> idsTextures = graphicEngine.initTextures(pathsTextures);
  262.         while (!glfwWindowShouldClose(window))
  263.         {
  264.             int width, height;
  265.             glfwGetFramebufferSize(window, &width, &height);
  266.             const float wndRatio = width / (float)height;
  267.  
  268.             glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
  269.             glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  270.             graphicEngine.drawTextures(idsTextures, wndRatio);
  271.             glfwSwapBuffers(window);
  272.             glfwPollEvents();
  273.         }
  274.     }
  275. };
  276.  
  277. int main(int argc, char* argv[])
  278. {
  279.     Window window;
  280.     if (window.initWindowResources()) {
  281.         window.mainWindowLoop();
  282.     }
  283.     return 0;
  284. }
  285.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement