1
Computer Graphics
Marco Tarini Università dell’Insubria
Facoltà di Scienze MFN di Varese Corso di Laurea in Informatica Anno Accademico 2012/13
Lezione 6:
Lezione 6:
Lezione 6:
Lezione 6: attributi per vertice
asdad asdsad fadfasdf asdf asdf asd asdfasdf asd fasdf asdasdf asd asdf asd asdf asd asdf asd asdad asdsad fadf asdf asdf asdf asd asdfasdf asd fasdf asdasdf asd asdf asd asdf asd asdf asd
asdad asdsad fadfasdf asdf asdf asd asdfasdf asd fasdf asdasdf asd asdf asd asdf asd asdf asd
Un pre-preambolo
• Ho uno spazio vettoriale A con elementi a,b,c … – Traduz: ho delle entità a,b,c… su cui ho definito:
• prodotto, con numeri reali (ottengo un elemento di A)
• somma, fra 2 elementi di A (ottengo un elemento di A)
• somma e prod hanno tutte le prop. matematiche che mi aspetto
• Allora se faccio: dico:
d = k
0a + k
1b + k
2c “d è una combinazone lineare combinazone lineare combinazone lineare combinazone lineare di a,b,c (con pesi pesi pesi pesi k
0k
1k
2)”
se k
0+ k
1+ k
2= 1 “d è una interpolazione interpolazione interpolazione interpolazione di e k
0, k
1, k
2>= 0 a,b,c (con pesi pesi pesi pesi k
0k
1k
2)”
se k
0+ k
1+ k
2= 1 “d è una estrapolazione estrapolazione estrapolazione estrapolazione di ma almeno uno <0 a,b,c (con pesi pesi pesi pesi k
0k
1k
2)”
M a r c o T a r i n i ‧ C o m p u t e r G r a p h i c s ‧ 2 0 1 2 / 1 3 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a
M a r c o T a r i n i ‧ C o m p u t e r G r a p h i c s ‧ 2 0 1 2 / 1 3 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a
Preambolo:
• Le coordinate baricentriche
M a r c o T a r i n i ‧ C o m p u t e r G r a p h i c s ‧ 2 0 1 2 / 1 3 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a
Cosa è un segmento?
V 1
V 2
con v 1 , v 2 ∈ R 2 o R 3 o R 4 ...
M a r c o T a r i n i ‧ C o m p u t e r G r a p h i c s ‧ 2 0 1 2 / 1 3 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a
Cosa è un segmento?
V 1
V 2
posso definirlo così:
un segmento avente estremi v
1e v
2è l'insieme di tutti i punti x esprimibili come
x = a v 1 + b v 2
a e b scalari positivi con a + b = 1
0.5 v
1+ 0.5 v
20.0 v
1+ 1 v
20.75 v
1+ 0.25 v
21 v
1+ 0 v
20.1 v
1+ 0.9 v
20.66 v
1+ 0.33 v
2≪ x è una
interpolazione interpolazione interpolazione interpolazione
di v1 e v2 ≫
M a r c o T a r i n i ‧ C o m p u t e r G r a p h i c s ‧ 2 0 1 2 / 1 3 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a
Parentesi notazione
V 1 V 2 x = a v 1 + b v 2 a e b scalari non negativi con a + b = 1
x interpolazione:
interpolazione:
interpolazione:
interpolazione:
V 1 V 2 x = a v 1 + b v 2
a e b scalari non negativi con a + b = 1
x estrapolazione:
estrapolazione:
estrapolazione:
estrapolazione:
(quindi 0 ≤ a ≤ 1 e 0 ≤ b ≤ 1 )
2
M a r c o T a r i n i ‧ C o m p u t e r G r a p h i c s ‧ 2 0 1 2 / 1 3 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a
Cosa è un triangolo?
V 1
V 2
con v 1 , v 2 , v 3 ∈ R 2 o R 3 o R 4 ...
V 3
≪ è l'insieme di tutti i punti x tali che... ≫
M a r c o T a r i n i ‧ C o m p u t e r G r a p h i c s ‧ 2 0 1 2 / 1 3 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a
Cosa è un triangolo?
V 1
V 2 V 3
p
q q = k 1 v 1 + k 2 v 2
k 1 + k 2 = 1 k 1 ,k 2 >= 0
p = h 1 v 3 + h 2 q
h 1 + h 2 = 1 h 1 ,h 2 >= 0 esercizio: sostituiamo e...
M a r c o T a r i n i ‧ C o m p u t e r G r a p h i c s ‧ 2 0 1 2 / 1 3 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a
Cosa è un triangolo?
V 1
V 2 V 3
un triangolo di vertici v
1v
2v
3é l'insieme di tutti i punti x esprimibili come
x = a 1 v 1 + a 2 v 2 + a 3 v 3 a 1 a 2 a 3 scalari non negativi
a 1 + a 2 + a 3 = 1
M a r c o T a r i n i ‧ C o m p u t e r G r a p h i c s ‧ 2 0 1 2 / 1 3 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a
Per esempio...
V 1
V 2 V 3
0 v
1+ 0 v
2+ 1 v
30 v
1+ 1 v
2+ 0 v
30.33 v
1+ 0.33 v
2+ 0.33 v
3(il baricentro del triangolo) 0.5 v
1+ 0.5 v
2+ 0 v
3(punto in mezzo al lato v
1v
2) 0.65 v
1+ 0.2 v
2+ 0.15 v
3M a r c o T a r i n i ‧ C o m p u t e r G r a p h i c s ‧ 2 0 1 2 / 1 3 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a
Ebbene...
V 1
V 2 V 3
dati a 1 a 2 a 3 scalari tali positivi con a 1 + a 2 + a 3 = 1
→ → →
x = a 1 v 1 + → a 2 v 2 + a 3 v 3
è un punto del triangolo
M a r c o T a r i n i ‧ C o m p u t e r G r a p h i c s ‧ 2 0 1 2 / 1 3 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a
E anche viceversa
V 1
V 2 V 3
dato un punto p nel triangolo
→
→
→
→
esistono unici a 1 a 2 a 3 tali che
p = a 1 v 1 + a 2 v 2 + a 3 v 3
e a 1 a 2 a 3 sono positivi e a somma 1
diciamo che a 1 a 2 a 3 sono le coordinate baricentriche di p
3
M a r c o T a r i n i ‧ C o m p u t e r G r a p h i c s ‧ 2 0 1 2 / 1 3 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a
Le coordinate baricentriche
V 1
V 2 V 3
p
p ha coordinate baricentriche
(a 1 a 2 a 3 ) sse
p = a 1 v 1 + a 2 v 2 + a 3 v 3
M a r c o T a r i n i ‧ C o m p u t e r G r a p h i c s ‧ 2 0 1 2 / 1 3 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a
a 3 Kg
a 2 Kg
Le coordinate baricentriche
Interpretazione fisica (storica) (buffa)
V 1
V 2 V 3
p ha coordinate baricentriche
(a 1 a 2 a 3 ) sse
è il baricentro di:
p
a 1 Kg
M a r c o T a r i n i ‧ C o m p u t e r G r a p h i c s ‧ 2 0 1 2 / 1 3 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a
Le coordinate baricentriche Interpretazione geometrica
V 1
V 2 V 3
p
1 1
p = v 1 + x ( v 2 -v 1 ) + y ( v 3 -v 1 ) x
y
= (1- x - y ) v 1 + x v 2 + y v 3
y
x
M a r c o T a r i n i ‧ C o m p u t e r G r a p h i c s ‧ 2 0 1 2 / 1 3 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a
Le coordinate baricentriche come rapporti fra aree
• Trovare le coord baricentriche di un punto dato p
V 1
V 2 V 3
p
p = a 1 v 1 + a 2 v 2 + a 3 v 3
A2 A3
A1
3 2 1 3
3 2 2 1
1 a a
a A A A A
A A A
A A
A
tot tot tot
tot
+ +
=
=
=
=
M a r c o T a r i n i ‧ C o m p u t e r G r a p h i c s ‧ 2 0 1 2 / 1 3 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a
Coordinate baricentriche:
improtante proprietà
V
1V
2V
3p
R 3 R 2
f(p)
f( v
1)
f( v
2) f( v
3)
trasformazione affine ffff
p ha coord.
baricentriche a,b,c nel triangolo v
1v
2v
3f(p) ha ancora coord.
baricentriche a,b,c nel triangolo f(v
1) f(v
2) f(v
3)
M a r c o T a r i n i ‧ C o m p u t e r G r a p h i c s ‧ 2 0 1 2 / 1 3 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a
il "diviso due"
lo possimo ignorare.
Tanto ci interessano solo i rapporti
fra le aree
Parentesi: area di un triangolo
• In R 3 posso calcolarla con il prodotto esterno
V 1 A
V 2
V 3
d
1d
2A = | d 1 x d 2 | /2
= | (v 3 – v 1 ) x (v 2 – v 1 ) | /2
e nel caso particolare di R2 ?
proviamo...
4
M a r c o T a r i n i ‧ C o m p u t e r G r a p h i c s ‧ 2 0 1 2 / 1 3 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a
Attributi nel pipeline
Fr am m en ti & at tri bu ti & at tri bu ti & at tri bu ti & at tri bu ti in te rp ol at i in te rp ol at i in te rp ol at i in te rp ol at i Ve rti ci & lo ro a ttr ib ut i & lo ro a ttr ib ut i & lo ro a ttr ib ut i & lo ro a ttr ib ut i
pixel finali
(nello screen-buffer)
Ve rti ci pr oi et ta ti & va ry in g & va ry in g & va ry in g & va ry in g
rasterizer triangoli
co m pu ta zi on i pe r f ra m m en to set- up
rasterizer segmenti set- up
rasterizer punti set-
up
co m pu ta zi on i pe r v er tic e
rasterizer triangoli set- up
associamo degli attributi attributi attributi attributi
ai vertici mandiamo che
es: colore RGB
qui gli attributi possono subire varie computazioni,
ottenendo varying varying varying varying
ogni frammento avrà un valore
interpolato dei varying computati per vertice qui i varying
vengono interpolati
M a r c o T a r i n i ‧ C o m p u t e r G r a p h i c s ‧ 2 0 1 2 / 1 3 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a
Attributi nella rasterizzazione
• Quindi il rasterizer deve fare DUE cose:
1 trovare i frammenti che compongono la primitiva
• i frammenti interni al triangolo,
• o i frammenti che compongono un segmento 2 interpolare gli attributi per questi frammenti
• tramite le coordinate baricentriche
• Il rasterizer produce frammenti CON attributi associati
– (interpolazione degli attributi computati per vertice)
M a r c o T a r i n i ‧ C o m p u t e r G r a p h i c s ‧ 2 0 1 2 / 1 3 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a
Con terminologia di opengGL
Fr ag m en ts + va ry in g + va ry in g + va ry in g + va ry in g s in te rp ol at i in te rp ol at i in te rp ol at i in te rp ol at i Ve rti ce s + at tri bu te + at tri bu te + at tri bu te + at tri bu te s
pixel finali
(nello screen-buffer)
Ve rti ce s (2 D ) + va ry in g + va ry in g + va ry in g + va ry in g s
rasterizer triangoli
FR AG M EN T SH AD ER set- up
rasterizer segmenti set- up
rasterizer punti set- up
VE R TE X SH AD ER rasterizer triangoli set- up
mando vertici con un insieme di ATTRIBUTE
computa i VARYING (es. a partire
da questi attributi)
prende in input le variabili VARYING interpolate interpola
le variabili VARYING
M a r c o T a r i n i ‧ C o m p u t e r G r a p h i c s ‧ 2 0 1 2 / 1 3 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a
Attributi nella rasterizzazione
• Idea:
– lavoriamo su R 2
V
1V
2V
3p
R 3 R 2
f(p)
f( v
1)
f( v
2) f( v
3)
trasformazione affine ffff
p ha coord.
baricentriche a,b,c nel triangolo v
1v
2v
3f(p) ha coord.
baricentriche a,b,c nel triangolo f(v
1) f(v
2) f(v
3) NOTA: vero solo per le
trasformazioni affini.
E' solo una approssimazione per la
distorsione prospettica
M a r c o T a r i n i ‧ C o m p u t e r G r a p h i c s ‧ 2 0 1 2 / 1 3 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a