Você está na página 1de 4

Ray Tracing - Refracción

Computación Gráfica
Universidad Católica del Uruguay
November 7, 2015

1 Intersección
1 I n t e r i n t e r s e c t a r ( Vec3 O, D, f l o a t lamb min , lamb max )
2 {
3 Inter inter
4 i n t e r . e = NULL
5 i n t e r . lamb = I n f
6 foreach e in e s f e r a s
7 {
8 /∗ R e s o l v e r e c u a c i o n de segundo grado ∗/
9 ( lamb1 , lamb2 ) = i n t e r e s f (O, D, e )
10 i f ( lamb1 > lamb min && lamb1 < lamb max && lamb1 < i n t e r . lamb )
11 {
12 inter . e = e
13 i n t e r . lamb = lamb1
14 }
15 i f ( lamb2 > lamb min && lamb2 < lamb max && lamb2 < i n t e r . lamb )
16 {
17 inter . e = e
18 i n t e r . lamb = lamb2
19 }
20 }
21 return inter
22 }

1
2 Calcular Rayo refractado
1 // v a r i a b l e s g l o b a l e s :
2 i n d i c e a c t u a l =1 // empezamos en e l a i r e
3 l i s t a r e f r = [ ] // aun no i n t e r s e c t a m o s nada
4 Vec3 c a l c u l a r r e f r a c t a d o ( Vec3 D, N, E s f e r a ∗ e )
5 {
6 Vec3 RR
7 i f ( ! l i s t a r e f r . contiene ( e ) )
8 {
9 // entramos a l o b j e t o
10 RR = l e y s n e l l (D, N, i n d i c e a c t u a l , e . mat . i n d i c e r e f r )
11 l i s t a r e f r . push ( e )
12 i n d i c e a c t u a l = e . mat . i n d i c e r e f r
13 }
14 else
15 {
16 // s a l i m o s d e l o b j e t o
17 l i s t a r e f r . sacar ( e )
18 i f ( ! l i s t a r e f r . vacia () )
19 indice nuevo = l i s t a r e f r . primero ( ) . i n d i c e r e f r
20 else
21 indice nuevo = 1
22 RR = l e y s n e l l (D, N, i n d i c e a c t u a l , i n d i c e n u e v o )
23 indice actual = indice nuevo
24 }
25 r e t u r n RR
26 }

2
3 Computar el Rayo
1 C o l o r s e g u i r r a y o ( Vec3 O, D, f l a o t lamb min , lamb max , i n t l i m r e c )
2 {
3 Color r e s = ( 0 , 0 , 0 )
4 I n t e r i n t e r = i n t e r s e c t a r (O, D, lamb min , lamb max )
5 i f ( i n t e r . e != NULL)
6 {
7 Vec3 P = O + i n t e r . lamb ∗ D
8 Vec3 N = P − i n t e r . e . c e n t r o
9 Vec3 V = −D
10 r e s = i n t e r . e . mat . cd ∗ IA
11 foreach luz in luces
12 {
13 Vec3 L = l u z . pos − P
14 I n t e r sombra = i n t e r s e c t a r (P , L , eps , 1 )
15 i f ( sombra . e == NULL)
16 r e s += l a m b e r t (P , N, L , V, i n t e r . e . mat )
17 }
18 i f ( l i m r e c > 0 && i n t e r . e . mat . c o e f r e f l > 0
19 && i n d i c e a c t u a l == 0 ) // r e f l e j o s o l o en e l a i r e
20 {
21 RV = 2 ∗ N ∗ <N, V> − V
22 C o l o r c r = s e g u i r r a y o (P , RV, eps , i n f , l i m r e c −1)
23 r e s = r e s ∗ ( 1 − i n t e r . e . mat . c o e f r e f l ) +
24 c r ∗ ( i n t e r . e . mat . c o e f r e f l )
25 }
26 i f ( l i m r e c > 0 && i n t e r . e . mat . c o e f r e f r > 0 )
27 {
28 RR = c a l c u l a r r e f r a c t a d o (D, N, e )
29 C o l o r c r = s e g u i r r a y o (P , RV, eps , i n f , l i m r e c −1)
30 r e s = r e s ∗ ( 1 − i n t e r . e . mat . c o e f r e f r ) +
31 c r ∗ ( i n t e r . e . mat . c o e f r e f r )
32 }
33 }
34 return res
35 }

3
4 Main
1 i n t main ( )
2 {
3 Vec3 O = cam pos
4 f o r (X = −cw / 2 ; X < cw / 2 ; X++)
5 {
6 f o r (Y = −ch / 2 ; Y < ch / 2 ; Y++)
7 {
8 X ’ = X ∗ vw/cw
9 Y ’ = Y ∗ vh/ ch
10 Z’ = d
11 Vec3 D = (X ’ ,Y ’ , Z ’ )
12 C o l o r c = s e g u i r r a y o (O, D, 1 , i n f , 4 )
13 p u t p i x e l (X, Y, c )
14 }
15 }
16 }

Você também pode gostar