mode_indicator_pipeline.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #include "mode_indicator_pipeline.h"
  2. #include "../backend.h"
  3. #include "../mesh.h"
  4. #include "../shader_cache.h"
  5. #include "../state_scopes.h"
  6. #include <QDebug>
  7. #include <QOpenGLContext>
  8. namespace Render::GL::BackendPipelines {
  9. namespace {
  10. void clear_gl_errors() {
  11. #ifndef NDEBUG
  12. while (glGetError() != GL_NO_ERROR) {
  13. }
  14. #endif
  15. }
  16. auto check_gl_error(const char *operation) -> bool {
  17. #ifndef NDEBUG
  18. GLenum err = glGetError();
  19. if (err != GL_NO_ERROR) {
  20. qWarning() << "ModeIndicatorPipeline GL error in" << operation << ":"
  21. << err;
  22. return false;
  23. }
  24. #else
  25. Q_UNUSED(operation);
  26. #endif
  27. return true;
  28. }
  29. } // namespace
  30. auto ModeIndicatorPipeline::initialize() -> bool {
  31. if (m_shaderCache == nullptr) {
  32. qWarning() << "ModeIndicatorPipeline::initialize: null ShaderCache";
  33. return false;
  34. }
  35. initializeOpenGLFunctions();
  36. clear_gl_errors();
  37. m_indicatorShader = m_shaderCache->get("mode_indicator");
  38. if (m_indicatorShader == nullptr) {
  39. qWarning() << "ModeIndicatorPipeline: Failed to get mode_indicator shader";
  40. return false;
  41. }
  42. cache_uniforms();
  43. qInfo() << "ModeIndicatorPipeline initialized successfully";
  44. return is_initialized();
  45. }
  46. void ModeIndicatorPipeline::shutdown() { m_indicatorShader = nullptr; }
  47. void ModeIndicatorPipeline::cache_uniforms() {
  48. if (m_indicatorShader == nullptr) {
  49. return;
  50. }
  51. m_uniforms.mvp = m_indicatorShader->uniform_handle("u_mvp");
  52. m_uniforms.modeColor = m_indicatorShader->uniform_handle("u_modeColor");
  53. m_uniforms.alpha = m_indicatorShader->uniform_handle("u_alpha");
  54. m_uniforms.time = m_indicatorShader->uniform_handle("u_time");
  55. }
  56. auto ModeIndicatorPipeline::is_initialized() const -> bool {
  57. return m_indicatorShader != nullptr;
  58. }
  59. void ModeIndicatorPipeline::render_indicator(Mesh *mesh,
  60. const QMatrix4x4 &model,
  61. const QMatrix4x4 &view_proj,
  62. const QVector3D &color,
  63. float alpha, float time) {
  64. if (!is_initialized() || mesh == nullptr) {
  65. return;
  66. }
  67. DepthMaskScope const depth_mask(false);
  68. BlendScope const blend(true);
  69. glEnable(GL_DEPTH_TEST);
  70. glBlendFunc(GL_SRC_ALPHA, GL_ONE);
  71. m_indicatorShader->use();
  72. QMatrix4x4 mvp = view_proj * model;
  73. m_indicatorShader->set_uniform(m_uniforms.mvp, mvp);
  74. m_indicatorShader->set_uniform(m_uniforms.modeColor, color);
  75. m_indicatorShader->set_uniform(m_uniforms.alpha, alpha);
  76. m_indicatorShader->set_uniform(m_uniforms.time, time);
  77. mesh->draw();
  78. check_gl_error("render_indicator");
  79. }
  80. } // namespace Render::GL::BackendPipelines