12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- #include "renderer.h"
- #include <glad/glad.h>
- #include <glm/gtc/matrix_transform.hpp>
- Renderer::Renderer(Shader shader): m_shader(shader) {
- this->init();
- }
- void Renderer::draw(glm::vec2 position, glm::vec2 size, float rotate, glm::vec3 color) {
- this->m_shader.use();
- glm::mat4 model = glm::mat4(1.0f);
- model = glm::translate(model, glm::vec3(position, 0.0f));
- model = glm::translate(model, glm::vec3(0.5f * size.x, 0.5f * size.y, 0.0f)); // move origin of rotation to center of quad
- model = glm::rotate(model, glm::radians(rotate), glm::vec3(0.0f, 0.0f, 1.0f)); // then rotate
- model = glm::translate(model, glm::vec3(-0.5f * size.x, -0.5f * size.y, 0.0f)); // move origin back
- model = glm::scale(model, glm::vec3(size, 1.0f)); // last scale
- this->m_shader.setMatrix4("model", model);
- // render textured quad
- this->m_shader.setVector3f("spriteColor", color);
- //glActiveTexture(GL_TEXTURE0);
- //texture.Bind();
- glBindVertexArray(this->m_vao);
- glDrawArrays(GL_TRIANGLES, 0, 6);
- glBindVertexArray(0);
- }
- void Renderer::init()
- {
- // configure VAO/VBO
- unsigned int VBO;
- float vertices[] = {
- // pos // tex
- 0.0f, 1.0f, 0.0f, 1.0f,
- 1.0f, 0.0f, 1.0f, 0.0f,
- 0.0f, 0.0f, 0.0f, 0.0f,
- 0.0f, 1.0f, 0.0f, 1.0f,
- 1.0f, 1.0f, 1.0f, 1.0f,
- 1.0f, 0.0f, 1.0f, 0.0f
- };
- glGenVertexArrays(1, &this->m_vao);
- glGenBuffers(1, &VBO);
- glBindBuffer(GL_ARRAY_BUFFER, VBO);
- glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
- glBindVertexArray(this->m_vao);
- glEnableVertexAttribArray(0);
- glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)0);
- glBindBuffer(GL_ARRAY_BUFFER, 0);
- glBindVertexArray(0);
- }
|