TDME2  1.9.200
Texture.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <string>
4 #include <vector>
5 
6 #include <tdme/tdme.h>
7 #include <tdme/engine/fwd-tdme.h>
8 #include <tdme/math/Math.h>
11 
12 using std::string;
13 using std::vector;
14 
15 using tdme::math::Math;
18 
19 /**
20  * Texture entity
21  * @author Andreas Drewke
22  */
23 class tdme::engine::Texture final: public Reference
24 {
25 public:
26 
27  enum TextureDepth {
31  };
32 
41  };
42 
50  };
51 
53 
54  /**
55  * Return RGB/A texture depth by bits per pixel
56  * @param bpp bits per pixel
57  * @return RGB/A texture depth by bits per pixel
58  */
60  switch (bpp) {
61  case 24: return TEXTUREDEPTH_RGB;
62  case 32: return TEXTUREDEPTH_RGBA;
63  default: return TEXTUREDEPTH_UNKNOWN;
64  }
65  }
66 
67  /**
68  * Return RGB/A texture format by bits per pixel
69  * @param bpp bits per pixel
70  * @return RGB/A texture format by bits per pixel
71  */
73  switch (bpp) {
74  case 24: return TEXTUREFORMAT_RGB;
75  case 32: return TEXTUREFORMAT_RGBA;
76  default: return TEXTUREFORMAT_UNKNOWN;
77  }
78  }
79 
80  /**
81  * Return PNG RGB/A texture format by bits per pixel
82  * @param bpp bits per pixel
83  * @return PNG RGB/A texture format by bits per pixel
84  */
86  switch (bpp) {
87  case 24: return TEXTUREFORMAT_RGB_PNG;
88  case 32: return TEXTUREFORMAT_RGBA_PNG;
89  default: return TEXTUREFORMAT_UNKNOWN;
90  }
91  }
92 
93  /**
94  * Return BC7 RGB/A texture format by bits per pixel
95  * @param bpp bits per pixel
96  * @return BC7 RGB/A texture format by bits per pixel
97  */
99  switch (bpp) {
100  case 24: return TEXTUREFORMAT_RGB_BC7;
101  case 32: return TEXTUREFORMAT_RGBA_BC7;
102  default: return TEXTUREFORMAT_UNKNOWN;
103  }
104  }
105 
106  /**
107  * Mip Map Texture
108  */
109  struct MipMapTexture {
112  uint16_t width,
113  uint16_t height,
115  :
116  format(format),
117  width(width),
118  height(height),
120  {}
122  uint16_t width;
123  uint16_t height;
125  };
126 
127  // forbid class copy
129 
130  /**
131  * Public constructor
132  * @param id id
133  * @param depth depth
134  * @param format texture backing format
135  * @param width width
136  * @param height height
137  * @param textureWidth texture width
138  * @param textureHeight texture height
139  * @param textureDataFormat texture data format
140  * @param textureData texture data
141  */
142  inline Texture(
143  const string& id,
146  uint16_t width, uint16_t height,
147  uint16_t textureWidth, uint16_t textureHeight,
148  TextureFormat textureDataFormat,
149  const ByteBuffer& textureData):
150  Reference(),
151  id(id),
152  depth(depth),
153  format(format),
154  width(width),
155  height(height),
158  useCompression(true),
159  useMipMap(true),
160  repeat(true),
164  atlasSize(1) {
165  //
166  setTextureData(textureDataFormat, textureData);
167  }
168 
169  /**
170  * @return id
171  */
172  inline const string& getId() const {
173  return id;
174  }
175 
176  /**
177  * @return depth
178  */
180  return depth;
181  }
182 
183  /**
184  * @return RGB depth in bits per pixel
185  */
186  inline uint8_t getRGBDepthBitsPerPixel() const {
187  switch (depth) {
188  case TEXTUREDEPTH_RGB: return 24;
189  case TEXTUREDEPTH_RGBA: return 32;
190  }
191  return 0;
192  }
193 
194  /**
195  * @return image width
196  */
197  inline uint16_t getWidth() const {
198  return width;
199  }
200 
201  /**
202  * @return image height
203  */
204  inline uint16_t getHeight() const {
205  return height;
206  }
207 
208  /**
209  * @return texture height
210  */
211  inline uint16_t getTextureHeight() const {
212  return textureHeight;
213  }
214 
215  /**
216  * @return texture width
217  */
218  inline uint16_t getTextureWidth() const {
219  return textureWidth;
220  }
221 
222  /**
223  * @return texture format
224  */
226  return format;
227  }
228 
229  /**
230  * @return texture has RGB/RGBA raw texture format
231  */
232  inline bool isRGBTextureFormat() const {
234  }
235 
236  /**
237  * @return texture has PNG texture format
238  */
239  inline bool isPNGTextureFormat() const {
241  }
242 
243  /**
244  * @return texture has BC7 texture format
245  */
246  inline bool isBC7TextureFormat() const {
248  }
249 
250  /**
251  * @return RGB/RGBA texture data wrapped in a byte buffer
252  */
255  }
256 
257  /**
258  * @return BC7 texture data wrapped in a byte buffer
259  */
261 
262  /**
263  * Set RGB(A) texture data
264  * @param format texture data format
265  * @param textureData texture data
266  */
268 
269  /**
270  * @return backing texture data wrapped in a byte buffer
271  */
273  return &textureData;
274  }
275 
276  /**
277  * @return is use compression
278  */
279  inline bool isUseCompression() const {
280  return useCompression;
281  }
282 
283  /**
284  * Set if to use compression
285  * @param useCompression use compression if available
286  */
287  inline void setUseCompression(bool useCompression) {
288  this->useCompression = useCompression;
289  }
290 
291  /**
292  * @return use mip map
293  */
294  inline bool isUseMipMap() const {
295  return useMipMap;
296  }
297 
298  /**
299  * Set if to use mip map
300  * @param useMipMap mip map enabled
301  */
302  inline void setUseMipMap(bool useMipMap) {
303  this->useMipMap = useMipMap;
304  }
305 
306  /**
307  * @return is repeat
308  */
309  inline bool isRepeat() const {
310  return repeat;
311  }
312 
313  /**
314  * Set repeat
315  * @param repeat repeat
316  */
317  inline void setRepeat(bool repeat) {
318  this->repeat = repeat;
319  }
320 
321  /**
322  * @return clamp mode
323  */
324  inline ClampMode getClampMode() const {
325  return clampMode;
326  }
327 
328  /**
329  * Set clamp mode
330  * @param clampMode clamp mode
331  */
333  this->clampMode = clampMode;
334  }
335 
336  /**
337  * @return texture min filter
338  */
339  inline TextureFilter getMinFilter() const {
340  return minFilter;
341  }
342 
343  /**
344  * Set texture min filter
345  * @param filter filter
346  */
347  inline void setMinFilter(TextureFilter filter) {
348  this->minFilter = filter;
349  }
350 
351  /**
352  * @return texture mag filter
353  */
354  inline TextureFilter getMagFilter() const {
355  return magFilter;
356  }
357 
358  /**
359  * Set texture mag filter
360  * @param filter filter
361  */
362  inline void setMagFilter(TextureFilter filter) {
363  this->magFilter = filter;
364  }
365 
366  /**
367  * @return atlas size
368  */
369  inline uint16_t getAtlasSize() const {
370  return atlasSize;
371  }
372 
373  /**
374  * Set atlas size
375  * @param atlasSize atlas size
376  */
377  inline void setAtlasSize(uint16_t atlasSize) {
378  this->atlasSize = atlasSize;
379  }
380 
381  /**
382  * @return mip levels
383  */
384  inline int getMipLevels() {
385  if (useMipMap == false) return 1;
386  auto widthMipLevels = 1;
387  auto heightMipLevels = 1;
388  auto textureWidth = this->textureWidth;
389  auto textureHeight = this->textureHeight;
390  while (textureWidth > 16) {
391  textureWidth/= 2;
392  widthMipLevels++;
393  }
394  while (textureHeight > 16) {
395  textureHeight/= 2;
396  heightMipLevels++;
397  }
398  auto mipLevels = Math::min(widthMipLevels, heightMipLevels);
399  if (atlasSize > 1) {
400  auto borderSize = 32;
401  auto maxLevel = 0;
402  while (borderSize > 4) {
403  maxLevel++;
404  borderSize/= 2;
405  }
406  return Math::min(mipLevels, maxLevel);
407  }
408  //
409  return mipLevels;
410  }
411 
412  /**
413  * Set mip map textures
414  * @param mipMapTextures mip map textures
415  */
416  inline void setMipMapTextures(const vector<MipMapTexture>& mipMapTextures) {
417  this->mipMapTextures = mipMapTextures;
418  }
419 
420  /**
421  * Get mip map textures
422  * @param bc7Encoded bc7 encoded if true or RGB/A if false
423  * @return mip map textures
424  */
425  const vector<MipMapTexture>& getMipMapTextures(bool bc7Encoded);
426 
427  // overridden methods
428  virtual void onDelete() override;
429 
430 private:
431  string id;
434  uint16_t width;
435  uint16_t height;
436  uint16_t textureHeight;
437  uint16_t textureWidth;
440  bool useMipMap;
441  bool repeat;
445  uint16_t atlasSize;
446  vector<MipMapTexture> mipMapTextures;
447 
448  /**
449  * Destructor
450  */
451  inline virtual ~Texture() {
452  }
453 
454  /**
455  * Get RGB/RGBA texture data wrapped in a byte buffer
456  * @param format texture data format
457  * @param textureData texture data
458  * @return RGB/RGBA texture data wrapped in a byte buffer
459  */
461 
462  /**
463  * Generate mip map texture
464  * @param textureWidth texture width
465  * @param textureHeight texture height
466  * @param bytesPerPixel bytes per pixel
467  * @param textureTextureData texture texture data
468  */
469  ByteBuffer generateMipMap(int textureWidth, int textureHeight, int bytesPerPixel, const ByteBuffer& textureTextureData);
470 
471 };
Texture entity.
Definition: Texture.h:24
TextureDepth depth
Definition: Texture.h:432
TextureFilter getMinFilter() const
Definition: Texture.h:339
static TextureFormat getBC7FormatByPixelBitsPerPixel(int bpp)
Return BC7 RGB/A texture format by bits per pixel.
Definition: Texture.h:98
uint16_t getWidth() const
Definition: Texture.h:197
const vector< MipMapTexture > & getMipMapTextures(bool bc7Encoded)
Get mip map textures.
Definition: Texture.cpp:286
bool isRepeat() const
Definition: Texture.h:309
uint16_t atlasSize
Definition: Texture.h:445
static TextureFormat getRGBFormatByPixelBitsPerPixel(int bpp)
Return RGB/A texture format by bits per pixel.
Definition: Texture.h:72
ByteBuffer generateMipMap(int textureWidth, int textureHeight, int bytesPerPixel, const ByteBuffer &textureTextureData)
Generate mip map texture.
Definition: Texture.cpp:240
bool isBC7TextureFormat() const
Definition: Texture.h:246
ByteBuffer getRGBTextureData()
Definition: Texture.h:253
bool isUseMipMap() const
Definition: Texture.h:294
uint8_t getRGBDepthBitsPerPixel() const
Definition: Texture.h:186
virtual void onDelete() override
Callback method to be overridden, will be called if object will be deleted.
Definition: Texture.cpp:336
const string & getId() const
Definition: Texture.h:172
uint16_t textureWidth
Definition: Texture.h:437
TextureFilter magFilter
Definition: Texture.h:444
ByteBuffer getBC7TextureData()
Definition: Texture.cpp:140
bool isRGBTextureFormat() const
Definition: Texture.h:232
TextureFilter minFilter
Definition: Texture.h:443
ByteBuffer * getTextureData()
Definition: Texture.h:272
uint16_t getTextureHeight() const
Definition: Texture.h:211
TextureFilter getMagFilter() const
Definition: Texture.h:354
vector< MipMapTexture > mipMapTextures
Definition: Texture.h:446
TextureDepth getDepth()
Definition: Texture.h:179
ClampMode clampMode
Definition: Texture.h:442
static TextureFormat getPNGFormatByPixelBitsPerPixel(int bpp)
Return PNG RGB/A texture format by bits per pixel.
Definition: Texture.h:85
uint16_t getTextureWidth() const
Definition: Texture.h:218
void setUseCompression(bool useCompression)
Set if to use compression.
Definition: Texture.h:287
void setClampMode(ClampMode clampMode)
Set clamp mode.
Definition: Texture.h:332
void setTextureData(TextureFormat format, const ByteBuffer &textureData)
Set RGB(A) texture data.
Definition: Texture.cpp:160
ClampMode getClampMode() const
Definition: Texture.h:324
TextureFormat getTextureFormat() const
Definition: Texture.h:225
ByteBuffer textureData
Definition: Texture.h:438
void setUseMipMap(bool useMipMap)
Set if to use mip map.
Definition: Texture.h:302
static TextureDepth getRGBDepthByPixelBitsPerPixel(int bpp)
Return RGB/A texture depth by bits per pixel.
Definition: Texture.h:59
uint16_t textureHeight
Definition: Texture.h:436
uint16_t getHeight() const
Definition: Texture.h:204
@ TEXTUREFILTER_LINEAR_MIPMAP_LINEAR
Definition: Texture.h:49
@ TEXTUREFILTER_LINEAR_MIPMAP_NEAREST
Definition: Texture.h:47
@ TEXTUREFILTER_NEAREST_MIPMAP_NEAREST
Definition: Texture.h:46
@ TEXTUREFILTER_NEAREST_MIPMAP_LINEAR
Definition: Texture.h:48
void setRepeat(bool repeat)
Set repeat.
Definition: Texture.h:317
void setAtlasSize(uint16_t atlasSize)
Set atlas size.
Definition: Texture.h:377
uint16_t getAtlasSize() const
Definition: Texture.h:369
bool isPNGTextureFormat() const
Definition: Texture.h:239
virtual ~Texture()
Destructor.
Definition: Texture.h:451
TextureFormat format
Definition: Texture.h:433
void setMinFilter(TextureFilter filter)
Set texture min filter.
Definition: Texture.h:347
bool isUseCompression() const
Definition: Texture.h:279
void setMagFilter(TextureFilter filter)
Set texture mag filter.
Definition: Texture.h:362
void setMipMapTextures(const vector< MipMapTexture > &mipMapTextures)
Set mip map textures.
Definition: Texture.h:416
Standard math functions.
Definition: Math.h:19
Byte buffer class.
Definition: ByteBuffer.h:27
Reference counter implementation to be used with inheritance.
Definition: Reference.h:13
MipMapTexture(TextureFormat format, uint16_t width, uint16_t height, ByteBuffer textureData)
Definition: Texture.h:110
#define FORBID_CLASS_COPY(CLASS)
Definition: tdme.h:6