Projective textures

advertisement
Projective Texture
Projective Texture
Texture projected on a scene as if by a projector
Can also be used to generate spot light and shadows
Spring 2006
2
Idea
Generate texture coordinates based on
the projector coordinate system
Spring 2006
3
In Projector Space …
yp
xp
Whatever is in the projector frustum has clip coord. of
[-1,1]x[-1,1]xZ … (Z is irrelevant)
Convert [-1,1] to [0,1] to become the texture
coordinates
Spring 2006
4
Review: Pipeline
Spring 2006
5
Pipeline Review
Modelview
Matrix
Model
Transform
Viewing
Transform
world
coordinates
Fall 2009 revised
6
Review: Texgen (Object_linear)
If the texture generation function is
GL_OBJECT_LINEAR, the function
g = p1 * xo + p2 * yo + p3 * zo + p4 * wo
is used, where g is the value computed for
the coordinate; p1, p2, p3, and p4 are the
four values supplied in params; and xo, yo, zo,
and wo are the object coordinates of the
vertex.
Spring 2006
7
Review: Texgen (Eye_linear)
If the texture generation function is
GL_EYE_LINEAR, the function
g =p1' * xe + p2' * ye + p3' * ze + p4' * we
is used, where
(p1' p2' p3' p4') = (p1 p2 p3 p4) * M -1
and xe, ye, ze, and we are the eye coordinates
of the vertex, p1, p2, p3, and p4 are the values
supplied in params, and M is the modelview
matrix when glTexGen is invoked.
Spring 2006
8
Review: Texgen Matrix
Concatenate texgen planes into a 4-by-4
matrix
Object-linear
 s   p1
t  
 
r  
  
q  
p2
p3
p4   v x 
 v 
 y 
  vz 
 
 vw  object
To
Eye-linear
Spring 2006
9
Coordinate Transformation
From Object to Projector:
v p  Vp Mvo
From Eye to Projector:
v p  VpV 1ve
Clip coordinate of Projector Space:
c p  Ppv p
M: model matrix
Vp: view matrix to projector space
V : view matrix to eye
Spring 2006
Pp: projection matrix to projector space
10
Scale to Texture Coordinates
Scale-and-bias matrix (S)
Clip Coordinate
[-1,1]
 12 0 0 12 
0 1 0 1 
2
2

0 0 12 12 


0 0 0 1 
Texture Coordinate
[0,1]
Note: we only care about [s,t] (and then [x,y]). Hence,
frustum clipping on z is of no importance
Spring 2006
11
Use Texgen for Coord. Transform1
s
t 
   SPpV p M



r 
To
 
q 
 vx 
v 
 y
 vz 
 
vw  object
v p  Vp Mvo
c p  Ppv p
s, t, r, q
T
 Scp
Not explicitly
available in
OpenGL!
Need to keep track of model matrix M
Texgen planes To should be updated
whenever M changes (model moves)
Spring 2006
12
Use Texgen for Coord. Transform2
 vx 
s
v 
t 
   SPpV p V 1  y 
  vz 
r 
Te
 
 
q 
vw  eye
v p  VpV 1ve
c p  Ppv p
s, t, r, q
T
 Scp
The texture coordinate generated is A’xe + B’ye + C’ze +
D’we where (A’,B’,C’,D’) is (A,B,C,D)M−1, M is the
modelview matrix when glTexGen is invoked. Thus,
simply set the modelview matrix to contain only the
view matrix
Spring 2006
13
Alternatively,
For the rest of transformations, set the
texture matrix instead
 s   12 0 0 12 
s
 t   0 1 0 1 
t 
2
2
 
 
P
V
p
p
 r   0 0 12 12 
r 

  
 
q
0 0 0 1
q 



Tex coords
after texture
transformation
Spring 2006
texture matrix
We can use OpenGL matrix
facility for matrix
multiplication
 vx 
 s  1

v 
t   1

 
 Ve1  y 
 vz 
r  
1 
 
  

vw  eye
1
q  



Tex coords
after texgen
eye plane eqns
14
Summary
Texgen: eye-linear
When texgen is called, set modelview to
contain Ve only
Specify the other transforms (SPpVp) as
texture matrix
s
t 
 
r 
 
q 
Spring 2006
 12 0 0 12 
 vx 
0 1 0 1 
v 
1  y 
2
2

P
V
V
0 0 12 12  p p e  vz 


 
vw  eye
0 0 0 1




texture matrix
15
Summary
Spring 2006
need to generate the texture coordinate
according to the projector coordinate
system
16
Demo (myprojspot - blended)
Scene: textured
plane and
untextured teapot,
subject to a textured
spotlight
Two rendering
passes blended
together (with
polygon offset to
resolve z-fighting)
Spring 2006
17
Demo
Polygon offset is
critical to resolve
z-fighting!
Projective Pass
Regular Pass
GL_ONE
GL_ONE
Spring 2006
Blended
18
Implementation Note
Border pixels

The projected
texture needs to be
padded with black
boundary pixels so
that the clamped
texture appears Ok
Spring 2006
19
Multitexture Implementation
Rather than using two blended passes,
use a single (multitextured) pass
Assign a separate texture unit for spot
light texture


Projective texture coordinate set by texgen
Texture environment set to GL_ADD
 Base fragment/texture + spot light RGB:
Spring 2006
equivalent to one/one blending
 The padding is black (0,0,0); GL_ADD plays no
role.
20
Projective Texture Shadow
Esp. good for non-planar shadow receivers
Spring 2006
21
Projective Shadow Texture
Generate a shadow texture from light
coordinate system

Render unlit occluder only
spot or
point light
Render the shadow receiver with the
projected shadow; then render the occluder
Note the shadow texture is padded with
white pixels. The texture environment used is
GL_MODULATE (white (1,1,1) plays no role)
Spring 2006
22
This is normal!
If you see something like this,
the artifact comes from
projective texture clamping: the
shadow touches the texture
border (not enough padding). It
can be alleviated by increasing
the fov or raising the light
position
Frustum Display
Shows the
location/orientation
of the spotlight
Idea
Projector is set by “gluLookAt” API

Eye, center, up
Vertices of the viewing frustum have
clip coordinates of [-1,1]3
Need to know their corresponding world
coordinates to render
Spring 2006
25
s -f
gluLookAt (eye, center, up)
s
s
s
LT
Change of Basis
 world x   s[0] u[0]  f [0] eyex 
 world    s[1] u[1]  f [1]  eye 
y


 y 
 world z   s[2] u[2]  f [2] eyez 
xworld  Lxeye
xeye  L1 xworld  LT xworld
Spring 2006
gluLookAt is equivalent to:
M
T
26
Modelview
Matrix
Model
Transform
Viewing
Transform
MT
world
coordinates
[-1,1]3
[clip] = [Proj] [MT] [world]
[world] = [MT]-1[Proj]-1[clip]
= T-1 M-1 P-1 [clip]
Spring 2006
27
glFrustum (l,r,b,t,n,f)
n,f must be positive
n cannot be zero
 r l
r l
 1  2 n  2 n   nl   l 
 1  t b  t b   b  b 
2n
2n 
R 1    
 n  
 1   1   1 n 
   f n f  n   1   
 1   2 fn   n  1 
1  r2nl  r2nl   nr   rfn 
1  t b  t b   t   tf 
n
2n
2n 
R 1    
  n 
1   1   1  f 
   n f  f n   1   
1  2 fn   f   1 
y
(1,1,1)
rf/n
z
x (-1,-1,-1)
f
r
n
Spring 2006
28
Implementation
Spring 2006
29
Download