TextRenderer.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. #include <nanovg.h>
  11. #define NANOVG_GL3
  12. #include <nanovg_gl.h>
  13. using namespace std;
  14. IGL_INLINE igl::TextRenderer::TextRenderer() { }
  15. IGL_INLINE int igl::TextRenderer::Init()
  16. {
  17. cerr << "Init TextRenderer" << endl;
  18. #ifdef NDEBUG
  19. ctx = nvgCreateGL3(NVG_STENCIL_STROKES | NVG_ANTIALIAS);
  20. #else
  21. ctx = nvgCreateGL3(NVG_STENCIL_STROKES | NVG_ANTIALIAS | NVG_DEBUG);
  22. #endif
  23. nvgFontSize(ctx, 16);
  24. nvgFontFace(ctx, "sans");
  25. nvgTextAlign(ctx, NVG_ALIGN_LEFT | NVG_ALIGN_MIDDLE);
  26. NVGcolor c;
  27. c.r = 0.2;
  28. c.g = 0.2;
  29. c.b = 255;
  30. c.a = 255;
  31. nvgFillColor(ctx, c);
  32. nvgStrokeColor(ctx, c);
  33. }
  34. IGL_INLINE int igl::TextRenderer::Shut()
  35. {
  36. cerr << "Shut TextRenderer" << endl;
  37. nvgDeleteGL3(ctx);
  38. }
  39. IGL_INLINE void igl::TextRenderer::BeginDraw(const Eigen::Matrix4f &view, const Eigen::Matrix4f &proj,
  40. const Eigen::Vector4f &_viewport, float _object_scale)
  41. {
  42. cerr << "BeginDraw TextRenderer" << endl;
  43. viewport = _viewport;
  44. proj_matrix = proj;
  45. view_matrix = view;
  46. // CTwGraphOpenGLCore::BeginDraw(viewport[2], viewport[3]);
  47. // glEnable(GL_DEPTH_TEST);
  48. // glDepthMask(GL_FALSE);
  49. object_scale = _object_scale;
  50. Eigen::Vector2i mFBSize;
  51. Eigen::Vector2i mSize;
  52. GLFWwindow* mGLFWWindow = glfwGetCurrentContext();
  53. glfwGetFramebufferSize(mGLFWWindow,&mFBSize[0],&mFBSize[1]);
  54. glfwGetWindowSize(mGLFWWindow,&mSize[0],&mSize[1]);
  55. glViewport(0,0,mFBSize[0],mFBSize[1]);
  56. /* Calculate pixel ratio for hi-dpi devices. */
  57. float mPixelRatio = (float)mFBSize[0] / (float)mSize[0];
  58. nvgBeginFrame(ctx,mSize[0],mSize[1],mPixelRatio);
  59. }
  60. IGL_INLINE void igl::TextRenderer::EndDraw()
  61. {
  62. // /* Limit the number of cached text objects */
  63. // for (auto it = m_textObjects.cbegin(); it != m_textObjects.cend(); )
  64. // {
  65. // if (m_textObjects.size() < 1000000)
  66. // break;
  67. // DeleteTextObj(it->second);
  68. // m_textObjects.erase(it++);
  69. // }
  70. // glDepthMask(GL_TRUE);
  71. // CTwGraphOpenGLCore::EndDraw();
  72. nvgEndFrame(ctx);
  73. }
  74. IGL_INLINE void igl::TextRenderer::DrawText(Eigen::Vector3d pos, Eigen::Vector3d normal, const std::string &text)
  75. {
  76. pos += normal * 0.005f * object_scale;
  77. Eigen::Vector3f coord = igl::project(Eigen::Vector3f(pos(0), pos(1), pos(2)),
  78. view_matrix, proj_matrix, viewport);
  79. // auto it = m_textObjects.find(text);
  80. // void *text_obj = nullptr;
  81. // if (it == m_textObjects.end())
  82. // {
  83. // text_obj = NewTextObj();
  84. // BuildText(text_obj, &text, NULL, NULL, 1, g_DefaultNormalFont, 0, 0);
  85. // m_textObjects[text] = text_obj;
  86. // } else {
  87. // text_obj = it->second;
  88. // }
  89. // m_shader.bind();
  90. // glUniform1f(m_TriTexUniLocationDepth, 2*(coord(2)-0.5f));
  91. //CTwGraphOpenGLCore::DrawText(text_obj, coord[0], viewport[3] - coord[1], COLOR32_BLUE, 0);
  92. nvgText(ctx, coord[0], viewport[3] - coord[1], text.c_str(), nullptr);
  93. nvgText(ctx, 10, 10, "Ciao", nullptr);
  94. cerr << "Draw TextRenderer " << coord[0] << " " << viewport[3] - coord[1] << " " << text << endl;
  95. }