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

Slides:



Advertisements
Similar presentations
Better Interactive Programs
Advertisements

Chapter 3: Input and Interaction Instructor: Shih-Shinh Huang 1.
CSC 461: Lecture 51 CSC461 Lecture 5: Simple OpenGL Program Objectives: Discuss a simple program Discuss a simple program Introduce the OpenGL program.
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Programming with OpenGL Part 2: Complete Programs Ed Angel Professor of Computer Science,
Objectives Learn to build arbitrary transformation matrices from simple transformations Learn to build arbitrary transformation matrices from simple transformations.
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Better Interactive Programs Ed Angel Professor of Computer Science, Electrical and Computer.
InteractionHofstra University1 Graphics Programming Input and Interaction.
31/1/2006Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition)1 CSC345: Advanced Graphics & Virtual Environments Lecture 3: Introduction.
InteractionHofstra University1 Graphics Programming Input and Interaction.
1 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 GLUT Callback Functions.
CSC461 Lecture 9: GLUT Callbacks Objectives Introduce double buffering for smooth animations Programming event input with GLUT.
CS 480/680 Computer Graphics Programming with Open GL Part 8: Working with Callbacks Dr. Frederick C Harris, Jr. Fall 2011.
Using Graphics Libraries Lecture 3 Mon, Sep 1, 2003.
Demetriou/Loizidou – ACSC330 – Chapter 3 Input and Interaction Dr. Giorgos A. Demetriou Dr. Stephania Loizidou Himona Computer Science Frederick Institute.
Introduction to OpenGL and GLUT GLUT. What is OpenGL? An application programming interface (API) A (low-level) Graphics rendering API Generate high-quality.
1 Working with Callbacks Yuanfeng Zhou Shandong University.
COMPUTER GRAPHICS Hochiminh city University of Technology Faculty of Computer Science and Engineering CHAPTER 03: Input & Interaction.
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Chapter 3: Input and Interaction.
More on Drawing in OpenGL: Examples CSC 2141 Introduction to Computer Graphics.
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Programming with OpenGL Part 3: Three Dimensions Ed Angel Professor of Computer Science,
WORKING WITH CALLBACKS Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts University of New Mexico Angel: Interactive.
Fundamentals of Computer Graphics Part 3 prof.ing.Václav Skala, CSc. University of West Bohemia Plzeň, Czech Republic ©2002 Prepared with Angel,E.: Interactive.
Interaction with Graphics System
Input and Interaction Chapter 3. CS 480/680 2Chapter 3 -- Input and Interaction Introduction: Introduction: We now turn to the development of interactive.
CAP 4703 Computer Graphic Methods Prof. Roy Levow Lecture 3.
Input and Interaction Lecture No. 4.
Lecture 3 OpenGL.
1 Input and Interaction. 2 Input Devices Physical input devices Keyboard devices and pointing devices Logical input devices.
 “OpenGL (Open Graphics Library) is a standard specification defining a cross- language cross-platform API for writing applications that produce 2D and.
Ch 2 Graphics Programming page 1 CSC 367 Coordinate Systems (2.1.2) Device coordinates, or screen coordinates (pixels) put limitations on programmers and.
Computing & Information Sciences Kansas State University Lecture 20 of 42CIS 636/736: (Introduction to) Computer Graphics Lecture 21 of 42 William H. Hsu.
DREAM PLAN IDEA IMPLEMENTATION Introduction to Computer Graphics Dr. Kourosh Kiani
1 Better Interactive Programs. 2 Objectives Learn to build more sophisticated interactive programs using ­Picking Select objects from the display Three.
Introduction to OpenGL and GLUT. What’s OpenGL? An Application Programming Interface (API) A low-level graphics programming API – Contains over 250 functions.
Computer Graphics: Programming, Problem Solving, and Visual Communication Steve Cunningham California State University Stanislaus and Grinnell College.
Computer Graphics I, Fall 2010 Working with Callbacks.
1 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 OpenGL Transformations.
Input and Interaction Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts University of New Mexico.
Pop-Up Menus Glenn G. Chappell U. of Alaska Fairbanks CS 381 Lecture Notes Friday, September 26, 2003.
GLUT functions glutInit allows application to get command line arguments and initializes system gluInitDisplayMode requests properties for the window.
Doç. Dr. Cemil Öz SAÜ Bilgisayar Mühendisliği Dr. Cemil Öz.
Chap 3 Viewing and Transformation
OpenGL Basic Drawing 2003 Spring Keng Shih-Ling
1 Programming with OpenGL Part 2: Complete Programs.
OpenGL API 2D Graphic Primitives Angel Angel: Interactive Computer Graphics5E © Addison-Wesley
12/22/ :38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert.
CS559: Computer Graphics Lecture 12: OpenGL - Transformation Li Zhang Spring 2008.
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Input and Interaction Ed Angel Professor of Computer Science, Electrical and Computer Engineering,
CS559: Computer Graphics Lecture 12: OpenGL: ModelView Li Zhang Spring 2010.
OpenGL Basic Drawing Jian-Liang Lin A Smidgen of OpenGL Code #include main() { InitializeAWindowPlease(); glClearColor (0.0, 0.0, 0.0, 0.0); glClear.
Computer Graphics I, Fall Programming with OpenGL Part 2: Complete Programs.
1 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 Working with Callbacks.
CSC Graphics Programming Budditha Hettige Department of Statistics and Computer Science.
INTRODUCTION TO OPENGL
Review GLUT Callback Functions
Working with Callbacks
Better Interactive Programs
CS5500 Computer Graphics March 2, 2006.
Programming with OpenGL Part 2: Complete Programs
Programming with OpenGL Part 2: Complete Programs
Advanced Menuing, Introduction to Picking
גרפיקה ממוחשבת: מבוא ל-OpenGL
Input and Interaction Chapter 3.
Input and Interactions
Working with Callbacks
Better Interactive Programs
Fundamentals of Computer Graphics Part 3
Programming with OpenGL Part 2: Complete Programs
Programming with OpenGL Part 2: Complete Programs
Programming with OpenGL Part 2: Complete Programs
Presentation transcript:

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

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

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

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

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

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

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

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);

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();}

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();}

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();}

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();}

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); }

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 ();}

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); }

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?

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

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

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

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