Docsity

Bump Mapping: Converting Between Eye and Surface Local Coordinates, Exams of Computer Science

Partial preview of the text

Download Bump Mapping: Converting Between Eye and Surface Local Coordinates and more Exams Computer Science in PDF only on Docsity!

Bump Mapping

Mike Bailey

Oregon State University

mjb – April 24, 2007

Oregon State University Computer Graphics

Bump Mapping: Surface Local Coordinate System

mjb – April 24, 2007

Oregon State University Computer Graphics

  • N is the surface normal
  • T is the tangent, which must be consistently oriented from vertex to vertex (glman does this automatically in the Sphere primitive)
  • B is the Binormal

Bump Mapping: A Problem

The problem is that lighting information is in Eye Coordinates, but the bump information is in Surface Local Coordinates!

We need to:We need to:

  1. Figure out how to convert from one to the other, and,
  2. Decide which of light information or bump information gets converted to the other’s coordinate system

mjb – April 24, 2007

Oregon State University Computer Graphics

While we are at it, let’s also rename the Surface Local coordinates to (s,t,h) for (texture_ s , texture_ t , bump_ h eight). This is the same as (B,T,N), but uses terminology that is more bump-specific.

Bump Mapping: Converting Between Coordinate Systems

s B x By Bz x

t T T T

⎧ ⎫ ⎡^ ⎤⎧ ⎫ ⎪ ⎪ ⎢ ⎥⎪ ⎪ ⎨ ⎬ (^) ⎢ ⎥⎨ ⎬

Converting from Eye Coordinates to Surface Local Coordinates:

x y z

x y z

t T T T y

h N N N z

⎪ ⎪ ⎢ ⎥⎪ ⎪ ⎨ ⎬ =⎢ ⎥⎨ ⎬ ⎪ ⎪ (^) ⎢ ⎥⎪ ⎪ ⎩ ⎭ (^) ⎣ ⎦⎩ ⎭

Converting from Surface Local Coordinates to Eye Coordinates:

( The “Orange Book” uses this to convert the light vector to Surface Local Coordinates. )

mjb – April 24, 2007

Oregon State University Computer Graphics

x x x

y y y

z z z

x B T N s

y B T N t

z B T N h

⎧ ⎫ ⎡ ⎤ ⎧ ⎫ ⎪ ⎪ (^) ⎢ ⎥⎪ ⎪ ⎨ ⎬ = ⎨ ⎬ ⎢ ⎥ ⎪ ⎪ (^) ⎢ ⎥⎪ ⎪ ⎩ ⎭ ⎣ ⎦ ⎩ ⎭ ( I prefer to use this one to convert the bump normal to Eye Coordinates. )

Bump Mapping: Establishing the Surface Local Coordinate System

// Produce the transformation from Surface coords to Eye coords: BTNx = vec3( B.x, T.x, N.x ); BTNy = vec3( B.y, T.y, N.y ); BTNz = vec3( B.z, T.z, N.z ); // where the light is coming from: vec3 LightPosition = vec3( LightX, LightY, LightZ ); vec3 ECposition = ( gl_ModelViewMatrix * gl_Vertex ).xyz; DirToLight = normalize( LightPosition - ECposition ); gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;

mjb – April 24, 2007

Oregon State University Computer Graphics

Bump Mapping: Using the Surface-Local-to-Eye-Coordinate Transform

vec ToXyz( vec3 sth ) {{ sth = normalize( sth ); vec3 xyz; xyz.x = dot( BTNx, sth ); xyz.y = dot( BTNy, sth ); xyz.z = dot( BTNz, sth ); return normalize( xyz ); }

mjb – April 24, 2007

Oregon State University Computer Graphics

Bump Mapping: Using the Surface Local Transform

void main() { const float PI = 3.14159265; vec2 st = gl_TexCoord[0].st; // locate the bumps based on (s,t) float Swidth = 1. / BumpDensity; float Theight = 1. / BumpDensity; float numInS = floor( st.s / Swidth ); float numInT = floor( st.t / Theight ); vec2 center; center.s = numInS * Swidth + Swidth/2.; center.t = numInT * Theight + Theight/2.; vec3 stp = st - center; // st’ is now wrt the center of the bump float theta = atan( stp.t, stp.s );

mjb – April 24, 2007

Oregon State University Computer Graphics

Bump Mapping: Using the Surface Local Transform

vec3 normal = ToXyz( vec3( 0., 0., 1. ) ); // un-bumped normal if( abs(stp.s) > Swidth/4. || abs(stp.t) > Theight/4. ) { normal = ToXyz( vec3( 0., 0., 1. ) ); } else { if( PI/4. <= theta && theta <= 3.PI/4. ) { normal = ToXyz( vec3( 0., Height, Theight/4. ) ); } else if( -PI/4. <= theta && theta <= PI/4. ) { normal = ToXyz( vec3( Height, 0., Swidth/4. ) ); } else if( -3.PI/4. <= theta && theta <= -PI/4. ) {

mjb – April 24, 2007

Oregon State University Computer Graphics

normal = ToXyz( vec3( 0., -Height, Theight/4. ) ); } else if( theta >= 3.PI/4. || theta <= -3.PI/4. ) { normal = ToXyz( vec3( -Height, 0., Swidth/4. ) ); } } float intensity = Ambient + (1.-Ambient)*dot(normal, DirToLight); vec3 litColor = SurfaceColor.rgb * intensity; gl_FragColor = vec4( litColor, SurfaceColor.a ); }

It’s handy to not need a Program-supplied Tangent Vector

mjb – April 24, 2007

Oregon State University Computer Graphics

Cow Pox? :-)

Combining Bump and Cube Mapping: A Good Reason to Work in Eye Coordinates instead of Surface Local Coordinates

mjb – April 24, 2007

Oregon State University Computer Graphics

Combining Bump and Cube Mapping: A Good Reason to Work in Eye Coordinates instead of Surface Local Coordinates

mjb – April 24, 2007

Oregon State University Computer Graphics

Combining Bump and Cube Mapping: A Good Reason to Work in Eye Coordinates instead of Surface Local Coordinates

mjb – April 24, 2007

Oregon State University Computer Graphics

Following the pattern from before, the normal vector is:

[ Normal ] = [ -Amp * 2 π /Pd * sin( 2 π x/Pd - 2 π Time ), 0., 1. ]

This is true along just the X axis. The trick now is to rotate the normal vector into where we really areinto where we really are. Because we are just talking about a rotation, the Because we are just talking about a rotation the transformation is the same as if we were rotating a vertex.

X

Y

Nx’ = Nx * cos Θ - Ny * sin Θ = Nx * cos Θ

Ny’ = Nx * sin Θ + Ny * cos Θ = Nx * sin Θ

R

mjb – April 24, 2007

Oregon State University Computer Graphics

X

Nz’ = Nz = 1.

(Note that in the final version, you will substitute R for x in the slope equation)

Because each linear ripple has an angle Θ , we can think of its direction and perpendicular normal like this:

sin Θ

cos Θ

[-sin Θ ,cos Θ ] C

(Note that slope · normal = 0, as it must be.)

The linear ripple goes through the point C in the direction [cos Θ , sin Θ ]

The normal is then [-sin Θ , cos Θ ]

MCposition

mjb – April 24, 2007

Oregon State University Computer Graphics

[-sin Θ ,cos Θ ]

p

s The distance, s, of a Model Coordinate position perpendicular to the linear ripple is:

C s = ( MCposition-C) · ( sin Θ , cos Θ )

z = - Amp * cos( 2 π s/P - 2 π Time )

The amplitude of the wave, z, is:

And the slope dz/ds is:

(where P is the wave period)

dz/ds = Amp * 2 π /P * sin( 2 π s/P - 2 π Time )

slope = [ 0., 1., dz/dy] = [ 0., 1., Amp * 2 π /P * sin( 2 π s/P - 2 π Time ) ]

And the slope dz/ds is:

If we start by assuming that the ripple angle is 0º (i.e., the wave is propagating in y), then the vector slope of the wave is:

mjb – April 24, 2007

Oregon State University Computer Graphics

normal = [ 0., -Amp * 2 π /P * sin( 2 π s/P - 2 π Time ), 1. ]

So the wave’s vector normal while propagating in y is:

Θ = 0º

N

This is true if the wave is propagating in y, i.e., the ripple angle is 0º. The trick now is to rotate the normal vector into where we really are. Because we are just talking about a rotation, the transformation is the same as if we were rotating a vertex.

N Nx’ = Nx * cos Θ^ - Ny * sin Θ N

Ny’ = Nx * sin Θ + Ny * cos Θ

Nz’ = Nz

X

vec3 normal = normalize( vec3(Nx’,Ny’,Nz’) );

mjb – April 24, 2007

Oregon State University Computer Graphics

So, for any MCposition of a fragment, we compute the normal vector to the simulated rippled surface. We then make this interact with the light source location to make variations in intensity give the rippled appearance.