Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC461 Lecture 10: Widgets and Picking Objectives Introduce menus in GLUT Picking –Select objects from the display –Three methods Put things together.

Similar presentations


Presentation on theme: "CSC461 Lecture 10: Widgets and Picking Objectives Introduce menus in GLUT Picking –Select objects from the display –Three methods Put things together."— Presentation transcript:

1 CSC461 Lecture 10: Widgets and Picking Objectives Introduce menus in GLUT Picking –Select objects from the display –Three methods Put things together

2 Toolkits and Widgets Most window systems provide a toolkit or library of functions for building user interfaces that use special types of windows called widgets Widget sets include tools such as –Menus –Slidebars –Dials –Input boxes But toolkits tend to be platform dependent GLUT provides a few widgets including menus

3 Menus GLUT supports pop-up menus –A menu can have submenus Three steps –Define entries for the menu –Define action for each menu item Action carried out if entry selected –Attach menu to a mouse button

4 Defining a simple menu In main.c menu_id = glutCreateMenu(mymenu); glutAddmenuEntry(“clear Screen”, 1); gluAddMenuEntry(“exit”, 2); glutAttachMenu(GLUT_RIGHT_BUTTON); entries that appear when right button depressed identifiers clear screen exit

5 Menu actions –Menu callback –Note each menu has an id that is returned when it is created –Add submenus by glutAddSubMenu(char *submenu_name, submenu id) void mymenu(int id) { if(id == 1) glClear(); if(id == 2) exit(0); } entry in parent menu

6 Other functions in GLUT Dynamic Windows –Create and destroy during execution Subwindows Multiple Windows Changing callbacks during execution Timers Portable fonts –glutBitmapCharacter –glutStrokeCharacter

7 Put things together: single_double Demonstrate single and double buffering using a rotate square Two windows, one for each buffering Stop the rotation at will Each window has some callbacks defined for it, but both also share some others

8 Single_double: main static int singleb, doubleb; // window ids static GLFloat theta = 0.0; int main(int argc, char** argv) { glutInit(&argc, argv); /* single buffered window */ glutinitDisplayMode(GLU_SINGLE|GLU_RGB); singleb=glutCreateWindow(“Single buffered”); init();glutDisplayFunc(displays);glutReshapeunc(reshape);glutIdleFunc(sinDisplay);glutMouseFunc(mouse);glutKeyboardFunc(key);

9 Single_double: main (cont.) /* double buffered window */ glutinitDisplayMode(GLU_DOUBLE|GLU_RGB); glutInitWindowPosition(310, 0); doubleb=glutCreateWindow(“Double buffered”); init();glutDisplayFunc(displayd);glutReshapeunc(reshape);glutIdleFunc(sinDisplay);glutMouseFunc(mouse);glutCreateMenu(quit_menu); glutAddMenuEntry(“quit”, 1); glutAttachMenu(GLUT_RIGHT_BUTTON);glutMainLoop();}

10 Single_double: displays void displays() { GLfloat x = sin(DEG_TO_RAD*theta); GLfloat y = cos(DEG_TO_RAD*theta); glClear(GL_COLOR_BUFFER_BIT);glBegin(GL_POLYGON); glVertex2f(x, y); glVertex2f(-x, y); glVertex2f(-x, -y); glVertex2f(x, -y); glEnd();glFlush();}

11 Single_double: displayd void displayd() { GLfloat x = sin(DEG_TO_RAD*theta); GLfloat y = cos(DEG_TO_RAD*theta); glClear(GL_COLOR_BUFFER_BIT);glBegin(GL_POLYGON); glVertex2f(x, y); glVertex2f(-x, y); glVertex2f(-x, -y); glVertex2f(x, -y); glEnd();glSwapBuffers();}

12 Single_double: spinDisplay void spinDisplay(void) { /* increment angle */ theta += 2.0; if (theta > 360.0) theta -= 360.0; // draw single buffered window glutSetWindow(singleb);glutPostWindowRedisplay(singleb); // draw double buffered window glutSetWindow(doubleb);glutPostWindowRedisplay();}

13 Single_double: mouse and init //Two windows share the same mouse callback void mouse(int btn, int state, int x, int y) { if( btn==GLUT_LEFT_BUTTON && state==GLUT_DOWN) glutIdleFunc(spinDisplay); if(btn==GLUT_MIDDLE_BUTTON && state==GLUT_DOWN) glutIdleFunc(NULL); state==GLUT_DOWN) glutIdleFunc(NULL);} void init() // set flat coloring { glClearColor (0.0, 0.0, 0.0, 1.0); glColor3f (1.0, 1.0, 1.0); glShadeModel (GL_FLAT); }

14 Single_double: reshape void reshape(int w, int h) { glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glLoadIdentity(); if (w <= h) // keep the aspect ration if (w <= h) // keep the aspect ration glOrtho (-50.0, 50.0, -50.0*(GLfloat)h/(GLfloat)w, 50.0*(GLfloat)h/(GLfloat)w, -1.0, 1.0); 50.0*(GLfloat)h/(GLfloat)w, -1.0, 1.0); else else glOrtho (-50.0*(GLfloat)w/(GLfloat)h, 50.0*(GLfloat)w/(GLfloat)h, 50.0*(GLfloat)w/(GLfloat)h, -50.0, 50.0, -1.0, 1.0); glMatrixMode(GL_MODELVIEW); glMatrixMode(GL_MODELVIEW); glLoadIdentity (); glLoadIdentity ();}

15 Single_double: key and menu // keyboard callback to exit void key(int key) { if (key==‘Q’ || key==‘q’) exit(0); } // Menu callback to exit Void quit_menu(int id) { if (id ==1 ) exit(0); }

16 Picking Identify a user-defined object on the display In principle, it should be simple because the mouse gives the position and we should be able to determine to which object(s) a position corresponds Practical difficulties –Pipeline architecture is feed forward, hard to go from screen back to world –Complicated by screen being 2D, world is 3D –How close do we have to come to object to say we selected it?

17 Three Approaches Color-based –Use back or some other buffer to store object ids as the objects are rendered Bounding rectangles – extent –Use rectangles to constraint the objects to pick –Easy to implement for many applications –But impractical for 3D images Hit list – selection –Redraw objects in a smaller viewport to find hits –Most general approach but most difficult to implement

18 Color-based Picking– Using another buffer Use back buffer and extra rendering Back buffer not for swap For a small number of objects, can assign a unique color (often in color index) to each object Four steps to implement picking –Draw objects into back buffer with pick colors, each object has a distinct color –Use mouse callback to get the mouse position –Find the corresponding position in the back buffer, read the pixels to get the color glReadPixels(x, y, w, h, format, type, image) returns the color returns the color –Search a table of colors to find the object picked

19 Picking -- Using Regions of the Screen Many applications use a simple rectangular arrangement of the screen –Example: paint/CAD program Easier to look at mouse position and determine which area of screen it is in that using selection mode picking drawing area tools menus

20 Picking -- Bounding Rectangles Each object in the viewport is associated with a small rectangle aligned with axes Use mouse callback to get the cursor position Find the picked object by search the rectangles with the cursor position


Download ppt "CSC461 Lecture 10: Widgets and Picking Objectives Introduce menus in GLUT Picking –Select objects from the display –Three methods Put things together."

Similar presentations


Ads by Google