gpt4 book ai didi

c++ - 没有 OpenGL 的重复 OpenGL 正交投影行为

转载 作者:可可西里 更新时间:2023-11-01 16:22:35 25 4
gpt4 key购买 nike

我在尝试在没有 OpenGL 的环境中复制 OpenGL 行为时遇到问题。

基本上,我需要根据我的程序创建的行列表创建一个 SVG 文件。这些线是使用正交投影创建的。

我确信这些线的计算是正确的,因为如果我尝试将它们与具有正交投影的 OpenGL 上下文一起使用并将结果保存到图像中,则图像是正确的。

当我在没有 OpenGL 的情况下使用完全相同的线条时,问题出现了。

我已经复制了 OpenGL 投影和 View 矩阵,并且我像这样处理每个线点:

3D_output_point = projection_matrix * view_matrix * 3D_input_point

然后我像这样计算它的屏幕(SVG 文件)位置:

2D_point_x = (windowWidth / 2) * 3D_point_x + (windowWidth / 2)
2D_point_y = (windowHeight / 2) * 3D_point_y + (windowHeight / 2)

我这样计算正交投影矩阵:

float range = 700.0f;
float l, t, r, b, n, f;

l = -range;
r = range;
b = -range;
t = range;
n = -6000;
f = 8000;

matProj.SetValore(0, 0, 2.0f / (r - l));
matProj.SetValore(0, 1, 0.0f);
matProj.SetValore(0, 2, 0.0f);
matProj.SetValore(0, 3, 0.0f);

matProj.SetValore(1, 0, 0.0f);
matProj.SetValore(1, 1, 2.0f / (t - b));
matProj.SetValore(1, 2, 0.0f);
matProj.SetValore(1, 3, 0.0f);

matProj.SetValore(2, 0, 0.0f);
matProj.SetValore(2, 1, 0.0f);
matProj.SetValore(2, 2, (-1.0f) / (f - n));
matProj.SetValore(2, 3, 0.0f);

matProj.SetValore(3, 0, -(r + l) / (r - l));
matProj.SetValore(3, 1, -(t + b) / (t - b));
matProj.SetValore(3, 2, -n / (f - n));
matProj.SetValore(3, 3, 1.0f);

这样的 View 矩阵:

CVettore position, lookAt, up;

position.AssegnaCoordinate(rtRay->m_pCam->Vp.x, rtRay->m_pCam->Vp.y, rtRay->m_pCam->Vp.z);
lookAt.AssegnaCoordinate(rtRay->m_pCam->Lp.x, rtRay->m_pCam->Lp.y, rtRay->m_pCam->Lp.z);
up.AssegnaCoordinate(rtRay->m_pCam->Up.x, rtRay->m_pCam->Up.y, rtRay->m_pCam->Up.z);

up[0] = -up[0];
up[1] = -up[1];
up[2] = -up[2];

CVettore zAxis, xAxis, yAxis;
float length, result1, result2, result3;

// zAxis = normal(lookAt - position)
zAxis[0] = lookAt[0] - position[0];
zAxis[1] = lookAt[1] - position[1];
zAxis[2] = lookAt[2] - position[2];
length = sqrt((zAxis[0] * zAxis[0]) + (zAxis[1] * zAxis[1]) + (zAxis[2] * zAxis[2]));
zAxis[0] = zAxis[0] / length;
zAxis[1] = zAxis[1] / length;
zAxis[2] = zAxis[2] / length;

// xAxis = normal(cross(up, zAxis))
xAxis[0] = (up[1] * zAxis[2]) - (up[2] * zAxis[1]);
xAxis[1] = (up[2] * zAxis[0]) - (up[0] * zAxis[2]);
xAxis[2] = (up[0] * zAxis[1]) - (up[1] * zAxis[0]);
length = sqrt((xAxis[0] * xAxis[0]) + (xAxis[1] * xAxis[1]) + (xAxis[2] * xAxis[2]));
xAxis[0] = xAxis[0] / length;
xAxis[1] = xAxis[1] / length;
xAxis[2] = xAxis[2] / length;

// yAxis = cross(zAxis, xAxis)
yAxis[0] = (zAxis[1] * xAxis[2]) - (zAxis[2] * xAxis[1]);
yAxis[1] = (zAxis[2] * xAxis[0]) - (zAxis[0] * xAxis[2]);
yAxis[2] = (zAxis[0] * xAxis[1]) - (zAxis[1] * xAxis[0]);

// -dot(xAxis, position)
result1 = ((xAxis[0] * position[0]) + (xAxis[1] * position[1]) + (xAxis[2] * position[2])) * -1.0f;

// -dot(yaxis, eye)
result2 = ((yAxis[0] * position[0]) + (yAxis[1] * position[1]) + (yAxis[2] * position[2])) * -1.0f;

// -dot(zaxis, eye)
result3 = ((zAxis[0] * position[0]) + (zAxis[1] * position[1]) + (zAxis[2] * position[2])) * -1.0f;

// Set the computed values in the view matrix.
matView.SetValore(0, 0, xAxis[0]);
matView.SetValore(0, 1, yAxis[0]);
matView.SetValore(0, 2, zAxis[0]);
matView.SetValore(0, 3, 0.0f);

matView.SetValore(1, 0, xAxis[1]);
matView.SetValore(1, 1, yAxis[1]);
matView.SetValore(1, 2, zAxis[1]);
matView.SetValore(1, 3, 0.0f);

matView.SetValore(2, 0, xAxis[2]);
matView.SetValore(2, 1, yAxis[2]);
matView.SetValore(2, 2, zAxis[2]);
matView.SetValore(2, 3, 0.0f);

matView.SetValore(3, 0, result1);
matView.SetValore(3, 1, result2);
matView.SetValore(3, 2, result3);
matView.SetValore(3, 3, 1.0f);

我从 OpenGL 和 SVG 输出中得到的结果大不相同,但在两天内我想不出解决方案。

这是 OpenGL 输出 enter image description here

这是我的 SVG 输出 enter image description here

如您所见,它的旋转不正确。

知道为什么吗?希望线点相同,矩阵也相同。


传递我创建的矩阵没有用。我的意思是,我认为矩阵是错误的,因为 OpenGL 没有显示任何东西。所以我尝试做相反的事情,我在 OpenGL 中创建了矩阵并将它们与我的代码一起使用。结果更好,但还不够完美。

现在我认为我在将 3D 点映射到 2D 屏幕点时做错了,因为我得到的点在 Y 轴上反转了,而且我仍然有一些线不完全匹配。

这是我使用 OpenGL 矩阵和我之前将 3D 点映射到 2D 屏幕空间的方法(这是 SVG,而不是 OpenGL 渲染)得到的结果:

enter image description here


好的,这是我从 OpenGL 获得的 View 矩阵的内容:

enter image description here

这是我从 OpenGL 得到的投影矩阵:

enter image description here

这是我用这些矩阵得到的结果,通过改变我的 2D 点 Y 坐标计算,就像 bofjas 说的:

enter image description here

似乎缺少一些旋转。我的相机在 X 轴和 Y 轴上都旋转了 30°,看起来它们计算不正确。

现在我正在使用与 OpenGL 相同的矩阵。所以我认为我在将 3D 点映射到 2D 屏幕坐标时进行了一些错误的计算。

最佳答案

与其调试自己的代码,不如使用 transform feedback使用 OpenGL 管道计算线条的投影。与其在屏幕上光栅化它们,不如将它们捕获到内存缓冲区中,然后直接保存到 SVG 中。设置这个有点复杂,取决于 OpenGL 代码路径的确切设置,但这可能是一个更简单的解决方案。

根据您自己的代码,您似乎在某处混合了 x 和 y 坐标,或者混合了行主矩阵和列主矩阵。

关于c++ - 没有 OpenGL 的重复 OpenGL 正交投影行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26220030/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com