[OpenGL] 9. Texture Mapping

scarleter99·2023년 5월 30일
0

OpenGL

목록 보기
9/10

📌 Texture Mapping

  • 오브젝트 표면에 세부적인 질감의 묘사를 하거나 색을 칠한다.

📌 Texture Mapping

  • Texture 좌표계에 Bilinear interpolation을 적용하여 색을 결정한다.
    • 믹싱된 색상이 나타난다.
  • GPU 메모리로 Texture 데이터를 옳긴 후에만 가능하다.

Initialization

// variables for texture mapping
GLuint tex_id;
GLsizei width, height;
GLbyte *img_pixels;

// load an image from system
// img_pixels must locate the client-side memory of the image
// width/height should be update
// pixel format is important
// …

// Generate a texture
glGenTextures(1, &tex_id);
// Bind a texture w/ the following OpenGL texture functions
glBindTexture(GL_TEXTURE_2D, tex_id);
// Select active texture unit 0
glActiveTexture(GL_TEXTURE0);

// Set texture parameters (wrapping modes, sampling methods)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

// Transfer an image data in the client side to the server side
glTexImage2D(tex_id, 0, GL_RGBA, width, height, 0,
	GL_RGBA, GL_UNSIGNED_BYTE, pixels);
    
// Specify texture sampler in shader
glUniform1i(glGetUniformLocation(program, "my_sampler"), 0);

Rendering

// Select active texture unit
glActiveTexture(GL_TEXTURE0);

// Bind a texture w/ the following OpenGL texture functions
glBindTexture(GL_TEXTURE_2D, tex_id);

// Rendering w/ texcoords
glBindBuffer()
glEnableVertexAttribArray(loc_a_texcoord)

glVertexAttribPointer(loc_a_texcoord,)

glDrawArrays();

glDisableClientState(loc_a_texcoord);

Texture Address Mode

// texture address mode as repeat
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

// texture address mode as clamp
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  • Texture 좌표 값이 [0, 1]에 들어가지 않을 경우 처리 방법을 결정한다.

Texture Filtering Mode

// setting for nearest samplings
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

// setting for linear samplings
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  • Antialiasing 기법 중 Blurring하는 방법이다.
  • Magnification(texel이 pixel보다 큰 경우) / Minification(texel이 pixel보다 작은 경우) 문제를 처리하는 방법이다.

Aliasing

  • 오브젝트 경계에서의 계단 현상이다.

Antialiasing

  • 경계 안쪽과 바깥쪽 색을 해당 픽셀의 점유율에 비례해서 믹싱하여 픽셀의 색을 결정한다.(Blurring)

Mipmap

// Bind texture
glBindTexture(GL_TEXTURE_2D, tex_id);

// Setting several texture parameters
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);

// Specify texture image data
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture_width, texture_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, texture_data);

glGenerateMipmap(GL_TEXTURE_2D); // Generate a mipmap texture, Unavailable in OpenGL 2.1
  • Minification 문제를 효율적으로 해결하는 방법이다.
  • 미리 이미지 피라미드를 만들어두고 랜더링 시 사용한다.

Multisample Anti-Aliasing (MSAA) & Full-Scene Anti-Aliasing (FSAA, SSAA)

  • Multisample Anti-Aliasing (MSAA)
    • 픽셀 당 추출 지점을 정해 sampling한다.
  • Full-Scene Anti-Aliasing (FSAA, SSAA)
    • n배 많은 sampling을 하여서 그래픽 품질이 좋다.
      • ex) 4x FSAA, 16x FSAA
    • 많은 양의 연산량을 필요로 한다.
profile
매일 공부

0개의 댓글