我有一个显示解析的 OBJ 模型的基本模型查看器。我在相机所在的位置有一盏灯,因此正在查看的模型在一侧完全亮起。出于某种原因,我的转换没有按照我在渲染器中的预期进行。当我将模型绕 x 轴或 y 轴旋转 90 度时,网格完全变暗。但是再旋转 90 度,它又完全亮了起来。
我做错了吗?还是我的法线一开始就错了?
我计算并将适当的变换应用于我的应用程序中的法线(转置 ModelView 矩阵的逆矩阵)
Matrix.invertM(mNormalMatrix, 0, mMVMatrix, 0);
Matrix.transposeM(mNormalMatrix, 0, mNormalMatrix, 0);
在将它传递到我的着色器之前:
/*Vertex shader*/
attribute vec3 vPosition;
attribute vec3 vNormal;
uniform mat4 modelViewMatrix;
uniform mat4 mMVPMatrix;
uniform mat4 mViewMatrix;
uniform mat4 normalMatrix;
uniform float lightingEnabled;
varying float lightsEnabled;
varying vec3 lightPosEye;
varying vec3 normalEye;
varying vec3 vertEye;
void main() {
/*Calculate normal matrix*/
vec4 normal = vec4(vNormal, 0.0);
normalEye = normalize(vec3(normalMatrix * normal));
lightsEnabled = lightingEnabled;
lightPosEye = vec3(mViewMatrix * vec4(0.0, 0.0, 3.0, 1.0));
vertEye = vec3(modelViewMatrix * vec4(vPosition, 1.0));
gl_Position = mMVPMatrix * vec4(vPosition, 1.0);
}
/*Fragment shader*/
precision mediump float;
/*uniform vec4 vColor; */
varying float lightsEnabled;
varying vec3 lightPosEye;
varying vec3 normalEye;
varying vec3 vertEye;
void main() {
/*Light output components*/
vec3 Ia;
vec3 Id;
/*light source components*/
vec3 La = vec3(0.5);
vec3 Ld = vec3(1.0);
/*vec3 Ls = vec3(1.0);*/
vec3 Ka = vec3(0.3); /*ambient reflectance term*/
vec3 Kd = vec3(1.0); /*diffuse term*/
/*ambient light term*/
Ia = La * Ka;
float dotProd;
vec3 lightToSurface;
if(lightsEnabled > 0.5){
/*diffuse light term*/
lightToSurface = normalize(lightPosEye - vertEye);
dotProd = dot(lightToSurface, normalEye);
dotProd = max(dotProd, 0.0);
}
else {
dotProd = 1.0;
}
Id = Ld * Kd * dotProd;
gl_FragColor = vec4(Ia + Id, 1.0);
}