Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Computer Graphics OpenGL - Texture mapping. OpenGL Texture mapping zTexture Mapping allows us to glue an image on polygons. In this method we can produce."— Presentation transcript:

1 Computer Graphics OpenGL - Texture mapping

2 OpenGL Texture mapping zTexture 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. zThink 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…)

3 What is a Texture? zA texture image is a rectangular array of pixel data yUsually a 2D array, but can be 1D or 3D zA "texture pixel" is called a "texel” yCan contain color, luminance and/or alpha information zRaster (framebuffer) images -such as.bmp,.tif,.rgb and etc.-, can be used as textures. zIn OpenGL 1.0-1.1, three-dimensional textures are available if the EXT_texture3D extension is supported. Starting with OpenGL 1.2, 3D textures are a core feature.

4 What is Texture Mapping? zApplying textures (images) to polygons. zThe basic texture mapping steps are: ySpecify the texture. xglTexImage xgluScaleImage yIndicate how the texture is to be applied to each pixel. xglTexEnv xglTexParameter yEnable texture mapping. xglEnable  Draw the scene, providing geometric and texture coordinates. glTexCoord

5 Specifying a 2D Texture ztarget - target texture zlevel - resolution level zinternalformat - internal storage format of the texture zwidth - width of the texture image zheight - height of the texture image zborder - the width of the border (either 0 or 1) zformat - format of the pixel data ztype - data type of the pixel data zpixels - 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)

6 What if the Texture is the Wrong Size? zglTexImage2D() requires a texture whose pixel dimensions are powers of two zIf necessary, use the OpenGL utility routine gluScaleImage() to scale the image zPass the scaled image to glTexImage2D() 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.

7 Scaling Texture Images GLint gluScaleImage( GLenum format, GLsizei widthin, GLsizei heightin, GLenum typein, const void *datain, GLsizei widthout, GLsizei heightout, GLenum typeout, void *dataout ) yformat specifies the format of the pixel data ywidthin, heightin specify the dimensions of the source image to be scaled ydatain specifies a pointer to the source image ywidthout, heightout specify the dimensions for the scaled image ydataout specifies a pointer where the scaled image is to be stored ytypein and typeout specify the data type for datain and dataout, respectively

8 Scaling Texture Images (cont.) 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).

9 Texture Coordinates Texture coordinates are part of the data that is associated with each vertex. yA 2D texture is treated as a 1x1 square whose texture coordinates go from 0.0 to 1.0 in each dimension. xTexture coordinates are homogeneous (s, t, r, q) xr is unused and q is 1.0 yTexture coordinates are assigned to each vertex of a polygon. xAssigned explicitly or generated automatically yTexture coordinates are interpolated as a polygon is filled xFilters control how the interpolation is performed

10 Setting Texture Coordinates Explicitly void glTexCoord2fv( const GLfloat *v ) yv Specifies a pointer to an array of two, which in turn specify the s, t, r, and q texture coordinates zExample: glBegin( GL_QUADS ); glTexCoord2fv( t0 ); glVertex3fv( v0 ); glTexCoord2fv( t1 ); glVertex3fv( v1 ); glTexCoord2fv( t2 ); glVertex3fv( v2 ); glTexCoord2fv( t3 ); glVertex3fv( v3 ); glEnd(); zThere are many variations of glTexCoord for specifying texture coordinates 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 texture coordinates as (s, t, r, 1), and glTexCoord4 defines all four components explicitly as (s, t, r, q). zTexture coordinates can be specified in scalar or vector form as short, int, float, or double.

11 Which Texel Goes With Which Pixel? zOne texel rarely corresponds to one pixel on the final screen image. zOpenGL provides several filters to magnify or minify the texture. This allows you to make a trade-off between speed and image quality. yThe texture magnification filter is used when the pixel being textured maps to an area less than or equal to one texture element. yThe texture minification filter is used whenever the pixel being textured maps to an area greater than one texture element. In this case, the texture information must be minified.

12 Specifying Filters void glTexParameterf( GLenum target, GLenum pname, const GLfloat param ) ytarget specifies the type of the target texture yGL_TEXTURE_1D or GL_TEXTURE_2D ypname specifies which filter to set yGL_TEXTURE_MAG_FILTER or GL_TEXTURE_MIN_FILTER yparam specifies whether speed or image quality is more important yGL_NEAREST to choose the texel nearest to the texture coordinate computed for the pixel. yGL_LINEAR to use the weighted average of the four texels nearest to the texture coordinate computed for the pixel.

13 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()

14 Specifying Texture Coordinates It’s about where to map the texture and how to orient the texture before mapping it  glTexCoord command specifies the mapping between coordinates and polygon vertexes.  The coordinate passed to glTexCoord is associated with the vertex specifies in the next glVertex call.  Example: 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();

15 Specifying Texture Coordinates (cont.) The texture coordinate (0,0) is mapped to the object vertex (-s,-s,-s) http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnopen/html/msdn_gl7.asp

16 Specifying Texture Coordinates (cont.) Flipping a texture http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnopen/html/msdn_gl7.asp

17 Repeating Textures To repeat a texture  Use texture coordinates greater then 1.0 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 xglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); xglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

18 Repeating Textures (cont.) Specifying texture coordinate > 1.0 http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnopen/html/msdn_gl7.asp

19 Repeating Textures (cont.) Specifying texture coordinate < 1.0 or = 1.0 http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnopen/html/msdn_gl7.asp

20 What if I want to texture-map to not-a-plane shapes If you are using OpenGL Quadrics, then you can project a texture on a quadric surface letting OpenGL compute the coordinates. yFirst 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. yWhen creating your Quadric make this call to have OpenGL create the texture coordinates: gluQuadricTexture(ptrQuad, GL_TRUE); yThen make the Texture calls as you normally would.

21 Setting Texture Environment A texture environment specifies how texture values are interpreted when a fragment is textured. 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);

22 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);

23 Multiple Textures zA set of textures and their related state can be treated as a single object. yTexture objects let you create as many textures as you need. yYou bind a name with a texture object when you create it, then define the image data and parameters of the texture. yAs you render your scene, bind the name of each desired texture object to the appropriate texture target. xThe texture names become aliases for the textures currently bound to them. ySince binding (reusing) a texture takes less time than defining (and reloading) one, this is a more efficient way to switch from one texture to another. ySo, if you are using more than one texture, always set up the textures as texture objects!

24 Using Texture Objects zvoid glGenTextures(GLsize n, GLuint *texnames) yGenerate texture names zvoid glBindTexture(GLenum target, GLuint texname) yCreate a new texture object and assign it a name (first time) yBind a named texture to a texturing target (either TEXTURE_1D or TEXTURE_2D)  Call with texname = 0 to stop using texture objects (unbind) zvoid glPrioritizeTextures(GLsize n, const GLuint *texnames, const GLclampf *priorities) ySet texture residence priority zvoid glDeleteTextures ( sizei n, uint *textures ) yDelete texture objects when you are finished with them

25 A Simple Texture Object Example 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 */

26 Multiple Levels of Detail zObjects with textures can be viewed at different distances from the viewpoint zYou can specify a series of pre-filtered texture maps of decreasing resolution called mipmaps yOpenGL automatically determines which texture map to use based on the object's size (in pixels)

27 Creating Mipmaps Automatically  You can use the OpenGL Utility routine gluBuild2DMipmaps() to build and load mipmaps automatically. zGLint gluBuild2DMipmaps( GLenum target, GLint internalformat, GLsizei width, GLint height, GLenum format, GLenum type, void *data ) ytarget specifies the target texture yinternalformat internal storage format of the texture ywidth, height specify the dimensions of the texture yformat specifies the format of the pixel data ytype specifies the data type for data ydata specifies a pointer to the image data in memory

28 Mipmapped Minification Filters zvoid glTexParameterf( target,GL_TEXTURE_MIN_FILTER,param ) yThese minification filters use the closest mipmap: xGL_NEAREST_MIPMAP_NEAREST uses the texel closest to the computed texture coordinate. xGL_LINEAR_MIPMAP_NEAREST uses the weighted average of four closest texels. yThese minification filters use the two closest mipmaps: xGL_NEAREST_MIPMAP_LINEAR (default) uses the weighted average of the texel closest to the computed texture coordinate in each mipmap. xGL_LINEAR_MIPMAP_LINEAR uses the weighted average of the four texels closest to the computed texture coordinate in each mipmap. yGL_LINEAR_MIPMAP_LINEAR will generally produce the smoothest results. It is the most computationally intensive, and therefore may be the slowest.

29 Mapping Textures Smoothly onto 3D Objects zYou need to provide explicit texture coordinates to improve the mapping z3D modeling and animation software generally create these for you zA quadric surface is one that can be calculated using a quadratic equation. GLU provides the quadric object as a utility to model and render cylinders, spheres, and disks. yGLUquadricObj* gluNewQuadric( void ) zGLU, the OpenGL Utility library, can map a texture smoothly onto its quadric objects

30 A Textured Quadric zvoid gluQuadricTexture( GLUquadric* quad, GLboolean texture ) yquad specifies the quadrics object (created with gluNewQuadric()) ytexture is a flag indicating if texture coordinates should be generated (GL_TRUE, GL_FALSE) zExample: GLUquadricObj *quadObj; quadObj = gluNewQuadric(); gluQuadricDrawStyle( quadObj, GLU_FILL ); gluQuadricTexture( quadObj, GL_TRUE ); /*... in the draw routine */ gluSphere( quadObj, 0.75, 32, 32 );


Download ppt "Computer Graphics OpenGL - Texture mapping. OpenGL Texture mapping zTexture Mapping allows us to glue an image on polygons. In this method we can produce."

Similar presentations


Ads by Google