김형준 GIS 연구소 (for Developers)  
Front Page
Notice | E-Mail | Admin | Write Article   
 
2007/10/10 10:20 2007/10/10 10:20
OpenGL Shader - 27
GLSL 예제 : Lighting(Directional Light Per Pixel) - 3/6
원문 : http://www.lighthouse3d.com/opengl/glsl/index.php?dirlightpix

이 션섹에서는 이전 셕센에서의 쉐이더를 Directional Light를 픽셀 마다 계산하도록 수정할 것이다.

먼저 버텍스 당 우리가 받는 정보를 살펴보면, ...

  • 법선벡터
  • Half 벡터
  • 빛의 방향
법선벡터를 카메라 공간 좌표계로 변환하고 정규화해야한다. 또한 이미 카메라 공간 좌표계인 Half 벡터와 빛의 방향 벡터 역시 정규화해야 한다. 이들 정규화된 벡터는 보간되어질 것이고 프레그먼트 쉐이더로 보내지는데, 이를 위해서 정규화된 벡터를 유지하기 위해서 varying 변수를 선언할 필요가 있다.

버텍스 쉐이더에서는 광원설정값과 재질을 조합하는 몇가지 연산을 수행할 수 있다.

아래는 버텍스 쉐이더의 코드이다.

varying vec4 diffuse,ambient;
varying vec3 normal,lightDir,halfVector;
	
void main()
{	
    /* first transform the normal into eye space and 
    normalize the result */
    normal = normalize(gl_NormalMatrix * gl_Normal);
		
    /* now normalize the light's direction. Note that 
       according to the OpenGL specification, the light 
       is stored in eye space. Also since we're talking about 
       a directional light, the position field is actually direction */
    lightDir = normalize(vec3(gl_LightSource[0].position));
	
    /* Normalize the halfVector to pass it to the fragment shader */
    halfVector = normalize(gl_LightSource[0].halfVector.xyz);
					
    /* Compute the diffuse, ambient and globalAmbient terms */
    diffuse = gl_FrontMaterial.diffuse * gl_LightSource[0].diffuse;
    ambient = gl_FrontMaterial.ambient * gl_LightSource[0].ambient;
    ambient += gl_LightModel.ambient * gl_FrontMaterial.ambient;
	
    gl_Position = ftransform();
}
이제 프레그먼트 쉐이더에 대해서 살펴보자. 동일한 Varying 변수가 선언되어야 한다. 법선벡터를 다시 정규화해야한다. 하지만 빛의 방향벡터는 다시 정규화할 필요가 없다. 우리는 Directional Light에 대해 이야기 하고 있으므로 이 마지막 벡터는 모든 버텍스에 공통이다(쌩뚱맞은 말같은데....... =_=). 두개의 동일한 벡터 사이의 보간에 대한 결과는 같은 벡터이므로, 다시 정규화할 필요가 없는 것이다. 다음으로 우리는 보간되고 정규화된 법선벡터와 빛의 방향 벡터를 내적계산한다. 아래가 여기서 언급한 프레그먼트 쉐이더에 대한 시작 부분에 대한 코드이다.
varying vec4 diffuse,ambient;
varying vec3 normal,lightDir,halfVector;

void main()
{
    vec3 n,halfV;
    float NdotL,NdotHV;
		
    /* The ambient term will always be present */
    vec4 color = ambient;
		
    /* a fragment shader can't write a varying variable, hence we need
       a new variable to store the normalized interpolated normal */
    n = normalize(normal);
		
    /* compute the dot product between normal and ldir */
    NdotL = max(dot(n,lightDir),0.0);
	
    ....
}
만약 NdotL이 0보다 크다면, Diffuse 요소를 계산해야 하는데, 버텍스 쉐이더로부터 받은 Diffuse 설정값은 내적값으로 곱해진다. Specular 요소도 반드시 계산해야 한다. Specular 요소를 계산하기 위해서는 먼저 버텍스 쉐이더로부터 받은 halfVector를 정규화해야하고, halfVector와 normal 간의 내적 계산을 한다.
    ....

    if (NdotL > 0.0) {
        color += diffuse * NdotL;
        halfV = normalize(halfVector);
        NdotHV = max(dot(n,halfV),0.0);
        color += gl_FrontMaterial.specular * 
        gl_LightSource[0].specular * 
        pow(NdotHV, gl_FrontMaterial.shininess);
    }
	
    gl_FragColor = color;
}

다음 이미지는 Per Pixel과 Per Vertex 광원에 대한 결과화면이다.

Tag : ,
Track this back : http://www.gisdeveloper.co.kr/trackback/326
Commented by Smle at 2009/04/16 14:33  r x
정말 좋은 자료 감사해요 도움이 많이 됐어요-
Commented by 김형준 at 2009/04/16 18:46  r x
Smle님, 댓글 감사합니다~ ^^

name    password    homepage
 hidden
BLOG main image
 Notice
DuraMap-Xr 소개 및 다운로드
[오픈소스] SimpleSHP v0.1
FingerEyes-Xr 소개 및 다운로드
OpenGL Tutorials
 Category
전체 (531)
GIS 개발 (146)
프로그래밍 (233)
스치는 생각들 (129)
번역 또는 집필 (3)
 TAGS
GIS Xr OpenGL Shader FingerEyes BlackPoint Algorithm Java Map Engine WPF
 Calendar
«   2012/02   »
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29      
 Recent Entries
[FingerEyes] 지오메트리...
[FingerEyes] Geometry로...
[FingerEyes] FID 리스트...
[FingerEyes] UPDATE, INS...
영화, "부러진 화살"
 Recent Comments
소스코드 그대로 써보아도...
인현환 - 13:50
글의 예제 코드의 숫자들...
김형준 - 12:57
안녕하세요. 포스트 잘 보...
인현환 - 11:15
메일로 답변드렸습니다....
김형준 - 02/01
txt파일을 엑셀로 변환하...
최상준 - 02/01
코봉히님두 새해 복 많이...
김형준 - 01/25
아 너무 감사합니다. 새해...
코봉히 - 01/23
wkb는 http://www.gisdeve...
김형준(Dip2K) - 01/23
wkb의 구조가 shp파일의...
코봉히 - 01/20
wkb는 바이너리인지라.....
김형준(Dip2K) - 01/20
 Archive
2012/02
2012/01
2011/12
2011/11
2011/10
2011/09
2011/08
2011/07
2011/06
2011/05
2011/04
2011/03
 Link Site
Adobe Flex 3 Help
Cartograph 2.0
GADM
GIS 위키디피아
GIS 프로그래밍 연구소
MapTools.org
OGC
OGRE3D
OSGeo 한국 지부
Paul Bourke Site
Wikipedia
국가수자원관리 정보시스템
국립지리원
국토연구원
국토해양부
네이버 과학
대한측량협회
류광님의 블로그
이민파님의 공간분석과 리...
지오서비스(GeoService)
 Visitor Statistics
Total : 929877
Today : 528
Yesterday : 511
태터툴즈 배너
rss