eecs 487: interactive what is texture mapping texture mapping in

12
EECS 487: Interactive Computer Graphics Lecture 24: Texture Mapping Texture Mapping What is texture mapping Texture mapping in OpenGL texture-coordinates array Texture coordinates generation Perspective-correct interpolation Multitexture and Light Map Texture mapping in GLSL Texture Mapping What determines the “look” of a pixel? Often results in 3D objects that look like “plastic objects floating in free space” “If it looks like computer graphics, it is not good computer graphics” Jeremy Birn Surface Detail How to make 3D objects look less like “plastic objects floating in free space”? add surface detail but surface details are too expensive to do geometrically, too much geometric detail to model: O’Brien label structured noise pattern with randomness section through volume bumps

Upload: others

Post on 03-Feb-2022

18 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: EECS 487: Interactive What is texture mapping Texture mapping in

EECS487:InteractiveComputerGraphicsLecture24:•  TextureMapping

TextureMapping

Whatistexturemapping

TexturemappinginOpenGL• texture-coordinatesarray

Texturecoordinatesgeneration

Perspective-correctinterpolation

MultitextureandLightMap

TexturemappinginGLSL

TextureMappingWhatdeterminesthe“look”ofapixel?

Oftenresultsin3Dobjectsthatlooklike“plasticobjectsfloatinginfreespace”

“Ifitlookslikecomputergraphics,itisnotgoodcomputergraphics”

−JeremyBirn

SurfaceDetailHowtomake3Dobjectslooklesslike“plasticobjectsfloatinginfreespace”?•  addsurfacedetail•  butsurfacedetailsaretooexpensivetodogeometrically,toomuchgeometricdetailtomodel:

O’Brien

label

structurednoise

patternwithrandomness

sectionthroughvolume

bumps

Page 2: EECS 487: Interactive What is texture mapping Texture mapping in

TextureMapping

Yu08

Instead,“glueon”a2Dimagethatcapturesthesurfacedetailoftheobject

Modifythesurfacepropertiesusedinlightingcomputationwithoutchangingtheunderlyinggeometry,providinganillusionofdetail•  combinefragmentcolorwithalookupvalue•  orcomputefragmentcolorbasedonalookupvalue

� Imagecomplexitydoesn’tincreaseprocessingcomplexity

TextureMappingTexturemap:anarrayofvaluesloadedfromafileandstoredintexturememory•  canbe1D,2D,or3D•  aunitoftextureelementiscalledatexel

Simplestcase,texelscontainscalarvalues:•  imagetexturing:surfacecolor(RGB(A))

Moregenerally,texelscanalsocontainvectors:•  bumpmapping:surfacenormals,tosimulateapparentroughness•  environmentmapping:reflectionvectors,tosimulateshinyandglossysurfaces

Proceduraltexture:insteadofrelyingonapre-computedlookuptable,texturingcanalsobedonealgorithmically

2DTextureMap

Hanrahan

Textureisa2Drasterimage:texture[width(s)][height(t)]oftypeRGB(A)Texturecoordinate(s, t)parameterizedto[0, 1]rangeCanbescaledtocovermanydifferentsurfacesofarbitrarysizeandshape

(0, 0) (1, 0)

(0, 1) (1, 1)t

sy

x

z

2DTextureMapping

cyberware.com(0, 0) (1, 0)

(0, 1) (1, 1)t

s

EstablishamappingbetweensurfacepointandtextureWhenshadingaparticularsurfacepoint•  lookupcorrespondingtexelinthetextureimage• finalcolorofpointwillbeafunctionofthetexel

Page 3: EECS 487: Interactive What is texture mapping Texture mapping in

Image-BasedRendering

debevec.org

Texturemappingintheextreme:usingphotosastexturestorenderdominantsurfacesinscene

WhatYouSeeIsALLYouGet(butthatmaybeallyouneed)

(s0, t0)

(s1, t1)

(s2, t2)

(0,0) (1,0)

(1,1)(0,1)

(s, t) ∈ [0,1]

TextureCoordinatesAssigntexturecoordinatestoeachvertex• 1,2,3,or4texturedimensionspervertex•  indexintothetextureimage,toretrievetexelcorrespondingtothevertex

Texturecoordinates• manuallyspecifiedbyprogrammerorautomaticallygeneratedforeveryvertex•  interpolatedduringrasterization•  texturingitselfdoneduringfragmentprocessing

s

t

TextureMappinginOpenGL1. Createatextureobject:glGenTextures(), glBindTexture()

2. Specifyatextureforthatobject:glTexImage2D() •  optional:•  gluScaleImage() // if dimensions are not powers of 2 •  glPixelStore*() // specify data format

3. Specifywrappingandfilteringmodes:glTexParameter*() 4. Specifyhowthetextureistobeappliedtoeachpixel:glTexEnv*()

5. Enabletexturemapping:glEnable(GL_TEXTURE_2D) 6. Renderthescene,supplyingbothgeometricandtexturecoordinates:glTexCoord2f()

CreatingaTextureObjectAswithotherOpenGLobjects,firstgeneratetextureobjectdescriptors*:

int tods[N]; glGenTextures(N, tods) // Nisthenumberoftextureobjectstobeallocated// todsisanarraytostorethehandles

Next,specify(byhandle)whichparticulartextureobjecttouseforwhichtypeoftextureandmakeit“current”

glBindTexture(GL_TEXTURE_2D, tods[i]);

*texturedescriptor==texturehandle==texturename==textureID

Page 4: EECS 487: Interactive What is texture mapping Texture mapping in

SpecifytheTextureImage

Specifythetextureimagetouse:glTexImage2D(target, level, internalFormat,

width, height, border, format, type, teximage)

with:•  target:GL_TEXTURE_2D(orcubefacesorothers)•  level:mipmaplevel,0ifnotmipmapping•  internalFormat:GL_RGBorGL_RGBA,oracompressedformat•  width:widthofimage,includingborder•  height:heightoftheimage,includingborder

•  border:whetherimagehasborder,mustbe0or1•  format:formatofimage’spixeldata,GL_RGBorGL_RGBA •  type:datatypeofpixeldata,GL_UNSIGNED_BYTE, GL_FLOAT,etc.•  teximage:pointertoimageoroffsetifpixelbufferobjectisused

SurfaceLargerthanTexture

Whatifsurfacemapsto(s, t) > 1.0or< 0.0?Alternatives:

repeat/wrap/tile mirror clamptoedge clamp(toborder)Torepeattextures,usethefractionalpartofvertexcoordinatesastexturecoordinates,forexample:5.3�0.3InOpenGLuseglTexParameter*()tospecifyalternative

(-1,-1)

(2, 2)

Akenine-Moller02

SettingTextureParameters glTexParameteri(target, pname, param); where•  targetisGL_TEXTURE_2D •  pnameisaparameternamethatyouwanttochange:•  GL_TEXTURE_WRAP_T •  GL_TEXTURE_WRAP_S •  GL_TEXTURE_MIN_FILTER •  GL_TEXTURE_MAG_FILTER

•  paramistheparametervaluetochangeto

Forexample:glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);

TextureApplicationModeglTexEnv*():tellOpenGLhoweachtextureshallbecombinedwithpre-existingfragmentcolor •  GL_REPLACE:texturecolorreplacesfragmentcolorcf’ = cs, αf’ = αs

•  GL_ADD:cf’ = cf + cs,αf’ = αf + αs

•  GL_MODULATE:multiplytextureandfragmentcolorcf’ = cf * cs,αf’ = αf * αs

•  GL_BLEND:usetexturevalueasblendingvaluetoblendfragmentcolorandapredeterminedcolorcf’ = (1 - cs) * cf + cs * cc,αf’ = αf * αs

•  GL_DECAL:replacefragmentcolorwithtexturecoloriftexelisopaquecf’ = (1-αs) * cf + αs * cs,αf’ = αf

cc, αccf , αf

cs, αs

cf’, αf’

Page 5: EECS 487: Interactive What is texture mapping Texture mapping in

Example:DiffuseShadingandTexture

Want:textureappeartobeshaded,allowingfortheperceptionofshape• modulatetextureonlywithdiffuselight•  colorthepolygonwhiteandlightitnormally•  useglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); •  texturecolorismultipliedbysurface(fragment)color,loweringtexturebrightness

Problem:modulatingtexturebylightonlymakesitdarker,welostspecularhighlights!

Solution:•  separateoutspecularcomponentasasecondarycolor

Chenney

SettingUpTexture(ininit())/* First, read in the image file */ assert(fp = fopen("wood.ppm","rb")); fscanf(fp,"%*s %*d %*d %*d%*c”); for (i = 0 ; i < 256 ; i++) for (j = 0 ; j < 256 ; j++) for (k = 0 ; k < 3 ; k++) // RGB

fscanf(fp,"%c",&(teximage[i][j][k])); fclose(fp); /* Then set up the texture */ int tod; glGenTextures(1, &tod); glBindTexture(GL_TEXTURE_2D, tod); glTexImage2D(GL_TEXTURE_2D,0,GL_RGB, 256, 256, 0, GL_RGB,

GL_UNSIGNED_BYTE, teximage); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

RenderingwithTexture(indisplay())

/* Also note some effort to find the error if any */ glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); glEnable(GL_TEXTURE_2D); glColor3f(1.0,1.0,1.0); err = glGetError(); assert(err == GL_NO_ERROR); glBegin(GL_POLYGON); glTexCoord2f(1.0, 1.0); glVertex3f(0.5, 0.5, 0.0); glTexCoord2f(0.0,1.0); glVertex3f(-0.5, 0.5, 0.0); glTexCoord2f(0.0,0.0); glVertex3f(-0.5, -0.5, 0.0); glTexCoord2f(1.0,0.0); glVertex3f(0.5, -0.5, 0.0); glEnd(); err = glGetError(); assert(err == GL_NO_ERROR); glDisable(GL_TEXTURE_2D); // state machine!

RenderingwithDifferentTextures// in init(): glGenTextures(2, textures); glBindTexture(GL_TEXTURE_2D, textures[0]);

glTexParameteri(…); … ; glTexImage2D(GL_TEXTURE_2D,…); glBindTexture(GL_TEXTURE_2D, textures[1]);

glTexParameteri(…); … ; glTexImage2D(GL_TEXTURE_2D,…); // in display(): glBindTexture(GL_TEXTURE_2D, textures[0]); glBegin(...); glTexCoord (...); glVertex (...); glEnd (...); glBindTexture (GL_TEXTURE_2D, textures[1]); glBegin (...); glTexCoord (...); glVertex (...); glEnd (...);

Page 6: EECS 487: Interactive What is texture mapping Texture mapping in

Texture-CoordinatesArrayWhenavertexarrayisused,texturecoordinatescorrespondingtotheverticesmustbeprovidedinatexture-coordinatesarray(seeLab6)// texcoords must have a 1-1 mapping with vertices float vertices[][] = { { 0.5, 0.5, 0.0 }, {-0.5, 0.5, 0.0 }, {-0.5, -0.5, 0.0 }, {0.5, -0.5, 0.0 } };

float texcoords[][] = { { 1.0, 1.0 }, { 0.0, 1.0 }, { 0.0, 0.0 }, { 1.0, 0.0 } };

glEnableClientState(GL_VERTEX_ARRAY); glVertexPointer(3, GL_FLOAT, 0, vertices); glEnableClientState(GL_TEXTURE_COORD_ARRAY); glTexCoordPointer(2, GL_FLOAT, 0, texcoords);

TextureCoordinatesAutogenHowdowe“paste”a2Dtextureimageontoa3Dobject?Non-parametrically•  texturesizeandorientationarefixedinworldcoordinates• givesa“projector”effect:object“swims”throughtexture

Parametrically•  texturesizeandorientationtiedtoobject,inobjectcoordinates• mapobjectcoordinatestotexturecoordinates

Wolfe97

Howdowemaptopolygonalmeshes?Planar/orthographicmap:•  simplyremoveoneoftheobject’scoordinatestoprojectontothatcoordinateplane•  thetextureisconstantinonedirection

(z, x, y)

PlanarMapping

Wolfe97

CylindricalMapObjectcoordinate(x, y, z)isconvertedto(r(adius), θ, h(eight))

Fortexturemapping,θisconvertedintos-coordinateandhisconvertedintot-coordinate

Thiswrapsthetexturemaparoundtheobject

Usefulforfaces

TP3

Schulze

s

t

r

h

Page 7: EECS 487: Interactive What is texture mapping Texture mapping in

CylindricalMap

Atminimumandmaximumextentsofthecylinder,thetexturegetspinchedtogether

Wolfe97

t

s

SphericalMap

TP3

Convertfrom(x, y, z)tosphericalcoordinates(θ, ϕ)

Longitude(ϕ)isconvertedintos-coordinate,latitude(θ)isconvertedintot-coordinate(notezisnotpointingupinimage)

SphericalMap

Notonlypinchesthetextureatthepoles,butalsostretchesthesquaresalongtheequator

Wolfe97

Cube/BoxMap

Usesixplanarmaps,oneforeachfaceofthecube

Wolfe97

TP3

t

s

t

s

t

s

y

Page 8: EECS 487: Interactive What is texture mapping Texture mapping in

GeneratingTextureCoordinates

OpenGLcangeneratetexturecoordinatesautomaticallyusingglTexGen*()•  basedondistanceofvertexfromagivenplaneineither•  object-coordinates(GL_OBJECT_LINEAR):textureattachedtoobject,or

•  eye-coordinates(GL_EYE_LINEAR):objectappearsswimmingintexture,e.g.,torenderanoildrill,asitgoesdeeperintoground,itchangescolor !

s = rcθ − θ0( ),

t = 1dh − h0( ),

r:!radius!of!!cylinder,c, !d:!scaling!factors,θ0, !h0:!position!the!texture!!!!!!!!!!!!on!the!cylinder

Two-stagemapping1. mapthetexturetoasimpleintermediatesurface(cube,cylinder,sphere)

2. maptheintermediatesurface(withthetexture)ontothesurfacebeingrendered

TextureMappingwithanIntermediateSurface

Surface(θ, h)

Texture(s,t)

s

t

Pagan

IntermediatetoObjectMapping

(x,y,z)(xi,yi,zi)

ObjectCentroid

Plane Cylinder Sphere Box

ObjectNormal - ✗ ok ok

ObjectCentroid - ✗ ✗ ✗

IntermediateSurfaceNormal

slideprojector

shrinkwrap - ok

Reflectedray EM EM EM EM

(x,y,z)

(xi,yi,zi)

ReflectedRay

Object

(x,y,z)

(xi,yi,zi)

ObjectNormal

n (x,y,z)

(xi,yi,zi)

IntermediateSurfaceNormal

n

Blinn&Newell76

RasterizingTextureCoordinatesWhenrasterizingprimitives:•  assigntexturecoordinatestoeachvertex•  withinatriangle,uselinearinterpolation(barycentriccoordinates!)

s

t(1, 1)

(0, 1)

(0, 0) (1, 0)

(s, t) = (0.2, 0.8)

(0.4, 0.2)

(0.8, 0.4)

A

B C

a

bc

TextureSpace ObjectSpace

••

Akenine-Möller02

Page 9: EECS 487: Interactive What is texture mapping Texture mapping in

PerspectiveProjectionCharacteristicspreserved:Rigidbody/Euclidean:•  angles,lengths,areas

Similitudes/similarity:•  angles,lengthratios

Affine:•  parallellines,lengthratios,arearatios

Perspective:•  collinearity,cross-ratios

TranslationIdentityRotation

RigidBody

Scaling

Linear

Affine

Perspective

Projective

Similitudes

DeMenthonDurand08

not

Linearinterpolationinscreencoordinatesisnotequaltolinearinterpolationineyecoordinates!Solution?

LinearInterpolationinPerspective

p0 , s0 = 0.0

view

plan

e

e

pα = p0 +α(p1 − p0 ), sα ≠ 0.5

p1, s1 = 1.0�

α

1−α ≠α

q0 , s0' = 0.0

q1, s'1 = 1.0

qβ , s'β = 0.5

β

1−β = β

Low02

Perspective-correctInterpolationInsteadofinterpolatingtheparameters’afterperspectivedivide,dotheperspectivedivideontheinterpolateds andinterpolatedhomogenouscoordinatew:•  sα = lerp(s0, s1)• wα = lerp(w0, w1)•  sβ’ = sα/wα

homogeneous texture space(u/w, v/w, 1/w)

screen space(x, y)

homogeneous screen space(xw, yw, w)

world space

texture space(u, v)

div by w

object space

OBJECT-AFFINE SPACES SCREEN-AFFINE SPACES

div by 1/wtexturespace (s, t)

homogeneoustexturespace(s/w, t/w, 1/w)

windowspace(x, y)

homogeneouswindowspace(xw, yw, w, s, t, q=1)

modelspace

worldspace

Heckbert89

Linearinterpolationin2D:Alsorequiresperspectivecorrection:

BilinearInterpolation

Heckbert89

e = (1− u)a + ubf = (1− u)c + ud

p(u,v) = (1− v)e + vf= (1− u)(1− v)a + u(1− v)b + (1− u)vc + uvd

Page 10: EECS 487: Interactive What is texture mapping Texture mapping in

BilinearInterpolationinPerspectiveUncorrected,notonlylackofforeshortening,worseeffectifsquareisrotated:

Effectismostvisibleontexturemapping,butalsopresentsincolorshading,thoughgenerallytoleratedPerspective-correctinterpolationinOpenGL:glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

Blinn75

MultitexturePipelineApplyingmultipletexturestoasinglefragment• appliedonebyoneinapipelinedfashion• eachstageconsistsofatextureunit/environment• allowsfortextureblending,forlightingeffects,decals,compositing

TextureUnitsAtextureunitallowstexturebindingtobeencapsulatedintoasingletexturecontext/environmentInOpenGL,thereareonlyafixed,pre-determinednumber(≥80)oftextureunitsandtheyarenotdynamicOpenGLobjectsToselectaparticulartextureunit,useglActiveTexture() • afterwhichcallstoglBindTexture(),affectonlytheselectedtextureunit

UsingMultitexturePipeline// In init(), load images and initialize textures as before. // In display(): // bind and enable texture unit 0 glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, tbods[0]); glEnable(GL_TEXTURE_2D); // bind and enable texture unit 1 glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, tbods[1]); glEnable(GL_TEXTURE_2D); // specify two sets of texture coordinates per vertex glBegin(GL_TRIANGLES);

glColor3f(1.0f, 1.0f, 1.0f); glMultiTexCoord2f(GL_TEXTURE0, 0.0, 1.0); glMultiTexCoord2f(GL_TEXTURE1, 0.0, 1.0); glVertex3f(…); …

glEnd();

Page 11: EECS 487: Interactive What is texture mapping Texture mapping in

Texturesampler Atextureunitispassed,asauniformvariable,fromapplicationtoshadersasatexturesampler

Fragmentshadersusetexture*()tosampletexturefromsampler*

Example:topasstextureunit0fromapplicationtoshaderasasampler,assumingtextureunit0hasbeensetupasshownpreviously:GLuint texid;

texid = glGetUniformLocation(myprog, “mytexture”);

glUniform1i(texid,0) // assign texture object and // texture unit 0 to sampler

InterpolatedTextureCoordinates// vertex shader: generally only worries about texcoords uniform vec4 lightPos; varying vec3 normal; varying vec3 lightVec; varying vec2 texcoord; void main() { gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; texcoord = 0.01*gl_Vertex.xz; vec4 vert = gl_ModelViewMatrix * gl_Vertex; normal = gl_NormalMatrix * gl_Normal; lightVec = vec3(lightPos - vert); }

TextureLookupinGLSL1.2// fragment shader varying vec3 normal; varying vec3 lightVec; varying vec2 texcoord; uniform sampler2D mytexture; void main(){

vec3 norm = normalize(normal); vec3 L = normalize(lightVec); vec4 color = texture2D(mytexture, texcoord); float NdotL = dot(L, norm); float diffuse = 0.5 * NdotL + 0.5; gl_FragColor = color*vec4(vec3(diffuse),1.0);

}

s

t

TextureCoordinatesasGLSL1.3+CustomVertexAttributeattribute vec4 va_Position; attribute vec2 va_TexCoords; varying texcoords; void main(void) { gl_Position = gl_ModelViewProjectionMatrix*va_Position; texcoords = va_TexCoords; }

Applicationloads,compiles,andlinksshadersasusual

Thenapplicationgetsthecvalocations:

int vPos = glGetAttribLocation(pd, ”va_Position”); int vTex = glGetAttribLocation(pd, ”va_TexCoords”);

Page 12: EECS 487: Interactive What is texture mapping Texture mapping in

BindingCVAwithDataStream

SetupvboGL_ARRAY_BUFFERtoincludetexturecoordinates(seePA3)Enablecva:

glEnableVertexAttribArray(vTex);

Thenbindthecvatothevboholdingthedatastream:glVertexAttribPointer(vTex, 2, GL_FLOAT, GL_FALSE,

stride, offset);