在 OpenGL ES 中获取眼睛位置坐标
Get eye position coordinates in OpenGL ES
3D 应用程序有静态相机:
float eyeX = 0.0f;
float eyeY = 0.0f;
float eyeZ = 0.0f;
Matrix.setLookAtM(viewMatrix, 0, eyeX, eyeY, eyeZ,
0f, 0f, -4f, 0f, 1.0f, 0.0f)
那么这个向量在着色器中用于眼睛坐标?:
const vec4 eyePos = vec4(0.0, 0.0, 0.0, 0.0);
或者需要额外的转换?
注意: 阅读 post What exactly are eye space coordinates? 但我仍然有疑问,因为我的雾着色器无法正常工作。在这个着色器中,观察者到物体的距离是这样计算的:
uniform mat4 u_vMatrix;
in vec4 a_position;
out float v_eyeDist;
const vec4 eyePos = vec4(0.0, 0.0, 0.0, 0.0);
...
void main() {
...
vec4 viewPos = u_vMatrix * a_position;
v_eyeDist = sqrt(pow((viewPos.x - eyePos.x), 2.0) +
pow((viewPos.y - eyePos.y), 2.0) +
pow((viewPos.z - eyePos.z), 2.0));
...
}
提前致谢!
解决方案:根据 Rabbid76 的建议,我使用了 length() 函数,以及模型视图矩阵。
uniform mat4 u_mvMatrix; // model-view matrix
in vec4 a_position;
out float v_eyeDist;
...
void main() {
...
vec4 viewPos = u_mvMatrix * a_position;
v_eyeDist = length(viewPos);
...
}
视图矩阵从世界 space 转换为视图 space。视图 space 是局部系统,由场景的视角定义。视图的位置,视线和视图的向上方向,定义一个相对于世界坐标系的坐标系。
视图 space 的原点是 "eye" 位置,因此在视图 space 中, "eye" 位置位于 (0, 0, 0)。
在 glsl 中,到点的距离可以通过内置函数计算 distance
. It is sufficient to compute the Euclidean distance of the componets x
, y
, z
(Cartesian coordinates), since the w
components (Homogeneous coordinates) 对于两个向量都是 1。例如:
v_eyeDist = distance(viewPos.xyz, eyePos.xyz);
由于视角(相机的位置)在viespace中是(0, 0, 0),所以计算视角向量的长度就足够了,计算距离.一点到坐标系原点的距离就是向量到该点的长度。在 glsl 中,这可以通过内置函数 length
计算得出。在这种情况下,计算分量 x
、y
、z
的长度并排除 w
分量很重要。包含 w
组件会导致错误的结果:
v_eyeDist = length(viewPos.xyz);
3D 应用程序有静态相机:
float eyeX = 0.0f;
float eyeY = 0.0f;
float eyeZ = 0.0f;
Matrix.setLookAtM(viewMatrix, 0, eyeX, eyeY, eyeZ,
0f, 0f, -4f, 0f, 1.0f, 0.0f)
那么这个向量在着色器中用于眼睛坐标?:
const vec4 eyePos = vec4(0.0, 0.0, 0.0, 0.0);
或者需要额外的转换?
注意: 阅读 post What exactly are eye space coordinates? 但我仍然有疑问,因为我的雾着色器无法正常工作。在这个着色器中,观察者到物体的距离是这样计算的:
uniform mat4 u_vMatrix;
in vec4 a_position;
out float v_eyeDist;
const vec4 eyePos = vec4(0.0, 0.0, 0.0, 0.0);
...
void main() {
...
vec4 viewPos = u_vMatrix * a_position;
v_eyeDist = sqrt(pow((viewPos.x - eyePos.x), 2.0) +
pow((viewPos.y - eyePos.y), 2.0) +
pow((viewPos.z - eyePos.z), 2.0));
...
}
提前致谢!
解决方案:根据 Rabbid76 的建议,我使用了 length() 函数,以及模型视图矩阵。
uniform mat4 u_mvMatrix; // model-view matrix
in vec4 a_position;
out float v_eyeDist;
...
void main() {
...
vec4 viewPos = u_mvMatrix * a_position;
v_eyeDist = length(viewPos);
...
}
视图矩阵从世界 space 转换为视图 space。视图 space 是局部系统,由场景的视角定义。视图的位置,视线和视图的向上方向,定义一个相对于世界坐标系的坐标系。
视图 space 的原点是 "eye" 位置,因此在视图 space 中, "eye" 位置位于 (0, 0, 0)。
在 glsl 中,到点的距离可以通过内置函数计算 distance
. It is sufficient to compute the Euclidean distance of the componets x
, y
, z
(Cartesian coordinates), since the w
components (Homogeneous coordinates) 对于两个向量都是 1。例如:
v_eyeDist = distance(viewPos.xyz, eyePos.xyz);
由于视角(相机的位置)在viespace中是(0, 0, 0),所以计算视角向量的长度就足够了,计算距离.一点到坐标系原点的距离就是向量到该点的长度。在 glsl 中,这可以通过内置函数 length
计算得出。在这种情况下,计算分量 x
、y
、z
的长度并排除 w
分量很重要。包含 w
组件会导致错误的结果:
v_eyeDist = length(viewPos.xyz);