From: Maurizio Colucci To: Olivier Andrieu , lablgtk at kaba.or.jp Subject: Re: Loading a texture from file and applying it? Date: Thu, 19 Aug 2004 19:34:19 +0200 References: <200408181845.37672.seguso.forever at tin.it> <20040819.120933.41629782.oandrieu at nerim.net> In-Reply-To: <20040819.120933.41629782.oandrieu at nerim.net> MIME-Version: 1.0 Content-Disposition: inline Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Message-Id: <200408191934.19330.seguso.forever at tin.it> 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? 2. Do you see other errors? 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? 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? > But that will copy the pixel data twice: once from the pixbuf into a > caml string then from the caml string into the raw array, so it's not > as efficient as it could be. That's not important because texture creation is done on initialization. > > It's possible to share the pixel data between the pixbuf and the raw > array but it's a bit messy (breaks encapsulation). If you're > interested, I can send you the code to do this. > > > The GlPix should have rgba format, and the png file may not have > > alpha information. > > Use GdkPixbuf.get_has_alpha and GdkPixbuf.add_alpha . Thanks for your patience :-) -- Maurizio Colucci http://logicaldesktop.sourceforge.net