On Mon, Jul 13, 2009 at 10:23 PM, Elliott Slaughter <
elliottslaugh...@gmail.com> wrote:

> On Mon, Jul 13, 2009 at 10:00 PM, Elliott Slaughter <
> elliottslaugh...@gmail.com> wrote:
>
>> The information I get after using load-and-convert-image seems
>> contradictory:
>>
>> bytes-per-pixel 4
>> rmask 0x00FF0000
>> gmask 0x0000FF00
>> bmask 0x000000FF
>> amask 0x00000000
>>
>> Why is the alpha mask 0 if it is 4 bytes per pixel?
>>
>> If I try load-and-convert-image on my png image, SDL still switches to
>> BGRA ordering, but if I am careful, I can get the image to display properly
>> (since it keeps the alpha channel). The pastebin code above gives me a blue
>> image instead of a red one here :-)
>>
>
> Having thought about it a little more, here is my guess:
>
> SDL seems want the image to be BGRA, even though the source didn't have an
> alpha channel. So it adds an alpha channel and leaves a mask of 0. When I
> put the image into Open GL, 0 alpha is interpreted as transparent.
>
> When I hard code BGR ordering and 3 bytes per pixel, I do get something
> drawn on the screen (but it's just a jumble of color). So maybe all of the
> data is there, it just has this empty alpha byte for every pixel?
>
> Now how would I test this/fix it?


I got it!

After reading some old mailing list archives [1], I got the idea of blitting
to a 32 bit surface to get the right format.

Here is my code (which works for every image I have tested so far, including
the palleted gif image and the BGR ordered bmp):

(defun load-and-convert-image (source)
  (assert (probe-file source))
  (let* ((image (sdl-image:load-image source))
         (w (sdl:width image)) (h (sdl:height image))
         (surface (sdl:create-surface  w h :bpp 32 :pixel-alpha t)))
    (sdl:blit-surface image surface)))

(defun load-image-to-texture (source)
  (assert (probe-file source))
  (let* ((texture (car (gl:gen-textures 1)))
         (surface (load-and-convert-image source))
         (w (sdl:width surface)) (h (sdl:height surface)))
    (gl:bind-texture :texture-2d texture)
    (gl:tex-parameter :texture-2d :texture-min-filter :nearest)
    (gl:tex-parameter :texture-2d :texture-mag-filter :nearest)
    (gl:tex-image-2d
       :texture-2d 0 :rgba w h 0 :rgba :unsigned-byte
       (sdl-base::with-pixel (pixels (sdl:fp surface))
         (sdl-base::pixel-data pixels)))
    texture))

[1] http://twomix.devolution.com/pipermail/sdl/2002-September/049065.html

-- 
Elliott Slaughter

"Don't worry about what anybody else is going to do. The best way to predict
the future is to invent it." - Alan Kay
_______________________________________________
application-builder mailing list
application-builder@lispniks.com
http://www.lispniks.com/mailman/listinfo/application-builder

Reply via email to