Problem with SDL_LoadBMP & glDrawPixels

Started by
5 comments, last by Acharis 11 years, 6 months ago
I have a very simple code:
1) First I load BMP via SDL_LoadBMP (it's BGR)
2) Next I convert it to RBGA and flip (manually, pixel by pixel)
3) Then I display it via glDrawPixels(w,h,GL_RGBA,GL_UNSIGNED_BYTE,image);

The odd thing, everything works fine as long as the image size can be divided by 4... When it is not (like 250x250) it shows quite random results (sometimes colours are messed up, sometimes parts of the image in different parts). I have not touched glPixelsStorage, it should have default values.

Stellar Monarch (4X, turn based, released): GDN forum topic - Twitter - Facebook - YouTube

Advertisement
The alignment works per row on byte level, not on pixel level. Since each pixel of your RGBA image has four components, then it by definition must always satisfy the byte alignment (which is 4, since you said the storage parameters have their default values). This can not be the problem under the circumstances you have explained it.

Can you provide screen shots of what the different results look like? I have seen many different cases of "messed up colors" and "different parts" and images to clearly show what you mean would be great,

As a side note, your step 2 is not necessary. OpenGL is very flexible with what parameters you can pass and how color components can be ordered. If you have a BGR image you can pass GL_BGR to glDrawPixels. If you add an alpha channel, you have some options depending on where in the pixel data you put it, but GL_BGRA may be an option.

edit: And of course, make sure that your code handles SDL's alignment padding as well. OpenGL is not the only one with alignment requirements.
Screenshot:
http://s1165.photobucket.com/albums/q591/LordArchibald/?action=view&current=divby4Error.png
The first rectangle is 250x250, the second is 255x255, the third is 256x256. All should look like the last one.



As a side note, your step 2 is not necessary. OpenGL is very flexible with what parameters you can pass and how color components can be ordered. If you have a BGR image you can pass GL_BGR to glDrawPixels. If you add an alpha channel, you have some options depending on where in the pixel data you put it, but GL_BGRA may be an option.
Yes, I know, but I wanted it to work with very outdated hardware and BGR/BGRA requires higher OpenGL version.


edit: And of course, make sure that your code handles SDL's alignment padding as well. OpenGL is not the only one with alignment requirements.
Can you explain more? I have never used SDL alignmnent padding (so it should have default values).


BTW, which SDL version exactly are you using and can you drop me a link where you downloaded it from? I used to have a problem with TTF library and it turned out I was caused by using some old and buggy version...

Stellar Monarch (4X, turn based, released): GDN forum topic - Twitter - Facebook - YouTube


Screenshot:
http://s1165.photobu...divby4Error.png
The first rectangle is 250x250, the second is 255x255, the third is 256x256. All should look like the last one.

That is definitely an alignment issue. Solution is likely below.

[quote name='Brother Bob' timestamp='1349718105' post='4988053']
As a side note, your step 2 is not necessary. OpenGL is very flexible with what parameters you can pass and how color components can be ordered. If you have a BGR image you can pass GL_BGR to glDrawPixels. If you add an alpha channel, you have some options depending on where in the pixel data you put it, but GL_BGRA may be an option.
Yes, I know, but I wanted it to work with very outdated hardware and BGR/BGRA requires higher OpenGL version.
[/quote]
It was added to the core API in early 1998, over 14 years ago now. It has even been available as a very common extension since before then. You're the one knowing how old hardware you're willing to deal with, though.

[quote name='Brother Bob' timestamp='1349718105' post='4988053']
edit: And of course, make sure that your code handles SDL's alignment padding as well. OpenGL is not the only one with alignment requirements.
Can you explain more? I have never used SDL alignmnent padding (so it should have default values).
[/quote]
The SDL_surface structure has a member called pitch that hold the offset between two consecutive rows of the image. I assume that SDL enforces at least a 4 byte alignment and so if a row is not a multiple of 4 bytes, then you cannot assume that width*3 is equal to pitch either. You must use the pitch member to calculate the location of each row in memory, not the width of the image.


BTW, which SDL version exactly are you using and can you drop me a link where you downloaded it from? I used to have a problem with TTF library and it turned out I was caused by using some old and buggy version...

I'm not using SDL.
What version of SDL are you using - and where did you download it from? (hint)

What version of SDL are you using - and where did you download it from? (hint)
SDL 1.2.15 from http://www.libsdl.org/download-1.2.php
SDL_mixer_version - CompiledVersion: 1.2.12 LinkedVersion: 1.2.12 (do not remember where I downloaded it from)
SDL_TTF_version - CompiledVersion:2.0.11 LinkedVersion:2.0.11 (do not remember where I downloaded it from)

Stellar Monarch (4X, turn based, released): GDN forum topic - Twitter - Facebook - YouTube


That is definitely an alignment issue. Solution is likely below.
Yes, that was it. Many thanks!


BTW, if anyone has a comment on the SDL/TTF/Mixer version I'm using, please do so, I already was using a broken TTF library.

Stellar Monarch (4X, turn based, released): GDN forum topic - Twitter - Facebook - YouTube

This topic is closed to new replies.

Advertisement