为什么这不起作用(glm :: make_mat4)?

时间:2018-04-23 21:59:07

标签: c++ opengl glm-math

我有这段代码:

glm::mat4 aLookAt(const glm::vec3& Eye, const glm::vec3& Center, const glm::vec3& Up)
{
    glm::vec3 Z = glm::normalize(Eye - Center);
    glm::vec3 Y = Up;
    glm::vec3 X = glm::normalize(glm::cross(Y, Z));
    Y = glm::normalize(glm::cross(Z, X));
    float Matrix[4][4];
    Matrix[0][0] = X.x;
    Matrix[1][0] = X.y;
    Matrix[2][0] = X.z;
    Matrix[3][0] = (float)glm::dot(-X, Eye);
    Matrix[0][1] = Y.x;
    Matrix[1][1] = Y.y;
    Matrix[2][1] = Y.z;
    Matrix[3][1] = (float)glm::dot(-Y, Eye);
    Matrix[0][2] = Z.x;
    Matrix[1][2] = Z.y;
    Matrix[2][2] = Z.z;
    Matrix[3][2] = (float)glm::dot(Z, Eye);
    Matrix[0][3] = 0;
    Matrix[1][3] = 0;
    Matrix[2][3] = 0;
    Matrix[3][3] = 1.0f;

    glm::mat4 theMatrix = glm::make_mat4(Matrix);
    return theMatrix;
}

但是每当我尝试编译它时,都会出现以下错误:

enter image description here

为什么呢?这次我实际上预计没有错误..

1 个答案:

答案 0 :(得分:3)

编译器告诉你原因。您正在将float[4][4]传递给期望pointer to float

的方法
template<typename T >
detail::tmat4x4< T >    make_mat4 (T const *const ptr)

这些类型不兼容。您需要float[16]代替:

float Matrix[16];
Matrix[0] = X.x;
Matrix[1] = X.y;
Matrix[2] = X.z;
Matrix[3] = (float)glm::dot(-X, Eye);
Matrix[4] = Y.x;
Matrix[5] = Y.y;
Matrix[6] = Y.z;
Matrix[7] = (float)glm::dot(-Y, Eye);
Matrix[8] = Z.x;
Matrix[9] = Z.y;
Matrix[10] = Z.z;
Matrix[11] = (float)glm::dot(Z, Eye);
Matrix[12] = 0;
Matrix[13] = 0;
Matrix[14] = 0;
Matrix[15] = 1.0f;