Computer Graphics

29
Computer Graphics Marco Tarini Università dell’Insubria Facoltà di Scienze MFN di Varese Corso di Laurea in Informatica Anno Accademico 2006/07 Lezione 8: attributi per vertice asdad asdsad fadf asdf 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 fadf asdf asdf asdf asd asdfasdf asd fasdf asdasdf asd asdf asd asdf asd asdf asd

description

Lezione 8: attributi per vertice. asdad asdsad fadf asdf asdf asdf asd asdfasdf asd fasdf asdasdf asd asdf asd asdf asd asdf asd. Università dell’Insubria Facoltà di Scienze MFN di Varese Corso di Laurea in Informatica Anno Accademico 200 6 /0 7. Computer Graphics. asdad asdsad - PowerPoint PPT Presentation

Transcript of Computer Graphics

Page 1: Computer Graphics

Computer Graphics

Marco Tarini

Università dell’Insubria

Facoltà di Scienze MFN di Varese

Corso di Laurea in Informatica

Anno Accademico 2006/07

Lezione 8: attributi per vertice

asdadasdsadfadfasdf asdf asdf asdasdfasdf asdfasdf asdasdf asd asdf asd asdf asdasdf asd

asdadasdsadfadfasdf asdf asdf asdasdfasdf asdfasdf asdasdf asd asdf asd asdf asdasdf asd

asdadasdsadfadfasdf asdf asdf asdasdfasdf asdfasdf asdasdf asd asdf asd asdf asdasdf asd

Page 2: Computer Graphics

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 0 6 / 0 7 ‧ 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

Page 3: Computer Graphics

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 0 6 / 0 7 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a

Cosa è un segmento?

V1

V2

con ...RRRv,v 43221 o o

Page 4: Computer Graphics

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 0 6 / 0 7 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a

Cosa è un segmento?

V1

V2

posso definirlo cosi':un segmento di vertici v1 e v2

é l'insieme di tutti i punti x esprimibili come

x = a v1 + b v2

a e b scalari positivi con a + b = 1

0.5 v1 + 0.5 v2

0.0 v1 + 1 v2

0.75 v1 + 0.25 v2

1 v1 + 0 v2

0.1 v1 + 0.9 v20.66 v1 + 0.33 v2 ≪ x è una

interpolazione

di v1 e v2 ≫

Page 5: Computer Graphics

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 0 6 / 0 7 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a

Parentesi notazione

V1

V2

x = a v1 + b v2

a e b scalari positivi con a + b = 1

x

interpolazione:

V1

V2

x = a v1 + b v2

a e b scalari positivi con a + b = 1

x

estrapolazione:

(quindi 0 ≤ a ≤ 1 e 0 ≤ b ≤ 1 )

Page 6: Computer Graphics

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 0 6 / 0 7 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a

Cosa è un triangolo?

V1

V2

con ...RRRv,v,v 432321 o o

V3

≪ é l'insieme di tutti i punti x tali che... ≫

Page 7: Computer Graphics

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 0 6 / 0 7 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a

Cosa è un triangolo?

V1

V2

V3

p

qq = k1 v1 + k2 v2

k1 + k2 = 1 k1,k2 > 0

p = h1 v3 + h2 qh1 + h2 = 1 h1,h2 > 0

esercizio: sostituiamo e...

Page 8: Computer Graphics

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 0 6 / 0 7 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a

Cosa è un triangolo?

V1

V2

V3

un triangolo di vertici v1 v2 v3

é l'insieme di tutti i punti x esprimibili come

x = a1 v1 + a2 v2 + a3 v3

a1 a2 a3 scalari positivi

a1 + a2 + a3 = 1

Page 9: Computer Graphics

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 0 6 / 0 7 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a

Per esempio...

V1

V2

V3

0 v1 + 0 v2 + 1 v3

0 v1 + 1 v2 + 0 v30.33 v1 + 0.33 v2 + 0.33 v3

(il baricentro del triangolo)0.5 v1 + 0.5 v2 + 0 v3

(punto in mezzo al lato v1 v2)0.65 v1 + 0.2 v2 + 0.15 v3

Page 10: Computer Graphics

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 0 6 / 0 7 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a

Ebbene...

V1

V2

V3

dati a1 a2 a3 scalari tali positivi

con a1 + a2 + a3 = 1

→x = a1 v1 + a2 v2 + a3 v3

é un punto del triangolo

Page 11: Computer Graphics

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 0 6 / 0 7 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a

Ma anche...

V1

V2

V3

dato un punto p nel triangolo

→esistono unici a1 a2 a3 tali che

p = a1 v1 + a2 v2 + a3 v3

e a1 a2 a3 sono positivi e a somma 1

diciamo che a1 a2 a3 sono le coordinate baricentriche di p

Page 12: Computer Graphics

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 0 6 / 0 7 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a

Le coordinate baricentriche

• Concetto F O N D A M E N T A L E

V1

V2

V3

p

p ha coordinate baricentriche (a1 a2 a3) sse

p = a1 v1 + a2 v2 + a3 v3

Page 13: Computer Graphics

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 0 6 / 0 7 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a

a3 Kg

a2 Kg

Le coordinate baricentriche Interpretazione fisica (storica) (buffa)

V1 V2

V3

p ha coordinate

baricentriche (a1 a2 a3) sse

é il baricentro di:

p

a1 Kg

Page 14: Computer Graphics

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 0 6 / 0 7 ‧ 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

V1

V2

V3 p1

1

p = v1 + h ( v2-v1 ) + k ( v3-v1 )

h

k

Page 15: Computer Graphics

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 0 6 / 0 7 ‧ 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à

V1

V2

V3

p

3R 2R

f(p)

f( v1 )

f( v2 )

f( v3 )

trasformazione affine f

p ha coord. baricentriche a,b,cnel triangolo v1 v2 v3

f(p) ha coord. baricentriche a,b,cnel triangolo f(v1) f(v2) f(v3)

Page 16: Computer Graphics

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 0 6 / 0 7 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a

Le coordinate baricentrichecome rapporti fra aree

• Quali sono le coord baricentriche di un punto p?

V1

V2

V3

p

p = a1 v1 + a2 v2 + a3 v3

A2

A3

A1

3213

32

21

1 aaa AAAAA

A

A

A

A

Atot

tottottot

Page 17: Computer Graphics

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 0 6 / 0 7 ‧ 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

Page 18: Computer Graphics

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 0 6 / 0 7 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a

Attributi nel pipeline

Fram

men

ti&

att

rib

uti

in

terp

ola

ti

Vert

ici

& loro

att

rib

uti pixel

finali(nello

screen-buffer)

Vert

ici

pro

iett

&

att

rib

uti

com

pu

tat

i

rasterizer

triangoli

com

puta

zioni

per

fram

mento

set-up

rasterizer

segmenti

set-up

rasterizer

punti

set-up

com

puta

zioni

per

vert

ice

rasterizer

triangoli

set-up

associamodegli

attributiai vertici

chemandiamo

es: colore RGB

qui gli attributi

possono subirevarie

compuatzioni

ogni frammento avrà

una valoreinterpolato

degli attributiper vertice

qui gli attributivengono interpolati

Page 19: Computer Graphics

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 0 6 / 0 7 ‧ 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• e.g. i frammenti interni al triangolo

2 interpolare gli attributi per questi frammenti• tramite le coordinate baricentriche

• Il rasterizer produce frammenti CON attributi associati

Page 20: Computer Graphics

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 0 6 / 0 7 ‧ 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 R2

V1

V2

V3

p

3R 2R

f(p)

f( v1 )

f( v2 )

f( v3 )

trasformazione affine f

p ha coord. baricentriche a,b,cnel triangolo v1 v2 v3

f(p) ha coord. baricentriche a,b,cnel triangolo f(v1) f(v2) f(v3)

NOTA: vero solo per le trasformazioni affini.

E' solo una approssimazione per

la distorsione prospettica

Page 21: Computer Graphics

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 0 6 / 0 7 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a

Scan-line rasterizer & attributi

• Otteniamo gli attributi per frammentointerpolando gli attributi per verticelungo segmenti (linearmente)

• Equivalente ad usare le coordinate baricentriche del frammentocome pesi

Page 22: Computer Graphics

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 0 6 / 0 7 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a

Scan-line rasterizer & attributi

• Interpoliamo gli attributi:– linearmente sui bordi– e in ogni scanline, tra gli estremi dello span

v0

v2

v1

p qf

p interpolatofra a2 e a1

q interpolatofra a0 e a1

f interpolatofra p e q

a2

a1

a0

Page 23: Computer Graphics

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 0 6 / 0 7 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a

Scan-line rasterizer & attributi

• Lo stesso per tutti gli attributi

v0

v2

v1

p qf

a2, b2, c2...

a1, b1, c1...

a0, b0, c0...

Ottimaizzatione:

si puo'calcolare

incrementalme.

vediamo come →

Page 24: Computer Graphics

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 0 6 / 0 7 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a

Scan-line rasterizer & attributi

a2

a1

a0

Primo frammento prodotto:

ha come attributo a0

ogni volta che mi sposto 1 pixel a dx:

aumento di una costante dx

ogni volta che mi sposto 1 pixel in alto:

aumento di una costante dy

Page 25: Computer Graphics

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 0 6 / 0 7 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a

Scan-line rasterizer & attributi

a2

a1

a0

Primo frammento prodotto:

ha come attributo a0

ogni volta che mi sposto 1 pixel a dx:

aumento di una costante dx

ogni volta che mi sposto 1 pixel in alto:

aumento di una costante dy

9 dx + 8 dy = a1 - a0

8 p

ass

i

9 passi

Page 26: Computer Graphics

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 0 6 / 0 7 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a

Scan-line rasterizer & attributi

a2

a1

a0

Primo frammento prodotto:

ha come attributo a0

ogni volta che mi sposto 1 pixel a dx:

aumento di una costante dx

ogni volta che mi sposto 1 pixel in alto:

aumento di una costante dy

9 dx + 8 dy = a1 - a0

11 p

ass

i

1 passo

dx + 11 dy = a2 - a0

risolvo nella fase diSET-UP di quel triangolo

Page 27: Computer Graphics

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 0 6 / 0 7 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a

Edge functions e attributi

• Per i rastertizzatori basati su edge-functions:– useremo la regola delle aree:

V1

V2

V3

pp = a1 v1 + a2 v2 + a3 v3A2

A3

A1

3213

32

21

1 aaa AAAAA

A

A

A

A

Atot

tottottot

Page 28: Computer Graphics

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 0 6 / 0 7 ‧ 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 rapportifra le aree

Parentesi: area di un triangolo

• In R3 posso calcolarla con il prodotto esterno

AV1

V2

V3

d1

d2

A = | d1 x d2 | /2 = | (v3 – v1) x (v2 – v1 ) | /2

e nel caso particolare di R2 ?

proviamo...

Page 29: Computer Graphics

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 0 6 / 0 7 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a

FlashBack: Edge functions

SI

NO

• La funzione il cui segno ci dice in quale semipiano siamo

n

q

v0=(x0, y0 )

v1=(x1, y1 )

n = ( - ( y1 -y0 ) , x1 -x0)p = (x0 , y0)

f(q) = n‧q - n‧p

v2

totale: l'edge functionper un lato è

la coord baricentricaralativa al vertice

opposto

(dopo aver diviso perla somma delle 3 edge functions)