ale*_*nov 3 java opengl shader android glsl
当计算移动物体的漫射照明时,我必须将光源与物体本身一起移动:
@Override
public void draw() { // draw frame
...
// Move object
GLES20.glVertexAttribPointer(aPositionLink, 3, GLES30.GL_FLOAT,
false, 0, object3D.getVertices());
// The luminous source moves nearby the object, so the
// object is always illuminated from one side
GLES20.glUniform3f(lightPositionLink, object3D.getX(),
object3D.getY(), object3D.getZ() + 2.0f);
...
}
Run Code Online (Sandbox Code Playgroud)
顶点着色器的片段:
#version 300 es
uniform mat4 u_mvMatrix; // model-view matrix of object
uniform vec3 u_lightPosition; // position of the luminous source
in vec4 a_position; // vertex data is loaded here
in vec3 a_normal; // normal data is loaded here
struct DiffuseLight {
vec3 color;
float intensity;
};
uniform DiffuseLight u_diffuseLight;
...
void main() {
...
vec3 modelViewNormal = vec3(u_mvMatrix * vec4(a_normal, 0.0));
vec3 modelViewVertex = vec3(u_mvMatrix * a_position);
// calculate the light vector by subtracting the
// position of the object from the light position
vec3 lightVector = normalize(u_lightPosition - modelViewVertex);
float diffuse = max(dot(modelViewNormal, lightVector), 0.1);
float distance = length(u_lightPosition - modelViewVertex);
diffuse = diffuse * (1.0 / (1.0 + pow(distance, 2.0)));
// calculate the final color for diffuse lighting
lowp vec3 diffuseColor = diffuse * u_diffuseLight.color * u_diffuseLight.intensity;
v_commonLight = vec4((ambientColor + diffuseColor), 1.0);
...
}
Run Code Online (Sandbox Code Playgroud)
这是正确的方法吗?或者是否有另一种合理的选择,使用固定发光源,以免花费资源来计算每帧发光源的位置?注意:增加距离没有帮助。提前致谢。
解决方案:
根据 Rabbid76 的建议,我按照此处所述应用了定向光。
我必须将光源与物体本身一起移动
为什么光源会随着物体移动?
如果光是世界中的点光源,并且对象移动,则对象的照明会发生变化(在“真实”世界中)。
在您的情况下,照明是在视图空间中计算的。如果光源是世界上的一个点,那么你必须通过视图矩阵来变换位置(视图矩阵从世界空间变换到视图空间)。例如:
uniform mat4 u_viewMatrix;
void main()
{
// [...]
vec3 lightPosView = vec3(u_viewMatrix * vec4(u_lightPosition.xyz, 1.0));
vec3 lightVector = normalize(u_lightPosition - modelViewVertex);
// [...]
}
Run Code Online (Sandbox Code Playgroud)
无论如何,如果对象移动并且光源以某种方式锚定到该对象,则您必须将应用于对象顶点的变换也应用于光源。
在这种情况下,u_lightPosition必须是对象模型空间中的位置,这意味着它是相对于对象 ( u_lightModelPosition) 的。然后你可以这样做:
uniform vec3 u_lightModelPosition;
void main()
{
mat3 normalMat = inverse(transpose(mat3(u_mvMatrix)));
vec3 modelViewNormal = normalMat * a_normal;
vec3 modelViewVertex = vec3(u_mvMatrix * a_position);
vec3 modelViewLight = vec3(u_mvMatrix * vec4(u_lightModelPosition, 1.0));
vec3 lightVector = normalize(modelViewLight - modelViewVertex);
// [...]
}
Run Code Online (Sandbox Code Playgroud)
如果你想要一盏灯,那与位置无关,你必须使用定向灯。在这种情况下,光源就不是世界上的一个点,而只是一个方向。例如:
vec3 lightVector = -u_lightRayDirection;
Run Code Online (Sandbox Code Playgroud)
u_lightRayDirection必须在光计算的空间中。由于照明是在视图空间中计算的,u_lightRayDirection因此也必须是视图空间中的方向。如果u_lightRayDirection是世界空间中的向量,那么它必须通过 进行变换mat3(u_viewMatrix)。
定向光没有距离(或恒定距离)。
如果光源锚定到相机,则根本不需要转换(因为您在视图空间中进行灯光计算)。