Re:TBitmap -> OpenGL Texture
Here's something I just cooked up this morning for my own use and it
seems to work okay. First you convert the bitmap then swap the red and
blue bytes of the pixels. Don't forget to free the memory after you're
done.
I'm using a 32 bit pixel so I can do transparency but you can change it
back to 24 bit pretty easily. Let me know if you spot any problems.
Can anybody tell me how to make certain colors in the texture
transparent in OpenGL?
Mitch Wolberg,
RockWare, Inc.
function PointerFromTBitmap ( bmp : TBitmap ) : Pointer ;
var
bi : TBitmapInfo ;
ptr : Pointer ;
begin
result := nil ;
bi.bmiHeader.biWidth := bmp.width ;
bi.bmiHeader.biHeight := bmp.height ;
bi.bmiHeader.biPlanes := 1 ;
bi.bmiHeader.biBitCount := 32 ; //24
bi.bmiHeader.biCompression := BI_RGB ;
bi.bmiHeader.biSizeImage := 0 ;
bi.bmiHeader.biXPel{*word*237}eter := 0 ;
bi.bmiHeader.biYPel{*word*237}eter := 0 ;
bi.bmiHeader.biClrUsed := 0 ;
bi.bmiHeader.biClrImportant := 0 ;
bi.bmiHeader.biSize := SizeOf ( TBitmapInfoHeader ) ;
GetMem ( ptr , bmp.width * bmp.height * 4 * 4 ) ; //24: 3 * 3
if GetDIBits ( Application.MainForm.Canvas.Handle ,
bmp.Handle ,
0 ,
bmp.Height ,
ptr ,
bi ,
DIB_RGB_COLORS ) <> 0 then
result := ptr
else //error
FreeMem ( ptr ) ;
end ;
procedure FlipRGBBytes ( PPixels : Pointer ; Size : Integer ) ;
type
TRGB = packed record
r , g , b , a : byte ; //: remove the a
end ;
TPixels = array [0..0] of TRGB ;
var
i : Integer ;
t : Byte ;
begin
{$R-} //swap red and green bytes
for i := 0 to size - 1 do
with TPixels(PPixels^)[i] do begin
t := r ;
r := b ;
b := t ;
//24: remove this logic - it sets the white pixels to transparent
if ( r <> 255 ) or ( g <> 255 ) or ( b <> 255 ) then a := 1
else a := 0 ;
end ;
{ $R+}
end ;
Quote
technomage wrote:
> Hi
> Being new to OpenGL, I wanted to know if it is possible to go from a TBitmap
> (or TBitmap descendent) to an OpenGL Texture? Most of the code I've seen has
> used Windows API Calls rather than the VCL.
> Can anyone shed any light on this?
> Technomage
> -----------------------------------------------------
> technom...@delphigamer.com
> http://www.delphigamer.com
> -----------------------------------------------------
> Support Project JEDI
> http://www.delphi-jedi.org
> -----------------------------------------------------