Graphics Texture Mapping 고려대학교컴퓨터그래픽스연구실
3D Rendering Pipeline 3D Primitives 3D Modeling Coordinates Model Transformation 3D World Coordinates Lighting 3D World Coordinates Viewing Transformation 3D Viewing Coordinates Projection Transformation 2D Projection Coordinates Clipping 2D Projection Coordinates Viewport Transformation 2D Device Coordinates Scan Conversion Image 2D Device Coordinates Surface details can be added...
Texture Surface Add visual detail to surfaces of 3D objects With Surface Texture Polygonal Model
Texture Mapping 2D projective transformation Texture coordinate system (s, t) to Device coordinate system (x, y)
2D Texture Texture map as a continuous (s, t) coordinate space [0,1] For mapping it into a discrete texture image space Texture Map Texture Image
Textures Color variation in interior of 3D polygon When scan converting a polygon Vary pixel colors according to values fetched from a texture t Texture-Surface Transformation y y s Viewing and Projection Transformation s Texture Space z Object Space x x s Image Space
Texels and Textures Texels texture elements Texture coordinates (s, t) s = f t = g ( x, y, z) ( x, y, z)
Texture Mapping Overview Steps: Define texture Specify mapping from texture to surface Lookup texture values during scan conversion (0, 1) t s Texture Coordinate System v (0, 0) u Modeling Coordinate System (1, 0) y x Device Coordinate System
Texture Mapping Process Scan conversion Interpolate texture coordinates Distortion due to the bilinear interpolation Subdivide the polygon into smaller ones α (u 1, v 1 ) γ β (u 2, v 2 ) (u 3, v 3 )
Texture Mapping : Artifact of Linear Interpolation Linear Interpolation of Texture Coordinate Corrected Interpolation
Texture Modulation Texture Shaded Color Texture Value
Mipmap Problem Some objects can be too far or too small to be rendered with full-scale texture without any texture artifacts Mipmap Artifacts occur due to sparse texture sampling A collection of textures which are pre-calculated to reduce the artifacts Each level have a half size of the previous level Far or small objects will use smaller mipmap texture While close or large objects will use higher resolution of mipmap
Mipmap Construction Example
Mipmap Effect Without Mipmap With Mipmap
Normal Mapping Overview So called bump mapping Perturbs surface normal vectors
Normal Mapping Normal information can be stored in a RGB image (x, y, z) to (R, G, B) respectively The textures with normal information Can be used to add details to the surface rendered result
Normal Mapping Example (1/3)
Normal Mapping Example (2/3)
Normal Mapping Example (3/3)
Environment Mapping Maps a surrounding environment texture Around a surface of an object To approximate effects like reflection or refraction View dependant Due to reflective mapping process Not need to assign texture coordinates to a model Since surface normals are used to map the environment texture
2 Kinds of Environment Mapping (1/2) Spherical environment mapping Efficient use of texture memory Poor distribution of texels per solid angle Cannot linearly interpolate between texture coordinates Particularly near the edge of the map Map is only valid for one viewer direction Cubic environment mapping A slower technique compared to spherical mapping Unless hardware accelerated Uses more memory than sphere mapping But gains in lower CPU usage Independent of viewing position
2 Kinds of Environment Mapping (2/2) Spherical Cubic
Environment Mapping
Constructing a Texture Map Steps: Read an image for the texture map Define parameters that determine how the texture should be applied Define texture coordinates for the vertices ex) making the image into a 2D texture map GLubyte myimage[64][64][3]; glenable(gl_texture_2d); glteximage2d(gl_texture_2d, 0, 3, 64, 64, 0, GL_RGB, GL_UNSIGNED_BYTE, myimage);
1D, 2D, & 3D Texture Maps void glteximage1d(glenum target, GLint level, GLint iformat, GLsizei width, GLint border, GLenum format, GLenum type, Glvoid *texels); void glteximage2d(glenum target, GLint level, GLint iformat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, Glvoid *texels); void glteximage3d(glenum target, GLint level, GLint iformat, GLsizei width, GLsizei height, Glsizei depth, GLint border, GLenum format, GLenum type, Glvoid *texels);
2D Texture Map 2D texture function void glteximage2d(glenum target, GLint level, GLint internalformat, Glsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const GLvoid* texels); Defines a texture image on 2D target GL_TEXTURE_2D Only level Level of detail. 0 if there are no Mipmaps internalformat format of texel data width, height Width and height of the texture image Border The width of the border format Format of pixel data type Data type of the pixel data texels A pointer to the image data in memory
Texture Map Internal Format constants Type constants The name of constants meaning The name of constants meaning GL_RGBA R, G, B, A GL_UNSIGNED_BYTE unsigned 8 bit integer GL_RGB R, G, B GL_BYTE signed 8 bit integer GL_ALPHA A GL_UNSIGNED_SHORT unsigned 16 bit integer GL_LUMINANCE Intensity GL_SHORT signed 16 bit integer GL_LUMINANCE_ALPHA Intensity, A GL_INT signed 32 bit integer GL_FLOAT single precision float Format constants The name of constants GL_COLOR_INDEX GL_RGB GL_RGBA GL_RED GL_GREEN GL_BLUE GL_ALPHA meaning Color index R, G, B value R, G, B, value Red value Green value Blue value Alpha value
Texture Map Generating the texture name void glgentextures(glsizei n, GLuint* texturenames); Generates the texture name to identify from others. n The number of the texture texturenames Array for texture name unsigned int texturenames[3]; glgentextures(3, texturename);
Texture Map The use of texture void glbindtexture(glsizei target, GLuint texturename); Binds the texture name Select texture you want to use among some textures Target GL_TEXTURE_1D, GL_TEXTURE_2D, GL_TEXTURE_3D texturename Texture name
Texture Map Texture function void gltexenvf(glenum target, GLenum pname, GLint param) target pname param A texture environment. Must be GL_TEXTURE_ENV The symbolic name of a single-valued texture environment parameter. Must be GL_TEXTURE_ENV_MODE Parameter value PARAM GL_BLEND GL_DECAL GL_MODULATE GL_REPLACE meaning Blended with environment color The object color totally depends on the texture color The computed surface color is multiplied by the texture color Just draw the surface using the texture colors only gltexenvi(gl_texture_env, GL_TEXTURE_ENV_MODE, GL_DECAL)
Texture Parameters Preimage of the pixel Texture mapping is not really applied to the surface But rather a pixel in screen Magnification each texel covers multiple pixels Minification each pixel covers multiple texels Magnification Minification
Texture Filtering Discussion Texture map is a square But the polygon-to-be-textured is not always a square There could be a distortion like magnification or minification Magnification : one and more pixel for one texel Minification : one pixel for some texels void gltexparameterf(glenum target, GLenum pname, GLfloat param) In short, one texel doesn t always belong to one pixel. Target Dimension (GL_TEXTURE_1D, GL_TEXTURE_2D, GL_TEXTURE_3D) pname The filter (e.g. GL_TEXTURE_MAG_FILTER or GL_TEXTURE_MIN_FILTER) Param Parameter for filtering
Mipmap Parameter for filtering param GL_NEAREST_MIPMAP_NEAREST GL_NEAREST_MIPMAP_LINEAR GL_LINEAR_MIPMAP_NEAREST GL_LINEAR_MIPMAP_LINEAR meaning Chooses the mipmap that most closely matches the size of the pixel being textured. (the texture element nearest to the center of the pixel) Chooses the two mipmaps that most closely match the size of the pixel being textured (the texture element nearest to the center of the pixel) Chooses the mipmap that most closely matches the size of the pixel being textured (a weighted average of the four texture elements that are closest to the center of the pixel) Chooses the two mipmaps that most closely match the size of the pixel being textured (a weighted average of the four texture elements that are closest to the center of the pixel)
Texture Filtering The parameter of texture filter Param Meaning GL_NEAREST GL_LINEAR Use the nearest texel from the center of pixel Interpolate the nearest 4 texels from the center of pixel
Repeating and Clamping Texture repeating and clamping If the texture coordinate is below 0 or above 1 OpenGL repeats texture or clamp Repeating is just like tiling Clamping limits the value within specified range ex) the value above 1.0 is re-set to 1.0 Uses gltexparameterf()
Repeating and Clamping Texture repeating and clamping pname constant value Pname meaning GL_TEXTURE_WRAP_S Assign the method for texture coordinate s that is out of 0.0~1.0 GL_TEXTURE_WRAP_T Assign the method for texture coordinate t that is out of 0.0~1.0 param constant value Param meaning GL_REPEAT Repeat if texture coordinate is out of 0.0~1.0 GL_CLAMP Replace the border value if texture coordinate is out of 0.0~1.0
Repeating and Clamping Repeating Clamping Wrap S : GL_REPEAT Wrap T : GL_REPEAT Wrap S : GL_CLAMP Wrap T : GL_CLAMP
Texture Coordinate Mapping between points on geometric objects and texels void gltexcoord{1234}{sifd}(type scoord,...); void gltexcoord{1234}{sifd}v(type *coord); ex) mapping the texture to a quadrilateral glbegin(gl_quads); gltexcoord2f(0.0, 0.0); glvertex3fv(vertex[0]); gltexcoord2f(0.0, 1.0); glvertex3fv(vertex[1]); gltexcoord2f(1.0, 1.0); glvertex3fv(vertex[2]); gltexcoord2f(1.0, 0.0); glvertex3fv(vertex[3]); glend();
Texture Coordinate Texture Coordinate void gltexcoord2f(float s, float t) Set 2D texture coordinate s t Image coordinate for horizontal direction Image coordinate for vertical direction Range : usually 0~1 If not, texture is repeating or clamping glbegin(gl_quads); gltexcoord2f(0.0f, 0.0f); glvertex3f(-1.0, 1.0, 0.0f); gltexcoord2f(1.0f, 0.0f); glvertex3f(1.0, 1.0, 0.0f); gltexcoord2f(1.0f, 1.0f); glvertex3f(1.0, -1.0, 0.0f); gltexcoord2f(0.0f, 1.0f); glvertex3f(-1.0, -1.0, 0.0f); glend();
Curved Surfaces with Specular Reflection
Improved Illumination Model with Multiple Lights
Original Texture Mapping