41 auto textureBytesPerPixel = 3;
47 auto offset = y *
textureWidth * textureBytesPerPixel + x * textureBytesPerPixel;
53 rgbTextureData.put(red);
54 rgbTextureData.put(green);
55 rgbTextureData.put(blue);
56 if (rgbTextureDataBytesPerPixel == 4) rgbTextureData.put(0xff);
60 return rgbTextureData;
65 auto textureBytesPerPixel = 4;
71 auto offset = y *
textureWidth * textureBytesPerPixel + x * textureBytesPerPixel;
77 rgbTextureData.put(red);
78 rgbTextureData.put(green);
79 rgbTextureData.put(blue);
80 if (rgbTextureDataBytesPerPixel == 4) rgbTextureData.put(alpha);
84 return rgbTextureData;
96 return rgbTextureData;
108 return rgbTextureData;
120 return rgbTextureData;
132 return rgbTextureData;
143 if (
format == bc7Format) {
153 vector<uint8_t> bc7Data;
161 if (this->format ==
format) {
164 switch(this->format) {
175 auto offset = y *
textureWidth * rgbTextureDataBytesPerPixel + x * rgbTextureDataBytesPerPixel;
176 auto red = rgbTextureData.get(offset + 0);
177 auto green = rgbTextureData.get(offset + 1);
178 auto blue = rgbTextureData.get(offset + 2);
179 auto alpha = rgbTextureDataBytesPerPixel == 4?rgbTextureData.get(offset + 3):0xff;
181 this->textureData.
put(red);
182 this->textureData.
put(green);
183 this->textureData.
put(blue);
198 auto offset = y *
textureWidth * rgbTextureDataBytesPerPixel + x * rgbTextureDataBytesPerPixel;
199 auto red = rgbTextureData.get(offset + 0);
200 auto green = rgbTextureData.get(offset + 1);
201 auto blue = rgbTextureData.get(offset + 2);
202 auto alpha = rgbTextureDataBytesPerPixel == 4?rgbTextureData.get(offset + 3):0xff;
204 this->textureData.
put(red);
205 this->textureData.
put(green);
206 this->textureData.
put(blue);
207 this->textureData.
put(alpha);
217 vector<uint8_t> pngData;
229 vector<uint8_t> pngData;
243 auto generatedTextureByteBuffer =
ByteBuffer(generatedTextureWidth * generatedTextureHeight * bytesPerPixel);
246 auto materialTextureBytesPerPixel = bytesPerPixel;
247 for (
auto y = 0; y < generatedTextureHeight; y++)
248 for (
auto x = 0; x < generatedTextureWidth; x++) {
249 auto atlasTextureIdxX = (x * 2) / materialTextureWidth;
250 auto atlasTextureIdxY = (y * 2) / materialTextureHeight;
251 auto materialTextureX = (x * 2) - (atlasTextureIdxX * materialTextureWidth);
252 auto materialTextureY = (y * 2) - (atlasTextureIdxY * materialTextureHeight);
253 auto materialTextureXFloat =
static_cast<float>(materialTextureX) /
static_cast<float>(materialTextureWidth);
254 auto materialTextureYFloat =
static_cast<float>(materialTextureY) /
static_cast<float>(materialTextureHeight);
256 auto materialSamples = 0;
257 auto materialTextureXInt =
static_cast<int>(materialTextureXFloat *
static_cast<float>(materialTextureWidth));
258 auto materialTextureYInt =
static_cast<int>(materialTextureYFloat *
static_cast<float>(materialTextureHeight));
259 auto materialPixelR = 0;
260 auto materialPixelG = 0;
261 auto materialPixelB = 0;
262 auto materialPixelA = 0;
263 for (
auto y = -1; y <= 1; y++)
264 for (
auto x = -1; x <= 1; x++)
265 if ((Math::abs(x) == 1 && Math::abs(y) == 1) ==
false &&
266 materialTextureXInt + x >= 0 && materialTextureXInt + x < materialTextureWidth &&
267 materialTextureYInt + y >= 0 && materialTextureYInt + y < materialTextureHeight) {
268 auto materialTexturePixelOffset =
269 (atlasTextureIdxY * materialTextureHeight + materialTextureYInt + y) *
textureWidth * materialTextureBytesPerPixel +
270 (atlasTextureIdxX * materialTextureWidth + materialTextureXInt + x) * materialTextureBytesPerPixel;
271 materialPixelR+= textureTextureData.
get(materialTexturePixelOffset + 0);
272 materialPixelG+= textureTextureData.
get(materialTexturePixelOffset + 1);
273 materialPixelB+= textureTextureData.
get(materialTexturePixelOffset + 2);
274 materialPixelA+= materialTextureBytesPerPixel == 4?textureTextureData.
get(materialTexturePixelOffset + 3):0xff;
277 generatedTextureByteBuffer.put(materialPixelR / materialSamples);
278 generatedTextureByteBuffer.put(materialPixelG / materialSamples);
279 generatedTextureByteBuffer.put(materialPixelB / materialSamples);
280 if (bytesPerPixel == 4) generatedTextureByteBuffer.put(materialPixelA / materialSamples);
283 return generatedTextureByteBuffer;
292 if (mipMapFormatBC7 ==
true) {
307 for (
auto i = 1; i < mipLevels; i++) {
308 textureTextureData =
generateMipMap(mipMapTextureWidth, mipMapTextureHeight, textureBytePerPixel, textureTextureData);
310 mipMapTextureWidth/= 2;
311 mipMapTextureHeight/= 2;
313 if (bc7Encoded ==
true) {
314 vector<uint8_t> bc7Data;
315 BC7TextureWriter::write(mipMapTextureWidth, mipMapTextureHeight, textureBytePerPixel, textureTextureData, bc7Data);
337 TextureReader::removeFromCache(
this);
static TextureFormat getBC7FormatByPixelBitsPerPixel(int bpp)
Return BC7 RGB/A texture format by bits per pixel.
const vector< MipMapTexture > & getMipMapTextures(bool bc7Encoded)
Get mip map textures.
static TextureFormat getRGBFormatByPixelBitsPerPixel(int bpp)
Return RGB/A texture format by bits per pixel.
ByteBuffer generateMipMap(int textureWidth, int textureHeight, int bytesPerPixel, const ByteBuffer &textureTextureData)
Generate mip map texture.
ByteBuffer getRGBTextureData()
uint8_t getRGBDepthBitsPerPixel() const
virtual void onDelete() override
Callback method to be overridden, will be called if object will be deleted.
ByteBuffer getBC7TextureData()
vector< MipMapTexture > mipMapTextures
void setTextureData(TextureFormat format, const ByteBuffer &textureData)
Set RGB(A) texture data.
BC7 texture reader class.
BC7 texture writer class.
PNG texture reader class.
PNG texture writer class.
uint8_t get(int64_t position) const
const vector< uint8_t > * getBufferVector() const
Buffer * put(uint8_t value)
Put value into buffer.