OpenGL Texturing: Understanding Texture Mapping, Interpolation, and Application, Study notes of Computer Science

An interactive introduction to opengl programming, focusing on texture mapping, interpolation, and application. It covers topics such as texture mapping in world and screen space, texture interpolation, perspective correction hint, texture objects, and texture parameters. It also explains how to define, generate, and bind texture objects, and discusses various methods for specifying texture coordinates and controlling texture data layout.

Typology: Study notes

Pre 2010

Uploaded on 08/31/2009

koofers-user-9qj
koofers-user-9qj 🇺🇸

10 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
An Interactive Introduction to OpenGL
Programming
1
Texture Mapping
Texture Mapping
Thanks to Ed Angel, UNM
Thanks to Ed Angel, UNM
2
Texture Mapping
Texture Mapping
The soup tin is easily described by pasting a label on the
The soup tin is easily described by pasting a label on the
plain cylinder
plain cylinder
Texture mapping
Texture mapping associates the color of a po int with the
associates the color of a point with the
color in an image: the
color in an image: the texture
texture
Soup tin: Each point on the cylinder get the label’s color
Question to address: Which point of the texture do we
Question 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 a
Establish a mapping
mapping from surface points to image points
from 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 geometric
Apply a 1D, 2D, or 3D image to geometric
primitives
primitives
Uses of Texturing
Uses of Texturing
simulating materials
reducing geometric complexity
image warping
reflections
Texture
Mapping
Texture
Mapping CPU
CPU DL
DL
Poly.
Poly. Per
Vertex
Per
Vertex
Raster
Raster Frag
Frag FB
FB
Pixel
Pixel Texture
Texture
5
Texture Mapping
Texture Mapping
s
t
x
y
z
image
geometry screen
6
Texture Interpolation
Texture Interpolation
Specify where the
Specify where the
vertices in world
vertices in world
space are mapped to
space are mapped to
in texture space
in texture space
Linearly interpolate
Linearly interpolate
the mapping for other
the mapping for other
points in world space
points in world space
Straight lines in world
space go to straight lines
in texture space
Texture map
s
t
Triangle in
world space
pf3
pf4
pf5

Partial preview of the text

Download OpenGL Texturing: Understanding Texture Mapping, Interpolation, and Application and more Study notes Computer Science in PDF only on Docsity!

Programming

Texture Mapping Texture Mapping

Thanks to Ed Angel, UNMThanks to Ed Angel, UNM

2

Texture Mapping Texture 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. (^) VertexPer Per Vertex RasterRaster FragFrag FBFB

PixelPixel

TextureTexture

5

Texture Mapping Texture Mapping

s

t

x

y

z

image

geometry screen

6

Texture Interpolation Texture 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 3 1

1 1 3 1

1 1 s y y

y y s y y

yy s (^) R (^) ⎟⎟ ⎠

3 3 2

2 2 3 2

1 2 s y y

y y s y y

yy s (^) L (^) ⎟⎟ ⎠

R R L

L L R L

L (^) s x x

xx s x x

xx s (^) ⎟⎟ ⎠

8

Pipelines and Texture Mapping Pipelines 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 thing” will happen if we apply our simple“wrong thing” 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?

9

Linear Interpolation of Textures Linear Interpolation of Textures

(^10)

Texture Index Interpolation Texture Index Interpolation

1 1

Texture Interpolation Problem Texture Interpolation Problem

1 2

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 “on edge”Noticeable for polygons “on edge”

glHint( glHint( GL_PERSPECTIVE_CORRECTION_HINT,GL_PERSPECTIVE_CORRECTION_HINT, hinthint ))

where hint is one of

• GL_DONT_CARE

• GL_NICEST

• GL_FASTEST

Programming

1 9

• •^ 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, components,level, components,

w,w, h,h, border, format, type, *texelsborder, format, type, *texels ););

• dimensions of image must be 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. (^) VertexPer Per Vertex RasterRaster FragFrag FBFB

PixelPixel

TextureTexture

2 0

Converting A Texture Image

• • If dimensions of image are not power of 2If dimensions of image are not power of 2

gluScaleImage( gluScaleImage( format, w_in, h_in,format, w_in, h_in,

type_in,type_in, data_in,data_in, w_out,w_out, h_out,h_out,

type_out,type_out, data_outdata_out ););

• *_in is for source image

• *_out is for destination image

• •^ Image interpolated and filtered duringImage interpolated and filtered during

scalingscaling

(^21)

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.

(^22)

• • Based on parametric texture coordinatesBased on parametric texture coordinates

• • glTexCoordglTexCoord()() specified at each vertexspecified at each vertex

s

t

(s, t) = (0.2, 0.8)

A

B C

a

b

c

Texture Space Object Space

Mapping a

Texture

Mapping a

Texture

CPUCPU DLDL

Poly.Poly. (^) VertexPer Per Vertex RasterRaster FragFrag FBFB

PixelPixel

TextureTexture

2 3

Basic OpenGL Texturing Basic OpenGL Texturing

MORE…

2 4

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)

Programming

2 5

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:

• glTexEnvf(…)

• • Texturing is doneTexturing is done afterafter lightinglighting

2 6

Nasty Details Nasty Details

• • There are a large range of functions for controlling the layoutThere are a large range of functions for controlling the layout ofof

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

(^27)

Example Texture Map Example Texture Map

glVertex3d (s, s, s) glTexCoord2d(1,1);

glVertex3d (-s, -s, -s) glTexCoord2d(0, 0);

(^28)

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_REPEAT

wrapping

GL_CLAMP

wrapping

s

t

2 9

Texture Functions

• •^ Controls how texture is appliedControls how texture is applied

glTexEnv{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 withSet blend color with

GL_TEXTURE_ENV_COLORGL_TEXTURE_ENV_COLOR

3 0

Basic OpenGL Texturing Basic OpenGL Texturing

MORE…

Programming

3 7

Tutorial: Texture Tutorial: Texture

3 8

The Art of 3D Computer Animation and Effects Isaac Kerlow

(^3940)

4 1

4 2