Sistemi Multimediali II
Lezione 11:
Università dell’Insubria
Facoltà di Scienze MFN di Varese
Corso di Laurea in Informatica
Anno Accademico 2004/05
Marco Tarini
lo Shading
I 4 fattori che consideriamo
luce finale
=
ambiente
+
riflessione diffusa
+
riflessione speculare
+
emissione
M a r c o T a r i n i ‧ S i s t e m i M u l t i m e d i a l i I I ‧ 2 0 0 4 / 0 5 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a - 2/40
Equazione di Lighting
I luce ambient  k materialeambient 
I tot 
I luce diffuse  kmaterialediffuse  ( Lˆ  Nˆ ) 
n
ˆ
ˆ
I luce spacular  kmaterialespacular  ( H  N )
caratteristiche della luce
caratteristiche del materiale
dati della geometria
 f attenuazione luce
 f effettospotlight
 k materialeemission


1
f attentuazione luce  min 
, 1
2
 c1  c2 d L  c3d L 
f effettospotlight  f L, spot direction, spot cutoff Angle, spot beam width 
M a r c o T a r i n i ‧ S i s t e m i M u l t i m e d i a l i I I ‧ 2 0 0 4 / 0 5 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a - 3/40
Normale di un triangolo
• Cioe' il suo orientamento nello spazio
v1
^
N
v0
N  (v1  v0 )  (v2  v0 )
v2
N
ˆ
N
|N|
M a r c o T a r i n i ‧ S i s t e m i M u l t i m e d i a l i I I ‧ 2 0 0 4 / 0 5 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a - 4/40
Lighting faccia per faccia
"flat shading"
1.
geometria di partenza
2.
per ogni faccia,
calcolo normale
3.
applico lighting
ad ogni normale
M a r c o T a r i n i ‧ S i s t e m i M u l t i m e d i a l i I I ‧ 2 0 0 4 / 0 5 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a - 5/40
Definizione
•
Shading:
–
•
ricetta per applicare un lighting
Ad esempio:
flat shading
1. Applico lighting a normale di faccia
- (ottengo un colore)
2. Copro tutta la faccia di quel colore
M a r c o T a r i n i ‧ S i s t e m i M u l t i m e d i a l i I I ‧ 2 0 0 4 / 0 5 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a - 6/40
Flat shading: problema
• Approssimo superfici curve con triangoli
• Applico il flat shading
• Risultato:
– spigoli apparenti su superfici curve
non sembra nemmeno
una sfera
un
brutto
artefatto
!
M a r c o T a r i n i ‧ S i s t e m i M u l t i m e d i a l i I I ‧ 2 0 0 4 / 0 5 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a - 7/40
Flat shading: problema
• Altro esempio:
M a r c o T a r i n i ‧ S i s t e m i M u l t i m e d i a l i I I ‧ 2 0 0 4 / 0 5 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a - 8/40
Flat shading: problema
• Più faccie uso,
meno evidente
il problema
>10.000 faccie,
e ancora si vedono
gli spigoli artefatti
perche?
M a r c o T a r i n i ‧ S i s t e m i M u l t i m e d i a l i I I ‧ 2 0 0 4 / 0 5 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a - 9/40
Flat shading: problema
• A peggiorare le cose:
l'effetto ottico Mach-band
Il contrasto fra zone di colore uniforme non
sfugge mai al nostro occhio.
(neanche se le zone sono molte,
e la differenza fra loro è relativamente piccola).
Il cervello aumenta il contrasto fra le zone di colore uniformi
L'artefatto e' duro a morire.
M a r c o T a r i n i ‧ S i s t e m i M u l t i m e d i a l i I I ‧ 2 0 0 4 / 0 5 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a - 10/40
Idea
• Utilizzare l'interpolazione del colore
dentro alla faccia
M a r c o T a r i n i ‧ S i s t e m i M u l t i m e d i a l i I I ‧ 2 0 0 4 / 0 5 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a - 11/40
Idea
• Utilizzare l'interpolazione del colore
dentro alla faccia
"Gouraud" Shading
by Henri Gouraud, 1971
1- Applico lighting ai 3 vertici di ogni triangolo
• (ottengo un colore)
2- Interpolo il colore nel triangolo
Per applicare il lighting, devo avere la normale!
Normale definita per una faccia.
Ma per un vertice?
M a r c o T a r i n i ‧ S i s t e m i M u l t i m e d i a l i I I ‧ 2 0 0 4 / 0 5 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a - 12/40
Normali per vertice
• In certi contesti, la normale dei vertici
nasce insieme al resto del modello 3D.
– per esempio,
• quando si modella una sfera, un cilindro, un cono...
• quando si estrae la superficie da un volume
• quando si costruisce una superficie triangolata
campionando una superficie parametrica
• ...
• Sennò...
M a r c o T a r i n i ‧ S i s t e m i M u l t i m e d i a l i I I ‧ 2 0 0 4 / 0 5 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a - 13/40
Normali per vertice
Normale di un Triangolo:
v1
v1×v2
v2
Normale di un vertice
condiviso da n triangoli:
N  Nˆ 1  Nˆ 2  ...  Nˆ n
N
ˆ
N
|N|
N̂1
N̂v
N̂ 6
N̂ 2
N̂ 5
N̂3
N̂ 4
M a r c o T a r i n i ‧ S i s t e m i M u l t i m e d i a l i I I ‧ 2 0 0 4 / 0 5 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a - 14/40
rasterizer
punti
setup
rasterizer
triangoli
setup
rasterizer
segmenti
computazioni
per frammento
setup
Frammenti
& attributi
interpolati
Vertici poriettati
& attributi
computati
Vertici
computazioni
per vertice
& loro attributi
Dove avviene la computazione del lighting?
Screen
buffer
y
v1
v0
x
v2
v1
v0
z
v2
M a r c o T a r i n i ‧ S i s t e m i M u l t i m e d i a l i I I ‧ 2 0 0 4 / 0 5 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a - 15/40
Scelta Fondamentale
• Nel nostro paradigma di rendering,
la normale (dei vertici):
• NON viene calcolata nel pipeline (e dove?)
• viene mandata come ATTRIBUTO per VERTICE
• la normale "fa parte del modello"
• proprio come le posizioni dei suoi vertici
• la computazione delle normali, se necessaria,
è tipicamente un pre-processing
• concettualmente giusto, e pratico
M a r c o T a r i n i ‧ S i s t e m i M u l t i m e d i a l i I I ‧ 2 0 0 4 / 0 5 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a - 16/40
compreso:
proprietà
del
materiale
e normale
proietto
e
applico
lighting
rasterizer
punti
setup
rasterizer
triangoli
setup
rasterizer
segmenti
compreso:
colore
per vertice
interpolo
colore
computazioni
per frammento
setup
Frammenti
& attributi
interpolati
Vertici poriettati
& attributi
computati
computazioni
per vertice
Vertici
& loro attributi
Gouraud shading
Screen
buffer
compreso:c
ol. finale
(risultato
del lighting)
M a r c o T a r i n i ‧ S i s t e m i M u l t i m e d i a l i I I ‧ 2 0 0 4 / 0 5 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a - 17/40
Gouraud shading
• Risultati:
M a r c o T a r i n i ‧ S i s t e m i M u l t i m e d i a l i I I ‧ 2 0 0 4 / 0 5 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a - 18/40
Si può fare meglio
• Invece di interpolare il colore dopo il lighting.
interpolo la normale prima del lighting!
• occhio:
interpolando due vettori normali,
non ottengo un vettore normale:
• (devo rinormalizzare dopo l'interpolazione)
M a r c o T a r i n i ‧ S i s t e m i M u l t i m e d i a l i I I ‧ 2 0 0 4 / 0 5 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a - 19/40
Si può fare meglio
• Invece di interpolare il colore dopo il lighting.
interpolo la normale prima del lighting!
"Phong" * Shading
by Bui-Tuong Phong , 1973
1- Interpolo la normale nella faccia
2- Rinormalizzo
3- Applico lighting
* Attenzione a non confondere il Phong Shading (uno shading)
con il Phong Lighting Model (modello di illuminazione)
M a r c o T a r i n i ‧ S i s t e m i M u l t i m e d i a l i I I ‧ 2 0 0 4 / 0 5 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a - 20/40
compreso:
proprietà
del
materiale
e normale
proietto
rasterizer
punti
setup
rasterizer
triangoli
setup
rasterizer
segmenti
compreso:
normale
trasformata
interpolo
normale
computazioni
per frammento
setup
Frammenti
& attributi
interpolati
Vertici poriettati
& attributi
computati
computazioni
per vertice
Vertici
& loro attributi
Phong shading
compreso:
normale
interpolata
Screen
buffer
rinormalizzo e
applico
lighting
per ottenere
il colore
del frammento
M a r c o T a r i n i ‧ S i s t e m i M u l t i m e d i a l i I I ‧ 2 0 0 4 / 0 5 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a - 21/40
Gouraud contro Phong shading
• Goraud Shading - lighting per vertice
molto meno oneroso:
applico il lighting una volta per vertice!
• Phong Shading - lighting per frammento
risultati migliori
specialmente con i riflessi luminosi e piccoli (esponente speculare alto)
M a r c o T aFlat
r i n i shading
‧ S i s t e m i M u l t i m e d i a lGoraund
i I I ‧ 2 0 0shading
4 / 0 5 ‧ U n i v e r s i t à d ePhong
l l ’ I n s u Shading
b r i a - 22/40
sia per il Gouraud che per il Phong shading
flat shading
Marco Tarini ‧ Sistemi Multimediali II ‧ 2004/05 ‧
Goraud shading
simile)
U n i v e r s i t(Phong
à d e l l ’ I shading
nsubria è
- 23/40
sia per il Gouraud che per il Phong shading
• Goraud e Phong servono per superfici lisce
– eliminano gli spigoli artefatti
– eliminano anche gli spigoli corretti
• Soluzione: duplicare i vertici
M a r c o T a r i n i ‧ S i s t e m i M u l t i m e d i a l i I I ‧ 2 0 0 4 / 0 5 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a - 24/40
Gouraud o Phong shading?
• le specifiche di OpenGL non prescrivono
quale debba essere usato
– di solito (per ora) Gouraud
• dipende dall'implementatore HW
– non esitono nemmeno comandi OpenGL
che cambiano shading
M a r c o T a r i n i ‧ S i s t e m i M u l t i m e d i a l i I I ‧ 2 0 0 4 / 0 5 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a - 25/40
Passiamo a OpenGL
• Abilitare il lighting:
glEnable(GL_LIGHTING);
( il colore corrente – per es glColor3f –
non conta più.
Conta il materiale corrente!)
• Ora dobbiamo mandare le normali,
settare le luci e i materiali
M a r c o T a r i n i ‧ S i s t e m i M u l t i m e d i a l i I I ‧ 2 0 0 4 / 0 5 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a - 26/40
Normali
• Setto la "normale corrente".
Proprio come facevo con i colori:
glNormal3d(x,y,z);
la quarta coordinata e' sottointesa: ZERO!
(si tratta di vettori)
M a r c o T a r i n i ‧ S i s t e m i M u l t i m e d i a l i I I ‧ 2 0 0 4 / 0 5 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a - 27/40
Cosa succede alle normali?
Modellazione + Vista:
trasformazioni rigide
(o almeno
mantengono
gli angoli)
y
v0
v1
x
v2
risposta: subiscono la Model-View matrix,
ma non la Projection matrix.
E' proprio per questo che le due sono
tenute separate!
z
Proiezione:
non mantiene gli angoli
object Coordinates
v1
v0
v2
screen Space
modellazione
viewport
v2
z
vista
v1
v0
x
y
v1
-z
x
v0
v2
proiezione
y
1
v0
1
v2
-1
v1
y
v1
x
-z
v2
v0
-1
view Coordinates
Normalized Device
M a r c o T a r i n i ‧ S i s t e m i M u l (a.k.a.
t i m e deye
i a l Coordinates)
i I I ‧ 2 0 0 4 / 0 5 ‧ U n i v e r s i t à d e l l ’Coordinates
I n s u b r i a - 28/40
world Coordinates
Le normali rimangono normali nella Transform?
• Solo se la modellazione-vista è rigida
modellazione-vista = V ‧ M
rotazioni,
traslazioni
(quindi sempre rigida)
rotazioni,
traslazioni
e forse scalature
(quindi non sempre rigida)
M a r c o T a r i n i ‧ S i s t e m i M u l t i m e d i a l i I I ‧ 2 0 0 4 / 0 5 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a - 29/40
Le normali rimangono normali nella Transform?
• Morale: se uso scalature nella ModelView devo
rinormalizzare le normali prima del Lighting
• chiedo ad OpenGL di farlo:
glEnable(GL_NORMALIZE);
o di non farlo: (default)
glDisable(GL_NORMALIZE);
M a r c o T a r i n i ‧ S i s t e m i M u l t i m e d i a l i I I ‧ 2 0 0 4 / 0 5 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a - 30/40
Normali come attributi
• Proprio come il colore:
glBegin(GL_TRIANGLES);
glNormal3fv( n );
glVertex3fv( v0 );
glVertex3fv( v1 );
glVertex3fv( v2 );
glBegin(GL_END);
glBegin(GL_TRIANGLES);
glNormal3fv( n0 );
glVertex3fv( v0 );
glNormal3fv( n1 );
glVertex3fv( v1 );
glNormal3fv( n2 );
glVertex3fv( v2 );
glBegin(GL_END);
flat shading !
Gouraud shading
(o forse Phong)
M a r c o T a r i n i ‧ S i s t e m i M u l t i m e d i a l i I I ‧ 2 0 0 4 / 0 5 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a - 31/40
Normali come attributi
• Scorciatoia:
– se setto:
glShadeModel(GL_FLAT);
gli attributi non vongono interpolati
ma rimangono costanti nella faccia
(utile per le triangle strip e i triangle fan),
finchè non rimetto
glShadeModel(GL_SMOOTH);
M a r c o T a r i n i ‧ S i s t e m i M u l t i m e d i a l i I I ‧ 2 0 0 4 / 0 5 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a - 32/40
OpenGL: luci!
• Abbiamo a disposizione N luci
– ricordiamoci: il loro effetto
(ambient + diffuse + specular)
si somma
• quante?
– dipende dall'implementazione di OpenGL
– le specifiche di OpenGL impongono: almeno 8
– il numero esatto lo troviamo nella costante
GL_MAX_LIGHT
M a r c o T a r i n i ‧ S i s t e m i M u l t i m e d i a l i I I ‧ 2 0 0 4 / 0 5 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a - 33/40
OpenGL: luci!
• Ogni luce può essere accesa o spenta
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
...
• dove GL_LIGHT0, GL_LIGHT1 etc sono costanti
– nota: GL_LIGHTk vale GL_LIGHT0+k. Utile per i for
• di default, la luce 0 è l'unica accesa
M a r c o T a r i n i ‧ S i s t e m i M u l t i m e d i a l i I I ‧ 2 0 0 4 / 0 5 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a - 34/40
OpenGL: luci!
• Di ogni luce, settiamo i colori
glLightfv(GL_LIGHT0, GL_DIFFUSE, v);
glLightfv(GL_LIGHT0, GL_AMBIENT, v);
glLightfv(GL_LIGHT0, GL_SPECULAR, v);
M a r c o T a r i n i ‧ S i s t e m i M u l t i m e d i a l i I I ‧ 2 0 0 4 / 0 5 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a - 35/40
OpenGL: luci!
• Di ogni luce, possiamo anche settare:
– se voglio effetto spotlight:
default:
glLightfv(GL_LIGHT0,GL_SPOT_DIRECTION,v);
glLightf (GL_LIGHT0,GL_SPOT_CUTOFF,v);
glLightf (GL_LIGHT0,GL_SPOT_EXPONENT,v);
(0,0,-1)
180
0.0
– se voglio attenuazione con la distanza:
glLightf(GL_LIGHT0,GL_CONSTANT_ATTENUATION,a);
glLightf(GL_LIGHT0,GL_LINEAR_ATTENUATION,b);
glLightf(GL_LIGHT0,GL_QUADRATIC_ATTENUATION,c);
default:
1
0
0
M a r c o T a r i n i ‧ S i s t e m i M u l t i m e d i a l i I I ‧ 2 0 0 4 / 0 5 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a - 36/40
OpenGL: luci!
• Di ogni luce, settiamo la posizione:
glLightfv(GL_LIGHT0,GL_POSITION,v);
– Se luce posizionale,
v = {x,y,z,1}
– Se luce direzionale, ("distante all'infinito")
v = {x,y,z,0}
– Coordinate affini!
M a r c o T a r i n i ‧ S i s t e m i M u l t i m e d i a l i I I ‧ 2 0 0 4 / 0 5 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a - 37/40
OpenGL: luci!
• Di ogni luce, settiamo la posizione:
glLightfv(GL_LIGHT0,GL_POSITION,v);
• importante:
la posizione delle luci subisce la moltiplicazione
per la matrice MODEL_VIEW
per es: come faccio a fare la tipica headlight?
M a r c o T a r i n i ‧ S i s t e m i M u l t i m e d i a l i I I ‧ 2 0 0 4 / 0 5 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a - 38/40
Equazione di Lighting
I luce ambient  k materialeambient 
I tot 
I luce diffuse  kmaterialediffuse  ( Lˆ  Nˆ ) 
n
ˆ
ˆ
I luce spacular  kmaterialespacular  ( H  N )
caratteristiche della luce
caratteristiche del materiale
dati della geometria
 f attenuazione luce
 f effettospotlight
 k materialeemission


1
f attentuazione luce  min 
, 1
2
 c1  c2 d L  c3d L 
f effettospotlight  f L, spot direction, spot cutoff Angle, spot beam width 
M a r c o T a r i n i ‧ S i s t e m i M u l t i m e d i a l i I I ‧ 2 0 0 4 / 0 5 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a - 39/40
OpenGL: materiali!
• Settiamo tutti i parametri dei materiali:
– i colori:
glMaterialfv(face,
glMaterialfv(face,
glMaterialfv(face,
glMaterialfv(face,
GL_AMBIENT, colorvec);
GL_EMISSION, colorvec);
GL_DIFFUSE, colorvec);
GL_SPECULAR, colorvec);
– l'esponente speculare:
glMaterialf (face, GL_SHININESS, intval);
"GL_FRONT"
scorciatoia: esiste anche
GL_AMBIENT_AND_DIFFUSE
asdasdsadasdasd
che setta entrambi
i colori allo stesso valore
M a r c o T a r i n i ‧ S i s t e m i M u l t i m e d i a l i I I ‧ 2 0 0 4 / 0 5 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a - 40/40
Non abbiamo ancora visto:
Color - Material
attivazione:
glEnable(GL_COLOR_MATERIAL);
uso:
glColorMaterial(face, mode);
es: se si mette
glColorMaterial(GL_FRONT, GL_DIFFUSE);
allora come colore diffuso del materiale si
userà (l'altrimenti ingorato) colore corrente
- si, proprio quello settato col vecchio glColor3f
M a r c o T a r i n i ‧ S i s t e m i M u l t i m e d i a l i I I ‧ 2 0 0 4 / 0 5 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a - 41/40
Non abbiamo ancora visto:
illuminazione da due lati contemporaneamente
attivazione:
glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,1);
uso:
glColorMaterial3f(GL_BACK, ... );
glColorMaterial3f(GL_FRONT, ... );
glColorMaterial3f(GL_FRONT_AND_BACK, ... );
M a r c o T a r i n i ‧ S i s t e m i M u l t i m e d i a l i I I ‧ 2 0 0 4 / 0 5 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a - 42/40
Scarica

ppt