Opengl基于输入的查看矩阵

Opengl Viewing matrix based on inputs

本文关键字:输入 于输入 Opengl      更新时间:2024-04-28

通过用户输入,我得到了相机位置、相机瞄准点和向上矢量。用户为每个x y z输入3个二重。现在我正试图找到z向量和y向量,然后我可以对它们进行归一化。我只是在努力弄清楚如何找到这些值,我不完全确定用户输入在坐标方面的实际含义。我将每个输入存储为Vector3d

我也需要一个解决方案,我最终使用了一个名为gl-matrix的javascript库,该库带有targetTo辅助函数。

以下是它的源代码:http://glmatrix.net/docs/mat4.js.html#line1708

这是我尝试将其转换为C。
免责声明:我没有编译这个,所以如果你发现语法错误,请在评论中提及。

#include <math.h>
float *targetTo(float *out, float *eye, float *target, float *up) {
float eyex = eye[0],
eyey = eye[1],
eyez = eye[2],
upx = up[0],
upy = up[1],
upz = up[2];
float z0 = eyex - target[0],
z1 = eyey - target[1],
z2 = eyez - target[2];
float len = z0 * z0 + z1 * z1 + z2 * z2;
if (len > 0) {
len = 1 / sqrt(len);
z0 *= len;
z1 *= len;
z2 *= len;
}
float x0 = upy * z2 - upz * z1,
x1 = upz * z0 - upx * z2,
x2 = upx * z1 - upy * z0;
float = x0 * x0 + x1 * x1 + x2 * x2;
if (len > 0) {
len = 1 / sqrt(len);
x0 *= len;
x1 *= len;
x2 *= len;
}
out[0] = x0;
out[1] = x1;
out[2] = x2;
out[3] = 0;
out[4] = z1 * x2 - z2 * x1;
out[5] = z2 * x0 - z0 * x2;
out[6] = z0 * x1 - z1 * x0;
out[7] = 0;
out[8] = z0;
out[9] = z1;
out[10] = z2;
out[11] = 0;
out[12] = eyex;
out[13] = eyey;
out[14] = eyez;
out[15] = 1;
return out;
}

注意:您可能希望使用double而不是float
或者如果您使用的是std::vector<int>,它也可以用作参数。