TextRenderer.cpp 3.6 KB

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