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
Texture Mapping Texture Mapping � Texture map is an image, two Texture map is an image, two- -dimensional array of dimensional array of � color values (texels texels) ) color values ( � 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’ ’s surface property (color) s surface property (color) polygon � We must map (u,v) space to polygon We must map (u,v) space to polygon’ ’s (s, t) space s (s, t) space � T V U S
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 texture coordinates texture coordinates with each with each vertices by storing 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
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
OpenGL textures OpenGL textures OpenGL textures have dimensions (x and y [and z]) are powers of 2 256 256 512 240 512 256 320 64 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, or you can paste it into a sub-region of a valid OpenGL texture 256 256 512 512
Texture coordinate values Texture coordinate values All OpenGL textures span the range [0,1] in both x and y 1.0 1.0 1.0 256 256 512 0.0 0.0 0.0 1.0 0.0 1.0 512 256 0.0 64 0.0 1.0 0.625 1.0 0.9375 ( ) 256 240 0.0 0.0 1.0 512 320
Unnecessary Limitation Unnecessary Limitation � Extensions Extensions � � GLEW GLEW �
Example Texture Map Example Texture Map glTexCoord2d(1,1); glVertex3d (s, s, s); glTexCoord2d(0, 0); glVertex3d (-s, -s, -s);
Example Texture Map Example Texture Map glTexCoord2d(5, 5); glVertex3d (s, s, s); Repeating textures vs. Clamped textures glTexCoord2d(1, 1); glVertex3d (s, s, s);
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 - -> 1) > 1) each vertex (usually 0 � 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 1 � 4 coords? s, t, r, and q (for homogeneous coordinates, an advanced topic)
Texture objects Texture objects Since version 1.2, OpenGL has supported the idea of texture objects This allows multiple, independent textures to be used 1 2 3 Texture objects are created, deleted and each is given a name (an unsigned integer identifier) One texture at a time is active As usual we start with glEnable(GL_TEXTURE_2D); 1-D & 3-D textures are possible
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) of the Texture Objects it creates of the Texture Objects it creates � texture[ ] is of type texture[ ] is of type GLuint GLuint �
Specify which texture Specify which texture object is about to be defined object 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 �
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);
Wrapping Mode Wrapping Mode Clamping: if s,t > 1 s,t > 1 use 1, if use 1, if s,t <0 s,t <0 use 0 use 0 Clamping: if Wrapping: use s,t s,t modulo 1 modulo 1 Wrapping: use glTexParameteri( GL_TEXTURE_2D, ( GL_TEXTURE_2D, glTexParameteri GL_TEXTURE_WRAP_S, GL_CLAMP ) GL_TEXTURE_WRAP_S, GL_CLAMP ) glTexParameteri( GL_TEXTURE_2D, ( GL_TEXTURE_2D, glTexParameteri GL_TEXTURE_WRAP_T, GL_REPEAT ) GL_TEXTURE_WRAP_T, GL_REPEAT ) t s GL_REPEAT GL_CLAMP texture wrapping wrapping
Assign image data Assign image data � glTexImage2D(); glTexImage2D(); � (2D Texture) (2D Texture) GL_TEXTURE_2D GL_TEXTURE_2D (level of detail 0) (level of detail 0) 0 0 (3 components, RGB) (3 components, RGB) 3 3 (size) (size) image1- -> >sizeX sizeX image1 (size) (size) image1- -> >sizeY sizeY image1 (no border pixel) (no border pixel) 0 0 (RGB color order) (RGB color order) GL_RGB GL_RGB (unsigned byte data) (unsigned byte data) GL_UNSIGNED_BYTE GL_UNSIGNED_BYTE (pointer to the data)) (pointer to the data)) image1- ->data >data image1
glTexImage2D – – Arg Arg 1 1 glTexImage2D � 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 �
glTexImage2D – – Arg Arg 2 2 glTexImage2D � 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 �
glTexImage2D – – Arg Arg 3 3 glTexImage2D � 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 texels G, B, and A are used in internal representation of 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
Recommend
More recommend