#include /* An example of using OpenGL to render a simple object with a texture. The texture is saved in a ppm format file. You can also use the program dmconvert to tell you more about the image file. */ #include #include #include /*This next is referred to in several places, and because of the callback interface, cannot be passed as a parameter, hence, global*/ /*WARNING - using the PPM file format, and you must delete the first few lines of text starting only from the image size data*/ typedef GLubyte Pixel[3]; /*represents red green blue*/ int Width, Height; /*of image*/ /*array of pixels*/ Pixel *Image; /*name of image file*/ char *Filename = "../default.ppm"; int allowedSize(int x) /*returns max power of 2 <= x*/ { int r; r = 1; while(r < x) r=(r<<1); if(r==x) return r; else return r>>1; } void readImage(void) /*reads the image file assumes ppm format*/ { int w,h,max; int i,j; unsigned int r,g,b; int k; char ch; FILE *fp; fp = fopen(Filename,"r"); printf("filename = %s\n",Filename); /*read the header*/ fscanf(fp, "P%c\n", &ch); if (ch != '3') { fprintf(stderr, "Only ascii mode 3 channel PPM files"); exit(-1); } /*strip comment lines*/ ch = getc(fp); while (ch == '#') { do { ch = getc(fp); } while (ch != '\n'); ch = getc(fp); } ungetc(ch, fp); /*read the width*/ fscanf(fp,"%d",&w); /*read the height*/ fscanf(fp,"%d",&h); /*max intensity - not used here*/ fscanf(fp,"%d",&max); /*width and height must be powers of 2 - taking the simple option here of finding the max power of 2 <= w and h*/ Width = allowedSize(w); Height = allowedSize(h); printf("Width = %d, Height = %d\n",Width,Height); Image = (Pixel *)malloc(Width*Height*sizeof(Pixel)); for(i=0;i