Dengine/src/Renderer.cpp

78 lines
2.1 KiB
C++
Raw Normal View History

2016-06-09 05:49:03 +00:00
#include <Dengine/OpenGL.h>
2016-06-16 14:14:58 +00:00
#include <Dengine/Renderer.h>
2016-06-09 05:49:03 +00:00
#include <glm/gtc/matrix_transform.hpp>
2016-06-16 14:14:58 +00:00
#include <glm/gtx/transform.hpp>
2016-06-09 05:49:03 +00:00
namespace Dengine
{
Renderer::Renderer(Shader *shader)
{
this->shader = shader;
this->initRenderData();
}
2016-06-16 14:14:58 +00:00
Renderer::~Renderer() { glDeleteVertexArrays(1, &this->quadVAO); }
void Renderer::drawEntity(Entity *entity, GLfloat rotate, glm::vec3 color)
2016-06-09 05:49:03 +00:00
{
this->shader->use();
2016-06-16 14:14:58 +00:00
glm::mat4 transMatrix = glm::translate(glm::vec3(entity->pos, 0.0f));
glm::mat4 rotateMatrix = glm::rotate(rotate, glm::vec3(0.0f, 0.0f, 1.0f));
2016-06-09 05:49:03 +00:00
// NOTE(doyle): We draw everything as a unit square in OGL. Scale it to size
2016-06-16 14:14:58 +00:00
glm::mat4 scaleMatrix = glm::scale(glm::vec3(entity->size, 1.0f));
2016-06-09 05:49:03 +00:00
glm::mat4 model = transMatrix * rotateMatrix * scaleMatrix;
2016-06-09 05:49:03 +00:00
this->shader->uniformSetMat4fv("model", model);
// TODO(doyle): Unimplemented
// this->shader->uniformSetVec3f("spriteColor", color);
glActiveTexture(GL_TEXTURE0);
2016-06-16 14:14:58 +00:00
entity->tex->bind();
2016-06-09 05:49:03 +00:00
this->shader->uniformSet1i("tex", 0);
glBindVertexArray(this->quadVAO);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glBindVertexArray(0);
}
void Renderer::initRenderData()
{
// NOTE(doyle): Draws a series of triangles (three-sided polygons) using
// vertices v0, v1, v2, then v2, v1, v3 (note the order)
2016-06-09 05:49:03 +00:00
glm::vec4 vertices[] = {
// x y s t
{0.0f, 1.0f, 0.0f, 1.0f}, // Top left
{0.0f, 0.0f, 0.0f, 0.0f}, // Bottom left
{1.0f, 1.0f, 1.0f, 1.0f}, // Top right
{1.0f, 0.0f, 1.0f, 0.0f}, // Bottom right
2016-06-09 05:49:03 +00:00
};
GLuint VBO;
/* Create buffers */
glGenVertexArrays(1, &this->quadVAO);
glGenBuffers(1, &VBO);
/* Bind buffers */
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBindVertexArray(this->quadVAO);
/* Configure VBO */
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
/* Configure VAO */
const GLuint numVertexElements = 4;
2016-06-16 14:14:58 +00:00
const GLuint vertexSize = sizeof(glm::vec4);
2016-06-09 05:49:03 +00:00
glEnableVertexAttribArray(0);
2016-06-16 14:14:58 +00:00
glVertexAttribPointer(0, numVertexElements, GL_FLOAT, GL_FALSE, vertexSize, (GLvoid *)0);
2016-06-09 05:49:03 +00:00
/* Unbind */
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
}