• Non ci sono risultati.

Computer Graphics

N/A
N/A
Protected

Academic year: 2021

Condividi "Computer Graphics"

Copied!
11
0
0

Testo completo

(1)

Computer Graphics

Marco Tarini Università dell’Insubria

Facoltà di Scienze MFN di Varese Corso di Laurea in Informatica Anno Accademico 2011/12

Lezione 13:

Lezione 13:

Lezione 13:

Lezione 13: realismo a buon prezzo:

il texture Mapping

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

Memoria RAM nelle schede grafiche

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 (RGBA) Screen buffer 2 (RGBA) Depth 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

Texture RAM

co m pu ta zi on i pe r f ra m m en to

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

Concetto

• Tessiture:

– una memoria RAM a bordo della scheda video – si può accedere dal fragment shader

• (recentemente anche dal vertex shader) – solo lettura!

– condivisa

– specifiche modalità di accesso

• locazione: locazione 2D (o anche D o 3D)

• interpolazione fra valori vicini

• prefiltering

• …

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: paradigma di memoria

• Nelle operazioni per frammento si può accedere ad una RAM apposita

– la Texture RAM

– strutturata (paginata) in un insieme di Textures ("tessiture") – da qualche tempo, possibile accedere anche nelle

operazioni x vertice

• Ogni tessitura è un array uniforme (*) 1D, 2D o 3D

di Texels (lett: elementi (campioni) di tessitura) (*) cioè: ogni texel è dello stesso tipo

Caso più comune

• Tessiture 2D 2D 2D 2D

• Ogni texel memorizza attributi attributi attributi attributi

di un intorno di un punto della superficie

– (tipo simile a quelli che abbiamo associato ai vertici) – utile quando i valori degli attributi variano

più repentinamente sulla superficie

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 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

Texels

• Tipici esempi di texels:

– ogni texel un colore (componenti: R-G-B, o R-G-B-A)

• la tessitura è una "color-map"

– ogni texel una componente alpha

• la tessitura è una "alpha-map"

– ogni texel una normale (componenti: X-Y-Z)

• la tessitura è una "normal-map" o "bump-map“ (in che spazio?)

– ogni texel contiene un valore di specularità

• la tessitura è una “specular-map"

– ogni texel contiene un valore di altezza

• la tessitura è una "displacement-map"

– ...

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

Tipica applicazione (quella storica):

rimappare immagini sulla geometria

geometria 3D (insieme di quadrilateri)

+

RGB texture 2D (color-map)

=

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

Altro esempio di color-map

+ =

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

Altri esempi di color-map

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: Storia

• 1974 introdotto da Ed Catmull

– nella sua Phd Thesis

• Solo nel 1992 (!) si ha text. mapping hardware

– Silicon Graphics RealityEngine

• Dal 92 a oggi:

rapido aumento della diffusione

– strada intrapresa soprattutto da low end graphic boards

• Oggi:

una delle fondamentali primitive di rendering

– la principale tecnica image based di rendering

Ed Catmull

(MEGA-MEGA- GURU)

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

Notazione

Texture 2D

s t

texel

Texture Space

(o "spazio parametrico" o "spazio s-t" o "spazio u-v")

Una Texutre e' definita nella regione [0,1] x [0,1]

dello "spazio parametrico"

es: 512 texels

es: 1024 texels

1.0 1.0

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

Due notazioni

Texture 2D Texture Space

in OpenGL (s,t)

s t

1.0

1.0 u

v

1.0

1.0

Texture Space in DirectX

(u,v)

(0,0)

(0,0)

(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 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 Space t s

Texture Mapping

• Ad vertice (di ogni triangolo) assegno le sue coordinate s,t nello spazio tessitura

Screen Space

x

0

,y

0

x

2

,y

2

x

1

,y

1

s

0

,t

0

s

1

,t

1

s

2

,t

2

posizione del 1mo vertice

attributi del 1mo vertice

s0,t0 s1,t1

s2,t2

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

• Così in pratica definisco un mapping fra il triangolo 3D e un triangolo di tessitura

Texture Space Screen Space

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

• Ogni frammento

ha le sue coordinate s,t nello spazio tessitura

Texture Space Screen Space

texture look-up

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

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 Screen Screen Screen buffer buffer buffer 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

Texture RAM co m pu ta zi on i pe r f ra m m en to

interpolo coordinate

texture

coordinate texture interpolate compreso:

coordinate texture

(per vertice!)

texture

look-up

interpolo coordinate

texture

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

Problema:

interpolazione lineare coordinate texture

• Non vale per la proiezione prospettica!

– era solo una approssimazione

– andava bene quando interpolavamo colori, normali – non va bene quando interpoliamo coordinate texture...

V

1

V

2

V

3

p

R 3 R 2

f(p)

f( v

1

)

f( v

2

) f( v

3

)

proiezione ffff

p ha coord.

baricentriche a,b,c nel triangolo v

1

v

2

v

3

f(p) ha 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 1 / 1 2 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a

Problema:

interpolazione lineare coordinate texture

• Esempio:

s t

1 1

u,v= (1,0) u

1

,v

1

= (1,1)

u

1

,v

1

= (0,1)

u

1

,v

1

= (0,0)

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

Problema:

interpolazione lineare coordinate texture

• Esempio:

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

Soluzione:

Interpolaz Attr. con Correzione Prospettica

• p ha coordinate baricentriche c 0 c 1 c 2

V

0

V

2

V

1

A

0

,B

0

...

A

1

,B

1

...

A

2

,B

2

...

p

p = c 0 v 0 + c 1 v 1 + c 2 v 2

attributi di p:

(senza “correzione prospettica”)

A p = c 0 A 0 + c 1 A 1 + c 2 A 2

B p = c 0 B 0 + c 1 B 1 + c 2 B 2

= ( x

0

, y

0

, z

0

, w

0

)

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

Soluzione:

Interpolaz Attr. con Correzione Prospettica

• p ha coordinate baricentriche c 0 c 1 c 2

V

0

V

2

V

1

A

0

,B

0

...

A

1

,B

1

...

A

2

,B

2

...

p

p = c 0 v 0 + c 1 v 1 + c 2 v 2

attributi di p:

(con “correzione prospettica”)

A p = c 0 A 0 + c 1 A 1 + c 2 A 2

A 0

w 0

A 1

w 1

A 2

w 2

A p =

A p = c 0 A 1 0 + c 1 A 1 + c 2 A 2

w 0

w 1 1

w 1 2

= ( x

0

, y

0

, z

0

, w

0

)

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

Soluzione:

Interpolaz Attr. con Correzione Prospettica

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 Screen Screen Screen buffer buffer buffer 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

attributo

originale A applico transofrm.

poi calcolo:

A' = A / w w' = 1 / w e

c 0 + c 1 + c 2

A 0

w 0

A 1

w 1

A 2

w 2

A p = w 1 0

w 1 1

w 1 2 interpolo

A' e w' attributo finale del frammento:

A' / w' c 0 + c 1 + c 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 1 / 1 2 ‧ U n i v e r s i t à d e l l ’ I n s u b r i a

Interpolazione Coordinate s e t con Correzione Prospettica

• Senza • Con

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

La tessitura va caricata in Texture RAM

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 Screen Screen Screen buffer buffer buffer 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

Texture RAM

L O A D

(5)

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

La tessitura va caricata

1. Da disco a memoria RAM (main)

• (sulla scheda madre)

2. Da memoria RAM main a Texture RAM

• (on board dell'HW grafico)

Operazione 2 piuttosto lenta (1, peggio ancora)

Non fare una volta per frame!

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

La tessitura va caricata: OpenGL

• esempio:

glEnable(GL_TEXTURE_2D);

glBindTexture (GL_TEXTURE_2D, ID);

glTexImage2D ( GL_TEXTURE_2D, 0, // mipmapping GL_RGB, // formato interno imageWidth, imageHeight, 0, // bordo

GL_RGB, // formato nella RAM GL_UNSIGNED_BYTE,

imageData);

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

Come si assegnano le coordinate texture ai vertici?

Fr am m en ti & va ry in g & va ry in g & va ry in g & va ry in g 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 Screen Screen Screen buffer buffer buffer buffer Ve rti ci p or ie tta ti & va ry in g & va ry in g & va ry in g & va ry in g 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

Texture RAM co m pu ta zi on i pe r f ra m m en to

interpolo coordinate

texture

coordinate texture interpolate compreso:

coordinate texture

(per vertice!)

texture

look-up

compreso:

coordinate texture

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

Come si assegnano le coordinate texture ai vertici?

• Due usi possibili del texture map:

– Computare le coord. texture al volo

• durante il rendering (per vertice)…

– Precomputarle

• e mandarle come attributo x vertice

• (es: salvarle insieme alla mesh)

Dipende dall’applicazione!

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

Problema difficile: u-v mapping

• Assegnare una coppia di coordinate texutres ad ogni vertice della mesh

– In preprocessing

s t

s t

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

Problema difficile: “u-v mapping” (“u-v” == “s-t”)

fatto a mano,

oppure automatizzato

(6)

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

In OpenGL

• Mandiamo le coord texture come ogni altro attributo

• In GLSL:

– sinomini delle componenti di un vec4:

r g b a x v z w

s t r q

coordinate affini xyz di un punto

componenti rosso verde blu (e alpha) di un colore coordiante texture affini s t (e r,q) (spazio parametrico)

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

Come si assegnano le coordinate texture ai vertici?

Fr am m en ti & va ry in g & va ry in g & va ry in g & va ry in g 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 Screen Screen Screen buffer buffer buffer buffer Ve rti ci p or ie tta ti & va ry in g & va ry in g & va ry in g & va ry in g 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

Texture RAM co m pu ta zi on i pe r f ra m m en to

interpolo coordinate

texture

coordinate texture interpolate

texture

look-up

coordinate texture

(eventualmente

trasformate)

compreso:

coordinate texture

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

Come si assegnano le coordinate texture ai vertici?

• Due tipi di usi del texture map:

– Assegnarle le coordinate textures al volo

• durante il rendering…

– Precomputarle

• (e salvarle insieme alla mesh)

esempio 1:

oggetti con pattern regolari (a quadretti…)

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

Come si assegnano le coordinate texture ai vertici?

Fr am m en ti & va ry in g & va ry in g & va ry in g & va ry in g 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 Screen Screen Screen buffer buffer buffer buffer Ve rti ci p or ie tta ti & va ry in g & va ry in g & va ry in g & va ry in g 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

Texture RAM co m pu ta zi on i pe r f ra m m en to

interpolo coordinate

texture

coordinate texture interpolate

texture

look-up

coordinate texture produco

coordinate texture a partire

dalla posizione

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

Coordinate texture create proceduralmente

• Concetto: da ( x,y,z ) a ( s,t ) – es. linearmente

• In coordinate oggetto, mondo oppure vista – (cioè prima o dopo le trasformazioni)

• Esempi:

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

Create automaticamente

• Anche 1D

1D texture!

(7)

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

Come si assegnano le coordinate texture ai vertici?

• Due classi di soluzioni:

– Assegnarle le coordinate textures al volo

• durante il rendering…

– Precomputarle

• (e salvarle insieme alla mesh)

esempio 2:

envionment mapping

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

Environment mapping: sferico

Environment map:

una tessitura che memorizza

il colore dell’ambiente “riflesso”

da ogni normale della semisfera.

Come coordinata tessitura, basta usare la normale (trasformata)!

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

Environment mapping: sferico

simula oggetto a specchio che riflette uno sfondo lontano

simula un materiale complesso (a condizioni di luce fisse)

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

Environment mapping: cubico

front right back below above left

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

Environment mapping: cubico

Fr am m en ti & va ry in g & va ry in g & va ry in g & va ry in g 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 Screen Screen Screen buffer buffer buffer buffer Ve rti ci p or ie tta ti & va ry in g & va ry in g & va ry in g & va ry in g 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

Texture RAM co m pu ta zi on i pe r f ra m m en to

interpolo coordinate texture 3D

coordinate texture 3D interpolate

proietto sul cubo, accedo alla

faccia corrispondente calcolo

coordinate texture 3D

[-1,+1] x [-1,+1] x [-1,+1]

come raggio riflesso raggio riflesso raggio riflesso raggio riflesso (dir. di vista riflessa dalla norm.)

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

Environment mapping: cubico

front right back

below

above

left

(8)

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

Il texture look-up

Fr am m en ti & va ry in g & va ry in g & va ry in g & va ry in g 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 Screen Screen Screen buffer buffer buffer buffer Ve rti ci p or ie tta ti & va ry in g & va ry in g & va ry in g & va ry in g 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

Texture RAM co m pu ta zi on i pe r f ra m m en to

interpolo coordinate

texture

coordinate texture interpolate compreso:

coordinate texture

(per vertice!)

texture

look-up

texture

look-up

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

u Texture Look-up:

fuori dai bordi: modo "clamp"

if (u<0) u←0; if (u>1) u←1;

if (v<0) v←0; if (v>1) v←1;

1 1

v

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 Look-up:

fuori dai bordi: modo "repeat"

u v

1 1

u ← u – [ u ] v ← v – [ v ]

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

Tessiture ripetute ripetute ripetute (tiled textures) ripetute

• Tipico utilizzo:

Molto efficiente in spazio!

Nota: deve essere un’immagine “TILEABLE”

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

In OpenGL

glTexParameteri(

GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );

glTexParameteri(

GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );

o

nota:

setto per u e v separatemente

esempio: repeat sulla uuuu e clamp sulla vvvv sono parametri della tessitura. Ogni tessitura caricata in memoria ha i

propri parametri.

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

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 Screen Screen Screen buffer buffer buffer 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

Texture RAM co m pu ta zi on i pe r f ra m m en to

interpolo coordinate

texture

coordinate texture interpolate compreso:

coordinate texture

(per vertice!)

texture

look-up

texture

look-up

(9)

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 Look-up

• Un frammento ha coordinate non intere (in texels)

Texture Space Screen Space

texture look-up

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

pixel

Texture Look-up

Texture Space Screen Space

pixel texel

un pixel = meno di un texel

un pixel = più di un texel

minification magnification

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

0.5 1.5 2.5 3.5 4.5 5.5 6.5 0.5

1.5 2.5 3.5 4.5 5.5 6.5 7.5

Caso Magnification

u v

Soluzione 1:

prendo il texel in cui casco (cioè il texel il cui centro è più vicino alle coordinate u,v

del frammento) equivale ad arrotondare

le coordinate texel ad interi

"Nearest Filtering"

7.5

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

Caso Magnification

texture 128x128

Nearest Filtering: risutlato visivo

"si vedono i texel !"

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

0.5 1.5 2.5 3.5 4.5 5.5 6.5 0.5

1.5 2.5 3.5 4.5 5.5 6.5 7.5

Caso Magnification

u v

Soluzione 2:

Medio il valore dei quattro texel più vicini

Interpolazione Bilineare

7.5 Ripasso:

interpolazione lineare (1D) interpolazione bi-lineare (2D)

Formule...

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

Caso Magnification

texture 128x128

Bilinear Interpolation: risutlato visivo

(10)

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

Caso Magnification

• Modo Nearest:

– si vedono i texel

– va bene se i bordi fra i texel sono utili – più veloce

• Modo Interpolazione Bilineare – di solito qualità migliore – può essere più lento

– rischia di avere un effetto "sfuocato"

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

Caso Minification

Nearest Filtering Bilinear interpolation

non risolve il problema

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

Caso Minification: MIP-mapping

MIP-map level 0

MIP-map level 1

MIP-map level 2

MIP-map level 3

MIP-map level 4 (un solo texel)

MIP-mapping: "Multum In Parvo"

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

Mipmap Math

• Definiamo un fattore di scala, ρ=texels/pixel – ρ è il massimo fra ρ x e ρ y

– può variare entro lo stesso triangolo

– può essere derivato dalle matrici di trasformazione – e calcolato nei vertici, interpolato nei frammenti

• il livello di mipmap da utilizzare è: log 2 ρ

– livello 0 = massima risoluzione – se livello<0 cosa significa?

– nota: il livello non è necess. un numero intero

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

Caso Minification: MIP-mapping

Bilinear interpolation

non risolve il problema

MIP-mapping

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

Caso Minification: MIP-mapping

0 1 2 3 4 5

altro

esempio

(11)

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

In OpenGL

glTexParameteri(

GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

glTexParameteri(

GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );

oppure

decidere che tipo di magnificazione usare:

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

In OpenGL

glTexParameteri(

GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, modo );

modo = GL_NEAREST GL_LINEAR

GL_NEAREST_MIPMAP_NEAREST GL_LINEAR_MIPMAP_NEAREST GL_NEAREST_MIPMAP_LINEAR GL_LINEAR_MIPMAP_LINEAR

dove

decidere che tipo di minifacazione usare:

interpolazione trilineare

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

In OpenGL

• Caricare sulla scheda i vari livelli di mipmapping.

– uno per uno:

glTexImage2D ( GL_TEXTURE_2D,

i, // MIP-map level GL_RGB, // formato interno imageWidth, imageHeight, 0, // bordo

GL_RGB, // formato nella RAM GL_UNSIGNED_BYTE,

imageData);

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

In OpenGL

• Caricare sulla scheda i vari livelli di mipmapping.

– automaticamente:

abilitare: GL_GENERATE_MIPMAP

Riferimenti

Documenti correlati

[r]

[r]

[r]

Flat shading Goraund shading Phong Shading (*) talvolta (ma impropriamente) detto: per-pixel lighting. quando si usano normali

[r]

di vista riflessa dalla norm.). Environment

[r]

[r]