【发布时间】:2016-09-22 02:21:51
【问题描述】:
我在围绕 X 轴旋转相机时遇到了一些问题。我在相机的场景中放置了一张图像,当我用相机抬头时,我想在场景中保留一张图像。
首先我建立了一些函数来创建矩阵:
mat4 makeTranslation(float tx, float ty, float tz) {
return mat4(
1., 0., 0., 0.,
0., 1., 0., 0.,
0., 0., 1., 0.,
tx, ty, tz, 1.
);
}
mat4 makeXRotation(float angleInDegrees) {
float angleInRadians = angleInDegrees * M_PI / 180.;
float c = cos(angleInRadians);
float s = sin(angleInRadians);
return mat4(
1., 0., 0., 0.,
0., c, s, 0.,
0., -s, c, 0.,
0., 0., 0., 1.
);
}
mat4 makeZRotation(float angleInDegrees) {
float angleInRadians = angleInDegrees * M_PI / 180.;
float c = cos(angleInRadians);
float s = sin(angleInRadians);
return mat4(
c, s, 0., 0.,
-s, c, 0., 0.,
0., 0., 1., 0.,
0., 0., 0., 1.
);
}
// camera
mat4 myW2N(float ax, float ay, float zNear, float zFar) {
float cx = 1.0 / ax;
float cy = 1.0 / ay;
float z0 = -zNear;
float z1 = -zFar;
float az = (z0 + z1) / (z0 - z1);
float bz = (1. - az) * z0;
return mat4(
cx, 0., 0., 0.,
0., cy, 0., 0.,
0., 0., az, bz,
0., 0., -1., 0.
);
}
// transpose
mat3 rotationW2R() {
return mat3(
0., 0., 1.,
1., 0., 0.,
0., 1., 0.
);
}
不仅仅是在 Y 轴上平移相机位置
float ax = tan(hFOV * M_PI);
float ay = ax / aspectRatio;
mat4 res = makeTranslation(0., move_y, 0.) * myW2N(ax,ay,6.,2.);
但我不想平移相机位置,我想围绕轴旋转并将图像保留在场景中
这就是我正在尝试的方式:
float ax = tan(hFOV * M_PI);
float ay = ax / aspectRatio;
mat4 res = makeXRotation(pitch) * makeZRotation(roll) * makeTranslation(0., move_y, 0.) * myW2N(ax,ay,6.,2.);
但最后我的图像不会向上移动它会在两侧向上和向下扩展,而不仅仅是向上或向下扩展,并且要垂直扩展它我需要围绕 X 轴旋转相机,当我围绕 Y 旋转它时-axis 它水平扩展。
你对如何解决它没有任何建议吗?
【问题讨论】:
-
那么你想要什么广告牌?始终面向相机的四边形?
-
@WacławJasper 不确定你所说的广告牌是什么意思,但是是的,我想要一个始终面向相机的四边形,无论相机场景移动到哪里,我也想将我的图像移动到那里。
标签: opengl-es camera rotation webgl opengl-es-2.0