2020-03-18 22:42:04 +01:00
|
|
|
#include <opengl-playground/textured-rectangle.hpp>
|
2020-03-21 22:38:10 +01:00
|
|
|
#include <glm/glm.hpp>
|
|
|
|
#include <glm/gtc/matrix_transform.hpp>
|
2020-03-18 22:42:04 +01:00
|
|
|
|
2020-03-21 22:38:10 +01:00
|
|
|
TexturedRectangle::TexturedRectangle(float x0, float y0, float x1, float y1, std::shared_ptr<OpenGlShaderProgram> shaderprog) :
|
2020-03-18 22:42:04 +01:00
|
|
|
OpenGlGraphics(shaderprog)
|
|
|
|
{
|
|
|
|
this->pos1[0] = x0;
|
|
|
|
this->pos1[1] = y0;
|
|
|
|
this->pos2[0] = x1;
|
|
|
|
this->pos2[1] = y1;
|
|
|
|
this->setZoom(1.0f);
|
|
|
|
this->setOffset(0.0f, 0.0f);
|
|
|
|
this->vao = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TexturedRectangle::realize()
|
|
|
|
{
|
|
|
|
GLuint vbo;
|
|
|
|
GLuint elem_buff;
|
|
|
|
const unsigned int indices[] = {
|
|
|
|
0, 1, 2,
|
|
|
|
2, 0, 3
|
|
|
|
};
|
|
|
|
|
|
|
|
float vertices[8] = {
|
|
|
|
this->pos1[0], this->pos1[1],
|
|
|
|
this->pos1[0], this->pos2[1],
|
|
|
|
this->pos2[0], this->pos2[1],
|
|
|
|
this->pos2[0], this->pos1[1],
|
|
|
|
};
|
|
|
|
|
|
|
|
glGenVertexArrays(1, &this->vao);
|
|
|
|
glBindVertexArray(this->vao);
|
|
|
|
glGenBuffers(1, &vbo);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
|
|
|
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
|
|
|
glGenBuffers(1, &elem_buff);
|
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elem_buff);
|
|
|
|
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
|
|
|
|
|
|
|
|
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*)0);
|
|
|
|
glEnableVertexAttribArray(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TexturedRectangle::render()
|
|
|
|
{
|
2020-03-21 22:38:10 +01:00
|
|
|
this->shaderprog->use();
|
2020-03-18 22:42:04 +01:00
|
|
|
|
2020-03-21 22:38:10 +01:00
|
|
|
this->shaderprog->setUniformMat4("model_matrix", this->model_matrix);
|
2020-03-18 22:42:04 +01:00
|
|
|
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
|
|
|
glBindVertexArray(this->vao);
|
|
|
|
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void TexturedRectangle::setZoom(float zoom)
|
|
|
|
{
|
|
|
|
this->zoom = zoom;
|
2020-03-21 22:38:10 +01:00
|
|
|
this->calculateModelMatrix();
|
2020-03-18 22:42:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
float TexturedRectangle::getZoom()
|
|
|
|
{
|
|
|
|
return this->zoom;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TexturedRectangle::setOffset(float x_off, float y_off)
|
|
|
|
{
|
|
|
|
this->x_offset = x_off;
|
|
|
|
this->y_offset = y_off;
|
2020-03-21 22:38:10 +01:00
|
|
|
this->calculateModelMatrix();
|
2020-03-18 22:42:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
float TexturedRectangle::getOffsetX()
|
|
|
|
{
|
|
|
|
return this->x_offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
float TexturedRectangle::getOffsetY()
|
|
|
|
{
|
|
|
|
return this->y_offset;
|
|
|
|
}
|
2020-03-21 22:38:10 +01:00
|
|
|
|
|
|
|
void TexturedRectangle::calculateModelMatrix()
|
|
|
|
{
|
|
|
|
glm::mat4 moved_matrix = glm::translate(glm::mat4(1.0f), glm::vec3(-this->x_offset, -this->y_offset, 0.0f));
|
|
|
|
this->model_matrix = glm::scale(moved_matrix, glm::vec3(this->zoom, this->zoom, 0.0f));
|
|
|
|
}
|