[Last Call] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3146
  • Last Modified:

Delphi, displaying 2D bitmap using OpenGL function calls

Hello all,

I am trying to load and display a bitmap using OpenGL function calls.  I want to implement with Delphi.
All examples I have seen are 3D related.  I just want to load a .jpg to Bitmap object then display it to the screen using OpenGL calls.   I want to only use the 2D x and y screen positions.

I can load a bitmap to a 'Texture : GLuint;', and I see that I might have to then use glTexImage2D call.  

A simple tutorial on displaying a bitmap image would be greatly appreciated.

Thank you.
  • 4
  • 2
2 Solutions
hi cyman73,

here is one:


a link for the delphi-code is at the bottom of that side.

> I want to only use the 2D x and y screen positions.

instead of "glVertex3f" in that tutorial use "glVertex2f", that sets the z-component to zero ..

glVertex3f( 1.0, 1.0, 0.0 )

  is the same as

glVertex2f( 1.0, 1.0 )
Jose ParrotGraphics ExpertCommented:

If you just need to load a jpg and show it in the screen, GDI is enough for such purpose. OpenGL is a powerful tool, with a large set of functions and requires an initialization to work properly. Besides that, the bitmap will be applied on a surface of an object (say a cube, or a rectangle) and such object projection rendered in the screen.

GDI is a very simpler approach.

Anyway, you'll notice that the bitmap size (height and weight) should be the same as the display resolution (say, 1024x768, or 1600x1200). That is, the image and the screen should have the same size, to avoid displaying only part of the image (if the image is larger than the scree) or black regions (if the image is smaller than the screen).

If image and screen sizes are different, OpenGL can make the adjustments, but you must by your own to determine the right parameters to make the image to cover exactly the entire surface of the screen, which isn't an easy task.

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Jose ParrotGraphics ExpertCommented:
Sorry... "width" instead of "weight"...
> to cover exactly the entire surface of the screen, which isn't an easy task.
its easy with ortographic projection, you can use the bounds of the rectangle as projection-bounds passed to glOrtho.

if you use opengl, the problem is, that normal gl-textures have to have the same width and height and it has to be a multiple of 2, i.e. 32, 64, 128 .. so you have to use an opengl-extension, which supports your texture-sizes.

as Jose said already, if you just want to display an image, there are much easier solutions, but opengl worths learning as well .. ;) what excactly is your application supposed to do?

cyman73Author Commented:
Thank you JoseParrot and ikework.

I am trying to learn OpenGL.  I would like to just start with displaying images.  My goal is to rotate the images later and maybe wrap them around objects.

I am confused on how to display the image.  I can load a texture with a bitmap.  I can even read the bitmap and get a pointer to the pixels in memory.  I just can't seem to display the image.

Here is what I did so far:
// Wrapper is a pointer to the pixel data.  sWidth and tHeight is a multiple of 2
Wrapper := ReadBitmap(ExtractFilePath(Application.ExeName) + 'Image.bmp', sWidth, tHeight);

so if glTexImage2D is like StretchDraw (which I read on EE) then should I be able to do the following
glTexImage2D(GL_TEXTURE_2D, 0,  3, sWidth, tHeight, Border, GL_RGBA,GL_UNSIGNED_BYTE,Wrapper);

where ReadBitmap is as follows (take from Delphi Developer's Guide to OpenGL by :Jon Q Jacobs).

function TForm1.ReadBitmap(const FilePath: string; var sWidth, tHeight: GLsizei): pointer;
  szh = SizeOf(TBitmapFileHeader);
  szi = SizeOf(TBitmapInfoHeader);
  bmpfile: file;
  bfh: TBitmapFileheader;
  bmi: TBitmapInfoHeader;
  t: Byte;
  size: Integer;
  assignFile(bmpfile, FilePath);
  reset(bmpfile, 1);
  size := FileSize(bmpfile) - szh - szi;
  blockread(bmpfile, bfh, szh);
  if Bfh.bfType <> $4D42 then
    raise EInvalidGraphic.Create('Invalid Bitmap');
  blockread(bmpfile, bmi, szi);

  with bmi do
    sWidth := biWidth;
    tHeight := biHeight;
  getmem(result, size);
  blockread(bmpfile, result^, size);
  for x := 0 to sWidth*tHeight -1 do
    with TWrap(result^)[x] do
      t := r;
      r := b;
      b := t;

Any suggestions?
what is the width and the height of your bitmap?
you cant take *any* bitmap. width and height must be the same and a multiple of 2, i.e. 32, 64, 128 .. test your code with the bitmap "data/NeHe.bmp" in:


if it doesn't work either, then the problem is in your code. here is delphi-code for that example:



Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now