Bài giảng môn Đồ họa và hiện thực ảo - Bài 2: Các giải thuật sinh các thực thể cơ sở

7 8 0
Bài giảng môn Đồ họa và hiện thực ảo - Bài 2: Các giải thuật sinh các thực thể cơ sở

Đang tải... (xem toàn văn)

Thông tin tài liệu

z 2b is the length of the minor axis along the y axis.. z The midpoint can also be applied to ellipses.[r]

(1)

Các gii thut sinh thc th

cơs

Le Tan Hung hunglt@it-hut.edu.vn 0913030731

Rendering Pipeline: 3-D Transform

Illuminate Transform

Clip Project Rasterize

Model & Camera Model & Camera Parameters

Parameters Rendering PipelineRendering Pipeline FramebufferFramebuffer DisplayDisplay

The Rendering Pipeline: 3-D Scene graph

Object geometry

Lighting Calculations

Clipping

•Các Các đđiiểm hệ thống tọa ểm hệ thống tọa đđộ 3D giới thực ộ 3D giới thực •

•Các Các đđiiểểm bóng theo mơ hình chim bóng theo mơ hình chiếếu sángu sáng

•Các Các đđiiểm mơ hình hệ tọa ểm mơ hình hệ tọa đđộ Camera hay tọa ộ Camera hay tọa đđộộ đđiiểm nhìnểm nhìn •

•Các tCác tọọa a đđộộđđiiểểm cm củủa vùng hình chóp ca vùng hình chóp cụụt vt vớới i đđiiểểm nhìn xác m nhìn xác đđịịnhnh •

•ĐĐiiểm 2ểm DDtheo tọa theo tọa đđộ hình sau phép chiếu ộ hình sau phép chiếu đđượược xén tỉac xén tỉa Modeling

Transforms

Viewing Transform

Projection Transform

Phép biến đổi Transformations

zscreen space-khơng gian hình

zmodel space Khơng gian mơ hình

(a.k.a object space or world space)

z3 loại phép biến đổi: – Modeling transforms

Viewing transforms

Projection transforms

Rendering: Transformations

zModeling transforms

– Size, place, scale, and rotate objects parts of the model w.r.t each other

– Object coordinates Ỉworld coordinates

Z X Y

X Z Y

Rendering: Transformations

zViewing transform

– Rotate & translate the world to lie directly in front of the camera

zTypically place camera at origin zTypically looking down -Z axis

(2)

Rendering: Transformations

zProjection transform

– Apply perspective foreshortening

zDistant = small: the pinhole cameramodel – View coordinates Ỉscreen coordinates

Rendering: Transformations zAll these transformations involve shifting

coordinate systems (i.e., basis sets)

zOh yeah, that’s what matrices do…

zRepresent coordinates as vectors, transforms

as matrices

zMultiply matrices = concatenate transforms!

⎥ ⎦ ⎤ ⎢ ⎣ ⎡ ⎥ ⎦ ⎤ ⎢

⎡ −

= ⎥ ⎦ ⎤ ⎢ ⎣ ⎡

′ ′

Y X Y

X

θ θ

θ θ

cos sin

sin cos

Rendering: Transformations zHomogeneous coordinates:represent

coordinates in dimensions with a 4-vector – Denoted [x, y, z, w]T

zNote that w = 1in model coordinates

– To get 3-D coordinates, divide by w: [x’, y’, z’]T= [x/w, y/w, z/w]T

zTransformations are 4x4 matrices

zWhy? To handle translation and projection

The Rendering Pipeline: 3-D

Modeling Transforms

Scene graph Object geometry

Lighting Calculations

Viewing Transform

Clipping Projection Transform

Result:

Result: •

•All vertices of scene in shared 3All vertices of scene in shared D “world” coordinate systemD “world” coordinate system •

•Vertices shaded according to lighting modelVertices shaded according to lighting model

•Scene vertices in 3Scene vertices in D “view” or “camera” coordinate systemD “view” or “camera” coordinate system •

•Exactly those vertices & portions of polygons in view frustumExactly those vertices & portions of polygons in view frustum

•22 D screen coordinates of clipped verticesD screen coordinates of clipped vertices

Rendering: Ánh sáng - Lighting zIlluminatinga scene: coloring pixels according to

some approximation of lighting

Global illumination: solves for lighting of the whole scene at once

Local illumination: local approximation, typically lighting each polygon separately

zInteractive graphics (e.g., hardware) does only

local illumination at run time

The Rendering Pipeline: 3-D

Modeling Transforms

Scene graph Object geometry

Lighting Calculations

Viewing Transform

Clipping Projection Transform

Result:

Result: •

•All vertices of scene in shared 3All vertices of scene in shared D “world” coordinate D “world” coordinate system

system •

•Vertices shaded according to lighting modelVertices shaded according to lighting model •

•Scene vertices in 3Scene vertices in D “view” or “camera” coordinate D “view” or “camera” coordinate system

system •

•Exactly those vertices & portions of polygons in view Exactly those vertices & portions of polygons in view frustum

frustum •

(3)

Rendering: Clipping

zClippinga 3-D primitive returns its intersection with the view frustum:

Rendering: Xén ta - Clipping

zClipping is tricky!

– We will have a whole assignment on clipping

In: vertices

In: vertices

Out: vertices

Out: vertices

Clip

Clip In: polygonIn: polygon Out: polygons

Out: polygons

The Rendering Pipeline: 3-D Transform

Illuminate Transform

Clip Project Rasterize

Model & Camera Model & Camera Parameters

Parameters Rendering PipelineRendering Pipeline FramebufferFramebuffer DisplayDisplay

Modeling: The Basics

zCommon interactive 3-D primitives: points,

lines, polygons (i.e., triangles)

zOrganized into objects

– Collection of primitives, other objects – Associated matrix for transformations

zInstancing: using same geometry for multiple objects

– 4 wheels on a car, arms on a robot

Modeling: The Scene Graph

zĐồ thị cảnhscene graph: đồ thị lưu trữđối

tượng, quan hệ giũa đối tượng phép biến đổi đối tượng đó

zNút đối tượng;

zCành thực thể biến đổi – Tương ứng ma trận Robot

Body Head

Arm Trunk Leg Eye

Mouth

Modeling: The Scene Graph

zTraversethe scene graph in depth-first order, concatenating transformations

zMaintain a matrix stackof transformations

Arm Trunk Leg Eye Mouth

Head Body

Robot

Foot

Matrix Matrix Stack Stack Visited

(4)

Modeling: The Camera

zFinally: need a model of the virtual camera

– Can be verysophisticated

zField of view, depth of field, distortion, chromatic aberration… – Interactive graphics (OpenGL):

zCamera pose: position & orientation

–Captured in viewing transform (i.e., modelview matrix)

zPinhole camera model

–Field of view

–Aspect ratio

–Near & far clipping planes

Modeling: The Camera

zCamera parameters (FOV, etc) are encapsulated

in a projection matrix

– Homogeneous coordinates Æ4x4 matrix! – See OpenGL Appendix F for the matrix

zThe projection matrix premultiplies the viewing

matrix, which premultiplies the modeling matrices – Actually, OpenGL lumps viewing and modeling

transforms into modelview matrix

Ri rc hoá đimnh

(Scan Conversion rasterization)

z Là tiến trình sinh cácđối tượng hình học cơsởbằng phương pháp xấp xỉdựa lưới phân giải của màn hình

z Tính chất cácđối tượng cầnđảm bảo : – smooth

– continuous

– pass through specified points – uniform brightness – efficient

Biu dinđon thng z Biểu diễn tường minh (y-y1)/( x-x1) = ( y2-y1)/( x2-x1)1 y = kx + m

– k = (y2-y1)/( x2-x1) – m = y1- kx1 – Δy = k Δx

z Biểu diễn không tường minh

(y2-y1)x - (x2-x1)y + x2y1 - x1y2 = 0 hay rx + sy + t =

– s = -(x2-x1 ) – r = (y2-y1) t = x2y1 - x1y2

z Biểu diễn tham biến

P(u) = P1 + u(P2 - P1) u [0,1] X = x1 + u( x2 - x1 ) Y = y1 + u( y2 - y1 )

m

P(x1, y1)

P(x2 ,y2)

u

Sinhđường tròn Scan Converting Circles

z Implicit: f(x) = x2+y2-R2

z Explicit: y = f(x)

z Parametric: 2 y= ± Rx

cos sin

x R y R

θ θ

= =

If f(x,y) = then it is on the circle f(x,y) > then it is outside the circle f(x,y) < then it is inside the circle Usually, we draw a quarter circle by incrementing x from to R in unit steps and solving for +y for each step

- by stepping the angle from to 90 - avoids large gaps but still insufficient

Thut toán DDA

(Digital Differential Analizer)

Giải thuật DDA z Với < k <

xi+1= xi+

yi+1 = yi+ k

với i=1,2,3

Thuậttoán ddaline (x1, y1, x2, y2) x1, y1, x2, y2 : tọađộ2 điểmđầu

cuối k : hệsốgóc x,y,m :biến begin

m =(x2-x1)/(y2-y1); x = x1; y = y1; k = 1/m; putpixel(x,y); while x<x2

begin x = x+1; y = y+k;

putpixel(round(x),round(y)); end; end;

Giải thuật thông thường

DrawLine(int x1,int y1, int x2,int y2, int color)

{ float y; int x;

for (x=x1; x<=x2; x++) {

(5)

Gii thut Bresenham

z 1960 Bresenham thuộc IBM

z điểm gần với đường thẳng dựa trênđộphân giai hưu hạn

z loại bỏđược các phép toán chia vàphép toán làm tròn nhưtađã thấy gỉai thuật DDA

z Xétđoạn thẳng với < k < 1

0

0

2 d2

d1

Gii thut Bresenham

d2= y - yi = k(xi +1) + b - yi d1= yi+1 y = yi + k(xi + 1)

-b

z If d1≤d2=>yi+1= yi+ 1 else d1 > d2=> yi+1= yi

z D = d1- d2

= -2k(xi + 1) + 2yi - 2b + 1 z Pi = ΔxD = Δx (d1- d2) d1

d2

xi xi+1

yi

yi+1

Pi = -2Δyxi + 2Δxyi + c Pi+1- Pi

= -2Δy(xi+1- xi) + 2Δx(yi+1- yi)

z Nếu Pi ≤0 ⇒yi+1= yi +

Pi+1= Pi - 2Δy + 2Δx

z Nếu Pi > ⇒yi+1= yi

Pi+1= Pi - 2Δy

P1= Δx(d1- d2)

P1= -2Δy + Δx

Gii thut Bresenham

yi+1

M ( xi , yi)

xi xi+1 Gii thut trungđim-Midpoint z Jack Bresenham 1965 / Pitteway 1967

z VanAkenáp dụng cho việc sinh cácđường thẳng vàđường tròn 1985

z Các công thứcđơn giản hơn, tạođược

điểm tương tựnhưvới Bresenham z d = F (xi + 1, yi + 1/2) làtrungđiểm củađoạn

AB

z Việc so sánh, hay kiểm tra M sẽđược thay việc xét giátrịd

– Nếu d > điểm B chọn, yi+1= yi

– d < điểm A chọn ⇒yi+1= yi+

1

– Trong trường hợp d = cóthể

chọnđiểm bất kỳhoặc A, B

A M B

Bresenham’s Algorithm: Midpoint Algorithm

z Sdng phương pháp biu din không tường minh

z Ti mi trungđim cađon thng giá trị được tính là:

zChúng ta gọidilà biến quyếtđịnh của bước thứi 0

= + +by c ax

( )

( )

( i i) i

i

i i i

i

i i i

i

y x c by ax

y x c by ax

y x c by ax

, 0

, 0

, 0

⇒ > + +

⇒ < + +

⇒ = +

+ on line

above line below line

(x ) b y c

a

di i i ⎟+

⎠ ⎞ ⎜ ⎝ ⎛ + + + =

2 1 1

Bresenham’s Algorithm: Midpoint Algorithm

z If di> 0then chọnđiểmA⇒trungđiểm sẽcó dạng:

( )

b a d

c y b x a d y

x

i

i i i i

i

+ + =

+ ⎟ ⎠ ⎞ ⎜ ⎝ ⎛ + + + = ⇒ ⎟ ⎠ ⎞ ⎜

⎛ + +

+

2 3 2 2

3 ,

(6)

Bresenham’s Algorithm: Midpoint Algorithm

z if di< 0then chọnđiểmBvà trungđiểm

z Ta có:

z Ðiểmđầu

( ) [ ] 2 1 , b a c by ax c y b x a d y x start start start start start start start + + + + = + ⎟ ⎠ ⎞ ⎜ ⎝ ⎛ + + + = ⇒ ⎟ ⎠ ⎞ ⎜ ⎝ ⎛ + + ( ) a d c y b x a d y x i i i i i i + = + ⎟ ⎠ ⎞ ⎜ ⎝ ⎛ + + + = ⇒ ⎟ ⎠ ⎞ ⎜ ⎝ ⎛ + + + 2 1 2 2 1 , 2 C x x y y x C c x x x b y y y a start end start end + Δ Δ = ⎪ ⎭ ⎪ ⎬ ⎫ Δ = − = Δ − = − = Δ = where 0+a+b

=

Midpoint Line Algorithm

dx = x_end-x_start dy = y_end-y_start d = 2*dy-dx x = x_start y = y_start while x < x_end

if d <= 0then d = d+(2*dy) x = x+1 else

d = d+2*(dy-dx) x = x+1 y = y+1 endif SetPixel(x,y) endwhile initialisation chooseB chooseA

Gii thut

Bresenham's Midpoint

z d = a(xi + 1) + b(yi + 1/2) + c z Nếuđiểmđược chọn làB thi M sẽtang

theo x mộtđơn vị – di +1 = F(xi +2, yi + 1/2)

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

z Nếuđiểm A chọn thi` M tăng theo hướng x vày với mộtđơn vị

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

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

¾ Với a+ b= dy-dx

d <= 0 Bắt đầu

x = x1 ; y = y1; dx = x2 - x1; dy = y2 - y1; d = dy - dx/2;

Putpixel (x ,y);

x < x2

KÕt thóc d = d + dy

d = d + dy - dx

y = y + 1

yes

no No

yes

x = x + 1

Midpoint Circle Algorithm

z Sửdụng phương pháp biểu diễn không tường minh giải thuật z Thực giải thuật 1/8

đường tròn lấyđối xứng xho góc cịn lại

z Với dilà giá trịcủađường trịn mộtđiểm bất kỳta có

( − ) (2+ − )2− 2=0

r y y x

x c c

( ) ( )

( , )isoutsidecircle if circle on is , if circle inside is , if ⎪ ⎩ ⎪ ⎨ ⎧ > = < = i i i i i i i y x y x y x d

Midpoint Circle Algorithm

z As with the line, we determine the value of the decision variable by substituting the mid-point of the next pixel into the implicit form of the circle:

z If di< we choose pixel Aotherwise we choose pixel B

– Note: we currently assume the circle is centered at the origin

( )

2

2 1

1 y r

x

di i i ⎟ −

⎠ ⎞ ⎜ ⎝ ⎛ − + + =

Midpoint Circle Algorithm

z Again, as with the line algorithm, the choice of A or B can be used to determine the new value of di+1

z If Achosen then next midpoint has the following decision variable:

(7)

Midpoint Circle Algorithm

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

z Although the initial value is fractional, we note that all other values are integers

⇒ we can round down:

r

r r r d r

− =

− ⎟ ⎠ ⎞ ⎜ ⎝

⎛ − +

+ = ⇒ ⎟ ⎠ ⎞ ⎜ ⎝

⎛ −

4 5

4 1 1

2 1 ,

1 2

0

r d0=1−

Midpoint Circle Algorithm

d = 1-r x = 0 y = r while y < x

if d < 0then d = d+2*x+3 x = x+1 else

d = d+2*(x-y)+5 x = x+1 y = y-1 endif

SetPixel(cx+x,cy+y)

endwhile

initialisation

chooseB chooseA

Translate to the circle center

stop at diagonal ⇒end of octant

Scan Converting Ellipses

z 2a is the length of the major axis along the x axis. z 2b is the length of the minor axis along the y axis. z The midpoint can also be applied to ellipses. z For simplicity, we draw only the arc of the ellipse that

lies in the first quadrant, the other three quadrants can be drawn by symmetry

2 2 2

( , ) 0

F x y =b x +a ya b =

Scan Converting Ellipses: Algorithm

z Firstly we divide the quadrant into two regions z Boundary between the two regions is

– the point at which the curve has a slope of -1

– the point at which the gradient vector has the i and j components of equal magnitude

2

( , ) / / 2 2

grad F x y=∂ ∂ +∂ ∂ =F xi F yj b xi+ a yj

A

M tiep tuyen = -1

B gradient

B C M

i

Ellipses: Algorithm (cont.)

z At the next midpoint, if a2(y

p-0.5)<=b2(xp+1), we switch region 1=>2 z In region 1, choices are E and SE

– Initial condition: dinit= b2+a2(-b+0.25)

– For a move to E, dnew= dold+DeltaEwith DeltaE= b2(2xp+3)

– For a move to SE, dnew= dold+DeltaSEwith

DeltaSE= b2(2xp+3)+a2(-2yp+2)

z In region 2, choices are S and SE

– Initial condition: dinit= b2(xp+0.5)2+a2((y-1)2-b2)

– For a move to S, dnew= dold+Deltaswith Deltas= a2(-2yp+3)

– For a move to SE, dnew= dold+DeltaSEwith DeltaSE= b2(2xp+2)+a2(-2yp+3)

z Stop in region when the y value is zero

Ký tBitmap

z Trên cơsỏđịnh nghĩa mỗi ký tự với một font chưcho trước một bitmap chữnhật nhỏ

z Font/typeface: set of character shapes

z fontcache

– ký tựtheo chuỗi liên tiếp bộnhớ

z Dạng cơbản

– (thường N, nghiêng I, đậm B, nghiêngđậm B+I) z Thuộc tính

– Also colour, size, spacing and orientation

Ngày đăng: 09/03/2021, 05:34

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan