Board index » delphi » TBitmap -> OpenGL Texture

TBitmap -> OpenGL Texture

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
-----------------------------------------------------

 

Re:TBitmap -> OpenGL Texture


Check the latest GLScene release (WIP4), it now uses VCL to go from TGraphic
to OpenGL texture (TBitmap is a TGraphic).

http://glscene.org

Eric Grange

Re:TBitmap -> OpenGL Texture


In my book "Delphi Deveopers' Guide to OpenGL" (see link) I build textures
from 24-bit bmp files by reading the file from disk, stripping off the
headers, and holding the remains in memory as a texture. It's not VCL, but
it is simple pascal file manipulation rather than WIN API calls.
--
Jon
http://home1.gte.net/jqjacobs/index.htm
This posting was made entirely from recycled electrons. No innocent atoms
were harmed.
Quote
"technomage" <technomage@delphigamerdotcom> wrote in message

news:39e6e844$1_1@dnews...
Quote
> 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
> -----------------------------------------------------

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
> -----------------------------------------------------

Re:TBitmap -> OpenGL Texture


Thanks Jon,

I've actually got your book, but I've lent to a friend who wants to learn
Delphi and OpenGL.
looks like I'll have to nick it back ;-)

Technomage
-----------------------------------------------------
technom...@delphigamer.com
http://www.delphigamer.com
-----------------------------------------------------
Support Project JEDI
http://www.delphi-jedi.org
-----------------------------------------------------

Other Threads