Reversed color channels (BGR instead of RGB) Topic is solved

Problems building or running the engine, queries about how to use features etc.
Post Reply
PauliusLiekis
Gnoblar
Posts: 7
Joined: Tue Aug 22, 2017 7:46 am

Reversed color channels (BGR instead of RGB)

Post by PauliusLiekis »

Hi,

I have a problem, that when I load a texture from png I can see that channels are reversed, i.e. rendered colors are BGR instead of RGB.

I just do this:

Code: Select all

Ogre::TextureManager::getSingleton().load(name, Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
I see this in the console:

Code: Select all

Texture: UserMarker.png: Loading 1 faces(PF_A8B8G8R8,128x128x1) with 7 hardware generated mipmaps from Image. Internal format is PF_A8B8G8R8,128x128x1.
which seems to support my theory. Loading jpg (i.e. without alpha channel) works file - it is loaded and rendered as RGB.


How can I solve this problem? Is there some setting that I need to set on ResourceManagers or something like that?
PauliusLiekis
Gnoblar
Posts: 7
Joined: Tue Aug 22, 2017 7:46 am

Re: Reversed color channels (BGR instead of RGB)

Post by PauliusLiekis »

I made some more tests and here are the results:

OSX:

Code: Select all

Texture: Sunny2_front.png: Loading 1 faces(PF_A8B8G8R8,1024x1024x1) with 10 hardware generated mipmaps from Image. 
Internal format is PF_A8R8G8B8,1024x1024x1.

Texture: UserMarker.png: Loading 1 faces(PF_A8B8G8R8,88x110x1) with 6 hardware generated mipmaps from Image. 
Internal format is PF_A8R8G8B8,88x110x1.

Texture: UserMarker1.jpg: Loading 1 faces(PF_B8G8R8,88x110x1) with 6 hardware generated mipmaps from Image. 
Internal format is PF_R8G8B8,88x110x1.

Texture: 1__MainTex: Loading 1 faces(PF_B8G8R8,256x256x1) 
Internal format is PF_R8G8B8,256x256x1.
iOS:

Code: Select all

Texture: Sunny2_down.png: Loading 1 faces(PF_A8B8G8R8,128x128x1) with 7 hardware generated mipmaps from Image. 
Internal format is [b]PF_A8B8G8R8[/b],128x128x1.

Texture: UserMarker.png: Loading 1 faces(PF_A8B8G8R8,128x128x1) with 7 hardware generated mipmaps from Image. 
Internal format is [b]PF_A8B8G8R8[/b],128x128x1.

Texture: UserMarker1.jpg: Loading 1 faces(PF_B8G8R8,128x128x1) with 7 hardware generated mipmaps from Image. 
Internal format is PF_R8G8B8,128x128x1.

Texture: 1__MainTex: Loading 1 faces(PF_B8G8R8,256x256x1) 
Internal format is PF_R8G8B8,256x256x1.
As you can see png textures are loaded as ABGR (instead of ARGB) on iOS, i.e. blue textures become yellow. Jpg textures load fine. Can anyone tell me why and how to solve this?
PauliusLiekis
Gnoblar
Posts: 7
Joined: Tue Aug 22, 2017 7:46 am

Re: Reversed color channels (BGR instead of RGB)

Post by PauliusLiekis »

I attached how source image looks and how it looks rendered on screen. You can see that light blue is rendered as yellow on iOS. It works fine on OSX. Any ideas?
Attachments
Result
Result
Source image
Source image
paroj
OGRE Team Member
OGRE Team Member
Posts: 1994
Joined: Sun Mar 30, 2014 2:51 pm
x 1074
Contact:

Re: Reversed color channels (BGR instead of RGB)

Post by paroj »

take a look at the ios ifdefs here:
https://github.com/OGRECave/ogre/blob/m ... t.cpp#L139
and here
https://github.com/OGRECave/ogre/blob/m ... er.cpp#L97

I was already suspicious regarding them, but there were no bugreports yet.
niparx
Gnoblar
Posts: 7
Joined: Thu Apr 27, 2017 3:41 pm
Location: Russia/Lithuania
x 1

Re: Reversed color channels (BGR instead of RGB)

Post by niparx »

paroj wrote:take a look at the ios ifdefs here:
https://github.com/OGRECave/ogre/blob/m ... t.cpp#L139
and here
https://github.com/OGRECave/ogre/blob/m ... er.cpp#L97

I was already suspicious regarding them, but there were no bugreports yet.
Hi,
Basically, You was almost right. The cause was some lines below:
https://github.com/OGRECave/ogre/blob/m ... t.cpp#L151

I don't know, may be I am wrong, but that is my thoughts.
So, we are loading png image. OgreSTBICodec is used for that. And at some point it set image format, which was decoded:

Code: Select all

imgData->format = PF_BYTE_RGBA;
https://github.com/OGRECave/ogre/blob/m ... c.cpp#L192

We are on iOS, so it is small endian:

Code: Select all

PF_BYTE_RGBA = PF_A8B8G8R8
https://github.com/OGRECave/ogre/blob/m ... mat.h#L111

At some point engine want to do OpenGL call. It must use OpenGL constants for that, so, need to convert Ogres PixelFormat to some OpenGL value.
mFormat is PF_A8B8G8R8:

Code: Select all

GLenum format = GLES2PixelUtil::getGLOriginFormat(mFormat);
https://github.com/OGRECave/ogre/blob/m ... e.cpp#L164

And there we find

Code: Select all

getGLOriginFormat(...)
...
return GL_BGRA_EXT;
https://github.com/OGRECave/ogre/blob/m ... er.cpp#L97

So, we have a mismatch after conversion: original format (in OpenGL terms RGBA) -> Ogre format -> originl format (in OpenGL terms BGRA)
After I have changed return value (GL_BGRA_EXT -> GL_RGBA), png files were loaded correctly.

I am not an Ogre expert, so, it might be, that it is not a solution. It would be nice to hear some comments from Ogre experts regarding this issue and possible solution.

p.s. I wonder, how such issues was not raised before. It looks like png images were not displaying correctly on iOS at all. Same is for Ogre SampleBrowser: same issue. So, nobody uses Ogre on iOS? Or it is used on iOS, but without png? :)
Attachments
See Ogre logo: it should be yellow
See Ogre logo: it should be yellow
Last edited by niparx on Wed Sep 27, 2017 1:55 am, edited 1 time in total.
niparx
Gnoblar
Posts: 7
Joined: Thu Apr 27, 2017 3:41 pm
Location: Russia/Lithuania
x 1

Re: Reversed color channels (BGR instead of RGB)

Post by niparx »

Nickak2003
Goblin
Posts: 272
Joined: Thu Jun 10, 2004 4:19 am
x 26

Re: Reversed color channels (BGR instead of RGB)

Post by Nickak2003 »

Still doesn't seem to be fixled on ogl renderer! 1-10-9 win64.
Post Reply