Computer Graphics OpenGL - Texture mapping. OpenGL Texture mapping zTexture Mapping allows us to...

30
Computer Graphics OpenGL - Texture mapping

Transcript of Computer Graphics OpenGL - Texture mapping. OpenGL Texture mapping zTexture Mapping allows us to...

Page 1: Computer Graphics OpenGL - Texture mapping. OpenGL Texture mapping zTexture Mapping allows us to glue an image on polygons. In this method we can produce.

Computer Graphics

OpenGL - Texture mapping

Page 2: Computer Graphics OpenGL - Texture mapping. OpenGL Texture mapping zTexture Mapping allows us to glue an image on polygons. In this method we can produce.

OpenGL Texture mapping

Texture Mapping allows us to glue an image on polygons. In this method we can produce objects with many details while the geometrical model is simple.

Think of a radio, we can use a simple cube as the geometrical model and apply different texture images on each face of the cube. (no need for modeling fine details such as buttons, panels…)

Page 3: Computer Graphics OpenGL - Texture mapping. OpenGL Texture mapping zTexture Mapping allows us to glue an image on polygons. In this method we can produce.

What is a Texture?

A texture image is a rectangular array of pixel data

2 1 3Usually a D array, but can be D or D

A "texture pixel" is called a "texel”

cccccc ccccccccc cccccc ccccc ccccccccccc,/ - Raster (framebuffer) images such as .bmp, .tif, .rgb a

- nd etc. , can be used as textures.

- - In OpenGL 1.0 1.1, three dimensional textures are av 3ailable if the EXT_texture D extension is supported. S tarting with OpenGL 1.2, 3D textures are a core featur

.

Page 4: Computer Graphics OpenGL - Texture mapping. OpenGL Texture mapping zTexture Mapping allows us to glue an image on polygons. In this method we can produce.

What is Texture Mapping?

Applying textures (images) to polygons. The basic texture mapping steps are:

Specify the texture.glTexImagegluScaleImage

Indicate how the texture is to be applied to each pixel.glTexEnv glTexParameter

Enable texture mapping.glEnable Draw the scene, providing geometric and texture coordinates

.glTexCoord

Page 5: Computer Graphics OpenGL - Texture mapping. OpenGL Texture mapping zTexture Mapping allows us to glue an image on polygons. In this method we can produce.

Specifying a 2D Texture

target - target texture level - resolution level internalformat - internal storage format of the te

xture width - width of the texture image height - height of the texture image border - the width of the border (either 0 or 1) format - format of the pixel data type - data type of the pixel data pixels - pointer to the image data in memory

void glTexImage2D( GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const GLvoid *pixels)

Page 6: Computer Graphics OpenGL - Texture mapping. OpenGL Texture mapping zTexture Mapping allows us to glue an image on polygons. In this method we can produce.

What if the Texture is the Wrong Size?

2() requires a texture whose pixel dimensions are pow ers of two

If necessary, use the OpenGL utility routine gluScaleImage() to scale the image

P PPP PPP PPPPPP PP PPP PP 2glTexImage D()

Notes:

gluScaleImage()uses linear interpolation and box filtering to scale an image to the requested size.

When shrinking an image, gluScaleImage()uses a box filter to sample the source image and create pixels for the destination image.

When magnifying an image, the pixels from the source image are linearly interpolated to create the destination image.

Page 7: Computer Graphics OpenGL - Texture mapping. OpenGL Texture mapping zTexture Mapping allows us to glue an image on polygons. In this method we can produce.

Scaling Texture Images

GLint gluScaleImage( GLenum format, GLsizei widthin, GLsizei heightin, GLenum typein, const void *datain, GLsizei widthout, GLsizei heightout, GLenum typeout, void *dataout )

format specifies the format of the pixel data widthin, heightin specify the dimensions of the source image to be scaled

datain specifies a pointer to the source image widthout, heightout specify the dimensions for the scaled image

dataout specifies a pointer where the scaled image is to be stored

typein and typeout specify the data type for datain and dataout, respectively

Page 8: Computer Graphics OpenGL - Texture mapping. OpenGL Texture mapping zTexture Mapping allows us to glue an image on polygons. In this method we can produce.

ccccccc ccccccc cccccc ccccccc

Notes: format can be one of: GL_COLOR_INDEX, GL_RED, GL_GREEN, GL_BLUE, GL_ALPHA, GL_RGB, GL_RGBA, GL_LUMINANCE, or GL_LUMINANCE_ALPHA.

typein and typeout can be one of: GL_UNSIGNED_BYTE, GL_BYTE, GL_BITMAP, GL_UNSIGNED_SHORT, GL_SHORT, GL_UNSIGNED_INT, GL_INT, or GL_FLOAT.

The storage pointed to by dataout must be allocated by the user.

widthout and heightout must be powers of two (plus border).

Page 9: Computer Graphics OpenGL - Texture mapping. OpenGL Texture mapping zTexture Mapping allows us to glue an image on polygons. In this method we can produce.

Texture Coordinates

Texture coordinates are part of the data that i s associated with each vertex. A 2D texture is treated as a 1x1 square whose textur

e coordinates go from 0.0 to 1.0 in each dimension. Texture coordinates are homogeneous ( s, t, r, q)

r is unused and q is 1.0

Texture coordinates are assigned to each vertex of apolygon.

Assigned explicitly or generated automatically

Texture coordinates are interpolated as a polygon isfilled

Filters control how the interpolation is performed

Page 10: Computer Graphics OpenGL - Texture mapping. OpenGL Texture mapping zTexture Mapping allows us to glue an image on polygons. In this method we can produce.

Setting Texture Coordinates Explicitly

void glTexCoord2fv( const GLfloat *v ) v Specifies a pointer to an array of two, which in turn specify the s, t, r, and q texture coordinates

Example:

glBegin( GL_QUADS ); glTexCoord2fv( t0 ); glVertex3fv( v0 ); glTexCoord2fv( t1 ); glVertex3fv( v1 ); glTexCoord2fv( t2 ); glVertex3fv( v2 ); glTexCoord2fv( t3 ); glVertex3fv( v3 ); glEnd(); There are many variations of glTexCoord for specifying texture co

ordinates in one, two, three, or four dimensions. glTexCoord1 sets the current texture coordinates to (s, 0, 0,1); a call to glTexCoord2 sets them to (s, t, 0, 1). Similarly, glTexCoord3 specifies the textur

e coordinates as (s, t, r, 1), and glTexCoord4 defines all four comp onents explicitly as (s, t, r, q).

Texture coordinates can be specified in scalar or vector form as sh ort, int, float, or double.

Page 11: Computer Graphics OpenGL - Texture mapping. OpenGL Texture mapping zTexture Mapping allows us to glue an image on polygons. In this method we can produce.

Which Texel Goes With Which Pixel?

ccc ccccc cccccc ccccccccccc cc ccc ccccc cc ccc ccccc cccccc cccccc

OpenGL provides several filters to magnify or minify the te cccc ccccc c ccc cc c ccc c ccccc-ccc cccc ccc ccccc c.

nd image quality. The texture magnification filter is used when the pixel

being textured maps to an area less than or equal to on e texture element.

The texture minification filter is used whenever the pix el being textured maps to an area greater than one tex

ture element. In this case, the texture information mus t be minified.

Page 12: Computer Graphics OpenGL - Texture mapping. OpenGL Texture mapping zTexture Mapping allows us to glue an image on polygons. In this method we can produce.

Specifying Filters

void glTexParameterf( GLenum target, GLenum pname, const GLfloat param )

target specifies the type of the target texture

GL_TEXTURE_1D or GL_TEXTURE_2D

pname specifies which filter to set

GL_TEXTURE_MAG_FILTER or GL_TEXTURE_MIN_FILTER

param specifies whether speed or image quality is more important

GL_NEAREST to choose the texel nearest to the texture coordinate computed for the pixel.

GL_LINEAR to use the weighted average of the four texels nearest to the texture coordinate computed for the pixel.

Page 13: Computer Graphics OpenGL - Texture mapping. OpenGL Texture mapping zTexture Mapping allows us to glue an image on polygons. In this method we can produce.

Enabling Texture Mapping

void glEnable( GLenum mode ) Set mode to GL_TEXTURE_1D if one-dimensional texturing is performed using glTexImage1D()

Set mode to GL_TEXTURE_2D if two-dimensional texturing is performed using glTexImage2D()

Set mode to GL_TEXTURE_3D_EXT if three-dimensional texturing is performed using glTexImage3DEXT()

Page 14: Computer Graphics OpenGL - Texture mapping. OpenGL Texture mapping zTexture Mapping allows us to glue an image on polygons. In this method we can produce.

Specifying Texture Coordinates

It’s about where to map the texture and how to ori ent the texture before mapping it glTexCoord commandspeci fi es t he mappi ng bet ween coor di n

ates and polygon vertexes. ccc cccccccccc cccccc cc glTexCoord is associated wit

h the vertex specifies in the next glVertex . :glBegin(GL_QUADS); glTexCoord2d(0.0, 0.0); glVertex3d(-s, -s, -s);

glTexCoord2d(1.0, 0.0); glVertex3d(-s, -s, s);

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

glTexCoord2d(0.0, 1.0); glVertex3d(-s, s, -s);glEnd();

Page 15: Computer Graphics OpenGL - Texture mapping. OpenGL Texture mapping zTexture Mapping allows us to glue an image on polygons. In this method we can produce.

Specifying Texture Coordinates (cont.)

cccccc cccccc c-cc-cc-cc( 0 , 0 ) ( , , )

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnopen/html/msdn_gl7.asp

Page 16: Computer Graphics OpenGL - Texture mapping. OpenGL Texture mapping zTexture Mapping allows us to glue an image on polygons. In this method we can produce.

Specifying Texture Coordinates (cont.)

cccccccc c ccccccc

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnopen/html/msdn_gl7.asp

Page 17: Computer Graphics OpenGL - Texture mapping. OpenGL Texture mapping zTexture Mapping allows us to glue an image on polygons. In this method we can produce.

Repeating Textures

To repeat a texture 1 0Usetexturecoor di nat es gr eat er t hen .glBegin(GL_QUADS); glTexCoord2d(0.0, 0.0); glVertex3d(-s, -s, -s);

glTexCoord2d(5.0, 0.0); glVertex3d(-s, -s, s);

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

glTexCoord2d(0.0, 5.0); glVertex3d(-s, s, -s);glEnd();

Set the following two texture parameters glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

Page 18: Computer Graphics OpenGL - Texture mapping. OpenGL Texture mapping zTexture Mapping allows us to glue an image on polygons. In this method we can produce.

Repeating Textures (cont.)

Specifying texture coor 1 0dinate> .

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnopen/html/msdn_gl7.asp

Page 19: Computer Graphics OpenGL - Texture mapping. OpenGL Texture mapping zTexture Mapping allows us to glue an image on polygons. In this method we can produce.

Repeating Textures (cont.)

10 10Specifying texture coordinate < . or = .

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnopen/html/msdn_gl7.asp

Page 20: Computer Graphics OpenGL - Texture mapping. OpenGL Texture mapping zTexture Mapping allows us to glue an image on polygons. In this method we can produce.

- - - What if I want to texture map to not a plane shapes

If you are using OpenGL Quadrics, then you can project a textu re on a quadric surface letting OpenGL compute the coordinate

. First you must enable a couple things:

glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);

glEnable(GL_TEXTURE_GEN_S);glEnable(GL_TEXTURE_GEN_T);

GL_SPHERE_MAP works best on quadrics, but GL_OBJECT_LINEAR and GL_EYE_LINEAR may be more what you are looking for.

When creating your Quadric make this call to have OpenGL create the texture coordinates:gluQuadricTexture(ptrQuad, GL_TRUE);

Then make the Texture calls as you normally would.

Page 21: Computer Graphics OpenGL - Texture mapping. OpenGL Texture mapping zTexture Mapping allows us to glue an image on polygons. In this method we can produce.

Setting Texture Environment

c ccccccc ccccccccccc ccccccccc ccc ccccccc cccccc ccc interpretedwhen a f r agment i s t ext ur ed.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnopen/html/msdn_gl7.asp

glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);

Page 22: Computer Graphics OpenGL - Texture mapping. OpenGL Texture mapping zTexture Mapping allows us to glue an image on polygons. In this method we can produce.

Setting Texture Environment

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnopen/html/msdn_gl7.asp

glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

Page 23: Computer Graphics OpenGL - Texture mapping. OpenGL Texture mapping zTexture Mapping allows us to glue an image on polygons. In this method we can produce.

Multiple Textures

A set of textures and their related state can be treated as a single object.

Texture objects let you create as many textures as you need. You bind a name with a texture object when you create it, then

define the image data and parameters of the texture. As you render your scene, bind the name of each desired textur

e object to the appropriate texture target. The texture names become aliases for the textures currently bound

to them. Since binding (reusing) a texture takes less time than defining (

and reloading) one, this is a more efficient way to switch from o ne texture to another.

So, if you are using more than one texture, always set up the te xtures as texture objects!

Page 24: Computer Graphics OpenGL - Texture mapping. OpenGL Texture mapping zTexture Mapping allows us to glue an image on polygons. In this method we can produce.

ccccc ccccccc ccccccc

void glGenTextures(GLsize n, GLuint *texnames) cccccccc ccccccc ccccc

void glBindTexture(GLenum target, GLuint texname) Create a new texture object and assign it a name (first ti

) 1 2Bindanamedtexturetoat ext ur i ng t ar get (ei t her TEXTURE_ Dor TEXTURE_

) Call with texname = 0 to stop using texture objects (unbind)

void glPrioritizeTextures(GLsize n, const GLuint *texnames, const GLclampf *priorities) Set texture residence priority

void glDeleteTextures ( sizei n, uint *textures ) Del et e t ext ur e obj ect s when you ar e fi ni shed wi t h t h

cc

Page 25: Computer Graphics OpenGL - Texture mapping. OpenGL Texture mapping zTexture Mapping allows us to glue an image on polygons. In this method we can produce.

c cccccc ccccccc cccccc ccccccc

static GLuint texnames[2];

/* generate unused texture names */ glGenTextures(2, texnames);

/* bind, then define, each texture */ glBindTexture(GL_TEXTURE_2D, texnames[0]); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexImage2D(GL_TEXTURE_2D, 0, 4, 64, 64, 0, GL_RGBA, GL_UNSIGNED_BYTE, redtex); glBindTexture(GL_TEXTURE_2D, texnames[1]); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexImage2D(GL_TEXTURE_2D, 0, 4, 64, 64, 0, GL_RGBA, GL_UNSIGNED_BYTE, greentex); ...

/* in drawScene(), bind the texture you want before drawing each object */ glBindTexture(GL_TEXTURE_2D, texnames[1]); drawTexturesPolygon(); /* uses greentex */ glBindTexture(GL_TEXTURE_2D, texnames[0]); drawTexturesPolygon(); /* uses redtex */

Page 26: Computer Graphics OpenGL - Texture mapping. OpenGL Texture mapping zTexture Mapping allows us to glue an image on polygons. In this method we can produce.

Multiple Levels of Detail

Objects with textures can be viewed at different distances fr om the viewpoint

ccc ccc ccccccc c cccccc cc ccc-cccccccc ccccccc cccc cc cccccccccc cccccccccc cccccc mipmaps OpenGL automatically determines which texture map to use ba

sed on the object's size (in pixels)

Page 27: Computer Graphics OpenGL - Texture mapping. OpenGL Texture mapping zTexture Mapping allows us to glue an image on polygons. In this method we can produce.

Creating Mipmaps Automatically

You can use the OpenGL Utility routine gluBuild2DMipmaps() ccccc ccc cccc ccccccc cccccccccccccc.

GLint gluBuild2DMipmaps( GLenum target, GLint internalformat, GLsizei width, GLint height, GLenum format, GLenum type, void *data )

target specifies the target texture internalformat internal storage format of the texture

width, height specify the dimensions of the texture

format specifies the format of the pixel data type specifies the data type for data data specifies a pointer to the image data in memory

Page 28: Computer Graphics OpenGL - Texture mapping. OpenGL Texture mapping zTexture Mapping allows us to glue an image on polygons. In this method we can produce.

Mipmapped Minification Filters

void glTexParameterf( target,GL_TEXTURE_MIN_FILTER,param ) These minification filters use the closest mipmap:

GL_NEAREST_MIPMAP_NEAREST uses the texel closest to the computed texture coordinate.

GL_LINEAR_MIPMAP_NEAREST uses the weighted average of four closest texels.

These minification filters use the two closest mipmaps:

GL_NEAREST_MIPMAP_LINEAR (default) uses the weighted average of the texel closest to the computed texture coordinate in each mipmap.

GL_LINEAR_MIPMAP_LINEAR uses the weighted average of the four texels closest to the computed texture coordinate in each mipmap.

GL_LINEAR_MIPMAP_LINEAR will generally produce the smoothest results. It is the most computationally intensive, and therefore may be the slowest.

Page 29: Computer Graphics OpenGL - Texture mapping. OpenGL Texture mapping zTexture Mapping allows us to glue an image on polygons. In this method we can produce.

Mapping Textures Smoothly onto 3D Objects

You need to provide explicit texture coordinates to improve thec cccccc

3D modeling and animation software generally create these foc ccc

Aquadricsurfaceisone t hat can be cal cul at ed usi ng a quadr at i c equat i on. GL U provides the quadric object as a utility to model and render cyl

cccccc, , . GLUquadricObj* gluNewQuadric( void )

GLU, the OpenGL Utility library, can map a texture smoothly ont o its quadric objects

Page 30: Computer Graphics OpenGL - Texture mapping. OpenGL Texture mapping zTexture Mapping allows us to glue an image on polygons. In this method we can produce.

A Textured Quadric

void gluQuadricTexture( GLUquadric* quad, GLboolean texture )

quad specifies the quadrics object (created with gluNewQuadric())

texture is a flag indicating if texture coordinates should be generated (GL_TRUE, GL_FALSE)

Example:

GLUquadricObj *quadObj;

quadObj = gluNewQuadric(); gluQuadricDrawStyle( quadObj, GLU_FILL ); gluQuadricTexture( quadObj, GL_TRUE );

/* ... in the draw routine */ gluSphere( quadObj, 0.75, 32, 32 );