3. that appears in a video game needs to be textured; this includes everything from plants
to people. If things aren’t textured well, your game just won’t look right.
6. How to use a Texture?
• How to use a Texture?
1. Load the texture
2. Map it into a polygon
3. Draw the polygon
7. How to use a Texture?
• How to use a Texture?
1. Load the texture
2. Map it into a polygon
3. Draw the polygon
• How to use a Texture? (OpenGL)
1. Specify textures in texture objects
2. Set texture filter
3. Set texture function
4. Set texture wrap mode
5. Set optional perspective correction hint
6. Bind texture object
7. Enable texturing
8. Supply texture coordinates for vertex
• How OpenGL store images?
– One image per texture object
– May be shared by several graphics contexts
21. Texturing Functions
• Generate texture names
glGenTextures(n, *texIds);
• Create texture objects with texture data and state
glBindTexture(target, id);
• Bind textures before using
glBindTexture(target, id);
• Define a texture image from an array of texels in CPU memory
glTexImage2D(target, level, components, w, h, border, format, type,
*texels);
• Texel colors are processed by pixel pipeline
– pixel scales, biases and lookups can be done
22. Filter Modes
• Filter modes control how pixels are minified or magnified. Generally a color is
computed using the nearest texel or by a linear average of several texels.
• The filter type, above is one of GL_TEXTURE_MIN_FILTER or GL_TEXTURE_MAG_FILTER.
• The mode is one of GL_NEAREST, GL_LINEAR, or special modes for mipmapping.
Mipmapping modes are used for minification only, and have values of:
– GL_NEAREST_MIPMAP_NEAREST
– GL_NEAREST_MIPMAP_LINEAR
– GL_LINEAR_MIPMAP_NEAREST
– GL_LINEAR_MIPMAP_LINEAR
24. Wrapping Mode
• Wrap mode determines what should happen if a texture coordinate lies outside of the [0,1] range.
• If the GL_REPEAT wrap mode is used, for texture coordinate values less than zero or greater than
one, the integer is ignored and only the fractional value is used.
• If the GL_CLAMP wrap mode is used, the texture value at the extreme (either 0 or 1) is used.
• Example:
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
texture
GL_REPEAT
wrapping
GL_CLAMP
wrapping
s
t
28. Something to pay attention for
• The image must be 24 bit (RGB)
• The image extension should be .bmp
• The image dimensions must be a power of 2 (2, 4, 8, 16, 32, 64, ..etc.)
– If dimensions of image are not power of 2 then scale:
• gluScaleImage(format, w_in, h_in, type_in, *data_in, w_out, h_out,
type_out, *data_out);
*_in is for source image
*_out is for destination image
• White color should be assigned before drawing a textured object by setting:
glColor3f(1,1,1);
• You should call: glBindTexture(GL_TEXTURE_2D, textureID) between glBegin() and
glEnd()
35. Blending
• You can blend objects using:
– glBlendFunc(GLenum sfactor, GLenum dfactor);
• sfactor: is the source blend factor
• dfactor: is the destination blend factor
• Transparency is implemented using:
– “GL_SRC_ALPHA” for the source
– “GL_ONE_MINUS_SRC_ALPHA” for the destination
36. Blending
• You can blend objects using:
– glBlendFunc(GLenum sfactor, GLenum dfactor);
• sfactor: is the source blend factor
• dfactor: is the destination blend factor
• Transparency is implemented using:
– “GL_SRC_ALPHA” for the source
– “GL_ONE_MINUS_SRC_ALPHA” for the destination
54. Something to pay attention for, again
• The image must be 24 bit (RGB)
• The image extension should be .bmp
• The image dimensions must be a power of 2 (2, 4, 8, 16, 32, 64, ..etc.) or scale.
• White color should be assigned before drawing a textured object by setting:
glColor3f(1,1,1);
• You should call: glBindTexture(GL_TEXTURE_2D, textureID) between
glBegin() and glEnd()
• Additional stuff when you use transparency with .tga
– The image must be 32 bit (RGB)
– The image extension should be .tga
– In InitGL(), You must load .tga files LAST
– In DrawGLScene(), you must draw the transparent objects LAST
56. Texture Tilling
Tiling is a very simple effect that creates a repeating pattern of
an image on the surface of a primitive object
57. Texture Tilling
Using a small image to cover a large surface makes tiling a useful way to increase the
performance of your textures and decrease the size of your image files.
58. Texture Tilling, How to
Simply set the texture coordinates larger than 1
as many time as you want the texture to be tilled
63. Mipmapped Textures
• Mipmap allows for prefiltered texture maps of decreasing resolutions
• Lessens interpolation errors for smaller textured objects
• Declare mipmap level during texture definition
glTexImage*D(GL_TEXTURE_*D, level, …)
• GLU mipmap builder routines
gluBuild*DMipmaps()
64. 2D Texture and 3D Texture?
http://gamedev.stackexchange.com/questions/9668/what-are-3d-textures
65. 2D Texture and 3D Texture
• All we have used so far are 2D textures mapped on a 3D coordinates.
• So what’s a 3D texture?!
– 3D texture (or sometimes called “Volume textures”) works like regular 2D texture but it is truly
3D. 2D textures has UV coordinates, 3D has UVW coordinates
67. 2D Texture and 3D Texture
• 3D textures are used in:
– Textures mapped into a model vertices
– volumetric effects in games (fire, smoke, light rays, realistic fog)
– caching light for realtime global illumination (CryEngine for example)
– scientific (MRI, CT scans are saved into volumes)
68. 2D Texture and 3D Texture
• Watch Nvidia smoke box in a XFX 9600gt
– https://www.youtube.com/watch?v=9AS4xV-CK14
69. 2D Texture and 3D Texture
• Watch Global illumination in CryEngine 3 Tech Trailer (HD)
– https://www.youtube.com/watch?v=Pq39Xb7OdH8