renderer.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #include "renderer.h"
  2. #include <glad/glad.h>
  3. #include <glm/gtc/matrix_transform.hpp>
  4. Renderer::Renderer(Shader shader): m_shader(shader) {
  5. this->init();
  6. }
  7. void Renderer::draw(glm::vec2 position, glm::vec2 size, float rotate, glm::vec3 color) {
  8. this->m_shader.use();
  9. glm::mat4 model = glm::mat4(1.0f);
  10. model = glm::translate(model, glm::vec3(position, 0.0f));
  11. model = glm::translate(model, glm::vec3(0.5f * size.x, 0.5f * size.y, 0.0f)); // move origin of rotation to center of quad
  12. model = glm::rotate(model, glm::radians(rotate), glm::vec3(0.0f, 0.0f, 1.0f)); // then rotate
  13. model = glm::translate(model, glm::vec3(-0.5f * size.x, -0.5f * size.y, 0.0f)); // move origin back
  14. model = glm::scale(model, glm::vec3(size, 1.0f)); // last scale
  15. this->m_shader.setMatrix4("model", model);
  16. // render textured quad
  17. this->m_shader.setVector3f("spriteColor", color);
  18. //glActiveTexture(GL_TEXTURE0);
  19. //texture.Bind();
  20. glBindVertexArray(this->m_vao);
  21. glDrawArrays(GL_TRIANGLES, 0, 6);
  22. glBindVertexArray(0);
  23. }
  24. void Renderer::init()
  25. {
  26. // configure VAO/VBO
  27. unsigned int VBO;
  28. float vertices[] = {
  29. // pos // tex
  30. 0.0f, 1.0f, 0.0f, 1.0f,
  31. 1.0f, 0.0f, 1.0f, 0.0f,
  32. 0.0f, 0.0f, 0.0f, 0.0f,
  33. 0.0f, 1.0f, 0.0f, 1.0f,
  34. 1.0f, 1.0f, 1.0f, 1.0f,
  35. 1.0f, 0.0f, 1.0f, 0.0f
  36. };
  37. glGenVertexArrays(1, &this->m_vao);
  38. glGenBuffers(1, &VBO);
  39. glBindBuffer(GL_ARRAY_BUFFER, VBO);
  40. glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
  41. glBindVertexArray(this->m_vao);
  42. glEnableVertexAttribArray(0);
  43. glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)0);
  44. glBindBuffer(GL_ARRAY_BUFFER, 0);
  45. glBindVertexArray(0);
  46. }