Section 3 adjusted by Beam Wisman rwisman@ius.


110 views
Uploaded on:
Category: News / Events
Description
Working with Callbacks Section 3 changed by Beam Wisman rwisman@ius.edu Ed Blessed messenger Teacher of Software engineering, Electrical and PC Designing, and Media Expressions College of New Mexico Goals Figure out how to assemble intelligent projects utilizing Excess callbacks Mouse Console Reshape
Transcripts
Slide 1

Working with Callbacks Chapter 3 changed by Ray Wisman rwisman@ius.edu Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts University of New Mexico Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

Slide 2

Objectives Learn to fabricate intelligent projects utilizing GLUT callbacks Mouse Keyboard Reshape Introduce menus in GLUT Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

Slide 3

The mouse callback glutMouseFunc(mymouse) void mymouse(GLint catch, GLint state, GLint x, GLint y) Callback passes which catch ( GLUT_LEFT_BUTTON , GLUT_MIDDLE_BUTTON , GLUT_RIGHT_BUTTON ) brought on occasion condition of that catch ( GLUT_UP , GLUT_DOWN ) Position in window (x,y) where y=0 is upper-cleared out. Holy messenger: Interactive Computer Graphics 4E © Addison-Wesley 2005

Slide 4

Positioning The position in the screen window is normally measured in pixels with the birthplace at the upper left corner Artifact of screen invigorate done start to finish OpenGL utilizes a world direction framework with beginning at the base left Must reverse y direction returned by callback by tallness of window y = h – y; (0,0) h w Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

Slide 5

Obtaining the window size To transform the y position we require the window stature Height can change amid system execution Track with a worldwide variable New stature came back to reshape callback that we will take a gander at in subtle element soon Can likewise utilize inquiry capacities glGetIntegerv glGetFloatv to acquire any worth that is a state\'s piece Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

Slide 6

Terminating a project In our unique projects, there was no real way to end them through OpenGL We can utilize the basic mouse callback void mouse(int btn, int state, int x, int y) { if(btn==GLUT_RIGHT_BUTTON && state==GLUT_DOWN) exit(0); } Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

Slide 7

Using the mouse position In the following sample, we draw a little square at the mouse\'s area every time the left mouse catch is clicked This illustration does not utilize the showcase callback but rather one is needed by GLUT; the capacity is called when window opened/resized. mydisplay(){ glClear(GL_COLOR_BUFFER_BIT); glFlush(); } Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

Slide 8

Drawing squares at cursor area on left mouse snap void mymouse(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); } void drawSquare(int x, int y) { y = h - y;/* window stature h, transform y */glColor3ub( (char) rand()%256,/Random (char) rand )%256,/shading (char) rand()%256); glRecti(x-size, y-size, x+size, y+size); glFlush(); } Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

Slide 9

Using the movement callback We can draw squares (or something else) constantly the length of a mouse catch is discouraged by utilizing the movement callback glutMotionFunc( drawSquare ) We can draw squares without discouraging a catch utilizing the uninvolved movement callback glutPassiveMotionFunc( drawSquare ) Because no presentation callback is made clearing the screen, a line of squares is shown Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

Slide 10

Using the console glutKeyboardFunc( mykey ) void mykey(unsigned burn key, int x, int y) Returns ASCII code of key discouraged and mouse area void mykey(unsigned singe key, int x, int y) { if(key == ‘Q’ | key == ‘q’) exit(0); } Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

Slide 11

Special and Modifier Keys GLUT characterizes the uncommon keys in glut.h Function key 1: GLUT_KEY_F1 Up bolt key: GLUT_KEY_UP if(key == ‘GLUT_KEY_F1’ …… Can likewise check of one of the modifiers GLUT_ACTIVE_SHIFT GLUT_ACTIVE_CTRL GLUT_ACTIVE_ALT is discouraged by glutGetModifiers() Allows imitating of three-catch mouse with maybe a couple catch mice Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

Slide 12

Reshaping the window We can reshape and resize the OpenGL show window by pulling the window\'s edge What happens to the showcase? Must redraw from application Two potential outcomes Display some piece of world Display entire world yet constrain to fit in new window Can change angle proportion Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

Slide 13

Reshape possiblities unique reshaped Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

Slide 14

The Reshape callback glutReshapeFunc(myreshape) void myreshape( int w, int h) Receives width and tallness of new window (in pixels) A redisplay is posted consequently at end of execution of the callback GLUT has a default reshape callback however you most likely need to characterize your own particular The reshape callback is great spot to put review capacities on the grounds that it is conjured when the window is initially opened Without reshape. With reshape. Blessed messenger: Interactive Computer Graphics 4E © Addison-Wesley 2005

Slide 15

Example Reshape This reshape jam shapes by making the viewport and world window have the same angle proportion Suppose at first: gluOrtho2D(- 2.0, 2.0, - 2.0, 2.0 ); gluOrtho2D( left, right, base, top ) void myReshape(int w, int h) { glViewport(0, 0, w, h); /* x, y, w, h */glMatrixMode(GL_PROJECTION); /* switch lattice mode */glLoadIdentity(); if (w <= h) /* change base and top */gluOrtho2D(- 2.0, 2.0, - 2.0 * (GLfloat) h/(GLfloat) w, 2.0 * (GLfloat) h/(GLfloat) w); else /* change lower-left x and y */ gluOrtho2D(- 2.0 * (GLfloat) w/(GLfloat) h, 2.0 * (GLfloat) w/(GLfloat) h, - 2.0, 2.0); glMatrixMode(GL_MODELVIEW); /* come back to modelview mode */} Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

Slide 16

Example Reshape Suppose initially: gluOrtho2D(- 2.0, 2.0, - 2.0, 2.0 ); gluOrtho2D( left, right, base, top ) Change (width, tallness) from (100,100) to (50,100) Viewport perspective proportion (width-to-stature) =50/100 = ½ Clipping rectangle viewpoint proportion = 4/8 = ½ void myReshape(int w, int h) { glViewport(0, 0, 50, 100); /* x, y, w, h */if (50 <= 100) /* change base and top */gluOrtho2D(- 2.0, 2.0, - 2.0 * 100/50 , 2.0 * 100/50 ; 50 4 100 (0,0) - 4 - 2 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

Slide 17

Toolkits and Widgets Most window frameworks give a toolbox or library of capacities for building client interfaces that utilization exceptional sorts of windows called gadgets Widget sets incorporate devices, for example, Menus Slidebars Dials Input boxes But tool compartments have a tendency to be stage ward GLUT gives a couple of gadgets including menus Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

Slide 18

Menus GLUT backings pop-up menus A menu can have submenus Three stages Define passages for the menu Define activity for every menu thing Action did if section chose Attach menu to a mouse catch Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

Slide 19

Defining a basic menu In main.c menu_id = glutCreateMenu(mymenu); glutAddmenuEntry(“clear Screen”, 1); gluAddMenuEntry(“exit”, 2); glutAttachMenu(GLUT_RIGHT_BUTTON); clear screen exit sections that show up when right mouse catch discouraged identifiers Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

Slide 20

Menu activities Menu callback Note every menu has an id that is returned when it is made Add submenus by glutAddSubMenu(char *submenu_name, submenu id) void mymenu(int id) { if(id == 1) glClear(); if(id == 2) exit(0); } section in guardian menu Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

Slide 21

Menu Example int size=1; void showcase() { glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0, 1.0, 1.0); glRectf(- size, - size, size, size); glFlush(); } void menu( int id ) { switch(id) { case 1 : exit(0); case 2 : if( size < 10) size++; break; case 3 : if( size > 1) size- - ; break; } glutPostRedisplay(); } int main(int argc, char** argv) { : : glutCreateMenu( menu ); glutAddMenuEntry("quit", 1); glutAddMenuEntry("+", 2); glutAddMenuEntry("- ", 3); glutAttachMenu( GLUT_RIGHT_BUTTON); glutMainLoop(); } Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

Slide 22

Other capacities in GLUT Dynamic Windows Create and devastate amid execution Subwindows Multiple Windows Changing callbacks amid execution Timers Portable text styles glutBitmapCharacter glutStrokeCharacter Angel

Recommended
View more...