PDA

View Full Version : Bitmap problem in OpenGL



blood.angel
04-16-2002, 08:44 AM
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?

blood.angel
04-16-2002, 10:41 AM
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 http://www.opengl.org/discussion_boards/ubb/smile.gif ).
so please can anyone point me in the right direction.

nexusone
04-16-2002, 10:43 AM
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.

http://www.opengl.org/discussion_boards/ubb/smile.gif





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).]

nexusone
04-16-2002, 10:45 AM
For some good tutor's:

nehe.gamedev.net

http://www.opengl.org/discussion_boards/ubb/smile.gif



Originally posted by blood.angel:
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 http://www.opengl.org/discussion_boards/ubb/smile.gif ).
so please can anyone point me in the right direction.

blood.angel
04-16-2002, 11:03 AM
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).]

Deiussum
04-16-2002, 12:46 PM
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);

Bob
04-16-2002, 10:56 PM
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);
}

Gavin
04-17-2002, 03:22 AM
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[(j*width) + i].r = (255/width) * i;
image[(j*width) + i].g = 0;
image[(j*width) + i].b = 0;
}

Miguel_dup1
04-17-2002, 06:48 AM
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...

blood.angel
04-17-2002, 08:32 AM
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.

Bob
04-17-2002, 11:17 PM
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.

Miguel_dup1
04-18-2002, 06:14 AM
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...

Bob
04-18-2002, 06:57 AM
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.

Miguel_dup1
04-18-2002, 11:16 AM
Oh my bad....
I was talking about bmp formats... I forgot to include it.

Yes, you are tight raw formats dont have headers...