computer graphics - jordan university of science and ...yaser/courses/cs480/slides... · computer...

46
Computer Graphics Computer Graphics Computer Graphics Computer Graphics CSC CSC480 476 Chapter Three Chapter Three Computer Graphics C S 4 8 0 This Week Windows, Viewports and Clippings Creating Useful Drawing Tools Turtle Graphics

Upload: vokien

Post on 07-Jul-2018

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Computer Graphics - Jordan University of Science and ...yaser/courses/cs480/slides... · Computer Graphics CS480 Screen Windows and Viewports The World (what you can see, the real

Computer GraphicsComputer GraphicsComputer GraphicsComputer Graphics

CSCCSC480476

Chapter ThreeChapter Three

Computer Graphics C S 4 8 0

This Week• Windows, Viewports and Clippings

• Creating Useful Drawing Tools

• Turtle Graphics

Page 2: Computer Graphics - Jordan University of Science and ...yaser/courses/cs480/slides... · Computer Graphics CS480 Screen Windows and Viewports The World (what you can see, the real

Computer Graphics C S 4 8 0

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.

Computer Graphics C S 4 8 0

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 Graphics - Jordan University of Science and ...yaser/courses/cs480/slides... · Computer Graphics CS480 Screen Windows and Viewports The World (what you can see, the real

Computer Graphics C S 4 8 0

Screen

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

Screen Window

The World Window (the bit we want to capture)

Viewport

Computer Graphics C S 4 8 0

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 Graphics - Jordan University of Science and ...yaser/courses/cs480/slides... · Computer Graphics CS480 Screen Windows and Viewports The World (what you can see, the real

Computer Graphics CS 480 `

Windows and Viewports

World Window

Example Viewports

Computer Graphics CS 480 ´

Windows and Viewports

World Window Viewport

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

This is called Mapping

Page 5: Computer Graphics - Jordan University of Science and ...yaser/courses/cs480/slides... · Computer Graphics CS480 Screen Windows and Viewports The World (what you can see, the real

Computer Graphics CS 480 ^

Windows and Viewports• Recall from the last lecture:

§ x’ = Ax + B

§ y’ = Cy + D

• This is exactly how mapping is achieved!!

Computer Graphics C S 4 8 0

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 Graphics - Jordan University of Science and ...yaser/courses/cs480/slides... · Computer Graphics CS480 Screen Windows and Viewports The World (what you can see, the real

4/6/2009 Computer Graphics C S 4 8 0

4/6/2009 Computer Graphics C S 4 8 0

Page 7: Computer Graphics - Jordan University of Science and ...yaser/courses/cs480/slides... · Computer Graphics CS480 Screen Windows and Viewports The World (what you can see, the real

`

4/6/2009 Computer Graphics C S 4 8 0

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 the screen '

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

Computer Graphics C S 4 8 0

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 Graphics - Jordan University of Science and ...yaser/courses/cs480/slides... · Computer Graphics CS480 Screen Windows and Viewports The World (what you can see, the real

´

Computer Graphics C S 4 8 0

Windows and Viewports

xmin, ymin, xmax, ymax = W.l, W.b, W.r, W.t

SCREENWIDTH = V.r – V.lSCREENHEIGHT = V.t – V.b

4/6/2009 Computer Graphics C S 4 8 0

• 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

A C

Page 9: Computer Graphics - Jordan University of Science and ...yaser/courses/cs480/slides... · Computer Graphics CS480 Screen Windows and Viewports The World (what you can see, the real

^

Computer Graphics CS 480 `

Windows and Viewports

bWtW

bWy

bVtV

bVsy

..

.

..

.

-

-=

-

-

• The same holds for y:

• or by rearranging

÷ø

öçè

æ

-

--+

-

-= bW

bWtW

bWtVbVy

bWtW

bVtVsy .

..

...

..

..

B D

4/6/2009 Computer Graphics CS 480 ´

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 Graphics - Jordan University of Science and ...yaser/courses/cs480/slides... · Computer Graphics CS480 Screen Windows and Viewports The World (what you can see, the real

4/6/2009 Computer Graphics CS 480 ^

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 ??

Computer Graphics C S 4 8 0

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 Graphics - Jordan University of Science and ...yaser/courses/cs480/slides... · Computer Graphics CS480 Screen Windows and Viewports The World (what you can see, the real

Computer Graphics C S 4 8 0

Windows and Viewports• Do you need to perform these

calculations each time you draw something with OpenGL??

Computer Graphics C S 4 8 0

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 Graphics - Jordan University of Science and ...yaser/courses/cs480/slides... · Computer Graphics CS480 Screen Windows and Viewports The World (what you can see, the real

Computer Graphics C S 4 8 0

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.

Computer Graphics C S 4 8 0

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 Graphics - Jordan University of Science and ...yaser/courses/cs480/slides... · Computer Graphics CS480 Screen Windows and Viewports The World (what you can see, the real

4/6/2009 Computer Graphics C S 4 8 0

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 gluOrtho D(GLdouble gluOrtho22D(GLdouble left, GLdouble rightleft, GLdouble , GLdouble bottomright, GLdouble , GLdouble bottom, GLdouble toptop););

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

void void gIViewport(GLint xx, GLint gIViewport(GLint , GLint width, GLint yy, GLint , GLint heightwidth, GLint height););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/6/2009 Computer Graphics C S 4 8 0

• 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();

gluOrthogluOrtho22D(D(00..00, , 22..00, , 00..00, , 11..00); );

// sets the viewport

gIViewport(gIViewport(4040, , 6060, , 360360, , 240240););

Page 14: Computer Graphics - Jordan University of Science and ...yaser/courses/cs480/slides... · Computer Graphics CS480 Screen Windows and Viewports The World (what you can see, the real

4/6/2009 Computer Graphics CS 480 `

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 gIViewport () The default viewport was used, since no gIViewport () 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);

Computer Graphics CS 480 ´

Windows and ViewportsExample

World Window Viewport(10,6)

(-10,-6)

400

0

0 600

Page 15: Computer Graphics - Jordan University of Science and ...yaser/courses/cs480/slides... · Computer Graphics CS480 Screen Windows and Viewports The World (what you can see, the real

Computer Graphics CS 480 ^

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);

}

Computer Graphics C S 4 8 0

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 Graphics - Jordan University of Science and ...yaser/courses/cs480/slides... · Computer Graphics CS480 Screen Windows and Viewports The World (what you can see, the real

Computer Graphics C S 4 8 0

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);}

Computer Graphics C S 4 8 0

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 Graphics - Jordan University of Science and ...yaser/courses/cs480/slides... · Computer Graphics CS480 Screen Windows and Viewports The World (what you can see, the real

`

4/6/2009 Computer Graphics C S 4 8 0

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();}

4/6/2009 Computer Graphics C S 4 8 0

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

Page 18: Computer Graphics - Jordan University of Science and ...yaser/courses/cs480/slides... · Computer Graphics CS480 Screen Windows and Viewports The World (what you can see, the real

´

4/6/2009 Computer Graphics C S 4 8 0

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 ina viewport of size 64 by 44 pixelswhose aspect ratio (64/44)matches that of the worldwindow. Thus, each dinosaur isdrawn without distortion.

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

4/6/2009 Computer Graphics C S 4 8 0

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

}

?

Page 19: Computer Graphics - Jordan University of Science and ...yaser/courses/cs480/slides... · Computer Graphics CS480 Screen Windows and Viewports The World (what you can see, the real

^

4/6/2009 Computer Graphics CS 480 `

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.

4/6/2009 Computer Graphics CS 480 ´

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.

Page 20: Computer Graphics - Jordan University of Science and ...yaser/courses/cs480/slides... · Computer Graphics CS480 Screen Windows and Viewports The World (what you can see, the real

4/6/2009 Computer Graphics CS 480 ^

[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

4/6/2009 Computer Graphics C S 4 8 0

[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.

Page 21: Computer Graphics - Jordan University of Science and ...yaser/courses/cs480/slides... · Computer Graphics CS480 Screen Windows and Viewports The World (what you can see, the real

4/6/2009 Computer Graphics C S 4 8 0

[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!

4/6/2009 Computer Graphics C S 4 8 0

[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

Page 22: Computer Graphics - Jordan University of Science and ...yaser/courses/cs480/slides... · Computer Graphics CS480 Screen Windows and Viewports The World (what you can see, the real

4/6/2009 Computer Graphics C S 4 8 0

[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 =

4/6/2009 Computer Graphics C S 4 8 0

[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

Page 23: Computer Graphics - Jordan University of Science and ...yaser/courses/cs480/slides... · Computer Graphics CS480 Screen Windows and Viewports The World (what you can see, the real

4/6/2009 Computer Graphics C S 4 8 0

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).

4/6/2009 Computer Graphics C S 4 8 0

•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(setViewport(00, W, , W, 00, W/R);, 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 have height H, but width HR, so it is set with the command

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

Page 24: Computer Graphics - Jordan University of Science and ...yaser/courses/cs480/slides... · Computer Graphics CS480 Screen Windows and Viewports The World (what you can see, the real

4/6/2009 Computer Graphics CS 480 `

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 width of the screen window. setViewport(0,W,0,W/R);

4/6/2009 Computer Graphics CS 480 ´

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);

Page 25: Computer Graphics - Jordan University of Science and ...yaser/courses/cs480/slides... · Computer Graphics CS480 Screen Windows and Viewports The World (what you can see, the real

4/6/2009 Computer Graphics CS 480 ^

• EXAMPLE 3.2.7 A tall window

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

Therefore, Case (b) applies, and the viewport is set to have a height ofH = 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 previousexample.

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.

4/6/2009 Computer Graphics C S 4 8 0

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 glViewport defaults to the window coordinates.

Page 26: Computer Graphics - Jordan University of Science and ...yaser/courses/cs480/slides... · Computer Graphics CS480 Screen Windows and Viewports The World (what you can see, the real

4/6/2009 Computer Graphics C S 4 8 0

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:

4/6/2009 Computer Graphics C S 4 8 0

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 matchesthe aspect ratio R of the window) that will fit into the newscreen window. The routine obtains the width and height ofthe new screen window through its arguments. The code is asimple embodiment of the result in Figure 3.16.

Page 27: Computer Graphics - Jordan University of Science and ...yaser/courses/cs480/slides... · Computer Graphics CS480 Screen Windows and Viewports The World (what you can see, the real

`

4/6/2009 Computer Graphics C S 4 8 0

PRACTICE EXERCISESPage 95 By the student

4/6/2009 Computer Graphics C S 4 8 0

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 1and p2 lies inside the world windowlies inside the world window and then reports back the endpoints of that part.

Page 28: Computer Graphics - Jordan University of Science and ...yaser/courses/cs480/slides... · Computer Graphics CS480 Screen Windows and Viewports The World (what you can see, the real

´

4/6/2009 Computer Graphics C S 4 8 0

• 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.

4/6/2009 Computer Graphics C S 4 8 0

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

Page 29: Computer Graphics - Jordan University of Science and ...yaser/courses/cs480/slides... · Computer Graphics CS480 Screen Windows and Viewports The World (what you can see, the real

^

4/6/2009 Computer Graphics CS 480 `

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?

4/6/2009 Computer Graphics CS 480 ´

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.

Page 30: Computer Graphics - Jordan University of Science and ...yaser/courses/cs480/slides... · Computer Graphics CS480 Screen Windows and Viewports The World (what you can see, the real

4/6/2009 Computer Graphics CS 480 ^

• The entire procedure gives rise to the following strategy:

do{

form the code words for p 1 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.

4/6/2009 Computer Graphics C S 4 8 0

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 one end point inside the window and one outside, or;

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

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

Page 31: Computer Graphics - Jordan University of Science and ...yaser/courses/cs480/slides... · Computer Graphics CS480 Screen Windows and Viewports The World (what you can see, the real

4/6/2009 Computer Graphics C S 4 8 0

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 =

4/6/2009 Computer Graphics C S 4 8 0

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

Look to the book page 98

Page 32: Computer Graphics - Jordan University of Science and ...yaser/courses/cs480/slides... · Computer Graphics CS480 Screen Windows and Viewports The World (what you can see, the real

4/6/2009 Computer Graphics C S 4 8 0

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

4/6/2009 Computer Graphics C S 4 8 0

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);}

Page 33: Computer Graphics - Jordan University of Science and ...yaser/courses/cs480/slides... · Computer Graphics CS480 Screen Windows and Viewports The World (what you can see, the real

4/6/2009 Computer Graphics C S 4 8 0

• 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

4/6/2009 Computer Graphics C S 4 8 0

Page 34: Computer Graphics - Jordan University of Science and ...yaser/courses/cs480/slides... · Computer Graphics CS480 Screen Windows and Viewports The World (what you can see, the real

4/6/2009 Computer Graphics CS 480 `

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.

4/6/2009 Computer Graphics CS 480 ´

Section: 3.4 and 3.5

• We will study only Some useful Supporting Classes

Page 35: Computer Graphics - Jordan University of Science and ...yaser/courses/cs480/slides... · Computer Graphics CS480 Screen Windows and Viewports The World (what you can see, the real

4/6/2009 Computer Graphics CS 480 ^

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;};

4/6/2009 Computer Graphics CS 480 `

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;

};

Page 36: Computer Graphics - Jordan University of Science and ...yaser/courses/cs480/slides... · Computer Graphics CS480 Screen Windows and Viewports The World (what you can see, the real

4/6/2009 Computer Graphics CS 480 `

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>>>

4/6/2009 Computer Graphics CS 480 `

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

} ;

Page 37: Computer Graphics - Jordan University of Science and ...yaser/courses/cs480/slides... · Computer Graphics CS480 Screen Windows and Viewports The World (what you can see, the real

`

4/6/2009 Computer Graphics CS 480 `

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.

4/6/2009 Computer Graphics CS 480 `

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).

Page 38: Computer Graphics - Jordan University of Science and ...yaser/courses/cs480/slides... · Computer Graphics CS480 Screen Windows and Viewports The World (what you can see, the real

´

4/6/2009 Computer Graphics CS 480 `

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);

}

4/6/2009 Computer Graphics CS 480 `

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

Page 39: Computer Graphics - Jordan University of Science and ...yaser/courses/cs480/slides... · Computer Graphics CS480 Screen Windows and Viewports The World (what you can see, the real

^

4/6/2009 Computer Graphics CS 480 ` `

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

// Code …. See page 107

}

Old world CP

New world CP

4/6/2009 Computer Graphics CS 480 ` ´

Page 40: Computer Graphics - Jordan University of Science and ...yaser/courses/cs480/slides... · Computer Graphics CS480 Screen Windows and Viewports The World (what you can see, the real

4/6/2009 Computer Graphics CS 480 ` ^

Turtle Graphics

Example: Polyspirals

for( some iterations )

{

forward(length, 1);

turn(angle);

length += increment;

}

- polyspiral.cpp

4/6/2009 Computer Graphics CS 480 ´

Turtle Graphics

Example: Polyspiralsfloat angle = 87, inc = 0.5;

Page 41: Computer Graphics - Jordan University of Science and ...yaser/courses/cs480/slides... · Computer Graphics CS480 Screen Windows and Viewports The World (what you can see, the real

4/6/2009 Computer Graphics CS 480 ´

Turtle Graphics

Example: Polyspiralsfloat angle = 170, inc = 1;

4/6/2009 Computer Graphics CS 480 ´

Turtle Graphics

Example: Polyspiralsfloat angle = 89.5, inc = 1;

Page 42: Computer Graphics - Jordan University of Science and ...yaser/courses/cs480/slides... · Computer Graphics CS480 Screen Windows and Viewports The World (what you can see, the real

4/6/2009 Computer Graphics CS 480 ´

3.6 Figures Based on Regular Polygons

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

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

4/6/2009 Computer Graphics CS 480 ´

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)

Page 43: Computer Graphics - Jordan University of Science and ...yaser/courses/cs480/slides... · Computer Graphics CS480 Screen Windows and Viewports The World (what you can see, the real

4/6/2009 Computer Graphics CS 480 ´

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; // rotAngle is the starting angle = 0 for the

// above figure … angle in radians

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

// (cx,cy) is the origin (0,0) for the above figure.

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

4/6/2009 Computer Graphics CS 480 ´

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

void ngon(int n, float cx, float cy, float radius, float rotAngle)

Page 44: Computer Graphics - Jordan University of Science and ...yaser/courses/cs480/slides... · Computer Graphics CS480 Screen Windows and Viewports The World (what you can see, the real

4/6/2009 Computer Graphics CS 480 ´ `

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

4/6/2009 Computer Graphics CS 480 ´ ´

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;

// void ngon(int n, float cx, float cy, float radius, float rotAngle)

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

}

number of intermediate segments in circle

Page 45: Computer Graphics - Jordan University of Science and ...yaser/courses/cs480/slides... · Computer Graphics CS480 Screen Windows and Viewports The World (what you can see, the real

4/6/2009 Computer Graphics CS 480 ´ ^

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).

4/6/2009 Computer Graphics CS 480 ^

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));}}

Page 46: Computer Graphics - Jordan University of Science and ...yaser/courses/cs480/slides... · Computer Graphics CS480 Screen Windows and Viewports The World (what you can see, the real

4/6/2009 Computer Graphics CS 480 ^

Circles and Arcs

// set center with (0, 0)

p.set(0.0,0.0);

cvs.clearScreen();

glLineWidth(2.0);

//drawArc(center, radius,startAngle, sweep)

drawArc(p, 50,45,90);

drawArc(p, 40,15,90);

drawArc(p, 30,0,90);

drawArc(p, 20,90,90);