PNG to OpenGL Texture (libpng)


#1

Hi,
I’m trying to load a PNG file into an OpenGL texture using
ruby-libpng-0.3.3
I can’t quite figure out how to convert libpng data to the correct
format.
I’ve dumped png.read_image to screen and it looks like my picture (with
numbers instead of colors). At the moment I’m trying to use this index
as my Red index (just to get something displaying) but am getting a
white square instead. I’m already using textures for fonts, so I know
that bit works.

Any Ideas?

As an aside, if anyone has a better x-platform way of doing this I’m all
ears…

  • Xavier

Load a test png

rpng = Reader.new(‘res/img/test.png’);

width, height, bit_depth, color_type,
interlace_type, compression_type, filter_type = rpng.get_IHDR

puts width # 20#for i in (0…height)

buffer += data[i].pack(“C*”)

#end
puts height # 20
puts bit_depth # 8
puts color_type # 6 … not sure what this means yet
#for i in (0…height)

buffer += data[i].pack(“C*”)

#end

Create and bind to OGL texture

@testogl = GL::GenTextures(1)[0];
GL.BindTexture(GL::TEXTURE_2D, @testogl);
GL.TexParameteri(GL::TEXTURE_2D, GL::TEXTURE_WRAP_S, GL::CLAMP);
GL.TexParameteri(GL::TEXTURE_2D, GL::TEXTURE_WRAP_T, GL::CLAMP);
GL.TexParameteri(GL::TEXTURE_2D, GL::TEXTURE_MAG_FILTER,GL::LINEAR);
GL.TexParameteri(GL::TEXTURE_2D, GL::TEXTURE_MIN_FILTER,GL::LINEAR);

Abomination follows

data = rpng.read_image
data2 = data.flatten
data3 = “”
for i in (0…data2.length-1)
data3 += " "
data3[i] = data2[i]
end

GL.TexImage2D(GL::TEXTURE_2D, 0, GL::RED, width,
height, 0, GL::RED, GL::UNSIGNED_BYTE, data);


#2

Hi,

On 5/29/06, Xavier removed_email_address@domain.invalid wrote:

Any Ideas?

As an aside, if anyone has a better x-platform way of doing this I’m all
ears…

  • Xavier

With RMagick you can load image data in a
suitable format for gl textures like this
(dunno if there’s a better way):

require ‘RMagick’

image = RMagick::Image.read(filename).first
rgba_data = image.to_blob{|i|
i.format = “RGBA”
i.depth = 8
}

And with Imlib2:

require ‘imlib2’

image = Imlib2::Image.load(filename)
data = image.data # IIRC the channel order depends on system endianness
here

No idea about libpng, sorry :I

HTH,
Ilmari