Texture Buffering color issues

I’m playing with texture buffering, in this case scenario I create some random numbers and feed them as pixels in RGBA8888 format.

The left side of screen I use a bit of bitshifting and the right size I use the SDL_MapRGBA().

Color on right side is more “washed out” while the bitshifted not.

Anyhow in both cases I would expect more colors not only the blue tinted screen. I cannot see red, in picture, not in a single pixel.

It’s important to say that both sides use the SAME random value to draw the pixel.

rnd = rand();
r = rnd       & 0x000000ff;
g = rnd >>  8 & 0x000000ff;
b = rnd >> 16 & 0x000000ff;
*pixels_l = SDL_MapRGBA( mapformat, r, g, b, 0xff);
*pixels_r = rnd | 0x000000ff;  

The code is here texture_buffering.c (2.0 KB)

Best regards.

Looks like your pixels are being rendered as 0xaarrggbb not 0xrrggbbaa (so when you think you are setting alpha to 0xFF you are actually setting blue to 0xFF, hence the overall blue cast). Check your pixel format.

Unfortunately that is not the issue…

I have changed the code for:

        rnd = rand();
        // Assuming  0xAARRGGBB
        b = rnd        & 0x000000ff;
        g = rnd >>  8  & 0x000000ff;
        r = rnd >> 16  & 0x000000ff;
        *pixels_l = SDL_MapRGBA( mapformat, r, g, b, 0xff);
        *pixels_r = rnd | 0xff000000;  

And the output is:

forcing 0xff in letfmost byte.

Another possibility is that you are assuming that, with your particular C compiler, RAND_MAX is at least 0xFFFFFF. That is not guaranteed: according to the standard RAND_MAX is allowed to be as small as 32767. If it is, your expression:

b = rnd >> 16 & 0x000000ff;

will always return zero. Of course that would result in an overall yellow cast (no blue) so there would have to be a problem with the pixel format as well if that is the explanation.

The color cast is because mapformat is the window’s pixel format (probably bgra8888), whereas your texture’s pixel format is created as rgba8888. So SDL_MapRGBA is putting the alpha channel value in the wrong place for the texture’s pixel format, giving you the color cast. Creating the textures with the same pixel format as the window fixes this, as would using the texture’s pixel format in the call to SDL_MapRGBA().

edit: also, you need to take into account pitch when setting pixels in the texture. It may be wider than width

1 Like