Date: Fri, 20 Aug 2004 14:00:44 +0200 (CEST) Message-Id: <20040820.140044.59461890.andrieu at ijm.jussieu.fr> To: seguso.forever at tin.it Cc: lablgtk at kaba.or.jp Subject: Re: Loading a texture from file and applying it? From: Olivier Andrieu In-Reply-To: <200408191934.19330.seguso.forever at tin.it> References: <200408181845.37672.seguso.forever at tin.it> <20040819.120933.41629782.oandrieu@nerim.net> <200408191934.19330.seguso.forever@tin.it> Mime-Version: 1.0 Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit Maurizio Colucci [Thu, 19 Aug 2004]: > On Thursday 19 August 2004 12:09, Olivier Andrieu wrote: > > Maurizio Colucci [Wed, 18 Aug 2004]: > > > Hello again, > > > > > > In lablgl + lablgtk, I need to load a texture from file and apply it to > > > a triangle. > > > > > > More precisely: GlTex.image2d wants a byte array as a GlPix. I need > > > to know how to create a GlPix from a png file (or similar). > > > > You can use GdkPixbuf: it will load the png file and convert it > > into a "pixel buffer" structure. You then need to convert this > > into a Raw array. > > > > The easiest way is to do this: > > ,---- > > | let raw_of_pixbuf pb kind = > > | Raw.of_string (Gpointer.string_of_region (GdkPixbuf.get_pixels pb)) > > | ~kind > > `---- > > > > Thank you very much Olivier. I was looking at ocamlimages, so your hint was > very useful. I still have some doubt though. > > This is the code I have managed to write so far: > > let pb = GdkPixbuf.from_file "avatar1giu.bmp" in > let wt = GdkPixbuf.get_width pb in > let ht = GdkPixbuf.get_height pb in > let wt2 = round wt in > let ht2 = round ht in > let pb_sc = GdkPixbuf.create ~width:wt2 ~height:ht2 > ~bits:8 (* 8 bits per sample * 4 samples=32 bits *) > ~has_alpha:true () in > GdkPixbuf.scale ~dest:pb_sc ~width:wt2 ~height:ht2 pb; > let raw_of_pixbuf pb kind = > Raw.of_string (Gpointer.string_of_region > (GdkPixbuf.get_pixels pb)) ~kind in > let raw = raw_of_pixbuf pb_sc ??? in > > > 1. As you can see, I don't know what to pass as kind. Could you please tell > me? `ubyte should be ok I guess; it doesn't really matter unless you want to read/write the pixels in the raw array. > 3. What do I do with this raw array? At the end, I need a GlPix.t to pass to > GlTex.image_2d. Must I use GlPix.of_raw? most probably > 4. Before calling GlTex.image_2d, I have to set the alpha value of > my image manually, pixel per pixel (to set up color key). What can > I use to read and write the red, green, blue and alpha values? If what you have to do is make a certain color transparent, that's easy to do with the gdkpixbuf. GdkPixbuf.add_alpha has an optional argument that specify a color to be made transparent. -- Olivier