Advertisement
Masterchoc

Untitled

Jan 4th, 2018
377
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.61 KB | None | 0 0
  1. #include "text.h"
  2.  
  3. Text::Text(const std::string& text, int x, int y, int size) : m_text(text), m_x(x), m_y(y), m_size(size), m_length(text.length())
  4. {
  5.     glGenBuffers(1, &m_vbo_vertices);
  6.     glGenBuffers(1, &m_vbo_uvs);
  7. }
  8.  
  9. void Text::draw()
  10. {
  11.     std::vector<glm::vec2> vertices;
  12.     std::vector<glm::vec2> uvs;
  13.  
  14.     for (unsigned int i = 0; i < m_length; i++)
  15.     {
  16.         glm::vec2 v_up_left = glm::vec2(m_x + i * m_size, m_y + m_size);
  17.         glm::vec2 v_up_right = glm::vec2(m_x + i * m_size + m_size, m_y + m_size);
  18.         glm::vec2 v_down_right = glm::vec2(m_x + i * m_size + m_size, m_y);
  19.         glm::vec2 v_down_left = glm::vec2(m_x + i * m_size, m_y);
  20.  
  21.         vertices.push_back(v_up_left);
  22.         vertices.push_back(v_down_left);
  23.         vertices.push_back(v_up_right);
  24.  
  25.         vertices.push_back(v_down_right);
  26.         vertices.push_back(v_up_right);
  27.         vertices.push_back(v_down_left);
  28.  
  29.         char character = m_text.at(i);
  30.  
  31.         float uv_x = (character % 16) / 16.0f;
  32.         float uv_y = (character / 16) / 16.0f;
  33.  
  34.         glm::vec2 u_up_left = glm::vec2(uv_x, 1.0f - uv_y);
  35.         glm::vec2 u_up_right = glm::vec2(uv_x + 1.0f / 16.0f, 1.0f - uv_y);
  36.         glm::vec2 u_down_right = glm::vec2(uv_x + 1.0f / 16.0f, 1.0f - (uv_y + 1.0f / 16.0f));
  37.         glm::vec2 u_down_left = glm::vec2(uv_x, 1.0f - (uv_y + 1.0f / 16.0f));
  38.  
  39.         uvs.push_back(u_up_left);
  40.         uvs.push_back(u_down_left);
  41.         uvs.push_back(u_up_right);
  42.  
  43.         uvs.push_back(u_down_right);
  44.         uvs.push_back(u_up_right);
  45.         uvs.push_back(u_down_left);
  46.     }
  47.  
  48.     glBindBuffer(GL_ARRAY_BUFFER, m_vbo_vertices);
  49.     glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec2), &vertices[0], GL_STATIC_DRAW);
  50.     glBindBuffer(GL_ARRAY_BUFFER, m_vbo_uvs);
  51.     glBufferData(GL_ARRAY_BUFFER, uvs.size() * sizeof(glm::vec2), &uvs[0], GL_STATIC_DRAW);
  52.  
  53.     this->getMaterial()->getShader()->bind();
  54.     this->getMaterial()->bindAttributes();
  55.     this->getMaterial()->getDiffuseTexture()->bind(0);
  56.     this->getMaterial()->getShader()->setUniform1i("diffuseTexture", 0);
  57.  
  58.     glEnableVertexAttribArray(0);
  59.     glBindBuffer(GL_ARRAY_BUFFER, m_vbo_vertices);
  60.     glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, nullptr);
  61.  
  62.     glEnableVertexAttribArray(1);
  63.     glBindBuffer(GL_ARRAY_BUFFER, m_vbo_uvs);
  64.     glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, nullptr);
  65.  
  66.     glEnable(GL_BLEND);
  67.     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  68.  
  69.     glDrawArrays(GL_TRIANGLES, 0, vertices.size());
  70.     glDisable(GL_BLEND);
  71.  
  72.  
  73.     glDisableVertexAttribArray(0);
  74.     glDisableVertexAttribArray(1);
  75.  
  76.     this->getMaterial()->getShader()->unbind();
  77.     this->getMaterial()->getDiffuseTexture()->unbind();
  78. }
  79.  
  80. Text::~Text()
  81. {
  82.     glDeleteBuffers(1, &m_vbo_vertices);
  83.     glDeleteBuffers(1, &m_vbo_uvs);
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement