shading 표현

2023. 5. 6. 10:01컴퓨터 그래픽스/그래픽 알고리즘 및 표현

shader는 vertex shader와 pixel shader(hlsl기준)이 있습니다.

vertex shader는 직접 vertex들을 받아와서 좌표계 변환을 통해 실제 우리가 보는 화면은 어떻게 보이는지 좌표를 나타내고

 

rasterization을 통해 필요한 픽셀을 계산하고

 

pixel shader로 넘겨주어서 색을 칠해주게 됩니다.

 

vertex에서는 rotation, scaling, translation의 과정을 거치고 원근 표현을 거치고, world좌표계로 바꾼 뒤 raster(2차원)좌표계로 변하게 됩니다. 

 

vertex shader에서 

vertex 값들을 가져오고 

rotation을 위해 픽셀과, 변하는 각도를 넣어주고 행렬과 곱

translation을 위해 vertex.x + translation, vertex.y + translation ... 

scaling을 위해 곱셈을 하고 그 결과를 return 해줍니다. 

 

3차원 vertex를 받아오고 그걸 raster로 바꿔줍니다. 

ProjectWorldToRaster(vec3 pointWorld)
{
	const float scale = distEyeToScreen / (distEyeToscreen * pointWorld.z);
	vec2 projPoint = (pointWorld.x * scale, pointWorld.y * scale);
    
    const float aspectRatio = (float)width/ height;
    const vec2 pointNDC = vec2(projPoint.x / aspectRatio, projPoint.y); // 둘 다 나눠도 되고 하나를 기준으로 나누어도 된다.
    //raster
    const float xscale = width / 2;
    const float yscale = height/2;
    
    return vec2(pointNDC.x * xscale , pointNDC.y * yscale);
}

 

 

그리고 pixel shader로 넘깁니다.

 

pixel shader에서는 간단한 빛 표현을 합니다.

blinn phong 을 이용하여 ambient, specular, diffuse 를 더하여 결과를 return 합니다.

 

 

'컴퓨터 그래픽스 > 그래픽 알고리즘 및 표현' 카테고리의 다른 글

backface culling, 원근법  (0) 2023.05.03
깊이 버퍼(depth buffer)  (0) 2023.04.01
2차원 transformation  (0) 2023.02.22
Rasterization 원 그리기  (0) 2023.02.22
Rasterization  (0) 2023.02.15