【问题标题】:3d projection to 2d screen coordinates3d 投影到 2d 屏幕坐标
【发布时间】:2014-05-16 21:07:43
【问题描述】:

当前代码:

//calculating View Project matrix
abfw::Matrix44 view_proj_matrix_ = camera_.GetMatrix(PROJ) * camera_.GetMatrix(VIEW);

//3D position
abfw::Vector3 ball_position_ =  balls_[look_at_index_].GetPosition();
abfw::Vector3 sprite_position;

    //transform world to screen ratio in rang -1 to 1
    ball_position_ = ball_position_.Transform(view_proj_matrix_);

    //move to range 0 - 2
    ball_position_.x += 1;

    //scale to range 0 -1
    ball_position_.x /= 2;

    //scale to screen size
    ball_position_.x *= platform_.width();

    // same as above
    ball_position_.y -= 1;
    ball_position_.y /= -2;
    ball_position_.y *= platform_.height();

    //set sprite position
    sprite_position = ball_position_;

    // z is discounted
    sprite_position.z = 0;

我得到的 x 和 y 值超出了屏幕的范围,但我不明白为什么。我已经尽可能地分解了​​算法,试图看看我哪里出错了,但我做不到。

编辑:当相机位于 (0,0,7) 时,在相同 y 值的球之间切换会给出相同的精灵位置。

GetMatrix() 使用此代码:

view_matrix_.LookAt(camera_eye_, camera_lookat_, camera_up_);
projection_matrix_ = platform_.PerspectiveProjectionFov(camera_fov_, (float)platform_.width() / (float)platform_.height(), near_plane_, far_plane_);

这也是我用来画画的,所以我假设它是正确的。

我相信使用行向量。

转换代码:

Vector3 result = Vector3(0.0f, 0.0f, 0.0f);

result.x = x*_mat.m[0][0]+y*_mat.m[1][0]+z*_mat.m[2][0]+_mat.m[3][0];
result.y = x*_mat.m[0][1]+y*_mat.m[1][1]+z*_mat.m[2][1]+_mat.m[3][1];
result.z = x*_mat.m[0][2]+y*_mat.m[1][2]+z*_mat.m[2][2]+_mat.m[3][2];

【问题讨论】:

  • 由于所有转换的实现都是隐藏的,所以很难说。你能发布camera_参数和camera_.GetMatrix和abfw::Vector3::Transform的实现吗?您使用的是行向量还是列向量(在执行转换时,您的矩阵是在向量之前还是之后相乘)?

标签: c++ 3d directx 2d screen


【解决方案1】:

我不知道命名空间abfw 代表什么,但如果它以与 DirectX 相同的方式处理向量和矩阵,则必须交换投影和视图转换:

abfw::Matrix44 view_proj_matrix_ = camera_.GetMatrix(VIEW) * camera_.GetMatrix(PROJ);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-15
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 2019-06-08
    • 1970-01-01
    • 2021-05-05
    相关资源
    最近更新 更多