/* In the following code we are going to begin our study of blending. 1. Run the code as-is, and notice how the colors of the two triangles are not blended. The 't' or 'T' buttons toggle which of the triangles are rendered first. 2. Uncomment the two lines enabling blending in myInit() , and re-run. Notice how how there is a sense of translucence from the blended color in the overlap. 3. The alpha parameter for each triangle is 0.75. Try changing each of these a few times to see how it affects the blended color in the middle. Note that the smaller alpha value corresponds to more translucence. When you are done, change each of the alpha's back to 0.75. 4. Finally, uncomment the 4 calls setting up blending functions in the display(), and note the changes in both triangles. */ #include #include GLint leftFirst=GL_TRUE; void myInit() { //glEnable(GL_BLEND); //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glShadeModel(GL_FLAT); glClearColor(0.0 , 0.0 , 0.0 , 0.0); } void reshape (GLint w, GLint h) { glViewport(0,0,w,h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); if (w<=h) { gluOrtho2D(0.0,1.0,0.0,(double) h/w); } else { gluOrtho2D(0.0,(double) w/h, 0.0,1.0); } glMatrixMode(GL_MODELVIEW); } void drawLeftTriangle() { glBegin(GL_TRIANGLES); glColor4f(1.0,1.0,0.0,0.75); glVertex3f(0.1,0.9,0.0); glVertex3f(0.1,0.1,0.0); glVertex3f(0.7,0.5,0.0); glEnd(); } void drawRightTriangle() { glBegin(GL_TRIANGLES); glColor4f(0.0,1.0,1.0,0.75); glVertex3f(0.9,0.9,0.0); glVertex3f(0.3,0.5,0.0); glVertex3f(0.9,0.1,0.0); glEnd(); } void keyboard(unsigned char key, GLint x, GLint y) { switch (key) { case 't': case 'T': leftFirst =!leftFirst; glutPostRedisplay(); break; case 'q': exit( 0 ); break; } return; } void display() { glClear(GL_COLOR_BUFFER_BIT); if (leftFirst) { //glBlendFunc(GL_ONE, GL_ZERO); drawLeftTriangle(); //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); drawRightTriangle(); } else { //glBlendFunc(GL_ONE, GL_ZERO); drawRightTriangle(); //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); drawLeftTriangle(); } glFlush(); } int main (int argc , char **argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(200,200); glutInitWindowPosition(0,0); glutCreateWindow("Blending"); myInit(); glutDisplayFunc(display); glutKeyboardFunc(keyboard); glutReshapeFunc(reshape); glutMainLoop(); return 0; }