0% found this document useful (0 votes)
62 views

Bump Mapping. Displacement Mapping. Deferred Shading

This document discusses local vertex coordinate systems and normal mapping techniques. It begins by explaining that normal vectors are defined in a coordinate system associated with each vertex (TBN system) and transformations are needed when sharing textures across faces. It then provides details on the TBN system and converting between local, texture, and world coordinate systems. Methods for bump, displacement, and parallax mapping are described along with algorithms for subdivision and displacement mapping.

Uploaded by

Stefan Preda
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views

Bump Mapping. Displacement Mapping. Deferred Shading

This document discusses local vertex coordinate systems and normal mapping techniques. It begins by explaining that normal vectors are defined in a coordinate system associated with each vertex (TBN system) and transformations are needed when sharing textures across faces. It then provides details on the TBN system and converting between local, texture, and world coordinate systems. Methods for bump, displacement, and parallax mapping are described along with algorithms for subdivision and displacement mapping.

Uploaded by

Stefan Preda
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

Sistemul de coordonate local varfului

Perete vertical
Harta normalelor

Folosirea aceleiasi texturi pentru


fiecare fata a cubuluinecesita
transformarea normalelor
Normalele se definesc intr-un sistem
de coordonate asociat fiecarui varf

Sistemul de coordonate local varfului

Sistemul de coordonate: TBN


T tangenta

N normala
B binormala - B = N x T

Sistemul de coordonate local varfului

Informatiile pentru iluminare sunt definite in sistemul


de coordonate obiect/real/vizualizare
Informatiile de deformare sunt definite in sistemul de
coordonate local suprafetei (local varfului)
Trebuie facuta conversia dintr-un sistem in altul
3

Transformarea sistemului de coordonate

Sistem local -> sistemul TBN


x' Tx Bx N x x
y' T B N y
y y y
z' Tz Bz N z z

Vertex shader
M
poz_lumina
poz_observator

P
N, T
texCoord

uniformi
Vertex shader

P= P
texCoord (N)
Raza de lumina (L) in spatiul TBN
Culoare = componenta difuza
5

Bump mapping - Vertex shader


in vec3 T, N;
in vec4 position;
In vec3 in_texcoord;
uniform mat4 M, V, P;
out vec3 texcoord;
out vec3 L;
void main()

{
gl_Position = P*V*M*position;
texcoord = in_texcoord;
position = pos.xyz;

vec3 B = cross(T, N);


mat3 TBNMatrix = mat3(T, B, N);
L = lightposition.xyz position;
L*= TBNMatrix;

Bump mapping - Fragment shader

texcoord(N)
Raza de lumina (L)
variabili

Harta normalelor

uniformi
Fragment shader

Culoare fragment (lumina difuza = N.L)


7

Bump mapping - Fragment shader


uniform sampler2D normalMap;

Componenta difuza

in vec3 L;
in vec3 texcoord;
out vec4 frag_color;
void main()
{
vec3 Nn = texture(normalMap, texcoord.st).rgb * 2.0 - 1.0;
vec3 Ln = normalize(L);
frag_color = dot(Nn, Ln);
}
8

Bump mapping pe o suprafata


definita parametric
Suprafata definita prin puncte P(s,t):

Normala in punctul P(s,t) este:

Bump mapping - tor

Ecuatiile torului:

10

Bump mapping - tor

11

Bump mapping - tor

12

Displacement mapping

Bump mapping

Displacement
mapping

13

Displacement mapping

Similara cu bump mapping

se modifica geometria

suprafata finala

suprafata initiala

14

Divizare Catmull-Clark

Sunt generate noi puncte


Face points
Edge points
Vertex points
15

Divizare Catmull-Clark
Face points
1
F1 (V1 V2 V3 V4 )
4

V4

V1
F1
V2

V6
F2

V3

V5

16

Divizare Catmull-Clark

Edge points

V3 V4 F1 F2
e1
4

V4

V1
V2

F1
e2

e1

V3
e3

V6
F2
e4

V5

17

Divizare Catmull-Clark
V4

V1
V2

F1
e2
F3

e1
V3
e3

V6
F2
e4

V5

F4

Vertex points

1
V3 ' [8V3 (e1 e2 e3 e4 ) ( F1 F2 F3 F4 )]
16

18

Divizare Catmull-Clark
Face points
1 n
f vi
n 1
Edge points

v1 v2 f1 f 2
e
4
Vertex points

n2
1
vi 1
vi 2
n
n

1
j e j n2

19

Displacement mapping

Randare: pentru fiecare parte a geometriei


determina pixelul corespunzator din imagine care
trebuie mapat.
Deplasarea varfurilor: pentru un anumit pixel din
imagine determina partea din geometrie ce este
vizibila. Aceasta tehnica este folosita in algoritmul
Ray Tracing
Problema deplasarii varfurilor vazuta ca o
problema de Ray- Tracing

20

Deplasarea varfurilor

esantionare uniforma

se pierd importante parti din geometrie


21

Deplasarea varfurilor

Harta de inaltimi nu poate fi interogata la


intervale fixate (pot rezulta artefacte)
Trebuie determinata pozitia punctelor de
esantionare astfel incat sa nu se piarda din
punctele de pe suprafata
Se defineste harta distantelor pentru
suprafata
22

Deplasarea varfurilor

Pentru fiecare punct p din spatiul textura si


o suprafata S se defineste functia:

dist(p, S) = min{d(p, q) : q in S}.

dist(p, S) este distanta minima de la p la


cel mai apropiat punct de pe suprafata S

23

Deplasarea varfurilor aplicata pe un plan

Raza cu originea in p0

si vectorul de directie d

se defineste un nou punct:

p1 = p0 + dist( p0, S) d

daca p0 este in afara suprafetei, atunci si p1 este in


afara suprafetei
24

Deplasarea varfurilor aplicata pe un plan

Raza porneste din p0.


Se determina distanta pana la cel mai apropiat
punct de pe suprafata

Trasarea unei sfere S0

cu centrul in p0 pana intersecteaza


suprafata punctul p1
(intersectia dintre raza si sfera).

Se repeta acest proces,

generand punctele p2..p4.

p4 reprezinta intersectia cu suprafata

algoritmul converge in 4 pasi

25

Rim lighting (back-lighting)

Produce o margine
luminoasa in jurul
conturului unui obiect
Sursa se afla pozitionata
in spatele obiectului si
nu influenteaza
iluminarea fetelor
acestuia
26

Rim lighting (back-lighting)

Sursa de lumina este


plasata in spatele
obiectului de interes =>
Obiectul de interes este
plasat intre camera si
sursa de lumina

27

Rim lighting (back-lighting)

Crim culoarea luminii back


Prim puterea luminii back
28

Rim lighting Vertex shader


in vec4 position;
in vec3 normal;
uniform mat4 MV, V, P;

void main(void)
{
vec4 PW = MV * position;
N = mat3(MV) * normal;

out vec3 N;

L = light_pos - PW.xyz;
V = -PW.xyz;

out vec3 L;
out vec3 V;
uniform vec3 light_pos;

gl_Position = P * PW;
}

29

Rim lighting Fragment shader


out vec4 color;

vec3 calculate_rim(vec3 N, vec3 V)

{
float f = 1.0 - dot(N, V);

in vec3 N;
in vec3 L;

f = smoothstep(0.0, 1.0, f);

in vec3 V;

f = pow(f, rim_power);

uniform vec3 rim_color;


uniform float rim_power;

return f * rim_color;
}

30

Rim lighting Fragment shader


void main(void)
{
N = normalize(N);
L = normalize(L);
V = normalize(V);
vec3 R = reflect(L, N);

vec3 diffuse = max(dot(N, L), 0.0) * kd;


vec3 specular = pow(max(dot(R, V), 0.0), n) * ks;
vec3 rim = calculate_rim(N, V);
color = vec4(diffuse + specular + rim, 1.0);
}
31

Reducerea detaliilor de iluminare

Lumina difuza, speculara

Componenta difuza este reprezentata prin doua valori:


una pentru zonele luminate si alta pentru zonele
intunecate
Componenta speculara este reprezentata printr-o singura
culoare cu intensitatea suficient de mare
Conturul obiectelor trebuie pus in evidenta
32

Componenta difuza

Functie continua

Functie treapta

difuza = N.L

33

Componenta speculara. Contur

Componenta speculara

Contur

Un punct este vizibil daca N .V > 0, altfel ascuns.


Punctele pentru care N .V 0 reprezinta tranzitii de
la puncte vizibile -> puncte nevizibile (pot fi
considerate puncte de contur).

34

Toon shading
Fragment shader
in vec3 L, N;
in vec4 position; //spatiul de vizualizare
out vec4 Frag_Color;

void main() {
vec3 n = normalize(N);
vec3 V = normalize(-position.xyz); //spatiul de vizualizare
vec3 l = normalize(L);
vec3 h = normalize(l + V);
35

Toon shading
float edgeMask = (dot(V, n) > 0.4) ? 1 : 0;
float specMask = (pow(dot(h, n), nspec) > 0.5)? 1:0;
float difuseMask = (dot(l, n) > 0.4) ? 1 : 0;
color.xyz = edgeMask *
(difuseMask * kd) *
(specMask * ks);
Frag_Color = color;
}

36

Deferred shading

Realizarea iluminarii:
pentru fiecare obiect
pentru fiecare sursa de lumina
randare obiect iluminat
Complexitate: O(nr_obiecte * nr_lumini)

37

Deferred shading

1. Randare obiecte (in G-buffer):

Pozitie
Normala
Culoare
Depth, etc

Normale

Depth

Culoare

38

Deferred shading

2. Pentru fiecare sursa de lumina


randeaza fiecare obiect folosind
proprietatile salvate in G-buffer

39

Deferring shading
1. Pas 1: vertex shader
in vec4 position;

in vec3 in_normal;
in vec2 in_texcoord;

void main(void)
{
mat4 MV = V *M;
gl_Position = P * V * M* position;
ws_coords = (M * position).xyz;
normal = mat3(M) * in_normal;
texcoord = in_texcoord;

out vec3 ws_coords;


out vec3 normal;
out vec2 texcoord;
}
uniform mat4 P, V, M;

40

Deferring shading
1. Pas 1: fragment shader
in vec3 ws_coords;
in vec3 normal;
in vec2 texcoord;

out vec3 WS_coord;


out vec3 color;
out vec3 N;

uniform sampler2D texture;

void main(void)
{
color = texture(texture, texcoord).rgb;
WS_coord = ws_coord;
N = normal;
}
41

Deffered shading
Pas 2: Vertex shader
void main()
{
gl_Position = .;

.
}

42

Deffered shading
Pas 2: Fragment shader
out vec4 color_out;
uniform usampler2D texture_P;

void main(void)
{
fragment_info fragment;

uniform sampler2D texture_N;

vec2 texcoord = gl_FragCoord.xy;


vec3 fragment.ws_coord =
texture(texture_P, texcoord).xyz;
vec3 fragment.normal =
texture(texture_N, texcoord).xyz;
vec3 fragment.color =
texture(texture_C, texcoord).xyz;

uniform sampler2D texture_C;


struct fragment_info
{
vec3 color;

vec3 normal;

color_out = light_fragment(fragment);
}

vec3 ws_coord;
};

43

Deffered shading
vec4 light_fragment(fragment_info fragment){
vec4 result;
vec3 L = light.position - fragment.ws_coord;
L = normalize(L);
vec3 N = normalize(fragment.normal);

vec3 R = reflect(L, N);


float NdotR = max(0.0, dot(N, R));
float NdotL = max(0.0, dot(N, L));
vec3 diffuse_color = light.color * fragment.color * NdotL;
vec3 specular_color = light.color * pow(NdotR, specular_power);
result += vec4(diffuse_color + specular_color, 0.0);
return result;
}

44

You might also like