#include #include #include #include struct image { int depth; int width; int height; int components; GLubyte *data; }; struct image img; // Borrowed from ATI ImageIO library int load_tga (struct image *img, char *filename) { FILE *fp; GLubyte tmp; unsigned char header [18]; if (!(fp = fopen(filename, "rb"))) { return 1; } fread(header, 18, 1, fp); if (header[2] != 2) { fclose(fp); return 1; } if (header[16] < 24) { fclose(fp); return 1; } img->depth = 1; img->width = header[12] + (header[13] << 8); img->height = header[14] + (header[15] << 8); img->components = header[16] / 8; img->data = new GLubyte[img->width * img->height * img->components]; fseek(fp, header[0], SEEK_CUR); fread(img->data, img->width * img->height * img->components, 1, fp); fclose(fp); for (int i = 0; i < img->width * img->height * img->components; i += img->components) { tmp = img->data[i]; img->data[i] = img->data[i+2]; img->data[i+2] = tmp; } return 0; } void draw () { glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_POINTS); for (int i = 0; i < img.width; i++) for (int j = 0; j < img.height; j++) { glColor3ubv(&(img.data[j * (img.width * img.components) + (i * img.components)])); glVertex2f(i, j); } glEnd(); glFlush(); } int main (int argc, char **argv) { if (load_tga(&img, "wood.tga")) printf("File Not Found\n"); glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE); glutInitWindowSize(img.width, img.height); glutCreateWindow("Basic 2D Drawing"); glutDisplayFunc(draw); glMatrixMode(GL_PROJECTION); glOrtho(0, img.width, 0, img.height, -1000, 1000); glutMainLoop(); return 0; }