#include "GL/glut.h" #include #include const int toggle=-1; enum {source,destination}; int currstate=source; float curralpha = 0.5; int factor[2]={GL_ONE,GL_ZERO}; char* fname[4]= {"GL_ZERO", "GL_ONE", "GL_SRC_ALPHA", "GL_ONE_MINUS_SRC_ALPHA"}; int width=500, height=500; int numcolors=6; float colors0[7][4] = { {1.0,0.0,0.0,0.25}, {1.0,1.0,0.0,0.25}, {0.0,1.0,0.0,0.25}, {0.0,1.0,1.0,0.25}, {0.0,0.0,1.0,0.25}, {1.0,0.0,1.0,0.25}, {1.0,0.0,0.0,0.25}, }; float colors1[7][4] = { {1.0,0.0,0.0,0.5}, {1.0,1.0,0.0,0.5}, {0.0,1.0,0.0,0.5}, {0.0,1.0,1.0,0.5}, {0.0,0.0,1.0,0.5}, {1.0,0.0,1.0,0.5}, {1.0,0.0,0.0,0.5}, }; const float points[14][3]= { {-120.0,-250.0,-20.0}, {-120.0,250.0,-20.0}, {-80.0,-250.0,-20.0}, {-80.0,250.0,-20.0}, {-40.0,-250.0,-20.0}, {-40.0,250.0,-20.0}, {0.0,-250.0,-20.0}, {0.0,250.0,-20.0}, {40.0,-250.0,-20.0}, {40.0,250.0,-20.0}, {80.0,-250.0,-20.0}, {80.0,250.0,-20.0}, {120.0,-250.0,-20.0}, {120.0,250.0,-20.0} }; void init(); void display(); void reshape(int w, int h); void mouse(int button, int state, int x, int y); void keyboard(unsigned char key, int x, int y); int round(float x); void menu(int menuitem); char* getname(int glcode); main(int argc, char* argv[]) { int mainmenu; /* set up window */ glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE ); glutInitWindowSize(width,height); glutInitWindowPosition(100,100); glutCreateWindow("Blending"); /* initialize state */ init(); /* set up menus */ mainmenu=glutCreateMenu(menu); glutAddMenuEntry("Toggle to Set Destination Factor",toggle); glutAddMenuEntry("GL_Zero",GL_ZERO); glutAddMenuEntry("GL_One",GL_ONE); glutAddMenuEntry("GL_Src_Alpha",GL_SRC_ALPHA); glutAddMenuEntry("GL_One_Minus_Src_Alph",GL_ONE_MINUS_SRC_ALPHA); glutAttachMenu(GLUT_RIGHT_BUTTON); /* register call back functions */ glutDisplayFunc(display); glutReshapeFunc(reshape); // glutMouseFunc(mouse); glutKeyboardFunc(keyboard); /* wait until something happens */ glutMainLoop(); return 1; } void init() { /* clear color is black */ glClearColor(0.0,0.0,0.0,0.0); /* define the viewing system */ glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-250.0, 250.0, -250.0, 250.0, 10.0, 50.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glShadeModel(GL_FLAT); glEnable(GL_BLEND); cerr << "SRC alpha = " << curralpha << "\n"; cerr << "Source: " << getname(factor[source]) << ", Destination: " << getname(factor[destination]) << "\n"; } void display() { int i; glClear(GL_COLOR_BUFFER_BIT); // draw the bottom pallette glBlendFunc(GL_ONE,GL_ZERO); glBegin(GL_QUAD_STRIP); for(i= 0; i<=numcolors; i += 1) { glColor4fv(&colors0[i][0]); glVertex3fv(&points[2*i][0]); glVertex3fv(&points[2*i+1][0]); } glEnd(); glBlendFunc(factor[source],factor[destination]); glPushMatrix(); glRotatef(90.0,0.0,0.0,1.0); glBegin(GL_QUAD_STRIP); for(i= 0; i<=numcolors; i += 1) { glColor4fv(&colors1[i][0]); glVertex3fv(&points[2*i][0]); glVertex3fv(&points[2*i+1][0]); } glEnd(); glPopMatrix(); glutSwapBuffers(); } void reshape(int w, int h) { /* width and height store current window dimensions */ width = w; height = h; /* map the projection plane to the entire window */ glViewport(0,0,w,h); } void keyboard(unsigned char key, int x, int y) { int i; switch(key) { // add new points case '+': if (curralpha < 1.0) { curralpha += (float) 0.05; for (i=0;i<=numcolors;i++) colors1[i][3]=curralpha; } cerr << "SRC Alpha = " << curralpha << "\n"; glutPostRedisplay(); break; case '-': if (curralpha > 0.0) { curralpha -= (float) 0.05; for (i=0;i<=numcolors;i++) colors1[i][3]=curralpha; } cerr << "SRC Alpha = " << curralpha << "\n"; glutPostRedisplay(); break; break; } return; } void menu(int menuitem) { switch(menuitem) { case toggle: if (currstate==source) { glutChangeToMenuEntry(1,"Toggle to Set Source Factor",toggle); currstate=destination; } else { glutChangeToMenuEntry(1,"Toggle to Set Destination Factor",toggle); currstate=source; } break; default: factor[currstate]=menuitem; cerr << "Source: " << getname(factor[source]) << ", Destination: " << getname(factor[destination]) << "\n"; glutPostRedisplay(); break; } return; } char* getname(int glcode) { switch (glcode) { case GL_ZERO: return fname[0]; case GL_ONE: return fname[1]; case GL_SRC_ALPHA: return fname[2]; case GL_ONE_MINUS_SRC_ALPHA: return fname[3]; } return ""; }