【问题标题】:OpenGLES2 Shader : Lighting position and camera movement?OpenGLES2 Shader:光照位置和摄像机运动?
【发布时间】:2012-05-14 04:20:47
【问题描述】:

我尝试按照http://www.learnopengles.com/android-lesson-two-ambient-and-diffuse-lighting/ 的教程为我的 OpenGLES2 应用程序添加光照

与上面的教程不同,我有 FPS 摄像机运动。在顶点着色器中,我在世界坐标中硬编码了摄像机位置 (u_LightPos)。但是当我移动摄像机时它会产生奇怪的照明效果。我必须转换这个位置吗使用投影/视图矩阵?

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;                         
}  

【问题讨论】:

    标签: graphics opengl-es opengl-es-2.0 shader vertex-shader


    【解决方案1】:

    当对向量进行算术运算时,它们必须在同一个坐标空间中。你从 u_LightPos (世界空间)中减去 modelViewVertex (视图空间),这会给你一个虚假的结果。

    您需要决定是要在世界空间还是视图空间中进行光照计算(两者都应该有效),但您必须将所有输入转换到同一个空间。

    这意味着要么在世界空间中获取顶点/法线/光照位置,要么在视图空间中获取顶点/法线/光照位置。

    尝试将您的 lightpos 乘以 view 矩阵(不是模型视图),然后在计算中使用它而不是 u_Lightpos,我认为它应该可以工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-09-14
      • 2020-09-02
      • 1970-01-01
      • 2022-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多