Gestione degli eventi Daniele Marini Davide Gadia Marco Ronchetti Davide Selmo Corso Di Programmazione Grafica aa2005/2006.

Slides:



Advertisements
Similar presentations
SUJAN CHOWDHURY Lecturer, CSE, CUET
Advertisements

Computer Graphics - Graphics Programming -
CS2401-Computer Graphics &&
Department of nskinfo i-education
1 Gestione degli eventi Daniele Marini. 2 Pipe-line di OGL pixel data vertex data display list pixel operation evaluator rasterization per vertex op &
OpenGL Open a Win32 Console Application in Microsoft Visual C++.
OpenGL CMSC340 3D Character Design & Animation. What is OpenGL? A low-level graphics library specification designed for use with the C and C++ provides…
Coordinate System.
OPEN GL. Install GLUT Download package di sini Dari devcpp, buka Tools->PackageManager-
Line and Curve Drawing Algorithms. Line Drawing x0x0 y0y0 x end y end.
Chapter 2: Graphics Programming
OPENGL.
Computer Graphics CSCE 441
Pemrograman OpenGL Dasar
#include int line_width = 1; void Display( void ) { glEnable( GL_LINE_STIPPLE ); glClearColor (0.0, 0.0, 0.0, 0.0); glClear(GL_COLOR_BUFFER_BIT);
© 2004, Tom Duff and George Ledin Jr1 Lectures OpenGL Introduction By Tom Duff Pixar Animation Studios Emeryville, California and George Ledin Jr Sonoma.
OpenGL (Graphics Library) Software Interface to graphics software Allows to create interactive programs that produce color images of moving 3D objects.
CSC461 Lecture 10: Widgets and Picking Objectives Introduce menus in GLUT Picking –Select objects from the display –Three methods Put things together.
CSC 461: Lecture 51 CSC461 Lecture 5: Simple OpenGL Program Objectives: Discuss a simple program Discuss a simple program Introduce the OpenGL program.
InteractionHofstra University1 Graphics Programming Input and Interaction.
Device Independent Graphics and OpenGL
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.
Gestione degli eventi Daniele Marini Corso Di Programmazione Grafica e Laboratorio.
Introduction to OpenGL M. Ramanathan STTP CAD 2011Introduction to OpenGL.
Introduction to OpenGL Jian Huang This set of slides are extracted from the Interactive OpenGL Programming course given by Dave Shreine, Ed Angel and Vicki.
Chapter 03: Graphics Primitives Course web page: Chapter #3.
OpenGL A Brief Overview. What is OpenGL?  It is NOT a programming language.  It is a Graphics Rendering API consisting of a set of function with a well.
CAP 4703 Computer Graphic Methods Prof. Roy Levow Lecture 2.
1 Working with Callbacks Yuanfeng Zhou Shandong University.
More on Drawing in OpenGL: Examples CSC 2141 Introduction to Computer Graphics.
WORKING WITH CALLBACKS Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts University of New Mexico Angel: Interactive.
Interaction with Graphics System
Lecture 3 OpenGL.
1 Figures are extracted from Angel's book (ISBN x) The Human Visual System vs The Pinhole camera Human Visual System Visible Spectrum Pinhole.
Introduction to GL Geb Thomas. Example Code int main(int argc, char **argv) { glutInit(&argc, argv); glutInitDisplayMode ( GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
DREAM PLAN IDEA IMPLEMENTATION Introduction to Computer Graphics Dr. Kourosh Kiani
Interactive Computer Graphics CS 418 MP1: Dancing I TA: Zhicheng Yan Sushma S Kini Mary Pietrowicz Slides Taken from: “An Interactive Introduction to OpenGL.
Intro to OpenGL (Version 2) Geb Thomas. Setting Up GLUT You will need GLUT for opening windows We can use the version made by Nate Robins: –
Computer Graphics I, Fall 2010 Working with Callbacks.
Input and Interaction Yuanfeng Zhou Shandong University Software College 1.
Chun-Yuan Lin Introduction to OpenGL 2015/12/19 1 CG.
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.
Graphics: Conceptual Model
OpenGL Basic Drawing 2003 Spring Keng Shih-Ling
CS552: Computer Graphics Lecture 6: Viewing in 2D.
1 Programming with OpenGL Part 2: Complete Programs.
12/22/ :38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert.
31/1/2006Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition)1 CSC345: Advanced Graphics & Virtual Environments Lecture 2: Introduction.
OpenGL Basic Drawing Jian-Liang Lin A Smidgen of OpenGL Code #include main() { InitializeAWindowPlease(); glClearColor (0.0, 0.0, 0.0, 0.0); glClear.
Introduction to OpenGL Muhammad Aamir Khan Lecturer, DCS, UOP.
Introduction to Graphics Programming. Graphics API.
Graphics Graphics Korea University kucg.korea.ac.kr Graphics Programming 고려대학교 컴퓨터 그래픽스 연구실.
Introduction to Graphics Programming. Graphics: Conceptual Model Real Object Human Eye Display Device Graphics System Synthetic Model Synthetic Camera.
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
Computer Graphics (Fall 2003) COMS 4160, Lecture 5: OpenGL 1 Ravi Ramamoorthi Many slides courtesy Greg Humphreys.
The Human Visual System vs The Pinhole camera
Working with Callbacks
CS5500 Computer Graphics March 2, 2006.
OpenGL API 2D Graphic Primitives
OpenGL (Open Graphics Library) Mr. B.A.Swamy Assistant Professor Dept of CSE.
גרפיקה ממוחשבת: מבוא ל-OpenGL
Working with Callbacks
PROGRAM 8 newpaint.c E. Angel.
Programming with OpenGL Part 2: Complete Programs
Presentation transcript:

Gestione degli eventi Daniele Marini Davide Gadia Marco Ronchetti Davide Selmo Corso Di Programmazione Grafica aa2005/2006

Programmazione Grafica aa2005/20062 Eventi principali Move (passive) event - con (senza) bottone, mouse event –glutMouseFunc(mouse_callback) Window event - resize, reshape, move window –glutDisplayFunc(display_callback) Keyboard event –glutKeyboardFunc(keyboard_callback) Window management –glutCreateWindow(“window_name”) –glutSetWindow(window_id)

Programmazione Grafica aa2005/20063 Eventi principali (cont.) Display event - per animazione: –glutPpostDisplay() Idle callback - es. di animazione –glutIdleFunc(idle_callback) Menu events –glutCreateMenu(menu_callback)

Programmazione Grafica aa2005/20064 Esempio completo: programma di paint

Programmazione Grafica aa2005/20065 #define NULL 0 #define LINE 1 #define RECTANGLE 2 #define TRIANGLE 3 #define POINTS 4 #define TEXT 5 /* libreria di interfaccia col window manager */ #include /* Costanti */

Programmazione Grafica aa2005/20066 /* callback principali */ void mouse(int, int, int, int); void key(unsigned char, int, int); void display(void); void drawSquare(int, int); void myReshape(GLsizei, GLsizei); void myinit(void); void screen_box(int, int, int); void right_menu(int); void middle_menu(int); void color_menu(int); void pixel_menu(int); void fill_menu(int); int pick(int, int);

Programmazione Grafica aa2005/20067 /* variabili globali */ GLsizei wh = 500, ww = 500; /* dimensione window*/ GLfloat size = 3.0; /* semilato del quadrato */ int draw_mode = 0; /* modalità disegno */ int rx, ry; /*coordinate pixel (SC)*/ GLfloat r = 1.0, g = 1.0, b = 1.0;/* colore del disegno */ int fill = 0; /* flag per fillling*/

Programmazione Grafica aa2005/20068 /* main */ int main(int argc, char** argv) { int c_menu, p_menu, f_menu; glutInit(&argc,argv); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(500, 500); glutCreateWindow("square"); glutDisplayFunc(display);

Programmazione Grafica aa2005/20069 /* creazione Menu pop-up gerarchico */ c_menu = glutCreateMenu(color_menu); glutAddMenuEntry("Red",1); glutAddMenuEntry("Green",2); glutAddMenuEntry("Blue",3); glutAddMenuEntry("Cyan",4); glutAddMenuEntry("Magenta",5); glutAddMenuEntry("Yellow",6); glutAddMenuEntry("White",7); 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); glutCreateMenu(middle_menu); glutAddSubMenu("Colors", c_menu); glutAddSubMenu("Pixel Size", p_menu); glutAddSubMenu("Fill", f_menu); glutAttachMenu(GLUT_MIDDLE_BUTTON);

Programmazione Grafica aa2005/ myinit (); glutReshapeFunc (myReshape); glutKeyboardFunc(key); glutMouseFunc (mouse); glutMainLoop(); } /* fine main /*

Programmazione Grafica aa2005/ void drawSquare(int x, int y) { y=wh-y; glColor3ub( (char) random()%256, (char) random()%256, (char) random()%256); glBegin(GL_POLYGON); glVertex2f(x+size, y+size); glVertex2f(x-size, y+size); glVertex2f(x-size, y-size); glVertex2f(x+size, y-size); glEnd(); }

Programmazione Grafica aa2005/ /* callback per la modifica della window*/ void myReshape(GLsizei w, GLsizei h) { /* ridefinisci clipping box */ glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0.0,(GLdouble)w, 0.0,(GLdouble)h, -1.0, 1.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); /* ridefinisci viewport pulisci spazio */ glViewport(0,0,w,h); glClearColor (0.8, 0.8, 0.8, 1.0); glClear(GL_COLOR_BUFFER_BIT); display(); glFlush(); /* assegna estremi WC per il disegno */ ww = w; wh = h; }

Programmazione Grafica aa2005/ void myinit(void) { glViewport(0,0,ww,wh); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0.0,(GLdouble)ww,0.0,(GLdouble)wh,-1.0,1.0); /* colore di sfondo grigio chiaro clear window */ glClearColor (0.8, 0.8, 0.8, 1.0); glClear(GL_COLOR_BUFFER_BIT); glFlush(); }

Programmazione Grafica aa2005/ /* callback disegno di un quadrato */ void drawSquare(int x, int y) { y=wh-y; glColor3ub( (char) random()%256, (char) random()%256, (char) random()%256); glBegin(GL_POLYGON); glVertex2f(x+size, y+size); glVertex2f(x-size, y+size); glVertex2f(x-size, y-size); glVertex2f(x+size, y-size); glEnd(); }

Programmazione Grafica aa2005/ /* callback per disegnare bottoni e icone di scelta */ void display(void) { int shift=0; glPushAttrib(GL_ALL_ATTRIB_BITS); glClearColor (0.8, 0.8, 0.8, 1.0); glClear(GL_COLOR_BUFFER_BIT);

Programmazione Grafica aa2005/ /* disegna bottoni colorati */ glColor3f(1.0, 1.0, 1.0); screen_box(0,wh-ww/10,ww/10); glColor3f(1.0, 0.0, 0.0); screen_box(ww/10,wh-ww/10,ww/10); glColor3f(0.0, 1.0, 0.0); screen_box(ww/5,wh-ww/10,ww/10); glColor3f(0.0, 0.0, 1.0); screen_box(3*ww/10,wh-ww/10,ww/10); glColor3f(1.0, 1.0, 0.0); screen_box(2*ww/5,wh-ww/10,ww/10); glColor3f(0.0, 0.0, 0.0); screen_box(ww/10+ww/40,wh-ww/10+ww/40,ww/20); Menu di scelta dell’oggetto da tracciare Disegnamo prima i bottoni, poi le icone che rappresentano i possibili oggetti

Programmazione Grafica aa2005/ /* disegna icone */ glBegin(GL_LINES); glVertex2i(wh/40,wh-ww/20); glVertex2i(wh/40+ww/20,wh-ww/20); glEnd(); glBegin(GL_TRIANGLES); glVertex2i(ww/5+ww/40,wh-ww/10+ww/40); glVertex2i(ww/5+ww/20,wh-ww/40); glVertex2i(ww/5+3*ww/40,wh-ww/10+ww/40); glEnd(); glPointSize(3.0); glBegin(GL_POINTS); glVertex2i(3*ww/10+ww/20, wh-ww/20); glEnd();

Programmazione Grafica aa2005/ /* disegna caratteri per icona selezione testo */ glRasterPos2i(2*ww/5,wh-ww/20); glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'A'); shift=glutBitmapWidth(GLUT_BITMAP_9_BY_15, 'A'); glRasterPos2i(2*ww/5+shift,wh-ww/20); glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'B'); shift+=glutBitmapWidth(GLUT_BITMAP_9_BY_15, 'B'); glRasterPos2i(2*ww/5+shift,wh-ww/20); glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'C'); glFlush(); glPopAttrib(); } /* fine display */

Programmazione Grafica aa2005/ /* callback gestione mouse*/ void mouse(int btn, int state, int x, int y) { static int count; int where; static int xp[2],yp[2]; if(btn==GLUT_LEFT_BUTTON && state==GLUT_DOWN) { glPushAttrib(GL_ALL_ATTRIB_BITS); /* scelta sul menu di icone */ where = pick(x,y); glColor3f(r, g, b); if(where != 0) { count = 0; draw_mode = where; } else switch(draw_mode)

Programmazione Grafica aa2005/ { case(LINE): if(count==0) { count++; xp[0] = x; yp[0] = y; } else { glBegin(GL_LINES); glVertex2i(x,wh-y); glVertex2i(xp[0],wh-yp[0]); glEnd(); draw_mode=0; count=0; } break;

Programmazione Grafica aa2005/ case(RECTANGLE): if(count == 0) { count++; xp[0] = x; yp[0] = y; } else { if(fill) glBegin(GL_POLYGON); else glBegin(GL_LINE_LOOP); glVertex2i(x,wh-y); glVertex2i(x,wh-yp[0]); glVertex2i(xp[0],wh-yp[0]); glVertex2i(xp[0],wh-y); glEnd(); draw_mode=0; count=0; } break;

Programmazione Grafica aa2005/ case (TRIANGLE): switch(count) { case(0): count++; xp[0] = x; yp[0] = y; break; case(1): count++; xp[1] = x; yp[1] = y; break; case(2): if(fill) glBegin(GL_POLYGON); else glBegin(GL_LINE_LOOP); glVertex2i(xp[0],wh-yp[0]); glVertex2i(xp[1],wh-yp[1]); glVertex2i(x,wh-y); glEnd(); draw_mode=0; count=0; } break;

Programmazione Grafica aa2005/ case(POINTS): { drawSquare(x,y); count++; } break; case(TEXT): { rx=x; ry=wh-y; glRasterPos2i(rx,ry); count=0; } /* fine mouse call back */

Programmazione Grafica aa2005/ /* rilevazione della scelta nel menu di bottoni*/ int pick(int x, int y) { y = wh - y; if(y < wh-ww/10) return 0; else if(x < ww/10) return LINE; else if(x < ww/5) return RECTANGLE; else if(x < 3*ww/10) return TRIANGLE; else if(x < 2*ww/5) return POINTS; else if(x < ww/2) return TEXT; else return 0; }

Programmazione Grafica aa2005/ /* creazione bottoni per icone di scelta */ void screen_box(int x, int y, int s ) { glBegin(GL_QUADS); glVertex2i(x, y); glVertex2i(x+s, y); glVertex2i(x+s, y+s); glVertex2i(x, y+s); glEnd(); }

Programmazione Grafica aa2005/ /* callback per i menu pop-up */ void right_menu(int id) /* attivato dal bottone destro del mouse */ { if(id == 1) exit(); else display(); } void middle_menu(int id) /* attivato dal bottone intermedio del mouse */ { }

Programmazione Grafica aa2005/ void color_menu(int id) /* sub-menu del tasto medio */ { if(id == 1) {r = 1.0; g = 0.0; b = 0.0;} else if(id == 2) {r = 0.0; g = 1.0; b = 0.0;} else if(id == 3) {r = 0.0; g = 0.0; b = 1.0;} else if(id == 4) {r = 0.0; g = 1.0; b = 1.0;} else if(id == 5) {r = 1.0; g = 0.0; b = 1.0;} else if(id == 6) {r = 1.0; g = 1.0; b = 0.0;} else if(id == 7) {r = 1.0; g = 1.0; b = 1.0;} else if(id == 8) {r = 0.0; g = 0.0; b = 0.0;} }

Programmazione Grafica aa2005/ void pixel_menu(int id) /* sub-menu del tasto medio */ { if (id == 1) size = 2 * size; else if (size > 1) size = size/2; } void fill_menu(int id) /* sub-menu del tasto medio */ { if (id == 1) fill = 1; else fill = 0; }

Programmazione Grafica aa2005/ /* callback per gestione tastiera */ void key(unsigned char k, int xx, int yy) { if(draw_mode!=TEXT) return; glColor3f(0.0,0.0,0.0); glRasterPos2i(rx,ry); glutBitmapCharacter(GLUT_BITMAP_9_BY_15, k); /*glutStrokeCharacter(GLUT_STROKE_ROMAN,i); */ rx+=glutBitmapWidth(GLUT_BITMAP_9_BY_15,k); }