#include<stdio.h> #include<math.h> #include<GL/glut.h> int width=400,height=400; void myInit() { glClearColor(1.0,1.0,1.0,1.0); glColor3f(1.0,0.0,0.0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0.0,10.0,0.0,10.0); glMatrixMode(GL_MODELVIEW); } void square() { glClear(GL_COLOR_BUFFER_BIT); //glColor3f(1.0,0.0,0.0); glBegin(GL_POLYGON); glVertex2i(1,1); glVertex2i(9,1); glVertex2i(9,9); glVertex2i(1,9); glEnd(); glutSwapBuffers(); glFlush(); } void triangle() { glClear(GL_COLOR_BUFFER_BIT); // glColor3f(1.0,0.0,1.0); glBegin(GL_POLYGON); glVertex2i(1,1); glVertex2i(5,9); glVertex2i(9,1); glEnd(); glutSwapBuffers(); glFlush(); } void circle() { int r=4; float theta; glClear(GL_COLOR_BUFFER_BIT); glPointSize(4.0); // glColor3f(0.0,1.0,1.0); glBegin(GL_POLYGON); for(theta=0;theta<=360;theta+=0.01){ glVertex2f(r*cos(theta*3.142/180)+5,r*sin(theta*3.142/180)+5); } glEnd(); glutSwapBuffers(); glFlush(); } void myDisplay() { glClear(GL_COLOR_BUFFER_BIT); glFlush(); } void subMenu(int id){ //glClear(GL_COLOR_BUFFER_BIT); printf("subMenu called with id: %d\n",id); switch(id){ case 1: glColor3f(1.0,0.0,0.0); break; case 2: glColor3f(0.0,1.0,0.0); break; case 3: glColor3f(0.0,0.0,1.0); break; case 4: glColor3f(0.0,1.0,1.0); break; // case 4: exit(0); break; } } void myMenu(int id){ printf("MyMenu called with id: %d\n",id); switch(id){ case 1: triangle(); break; case 2: circle(); break; case 3: square(); break; case 4: exit(0); break; } } void myKey(unsigned char key, int x,int yy){ if(key=='q' || key == 'Q') exit(0); } int main(int argc, char **argv){ int menu_id; glutInit(&argc,argv); glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB); glutInitWindowSize(400,400); glutInitWindowPosition(100,100); glutCreateWindow("Input Interaction"); glutDisplayFunc(myDisplay); glutKeyboardFunc(myKey); int sub_menu=glutCreateMenu(subMenu); glutAddMenuEntry("red",1); glutAddMenuEntry("blue",3); glutAddMenuEntry("green",2); glutAddMenuEntry("yellow",4); menu_id=glutCreateMenu(myMenu); glutAddMenuEntry("Triangle",1); glutAddMenuEntry("Circle",2); glutAddMenuEntry("Square",3); glutAddSubMenu("Colours",sub_menu); glutAddMenuEntry("exit",4); glutAttachMenu(GLUT_RIGHT_BUTTON); myInit(); glutMainLoop(); return 0; }
0 comments:
Post a Comment