mode_indicator_pipeline.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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.modeColor = m_indicatorShader->uniform_handle("u_modeColor");
  47. m_uniforms.alpha = m_indicatorShader->uniform_handle("u_alpha");
  48. m_uniforms.time = m_indicatorShader->uniform_handle("u_time");
  49. }
  50. auto ModeIndicatorPipeline::is_initialized() const -> bool {
  51. return m_indicatorShader != nullptr;
  52. }
  53. void ModeIndicatorPipeline::render_indicator(Mesh *mesh,
  54. const QMatrix4x4 &model,
  55. const QMatrix4x4 &view_proj,
  56. const QVector3D &color,
  57. float alpha, float time) {
  58. if (!is_initialized() || mesh == nullptr) {
  59. return;
  60. }
  61. DepthMaskScope const depth_mask(false);
  62. BlendScope const blend(true);
  63. glEnable(GL_DEPTH_TEST);
  64. glBlendFunc(GL_SRC_ALPHA, GL_ONE);
  65. m_indicatorShader->use();
  66. QMatrix4x4 mvp = view_proj * model;
  67. m_indicatorShader->set_uniform(m_uniforms.mvp, mvp);
  68. m_indicatorShader->set_uniform(m_uniforms.modeColor, color);
  69. m_indicatorShader->set_uniform(m_uniforms.alpha, alpha);
  70. m_indicatorShader->set_uniform(m_uniforms.time, time);
  71. mesh->draw();
  72. check_gl_error("render_indicator");
  73. }
  74. } // namespace Render::GL::BackendPipelines