mode_indicator_pipeline.cpp 2.7 KB

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