//--------------------------------------------------------------------- // Priklad cislo 13 // Autor: Pavel Tisnovsky // // Vytvoreni dvou kaskadovych menu z nichz kazde je navazano na jine // tlacitko mysi (leve tlacitko vybira barvu, prave zpusob zobrazeni) //--------------------------------------------------------------------- #include <GL/glut.h> // hlavickovy soubor funkci GLUTu enum { BlackColor=0x000000, // cisla prikazu ve svem kodu GrayColor=0x808080, // primo obsahuji zvolenou barvu RedColor=0xff0000, GreenColor=0x00ff00, BlueColor=0x0000ff, WhiteColor=0xffffff } commands; #define FullScreen 1 // prikazy pro druhe menu #define Window 2 #define Quit 3 #define NoQuit 4 float firstColor[3]={0.3, 0.3, 0.3}; // barevne slozky jednotlivych vertexu float secondColor[3]={0.3, 0.3, 0.3}; float thirdColor[3]={0.3, 0.3, 0.3}; int width=0, height=0; // velikost okna //--------------------------------------------------------------------- // Nastaveni souradneho systemu v zavislosti na velikosti okna //--------------------------------------------------------------------- void onResize(int w, int h) // w a h reprezentuje novou velikost okna { glViewport(0, 0, w, h); // viditelna oblast pres cele okno glMatrixMode(GL_PROJECTION); // zacatek modifikace projekcni matice glLoadIdentity(); // vymazani projekcni matice (=identita) glOrtho(0, w, 0, h, -1, 1); // mapovani abstraktnich souradnic do souradnic okna glScalef(1, -1, 1); // inverze y-ove osy, aby se y zvetsovalo smerem dolu glTranslatef(0, -h, 0); // posun pocatku do leveho horniho rohu width=w; // zapamatovat si velikost okna height=h; } //--------------------------------------------------------------------- // Tato funkce je volana pri kazdem prekresleni okna //--------------------------------------------------------------------- void onDisplay(void) { glClear(GL_COLOR_BUFFER_BIT); // vymazani bitovych rovin barvoveho bufferu glBegin(GL_TRIANGLES); glColor3f(firstColor[0], firstColor[1], firstColor[2]);// barva prvniho vertexu glVertex2i(width>>1, 0); // pozice prvniho vertexu glColor3f(secondColor[0], secondColor[1], secondColor[2]);// barva druheho vertexu glVertex2i(0, height-1); // pozice druheho vertexu glColor3f(thirdColor[0], thirdColor[1], thirdColor[2]);// barva tretiho vertexu glVertex2i(width-1, height-1); // pozice tretiho vertexu glEnd(); glFlush(); // provedeni a vykresleni zmen } //--------------------------------------------------------------------- // Tato funkce je volana pri stlaceni ASCII klavesy //--------------------------------------------------------------------- void onKeyboard(unsigned char key, int x, int y) { key=(key>'A' && key<='Z') ? key+'a'-'A':key; // prevod na mala pismena switch (key) { case 27: // Escape exit(0); break; case 'f': glutFullScreen(); // prepnuti na celou obrazovku break; case 'w': glutReshapeWindow(400, 400);// prepnuti zpet do okna glutPositionWindow(50, 50); break; default: break; } } //--------------------------------------------------------------------- // Tato funkce je zavolana pri vyberu polozky z prvniho menu //--------------------------------------------------------------------- void onFirstMenuCommand(int command) { if (((command >> 24) & 0xff)==1) { // zmena barvy prvniho vertexu firstColor[0]=((command >> 16) & 0xff)/255.0; firstColor[1]=((command >> 8) & 0xff)/255.0; firstColor[2]=(command & 0xff)/255.0; } if (((command >> 24) & 0xff)==2) { // zmena barvy druheho vertexu secondColor[0]=((command >> 16) & 0xff)/255.0; secondColor[1]=((command >> 8) & 0xff)/255.0; secondColor[2]=(command & 0xff)/255.0; } if (((command >> 24) & 0xff)==3) { // zmena barvy tretiho vertexu thirdColor[0]=((command >> 16) & 0xff)/255.0; thirdColor[1]=((command >> 8) & 0xff)/255.0; thirdColor[2]=(command & 0xff)/255.0; } glutPostRedisplay(); // prekresleni okna } //--------------------------------------------------------------------- // Tato funkce je zavolana pri vyberu polozky z druheho menu //--------------------------------------------------------------------- void onSecondMenuCommand(int command) { switch (command) { case Quit: exit(0); // pri vyberu prikazu Quit ukoncime aplikace break; case FullScreen: // prikaz pro prepnuti na celou obrazovku glutFullScreen(); break; case Window: // prikaz pro prepnuti zpatky do okna glutReshapeWindow(400, 400); glutPositionWindow(50, 50); break; default: break; } } //--------------------------------------------------------------------- // Tato funkce vytvori menu a navaze ho na leve tlacitko mysi //--------------------------------------------------------------------- void createMenu(void) { int i, j, k; i=glutCreateMenu(onFirstMenuCommand); // vytvoreni prvniho submenu glutAddMenuEntry("Black", BlackColor | (1<<24));// pridani polozek do submenu glutAddMenuEntry("Gray", GrayColor | (1<<24)); glutAddMenuEntry("Red", RedColor | (1<<24)); glutAddMenuEntry("Green", GreenColor | (1<<24)); glutAddMenuEntry("Blue", BlueColor | (1<<24)); glutAddMenuEntry("White", WhiteColor | (1<<24)); j=glutCreateMenu(onFirstMenuCommand); // vytvoreni druheho submenu glutAddMenuEntry("Black", BlackColor | (2<<24));// pridani polozek do submenu glutAddMenuEntry("Gray", GrayColor | (2<<24)); glutAddMenuEntry("Red", RedColor | (2<<24)); glutAddMenuEntry("Green", GreenColor | (2<<24)); glutAddMenuEntry("Blue", BlueColor | (2<<24)); glutAddMenuEntry("White", WhiteColor | (2<<24)); k=glutCreateMenu(onFirstMenuCommand); // vytvoreni tretiho submenu glutAddMenuEntry("Black", BlackColor | (3<<24));// pridani polozek do submenu glutAddMenuEntry("Gray", GrayColor | (3<<24)); glutAddMenuEntry("Red", RedColor | (3<<24)); glutAddMenuEntry("Green", GreenColor | (3<<24)); glutAddMenuEntry("Blue", BlueColor | (3<<24)); glutAddMenuEntry("White", WhiteColor | (3<<24)); glutCreateMenu(onFirstMenuCommand); // vytvoreni kaskadoveho menu glutAddSubMenu("First vertex", i); // pridani vsech submenu glutAddSubMenu("Second vertex", j); glutAddSubMenu("Third vertex", k); glutAttachMenu(GLUT_LEFT_BUTTON); i=glutCreateMenu(onSecondMenuCommand); // vytvoreni prvniho submenu glutAddMenuEntry("Yes", Quit); // pridani polozek do submenu glutAddMenuEntry("No", NoQuit); glutCreateMenu(onSecondMenuCommand); // vytvoreni kaskadoveho menu glutAddMenuEntry("Full screen", FullScreen); glutAddMenuEntry("Window", Window); glutAddSubMenu("Quit", i); // pridani submenu glutAttachMenu(GLUT_RIGHT_BUTTON); } //--------------------------------------------------------------------- // Hlavni funkce konzolove aplikace //--------------------------------------------------------------------- int main(int argc, char **argv) { glutInit(&argc, argv); // inicializace knihovny GLUT glutCreateWindow("Priklad cislo 13");// vytvoreni okna pro kresleni glutReshapeWindow(400, 400); // nastaveni velikosti okna glutDisplayFunc(onDisplay); // registrace funkce volane pri prekreslovani okna glutReshapeFunc(onResize); // registrace funkce volane pri zmene velikosti okna glutKeyboardFunc(onKeyboard); // registrace funkce volani pri stlaceni ASCII klavesy createMenu(); // vytvoreni menu a registrace callback funkce glutMainLoop(); // nekonecna smycka, kde se volaji zaregistrovane fce return 0; // navratova hodnota vracena operacnimu systemu } //--------------------------------------------------------------------- // Konec zdrojoveho souboru //---------------------------------------------------------------------