opengl_renderer.cxx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //
  2. // opengl_renderer.cxx
  3. // graphics
  4. //
  5. // Created by Sam Jaffe on 5/20/19.
  6. // Copyright © 2019 Sam Jaffe. All rights reserved.
  7. //
  8. #include "renderer_impl.hpp"
  9. #ifdef __APPLE__
  10. #include <OpenGL/gl3.h>
  11. #endif
  12. #include "game/graphics/material.hpp"
  13. #include "game/graphics/vertex.h"
  14. #include "matrix/matrix.hpp"
  15. #include "matrix/matrix_helpers.hpp"
  16. using namespace graphics;
  17. class opengl_renderer : public renderer_impl {
  18. public:
  19. opengl_renderer();
  20. ~opengl_renderer();
  21. void draw(flyweight<material>, math::matr4 const &,
  22. std::vector<vertex> const &) override;
  23. void clear() override;
  24. void flush() override;
  25. private:
  26. const math::matr4 identity{math::matrix::identity<float, 4>()};
  27. math::matr4 world_to_clip{identity};
  28. double current_time{0.0};
  29. unsigned int vertex_array_object{0}, vertex_buffer_object{0};
  30. };
  31. opengl_renderer::opengl_renderer() {
  32. glGenVertexArrays(1, &vertex_array_object);
  33. glBindVertexArray(vertex_array_object);
  34. glGenBuffers(1, &vertex_buffer_object);
  35. glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer_object);
  36. }
  37. opengl_renderer::~opengl_renderer() {
  38. glDeleteBuffers(1, &vertex_buffer_object);
  39. glDeleteVertexArrays(1, &vertex_array_object);
  40. }
  41. void opengl_renderer::clear() {
  42. world_to_clip = identity;
  43. // vec2i resolution = env::resolution();
  44. // orthogonal_view(0.0, resolution[0], 0.0, resolution[1], -1.0, 1.0);
  45. glClearDepth(1.f);
  46. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  47. glClearColor(0.f, 0.f, 0.0f, 1.f);
  48. glEnable(GL_DEPTH_TEST);
  49. // TODO: Use a unified time calculation
  50. current_time = time(NULL);
  51. // TODO: ???
  52. // scale(0.5, 0.5, 1); // Don't know why I need to do this
  53. }
  54. void opengl_renderer::flush() { glFlush(); }
  55. void opengl_renderer::draw(flyweight<material> material,
  56. math::matr4 const & object_to_world,
  57. std::vector<vertex> const & vertices) {
  58. // TODO: Don't activate here?
  59. auto const & mat = material.actual();
  60. // TODO: Attatch shader-id to material-id
  61. unsigned int const id = mat.shaders.id;
  62. mat.activate();
  63. int objectLocation = glGetUniformLocation(id, "u_objectToWorldMatrix");
  64. glUniformMatrix4fv(objectLocation, 1, false, &object_to_world(0, 0));
  65. int clipLocation = glGetUniformLocation(id, "u_worldToClipMatrix");
  66. glUniformMatrix4fv(clipLocation, 1, false, &world_to_clip(0, 0));
  67. int timeLocation = glGetUniformLocation(id, "u_time");
  68. glUniform1d(timeLocation, current_time);
  69. // TODO: Cache attribute locations
  70. int positionLocation = glGetAttribLocation(id, "a_position");
  71. int colorLocation = glGetAttribLocation(id, "a_color");
  72. int texCoordsLocation = glGetAttribLocation(id, "a_texCoords");
  73. glEnableVertexAttribArray(positionLocation);
  74. glEnableVertexAttribArray(colorLocation);
  75. glEnableVertexAttribArray(texCoordsLocation);
  76. glVertexAttribPointer(positionLocation, 2, GL_FLOAT, GL_FALSE, sizeof(vertex),
  77. &vertices[0].position);
  78. glVertexAttribPointer(colorLocation, 4, GL_UNSIGNED_BYTE, GL_TRUE,
  79. sizeof(vertex), &vertices[0].color);
  80. glVertexAttribPointer(texCoordsLocation, 2, GL_FLOAT, GL_FALSE,
  81. sizeof(vertex), &vertices[0].texture_coords);
  82. glDrawArrays(GL_TRIANGLES, 0, static_cast<GLsizei>(vertices.size()));
  83. glDisableVertexAttribArray(positionLocation);
  84. glDisableVertexAttribArray(colorLocation);
  85. glDisableVertexAttribArray(texCoordsLocation);
  86. }
  87. template <> renderer_impl * graphics::get_renderer_impl<driver::openGL>() {
  88. static opengl_renderer impl;
  89. return &impl;
  90. }