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

x
  • 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.
0
cyman73
Asked:
cyman73
  • 4
  • 2
2 Solutions
 
ikeworkCommented:
hi cyman73,

here is one:

http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=06

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


ike
0
 
ikeworkCommented:
> 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 )
0
 
Jose ParrotGraphics ExpertCommented:
Hi,

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.

Jose
0
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"...
0
 
ikeworkCommented:
> 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?


ike
0
 
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;
const
  szh = SizeOf(TBitmapFileHeader);
  szi = SizeOf(TBitmapInfoHeader);
var
  bmpfile: file;
  bfh: TBitmapFileheader;
  bmi: TBitmapInfoHeader;
  t: Byte;
  x,
  fpos,
  size: Integer;
begin
  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
  begin
    sWidth := biWidth;
    tHeight := biHeight;
  end;
  getmem(result, size);
  blockread(bmpfile, result^, size);
  for x := 0 to sWidth*tHeight -1 do
  begin
    with TWrap(result^)[x] do
    begin
      t := r;
      r := b;
      b := t;
    end;
  end;
end;

Any suggestions?
0
 
ikeworkCommented:
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:

http://nehe.gamedev.net/data/lessons/vc/lesson06.zip

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

http://nehe.gamedev.net/data/lessons/delphi/lesson06.zip


ike
0

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