기술(Tech, IT)/컴퓨터 그래픽스 (Computer Graphics)

[CG] Vertex Shader (버텍스 쉐이더) - 2

Daniel803 2023. 9. 28. 08:59

 vertex shader의 주요 작업을 세분화 해보자.

 

  1. Transforming Vertex Postion (vertex 위치 변환)
    : 가장 일반적인 작업은 vertex 위치를 오브젝트 공간 (각 오브젝트가 자체 원점을 기준으로 정의되는 곳)에서 클립 공간 (화면에서 표시된 공간) 으로 변환하는 것. vertex 위치에 model, view, projection 등 여러 변환 행렬을 곱하는 작업이 포함.

  2. Normalizing Texture Coordinates (texture 좌표 정규화)
    : texture 좌표는 texture 모델의 경우 (0, 0)에서 (1, 1)까지로, 때로는 이러한 좌표가 vertex sahder에서 변환되거나 스케일링 될 수 있지만, 이 작업은 애플리케이션에 따라 선택적이다.

  3. Other Per-Vertex Operations
    : vertex normals를 기반으로 조명 값을 계산하거나 특수 효과를 위해 vertex 데이터를 사용자 정의 방식으로 변경하는 등의 작업이 포함될 수 있다.

 아래 예시는 두 개의 삼각형으로 구성된 정사각형이 있고 아래와 같은 작업을 하고 싶다고 가정해보자.

- 정사각형을 y 방향으로 1단위 위로 이동 (vertex 위치 변형)

- texture를 세로로 뒤집기 (texture 좌표 정규화)

 

원본 데이터

vertex 위치: (0, 0, 0), (1, 0, 0), (1, 1, 0), (0, 1, 0)

texture 좌표: (0, 0), (1, 0), (1, 1), (0, 1)

 

vertex shader (Pseudo-code)

for each vertex
    // Transforming Vertex Position
    vertex_position = vertex_position + [0, 1, 0]
    
    // Normalizing Texture Coordinates (flipping texture vertically)
    texture_coordinate.y = 1 - texture_coordinate.y

    // Output the transformed data
    output_vertex_position = projection_matrix * view_matrix * model_matrix * vertex_position
    output_texture_coordinate = texture_coordinate

변형된 데이터

- 변형된 vertex 위치: (0, 1, 0), (1, 1, 0), (1, 2, 0), (0, 2, 0)

- 변형된 texture 좌표: (0, 1), (1, 1), (1, 0), (0, 0)

 

 이 예시에서, vertex shader는 vertex 위치를 Y축으로 1단위 위로 이동해 정사각형을 위로 이동하고, texture 좌표의 y성분을 반전해 texture를 수직으로 뒤집었다.

 

 실제 애플리케이션에서 이러한 작업은 vertex shader 프로그램에 GLSL 또는 HLSL과 같은 high-level shading language로 작성되고 GPU에서 수행된다. 변형된 vertex 데이터는 그래픽 pipeline의 다음 단계로 전달된다.

 

참고 

- https://mingyu0403.tistory.com/110

- https://m.blog.naver.com/trick14/100148130691