Jump to content
  • Advertisement
Sign in to follow this  
GogetaX

OpenGL PNGs

This topic is 3324 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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;

Share this post


Link to post
Share on other sites
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;

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!