20/2/2006based on: angel (4th edition) & akeine-möller & haines (2nd edition)1 csc345:...

24
20/2/2006 Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition) 1 CSC345: Advanced Graphics & Virtual Environments Lecture 7: Textures (2) Patrick Olivier [email protected] 2 nd floor in the Devonshire Building

Post on 21-Dec-2015

215 views

Category:

Documents


1 download

TRANSCRIPT

20/2/2006 Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition) 1

CSC345: Advanced Graphics &Virtual Environments

Lecture 7: Textures (2)Patrick Olivier

[email protected] floor in the Devonshire Building

20/2/2006 Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition) 2

Textures: the story so far… textures/environment

maps/bump maps transformations between

three of more coordinates need a backward

mapping two-part mapping uses

an intermediate object cylinder/box/sphere

area averaging can address aliasing problems

v

20/2/2006 Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition) 3

(0,0) (1,0)

(1,1)(0,1)

(u0,v0)

(u1,v1)

(u2,v2)

(u,v) in range (0.0,1.0) but what if (u,v) >1.0 or <0.0 ?

to repeat textures use fractional part (e.g 5.3 -> 0.3)

different repeat patterns (repeat, mirror, clamp, border):

Textures coordinates

(2,2)

(-1,-1)

20/2/2006 Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition) 4

Textures in OpenGL1. specify the texture

• read or generate image• assign to texture• enable texturing

2. assign texture coordinates to vertices

• mapping function is left to application

3. specify texture parameters• wrapping• filtering

20/2/2006 Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition) 5

Example 1: specifying textures (1) define a texture image from an array of texels (texture elements) in memory:

Glubyte my_texels[512][512];

define as any other pixel map: scanned image generate by application code

enable texture mappingglEnable(GL_TEXTURE_2D);

OpenGL supports 1-4 dimensional texture maps

20/2/2006 Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition) 6

Example 1: specifying textures (2)glTexImage2D( target, level, components, w, h, border,

format, type, texels );

target: type of texture, e.g. GL_TEXTURE_2Dlevel: used for mipmapping (discussed later)components: elements per texelw, h: width and height of texels in pixelsborder: used for smoothing (discussed later)format and type: describe texelstexels: pointer to texel array

glTexImage2D( GL_TEXTURE_2D, 0, 3, 512, 512, 0, GL_RGB, GL_UNSIGNED_BYTE, my_texels);

20/2/2006 Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition) 7

Example 1: texture coordinates (1) Based on parametric texture

coordinates glTexCoord*() specified at each vertex

s

t1, 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

Texture Space Object Space

20/2/2006 Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition) 8

Example 1: texture coordinates (2)glBegin(GL_POLYGON);

glColor3f(r0, g0, b0); // if no shading usedglNormal3f(u0, v0, w0); // if shading usedglTexCoord2f(s0, t0);glVertex3f(x0, y0, z0);glColor3f(r1, g1, b1);glNormal3f(u1, v1, w1);glTexCoord2f(s1, t1);glVertex3f(x1, y1, z1);

.

.glEnd();

Note we can use vertex arrays to increase efficiency

20/2/2006 Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition) 9

GLfloat vertices[][3] = {{-1.0,-1.0,-1.0}, {1.0,-1.0,-1.0},

{1.0,1.0,-1.0}, {-1.0,1.0,-1.0},{-1.0,-1.0,1.0}, {1.0,-1.0,1.0}, {1.0,1.0,1.0}, {-1.0,1.0,1.0}};

GLfloat colors[][4] = { {0.0,0.0,0.0,0.5}, {1.0,0.0,0.0,0.5},

{1.0,1.0,0.0,0.5}, {0.0,1.0,0.0,0.5}, {0.0,0.0,1.0,0.5}, {1.0,0.0,1.0,0.5}, {1.0,1.0,1.0,0.5}, {0.0,1.0,1.0,0.5}};

void polygon(int a,int b,int c,int d){

glBegin(GL_POLYGON); glColor4fv(colors[a]); glTexCoord2f(0.0,0.0); glVertex3fv(vertices[a]); glColor4fv(colors[b]); glTexCoord2f(0.0,1.0); glVertex3fv(vertices[b]);

glColor4fv(colors[c]); glTexCoord2f(1.0,1.0); glVertex3fv(vertices[c]); glColor4fv(colors[d]); glTexCoord2f(1.0,0.0); glVertex3fv(vertices[d]); glEnd();

}

void colorcube(){

/* map vertices to faces */ polygon(0,3,2,1); polygon(2,3,7,6); polygon(0,4,7,3); polygon(1,2,6,5); polygon(4,5,6,7); polygon(0,1,5,4);

}

GLubyte image[64][64][3]; // in “main()” int i, j, r, c; for(i=0;i<64;i++) { for(j=0;j<64;j++) { c =((((i&0x8)==0)^((j&0x8))==0))*255; image[i][j][0]= (GLubyte) c; image[i][j][1]= (GLubyte) c; image[i][j][2]= (GLubyte) c; } }...glEnable(GL_TEXTURE_2D); glTexImage2D(GL_TEXTURE_2D,0,3,64,64,0, GL_RGB,GL_UNSIGNED_BYTE, image);...

20/2/2006 Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition) 10

Converting a texture image OpenGL requires texture dimensions to be

powers of 2 If dimensions of image are not powers of 2

gluScaleImage( format, w_in, h_in, type_in, *data_in, w_out, h_out, type_out, *data_out );data_in is source imagedata_out is for destination image

Image interpolated and filtered during scaling

20/2/2006 Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition) 11

Texture parametersOpenGL has a variety of parameters that determine how a texture is applied:

Wrapping parameters determine what happens if s and t are outside the (0,1) range

Filter modes allow us to use area averaging instead of point samples

Mipmapping allows us to use textures at multiple resolutions

Environment parameters determine how texture mapping interacts with shading

20/2/2006 Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition) 12

Wrapping mode Clamping: if s,t > 1 use 1, if s,t <0 use 0 Wrapping: use s,t modulo 1

glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP )

glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT )

s

t

GL_CLAMPGL_REPEAT

20/2/2006 Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition) 13

Magnification One texel cover more than one pixel? Box filter (nearest neighbour)

leads to “pixelation” (blocky) one texel per pixel (fast)glTexParameteri(GL_TEXTURE_2D,

GL_TEXURE_MAG_FILTER,

GL_NEAREST);

Bilinear interpolation interpolates neighbouring pixelsglTexParameteri(GL_TEXTURE_2D,

GL_TEXURE_MAG_FILTER,

GL_LINEAR);

GL_NEAREST

GL_LINEAR

20/2/2006 Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition) 14

Bilinear interpolation t(u,v) accesses the texture map b(u,v) filtered texel (u’,v’) = (pu – | pu |, pv – | pv |)

b(pu ,pv) = (1 – u’)(1 – v’)t(xl ,yb) +

u’(1 – v’)t(xr ,yb) +

(1 – u’)v’t(xl ,yt) + u’ v’ t(xr ,yt) Example:

(pu,pv) = (81.92,74.24)

(xl,yb) = (81,74)

(xr,yt) = (82,75)

20/2/2006 Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition) 15

Minification One pixel covers many texels?

Towards horizon many artefacts e.g. temporal aliasing (object motion)

Bilinear filtering (fails > 4 texels)glTexParameteri(GL_TEXTURE_2D,

GL_TEXURE_MIN_FILTER,

GL_LINEAR);GL_LINEAR

GL_NEAREST

20/2/2006 Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition) 16

Mipmapping Multum in parvo (MIP) Image pyramid Half width & height going upwards Average 4 ”parents” ”child texel” Depending on amount minification,

determine image to fetch from Compute d (λ in OpenGL) to give

two images: how to use the two images? but how to interpolate and calculate d?

20/2/2006 Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition) 17

Mipmapping: filtering Usual filtering options:

point sampling (nearest neighbour) linear filtering

Interpolate the bilinear values trilinear interpolation

Constant time filtering 8 texel accesses

dv

u

(u0,v0,d0)

Level n+1

Level n

GL_NEAREST

GL_LINEAR

20/2/2006 Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition) 18

Mipmapping: calculating d Approximate quad with square:

A = approximate area of of square

b = √ A

d = log2 b Gives overblur! Better: anisotropic texture filtering

approximate quad with several smaller mipmap samples

texelpixel projectedto texture space

20/2/2006 Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition) 19

Controls how texture is applied:glTexEnv{fi}[v]( GL_TEXTURE_ENV, prop, param)

GL_TEXTURE_ENV_MODE modes GL_MODULATE: modulates with computed shade GL_BLEND: blends with an environmental coloor GL_REPLACE: use only texture coloor GL(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,

GL_MODULATE); Set blend color with GL_TEXTURE_ENV_COLOR

20/2/2006 Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition) 20

Perspective correction hint Hints are… Texture coordinate and color interpolation

either linearly in screen space or using depth/perspective values (slower)

perspective projections are nonlinear w.r.t. depth Noticeable for polygons “on edge”

glHint(GL_PERSPECTIVE_CORRECTION_HINT, hint) Where hint is one of:

GL_DONT_CARE GL_NICEST GL_FASTEST

20/2/2006 Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition) 21

Generating texture coordinates OpenGL can generate texture

coordinates automatically: glTexGen{ifd}[v]()

Specify a plane & generate texture coordinates based upon distance from the plane

Generation modes GL_OBJECT_LINEAR GL_EYE_LINEAR GL_SPHERE_MAP

Utah teapot geometry (quads)

Same texture coordinates for each quad

Automatic texture coordinates (object space)

Automatic texture coordinates (eye space)

20/2/2006 Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition) 22

Texture Objects Texture is part of the OpenGL state

If we have different textures for different objects, OpenGL will be moving large amounts data from processor memory to texture memory

Recent versions OpenGL have texture objects one image per texture object texture memory can hold multiple texture objects

Significant improvements in performance

20/2/2006 Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition) 23

Textures and shading Must consider the interaction between texture

and shading, various possibilities: modulate texture colour with result of shading

(default) blend texture colour with shader colour only use the texture colour

To controls how texture is appliedglTexEnv{fi}[v](GL_TEXTURE_ENV, prop, param)

Some prop = GL_TEXTURE_ENV_MODE modes: GL_MODULATE: modulates with computed shade GL_BLEND: blends with an environmental colour GL_REPLACE: use only texture color

20/2/2006 Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition) 24

More advanced textures… Ripmaps and anisotropic filtering Environment Maps Bump maps Multitexturing 3D textures ....