1 cpe 333 : computer graphics...

39
1 CPE 333 : Computer CPE 333 : Computer Graphics Graphics มมมมมมมมมมมมมมมมมมมม มมมมมมมมมมมมมมมมมมมม มมมมมมมมมมมมมมมมม มมมมมมมมมมมมมมมมม Dr. Natasha Dejdumrong

Upload: regina-cummings

Post on 27-Dec-2015

226 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 CPE 333 : Computer Graphics มหาวิทยาลัยเทคโนโลยีพระจอม เกล้าธนบุรี Dr. Natasha Dejdumrong

1

CPE 333 : Computer CPE 333 : Computer GraphicsGraphics

มหาวิ�ทยาลัยเทคโนโลัย�มหาวิ�ทยาลัยเทคโนโลัย�พระจอมเกลั�าธนบุ�ร�พระจอมเกลั�าธนบุ�ร�

Dr. Natasha Dejdumrong

Page 2: 1 CPE 333 : Computer Graphics มหาวิทยาลัยเทคโนโลยีพระจอม เกล้าธนบุรี Dr. Natasha Dejdumrong

2

Course DescriptionCourse Description • Line Drawing• Circle Drawing [ Ellipse Drawing ]• 2D and 3D Transformations• Line and Polygon Clipping techniques• 3D Viewing• Geometric Modeling: Representation of Curves and Surfaces, • Solid Modeling• Hidden Surface Removal Algorithms• Illumination and Shading models• Ray tracing

Page 3: 1 CPE 333 : Computer Graphics มหาวิทยาลัยเทคโนโลยีพระจอม เกล้าธนบุรี Dr. Natasha Dejdumrong

3

Text Book and SupplementsText Book and Supplements

• Textbook– Hearn Baker, Computer Graphics with OpenGL,

3rd Ed., Pearson International Edition, 2004.

• Lecture Note• Powerpoints

Page 4: 1 CPE 333 : Computer Graphics มหาวิทยาลัยเทคโนโลยีพระจอม เกล้าธนบุรี Dr. Natasha Dejdumrong

4

ScheduleSchedule

• Classroom: CB40805• Lecture Time: Monday 13:30 – 15:20 [AB]

Tuesday 14:30 – 16:20 [CD]• Lab Time: Monday 14:30 – 16:20 [AB]

Tuesday 16:30 – 17:20 [CD]

Page 5: 1 CPE 333 : Computer Graphics มหาวิทยาลัยเทคโนโลยีพระจอม เกล้าธนบุรี Dr. Natasha Dejdumrong

5

Active LearningActive Learning

• Lecture: 2 Hours• Laboratory: 2 Hours• Teaching Assistant: Pitchaya Jamjun

Page 6: 1 CPE 333 : Computer Graphics มหาวิทยาลัยเทคโนโลยีพระจอม เกล้าธนบุรี Dr. Natasha Dejdumrong

6

MarkingMarking

Midterm Examination 20%

Final Examination 30%

Assignments 40%

Class + Lab Attendance 10%

Total 100%

Page 7: 1 CPE 333 : Computer Graphics มหาวิทยาลัยเทคโนโลยีพระจอม เกล้าธนบุรี Dr. Natasha Dejdumrong

7

Grading PolicyGrading Policy

• 80 or above A• 75-79 B+• 70-74 B• 65-69 C+• 60-64 C• 55-59 D+• 50-54 D• under 50 F

Page 8: 1 CPE 333 : Computer Graphics มหาวิทยาลัยเทคโนโลยีพระจอม เกล้าธนบุรี Dr. Natasha Dejdumrong

8

Chapter 2Chapter 2

Scan Conversion

Page 9: 1 CPE 333 : Computer Graphics มหาวิทยาลัยเทคโนโลยีพระจอม เกล้าธนบุรี Dr. Natasha Dejdumrong

9

OverviewOverview

• Point• Line• Circle• Ellipse

Page 10: 1 CPE 333 : Computer Graphics มหาวิทยาลัยเทคโนโลยีพระจอม เกล้าธนบุรี Dr. Natasha Dejdumrong

10

Raster GraphicsRaster Graphics

A raster graphics image, digital image, or bitmap, is a data file or structure representing a generally rectangular grid of pixels, or points of color, on a computer monitor, paper, or other display device.

Page 11: 1 CPE 333 : Computer Graphics มหาวิทยาลัยเทคโนโลยีพระจอม เกล้าธนบุรี Dr. Natasha Dejdumrong

11

Vector GraphicsVector Graphics

• Vector graphics (also called Geometric Modeling or Object-Oriented Graphics) is the use of geometrical primitives such as points, lines, curves, and polygons, which are all based upon mathematical equations to represent images in computer graphics. It is used by contrast to the term raster graphics, which is the

representation of images as a collection of pixels (dots).

• Raster graphics are distinguished from vector graphics in that vector graphics represent an image through the use of geometric objects such as curves and polygons.

Page 12: 1 CPE 333 : Computer Graphics มหาวิทยาลัยเทคโนโลยีพระจอม เกล้าธนบุรี Dr. Natasha Dejdumrong

12

Raster vs Vector GraphicsRaster vs Vector Graphics

• Raster graphics cannot be scaled to a higher resolution without loss of apparent quality. This is in contrast to vector graphics, which easily scale to the quality of the device on which they are rendered.

• Raster graphics are more practical than vector graphics for photographs and photo-realistic images, while vector graphics are often more practical for typesetting or graphic

design.

Page 13: 1 CPE 333 : Computer Graphics มหาวิทยาลัยเทคโนโลยีพระจอม เกล้าธนบุรี Dr. Natasha Dejdumrong

13

RGBRGB • The color of each pixel is individually defined; images in the

RGB color space, for instance, often consist of colored pixels defined by three bytes—one byte each for red, green and blue.

• Less colorful images require less information per pixel; an image with only black and white pixels requires only a single bit for each pixel.

• A colored raster image (or pixmap) will usually have pixels with between one and eight bits for each of the red, green,

and blue components.

Page 14: 1 CPE 333 : Computer Graphics มหาวิทยาลัยเทคโนโลยีพระจอม เกล้าธนบุรี Dr. Natasha Dejdumrong

14

ResolutionResolution • The quality of a raster image is determined by the total

number of pixels (resolution), and the amount of information in each pixel (often called color depth).

• For example, an image that stores 24 bits of color information per pixel can represent smoother degrees of shading than one that only stores 16 bits per pixel, but not as smooth as one that stores 48 bits.

• Likewise, an image sampled at 640 x 480 pixels (therefore containing 307,200 pixels) will look rough and blocky compared to one sampled at 1280 x 1024 (1,310,720 pixels). Because it takes a large amount of data to store a high-quality image.

Page 15: 1 CPE 333 : Computer Graphics มหาวิทยาลัยเทคโนโลยีพระจอม เกล้าธนบุรี Dr. Natasha Dejdumrong

15

PointPoint

• Real WorldA point has no dimension.

• Practical RepresentationA point has size.

• Analytic GeometryA point P = (x, y) [For 2 Dimension]

A point P = (x, y, z) [For 3 Dimension]

1 2 3 4 5 6 7 8

12345678

1 2 3 4 5 6 7 8

12345678

00

Page 16: 1 CPE 333 : Computer Graphics มหาวิทยาลัยเทคโนโลยีพระจอม เกล้าธนบุรี Dr. Natasha Dejdumrong

16

Raster GraphicsRaster Graphics

• Raster Graphics• Pixels

Example

Point(4.3, 5.3) = Pixel(4, 5)

Point(3.8, 4.8) = Pixel(4, 5)

1 2 3 4 5 6 7 8

12345678

00

1 2 3 4 5 6 7 8

12345678

00

1 2 3 4 5 6 7 8

12345678

))(),((),( yxPixelyxPoint IntInt

Page 17: 1 CPE 333 : Computer Graphics มหาวิทยาลัยเทคโนโลยีพระจอม เกล้าธนบุรี Dr. Natasha Dejdumrong

Straight LineStraight Line

(x2,y2)

(x1,y1)

x

y

y = m x +b

x

ym

y

x

b

Page 18: 1 CPE 333 : Computer Graphics มหาวิทยาลัยเทคโนโลยีพระจอม เกล้าธนบุรี Dr. Natasha Dejdumrong

18

Equation : y = m x + b

b : y-intercept m : dy/dx

F(x, y) : Ax + By +C = 0

F(x, y) : dy x - dx y + b dx = 0

Therefore, A = dy, B = -dx, C = b dx

Straight LineStraight Line

bxdx

dyy

Page 19: 1 CPE 333 : Computer Graphics มหาวิทยาลัยเทคโนโลยีพระจอม เกล้าธนบุรี Dr. Natasha Dejdumrong

U(xi+1, yi+1)

M

D (xi, yi )

(xi+1, yi )

Bresenham’s Line AlgorithmBresenham’s Line Algorithm

Page 20: 1 CPE 333 : Computer Graphics มหาวิทยาลัยเทคโนโลยีพระจอม เกล้าธนบุรี Dr. Natasha Dejdumrong

U(xi+1, yi+1)

M

D (xi, yi )

(xi+1, yi )

U(xi+1, yi+1)

M

D (xi, yi )

(xi+1, yi )

Midpoint Line AlgorithmMidpoint Line Algorithm

Page 21: 1 CPE 333 : Computer Graphics มหาวิทยาลัยเทคโนโลยีพระจอม เกล้าธนบุรี Dr. Natasha Dejdumrong

Suppose that point (xi, yi) has been plotted.

We move xi to xi + 1.

The problem is how to select between two pixels,

U (xi + 1, yi + 1) and D (xi + 1, yi ).

For this purpose, we consider the middle pixel

M (xi + 1, yi + 1/2).

d = F(M) = a (xi + 1) + b( yi + 1/2) + c

IF d > 0 , choose U

d < 0 , choose D

d = 0 , choose either U or D, so choose U.

Page 22: 1 CPE 333 : Computer Graphics มหาวิทยาลัยเทคโนโลยีพระจอม เกล้าธนบุรี Dr. Natasha Dejdumrong

• When D is chosen, M is incremented one step in the x direction. So

dnew = F (xi +2, yi + 1/2)

= a (xi + 2) + b (yi + 1/2) + c

while

dold = F (xi + 1, yi + 1/2) = a (xi + 1) + b (yi + 1/2) + c

So the increment in d (denoted dD) is

dD = dnew - dold = a = dy

Page 23: 1 CPE 333 : Computer Graphics มหาวิทยาลัยเทคโนโลยีพระจอม เกล้าธนบุรี Dr. Natasha Dejdumrong

• When U (xi + 1, yi + 1) is chosen, M is incremented one step in both directions:

dnew = F (xi +2, yi + 3/2)

= a (xi + 2) + b( yi + 3/2) + c

= dold + a + b

So the increment in d (denoted dU ) is

dU = a + b = dy - dx

Page 24: 1 CPE 333 : Computer Graphics มหาวิทยาลัยเทคโนโลยีพระจอม เกล้าธนบุรี Dr. Natasha Dejdumrong

• First, we have the point (x1, y1). So M (x1 + 1, y1 + 1/2) and

F(M) = a (x1 + 1) + b (y1 + 1/2) + c

= F (x1, y1 ) + a + b/2

Since F (x1 , y1) = 0, we have

d = d1 = dy - dx/2

Page 25: 1 CPE 333 : Computer Graphics มหาวิทยาลัยเทคโนโลยีพระจอม เกล้าธนบุรี Dr. Natasha Dejdumrong

In order to avoid a division by 2, we use 2d1 instead.

Afterward, 2d is used. So, with d used in place of 2d, we have

• First set d1 = 2dy - dx

• IF di >= 0 THEN

xi+1 = xi + 1, yi+1 = yi + 1 and di+1 = di + 2 (dy-dx)

• IF di < 0 THEN

xi+1 = xi + 1, yi+1 = yi and di+1 = di + 2dy

Page 26: 1 CPE 333 : Computer Graphics มหาวิทยาลัยเทคโนโลยีพระจอม เกล้าธนบุรี Dr. Natasha Dejdumrong

26

Midpoint Line Algorithm [Scan-convert the line between (x1, y1) and (x2, y2)]

dx = x2 - x1;

dy = y2- y1;

d = 2 * dy - dx; /* initial value of d */

dD = 2 * dy; /* increment used to move D */

dU = 2 * (dy-dx); /* increment used to move U */

x = x1;

y = y1;

Plot(x,y); /* the first pixel */

While (x < x2)

if d < 0 then

d = d + dD; / * choose D */

x = x + 1;

else

d = d + dU; /* choose U */

x = x + 1;

y = y + 1;

endif

Plot(x,y); /* the selected pixel closest to the line */

EndWhile

Midpoint Line AlgorithmMidpoint Line Algorithm

Page 27: 1 CPE 333 : Computer Graphics มหาวิทยาลัยเทคโนโลยีพระจอม เกล้าธนบุรี Dr. Natasha Dejdumrong

Since for the points, x < y, consequently the algorithm can apply.

Here dy = 11 - 8 = 3, dx = 9-5 = 4

• First d1 = 2dy - dx = 6 - 4 = 2 > 0 Select U

So the new point is (6, 9) and

d2 = d1 + 2 (dy - dx) = 2 + 2(-1) = 0 Select U

The chosen pixel is (7, 10) and

d3 = d2 + 2 (dy - dx) = 0 +2(-1) = -2 < 0 Select D

The chosen pixel is (8, 10) then

d4 = d3 + 2dy = - 1 + 6 = 5 > 0 Select U

The chosen pixel is (9, 11).

Example. Scan-convert the line between (5, 8) and (9, 11).

ExampleExample

Page 28: 1 CPE 333 : Computer Graphics มหาวิทยาลัยเทคโนโลยีพระจอม เกล้าธนบุรี Dr. Natasha Dejdumrong

28

Bresenham’s Line Algorithm can work only with the line segment with slope m from 0 to 1. (45o)

y

x

RemarksRemarks

Page 29: 1 CPE 333 : Computer Graphics มหาวิทยาลัยเทคโนโลยีพระจอม เกล้าธนบุรี Dr. Natasha Dejdumrong

29

Bresenham’s Line Algorithm can work only with the line segment with slope m from 0 to 1. (45o)

RemarksRemarks

Page 30: 1 CPE 333 : Computer Graphics มหาวิทยาลัยเทคโนโลยีพระจอม เกล้าธนบุรี Dr. Natasha Dejdumrong

30

Bresenham’s Line Algorithm can work only with the line segment with slope m from 0 to 1. (45o)

RemarksRemarks

Page 31: 1 CPE 333 : Computer Graphics มหาวิทยาลัยเทคโนโลยีพระจอม เกล้าธนบุรี Dr. Natasha Dejdumrong

31

x2+ y2 = r2

F(x,y) = x2 + y2 - r2

F(x,y) = (x - xc)2+ (y - yc)2 - r2

x = r cos(theta)

y = r sin(theta)

CircleCircle

r y

x

y

x

P x r x ( , )2 2 y

r cos

r sin

P=(r cos , r sin )

x

Page 32: 1 CPE 333 : Computer Graphics มหาวิทยาลัยเทคโนโลยีพระจอม เกล้าธนบุรี Dr. Natasha Dejdumrong

32

F(x,y) = (x - xc)2+ (y - yc)2 - r2

IF F(x,y) = 0 :

Point (x,y) is on the circle.

IF F(x,y) < 0 :

Point (x,y) is inside the circle.

IF F(x,y) > 0 :

Point (x,y) is outside the circle.

F(x,y) < 0

F(x,y) > 0

CircleCircle

Page 33: 1 CPE 333 : Computer Graphics มหาวิทยาลัยเทคโนโลยีพระจอม เกล้าธนบุรี Dr. Natasha Dejdumrong

33

Midpoint Circle AlgorithmMidpoint Circle Algorithm

-y 45

+x

Page 34: 1 CPE 333 : Computer Graphics มหาวิทยาลัยเทคโนโลยีพระจอม เกล้าธนบุรี Dr. Natasha Dejdumrong

34

P1 = (x, y) P5 = (-y, -x)

P2 = (y, x) P6 = (-y, -x)

P3 = (-y, x) P7 = (y, -x)

P4 = (-x, y) P8 = (x, - y)

(2, 8) (y, x)

(-2, 8) (-y, x)

y

(8, 2) (x, y)

x

(x, -y) (8, -2)

(y, -x) (2, -8)

(-y, -x) (-2, -8)

(-x, -y) (-8, -2)

(-8, 2) (-x, y)

9

9

Eight-Way SymmetryEight-Way Symmetry

Page 35: 1 CPE 333 : Computer Graphics มหาวิทยาลัยเทคโนโลยีพระจอม เกล้าธนบุรี Dr. Natasha Dejdumrong

35

d = F(M) = F(xi, yi) = xi2+ yi

2 - r2

IF d < 0, choose U

d > 0, choose D

d = 0, choose either U or D,

so choose U.

Midpoint AlgorithmMidpoint Algorithm

P(xi, yi) U(xi + 1, yi )

D(xi +1, yi - 1)

M

(b)

Page 36: 1 CPE 333 : Computer Graphics มหาวิทยาลัยเทคโนโลยีพระจอม เกล้าธนบุรี Dr. Natasha Dejdumrong

Let dold = F(xi+1, yi - 1/2)

= (xi + 1)2 + (yi - 1/2)2 - r2

If dold < 0, then U (xi+1, yi) is chosen and the next midpoint will be one increment over x.

Thus

dnew = F(xi+2, yi - 1/2)

= dold + 2xi + 3

The increment in d is

dU = dnew - dold = 2xi + 3

Page 37: 1 CPE 333 : Computer Graphics มหาวิทยาลัยเทคโนโลยีพระจอม เกล้าธนบุรี Dr. Natasha Dejdumrong

IF dold >= 0, M is outside the circle and D is chosen.

The new midpoint will be one increment over x and one increment down in y:

dnew = F (xi + 2, yi - 3/2)

= dold + 2xi - 2yi + 5

The increment in d is therefore

dD = dnew - dold = 2(xi - yi ) + 5

Page 38: 1 CPE 333 : Computer Graphics มหาวิทยาลัยเทคโนโลยีพระจอม เกล้าธนบุรี Dr. Natasha Dejdumrong

Initial point : (0, r).

The next midpoint lies at (1, r- 1/2) and so

F (1, r- 1/2) = 1 + (r- 1/2)2 - r2 = 5/4 - r

To avoid the fractional initialization of d,

we take h = d - 1/4.

So the initials value of h is 1-r and the comparison d < 0 becomes h < -1/4.

However, since h starts out with an integer value and is incremented with integer values ( dU and dD),

we can change the comparison to h < 0.

Page 39: 1 CPE 333 : Computer Graphics มหาวิทยาลัยเทคโนโลยีพระจอม เกล้าธนบุรี Dr. Natasha Dejdumrong

39

h = 1-r; /* initialization */

x = 0;

y = r;

Plot(x,y);

While y > x

if h < 0 then /* Select U */

dU = 2*x + 3;

h = h + dU;

x = x + 1;

else /* Select D */

dD = 2*(x-y) + 5;

h = h + dD;

x = x + 1;

y = y - 1;

endif

Plot(x,y);

EndWhile

Midpoint Circle AlgorithmMidpoint Circle Algorithm