Bump mapping - Lecture overview Bump Mapping in Cg Bedřich...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Lecture overview Bump Mapping in Cg Bedřich Beneš, Ph.D. Purdue University Department of Computer Graphics • • • • Motivation Theory Normal Map generation Cg implementation © Bedrich Benes Motivation Original Idea • The bump mapping is a common technique in Computer Graphics • It is a simplification of complex geometry by a 2D texture • It works only with lighting and it alternates the normal vector to the surface • It results in a bump-like appearance • © Bedrich Benes Blinn, James F. "Simulation of Wrinkled Surfaces", Computer Graphics, Vol. 12 (3), pp. 286-292 SIGGRAPH-ACM (August 1978) © Bedrich Benes Examples Advantages • high geometry details but no excessive geometry requirements • looks great with moving lights • simplified authoring – use one texture • normals must be stored in a normal map • possible bump instancing via reusing the normal map © Bedrich Benes © Bedrich Benes Normal Map Normal Map Generation • • • • • • Normal map is calculated by a discrete operator • We need to know the tangent vectors • u and v in the corresponding directions • If the original continuous function is available it is simply Store the normal map as the RGB image 0≤R,G,B≤1 But the values of the normals are -1≤R,G,B≤1 We need to remap them by x=2*R-1 y=2*G-1 z=2*B-1 © Bedrich Benes ∂f → ∂f ,v = u= ∂u ∂v → © Bedrich Benes Normal Map Generation • Usually we start with an image • The partial derivatives must be approximated by discrete functions • We want to know the normal at X u=B-X A v=A-X XB o • n=uxv © Bedrich Benes The Cross Product (x) the cross (vector) product of vectors P and Q is a vector R=PxQ = (PyQz-PzQy, -(PxQz-PzQx), PxQy-PyQx) Gives vector perpendicular to P and Q, or zero vector if P is parallel to Q or zero vector if P=o or Q=o PQR form a basis of the coordinate system! (The vector space) © Bedrich Benes (x) easy way to remember the formula Ry Rz R=PxQ= Rx Px Py Pz Qx Qy Qz it is NOT commutative, i.e., PxQ ≠ QxP in fact -R = Q x P © Bedrich Benes The Cross Product Normal Map Generation • The normal vector has values [-1,1] and must be compressed to the space [0,1] • n’=0.5(n+1) • normals are then stored as a RGB image • this is done as a preprocess © Bedrich Benes Normal Map Generation Normal Map Generation • Another way: use PSP plugin http://developer.nvidia.com/object/photoshop_dds_plugins.html © Bedrich Benes © Bedrich Benes Reading and Storing the Texture Cg implementation #include <gl/glaux.h> #pragma comment(lib,"glaux.lib") ... AUX_RGBImageRec *tex= auxDIBImageLoad((const char*)filename); if (!tex) exit(-1);//problem glTexImage2D(GL_TEXTURE_2D,0,GL_RGB, tex->sizeX,tex->sizeY,0, GL_RGB,GL_UNSIGNED_BYTE,tex->data); • • • • • © Bedrich Benes is a combination of lighting and texturing we will use just diffuse lighting there are issues with spaces (as always) vp makes the preprocess fp does the hardwork © Bedrich Benes Cg implementation Cg implementation • Problem: textures are stored in the texture space lighting is calculated in the world space • We need to put everything into one space • The fastest is the texture space (less calculation) • We need to send the tangent to the surface in each vertex • the tangent, normal, and the binormal form so called Frenet Frame • they are mutually orthogonal • we need to know just two of them to get the last one © Bedrich Benes © Bedrich Benes Cg implementation Cg implementation • Knowing the normal vector n • Knowing the tangent vector t • The binormal is b=nxt • The triplet [t,b,n] form a coordinate system • It converts vectors to the texture space • We need to move just the light vector • The vectors are stored as columns © Bedrich Benes M = tx bx nx ty b y n y tz b z n z • So we need to multiply from the right side v’=vM © Bedrich Benes vp and fp communication vp (body) TEXCOORD0 oPos iPos iNormal iTangent iTexCoords vp oNormalMapCoord oLightDir oColor fp lightPos modelViewProjection TEXCOORD1 colMap (texture) normMap (texture) © Bedrich Benes 1. 2. 3. 4. oPos=mul(modelViewProjection,iPos); oLightDir=lightPos-iPos.xyz; iTan=normalize(iTangent); float3 binormal=cross(iTan,iNormal); 5. float3x3 rot=//rotation matrix float3x3(iTan,binormal,iNormal); 6. oLightDir=mul(oLightDir,rot); 7. oNormMapCoord=iTexCoord;//pass away © Bedrich Benes vp (body) fp (body) 2. gets the light vector 1. float3 light=normalize(iLightDir); 2. float3 normTex= 3.-4. gets the Frenet Frame 5.-6. converts the light vector to the texture space 7. passes the texture coordinates to the fp © Bedrich Benes tex2D(normMap,normMapTextCoord).xyz; 3. 4. 5. 6. float3 n=2*(normTex-0.5); float4 bumpColor=dot(n,light); bumpColor=saturate(bumpColor); float4 colColor= tex2D(colMap,normMapTextCoord); 7. oColor=lerp(bumpColor,colColor,0.3); © Bedrich Benes fp (body) Influence of the lerp (0.1,0.4,0.7,0.9) 1. renormalizes the vector that is changed because of the interpolation 2. takes the normal from the normal map 3. recompresses it to [-1,1] 4. calculates illumination 5. clamps into [0,1]3 6. gets the color texture 7. calculates the result as a blending of the bump and the color © Bedrich Benes © Bedrich Benes OpenGL application OpenGL application • Must • tangents are sent as texture coordinates 1 • • • • • calculate the normals calculate the tangents provide the [u,v] mapping set the color map into the texturing unit 1 set the normal map into the texturing unit 0 glMultiTexCoord2fvARB(GL_TEXTURE1_ARB,t); • the instruction is the OpenGL extension • that is a lot of work… © Bedrich Benes © Bedrich Benes OpenGL application OpenGL application • Tangents are the most difficult part! • They are known only for analytics surfaces • The surfaces are differentiate • Se we can get their derivatives • Setting the textures to the correct units © Bedrich Benes Resources • R.Fernando, Mark J.Kilgard The Cg Tutorial The Definitive Guide to Programmable Real-Time Graphics, nVIDIA, Chapter 8 • Blinn, James F. "Simulation of Wrinkled Surfaces", Computer Graphics, Vol. 12 (3), pp. 286-292 SIGGRAPH-ACM (August 1978) © Bedrich Benes glEnable(GL_TEXTURE_2D); glActiveTextureARB(GL_TEXTURE1_ARB); glBindTexture(GL_TEXTURE_2D,mapText); glActiveTextureARB(GL_TEXTURE0_ARB); glBindTexture(GL_TEXTURE_2D,colText); © Bedrich Benes ...
View Full Document

This note was uploaded on 02/19/2012 for the course CGT 411 taught by Professor Staff during the Spring '08 term at Purdue University-West Lafayette.

Ask a homework question - tutors are online