obb旋转计算

时间:2014-10-23 14:17:14

标签: c++ glm-math

我需要一些帮助whit obb rotation:

首先,我检查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)

1 个答案:

答案 0 :(得分:1)

  

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

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

仅通过模型矩阵将模型坐标转换为世界坐标。