简体   繁体   English

将3D变换转换为2D

[英]Converting 3D transformation to 2D

I am trying to figure out a way of converting a 3D transformation matrix to 2D in OpenGL. 我试图想出一种在OpenGL中将3D变换矩阵转换为2D的方法。 My z-coordinates are ignored (I am working with ground projections). 我的z坐标被忽略(我正在使用地面投影)。

Here is an example of what I am trying to achieve: 这是我想要实现的一个例子:

// Given a transformation matrix
// | r00 r01 r01 t0 |
// | r10 r11 r11 t1 |
// | r20 r21 r21 t2 |
// | 0.  0.  0.  1. |
//
// Ignore the z-coordinate, and convert it to
// | r00 r01 t0 |
// | r10 r11 t1 |
// | 0.  0.  1. |
glm::dmat4 t_matrix_3D = GetTransformation();
glm::dmat3 t_matrix_2D = glm::dmat3(t_matrix_3D);
t_matrix_2D[2] = dvec3(dvec2(t_matrix_3D[3]), 1.);
t_matrix_2D[0][2] = 0.;
t_matrix_2D[1][2] = 0.;

The question is if there is a prettier way of doing it? 问题是,是否有更漂亮的方法呢? Maybe using matrix slicing, or sub matrix extraction? 也许使用矩阵切片或子矩阵提取?

The most comprehensible and probably the best performing way is to write a inline method or function and to use directly the constructor of glm::mat3 : 最易理解,可能性能最好的方法是编写内联方法或函数,并直接使用glm::mat3的构造glm::mat3

glm::mat3 Mat3Dto2D( const glm::mat4 & m )
{
    return glm::mat3(
        m[0][0], m[0][1], 0.0f,
        m[1][0], m[1][1], 0.0f,
        m[3][0], m[3][1], 1.0f
    );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM