A smaller class for manipulating texture data before upload

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.

A smaller class for manipulating texture data before upload

Postby devsh » Sat Dec 02, 2017 11:36 am

cpp Code: Select all
#include "IReferenceCounted.h"
#include "SColor.h"
namespace irr
namespace video
struct CImageData : public IReferenceCounted
    void*       data;
    uint32_t    offset[3];
    uint32_t    size[3];
    uint32_t    mipLevelHint;
    uint32_t    colorFormat;
        data = NULL;
        offset[0] = 0;
        offset[1] = 0;
        offset[2] = 0;
        size[0] = 0;
        size[1] = 0;
        size[2] = 0;
        mipLevelHint = 0;
        colorFormat = ECF_UNKNOWN;
    CImageData(void* inData, uint32_t inOffset[3], uint32_t inSize[3],
               const uint32_t& inMipLevel, const ECOLOR_FORMAT& inFmt,
               const bool& dataAllocatedWithMallocAndCanTake=false)
        mipLevelHint = inMipLevel;
        colorFormat = inFmt;
        if (dataAllocatedWithMallocAndCanTake)
            data = inData;
            size_t imgByteSize = getImageDataSizeInBytes();
            data = malloc(imgByteSize);
    virtual ~CImageData()
        if (data)
    //! Returns pointer to raw data
    const void* getData() {return data;}
    //! Returns offset in width,height and depth of image slice.
    const uint32_t* getOffset() const {return offset;}
    //! Returns width,height and depth of image slice.
    const uint32_t* getSize() const {return size;}
    const uint32_t& getSupposedMipLevel() const {return mipLevelHint;}
    //! Returns bits per pixel.
    uint32_t getBitsPerPixel() const
        return getBitsPerPixelFromFormat(colorFormat);
    //! Returns image data size in bytes
    size_t getImageDataSizeInBytes() const
        size_t lineSize = getPitch();
        return lineSize*static_cast<size_t>(size[1])*static_cast<size_t>(size[2]);
    //! Returns image data size in pixels
    size_t getImageDataSizeInPixels() const
        return static_cast<size_t>(size[0])*static_cast<size_t>(size[1])*static_cast<size_t>(size[2]);
    //! Returns the color format
    const ECOLOR_FORMAT& getColorFormat() const {return colorFormat;}
    //! Returns pitch of image
    uint32_t getPitch() const
        return (getBitsPerPixel()*size[0])/8;
} // end namespace video
} // end namespace irr

Made this little class to facilitate passing around pixel data for all texture format types, such as: 1D textures, 2D textures, cube maps; and their respective arrays, as well as 3D textures;

The data within is opaque, and it has no manipulation functions, which makes it a perfect solution for passing compressed texture data around.

My idea is to pass an array of these to the old createTextureFromIImage function, and have the function use glTextureSubImage calls to upload parts or all of the texture.

This is because writing unified loaders for cubemaps, 2D arrays and other things obfuscates and hides simple concepts behind thick layers of abstraction.
User avatar
Competition winner
Posts: 1833
Joined: Tue Dec 09, 2008 6:00 pm
Location: UK

Return to Code Snippets

Who is online

Users browsing this forum: No registered users and 1 guest