Files
TheSubterranean/Object.cpp
2021-07-07 22:09:21 +02:00

53 lines
2.1 KiB
C++

#include "Object.h"
Object::Object(Texture sprite, glm::vec2 pos, glm::vec2 size, int blockType)
:Sprite(sprite),Position(pos),Size(size), blockType(blockType)
{
this->spriteUVs = this->spriteSheet->getUVs(this->blockType + 10);
//if (this->blockType) {
this->objectBody.min = c2V(this->Position.x, this->Position.y);
this->objectBody.max = c2V(this->Position.x + this->Size.x, this->Position.y + this->Size.y);
//}
GLfloat newVertices[] = {
// Pos // Tex
0.0f*this->Size.x + this->Position.x, 1.0f*this->Size.y + this->Position.y, this->spriteUVs.x, this->spriteUVs.y,
1.0f*this->Size.x + this->Position.x, 0.0f*this->Size.y + this->Position.y, this->spriteUVs.z, this->spriteUVs.w,
0.0f*this->Size.x + this->Position.x, 0.0f*this->Size.y + this->Position.y, this->spriteUVs.x, this->spriteUVs.w,
1.0f*this->Size.x + this->Position.x, 1.0f*this->Size.y + this->Position.y, this->spriteUVs.z, this->spriteUVs.y
};
for (int i = 0; i < 16; i++) {
this->vertices[i] = newVertices[i];
}
// Configure VAO/VBO
unsigned int indices[] = {
0, 1, 2,
0, 1, 3
};
glGenVertexArrays(1, &this->quadVAO);
glGenBuffers(1, &this->VBO);
glGenBuffers(1, &this->EBO);
glBindBuffer(GL_ARRAY_BUFFER, this->VBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
glBindVertexArray(this->quadVAO);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), (GLvoid*)0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
glActiveTexture(GL_TEXTURE0);
glBindBuffer(GL_ARRAY_BUFFER, this->VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(this->vertices), this->vertices, GL_STATIC_DRAW);
}
void Object::drawObject(SpriteRenderer& renderer) {
//renderer.DrawSprite(this->Sprite, this->Position, this->spriteUVs, this->vertices, this->Size, this->Rotation);
this->Sprite.bind();
glBindVertexArray(this->quadVAO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->EBO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
}