OpenGL Texturing Texture Mapping 321190 2007년봄학기 6/2/2007 박경신 OpenGL 에서텍스쳐맵핑 (texture mapping) 을위한 3 단계 텍스쳐활성화 glenable(gl_texture_2d) 텍스쳐맵핑방법 ( 랩핑, 필터등 ) 정의 gltexparameteri(gl_texture_2d, GL_TEXTURE_WRAP_S, GL_REPEAT) gltexparameteri(gl_texture_2d, GL_TEXTURE_WRAP_T, GL_REPEAT) gltexparameteri(gl_texture_2d, GL_TEXTURE_MAG_FILTER, GL_LINEAR) gltexparameteri(gl_texture_2d, GL_TEXTURE_MIN_FILTER, GL_LINEAR) glteximage2d(gl_texture_2d, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, imagedata) 텍스쳐좌표 (texture coordinates) 지정 gltexcoord2f(0, 0); glvertex3f(-1.0, -1.0, 0.0); gltexcoord2f(1, 0); glvertex3f(1.0, -1.0, 0.0); OpenGL Texture Names 텍스쳐의이름 (name) 지정하기 (textureid) GLuint textureid; glgentextures(1, &textureid); glbindtexture(gl_texture_2d, textureid); glteximage2d( ); glbindtexture(gl_texture_2d, 0); glbindtexture(gl_texture_2d, textureid); glteximage2d glteximage2d(glenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const GLvoid *pixels); target: GL_TEXTURE_2D level: 텍스쳐맵의다양한해상도를지원하기위해설정. 1 개의해상도를지정하려면 1 로설정. 밉맵 (mipmapping) 에사용 각텍스쳐를위한다수의크기를가지고있는이미지를사용하지않는다면 0 으로지정 internalformat: 일반적으로 format 과같음. RGB 라면 3, RGBA 라면 4 로설정 width, height: 텍스쳐이미지의너비와높이는 2 의자승으로되어야함 ( 즉, 2, 4, 8, 16, 32, 64, 128, 256, 512, etc) border: 텍스쳐의경계선너비를지정. 보통 0 이고이미지데이터가 border 를가지고있으면 1 로지정. type: 텍스쳐이미지데이터의형식을설정.
glteximage1d glteximage1d(glenum target, GLint level, GLint internalformat, GLsizei width, GLint border, GLenum format, GLenum type, const GLvoid *pixels); glteximage2d() 함수는 2차원텍스처이미지를정의하고 glteximage1d() 함수는 1차원텍스처이미지를정의한다. glteximage1d() 함수와 glteximage2d() 함수의사용방법과인자의의미는거의동일하며 glteximage2d() 함수에만이미지텍스처의 height( 높이 ) 인자가추가된다. gltexsubimage2d 텍스쳐크기가 2 의승수 (e.g., 64x64, 128x256,..) 가아닌경우텍스쳐이미지의일부분만읽어들일때사용함 gltexsubimage2d(glenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, const GLvoid *pixels); OpenGL Texture Coordinates 텍스쳐맵핑이사용되려면객체에텍스쳐좌표를정의해야한다. GLUT teapot 은텍스쳐좌표를포함하고있다. GLU quadrics 도텍스쳐좌표를옵션으로정의할수있다. gluquadrictexture(quadric, GL_TRUE) 를사용하여텍스쳐맵핑활성화 OpenGL Texture Coordinates OpenGL에서텍스쳐좌표는텍스쳐이미지의각방향 (S, T) 의 0부터 1의영역으로형성됨 OpenGL에서각정점별로텍스쳐좌표를지정해야함. 정점의텍스쳐좌표가객체의표면에보간되어나타남. t (2.5,5.0,0.0) (0,1) (1,1) (0.0,0.0,0.0) (0,0) Texture space (1,0) s Object space (3.0,-2.0,0.0)
OpenGL Texture Coordinates 텍스쳐는다각형에고르게입힐필요는없음. 기하하적모델이나텍스쳐좌표사용에따라서, 이미지가때론왜곡되게나타날수도있음. OpenGL 에서텍스쳐좌표가 [0, 1] 영역을벗어날경우에 texture wrapping 방법으로정의함 : Repeat, Clamp 사각형에 4 개의텍스쳐좌표를 (0,0), (0,3.5), (3.5,0), (3.5,3.5) 로정의한예 glbegin(gl_quads); gltexcoord2i(0,0); glvertex3f(-1.0, -1.0, 0.0); gltexcoord2i(1,0); glvertex3f(1.0, -1.0, 0.0); gltexcoord2i(1,1); glvertex3f(x1, y1, 0.0); gltexcoord2i(0,1); glvertex3f(x0, y0, 0.0); glend(); Repeat Clamp Repeat &Clamp Mipmap 은이전 mipmap 너비와폭의절반크기임. 텍스쳐가작아질수록, 보다많은텍셀이한픽셀에적용되야하므로 GL_NEAREST 나 GL_LINEAR 필터가정확한계산결과를만들지않을수있음. 따라서객체가움직일때텍스쳐가 flickering 하게나타날수있음. 밉맵은이런 flickering 문제를줄여줄수있음. 그러나일반적으로희미하게보임. 텍스쳐맵핑을위한필터링방법 최근점필터 (nearest neighbor filter) GL_NEAREST 이선형필터 (bilinear interpolation filter) GL_LINEAR 삼선형필터 (trilinear interpolation filter) mipmap filter GL_LINEAR_MIPMAP_LINEAR 혼합필터 (hybrid filter) GL_NEAREST_MIPMAP_LINEAR GL_LINEAR_MIPMAP_NEAREST GL_NEAREST_MIPMAP_NEAREST GL_LINEAR GL_LINEAR_MIPMAP_LINEAR
gltexparameter{if}v(glenum target, GLenum pname, TYPE *param); GL_TEXTURE_WRAP_S, GL_TEXTURE_WRAP_T GL_CLAMP, GL_REPEAT GL_TEXTURE_MAG_FILTER GL_NEAREST, GL_LINEAR GL_TEXTURE_MIN_FILTER GL_NEAREST, GL_LINEAR (Mipmap 을사용하지않는경우 ) GL_NEAREST_MIPMAP_NEAREST, GL_NEAREST_MIPMAP_LINEAR, GL_LINEAR_MIPMAP_NEAREST, GL_NEAREST_MIPMAP_NEAREST GL_TEXTURE_BORDER_COLOR [0.0, 1.0] 영역의값 GL_TEXTURE_PRIORITY 0 또는 1 Texture Environment Parameters gltexenv{fi}[v](..) 를사용하여텍스쳐의색 (C t, A t ) 과현재처리하는프레임버퍼의픽셀색 (C f, A f ) 을어떻게혼합할지설정함 GL_TEXTURE_ENV_MODE 의모드 : GL_MODULATE: 텍스쳐의색성분과음영에서주어지는색성분을곱함으로써텍스쳐맵핑없이할당될음영을변조가능 GL_DECAL: 텍스쳐의색이객체의색을완전히결정 GL_BLEND: 환경색과합성함 GL_REPLACE: 텍스쳐색만사용함 GL_BLEND 의합성색은 GL_TEXTURE_ENV_COLOR 으로지정함 GLfloat blendcolor[] = {0.0, 1.0, 0.0, 0.5}; gltexenvfv(gl_texture_env, GL_TEXTURE_ENV_COLOR, blendcolor); Texture Environment Parameters OpenGL Texture Transformations 텍스쳐좌표의변환 기하물체의정점에변환하듯이텍스쳐좌표에이동 (translation), 회전 (rotate), 크기변환 (scaling) 을적용함 glmatrixmode(gl_texture) 를사용하여정점이아닌텍스쳐좌표에변환을적용함을지시함 glmatrixmode(gl_texture); glloadidentity(); gltranslatef(0.1, 0.05, 0); glrotatef(30.0, 0, 0, 1); glmatrixmode(gl_modelview); // geometry with texture coordinates
OpenGL Texture Transformations 텍스쳐좌표의변환 텍스쳐좌표에크기변환 (scaling) 을적용할때물체도또한같은크기변환을해야함 glmatrixmode(gl_texture); glloadidentity(); glscalef(size, 1, 1); glmatrixmode(gl_modelview); glscalef(size, 1, 1); // geometry with texture coordinates OpenGL Texture Movies 텍스쳐이미지 sequence 를이용하여 flipbook 애니메이션제작 inittexture() 함수에서전체텍스쳐이미지를읽어들임 idle() 함수에서 currenttextureid 를 update 함 drawtexture() 함수에서는동일한정점좌표와텍스쳐좌표에 glbindtexture(gl_texture_2d, currenttextureid); 를사용하여프레임당한텍스쳐를 binding 함 애니메이션효과를줌 image1 image2 image3 image4 image5 image6 OpenGL Compressed Textures glcompressedteximage2darb 를사용하여압축한텍스쳐를생성할수있음. 너비와높이의 RGB 값을가진일반텍스쳐보다압축한텍스쳐는메모리사용량을줄이고빨리그릴수있음 glcompressedteximage2darb(gl_texture_2d, 0, format, width, height, 0, size, imagebuffer); OpenGL Texture Coordinate Generation OpenGL 에서는텍스쳐좌표를자동적으로생성할수있음 S, T 방향으로텍스쳐좌표자동생성을활성화해야함 glenable(gl_texture_gen_s), glenable(gl_texture_gen_t) GL_TEXTURE_GEN_MODE 모드 : GL_OBJECT_LINEAR, GL_EYE_LINEAR, GL_SPHERE_MAP 평면 (plane) 을지정해야함 - 평면으로부터의거리에바탕을둔텍스쳐좌표를생성 gltexgenfv(gl_s, GL_OBJECT_PLANE, planecoefficients) planecoefficients = { 1, 0, 0, 0 }; gltexgeni(gl_s, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR) ; gltexgenfv(gl_s, GL_OBJECT_PLANE, planecoefficients); glenable(gl_texture_gen_s) ; glbegin(gl_quads) ; glvertex3f(-3.25, -1, 0); glvertex3f(-1.25, -1, 0) ; glvertex3f(-1.25, 1, 0); glvertex3f(-3.25, 1, 0) ; glend() ;
OpenGL Sphere Mapping OpenGL 에서는구형맵핑 (sphere mapping) 지원 구형맵핑텍스쳐좌표는 view 벡터가구표면의법선벡터에반사된 reflection 벡터로계산됨. 반사벡터를 2 차원텍스쳐좌표로맵핑하는것이간단하고하드웨어, 소프트웨어로도구현이가능. 그러나원형이미지를구하는것이어려움 (360 도의주변환경을담은이미지여야함 ). 아주넓은광학렌즈에의한원근투영을구하거나, 입방체투영을이용하여근사한값을얻음. gltexgeni(gl_s, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP) ; gltexgeni(gl_t, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP) ; glenable(gl_texture_gen_s) ; glenable(gl_texture_gen_t) ; OpenGL Box Mapping OpenGL 에서는입방체맵핑 (box mapping) 지원 입방체맵은반사맵핑 (reflection mapping) 의하나임 그러나입방체맵은 3 차원텍스쳐좌표를사용해야함 반사텍스쳐는환경을둘러싸고있는입방체의 6 면 2 차원텍스쳐 gltexgeni(gl_s, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP) ; gltexgeni(gl_t, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP) ; gltexgeni(gl_r, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP) ; glenable(gl_texture_gen_s) ; glenable(gl_texture_gen_t) ; glenable(gl_texture_gen_r) ; glenable(gl_texture_cube_map);