25
An Interactive Introduction to OpenGL Programming 1 1 Pipelines and Texture Mapping Pipelines and Texture Mapping Texture mapping is done at Texture mapping is done at rasterization rasterization in canonical screen space When describing a scene, you assume that texture When describing a scene, you assume that texture interpolation will be done in world space interpolation will be done in world space Which property of perspective projection means that the Which property of perspective projection means that the wrong thing wrong thing” will happen if we apply our simple will happen if we apply our simple interpolations from the previous slide? interpolations from the previous slide? Perspective correct texture mapping Perspective correct texture mapping does the right thing, does the right thing, but at a cost but at a cost Is it a problem with orthographic viewing? Is it a problem with orthographic viewing? 2 Linear Interpolation of Textures Linear Interpolation of Textures

texture mapping 2010 - University of Utah College of

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

An Interactive Introduction to OpenGL Programming

1

1

Pipelines and Texture MappingPipelines and Texture Mapping

•• Texture mapping is done at Texture mapping is done at rasterizationrasterization

• in canonical screen space

•• When describing a scene, you assume that texture When 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 the Which property of perspective projection means that the ““wrong thingwrong thing”” will happen if we apply our simple will 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?

2

Linear Interpolation of TexturesLinear Interpolation of Textures

An Interactive Introduction to OpenGL Programming

2

3

Texture Index InterpolationTexture Index Interpolation

4

Texture Interpolation ProblemTexture Interpolation Problem

An Interactive Introduction to OpenGL Programming

3

5

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

6

Perspective Correct TexturingPerspective Correct Texturing

An Interactive Introduction to OpenGL Programming

4

7

Texture Mapping and the OpenGL PipelineTexture Mapping and the OpenGL Pipeline

geometry pipelinevertices

pixel pipelineimage

rasterizer

•• Images and geometry flow through Images and geometry flow through separate pipelines that join at the separate pipelines that join at the rasterizerrasterizer• “complex” textures do not affect geometric

complexity

8

Applying Textures IApplying Textures I

•• Three stepsThree steps specify texture

• read or generate image

• assign to texture

assign texture coordinates to vertices

specify texture parameters

• wrapping, filtering

An Interactive Introduction to OpenGL Programming

5

9

Applying Textures II

• specify textures in texture objects

• set texture filter

• set texture function

• set texture wrap mode

• set optional perspective correction hint

• bind texture object

• enable texturing

• supply texture coordinates for vertex

• coordinates can also be generated

• specify textures in texture objects

• set texture filter

• set texture function

• set texture wrap mode

• set optional perspective correction hint

• bind texture object

• enable texturing

• supply texture coordinates for vertex

• coordinates can also be generated

10

Texture Objects

•• Like display lists for texture imagesLike display lists for texture images• one image per texture object

• may be shared by several graphics contexts

•• Generate texture namesGenerate texture namesglGenTextures( n, *texIds );

An Interactive Introduction to OpenGL Programming

6

11

Texture Functions

•• Controls how texture is appliedControls how texture is appliedglTexEnv{fi}[v](glTexEnv{fi}[v]( GL_TEXTURE_ENV, prop, paramGL_TEXTURE_ENV, prop, param ))

••GL_TEXTURE_ENV_MODEGL_TEXTURE_ENV_MODE modesmodes•GL_MODULATE

•GL_BLEND

•GL_REPLACE

•• Set blend color with Set blend color with GL_TEXTURE_ENV_COLORGL_TEXTURE_ENV_COLOR

12

Is There Room for a Texture?

•• Query largest dimension of texture imageQuery largest dimension of texture image• typically largest square texture

• doesn’t consider internal format size

glGetIntegerv(glGetIntegerv( GL_MAX_TEXTURE_SIZE, &size GL_MAX_TEXTURE_SIZE, &size ))

•• Texture proxyTexture proxy• will memory accommodate requested texture size?

• no image specified; placeholder

• if texture won’t fit, texture state variables set to 0• doesn’t know about other textures• only considers whether this one texture will fit all of memory

An Interactive Introduction to OpenGL Programming

7

13

Texture Residency

•• Working set of texturesWorking set of textures• high-performance, usually hardware accelerated

• textures must be in texture objects

• a texture in the working set is resident

• for residency of current texture, check GL_TEXTURE_RESIDENT state

•• If too many textures, not all are residentIf too many textures, not all are resident• can set priority to have some kicked out first

• establish 0.0 to 1.0 priorities for texture objects

14

Tutorial: TextureTutorial: Texture

An Interactive Introduction to OpenGL Programming

8

15

Mathias stopped here on 8/31Mathias stopped here on 8/31

16

The Art of 3D Computer Animation and Effects

Isaac Kerlow

An Interactive Introduction to OpenGL Programming

9

17

18

An Interactive Introduction to OpenGL Programming

10

19

20

An Interactive Introduction to OpenGL Programming

11

Texture Coord GenerationTexture Coord Generation

22

glTexGenglTexGen

•• Powerful, flexible, underutilizedPowerful, flexible, underutilized• Contour mapping

• Reflection mapping

• Lighting effects

• Atmospheric effects

An Interactive Introduction to OpenGL Programming

12

23

24

glTexGen (cont’d)glTexGen (cont’d)

•• Generate texture coordinates from Generate texture coordinates from geometrygeometry• Object space

• texture is “attached” to object

• Eye space

• object moves within texture “field”

• Sphere map

• based on reflection vector

An Interactive Introduction to OpenGL Programming

13

25

Reference PlaneReference Plane

•• Uses plane equationUses plane equation• Ax + By +Cz = D

•• Computes dot productComputes dot product• coord = Ax + By + Cz + Dw

• coord is distance from plane

•• Computation is Computation is ““separableseparable””

26

Object Linear MappingObject Linear Mapping

•• Texture is Texture is ““attachedattached”” to objectto objectGLfloat 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);

An Interactive Introduction to OpenGL Programming

14

27

Object Linear SampleObject Linear Sample

•• Texture is Texture is ““attachedattached”” to objectto object

28

Object Linear MappingObject Linear Mapping

•• DemoDemo

An Interactive Introduction to OpenGL Programming

15

29

Eye Linear MappingEye Linear Mapping

•• Texture is Texture is ““fixedfixed”” in eye spacein eye spaceGLfloat params = {A,B,C,D};

glTexGenfv(GL_S, GL_EYE_PLANE, params);

glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);

glEnable(GL_TEXTURE_GEN_S);

•• Default mapping is identityDefault mapping is identity(s,t,r,q) = (Xe, Ye, Ze, We);

30

Eye Linear SampleEye Linear Sample

•• Texture is Texture is ““fixedfixed”” in eye spacein eye space

An Interactive Introduction to OpenGL Programming

16

31

Eye Linear MappingEye Linear Mapping

•• DemoDemo

32

UsesUses

•• Projected shadowsProjected shadows

•• SpotlightsSpotlights

•• Aligning screen space texturesAligning screen space textures• NPR “paper” background

•• Distance functionsDistance functions• point light attenuation

• texture fog

An Interactive Introduction to OpenGL Programming

17

33

Sphere MappingSphere Mapping

•• Based on reflection vectorBased on reflection vectorglTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);

glEnable(GL_TEXTURE_GEN_S);

•• S, T coordinates onlyS, T coordinates only

34

Sphere MapSphere Map

•• Show PDFShow PDF

An Interactive Introduction to OpenGL Programming

18

35

Phong ShadingPhong Shading

•• SpheremapSpheremap DetailsDetails

0

90 180

Eye TextureMap

Specular Map Texture looks like ahighlight on a sphere projected to aplane

90 degreeReflection

Unused

Center

36

Sphere MappingSphere Mapping

•• Based on reflection vectorBased on reflection vector

An Interactive Introduction to OpenGL Programming

19

37

Uses

•• Environment mappingEnvironment mapping

•• PrePre--computed computed specularspecular lighting lighting (recall lighting lecture)(recall lighting lecture)

38

Phong ShadingPhong Shading

•• Getting Highlights RightGetting Highlights Right

With texture GL_SEPARATE_SPECULAR_COLOR

An Interactive Introduction to OpenGL Programming

20

39

Getting Finer Highlight and Lighting FxGetting 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)

+Dynamic scene, lights

•• Play texture tricksPlay texture tricks• Compute the lighting beforehand, store in texture

• Need to recompute when scene/lights change

40

Texture TricksTexture Tricks

vs.

+

Precompute lighting,Fold into texture

Surface Texture plus Lightmap

An Interactive Introduction to OpenGL Programming

21

41

Phong ShadingPhong Shading

•• Sphere mapping approximates Sphere mapping approximates PhongPhongShadingShading

Texture coordinatesfrom reflection vector

(eye vector and normal)

Texture

(s,t)

(s,t)Interpolated

coordinates mapto texels

LE N LEN

LEN

... ...

Texel values computed fromPhong lighting equation

42

Why would texturing help?Why would texturing help?

•• Put linear interpolation BEFORE highly Put linear interpolation BEFORE highly nonnon--linear computation (EXP)linear computation (EXP)

shinynlightsspecular IkI cos

An Interactive Introduction to OpenGL Programming

22

43

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)

44

Phong ShadingPhong Shading

•• SpheremapSpheremap DetailsDetails

0

90 180

Eye TextureMap

Specular Map Texture looks like ahighlight on a sphere projected to aplane

90 degreeReflection

Unused

Center

An Interactive Introduction to OpenGL Programming

23

45

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

• Need additive texenv

46

An Interactive Introduction to OpenGL Programming

24

47

Phong ShadingPhong Shading

•• Steps to adding texture highlightsSteps to adding texture highlights

Diffuse Lit,Textured

White, Modulatedby Specular Texture

Combined Image(additive blending)

+ =

48

Phong ShadingPhong Shading

•• Creating Highlight TexturesCreating Highlight Textures• Build an environment map

• Sphere lit by object’s light

• Relative light position same as object

• Copy image into highlight texture

• New texture when light moves relative to viewer

• Object movement doesn’t require changing texture

An Interactive Introduction to OpenGL Programming

25

49

Phong ShadingPhong Shading

•• Compare the ResultsCompare the Results

Single passPhong Lighting

Gouraud Shading

Phong Lighting,Gouraud ShadingSeparate specular

Two passPhong Lighting,Phong Shading

50

End of Phong ShadingEnd of Phong Shading