87 lines
1.8 KiB
C++
87 lines
1.8 KiB
C++
|
#include <opengl-playground/textured-rectangle.hpp>
|
||
|
|
||
|
|
||
|
|
||
|
TexturedRectangle::TexturedRectangle(float x0, float y0, float x1, float y1, OpenGlShaderProgram &shaderprog) :
|
||
|
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()
|
||
|
{
|
||
|
float offset[2];
|
||
|
|
||
|
this->shaderprog.use();
|
||
|
offset[0] = this->getOffsetX();
|
||
|
offset[1] = this->getOffsetY();
|
||
|
this->shaderprog.setUniformVec2("pos_offset", offset);
|
||
|
this->shaderprog.setUniformFloat("zoom", this->zoom);
|
||
|
|
||
|
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;
|
||
|
}
|
||
|
|
||
|
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;
|
||
|
}
|
||
|
|
||
|
float TexturedRectangle::getOffsetX()
|
||
|
{
|
||
|
return this->x_offset;
|
||
|
}
|
||
|
|
||
|
float TexturedRectangle::getOffsetY()
|
||
|
{
|
||
|
return this->y_offset;
|
||
|
}
|