/* Demo of three interpenetrating quads, to show transparency. Implemented Fall 2003 Scott D. Anderson */ #include #include #include bool BgBlack = true; bool MidTrans = false; bool DepthTest = true; // I'm trying to make the cross section have an equilateral triangle, in // the unit square, so this constant is useful, since it's the slope of // the non-horizontal lines. const float C=1.7320508075688772; // sqrt(3); void display(void) { if(BgBlack) glClearColor(0,0,0,0); // transparent black else glClearColor(1,1,1,1); // opaque white glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); twCamera(); // Transparent Objects glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glEnable(GL_BLEND); if(!DepthTest) glDepthMask(GL_FALSE); glBegin(GL_QUADS); if(MidTrans) glColor4f(0,1,0,0.5); // middle opaque green else glColor4f(0,1,0,0.7); // fairly opaque green glVertex3f(0,1,0); // back left glVertex3f(0,1,1); // front left glVertex3f(1/C,0,1); // front right glVertex3f(1/C,0,0); // back right if(MidTrans) glColor4f(0,0,1,0.5); // middle opaque blue else glColor4f(0,0,1,0.3); // fairly transparent blue glVertex3f(1-1/C,0,0); // back left glVertex3f(1-1/C,0,1); // front left glVertex3f(1,1,1); // front right glVertex3f(1,1,0); // back right glEnd(); glEnable(GL_DEPTH_TEST); glDepthMask(GL_TRUE); // this is the default glDisable(GL_BLEND); // Opaque Objects glBegin(GL_QUADS); glColor4f(1,0,0,1); // solid red glVertex3f(0,C/2,0); // back left glVertex3f(0,C/2,1); // front left glVertex3f(1,C/2,1); // front right glVertex3f(1,C/2,0); // back right glEnd(); glFlush(); glutSwapBuffers(); } void keys(unsigned char k, int, int) { switch(k) { case 'b': BgBlack = !BgBlack; break; case 'd': DepthTest = !DepthTest; break; case 'm': MidTrans = !MidTrans; break; } glutPostRedisplay(); } int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); twInitWindowSize(500,500); glutCreateWindow(argv[0]); glutDisplayFunc(display); twBoundingBox(0,1,0,1,0,1); twMainInit(); twKeyCallback('b',keys,"toggle black background"); twKeyCallback('d',keys,"toggle depth test"); twKeyCallback('m',keys,"toggle middle transparency"); glutMainLoop(); return 0; }