Professional Documents
Culture Documents
L03 Output Primitives - VE
L03 Output Primitives - VE
1
OUTPUT PRIMITIVES
● Graphic SW and HW provide ● Đồ họa SW và HW cung cấp các chương
subroutines to describe a scene in terms of trình con để mô tả một cảnh theo các cấu trúc
basic geometric structures called output hình học cơ bản được gọi là nguyên thủy đầu
primitives. ra.
● Output primitives are combined to form ● Nguyên thủy đầu ra được kết hợp để tạo
complex structures thành các cấu trúc phức tạp
2
Screen coordinates
(0,0) x
POINT y
(399,299)
xmax,ymax
● To load an intensity value into the frame buffer at a position
corresponding to column x along scan line y,
(px,py)
setpixel (x, y, color)
x
● To retrieve the current frame buffer intensity setting for a specified (0,0)
location we use a low level function , xmin,ymin
color = getpixel (x, y) (799,599)
Setting a pixel in OpenGL
glBegin(GL_POINTS);
y
glVertex2f(px,py);
glEnd();
● Để tải một giá trị cường độ vào bộ đệm khung tại vị trí tương
Setting Orthogonal Camera ứng với cột x dọc theo đường quét y,
glMatrixMode(GL_PROJECTION); setpixel (x, y, color)
glLoadIdentity(); ● Để truy xuất cài đặt cường độ bộ đệm khung hiện tại cho một vị
gluOrtho2D(xmin,xmax,ymin,ymax);
trí đã chỉ định, chúng ta sử dụng chức năng mức thấp,
color = getpixel (x, y)
3
LINES
A line segment is completely defined in terms of its
two endpoints.
A line segment is thus defined as:
Line_Seg = { (x1, y1), (x2, y2) }
A line is produced by means of illuminating a set of
intermediary pixels between the two endpoints.
All line drawing algorithms make use of the fundamental
equations: Line Eqn.
Một đoạn đường được xác định hoàn toàn theo hai điểm cuối của nó.
Do đó, một phân đoạn dòng được định nghĩa là:
Line_Seg = {(x1, y1), (x2, y2)}
Một dòng được tạo ra bằng cách chiếu sáng một tập hợp các pixel trung
gian giữa hai điểm cuối.
Tất cả các thuật toán vẽ đường đều sử dụng các phương trình cơ bản:
Đường Eqn.
4
Parametric
6
P1 = [x1 y1] = 2,2
P2 = 6,6 = [x2 y2 ]
7
WHY SCAN CONVERSION?
RASTERIZATION: Process of determining
which pixels provide the best
approximation to a desired line on the
screen.
SCAN CONVERSION: Combination of
rasterization and generating the picture in
RASTERIZATION: Quá trình xác định pixel nào cung
scan line order. cấp xấp xỉ tốt nhất cho một dòng mong muốn trên màn
hình.
Graphics System convert each primitive
from its geometric definition into a set of CHUYỂN ĐỔI SCAN: Kết hợp rasterization và tạo hình
ảnh theo thứ tự dòng quét.
Pixels that make up the primitive in the
Image Space. Hệ thống đồ họa chuyển đổi từng nguyên thủy từ định
nghĩa hình học của nó thành một tập hợp các pixel tạo
nên nguyên thủy trong Không gian hình ảnh.
8
CONSTRAINTS OF LINE SCAN CONVERSION
● Converting output primitives into frame ● Chuyển đổi nguyên thủy đầu ra thành cập nhật
buffer updates. bộ đệm khung.
Choose which pixels contain which intensity Chọn pixel nào chứa giá trị cường độ nào.
value.
● Những ràng buộc
● Constraints
- Đường thẳng sẽ xuất hiện dưới dạng đường thẳng
– Straight lines should appear as a straight
line - Nguyên thủy nên bắt đầu và kết thúc chính xác
– Primitives should start and end accurately - Người nguyên thủy nên có độ sáng nhất quán dọc
theo chiều dài của chúng
– Primitives should have a consistent brightness
along their length - Chúng nên được rút ra nhanh chóng
– They should be drawn rapidly
9
LINE DRAWING ALGORITHMS
10
DIRECT SCAN CONVERSION
11
DDA
A line algorithm Based on calculating either Δy
or Δx using the above equations.
If m ≤ 1 then take Δx = 1
Compute successive y by
yk+1 = yk + m (1)
14
MERITS + DEMERITS
▪Faster than the direct use of line Eqn.
▪It eliminates the multiplication in line Eqn.
▪For long line segments, the true line Path may be mislead due to round off.
▪Rounding operations and floating-point arithmetic are still time consuming.
▪The algorithm can still be improved.
▪Other algorithms, with better performance also exist.
Jack Bresenham worked for 27 years Jack Bresenham đã làm việc 27 năm tại IBM
at IBM before entering academia. trước khi vào học viện.
Bresenham đã phát triển các thuật toán nổi
Bresenham developed his famous tiếng của mình tại IBM vào đầu những năm
algorithms at IBM in the early 1960s 1960
▪The Bresenham algorithm is another Thuật toán Bresenham là một thuật toán
incremental scan conversion algorithm chuyển đổi quét gia tăng khác
▪The big advantage of this algorithm Ưu điểm lớn của thuật toán này là nó chỉ sử
dụng các phép tính số nguyên
is that it uses only integer calculations
16
BRESENHAM’S LINE
For a positive slope, 0 < m < 1 and line is
starting from left to right.
17
DERIVING THE BRESENHAM LINE ALGORITHM
y = m( xk + 1) + b
Then
d1 = y − yk
= m( xk + 1) + b − yk
And d 2 = ( yk + 1) − y
= yk + 1 − m( xk + 1) − b
d1 − d 2 = 2m( xk + 1) − 2 yk + 2b − 1
18
DERIVING THE BRESENHAM LINE ALGORITHM Constant=2Δy + Δx(2b-1) Which is
independent of pixel position
20
WHAT’S THE ADVANTAGE?
Answer: involves only the Trả lời: chỉ liên quan đến việc tính
calculation of constants Δx, Δy, các hằng số Δx, y, 2Δy và 2Δy- 2Δx
2Δy and 2Δy- 2Δx once and
integer addition and subtraction in một lần và phép cộng và trừ số
each steps nguyên trong mỗi bước
Hence the arithmetic involves Do đó, số học chỉ liên quan đến
only integer addition and
subtraction of these two phép cộng và phép trừ số nguyên
constants. của hai hằng số này.
21
BRESENHAM LINE ALGORITHM SUMMARY
The Bresenham line algorithm has the Thuật toán vẽ đường Bresenham có
following advantages: những ưu điểm sau:
An fast incremental algorithm
Uses only integer calculations Một thuật toán gia tăng nhanh
Chỉ sử dụng tính toán số nguyên
Comparing this to the DDA
algorithm, DDA has the following So sánh điều này với thuật toán DDA,
problems: DDA có các vấn đề sau:
Accumulation of round-off errors can
make the pixelated line drift away from Tích lũy các lỗi làm tròn có thể làm cho dòng
what was intended pixel bị trôi đi khỏi những gì đã được dự định
The rounding operations and floating Các hoạt động làm tròn và số học dấu phẩy
point arithmetic involved are time
consuming động có liên quan đều tốn thời gian
22
OPENGL OUTPUT FUNCTIONS
glBegin(GL_LINES); // Specify what to draw,
// here lines
// Geometric info via vertices:
glVertex*(); // 1
glVertex*(); // 2
... // ...
glEnd;
glVertex[234][isfd]
[234]: 2D, 3D, 4D
[isfd]: integer, short, float, double
For instance: glVertex2i(100, 25);
23
CIRCLE
24
SCAN CONVERTING CIRCLES
Explicit: y = f(x) y = R2 − x2
Usually, we draw a quarter circle by incrementing x from 0 to R in unit steps and
solving for +y for each step.
Parametric: x = R cos - by stepping the angle from 0 to 90
- avoids large gaps but still insufficient.
y = R sin
Implicit: f(x) = x2+y2-R2 Thông thường, chúng ta vẽ một vòng tròn bằng cách tăng
x từ 0 đến R theo các bước đơn vị và giải + y cho mỗi
If f(x,y) = 0 then it is on the circle.
bước.
f(x,y) > 0 then it is outside the circle.
f(x,y) < 0 then it is inside the circle.
25
CIRCLE GENERATING ALGORITHMS
Problem 1 can be overcome by considering the symmetry of
circles as in Figure.
But it still requires a good deal of computation time.
Vấn đề 1 có thể được khắc phục bằng cách xem xét tính đối xứng của
các vòng tròn như trong Hình.
Nhưng nó vẫn đòi hỏi nhiều thời gian tính toán.
Efficient Solutions
Midpoint Circle Algorithm
26
BRESENHAM’S ALGORITHM COULD BE ADAPTED ??
Yes
How ?
Setting decision parameter for finding the closest pixel to
the circumference
what to do For Non-linear equation of circle ?
The square (multiply) operations
The square root operation – try really hard to avoid
these!
We need a more efficient, more accurate solution
27
MIDPOINT CIRCLE ALGORITHM
Similarly to the case with lines, there is an incremental
algorithm for drawing circles – the mid-point circle
algorithm
Circle function defined as:
f circle( x, y) = x + y − r
2 2 2
2
If di < 0 we choose pixel A otherwise we choose
pixel B
Note: we currently assume the circle is centered at the origin
Again, as with the line algorithm, the choice of A or B can be
used to determine the new value of di+1
29
MIDPOINT CIRCLE ALGORITHM
If B chosen then next midpoint has the Otherwise if A is chosen then the next
following decision variable: decision variable is given by:
2 2
3 3 1 1
( )2
xi + 2, yi − di +1 = xi + 2 + yi − − r
2
i
x + 2, yi − d i +1 = ( xi + 2 )2
+ yi − − r 2
2 2 2 2
= di + 2 xi − 2 yi + 5 = di + 2 xi + 3 30
MIDPOINT CIRCLE ALGORITHM
If we assume that the radius is an integral value, then the first pixel drawn is (0, r) and the
initial value for the decision variable is given by:
1 2 1 2
1, r − d 0 = 1 + r − r + − r
2 4
5
= −r
4
Although the initial value is fractional, we note that all other values are integers.
we can round down:
d0 = 1 − r
31
MIDPOINT CIRCLE ALGORITHM
d = 1-r
x = 0 initialisation
y = r stop at diagonal end of octant
while y > x
if d < 0 then
d = d+2*x+3
x = x+1 choose A
else
d = d+2*(x-y)+5
x = x+1 choose B
y = y-1
endif
SetPixel(cx+x,cy+y)
endwhile
Như trong thuật toán vẽ đường Bresenham, phương pháp trung điểm tính toán các
vị trí pixel dọc theo chu vi của vòng tròn bằng cách sử dụng phép cộng và phép trừ
số nguyên, giả sử rằng các tham số vòng tròn được chỉ định trong tọa độ màn hình.
33
ELLIPSE
34
ELLIPSE-GENERATING ALGORITHMS
rx
( x − x1 )2 + ( y − y1 )2 + ( x − x2 )2 + ( y − y2 )2 = constant
Cartesian coordinates:
2 2
x − xc y − yc
+ = 1
rx ry
Polar coordinates: x = xc + rx cos
y = yc + ry sin
36
ELLIPSE ALGORITHMS
Symmetry between quadrants
(-x, y) (x, y)
Not symmetric between the two octants of a quadrant
ry
Thus, we must calculate pixel positions along the elliptical arc
through one quadrant and then we obtain positions in the rx
remaining 3 quadrants by symmetry
(-x, -y) (x, -y)
Đối xứng giữa các góc phần tư
Không đối xứng giữa hai quãng tám của góc phần tư
Do đó, chúng ta phải tính toán các vị trí pixel dọc theo cung
elip qua một góc phần tư và sau đó chúng ta có được các vị trí
trong 3 góc phần tư còn lại bằng cách đối xứng
37
Slope = -1
ELLIPSE ALGORITHMS ry
1
2
rx
Decision parameter:
dy 2ry2 x
fellipse ( x, y) = r x + r y − r r
2
y
2
x
2 2 2 2
x y
Slope = =− 2
dx 2rx y
38
ELLIPSE ALGORITHMS
Starting at (0, ry) we take unit steps in the x direction until we
reach the boundary between region 1 and region 2. Then we
take unit steps in the y direction over the remainder of the curve
in the first quadrant.
Slope = -1
At the boundary 1
ry 2
dy
= −1 2r x = 2r y
y
2
x
2
dx rx
2ry2 x 2rx2 y
39
FILLED AREA PRIMITIVES
40
FILLED AREA PRIMITIVES
A standard output primitive in general graphics packages is a solid-
color or patterned polygon area.
There are two basic approaches to area filling on raster systems:
42
SCAN-LINE FILL
ALGORITHM
For each scan line crossing a
polygon, the area-fill algorithm
locates the intersection points of
the scan line with the polygon
edges.
These intersection points are then
sorted from left to right, and the
corresponding frame-buffer
positions between each
intersection pair are set to the
specified fill color.
43
ADVANTAGES & DISADVANTAGES
• Calculations performed in scan-conversion
take advantage of various coherence Problems
properties
• Coherence property is can be used to reduce • What happens if a vertex is shared by more
processing. than one polygon, e.g. three triangles?
• Coherence methods often involve incremental • What happens if the polygon intersects itself?
calculations applied along a single scan line
or between successive scan lines. • What happens for a “sliver”?
Các vấn đề
• Các tính toán được thực hiện trong chuyển đổi quét
tận dụng các thuộc tính kết hợp khác nhau • Điều gì xảy ra nếu một đỉnh được chia sẻ bởi
• Thuộc tính kết hợp có thể được sử dụng để giảm xử nhiều hơn một đa giác, ví dụ: ba tam giác?
lý.
• Các phương pháp kết hợp thường liên quan đến các • Điều gì xảy ra nếu đa giác giao nhau?
phép tính gia tăng được áp dụng dọc theo một đường
quét đơn hoặc giữa các đường quét liên tiếp. • Điều gì xảy ra với một sliver?
44
Start point
boundaryFill4 (x + 1, y, fillColor, borderColor); ● Nó rất hữu ích trong các gói vẽ tương tác, trong đó các
boundaryFill4 (x - 1, y, fillColor, borderColor);
điểm bên trong dễ dàng được chọn.
boundaryFill4 (x, y + 1, fillColor, borderColor);
boundaryFill4 (x, y - 1, fillColor, borderColor); Giả định: màu biên là một màu duy nhất
}
} ● (x, y): điểm bắt đầu; b: màu ranh giới, tô màu: tô màu
46
BOUNDARY FILL ALGORITHM
▪Starting from (x, y), the algorithm tests
neighboring pixels to determine whether they are
of the boundary color.
▪ If not, they are painted with the fill color, and
their neighbors are tested. This process continues
until all pixels up to the boundary have been
tested.
▪ There are two methods for proceeding to
neighboring pixels from the current test position:
▪Bắt đầu từ (x, y), thuật toán kiểm tra các pixel lân
cận để xác định xem chúng có phải là màu biên
hay không.
▪ Nếu không, chúng được sơn bằng màu tô và
hàng xóm của chúng được kiểm tra. Quá trình này
tiếp tục cho đến khi tất cả các pixel cho đến ranh
giới đã được kiểm tra.
▪ Có hai phương pháp để tiến tới các pixel lân cận
từ vị trí thử nghiệm hiện tại: 47
4-CONNECTED VS. 8-CONNECTED
1. The 4-connected method.
2. The 8-connected method.
4 điểm lân cận so với 8 điểm lân cận: phụ thuộc vào định
nghĩa về tính liên tục. 8 lân cận: ranh giới đường chéo sẽ
không dừng lại
● Đệ quy, quá chậm. Đối với các vùng lớn có hàng triệu
pixel, hàng triệu lệnh gọi hàm.
48
PIXEL SPAN METHOD
● Stack based improvement: keep neighbors in stack
● Number of elements in the stack can be reduced by
filling the area as pixel spans and pushing only the
pixels with pixel transitions.
● Cải thiện dựa trên ngăn xếp: giữ hàng xóm trong ngăn
xếp
● Có thể giảm số lượng phần tử trong ngăn xếp bằng
cách lấp đầy khu vực dưới dạng các pixel và chỉ đẩy các
pixel bằng các pixel chuyển tiếp.
49
FLOOD FILL ALGORITHM
• Similar to boundary, Flood fill algorithm is used
for filling the interior of a polygon .
• Used when an area defined with multiple color
boundaries
• Start at a point inside a region− Replace a
specified interior color (old color) with fill color
• Flood fill algorithm is simplest algorithm.
• Flood fill algorithm is slow, For large polygon flood
fill algorithm is fail because it requires a large
frame.
• Tương tự như đường biên, thuật toán lấp lũ được
sử dụng để lấp đầy phần bên trong của đa giác.
• Được sử dụng khi một khu vực được xác định có
nhiều ranh giới màu
• Bắt đầu tại một điểm trong khu vực - Thay thế
màu nội thất đã chỉ định (màu cũ) bằng màu tô
Thuật toán lấp đầy là thuật toán đơn giản nhất.
• Thuật toán lấp đầy lũ chậm, Đối với thuật toán lấp
lũ đa giác lớn bị lỗi vì nó yêu cầu một khung lớn. 50
Similar to boundary fill. Algorithm continues while
the neighbor pixels have the same color
void floodFill4 (int x, int y, int fillColor, int interiorColor)
{int color;
/* set current color to fillColor, then perform following
operations. */
getPixel (x, y, color);
if (color = interiorColor)
{
setPixel (x, y); // set color of pixel to fillColor
floodFill4 (x + 1, y, fillColor, interiorColor);
floodFill4 (x - 1, y, fillColor, interiorColor);
floodFill4 (x, y + 1, fillColor, interiorColor);
floodFill4 (x, y - 1, fillColor, interiorColor);
}}
51
CHARACTER
GENERATION
52
CHARACTER
GENERATION
•Not so Very important output primitive
• Many pictures require text
Two different representation are used for storing
computer fonts:
1. Bitmap font (or bitmapped font)
2. Outline font
Bold Italic 55
OUTLINE FONT
(STROKED)
Graphic primitives such as lines and arcs are used to
define the outline of each character.
Require less storage since variation does not require a
distinct font cash.
Each character represented (stored) as a series of line
segments
– sometimes as more complex primitives
Parameters needed to draw each stroke
– endpoint coordinates for line segments
56
SUMMARY
▪Rasterization
▪Point
▪Line
▪Circle
▪Filled Area Primitives
▪Character
57
Tài liệu phục vụ
học tập sinh viên
CNTT&TT ĐHBK -
HN
Tài liệu sử dụng Hình ảnh , Text , Video trên mạng theo
nguyên tắc FU – CC và tuân thủ theo quy tắc đó
Sinh viên, người học hoàn toàn có quyền chỉnh sửa,
copy và tái suất bản theo CC & FU
58