Jump to content

  • Log In with Google      Sign In   
  • Create Account

We need your feedback on a survey! Each completed response supports our community and gives you a chance to win a $25 Amazon gift card!


Problem with SDL_LoadBMP & glDrawPixels


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 Acharis   Crossbones+   -  Reputation: 4015

Like
0Likes
Like

Posted 08 October 2012 - 11:19 AM

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.

Europe1300.eu - Historical Realistic Medieval Sim (RELEASED!)

PocketSpaceEmpire - turn based 4X with no micromanagement FB


Sponsor:

#2 Brother Bob   Moderators   -  Reputation: 8633

Like
1Likes
Like

Posted 08 October 2012 - 11:41 AM

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.

Edited by Brother Bob, 08 October 2012 - 11:44 AM.


#3 Acharis   Crossbones+   -  Reputation: 4015

Like
0Likes
Like

Posted 08 October 2012 - 11:56 AM

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...

Europe1300.eu - Historical Realistic Medieval Sim (RELEASED!)

PocketSpaceEmpire - turn based 4X with no micromanagement FB


#4 Brother Bob   Moderators   -  Reputation: 8633

Like
1Likes
Like

Posted 08 October 2012 - 12:17 PM

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.


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.

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.


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).

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.

#5 rip-off   Moderators   -  Reputation: 8764

Like
0Likes
Like

Posted 08 October 2012 - 03:00 PM

What version of SDL are you using - and where did you download it from? (hint)

#6 Acharis   Crossbones+   -  Reputation: 4015

Like
0Likes
Like

Posted 09 October 2012 - 02:19 AM

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)

Europe1300.eu - Historical Realistic Medieval Sim (RELEASED!)

PocketSpaceEmpire - turn based 4X with no micromanagement FB


#7 Acharis   Crossbones+   -  Reputation: 4015

Like
0Likes
Like

Posted 09 October 2012 - 03:32 AM

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.

Europe1300.eu - Historical Realistic Medieval Sim (RELEASED!)

PocketSpaceEmpire - turn based 4X with no micromanagement FB





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS