如何将3D坐标转换为2D屏幕空间

时间:2015-10-19 09:45:22

标签: c++ math 3d 2d

我知道stackoverflow已经有一些关于这个主题的问题和答案,但我无法针对我的具体问题处理它们。

这是我的代码:

int main()
{

std::vector <int> v;
double x_screen, y_screen;
double screen_width = 640.0;
double screen_height = 480.0;
GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);
double half_screen_width = screen_width / 2;
double half_screen_height = screen_height / 2;
double window_aspect = screen_width / screen_height;
double x_3D, y_3D, z_3D;


for (int i = 0; i < 3; i++){
    v.push_back(i);
    ++v[i];
}

std::cout << "3D Point:" << std::endl;
for (int j = 0; j < v.size(); j++)
    std::cout << v[j] << std::endl;

x_3D = v[0] - viewport[0];
y_3D = v[1] - viewport[1];
z_3D = v[2] - viewport[2];


x_screen = (+(x_3D / z_3D)+half_screen_width)*half_screen_width;
y_screen = (-(y_3D / z_3D)+half_screen_height)*half_screen_height;

if (window_aspect > 1.0)
    x_screen = x_screen / window_aspect;
else
    y_screen = y_screen*window_aspect;

std::cout << "2D Point :" << std::endl;

std::cout << "[" << x_screen << "," << y_screen << "]" << std::endl;


getchar();
getchar();
return 0;
}

输出: 3D点:[1,2,3] 2D点:[77040,57360]

我得到数学背景的教程在这里: http://www.flipcode.com/archives/Plotting_A_3D_Point_On_A_2D_Screen.shtml

有人能告诉我这个结果是否符合逻辑?我是这个主题的新手,无法解释结果。

0 个答案:

没有答案