Download OpenGL Texture Interpolation and Application and more Study notes Computer Science in PDF only on Docsity!
Programming
Texture MappingTexture Mapping
Ed AngelEd Angel
2
Texture MappingTexture Mapping
- • The soup tin is easily described by pasting a label on theThe soup tin is easily described by pasting a label on the plain cylinderplain cylinder
- • Texture mappingTexture mapping associates the color of a point with theassociates the color of a point with the color in an image: thecolor in an image: the texturetexture - Soup tin: Each point on the cylinder get the label’s color
- • Question to address: Which point of the texture do weQuestion to address: Which point of the texture do we use for a given point on the surface?use for a given point on the surface?
- • (^) Establish aEstablish a (^) mappingmapping from surface points to image pointsfrom surface points to image points
- Different mappings are common for different shapes
- We will, for now, just look at triangles (polygons)
3
Example Mappings Example Mappings
4
- Apply a 1D, 2D, or 3D image to geometric primitives
- Uses of Texturing
- simulating materials
- reducing geometric complexity
- image warping
- reflections
- • Apply a 1D, 2D, or 3D image to geometricApply a 1D, 2D, or 3D image to geometric primitivesprimitives
- • (^) Uses of TexturingUses of Texturing
- simulating materials
- reducing geometric complexity
- image warping
- reflections
Texture
Mapping
Texture
Mapping
CPUCPU DLDL
Poly.Poly. (^) VertexVertexPerPer RasterRaster FragFrag FBFB PixelPixel TextureTexture
5
Texture Mapping Texture Mapping
s
t
x
y
z
image
geometry screen
6
Texture InterpolationTexture Interpolation
- • (^) Specify where theSpecify where the vertices in worldvertices in world space are mapped tospace are mapped to in texture spacein texture space
- • Linearly interpolateLinearly interpolate the mapping for otherthe mapping for other points in world spacepoints in world space - Straight lines in world space go to straight lines in texture space
Texture map
s
t
Triangle in world space
Programming
7
Interpolating Coordinates Interpolating Coordinates
(x 2 , y 2 ), (s 2 , t 2 ) (x 1 , y 1 ), (s 1 , t 1 )
(x 3 , y 3 ), (s 3 , t 3 )
3 s^ R^ =⎜⎜⎝⎛^1 − yy 3 −− yy^11 ⎟⎟⎠⎞ s^1 +⎜⎜⎝⎛ yy 3 −− yy 11 ⎟⎟⎠⎞ s^3 3 2 2 2 3 2 1 2 s y y s yy y y s (^) L y y ⎟⎟ ⎠ ⎜⎜ ⎞ ⎝
⎛ − ⎟⎟+ − ⎠ ⎜⎜ ⎞ ⎝
⎛ − = − −
s = (^) ⎜⎜⎝⎛ 1 − xxR −− xxLL^ ⎟⎟⎠⎞ sL +⎜⎜⎝⎛ xxR −− xxLL ⎟⎟⎠⎞ s R
8
Pipelines and Texture MappingPipelines and Texture Mapping
- • Texture mapping is done atTexture mapping is done at rasterizationrasterization
- in canonical screen space
- • When describing a scene, you assume that textureWhen describing a scene, you assume that texture interpolation will be done in world spaceinterpolation will be done in world space
- • (^) Which property of perspective projection means that theWhich property of perspective projection means that the ““wrong thingwrong thing”” will happen if we apply our simplewill happen if we apply our simple interpolations from the previous slide?interpolations from the previous slide?
- • (^) Perspective correct texture mappingPerspective correct texture mapping does the right thing,does the right thing, but at a costbut at a cost
- • Is it a problem with orthographic viewing?Is it a problem with orthographic viewing?
9
Linear Interpolation of Textures Linear Interpolation of Textures
(^10)
Texture Index InterpolationTexture Index Interpolation
(^11)
Texture Interpolation Problem Texture Interpolation Problem
(^12)
Perspective Correction Hint
- • Texture coordinate and color interpolationTexture coordinate and color interpolation
- either linearly in screen space
- or using depth/perspective values (slower)
- • Noticeable for polygons “Noticeable for polygons“on edgeon edge”” glHint( glHint( GL_PERSPECTIVE_CORRECTION_HINT, hintGL_PERSPECTIVE_CORRECTION_HINT, hint )) where hint is one of - GL_DONT_CARE - GL_NICEST - GL_FASTEST
Programming
(^19)
Texture Objects (cont.) Texture Objects (cont.)
- • Create texture objects with texture data and stateCreate texture objects with texture data and state glBindTexture( target, id ); - target: one of GL_TEXTURE_{123}D() - Once for each texture map - Typically in the initialization section of application
- • Bind textures before usingBind textures before using glBindTexture( target, id ); - Before rendering object using the texture map - Once per rendered frame: Display function
(^20)
- • Define a texture image from an array ofDefine a texture image from an array of texels in CPU memorytexels in CPU memory glTexImage2D(glTexImage2D( target,target, level,level, components,components, w,w, h,h, (^) border,border, format, type,format, type, texelstexels (^) ););
- dimensions of image usually powers of 2
- • Texel colors are processed by pixelTexel colors are processed by pixel pipelinepipeline - pixel scales, biases and lookups can be done
Specify Texture
Image
Specify Texture
Image
CPUCPU DLDL
Poly.Poly. (^) VertexVertexPerPer RasterRaster FragFrag FBFB PixelPixel Texture Texture
(^21)
Converting A Texture Image
- • (^) If dimensions of image are not power of 2If dimensions of image are not power of 2 gluScaleImage(gluScaleImage(^ format,format, w_in,w_in, h_in,h_in, **type_in, *data_in,type_in, *data_in, w_out,w_out, h_out,h_out,** **type_out, *data_outtype_out, *data_out** ););
- *_in is for source image
- *_out is for destination image
- • (^) Image interpolated and filtered duringImage interpolated and filtered during scalingscaling
(^22)
Specifying a Texture:
Other Methods
- • Use frame buffer as source of texture imageUse frame buffer as source of texture image
- uses current buffer as source image glCopyTexImage2D(...)glCopyTexImage2D(...) glCopyTexImage1D(...)glCopyTexImage1D(...)
- • Modify part of a defined textureModify part of a defined texture glTexSubImage2D(...) glTexSubImage2D(...) glTexSubImage1D(...)glTexSubImage1D(...)
- • Do both withDo both with glCopyTexSubImage2D(...)glCopyTexSubImage2D(...) , etc., etc.
(^23)
- • Based on parametric texture coordinatesBased on parametric texture coordinates
- •^ glTexCoord()glTexCoord()**^ specified at each vertexspecified at each vertex
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
b
c
Texture Space Object Space
Mapping a
Texture
Mapping a
Texture
CPUCPU DLDL
Poly.Poly. (^) VertexVertexPerPer RasterRaster FragFrag FBFB PixelPixel TextureTexture
(^24)
Basic OpenGL TexturingBasic OpenGL Texturing
MORE…
Programming
(^25)
Basic OpenGL Texturing Basic OpenGL Texturing
- • Specify texture coordinates for the polygon:Specify texture coordinates for the polygon:
- Use glTexCoord2f(s,t) before each vertex:
- Eg: glTexCoord2f(0,0); glVertex3f(x,y,z);
- •^ Create a texture object and fill it with texture data:Create a texture object and fill it with texture data:
- glGenTextures(num, &indices) to get identifiers for the objects
- glBindTexture(GL_TEXTURE_2D, identifier) to bind the texture
- Following texture commands refer to the bound texture
- glTexParameteri(GL_TEXTURE_2D, …, …) to specify parameters for use when applying the texture
- glTexImage2D(GL_TEXTURE_2D, ….) to specify the texture data (the image itself) (^26)
Basic OpenGL Texturing (cont)Basic OpenGL Texturing (cont)
- • (^) Enable texturing:Enable texturing: (^) glEnable(GL_TEXTURE_2D)glEnable(GL_TEXTURE_2D)
- • State how the texture will be used:State how the texture will be used:
- • Texturing is doneTexturing is done afterafter lightinglighting
(^27)
Nasty Details Nasty Details
- •^ There are a large range of functions for controlling the layout ofThere are a large range of functions for controlling the layoutof texture data:texture data: - You must state how the data in your image is arranged - Eg: glPixelStorei(GL_UNPACK_ALIGNMENT, 1) tells OpenGL not to skip bytes at the end of a row - You must state how you want the texture to be put in memory: how many bits per “pixel”, which channels,…
- • Textures typically are square with width/height a power of 2Textures typically are square with width/height a power of 2
- Common sizes are 32x32, 64x64, 256x
- Smaller uses less memory, and there is a finite amount of texture memory on graphics cards
(^28)
Example Texture MapExample Texture Map
glVertex3d (s, s, s) glTexCoord2d(1,1);
glVertex3d (-s, -s, -s) glTexCoord2d(0, 0);
(^29)
Wrapping Mode
- • Example:Example: glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP ) glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT )
texture GL_REPEATwrapping GL_CLAMPwrapping
s
t
(^30)
Texture Functions
- • Controls how texture is appliedControls how texture is applied glTexEnv{fi}[v](glTexEnv{fi}[v]( GL_TEXTURE_ENV,GL_TEXTURE_ENV, prop, paramprop, param ))
- • GL_TEXTURE_ENV_MODEGL_TEXTURE_ENV_MODE (^) modesmodes
- GL_MODULATE
- GL_BLEND
- GL_REPLACE
- • Set blend color withSet blend color with GL_TEXTURE_ENV_COLOR GL_TEXTURE_ENV_COLOR
Programming
(^3738)
Texture Coord Generation Texture Coord Generation
(^40)
Texture MappingTexture Mapping
Map a 2D Texture onto an ObjectMap a 2D Texture onto an Object
Consider a Cylinder Consider a Cylinder
(^4142)
Texture MappingTexture Mapping
What if we don’What if we don’t have a cylinder or sphere?t have a cylinder or sphere?
Programming
(^4344)
glTexGenglTexGen
- • Powerful, flexible, underutilizedPowerful, flexible, underutilized
- Contour mapping
- Reflection mapping
- Lighting effects
- Atmospheric effects
(^4546)
glTexGen (cont’d)glTexGen (cont’d)
- • (^) Generate texture coordinates fromGenerate texture coordinates from geometrygeometry - Object space - texture is “attached” to object - Eye space - object moves within texture “field” - Sphere map - based on reflection vector
(^47)
Reference Plane Reference Plane
- •^ Uses plane equationUses plane equation
- • Computes dot productComputes dot product
- coord = Ax + By + Cz + Dw
- coord is distance from plane
(^48)
Object Linear MappingObject Linear Mapping
- • (^) Texture isTexture is ““attachedattached”” to objectto object GLfloat params = {A,B,C,D}; glTexGenfv(GL_S, GL_OBJECT_PLANE, params); glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR); glEnable(GL_TEXTURE_GEN_S);
- • Default mapping is identityDefault mapping is identity (s,t,r,q) = (Xo, Yo, Zo, Wo);
Programming
(^55)
Sphere Mapping Sphere Mapping
- • Based on reflection vectorBased on reflection vector glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP); glEnable(GL_TEXTURE_GEN_S);
- • S, T coordinates onlyS, T coordinates only
(^56)
Sphere MapSphere Map
(^57)
Phong Shading Phong Shading
- •^ Spheremap DetailsSpheremapDetails
0 °
90 ° (^180) °
Eye (^) Texture Map
Specular Map Texture looks like a highlight on a sphere projected to a plane
90 degree Reflection Unused
Center
(^58)
Sphere MappingSphere Mapping
- • (^) Based on reflection vectorBased on reflection vector
(^59)
Uses
- • Environment mappingEnvironment mapping
- • PrePre--computedcomputed specularspecular lightinglighting (recall lighting lecture)(recall lighting lecture)
(^60)
Texture Mapping during ModelingTexture Mapping during Modeling
Programming
(^61)
Texture Mapping during Modeling Texture Mapping during Modeling
(^62)
Texture Mapping during ModelingTexture Mapping during Modeling
(^63)
Texture Mapping during Modeling Texture Mapping during Modeling
(^64)
Two-part mappingTwo-part mapping
- • One solution to the mapping problem is toOne solution to the mapping problem is to first map the texture to a simplefirst map the texture to a simple intermediate surfaceintermediate surface
- • Example: map to cylinderExample: map to cylinder
(^65)
Cylindrical Mapping Cylindrical Mapping
parametric cylinder x = r cos 2π u y = r sin 2πu z = v/h maps rectangle in u,v space to cylinder of radius ө and height h in world coordinates s = u t = v maps from texture space
h (0, 0)
(0.5, 0)
(0, 0.5)
(1, 1)
(0, 1)
( 1, 0.5) (1, 0)
( ө,h )
ө
(^66)
Spherical MapSpherical Map
We can use a parametric sphereWe can use a parametric sphere x = r cos 2πu y = r sin 2πu cos 2πv z = r sin 2πu sin 2πv in a similar manner to the cylinder but have to decide where to put the distortion
Spheres are used in environmental maps
Programming
(^73)
Table 2 Table 2
SlideSlide ProjProj
ShrinkShrink ISN/BoxISN/Box RedundantRedundant wrapwrap
IntermediateIntermediate SurfaceSurface NormalNormal
CentroidCentroid OKOK SphereSphere
CentroidCentroid BoxBox
BadBad openopen cylcyl
ObjectObject CentroidCentroid
ObjectObject BadBad OKOK OKOK BadBad NormalNormal
ClyinderClyinder BoxBox SphereSphere PlanePlane
(^74)
Table 2Table 2
SlideSlide ProjProj
ShrinkShrink ISN/BoxISN/Box RedundantRedundant wrapwrap
IntermediateIntermediate SurfaceSurface NormalNormal
CentroidCentroid OKOK SphereSphere
CentroidCentroid BoxBox
BadBad openopen cylcyl
ObjectObject CentroidCentroid
ObjectObject BadBad OKOK OKOK BadBad NormalNormal
ClyinderClyinder BoxBox SphereSphere PlanePlane
(^75)
Phong Shading Phong Shading
- •^ Getting Highlights RightGetting Highlights Right
With texture GL_SEPARATE_ SPECULAR_COLOR
(^76)
Getting Finer Highlight and Lighting Fx Getting Finer Highlight and Lighting Fx
- • (^) Run the computation in fragment programsRun the computation in fragment programs
- Needs to be local lighting
- (no radiosity / global illum)
- Potentially expensive (slow)
- • Play texture tricksPlay texture tricks
- Compute the lighting beforehand, store in texture
- Need to recompute when scene/lights change
(^77)
Texture Tricks Texture Tricks
vs.
Precompute lighting, Fold into texture
Surface Texture plus Lightmap
(^78)
Phong ShadingPhong Shading
- • Sphere mapping approximatesSphere mapping approximates PhongPhong ShadingShading
Texture coordinates from reflection vector (eye vector and normal)
Texture (s,t)
(s,t) (^) Interpolated coordinates map to texels
E N^ L E N^ L E N L ... ... Texel values computed from Phong lighting equation
Programming
(^79)
Why would texturing help? Why would texturing help?
- • Put linear interpolation BEFORE highlyPut linear interpolation BEFORE highly non-non-linear computation (EXP)linear computation (EXP)
Ispecular = ksIlight ( cos φ) nshiny
(^80)
Phong ShadingPhong Shading
- • SpheremapSpheremap DetailsDetails
- Spheremap texture indexed from reflection vector
- Computation done in eye space
- Texture looks like reflection from chrome hemisphere
- Maps all reflections to hemisphere
- Center of map reflects back to eye (180- degrees)
(^81)
Phong Shading Phong Shading
- •^ Spheremap DetailsSpheremapDetails
0 °
90 ° (^180) °
Eye (^) Texture Map
Specular Map Texture looks like a highlight on a sphere projected to a plane
90 degree Reflection Unused
Center
(^82)
Phong ShadingPhong Shading
- • (^) PhongPhong Shading with TextureShading with Texture
- Multipass technique:
- Render object without specular light
- Redraw object: no lighting, color, surface texture
- Modulate intensity with highlight texture
- Combine images with additive blend
- Can be done in single pass using fragment prog
(^8384)
Phong ShadingPhong Shading
- • (^) Steps to adding texture highlightsSteps to adding texture highlights
Diffuse Lit, Textured White, Modulated by Specular Texture
Combined Image (additive blending)
Programming
(^91)
Lightmaps Lightmaps
- • Moving Local LightsMoving Local Lights
- Recreate the texture; simple but slow
- Manipulate the lightmap
- Translate to move relative to the surface
- Scale to change spot size
- Change base polygon color to adjust intensity
- Projective textures ideal for spotlights
- 3D textures easy to use (if available) (^92)
Spotlights as Lightmap
Special Case
Spotlights as Lightmap
Special Case
- • Mapping Single Spotlight Texture PatternMapping Single Spotlight Texture Pattern
Use texture matrix to perform spotlight texture coordinates transformations.
TranslateSpotlight CoordinatesTexture
SpotlightScale CoordinatesTexture
Change BasePolygon Original Intensity
(^93)
Lightmaps Lightmaps
- •^ Creating a lightmapCreating alightmap
- Light white, tesselated surface with local light
- Render, capture image as texture
- Texture contains ambient and diffuse lighting
- glLight() parameters should match light
- Texture can also be computed analytically
(^94)
LightmapsLightmaps
- • (^) Creating aCreating a lightmaplightmap
Render surface lit by local light
Create a Texture Map from Image
(^95)
Lightmaps Lightmaps
- •^ Lightmap building tipsLightmapbuilding tips Boundary should have constant value
Intensity changes from light should be minimal near edge of lightmap
(^96)
LightmapsLightmaps
- • (^) Lighting with aLighting with a LightmapLightmap
- Local light is affected by surface color and texture
- Two step process adds local light contribution:
- Modulate textured, unlit surfaces with lightmap
- Add locally lit image to scene
- Can mix OpenGL, lightmap lighting in same scene
Programming
(^97)
Lightmaps Lightmaps
- • Creating local contributionCreating local contribution
Unlit Scene Lightmap Intensity Local Light Contribution
Lightmap
(^98)
LightmapsLightmaps
- • Adding local light to sceneAdding local light to scene
OpenGL Lighting Combined Image
(^99)
Lightmaps in Quake2 Lightmaps in Quake
× (modulate)
=
lightmaps onlylightmaps only decal onlydecal only
combined scenecombined scene (^1) (^00)
Packing Many Lightmaps
into a Single Texture
Packing Many Lightmaps
into a Single Texture
- • • (^) Quake 2 light map texture image exampleQuake 2 light map texture image exampleQuake 2 light map texture image example
- Lightmaps typically heavily magnified.
- Permits multiple lightmaps packed into a single texture.
- Quake 2 computes lightmaps via off-line radiosity solver.
- Lightmaps typically heavily magnified.
- Permits multiple lightmaps packed into a single texture.
- Quake 2 computes lightmaps via off-line radiosity solver.
(^10) 1
Lightmaps Lightmaps
- •^ Lightmap considerationsLightmapconsiderations
- Lightmaps are good:
- Under-tesselated surfaces
- Custom lighting
- Multiple identical lights
- Static scene lighting
(^10) 2
LightmapsLightmaps
- • LightmapLightmap considerationsconsiderations
- Lightmaps less helpful:
- Highly tesselated surfaces
- Directional lights
- Combine with other surface effects (e.g. bump- mapping) - eats a texture access in fragment programs - may need to go to multi-pass rendering (fill-bound app)
Programming
(^109)
Multitexture Lightmaps Multitexture Lightmaps
- • Bind the correct texture mapsBind the correct texture maps
- • Find the plane of the surfaceFind the plane of the surface
- •^ Use GL_EYE_PLANE with surface planeUse GL_EYE_PLANE with surface plane
- • Scale and translate the texture coordsScale and translate the texturecoords
- • Combine (sum) the lightmapCombine (sum) thelightmap and the otherand the other texture on the surface (use multitexturingtexture on the surface (usemultitexturing GL_ADD capability)GL_ADD capability)
- • Render the surfaceRender the surface (^110)
BillboardsBillboards
(^11) 1
Billboards Billboards
(^11) 2
Billboard CloudsBillboard Clouds
(^11) 3
Slides adapted fromSlides adapted from
- •^ Siggraph 2004 OpenGlSiggraph 2004OpenGl coursecourse
- • U. Virginia Intro to GraphicsU. Virginia Intro to Graphics
- • Michael Gold, NVidiaMichael Gold,NVidia
- • Cass EverittCassEveritt,, NVidiaNVidia
- •^ Paul HeckbertPaulHeckbert,, NVidiaNVidia
(^11) 4
Slide CreditsSlide Credits
- • (^) Michael I. Gold,Michael I. Gold, NVidiaNVidia
- • (^) CassCass EverittEveritt,, NVidiaNVidia
- • PaulPaul HeckbertHeckbert,, NVidiaNVidia
Programming
(^115)