TextRenderer.cpp 2.6 KB

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