C++ 移植OpenGL矩阵操作以增强QVM

C++ 移植OpenGL矩阵操作以增强QVM,c++,opengl,matrix,boost,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 yCo

我的应用程序中有一些使用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();
}
#包括
#包括
void foo()
{
自动航向=90.0;
自动速度=10.0;
自动xComponent=0.0f;
自动Y组件=0.0f;
glPushMatrix();
{
glLoadIdentity();
glRotatef(静态施法(航向),0,1,0);
glTranslatef(0,静态施法(-speed),0);
float modelviewMatrix[16];
glGetFloatv(GLU模型视图矩阵、模型视图矩阵);
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?
}
void foo()
{ 
自动航向=90.0;
自动速度=10.0;
自动xComponent=0.0f;
自动Y组件=0.0f;
boost::qvm::mat矩阵;
boost::qvm::set_标识(矩阵);
//轮换?
//翻译?
//获取组件?
}
最终代码:

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

#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);
#包括
#包括
#包括
#包括
// ...
boost::qvm::mat矩阵;
boost::qvm::set_标识(矩阵);
推进::qvm::垫旋转=推进::qvm::旋转垫(deg2rad(标题));
boost::qvm::vecv{{0.0,-速度,0.0};
boost::qvm::mat translation=boost::qvm::translation_mat(v);

旋转和平移矩阵可以使用boost获得(示例):

boost::qvm::rotx_mat(3.14159f);//以PI弧度在x轴上旋转
向量v={0,0,7};
mat tr=翻译_mat(v);//在z轴上平移7个单位
通过将modelview矩阵与这些矩阵相乘,您将获得组合变换


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

旋转平移矩阵可以使用boost获得(示例):

boost::qvm::rotx_mat(3.14159f);//以PI弧度在x轴上旋转
向量v={0,0,7};
mat tr=翻译_mat(v);//在z轴上平移7个单位
通过将modelview矩阵与这些矩阵相乘,您将获得组合变换


然后,获取组件就是从modelview矩阵中读取所需的值(modelview可以是您已经用行
boost::qvm::mat matrix;
声明的矩阵).

最好不要将类似
translation\u mat
roty\u mat
的中间结果放入
mat
对象中。在
auto const&
中捕获中间结果,或者只需将它们在线相乘,如下所示。这避免了创建任何临时对象

auto heading = 90.0;
auto speed = 10.0;
auto xComponent = 0.0f;
auto yComponent = 0.0f;
{
    using namespace boost::qvm;
    double const v[3] = {0,-speed,0};
    auto const & result = translation_mat(vref(v)) * roty_mat<4>(deg2rad(heading));
    xComponent = A02(result); //Not sure if this should be A20 instead
    yComponent = A00(result);
}
自动航向=90.0;
自动速度=10.0;
自动xComponent=0.0f;
自动Y组件=0.0f;
{
使用名称空间boost::qvm;
双常数v[3]={0,-速度,0};
自动常数和结果=平移矩阵(vref(v))*旋转矩阵(deg2rad(标题));
xComponent=A02(结果);//不确定是否应改为A20
Y成分=A00(结果);
}

最好不要将类似于
translation\u mat
roty\u mat
的中间结果放入
mat
对象中。在
auto const&
中捕获中间结果,或者只需将它们在线相乘,如下所示。这避免了创建任何临时对象

auto heading = 90.0;
auto speed = 10.0;
auto xComponent = 0.0f;
auto yComponent = 0.0f;
{
    using namespace boost::qvm;
    double const v[3] = {0,-speed,0};
    auto const & result = translation_mat(vref(v)) * roty_mat<4>(deg2rad(heading));
    xComponent = A02(result); //Not sure if this should be A20 instead
    yComponent = A00(result);
}
自动航向=90.0;
自动速度=10.0;
自动xComponent=0.0f;
自动Y组件=0.0f;
{
使用名称空间boost::qvm;
双常数v[3]={0,-速度,0};
自动常数和结果=平移矩阵(vref(v))*旋转矩阵(deg2rad(标题));
xComponent=A02(结果);//不确定是否应改为A20
Y成分=A00(结果);
}

boost::qvm是一项要求吗?您考虑过吗?提升只是一个要求,这样我们就不必再添加另一个外部依赖。我不知道glm,但因为它似乎只是标题,这可能是一种可能性。同时,我想看看qvm解决方案是否合理。boost::qvm是一项要求吗?您考虑过吗?提升只是一个要求,这样我们就不必再添加另一个外部依赖。我不知道glm,但因为它似乎只是标题,这可能是一种可能性。同时,我想看看qvm解决方案是否合理。