Comp 410/510 Computer Graphics Spring Input & Interaction

Similar documents
Project Sketchpad. Ivan Sutherland (MIT 1963) established the basic interactive paradigm that characterizes interactive computer graphics:

Working with Callbacks

Input and Interaction

Lecture 11: Callbacks. CITS 3003 Graphics & Animation

Input, Interaction and Animation Week 2

Lecture 4. Interaction / Graphical Devices. CS 354 Computer Graphics Sunday, January 20, 13

Input and Interaction. CS 537 Interactive Computer Graphics Prof. David E. Breen Department of Computer Science

Input and Interaction. Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts University of New Mexico

CS 4204 Computer Graphics

CS 432 Interactive Computer Graphics

Lecture 3 Advanced Computer Graphics (CS & SE )

Lecture 10: Input, Interaction & callbacks. CITS 3003 Graphics & Animation

Computer Graphics (CS 543) Lecture 2b: 2D Graphics Systems (Drawing Polylines, tiling, & Aspect Ratio)

Input and Interaction. 1. Interaction. Chapter 3. Introduction: - We now turn to the development of interactive graphics programs.

CSC Graphics Programming. Budditha Hettige Department of Statistics and Computer Science

Lecture 2 CISC440/640 Spring Department of Computer and Information Science

Computer Graphics. OpenGL

Introduction to Computer Graphics with OpenGL/GLUT

// double buffering and RGB glutinitdisplaymode(glut_double GLUT_RGBA); // your own initializations

Computer Graphics. Bing-Yu Chen National Taiwan University

Comp 410/510 Computer Graphics Spring Programming with OpenGL Part 4: Three Dimensions

RECITATION - 1. Ceng477 Fall

Computer Graphics CS 543 Lecture 5 (Part 2) Implementing Transformations

GL_COLOR_BUFFER_BIT, GL_PROJECTION, GL_MODELVIEW

COMPUTER GRAPHICS LAB # 3

OPENGL TM AND X, COLUMN 1: AN OPENGL TOOLKIT

Order of Transformations

VR-programming tools (procedural) More VRML later in this course! (declarative)

Drawing Primitives. OpenGL basics

OpenGL and X, Column 1: An OpenGL Toolkit

CS 543 Lecture 1 (Part II): Intro to OpenGL and GLUT (Part I) Emmanuel Agu

CS 432 Interactive Computer Graphics

VTU EDUSAT PROGRAMME. Computer Graphics & Visualization

GLUT Tutorial. Keyboard

Computer Graphics (CS 4731) Lecture 11: Implementing Transformations. Prof Emmanuel Agu. Computer Science Dept. Worcester Polytechnic Institute (WPI)

Computer Graphics (CS 543) Lecture 1 (Part 2): Introduction to OpenGL/GLUT (Part 1)

Interaction. CSCI 480 Computer Graphics Lecture 3

To Do. Computer Graphics (Fall 2008) Course Outline. Course Outline. Methodology for Lecture. Demo: Surreal (HW 3)

Computer Graphics (CS 4731) OpenGL/GLUT (Part 2)

Computer Graphics (4731) Lecture 4: 2D Graphics Systems (Drawing Polylines, tiling, & Aspect Ratio)

CMSC 425: Lecture 4 More about OpenGL and GLUT Tuesday, Feb 5, 2013

Computer Graphics 1 Computer Graphics 1

Exercise 1 Introduction to OpenGL

Programming using OpenGL: A first Introduction

CS 4731 Lecture 3: Introduction to OpenGL and GLUT: Part II. Emmanuel Agu

Modeling Transform. Chapter 4 Geometric Transformations. Overview. Instancing. Specify transformation for objects 李同益

Interaction Computer Graphics I Lecture 3

Computer Graphics Course 2005

Computer graphics MN1

C OMPUTER G RAPHICS Thursday

Teacher Assistant : Tamir Grossinger Reception hours: by - Building 37 / office -102 Assignments: 4 programing using

Interaction. CSCI 420 Computer Graphics Lecture 3

Input and Interaction

Early History of APIs. PHIGS and X. SGI and GL. Programming with OpenGL Part 1: Background. Objectives

Computer Graphics (CS 4731) Lecture 11: Implementing Transformations. Prof Emmanuel Agu. Computer Science Dept. Worcester Polytechnic Institute (WPI)

ERKELEY DAVIS IRVINE LOS ANGELES RIVERSIDE SAN DIEGO SAN FRANCISCO EECS 104. Fundamentals of Computer Graphics. OpenGL

2/3/16. Interaction. Triangles (Clarification) Choice of Programming Language. Buffer Objects. The CPU-GPU bus. CSCI 420 Computer Graphics Lecture 3

Computer Graphics (CS 4731) OpenGL/GLUT(Part 1)

Lecture 2 Sections 3.6, 3.7. Fri, Aug 28, 2009

C++ is Fun Part 13 at Turbine/Warner Bros.! Russell Hanson

Programming with OpenGL Part 1: Background

CMSC 427 Computer Graphics 1

CS Computer Graphics: Intro to OpenGL

CS Computer Graphics: Intro to OpenGL

Lectures OpenGL Introduction

CS559: Computer Graphics. Lecture 12: OpenGL Li Zhang Spring 2008

CS Computer Graphics: OpenGL, Continued

CS Computer Graphics: OpenGL, Continued

Programming with OpenGL Part 3: Three Dimensions

OpenGL refresher. Advanced Computer Graphics 2012

2. OpenGL -I. 2.1 What is OpenGL? Things OpenGL can do: -23-

Cameras (and eye) Ideal Pinhole. Real Pinhole. Real + lens. Depth of field

OpenGL/GLUT Intro. Week 1, Fri Jan 12

CS 4204 Computer Graphics

CSCI 4620/8626. The 2D Viewing Pipeline

Input and Interaction. Input and Interaction

Input and Interaction

3D computer graphics: geometric modeling of objects in the computer and rendering them

CSC 470 Computer Graphics

Precept 2 Aleksey Boyko February 18, 2011

Computer Graphics (Basic OpenGL)

Image Processing. Geometry Processing. Reading: (Not really covered in our text. See Sects 18.1, 18.2.) Overview: Display

DONALD HOUSE: CPSC 8170, FALL 2018 DESIGN OF A REAL-TIME ANIMATION PROGRAM

SOURCES AND URLS BIBLIOGRAPHY AND REFERENCES

Transformations. CS 537 Interactive Computer Graphics Prof. David E. Breen Department of Computer Science

Comp 410/510 Computer Graphics Spring Programming with OpenGL Part 2: First Program

Computer Graphics CS 543 Lecture 4 (Part 2) Building 3D Models (Part 2)

Computer Graphics. Downloaded from: LectureNotes 1 CSC-254

Announcements OpenGL. Computer Graphics. Spring CS4815

Introduction to OpenGL

Lecture 4 of 41. Lab 1a: OpenGL Basics

USHTRIME NGA GRAFIKA KOMPJUTERIKE

Information Coding / Computer Graphics, ISY, LiTH. OpenGL! ! where it fits!! what it contains!! how you work with it 11(40)

API Background. Prof. George Wolberg Dept. of Computer Science City College of New York

Objectives. transformation. General Transformations. Affine Transformations. Notation. Pipeline Implementation. Introduce standard transformations

Better Interactive Programs. Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts University of New Mexico

1 (Practice 1) Introduction to OpenGL

Introduce the basic input devices Input, Interaction and Animation

CS 380 Introduction to Computer Graphics. LAB (1) : OpenGL Tutorial Reference : Foundations of 3D Computer Graphics, Steven J.

Abel J. P. Gomes LAB. 1. INTRODUCTION TO OpenGL

Transcription:

Comp 410/510 Computer Graphics Spring 2018 Input & Interaction

Objectives Introduce the basic input devices - Physical Devices - Logical Devices Event-driven input Input Modes Programming event input with GLUT

Basic interaction Ivan Sutherland (MIT 1963) established the basic interactive paradigm that characterizes interactive computer graphics: - User sees an object on the display - User points to (picks) the object with an input device (light pen, mouse, trackball, etc.) - Object changes (moves, rotates, morphs) - Repeat

Graphical Input Input devices can be described by - Physical properties Mouse Keyboard Trackball - Logical Properties What is returned to program via API A position An object identifier An action identifier Modes - How and when input is obtained Request or event

Graphical Logical Devices Graphical input is more varied than input to standard programs, which is usually numbers, characters, or bits Examples to types of logical input: - Locator: return a position - Pick: return ID of an object - Keyboard: return strings of characters - Stroke: return array of positions - Valuator: return floating point number (such as slidebars) - Choice: return one of n items (such as menus)

Input Modes Input devices contain a trigger which can be used to send a signal to the operating system - Button on mouse - Pressing or releasing a key on the keyboard When triggered, input devices return information (their measure) to the system - Mouse returns position information - Keyboard returns ASCII code Request or Event Mode

Request Mode Program requests an input Input is provided to the program when user triggers the device Typical of keyboard input - Can erase (backspace), edit, correct until enter (return) key (the trigger) is depressed

Event Mode Program waits for inputs that user may possibly provide Most systems have more than one input device, each of which can be triggered at an arbitrary time by a user Each trigger generates an event whose measure is put in an event queue which can be examined by the user program

Event Types Window: resize, expose, iconify Mouse: click one or more buttons Motion: move mouse Keyboard: press or release a key Idle: non-event - Define what should be done if no other event is in queue

Callbacks Programming interface for event-driven input Define a callback function for each type of event the graphics system recognizes This user-supplied function is executed when the event occurs GLUT example: glutmousefunc(mymouse) mouse callback function

GLUT callbacks GLUT recognizes a subset of the events recognized by any particular window system (Windows, X, Mac OS, etc.) - glutdisplayfunc(mydisplay) - glutmousefunc(mymouse) - glutreshapefunc(myreshape) - glutkeyboardfunc(mykeyboard) - glutidlefunc(myidle) - glutmotionfunc(mymotion), glutpassivemotionfunc

GLUT Event Loop Remember that the last line in main.c for a program using GLUT must be glutmainloop(); which puts the program in an infinite event loop In each pass through the event loop, GLUT - looks at the events in the queue - for each event in the queue, GLUT executes the appropriate callback function if one is defined - if no callback is defined for the event, the event is ignored

The display callback The display callback is executed whenever GLUT determines that the window should be refreshed, for example - When the window is first opened - When the window is reshaped - When a window is exposed - When the user program decides to change the display e.g., using glutpostredisplay() In main.c - glutdisplayfunc(mydisplay) identifies the display callback function to be executed - Every GLUT program must have a display callback

#include <GL/glut.h> void myinit(){... } void mydisplay(){... } void mymouse(int button, int state, int x, int y){... } void mykey(unsigned char key, int x, int y){... } void myidle(){... } void mymotion(int x, int y){... } void myreshape(int w, int h){... } int main(int argc, char** argv) { /* window intializations*/ glutdisplayfunc(mydisplay); glutmousefunc(mymouse); glutreshapefunc(myreshape); glutkeyboardfunc(mykey); glutidlefunc(myidle); glutmotionfunc(mymotion); myinit(); } glutmainloop();

The mouse callback glutmousefunc(mymouse) void mymouse(glint button, GLint state, GLint x, GLint y) Is returned - which button (GLUT_LEFT_BUTTON, GLUT_MIDDLE_BUTTON, GLUT_RIGHT_BUTTON) causes the event - state of that button (GL_UP, GLUT_DOWN) - cursor position in window

Terminating a program We can use a simple mouse callback function to terminate the program execution through OpenGL void mouse(int btn, int state, int x, int y) { if(btn==glut_right_button && state==glut_down) exit(0); }

Using globals The form of all GLUT callbacks is fixed - void mydisplay() - void mymouse(glint button, GLint state, GLint x, GLint y) Must use global variables to pass information to callbacks: float t; /*global */ void mydisplay() { // draw something that depends on t }

Using the mouse position In the next example, we draw a small square at the location of the mouse each time the left mouse button is clicked

Drawing squares at cursor location void drawsquare(int x, int y) { gluniform2f( Cursor, x, y ); gluniform3f( RandomColor, ((float) rand()) / RAND_MAX, ((float) rand())/rand_max, ((float) rand())/rand_max ); glutpostredisplay(); } void mouse(int btn, int state, int x, int y) { if(btn==glut_right_button && state==glut_down) exit(0); if(btn==glut_left_button && state==glut_down){ drawsquare(x, y); } } See drawsmallsquares example

Positioning The cursor position in the screen window is usually measured in pixels with the origin at the top-left corner That is because the refresh is done from top to bottom OpenGL uses a default world coordinate system with origin located at the center of the window Must invert y coordinate (returned by callback) using the height h of window: y = h y; (0,0) h (0,0) (-1,-1) w See the vertex shader code

Obtaining the window size To invert the y position we need to know the window height - Height h can change during program execution - Can track with a global variable - New height is returned to reshape callback that we will look at in detail soon - Can also use enquiry functions glutget(glut_window_y) glgetintv( ) glgetfloatv( ) to obtain any value that is part of the state

Using the motion callback We can draw squares (or anything else) continuously as long as a mouse button is depressed by using the motion callback: - glutmotionfunc(drawsquare) We can draw squares without depressing a button using the passive motion callback: - glutpassivemotionfunc(drawsquare) The system returns to the callback function the x and y positions of the cursor.

Using the keyboard glutkeyboardfunc(mykey) void mykey(unsigned char key,int x, int y) - Is returned the ASCII code of the key depressed and the mouse location - Note that GLUT does not recognize the key release as an event void mykey(unsigned char key,int x, int y) { if(key == Q key == q ) exit(0); }

Special and Modifier Keys GLUT defines the special keys in glut.h - Function key 1: GLUT_KEY_F1 - Up arrow key: GLUT_KEY_UP if(key == GLUT_KEY_F1 Can also check one of the modifiers - GLUT_ACTIVE_SHIFT - GLUT_ACTIVE_CTRL - GLUT_ACTIVE_ALT is depressed by glutgetmodifiers() - Allows emulation of three-button mouse with one- or two-button mice

Reshaping the window We can reshape and resize the OpenGL display window by pulling the corner of the window What happens to the display? - Must redraw the scene from application - Three possibilities Display part of the world Display the whole world but force to fit in new window Can alter aspect ratio Display with modified view volume (or area)

Reshape possibilities original reshaped

The Reshape callback glutreshapefunc(myreshape) void myreshape(int w, int h) - Is returned width and height of the new resized window (in pixels) - A redisplay is posted automatically at the end of execution of the callback - GLUT has a default reshape callback but you probably want to define your own The reshape callback is a good place to put camera functions because it is also invoked when the window is first opened

Example Reshape The reshape callback below preserves shapes by making the viewport and the view volume have the same aspect ratio void reshape( int w, int h ) { glviewport( 0, 0, w, h ); } mat4 projection; if (w <= h) projection = Ortho(-1.0, 1.0, -1.0 * (GLfloat) h / (GLfloat) w, 1.0 * (GLfloat) h / (GLfloat) w,-1.0,1.0); else projection = Ortho(-1.0* (GLfloat) w / (GLfloat) h, 1.0 * (GLfloat) w / (GLfloat) h, -1.0, 1.0,-1.0,1.0); gluniformmatrix4fv( Projection, 1, GL_TRUE, projection ); See spincuberevisited example

Posting redisplays Many events may invoke the display callback function - Can lead to multiple executions of the display callback on a single pass through the event loop We can avoid this problem by instead using glutpostredisplay(); which sets a flag. GLUT checks to see if the flag is set at the end of the event loop If set then the display callback function is executed void idle( void ) { Theta[Axis] += 4.0; if ( Theta[Axis] > 360.0 ) { Theta[Axis] -= 360.0; } } glutpostredisplay();

Using gluttimerfunc void gluttimerfunc(unsigned int msecs, void (*func)(int value), value); gluttimerfunc registers the timer callback func to be triggered in at least msecs milliseconds. void mytimerfunc () { /* change something */ t += dt; glutpostredisplay(); gluttimerfunc(20,mytimerfunc,0); } int main(int argc, char **argv) { gluttimerfunc(20,mytimerfunc,0); } See spincuberevisited example

Hidden-Surface Removal We want to see only those surfaces in front of other surfaces OpenGL uses a hidden-surface removal method called the z-buffer algorithm Saves depth information as objects are rendered so that only the front objects appear in the image

Using the z-buffer algorithm The algorithm uses an extra buffer, the z-buffer, to store depth information as geometry travels down the pipeline It must be - Requested in main() glutinitdisplaymode (GLUT_SINGLE GLUT_RGB GLUT_DEPTH) - Enabled in init() glenable(gl_depth_test) - Cleared in the display callback glclear(gl_color_buffer_bit GL_DEPTH_BUFFER_BIT)

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 - Input boxes But toolkits tend to be platform dependent GLUT is platform-independent and provides a few widgets including pop-up menus

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

Defining a simple menu In main.c callback function glutcreatemenu(mymenu); glutaddmenuentry( clear screen, 1); glutaddmenuentry( exit, 2); glutattachmenu(glut_right_button); clear screen exit entries that appear when right button depressed identifiers

Menu actions - Menu callback void mymenu(int id) { if(id == 1) glclear(); if(id == 2) exit(0); } - Note each menu item has an id that is returned when selected - Add submenus by glutaddsubmenu(char *submenu_name, submenu id) entry in parent menu

Example c_menu = glutcreatemenu(color_menu); glutaddmenuentry("red",1); glutaddmenuentry("green",2);... glutaddmenuentry("black",8); p_menu = glutcreatemenu(pixel_menu); glutaddmenuentry("increase pixel size", 1); glutaddmenuentry("decrease pixel size", 2); f_menu = glutcreatemenu(fill_menu); glutaddmenuentry("fill on", 1); glutaddmenuentry("fill off", 2); glutcreatemenu(right_menu); glutaddmenuentry("quit",1); glutaddmenuentry("clear",2); glutattachmenu(glut_right_button); See the example in the textbook (Appendix A.9) glutcreatemenu(middle_menu); glutaddsubmenu("colors", c_menu); glutaddsubmenu("pixel Size", p_menu); glutaddsubmenu("fill", f_menu); glutattachmenu(glut_middle_button);

Other functions in GLUT Dynamic Windows - Create and destroy during execution Subwindows Multiple Windows Changing callbacks during execution Portable fonts

Picking Identifying 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 a position corresponds Practical difficulties - Pipeline architecture is fed forward, hard to go from screen back to world - Complicated by screen being 2D, world is 3D - How close should we come to object to say we ve selected it? See picking example

Two Approaches 1. Use of some other buffer to store object ids as the objects are rendered 2. Use of rectangular maps Easy to implement for many applications

Using Fixed Regions of the Screen Many applications use a simple rectangular arrangement of the screen - Example: paint/cad program tools drawing area menus Easier to look at mouse position and determine which area of screen it is in than using more sophisticated techniques

Using another buffer and colors for picking We first assign a unique color to each object (not necessarily the true color of the object) We then render the scene to a color buffer other than the front buffer, so the results of the rendering are not visible We then get the mouse position and use gldrawbuffer() and glreadpixels() functions to read the color in the buffer, that we just wrote at the position of the mouse The returned color gives the id of the object