Displaying a .pgm file with OpenGL and GLUT

Started by
0 comments, last by songho 15 years, 3 months ago
I've been all over the internet and this website for numerous hours looking for some hint as to what is going wrong with this, to no avail. I am trying to display an ASCII .pgm file (code P2) with OpenGL and the GLUT library. The following code is only a piece of the full code, and is called by the glutDisplayFunc(). I'd greatly appreciate it if someone could tell me what it is I am doing wrong and give me a hint on how to fix it. Currently the program either crashes or displays gobbledygook, with no discernible pattern. With all the searching and experimentation I've done over the past few days, I am fairly certain it is a problem glDrawPixels() and how I am handing the pixel array into said function. Thanks in advance, void displayPGM() { ifstream display; char nextline[500]; int* pixeldata; int height = 0; int width = 0; display.open(outname.c_str()); while(display.peek() < '0' || display.peek() > '9') display.getline(nextline, 500); display >> height >> width; display.getline(nextline, 1000); pixeldata = new int[height*width]; for(int i = 0; i < height*width; i++) display >> pixeldata; glClear(GL_COLOR_BUFFER_BIT); glDrawPixels(width, height, GL_LUMINANCE, GL_INT, &pixeldata); glFlush(); }
Advertisement
I believe PGM is 8-bit grayscale image format, therefore the data type in glDrawPixels() would be GL_UNSIGNED_BYTE instead of GL_INT.

You may have other mistakes on loading the pixmap and OpenGL codes. Please refer to the following codes. It includes a C++ PGM (P2 and P5) loader/writer class and OpenGL (GLUT) codes to draw the PGM image to the screen;

imagePgm.zip
imagePpm.zip

This topic is closed to new replies.

Advertisement