/* A demo of texture-mapping onto a cylinder, aligning the image two different ways. There is no lighting in this demo, so no need for normals on the cylinder. The texture is decaled on, so the color of the cylinder is unimportant. Scott D. Anderson, original, Fall 2002 Adapted and simplified for TW, Fall 2003 */ #include #include #include #include void textureInit(char* imgfile) { glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); twPPM_Tex2D(imgfile,true); } const float CylinderRadius = 1; const float CylinderHeight = 6; bool Long = true; // whether the parameter is parallel to the axis int Sides = 4; // the number of sides to the cylinder /* ==================================================================== This cylinder has texture coordinates on the vertices. It has s around the circumference (mapping s:0-1 as angle:0-2pi). It has t going along the axis. The axis is aligned with the z axis. Use transformations if that's not what you want. The bottom and top also have textures mapped onto them, just to show the distortion one would get. */ void cylinder(GLfloat radius, GLfloat height, int steps) { int i; GLfloat float_steps=(GLfloat) steps; GLfloat theta, x, y, p, cosine, sine; glEnable(GL_TEXTURE_2D); glBegin(GL_QUAD_STRIP); for(i=0; i<=steps; ++i) { p = i/float_steps; // the parameter theta = p*2*M_PI+M_PI/4; x = radius*cos(theta); y = radius*sin(theta); if(Long) glTexCoord2f(p,0); else glTexCoord2f(0,p); glVertex3f(x,y,0); if(Long) glTexCoord2f(p,1); else glTexCoord2f(1,p); glVertex3f(x,y,height); } glEnd(); } void display() { twDisplayInit(); twCamera(); glPushAttrib(GL_ALL_ATTRIB_BITS); glEnable(GL_TEXTURE_2D); cylinder(CylinderRadius,CylinderHeight,Sides); glPopAttrib(); glutSwapBuffers(); glFlush(); } void keys(unsigned char k, int, int) { switch (k) { case '+': Sides++; break; case '-': Sides = Sides > 3 ? Sides-1 : 3 ; break; case 'l': Long = !Long; break; } glutPostRedisplay(); } int main(int argc, char** argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH ); glutInitWindowSize(500,500); glutCreateWindow(argv[0]); glutDisplayFunc(display); twBoundingBox(-CylinderRadius,CylinderRadius, -CylinderRadius,CylinderRadius, 0,CylinderHeight); twMainInit(); if(argc < 2 ) textureInit("USflag.ppm"); else textureInit(argv[1]); twKeyCallback('+',keys,"Increase number of Sides"); twKeyCallback('-',keys,"Decrease number of Sides"); twKeyCallback('l',keys,"toggle the orientation of the texture"); glutMainLoop(); return 0; }