comp.lang.ada
 help / color / mirror / Atom feed
* AdaSDL and glTexImage2D
@ 2003-01-29 10:09 Stefan Soos
  2003-01-29 20:15 ` Chad R. Meiners
  0 siblings, 1 reply; 3+ messages in thread
From: Stefan Soos @ 2003-01-29 10:09 UTC (permalink / raw)



Hello,

I'm doing some programming in Ada with the thin binding to the SDL
library. I'd like to create some textured objects, but there I run
into some problems. My texture data is stored in an SDL surface:

Image : SDL.Video.Surface_ptr;

The actual data can be read from Image.Pixels. Now, I'd like to call
glTexImage2D:
         glTexImage2D (GL_TEXTURE_2D,
                       0,
                       GLint (GL_RGBA),
                       GLsizei (Width), GLsizei (Height),
                       0,
                       GL_RGBA,
                       GL_UNSIGNED_BYTE,
                       GLubyte_Array (Image.Pixels));
but this doesn't work. How can I convert Image.Pixels (wich is
actually defined as System.Address) to an array of GLubyte?

Thanks in advance and bye,
Stefan

-- 
include/asm-mips64/unistd.h:    
/* These are here for sake of fucking
lusercode living in the fucking believe
having to fuck around with the syscall interface themselfes.  */



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: AdaSDL and glTexImage2D
  2003-01-29 10:09 AdaSDL and glTexImage2D Stefan Soos
@ 2003-01-29 20:15 ` Chad R. Meiners
  2003-01-30  9:52   ` Stefan Soos
  0 siblings, 1 reply; 3+ messages in thread
From: Chad R. Meiners @ 2003-01-29 20:15 UTC (permalink / raw)



"Stefan Soos" <stefan.soos@gmx.de> wrote in message
news:49981b.cu.ln@ID-soos.user.dfncis.de...
>
> Hello,
>
> I'm doing some programming in Ada with the thin binding to the SDL
> library. I'd like to create some textured objects, but there I run
> into some problems. My texture data is stored in an SDL surface:
>
> Image : SDL.Video.Surface_ptr;
>
> The actual data can be read from Image.Pixels. Now, I'd like to call
> glTexImage2D:
>          glTexImage2D (GL_TEXTURE_2D,
>                        0,
>                        GLint (GL_RGBA),
>                        GLsizei (Width), GLsizei (Height),
>                        0,
>                        GL_RGBA,
>                        GL_UNSIGNED_BYTE,
>                        GLubyte_Array (Image.Pixels));
> but this doesn't work. How can I convert Image.Pixels (wich is
> actually defined as System.Address) to an array of GLubyte?

Well what you want to do is bit more complicated than what a simple type
conversion function can do for you automatically.
Image.Pixels is of type System.address.  First System.Address doesn't have
any bounds information, but there is hope because Ada gives you a very nice
package called System.Address_To_Access_Conversions.  This package is IMHO a
very cool package because it lets you transform System.Address into any type
you would like.  However invoking it in this case will be a little bit
tricky since your array will need bounds information added to it.  I don't
have immediate access to the GL libraries so I can't give you the exact code
to solve your problem, but I can point you to an example in the AdaSDL thick
binding where a similar conversion takes place.

In AdaSDL-Video-Drawing.adb around line 273 you will find:
            declare
               subtype Current_Screen is Screen_Eight(1..Array_Size);
               package Convert is new  System.Address_To_Access_Conversions
                 (Current_Screen);
            begin
               Eight_Bit_Draw
                 ( On => Convert.To_Pointer (Item.Item.Pixels).all,  -- The
important conversion
                   Colors => To_Palette_Provider (Item),
                   Coordinates => Coords);
            end;

Here is a example of converting the Image.Pixels (in this case
Item.Item.Pixels ;) into an access to an array of size Array_Size.  Notice
that we had to declare a subtype of the Screen_Eight array with a specified
range so that the compiler can reconstruct the range information.

Now I am guessing that GLubyte_Array is an array of bytes.  So for your code
your solution might look like this

declare
    Bytes_Per_Pixel : constant := 4;  -- I believe RGBA is 32 bit color, but
if I am wrong in your case change this value
    subtype Current_Buffer is
GLubyte_Array(0..(Width*Height*Bytes_Per_Pixel)-1);
    package Convert is new
System.Address_To_Access_Conversions(Current_Buffer);
begin
          glTexImage2D (GL_TEXTURE_2D,
                        0,
                        GLint (GL_RGBA),
                        GLsizei (Width), GLsizei (Height),
                        0,
                        GL_RGBA,
                        GL_UNSIGNED_BYTE,
                        Convert.To_Pointer (Image.Pixels).all);
end;

Now remember this might need some tinkering to get it right since I don't
have the packages to test this out.

I hope this solves your problem,

Chad R. Meiners





^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: AdaSDL and glTexImage2D
  2003-01-29 20:15 ` Chad R. Meiners
@ 2003-01-30  9:52   ` Stefan Soos
  0 siblings, 0 replies; 3+ messages in thread
From: Stefan Soos @ 2003-01-30  9:52 UTC (permalink / raw)


Hello,

On Wed, 29 Jan 2003 15:15:44 -0500, Chad R. Meiners wrote:
> In AdaSDL-Video-Drawing.adb around line 273 you will find:
>             declare
>                subtype Current_Screen is Screen_Eight(1..Array_Size);
>                package Convert is new  System.Address_To_Access_Conversions
>                  (Current_Screen);
>             begin
>                Eight_Bit_Draw
>                  ( On => Convert.To_Pointer (Item.Item.Pixels).all,  -- The
> important conversion
>                    Colors => To_Palette_Provider (Item),
>                    Coordinates => Coords);
>             end;
> 
> Here is a example of converting the Image.Pixels (in this case
> Item.Item.Pixels ;) into an access to an array of size Array_Size.  Notice
> that we had to declare a subtype of the Screen_Eight array with a specified
> range so that the compiler can reconstruct the range information.

I've only looked at the thin binding in adsdl.ad[bs] and
adsdl_framebuf and there he's doing some tricky stuff with
SDL.Types.Uint[8|16|32]_Ptrs. I've seen the package
Address_To_Access_Conversions in varios sources before but I didn't
know what it was good for ;)


> Now I am guessing that GLubyte_Array is an array of bytes.  So for your code
> your solution might look like this
> 
> declare
>     Bytes_Per_Pixel : constant := 4;  -- I believe RGBA is 32 bit color, but
> if I am wrong in your case change this value
>     subtype Current_Buffer is
> GLubyte_Array(0..(Width*Height*Bytes_Per_Pixel)-1);
>     package Convert is new
> System.Address_To_Access_Conversions(Current_Buffer);
> begin
>           glTexImage2D (GL_TEXTURE_2D,
>                         0,
>                         GLint (GL_RGBA),
>                         GLsizei (Width), GLsizei (Height),
>                         0,
>                         GL_RGBA,
>                         GL_UNSIGNED_BYTE,
>                         Convert.To_Pointer (Image.Pixels).all);
> end;

That's exactly how it works. Thank you very much.
The Bytes_Per_Pixel information is stored in Image.format.BytesPerPixel.

> Now remember this might need some tinkering to get it right since I don't
> have the packages to test this out.

There was a little bit of tinkering because I have to do some color
conversion and flipping of the textures but your code worked just
right out of the box ;) Again, thank you so much.


> I hope this solves your problem,
> 
> Chad R. Meiners

Indeed, it did. Thanks and bye,
Stefan


-- 




^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2003-01-30  9:52 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-01-29 10:09 AdaSDL and glTexImage2D Stefan Soos
2003-01-29 20:15 ` Chad R. Meiners
2003-01-30  9:52   ` Stefan Soos

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox