Bitmap problem in OpenGL

Im trying to do a console OpenGL and have a 2D image display on the sceen at all times. My problem is that its garbled when it appears on the screen. my code is as follows:

I declare the bitmap as follows:

GLubyte *scramble_bitmap;

I read in the bitmap (stored as raw RGB data in a file):

//now read in the scramble bitmap
if( (input = fopen(“scramble2.raw”, “r”)) == NULL )
{
exit(0);
}
else
{
count = 0;
scramble_bitmap = (GLubyte *) calloc(7350, sizeof(GLubyte));//70x35x3 bytes, size of scramble bitmap
while (!feof(input))
{
scramble_bitmap[count] = getc(input);
count++;

  }

}

fclose (input);

And I draw the thing to screen by:

glRasterPos3f(0.0, 0.0, 6.0 );
glDrawPixels(70, 35, GL_RGB, GL_UNSIGNED_BYTE, scramble_bitmap);

Anyone help?

Maybe Im describing it wrong.
Im trying to draw a bitmap image saved as a stripped header .raw file (only contains RGB values) on the bottom left hand corner of the camera viewpoint.
I draw the scene as normal and point the camera. Now I want to draw the bitmap after this is done. And I cant find any good tutorials (or bad ones matter of fact ).
so please can anyone point me in the right direction.

This could be your problem: Is the size of your image in the power of 2?
Look’s like it is not, I know for a texture map’s you have to do it this why…could be diffrent for bitmap image with drawpixels

example: 64 x 64, 64 x 32, 256 x 128.

Other thing could be that you are not reading in the file corectly.

GLubyte *scramble_bitmap;

I read in the bitmap (stored as raw RGB data in a file):

//now read in the scramble bitmap
if( (input = fopen(“scramble2.raw”, “r”)) == NULL )
{
exit(0);
}
else
{
count = 0;
scramble_bitmap = (GLubyte *) calloc(7350, sizeof(GLubyte));//70x35x3 bytes, size of scramble bitmap
while (!feof(input))
{
scramble_bitmap[count] = getc(input);
count++;

  }

}

fclose (input);

And I draw the thing to screen by:

glRasterPos3f(0.0, 0.0, 6.0 );
glDrawPixels(70, 35, GL_RGB, GL_UNSIGNED_BYTE, scramble_bitmap);

Anyone help?[/b]<HR></BLOCKQUOTE>

[This message has been edited by nexusone (edited 04-16-2002).]

For some good tutor’s:

Originally posted by blood.angel:
[b]Maybe Im describing it wrong.
Im trying to draw a bitmap image saved as a stripped header .raw file (only contains RGB values) on the bottom left hand corner of the camera viewpoint.
I draw the scene as normal and point the camera. Now I want to draw the bitmap after this is done. And I cant find any good tutorials (or bad ones matter of fact ).
so please can anyone point me in the right direction.

[/b]

Thanks nexusone, I resized the bitmap to 64x32 and changed the values accordingly in the program.
But now it displays the bottom half of the bitmap correct only, while the top half is all black.
WTF?

[This message has been edited by blood.angel (edited 04-16-2002).]

Textures need to be a power of 2, but when using glDrawPixels, the dimensions don’t matter.

When you say that it’s a bitmap file with the header stripped do you mean that you literally took a BMP and took out the header? If that’s the case, I believe BMP files actually store their data in BGR format, not RGB.

Also, you might want to try using the following…
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

I’m not sure about it, but I believe fopen() will open the file in text mode if you don’t explicitly tell it to open in binary mode. Try replace “r” in fopen() with “rb”. Then, when reading the map, read the entire file in one go instead, since you know how large it is. Something like this.

if((input = fopen(“scramble2.raw”, “rb”)) == NULL)
{
exit(0);
}
else
{
scramble_bitmap = (GLubyte *)calloc(7350, sizeof(GLubyte));
fread(scramble_bitmap, 7350, input);
fclose (input);
}

Just do some printf’s to see if it is reading the data in correctly. To test you opengl code try and draw a ramp

for(j=0;j<height;j++)
for(i=0;i<width;i++)
{
image[(jwidth) + i].r = (255/width) * i;
image[(j
width) + i].g = 0;
image[(j*width) + i].b = 0;
}

Yeah, when opening and reading a bitmap, you must do it as binary… In the case of .raw files, it is more standard to use binary, although in gamedev.net’s tutorials are saved in plane text… Why? I dont know…

fopen(file, “rb”);

fopen opens docs as plain text by default…

Thanks bob and the rest of you.

I got it sorted thanks to your help, but I find it weird. If I read the entire file in one go it works fine, but when I read it a byte at a time it only displays half the image and the rest black.

Thank you again.

When you say you read a byte at a time, do you use getc()?

Not sure about this either (abandoned C style I/O quite some time ago), but getc() may be a function related to text mode input, which can cause trouble with binary data.

Try read a byte at a time with fread(), with a size of 1 instead.

What I am guessing here is that you are not reading some of the header information before you get to the actually image…

There is a field called offset is the fileheader…

If you rewind your file with
rewind(file_pointer);

then you call
fseek( file_pointer , offset, SEEK_SET );

This will take you straight to the RGB goodies.
Then, I am pretty sure you can char by char…

The thing is there are some extra bits that are for the pallette info, and it seems like you are reading it as part of your RGB info…

Raw files doesn’t have any header. They are, as the name says, raw data.

And it seems like you are talking about a specific format, without mention which format it is. Not all formats look the same, not all have an “offset” field in the header, and not all have a palette.

Oh my bad…
I was talking about bmp formats… I forgot to include it.

Yes, you are tight raw formats dont have headers…