opengl_renderer.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. //
  2. // opengl_renderer.h
  3. // graphics
  4. //
  5. // Created by Sam Jaffe on 6/2/19.
  6. // Copyright © 2019 Sam Jaffe. All rights reserved.
  7. //
  8. #pragma once
  9. #include "../helper.hpp"
  10. #include "../renderer_impl.hpp"
  11. #include "game/graphics/manager.hpp"
  12. #include "game/util/identity.hpp"
  13. #include "matrix/matrix.hpp"
  14. #include "matrix/matrix_helpers.hpp"
  15. namespace graphics {
  16. struct opengl_uniform_data {
  17. unsigned int operator[](materials::uniform id) { return uniform_id[id]; }
  18. std::unordered_map<materials::uniform, unsigned int> uniform_id;
  19. };
  20. class opengl_manager : public manager {
  21. public:
  22. shader compile(shaders::type type, std::string const & path) const override;
  23. shader_program compile(identity<shader> fragment,
  24. identity<shader> vertex) const override;
  25. texture compile(textures::format color, math::vec2i size,
  26. void const * buffer) const override;
  27. opengl_uniform_data & data(identity<shader_program> id);
  28. private:
  29. std::unordered_map<identity<shader_program>, opengl_uniform_data> data_;
  30. };
  31. class opengl_renderer : public renderer_impl {
  32. public:
  33. opengl_renderer();
  34. ~opengl_renderer();
  35. void draw(identity<material>, math::matr4 const &,
  36. std::vector<vertex> const &) override;
  37. void clear() override;
  38. void flush() override;
  39. std::shared_ptr<class manager const> manager() const override {
  40. return manager_;
  41. }
  42. private:
  43. void activate(material const & mat);
  44. private:
  45. const math::matr4 identity{math::matrix::identity<float, 4>()};
  46. std::shared_ptr<opengl_manager> manager_;
  47. unsigned int active_material;
  48. math::matr4 world_to_clip{identity};
  49. double current_time{0.0};
  50. unsigned int vertex_array_object{0}, vertex_buffer_object{0};
  51. };
  52. }