TextRenderer.cpp 3.6 KB

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