79 lines
2.5 KiB
C++
79 lines
2.5 KiB
C++
#pragma once
|
|
|
|
#include "CompressedTexture.h"
|
|
#include "../../core/Serializer.h"
|
|
#include "../../inc/tbb/parallel_for.h"
|
|
#include "../../core/BitHelper.h"
|
|
|
|
template<typename T>
|
|
class BasicTexture : public CompressedTexture<T>
|
|
{
|
|
private:
|
|
std::vector<T> mData;
|
|
public:
|
|
BasicTexture() : mData(std::vector<T>()) {}
|
|
~BasicTexture() {}
|
|
|
|
// Uncompress and retrieve the texture (possibly from a certain index)
|
|
T operator[](size_t i) const override { return mData[i]; }
|
|
unsigned64 size() const override { return mData.size(); }
|
|
|
|
std::vector<T> GetTexture(size_t fromIndex = 0) override {
|
|
std::vector<T> res(size() - fromIndex);
|
|
std::copy(mData.begin() + fromIndex, mData.end(), res.begin());
|
|
return res;
|
|
}
|
|
// Compresses the texture, replacing everything after fromIndex by whatever is in texture
|
|
void SetTexture(const std::vector<T>& texture, size_t fromIndex = 0) override
|
|
{
|
|
mData.resize(fromIndex + texture.size());
|
|
std::copy(texture.begin(), texture.end(), mData.begin() + fromIndex);
|
|
}
|
|
// Replace all materials by other colors. This could be useful as some methods don't need to fully decompress and compress the whole texture to do this.
|
|
void ReplaceMaterials(const std::unordered_map<T, T>& replacers) override
|
|
{
|
|
bool replacersEqual = true;
|
|
for (auto replacer : replacers)
|
|
if (replacer.first != replacer.second)
|
|
{
|
|
replacersEqual = false;
|
|
break;
|
|
}
|
|
|
|
if (!replacersEqual)
|
|
{
|
|
tbb::parallel_for(size_t(0), mData.size(), [&](size_t i)
|
|
{
|
|
auto replacer = replacers.find(mData[i]);
|
|
if (replacer != replacers.end())
|
|
mData[i] = replacer->second;
|
|
});
|
|
}
|
|
}
|
|
void Recompress() override { return; }
|
|
|
|
void ReadFromFile(std::istream& file) override
|
|
{
|
|
Serializer<std::vector<T>, unsigned64>::Deserialize(mData, file);
|
|
}
|
|
virtual void WriteToFile(std::ostream& file) const override
|
|
{
|
|
Serializer<std::vector<T>, unsigned64>::Serialize(mData, file);
|
|
}
|
|
|
|
std::vector<unsigned8> GetTexturePool() const override
|
|
{
|
|
size_t poolSize = GetTexturePoolSize();
|
|
std::vector<unsigned8> pool = std::vector<unsigned8>(poolSize);
|
|
tbb::parallel_for(size_t(0), mData.size(), [&](size_t i)
|
|
{
|
|
// Convert all values to 32 bit integers and split them into bytes
|
|
unsigned32 value = (unsigned32)mData[i];
|
|
BitHelper::SplitInBytesAndMove(value, pool, i * 4);
|
|
});
|
|
return pool;
|
|
}
|
|
size_t GetTexturePoolSize() const override { return mData.size() * 4; }
|
|
|
|
virtual std::map<std::string, std::string> GetAdditionalProperties() const override { return std::map<std::string, std::string>(); }
|
|
}; |