TextRenderer.cpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. #ifdef IGL_VIEWER_WITH_NANOGUI
  9. #include "TextRenderer.h"
  10. #include "TextRenderer_fonts.h"
  11. #include <igl/project.h>
  12. #include <nanogui/opengl.h>
  13. #include <nanovg.h>
  14. #include <Eigen/Dense>
  15. #define NANOVG_GL3
  16. #include <nanovg_gl.h>
  17. IGL_INLINE igl::viewer::TextRenderer::TextRenderer(): ctx(nullptr) {}
  18. IGL_INLINE int igl::viewer::TextRenderer::Init()
  19. {
  20. using namespace std;
  21. #ifdef NDEBUG
  22. ctx = nvgCreateGL3(NVG_STENCIL_STROKES | NVG_ANTIALIAS);
  23. #else
  24. ctx = nvgCreateGL3(NVG_STENCIL_STROKES | NVG_ANTIALIAS | NVG_DEBUG);
  25. #endif
  26. nvgCreateFontMem(ctx, "sans", igl_roboto_regular_ttf,
  27. igl_roboto_regular_ttf_size, 0);
  28. return 0;
  29. }
  30. IGL_INLINE int igl::viewer::TextRenderer::Shut()
  31. {
  32. using namespace std;
  33. if(ctx)
  34. nvgDeleteGL3(ctx);
  35. return 0;
  36. }
  37. IGL_INLINE void igl::viewer::TextRenderer::BeginDraw(
  38. const Eigen::Matrix4f &view,
  39. const Eigen::Matrix4f &proj,
  40. const Eigen::Vector4f &_viewport,
  41. float _object_scale)
  42. {
  43. using namespace std;
  44. viewport = _viewport;
  45. proj_matrix = proj;
  46. view_matrix = view;
  47. object_scale = _object_scale;
  48. Eigen::Vector2i mFBSize;
  49. Eigen::Vector2i mSize;
  50. GLFWwindow* mGLFWWindow = glfwGetCurrentContext();
  51. glfwGetFramebufferSize(mGLFWWindow,&mFBSize[0],&mFBSize[1]);
  52. glfwGetWindowSize(mGLFWWindow,&mSize[0],&mSize[1]);
  53. glViewport(0,0,mFBSize[0],mFBSize[1]);
  54. glClear(GL_STENCIL_BUFFER_BIT);
  55. /* Calculate pixel ratio for hi-dpi devices. */
  56. mPixelRatio = (float)mFBSize[0] / (float)mSize[0];
  57. nvgBeginFrame(ctx,mSize[0],mSize[1],mPixelRatio);
  58. }
  59. IGL_INLINE void igl::viewer::TextRenderer::EndDraw()
  60. {
  61. using namespace std;
  62. nvgEndFrame(ctx);
  63. }
  64. IGL_INLINE void igl::viewer::TextRenderer::DrawText(
  65. Eigen::Vector3d pos, Eigen::Vector3d normal, const std::string &text)
  66. {
  67. using namespace std;
  68. pos += normal * 0.005f * object_scale;
  69. Eigen::Vector3f coord = igl::project(Eigen::Vector3f(pos(0), pos(1), pos(2)),
  70. view_matrix, proj_matrix, viewport);
  71. nvgFontSize(ctx, 16*mPixelRatio);
  72. nvgFontFace(ctx, "sans");
  73. nvgTextAlign(ctx, NVG_ALIGN_LEFT | NVG_ALIGN_MIDDLE);
  74. nvgFillColor(ctx, nvgRGBA(10,10,250,255));
  75. nvgText(ctx, coord[0]/mPixelRatio, (viewport[3] - coord[1])/mPixelRatio, text.c_str(), NULL);
  76. }
  77. #endif