+void dump_gl_texture(unsigned int tex, const char *fname)
+{
+ FILE *fp;
+ int i, width, height;
+ unsigned char *pixels;
+
+ glBindTexture(GL_TEXTURE_2D, tex);
+ glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &width);
+ glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &height);
+
+ if(!(pixels = malloc(width * height * 3))) {
+ return;
+ }
+ glGetTexImage(GL_TEXTURE_2D, 0, GL_RGB, GL_UNSIGNED_BYTE, pixels);
+
+ if(!(fp = fopen(fname, "wb"))) {
+ free(pixels);
+ return;
+ }
+ fprintf(fp, "P6\n%d %d\n255\n", width, height);
+ for(i=0; i<width * height * 3; i++) {
+ fputc(pixels[i], fp);
+ }
+ fclose(fp);
+ free(pixels);
+}
+