- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经为此苦苦挣扎了一段时间了。我正在尝试使用 devKitPro 确定 NDS 屏幕上模型中顶点的屏幕坐标。该库似乎实现了 OpenGL 的一些功能,但特别是缺少 gluProject 函数,这(我认为)可以让我轻松地做到这一点。
我已经尝试使用存储在 DS 寄存器中的投影矩阵手动计算屏幕坐标有一段时间了,但我一直没有太多运气,即使在尝试从以下位置构建投影矩阵时也是如此:基于 OpenGL 文档的暂存。这是我尝试使用的代码:
void get2DPoint(v16 x, v16 y, v16 z, float &result_x, float &result_y)
{
//Wait for the graphics engine to be ready
/*while (*(int*)(0x04000600) & BIT(27))
continue;*/
//Read in the matrix that we're currently transforming with
double currentMatrix[4][4]; int i;
for (i = 0; i < 16; i++)
currentMatrix[0][i] =
(double(((int*)0x04000640)[i]))/(double(1<<12));
//Now this hurts-- take that matrix, and multiply it by the projection matrix, so we obtain
//proper screen coordinates.
double f = 1.0 / tan(70.0/2.0);
double aspect = 256.0/192.0;
double zNear = 0.1;
double zFar = 40.0;
double projectionMatrix[4][4] =
{
{ (f/aspect), 0.0, 0.0, 0.0 },
{ 0.0, f, 0.0, 0.0 },
{ 0.0, 0.0, ((zFar + zNear) / (zNear - zFar)), ((2*zFar*zNear)/(zNear - zFar)) },
{ 0.0, 0.0, -1.0, 0.0 },
};
double finalMatrix[4][4];
//Ugh...
int mx = 0; int my = 0;
for (my = 0; my < 4; my++)
for (mx = 0; mx < 4; mx++)
finalMatrix[mx][my] =
currentMatrix[my][0] * projectionMatrix[0][mx] +
currentMatrix[my][1] * projectionMatrix[1][mx] +
currentMatrix[my][2] * projectionMatrix[2][mx] +
currentMatrix[my][3] * projectionMatrix[3][mx] ;
double dx = ((double)x) / (double(1<<12));
double dy = ((double)y) / (double(1<<12));
double dz = ((double)z) / (double(1<<12));
result_x = dx*finalMatrix[0][0] + dy*finalMatrix[0][1] + dz*finalMatrix[0][2] + finalMatrix[0][3];
result_y = dx*finalMatrix[1][0] + dy*finalMatrix[1][1] + dz*finalMatrix[1][2] + finalMatrix[1][3];
result_x = ((result_x*1.0) + 4.0)*32.0;
result_y = ((result_y*1.0) + 4.0)*32.0;
printf("Result: %f, %f\n", result_x, result_y);
}
涉及很多转换,DS 在内部使用定点表示法工作,我需要将其转换为 double 才能使用。我得到的似乎有些正确——如果我使用面向屏幕的平面四边形,像素会完美转换,但旋转很不稳定。另外,由于我要使用投影矩阵(它占屏幕宽度/高度?),所以我需要使用的最后步骤似乎根本不正确。难道投影矩阵不应该为我完成屏幕分辨率的提升吗?
我对这一切都很陌生,我对矩阵数学有一定的掌握,但我在 3D 图形方面的熟练程度不如我想要的。这里有谁知道一种方法,给定模型顶点的 3D、非变换坐标,并给出将应用于它的矩阵,以实际得出屏幕坐标,而不使用 OpenGL 的 gluProject 函数?你能看到我的代码中明显缺少的东西吗? (如果可能的话,我会澄清,我知道这很粗糙,这是我正在开发的原型(prototype),清洁度不是高优先级)
非常感谢!
PS:据我了解,我从 DS 寄存器中提取的 currentMatrix应该 为我提供组合的投影、平移和旋转矩阵,因为它应该是正在运行的精确矩阵用于由 DS 自己的硬件进行翻译,至少根据 GBATEK 的规范。实际上,它似乎实际上并没有应用投影坐标,我认为这与我的问题有关。但我不确定,因为我自己计算投影不会产生不同的结果。
最佳答案
这几乎是正确的。
正确的步骤是:
将模型 View 与投影矩阵相乘(就像您已经做过的那样)。
通过添加值为 1 的 W 分量,将 3D 顶点扩展到齐次坐标。例如,您的 (x,y,z) 向量变为 (x,y,z,w),其中 w = 1。
将此向量与矩阵乘积相乘。你的矩阵应该是 4x4,你的向量的大小应该是 4。结果也将是一个大小为 4 的向量(不要删除 w!)。此乘法的结果是剪辑空间中的向量。仅供引用:您已经可以使用此向量执行一些非常有用的操作:测试该点是否在屏幕上。这六个条件是:
x < -w : Point is outside the screen (left of the viewport) x > W : Point is outside the screen (right of the viewport) y < -w : Point is outside the screen (above the viewport) y > w : Point is outside the screen (below the viewport) z < -w : Point is outside the screen (beyond znear) z > w : Point is outside the screen (beyond zfar)
x' = x / w; y' = y / w;
z' = z / w
请注意,如果 w 为零,则上一步将不起作用。如果您的点等于相机位置,就会发生这种情况。在这种情况下,您最好将 x' 和 y' 设置为零。 (将在下一步中将该点移动到屏幕中心..)。
最后一步:获取 OpenGL 视口(viewport)坐标并应用它:
x_screen = viewport_left + (x' + 1) * viewport_width * 0.5; y_screen = viewport_top + (y' + 1) * viewport_height * 0.5;
仅此而已。
关于opengl - NDS 上的 gluProject?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1978963/
我正在尝试在我的游戏中使用 gluProject。我找到了一种使用它的方法,但我不知道如何处理旋转。 这是我编写的代码: public static Vector2 getScreenCoordina
我想使用 gluProject函数,在“渲染”后获取二维窗口中的点坐标。问题是,我得到了奇怪的结果。例如:我有一个 x=16.5 的点。当我使用 gluProject在它上面我得到 x= -6200.
gluProject 函数 (OpenGL) 有问题。我想将一个简单的 3D 点从对象空间转换到屏幕空间。我的代码如下: int main(){ GLdouble mvmatrix[16];
OpenGL documentation对于 gluProject 写道,点 v 的投影是使用给定的投影和模型 View 矩阵通过 v'' = P * M * v 然后使用当前视口(viewport)
我已经为此苦苦挣扎了一段时间了。我正在尝试使用 devKitPro 确定 NDS 屏幕上模型中顶点的屏幕坐标。该库似乎实现了 OpenGL 的一些功能,但特别是缺少 gluProject 函数,这(我
我需要显示一个占屏幕宽度 100% 的正方形多边形,然后,我想我必须缩放它(使用 Z 轴)直到多边形边界触及屏幕边界。 我正在尝试使用 gluProject 将 3D 坐标投影到 2D 屏幕坐标中来实
我想在使用 gluProject() 从 3D 点计算出的 2D 点显示 2D 图像。所以我有我的 3D 点,我使用 gluProject 获取它的 2D 坐标,然后我在这个点上显示我的图像。它运行良
我正在寻找 javascript 中 gluProject 的等效项,以便与 WebGL 一起使用。有谁知道开源实现在哪里或者如何用 javascript 实现它? 我正在使用这里的 glMatrix
我正在尝试从 opengl 坐标(投影在 3D 空间中)找到屏幕坐标,为此我使用了 glproject 调用,我在代码中使用了旋转和平移 在执行一些转换后的某个时刻,我调用 glproject api
我不知道为什么 wiki of opengl.org在标题中说“gluProject”,但在代码中是“glhProject”。有什么区别吗? 最佳答案 它使用 glh 因为编写该 Wiki 页面的人编
所以我使用 gluProject 获取 3D 坐标并告诉我它们在屏幕上渲染的位置。问题是我没有得到与 gluProject 相同的坐标以及它们实际最终渲染的位置。奇怪的是,如果相机的 x、y 和 z
我正在尝试将 3D 坐标转换为 2D 坐标。我知道我需要使用 gluProject()。但是我在设置它时遇到了麻烦。我需要使用像素坐标 我在 InitGL() 方面需要帮助,我该如何正确初始化它。还有
经过两个小时的谷歌搜索( here 、 here 、 here 、 here 和 here 以及一大堆我懒得去找的其他东西),我以为我终于学会了将 3D 坐标转换为 2D 坐标的理论。但它不起作用。这
我是一名优秀的程序员,十分优秀!