Tutorial 13 : Normal Mapping
文章推薦指數: 80 %
The basic idea of normal mapping is to give normals similar variations. Normal textures. A “normal texture” looks like this : In each RGB texel is encoded a XYZ ...
Normaltextures
TangentandBitangent
PreparingourVBO
Computingthetangentsandbitangents
Indexing
Theshader
Additionalbuffers&uniforms
Vertexshader
Fragmentshader
Results
Goingfurther
Orthogonalization
Handedness
Speculartexture
Debuggingwiththeimmediatemode
Debuggingwithcolors
Debuggingwithvariablenames
Howtocreateanormalmap
Exercises
Tools&Links
References
Welcomeforour13thtutorial!Todaywewilltalkaboutnormalmapping.
SinceTutorial8:Basicshading,youknowhowtogetdecentshadingusingtrianglenormals.Onecaveatisthatuntilnow,weonlyhadonenormalpervertex:insideeachtriangle,theyvarysmoothly,ontheoppositetothecolour,whichsamplesatexture.Thebasicideaofnormalmappingistogivenormalssimilarvariations.
Normaltextures
A“normaltexture”lookslikethis:
IneachRGBtexelisencodedaXYZvector:eachcolourcomponentisbetween0and1,andeachvectorcomponentisbetween-1and1,sothissimplemappinggoesfromthetexeltothenormal:
normal=(2*color)-1//oneachcomponent
Thetexturehasageneralbluetonebecauseoverall,thenormalistowardsthe“outsideofthesurface”.Asusual,Xisrightintheplaneofthetexture,Yisup(againintheplaneofthetexture),thusgiventherighthandruleZpointtothe“outside”oftheplaneofthetexture.
Thistextureismappedjustlikethediffuseone;thebigproblemishowtoconvertournormal,whichisexpressedinthespaceeachindividualtriangle(tangentspace,alsocalledimagespace),inmodelspace(sincethisiswhatisusedinourshadingequation).
TangentandBitangent
Youarenowsofamiliarwithmatricesthatyouknowthatinordertodefineaspace(inourcase,thetangentspace),weneed3vectors.WealreadyhaveourUPvector:it’sthenormal,givenbyBlenderorcomputedfromthetrianglebyasimplecrossproduct.It’srepresentedinblue,justliketheoverallcolorofthenormalmap:
Nextweneedatangent,T:avectorparalleltothesurface.Buttherearemanysuchvectors:
Whichoneshouldwechoose?Intheory,any,butwehavetobeconsistentwiththeneighborstoavoidintroducinguglyedges.Thestandardmethodistoorientthetangentinthesamedirectionthatourtexturecoordinates:
Sinceweneed3vectorstodefineabasis,wemustalsocomputethebitangentB(whichisanyothertangentvector,butifeverythingisperpendicular,mathissimpler):
Hereisthealgorithm:ifwenotedeltaPos1anddeltaPos2twoedgesofourtriangle,anddeltaUV1anddeltaUV2thecorrespondingdifferencesinUVs,wecanexpressourproblemwiththefollowingequation:
deltaPos1=deltaUV1.x*T+deltaUV1.y*B
deltaPos2=deltaUV2.x*T+deltaUV2.y*B
JustsolvethissystemforTandB,andyouhaveyourvectors!(Seecodebelow)
OncewehaveourT,B,Nvectors,wealsohavethisnicematrixwhichenablesustogofromTangentSpacetoModelSpace:
WiththisTBNmatrix,wecantransformnormals(extractedfromthetexture)intomodelspace.However,it’susuallydonetheotherwayaround:transformeverythingfromModelSpacetoTangentSpace,andkeeptheextractednormalas-is.AllcomputationsaredoneinTangentSpace,whichdoesn’tchangesanything.
Dohavethisinversetransformation,wesimplyhavetotakethematrixinverse,whichinthiscase(anorthogonalmatrix,i.eeachvectorisperpendiculartotheothers.See“goingfurther”below)isalsoitstranspose,muchcheapertocompute:
invTBN=transpose(TBN)
,i.e.:
PreparingourVBO
Computingthetangentsandbitangents
Sinceweneedourtangentsandbitangentsontopofournormals,wehavetocomputethemforthewholemesh.We’lldothisinaseparatefunction:
voidcomputeTangentBasis(
//inputs
std::vector<:vec3>&vertices,
std::vector<:vec2>&uvs,
std::vector<:vec3>&normals,
//outputs
std::vector<:vec3>&tangents,
std::vector<:vec3>&bitangents
){
Foreachtriangle,wecomputetheedge(deltaPos)andthedeltaUV
for(inti=0;i
延伸文章資訊
- 1Normal Mapping - LearnOpenGL
The normal map is defined in tangent space, so one way to solve the problem is to calculate a mat...
- 2Normal Mapping - OpenGL ES SDK for Android - GitHub Pages
A normal map is a regular texture but instead of defining what colour a pixel will be on screen, ...
- 3第十三课:法线贴图
镜面纹理(Specular texture); 用立即模式(immediate mode)进行调试; 利用颜色进行调试; 利用变量名进行调试 ... 今天的内容是法线贴图(normal mapp...
- 4Implementing Normal Mapping using OpenGL/GLSL - Stack ...
That normal map is in tangent-space, but you are treating it as object-space. You need a bitangen...
- 5Learn OpenGL. Lesson 5.5 - Normal Mapping
It is interesting to note the blue tint of this normal map (almost all normal maps have a similar...