💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
2.0 KiB

7 years ago
  1. /*
  2. * Copyright (C) 2017 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper Source Code.
  5. *
  6. * Antkeeper Source Code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper Source Code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper Source Code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "debug.hpp"
  20. #include <iostream>
  21. LineBatcher::LineBatcher(std::size_t lineCount):
  22. lineCount(lineCount),
  23. currentLine(0),
  24. width(1.0f),
  25. color(1.0f)
  26. {
  27. batch.resize(lineCount);
  28. range = batch.addRange();
  29. range->material = &material;
  30. material.color = Vector3(1.0f);
  31. }
  32. void LineBatcher::begin()
  33. {
  34. currentLine = 0;
  35. range->start = 0;
  36. range->length = 0;
  37. }
  38. void LineBatcher::end()
  39. {
  40. range->length = currentLine;
  41. batch.update();
  42. }
  43. void LineBatcher::draw(const Vector3& start, const Vector3& end)
  44. {
  45. if (currentLine >= batch.getBillboardCount())
  46. {
  47. std::cout << "LineBatcher::draw(): maximum line count exceeded" << std::endl;
  48. return;
  49. }
  50. Vector3 center = (start + end) * 0.5f;
  51. float length = glm::length(end - start);
  52. Vector3 forward = glm::normalize(end - start);
  53. glm::quat rotation = glm::normalize(glm::rotation(Vector3(1, 0, 0), forward));
  54. Billboard* billboard = batch.getBillboard(currentLine);
  55. billboard->setTranslation(center);
  56. billboard->setDimensions(Vector2(length, width));
  57. billboard->setRotation(rotation);
  58. billboard->setTintColor(color);
  59. ++currentLine;
  60. }
  61. void LineBatcher::setWidth(float width)
  62. {
  63. this->width = width;
  64. }
  65. void LineBatcher::setColor(const Vector4& color)
  66. {
  67. this->color = color;
  68. }