CS380: Introduction to Computer Graphics Texture Mapping … · 2018. 5. 3. · 18/05/03 1 Min H....
Transcript of CS380: Introduction to Computer Graphics Texture Mapping … · 2018. 5. 3. · 18/05/03 1 Min H....
18/05/03
1
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
CS380:IntroductiontoComputerGraphicsTextureMapping
Chapter15
MinH.KimKAISTSchoolofComputing
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
SUMMARYMaterials
2
18/05/03
2
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
LightblobfromPVCplastic• Recall:Givenanyvector(notnecessarilyofunitnorm)andaunitnormalvector,wecancomputethebouncevector(mirrorreflection)ofas
3
w
n
w
B(w) = 2( w ⋅ n)n − w
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Diffusereflectancemodel• “Diffuse”materials,likeroughwood,appearequallybrightwhenobservedfromalldirection– Thus,whencalculatingthecolorofapointonadiffusesurface,wedonotneedtousethevectoratall.
4©SzymonRusinkiewicz(Princeton)
v
v
fr = const.=ρπ
18/05/03
3
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Blinn-Phongreflectancemodel• SimpleBRDFdescribingspecularreflection,modeledinOpenGL
©SzymonRusinkiewicz(Princeton)
n is normalh is halfway between I and v
fr =ρπ+ ks (n ⋅h)
α
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
TEXTUREMAPPINGChapter15
6
18/05/03
4
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Texturemapping• Wehavealreadyseenandusedtexturemapping
• Inbasictexturing,wesimply‘glue’partofanimageontoatrianglebyspecifyingtexturecoordinatesatthethreevertices.
7
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Texturemapping• BunchofOpenGLcodestoloadatextureandsetvariousparameters(lin/const,mipmap,wrappingrules).
• Auniformvariableisusedtopointtothedesiredtextureunit.
8
18/05/03
5
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Texturemapping• Varyingvariablesareusedtostoretexturecoordinates.
• Inthissimplestincarnation,wejustfetchr,g,bvaluesfromthetextureandsendthemdirectlytotheframebuffer.
9
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Texturemapping• Alternatively,thetexturedatacouldbeinterpretedas,say,thediffusematerialcolorofthesurfacepoint,whichwouldthenbefollowedbythediffusematerialcomputationdescribedearlier.
10
18/05/03
6
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Texturemapping(main.cpp)• initGLState()
11
…glActiveTexture(GL_TEXTURE0);glGenTextures(1,&h_texture);glBindTexture(GL_TEXTURE_2D,h_texture);glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP);glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP);glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);inttwidth,theight;packed_pixel_t*pixdata=ppmread("reachup.ppm",&twidth,&theight);assert(pixdata);glTexImage2D(GL_TEXTURE_2D,0,GL_SRGB,twidth,theight,0,GL_RGB,GL_UNSIGNED_BYTE,pixdata);free(pixdata);...
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Texturemapping• initShaders()• display()
• Texturelocation(0,0)àlowerleft,(1,1)àupperright
12
h_texUnit0=safe_glGetUniformLocation(h_program,"texUnit0");h_aTexCoord=safe_glGetAttribLocation(h_program,"aTexCoord");
safe_glUniform1i(h_texUnit0,0);
GlfloatsqTex[12]={0,0,1,1,1,0,0,0,0,1,1,1};
18/05/03
7
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Texturemapping
13
#version130uniformfloatuVertexScale;uniformmat4uProjMatrix;uniformmat4uModelViewMatrix;invec2aVertex;invec2aTexCoord;invec3aColor;outvec3vColor;outvec2vTexCoord;voidmain(){gl_Position=vec4(uProjMatrix*uModelViewMatrix*aVertex);vColor=aColor;vTexCoord=aTexCoord;}
• Vertexshader
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Texturemapping
14
#version130uniformsampler2DuTexUnit0;//texturedatainvec2vTexCoord;//texturecoordinatesoutvec4fragColor;voidmain(){vec4texColor0=texture2D(uTexUnit0,vTexCoord);fragColor=texColor0;}
• Fragmentshader
18/05/03
8
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Normalmapping• Thedatafromatexturecanalsobeinterpretedinmoreinterestingways.
• Innormalmapping,ther,g,bvaluesfromatextureareinterpretedasthethreecoordinatesofthenormalatthepoint.
• Thisnormaldatacanthenbeusedaspartofsomematerialsimulation,
15
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Normalmapping
16
Originalmesh4Mtriangles
Simplifiedmesh500triangles
Simplifiedmeshandnormalmapping500triangles
18/05/03
9
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Normalmapping• Normaldatahasthreecoordinatevaluesofunitvectors,eachintherange[-1…1],whileRGBtexturesstorethreevalues,eachintherange[0…1](0…255)
• Soneedsomeconversions:– R=normal_x/2.0+0.5;– normal_x=2*R-1;
• Sometimeneedtodealwithcoordinatesystemconversions.
17
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Environmentcubemaps• Texturescanalsobeusedtomodeltheenvironmentinthedistancearoundtheobjectbeingrendered.
• Inthiscase,wetypicallyusesixsquaretexturesrepresentingthefacesofalargecubesurroundingthescene.
18
+y
-y
-z +z+x-x
18/05/03
10
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
CapturingEnvironmentMaps• Photographingalightprobeproducesanenvironmentmaprepresentingincidentradiancefromalldirections.
19 B(w) = 2( w ⋅ n)n − w
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
CapturingEnvironmentMaps
20
• Howtoremovethecamerafromtheenvironmentmap?
18/05/03
11
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Spherevs.Cube
21
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
SphericalEnvironmentMap&CubeMap
• Galileo’sTomb
22
http://www.pauldebevec.com/Probes/
Lightprovecaptured
Equirectangular(EQ)image
Six-facecubemap
18/05/03
12
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Environmentcubemaps• Eachtexturepixelrepresentsthecolorasseenalongonedirectionintheenvironment.
• Thisiscalledacubemap.GLSLprovidesacube-texturedatatype,samplerCubespecificallyforthispurpose.
23
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Environmentcubemaps• Duringtheshadingofapoint,wecantreatthematerialatthatpointasaperfectmirrorandfetchtheenvironmentdatafromtheappropriateincomingdirection.
24
18/05/03
13
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Environmentmapshader• Wecalculateinthepreviouslecture.
• Thisbouncedvectorwillpointpointstowardstheenvironmentdirection,whichwouldbeobservedinamirroredsurface.
• Bylookingupthecubemap,usingthisdirection,wegivethesurfacetheappearanceofamirror.
25
B(v)
B(w) = 2( w ⋅ n)n − w
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Environmentmapshader• Fragmentshader
26
#version130uniformsampler2DuTexUnit0;invec3nNormal;invec4vPosition;outvec4fragColor;vec3reflect(vec3w,vec3n){returnn*(dot(w,n)*2.0)-w;//bouncevector}voidmain(){vec3normal=normalize(vNormal);vec3reflected=reflect(normalize(vec3(-vPosition)),normal);vec4texColor0=textureCube(uTexUnit0,reflected);fragColor=vec4(texColor0.r,texColor0.g,texColor0.b,1.0);;}
18/05/03
14
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Environmentmapshader• -vPositionrepresentstheviewvector• textureCubeisaspecialGLSLfunctionthattakesadirectionvectorandreturnsthecolorstoredatthisdirectioninthecubetexturemap.
• Hereweassumeeye-coordinates,butframechangesmaybeneeded.
27
v
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Environmentmapshader• Thiscanbeusedforrefraction.
28
18/05/03
15
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Projectortexturemapping(cameramapping)
• Therearetimeswhenwewishtoglueourtextureontoourtrianglesusingaprojectormodel,insteadoftheaffinegluingmodel.
• Forexample,wemaywishtosimulateaslideprojectorilluminatingsometrianglesinspace.
29
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Projectortexturemapping
30
18/05/03
16
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
RECAP:projection
31
xnwn
ynwn
−wn
⎡
⎣
⎢⎢⎢⎢⎢
⎤
⎦
⎥⎥⎥⎥⎥
=
xcyc−wc
⎡
⎣
⎢⎢⎢⎢⎢
⎤
⎦
⎥⎥⎥⎥⎥
=
1 0 0 00 1 0 0− − − −0 0 −1 0
⎡
⎣
⎢⎢⎢⎢
⎤
⎦
⎥⎥⎥⎥
xeyeze1
⎡
⎣
⎢⎢⎢⎢⎢
⎤
⎦
⎥⎥⎥⎥⎥
[0,0,0,1]t
p
• Canonicalsquarespace:
xn = − xeze, yn = − ye
ze
−ze
ye
wn
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Projectortexturemapping• Theslideprojector(≈camera)ismodeledusing4by4,modelviewandprojectionmatrices,
32
Ms andPs . xtwt
ytwt
−wt
⎡
⎣
⎢⎢⎢⎢⎢
⎤
⎦
⎥⎥⎥⎥⎥
= PsMs
xoyozo1
⎡
⎣
⎢⎢⎢⎢⎢
⎤
⎦
⎥⎥⎥⎥⎥
18/05/03
17
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Projectortexturemapping• Withthetexturecoordinatesdefinedas
• Tocolorapointonatrianglewithobjectcoordinates,wefetchthetexturedatastoredatlocation
33
xt =
xtwt
wt
andyt =ytwt
wt
[xo , yo ,zo ,1]t
[xt , yt ]t
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Projectortexturemapping• Thethreequantitiesareallaffinefunctionsof.Thusthesequantitieswillbeproperlyinterpolatedoveratrianglewhenimplementedasvaryingvariables.
• Inthefragmentshader,weneedtodividebytoobtaintheactualtexturecoordinates.
• Whendoingprojectortexturemapping,wedonotneedtopassanytexturecoordinatesoftrianglesasattributevariablestoourvertexshader.
34
xtwt , ytwt and wt
(xo, yo, zo )
wt
18/05/03
18
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Projectortexturemapping• Wesimplyusetheobjectcoordinatesalreadyavailabletous.
• Wedoneedtopassintheobjectcoordinatesviathenecessaryprojectormatrices(affine/projectionmatrices),usinguniformvariables.
35
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Projectortexturemapping• Projectorvertexshader
36
#version130uniformmat4uModelViewMatrix;uniformmat4uProjMatrix;uniformmat4uSProjMatrix;uniformmat4uSModelViewMatrix;invec4aVertex;outvec4aTexCoord;voidmain(){vTexCoord=uSProjMatrix*uSModelViewMatrix*aVertex;gl_Position=uProjMatrix*uModelViewMatrix*aVertex;}
18/05/03
19
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Projectortexturemapping• Projectorfragmentshader
37
#version130uniformsampler2DvTexUnit0;invec4aTexCoord;outvec4fragColor;voidmain(){vec2tex2;tex2.x=vTexCoord.x/vTexCoord.w;tex2.y=vTexCoord.y/vTexCoord.w;vec4texColor0=texture2D(vTexUnit0,tex2);fragCoor=texColor0;}
xtwt
ytwt
−wt
⎡
⎣
⎢⎢⎢⎢⎢
⎤
⎦
⎥⎥⎥⎥⎥
=PsMs
xoyozo1
⎡
⎣
⎢⎢⎢⎢⎢
⎤
⎦
⎥⎥⎥⎥⎥
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Projectortexturemapping• Conveniently,OpenGLevengivesusaspecialcalltexture2DProj(vTexUnit0,pTexCoord),thatactuallydoesthedivideforus.
• Inconveniently,whendesigningourslideprojectormatrixuSProjMatrix,wehavetodealwiththefactthatthecanonicaltextureimagedomaininOpenGListheunitsquare,whoselowerleftandupperrightcornershavecoordinatesusedforthedisplaywindow.
38
[0,0]t and[1,1]t
18/05/03
20
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Multipassrendering• Moreinterestingrenderingeffectscanbeobtainedusingmultiplerenderingpassesoverthegeometryinthescene.
• Inthisapproach,theresultsofallbutthefinalpassarestoredofflineandnotdrawntothescreen.
• Todothis,thedataisrenderedintosomethingcalled,aFrameBufferObject,orFBO.
• Afterrendering,theFBOdataisthenloadedasatexture,andthuscanbeusedasinputdatainthenextrenderingpass.
39
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Multipassrendering
40
1stpassrendering(cubemap) 2ndpassrendering(mirrorball)
18/05/03
21
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Multipassrendering
41
1stpassrendering(cubemap) 2ndpassrendering(mirrorball)
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Shadowmapping• Theideaistofirstcreateandstoreaz-bufferedimagefromthepointofviewofthelight,andthencomparewhatweseeinourviewtowhatthelightsawititsview.
42
18/05/03
22
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Shadowmapping• Ifapointobservedbytheeyeisnotobservedbythelight,thentheremustbesomeoccludingobjectinbetween,andweshoulddrawthatpointasifitwereinshadow.
43
1stpassrendering
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Shadowmapping• Inafirstpass,werenderintoanFBOthesceneasobservedfromsomecamerawhoseorigincoincideswiththepositionofthepointlightsource.Letusmodelthiscameratransformas:
• forappropriatematrices,.
44
xtwt
ytwt
ztwt
wt
⎡
⎣
⎢⎢⎢⎢⎢⎢
⎤
⎦
⎥⎥⎥⎥⎥⎥
=PsMs
xoyozo1
⎡
⎣
⎢⎢⎢⎢⎢
⎤
⎦
⎥⎥⎥⎥⎥
Ps and Ms
18/05/03
23
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Shadowmapping• Duringthisfirstpass,werenderthescenetoanFBOusingasthemodelviewmatrixandastheprojectionmatrix.
• IntheFBO(oflightview),westore,notthecolorofthepoint,butratheritsvalue.
• Duetoz-buffering,thedatastoredatapixelintheFBOrepresentsthevalueofthegeometryclosesttothelightalongtherelevantlineofsight.ThisFBOisthentransferredtoatexture.
45
Ms
Ps
zt
zt
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Shadowmapping• Duringthesecondrenderingpass,werenderourdesiredimagefromtheeye’spointofview,
• butforeachpixel(fragmentshader),weneedtocomparethedistancesof(vPosition-to-light)andthedepthmapfromlight
• tocheckifthepointweareobservingwasalsoobservedbythelight,orifitwasblockedbysomethingcloserinthelight’sview.
46
18/05/03
24
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
• Todothis,weusethesamecomputationthatwasdonewithprojectortexturemapping
• Doingso,inthefragmentshader,wecanobtainthevaryingvariablesassociatedwiththepoint.
• Wethencomparethisvalue(projector)withthevalue(light)storedatinthetextureoflight.
Shadowmapping
47
xt , yt andzt[xo , yo ,zo ,1]
t
zt
[xt , yt ]t
zt
2ndpassrendering
MinH.Kim(KAIST) Foundationsof3DComputerGraphics,S.Gortler,MITPress,2012
Shadowmapping• Ifthesevaluesagreethenwearelookingatapointthatwasalsoseenbythelight;suchapointisnotinshadowandshouldbeshadedaccordinglyinfragmentshader.
• Conversely,ifthesevaluesdisagree,thenthepointwearelookingatwasoccludedinthelight’simage,isinshadowandshouldbeshadedassuch.
48