Advertisement
Guest User

Untitled

a guest
Jun 4th, 2019
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.39 KB | None | 0 0
  1. // Std. Includes
  2. #include <iostream>
  3. #include <map>
  4. #include <string>
  5. // GLAD
  6. #include <glad/glad.h>
  7. // GLFW
  8. #include <GLFW/glfw3.h>
  9. // GLM
  10. #include <glm/glm.hpp>
  11. #include <glm/gtc/matrix_transform.hpp>
  12. #include <glm/gtc/type_ptr.hpp>
  13. // FreeType
  14. #include <ft2build.h>
  15. #include FT_FREETYPE_H
  16. // GL includes
  17. #include <shader/shader.h>
  18.  
  19. // Properties
  20. const GLuint WIDTH = 800, HEIGHT = 600;
  21.  
  22. struct Character {
  23.     GLuint      textureID;
  24.     glm::ivec2  size;
  25.     glm::ivec2  bearing;
  26.     GLuint      advance;
  27.     GLuint      xoffset;
  28. };
  29.  
  30. std::map<GLchar, Character> characters;
  31. GLuint VAO, VBO, fontTexture;
  32. void renderText(Shader &s, std::string text, GLfloat x, GLfloat y, GLfloat scale, glm::vec3 color);
  33.  
  34. // The MAIN function, from here we start our application and run the Game loop
  35. int main()
  36. {
  37.     // Init GLFW
  38.     glfwInit();
  39.     glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  40.     glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  41.     glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  42.     glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
  43.  
  44.     GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "LearnOpenGL", nullptr, nullptr); // Windowed
  45.     glfwMakeContextCurrent(window);
  46.  
  47.     if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
  48.     {
  49.         std::cout << "Failed to initialize GLAD" << std::endl;
  50.         return -1;
  51.     }
  52.  
  53.     // Define the viewport dimensions
  54.     glViewport(0, 0, WIDTH, HEIGHT);
  55.  
  56.     // Set OpenGL options
  57.     glEnable(GL_CULL_FACE);
  58.     glEnable(GL_BLEND);
  59.     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  60.  
  61.     Shader shader("text.vert", "text.frag");
  62.     glm::mat4 projection = glm::ortho(0.0f, float(WIDTH), 0.0f, float(HEIGHT));
  63.     shader.use();
  64.     shader.setMat4("projection", projection);
  65.  
  66.     FT_Library ft;
  67.     if (FT_Init_FreeType(&ft))
  68.         std::cout << "ERROR::FREETYPE: Could not init FreeType Library" << std::endl;
  69.  
  70.     FT_Face face;
  71.     if (FT_New_Face(ft, "arial.ttf", 0, &face))
  72.         std::cout << "ERROR::FREETYPE: Failed to load font" << std::endl;
  73.  
  74.     FT_Set_Pixel_Sizes(face, 0, 48);
  75.  
  76.     if (FT_Load_Char(face, 'X', FT_LOAD_RENDER))
  77.         std::cout << "ERROR::FREETYPE: Failed to load Glyph" << std::endl;
  78.  
  79.     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  80.     glGenTextures(1, &fontTexture);
  81.     glBindTexture(GL_TEXTURE_2D, fontTexture);
  82.     glTexImage2D(
  83.         GL_TEXTURE_2D,
  84.         0,
  85.         GL_RED,
  86.         face->glyph->bitmap.width * 250,
  87.         face->glyph->bitmap.rows * 250,
  88.         0,
  89.         GL_RED,
  90.         GL_UNSIGNED_BYTE,
  91.         NULL
  92.     );
  93.     GLuint font_texture_width = 0;
  94.     for (GLubyte c = 0; c < 128; ++c)
  95.     {
  96.         if (FT_Load_Char(face, c, FT_LOAD_RENDER))
  97.         {
  98.             std::cout << "ERROR::FREETYPE: Failed to load Glyph" << std::endl;
  99.             continue;
  100.         }
  101.         GLuint texture;
  102.         glGenTextures(1, &texture);
  103.         glBindTexture(GL_TEXTURE_2D, texture);
  104.         glTexImage2D(
  105.             GL_TEXTURE_2D,
  106.             0,
  107.             GL_RED,
  108.             face->glyph->bitmap.width,
  109.             face->glyph->bitmap.rows,
  110.             0,
  111.             GL_RED,
  112.             GL_UNSIGNED_BYTE,
  113.             face->glyph->bitmap.buffer
  114.         );
  115.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  116.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  117.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  118.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  119.  
  120.         Character character = {
  121.             texture,
  122.             glm::ivec2(face->glyph->bitmap.width, face->glyph->bitmap.rows),
  123.             glm::ivec2(face->glyph->bitmap_left, face->glyph->bitmap_top),
  124.             face->glyph->advance.x,
  125.             font_texture_width
  126.         };
  127.  
  128.         font_texture_width += character.size.x;
  129.         characters.insert(std::pair<GLchar, Character>(c, character));
  130.     }
  131.     glBindTexture(GL_TEXTURE_2D, 0);
  132.     FT_Done_Face(face);
  133.     FT_Done_FreeType(ft);
  134.    
  135.     for (auto ch : characters)
  136.         font_texture_width  += ch.second.size.x;
  137.  
  138.     glGenTextures(1, &fontTexture);
  139.     glBindTexture(GL_TEXTURE_2D, fontTexture);
  140.  
  141.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, font_texture_width, 100, 0, GL_RED, GL_UNSIGNED_BYTE, NULL);
  142.  
  143.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  144.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  145.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  146.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  147.     std::cout << glGetError();
  148.     for (GLubyte c = 0; c < 128; ++c)
  149.         glCopyImageSubData( characters[c].textureID, GL_TEXTURE_2D, 0, 0, 0, 0,
  150.                             fontTexture, GL_TEXTURE_2D, 0, characters[c].xoffset, 0, 0,
  151.                             characters[c].size.x, characters[c].size.y, 0);
  152.    
  153.     glGenVertexArrays(1, &VAO);
  154.     glGenBuffers(1, &VBO);
  155.     glBindVertexArray(VAO);
  156.     glBindBuffer(GL_ARRAY_BUFFER, VBO);
  157.     glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 6 * 4, NULL, GL_DYNAMIC_DRAW);
  158.     glEnableVertexAttribArray(0);
  159.     glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), 0);
  160.     glBindBuffer(GL_ARRAY_BUFFER, 0);
  161.     glBindVertexArray(0);
  162.  
  163.  
  164.     // Game loop
  165.     while (!glfwWindowShouldClose(window))
  166.     {
  167.         // Check and call events
  168.         glfwPollEvents();
  169.  
  170.         // Clear the colorbuffer
  171.         glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
  172.         glClear(GL_COLOR_BUFFER_BIT);
  173.  
  174.         renderText(shader, "This is sample text", 25.0f, 25.0f, 1.0f, glm::vec3(0.5, 0.8f, 0.2f));
  175.         renderText(shader, "(C) LearnOpenGL.com", 540.0f, 570.0f, 0.5f, glm::vec3(0.3, 0.7f, 0.9f));
  176.  
  177.         // Swap the buffers
  178.         glfwSwapBuffers(window);
  179.     }
  180.  
  181.     glfwTerminate();
  182.     return 0;
  183.  
  184. }
  185.  
  186.  
  187. void renderText(Shader &s, std::string text, GLfloat x, GLfloat y, GLfloat scale, glm::vec3 color)
  188. {
  189.     s.use();
  190.     s.setVec3("textColor", color);
  191.     glActiveTexture(GL_TEXTURE0);
  192.     glBindVertexArray(VAO);
  193.  
  194.     std::string::const_iterator c;
  195.     glBindTexture(GL_TEXTURE_2D, fontTexture);
  196.     for (c = text.begin(); c != text.end(); ++c)
  197.     {
  198.         Character ch = characters[*c];
  199.  
  200.         GLfloat xpos = x + ch.bearing.x * scale;
  201.         GLfloat ypos = y - (ch.size.y - ch.bearing.y) * scale;
  202.  
  203.         GLfloat w = ch.size.x * scale;
  204.         GLfloat h = ch.size.y * scale;
  205.  
  206.         GLfloat vertices[6][4] = {
  207.             { xpos + characters[*c].xoffset,        ypos + h,   0.0, 0.0 },
  208.             { xpos + characters[*c].xoffset,        ypos,       0.0, 1.0 },
  209.             { xpos + w + characters[*c].xoffset,    ypos,       1.0, 1.0 },
  210.  
  211.             { xpos + characters[*c].xoffset,        ypos + h,   0.0, 0.0 },
  212.             { xpos + w + characters[*c].xoffset,    ypos,       1.0, 1.0 },
  213.             { xpos + w + characters[*c].xoffset,    ypos + h,   1.0, 0.0 }
  214.         };
  215.  
  216.         glBindBuffer(GL_ARRAY_BUFFER, VBO);
  217.         glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices);
  218.         glBindBuffer(GL_ARRAY_BUFFER, 0);
  219.  
  220.         glDrawArrays(GL_TRIANGLES, 0, 6);
  221.  
  222.         x += (ch.advance >> 6) * scale;
  223.     }
  224.     glBindVertexArray(0);
  225.     glBindTexture(GL_TEXTURE_2D, 0);
  226. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement