123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- /*!
- * \file src/Sprite.cpp
- * \brief 2D Billboard Collection
- *
- * \author xythobuz
- */
-
- #include "global.h"
- #include "Camera.h"
- #include "Render.h"
- #include "TextureManager.h"
- #include "World.h"
- #include "Sprite.h"
-
- const static float scale = 4.0f;
- const static float texelScale = 256.0f;
- const static int texelOffset = 2;
-
- Sprite::Sprite(int tile, int x, int y, int width, int height) : texture(tile) {
- width >>= 8;
- height >>= 8;
-
- int width2 = static_cast<int>(width * scale);
- int height2 = static_cast<int>(height * scale);
-
- uvBuff.emplace_back(float(x + texelOffset) / texelScale, float(y + height) / texelScale);
- uvBuff.emplace_back(float(x + texelOffset) / texelScale, float(y + texelOffset) / texelScale);
- uvBuff.emplace_back(float(x + width) / texelScale, float(y + texelOffset) / texelScale);
- uvBuff.emplace_back(float(x + width) / texelScale, float(y + height) / texelScale);
- uvBuff.emplace_back(uvBuff.at(0));
- uvBuff.emplace_back(uvBuff.at(2));
-
- vertexBuff.emplace_back(float(-width2) / 2.0f, 0.0f, 0.0f);
- vertexBuff.emplace_back(float(-width2) / 2.0f, float(-height2), 0.0f);
- vertexBuff.emplace_back(float(width2) / 2.0f, float(-height2), 0.0f);
- vertexBuff.emplace_back(float(width2) / 2.0f, 0.0f, 0.0f);
- vertexBuff.emplace_back(vertexBuff.at(0));
- vertexBuff.emplace_back(vertexBuff.at(2));
-
- uv2D = glm::vec4(uvBuff.at(0), uvBuff.at(2));
-
- glm::vec3 average(0.0f, 0.0f, 0.0f);
- int averageCount = 0;
- for (auto& vert : vertexBuff) {
- average += vert;
- averageCount++;
- }
- glm::vec3 center = average / float(averageCount);
- float radius = 0.0f;
- for (auto& vert : vertexBuff) {
- float dist = glm::distance(center, vert);
- if (dist > radius) radius = dist;
- }
- boundingSphere.setPosition(center);
- boundingSphere.setRadius(radius);
- }
-
- void Sprite::display(glm::mat4 MVP) {
- Shader::drawGL(vertexBuff, uvBuff, MVP, texture, TextureStorage::GAME);
- }
-
- // ----------------------------------------------------------------------------
-
- void SpriteSequence::display(glm::mat4 MVP, int index) {
- orAssertGreaterThanEqual(index, 0);
- orAssertLessThan(index, length);
- World::getSprite(start + index).display(MVP);
- }
|