Texture channel format order

I have looked and don’t see anything in the SDL_Surface struct that allows
the SDL image loader lib to store information on the color channel order.
e.g. RGBA or BGRA. This or the file extension would be nice to have saved
with the surface since SDL already had to load this data once already. Any
thoughts or ideas if this has been asked or even if it’s doable?

Thanks

Dave

Hello,

Usually I need to flip bitmaps vertically (OpenGL’s texture coordinate
origin is bottom left, and that’s the first pixel of the image data)
anyway, so even if SDL automatically converted to an easily loadable
channel format (like GL_RGB or GL_RGBA), there would still be extra
processing required before the bitmap was loadable by OpenGL.

What I do is convert to 24 bit RGB or 32 bit RGBA using
SDL_ConvertSurface, then manually flip the rows prior to calling
glTexImage2D.

Hope this helps.

Peter

2008/5/15 mars999 :> I have looked and don’t see anything in the SDL_Surface struct that allows

the SDL image loader lib to store information on the color channel order.
e.g. RGBA or BGRA. This or the file extension would be nice to have saved
with the surface since SDL already had to load this data once already. Any
thoughts or ideas if this has been asked or even if it’s doable?

Thanks

Dave


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

This is more for setting the GL_RGB or GL_BGR format when you create your
texture objects. Reason I say ask this for is, with IMG_Load you can load
some many different types and .tga and .bmp IIRC are in BGR format where
.png, .tif, .jpg are in RGB? I as of now am check the extension to set this
parameter, but figured since the lib already has to load this texture file
why not store the extension or color order so all one has to do is

e.g.
SDL_Surface* texture->colorOrder;
texture->colorOrder would have SDL_RGB(A) or SDL_BGR(A)> ----- Original Message -----

From: sdl-bounces@lists.libsdl.org [mailto:sdl-bounces at lists.libsdl.org] On
Behalf Of Peter Mackay
Sent: Thursday, May 15, 2008 1:28 AM
To: A list for developers using the SDL library. (includes SDL-announce)
Subject: Re: [SDL] Texture channel format order

Hello,

Usually I need to flip bitmaps vertically (OpenGL’s texture coordinate
origin is bottom left, and that’s the first pixel of the image data)
anyway, so even if SDL automatically converted to an easily loadable
channel format (like GL_RGB or GL_RGBA), there would still be extra
processing required before the bitmap was loadable by OpenGL.

What I do is convert to 24 bit RGB or 32 bit RGBA using
SDL_ConvertSurface, then manually flip the rows prior to calling
glTexImage2D.

Hope this helps.

Peter

2008/5/15 mars999 <@Mars_999>:

I have looked and don’t see anything in the SDL_Surface struct that allows
the SDL image loader lib to store information on the color channel order.
e.g. RGBA or BGRA. This or the file extension would be nice to have saved
with the surface since SDL already had to load this data once already. Any
thoughts or ideas if this has been asked or even if it’s doable?

Thanks

Dave


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

mars999 wrote:

I have looked and don’t see anything in the SDL_Surface struct that
allows the SDL image loader lib to store information on the color
channel order. e.g. RGBA or BGRA. This or the file extension would be
nice to have saved with the surface since SDL already had to load this
data once already. Any thoughts or ideas if this has been asked or
even if it’s doable?

Check surface->format->Rmask, Gmask, etc.
(http://www.libsdl.org/cgi/docwiki.cgi/SDL_PixelFormat)

Peter Mackay wrote:

Usually I need to flip bitmaps vertically (OpenGL’s texture coordinate
origin is bottom left, and that’s the first pixel of the image data)
anyway, so even if SDL automatically converted to an easily loadable
channel format (like GL_RGB or GL_RGBA), there would still be extra
processing required before the bitmap was loadable by OpenGL.

Maybe I’m missing something, but I just don’t get why people often think
they need to flip images before uploading them to GL textures, because
"the origin is at the bottom". That’s true (by default) in vertex space,
but there is no “up” and “down” in texture space. You just choose the
texture coordinates of your vertices in such a way that the picture
appears oriented correctly.

Examples for both things in my code (i.e. how I upload SDL_Surfaces
loaded by SDL_image to GL textures without any conversion, if possible):
functions getImageData() and feedImageToGL() in
http://pipmak.svn.sourceforge.net/viewvc/pipmak/trunk/pipmak/source/images.c?view=markup.

-Christian

El Jueves 15 Mayo 2008ES 13:17:02 Christian Walther escribi?:

Maybe I’m missing something, but I just don’t get why people often think
they need to flip images before uploading them to GL textures, because
"the origin is at the bottom". That’s true (by default) in vertex space,
but there is no “up” and “down” in texture space. You just choose the
texture coordinates of your vertices in such a way that the picture
appears oriented correctly.

I think it is done so because most 3D modelling packages also define the
origin of UV texture coordinates at the bottom, so to have the correct thing
you could:

1- Do your texturing and, when finished, flip the UV set on your modelling
application so your program hasn’t to do any special. The disadvantage is
that every viewer/modeller out there except your program will display your
model textures flipped.

2- Export as usual, but invert the texture coordinates internally in your
program. That seems to me as good as invert the image.

3- Set the texture matrix with a -1 scale on the vertical axis. This seems to
be the cleanest way, but I have read somewhere that usually OpenGL drivers
are optimized to not perform any transformation on the UV coordinates if the
texture matrix is the identity, so if it existed would carry a small
performance overhead since GL would have to perform the matrix
multiplication.

4- Special fragment shader that flips coordinates sent to it… an overkill :slight_smile:

If I remember correctly, the glTexImage2D manual page also specifies
that the first element of texture data is the lower left corner.

But yes, I agree that flipping UVs would achieve the same effect, and
probably with a lot less data crunching!

Peter

2008/5/15 Alberto Luaces :> El Jueves 15 Mayo 2008ES 13:17:02 Christian Walther escribi?:

Maybe I’m missing something, but I just don’t get why people often think
they need to flip images before uploading them to GL textures, because
"the origin is at the bottom". That’s true (by default) in vertex space,
but there is no “up” and “down” in texture space. You just choose the
texture coordinates of your vertices in such a way that the picture
appears oriented correctly.

I think it is done so because most 3D modelling packages also define the
origin of UV texture coordinates at the bottom, so to have the correct thing
you could:

1- Do your texturing and, when finished, flip the UV set on your modelling
application so your program hasn’t to do any special. The disadvantage is
that every viewer/modeller out there except your program will display your
model textures flipped.

2- Export as usual, but invert the texture coordinates internally in your
program. That seems to me as good as invert the image.

3- Set the texture matrix with a -1 scale on the vertical axis. This seems to
be the cleanest way, but I have read somewhere that usually OpenGL drivers
are optimized to not perform any transformation on the UV coordinates if the
texture matrix is the identity, so if it existed would carry a small
performance overhead since GL would have to perform the matrix
multiplication.

4- Special fragment shader that flips coordinates sent to it… an overkill :slight_smile:


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Peter Mackay wrote:

If I remember correctly, the glTexImage2D manual page also specifies
that the first element of texture data is the lower left corner.

Hmm, you’re right. I fully admit not being an OpenGL expert, but that
just seems misleading to me. What it should specify is that the first
element is the s = t = 0 corner - whether that is lower left or not is
up to the user to define.

-Christian