Factor out creation of plain tiles to static method in Tile

This commit is contained in:
nusic
2016-07-13 18:26:19 -04:00
parent 5f2d692230
commit f8bcd71bf4
2 changed files with 48 additions and 19 deletions

View File

@@ -54,14 +54,45 @@ namespace {
namespace openspace {
const Tile Tile::TileUnavailable = {nullptr, nullptr, Tile::Status::Unavailable };
Tile Tile::createPlainTile(const glm::uvec2& size, const glm::uvec4& color) {
using namespace ghoul::opengl;
// Create pixel data
int numBytes = size.x * size.y * 4 * 1;
char* pixels = new char[numBytes];
size_t numPixels = size.x * size.y;
size_t i = 0;
for (size_t p = 0; p < numPixels; p++){
pixels[i++] = color.r;
pixels[i++] = color.g;
pixels[i++] = color.b;
pixels[i++] = color.a;
}
// Create ghoul texture
auto texture = std::make_shared<Texture>(glm::uvec3(size, 1));
texture->setDataOwnership(Texture::TakeOwnership::Yes);
texture->setPixelData(pixels);
texture->uploadTexture();
texture->setFilter(ghoul::opengl::Texture::FilterMode::Linear);
// Create tile
Tile tile;
tile.status = Tile::Status::OK;
tile.preprocessData = nullptr;
tile.texture = texture;
return tile;
}
//////////////////////////////////////////////////////////////////////////////////////
// Chunk Index Tile Provider //
//////////////////////////////////////////////////////////////////////////////////////
ChunkIndexTileProvider::ChunkIndexTileProvider(const glm::uvec2 textureSize, size_t fontSize)
ChunkIndexTileProvider::ChunkIndexTileProvider(const glm::uvec2& textureSize, size_t fontSize)
: _tileCache(500)
, _textureSize(textureSize)
, _fontSize(fontSize)
@@ -72,6 +103,7 @@ namespace openspace {
_fontRenderer = std::unique_ptr<FontRenderer>(FontRenderer::createDefault());
_fontRenderer->setFramebufferSize(textureSize);
glGenFramebuffers(1, &_fbo);
}
@@ -83,7 +115,7 @@ namespace openspace {
ChunkHashKey key = chunkIndex.hashKey();
if (!_tileCache.exist(key)) {
_tileCache.put(key, createTile(chunkIndex));
_tileCache.put(key, createChunkIndexTile(chunkIndex));
}
return _tileCache.get(key);
@@ -113,20 +145,9 @@ namespace openspace {
_tileCache.clear();
}
Tile ChunkIndexTileProvider::createTile(const ChunkIndex& chunkIndex) {
Tile tile = Tile();
tile.texture = std::make_shared<Texture>(glm::uvec3(_textureSize, 1));
int numBytes = _textureSize.x * _textureSize.y * 4 * 1;
char* pixels = new char[numBytes];
memset(pixels, 0, numBytes); // set to transparent black
tile.texture->setPixelData(pixels);
tile.status = Tile::Status::OK;
tile.preprocessData = nullptr;
tile.texture->uploadTexture();
tile.texture->setFilter(ghoul::opengl::Texture::FilterMode::Linear);
Tile ChunkIndexTileProvider::createChunkIndexTile(const ChunkIndex& chunkIndex) {
glm::uvec4 color = { 0, 0, 0, 0 };
Tile tile = Tile::createPlainTile(_textureSize, color);
// Keep track of defaultFBO and viewport to be able to reset state when done
GLint defaultFBO;

View File

@@ -62,7 +62,15 @@ namespace openspace {
enum class Status { Unavailable, OutOfRange, IOError, OK } status;
/**
* Instantiaes a new tile unicolored tile. The texture gets the provided size and
* color in rgba. Color values ranges between 0-255.
*/
static Tile createPlainTile(const glm::uvec2& size, const glm::uvec4& color);
static const Tile TileUnavailable;
};
@@ -86,7 +94,7 @@ namespace openspace {
class ChunkIndexTileProvider : public TileProvider {
public:
ChunkIndexTileProvider(const glm::uvec2 textureSize = {512, 512}, size_t fontSize = 48);
ChunkIndexTileProvider(const glm::uvec2& textureSize = {512, 512}, size_t fontSize = 48);
virtual ~ChunkIndexTileProvider();
virtual Tile getTile(const ChunkIndex& chunkIndex);
@@ -97,7 +105,7 @@ namespace openspace {
virtual void reset();
virtual int maxLevel();
private:
Tile createTile(const ChunkIndex& chunkIndex);
Tile createChunkIndexTile(const ChunkIndex& chunkIndex);
std::shared_ptr<ghoul::fontrendering::Font> _font;
std::unique_ptr<ghoul::fontrendering::FontRenderer> _fontRenderer;
@@ -105,7 +113,7 @@ namespace openspace {
TileCache _tileCache;
glm::uvec2 _textureSize;
size_t _fontSize;
GLuint _fbo;
};