c++ - obb 旋转计算

标签 c++ glm-math

我需要一些关于 obb 旋转的帮助:

首先,我检查 3d 模型的每个顶点并获取最小值和最大值以找到 obb 最小/最大点。

void obb::checkVertex(vector3f& vertex)
{
    vLowerLeftFront.x = min(vLowerLeftFront.x, vertex.x);
    vLowerLeftFront.y = min(vLowerLeftFront.y, vertex.y);
    vLowerLeftFront.z = max(vLowerLeftFront.z, vertex.z);
    // Update upper-right-back corner of BB
    vUpperRightBack.x = max(vUpperRightBack.x, vertex.x);
    vUpperRightBack.y = max(vUpperRightBack.y, vertex.y);
    vUpperRightBack.z = min(vUpperRightBack.z, vertex.z);
}

然后,对于每一帧,更新 obb(八个角)乘以 modelViewProjection 矩阵。

void obb::update()
{
    //vector3f is a struct whit 3 floats (x,y,z);
    vector3f tmpV = vector3f(0.0f,0.0f,0.0f);
    float *floatMatrix = glm::value_ptr(matrix::getInstance()->getModelViewProjectionMatrix());


    tmpV.x = (floatMatrix[0] * vLowerLeftFront.x) +
               (floatMatrix[4] * vLowerLeftFront.y) +
               (floatMatrix[8] * vLowerLeftFront.z) +
               floatMatrix[12];
    tmpV.y = (floatMatrix[1] * vLowerLeftFront.x) +
               (floatMatrix[5] * vLowerLeftFront.y) +
               (floatMatrix[9] * vLowerLeftFront.z) +
               floatMatrix[13];
    tmpV.z = (floatMatrix[2] * vLowerLeftFront.x) +
               (floatMatrix[6] * vLowerLeftFront.y) +
               (floatMatrix[10] * vLowerLeftFront.z) +
               floatMatrix[14];

    vLowerLeftFront = tmpV;


    tmpV.x = (floatMatrix[0] * vUpperRightBack.x) +
               (floatMatrix[4] * vUpperRightBack.y) +
               (floatMatrix[8] * vUpperRightBack.z) +
               floatMatrix[12];
    tmpV.y = (floatMatrix[1] * vUpperRightBack.x) +
               (floatMatrix[5] * vUpperRightBack.y) +
               (floatMatrix[9] * vUpperRightBack.z) +
               floatMatrix[13];
    tmpV.z = (floatMatrix[2] * vUpperRightBack.x) +
               (floatMatrix[6] * vUpperRightBack.y) +
               (floatMatrix[10] * vUpperRightBack.z) +
               floatMatrix[14];

    vUpperRightBack = tmpV;


    tmpV.x = (floatMatrix[0] * vLowerRightFront.x) +
               (floatMatrix[4] * vLowerRightFront.y) +
               (floatMatrix[8] * vLowerRightFront.z) +
               floatMatrix[12];
    tmpV.y = (floatMatrix[1] * vLowerRightFront.x) +
               (floatMatrix[5] * vLowerRightFront.y) +
               (floatMatrix[9] * vLowerRightFront.z) +
               floatMatrix[13];
    tmpV.z = (floatMatrix[2] * vLowerRightFront.x) +
               (floatMatrix[6] * vLowerRightFront.y) +
               (floatMatrix[10] * vLowerRightFront.z) +
               floatMatrix[14];

    vLowerRightFront = tmpV;

    tmpV.x = (floatMatrix[0] * vLowerRightBack.x) +
               (floatMatrix[4] * vLowerRightBack.y) +
               (floatMatrix[8] * vLowerRightBack.z) +
               floatMatrix[12];
    tmpV.y = (floatMatrix[1] * vLowerRightBack.x) +
               (floatMatrix[5] * vLowerRightBack.y) +
               (floatMatrix[9] * vLowerRightBack.z) +
               floatMatrix[13];
    tmpV.z = (floatMatrix[2] * vLowerRightBack.x) +
               (floatMatrix[6] * vLowerRightBack.y) +
               (floatMatrix[10] * vLowerRightBack.z) +
               floatMatrix[14];

    vLowerRightBack = tmpV;

    tmpV.x = (floatMatrix[0] * vLowerLeftBack.x) +
               (floatMatrix[4] * vLowerLeftBack.y) +
               (floatMatrix[8] * vLowerLeftBack.z) +
               floatMatrix[12];
    tmpV.y = (floatMatrix[1] * vLowerLeftBack.x) +
               (floatMatrix[5] * vLowerLeftBack.y) +
               (floatMatrix[9] * vLowerLeftBack.z) +
               floatMatrix[13];
    tmpV.z = (floatMatrix[2] * vLowerLeftBack.x) +
               (floatMatrix[6] * vLowerLeftBack.y) +
               (floatMatrix[10] * vLowerLeftBack.z) +
               floatMatrix[14];

    vLowerLeftBack = tmpV;


    tmpV.x = (floatMatrix[0] * vUpperRightFront.x) +
               (floatMatrix[4] * vUpperRightFront.y) +
               (floatMatrix[8] * vUpperRightFront.z) +
               floatMatrix[12];
    tmpV.y = (floatMatrix[1] * vUpperRightFront.x) +
               (floatMatrix[5] * vUpperRightFront.y) +
               (floatMatrix[9] * vUpperRightFront.z) +
               floatMatrix[13];
    tmpV.z = (floatMatrix[2] * vUpperRightFront.x) +
               (floatMatrix[6] * vUpperRightFront.y) +
               (floatMatrix[10] * vUpperRightFront.z) +
               floatMatrix[14];

    vUpperRightFront = tmpV;


    tmpV.x = (floatMatrix[0] * vUpperLeftBack.x) +
               (floatMatrix[4] * vUpperLeftBack.y) +
               (floatMatrix[8] * vUpperLeftBack.z) +
               floatMatrix[12];
    tmpV.y = (floatMatrix[1] * vUpperLeftBack.x) +
               (floatMatrix[5] * vUpperLeftBack.y) +
               (floatMatrix[9] * vUpperLeftBack.z) +
               floatMatrix[13];
    tmpV.z = (floatMatrix[2] * vUpperLeftBack.x) +
               (floatMatrix[6] * vUpperLeftBack.y) +
               (floatMatrix[10] * vUpperLeftBack.z) +
               floatMatrix[14];

    vUpperLeftBack = tmpV;



    tmpV.x = (floatMatrix[0] * vUpperLeftFront.x) +
               (floatMatrix[4] * vUpperLeftFront.y) +
               (floatMatrix[8] * vUpperLeftFront.z) +
               floatMatrix[12];
    tmpV.y = (floatMatrix[1] * vUpperLeftFront.x) +
               (floatMatrix[5] * vUpperLeftFront.y) +
               (floatMatrix[9] * vUpperLeftFront.z) +
               floatMatrix[13];
    tmpV.z = (floatMatrix[2] * vUpperLeftFront.x) +
               (floatMatrix[6] * vUpperLeftFront.y) +
               (floatMatrix[10] * vUpperLeftFront.z) +
               floatMatrix[14];

    vUpperLeftFront = tmpV;

}

问题是旋转计算不正确..我不知道为什么..

我需要帮助来解决这个问题并正确计算 obb 旋转。

变量内容示例:

floatMatrix[0] = -7.91465e-008
floatMatrix[1] = -0.847687 
floatMatrix[2] = -0.936392 
floatMatrix[3] = -0.936329 
floatMatrix[4] = 1.81066 
floatMatrix[5] = -3.70536e-008 
floatMatrix[6] = -4.0931e-008 
floatMatrix[7] = -4.09282e-008
floatMatrix[8] = 0 
floatMatrix[9] = 2.2605
floatMatrix[10] = -0.351147
floatMatrix[11] = -0.351123
floatMatrix[12] = 1.0864
floatMatrix[13] = -7.68569
floatMatrix[14] = 45.4257
floatMatrix[15] = 45.6226

vLowerLeftFront: (-727.46, 84.64, 273.49)
vUpperRightBack: (-459.98, -19.68, 19.6)

最佳答案

Then, for each frame, update the obb (the eight corners) multiplying by the modelViewProjection matrix.

这种矩阵组合将顶点从模型空间转换到世界空间(这可能是您想要的),然后转换到相机空间,最后转换到裁剪空间,这根本不是很有用。

仅乘以模型矩阵将模型坐标转换为世界坐标。

关于c++ - obb 旋转计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26530219/

相关文章:

c++ - 在 switch 语句中初始化变量 (int32)

c++ - boost 线程链接在 Netbeans 7.1 调试/测试 session 中失败

c++ - 如何检测和隔离 C++ 访问冲突错误

c++ - openGL:使用 glm 的转换顺序是什么?

c++ - 使用 GLM + Answer 将屏幕转换为 3D 世界坐标后结果不佳

opengl - 如何按给定方向旋转矢量

c++ - qt creator可以使用环境变量吗?

c++ - qt 将对象传递给不同的类

python - 如何将 C++ 或 GLM 结构表示为 numpy 数组?

c++ - glm 在简单翻译中返回 nan