csc 470 introduction to computer graphicsnatacha/teachspring_12/csc470/... · computer graphics •...

35
1 CSC 470 Introduction to Computer Graphics Drawing Polylines Stored in a File Parameterizing Figures Menus

Upload: others

Post on 24-Sep-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSC 470 Introduction to Computer Graphicsnatacha/TeachSpring_12/CSC470/... · Computer Graphics • Drawing Polylines ... Suppose the file dino.dat contains a collection of polylines,

1

CSC 470 Introduction to Computer Graphics

• Drawing Polylines Stored in a File

• Parameterizing Figures

• Menus

Page 2: CSC 470 Introduction to Computer Graphicsnatacha/TeachSpring_12/CSC470/... · Computer Graphics • Drawing Polylines ... Suppose the file dino.dat contains a collection of polylines,

2

Drawing Drawing PolylinesPolylines

Drawing polyline from vertices in a file• # polylines• # vertices in first polyline• Coordinates of vertices, x y, one pair per line• Repeat last 2 lines as necessary

File for dinosaur available from theWeb siteCode to draw polylines/polygons in Fig. 2.24.

Page 3: CSC 470 Introduction to Computer Graphicsnatacha/TeachSpring_12/CSC470/... · Computer Graphics • Drawing Polylines ... Suppose the file dino.dat contains a collection of polylines,

3

Example “Example “dinodino””

Page 4: CSC 470 Introduction to Computer Graphicsnatacha/TeachSpring_12/CSC470/... · Computer Graphics • Drawing Polylines ... Suppose the file dino.dat contains a collection of polylines,

4

Suppose the file dino.dat contains a collection of polylines, in the following format (the comments are not part of the file):

21 number of polylines in the file4 number of points in the first polyline169 118 first point of first polyline174 120 second point of first polyline179 124178 1265 number of points in the second polyline298 86 first point of second polyline304 92310 104314 114314 1192932 43510 439. . . etc.

Page 5: CSC 470 Introduction to Computer Graphicsnatacha/TeachSpring_12/CSC470/... · Computer Graphics • Drawing Polylines ... Suppose the file dino.dat contains a collection of polylines,

5

Fig. 2.24 A Function for Drawing Fig. 2.24 A Function for Drawing polylinespolylinesstored in a Filestored in a File

void drawPolyLineFile(char * fileName){

fstream inStream;inStream.open(fileName, ios ::in); // open the fileif(inStream.fail())return;glClear(GL_COLOR_BUFFER_BIT); // clear the screenGLint numpolys, numLines, x ,y;inStream >> numpolys; // read the number of polylinesfor(int j = 0; j < numpolys; j++) // read each polyline

{inStream >> numLines;glBegin(GL_LINE_STRIP); // draw the next polylinefor (int i = 0; i < numLines; i++)

{inStream >> x >> y; // read the next x, y pairglVertex2i(x, y);

}glEnd();

}glFlush();inStream.close();

}

Page 6: CSC 470 Introduction to Computer Graphicsnatacha/TeachSpring_12/CSC470/... · Computer Graphics • Drawing Polylines ... Suppose the file dino.dat contains a collection of polylines,

6

ParameterizingParameterizing FiguresFigures

Parameterizing Drawings: allows making them different sizes and aspect ratiosCode for a parameterized house is in Fig. 2.27.

Page 7: CSC 470 Introduction to Computer Graphicsnatacha/TeachSpring_12/CSC470/... · Computer Graphics • Drawing Polylines ... Suppose the file dino.dat contains a collection of polylines,

7

Parameterizing Figures

Page 8: CSC 470 Introduction to Computer Graphicsnatacha/TeachSpring_12/CSC470/... · Computer Graphics • Drawing Polylines ... Suppose the file dino.dat contains a collection of polylines,

8

Drawing a village by calling Drawing a village by calling parameterizedHouseparameterizedHouse with different parameterswith different parameters

Page 9: CSC 470 Introduction to Computer Graphicsnatacha/TeachSpring_12/CSC470/... · Computer Graphics • Drawing Polylines ... Suppose the file dino.dat contains a collection of polylines,

9

Building a Building a PolylinePolyline DrawerDrawerclass GLintPointArray{

const int MAX_NUM = 100;public:int num;GLintPoint pt[MAX_NUM];

};

void drawPolyLine(GlintPointArray poly, int closed){

glBegin(closed ? GL_LINE_LOOP : GL_LINE_STRIP);for(int i = 0; i < poly.num; i++)

glVertex2i(poly.pt[i].x, poly.pt[i].y);glEnd();glFlush();

}

Page 10: CSC 470 Introduction to Computer Graphicsnatacha/TeachSpring_12/CSC470/... · Computer Graphics • Drawing Polylines ... Suppose the file dino.dat contains a collection of polylines,

10

Relative Line DrawingRelative Line Drawing

Requires keeping track of current position on screen (CP).moveTo(x, y); set CP to (x, y)lineTo(x, y); draw a line from CP to (x, y), and then update CP to (x, y).Code is in Fig. 2.31.Caution! CP is a global variable, and therefore vulnerable to tampering from instructions at other points in your program.

Page 11: CSC 470 Introduction to Computer Graphicsnatacha/TeachSpring_12/CSC470/... · Computer Graphics • Drawing Polylines ... Suppose the file dino.dat contains a collection of polylines,

11

class GLintPoint{public:GLint x, y;};void myInit(void){}GLintPoint CP; void moveto(GLint x, GLint y){CP.x = x; CP.y = y; // update the CP}

void lineto(GLint x, GLint y){glBegin(GL_LINES); // draw the line

glVertex2i(CP.x, CP.y);glVertex2i(x, y);

glEnd();glFlush();CP.x = x+10; CP.y = y+10; // update the CP}void myDisplay(void){ GLintPoint temp={100,200};

GLint x[100], y[100];x[0]=30;y[0]=30;

moveto(x[0],y[0]);for(int i = 1; i < 10; i++){ x[i]=x[i-1]+60;

y[i]=y[i-1]+90;lineto(x[i], y[i]);

}glFlush();

}

void main(int argc, char **argv){}

What will change with the red line change?

Page 12: CSC 470 Introduction to Computer Graphicsnatacha/TeachSpring_12/CSC470/... · Computer Graphics • Drawing Polylines ... Suppose the file dino.dat contains a collection of polylines,

12

Using MenusUsing Menus

Both GLUT and GLUI make menus available.GLUT menus are simple, and GLUI menus are more powerful.We will build a single menu that will allow the user to change the color of a triangle, which is undulating back and forth as the application proceeds.

Page 13: CSC 470 Introduction to Computer Graphicsnatacha/TeachSpring_12/CSC470/... · Computer Graphics • Drawing Polylines ... Suppose the file dino.dat contains a collection of polylines,

13

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

Page 14: CSC 470 Introduction to Computer Graphicsnatacha/TeachSpring_12/CSC470/... · Computer Graphics • Drawing Polylines ... Suppose the file dino.dat contains a collection of polylines,

14

GLUT Menu Callback FunctionGLUT Menu Callback Function

int glutCreateMenu(myMenu); returns menu IDvoid myMenu(int num); //handles choice numvoid glutAddMenuEntry(char* name, int value); // value used in myMenu switch to handle choicevoid glutAttachMenu(int button); // one of GLUT_RIGHT_BUTTON, GLUT_MIDDLE_BUTTON, or GLUT_LEFT_BUTTON• Usually GLUT_RIGHT_BUTTON

Page 15: CSC 470 Introduction to Computer Graphicsnatacha/TeachSpring_12/CSC470/... · Computer Graphics • Drawing Polylines ... Suppose the file dino.dat contains a collection of polylines,

15

Defining a simple menu

In main.cmenu_id = glutCreateMenu(mymenu);glutAddMenuEntry(“clear Screen”, 1);

gluAddMenuEntry(“exit”, 2);

glutAttachMenu(GLUT_RIGHT_BUTTON);

entries that appear whenright button depressed

identifiers

clear screen

exit

Page 16: CSC 470 Introduction to Computer Graphicsnatacha/TeachSpring_12/CSC470/... · Computer Graphics • Drawing Polylines ... Suppose the file dino.dat contains a collection of polylines,

16

GLUT GLUT subMenussubMenus

Create a subMenu first, using menu commands, then add it to main menu.• A submenu pops up when a main menu item is

selected.glutAddSubMenu (char* name, int menuID); // menuID is the value returned by glutCreateMenuwhen the submenu was createdComplete code for a GLUT Menu application is in Fig. 2.44. (No submenus are used.)

Page 17: CSC 470 Introduction to Computer Graphicsnatacha/TeachSpring_12/CSC470/... · Computer Graphics • Drawing Polylines ... Suppose the file dino.dat contains a collection of polylines,

17

Defining a simple menuDefining a simple menu

Page 18: CSC 470 Introduction to Computer Graphicsnatacha/TeachSpring_12/CSC470/... · Computer Graphics • Drawing Polylines ... Suppose the file dino.dat contains a collection of polylines,

18

Menu actions• Menu callback

• Note each menu has an id that is returned when it is created

• Add submenus byglutAddSubMenu(char *submenu_name, submenu id)

void mymenu(int id){

if(id == 1) glClear();if(id == 2) exit(0);

}

entry in parent menu

Page 19: CSC 470 Introduction to Computer Graphicsnatacha/TeachSpring_12/CSC470/... · Computer Graphics • Drawing Polylines ... Suppose the file dino.dat contains a collection of polylines,

19

Menu Action

Page 20: CSC 470 Introduction to Computer Graphicsnatacha/TeachSpring_12/CSC470/... · Computer Graphics • Drawing Polylines ... Suppose the file dino.dat contains a collection of polylines,

20

Structure of Hierarchical Menus

Page 21: CSC 470 Introduction to Computer Graphicsnatacha/TeachSpring_12/CSC470/... · Computer Graphics • Drawing Polylines ... Suppose the file dino.dat contains a collection of polylines,

21

Code for hierarchical menu

Page 22: CSC 470 Introduction to Computer Graphicsnatacha/TeachSpring_12/CSC470/... · Computer Graphics • Drawing Polylines ... Suppose the file dino.dat contains a collection of polylines,

22

Simple Paint Program

Page 23: CSC 470 Introduction to Computer Graphicsnatacha/TeachSpring_12/CSC470/... · Computer Graphics • Drawing Polylines ... Suppose the file dino.dat contains a collection of polylines,

23

User screen

Page 24: CSC 470 Introduction to Computer Graphicsnatacha/TeachSpring_12/CSC470/... · Computer Graphics • Drawing Polylines ... Suppose the file dino.dat contains a collection of polylines,

24

Menu structure

Page 25: CSC 470 Introduction to Computer Graphicsnatacha/TeachSpring_12/CSC470/... · Computer Graphics • Drawing Polylines ... Suppose the file dino.dat contains a collection of polylines,

25

Functions

Page 26: CSC 470 Introduction to Computer Graphicsnatacha/TeachSpring_12/CSC470/... · Computer Graphics • Drawing Polylines ... Suppose the file dino.dat contains a collection of polylines,

26

Program main

Page 27: CSC 470 Introduction to Computer Graphicsnatacha/TeachSpring_12/CSC470/... · Computer Graphics • Drawing Polylines ... Suppose the file dino.dat contains a collection of polylines,

27

Program main

Page 28: CSC 470 Introduction to Computer Graphicsnatacha/TeachSpring_12/CSC470/... · Computer Graphics • Drawing Polylines ... Suppose the file dino.dat contains a collection of polylines,

28

myInit( )

Page 29: CSC 470 Introduction to Computer Graphicsnatacha/TeachSpring_12/CSC470/... · Computer Graphics • Drawing Polylines ... Suppose the file dino.dat contains a collection of polylines,

29

myInit( ) cont’d

Page 30: CSC 470 Introduction to Computer Graphicsnatacha/TeachSpring_12/CSC470/... · Computer Graphics • Drawing Polylines ... Suppose the file dino.dat contains a collection of polylines,

30

Code in mouse

Page 31: CSC 470 Introduction to Computer Graphicsnatacha/TeachSpring_12/CSC470/... · Computer Graphics • Drawing Polylines ... Suppose the file dino.dat contains a collection of polylines,

31

Code in mouse cont’d

Page 32: CSC 470 Introduction to Computer Graphicsnatacha/TeachSpring_12/CSC470/... · Computer Graphics • Drawing Polylines ... Suppose the file dino.dat contains a collection of polylines,

32

Function key

Page 33: CSC 470 Introduction to Computer Graphicsnatacha/TeachSpring_12/CSC470/... · Computer Graphics • Drawing Polylines ... Suppose the file dino.dat contains a collection of polylines,

33

User screen

Page 34: CSC 470 Introduction to Computer Graphicsnatacha/TeachSpring_12/CSC470/... · Computer Graphics • Drawing Polylines ... Suppose the file dino.dat contains a collection of polylines,

34

GLUI Interfaces and Menus

Page 35: CSC 470 Introduction to Computer Graphicsnatacha/TeachSpring_12/CSC470/... · Computer Graphics • Drawing Polylines ... Suppose the file dino.dat contains a collection of polylines,

35

GLUI ExampleGLUI Example