Computer Graphics
Marco Tarini Università dell’Insubria
Facoltà di Scienze MFN di Varese Corso di Laurea in Informatica Anno Accademico 2011/12
Lezione 14:
Lezione 14: Lezione 14:
Lezione 14: alpha blending
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 1 / 1 2 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a
Semitrasparenze: come?
+ 0.5 x
0.5 x =
+ 0.25 x
0.75 x =
+ 0.75 x
0.25 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 1 / 1 2 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a
Semitrasparenze: come?
+ α x
(1- α ) x =
+ α x
(1- α ) x =
+ α x
(1- α ) x =
vecchia:
(già sul buffer)
nuova:
che sovrascrivo
α = 0.25
α = 0.5
α = 0.75
risultato
α = livello
trasparenza di
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 1 / 1 2 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a
Semitrasparenze: come?
• Quando scrivo un colore nello screen buffer, invece di:
– sovrascrivere il colore vecchio con quello nuovo eseguo:
– “mischio” il colore vecchio con quello nuovo – (cioè: sovrascrivo il col. vecchio
con una interpolazione fra: col vecchio e col nuovo)
• peso dell’interpolaz: il parametro alpha
Alpha Blending
• Semitrasparenze
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
Screen buffer Ve rti ci p or ie tta ti & at tri bu ti & at tri bu ti & at tri bu ti & at tri bu ti co m pu ta ti co m pu ta ti co m pu ta ti co m pu ta ti
rasterizer triangoli set- up
rasterizer segmenti set- up
rasterizer punti set- up
co m pu ta zi on i pe r v er tic e co m pu ta zi on i pe r f ra m m en to
Alpha Blending
• I colori hanno 4 componenti:
– R,G,B, α α α α
• Quando
arriva un frammento – (che sopravviva al depth test) invece di sovrascriverlo, uso la formula
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
Screen buffer
co m pu ta zi on i pe r f ra m m en to
) ( ) , , ( ) 1 ( ) , , ( ) , ,
( = ⋅ − α + ⋅ α
nuovo vecchio
finale
r g b r g b
b g r
"alpha blending"
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 1 / 1 2 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a
Alpha Blending
• Il fragment shader dovrà dare in output:
– …un colore RGB e…
– …una profondità e…
– un parametro alpha un parametro alpha un parametro alpha un parametro alpha
• la trasparenza di quel quel quel quel pixel
• E’ la quarta componente del colore RGB α
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 co m pu ta zi on i pe r f ra m m en to
Screen buffer
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 1 / 1 2 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a
Alpha Blending
• Nota:
l'alpha blending e' una operazione di read-write
su memoria condivisa (come il depth test)
• cautela da parte di chi la implementa in HW...
• alpha blend avviene dopo la fine dello shader, se abilitato
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 co m pu ta zi on i pe r f ra m m en to
Screen buffer
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 1 / 1 2 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a
Alpha Blending
• Problema (grave):
alpha blending è ORDER DEPENDENT ORDER DEPENDENT ORDER DEPENDENT ORDER DEPENDENT
• Per oggetti semi-trasparenti, l'ordine in cui mando le primitive di rendering torna ad essere determinante
(con o senza il depth test!)
• (Perchè?)
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 co m pu ta zi on i pe r f ra m m en to
Screen buffer
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 1 / 1 2 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a
Screen buffer
Alpha Blending
• Posso avere 4 componenti anche nello screen buffer
– nel caso, posso usare l'alpha dello screen buffer
invece-che / oltre-a quello del frammento 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
componentiR G B
componente
α α α
co m pu ta zi on i pe r f ra m m en to α
alpha dello screen buffer alpha "di destinazione" =
alpha del frammento alpha "sorgente" =
Nota sui nomi
• Il parametro alpha può rappresentare la trasparenza
o l’ opacità
– ovviamente: opacità = 1 – trasparenza – Alpha = trasparenza:
– Alpha = opacità:
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 1 / 1 2 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a
) ( ) , , ( ) 1 ( ) , , ( ) , ,
( = ⋅ − α + ⋅ α
vecchio nuovo
finale
r g b r g b
b g r
) 1 ( ) , , ( ) ( ) , , ( ) , ,
( = ⋅ α + ⋅ − α
vecchio nuovo
finale
r g b r g b
b g r
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 1 / 1 2 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a
Alpha Blending con OpenGL
• Per prima cosa, attivare l'alpha blending:
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
• Determinare la funzione di blending:
• ad esempio
significa:
) 1 ( ) , , ( ) ( ) , , ( ) , ,
( = ⋅ α + ⋅ − α
vecchio nuovo
finale
r g b r g b
b
g
r
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 1 / 1 2 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a
Alpha Blending con OpenGL
• Ma ci sono molte altre compinazioni possibili!
glBlendFunc( source_factor , dest_factor )
GL_ZERO, GL_ONE,
GL_SRC_ALPHA, GL_ONE_MINUS_SRC_COLOR, GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA GL_DST_COLOR, GL_ONE_MINUS_DST_COLOR,
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 1 / 1 2 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a
Set dell’attributo alpha
• Nel fragment shader, l’ alpha è
la 4ta componente del colore prodotto in usicta
Es:
oppure:
etc…
gl_FragColor = vec4( red , green , blue , alpha ) ;
gl_FragColor.rgb = vec3( red , green , blue );
gl_FragColor.a = alpha ;
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 1 / 1 2 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a
Infine: "alpha test" (o: “alpha kill”)
• Se un frammento é troppo trasparente, scartiamolo (nel fragment shader):
if ( alpha < k) then scarta frammento
• Note:
– E' un test facile facile
– Non è read-write in memoria condivisa
– Prima viene fatto, più operazioni risparmio (se scarto) – Indipendente dall'ordine delle primitive (order independent! ☺ ) – E’ un test molto efficiente
– se scarta, non si aggiorna neanche il depth buffer
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 1 / 1 2 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a
Texture mapping e Alpha Test
• In una tessitura, ogni texel può avere anche (o, solo) una componente alpha
– "opacity maps", "alpha maps"
"RGBA maps"
• Un canale alpha può
anche essere composto di soli – 1 completamente opaco – 0 completamente trasparente
• frammenti corrispondenti cassati dall'alpha alpha alpha----test alpha test test test ☺ ☺ ☺ ☺
Texture mapping e Alpha Test
• Trucco molto utile:
– es: simboli e testo sulla scena...
– es: sistemi di particelle con particelle grandi
Texture mapping e Alpha Test
• Trucco molto utile:
– es: erba...
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 1 / 1 2 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a
Texture mapping e Alpha Test
• Trucco molto utile:
– es: drappi, barba...
by Micheal Filipowski 2004
tessitura
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 1 / 1 2 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a
Texture mapping e Alpha Test
• Trucco molto utile:
– es: alberi
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 1 / 1 2 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a
Texture mapping e Alpha Test
• Trucco molto utile:
– es: pelliccia
tessitura (ripetuta)
Computer Graphics
Marco Tarini Università dell’Insubria
Facoltà di Scienze MFN di Varese Corso di Laurea in Informatica Anno Accademico 2011/12
Lezione 14:
Lezione 14:
Lezione 14:
Lezione 14: display lists
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 1 / 1 2 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a
Display Lists
• Codice (es. in C++) che fa rendering:
mix di
– istruzioni openGL che mandano primitive – comandi vari
• (cicli, guardie...)
• Idea:
– la prima volta,
registrare tutte le primitive e i comandi in una struttura (in RAM);
le volte successive mandare solo le primitive (senza comandi intermedi)
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 1 / 1 2 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a
Display Lists
• Esempio di codice
sedia = glGenLists( 1 );
glNewList( sedia, GL_COMPILE );
... /* qui tutto il rendering ... dell oggetto “sedia" */
glEndList();
/* quando la voglio richiamare: */
glCallList( sedia );
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 1 / 1 2 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a
Display Lists
• Difetti Display List:
– sono statiche
– consumo di memoria (in RAM e GPU RAM).
• Vantaggi
– evitano tutte le chiamate di funzione, i cicli, etc – danno la possibilità all’API openGL di ottimizzare
(durante la "compilazione" della lista), per es:
• convertire tutti i dati nel formato interno piu’ conveniente
• stripificazione
• memorizzare nella scheda video – come V.B.O., Vertex Buffer Object (vedi poi)
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 1 / 1 2 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a
Colli di bottiglia...
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
Screen buffer Ve rti ci p or ie tta ti & at tri bu ti & at tri bu ti & at tri bu ti & at tri bu ti co m pu ta ti co m pu ta ti co m pu ta ti co m pu ta ti
rasterizer triangoli set- up
rasterizer segmenti set- up
rasterizer punti set- up
co m pu ta zi on i pe r v er tic e co m pu ta zi on i pe r f ra m m en to
Qui? Allora applicazione
“transofrm limited”
(o “gemoetry limited”)
QUI?
Allora applicazione
“fill limited”
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 1 / 1 2 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a
Può essere anche la comunicazione!
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
Screen buffer Ve rti ci p or ie tta ti & at tri bu ti & at tri bu ti & at tri bu ti & at tri bu ti co m pu ta ti co m pu ta ti co m pu ta ti co m pu ta ti
rasterizer triangoli set- up
rasterizer segmenti set- up
rasterizer punti set- up
co m pu ta zi on i pe r v er tic e co m pu ta zi on i pe r f ra m m en to
collo di bottiglia qui?
applicazione BUS LIMITED (o BANDWIDTH LIMITED)
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 1 / 1 2 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a