/* Simplest demo of transparency Written by Scott D. Anderson scott.anderson@acm.org Fall 2007 */ #include bool ShowScreen = false; void display() { twDisplayInit(); twCamera(); glClearColor(0,0,0,0); //clear sub-window to black glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glPushAttrib(GL_ALL_ATTRIB_BITS); // Normal depth stuff glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); // Lighting GLfloat lightdir[4] = {1,1,1,0}; twGrayLight(GL_LIGHT0,lightdir,0.1,0.8,0.5); glEnable(GL_LIGHTING); glShadeModel(GL_SMOOTH); // Draw solid stuff, a nice brass teapot, roughly brass. I found "brass" at http://www.colourprep.com/hexColorsName.html twTriple brass = { 0xB5/255.0, 0xA6/255.0, 0x42/255.0 }; twColor(brass,0.9,20); glutSolidTeapot(1); if(ShowScreen) { // switch to transparency glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glDepthMask(GL_FALSE); // Turn off lighting glDisable(GL_LIGHTING); // Draw a sheer green screen in front of and to one side of the teapot glColor4f(0,1,0,0.5); GLfloat Z = 1.0; glBegin(GL_QUADS); glVertex3f(0,1,Z); glVertex3f(0,-1,Z); glVertex3f(1,-1,Z); glVertex3f(1,1,Z); glEnd(); } glPopAttrib(); glFlush(); glutSwapBuffers(); } void keys(unsigned char key, int x, int y) { switch(key) { case 's': ShowScreen = ! ShowScreen; break; } glutPostRedisplay(); } int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGBA | GLUT_ALPHA | GLUT_DEPTH | GLUT_DOUBLE); twInitWindowSize(500,500); glutCreateWindow(argv[0]); glutDisplayFunc(display); twBoundingBox(-1,1,-1,1,-1,1); twMainInit(); twKeyCallback('s',keys,"toggle showing the screen"); glutMainLoop(); return 0; }