1
Computer Graphics
Marco Tarini Università dell’Insubria
Facoltà di Scienze MFN di Varese Corso di Laurea in Informatica Anno Accademico 2012/13
Lezione Lezione Lezione
Lezione 15: 15: 15: 15: 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 2 / 1 3 ‧ 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 2 / 1 3 ‧ 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
α
= livellodi trasparenza
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
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
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
Alpha Blending
• Semitrasparenze
F ram m enti & a ttr ib u ti & a ttr ib u ti & a ttr ib u ti & a ttr ib u ti in te rp o la ti in te rp o la ti in te rp o la ti in te rp o la ti Ver ti c i & l o ro a ttr ib u ti & l o ro a ttr ib u ti & l o ro a ttr ib u ti & l o ro a ttr ib u ti
Screen buffer Ver ti c i por iettati & a ttr ib u ti & a ttr ib u ti & a ttr ib u ti & a ttr ib u ti co m p u ta ti co m p u ta ti co m p u ta ti co m p u ta ti
rasterizer triangoli set-
up rasterizer segmenti set-
up rasterizer
punti set-
up
c om put az ioni per v er tic e c om put az ioni per f ram m ent o
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
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 frammento
• E’ la quarta componente del colore:
rgb rgb rgb rgb α
F ram m enti & a ttr ib u ti & a ttr ib u ti & a ttr ib u ti & a ttr ib u ti in te rp o la ti in te rp o la ti in te rp o la ti in te rp o la ti c om put az ioni per f ram m ent o
Screen
buffer
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
Alpha Blending
• Il colore finale di un frammento consiste di 4 componenti:
– r,g,b r,g,b r,g,b r,g,b,,,, α
• Quando produco un frammento – (che sopravviva al depth test) invece di scriverlo
nello screen buffer (overwrite), scrivo (ad es):
F ram m enti & a ttr ib u ti & a ttr ib u ti & a ttr ib u ti & a ttr ib u ti in te rp o la ti in te rp o la ti in te rp o la ti in te rp o la ti
Screen buffer
c om put az ioni per f ram m ent o
) ( ) , , ( ) 1 ( ) , , ( ) , ,
( r g b finale = r g b vecchio ⋅ − α + r g b nuovo ⋅ α
"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 2 / 1 3 ‧ 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 (in GLSL)
• 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 2 / 1 3 ‧ 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
F ram m enti & a ttr ib u ti & a ttr ib u ti & a ttr ib u ti & a ttr ib u ti in te rp o la ti in te rp o la ti in te rp o la ti in te rp o la ti c om put az ioni per f ram m ent o
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 2 / 1 3 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a
Attivare Alpha Blending (in OpenGL)
• Abilitare l'alpha blending:
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
• Determinare la funzione di blending:
• ad esempio
significa:
) 1 ( ) , , ( ) ( ) , , ( ) , ,
( r g b finale = r g b nuovo ⋅ α + r g b vecchio ⋅ − α
Nota sulla terminologia
• 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 2 / 1 3 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a
) ( ) , , ( ) 1 ( ) , , ( ) , ,
( r g b finale = r g b nuovo ⋅ − α + r g b vecchio ⋅ α
) 1 ( ) , , ( ) ( ) , , ( ) , ,
( r g b finale = r g b nuovo ⋅ α + r g b vecchio ⋅ − α
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
Alpha Blending con OpenGL
• 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,
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
Screen buffer
Alpha Blending
• Posso avere 4 componenti anche nello screen buffer
– nel caso, posso usare l'alpha letto dallo screen buffer oltre a
quello prodotto dal frammento F ram m enti & a ttr ib u ti & a ttr ib u ti & a ttr ib u ti & a ttr ib u ti in te rp o la ti in te rp o la ti in te rp o la ti in te rp o la ti
componentiR G B
componente
c om put az ioni per f ram m ent o α
alpha dello screen buffer
=
alpha "di destinazione"
alpha del frammento
= alpha "sorgente"
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
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è?)
F ram m enti & a ttr ib u ti & a ttr ib u ti & a ttr ib u ti & a ttr ib u ti in te rp o la ti in te rp o la ti in te rp o la ti in te rp o la ti c om put az ioni per f ram m ent o
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 2 / 1 3 ‧ 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 2 / 1 3 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a
“alpha test” (in GLSL)
float alpha = ... ;
if ( alpha < 0.01 ) discard;
(frammento passa il test se più opaco dell'1%)
(frammenti più trasparenti dell'1%
vengono scartati) Per es...
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
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 alpha----test test test test ☺
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
Texture mapping e Alpha Test
• Trucco molto utile:
– es: simboli e testo sulla scena...
– es: sistemi di particelle
con particelle grandi
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
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 2 / 1 3 ‧ 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 2 / 1 3 ‧ 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 2 / 1 3 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a