cyman73
asked on
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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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?
ike
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
ASKER
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 (Applicati on.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,W rapper);
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('In valid 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?
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
so if glTexImage2D is like StretchDraw (which I read on EE) then should I be able to do the following
glTexImage2D(GL_TEXTURE_2D
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('In
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?
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
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
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 )