TextRenderer.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2014 Wenzel Jacob <wenzel@inf.ethz.ch>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #include "TextRenderer.h"
  9. #include <igl/project.h>
  10. IGL_INLINE igl::TextRenderer::TextRenderer() : m_shaderHandleBackup(0) { }
  11. IGL_INLINE int igl::TextRenderer::Init()
  12. {
  13. int retval = CTwGraphOpenGLCore::Init();
  14. if (retval == 1)
  15. {
  16. std::string vertexShader =
  17. "#version 150\n"
  18. "uniform vec2 offset;"
  19. "uniform vec2 wndSize;"
  20. "uniform vec4 color;"
  21. "uniform float depth;"
  22. "in vec2 vertex;"
  23. "in vec2 uv;"
  24. "out vec4 fcolor;"
  25. "out vec2 fuv;"
  26. "void main() {"
  27. " gl_Position = vec4(2.0*(vertex.x+offset.x-0.5)/wndSize.x - 1.0,"
  28. " 1.0 - 2.0*(vertex.y+offset.y-0.5)/wndSize.y,"
  29. " depth, 1);"
  30. " fuv = uv;"
  31. " fcolor = color;"
  32. "}";
  33. std::string fragmentShader =
  34. "#version 150\n"
  35. "uniform sampler2D tex;"
  36. "in vec2 fuv;"
  37. "in vec4 fcolor;"
  38. "out vec4 outColor;"
  39. "void main() { outColor.rgb = fcolor.bgr; outColor.a = fcolor.a * texture(tex, fuv).r; }";
  40. if (!m_shader.init(vertexShader, fragmentShader, "outColor"))
  41. return 0;
  42. /* Adjust location bindings */
  43. glBindAttribLocation(m_shader.program_shader, 0, "vertex");
  44. glBindAttribLocation(m_shader.program_shader, 1, "uv");
  45. glBindAttribLocation(m_shader.program_shader, 2, "color");
  46. glLinkProgram(m_shader.program_shader);
  47. m_shaderHandleBackup = m_TriTexUniProgram;
  48. m_TriTexUniProgram = m_shader.program_shader;
  49. m_TriTexUniLocationOffset = m_shader.uniform("offset");
  50. m_TriTexUniLocationWndSize = m_shader.uniform("wndSize");
  51. m_TriTexUniLocationColor = m_shader.uniform("color");
  52. m_TriTexUniLocationTexture = m_shader.uniform("tex");
  53. m_TriTexUniLocationDepth = m_shader.uniform("depth");
  54. }
  55. return retval;
  56. }
  57. IGL_INLINE int igl::TextRenderer::Shut()
  58. {
  59. for (auto kv : m_textObjects)
  60. DeleteTextObj(kv.second);
  61. m_shader.free();
  62. m_TriTexUniProgram = m_shaderHandleBackup;
  63. return CTwGraphOpenGLCore::Shut();
  64. }
  65. IGL_INLINE void igl::TextRenderer::BeginDraw(const Eigen::Matrix4f &view, const Eigen::Matrix4f &proj,
  66. const Eigen::Vector4f &_viewport, float _object_scale)
  67. {
  68. viewport = _viewport;
  69. proj_matrix = proj;
  70. view_matrix = view;
  71. CTwGraphOpenGLCore::BeginDraw(viewport[2], viewport[3]);
  72. glEnable(GL_DEPTH_TEST);
  73. glDepthMask(GL_FALSE);
  74. object_scale = _object_scale;
  75. }
  76. IGL_INLINE void igl::TextRenderer::EndDraw()
  77. {
  78. /* Limit the number of cached text objects */
  79. for (auto it = m_textObjects.cbegin(); it != m_textObjects.cend(); )
  80. {
  81. if (m_textObjects.size() < 1000000)
  82. break;
  83. DeleteTextObj(it->second);
  84. m_textObjects.erase(it++);
  85. }
  86. glDepthMask(GL_TRUE);
  87. CTwGraphOpenGLCore::EndDraw();
  88. }
  89. IGL_INLINE void igl::TextRenderer::DrawText(Eigen::Vector3d pos, Eigen::Vector3d normal, const std::string &text)
  90. {
  91. pos += normal * 0.005f * object_scale;
  92. Eigen::Vector3f coord = igl::project(Eigen::Vector3f(pos(0), pos(1), pos(2)),
  93. view_matrix, proj_matrix, viewport);
  94. auto it = m_textObjects.find(text);
  95. void *text_obj = nullptr;
  96. if (it == m_textObjects.end())
  97. {
  98. text_obj = NewTextObj();
  99. BuildText(text_obj, &text, NULL, NULL, 1, g_DefaultNormalFont, 0, 0);
  100. m_textObjects[text] = text_obj;
  101. } else {
  102. text_obj = it->second;
  103. }
  104. m_shader.bind();
  105. glUniform1f(m_TriTexUniLocationDepth, 2*(coord(2)-0.5f));
  106. CTwGraphOpenGLCore::DrawText(text_obj, coord[0], viewport[3] - coord[1], COLOR32_BLUE, 0);
  107. }