/* Demo of using binding to allow the efficient use of multiple textures. 6 textures are loaded and bound to the sides of a cube. Scott D. Anderson Fall 2002 original Fall 2003 adapted to use TW */ #include #include #include /* ================================================================ Cube */ GLfloat vertices[][3] = { {-1,-1,-1}, {+1,-1,-1}, {+1,+1,-1}, {-1,+1,-1}, {-1,-1,+1}, {+1,-1,+1}, {+1,+1,+1}, {-1,+1,+1}}; GLuint textureIDs[6]; /* Polgons as faces of a cube. We set the color and texture for each vertex. Vertex a is the upper left (that is, it corresponds to the upper left of the texture, and then we go counterclockwise, so vertex b is the lower left of the texture. */ void polygon(int a, int b, int c , int d) { /* draw a polygon via list of vertices */ glBegin(GL_POLYGON); glTexCoord2f(0,0); glVertex3fv(vertices[a]); glTexCoord2f(0,1); glVertex3fv(vertices[b]); glTexCoord2f(1,1); glVertex3fv(vertices[c]); glTexCoord2f(1,0); glVertex3fv(vertices[d]); glEnd(); } void texturecube() { /* map vertices to faces */ glBindTexture(GL_TEXTURE_2D, textureIDs[0]); polygon(0,3,2,1); // back: z=-1 glBindTexture(GL_TEXTURE_2D, textureIDs[1]); polygon(6,2,3,7); // top: y=+1 glBindTexture(GL_TEXTURE_2D, textureIDs[2]); polygon(0,4,7,3); // left: x=-1 glBindTexture(GL_TEXTURE_2D, textureIDs[3]); polygon(6,5,1,2); // right: x=+1 glBindTexture(GL_TEXTURE_2D, textureIDs[4]); polygon(6,7,4,5); // front: z=+1 glBindTexture(GL_TEXTURE_2D, textureIDs[5]); polygon(0,1,5,4); // bottom: y=-1 } void init() { glGenTextures(6,textureIDs); /* get all the texture ids */ twLoadTexture(textureIDs[0],"mandrill.ppm"); twLoadTexture(textureIDs[1],"cokecan.ppm"); twLoadTexture(textureIDs[2],"homer2.ppm"); twLoadTexture(textureIDs[3],"peterms.ppm"); twLoadTexture(textureIDs[4],"eac512x256.ppm"); twLoadTexture(textureIDs[5],"USflag.ppm"); // for(int i=0; i<6; i++) { printf("%d: %d\n",i,textureIDs[i]); } } void display(void) { twDisplayInit(); twCamera(); glPushAttrib(GL_ALL_ATTRIB_BITS); glEnable(GL_TEXTURE_2D); // From now on, we'll use textures glColor3f(1,1,1); // on white cubes texturecube(); glPopAttrib(); glFlush(); glutSwapBuffers(); } int main(int argc, char *argv[]) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); glutInitWindowSize(500,500); glutCreateWindow(argv[0]); init(); glutDisplayFunc(display); twBoundingBox(-2,2,-2,2,-1,1); twMainInit(); glutMainLoop(); return (0); }