OpenGL Frame Buffer Buffer, Image, and Texture Mapping 514780 2017 년가을학기 11/16/2017 단국대학교박경신 색버퍼 (Color buffers) 전면버퍼 (Front buffer) 후면버퍼 (Back buffer) 보조버퍼 (Auxiliary buffer) 오버레이면 (Overlay plane) 깊이버퍼 (Depth buffer) 누적버퍼 (Accumulation buffer) High resolution buffer 스텐실버퍼 (Stencil buffer) Holds masks Buffer Selection OpenGL 은 front, back, depth buffer 등모든버퍼로부터데이터를읽는것이가능 기본적프레임버퍼로 back buffer 가선택되어있으며, glreadbuffer 를이용하여다른버퍼로바꿀수있음 Note: 프레임버퍼에픽셀포멧은프로세서메모리에포멧과는다름. 또한이메모리는각각다른곳에저장됨. 따라서, 데이터 packing과 unpacking이필요함 Reading은다소느릴수있음 OpenGL Buffer Management Functions 버퍼를 clear glclear(glbitfield mask); // 특정버퍼를 clear 함 GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT GL_ACCUM_BUFFER_BIT GL_STENCIL_BUFFER_BIT glclearbuffer(); 버퍼의 clear value set glclearcolor(); glcleardepth(); glcleardepthf(); glclearstencil(); 버퍼를 mask (i.e., enabled or disabled) glcolormask[i](glboolean red, GLboolean green, GLbooleanblue, GLboolean alpha); // r,g,b,a 색을프레임버퍼에쓸지아닌지지정 gldepthmask(glboolean flag); // depth 버퍼에쓸지아닌지지정 glstencilmask(gluint mask); // stencil 버퍼에 bit mask 를쓸지아닌지지정 3
Images 이미지데이터는파일에서부터읽어들이거나직접생성 일반적인이미지파일포멧 : JPEG, TIFF, PNG, GIF, RGB, EPS, BMP, etc 이미지포멧 : Color channel: greyscale, RGB, RGBA Bit resolution Compression: 손실부호화 (lossy coding), 무손실부호화 (lossless coding) COIN3D simage http://www.coin3d.org/lib/simage COIN3D simage library가제공하는이미지파일포멧 JPEG, TIFF, PNG, PIC, TGA, EPS, GIF, RGB, etc COIN3D의 simage library를사용하려면, 프로젝트에 additional library and include directory를추가해야한다. Project -> Properties(ALT+F7) -> Configuration Properties -> C/C++ -> General에 Additional Include Directories에.\include를넣는다. Project -> Properties(ALT+F7) -> Configuration Properties -> C/C++ -> Preprocessor에 Preprocessor Definitions 에 ;SIMAGE_DLL를넣는다. Project -> Properties(ALT+F7) -> Configuration Properties -> Linker -> General에 Additional Library Directories에.\lib\debug를넣는다. Project -> Properties(ALT+F7) -> Configuration Properties - > Linker -> Input에 Additional Dependencies에 simage1.lib을넣는다. COIN3D simage Example Texture Mapping unsigned char *imgptr; unsigned char *imagedata; unsigned char *rescaledimagedata; int imagewidth = 0, imageheight = 0, numcomponents = 0; imagedata = simage_read_image (filename, &imagewidth, &imageheight, &numcomponents); // read GLsizei xdim2,ydim2; // 만약이미지크기가 2의승수가아니면, resize GLenum type; xdim2 = 1; while (xdim2 <= imagewidth) xdim2 *= 2; xdim2 /= 2; ydim2 = 1; while (ydim2 <= imageheight) ydim2 *= 2; ydim2 /= 2; if ((imagewidth!= xdim2) (imageheight!= ydim2)) { rescaledimagedata = simage_resize(imagedata, imagewidth, imageheight, numcomponents, xdim2, ydim2); imgptr = rescaledimagedata; } else imgptr = imagedata; Wireframe Smooth shading Flat shading Texture mapping
The Limits of Geometric Modeling 기하학적모델링 고성능그래픽스카드가초당 1000만개의폴리곤을렌더링할수있다하더라도구름, 초원, 지형, 스킨 (skin) 등과같은객체 / 현상을기하학적텍스쳐들로모델링하여처리하기엔무리가있음. 텍스쳐맵핑 2차원영상을직접평면표면에입힘. 제한된수의다각형을사용해야하는실시간렌더링에있어서비교적적은추가비용으로이미지의사실성을상당히높일수있는기법임. Three Types of Mapping 텍스쳐맵핑 (Texture Mapping) 이미지를사용하여다각형 (polygon) 의내부에그려짐. 환경맵핑 (Environment/Reflection mapping) 환경이미지가렌더링될표면위에그려짐. 반사맵 (reflection map) 또는환경맵 (environmental map) 은반사광을추적하지않고도광선추적법에의한매우반짝이는표면 (highly specular surface) 을만들수있음. 범프맵핑 (Bump mapping) 렌더링음영작업시법선벡터 (normals) 를왜곡시켜실제오렌지표면의융기와같은모양을생성. Texture Mapping Environment Mapping geometric model texture mapped
Bump Mapping Where does texture mapping take place? 텍스쳐맵핑은렌더링파이프라인의마지막단계에서실행 Texture Mapping 개념적인 2 차원텍스쳐맵핑과정 표면매개화 (surface parameterization) 과정 텍스쳐이미지를물체에어떻게입힐것인가? 물체의각점에맵핑되는텍스쳐이미지의좌표가필요함 (x 0, y 0, z 0 ) => (x t, y t ) 기하변환과정 기하변환은물체의각점과투영화면에서의위치간의맵핑관계를결정. (x 0, y 0, z 0 ) => (x s, y s ) 래스터과정 각기하물체가투영되는화소들을찾아주는과정 텍스쳐색계산과정 각화소에적절히텍스쳐색으로칠해주는과정 각화소를통하여보이는텍스쳐이미지의색을어떻게계산할것인가? 계산된텍스쳐색을물체의원래의색깔과어떻게혼합할것인가? 2D Texture Mapping 텍스쳐맵핑은 2 차원의이미지를 3 차원의표면에맵핑 모두 3~4 개의좌표계간맵핑처리를포함한일련의과정이필요함 2D image 3D surface
Coordinate Systems Parametric coordinates 곡선과곡면 (curves & surfaces) 모델링에사용하는매개변수형좌표계 (u, v) Texture coordinates 텍스쳐내의위치지정을위해사용하는텍스쳐좌표계 (s, t) Object or World Coordinates 텍스쳐가입혀질물체를기술하는객체좌표계또는세상좌표계 (x, y, z) Window Coordinates 최종적인이미지가형성되는윈도우좌표계 (x s, y s ) Texture Mapping Parametric coordinates Texture coordinates World coordinates Window coordinates Mapping Functions 텍스쳐는생성하여프로세서메모리에배열로저장됨 텍스쳐는응용프로그램에의해생성되거나사진으로부터스캔하여생성할수있음 이배열의요소들을텍셀 (texture element = texel) 이라부름 텍스쳐좌표에서표면의한점으로맵핑하는함수 텍스쳐좌표 : T(s, t) 객체좌표 : P(x, y, z) (x,y,z) x = x(s,t) y = y(s,t) z = z(s,t) t s Backward Mapping 물체위의점 (x, y, z) 이주어졌을때, 대응하는텍스쳐좌표를어떻게찾을것인가? 한픽셀의음영을결정할때텍스쳐이미지의어떤점을사용할지를결정해야할경우, 화면좌표로부터텍스쳐좌표로의맵핑을필요로함 텍셀 T(s, t) 를찾는역함수 s = s(x,y,z) (x,y,z) t = t(x,y,z) t s
Two-part mapping 맵핑문제의해법 : Two-part mapping 텍스쳐를먼저구 (sphere), 원기둥 (cylinder), 입방체와같은간단한 3 차원매개변수형표면에텍스쳐를맵핑 예제 : 원기둥 (cylinder) 으로맵핑 r h Cylindrical Mapping 매개변수형원기둥함수 (parametric cylinder) x = r cos 2 u y = r sin 2 u z = v/h u: (0,1) v: (0,1) u, v 공간의사각형을세상좌표계의반지름 r 과높이 h 를가진원기둥에맵핑 s = u t = v 그리고, 텍스쳐공간으로부터맵핑 x = r cos 2 s y = r sin 2 s z = t/h Spherical Map 매개변수형구함수 (parametric sphere) x = r cos 2 u y = r sin 2 u cos 2 v z = r sin 2 u sin 2 v 원기둥함수와비슷함. 그러나구와같이닫힌객체에맵핑할경우형상의일그러짐이필요함. 메르카토르 (Mercator) 투영의경우, 양극 (pole) 에가장큰일그러짐을만듬 Spherical mapping은환경맵 (environmental maps) 에서 사용됨 x = r cos 2 s y = r sin 2 s cos 2 t z = r sin 2 s sin 2 t Box Mapping 간단한직교투영 (simple orthographic projection) 과사용하기에편리함 Box mapping 은환경맵 (environmental maps) 에서사용됨
Second Mapping 두번째맵핑 (second mapping): 중간객체 (intermediate object) 상의텍스쳐값을원하는표면 (actual object) 에맵핑 중간매개표면으로부터의법선사용 객체표면으로부터의법선사용 객체중심으로부터의벡터사용 Second Mapping 물체를중개면내부에넣고물체면에텍스쳐를입힘. actual object intermediate object Aliasing 텍스쳐의포인트샘플링 (point sampling) 방법은앨리어싱문제 (aliasing error) 를만들수있음 포인트샘플링 점에서점으로의맵핑 Area Averaging 보다효과적이나다소느린영역평균법 (area averaging) 방법을사용가능함 영역평균법 - 영역에서영역으로의맵핑 miss blue stripes point samples in u,v (or x,y,z) space pixel preimage point samples in texture space Note: 화소의전이미지 (preimage) 는곡면임
Basic Strategy Texture Mapping 텍스쳐를입히기위한 3 가지단계 1. 텍스쳐를지정 이미지를파일에서읽거나직접생성 텍스쳐에할당 y 텍스쳐링활성화 2. 각정점에텍스쳐좌표를할당 프로그래머가직접적절한맵핑함수작성 z x geometry display 3. 텍스쳐맵핑방법을지정 랩핑 (wrapping) 필터링 (filtering) t image Texture coordinates: T(s, t) s = f(x, y, z) t = g(x, y, z) Value: s LUMINANCE, RGB, RGBA OpenGL Texture Example 다음예제는 256 x 256 이미지를사각형 (rectangular polygon) 에텍스쳐맵핑하여원근투영한모습을보여주고있음. Texture Mapping in the OpenGL Pipeline 이미지와기하학적객체는각기다른파이프라인에서연산된후래스터과정에서합성됨 즉, 복잡한텍스쳐가있다하더라도기하학적객체의복잡성 (complexity) 에전혀영향을주지않음 정점 geometry pipeline t 256x256 rasterizer s 이미지 pixel pipeline
Specifying a Texture Image in OpenGL 512x512크기의이미지 imagedata 텍스쳐이미지를프로그램에서생성하거나파일로부터읽혀서 CPU 메모리에배열로정의함 GLubyte imagedata[512][512]; 텍스쳐맵핑을활성화 glenable(gl_texture_2d) OpenGL은 1,2,3,4-차원텍스쳐맵을지원함 Define Image as a Texture glteximage2d(target, level, components, width, height, border, format, type, texels ); target: 텍스쳐타입, e.g., GL_TEXTURE_2D level: mipmapping에서사용 components: texel별요소, e.g., RGB width, height: texel의너비와높이 (in pixels) border: smoothing에사용 format, type: texel을정의하는포멧과타입 texels: texel 배열에대한포인터 glteximage2d(gl_texture_2d, 0, RGB, imagewidth, imageheight, 0, GL_RGB, GL_UNSIGNED_BYTE, imagedata); Converting A Texture Image OpenGL은텍스쳐이미지크기가 2의승수 (power of 2) 로지정되어야함 64x64, 64x128, 512x512, 만약이미지의크기가 2의승수가아니면, 프로그램상에서 gluscaleimage를사용하여크기조절을해야함 gluscaleimage( format, w_in, h_in, type_in, *data_in, w_out, h_out, type_out, *data_out ); data_in 원본이미지 data_out 이미지크기가조절된최종이미지 크기조절시이미지는자동적으로보간과필터링됨 Mapping a Texture 매개변수형텍스쳐좌표에바탕을두고있음 각정점별로텍스쳐좌표를지정해야함 t (1, 1) (s, t) = (0.2, 0.8) (0, 1) A a c (0.4, 0.2) b B C (0, 0) (1, 0) s (0.8, 0.4) Texture Space Object Space
Mapping a Texture 각정점별로텍스쳐좌표지정 // Square Vertices Positions squarevertices.push_back(glm::vec3(-0.75f, -0.75f, 0.0f)); squarevertices.push_back(glm::vec3(0.75f, -0.75f, 0.0f)); squarevertices.push_back(glm::vec3(0.75f, 0.75f, 0.0f)); squarevertices.push_back(glm::vec3(-0.75f, 0.75f, 0.0f)); // Square Vertices Texture Coordinates squaretexturecoords.push_back(glm::vec2(0.0f, 0.0f)); squaretexturecoords.push_back(glm::vec2(1.0f, 0.0f)); squaretexturecoords.push_back(glm::vec2(1.0f, 1.0f)); squaretexturecoords.push_back(glm::vec2(0.0f, 1.0f)); Interpolation OpenGL 은다각형내부점에대한텍스쳐좌표를계산하기위해보간 (interpolation) 을이용함 good selection of tex coordinates poor selection of tex coordinates texture stretched over trapezoid showing effects of bilinear interpolation Note: 코드의효율을향상시키기위하여정점배열을사용 Checkerboard 텍스쳐를삼각형에맵핑하기 Checkerboard 텍스쳐를사다리꼴에맵핑하기 Texture Parameters Wrapping Mode OpenGL에서텍스쳐가어떻게적용될지결정해주는다양한인자 (parameters) 를제공하고있음 랩핑 (Wrapping) s, t가 (0, 1) 의범위를넘었을때어떻게 s, t 값을어떻게정할것인지결정해주는인자. CLAMP, REPEAT 필터 (Filter modes) 포인트샘플링대신영역평균법을사용하도록함. 밉맵 (Mipmapping) 텍스쳐를여러개의해상도를사용할수있도록함. 환경인자 (Environment parameters) 텍스쳐맵핑이음영 (shading) 과어떻게상호작용할지결정해줌 Clamp: (0, 1) 범위내로값을강제조정 만약 s, t가 1보다크면, 1을사용 만약 s, t가 0보다작으면, 0을사용 Repeat: (0, 1) 범위를넘어선값에대해텍스쳐반복 s,t %1을사용 gltexparameteri(gl_texture_2d, GL_TEXTURE_WRAP_S, GL_CLAMP) gltexparameteri(gl_texture_2d, GL_TEXTURE_WRAP_T, GL_REPEAT) t texture s GL_REPEAT wrapping GL_CLAMP wrapping
Magnification and Minification 확대 (Magnification) texel이한 pixel보다클때 축소 (Minification) texel이한 pixel보다작을때 포인트샘플링 (Point sampling) 선형필터링 (linear filtering) 포인트샘플링에의해결정된텍셀의이웃을포함한텍셀그룹의가중평균을사용 최근점 (nearest) 선형보간에의해계산된값에가장가까운텍셀값사용 Filter Modes 텍스쳐필터지정 gltexparameteri( target, type, mode ) gltexparameteri(gl_texture_2d, GL_TEXURE_MAG_FILTER, GL_NEAREST); gltexparameteri(gl_texture_2d, GL_TEXURE_MIN_FILTER, GL_LINEAR); Texture Polygon Magnification Texture Polygon Minification Mipmapped Textures 밉맵핑 (Mipmapping) 은일련의축소된크기의텍스쳐배열 (prefiltered texture map) 을생성하는것을허용 밉맵핑은작은객체에텍스쳐맵핑을할시보간문제를줄여줌 텍스쳐정의할시밉맵계층레벨을지정할수있음 glteximage2d( GL_TEXTURE_*D, level, ) 밉맵함수를통해서자동으로축소된크기의텍스쳐생성 glgeneratemipmap(gl_texture_2d) OpenGL 에서최적의밉맵핑과포인트샘플링을이용하기위해다음옵션사용 gltexparameteri(gl_texture_2d, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST) gltexparameteri(gl_texture_2d, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR) Aliasing Example point sampling mipmapped point sampling linear filtering mipmapped linear filtering
Texture Environment gltexenv{fi}[v](..) 를사용하여텍스쳐와음영간의상호작용을지정함 gltexenvf(gl_texture_env, GL_TEXTURE_ENV_MODE, mode); GL_TEXTURE_ENV_MODE 의모드 : GL_MODULATE: 텍스쳐의색성분과음영에서주어지는색성분을곱함으로써텍스쳐맵핑없이할당될음영을변조가능 GL_DECAL: 텍스쳐의색이객체의색을완전히결정 GL_BLEND: 환경색과합성함 GL_REPLACE: 텍스쳐색만사용함 GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE 합성할색은 GL_TEXTURE_ENV_COLOR 으로지정함 Generating Texture Coordinates gltexgen{ifd}[v]() 를사용하여텍스쳐좌표를자동적으로생성할수있음 평면 (plane) 을지정해야함 - 평면으로부터의거리에바탕을둔텍스쳐좌표를생성 모드 : GL_OBJECT_LINEAR: 탁자상단에나무결을입힐때 GL_EYE_LINEAR: 동적으로움직이는객체의외형을표현할때 GL_SPHERE_MAP: 환경맵에서사용함 Glfloat planes[] = {0.5, 0.0, 0.0, 0.5} // s=x/2 + ½ Glfloat planet[] = {0.0, 0.5, 0.0, 0.5} // t=y/2 + ½ gltexgeni(gl_s, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR); gltexgeni(gl_t, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR); gltexgenfv(gl_s, GL_OBJECT_LINEAR, planes); gltexgenfv(gl_t, GL_OBJECT_LINEAR, planet); Texture Objects 텍스쳐는 OpenGL state임 다른객체마다다른텍스쳐를가진다면, OpenGL은 CPU 메모리에서텍스쳐메모리로아주큰데이터를움직여야함. 최신 OpenGL은텍스쳐객체 (texture objects) 를가짐 텍스쳐객체당 1개이미지 텍스쳐메모리 (Texture memory) 는여러개텍스쳐객체를가질수있음 Environment Mapping 환경맵 (Environment Maps) 물체를둘러싸고있는주위환경이이물체에반사되는결과를원할경우 3차원공간이매우큰가상적구 (sphere) 또는입방체 (cube) 에의해둘러싸였다고가정하고 Second mapping에서시점반사벡터를사용 - 시점위치에따라서로다른모습을보임. Spherical environment mapping 180도와이드앵글렌즈로찍은환경이미지로부터 spherical map 생성후자동텍스쳐좌표생성을사용
Multitexturing 멀티텍스쳐링 (Multitexturing) 하나이상의텍스쳐를객체에적용해서렌더링효과를높이는경우 + = 라이트매핑 (Light Mapping) 물체면의밝기를계산하는대신텍스쳐와조명결과를혼합하여결과적으로영상을직접물체면에입힘 + =