Delphi, displaying 2D bitmap using OpenGL function calls

Posted on 2007-10-08
Last Modified: 2013-12-06
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.
Question by:cyman73
    LVL 20

    Accepted Solution

    hi cyman73,

    here is one:

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

    LVL 20

    Expert Comment

    > 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 )
    LVL 18

    Assisted Solution


    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.

    LVL 18

    Expert Comment

    Sorry... "width" instead of "weight"...
    LVL 20

    Expert Comment

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


    Author Comment

    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?
    LVL 20

    Expert Comment

    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:


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Suggested Solutions

    Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
    Recently, in one of the tech-blogs I usually read, I saw a post about the best-selling video games through history. The first place in the list is for the classic, extremely addictive Tetris. Well, a long time ago, in a galaxy far far away, I was…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now