renderer.h 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #pragma once
  2. #include "shader.h"
  3. #include "camera.h"
  4. #include "mesh.h"
  5. #include "texture.h"
  6. #include <QOpenGLFunctions_3_3_Core>
  7. #include <memory>
  8. #include <vector>
  9. namespace Engine::Core {
  10. class World;
  11. class Entity;
  12. }
  13. namespace Render::GL {
  14. struct RenderCommand {
  15. Mesh* mesh = nullptr;
  16. Texture* texture = nullptr;
  17. QMatrix4x4 modelMatrix;
  18. QVector3D color{1.0f, 1.0f, 1.0f};
  19. };
  20. class Renderer : protected QOpenGLFunctions_3_3_Core {
  21. public:
  22. Renderer();
  23. ~Renderer();
  24. bool initialize();
  25. void shutdown();
  26. void beginFrame();
  27. void endFrame();
  28. void setCamera(Camera* camera);
  29. void setClearColor(float r, float g, float b, float a = 1.0f);
  30. // Immediate mode rendering
  31. void drawMesh(Mesh* mesh, const QMatrix4x4& modelMatrix, Texture* texture = nullptr);
  32. void drawLine(const QVector3D& start, const QVector3D& end, const QVector3D& color);
  33. // Batch rendering
  34. void submitRenderCommand(const RenderCommand& command);
  35. void flushBatch();
  36. // Render ECS entities
  37. void renderWorld(Engine::Core::World* world);
  38. private:
  39. Camera* m_camera = nullptr;
  40. std::unique_ptr<Shader> m_basicShader;
  41. std::unique_ptr<Shader> m_lineShader;
  42. std::vector<RenderCommand> m_renderQueue;
  43. // Default resources
  44. std::unique_ptr<Mesh> m_quadMesh;
  45. std::unique_ptr<Texture> m_whiteTexture;
  46. bool loadShaders();
  47. void createDefaultResources();
  48. void sortRenderQueue();
  49. };
  50. } // namespace Render::GL