在 OpenGL ES 2.03.0 中,方向性照明不是恒定的。

问题描述 投票:0回答:1

问题:当对象的位置发生变化时,方向光的方向会发生变化。 当物体的位置发生变化时,方向光的方向会发生变化。

我看了有类似问题的帖子。

WorldSpace中的方向光依赖于viewMatrix。

OpenGL定向光着色器

移动物体的漫射照明

根据这些帖子,我试着应用这个方法。

#version 300 es
uniform mat4 u_mvMatrix;
uniform mat4 u_vMatrix;
in vec4 a_position;
in vec3 a_normal;
const vec3 lightDirection = vec3(-1.9, 0.0, -5.0);
...
void main() {
    vec3 modelViewNormal = vec3(u_mvMatrix * vec4(a_normal, 0.0));
    vec3 lightVector = lightDirection * mat3(u_vMatrix);
    float diffuseFactor = max(dot(modelViewNormal, -lightVector), 0.0);
    ...
}

但结果是这样的

enter image description here

我也试过了

vec3 modelViewVertex = vec3(u_mvMatrix * a_position);
vec3 lightVector = normalize(lightDirection - modelViewVertex);
float diffuseFactor = max(dot(modelViewNormal, lightVector), 0.0);

还有..:

vec3 lightVector = normalize(lightDirection - modelViewVertex);
lightVector = lightVector * mat3(u_vMatrix);

但结果是:

enter image description here

需要对代码做什么修改才能让所有对象都亮起来?

先谢谢了!

解决办法在实践中,创建定向照明对我来说并不是一件容易的事。在Rabbid76的建议下,我改变了乘法的顺序。在另一个Rabbid76的建议上(岗位),我还创建了一个自定义的视点。

Matrix.setLookAtM(pointViewMatrix, rmOffset:0, eyeX:3.8f, eyeY:0.0f, eyeZ:2.8f,
        centerX:0.0f, centerY:0f, centerZ:0f, upX:0f, upY:1.0f, upZ:0.0f)

还计算了眼睛的坐标和光照矢量,尽管相机设置在[0,0,0]。

#version 300 es
uniform mat4 u_mvMatrix;
uniform mat4 u_pointViewMatrix;
in vec4 a_position;
in vec3 a_normal;
const vec3 lightPosition = vec3(-5.0, 0.0, 1.0);
...
void main() {
    // transform normal orientation into eye space
    vec3 modelViewNormal = vec3(u_mvMatrix * vec4(a_normal, 0.0));
    vec3 modelViewVertex = vec3(u_mvMatrix * a_position); // eye coordinates
    vec3 lightVector = normalize(lightPosition - modelViewVertex);
    lightVector = mat3(u_pointViewMatrix) * lightVector;
    float diffuseFactor = max(dot(modelViewNormal, lightVector), 0.0);
    ...
}

只有在这些步骤之后,图片才变得很好。

enter image description here

小的差异可能是由于大的视角造成的。

android shader opengl-es-2.0 opengl-es-3.0
1个回答
1
投票

矢量要从右边乘到矩阵。请看 GLSL编程矢量和矩阵操作.

vec3 lightVector = lightDirection * mat3(u_vMatrix);

vec3 lightVector = mat3(u_vMatrix) * lightDirection;

如果你想在视图空间中进行光照计算,那么法线矢量必须通过模型视图矩阵将对象(模型)空间转换为视图空间,而光照方向hss必须通过视图矩阵将世界空间转换为视图空间。例如

void main() {
    vec3  modelViewNormal = mat3(u_mvMatrix) * a_normal;
    vec3  lightVector     = mat3(u_vMatrix) * lightDirection;
    float diffuseFactor   = max(dot(modelViewNormal, -lightVector), 0.0);

    // [...]
} 
© www.soinside.com 2019 - 2024. All rights reserved.