OpenGL texture loading example

I threw up a quick example for loading a image file as SDL_Surface
and converting it to an OpenGL texture. Its available at:

http://www.ards.net/Andreas/sdl_textures.html

Two things:

  • Currently supports only RGB (24bit) and RGBA (32bit) surfaces
  • I had to invert the alpha values, since somehow the alphas in my
    sample png image got inverted upon loading (is this a bug in SDL_Image?).

It would be nice if someone could comment on the alpha issue.

Hello Wordl,

Andreas Umbach wrote:

  • I had to invert the alpha values, since somehow the alphas in my
    sample png image got inverted upon loading (is this a bug in SDL_Image?).

It’s not a bug. IIRC, SDL maps alpha values from 0.0 (no alpha) to
1.0 (fully transparent). But OpenGL maps 0.0=fully transparent to 1.0=no
alpha.

                Franck.> 

It would be nice if someone could comment on the alpha issue.

Hello Wordl,

Andreas Umbach wrote:

  • I had to invert the alpha values, since somehow the alphas in my
    sample png image got inverted upon loading (is this a bug in SDL_Image?).

It’s not a bug. IIRC, SDL maps alpha values from 0.0 (no alpha) to
1.0 (fully transparent). But OpenGL maps 0.0=fully transparent to 1.0=no
alpha.

There is a function in SDL_image: IMG_InvertAlpha(1) which sets a flag
(only respected by the PNG loader) to invert the alpha channel for use
with OpenGL textures.

-Sam Lantinga				(slouken at devolution.com)

Lead Programmer, Loki Entertainment Software–
“Any sufficiently advanced bug is indistinguishable from a feature”
– Rich Kulawiec

“Andreas Umbach” wrote:

  • I had to invert the alpha values, since somehow the alphas in my
    sample png image got inverted upon loading (is this a bug in SDL_Image?).

It would be nice if someone could comment on the alpha issue.

It’s a slight difference between SDL interpretation of the alpha and the
normal interpretation of “alpha”. The alpha channel normally quantifies
the opacity of the pixel. Thus, a pixel with an alpha of 0 is
completely transparent and with an alpha of 255 is completely opaque.

But SDL interprets the alpha as quantifying the transparency of the
pixel, the opposite. I think the reason is that this is more general and
simple, as most images in 32 bits per pixel format with no alpha
information usually have the padding byte set to zero. The SDL
interpretation allows you to use a non-alpha image straightforwardly (it
would be completely transparent with the real meaning of “alpha”).

Personally, I prefer not making any assumption about the content of the
padding byte (it might contain garbage for example, for a non-alpha
image, and this would be valid). This implies filling the padding byte
myself, and filling it with 0xff or with 0x00 takes the same time, so I
use 0xff and the normal meaning of alpha can be used (ORing with
0xff000000 instead of ANDing with 0x00ffffff).–
Pierre Phaneuf
Ludus Design, http://ludusdesign.com/

There is a function in SDL_image: IMG_InvertAlpha(1) which sets a flag
(only respected by the PNG loader) to invert the alpha channel for use
with OpenGL textures.

Actually, if you use JPGs, and call glTexture2D with GL_RGB instead of
GL_RGBA, you can just ignore the alpha, and it works wonderfully. You really
don’t need an alpha inverter unless you’re planning on drawing
transparencies all over the damned thing.

-Sam Lantinga (slouken at devolution.com)

Nicholas

Nicholas Vining “While you’re out there struggling
vining at pacificcoast.net with your computer, I’m naked,
icq: 20872003 clueless, and feeling good!”
- Ratbert

----- Original Message -----
From: slouken@devolution.com (Sam Lantinga)
To: sdl at lokigames.com
Date: Thursday, January 27, 2000 8:47 AM
Subject: Re: [SDL] OpenGL texture loading example