openglpixelstructure-packing

1 Byte Alignment vs 4 Byte Alignment (Pixel Packing)


I am reading the OpenGL SuperBible for OpenGL 3.x. I am having a hard time understanding the whole "pixel packing concept." I get that typically a 199px wide image would require 597 bytes [(199 * 3)3 for each color channel RGB]. My first question is why would this only sometimes be right, the author says this only woks with a 4byte alignment system. He goes on to say that an extra three bytes will be added to make it divisible by four easily, which I don't understand either. So really my question is what is the significance of a 4 byte alignment -- what does that actually mean?? The author then says the alternative is 1-byte alignment, which I don't understand either.

The author says that .TGA is 1 byte aligned or "tight" and .bmp is 4 byte aligned.

What is a 4 byte alignment, a 1 byte alignment, and why should I use one over the other? When should I use .tga or .bmp for texturing?


Solution

  • Well it is about data alignment. It differs on different architectures and memory management. For example when you code a in 32-bit processor the default padding on a structure / int is 4-byte. In 64-bit is 8-byte. When you want to override the default padding on a code structure you can use #pragma pack (on visual studio), attribute((packed)) (gcc).

    Look here for additional reference: