Textures - GitHub Pages · OpenGL Texturing Snippet CS 148: Introduction to Computer Graphics and...

Post on 04-Jul-2020

17 views 0 download

Transcript of Textures - GitHub Pages · OpenGL Texturing Snippet CS 148: Introduction to Computer Graphics and...

CS 148: Summer 2016Introduction of Graphics and ImagingZahid Hossain

Textures

http://www.pling.org.uk/cs/cgv.html

TextureMapping§ Atechniqueforspecifyingvariationsinsurfacereflectancepropertiesofan

object§ Storethereflectanceasanimageand“map”itontotheobject§ Thestoredimageiscalledatexturemap

CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 2

§ Atexturemapisdefiedinitsown2Dcoordinatesystem,parameterizedby(u,v)

§ Establishacorrespondencebyassigning(u,v)coordinatestotrianglevertices

TextureCorrespondence

(0,0) (1,0)

(0,1) (1,1)

CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 3

OpenGLTexturingSnippet

CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 4

§ Then,foreachpixelinsideatriangle,calculatethepixel’s(u,v)texturecoordinatesusingbarycentric interpolationofthetrianglevertices’texturecoordinates

TextureCoordinates

CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 5

PixelColor§ Giventhepixel’s(u,v)texturecoordinates,useinterpolationinthetexture

maptofindthepixel’scolor

??

CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 6

PixelColor:NearestNeighbor

CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 7

PixelColor:BilinearInterpolation

CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 8

LinearlyInterpolate

PixelColor:BilinearInterpolation

CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 9

AnotherLinearlyIn

terpolate

NearestNeighborVsBilinear

CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 10

NearestNeighborVsBilinear

CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 11

More on this when we discuss “Sampling”

ScreenSpacevs.WorldSpace§ Triangleschangeshapenonlinearlyviaperspectivetransformation,

leadingtodifferentbarycentricweightsbeforeandaftertheperspectivetransformation

§ Interpolatinginscreenspaceresultsintexturedistortion§ Interpolatinginworldspacerequiresprojectingallpixellocations

backwardsfromscreenspacetoworldspace,whichisexpensive

CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 12

TextureDistortion

CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 13

p’0

p’1

p0

p1𝑠 ≠ 0.5

𝑡 = 0.5

𝑡: Screen-spaceparameter𝑠:World-spaceparameter

TextureDistortion

CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 14

p’0

p’1

p0

p1𝑡 ≠ 0.5

𝑠 = 0.5

𝑠: Screen-spaceparameter𝑡:World-spaceparameter

Screen-space and World-space parameters don’t match !

TextureDistortion§ Uniformincrementsalongtheedgeinworldspacedonotcorrespondtouniformincrements

alongtheedgeinscreenspace§ Barycentric interpolation (whichislinear)doesnotaccountforthisnonlinearity

CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 15

MeshRefinement§ Refinementofthetrianglemeshimprovestheresult§ Anonlinearfunctioncanbeapproximatedasa

piecewiselinearfunctioniftheintervalsaresmallenough

§ Howeversomeerrorsarestillobvious,especiallyatT-junctionswherelevels-of-refinementchange

CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 16

Subdivide

Tjunction

MeshRefinement

CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 17

Does not work !

PerspectiveCorrectInterpolation

§ Findtherelationshipbetweenthebarycentricweightsinscreenspaceandthoseinworldspace

§ Usethisrelationshiptocomputetheworldspacebarycentric weightsfromthescreenspacebarycentric weights

CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 18

PerspectiveCorrectInterpolation

CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 19

Twopoints inworldspace

PerspectiveCorrectInterpolation

CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 20

Twopoints inworldspace

Interpolation inworldspace

PerspectiveCorrectInterpolation

CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 21

Twopoints inworldspace

Interpolation inworldspace

Projecttheinterpolatedpointonthescreen

PerspectiveCorrectInterpolation

CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 22

Twopoints inworldspace

Interpolation inworldspace

Projecttheinterpolatedpointonthescreen

PerspectiveCorrectInterpolation

CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 23

Interpolationofthesametwopoints inscreenspace(afterprojection)

PerspectiveCorrectInterpolation

CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 24

Interpolationofthesametwopoints inscreenspace(afterprojection)

Screenspacepointandworld-spacepointafterprojectionmustmatch

PerspectiveCorrectInterpolation

CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 25

Interpolationofthesametwopoints inscreenspace(afterprojection)

Screenspacepointandworld-spacepointafterprojectionmustmatch

Afteralgebra

PerspectiveCorrectInterpolation

CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 26

ScreenSpaceTriangle WorldSpaceTriangle

PerspectiveCorrectInterpolation

CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 27

Finally!

PerspectiveCorrectInterpolation

CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 28

Finally!

Aliasing

Whatweget Whatwewant

CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 29

Aliasing

CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 30

LargetextureSmallimage

Source pixels covers many destination pixels

Aliasing

CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 31

LargetextureSmallimage

Source pixels covers many destination pixels

MIPMaps§ Multum inParvo:Muchinlittle,manyinsmallplaces

§ Precomputes thetexturemapsatmultiple resolutions, usingaveraging asalowpassfilter

§ Whentexturemapping, choosetheimagesizethatapproximatelygivesa1to1pixeltotexel correspondence

§ Theaveraging“bakes-in”allthenearbypixelsthatotherwisewouldnotbesampledcorrectly

CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 32

MIPMaps§ 4neighboringpixelsofthehigherlevelareaveragedtoformasingle

pixelinthelowerlevel

§ Startingatabaseresolution,youcanstoreEVERYcoarserresolutioninpowersof2

CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 33

MIPMaps§ 4neighboringpixelsofthehigherlevelareaveragedtoformasingle

pixelinthelowerlevel

§ Startingatabaseresolution,youcanstoreEVERYcoarserresolutioninpowersof2

CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 34

MIPMaps

CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 35

MIPMaps

CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 36

gluBuild2DMipmaps

Φ

y

AssigningTextureCoordinates§ Forcertainsurfaces,the(u,v)texturecoordinatescanbegenerated

procedurally§ Example:Cylinder

§ maptheucoordinatefrom[0,1]to[0,2 π]forΦ§ mapthevcoordinatefrom[0,1]to[0,h]fory§ Thiswrapstheimagearoundthecylinder

§ Formorecomplexsurfaces,(u,v)mustbedefinedpervertexmanuallyorbyusingproxyobjects

CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 37

ProxyObjects– Step1• Assigntexturecoordinatestointermediate/proxyobjects:§ Example:Cylinder§ wraptexturearoundtheoutsideofthecylinder§ notthetoporbottom,inordertoavoiddistortingthetexture

§ Example:Cube§ unwrapthecubeandmaptextureovertheunwrappedcube§ thetextureisseamless acrosssomeoftheedges,butnotnecessarily

others

CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 38

ProxyObjects– Step2§ Maptexturecoordinatesfromtheintermediate/proxyobjecttothe

finalobject§ Threewaysofmappingaretypicallyused

§ Usetheintermediate/proxyobject’ssurfacenormal§ Usethetargetobject’ssurfacenormal§ Useraysemanatingfromcenteroftargetobject

CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 39

CubeMapping

CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 40

http://learnopengl.com/#!Advanced-OpenGL/Cubemaps

ProjectiveTexturing

CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 41

• TreatlightSourceasa• Renderthescenenormallyfromtheactualcamera

http://www.nvidia.com/object/Projective_Texture_Mapping.htmlSegal et.al.SIGGRAPH’92

ProjectiveTexturing• AssignTextureCoordinates(s,t,r)toposition(x,y,z)

CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 42

ProjectiveTexturing• AssignTextureCoordinates(s,t,r)toposition(x,y,z)

CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 43

ProjectiveTexturing• AssignTextureCoordinates(s,t,r)toposition(x,y,z)

CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 44

ProjectiveTexturing• AssignTextureCoordinates(s,t,r)toposition(x,y,z)

CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 45

So much work just to say (s,t,r) = (x,y,z)Much easily done in newer OpenGL

ProjectiveTexturing

• UseasimilarView(fromthelight’spointofview)andProjectionmatrixtotransformtexturecoordinatestoNDC(-1,1)

CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 46

ProjectiveTexturing

• MapNDC(-1,1)toTextureCoordinatespace(0-1)• ScaleandaddBias

CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 47

ProjectiveTexturing

• MapNDC(-1,1)toTextureCoordinatespace(0-1)• ScaleandaddBias

CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 48

Finaltexturecoordinatesafterperspective-correctinterpolation of

ProjectiveTexturing

• MapNDC(-1,1)toTextureCoordinatespace(0-1)• ScaleandaddBias

CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 49

Finaltexturecoordinatesafterperspective-correctinterpolation of

Hardware does it for you :)

HowToSetTextureMatrices?

CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 50

HowToSetTextureMatrices?

CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 51

Much simpler in newer OpenGL(will discuss later)

ProjectiveTexturing:Issues

CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 52

ProjectiveTexturing:Issues

CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 53

SevereAliasing

CS 148: Summer 2016Introduction of Graphics and ImagingZahid Hossain

Textures

http://www.pling.org.uk/cs/cgv.html