Search code examples
c++texturespixelcinder

Fill C++ Cinder textures with RGBA values


I use the Cinder Library and want to create a texture, filled with RGBA values which I saved in an array. There is no helpfull explanation on the internet.


Solution

  • I've not used cinder before but a quick perusal of the documentation seems to suggest you can load a texture either form a file or from a Surface.

    So looking at the docs it would seem you create a surface as follows:

    cinder::Surface8u surf( 128, 128, SurfaceChannelOrder::RGBA );
    

    You can then fill it using the getData function as follows:

    uint8_t* pCols = surf.getData();
    for( int y = 0; y < 128; y++ )
    {
        for( int x = 0; x < 128; x++ )
        {
            // Fill each pixel with red.
            const idx = (y * (128 * 4)) + (x * 4);
            pCols[idx + 0] = 0xff;
            pCols[idx + 1] = 0x00;
            pCols[idx + 2] = 0x00;
            pCols[idx + 3] = 0xff;
        }
    }
    

    You would then load the texture from the surface as follows:

    cinder::gl::Texture texture( surf );