computer graphicsmohamed_berbar.tripod.com/ch3.pdf · 2007. 4. 2. · 2/04/2007 computer graphics...

49
2/04/2007 Computer Graphics Computer Graphics Computer Graphics CS510737 CS510737 Chapter Three Chapter Three 2/04/2007 Computer Graphics CS510737 This Week Windows, Viewports and Clippings Creating Useful Drawing Tools Turtle Graphics

Upload: others

Post on 21-Aug-2020

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Computer Graphicsmohamed_berbar.tripod.com/ch3.pdf · 2007. 4. 2. · 2/04/2007 Computer Graphics CS510737 Windows and Viewports • Previously we looked at an OpenGL window where

2/04/2007 �

Computer GraphicsComputer GraphicsComputer Graphics

CS510737CS510737

Chapter ThreeChapter Three

2/04/2007 Computer Graphics CS510737 �

This Week• Windows, Viewports and Clippings

• Creating Useful Drawing Tools

• Turtle Graphics

Page 2: Computer Graphicsmohamed_berbar.tripod.com/ch3.pdf · 2007. 4. 2. · 2/04/2007 Computer Graphics CS510737 Windows and Viewports • Previously we looked at an OpenGL window where

2/04/2007 Computer Graphics CS510737 �

Windows and Viewports• Previously we looked at an OpenGL window

where x and y were plotted as positive pixel values.

• However, we may not be interested in keeping track of pixels like this.

• We may prefer to plot points in the coordinates in which they are given.

• This means handling both positive and negative values.

2/04/2007 Computer Graphics CS510737 �

Windows and Viewports• We need to distinguish between the

world, world coordinates, world windows, the screen window and the viewport.

• It’s like this….

Page 3: Computer Graphicsmohamed_berbar.tripod.com/ch3.pdf · 2007. 4. 2. · 2/04/2007 Computer Graphics CS510737 Windows and Viewports • Previously we looked at an OpenGL window where

2/04/2007 Computer Graphics CS510737 �

Screen

Windows and ViewportsThe World (what you can see, the real world)

Screen Window

The World Window (the bit we want to capture)

Viewport

2/04/2007 Computer Graphics CS510737 �

Windows and Viewports• The world window is a rectangle.

• The viewport is a rectangle.

• Both are not necessarily the same size or have the same aspect ratio.

• Coordinates need to be stretched, shrunk and moved to make them fit.

Page 4: Computer Graphicsmohamed_berbar.tripod.com/ch3.pdf · 2007. 4. 2. · 2/04/2007 Computer Graphics CS510737 Windows and Viewports • Previously we looked at an OpenGL window where

2/04/2007 Computer Graphics CS510737 �

Windows and Viewports

World Window

Example Viewports

2/04/2007 Computer Graphics CS510737 �

Windows and Viewports

World Window Viewport

(0,0) (0,0)(100,0) (100,0)

This is called Mapping

Page 5: Computer Graphicsmohamed_berbar.tripod.com/ch3.pdf · 2007. 4. 2. · 2/04/2007 Computer Graphics CS510737 Windows and Viewports • Previously we looked at an OpenGL window where

2/04/2007 Computer Graphics CS510737 �

Windows and Viewports• Recall from the last lecture:

§ x’ = Ax + B

§ y’ = Cy + D

• This is exactly how mapping is achieved!!

2/04/2007 Computer Graphics CS510737 � �

Windows and Viewports

• Mapping involves scaling and translation (moving).

• Both the world window and viewportcan be any aligned rectangle.

• Usually the viewport is set to take up the entire screen window.

Page 6: Computer Graphicsmohamed_berbar.tripod.com/ch3.pdf · 2007. 4. 2. · 2/04/2007 Computer Graphics CS510737 Windows and Viewports • Previously we looked at an OpenGL window where

4/2/2007 Computer Graphics CS510737 � �

4/2/2007 Computer Graphics CS510737 � �

Page 7: Computer Graphicsmohamed_berbar.tripod.com/ch3.pdf · 2007. 4. 2. · 2/04/2007 Computer Graphics CS510737 Windows and Viewports • Previously we looked at an OpenGL window where

4/2/2007 Computer Graphics CS510737 � �

Mapping from the Window to the Viewport

• The Figure shows a world window and a viewport in more detail. The world window is described by its left, top, right, and bottom borders as W.l, W.t, W.r,and W.b

• The viewport is described likewise in the coordinate system of t he screen '

opened at some place on the screen) by V.l, V.t, V.r, and V.b

2/04/2007 Computer Graphics CS510737 � �

Windows and Viewports

W.t = Window topW.l = Window leftW.b = Window bottomW.r = Window right

V.t = Viewport topV.l = Viewport leftV.b = Viewport bottomV.r = Viewport right

Page 8: Computer Graphicsmohamed_berbar.tripod.com/ch3.pdf · 2007. 4. 2. · 2/04/2007 Computer Graphics CS510737 Windows and Viewports • Previously we looked at an OpenGL window where

2/04/2007 Computer Graphics CS510737 � �

Windows and Viewports

xmin, ymin, xmax, ymax = W.l, W.b, W.r, W.tSCREENWIDTH = V.r – V.lSCREENHEIGHT = V.t – V.b

4/2/2007 Computer Graphics CS510737 � �

• We derive a mapping or transformation, called the window-to-viewport mapping. This mapping is based on a formula that produces a point (sx, sy) in the screen window coordinates for any given point (x, y) in the world.

sx = Ax + C, sy = By + D

AC

Page 9: Computer Graphicsmohamed_berbar.tripod.com/ch3.pdf · 2007. 4. 2. · 2/04/2007 Computer Graphics CS510737 Windows and Viewports • Previously we looked at an OpenGL window where

2/04/2007 Computer Graphics CS510737 � �

Windows and Viewports

bWtW

bWy

bVtV

bVsy

..

.

..

.

-

-=

-

-

• The same holds for y:

• or by rearranging

÷ø

öçè

æ

-

--+

-

-= bW

bWtW

bWtVbVy

bWtW

bVtVsy .

..

...

..

..

BD

4/2/2007 Computer Graphics CS510737 � �

Using the formulas in Equation 3.3, we obtainA = 180, C = 40, B = 240, and D = 60. Thus, for this example, the window-to-viewport mapping issx = 180x + 40;sy = 240y + 60.

Page 10: Computer Graphicsmohamed_berbar.tripod.com/ch3.pdf · 2007. 4. 2. · 2/04/2007 Computer Graphics CS510737 Windows and Viewports • Previously we looked at an OpenGL window where

� �

4/2/2007 Computer Graphics CS510737 � �

PRACTICE EXERCISE

• Find values of A,B, C, and D for the case of a world window of (-10.0,10.0, -6.0,6.0) and a viewport of (0,600,0,400).

What about the -ve values ??

2/04/2007 Computer Graphics CS510737 � �

Windows and ViewportsExample

What are A, B, C & D ??

World Window ViewportWorld Window Viewport(10,6)

(-10,-6)

400

0

0 600

Page 11: Computer Graphicsmohamed_berbar.tripod.com/ch3.pdf · 2007. 4. 2. · 2/04/2007 Computer Graphics CS510737 Windows and Viewports • Previously we looked at an OpenGL window where

� �

2/04/2007 Computer Graphics CS510737 � �

Windows and Viewports• Do you need to perform these

calculations each time you draw something with OpenGL??

2/04/2007 Computer Graphics CS510737 � �

Windows and Viewports• No

• OpenGL does all the hard work for you.

• But it is important that you understand what is going on…..

Page 12: Computer Graphicsmohamed_berbar.tripod.com/ch3.pdf · 2007. 4. 2. · 2/04/2007 Computer Graphics CS510737 Windows and Viewports • Previously we looked at an OpenGL window where

� �

2/04/2007 Computer Graphics CS510737 � �

Windows and Viewports• Each time you call for a vertex to be

drawn (e.g. glVertex2f() etc..) the the coordinates of the point are passed coordinates of the point are passed through a set of transformationsthrough a set of transformationsthat map world coordinates into viewport coordinates.

2/04/2007 Computer Graphics CS510737 � �

Windows and Viewports• First set the world window

coordinates with:void glOrtho2D(GLDouble left, GLDouble right,

GLDouble bottom, GLDouble top);

• Then set the viewport with:void glViewport(GLint xmin, GLint ymin,

GLint width, GLint height);

Page 13: Computer Graphicsmohamed_berbar.tripod.com/ch3.pdf · 2007. 4. 2. · 2/04/2007 Computer Graphics CS510737 Windows and Viewports • Previously we looked at an OpenGL window where

� �

4/2/2007 Computer Graphics CS510737 � �

Doing It in OpenGL

• The world window is set by the function gluOrtho2D(),andthe• viewport is set by the function gIViewport () .These functions have the

prototypes

void void gluOrtho2DgluOrtho2D(GLdouble (GLdouble leftleft, , GLdoubleGLdouble rightright, , GLdoubleGLdouble bottombottom, , GLdoubleGLdoubletoptop););

which sets the window to have a lower left corner of (left, bottom) and an upper right corner of (right, top), and

void void gIViewportgIViewport(GLint(GLint xx, , GLintGLint yy, , GLintGLint widthwidth, , GLintGLint heightheight););which sets the viewport to have a lower left corner of (x, y) and an upper right

corner of (x + width, y + height).

•• By default, the viewport is the entire screen window: By default, the viewport is the entire screen window: If W and H are the width and height of the screen window, respectively, the

default viewport has lower left corner at (0, 0) and upper right corner at (W, H).

4/2/2007 Computer Graphics CS510737 � �

• Because OpenGL uses matrices to set up all its transformations, gluOrtho2D()

must be preceded by two "setup" functions:

gIMatrixMode (GL_PROJECTION)

gILoadIdentity()

// sets the window

gIMatrixMode(GL_PROJECTION);

glLoadIdentity();

gluOrtho2D(0.0, 2.0, 0.0, 1.0); gluOrtho2D(0.0, 2.0, 0.0, 1.0);

// sets the viewport

gIViewport(40, 60, 360, 240);gIViewport(40, 60, 360, 240);

Page 14: Computer Graphicsmohamed_berbar.tripod.com/ch3.pdf · 2007. 4. 2. · 2/04/2007 Computer Graphics CS510737 Windows and Viewports • Previously we looked at an OpenGL window where

� �

4/2/2007 Computer Graphics CS510737 � �

In Figures 2.10 and 2.17, the programs used the following instructions:

1. in main():

glutInitWindowSize(640,480); // set screen window size This instruction sets the size of the screen window to 640 by 480.

The default viewport was used, since no The default viewport was used, since no gIViewportgIViewport () () command was issued; the default viewport is the command was issued; the default viewport is the entire screen windowentire screen window.

2. in mylnit () :

glMatrixMode(GL_PROJECTION);

gILoadIdentity();

gluOrtho2D(0.0, 640.0, 0.0, 480.0);

2/04/2007 Computer Graphics CS510737 � �

Windows and ViewportsExample

World Window Viewport(10,6)

(-10,-6)

400

0

0 600

˘

Page 15: Computer Graphicsmohamed_berbar.tripod.com/ch3.pdf · 2007. 4. 2. · 2/04/2007 Computer Graphics CS510737 Windows and Viewports • Previously we looked at an OpenGL window where

� �

2/04/2007 Computer Graphics CS510737 � �

Windows and Viewportsvoid myInit(void){

glClearColor(1.0,1.0,1.0,0.0); glColor3f(0,0,0);glClear(GL_COLOR_BUFFER_BIT);

glMatrixMode(GL_PROJECTION);glLoadIdentity();

//set the viewing coordinates

gluOrtho2D(-10.0, 10.0, -6.0, 6.0);glViewport(0,0,600,400);

}

2/04/2007 Computer Graphics CS510737 � �

Windows and ViewportsglPointSize(10.0);

glBegin(GL_POINTS);

glVertex2i(-10,-6);

glVertex2i(0,0);

glVertex2i(10,6);

glEnd();

*NOTE: Vertex are given in World Coordinates and OpenGL maps them to the Viewport Coordinates.

Page 16: Computer Graphicsmohamed_berbar.tripod.com/ch3.pdf · 2007. 4. 2. · 2/04/2007 Computer Graphics CS510737 Windows and Viewports • Previously we looked at an OpenGL window where

� �

2/04/2007 Computer Graphics CS510737 � �

Handy Functions//--------------- setWindow ---------------------void setWindow(GLdouble left, Gldouble right, GLdouble

bottom, GLdouble top){

glMatrixMode(GL_PROJECTION);glLoadIdentity();gluOrtho2D(left, right, bottom, top);

}

//---------------- setViewport ------------------void setViewport(GLdouble left, Gldouble right, GLdouble

bottom, GLdouble top){

glViewport(left, bottom, right – left, top - bottom);}

2/04/2007 Computer Graphics CS510737 � �

Windows and Viewports• Plotting a function revisted..

//set the viewing coordinates

setWindow(xmin, xmax, ymin, ymax);

setViewport(0,640,0,480);

glBegin(GL_POINTS);

for(GLdouble x = xmin; x < xmax; x+=0.005 )

{

glVertex2d(x, pow(2.7183, -x)*cos(2*3.14*x));

}

glEnd();

-example322.cpp

Page 17: Computer Graphicsmohamed_berbar.tripod.com/ch3.pdf · 2007. 4. 2. · 2/04/2007 Computer Graphics CS510737 Windows and Viewports • Previously we looked at an OpenGL window where

� �

4/2/2007 Computer Graphics CS510737 � �

sinc(x) = [sin(PI *x)] / PI * x

void myDisplay(void) // plot the sine function, using world coordinates {setWindow(-5.0, 5.0, -0.3, 1.0); // set the window setViewport(0, 640, 0, 480); // set the viewport

glBegin(GL_LINE_STRIP);for(GLfloat x = -4.0; x < 4.0; x += 0.1) // draw the plot

glVertex2f(x, sin(3.14159 * x) / (3.14159 * x));glEnd();glFlush();}

2/04/20072/04/2007 Computer Graphics CS510737Computer Graphics CS510737 � �� �

Making PicturesMaking Pictures

§§ Step One: Step One: InitialisationInitialisation–– Setting up the graphics display.Setting up the graphics display.

1.1. Entire Screen is used (computer games, slide shows)Entire Screen is used (computer games, slide shows)

Page 18: Computer Graphicsmohamed_berbar.tripod.com/ch3.pdf · 2007. 4. 2. · 2/04/2007 Computer Graphics CS510737 Windows and Viewports • Previously we looked at an OpenGL window where

� �

2/04/20072/04/2007 Computer Graphics CS510737Computer Graphics CS510737 � �� �

Making PicturesMaking Pictures

§§ Step One: Step One: InitialisationInitialisation–– Setting up the graphics display.Setting up the graphics display.

2.2. WindowWindow--based (upside down coordinates) (when multiple based (upside down coordinates) (when multiple windows are useful, paint packages, word processing, windows are useful, paint packages, word processing, graphing)graphing)

2/04/20072/04/2007 Computer Graphics CS510737Computer Graphics CS510737 � �� �

Making PicturesMaking Pictures

§§ Step One: Step One: InitialisationInitialisation–– Setting up the graphics display.Setting up the graphics display.

3.3. WindowWindow--based (right side up coordinates) (an based (right side up coordinates) (an alternative more natural representation)alternative more natural representation)

Page 19: Computer Graphicsmohamed_berbar.tripod.com/ch3.pdf · 2007. 4. 2. · 2/04/2007 Computer Graphics CS510737 Windows and Viewports • Previously we looked at an OpenGL window where

� �

2/04/20072/04/2007 Computer Graphics CS510737Computer Graphics CS510737 � �� �

Making PicturesMaking Pictures

OpenGL OpenGL –– Inverted WindowsInverted Windows

glutInitWindowSize(640,480); glutInitWindowPosition(100, 150); glutCreateWindow(“An OpenGL Window");

gluOrtho2D(0.0, 640.0, 480.0, 0.0);void gluOrtho2D (GLdouble left, GLdouble right, GLdouble bottom, GLdouble top);Creates a matrix for projecting two -dimensional coordinates onto the screen and multiplies the current projection matrix by it. The clipping reg ion is a rectangle with the lower -left corner at (left, bottom) and the upper -right corner at (right, top).

void gluOrtho2D( GLdouble left,GLdouble right ,GLdouble bottom , GLdouble top)

2/04/20072/04/2007 Computer Graphics CS510737Computer Graphics CS510737 � �� �

Making PicturesMaking PicturesOpenGL OpenGL –– Inverted WindowsInverted Windows

Window size = 640 x 480

(640,480)

(0,0)

(100,410)

Page 20: Computer Graphicsmohamed_berbar.tripod.com/ch3.pdf · 2007. 4. 2. · 2/04/2007 Computer Graphics CS510737 Windows and Viewports • Previously we looked at an OpenGL window where

� �

2/04/20072/04/2007 Computer Graphics CS510737Computer Graphics CS510737 � �� �

Making PicturesMaking Pictures

OpenGL OpenGL –– ““RightRight--side Upside Up”” WindowsWindows

glutInitWindowSize(640,480); glutInitWindowPosition(100, 150); glutCreateWindow(“An OpenGL Window");

gluOrtho2D(0.0, 640.0, 0.0, 480.0);

2/04/20072/04/2007 Computer Graphics CS510737Computer Graphics CS510737 � �� �

Making PicturesMaking PicturesOpenGL OpenGL –– ““RightRight--side Upside Up”” WindowsWindows

Window size = 640 x 480

(640,0)

(0,480)

(100,410)

Page 21: Computer Graphicsmohamed_berbar.tripod.com/ch3.pdf · 2007. 4. 2. · 2/04/2007 Computer Graphics CS510737 Windows and Viewports • Previously we looked at an OpenGL window where

� �

4/2/2007 Computer Graphics CS510737 � �

Laying lots of copies of the same thing side by side to cover the entire screen window is called tiling the window.

The picture that is copied at different positions is often called a motif.

tiling the windowtiling the window

4/2/2007 Computer Graphics CS510737 � �

Tiling a screen window is easily achieved by using a different viewport for each instance of the motif. The Fig. shows a tiling involving 25 copies of the motif. This tiling was generated with the following code:

setWindow(0, 640.0, 0, 440.0); // set a fixed window

for(int i=0; i < 5; i++) // for each column

for(int j=0; j < 5; j++) // for each row

{ glViewport(i * 64, j * 44, 64, 44); // set the next viewport

drawPolylineFile("dino.dat"); // draw it again

}

Note that each copy is drawn in a viewport of size 64 by 44 pixels whose aspect ratio (64/44) matches that of the world window. Thus, each dinosaur is drawn without distortion.

void void gIViewportgIViewport(GLint(GLint xx, , GLintGLint yy, , GLintGLint widthwidth, , GLintGLint heightheight););

Page 22: Computer Graphicsmohamed_berbar.tripod.com/ch3.pdf · 2007. 4. 2. · 2/04/2007 Computer Graphics CS510737 Windows and Viewports • Previously we looked at an OpenGL window where

� �

4/2/2007 Computer Graphics CS510737 � �

Shows another tiling, but here alternate motifs are flipped upside down to produce an intriguing effect. This was done by flipping the window upside down every other iteration: interchanging the top and bottom values in setWindow ()

for(int i = 0 ; i < 5; i++) for(int j = 0; j < 5; j++){

if((i+j) % 2 == 0) // if (i+j) is even

setWindow(0.0, 640.0, 440.0, 0.0); // upside-downelse

setWindow(0 . 0 , 640.0, 0.0, 440.0); // right-side-upglViewport(i * 64, j * 44, 64, 44); // set the next viewport drawPolylineFile("dino.dat"); // draw it again

}

?

4/2/2007 Computer Graphics CS510737 � �

Setting the Window and Viewport Automatically

Setting of the Window

•• Often, the programmer does not know where the object Often, the programmer does not know where the object of interest lies, or how big it is, in world coordinatesof interest lies, or how big it is, in world coordinates.

Like the dinosaur earlier, the object might be stored in a stored in a filefile, or it might be generated procedurallygenerated procedurally by some algorithm whose details are not known.

• In either case, it is convenient to let the application it is convenient to let the application determine a good window to usedetermine a good window to use.

Page 23: Computer Graphicsmohamed_berbar.tripod.com/ch3.pdf · 2007. 4. 2. · 2/04/2007 Computer Graphics CS510737 Windows and Viewports • Previously we looked at an OpenGL window where

� �

4/2/2007 Computer Graphics CS510737 � �

Windows and ViewportsWindows and Viewports

• Example: Zooming In– To zoom in on an object and create an animation, we

need to create a number of frames and draw one after the other.

4/2/2007 Computer Graphics CS510737 � �

[A] Setting the Window[A] Setting the Window

• When setting the window size, you will want to consider the scene you are trying to capture.

• Too big and the objects will appear too small

• Too small and the objects will be clipped

Page 24: Computer Graphicsmohamed_berbar.tripod.com/ch3.pdf · 2007. 4. 2. · 2/04/2007 Computer Graphics CS510737 Windows and Viewports • Previously we looked at an OpenGL window where

� �

4/2/2007 Computer Graphics CS510737 � �

[A] Setting the Window[A] Setting the Window

• To set the window to an appropriate size, you need to predetermine the size of the coordinates to be captured.

• Then set the window to the minimum and maximum extent of these coordinates.

4/2/2007 Computer Graphics CS510737 � �

[A] Setting the Window[A] Setting the Window

• The program needs to pass over the data twice:

1. Execute the drawing routine, but don’t do any drawing.

2. Execute the drawing routine again, but this time draw!

Page 25: Computer Graphicsmohamed_berbar.tripod.com/ch3.pdf · 2007. 4. 2. · 2/04/2007 Computer Graphics CS510737 Windows and Viewports • Previously we looked at an OpenGL window where

� �

4/2/2007 Computer Graphics CS510737 � �

[A] Setting the Window[A] Setting the Window

GLdouble ytemp;for(GLdouble x = xmin+0.005; x < xmax; x+=0.005 ){

ytemp = pow(2.7183, -x)*cos(2*3.14*x);if (ytemp > ymax)

ymax = ytemp;if (ytemp < ymin)

ymin = ytemp;}

setWindow(xmin, xmax, ymin, ymax);

glBegin(GL_POINTS);for(GLdouble x = xmin; x < xmax; x+=0.005 ){

glVertex2d(x, pow(2.7183, -x)*cos(2*3.14*x));

}glEnd();

Pass 1 to calculate suitable ymin and ymax

Pass 2

Set the window boundaries

4/2/2007 Computer Graphics CS510737 � �

[B] Setting the Viewport

• To draw an undistorted version of the data in a viewport, you need to ensure the viewport and the window have the same aspect ratio .

• i.e.

viewport

viewport

window

window

H

W

H

W=

Page 26: Computer Graphicsmohamed_berbar.tripod.com/ch3.pdf · 2007. 4. 2. · 2/04/2007 Computer Graphics CS510737 Windows and Viewports • Previously we looked at an OpenGL window where

� �

4/2/2007 Computer Graphics CS510737 � �

[B] Setting the Viewport

• Recall, if the aspect ratio of a rectangle is less than 1, the rectangle is taller than wide.

• E.g. W/H = 3/5

• If the aspect ratio is greater than 1, the rectangle is wider than tall.

• E.g. W/H = 5/3

4/2/2007 Computer Graphics CS510737 � �

Automatic Setting of the Viewport to Preserve the Aspect Ratio

• You need to specify a viewport that has the same aspect ratio as the world window to avoid distortion version of a figure and will fit in the screen window.

• Suppose the aspect ratio of the world window is known to be R and the screen window has width W and height H.

• There are two distinct situations: The world window may have a larger aspect ratio than the screen window (R >

W/H), or it may have a smaller aspect ratio (R < W/H).

Page 27: Computer Graphicsmohamed_berbar.tripod.com/ch3.pdf · 2007. 4. 2. · 2/04/2007 Computer Graphics CS510737 Windows and Viewports • Previously we looked at an OpenGL window where

� �

4/2/2007 Computer Graphics CS510737 � �

•The two situations are shown in Figure 3.16. We consider each in turn.

Case (a): R > W/H. the viewport to match aspect ratio R will extend fully across the screen window, therefore, the viewport will have width W and height W/R, so it is set with the following command

setViewport(0, W, 0, W/R);setViewport(0, W, 0, W/R);Case (b): R < W/H. the viewport to match aspect ratio R will reach from the top to the bot­tom of the screen window. At its largest, the viewport will haveheight H, but width HR, so it is set with the command

setViewport(0, H * R, 0, H);setViewport(0, H * R, 0, H);

4/2/2007 Computer Graphics CS510737 � �

Set the Viewport

a) R > W/H (where R is the aspect ratio of the world window)

• If the world window is ‘flatter’ than the screen window, there will be unused space above and/or below.

• The width of the world window will be mapped to the entire widthof the screen window. setViewport(0,W,0,W/R);

Page 28: Computer Graphicsmohamed_berbar.tripod.com/ch3.pdf · 2007. 4. 2. · 2/04/2007 Computer Graphics CS510737 Windows and Viewports • Previously we looked at an OpenGL window where

� �

4/2/2007 Computer Graphics CS510737 � �

Set the Viewport

b) R < W/H (where R is the aspect ratio of the world window)

• If the world window is ‘taller’ than the screen window, there will be unused space on the sides.

• The height of the world window will be mapped to the entire height of the screen window. setViewport(0,H*R,0,H);

4/2/2007 Computer Graphics CS510737 � �

• EXAMPLE 3.2.7 A tall window

Suppose the window has aspect ratio R = 1.6 and the screen window has H = 200 and W = 360, and hence, W/H = 1.8.

Therefore, Case (b) applies, and the viewport is set to have a height of H = 200 pixels and a width of = H * R = 320 pixels.

• EXAMPLE 3.2.8 A short window

Suppose R = 2 and the screen window is the same as in the previous example.

Then Case (a) applies, and the viewport is set to have a height of W/R = 180 pixels and a width of W = 360 pixels.

Page 29: Computer Graphicsmohamed_berbar.tripod.com/ch3.pdf · 2007. 4. 2. · 2/04/2007 Computer Graphics CS510737 Windows and Viewports • Previously we looked at an OpenGL window where

� �

4/2/2007 Computer Graphics CS510737 � �

Resizing the Screen Window• In a windows-based system the user can resize the screen window

at run time. This action generates a resize event that the system can respond to.

// specifies the function myReshape called on an resize event

• glutReshapeFunc(myReshape); • void myReshape(GLsizei W, GLsizei H);

When this function is executed, the system automatically passes it the new width and height of the screen window, which the function can then use in its calculations. (GLsizei is a 32-bit integer; see Figure 2.7.)

• This works the same as for the glutMouseFunc and glutKeyboardFunc …

• If you don’t run a glViewport after a window resize, the glViewportdefaults to the window coordinates.

4/2/2007 Computer Graphics CS510737 � �

Making a Matched Viewport

• One common approach is to find a new viewport that both fits into the new screen window and has the same aspect ratio as the world window. Matching the aspect ratios of the viewport and world window in this way will prevent distortion in the new picture.

• Figure 3.17 shows a version of myReshape () that does the desired matching:

Page 30: Computer Graphicsmohamed_berbar.tripod.com/ch3.pdf · 2007. 4. 2. · 2/04/2007 Computer Graphics CS510737 Windows and Viewports • Previously we looked at an OpenGL window where

� �

4/2/2007 Computer Graphics CS510737 � �

void myReshape(GLsizei W, GLsizei H)

{

if(R > W/H) // use (global) window aspect ratio

setViewport(0, W, 0, W/R);

else

setViewport(0, H * R, 0, H);

}

•It finds the largest matching viewport (i.e., one that matches the aspect ratio R of the window) that will fit into the new screen window. The routine obtains the width and height of the new screen window through its arguments. The code is a simple embodiment of the result in Figure 3.16.

4/2/2007 Computer Graphics CS510737 � �

PRACTICE EXERCISESPage 95 By the student

Page 31: Computer Graphicsmohamed_berbar.tripod.com/ch3.pdf · 2007. 4. 2. · 2/04/2007 Computer Graphics CS510737 Windows and Viewports • Previously we looked at an OpenGL window where

� �

4/2/2007 Computer Graphics CS510737 � �

Clipping Lines

• In this section, we describe a classic line-clipping algorithm, the Cohen-Sutherlan clipper, that computes which part (if any) of a line segment with endpoints p 1 and p2 lies inside the world windowlies inside the world window and then reports back the endpoints of that part.

4/2/2007 Computer Graphics CS510737 � �

• Figure 3.18 shows a typical situation covering some of the many possible actions of a clipper.

• The function clipSegment ( ) does one of four things to each line segment

1. If the entire line lies within the window (e.g., like segment CD), the function returns a value of 1.

2. If the entire line lies outside the window (e.g., like segment AB), the function it returns a value of 0.

3. If one endpoint is inside the window and one is outside (e.g., like segment ED),| the function clips the portion of the segment that lies outside the window and returns a value of 1.

4. If both endpoints are outside the window, but a portion of the segment passe( through it (e.g., like segment AE), the function clips both ends and returns value of 1.

Page 32: Computer Graphicsmohamed_berbar.tripod.com/ch3.pdf · 2007. 4. 2. · 2/04/2007 Computer Graphics CS510737 Windows and Viewports • Previously we looked at an OpenGL window where

� �

4/2/2007 Computer Graphics CS510737 � �

Left -- Above ---- Right --- Below

4/2/2007 Computer Graphics CS510737 � �

Testing for a Trivial Accept or Trivial Reject

• Trivial accept: Both code words are FFFF;

• Trivial reject: The code words have a T in the same position; both points are to the left of the window, or both are above, etc.How?Code1 AND Code2 != 0

. First, endpoint pairs are checked for trivial acceptance. If the line cannot be trivially accepted, region checks are done.

Now logically AND together the two region codes. A non-zero result means the line must be totally outside the window. Why?

Page 33: Computer Graphicsmohamed_berbar.tripod.com/ch3.pdf · 2007. 4. 2. · 2/04/2007 Computer Graphics CS510737 Windows and Viewports • Previously we looked at an OpenGL window where

� �

4/2/2007 Computer Graphics CS510737 � �

Chopping When There Is Neither Trivial Accept nor Trivial Reject

• The CohenCohen--Sutherland algorithmSutherland algorithm uses a divide-and-conquer strategy. If the segment can be neither trivially accepted nor rejected, it is broken into two parts at one of the window boundaries. One part lies outside the window and is discarded. The other part is potentially visible, so the entire process is repeated for this segment against another of the four window boundaries.

4/2/2007 Computer Graphics CS510737 � �

• The entire procedure gives rise to the following strategy:

do{

form the code words for p1 and p2

if (trivial accept) return 1;

if (trivial reject) return 0;

chop the line at the "next" window border;

against the left edge

against the right edge

against the bottom edge

against the top edge

discard the "outside" part;

}while(1);

Thus, after at most four iterations, trivial acceptance or rejection is assured.

Page 34: Computer Graphicsmohamed_berbar.tripod.com/ch3.pdf · 2007. 4. 2. · 2/04/2007 Computer Graphics CS510737 Windows and Viewports • Previously we looked at an OpenGL window where

� �

4/2/2007 Computer Graphics CS510737 � �

Clipping Lines

Cohen-Sutherland Clipping Algorithm

Clipping when there is neither trivial accept nor reject.

• A line that cannot be trivial accepted or rejected will have oneend point inside the window and one outside, or;

• Will have one endpoint on one side and the other endpoint on theother so that the line crosses the window.

These lines need to be chopped at the window border intersections.

4/2/2007 Computer Graphics CS510737 � �

Clipping Lines

Cohen-Sutherland Clipping Algorithm

We need to determine where A is.

•its x coordinate is window.right

•its y coordinate can be calculated using similar triangles..

delx

e

dely

d=

Page 35: Computer Graphicsmohamed_berbar.tripod.com/ch3.pdf · 2007. 4. 2. · 2/04/2007 Computer Graphics CS510737 Windows and Viewports • Previously we looked at an OpenGL window where

� �

4/2/2007 Computer Graphics CS510737 � �

• How is the chopping at each boundary done? Figure 3.22 shows an example involving the right edge of the window.

Look to the book page 98

4/2/2007 Computer Graphics CS510737 � �

Clipping Lines

Cohen-Sutherland Clipping Algorithm

We need to determine where A is.

e = p1.x – W.right

delx = p2.x – p1.x;

dely = p2.y – p1.y;

d = e/delx * dely;

p1.y += (W.right – p1.x) * dely/delx

Page 36: Computer Graphicsmohamed_berbar.tripod.com/ch3.pdf · 2007. 4. 2. · 2/04/2007 Computer Graphics CS510737 Windows and Viewports • Previously we looked at an OpenGL window where

� �

4/2/2007 Computer Graphics CS510737 � �

The type Point2 holds a 2D point, and the type RealRect holds an aligned rectangle. Both types are described fully in Section 3.4.)

int clipSegment(Point2& pi, Point2& p2, RealRect W){do{

if (trivial accept) return 1; // some portion survives if (trivial reject) return 0; //no portion survivesif (p1 is outside}{if (p1 is to the left) chop against the left edge

else if (p1 is to the right) chop against the right edge else if (p1 is below) chop against the bottom edge else if (p1 is above) chop against the top edge}

else // p2 is outside{ if (p2 is to the left) chop against the left edge

else if (p2 is to the right) chop against the right edge else if (p2 is below) chop against the bottom edge else if (p2 is above) chop against the top edge

}

}while(1);}

4/2/2007 Computer Graphics CS510737 � �

• A situation that requires all four clips is shown in Figure 3.24.

The 1st clip changes p1 to A

2nd finds p1 still outside and bellow and so changes A to C

Last changes p2 to D

3rd alters p2 to B

Page 37: Computer Graphicsmohamed_berbar.tripod.com/ch3.pdf · 2007. 4. 2. · 2/04/2007 Computer Graphics CS510737 Windows and Viewports • Previously we looked at an OpenGL window where

� �

4/2/2007 Computer Graphics CS510737 � �

4/2/2007 Computer Graphics CS510737 � �

PRACTICE EXERCISE

3.3.1 Hand simulation of clipSegment ()

Go through the clipping routine by hand for the case of a window given by {left, right, bottom, top} = (30, 220, 50, 240) and the following line segments:

1. p1 = (40,140),p2 = (100,200);

2. p2 = (10, 270), p1 = (300, 0);

3. p1 = (20,10),p2 = (20,200);

4. p1 = (0, 0), p2 = (250,250);

In each case, determine the endpoints of the clipped segment, and for a visual check, sketch the situation on graph paper.

Page 38: Computer Graphicsmohamed_berbar.tripod.com/ch3.pdf · 2007. 4. 2. · 2/04/2007 Computer Graphics CS510737 Windows and Viewports • Previously we looked at an OpenGL window where

� �

4/2/2007 Computer Graphics CS510737 � �

Section: 3.4 and 3.5

• We will study only Some useful Supporting Classes

4/2/2007 Computer Graphics CS510737 � �

Developing The CANVAS classSome useful Supporting Classes

class Point2 //single point w/ floating point coordinates{public:

Point2() {x = y = 0.0f;} //constructor 1Point2(float xx, float yy) {x=xx; y=yy;} //constructor 2void set(float xx, float yy) {x=xx; y=yy;}float getX() {return x;}float getY() {return y;}void draw(void){

glBegin(GL_POINTS); //draw this pointglVertex2f((GLfloat)x, (GLfloat)y);glEnd();

}private:

float x ,y;};

Page 39: Computer Graphicsmohamed_berbar.tripod.com/ch3.pdf · 2007. 4. 2. · 2/04/2007 Computer Graphics CS510737 Windows and Viewports • Previously we looked at an OpenGL window where

� �

4/2/2007 Computer Graphics CS510737 � �

class IntRect //aligned rectangle with integer coordinates, used for viewport{public:

IntRect() {l = 0; r=100; b=0; t=100;} //constructorsIntRect(int left, int right, int bottom, int top)

{l = left; r=right; b=bottom; t=top;}void set(int left, int right, int bottom, int top){

l=left; r=right; b=bottom; t=top;}void draw(void); //draw this rectangle using OpenGLint getL(void) {

return l;}int getR(void) {

return r;}int getT(void) {

return t;}int getB(void) {

return b;}

private:int l, r, b, t;

};

4/2/2007 Computer Graphics CS510737 � �

class RealRect //simlar to IntRect but w/ floating points & used for world window{public:

RealRect() {l = 0; r=100; b=0; t=100;} //constructorsRealRect(float left, float right, float bottom, float top)

{l = left; r=right; b=bottom; t=top;}void set(float left, float right, float bottom, float top){

l=left; r=right; b=bottom; t=top;}float getL(void) {

return l;}float getR(void) {

return r;}float getT(void) {

return t;}float getB(void) {

return b;}void draw(void); //draw this rectangle using OpenGL

private:float l, r, b, t;

};//<<End Support Classes>>>

Page 40: Computer Graphicsmohamed_berbar.tripod.com/ch3.pdf · 2007. 4. 2. · 2/04/2007 Computer Graphics CS510737 Windows and Viewports • Previously we looked at an OpenGL window where

� �

4/2/2007 Computer Graphics CS510737 � �

class Canvas{public:

Canvas(int width, int height, char* windowTitle); //constructorvoid setWindow(float l, float r, float b, float t);void setViewport(int l, int r, int b, int t);IntRect getViewport(void); //divulge the viewport dataRealRect getWindow(void); // divulge the window datafloat getWindowAspectRatio(void);void clearScreen();void setBackgroundColor(float r, float g, float b);void setColor(float r, float g, float b);void lineTo(float x, float y);void lineTo(Point2 p);void moveTo(float x, float y);void moveTo(Point2 p);

void moveRel(float dx, float dy);private:

Point2 CP; //current position in the worldIntRect viewport; //the current windowRealRect window; //the current viewport

} ;

4/2/2007 Computer Graphics CS510737 � �

Relative Drawing

• This section refers to the functions developed in the Canvas class from section 3.4 in the textbook.

• You will need to program this class on your own as you will need to implement it.

Page 41: Computer Graphicsmohamed_berbar.tripod.com/ch3.pdf · 2007. 4. 2. · 2/04/2007 Computer Graphics CS510737 Windows and Viewports • Previously we looked at an OpenGL window where

� �

4/2/2007 Computer Graphics CS510737 � �

Relative Drawing

• Relative Drawing refers to moving the drawing

point on the canvas relative to the current

drawing location.

• Think of it like moving a pen on paper.

• moveRel() and lineRel()

The function moveRel() is simple: it just moves

the CP through the displacement (dx, dy).

4/2/2007 Computer Graphics CS510737 � �

void Canvas:: moveRel(float dx, float dy)

{

CP.set(CP.getX() + dx, CP.getY() + dy);

}

The function lineRel(float dx, float dy) does this too, but it first draws a line from the old CP to the new one.

void Canvas:: lineRel( float dx, float dy)

{ float x = CP.getX() + dx, y = CP.getY() + dy ;

lineTo(x,y);

CP.set(x, y);

}

Page 42: Computer Graphicsmohamed_berbar.tripod.com/ch3.pdf · 2007. 4. 2. · 2/04/2007 Computer Graphics CS510737 Windows and Viewports • Previously we looked at an OpenGL window where

� �

4/2/2007 Computer Graphics CS510737 � �

Turtle Graphics

• Drawing lines and points on the screen.

• Commanding a drawing turtle (pen in a plotter).

moveto(), forward(), turn()

turnTo(float angle) This function turns the turtle to given angle and is implemented as

Void Canvas :: turnTo(float angle) { CD = angle;}

turn (float angle)

This function turns the turtle through angle degrees

Void Canvas :: turn(angle) {CD += angle;}

See page 107-110 for more details

4/2/2007 Computer Graphics CS510737 � �

Void Canvas :: forward(float dist, int isVisible){

// Code …. See page 107

}

Old world CP

New world CP

Page 43: Computer Graphicsmohamed_berbar.tripod.com/ch3.pdf · 2007. 4. 2. · 2/04/2007 Computer Graphics CS510737 Windows and Viewports • Previously we looked at an OpenGL window where

� �

4/2/2007 Computer Graphics CS510737 � �

4/2/2007 Computer Graphics CS510737 � �

Turtle Graphics

Example: Polyspirals

for( some iterations )

{

forward(length, 1);

turn(angle);

length += increment;

}

- polyspiral.cpp

Page 44: Computer Graphicsmohamed_berbar.tripod.com/ch3.pdf · 2007. 4. 2. · 2/04/2007 Computer Graphics CS510737 Windows and Viewports • Previously we looked at an OpenGL window where

� �

4/2/2007 Computer Graphics CS510737 � �

Turtle Graphics

Example: Polyspiralsfloat angle = 87, inc = 0.5;

4/2/2007 Computer Graphics CS510737 � �

Turtle Graphics

Example: Polyspiralsfloat angle = 170, inc = 1;

Page 45: Computer Graphicsmohamed_berbar.tripod.com/ch3.pdf · 2007. 4. 2. · 2/04/2007 Computer Graphics CS510737 Windows and Viewports • Previously we looked at an OpenGL window where

� �

4/2/2007 Computer Graphics CS510737 � �

Turtle Graphics

Example: Polyspiralsfloat angle = 89.5, inc = 1;

4/2/2007 Computer Graphics CS510737 � �

3.6 Figures Based on Regular Polygons

• Definition: A polygon is a regular if it is simple, if all its sides have equal lengths, and if adjacent sides meet at equal interior angles.

• Give the name n-gon to a regular polygon having n sides.

Page 46: Computer Graphicsmohamed_berbar.tripod.com/ch3.pdf · 2007. 4. 2. · 2/04/2007 Computer Graphics CS510737 Windows and Viewports • Previously we looked at an OpenGL window where

� �

4/2/2007 Computer Graphics CS510737 � �

Regular Polygons

• To draw a regular polygon we need to specify the centre, the radius and a rotation angle.

( ) /n)i sin(2π Ri/n), cos(2π RiP =

0 <= i <= n -1

The center is (0, 0)

4/2/2007 Computer Graphics CS510737 � �

void ngon(int n, float cx, float cy, float radius, float rotAngle){ //assumes global Canvas object, cvs

if(n<3) return ; //bad number of sides

double angle = rotAngle * 3.14159265 / 180; // starting angle = 0 for fig above

//angle in radians

double angleInc = 2 * 3.14159265 / n; //angle incrementcvs.moveTo(radius * cos(angle) + cx, radius * sin(angle) + cy);

for(int k=0; k<n; k++) //repeat n times{angle += angleInc;cvs.lineTo(radius * cos(angle) + cx, radius + sin(angle) + cy);}

}

The center point (cx, cy)

60o

Page 47: Computer Graphicsmohamed_berbar.tripod.com/ch3.pdf · 2007. 4. 2. · 2/04/2007 Computer Graphics CS510737 Windows and Viewports • Previously we looked at an OpenGL window where

� �

4/2/2007 Computer Graphics CS510737 � �

Regular Polygons

ngon(6,0.0,0.0,50.0, 20.0);

ngon(10,0.0,0.0,40.0, 30.0);

ngon(4,0.0,0.0,30.0, 10.0);

ngon(20,0.0,0.0,20.0, 5.0);

- ngon.cpp

4/2/2007 Computer Graphics CS510737 � �

The rosette and the golden 5-rosette

• Rosettes are easy to draw: Simply connect every vertex to every other.

The 5, 11, and 17 rosettes

Page 48: Computer Graphicsmohamed_berbar.tripod.com/ch3.pdf · 2007. 4. 2. · 2/04/2007 Computer Graphics CS510737 Windows and Viewports • Previously we looked at an OpenGL window where

� �

4/2/2007 Computer Graphics CS510737 � �

Circles and Arcs

• A regular polygon begins to represent a circle as the number of vertices becomes larger.

void drawCircle(Point2 center, float radius)

{

const int numVerts = 50;

ngon(numVerts, center.getX(), center.getY(), radius, 0);

}

number of intermediate segments in circle

4/2/2007 Computer Graphics CS510737 � �

Circles and Arcs

• However this code won’t allow you to draw an arc.

• To draw an arc or circle you need to know, the centre, the radius, the starting angle (a) and the sweep angle (b).

Page 49: Computer Graphicsmohamed_berbar.tripod.com/ch3.pdf · 2007. 4. 2. · 2/04/2007 Computer Graphics CS510737 Windows and Viewports • Previously we looked at an OpenGL window where

� �

4/2/2007 Computer Graphics CS510737 � �

Circles and ArcsThe code is similar to ngon() however it allows an arc to be drawn, which means that the shape isn’t drawn around 360 degrees.

drawArc(Point2 center, float radius, float startAngle, float sweep)

{ // startAngle and sweep are in degreesconst int n = 30; // number of intermediate segments in arc

float angle = startAngle * 3.14159265 / 180; // starting angle in radians

float angleInc = sweep * 3.14159265 /(180 * n); // angle increment

float cx = center.getX(), cy = center.getY(); // get center

cvs.moveTo( cx + radius * cos(angle), cy + radius * sin(angle));

for(int k = 1; k < n; k++){angle += angleInc;cvs.lineTo(cx + radius * cos(angle), cy + radius * sin(angle));}}

4/2/2007 Computer Graphics CS510737 � �

Circles and Arcs

// set center with (0, 0)

p.set(0.0,0.0);

cvs.clearScreen();

glLineWidth(2.0);

drawArc(p, 50,45,90);

drawArc(p, 40,15,90);

drawArc(p, 30,0,90);

drawArc(p, 20,90,90);