我正在为我的OpenGLES 2.0渲染器编写一个灯光着色器。我已经搞乱了一个示例项目的着色器,并且已经开始使用定向照明。但是当我尝试将它实现到我们自己的代码中时,它的行为非常奇怪 - 某些部分看起来似乎在他们不应该等等时点亮。
我会在此处发布着色器代码,如果您想要更多,请提出要求。我和一个队友一直试图解决这个过去一周半没有进展,我们真的需要一些帮助。感谢。
final String vertexShader =
"uniform mat4 u_MVPMatrix;" // A constant representing the combined model/view/projection matrix.
+ "uniform mat4 u_MVMatrix;" // A constant representing the combined model/view matrix.
+ "uniform vec3 u_LightPos;" // The position of the light in eye space.
+ "uniform vec3 u_VectorToLight;"
+ "attribute vec4 a_Position;" // Per-vertex position information we will pass in.
+ "attribute vec4 a_Color;" // Per-vertex color information we will pass in.
+ "attribute vec3 a_Normal;" // Per-vertex normal information we will pass in.
+ "varying vec4 v_Color;" // This will be passed into the fragment shader.
+"vec3 modelViewVertex;"
+"vec3 modelViewNormal;"
+"vec4 getPointLighting();"
+"vec4 getAmbientLighting();"
+"vec4 getDirectionalLighting();"
+ "void main()" // The entry point for our vertex shader.
+ "{"
// Transform the vertex into eye space.
+ " modelViewVertex = vec3(u_MVMatrix * a_Position);"
// Transform the normal's orientation into eye space.
+ " modelViewNormal = vec3(u_MVMatrix * vec4(a_Normal, 0.0));"
// Will be used for attenuation.
// Multiply the color by the illumination level. It will be interpolated across the triangle.
// + " v_Color = getAmbientLighting();"
+ " v_Color = getDirectionalLighting();"
//+ " v_Color += getPointLighting(); \n"
// 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;"
+ "}"
+ "vec4 getAmbientLighting(){"
+ " return a_Color * 0.1;"
+ "}"
+ "vec4 getPointLighting(){"
+ " 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.0);"
// Attenuate the light based on distance.
+ " diffuse = diffuse * (1.0 / (1.0 + distance));" +
" return a_Color * diffuse;"
+"}"
+"vec4 getDirectionalLighting(){" +
"vec3 lightVector = normalize(u_VectorToLight - modelViewVertex);"+
"float diffuse = max(dot(modelViewNormal, lightVector), 0.0);" +
"diffuse *= .3;"+
"return a_Color * diffuse;"+
"}";
我已将所有内容留在那里,即使是我们放弃的内容。 再次感谢。
编辑:我可能也应该认为变换是标准的;通过改变视图矩阵来旋转和移动摄像机,通过改变模型矩阵来旋转对象。
答案 0 :(得分:1)
在定向照明模型中,所有光线都以单一,均匀的方向拍摄,换句话说,它们都是相互平行的。但是,在函数getDirectionalLighting()中,lightVector依赖于modelViewVertex,而这似乎是导致问题的原因。 尝试将其更改为
vec3 lightVector = normalize(u_VectorToLight);
或只是
vec3 lightVector = normalize(-u_LightPos);