15 #ifndef FPLBASE_TEXTURE_H
16 #define FPLBASE_TEXTURE_H
20 #include "fplbase/config.h"
23 #include "fplbase/handles.h"
24 #include "mathfu/constants.h"
25 #include "mathfu/glsl_mappings.h"
47 kFormatLuminanceAlpha,
69 return static_cast<TextureFlags>(
static_cast<int>(a) | static_cast<int>(b));
132 bool IsValid() {
return ValidTextureHandle(id_); }
140 void Set(
size_t unit);
148 void Set(
size_t unit)
const;
163 int yoffset,
int width,
int height,
const void *data);
177 mathfu::vec2i *dimensions,
193 static uint8_t *
UnpackWebP(
const void *webp_buf,
size_t size,
195 mathfu::vec2i *dimensions,
209 static uint8_t *
UnpackASTC(
const void *astc_buf,
size_t size,
224 static uint8_t *
UnpackPKM(
const void *file_buf,
size_t size,
239 static uint8_t *
UnpackKTX(
const void *file_buf,
size_t size,
255 static uint8_t *
UnpackPng(
const void *png_buf,
size_t size,
257 mathfu::vec2i *dimensions,
259 return UnpackImage(png_buf, size, scale, flags, dimensions, texture_format);
274 static uint8_t *
UnpackJpg(
const void *jpg_buf,
size_t size,
276 mathfu::vec2i *dimensions,
278 return UnpackImage(jpg_buf, size, scale, flags, dimensions, texture_format);
300 const mathfu::vec2 &
scale,
302 mathfu::vec2i *dimensions,
309 const mathfu::vec2i &size);
314 const mathfu::vec2i &size);
330 const mathfu::vec2i &
size()
const {
return size_; }
335 const mathfu::vec2 &
scale()
const {
return scale_; }
363 if (original_size_.x == 0 && original_size_.y == 0) {
364 original_size_ =
size;
369 TextureImpl *impl() {
return impl_; }
371 MATHFU_DEFINE_CLASS_SIMD_AWARE_NEW_DELETE
376 static TextureImpl *CreateTextureImpl();
377 static void DestroyTextureImpl(TextureImpl *impl);
390 const uint8_t *buffer,
const mathfu::vec2i &size,
407 static uint8_t *UnpackImage(
const void *img_buf,
size_t size,
409 mathfu::vec2i *dimensions,
418 mathfu::vec2i original_size_;
421 TextureTarget target_;
429 typedef std::function<Texture *(
const char *filename,
TextureFormat format,
436 #endif // FPLBASE_TEXTURE_H
static uint8_t * UnpackWebP(const void *webp_buf, size_t size, const mathfu::vec2 &scale, TextureFlags flags, mathfu::vec2i *dimensions, TextureFormat *texture_format)
Unpacks a memory buffer containing a Webp format file.
static uint8_t * UnpackASTC(const void *astc_buf, size_t size, TextureFlags flags, mathfu::vec2i *dimensions, TextureFormat *texture_format)
Reads a memory buffer containing an ASTC format (.astc) file.
const mathfu::vec2 & scale() const
Get the Texture scale.
Definition: texture.h:335
Texture(const char *filename=nullptr, TextureFormat format=kFormatAuto, TextureFlags flags=kTextureFlagsUseMipMaps)
Constructor for a Texture.
TextureFlags flags() const
returns the texture flags.
Definition: texture.h:343
Load texture asynchronously.
Definition: texture.h:62
void Delete()
Delete the Texture stored in id_, and reset id_ to 0.
static uint8_t * UnpackJpg(const void *jpg_buf, size_t size, const mathfu::vec2 &scale, TextureFlags flags, mathfu::vec2i *dimensions, TextureFormat *texture_format)
Unpacks a memory buffer containing a Jpeg format file.
Definition: texture.h:274
const std::string & filename() const
The name of the file associated with the resource.
Definition: async_loader.h:107
static uint16_t * Convert8888To5551(const uint8_t *buffer, const mathfu::vec2i &size)
Utility function to convert 32bit RGBA (8-bits each) to 16bit RGB in hex 5551 format.
If not set, use repeating texcoords.
Definition: texture.h:56
Abstraction for a texture object loaded on the GPU.
Definition: texture.h:104
const mathfu::vec2i & size() const
Get the Texture size.
Definition: texture.h:330
static uint8_t * UnpackTGA(const void *tga_buf, TextureFlags flags, mathfu::vec2i *dimensions, TextureFormat *texture_format)
Unpacks a memory buffer containing a TGA format file.
bool IsValid()
Whether this object loaded and finalized correctly. Call after Finalize has been called (by AssetMana...
Definition: texture.h:132
TextureFormat
Definition: texture.h:36
Default behavior.
Definition: texture.h:54
const TextureHandle & id() const
Get the Texture handle ID.
Definition: texture.h:325
const mathfu::vec2i & original_size() const
Get the original size of the Texture.
Definition: texture.h:348
static uint8_t * UnpackPng(const void *png_buf, size_t size, const mathfu::vec2 &scale, TextureFlags flags, mathfu::vec2i *dimensions, TextureFormat *texture_format)
Unpacks a memory buffer containing a Png format file.
Definition: texture.h:255
void UpdateTexture(size_t unit, TextureFormat format, int xoffset, int yoffset, int width, int height, const void *data)
Update (part of) the current texture with new pixel data. For now, must always update at least entire...
static uint8_t * UnpackPKM(const void *file_buf, size_t size, TextureFlags flags, mathfu::vec2i *dimensions, TextureFormat *texture_format)
Reads a memory buffer containing an ETC2 format (.pkm) file.
void set_scale(const mathfu::vec2 &scale)
Set the Texture scale.
Definition: texture.h:340
void set_original_size(const mathfu::vec2i &size)
Set the original size of the Texture.
Definition: texture.h:353
TextureFlags
Flags affecting loading and sampler modes for a texture.
Definition: texture.h:52
Uses (or generates) mipmaps.
Definition: texture.h:58
virtual void LoadFromMemory(const uint8_t *data, const mathfu::vec2i &size, TextureFormat texture_format)
Create a texture from data in memory.
static uint8_t * LoadAndUnpackTexture(const char *filename, const mathfu::vec2 &scale, TextureFlags flags, mathfu::vec2i *dimensions, TextureFormat *texture_format)
Loads the file in filename, and then unpacks the file format (supports TGA, WebP, KTX...
Data represents a 1x6 cubemap.
Definition: texture.h:60
virtual bool Finalize()
Creates a Texture from data_ and stores the handle in id_.
Uses the same format as the source file.
Definition: texture.h:46
virtual ~Texture()
Destructor for a Texture.
internal::OpaqueHandle TextureHandle
Backend agnostic handles to various resources.
Definition: handles.h:40
TextureFormat format() const
Get the Texture format.
Definition: texture.h:357
static uint8_t * UnpackKTX(const void *file_buf, size_t size, TextureFlags flags, mathfu::vec2i *dimensions, TextureFormat *texture_format)
Reads a memory buffer containing an KTX format (.ktx) file.
void Set(size_t unit, Renderer *renderer)
Set the active Texture and binds id_ to GL_TEXTURE_2D.
static uint16_t * Convert888To565(const uint8_t *buffer, const mathfu::vec2i &size)
Utility function to convert 24bit RGB (8-bits each) to 16bit RGB in hex 565 format.
bool HasAlpha(TextureFormat format)
determines if the format has an alpha component.
Definition: texture.h:73
std::function< Texture *(const char *filename, TextureFormat format, TextureFlags flags)> TextureLoaderFn
used by some functions to allow the texture loading mechanism to be specified by the caller...
Definition: texture.h:431
The default, picks based on loaded data.
Definition: texture.h:37
virtual void Load()
Loads and unpacks the Texture from filename_ into data_. It also sets the original size...
void SetTextureId(TextureTarget target, TextureHandle id)
Set texture target and id directly for textures that have been created outside of this class...
Renderer is the main API class for rendering commands.
Definition: renderer.h:310
void SetOriginalSizeIfNotYetSet(const mathfu::vec2i &size)
If the original size has not yet been set, then set it.
Definition: texture.h:362
Definition: async_loader.h:48
bool IsCompressed(TextureFormat format)
determines if the format is already compressed in some way. If image data is supplied in these format...
Definition: texture.h:87