/* Demonstration of simple texture mapping. In this case, we create a simple checkerboard texture, and use it in several different ways in rendering a colored cube. Based on Edward Angel's original cube.c program. Written by Scott D. Anderson Fall 2000 */ /* Both textures and colors are assigned to the vertices */ #include #include // This program doesn't actually use TW, but tw.h includes the GL header files #include GLfloat vertices[][3] = { {-1,-1,-1}, // left bottom back {1,-1,-1}, // right bottom back {1,1,-1}, // right top back {-1,1,-1}, // left top back {-1,-1,1}, // left bottom front {1,-1,1}, // right bottom front {1,1,1}, // right top front {-1,1,1}}; // left top front GLfloat colors[][3] = { {0,0,0}, // black {1,0,0}, // red {1,1,0}, // yellow {0,1,0}, // green {0,0,1}, // blue {1,0,1}, // magenta {1,1,1}, // white {0,1,1}}; // cyan /* ================================================================ Texture stuff. The arrays are used in the texture mapping, and are initialized when the program begins, by a call to init_textures. */ #define TEXTURE_SIZE 64 typedef struct { GLubyte r; GLubyte g; GLubyte b; } RGB_color; GLubyte checkerboard[TEXTURE_SIZE*TEXTURE_SIZE]; // RGB_color checks_rgb[TEXTURE_SIZE*TEXTURE_SIZE]; GLubyte checks_rgb[TEXTURE_SIZE*TEXTURE_SIZE*3]; void init_textures() { int i,j; { /* The following is a black&white checkerboard. It's done using a 1D array where we do 8 black in a row, then 8 white in a row, and so forth. */ int count=0; for(i=0;i 360.0 ) theta[axis] -= 360.0; glutPostRedisplay(); } void key(unsigned char k, int xx, int yy) { switch(k) { case 'q': exit(0); break; case '+': delta = +2.0; break; case '-': delta = -2.0; break; case '!': delta = -delta; break; } } void myReshape(int w, int h) { glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); if (w <= h) { GLfloat ar = (GLfloat) h / (GLfloat) w; /* aspect ratio */ glOrtho(-2, 2, -2 * ar, 2 * ar, -10, 10); } else { GLfloat ar = (GLfloat) w / (GLfloat) h; /* aspect ratio */ glOrtho(-2 * ar, 2 * ar, -2, 2, -10, 10); } glMatrixMode(GL_MODELVIEW); } int main(int argc, char **argv) { glutInit(&argc, argv); init_textures(); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(500, 500); glutCreateWindow(argv[0]); glutReshapeFunc(myReshape); glutDisplayFunc(display); glutMouseFunc(mouse); glutKeyboardFunc(key); glEnable(GL_DEPTH_TEST); glutMainLoop(); return 0; }