Download Texture Mapping Concepts - Introduction: Computer Graphics | CS 470 and more Study notes Computer Graphics in PDF only on Docsity!
Computer Graphics
CS 470
Computer Science and Electrical Engineering Dept. West Virginia University
October 15, 2008
Outline
1 Texture Mapping Concepts
2 Texture Mapping in OpenGL
Texture Mapping Concepts
Texture - Definition
Texture
An image whose pattern is suggestive of the structure or composition of a
material.
Examples of textures.
Texture Mapping Concepts
Texture Mapping
Texture Mapping
Coloring the fragments of a primitive using pixels from a texture image.
The pixels in a texture image are often referred to as ’texels’.
Until now, the primitives we rendered have been either
Flat shaded : solid color, or
Smooth shaded : colors interpolated from vertices.
To increase the level of detail, we would need to increase the geometric
complexity (number of triangles).
Texture Mapping Concepts
How To Perform Texture Mapping
2D → 3D mapping
Define a texture mapping function
from 2D image coordinates to 3D
vertex coordinates
Associate a ”texture coordinate”
with each vertex.
I Texture coordinate is a vertex
attribute, like color or normal
vector.
I Specified like other attributes.
Linearly interpolate texture
coordinates per fragment.
Shade fragments using the color
of the image at the interpolated
coordinates.
Texture Mapping Concepts
Issues in Texture Mapping
Texture sampling.
I Note : Interpolated texture coordinates may not be integers.
How to incorporate lighting.
Rendering artifacts.
I Aliasing.
Speed / Efficiency.
I Texture images should stay on the server-side (GPU).
Handling the boundary of the texture image.
I Handling texture coordinates out of bounds.
I Tiling or repeating the texture image over a primitive.
Texture Mapping in OpenGL
Overview
1 Create a texture object and specify the texture image.
I Load data from file or generate procedurally
I glBindTexture*()
I glTexImage*()
2 Indicate how the texture mapping is to be performed.
I glTexParameter*()
I glTexEnv*()
3 Enable texture mapping.
I glEnable(GL_TEXTURE_2D);
4 Draw the scene.
I Specify texture coordinates : glTexCoord*()
I Specify vertices : glVertex*()
See also Chapter 9 of the OpenGL Programming Guide (red book online).
Texture Mapping in OpenGL Texture Objects
Texture Objects
Encapsulates the texture image and some parameters.
Textures are stored on the graphics server when possible.
Client refers to texture objects by an unsigned int identifier.
(Similar to the way display lists used IDs)
The relevant OpenGL functions are:
glGenTextures
I Generates a range of unused texture IDs (similar to glGenLists.)
glBindTexture
I The first call with a particular ID creates the texture object.
I The named texture object becomes ’bound’.
glTexImage*
I Upload the image data in the specified format.
Texture Mapping in OpenGL Texture Objects
Texture Creation Details
void glTexImage2D(GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const GLvoid *pixels)
Uploads the texture image to the currently bound object.
target : GL_TEXTURE_1D, GL_TEXTURE_2D, GL_TEXTURE_3D
level : level-of-detail (mipmap level) - 0 is the base image level.
internalformat : texel format within texture object (server-side).
width, height : dimensions of the texture.
border : border width - either 0 or 1.
format : format of the image data (client-side).
type : type of the image data (client-side).
pixels : void pointer to the image data (client-side).
I Load from file.
I Generate in program.
Texture Mapping in OpenGL Texture Objects
Texture Level-Of-Detail : Mipmaps
void glTexImage2D(GLenum target, GLint level , GLint internalformat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const GLvoid *pixels)
Mipmaps are different resolution versions of the same texture image.
Mipmap level 0 is the largest, most detailed image
Increasing level by 1 usually decreases width and height by half.
Texture Mapping in OpenGL Texture Objects
Internal format
You will always specify, at least, the color components
GL_LUMINANCE : 1 color component per texel - grey scale
GL_LUMINANCE_ALPHA : 2 color components per texel
- grey scale with transparency
GL_RGB : 3 color components per texel
GL_RGBA : 4 color components per texel
You can also specify sizes
GL_LUMINANCE8 : 8 bit grey scale
GL_LUMINANCE8_ALPHA8 : 16 bits per texel
GL_RGB5_A1 : 16 bits per texel
GL_RGB8 : 24 bits per texel
GL_RGBA : 32 bits per texel
Texture Mapping in OpenGL Texture Objects
Format
void glTexImage2D(GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLint border, GLenum format , GLenum type, const GLvoid *pixels)
The format of the data in client-side system memory.
GL_LUMINANCE
GL_LUMINANCE_ALPHA
GL_RGB
GL_RGBA
Only color components are specified.
Texture Mapping in OpenGL Texture Objects
Example texture object creation
#define checkImageWidth 64 #define checkImageHeight 64 GLubyte checkImage[checkImageHeight][checkImageWidth][4]; GLuint texName;
void init_texture(void) { makeCheckImage(); //fill the checkImage array glGenTextures(1, &texName); glBindTexture(GL_TEXTURE_2D, texName); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, checkImageWidth, checkImageHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, checkImage);
}
Texture Mapping in OpenGL Texture Object Parameters
Texture Object Parameters
Texture magnification.
I Function used when the pixel being textured maps to an area less than or
equal to one texel.
Texture minification.
I Function used when pixel being textured maps to an area greater than one
texel.
Texture wrapping mode.
I How to handle texture coordinates out side the range [0, 1].
I Can handle the s and t coordinates differently