Model.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. * Copyright (C) 2018 Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
  3. * Author: Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "Model.hpp"
  19. #include <GL/glew.h>
  20. #include <GL/gl.h>
  21. Model::Model(const std::vector<struct Vertex> &vertices,
  22. const std::vector<unsigned int> &indices) :
  23. vertices(vertices), indices(indices)
  24. {
  25. glGenVertexArrays(1, &vao);
  26. glGenBuffers(1, &vbo);
  27. glGenBuffers(1, &ebo);
  28. glBindVertexArray(vao);
  29. glBindBuffer(GL_ARRAY_BUFFER, vbo);
  30. glBufferData(GL_ARRAY_BUFFER,
  31. vertices.size() * sizeof(struct Vertex),
  32. &vertices[0], GL_STATIC_DRAW);
  33. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
  34. glBufferData(GL_ELEMENT_ARRAY_BUFFER,
  35. indices.size() * sizeof(unsigned int),
  36. &indices[0], GL_STATIC_DRAW);
  37. glEnableVertexAttribArray(0);
  38. glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE,
  39. sizeof(struct Vertex), nullptr);
  40. glEnableVertexAttribArray(1);
  41. glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE,
  42. sizeof(struct Vertex),
  43. (void*)offsetof(struct Vertex, normal));
  44. glBindVertexArray(0);
  45. }
  46. void Model::draw(const Shader &shader) {
  47. glUniform3f(
  48. glGetUniformLocation(shader.getId(), "col"),
  49. color.r, color.g, color.b);
  50. glBindVertexArray(vao);
  51. glDrawElements(GL_TRIANGLES, indices.size(),
  52. GL_UNSIGNED_INT, 0);
  53. glBindVertexArray(0);
  54. }