[SOLVED] ram images from arrays in memory c++

I’m trying to stream images from memory to a texture.

I’m currently doing the following

Texture tex;
tex.set_compression(Texture::CM_off);
tex.setup_2d_texture(640, 480, Texture::T_unsigned_short, Texture::F_rgb8);
tex_.set_ram_image(data);

So the problem is coming from ‘data’. I cannot figure out how to assign my original array (uint8_t buffer[1920*480]) to a CPTA_uchar.
So I’m setting it terribly inefficiently through a loop.

data.push_back(buffer[i])

But even when i do this, i get an assertion when calling set_ram_image(), which i believe is coming from the fact that this pointertoarray is not correct.

Assertion failed: compression != CM_off || image.size() == do_get_expected_ram_image_size(cdata) at line 4264 of panda/src/gobj/texture.cxx

Any suggestions or alternative solutions?
Thanks

The error is coming from the fact that you have requested T_unsigned_short, which implies 16 bits (two bytes) per channel. Thus, Panda is expecting 2 bytes * 3 channels * 640 * 480 = 1843200 bytes, and you have presumably provided only 1 byte * 3 channels * 640 * 480 = 921600 bytes. If you have only one byte per channels, specify T_unsigned_byte instead.

As to the proper way to fill up a CPTA_uchar, well, you can’t, since the “C” stands for “const”. But you can fill up a PTA_uchar instead.

PTA_uchar data = PTA_uchar::empty_array(921600);
memcpy(data.p(), source, 921600);

Note the use of the empty_array() constructor to give you a PTA_uchar that’s pre-allocated to the appropriate size, and the p() accessor to give a pointer to the first element of the array.

Dvaid

perfect, thanks!

For the record, the proper way to use Texture is:

PT(Texture) tex = new Texture;
tex->set_compression(...);

Not doing it this way may result in Panda attempting to garbage collect it before you’re done with it.