Computer Graphics 9 Lee ByungGook 1122020 Computer Graphics
Computer Graphics 9 Lee Byung-Gook 11/2/2020 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 1
Texture Mapping • Definition: A 2 -dimensional (2 D) texture map is an image that is applied to one side of a 3 -dimensional polygon. • Problems: The image and the 3 D polygon typically do not have the same shape (most polygons are not rectangles). The image and the 3 D polygon typically do not have the same size 11/2/2020 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 2
Texture Mapping Texture image 200 by 100 pixels 5. 3 600. 51 Example polygons Defined in world coordinates 11/2/2020 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 3
Texture Mapping • • The image and the 3 D polygon typically do not have the same shape Solution : specify the portion of the image that is to be used to "paint" the polygon. Since we need to specify a subset of the whole image, it is natural to use percentages of the image dimensions. The percentages are specified as values between 0 (0%) and 1. 0 (100%). 11/2/2020 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 4
Texture Mapping (0. 39, 0. 95) (0. 11, 0. 6) (0. 0, 0. 0) 1. 0 (0. 48, 0. 06) 1. 0 Texture coordinates 11/2/2020 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 5
Texture Mapping • The image and the 3 D polygon typically do not have the same size. • Solution : the number of pixels used to display a polygon is dependent on the current MODELVIEW and PROJECTION matrices. (If the camera is close to the polygon, it will appear large in the window and require many pixels to represent it; if the camera is far away from the polygon, it will take very few pixels to represent it. ) 11/2/2020 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 6
Texture Mapping • If there are more pixels on the screen that require colors than there are pixels in the texture image, then the texture image must be magnified (made larger). • If there are fewer pixels on the screen as compared to the texture image, then the texture image must be minified (made smaller). • There are two general solutions: After mapping the pixel into the texture image take the color of the closest texture pixel (Open. GL calls this GL_NEAREST, which produces fast, but poor graphics)take a linear average of the colors of the surrounding pixels. (Open. GL calls this GL_LINEAR, which produces better, but slower graphics) 11/2/2020 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 7
Texture map compositing • Compositing occurs when a pixel's color value is determined by using a combination of two (or more) color values. • When using texture maps we have 2 colors to deal with - the color of the face and the color from the texture map Ct - the color defined by the texture map Cf - the color of the polygon face Cc - the color defined by the global texture color C - the color to be placed in the color buffer The "global texture color" is defined using the parameter GL_TEXTURE_ENV_COLOR 11/2/2020 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 8
Compositing methods • Replace the current color in the color buffer with the texture map color. (GL_REPLACE) C = Ct • Combine the color of the face with the color of the texture map by multiplying their values together. (GL_MODULATE) C = Cf*Ct • Combine the color of the face with the color of the texture map by adding a percentage of both (the percentage is controlled by the alpha values of the texture map) (GL_DECAL) C = Cf*(1 -At) +Ct*At • Combine the color of the face and the color of the texture map using the texture values as percentages of a "global texture color" Cc. (GL_BLEND) C = Cf*(1 -Ct) +Cc*Ct 11/2/2020 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 9
Lab 17 • downloading 11/2/2020 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 10
make. Check. Image #define check. Image. Width 64 #define check. Image. Height 64 GLubyte check. Image[check. Image. Height][check. Image. Width][4]; void make. Check. Image(void) { int i, j, c; for (i = 0; i < check. Image. Height; i++) { for (j = 0; j < check. Image. Width; j++) { c = ((((i&0 x 8)==0)^((j&0 x 8))==0))*255; check. Image[i][j][0] = (GLubyte) c; check. Image[i][j][1] = (GLubyte) c; check. Image[i][j][2] = (GLubyte) c; check. Image[i][j][3] = (GLubyte) i*(255/check. Image. Height); } } } 11/2/2020 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 11
my. Init make. Check. Image(); gl. Tex. Parameteri(GL_TEXTURE_2 D, GL_TEXTURE_MAG_FILT ER, GL_NEAREST); gl. Tex. Parameteri(GL_TEXTURE_2 D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); gl. Tex. Image 2 D(GL_TEXTURE_2 D, 0, GL_RGBA, check. Image. Width, check. Image. Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, check. Image); gl. Tex. Envf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); gl. Enable(GL_TEXTURE_2 D); 11/2/2020 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 12
my. Display gl. Color 3 fv(color[9]); gl. Begin(GL_QUADS); gl. Tex. Coord 2 f(0. 0, 0. 0); gl. Vertex 3 f(0. , 1. ); gl. Tex. Coord 2 f(1. 0, 0. 0); gl. Vertex 3 f(1. , 0. , 1. ); gl. Tex. Coord 2 f(1. 0, 1. 0); gl. Vertex 3 f(1. , 1. ); gl. Tex. Coord 2 f(0. 0, 1. 0); gl. Vertex 3 f(0. , 1. ); gl. Tex. Coord 2 f(0. 0, 0. 0); gl. Vertex 3 f(1. , 1. ); gl. Tex. Coord 2 f(1. 0, 0. 0); gl. Vertex 3 f(1. , 0. , 1. ); gl. Tex. Coord 2 f(1. 0, 1. 0); gl. Vertex 3 f(1. , 0. ); gl. Tex. Coord 2 f(0. 0, 1. 0); gl. Vertex 3 f(1. , 0. ); gl. End(); 11/2/2020 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 13
gl. Tex. Parameter • void gl. Tex. Parameter{fi}( GLenum target, GLenum pname, GLint param ); • These functions set texture parameters. • target : The target texture, which must be either GL_TEXTURE_1 D or GL_TEXTURE_2 D. • pname : The symbolic name of a single-valued texture parameter. The following symbols are accepted in pname: GL_TEXTURE_MIN_FILTER, GL_TEXTURE_MAG_FILTER, GL_TEXTURE_WRAP_S, GL_TEXTURE_WRAP_T 11/2/2020 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 14
gl. Tex. Parameter • GL_TEXTURE_MIN_FILTER – GL_NEAREST or GL_LINEAR • GL_TEXTURE_MAG_FILTER – GL_NEAREST, GL_LINEAR, GL_NEAREST_MIPMAP_NEAREST, GL_NEAREST_MIPMAP_LINEAR, GL_LINEAR_MIPMAP_NEAREST or GL_LINEAR_MIPMAP_LINEAR • GL_TEXTURE_WRAP_S, GL_TEXTURE_WRAP_T – GL_CLAMP or GL_REPEAT 11/2/2020 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 15
gl. Tex. Env • void gl. Tex. Env{fi}( GLenum target, GLenum pname, GLfloat param ); • These functions set texture environment parameters • target : A texture environment. Must be GL_TEXTURE_ENV. • pname : The symbolic name of a single-valued texture environment parameter. Must be GL_TEXTURE_ENV_MODE. • param : A single symbolic constant, one of GL_MODULATE, GL_DECAL, or GL_BLEND. 11/2/2020 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 16
gl. Tex. Image 2 D • void gl. Tex. Image 2 D( GLenum target, GLint level, GLint components, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const GLvoid *pixels ); • The gl. Tex. Image 2 D function specifies a two-dimensional texture image. • target : The target texture. Must be GL_TEXTURE_2 D. • level : The level-of-detail number. Level 0 is the base image level. Level n is the nth mipmap reduction image. • components : The number of color components in the texture. Must be 1, 2, 3, or 4. • width : The width of the texture image. Must be 2^n + 2(border) for some integer n. 11/2/2020 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 17
gl. Tex. Image 2 D • height : The height of the texture image. Must be 2^m + 2(border) for some integer m. • border : The width of the border. Must be either 0 or 1. • format : The format of the pixel data. It can assume one of nine symbolic values: GL_COLOR_INDEX, GL_RGBA, GL_RED, GL_GREEN, GL_BLUE, GL_ALPHA, GL_BGR_EXT, GL_BGRA_EXT, GL_LUMINACE_ALPHA. • type : The data type of the pixel data. The following symbolic values are accepted: GL_UNSIGNED_BYTE, GL_BYTE, GL_BITMAP, GL_UNSIGNED_SHORT, GL_SHORT, GL_UNSIGNED_INT, GL_INT, and GL_FLOAT. • pixels : A pointer to the image data in memory. 11/2/2020 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 18
Texture map repetition • Texture maps require a lot of memory! • To save memory, some types of textures can be defined as small images and then repeated in both the horizontal and vertical directions v (t) 2. 0 1. 0 0. 0 -1. 0 11/2/2020 0. 0 1. 0 2. 0 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 19
Texture map repetition (-0. 4, 1. 87) v ( t) 2. 0 (1. 9, 1. 35) 1. 0 0. 0 -1. 0 11/2/2020 (0. 24, -0. 92) 0. 0 1. 0 2. 0 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 20
Lab 18 • downloading 11/2/2020 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 21
my. Init Read. BMP("hgeri. bmp", &my. Image); gl. Bind. Texture(GL_TEXTURE_2 D, 1); gl. Tex. Parameteri(GL_TEXTURE_2 D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); gl. Tex. Parameteri(GL_TEXTURE_2 D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); gl. Tex. Parameteri(GL_TEXTURE_2 D, GL_TEXTURE_WRAP_S, GL_REPEAT); gl. Tex. Parameteri(GL_TEXTURE_2 D, GL_TEXTURE_WRAP_T, GL_REPEAT); gl. Tex. Envf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); gl. Tex. Image 2 D(GL_TEXTURE_2 D, 0, GL_RGB, my. Image. Width, my. Image. Height, 0, GL_RGB, GL_UNSIGNED_BYTE, my. Image. Pixels); 11/2/2020 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 22
make. Check. Image(); gl. Bind. Texture(GL_TEXTURE_2 D, 2); gl. Tex. Parameteri(GL_TEXTURE_2 D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); gl. Tex. Parameteri(GL_TEXTURE_2 D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); gl. Tex. Parameteri(GL_TEXTURE_2 D, GL_TEXTURE_WRAP_S, GL_REPEAT); gl. Tex. Parameteri(GL_TEXTURE_2 D, GL_TEXTURE_WRAP_T, GL_REPEAT); gl. Tex. Envf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); gl. Tex. Image 2 D(GL_TEXTURE_2 D, 0, GL_RGBA, check. Image. Width, check. Image. Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, check. Image); 11/2/2020 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 23
my. Display gl. Bind. Texture(GL_TEXTURE_2 D, 1); gl. Begin(GL_QUADS); gl. Tex. Coord 2 f(0. 0, 0. 0); gl. Vertex 3 f(0. , 1. ); gl. Tex. Coord 2 f(3. 0, 0. 0); gl. Vertex 3 f(1. , 0. , 1. ); gl. Tex. Coord 2 f(3. 0, 3. 0); gl. Vertex 3 f(1. , 1. ); gl. Tex. Coord 2 f(0. 0, 3. 0); gl. Vertex 3 f(0. , 1. ); gl. End(); gl. Begin(GL_POLYGON); gl. Tex. Coord 2 f(3. 0, 0. 0); gl. Vertex 3 f(0. , 1. ); gl. Tex. Coord 2 f(3. 0, 3. 0); gl. Vertex 3 f(0. , 1. ); gl. Tex. Coord 2 f(0. 0, 0. 0); gl. Vertex 3 f(0. , 0. ); gl. End(); 11/2/2020 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 24
my. Display gl. Bind. Texture(GL_TEXTURE_2 D, 2); gl. Begin(GL_QUADS); gl. Tex. Coord 2 f(-1. 0, -1. 0); gl. Vertex 3 f(1. , 1. ); gl. Tex. Coord 2 f(1. 0, -1. 0); gl. Vertex 3 f(1. , 0. , 1. ); gl. Tex. Coord 2 f(1. 0, 1. 0); gl. Vertex 3 f(1. , 0. ); gl. Tex. Coord 2 f(-1. 0, 1. 0); gl. Vertex 3 f(1. , 0. ); gl. End(); 11/2/2020 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 25
gl. Bind. Texture • void gl. Bind. Texture( GLenum target, GLuint texture ); • The gl. Bind. Texture function enables the creation of a named texture that is bound to a texture target. • target : The target to which the texture is bound. Must have the value GL_TEXTURE_1 D or GL_TEXTURE_2 D. • texture : The name of a texture; the texture name cannot currently be in use. • The gl. Bind. Texture function enables you to create a named texture. Calling gl. Bind. Texture with target set to GL_TEXTURE_1 D or GL_TEXTURE_2 D, and texture set to the name of the new texture you have created binds the texture name to the appropriate texture target. When a texture is bound to a target, the previous binding for that target is no longer in effect. 11/2/2020 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 26
Wrapping textures around spheres • Wrapping a texture around a sphere, such that there is no distortion, is impossible! (Take the surface of a ball and try to "flatten it out" without stretching it. It cannot be done!) • There are several possibilities for mapping a texture to a sphere. These are documented on page 461 in the textbook. The following is a straightforward application of a texture to the parametric equation of a sphere. 11/2/2020 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 27
Wrapping textures around spheres M M p/2 2 M u number. Slices 1 M -p/2 0. 0 1 1 N 2 2 N N-1 3 N v N N 2 p number. Sides 11/2/2020 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 28
Lab 19 • downloading 11/2/2020 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 29
Homework • In two weeks • Textute map your house with image 11/2/2020 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 30
- Slides: 30