0

I know there is a question about this already but it does not give a solution in code.

I'm trying to load a bitmap image into a GL application using this function:

void glBitmap(GLsizei  width,
              GLsizei  height,
              GLfloat  xorig,
              GLfloat  yorig,
              GLfloat  xmove,
              GLfloat  ymove,
              const GLubyte *  bitmap);

Can someone give me a function that returns a GLubyte* given a filename? I've been looking all over the web for a working algorithm but can't seem to get any to work.

Community
  • 1
  • 1
Nick Brunt
  • 9,533
  • 10
  • 54
  • 83
  • @Mat http://pastebin.com/sUz0e5h4 It's not very clean and would need some tweaking but I still can't see why it wouldn't work. – Nick Brunt Nov 22 '11 at 13:32
  • Post the relevant code in your question (not that link) so people can look at it and tell you what's wrong/what could be better. And describe _what_ doesn't work/_how_ it fails. – Mat Nov 22 '11 at 13:33
  • @Mat The point is, I have no idea what to do. Can you help me? – Nick Brunt Nov 22 '11 at 13:34
  • Just post the code here. People will look at it and spot the bugs. You're not the first person to try to read binary data from a file in C or C++ . – Mat Nov 22 '11 at 13:36
  • @Mat I don't really have any code to read in the bitmap. The link above just shows a function that I got from the net anyway and it doesn't work. What I'm after is as simple a function as possible that just reads in a bitmap image as a GLubyte array. Assume that I have no code. Or if I did, it would just be an empty function with this type: `GLubyte* loadBMP(const char* fileName) {}`. – Nick Brunt Nov 22 '11 at 13:40
  • 1
    Then you're not on the right site. There are hundreds of examples on the net that read binary data from files on C and in C++. Try to implement it yourself. Once you're stuck on a specific coding issue, feel free to ask here, but people won't write your code for you. – Mat Nov 22 '11 at 13:43
  • @Mat Well can you suggest a site that can help me then? – Nick Brunt Nov 22 '11 at 13:51
  • 1
    Google. Really. Look for tutorials that load bitmaps. Trust me, you're not the first person to try to load a bitmap from a file. – Mat Nov 22 '11 at 13:52
  • @Mat I'm fully aware that I'm not the first person. And do you really think I'd ask the question if I hadn't tried Google already? I've been trying this for hours with no success. I've found plenty of "solutions" but most of them don't compile, and the ones that do crash for other reasons, or simply don't display anything. I feel like there should be a really simple solution to this. It shouldn't have to be dozens and dozens of lines. – Nick Brunt Nov 22 '11 at 13:58
  • 2
    Nick, if you have a piece of code you have tried, and it sort of works but not really (crashes, bad results, whatever), then **post that here**. – Mat Nov 22 '11 at 14:00

2 Answers2

1

The problem with using glBitmap to display the image is that you need to load the image from a file, and then interpret the data as colour index array. Since every possible library and example interpret the image data as RGB (or RGBA), and since color table needs to be set using glColorTable, nobody is using glBitmap to load the image, therefore there are no real examples of how to use glBitmap with data loaded from the file.

By the way, this is from glBitmap reference page :

The bitmap image is interpreted like image data for the glDrawPixels command, with width and height corresponding to the width and height arguments of that command, and with type set to GL_BITMAP and format set to GL_COLOR_INDEX.

Save yourself trouble, and use glDrawPixels or textures to display the image from a file.

This page contains the example how to display the image using glBitmap, but not from the file.

BЈовић
  • 62,405
  • 41
  • 173
  • 273
0

First you need to understand what glBitmap does: glBitmap is a drawing function. It looks at each bit (hence bit map) of the given data, interpreted as a 2 dimensional array and relative to the current raster position (set with glRasterPos) sets those pixels to the currently set raster color (glRasterColor) whose corresponding bit in the bitmap are set. Each GLubyte contains 8 bits, so each byte covers 8 pixels width (endianess is set using glPixelStore).

A for reading a bitmap from a file. AFAIK there's only one widespread bitmap format (in contrast to pixmaps which have channels, where each pixel's channel can assume a range of values). That format is PBM: http://en.wikipedia.org/wiki/Netpbm_format

Given the information from the Wikipedia page it is straightforward to write a function that opens and reads a PBM file (functions required are fopen, fread and fclose) into a structure holding the data. Then one can use that structure to feed the data to glBitmap.

But remember, bitmaps are just binary valued images, i.e. black and white, no grays, not to speak of colour.

Now your notion was "loading a bitmap". I already told, that glBitmap immediately draws the data you give it. So I think what you're actually looking for is textures and a image file to texture loader.

datenwolf
  • 159,371
  • 13
  • 185
  • 298