2012-05-14 2 views
0

내가, 내가 세상에서 FPS 카메라 movements.In 내가 하드 코딩 한 버텍스 쉐이더 카메라 위치 (u_LightPos)가 위의 튜토리얼과는 달리 http://www.learnopengles.com/android-lesson-two-ambient-and-diffuse-lighting/OpenGLES2 셰이더 : 조명 위치 및 카메라 이동?

에서 튜토리얼 다음 내 OpenGLES2 응용 프로그램에 조명을 추가하려고 coodinates.But의 카메라를 움직일 때 이상한 조명 효과를줍니다. 투영/뷰 매트릭스를 사용하여이 위치를 변형해야합니까?

uniform mat4 u_MVPMatrix;   
uniform mat4 u_MVMatrix;  


attribute vec4 a_Position;  
attribute vec4 a_Color;  
attribute vec3 a_Normal;  

varying vec4 v_Color; 

void main()   
{       
vec3 u_LightPos=vec3(0,0,-20.0); 
vec3 modelViewVertex = vec3(u_MVMatrix * a_Position); 
vec3 modelViewNormal = vec3(u_MVMatrix * vec4(a_Normal, 0.0));  

float distance = length(u_LightPos - modelViewVertex);   

    // Get a lighting direction vector from the light to the vertex. 
    vec3 lightVector = normalize(u_LightPos - modelViewVertex); 

    // Calculate the dot product of the light vector and vertex normal. If the normal and light vector are 
    // pointing in the same direction then it will get max illumination. 
    float diffuse = max(dot(modelViewNormal, lightVector), 0.1);  

    // Attenuate the light based on distance. 
    diffuse = diffuse * (1.0/(1.0 + (0.25 * distance * distance))); 

    // Multiply the color by the illumination level. It will be interpolated across the triangle. 
    v_Color = a_Color * diffuse; 

    // gl_Position is a special variable used to store the final position. 
    // Multiply the vertex by the matrix to get the final point in normalized screen coordinates. 
gl_Position = u_MVPMatrix * a_Position;       
} 

답변

1

벡터에 대한 산술 연산을 수행 할 때는 동일한 좌표 공간에 있어야합니다. U_LightPos (세계 공간)에서 modelViewVertex (보기 공간)를 빼면 가짜 결과가 나옵니다.

월드 공간에서 조명 계산을 수행할지 또는 뷰 공간 (유효해야하는지)을 결정해야하지만 모든 입력을 동일한 공간으로 변환해야합니다.

이것은 월드 공간에서 정점/법선/조명 효과를 얻거나 뷰 공간에서 정점/법선/조명 효과를 얻음을 의미합니다.

lightwave에 보기 행렬 (모델 뷰가 아님)을 곱한 다음 u_Lightpos 대신 계산에 사용하면 좋을 것 같습니다.