在OpenGL ES 2.0/3.0中,定向照明不是恒定的 [英] Directional lighting is not constant in OpenGL ES 2.0/3.0

查看:74
本文介绍了在OpenGL ES 2.0/3.0中,定向照明不是恒定的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:当对象的位置改变时,定向光的方向也会改变.

我看过有类似问题的帖子:

也尝试过

  vec3 modelViewVertex = vec3(u_mvMatrix * a_position);vec3 lightVector = normalize(lightDirection-modelViewVertex);浮点法漫射因子= max(dot(modelViewNormal,lightVector),0.0); 

并且:

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

但是结果:

需要对代码进行哪些更改,以便所有对象均被照亮?

提前谢谢!

解决方案:在实践中,创建定向照明对我来说并不是一件容易的事.根据Rabbid76的建议,我更改了乘法顺序.关于另一个Rabbid76的建议(

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

解决方案

向量必须从右边乘以矩阵.参见 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;浮点法漫射因子= max(dot(modelViewNormal,-lightVector),0.0);//[...]} 

Problem: The direction of the directional light changes when the position of the object changes.

I watched posts with a similar problem:

Directional light in worldSpace is dependent on viewMatrix

OpenGL directional light shader

Diffuse lighting for a moving object

Based on these posts, I tried to apply this:

#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);
    ...
}

But the result looks like this:

Tried also:

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

And:

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

But the result:

What changes need to be made to the code so that all objects are lit identically?

Thanks in advance!

Solution: In practice, creating directional lighting was not such an easy task for me. On Rabbid76 advice, I changed the order of multiplication. On another Rabbid76 advice (post), I also created a custom point of view:

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)

Also calculated eye coordinates and light vector, although the camera is set in [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);
    ...
}

Only after these steps did the picture become good:

Small differences are probably caused by a big perspective.

解决方案

The vector has to be multiplied to the matrix from the right. See GLSL Programming/Vector and Matrix Operations.

vec3 lightVector = lightDirection * mat3(u_vMatrix);

vec3 lightVector = mat3(u_vMatrix) * lightDirection;

If you want to dot the light calculations in view space, then the normal vecotr has to be transformed form object (model) space to view space by the model view matrix and the light direction hss to be transformed form world space to view space, by the view matrix. For instance:

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

    // [...]
} 

这篇关于在OpenGL ES 2.0/3.0中,定向照明不是恒定的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆