PNGs

Started by
1 comment, last by Dunge 14 years, 7 months ago
Hey all, i did some research of how to load png files into an GL engine. and so far i found the GraphicEx that helps me with that, i was able to load the png file to the memory, convert it into the bmp image and only then load it. all goes good except the transparency part of the png file.. by converting the png to bmp, i was unable to use the transparency of the image, does i missing something? im loading pngs via TPicture class.(delphi) is there any other way to get the png into the OpenGL engine without using the bmp converting? my LoadPngTexture code: function CreateTexture(Width, Height, Format : Word; pData : Pointer) : GlUint; var Texture : GLuint; begin glGenTextures(1, Texture); glBindTexture(GL_TEXTURE_2D, Texture); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); if Format = GL_RGBA then gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, Width, Height, GL_RGBA, GL_UNSIGNED_BYTE, pData) else gluBuild2DMipmaps(GL_TEXTURE_2D, 3, Width, Height, GL_RGB, GL_UNSIGNED_BYTE, pData); result :=Texture; end; function LoadPNGTexture(Filename: String; var Texture: GLuint; LoadFromResource : Boolean): Boolean; var Png:TPicture; Data : Array of LongWord; W : Integer; H : Integer; BMP : TBitmap; C : LongWord; Line : ^LongWord; begin try png := TPicture.Create; png.LoadFromFile(FileName); BMP:=TBitmap.Create; BMP.pixelformat:=pf32bit; BMP.width:=png.width; BMP.height:=png.height; BMP.canvas.draw(0,0,Png.Bitmap); SetLength(Data, Png.Width*Png.Height); BMP.AlphaFormat := afPremultiplied; BMP.Transparent := True; BMP.TransparentColor := RGB(0,0,0); BMP.TransparentMode := tmAuto; For H:=0 to Png.Height-1 do Begin Line :=BMP.scanline[Png.Height-H-1]; For W:=0 to Png.Width-1 do Begin c:=Line^ and $FFFFFF; Data[W+(H*Png.Width)] :=(((c and $FF) shl 16)+(c shr 16)+(c and $FF00)) or $FF000000; inc(Line); End; End; ImgWidth := Png.Width; ImgHeight := Png.Height; Texture := CreateTexture(Png.Width,Png.Height,GL_RGBA,Addr(Data[0]) ); Finally if bmp <> nil then FreeAndnIl(Bmp); if Png <> nil then FreeAndNil(Png); Result := True; end; end;
Rakion Hacks, Downloads, videos and more!http://gplaces.net/spec/rakion.php
Advertisement
oh, and.. almost forgot, this is how i using the transparency code:

if A2Button[a].Transparency = True then begin
glDisable (GL_ALPHA_TEST);
glEnable (GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
end;

glBindTexture(GL_TEXTURE_2D,A2Button[a].Img1.Address);
glColor(1.0,1,1);
width := A2Button[a].Img1.Width;
Height := A2Button[a].Img1.Height;


glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex3f(-width, height, 0.0);
glTexCoord2f(1, 0); glVertex3f( width, height, 0.0);
glTexCoord2f(1, -1);glVertex3f( width,-height, 0.0);
glTexCoord2f(0, -1); glVertex3f(-width,-height, 0.0);
glEnd();

if A2Button[a].Transparency = True then begin
glDisable(GL_BLEND);
glEnable(GL_ALPHA_TEST);
end;
Rakion Hacks, Downloads, videos and more!http://gplaces.net/spec/rakion.php
There's no transparency in a BMP file, so of course you are missing something :).

Click me

This topic is closed to new replies.

Advertisement