vulkan-project/shape.cpp
2022-10-14 20:58:20 +02:00

59 lines
2.0 KiB
C++

#include "shape.hpp"
#include "texture_manager.hpp"
namespace gz::vk {
Rectangle::Rectangle(float top, float left, uint32_t width, uint32_t height, glm::vec3 color, std::string&& texture)
: top(top), left(left), width(width), height(height), color(color)
{
this->texture = std::move(texture);
generateVertices();
}
void Rectangle::generateVertices() {
vertices.clear();
vertices.emplace_back(Vertex2D{glm::vec2(top, left), color, glm::vec2(0, 0)});
vertices.emplace_back(Vertex2D{glm::vec2(top, left + width), color, glm::vec2(0, 1)});
vertices.emplace_back(Vertex2D{glm::vec2(top + height, left + width), color, glm::vec2(1, 1)});
vertices.emplace_back(Vertex2D{glm::vec2(top + height, left), color, glm::vec2(1, 0)});
indices = { 0, 1, 2, 2, 3, 0 };
/* indices = { 2, 1, 0, 2, 3, 0 }; */
}
void Shape::setIndexOffset(uint32_t offset) {
for (size_t i = 0; i < indices.size(); i++) {
indices[i] += offset;
}
}
void Shape::normalizeVertices(float width, float height) {
for (size_t i = 0; i < vertices.size(); i++) {
vertices[i].pos.x /= width;
vertices[i].pos.y /= height;
}
}
void Rectangle::setTextureCoordinates(glm::vec2 topLeft, glm::vec2 bottomRight) {
vertices[0].texCoord = topLeft;
vertices[1].texCoord.x = bottomRight.x;
vertices[1].texCoord.y = topLeft.y;
vertices[2].texCoord = bottomRight;
vertices[3].texCoord.x = topLeft.x;
vertices[3].texCoord.y = bottomRight.y;
}
void Rectangle::setTextureCoordinates(TextureManager& textureManager) {
if (texture != "atlas") {
textureManager.getTexCoords(texture, vertices[0].texCoord);
textureManager.getTexCoords(texture, vertices[1].texCoord);
textureManager.getTexCoords(texture, vertices[2].texCoord);
textureManager.getTexCoords(texture, vertices[3].texCoord);
}
}
} // namespace gz::vk