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

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

Daniel803 2023. 9. 27. 08:46

vertex shader는 컴퓨터 그래픽에서 3D 모델의 vertex 데이터를 조작하는 데 사용되는 shader 프로그램의 한 유형이다. vertex shader는 모델의 각 vertex에 대해 실행되며 vertex 위치 변환 (transforming the vertex position), texture 좌표 정규화 (normalizing texture coordinates) 및 기타 vertex별 작업 수행 (carrying out other per-vertex operations) 등을 수행한다. vertex shader의 출력은 일반적으로 특정 pipeline 구성에 따라 geometry shader와 같은 rendering pipeline의 다음 단계로 전달되거나 fragment shaer로 직접 전달된다.

 

 vertex shader는 GLSL (OpenGL Shading Language), HLSL (High-Level Shading Language) 또는 이와 비슷한 high-level shading langauge로 작성된다. vertex shader는 그래픽 드라이버에 의해 machine code로 컴파일되고 GPU (Graphics Processing Unit)에서 실행된다.

 

 아래는 GLSL로 작성한 vertex shader 예시 코드다.

#version 330 core
layout (location = 0) in vec3 aPos;  // Input vertex position
layout (location = 1) in vec3 aColor; // Input vertex color

out vec3 ourColor; // Output variable to be passed to the fragment shader

uniform mat4 model; // Model matrix
uniform mat4 view;  // View matrix
uniform mat4 projection; // Projection matrix

void main()
{
    gl_Position = projection * view * model * vec4(aPos, 1.0);
    ourColor = aColor;
}

 

 이 예제에서 vertex shader는 각 vertex에 대한 3D 위치 (aPos)와 색상 (aColor) 를 가져오고, vertex 위치에 일련의 변환 매트릭스 (model, view, projection) 를 곱해 vertex를 클립 공간 (clip space) 으로 변환한다. 변환된 vertex 위치는 내장된 출력 변수인 gl_Postion에 저장된다. 색상 (ourColor) 는 추가 처리를 위해 다음 단계 (fragment shader 같은) 로 전달된다.

 

 vertex shader는 프로그래밍하면 3D 그래픽을 렌더링할 때 유연성과 창의성을 크게 높일 수 있으므로 게임 개발자, 시각화 전문가 및 3D 렌더링에 관심 있는 모든 사람에게 강력한 툴이다.

 

 

참고

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

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