// 1*1 unit square drawn with triangle_strips. Normal points in pos. // y direction. Allows for lighting effects with textures. void twDrawUnitSquare(float w, float h) { int i,j; GLfloat dw = 1.0/w; GLfloat dh = 1.0/h; glNormal3f(0,1,0); for( i=0; i } #endif int twPPM_Tex2D(char *filename, bool verbose) { #ifdef NETPBM FILE *fp; int rows, cols; pixval maxPixValue; // this is just an unsigned int filename = twPathname(filename,verbose); if(NULL==filename) return 1; if(verbose) fprintf(stderr,"Opening texture file %s.\n",filename,strlen(filename)); fp = fopen(filename,"r"); if(!fp) { fprintf(stderr,"Unable to open %s\n",filename); perror("in twPPM_Tex2d"); return 1; } /* The format of the image is as an array of rows (so that the caller doesn't have to do the row-major-order stuff by hand), where each row is an array of pixel structures (see /usr/include/ppm.h) and the elements of the pixel structure are an unsigned int, so the sizeof(pixel) is 12. We use the accessor macros to hide this, of course, but it's important to know that we can't just cast this value to the kind of 2D array that OpenGL wants. */ pixel** image = ppm_readppm( fp, &cols, &rows, &maxPixValue); int rv=fclose(fp); if(rv != 0) { fprintf(stderr,"Error closing %s: %d\n",filename,errno); } if(!pow2(cols)) { fprintf(stderr,"%s: cols (%d) is not a power of two.\n",filename,cols); return 2; } if(!pow2(rows)) { fprintf(stderr,"%s: rows (%d) is not a power of two.\n",filename,rows); return 3; } if(verbose) printf("%s: %d x %d x %d image\n",filename,cols,rows,maxPixValue); /* This is the kind of array that OpenGL wants. We take advantage of the C++ ability to declare an array using dimensions that are run-time values. */ GLubyte texture[rows][cols][3]; texture[rows-1][cols-1][0] = 255; for(int i=0;i=x) return y; y = y << 1; } } /* returns zero if success, and 1 if failure (unable to open file). */ int twPPM_Tex2D(char *filename, bool verbose, GLfloat& width, GLfloat& height) { #ifdef NETPBM FILE *fp; int cols, rows, fmt; pixval maxPixValue; // this is just an unsigned int, typically 255 int rows2, cols2; // the rows and cols rounded up to a power of 2 filename = twPathname(filename,verbose); if(NULL==filename) return 1; if(verbose) fprintf(stderr,"Opening texture file %s.\n",filename,strlen(filename)); fp = fopen(filename,"r"); if(!fp) { fprintf(stderr,"Unable to open %s\n",filename); perror("in twPPM_Tex2D"); return 1; } ppm_readppminit( fp, &cols, &rows, &maxPixValue, &fmt); cols2 = ceil2(cols); rows2 = ceil2(rows); width = ((GLfloat) cols)/((GLfloat) cols2); height = ((GLfloat) rows)/((GLfloat) rows2); if(verbose) printf("%s: %d x %d x %d image rounded up to %d x %d (%f,%f)\n", filename,cols,rows,maxPixValue,cols2,rows2,width,height); pixel* imagerow = ppm_allocrow( cols ); GLubyte texture[rows2][cols2][3]; for(int i=0; i