• Advertisement
Sign in to follow this  

Problem with SDL_LoadBMP & glDrawPixels

This topic is 1927 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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 [i]your[/i] code handles [i]SDL's[/i] alignment padding as well. OpenGL is not the only one with alignment requirements. Edited by Brother Bob

Share this post


Link to post
Share on other sites
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.


[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.[/quote]Yes, I know, but I wanted it to work with very outdated hardware and BGR/BGRA requires higher OpenGL version.

[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.
[/quote]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...

Share this post


Link to post
Share on other sites
[quote name='Acharis' timestamp='1349718991' post='4988059']
Screenshot:
[url="http://s1165.photobucket.com/albums/q591/LordArchibald/?action=view&current=divby4Error.png"]http://s1165.photobu...divby4Error.png[/url]
The first rectangle is 250x250, the second is 255x255, the third is 256x256. All should look like the last one.
[/quote]
That is definitely an alignment issue. Solution is likely below.

[quote name='Acharis' timestamp='1349718991' post='4988059'][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.[/quote]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='Acharis' timestamp='1349718991' post='4988059'][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.
[/quote]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 [i]pitch[/i] 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 [i]width*3[/i] is equal to [i]pitch[/i] either. You must use the [i]pitch[/i] member to calculate the location of each row in memory, not the width of the image.

[quote name='Acharis' timestamp='1349718991' post='4988059']
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...
[/quote]
I'm not using SDL.

Share this post


Link to post
Share on other sites
What version of SDL are [b]you[/b] using - and [i]where did you download it from[/i]? ([url="http://www.libsdl.org/"]hint[/url])

Share this post


Link to post
Share on other sites
[quote name='rip-off' timestamp='1349730036' post='4988118']
What version of SDL are [b]you[/b] using - and [i]where did you download it from[/i]? ([url="http://www.libsdl.org/"]hint[/url])
[/quote]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)

Share this post


Link to post
Share on other sites
[quote name='Brother Bob' timestamp='1349720279' post='4988066']
That is definitely an alignment issue. Solution is likely below.
[/quote]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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement