c++ - 移植 OpenGL 矩阵运算以 boost QVM

标签 c++ opengl matrix boost

我的应用程序中有一些遗留代码使用 OpenGL 进行一些快速矩阵数学运算。这似乎可以用 boost::qvm 代替,但使用示例很少。基本代码是:

#include <boost/qvm/mat.hpp>
#include <boost/qvm/mat_operations.hpp>

void foo()
{
    auto heading = 90.0;
    auto speed = 10.0;
    auto xComponent = 0.0f;
    auto yComponent = 0.0f;

    glPushMatrix();
    {
        glLoadIdentity();

        glRotatef(static_cast<GLfloat>(heading), 0, 1, 0);
        glTranslatef(0, static_cast<GLfloat>(-speed), 0);

        float modelviewMatrix[16];
        glGetFloatv(GL_MODELVIEW_MATRIX, modelviewMatrix);

        xComponent = modelviewMatrix[2*4 + 0];
        yComponent = modelviewMatrix[0];
    }
    glPopMatrix();
}

所以我想知道是否有人有任何关于如何使用 boost::qvm 实现它的快速想法可以分享?

因此,boost::qvm 版本应该非常相似,但我不确定如何进行旋转和平移,因为 API 与 OpenGL 有很大不同。

void foo() 
{ 
    auto heading = 90.0;
    auto speed = 10.0;
    auto xComponent = 0.0f;
    auto yComponent = 0.0f;

    boost::qvm::mat<double, 4, 4> matrix;
    boost::qvm::set_identity(matrix);

    // rotate?

    // translate?

    // get components?
}

最终代码:

在这个问题之后代码最终看起来像这样:

#include <boost/qvm/mat.hpp>
#include <boost/qvm/vec.hpp>
#include <boost/qvm/mat_operations.hpp>
#include <boost/qvm/map_vec_mat.hpp>

// ...

boost::qvm::mat<double, 4, 4> matrix;
boost::qvm::set_identity(matrix);
boost::qvm::mat<double, 4, 4> rotation = boost::qvm::roty_mat<4>(deg2rad(heading)); 
boost::qvm::vec<double, 3> v{{0.0, -speed, 0.0}};
boost::qvm::mat<double, 4, 4> translation = boost::qvm::translation_mat(v);

最佳答案

Rotationtranslation 矩阵可以使用 boost 获得(示例):

boost::qvm::rotx_mat<4>(3.14159f); // rotation on x axis by PI radians

vec<float,3> v={0,0,7};
mat<float,4,4> tr=translation_mat(v); // translation by 7 units on z axis

通过将模型 View 矩阵与这些矩阵相乘,您将获得组合转换

然后,获取组件就是从您的模型 View 矩阵中读取所需的值(您的模型 View 可以是您已经用 boost::qvm::mat<double, 4, 4> matrix; 行声明的矩阵)。

关于c++ - 移植 OpenGL 矩阵运算以 boost QVM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41019513/

相关文章:

c++ - 具有潜在共享对象的循环转换

c++ - 如何使用嵌入式 C++ 检测 Windows Mobile 5 和 6 应用程序中的屏幕方向更改事件?

c++ - 在 OpenGL 中添加阴影

c++ - vector <字符串>或 vector < vector <字符>>?

c++ - 用于矩阵加法的 Cuda 程序

c++ - 这个有互锁吗? C++

c++ - 使用 istream::seekg 是不是太贵了?

opengl - 我可以使用 OpenGL 计算着色器来解析多重采样纹理吗?

OpenGL 半透明纹理超过其他纹理

c++ - 如何修复 LU 分解?