Date: Fri, 20 Aug 2004 11:16:46 +0900 (JST) Message-Id: <20040820.111646.132154322.garrigue at kurims.kyoto-u.ac.jp> To: andrieu at ijm.jussieu.fr Cc: seguso.forever at tin.it, lablgtk at kaba.or.jp Subject: Re: Loading a texture from file and applying it? From: Jacques GARRIGUE In-Reply-To: <20040819.120933.41629782.oandrieu at nerim.net> References: <200408181845.37672.seguso.forever at tin.it> <20040819.120933.41629782.oandrieu@nerim.net> Mime-Version: 1.0 Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit From: Olivier Andrieu > 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 > `---- > > 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. You can copy only once by using GlGtk.region_of_raw. I.e. your code would look like: let read_to_glpix myfile = let pix = GdkPixbuf.from_file myfile in let pix = if GdkPixbuf.get_has_alpha pix then pix else GdkPixbuf.add_alpha pix in let src = GdkPixbuf.get_pixels pix in let raw = Raw.create `ubyte ~len:(Gpointer.length src) in Gpointer.blit ~src ~dst:(GlGtk.region_of_raw raw); GlPix.of_raw raw ~format:`rgba ~width:(GdkPixbuf.get_width pix) ~height:(GdkPixbuf.get_height pix) (I didn't test it yet) The only actual copy occurs in blit. Of course, you must be sure that both share the same rgba representation. > 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? You have to read and write at the Raw.t level, by computing offsets by hand, one pixel being 4 bytes. --------------------------------------------------------------------------- Jacques Garrigue Kyoto University garrigue at kurims.kyoto-u.ac.jp JG