/* * 1D textured rainbow demo from Chapter 8. * * Written by Michael Sweet */ /* Added the ability to turn the rainbow on/off using the 'R' keyboard callback Scott D. Anderson Fall 2000 original Fall 2003 adapted to use TW. */ #include #include bool Rainbow = true; // whether to show the rainbow. void key(unsigned char k, int x, int y) { Rainbow = !Rainbow; glutPostRedisplay(); } void rainbowInit() { static GLubyte roygbiv[8][3] = { { 0x3f, 0x00, 0x3f }, /* Dark Violet (for 8 colors...) */ { 0x7f, 0x00, 0x7f }, /* Violet */ { 0xbf, 0x00, 0xbf }, /* Indigo */ { 0x00, 0x00, 0xff }, /* Blue */ { 0x00, 0xff, 0x00 }, /* Green */ { 0xff, 0xff, 0x00 }, /* Yellow */ { 0xff, 0x7f, 0x00 }, /* Orange */ { 0xff, 0x00, 0x00 } /* Red */ }; /* Load the texture data */ glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexImage1D(GL_TEXTURE_1D, 0, 3, 8, 0, GL_RGB, GL_UNSIGNED_BYTE, roygbiv); glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL); } void display(void) { twDisplayInit(0.5,0.5,1.0); // clear to sky blue twError(); twCamera(); /* this is the green ground, drawn as an enormous circle, which is what gives the horizon its slightly curved appearance. */ glDisable(GL_TEXTURE_1D); // have to make sure textures are off glColor3f(0.0, 0.8, 0.0); glPushMatrix(); glRotatef(-90,1,0,0); // now z points up twDisk(100,30); glPopMatrix(); twError(); rainbowInit(); /* Then a rainbow... */ twColorName(TW_YELLOW); // draw in Yellow, but this is irrelevant // if decal texture-mapping is enabled if(Rainbow) { glEnable(GL_TEXTURE_1D); } twError(); glBegin(GL_QUAD_STRIP); for (float th = 0.0; th <= M_PI; th += (0.03125 * M_PI)) { float x = cos(th) * 50.0; float y = sin(th) * 50.0; float z = -50.0; if(Rainbow) glTexCoord1f(0.0); glVertex3f(x, y, z); x = cos(th) * 55.0; y = sin(th) * 55.0; z = -50.0; if(Rainbow) glTexCoord1f(1.0); glVertex3f(x, y, z); } glEnd(); twError(); glFinish(); glutSwapBuffers(); } main(int argc, char *argv[]) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); twInitWindowSize(600,500); glutCreateWindow(argv[0]); twBoundingBox(-100,100,0,50,-100,100); twMainInit(); twKeyCallback('R',key,"Toggle whether to show the rainbow"); glutDisplayFunc(display); glutMainLoop(); return (0); }