2012-05-14 4 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

は、それらは同じ座標空間内になければなりません。モデルビューVertex(ビュースペース)をu_LightPos(ワールドスペース)から差し引くと、偽の結果が得られます。

ワールド空間で照度を計算するか、または表示領域を有効にするかを決定する必要がありますが、すべての入力を同じ空間に変換する必要があります。

これは、ワールド空間で頂点/法線/光度、またはビュー空間で頂点/法線/光度を取得することを意味します。

lightwaveにのビューマトリックス(モデルビューではない)を乗算し、u_Lightposの代わりに計算結果を使用してみてください。動作するはずです。

関連する問題