Download presentation
Presentation is loading. Please wait.
Published byMilton Matthews Modified over 8 years ago
1
Texture Mapping CEng 477 Introduction to Computer Graphics
2
What is texture mapping? ● Application of textures to surfaces of 3d objects to produce photo-realistic images. No texture mappingWith texture mapping
3
Examples of texture mapped scenes
5
What is a texture? ● A texture image is a rectangular array of pixel data ● Usually a 2D array, but can be 1D or 3D ● A "texture pixel" is called a "texel" ● Can contain color, luminance and/or alpha information
6
What information does a texture contain? ● A one component texture contains Luminance, Intensity, or Alpha data. ● Examples: Wood, grass, sand
7
What information does a texture contain? ● A two component texture contains Luminance and Alpha (transparency) data. ● Examples: Trees, clouds
8
What information does a texture contain? ● A three component texture contains Red, Green, and Blue values. ● Examples: Fabrics, bricks
9
What information does a texture contain? ● A four component texture contains Red, Green, Blue, and Alpha values. ● Examples: Objects
10
Loading texture data ● OpenGL has no built-in mechanism for loading textures. – OpenGL does not provide functions to read an image file into a texture array in memory. You have to fill the components of the texture array by yourself. ● However, there are texture loaders (free source code) that you can use to read PNG, RGB, BMP, etc. image files into a texture array in an OpenGL program. – BMP loader ● http://users.ox.ac.uk/~orie1330/bmploader.html http://users.ox.ac.uk/~orie1330/bmploader.html – glpng library: ● http://www.fifi.org/doc/libglpng-dev/glpng.html http://www.fifi.org/doc/libglpng-dev/glpng.html
11
Texture Mapping Process ● Applying texture images to polygons. ● The basic texture mapping steps are: ● Specify the texture. – glTexImage – gluScaleImage ● 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
12
Specifying a Texture ● void glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texWidth, texHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels) – pixels - const GLvoid *, pointer to the image data in memory – Width and height must be a power of 2.
13
What if the texture is the wrong size? ● glTexImage2D() requires a texture whose pixel dimensions are powers of two ● If necessary, use the OpenGL utility routine gluScaleImage() to scale the image ● Pass the scaled image to glTexImage2D()
14
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 ● GL_RGB, GL_RGBA, etc. – typein/typeout specifies the type ● GL_UNSIGNED_INT, GL_INT, GL_FLOAT, etc.
15
Texture coordinates ● Texture coordinates are part of the data that is associated with each vertex. ● A 2D texture is treated as a 1x1 square whose texture coordinates go from 0.0 to 1.0 in each dimension. ● Texture coordinates are usually specified with (s, t) ● Texture coordinates are assigned to each vertex of a polygon. ● Texture coordinates are interpolated as a polygon is filled. ● Texture filters control how the interpolation is performed
16
Assigning texture coordinates to 3D object vertices ● void glTexCoord2f (s, t) glBegin( GL_QUADS ); glTexCoord2fv( t0 ); glVertex3fv( v0 ); glTexCoord2fv( t1 ); glVertex3fv( v1 ); glTexCoord2fv( t2 ); glVertex3fv( v2 ); glTexCoord2fv( t3 ); glVertex3fv( v3 ); glEnd();
17
Which Texel Goes With Which Pixel? ● One texel rarely corresponds to one pixel on the final screen image Texture information must be magnified Texture information must be minified OpenGL provides several filters to magnify or minify a texture to make a trade-off between speed and image quality
18
Magnification/Minification Filters ● void glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, param ) – param specifies the option ● 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.
19
Multiple Textures ● Most of the time you will want to apply different textures to different objects on the scene. ● OpenGL provide you texture objects to manipulate different textures you have. ● A set of textures and their related states can be treated as a single texture object. ● You bind a name with a texture object when you create it, then define the image data and parameters of the texture
20
Multiple Textures ● As you render your scene, bind the name of each desired texture 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 one texture to another. – So, if you are using more than one texture, always set up the textures as texture objects!
21
Using Texture Objects ● void glGenTextures(GLsize n, GLuint *texnames) – Generate texture names ● void glBindTexture(GL_TEXTURE_2D, GLuint texname) – Create a new texture object and assign it a name (first time) – Call with texname = 0 to stop using texture objects (unbind) ● void glDeleteTextures ( sizei n, uint *textures )
22
Example static GLuint texnames[2]; glGenTextures(2, texnames); /* generate unused texture names */ /* 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);... glBindTexture(GL_TEXTURE_2D, texnames[1]); drawTexturesPolygon(); /* uses greentex */ glBindTexture(GL_TEXTURE_2D, texnames[0]); drawTexturesPolygon(); /* uses redtex */
23
Enabling Texture Mapping ● glEnable (GL_TEXTURE_2D)
24
Advanced Topics ● Applying textures to curved surfaces smoothly ● Multiple levels of detail: mipmaps ● Changing the texture environment: Specifying how the texture colors interact with the original polygon colors.
25
Changing Texture Environment ● glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); ● Tells OpenGL to apply lighting and coloring effects to the texture.
26
References ● http://profs.sci.univr.it/~colombar/html_ope nGL_tutorial/en/10texturemapping_000.html http://profs.sci.univr.it/~colombar/html_ope nGL_tutorial/en/10texturemapping_000.html ● http://www.gamedev.net/reference/articles/ article947.asp http://www.gamedev.net/reference/articles/ article947.asp
27
Texture Mapping Example The geometric model
28
Texture Mapping Example The texture
29
Texture Mapping Example Texture mapped object
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.