Computer Graphics Course Computer Graphics Course 2006 2006 - - PowerPoint PPT Presentation

computer graphics course computer graphics course 2006
SMART_READER_LITE
LIVE PREVIEW

Computer Graphics Course Computer Graphics Course 2006 2006 - - PowerPoint PPT Presentation

Computer Graphics Course Computer Graphics Course 2006 2006 OpenGL OpenGL Texture Mapping Texture Mapping Based on the slides from the University of Virgina CG course and Angels Interactive Computer Graphics, Addison-Wisley 2005


slide-1
SLIDE 1

Computer Graphics Course Computer Graphics Course 2006 2006 OpenGL OpenGL Texture Mapping Texture Mapping

Based on the slides from the University of Virgina CG course and Angel’s “Interactive Computer Graphics”, Addison-Wisley 2005

slide-2
SLIDE 2

Texture Mapping Texture Mapping

  • Texture map is an image, two

Texture map is an image, two-

  • dimensional array of

dimensional array of color values ( color values (texels texels) )

  • Texels

Texels are specified by texture are specified by texture’ ’s (u,v) space s (u,v) space

  • At each screen pixel,

At each screen pixel, texel texel can be used to substitute a can be used to substitute a polygon polygon’ ’s surface property (color) s surface property (color)

  • We must map (u,v) space to polygon

We must map (u,v) space to polygon’ ’s (s, t) space s (s, t) space

U V S T

slide-3
SLIDE 3

Texture Mapping Texture Mapping

  • (u,v) to (s,t) mapping can be explicitly set at

(u,v) to (s,t) mapping can be explicitly set at vertices by storing vertices by storing texture coordinates texture coordinates with each with each vertex vertex

  • How do we compute (u,v) to (s,t) mapping for

How do we compute (u,v) to (s,t) mapping for points in between points in between

  • Watch for aliasing

Watch for aliasing

  • Watch for many to one mappings

Watch for many to one mappings

  • Watch for perspective foreshortening effects and

Watch for perspective foreshortening effects and linear interpolation linear interpolation

slide-4
SLIDE 4

Texture coordinates Texture coordinates

To tell OpenGL how to map texture, it is necessary to tell it a texture coordinate for each vertex 3-D vertex coordinates 2-D texture coordinates

slide-5
SLIDE 5

OpenGL textures OpenGL textures

OpenGL textures have dimensions (x and y [and z]) are powers of 2 512 256 256 256 64 512 If the image you want to use as a texture isn’t sized like this, you can either stretch it appropriately before using it as an OpenGL texture,

  • r you can paste it into a sub-region of a valid OpenGL texture

320 240 512 256 512 256

slide-6
SLIDE 6

Texture coordinate values Texture coordinate values

512 256 256 256 64 512 All OpenGL textures span the range [0,1] in both x and y 512 256

0.0 1.0 0.0 1.0 0.0 1.0 0.0 1.0 0.0 1.0 1.0 0.0 0.0 1.0 0.0 1.0 0.625 0.9375

320 240

( )

slide-7
SLIDE 7

Unnecessary Limitation Unnecessary Limitation

  • Extensions

Extensions

  • GLEW

GLEW

slide-8
SLIDE 8

Example Texture Map Example Texture Map

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

slide-9
SLIDE 9

Example Texture Map Example Texture Map

glTexCoord2d(5, 5); glVertex3d (s, s, s); glTexCoord2d(1, 1); glVertex3d (s, s, s); Repeating textures vs. Clamped textures

slide-10
SLIDE 10

Texture Coordinates Texture Coordinates

  • Every polygon can have object coordinates and

Every polygon can have object coordinates and texture coordinates texture coordinates

  • Object coordinates describe where polygon vertices

Object coordinates describe where polygon vertices are on the screen are on the screen

  • Texture coordinates describe

Texture coordinates describe texel texel coordinates of coordinates of each vertex (usually 0 each vertex (usually 0 -

  • > 1)

> 1)

  • Texture coordinates are interpolated along vertex

Texture coordinates are interpolated along vertex-

  • vertex edges

vertex edges

  • glTexCoord{1234}{sifd}(TYPE

glTexCoord{1234}{sifd}(TYPE coords coords) )

Why 14 coords? s, t, r, and q (for homogeneous coordinates, an advanced topic)

slide-11
SLIDE 11

Texture objects Texture objects

Since version 1.2, OpenGL has supported the idea of texture objects This allows multiple, independent textures to be used One texture at a time is active As usual we start with glEnable(GL_TEXTURE_2D); Texture objects are created, deleted and each is given a name (an unsigned integer identifier) 1 2 3

1-D & 3-D textures are possible

slide-12
SLIDE 12

Create Texture Objects Create Texture Objects

  • glGenTextures(1,

glGenTextures(1, &texture[texture_num]); &texture[texture_num]);

  • First argument tells GL how many Texture

First argument tells GL how many Texture Objects to create Objects to create

  • Second argument is a pointer to the place where

Second argument is a pointer to the place where OpenGL will store the names (unsigned integers) OpenGL will store the names (unsigned integers)

  • f the Texture Objects it creates
  • f the Texture Objects it creates
  • texture[ ] is of type

texture[ ] is of type GLuint GLuint

slide-13
SLIDE 13

Specify which texture Specify which texture

  • bject is about to be defined
  • bject is about to be defined
  • Tell OpenGL that you are going to define the

Tell OpenGL that you are going to define the specifics of the Texture Object it created specifics of the Texture Object it created

  • glBindTexture(GL_TEXTURE_2D,

glBindTexture(GL_TEXTURE_2D, texture[texture_num]); texture[texture_num]);

  • Textures can be 1D and 3D as well

Textures can be 1D and 3D as well

slide-14
SLIDE 14

Begin defining texture Begin defining texture

  • glTexParameter

glTexParameter() ()

  • Sets various parameters that control how a texture is treated

Sets various parameters that control how a texture is treated as it as it’ ’s applied to a fragment or stored in a texture object s applied to a fragment or stored in a texture object

  • // scale linearly when image bigger than texture

// scale linearly when image bigger than texture glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE _MAG_FILTER,GL_LINEAR); _MAG_FILTER,GL_LINEAR);

  • // scale linearly when image smaller than texture

// scale linearly when image smaller than texture glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE _MIN_FILTER,GL_LINEAR); _MIN_FILTER,GL_LINEAR);

slide-15
SLIDE 15

Wrapping Mode Wrapping Mode

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

glTexParameteri glTexParameteri( GL_TEXTURE_2D, ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP ) GL_TEXTURE_WRAP_S, GL_CLAMP ) glTexParameteri glTexParameteri( GL_TEXTURE_2D, ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT ) GL_TEXTURE_WRAP_T, GL_REPEAT ) texture s t GL_CLAMP wrapping GL_REPEAT wrapping

slide-16
SLIDE 16

Assign image data Assign image data

  • glTexImage2D();

glTexImage2D();

GL_TEXTURE_2D GL_TEXTURE_2D (2D Texture) (2D Texture) (level of detail 0) (level of detail 0) 3 3 (3 components, RGB) (3 components, RGB) image1 image1-

  • >

>sizeX sizeX (size) (size) image1 image1-

  • >

>sizeY sizeY (size) (size) (no border pixel) (no border pixel) GL_RGB GL_RGB (RGB color order) (RGB color order) GL_UNSIGNED_BYTE GL_UNSIGNED_BYTE (unsigned byte data) (unsigned byte data) image1 image1-

  • >data

>data (pointer to the data)) (pointer to the data))

slide-17
SLIDE 17

glTexImage2D glTexImage2D – – Arg Arg 1 1

  • GLenum

GLenum target target

  • GL_TEXTURE_{1|2|3}D

GL_TEXTURE_{1|2|3}D

  • That

That’ ’s what you will use for the exercise s what you will use for the exercise

  • GL_TEXTURE_RECTANGLE_ARB

GL_TEXTURE_RECTANGLE_ARB

  • Extensions

Extensions

  • That

That’ ’s what you will use for the fun at your home machine s what you will use for the fun at your home machine

  • GL_PROXY_TEXTURE_2D

GL_PROXY_TEXTURE_2D

  • Provides queries for texture resources

Provides queries for texture resources

  • Proceed with hypothetical texture use (GL won

Proceed with hypothetical texture use (GL won’ ’t apply the texture) t apply the texture)

  • After query, call

After query, call GLGetTexLevelParamter GLGetTexLevelParamter to verify presence of to verify presence of required system components required system components

  • Doesn

Doesn’ ’t check possibility of multiple texture interference t check possibility of multiple texture interference

slide-18
SLIDE 18

glTexImage2D glTexImage2D – – Arg Arg 2 2

  • GLint

GLint level level

  • Used for Level of Detail (LOD)

Used for Level of Detail (LOD)

  • LOD stores multiple versions of texture that can be

LOD stores multiple versions of texture that can be used at runtime (set of sizes) used at runtime (set of sizes)

  • Runtime algorithms select appropriate version of

Runtime algorithms select appropriate version of texture texture

  • Pixel size of polygon used to select best texture

Pixel size of polygon used to select best texture

  • Eliminates need for error

Eliminates need for error-

  • prone filtering algorithms

prone filtering algorithms

slide-19
SLIDE 19

glTexImage2D glTexImage2D – – Arg Arg 3 3

  • GLint

GLint internalFormat internalFormat

  • GL defines 38 symbolic constants that describe which of R,

GL defines 38 symbolic constants that describe which of R, G, B, and A are used in internal representation of G, B, and A are used in internal representation of texels texels

  • GL_RGB, GL_RGBA

GL_RGB, GL_RGBA

  • Provides control over things texture can do

Provides control over things texture can do

  • High bit depth alpha blending

High bit depth alpha blending

  • High bit depth intensity mapping

High bit depth intensity mapping

  • General purpose RGB

General purpose RGB

  • GL doesn

GL doesn’ ’t guarantee all options are available on given t guarantee all options are available on given hardware hardware

slide-20
SLIDE 20

glTexImage2D glTexImage2D – – Args Args 4 4-

  • 6

6

  • GLsizei

GLsizei width width

  • GLsizei

GLsizei height height

  • Dimensions of texture image

Dimensions of texture image

  • Must be 2

Must be 2m

m + 2b (b=0 or 1 depending on border)

+ 2b (b=0 or 1 depending on border)

  • min, 64 x 64

min, 64 x 64

  • GLint

GLint border border

  • Width of border (1 or 0)

Width of border (1 or 0)

  • Border allows linear blending between overlapping textures

Border allows linear blending between overlapping textures

  • Useful when manually tiling textures

Useful when manually tiling textures

slide-21
SLIDE 21

glTexImage2D glTexImage2D – – Args Args 7 & 8 7 & 8

  • GLenum

GLenum format format

  • Describe how texture data is stored in input array

Describe how texture data is stored in input array

  • GL_RGB, GL_RGBA, GL_BLUE

GL_RGB, GL_RGBA, GL_BLUE… …

  • GLenum

GLenum type type

  • Data size of array components

Data size of array components

  • GL_SHORT, GL_BYTE, GL_INT

GL_SHORT, GL_BYTE, GL_INT… …

slide-22
SLIDE 22

glTexImage2D glTexImage2D – – Arg Arg 9 9

  • Const

Const GLvoid GLvoid * *texels texels

  • Pointer to data describing texture map

Pointer to data describing texture map

slide-23
SLIDE 23

Apply texture Apply texture

  • Before defining geometry

Before defining geometry

  • glEnable(GL_TEXTURE_2D);

glEnable(GL_TEXTURE_2D);

  • glBindTexture(GL_TEXTURE_2D,

glBindTexture(GL_TEXTURE_2D, texture[0]); texture[0]);

  • glTexEnvf(GL_TEXTURE_ENV

glTexEnvf(GL_TEXTURE_ENV, , GL_TEXTURE_ENV_MODE, GL_REPLACE); GL_TEXTURE_ENV_MODE, GL_REPLACE);

  • GL_TEXTURE_ENV is the only possible

GL_TEXTURE_ENV is the only possible value value

slide-24
SLIDE 24

glTexEnv glTexEnv() ()

GL_TEXTURE_ENV_MODE GL_TEXTURE_ENV_MODE GL_DECAL GL_DECAL (alpha blends texture with

(alpha blends texture with poly color) poly color)

GL_REPLACE GL_REPLACE (straight up

(straight up replacement) replacement)

GL_MODULATE GL_MODULATE (texture application

(texture application is a function of poly lighting) is a function of poly lighting)

GL_BLEND GL_BLEND (texture controls blending

(texture controls blending with another color) with another color)

If GL_BLEND selected, second call to If GL_BLEND selected, second call to glTexEnv glTexEnv() must specify () must specify GL_TEXTURE_ENV_COLOR GL_TEXTURE_ENV_COLOR 4 4-

  • float array for R,G,B,A blend

float array for R,G,B,A blend

First argument to function is always GL_TEXTURE_ENV

slide-25
SLIDE 25

gluScaleImage gluScaleImage() ()

  • Alters the size of an image to meet the 2

Alters the size of an image to meet the 2m

m size

size requirement of OpenGL requirement of OpenGL

  • Scaling performed by linear and box filtering

Scaling performed by linear and box filtering

slide-26
SLIDE 26

glCopyTexImage2D() glCopyTexImage2D()

  • Use current frame buffer contents as texture

Use current frame buffer contents as texture

  • Copy frame buffer to named texture location

Copy frame buffer to named texture location

slide-27
SLIDE 27

glTexSubImage2D() glTexSubImage2D()

  • Replace a region of current working texture with

Replace a region of current working texture with a smaller texture a smaller texture

  • SubImage

SubImage need not adhere to 2 need not adhere to 2m

m size limitation

size limitation

  • This is how you add data from your system

This is how you add data from your system’ ’s s camera to GL environment camera to GL environment

  • glCopyTexSubImage2D

glCopyTexSubImage2D

  • Frame buffer cut and paste possible too

Frame buffer cut and paste possible too

slide-28
SLIDE 28

Filtering Filtering

  • OpenGL tries to pick best

OpenGL tries to pick best mipmap mipmap level level

  • Question:

Question: Which Which texel texel corresponds to a particular corresponds to a particular pixel? pixel?

  • GL_NEAREST (Point Sampling)

GL_NEAREST (Point Sampling)

  • Pick the

Pick the texel texel with center nearest pixel with center nearest pixel

  • GL_LINEAR (Bilinear Sampling)

GL_LINEAR (Bilinear Sampling)

  • Weighted average of 2x2 closest

Weighted average of 2x2 closest texels texels

  • GL_NEAREST_MIPMAP_LINEAR

GL_NEAREST_MIPMAP_LINEAR

  • Average nearest

Average nearest texels texels from two from two mipmap mipmap levels levels

  • GL_LINEAR_MIPMAP_LINEAR (

GL_LINEAR_MIPMAP_LINEAR (Trilinear Trilinear) )

  • Average two averaged

Average two averaged texels texels from two from two mipmaps mipmaps

slide-29
SLIDE 29

Texture Mapping Texture Mapping

  • More details

More details

  • Mip

Mip mapping mapping

  • Perspective correction hint

Perspective correction hint

  • Environment mapping

Environment mapping

  • Different data layouts

Different data layouts

  • Practical advice

Practical advice

  • Glance at the existing source code (Red Book is a

Glance at the existing source code (Red Book is a good place to start) good place to start)