i'm working opengl @ moment, creating 'texture cache' handles loading images , buffering them opengl. in event image file can't loaded needs fall default texture i've hard-coded in constructor.
what need create texture of uniform colour. not difficult, it's array of size pixels * colour channels.
i using std::vector hold initial data before upload opengl. problem i'm having can't find information on best way initialize vector repeating pattern.
the first way occurred me use loop.
std::vector<unsigned char> blue_texture; (int iii = 0; iii < width * height; iii++) { blue_texture.push_back(0); blue_texture.push_back(0); blue_texture.push_back(255); blue_texture.push_back(255); } however, seems inefficient since vector have resize numerous times. if reserve space first , perform loop it's still not efficient since contents zeroed before loop means 2 writes each unsigned char.
currently i'm using following method:
struct colour {unsigned char r; unsigned char g; unsigned char b; unsigned char a;}; colour blue = {0, 0, 255, 255}; std::vector<colour> texture((width * height), blue); i extract data using:
reinterpret_cast<unsigned char*>(texture.data()); is there better way this? i'm new c/c++ , i'll honest, casting pointers scares me.
your loop solution right way go in opinion. make efficient removing repeated realloc calls, use blue_texture.reserve(width * height * 4)
the reserve call increase allocation, aka capacity size without zero-filling it. (note operating system may still 0 it, if pulls memory mmap example.) not change size of vector, push_back , friends still work same way.
Comments
Post a Comment