Skip to content

Class Texture

ClassList > Texture

OpenGL texture object wrapper with RAII semantics. More...

  • #include <Texture.h>

Public Types

Type Name
typedef const std::tuple< GLuint, GLuint, GLuint, GLuint > TexStorageInfo
Texture storage format information tuple.
enum TextureType
OpenGL texture format and channel layout.

Public Attributes

Type Name
GLuint tex_slot_offset = 0
Texture unit offset for binding.
TextureType tex_type = NONE\_TEXTURE
Texture format type.

Public Functions

Type Name
void Bind (GLuint slot=-1) const
Binds texture to a texture unit for sampling.
void BindC (GLuint slot=-1, GLuint read_or_write=GL_READ_WRITE, GLuint _level=0) const
Binds texture as compute shader image (read/write).
void BindU (GLuint slot=-1) const
Binds texture for shader uniform access (legacy, prefer Bind).
void ConvertDepthCubeFrom (const Texture & _Tar_Tex)
Converts texture to depth cubemap format.
void ConvertDepthFrom (const Texture & _Tar_Tex)
Converts texture to depth format.
void ConvertPNGFrom (const Texture & _Tar_Tex)
Converts texture to PNG-compatible format (RGBA8).
void FillColor (const glm::vec4 col)
Fills texture with solid color.
void GenCubeMapFrom (const Texture & _Tar_Tex, int res=1024)
Converts equirectangular map to cubemap.
void GenERectMapFrom (const Texture & _Tar_Tex, int _w=2048, int _h=1024)
Converts cubemap to equirectangular map.
void GenIBLDiffuseFrom (const Texture & _Tar_Tex, bool to_cubemap=false)
Generates diffuse IBL map.
void GenIBLSpecularFrom (const Texture & _Tar_Tex, bool to_cubemap=false)
Generates specular IBL map with importance sampling.
void GenIrradiaceConvFrom (const Texture & _Tar_Tex)
Generates irradiance convolution map for diffuse IBL.
int GetBPP () const
Returns bytes per pixel.
int GetH () const
Returns texture height.
glm::vec2 GetSize () const
Returns texture size as vec2.
GLuint GetTexID () const
Returns OpenGL texture ID.
std::string GetTexName () const
Returns texture name/path.
int GetW () const
Returns texture width.
void OffsetSlot (GLuint _offset)
Offsets the texture unit slot for binding.
void PrintTexture () const
Prints texture metadata to console.
void Resize (const glm::vec2 & size)
Resizes the texture.
void Resize (GLuint x, GLuint y)
Resizes the texture.
void SaveTexture (std::string _path, bool force_png=false, bool force_cube=false) const
Saves texture to file.
Texture (const std::string & texpath, TextureType tex_type, GLuint tile_type)
Constructs texture from image file.
Texture (int _w, int _h, TextureType tex_type, GLuint tile_type)
Constructs empty texture for framebuffer attachment.
Texture (int _w, int _h, GLuint _layout, const void * _ptr, GLint _min_filter=GL_LINEAR, GLint _mag_filter=GL_LINEAR, GLint _wrap_s=GL_REPEAT, GLint _wrap_t=GL_REPEAT)
Constructs texture from raw pixel data.
Texture (int _w, int _h, GLuint _ID, TextureType _type, std::string _name)
Constructs texture by copying metadata from another texture.
Texture (int _w, int _h, TextureType _type)
Constructs empty texture with specified type.
Texture ()
Default constructor (creates null texture).
Texture (const Texture & tex)
Copy constructor (deep copy, creates new texture).
Texture (Texture && tex) noexcept
Move constructor (transfers ownership of GPU resource).
void Unbind () const
Unbinds texture from texture unit.
void UnbindC (GLuint slot=-1, GLuint read_or_write=GL_READ_WRITE, GLuint _level=0) const
Unbinds texture from compute shader image unit.
Texture & operator= (const Texture & tex)
Copy assignment operator.
Texture & operator= (Texture && tex) noexcept
Move assignment operator (transfers ownership).
~Texture ()
Destructor (releases OpenGL texture object).

Public Static Functions

Type Name
void BindM (GLuint _id, GLuint _slot=0, TextureType _type=RGBA_TEXTURE)
Static method to bind texture by ID.
TexStorageInfo ParseFormat (TextureType _type)
Parses TextureType enum to OpenGL format constants.
void SetTexParam (GLuint _id, GLuint _fil_min, GLuint _fil_max, GLuint _warp_s=0, GLuint _warp_t=0, GLuint _lev_min=0, GLuint _lev_max=0, GLuint _warp_r=0)
Sets texture parameters (filtering, wrapping).

Detailed Description

Texture encapsulates an OpenGL texture object and provides methods for creation, binding, resizing, and format conversion. Supports 2D textures, cubemaps, and various pixel formats (LDR, HDR, depth, etc.).

Constructors: * From file: Loads image file and creates texture * From data: Creates texture from raw pixel data * For framebuffer: Allocates empty texture for render targets * For processing: Creates texture for IBL/processing outputs

Usage:

// Load from file
Texture tex("albedo.png", Texture::RGBA_TEXTURE, GL_REPEAT);
tex.Bind(0);  // Bind to texture unit 0

// Create framebuffer texture
Texture fb_tex(GL_REPEAT, 1920, 1080);  // For color attachment

// Create from data
std::vector<float> data(width * height * 4);
Texture gen_tex(width, height, GL_RGBA32F, data.data());

Note:

Thread-safety: Not thread-safe. Must be used from OpenGL context thread.

Note:

Resource ownership: Owns tex_ID, releases via glDeleteTextures in destructor.

Public Types Documentation

typedef TexStorageInfo

Texture storage format information tuple.

using Texture::TexStorageInfo =  const std::tuple<GLuint, GLuint, GLuint, GLuint>;

Contains OpenGL format constants for texture creation: * internal_layout: Internal format (e.g., GL_RGBA8, GL_RGBA32F) * layout: Pixel data format (e.g., GL_RGBA, GL_RGB) * data_type: Pixel data type (e.g., GL_UNSIGNED_BYTE, GL_FLOAT) * texture_type: Texture target (e.g., GL_TEXTURE_2D, GL_TEXTURE_CUBE_MAP)


enum TextureType

OpenGL texture format and channel layout.

enum Texture::TextureType {
    NONE_TEXTURE,
    RGBA_TEXTURE,
    RGB_TEXTURE,
    HDR_TEXTURE = 4,
    HDR_CUBE_TEXTURE = 5,
    BUFFER_TEXTURE = 6,
    HDR_BUFFER_TEXTURE,
    FLOAT_BUFFER_TEXTURE,
    RG_TEXTURE,
    LAYERED_TEXTURE,
    LIGHTING_CACHE = 30,
    DEPTH_CUBE_TEXTURE = 31,
    DEPTH_TEXTURE
};

Abstracts the OpenGL pixel format (channels and data type) used when allocating or uploading texture data. Values also serve as default texture unit slot bases: e.g. BUFFER_TEXTURE (6) is the base slot offset for framebuffer textures passed as shader uniforms.

Note:

BUFFER_TEXTURE must be kept as value 6; it is used as an offset for framebuffer texture slots (BUFFER_TEXTURE + FBType = slot).


Public Attributes Documentation

variable tex_slot_offset

Texture unit offset for binding.

GLuint Texture::tex_slot_offset;


variable tex_type

Texture format type.

TextureType Texture::tex_type;


Public Functions Documentation

function Bind

Binds texture to a texture unit for sampling.

void Texture::Bind (
    GLuint slot=-1
) const

Parameters:

  • slot Texture unit (0-31), or -1 to use tex_slot_offset

Note:

Call before rendering to make texture accessible to shaders


function BindC

Binds texture as compute shader image (read/write).

void Texture::BindC (
    GLuint slot=-1,
    GLuint read_or_write=GL_READ_WRITE,
    GLuint _level=0
) const

Parameters:

  • slot Image unit (0-7), or -1 to use tex_slot_offset
  • read_or_write Access mode (GL_READ_ONLY, GL_WRITE_ONLY, GL_READ_WRITE)
  • _level Mipmap level (default 0)

function BindU

Binds texture for shader uniform access (legacy, prefer Bind).

void Texture::BindU (
    GLuint slot=-1
) const

Parameters:


function ConvertDepthCubeFrom

Converts texture to depth cubemap format.

void Texture::ConvertDepthCubeFrom (
    const Texture & _Tar_Tex
) 

Parameters:

  • _Tar_Tex Source texture

function ConvertDepthFrom

Converts texture to depth format.

void Texture::ConvertDepthFrom (
    const Texture & _Tar_Tex
) 

Parameters:

  • _Tar_Tex Source texture

function ConvertPNGFrom

Converts texture to PNG-compatible format (RGBA8).

void Texture::ConvertPNGFrom (
    const Texture & _Tar_Tex
) 

Parameters:

  • _Tar_Tex Source texture

function FillColor

Fills texture with solid color.

void Texture::FillColor (
    const glm::vec4 col
) 

Parameters:

  • col Color to fill (RGBA)

function GenCubeMapFrom

Converts equirectangular map to cubemap.

void Texture::GenCubeMapFrom (
    const Texture & _Tar_Tex,
    int res=1024
) 

Parameters:

  • _Tar_Tex Source equirectangular texture
  • res Cubemap face resolution (default 1024)

function GenERectMapFrom

Converts cubemap to equirectangular map.

void Texture::GenERectMapFrom (
    const Texture & _Tar_Tex,
    int _w=2048,
    int _h=1024
) 

Parameters:

  • _Tar_Tex Source cubemap texture
  • _w Output width (default 2048)
  • _h Output height (default 1024)

function GenIBLDiffuseFrom

Generates diffuse IBL map.

void Texture::GenIBLDiffuseFrom (
    const Texture & _Tar_Tex,
    bool to_cubemap=false
) 

Parameters:

  • _Tar_Tex Source environment map
  • to_cubemap If true, outputs cubemap; otherwise equirectangular

function GenIBLSpecularFrom

Generates specular IBL map with importance sampling.

void Texture::GenIBLSpecularFrom (
    const Texture & _Tar_Tex,
    bool to_cubemap=false
) 

Parameters:

  • _Tar_Tex Source environment map
  • to_cubemap If true, outputs cubemap; otherwise equirectangular

function GenIrradiaceConvFrom

Generates irradiance convolution map for diffuse IBL.

void Texture::GenIrradiaceConvFrom (
    const Texture & _Tar_Tex
) 

Parameters:

  • _Tar_Tex Source environment map

Note:

Convolves environment to low-frequency diffuse lighting


function GetBPP

Returns bytes per pixel.

inline int Texture::GetBPP () const


function GetH

Returns texture height.

inline int Texture::GetH () const


function GetSize

Returns texture size as vec2.

inline glm::vec2 Texture::GetSize () const


function GetTexID

Returns OpenGL texture ID.

inline GLuint Texture::GetTexID () const


function GetTexName

Returns texture name/path.

inline std::string Texture::GetTexName () const


function GetW

Returns texture width.

inline int Texture::GetW () const


function OffsetSlot

Offsets the texture unit slot for binding.

inline void Texture::OffsetSlot (
    GLuint _offset
) 

Parameters:

  • _offset Slot offset to add

function PrintTexture

Prints texture metadata to console.

void Texture::PrintTexture () const

Note:

Debug utility for inspecting texture properties


function Resize [1/2]

Resizes the texture.

void Texture::Resize (
    const glm::vec2 & size
) 

Parameters:

  • size New size (width, height)

Note:

Reallocates GPU storage, existing data is lost


function Resize [2/2]

Resizes the texture.

void Texture::Resize (
    GLuint x,
    GLuint y
) 

Parameters:

  • x New width
  • y New height

function SaveTexture

Saves texture to file.

void Texture::SaveTexture (
    std::string _path,
    bool force_png=false,
    bool force_cube=false
) const

Parameters:

  • _path Output file path
  • force_png If true, always save as PNG (default false, uses extension)
  • force_cube If true, save as cubemap faces (6 images)

function Texture [1/8]

Constructs texture from image file.

Texture::Texture (
    const std::string & texpath,
    TextureType tex_type,
    GLuint tile_type
) 

Parameters:

  • texpath Path to image file
  • tex_type Texture type (RGBA_TEXTURE, HDR_TEXTURE, etc.)
  • tile_type Texture wrapping mode (GL_REPEAT, GL_CLAMP_TO_EDGE, etc.)

Note:

Automatically loads file and uploads to GPU


function Texture [2/8]

Constructs empty texture for framebuffer attachment.

Texture::Texture (
    int _w,
    int _h,
    TextureType tex_type,
    GLuint tile_type
) 

Parameters:

  • _w Width in pixels
  • _h Height in pixels
  • tex_type Texture type (RGBA_TEXTURE, HDR_TEXTURE, etc.)
  • tile_type Texture wrapping mode

Note:

Allocates GPU storage but does not initialize pixel data


function Texture [3/8]

Constructs texture from raw pixel data.

Texture::Texture (
    int _w,
    int _h,
    GLuint _layout,
    const void * _ptr,
    GLint _min_filter=GL_LINEAR,
    GLint _mag_filter=GL_LINEAR,
    GLint _wrap_s=GL_REPEAT,
    GLint _wrap_t=GL_REPEAT
) 

Parameters:

  • _w Width in pixels
  • _h Height in pixels
  • _layout Internal format (GL_RGBA8, GL_RGBA32F, etc.)
  • _ptr Pointer to pixel data
  • _min_filter Minification filter (default GL_LINEAR)
  • _mag_filter Magnification filter (default GL_LINEAR)
  • _wrap_s S-axis wrapping mode (default GL_REPEAT)
  • _wrap_t T-axis wrapping mode (default GL_REPEAT)

function Texture [4/8]

Constructs texture by copying metadata from another texture.

Texture::Texture (
    int _w,
    int _h,
    GLuint _ID,
    TextureType _type,
    std::string _name
) 

Parameters:

  • _w Width
  • _h Height
  • _ID OpenGL texture ID to wrap
  • _type Texture type
  • _name Texture name

Note:

Does not create new texture, wraps existing ID


function Texture [5/8]

Constructs empty texture with specified type.

Texture::Texture (
    int _w,
    int _h,
    TextureType _type
) 

Parameters:

  • _w Width
  • _h Height
  • _type Texture type

function Texture [6/8]

Default constructor (creates null texture).

Texture::Texture () 


function Texture [7/8]

Copy constructor (deep copy, creates new texture).

Texture::Texture (
    const Texture & tex
) 

Parameters:

  • tex Source texture to copy

function Texture [8/8]

Move constructor (transfers ownership of GPU resource).

Texture::Texture (
    Texture && tex
) noexcept

Parameters:

  • tex Source texture to move from

function Unbind

Unbinds texture from texture unit.

void Texture::Unbind () const


function UnbindC

Unbinds texture from compute shader image unit.

void Texture::UnbindC (
    GLuint slot=-1,
    GLuint read_or_write=GL_READ_WRITE,
    GLuint _level=0
) const

Parameters:

  • slot Image unit, or -1 for tex_slot_offset
  • read_or_write Access mode (must match BindC call)
  • _level Mipmap level

function operator=

Copy assignment operator.

Texture & Texture::operator= (
    const Texture & tex
) 

Parameters:

  • tex Source texture

Returns:

Reference to this texture


function operator=

Move assignment operator (transfers ownership).

Texture & Texture::operator= (
    Texture && tex
) noexcept

Parameters:

  • tex Source texture

Returns:

Reference to this texture


function ~Texture

Destructor (releases OpenGL texture object).

Texture::~Texture () 


Public Static Functions Documentation

function BindM

Static method to bind texture by ID.

static inline void Texture::BindM (
    GLuint _id,
    GLuint _slot=0,
    TextureType _type=RGBA_TEXTURE
) 

Parameters:

  • _id OpenGL texture ID
  • _slot Texture unit
  • _type Texture type (determines bind target)

function ParseFormat

Parses TextureType enum to OpenGL format constants.

static inline TexStorageInfo Texture::ParseFormat (
    TextureType _type
) 

Parameters:

Returns:

Tuple of (internal_layout, layout, data_type, texture_type)


function SetTexParam

Sets texture parameters (filtering, wrapping).

template<GLuint Type>
static inline void Texture::SetTexParam (
    GLuint _id,
    GLuint _fil_min,
    GLuint _fil_max,
    GLuint _warp_s=0,
    GLuint _warp_t=0,
    GLuint _lev_min=0,
    GLuint _lev_max=0,
    GLuint _warp_r=0
) 

Template parameters:

  • Type Texture target (GL_TEXTURE_2D, GL_TEXTURE_CUBE_MAP, etc.)

Parameters:

  • _id Texture ID
  • _fil_min Minification filter
  • _fil_max Magnification filter
  • _warp_s S-axis wrapping mode
  • _warp_t T-axis wrapping mode
  • _lev_min Minimum mipmap level
  • _lev_max Maximum mipmap level
  • _warp_r R-axis wrapping mode (for 3D/cubemap)


The documentation for this class was generated from the following file src/render/Texture.h